blob: 206ce30a595d1b4c05e57853a595342ff3dac074 [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.
321 */
322static void
323nfs4_free_slot(struct nfs4_slot_table *tbl, u8 free_slotid)
324{
325 int slotid = free_slotid;
326
327 spin_lock(&tbl->slot_tbl_lock);
328 /* clear used bit in bitmap */
329 __clear_bit(slotid, tbl->used_slots);
330
331 /* update highest_used_slotid when it is freed */
332 if (slotid == tbl->highest_used_slotid) {
333 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
334 if (slotid >= 0 && slotid < tbl->max_slots)
335 tbl->highest_used_slotid = slotid;
336 else
337 tbl->highest_used_slotid = -1;
338 }
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400339 spin_unlock(&tbl->slot_tbl_lock);
340 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
341 free_slotid, tbl->highest_used_slotid);
342}
343
Andy Adamson13615872009-04-01 09:22:17 -0400344void nfs41_sequence_free_slot(const struct nfs_client *clp,
345 struct nfs4_sequence_res *res)
346{
347 struct nfs4_slot_table *tbl;
348
349 if (!nfs4_has_session(clp)) {
350 dprintk("%s: No session\n", __func__);
351 return;
352 }
353 tbl = &clp->cl_session->fc_slot_table;
354 if (res->sr_slotid == NFS4_MAX_SLOT_TABLE) {
Andy Adamson13615872009-04-01 09:22:17 -0400355 /* just wake up the next guy waiting since
356 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500357 dprintk("%s: No slot\n", __func__);
358 } else {
359 nfs4_free_slot(tbl, res->sr_slotid);
360 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
Andy Adamson13615872009-04-01 09:22:17 -0400361 }
Andy Adamsonea028ac2009-12-04 15:55:38 -0500362
363 /* Signal state manager thread if session is drained */
364 if (test_bit(NFS4CLNT_SESSION_DRAINING, &clp->cl_state)) {
365 spin_lock(&tbl->slot_tbl_lock);
366 if (tbl->highest_used_slotid == -1) {
367 dprintk("%s COMPLETE: Session Drained\n", __func__);
368 complete(&clp->cl_session->complete);
369 }
370 spin_unlock(&tbl->slot_tbl_lock);
Andy Adamson691daf32009-12-04 15:55:39 -0500371 } else {
372 rpc_wake_up_next(&tbl->slot_tbl_waitq);
Andy Adamsonea028ac2009-12-04 15:55:38 -0500373 }
Andy Adamson13615872009-04-01 09:22:17 -0400374}
375
Andy Adamsonb0df8062009-04-01 09:22:18 -0400376static void nfs41_sequence_done(struct nfs_client *clp,
377 struct nfs4_sequence_res *res,
378 int rpc_status)
379{
380 unsigned long timestamp;
381 struct nfs4_slot_table *tbl;
382 struct nfs4_slot *slot;
383
384 /*
385 * sr_status remains 1 if an RPC level error occurred. The server
386 * may or may not have processed the sequence operation..
387 * Proceed as if the server received and processed the sequence
388 * operation.
389 */
390 if (res->sr_status == 1)
391 res->sr_status = NFS_OK;
392
393 /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
394 if (res->sr_slotid == NFS4_MAX_SLOT_TABLE)
395 goto out;
396
Andy Adamson691daf32009-12-04 15:55:39 -0500397 /* Check the SEQUENCE operation status */
Andy Adamsonb0df8062009-04-01 09:22:18 -0400398 if (res->sr_status == 0) {
Andy Adamson691daf32009-12-04 15:55:39 -0500399 tbl = &clp->cl_session->fc_slot_table;
400 slot = tbl->slots + res->sr_slotid;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400401 /* Update the slot's sequence and clientid lease timer */
402 ++slot->seq_nr;
403 timestamp = res->sr_renewal_time;
404 spin_lock(&clp->cl_lock);
405 if (time_before(clp->cl_last_renewal, timestamp))
406 clp->cl_last_renewal = timestamp;
407 spin_unlock(&clp->cl_lock);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500408 /* Check sequence flags */
409 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400410 return;
411 }
412out:
413 /* The session may be reset by one of the error handlers. */
414 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
415 nfs41_sequence_free_slot(clp, res);
416}
417
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400418/*
Benny Halevy510b8172009-04-01 09:22:14 -0400419 * nfs4_find_slot - efficiently look for a free slot
420 *
421 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
422 * If found, we mark the slot as used, update the highest_used_slotid,
423 * and respectively set up the sequence operation args.
424 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400425 *
426 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400427 */
428static u8
429nfs4_find_slot(struct nfs4_slot_table *tbl, struct rpc_task *task)
430{
431 int slotid;
432 u8 ret_id = NFS4_MAX_SLOT_TABLE;
433 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
434
Benny Halevy510b8172009-04-01 09:22:14 -0400435 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
436 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
437 tbl->max_slots);
438 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
439 if (slotid >= tbl->max_slots)
440 goto out;
441 __set_bit(slotid, tbl->used_slots);
442 if (slotid > tbl->highest_used_slotid)
443 tbl->highest_used_slotid = slotid;
444 ret_id = slotid;
445out:
446 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
447 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400448 return ret_id;
449}
450
Andy Adamsonce5039c2009-04-01 09:22:13 -0400451static int nfs41_setup_sequence(struct nfs4_session *session,
452 struct nfs4_sequence_args *args,
453 struct nfs4_sequence_res *res,
454 int cache_reply,
455 struct rpc_task *task)
456{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400457 struct nfs4_slot *slot;
458 struct nfs4_slot_table *tbl;
459 u8 slotid;
460
461 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400462 /* slot already allocated? */
463 if (res->sr_slotid != NFS4_MAX_SLOT_TABLE)
464 return 0;
465
466 memset(res, 0, sizeof(*res));
467 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400468 tbl = &session->fc_slot_table;
469
470 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson0b9e2d42009-12-04 16:02:14 -0500471 if (test_bit(NFS4CLNT_SESSION_DRAINING, &session->clp->cl_state)) {
Andy Adamsonea028ac2009-12-04 15:55:38 -0500472 /*
473 * The state manager will wait until the slot table is empty.
474 * Schedule the reset thread
475 */
476 dprintk("%s Schedule Session Reset\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500477 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
478 nfs4_schedule_state_manager(session->clp);
Andy Adamsonb069d942009-04-01 09:22:43 -0400479 spin_unlock(&tbl->slot_tbl_lock);
Andy Adamson05f0d232009-12-04 15:55:37 -0500480 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400481 }
482
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400483 slotid = nfs4_find_slot(tbl, task);
484 if (slotid == NFS4_MAX_SLOT_TABLE) {
485 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
486 spin_unlock(&tbl->slot_tbl_lock);
487 dprintk("<-- %s: no free slots\n", __func__);
488 return -EAGAIN;
489 }
490 spin_unlock(&tbl->slot_tbl_lock);
491
492 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400493 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400494 args->sa_slotid = slotid;
495 args->sa_cache_this = cache_reply;
496
497 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
498
Benny Halevy99fe60d2009-04-01 09:22:29 -0400499 res->sr_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400500 res->sr_slotid = slotid;
501 res->sr_renewal_time = jiffies;
502 /*
503 * sr_status is only set in decode_sequence, and so will remain
504 * set to 1 if an rpc level failure occurs.
505 */
506 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400507 return 0;
508}
509
510int nfs4_setup_sequence(struct nfs_client *clp,
511 struct nfs4_sequence_args *args,
512 struct nfs4_sequence_res *res,
513 int cache_reply,
514 struct rpc_task *task)
515{
516 int ret = 0;
517
518 dprintk("--> %s clp %p session %p sr_slotid %d\n",
519 __func__, clp, clp->cl_session, res->sr_slotid);
520
521 if (!nfs4_has_session(clp))
522 goto out;
523 ret = nfs41_setup_sequence(clp->cl_session, args, res, cache_reply,
524 task);
Andy Adamson2628edd2009-12-04 15:55:35 -0500525 if (ret && ret != -EAGAIN) {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400526 /* terminate rpc task */
527 task->tk_status = ret;
528 task->tk_action = NULL;
529 }
530out:
531 dprintk("<-- %s status=%d\n", __func__, ret);
532 return ret;
533}
534
535struct nfs41_call_sync_data {
536 struct nfs_client *clp;
537 struct nfs4_sequence_args *seq_args;
538 struct nfs4_sequence_res *seq_res;
539 int cache_reply;
540};
541
542static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
543{
544 struct nfs41_call_sync_data *data = calldata;
545
546 dprintk("--> %s data->clp->cl_session %p\n", __func__,
547 data->clp->cl_session);
548 if (nfs4_setup_sequence(data->clp, data->seq_args,
549 data->seq_res, data->cache_reply, task))
550 return;
551 rpc_call_start(task);
552}
553
Andy Adamson69ab40c2009-04-01 09:22:19 -0400554static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
555{
556 struct nfs41_call_sync_data *data = calldata;
557
558 nfs41_sequence_done(data->clp, data->seq_res, task->tk_status);
559 nfs41_sequence_free_slot(data->clp, data->seq_res);
560}
561
Andy Adamsonce5039c2009-04-01 09:22:13 -0400562struct rpc_call_ops nfs41_call_sync_ops = {
563 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400564 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400565};
566
567static int nfs4_call_sync_sequence(struct nfs_client *clp,
568 struct rpc_clnt *clnt,
569 struct rpc_message *msg,
570 struct nfs4_sequence_args *args,
571 struct nfs4_sequence_res *res,
572 int cache_reply)
573{
574 int ret;
575 struct rpc_task *task;
576 struct nfs41_call_sync_data data = {
577 .clp = clp,
578 .seq_args = args,
579 .seq_res = res,
580 .cache_reply = cache_reply,
581 };
582 struct rpc_task_setup task_setup = {
583 .rpc_client = clnt,
584 .rpc_message = msg,
585 .callback_ops = &nfs41_call_sync_ops,
586 .callback_data = &data
587 };
588
589 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
590 task = rpc_run_task(&task_setup);
591 if (IS_ERR(task))
592 ret = PTR_ERR(task);
593 else {
594 ret = task->tk_status;
595 rpc_put_task(task);
596 }
597 return ret;
598}
599
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400600int _nfs4_call_sync_session(struct nfs_server *server,
601 struct rpc_message *msg,
602 struct nfs4_sequence_args *args,
603 struct nfs4_sequence_res *res,
604 int cache_reply)
605{
Andy Adamsonce5039c2009-04-01 09:22:13 -0400606 return nfs4_call_sync_sequence(server->nfs_client, server->client,
607 msg, args, res, cache_reply);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400608}
609
610#endif /* CONFIG_NFS_V4_1 */
611
612int _nfs4_call_sync(struct nfs_server *server,
613 struct rpc_message *msg,
614 struct nfs4_sequence_args *args,
615 struct nfs4_sequence_res *res,
616 int cache_reply)
617{
Benny Halevyf3752972009-04-01 09:22:04 -0400618 args->sa_session = res->sr_session = NULL;
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400619 return rpc_call_sync(server->client, msg, 0);
620}
621
622#define nfs4_call_sync(server, msg, args, res, cache_reply) \
623 (server)->nfs_client->cl_call_sync((server), (msg), &(args)->seq_args, \
624 &(res)->seq_res, (cache_reply))
625
Andy Adamsonb0df8062009-04-01 09:22:18 -0400626static void nfs4_sequence_done(const struct nfs_server *server,
627 struct nfs4_sequence_res *res, int rpc_status)
628{
629#ifdef CONFIG_NFS_V4_1
630 if (nfs4_has_session(server->nfs_client))
631 nfs41_sequence_done(server->nfs_client, res, rpc_status);
632#endif /* CONFIG_NFS_V4_1 */
633}
634
Andy Adamsone608e792009-12-04 15:55:29 -0500635void nfs4_restart_rpc(struct rpc_task *task, const struct nfs_client *clp,
636 struct nfs4_sequence_res *res)
637{
638#ifdef CONFIG_NFS_V4_1
639 if (nfs4_has_session(clp)) {
640 nfs41_sequence_free_slot(clp, res);
641 rpc_restart_call_prepare(task);
642 return;
643 }
644#endif /* CONFIG_NFS_V4_1 */
645 rpc_restart_call(task);
646}
647
Andy Adamsonb0df8062009-04-01 09:22:18 -0400648/* no restart, therefore free slot here */
649static void nfs4_sequence_done_free_slot(const struct nfs_server *server,
650 struct nfs4_sequence_res *res,
651 int rpc_status)
652{
653 nfs4_sequence_done(server, res, rpc_status);
654 nfs4_sequence_free_slot(server->nfs_client, res);
655}
656
Trond Myklebust38478b22006-05-25 01:40:57 -0400657static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
Trond Myklebust38478b22006-05-25 01:40:57 -0400659 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Trond Myklebust38478b22006-05-25 01:40:57 -0400661 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400662 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
663 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400664 nfs_force_lookup_revalidate(dir);
Trond Myklebust40d24702007-10-08 09:24:22 -0400665 nfsi->change_attr = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400666 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667}
668
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100669struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400670 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100671 struct nfs_openargs o_arg;
672 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100673 struct nfs_open_confirmargs c_arg;
674 struct nfs_open_confirmres c_res;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100675 struct nfs_fattr f_attr;
676 struct nfs_fattr dir_attr;
Trond Myklebustad389da2007-06-05 12:30:00 -0400677 struct path path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100678 struct dentry *dir;
679 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400680 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100681 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100682 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400683 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100684 int rpc_status;
685 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100686};
687
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400688
689static void nfs4_init_opendata_res(struct nfs4_opendata *p)
690{
691 p->o_res.f_attr = &p->f_attr;
692 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400693 p->o_res.seqid = p->o_arg.seqid;
694 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400695 p->o_res.server = p->o_arg.server;
696 nfs_fattr_init(&p->f_attr);
697 nfs_fattr_init(&p->dir_attr);
Benny Halevy578e4582009-06-18 22:01:23 -0400698 p->o_res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400699}
700
Trond Myklebustad389da2007-06-05 12:30:00 -0400701static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500702 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100703 const struct iattr *attrs)
704{
Trond Myklebustad389da2007-06-05 12:30:00 -0400705 struct dentry *parent = dget_parent(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100706 struct inode *dir = parent->d_inode;
707 struct nfs_server *server = NFS_SERVER(dir);
708 struct nfs4_opendata *p;
709
710 p = kzalloc(sizeof(*p), GFP_KERNEL);
711 if (p == NULL)
712 goto err;
713 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
714 if (p->o_arg.seqid == NULL)
715 goto err_free;
Trond Myklebustad389da2007-06-05 12:30:00 -0400716 p->path.mnt = mntget(path->mnt);
717 p->path.dentry = dget(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100718 p->dir = parent;
719 p->owner = sp;
720 atomic_inc(&sp->so_count);
721 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500722 p->o_arg.open_flags = flags;
723 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400724 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400725 p->o_arg.id = sp->so_owner_id.id;
Trond Myklebustad389da2007-06-05 12:30:00 -0400726 p->o_arg.name = &p->path.dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100727 p->o_arg.server = server;
728 p->o_arg.bitmask = server->attr_bitmask;
729 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100730 if (flags & O_EXCL) {
Alexandros Batsakis4882ef72009-12-05 13:30:21 -0500731 if (nfs4_has_persistent_session(server->nfs_client)) {
732 /* GUARDED */
733 p->o_arg.u.attrs = &p->attrs;
734 memcpy(&p->attrs, attrs, sizeof(p->attrs));
735 } else { /* EXCLUSIVE4_1 */
736 u32 *s = (u32 *) p->o_arg.u.verifier.data;
737 s[0] = jiffies;
738 s[1] = current->pid;
739 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100740 } else if (flags & O_CREAT) {
741 p->o_arg.u.attrs = &p->attrs;
742 memcpy(&p->attrs, attrs, sizeof(p->attrs));
743 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100744 p->c_arg.fh = &p->o_res.fh;
745 p->c_arg.stateid = &p->o_res.stateid;
746 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400747 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400748 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100749 return p;
750err_free:
751 kfree(p);
752err:
753 dput(parent);
754 return NULL;
755}
756
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400757static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100758{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400759 struct nfs4_opendata *p = container_of(kref,
760 struct nfs4_opendata, kref);
761
762 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400763 if (p->state != NULL)
764 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400765 nfs4_put_state_owner(p->owner);
766 dput(p->dir);
Jan Blunck31f31db12008-05-02 13:42:45 -0700767 path_put(&p->path);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400768 kfree(p);
769}
770
771static void nfs4_opendata_put(struct nfs4_opendata *p)
772{
773 if (p != NULL)
774 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100775}
776
Trond Myklebust06f814a2006-01-03 09:55:07 +0100777static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
778{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100779 int ret;
780
Trond Myklebust06f814a2006-01-03 09:55:07 +0100781 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100782 return ret;
783}
784
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500785static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400786{
787 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500788
789 if (open_mode & O_EXCL)
790 goto out;
791 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400792 case FMODE_READ:
793 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400794 break;
795 case FMODE_WRITE:
796 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400797 break;
798 case FMODE_READ|FMODE_WRITE:
799 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0;
800 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500801out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400802 return ret;
803}
804
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500805static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400806{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500807 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400808 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500809 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400810 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500811 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400812 return 1;
813}
814
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500815static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100816{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500817 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100818 case FMODE_WRITE:
819 state->n_wronly++;
820 break;
821 case FMODE_READ:
822 state->n_rdonly++;
823 break;
824 case FMODE_READ|FMODE_WRITE:
825 state->n_rdwr++;
826 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500827 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100828}
829
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500830static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400831{
832 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
833 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
834 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500835 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400836 case FMODE_READ:
837 set_bit(NFS_O_RDONLY_STATE, &state->flags);
838 break;
839 case FMODE_WRITE:
840 set_bit(NFS_O_WRONLY_STATE, &state->flags);
841 break;
842 case FMODE_READ|FMODE_WRITE:
843 set_bit(NFS_O_RDWR_STATE, &state->flags);
844 }
845}
846
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500847static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400848{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400849 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500850 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400851 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400852}
853
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500854static 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 -0700855{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400856 /*
857 * Protect the call to nfs4_state_set_mode_locked and
858 * serialise the stateid update
859 */
860 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400861 if (deleg_stateid != NULL) {
862 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
863 set_bit(NFS_DELEGATED_STATE, &state->flags);
864 }
865 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500866 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400867 write_sequnlock(&state->seqlock);
868 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500869 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700870 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871}
872
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500873static 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 -0500874{
875 struct nfs_inode *nfsi = NFS_I(state->inode);
876 struct nfs_delegation *deleg_cur;
877 int ret = 0;
878
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500879 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500880
881 rcu_read_lock();
882 deleg_cur = rcu_dereference(nfsi->delegation);
883 if (deleg_cur == NULL)
884 goto no_delegation;
885
886 spin_lock(&deleg_cur->lock);
887 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500888 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500889 goto no_delegation_unlock;
890
891 if (delegation == NULL)
892 delegation = &deleg_cur->stateid;
893 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
894 goto no_delegation_unlock;
895
Trond Myklebustb7391f42008-12-23 15:21:52 -0500896 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500897 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500898 ret = 1;
899no_delegation_unlock:
900 spin_unlock(&deleg_cur->lock);
901no_delegation:
902 rcu_read_unlock();
903
904 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500905 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500906 ret = 1;
907 }
908
909 return ret;
910}
911
912
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500913static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400914{
915 struct nfs_delegation *delegation;
916
917 rcu_read_lock();
918 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500919 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -0400920 rcu_read_unlock();
921 return;
922 }
923 rcu_read_unlock();
924 nfs_inode_return_delegation(inode);
925}
926
Trond Myklebust6ee41262007-07-08 14:11:36 -0400927static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400928{
929 struct nfs4_state *state = opendata->state;
930 struct nfs_inode *nfsi = NFS_I(state->inode);
931 struct nfs_delegation *delegation;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500932 int open_mode = opendata->o_arg.open_flags & O_EXCL;
933 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400934 nfs4_stateid stateid;
935 int ret = -EAGAIN;
936
Trond Myklebustaac00a82007-07-05 19:02:21 -0400937 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500938 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400939 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500940 if (can_open_cached(state, fmode, open_mode)) {
941 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400942 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400943 goto out_return_state;
944 }
945 spin_unlock(&state->owner->so_lock);
946 }
Trond Myklebust34310432008-12-23 15:21:38 -0500947 rcu_read_lock();
948 delegation = rcu_dereference(nfsi->delegation);
949 if (delegation == NULL ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500950 !can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -0500951 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -0400952 break;
Trond Myklebust34310432008-12-23 15:21:38 -0500953 }
Trond Myklebustaac00a82007-07-05 19:02:21 -0400954 /* Save the delegation */
955 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
956 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -0400957 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400958 if (ret != 0)
959 goto out;
960 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -0500961
962 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500963 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -0500964 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400965 }
Trond Myklebustaac00a82007-07-05 19:02:21 -0400966out:
967 return ERR_PTR(ret);
968out_return_state:
969 atomic_inc(&state->count);
970 return state;
971}
972
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100973static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
974{
975 struct inode *inode;
976 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -0400977 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400978 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100979
Trond Myklebustaac00a82007-07-05 19:02:21 -0400980 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400981 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400982 goto out;
983 }
984
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400985 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100986 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400987 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100988 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400989 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -0500990 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400991 goto err;
992 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100993 state = nfs4_get_open_state(inode, data->owner);
994 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400995 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -0400996 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -0400997 int delegation_flags = 0;
998
Trond Myklebust003707c2007-07-05 18:07:55 -0400999 rcu_read_lock();
1000 delegation = rcu_dereference(NFS_I(inode)->delegation);
1001 if (delegation)
1002 delegation_flags = delegation->flags;
1003 rcu_read_unlock();
Trond Myklebust15c831b2008-12-23 15:21:39 -05001004 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001005 nfs_inode_set_delegation(state->inode,
1006 data->owner->so_cred,
1007 &data->o_res);
1008 else
1009 nfs_inode_reclaim_delegation(state->inode,
1010 data->owner->so_cred,
1011 &data->o_res);
1012 }
Trond Myklebust34310432008-12-23 15:21:38 -05001013
1014 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001015 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001016 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001017out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001018 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001019err_put_inode:
1020 iput(inode);
1021err:
1022 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001023}
1024
Trond Myklebust864472e2006-01-03 09:55:15 +01001025static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1026{
1027 struct nfs_inode *nfsi = NFS_I(state->inode);
1028 struct nfs_open_context *ctx;
1029
1030 spin_lock(&state->inode->i_lock);
1031 list_for_each_entry(ctx, &nfsi->open_files, list) {
1032 if (ctx->state != state)
1033 continue;
1034 get_nfs_open_context(ctx);
1035 spin_unlock(&state->inode->i_lock);
1036 return ctx;
1037 }
1038 spin_unlock(&state->inode->i_lock);
1039 return ERR_PTR(-ENOENT);
1040}
1041
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001042static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1043{
1044 struct nfs4_opendata *opendata;
1045
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001046 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001047 if (opendata == NULL)
1048 return ERR_PTR(-ENOMEM);
1049 opendata->state = state;
1050 atomic_inc(&state->count);
1051 return opendata;
1052}
1053
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001054static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001055{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001056 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001057 int ret;
1058
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001059 opendata->o_arg.open_flags = 0;
1060 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001061 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1062 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1063 nfs4_init_opendata_res(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001064 ret = _nfs4_proc_open(opendata);
1065 if (ret != 0)
1066 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001067 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001068 if (IS_ERR(newstate))
1069 return PTR_ERR(newstate);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001070 nfs4_close_state(&opendata->path, newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001071 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001072 return 0;
1073}
1074
1075static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1076{
Trond Myklebust864472e2006-01-03 09:55:15 +01001077 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001078 int ret;
1079
1080 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001081 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001082 smp_rmb();
1083 if (state->n_rdwr != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001084 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001085 if (ret != 0)
1086 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001087 if (newstate != state)
1088 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001089 }
1090 if (state->n_wronly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001091 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001092 if (ret != 0)
1093 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001094 if (newstate != state)
1095 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001096 }
1097 if (state->n_rdonly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001098 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001099 if (ret != 0)
1100 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001101 if (newstate != state)
1102 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001103 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001104 /*
1105 * We may have performed cached opens for all three recoveries.
1106 * Check if we need to update the current stateid.
1107 */
1108 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1109 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001110 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001111 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1112 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001113 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001114 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001115 return 0;
1116}
1117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118/*
1119 * OPEN_RECLAIM:
1120 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001122static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001124 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001125 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001126 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 int status;
1128
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001129 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1130 if (IS_ERR(opendata))
1131 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001132 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1133 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001134 rcu_read_lock();
1135 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001136 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001137 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001138 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001139 opendata->o_arg.u.delegation_type = delegation_type;
1140 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001141 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 return status;
1143}
1144
Trond Myklebust539cd032007-06-05 11:46:42 -04001145static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146{
1147 struct nfs_server *server = NFS_SERVER(state->inode);
1148 struct nfs4_exception exception = { };
1149 int err;
1150 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001151 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001152 if (err != -NFS4ERR_DELAY)
1153 break;
1154 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 } while (exception.retry);
1156 return err;
1157}
1158
Trond Myklebust864472e2006-01-03 09:55:15 +01001159static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1160{
1161 struct nfs_open_context *ctx;
1162 int ret;
1163
1164 ctx = nfs4_state_find_open_context(state);
1165 if (IS_ERR(ctx))
1166 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001167 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001168 put_nfs_open_context(ctx);
1169 return ret;
1170}
1171
Trond Myklebust13437e12007-07-06 15:10:43 -04001172static 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 -07001173{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001174 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001175 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001177 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1178 if (IS_ERR(opendata))
1179 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001180 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -04001181 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001182 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +01001183 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001184 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001185 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186}
1187
Trond Myklebust13437e12007-07-06 15:10:43 -04001188int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189{
1190 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001191 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 int err;
1193 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001194 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 switch (err) {
1196 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001197 case -ENOENT:
1198 case -ESTALE:
1199 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 case -NFS4ERR_STALE_CLIENTID:
1201 case -NFS4ERR_STALE_STATEID:
1202 case -NFS4ERR_EXPIRED:
1203 /* Don't recall a delegation if it was lost */
David Howells7539bba2006-08-22 20:06:09 -04001204 nfs4_schedule_state_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001205 goto out;
1206 case -ERESTARTSYS:
1207 /*
1208 * The show must go on: exit, but mark the
1209 * stateid as needing recovery.
1210 */
1211 case -NFS4ERR_ADMIN_REVOKED:
1212 case -NFS4ERR_BAD_STATEID:
1213 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
1214 case -ENOMEM:
1215 err = 0;
1216 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 }
1218 err = nfs4_handle_exception(server, err, &exception);
1219 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001220out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 return err;
1222}
1223
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001224static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1225{
1226 struct nfs4_opendata *data = calldata;
1227
1228 data->rpc_status = task->tk_status;
1229 if (RPC_ASSASSINATED(task))
1230 return;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001231 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001232 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1233 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -05001234 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001235 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001236 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001237 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001238}
1239
1240static void nfs4_open_confirm_release(void *calldata)
1241{
1242 struct nfs4_opendata *data = calldata;
1243 struct nfs4_state *state = NULL;
1244
1245 /* If this request hasn't been cancelled, do nothing */
1246 if (data->cancelled == 0)
1247 goto out_free;
1248 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001249 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001250 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001251 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001252 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001253 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001254out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001255 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001256}
1257
1258static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001259 .rpc_call_done = nfs4_open_confirm_done,
1260 .rpc_release = nfs4_open_confirm_release,
1261};
1262
1263/*
1264 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1265 */
1266static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1267{
1268 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1269 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001270 struct rpc_message msg = {
1271 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1272 .rpc_argp = &data->c_arg,
1273 .rpc_resp = &data->c_res,
1274 .rpc_cred = data->owner->so_cred,
1275 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001276 struct rpc_task_setup task_setup_data = {
1277 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001278 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001279 .callback_ops = &nfs4_open_confirm_ops,
1280 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001281 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001282 .flags = RPC_TASK_ASYNC,
1283 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 int status;
1285
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001286 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001287 data->rpc_done = 0;
1288 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001289 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001290 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001291 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001292 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001293 status = nfs4_wait_for_completion_rpc_task(task);
1294 if (status != 0) {
1295 data->cancelled = 1;
1296 smp_wmb();
1297 } else
1298 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001299 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 return status;
1301}
1302
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001303static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001305 struct nfs4_opendata *data = calldata;
1306 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001307
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001308 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1309 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001310 /*
1311 * Check if we still need to send an OPEN call, or if we can use
1312 * a delegation instead.
1313 */
1314 if (data->state != NULL) {
1315 struct nfs_delegation *delegation;
1316
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001317 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001318 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001319 rcu_read_lock();
1320 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1321 if (delegation != NULL &&
Trond Myklebust15c831b2008-12-23 15:21:39 -05001322 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001323 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001324 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001325 }
1326 rcu_read_unlock();
1327 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001328 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001329 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001330 data->o_arg.clientid = sp->so_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001331 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001332 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001333 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1334 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001335 data->timestamp = jiffies;
Andy Adamsond8985282009-04-01 09:22:21 -04001336 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
1337 &data->o_arg.seq_args,
1338 &data->o_res.seq_res, 1, task))
1339 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001340 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001341 return;
1342out_no_action:
1343 task->tk_action = NULL;
1344
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001345}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001347static void nfs4_open_done(struct rpc_task *task, void *calldata)
1348{
1349 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001351 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001352
1353 nfs4_sequence_done_free_slot(data->o_arg.server, &data->o_res.seq_res,
1354 task->tk_status);
1355
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001356 if (RPC_ASSASSINATED(task))
1357 return;
1358 if (task->tk_status == 0) {
1359 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001360 case S_IFREG:
1361 break;
1362 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001363 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001364 break;
1365 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001366 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001367 break;
1368 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001369 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001370 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001371 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001372 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1373 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001374 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001375 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001376}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001377
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001378static void nfs4_open_release(void *calldata)
1379{
1380 struct nfs4_opendata *data = calldata;
1381 struct nfs4_state *state = NULL;
1382
1383 /* If this request hasn't been cancelled, do nothing */
1384 if (data->cancelled == 0)
1385 goto out_free;
1386 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001387 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001388 goto out_free;
1389 /* In case we need an open_confirm, no cleanup! */
1390 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1391 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001392 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001393 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001394 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001395out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001396 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001397}
1398
1399static const struct rpc_call_ops nfs4_open_ops = {
1400 .rpc_call_prepare = nfs4_open_prepare,
1401 .rpc_call_done = nfs4_open_done,
1402 .rpc_release = nfs4_open_release,
1403};
1404
1405/*
1406 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1407 */
1408static int _nfs4_proc_open(struct nfs4_opendata *data)
1409{
1410 struct inode *dir = data->dir->d_inode;
1411 struct nfs_server *server = NFS_SERVER(dir);
1412 struct nfs_openargs *o_arg = &data->o_arg;
1413 struct nfs_openres *o_res = &data->o_res;
1414 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001415 struct rpc_message msg = {
1416 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1417 .rpc_argp = o_arg,
1418 .rpc_resp = o_res,
1419 .rpc_cred = data->owner->so_cred,
1420 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001421 struct rpc_task_setup task_setup_data = {
1422 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001423 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001424 .callback_ops = &nfs4_open_ops,
1425 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001426 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001427 .flags = RPC_TASK_ASYNC,
1428 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001429 int status;
1430
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001431 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001432 data->rpc_done = 0;
1433 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001434 data->cancelled = 0;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001435 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001436 if (IS_ERR(task))
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001437 return PTR_ERR(task);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001438 status = nfs4_wait_for_completion_rpc_task(task);
1439 if (status != 0) {
1440 data->cancelled = 1;
1441 smp_wmb();
1442 } else
1443 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001444 rpc_put_task(task);
Trond Myklebust3e309912007-07-07 13:19:59 -04001445 if (status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001446 return status;
1447
Trond Myklebust99367812007-07-17 21:52:41 -04001448 if (o_res->fh.size == 0)
1449 _nfs4_proc_lookup(dir, o_arg->name, &o_res->fh, o_res->f_attr);
1450
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001451 if (o_arg->open_flags & O_CREAT) {
1452 update_changeattr(dir, &o_res->cinfo);
1453 nfs_post_op_update_inode(dir, o_res->dir_attr);
1454 } else
1455 nfs_refresh_inode(dir, o_res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001457 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001459 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 }
1461 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001462 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001463 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464}
1465
Trond Myklebust10afec902007-05-14 17:16:04 -04001466static int nfs4_recover_expired_lease(struct nfs_server *server)
Trond Myklebust58d97142006-01-03 09:55:24 +01001467{
David Howells7539bba2006-08-22 20:06:09 -04001468 struct nfs_client *clp = server->nfs_client;
Trond Myklebusta78cb572009-08-09 15:06:19 -04001469 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001470 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001471
Trond Myklebusta78cb572009-08-09 15:06:19 -04001472 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001473 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001474 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001475 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001476 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1477 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001478 break;
Trond Myklebust58d97142006-01-03 09:55:24 +01001479 nfs4_schedule_state_recovery(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001480 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001481 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001482 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001483}
1484
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485/*
1486 * OPEN_EXPIRED:
1487 * reclaim state on the server after a network partition.
1488 * Assumes caller holds the appropriate lock
1489 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001490static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001492 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001493 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001495 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1496 if (IS_ERR(opendata))
1497 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001498 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001499 if (ret == -ESTALE)
Trond Myklebust539cd032007-06-05 11:46:42 -04001500 d_drop(ctx->path.dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001501 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001502 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503}
1504
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001505static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001506{
Trond Myklebust539cd032007-06-05 11:46:42 -04001507 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001508 struct nfs4_exception exception = { };
1509 int err;
1510
1511 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001512 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001513 switch (err) {
1514 default:
1515 goto out;
1516 case -NFS4ERR_GRACE:
1517 case -NFS4ERR_DELAY:
1518 nfs4_handle_exception(server, err, &exception);
1519 err = 0;
1520 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001521 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001522out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001523 return err;
1524}
1525
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1527{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001529 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
Trond Myklebust864472e2006-01-03 09:55:15 +01001531 ctx = nfs4_state_find_open_context(state);
1532 if (IS_ERR(ctx))
1533 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001534 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001535 put_nfs_open_context(ctx);
1536 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537}
1538
1539/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001540 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1541 * fields corresponding to attributes that were used to store the verifier.
1542 * Make sure we clobber those fields in the later setattr call
1543 */
1544static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1545{
1546 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1547 !(sattr->ia_valid & ATTR_ATIME_SET))
1548 sattr->ia_valid |= ATTR_ATIME;
1549
1550 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1551 !(sattr->ia_valid & ATTR_MTIME_SET))
1552 sattr->ia_valid |= ATTR_MTIME;
1553}
1554
1555/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001556 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001558static 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 -07001559{
1560 struct nfs4_state_owner *sp;
1561 struct nfs4_state *state = NULL;
1562 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001563 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001564 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
1566 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 status = -ENOMEM;
1568 if (!(sp = nfs4_get_state_owner(server, cred))) {
1569 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1570 goto out_err;
1571 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001572 status = nfs4_recover_expired_lease(server);
1573 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001574 goto err_put_state_owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001575 if (path->dentry->d_inode != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001576 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001577 status = -ENOMEM;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001578 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001579 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001580 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
Trond Myklebustaac00a82007-07-05 19:02:21 -04001582 if (path->dentry->d_inode != NULL)
1583 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1584
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001585 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001587 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001589 if (opendata->o_arg.open_flags & O_EXCL)
1590 nfs4_exclusive_attrset(opendata, sattr);
1591
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001592 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001593 status = PTR_ERR(state);
1594 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001595 goto err_opendata_put;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001596 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 *res = state;
1599 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001600err_opendata_put:
1601 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001602err_put_state_owner:
1603 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 *res = NULL;
1606 return status;
1607}
1608
1609
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001610static 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 -07001611{
1612 struct nfs4_exception exception = { };
1613 struct nfs4_state *res;
1614 int status;
1615
1616 do {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001617 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 if (status == 0)
1619 break;
1620 /* NOTE: BAD_SEQID means the server and client disagree about the
1621 * book-keeping w.r.t. state-changing operations
1622 * (OPEN/CLOSE/LOCK/LOCKU...)
1623 * It is actually a sign of a bug on the client or on the server.
1624 *
1625 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001626 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 * have unhashed the old state_owner for us, and that we can
1628 * therefore safely retry using a new one. We should still warn
1629 * the user though...
1630 */
1631 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001632 printk(KERN_WARNING "NFS: v4 server %s "
1633 " returned a bad sequence-id error!\n",
1634 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 exception.retry = 1;
1636 continue;
1637 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001638 /*
1639 * BAD_STATEID on OPEN means that the server cancelled our
1640 * state before it received the OPEN_CONFIRM.
1641 * Recover by retrying the request as per the discussion
1642 * on Page 181 of RFC3530.
1643 */
1644 if (status == -NFS4ERR_BAD_STATEID) {
1645 exception.retry = 1;
1646 continue;
1647 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001648 if (status == -EAGAIN) {
1649 /* We must have found a delegation */
1650 exception.retry = 1;
1651 continue;
1652 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1654 status, &exception));
1655 } while (exception.retry);
1656 return res;
1657}
1658
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001659static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1660 struct nfs_fattr *fattr, struct iattr *sattr,
1661 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001663 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001665 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 .iap = sattr,
1667 .server = server,
1668 .bitmask = server->attr_bitmask,
1669 };
1670 struct nfs_setattrres res = {
1671 .fattr = fattr,
1672 .server = server,
1673 };
1674 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001675 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1676 .rpc_argp = &arg,
1677 .rpc_resp = &res,
1678 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001680 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001681 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
Trond Myklebust0e574af2005-10-27 22:12:38 -04001683 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001685 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1686 /* Use that stateid */
1687 } else if (state != NULL) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001688 nfs4_copy_stateid(&arg.stateid, state, current->files);
1689 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1691
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001692 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001693 if (status == 0 && state != NULL)
1694 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001695 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696}
1697
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001698static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1699 struct nfs_fattr *fattr, struct iattr *sattr,
1700 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001702 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 struct nfs4_exception exception = { };
1704 int err;
1705 do {
1706 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001707 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 &exception);
1709 } while (exception.retry);
1710 return err;
1711}
1712
1713struct nfs4_closedata {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001714 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 struct inode *inode;
1716 struct nfs4_state *state;
1717 struct nfs_closeargs arg;
1718 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001719 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001720 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721};
1722
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001723static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001724{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001725 struct nfs4_closedata *calldata = data;
1726 struct nfs4_state_owner *sp = calldata->state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -07001727
1728 nfs4_put_open_state(calldata->state);
1729 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001730 nfs4_put_state_owner(sp);
Jan Blunck31f31db12008-05-02 13:42:45 -07001731 path_put(&calldata->path);
Trond Myklebust95121352005-10-18 14:20:12 -07001732 kfree(calldata);
1733}
1734
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001735static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001737 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 struct nfs_server *server = NFS_SERVER(calldata->inode);
1740
Andy Adamson19ddab02009-04-01 09:22:20 -04001741 nfs4_sequence_done(server, &calldata->res.seq_res, task->tk_status);
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001742 if (RPC_ASSASSINATED(task))
1743 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 /* hmm. we are done with the inode, and in the process of freeing
1745 * the state_owner. we keep this around to process errors
1746 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 switch (task->tk_status) {
1748 case 0:
Trond Myklebust45328c32007-07-26 17:47:34 -04001749 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001750 renew_lease(server, calldata->timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 break;
1752 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001753 case -NFS4ERR_OLD_STATEID:
1754 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001756 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001757 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001759 if (nfs4_async_handle_error(task, server, state) == -EAGAIN) {
Andy Adamsone608e792009-12-04 15:55:29 -05001760 nfs4_restart_rpc(task, server->nfs_client,
1761 &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 return;
1763 }
1764 }
Andy Adamson19ddab02009-04-01 09:22:20 -04001765 nfs4_sequence_free_slot(server->nfs_client, &calldata->res.seq_res);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001766 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767}
1768
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001769static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001771 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001772 struct nfs4_state *state = calldata->state;
Trond Myklebust003707c2007-07-05 18:07:55 -04001773 int clear_rd, clear_wr, clear_rdwr;
Trond Myklebust95121352005-10-18 14:20:12 -07001774
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001775 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001776 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001777
Trond Myklebust003707c2007-07-05 18:07:55 -04001778 clear_rd = clear_wr = clear_rdwr = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001779 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001780 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001781 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001782 if (state->n_rdonly == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001783 clear_rd |= test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1784 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1785 }
1786 if (state->n_wronly == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001787 clear_wr |= test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1788 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1789 }
Trond Myklebuste7616922006-01-03 09:55:13 +01001790 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001791 spin_unlock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001792 if (!clear_rd && !clear_wr && !clear_rdwr) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001793 /* Note: exit _without_ calling nfs4_close_done */
1794 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001795 return;
1796 }
Trond Myklebust516a6af2005-10-27 22:12:41 -04001797 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust45328c32007-07-26 17:47:34 -04001798 if (test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001799 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001800 calldata->arg.fmode = FMODE_READ;
Trond Myklebust45328c32007-07-26 17:47:34 -04001801 } else if (test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001802 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001803 calldata->arg.fmode = FMODE_WRITE;
Trond Myklebust45328c32007-07-26 17:47:34 -04001804 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001805 calldata->timestamp = jiffies;
Andy Adamson19ddab02009-04-01 09:22:20 -04001806 if (nfs4_setup_sequence((NFS_SERVER(calldata->inode))->nfs_client,
1807 &calldata->arg.seq_args, &calldata->res.seq_res,
1808 1, task))
1809 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001810 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811}
1812
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001813static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001814 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001815 .rpc_call_done = nfs4_close_done,
1816 .rpc_release = nfs4_free_closedata,
1817};
1818
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819/*
1820 * It is possible for data to be read/written from a mem-mapped file
1821 * after the sys_close call (which hits the vfs layer as a flush).
1822 * This means that we can't safely call nfsv4 close on a file until
1823 * the inode is cleared. This in turn means that we are not good
1824 * NFSv4 citizens - we do not indicate to the server to update the file's
1825 * share state even when we are done with one of the three share
1826 * stateid's in the inode.
1827 *
1828 * NOTE: Caller must be holding the sp->so_owner semaphore!
1829 */
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001830int nfs4_do_close(struct path *path, struct nfs4_state *state, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001832 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04001834 struct nfs4_state_owner *sp = state->owner;
1835 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001836 struct rpc_message msg = {
1837 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1838 .rpc_cred = state->owner->so_cred,
1839 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001840 struct rpc_task_setup task_setup_data = {
1841 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001842 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001843 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05001844 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001845 .flags = RPC_TASK_ASYNC,
1846 };
Trond Myklebust95121352005-10-18 14:20:12 -07001847 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
Andy Adamson19ddab02009-04-01 09:22:20 -04001849 calldata = kzalloc(sizeof(*calldata), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07001851 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001852 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001854 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04001855 calldata->arg.stateid = &state->open_stateid;
Benny Halevy34dc1ad2009-04-01 09:22:52 -04001856 if (nfs4_has_session(server->nfs_client))
1857 memset(calldata->arg.stateid->data, 0, 4); /* clear seqid */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 /* Serialization for the sequence id */
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001859 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001860 if (calldata->arg.seqid == NULL)
1861 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001862 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04001863 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001864 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04001865 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001866 calldata->res.server = server;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04001867 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001868 calldata->path.mnt = mntget(path->mnt);
1869 calldata->path.dentry = dget(path->dentry);
Trond Myklebust95121352005-10-18 14:20:12 -07001870
Trond Myklebust5138fde2007-07-14 15:40:01 -04001871 msg.rpc_argp = &calldata->arg,
1872 msg.rpc_resp = &calldata->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001873 task_setup_data.callback_data = calldata;
1874 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001875 if (IS_ERR(task))
1876 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001877 status = 0;
1878 if (wait)
1879 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001880 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001881 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07001882out_free_calldata:
1883 kfree(calldata);
1884out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04001885 nfs4_put_open_state(state);
1886 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07001887 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888}
1889
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001890static 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 -07001891{
1892 struct file *filp;
Trond Myklebust1b45c462007-07-03 13:04:56 -04001893 int ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001894
Trond Myklebust1b45c462007-07-03 13:04:56 -04001895 /* If the open_intent is for execute, we have an extra check to make */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001896 if (fmode & FMODE_EXEC) {
Trond Myklebustaf22f942007-08-10 17:45:10 -04001897 ret = nfs_may_open(state->inode,
Trond Myklebust1b45c462007-07-03 13:04:56 -04001898 state->owner->so_cred,
1899 nd->intent.open.flags);
1900 if (ret < 0)
1901 goto out_close;
1902 }
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001903 filp = lookup_instantiate_filp(nd, path->dentry, NULL);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001904 if (!IS_ERR(filp)) {
1905 struct nfs_open_context *ctx;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04001906 ctx = nfs_file_open_context(filp);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001907 ctx->state = state;
Trond Myklebust95cf9592006-04-18 13:14:06 -04001908 return 0;
1909 }
Trond Myklebust1b45c462007-07-03 13:04:56 -04001910 ret = PTR_ERR(filp);
1911out_close:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001912 nfs4_close_sync(path, state, fmode & (FMODE_READ|FMODE_WRITE));
Trond Myklebust1b45c462007-07-03 13:04:56 -04001913 return ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001914}
1915
1916struct dentry *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1918{
Trond Myklebustad389da2007-06-05 12:30:00 -04001919 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08001920 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001921 .dentry = dentry,
1922 };
Jan Blunck4ac91372008-02-14 19:34:32 -08001923 struct dentry *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 struct iattr attr;
1925 struct rpc_cred *cred;
1926 struct nfs4_state *state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001927 struct dentry *res;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001928 fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
1930 if (nd->flags & LOOKUP_CREATE) {
1931 attr.ia_mode = nd->intent.open.create_mode;
1932 attr.ia_valid = ATTR_MODE;
1933 if (!IS_POSIXACL(dir))
Al Viroce3b0f82009-03-29 19:08:22 -04001934 attr.ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 } else {
1936 attr.ia_valid = 0;
1937 BUG_ON(nd->intent.open.flags & O_CREAT);
1938 }
1939
Trond Myklebust98a8e322008-03-12 12:25:28 -04001940 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 if (IS_ERR(cred))
Trond Myklebust02a913a2005-10-18 14:20:17 -07001942 return (struct dentry *)cred;
Trond Myklebust565277f2007-10-15 18:17:53 -04001943 parent = dentry->d_parent;
1944 /* Protect against concurrent sillydeletes */
1945 nfs_block_sillyrename(parent);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001946 state = nfs4_do_open(dir, &path, fmode, nd->intent.open.flags, &attr, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001948 if (IS_ERR(state)) {
Trond Myklebustd75340c2007-10-01 21:42:01 -04001949 if (PTR_ERR(state) == -ENOENT) {
Trond Myklebust02a913a2005-10-18 14:20:17 -07001950 d_add(dentry, NULL);
Trond Myklebustd75340c2007-10-01 21:42:01 -04001951 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1952 }
Trond Myklebust565277f2007-10-15 18:17:53 -04001953 nfs_unblock_sillyrename(parent);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001954 return (struct dentry *)state;
1955 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001956 res = d_add_unique(dentry, igrab(state->inode));
Trond Myklebust02a913a2005-10-18 14:20:17 -07001957 if (res != NULL)
Trond Myklebustdeee9362007-08-27 11:33:00 -04001958 path.dentry = res;
Trond Myklebustd75340c2007-10-01 21:42:01 -04001959 nfs_set_verifier(path.dentry, nfs_save_change_attribute(dir));
Trond Myklebust565277f2007-10-15 18:17:53 -04001960 nfs_unblock_sillyrename(parent);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001961 nfs4_intent_set_file(nd, &path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001962 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963}
1964
1965int
Trond Myklebust02a913a2005-10-18 14:20:17 -07001966nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967{
Trond Myklebustad389da2007-06-05 12:30:00 -04001968 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08001969 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001970 .dentry = dentry,
1971 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 struct rpc_cred *cred;
1973 struct nfs4_state *state;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001974 fmode_t fmode = openflags & (FMODE_READ | FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
Trond Myklebust98a8e322008-03-12 12:25:28 -04001976 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 if (IS_ERR(cred))
1978 return PTR_ERR(cred);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001979 state = nfs4_do_open(dir, &path, fmode, openflags, NULL, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001981 if (IS_ERR(state)) {
1982 switch (PTR_ERR(state)) {
1983 case -EPERM:
1984 case -EACCES:
1985 case -EDQUOT:
1986 case -ENOSPC:
1987 case -EROFS:
1988 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
1989 return 1;
Chuck Leverb87c0ad2006-10-19 23:28:42 -07001990 default:
1991 goto out_drop;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001992 }
Trond Myklebust02a913a2005-10-18 14:20:17 -07001993 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001994 if (state->inode == dentry->d_inode) {
Trond Myklebustd75340c2007-10-01 21:42:01 -04001995 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001996 nfs4_intent_set_file(nd, &path, state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 return 1;
1998 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001999 nfs4_close_sync(&path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002000out_drop:
2001 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 return 0;
2003}
2004
Trond Myklebust1185a552009-12-03 15:54:02 -05002005static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002006{
2007 if (ctx->state == NULL)
2008 return;
2009 if (is_sync)
2010 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
2011 else
2012 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
2013}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
2015static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2016{
Benny Halevy43652ad2009-04-01 09:21:54 -04002017 struct nfs4_server_caps_arg args = {
2018 .fhandle = fhandle,
2019 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 struct nfs4_server_caps_res res = {};
2021 struct rpc_message msg = {
2022 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002023 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 .rpc_resp = &res,
2025 };
2026 int status;
2027
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002028 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 if (status == 0) {
2030 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04002031 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2032 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2033 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2034 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2035 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2037 server->caps |= NFS_CAP_ACLS;
2038 if (res.has_links != 0)
2039 server->caps |= NFS_CAP_HARDLINKS;
2040 if (res.has_symlinks != 0)
2041 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04002042 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2043 server->caps |= NFS_CAP_FILEID;
2044 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2045 server->caps |= NFS_CAP_MODE;
2046 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2047 server->caps |= NFS_CAP_NLINK;
2048 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2049 server->caps |= NFS_CAP_OWNER;
2050 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2051 server->caps |= NFS_CAP_OWNER_GROUP;
2052 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2053 server->caps |= NFS_CAP_ATIME;
2054 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2055 server->caps |= NFS_CAP_CTIME;
2056 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2057 server->caps |= NFS_CAP_MTIME;
2058
Trond Myklebusta65318b2009-03-11 14:10:28 -04002059 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2060 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2061 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 server->acl_bitmask = res.acl_bitmask;
2063 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002064
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 return status;
2066}
2067
Trond Myklebust55a97592006-06-09 09:34:19 -04002068int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069{
2070 struct nfs4_exception exception = { };
2071 int err;
2072 do {
2073 err = nfs4_handle_exception(server,
2074 _nfs4_server_capabilities(server, fhandle),
2075 &exception);
2076 } while (exception.retry);
2077 return err;
2078}
2079
2080static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2081 struct nfs_fsinfo *info)
2082{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 struct nfs4_lookup_root_arg args = {
2084 .bitmask = nfs4_fattr_bitmap,
2085 };
2086 struct nfs4_lookup_res res = {
2087 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002088 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 .fh = fhandle,
2090 };
2091 struct rpc_message msg = {
2092 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2093 .rpc_argp = &args,
2094 .rpc_resp = &res,
2095 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002096
Trond Myklebust0e574af2005-10-27 22:12:38 -04002097 nfs_fattr_init(info->fattr);
Trond Myklebustfccba802009-07-21 16:48:07 -04002098 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099}
2100
2101static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2102 struct nfs_fsinfo *info)
2103{
2104 struct nfs4_exception exception = { };
2105 int err;
2106 do {
2107 err = nfs4_handle_exception(server,
2108 _nfs4_lookup_root(server, fhandle, info),
2109 &exception);
2110 } while (exception.retry);
2111 return err;
2112}
2113
David Howells54ceac42006-08-22 20:06:13 -04002114/*
2115 * get the file handle for the "/" directory on the server
2116 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002118 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 int status;
2121
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 status = nfs4_lookup_root(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 if (status == 0)
2124 status = nfs4_server_capabilities(server, fhandle);
2125 if (status == 0)
2126 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002127 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128}
2129
Manoj Naik6b97fd32006-06-09 09:34:29 -04002130/*
2131 * Get locations and (maybe) other attributes of a referral.
2132 * Note that we'll actually follow the referral later when
2133 * we detect fsid mismatch in inode revalidation
2134 */
Trond Myklebust56659e92007-07-17 21:52:39 -04002135static 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 -04002136{
2137 int status = -ENOMEM;
2138 struct page *page = NULL;
2139 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002140
2141 page = alloc_page(GFP_KERNEL);
2142 if (page == NULL)
2143 goto out;
2144 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2145 if (locations == NULL)
2146 goto out;
2147
Trond Myklebustc228fd32007-01-13 02:28:11 -05002148 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002149 if (status != 0)
2150 goto out;
2151 /* Make sure server returned a different fsid for the referral */
2152 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -07002153 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 -04002154 status = -EIO;
2155 goto out;
2156 }
2157
2158 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2159 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2160 if (!fattr->mode)
2161 fattr->mode = S_IFDIR;
2162 memset(fhandle, 0, sizeof(struct nfs_fh));
2163out:
2164 if (page)
2165 __free_page(page);
2166 if (locations)
2167 kfree(locations);
2168 return status;
2169}
2170
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2172{
2173 struct nfs4_getattr_arg args = {
2174 .fh = fhandle,
2175 .bitmask = server->attr_bitmask,
2176 };
2177 struct nfs4_getattr_res res = {
2178 .fattr = fattr,
2179 .server = server,
2180 };
2181 struct rpc_message msg = {
2182 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2183 .rpc_argp = &args,
2184 .rpc_resp = &res,
2185 };
2186
Trond Myklebust0e574af2005-10-27 22:12:38 -04002187 nfs_fattr_init(fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002188 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189}
2190
2191static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2192{
2193 struct nfs4_exception exception = { };
2194 int err;
2195 do {
2196 err = nfs4_handle_exception(server,
2197 _nfs4_proc_getattr(server, fhandle, fattr),
2198 &exception);
2199 } while (exception.retry);
2200 return err;
2201}
2202
2203/*
2204 * The file is not closed if it is opened due to the a request to change
2205 * the size of the file. The open call will not be needed once the
2206 * VFS layer lookup-intents are implemented.
2207 *
2208 * Close is called when the inode is destroyed.
2209 * If we haven't opened the file for O_WRONLY, we
2210 * need to in the size_change case to obtain a stateid.
2211 *
2212 * Got race?
2213 * Because OPEN is always done by name in nfsv4, it is
2214 * possible that we opened a different file by the same
2215 * name. We can recognize this race condition, but we
2216 * can't do anything about it besides returning an error.
2217 *
2218 * This will be fixed with VFS changes (lookup-intent).
2219 */
2220static int
2221nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2222 struct iattr *sattr)
2223{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002224 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002225 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002226 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 int status;
2228
Trond Myklebust0e574af2005-10-27 22:12:38 -04002229 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230
Trond Myklebustd5308382005-11-04 15:33:38 -05002231 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002232 if (sattr->ia_valid & ATTR_FILE) {
2233 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002234
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002235 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002236 if (ctx) {
2237 cred = ctx->cred;
2238 state = ctx->state;
2239 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002240 }
2241
2242 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002243 if (status == 0)
2244 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 return status;
2246}
2247
Trond Myklebust56659e92007-07-17 21:52:39 -04002248static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
2249 const struct qstr *name, struct nfs_fh *fhandle,
David Howells2b3de442006-08-22 20:06:09 -04002250 struct nfs_fattr *fattr)
2251{
2252 int status;
2253 struct nfs4_lookup_arg args = {
2254 .bitmask = server->attr_bitmask,
2255 .dir_fh = dirfh,
2256 .name = name,
2257 };
2258 struct nfs4_lookup_res res = {
2259 .server = server,
2260 .fattr = fattr,
2261 .fh = fhandle,
2262 };
2263 struct rpc_message msg = {
2264 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2265 .rpc_argp = &args,
2266 .rpc_resp = &res,
2267 };
2268
2269 nfs_fattr_init(fattr);
2270
2271 dprintk("NFS call lookupfh %s\n", name->name);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002272 status = nfs4_call_sync(server, &msg, &args, &res, 0);
David Howells2b3de442006-08-22 20:06:09 -04002273 dprintk("NFS reply lookupfh: %d\n", status);
David Howells2b3de442006-08-22 20:06:09 -04002274 return status;
2275}
2276
2277static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2278 struct qstr *name, struct nfs_fh *fhandle,
2279 struct nfs_fattr *fattr)
2280{
2281 struct nfs4_exception exception = { };
2282 int err;
2283 do {
Trond Myklebust4e56e082007-07-01 18:13:52 -04002284 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2285 /* FIXME: !!!! */
2286 if (err == -NFS4ERR_MOVED) {
2287 err = -EREMOTE;
2288 break;
2289 }
2290 err = nfs4_handle_exception(server, err, &exception);
David Howells2b3de442006-08-22 20:06:09 -04002291 } while (exception.retry);
2292 return err;
2293}
2294
Trond Myklebust56659e92007-07-17 21:52:39 -04002295static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2297{
Trond Myklebust4e56e082007-07-01 18:13:52 -04002298 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299
2300 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust4e56e082007-07-01 18:13:52 -04002301 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002302 if (status == -NFS4ERR_MOVED)
2303 status = nfs4_get_referral(dir, name, fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 dprintk("NFS reply lookup: %d\n", status);
2305 return status;
2306}
2307
2308static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2309{
2310 struct nfs4_exception exception = { };
2311 int err;
2312 do {
2313 err = nfs4_handle_exception(NFS_SERVER(dir),
2314 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2315 &exception);
2316 } while (exception.retry);
2317 return err;
2318}
2319
2320static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2321{
Trond Myklebust76b32992007-08-10 17:45:11 -04002322 struct nfs_server *server = NFS_SERVER(inode);
2323 struct nfs_fattr fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 struct nfs4_accessargs args = {
2325 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04002326 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002328 struct nfs4_accessres res = {
2329 .server = server,
2330 .fattr = &fattr,
2331 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 struct rpc_message msg = {
2333 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2334 .rpc_argp = &args,
2335 .rpc_resp = &res,
2336 .rpc_cred = entry->cred,
2337 };
2338 int mode = entry->mask;
2339 int status;
2340
2341 /*
2342 * Determine which access bits we want to ask for...
2343 */
2344 if (mode & MAY_READ)
2345 args.access |= NFS4_ACCESS_READ;
2346 if (S_ISDIR(inode->i_mode)) {
2347 if (mode & MAY_WRITE)
2348 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2349 if (mode & MAY_EXEC)
2350 args.access |= NFS4_ACCESS_LOOKUP;
2351 } else {
2352 if (mode & MAY_WRITE)
2353 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2354 if (mode & MAY_EXEC)
2355 args.access |= NFS4_ACCESS_EXECUTE;
2356 }
Trond Myklebust76b32992007-08-10 17:45:11 -04002357 nfs_fattr_init(&fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002358 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 if (!status) {
2360 entry->mask = 0;
2361 if (res.access & NFS4_ACCESS_READ)
2362 entry->mask |= MAY_READ;
2363 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2364 entry->mask |= MAY_WRITE;
2365 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2366 entry->mask |= MAY_EXEC;
Trond Myklebust76b32992007-08-10 17:45:11 -04002367 nfs_refresh_inode(inode, &fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 }
2369 return status;
2370}
2371
2372static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2373{
2374 struct nfs4_exception exception = { };
2375 int err;
2376 do {
2377 err = nfs4_handle_exception(NFS_SERVER(inode),
2378 _nfs4_proc_access(inode, entry),
2379 &exception);
2380 } while (exception.retry);
2381 return err;
2382}
2383
2384/*
2385 * TODO: For the time being, we don't try to get any attributes
2386 * along with any of the zero-copy operations READ, READDIR,
2387 * READLINK, WRITE.
2388 *
2389 * In the case of the first three, we want to put the GETATTR
2390 * after the read-type operation -- this is because it is hard
2391 * to predict the length of a GETATTR response in v4, and thus
2392 * align the READ data correctly. This means that the GETATTR
2393 * may end up partially falling into the page cache, and we should
2394 * shift it into the 'tail' of the xdr_buf before processing.
2395 * To do this efficiently, we need to know the total length
2396 * of data received, which doesn't seem to be available outside
2397 * of the RPC layer.
2398 *
2399 * In the case of WRITE, we also want to put the GETATTR after
2400 * the operation -- in this case because we want to make sure
2401 * we get the post-operation mtime and size. This means that
2402 * we can't use xdr_encode_pages() as written: we need a variant
2403 * of it which would leave room in the 'tail' iovec.
2404 *
2405 * Both of these changes to the XDR layer would in fact be quite
2406 * minor, but I decided to leave them for a subsequent patch.
2407 */
2408static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2409 unsigned int pgbase, unsigned int pglen)
2410{
2411 struct nfs4_readlink args = {
2412 .fh = NFS_FH(inode),
2413 .pgbase = pgbase,
2414 .pglen = pglen,
2415 .pages = &page,
2416 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002417 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 struct rpc_message msg = {
2419 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2420 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002421 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 };
2423
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002424 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425}
2426
2427static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2428 unsigned int pgbase, unsigned int pglen)
2429{
2430 struct nfs4_exception exception = { };
2431 int err;
2432 do {
2433 err = nfs4_handle_exception(NFS_SERVER(inode),
2434 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2435 &exception);
2436 } while (exception.retry);
2437 return err;
2438}
2439
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440/*
2441 * Got race?
2442 * We will need to arrange for the VFS layer to provide an atomic open.
2443 * Until then, this create/open method is prone to inefficiency and race
2444 * conditions due to the lookup, create, and open VFS calls from sys_open()
2445 * placed on the wire.
2446 *
2447 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2448 * The file will be opened again in the subsequent VFS open call
2449 * (nfs4_proc_file_open).
2450 *
2451 * The open for read will just hang around to be used by any process that
2452 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2453 */
2454
2455static int
2456nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebust02a913a2005-10-18 14:20:17 -07002457 int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458{
Trond Myklebustad389da2007-06-05 12:30:00 -04002459 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08002460 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04002461 .dentry = dentry,
2462 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 struct nfs4_state *state;
2464 struct rpc_cred *cred;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002465 fmode_t fmode = flags & (FMODE_READ | FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 int status = 0;
2467
Trond Myklebust98a8e322008-03-12 12:25:28 -04002468 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 if (IS_ERR(cred)) {
2470 status = PTR_ERR(cred);
2471 goto out;
2472 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002473 state = nfs4_do_open(dir, &path, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002474 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 if (IS_ERR(state)) {
2476 status = PTR_ERR(state);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002477 goto out_putcred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002479 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002480 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 if (flags & O_EXCL) {
2482 struct nfs_fattr fattr;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002483 status = nfs4_do_setattr(state->inode, cred, &fattr, sattr, state);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002484 if (status == 0)
Trond Myklebust65e43082005-08-16 11:49:44 -04002485 nfs_setattr_update_inode(state->inode, sattr);
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002486 nfs_post_op_update_inode(state->inode, &fattr);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002487 }
Trond Myklebustad389da2007-06-05 12:30:00 -04002488 if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002489 status = nfs4_intent_set_file(nd, &path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002490 else
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002491 nfs4_close_sync(&path, state, fmode);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002492out_putcred:
2493 put_rpccred(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494out:
2495 return status;
2496}
2497
2498static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2499{
Trond Myklebust16e42952005-10-27 22:12:44 -04002500 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002501 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002503 .name.len = name->len,
2504 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002505 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002507 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002508 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002509 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002511 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2512 .rpc_argp = &args,
2513 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 };
2515 int status;
2516
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002517 nfs_fattr_init(&res.dir_attr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002518 status = nfs4_call_sync(server, &msg, &args, &res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002519 if (status == 0) {
2520 update_changeattr(dir, &res.cinfo);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002521 nfs_post_op_update_inode(dir, &res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 return status;
2524}
2525
2526static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2527{
2528 struct nfs4_exception exception = { };
2529 int err;
2530 do {
2531 err = nfs4_handle_exception(NFS_SERVER(dir),
2532 _nfs4_proc_remove(dir, name),
2533 &exception);
2534 } while (exception.retry);
2535 return err;
2536}
2537
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002538static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002540 struct nfs_server *server = NFS_SERVER(dir);
2541 struct nfs_removeargs *args = msg->rpc_argp;
2542 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543
Trond Myklebusta65318b2009-03-11 14:10:28 -04002544 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002545 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547}
2548
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002549static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002551 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2552
Andy Adamson472cfbd2009-04-01 09:22:24 -04002553 nfs4_sequence_done(res->server, &res->seq_res, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002554 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002555 return 0;
Andy Adamson472cfbd2009-04-01 09:22:24 -04002556 nfs4_sequence_free_slot(res->server->nfs_client, &res->seq_res);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002557 update_changeattr(dir, &res->cinfo);
2558 nfs_post_op_update_inode(dir, &res->dir_attr);
2559 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560}
2561
2562static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2563 struct inode *new_dir, struct qstr *new_name)
2564{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002565 struct nfs_server *server = NFS_SERVER(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 struct nfs4_rename_arg arg = {
2567 .old_dir = NFS_FH(old_dir),
2568 .new_dir = NFS_FH(new_dir),
2569 .old_name = old_name,
2570 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002571 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 };
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002573 struct nfs_fattr old_fattr, new_fattr;
2574 struct nfs4_rename_res res = {
2575 .server = server,
2576 .old_fattr = &old_fattr,
2577 .new_fattr = &new_fattr,
2578 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 struct rpc_message msg = {
2580 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2581 .rpc_argp = &arg,
2582 .rpc_resp = &res,
2583 };
2584 int status;
2585
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002586 nfs_fattr_init(res.old_fattr);
2587 nfs_fattr_init(res.new_fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002588 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589
2590 if (!status) {
2591 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002592 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002594 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 }
2596 return status;
2597}
2598
2599static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2600 struct inode *new_dir, struct qstr *new_name)
2601{
2602 struct nfs4_exception exception = { };
2603 int err;
2604 do {
2605 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2606 _nfs4_proc_rename(old_dir, old_name,
2607 new_dir, new_name),
2608 &exception);
2609 } while (exception.retry);
2610 return err;
2611}
2612
2613static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2614{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002615 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 struct nfs4_link_arg arg = {
2617 .fh = NFS_FH(inode),
2618 .dir_fh = NFS_FH(dir),
2619 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002620 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002622 struct nfs_fattr fattr, dir_attr;
2623 struct nfs4_link_res res = {
2624 .server = server,
2625 .fattr = &fattr,
2626 .dir_attr = &dir_attr,
2627 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 struct rpc_message msg = {
2629 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2630 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002631 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 };
2633 int status;
2634
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002635 nfs_fattr_init(res.fattr);
2636 nfs_fattr_init(res.dir_attr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002637 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002638 if (!status) {
2639 update_changeattr(dir, &res.cinfo);
2640 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002641 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002642 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643
2644 return status;
2645}
2646
2647static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2648{
2649 struct nfs4_exception exception = { };
2650 int err;
2651 do {
2652 err = nfs4_handle_exception(NFS_SERVER(inode),
2653 _nfs4_proc_link(inode, dir, name),
2654 &exception);
2655 } while (exception.retry);
2656 return err;
2657}
2658
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002659struct nfs4_createdata {
2660 struct rpc_message msg;
2661 struct nfs4_create_arg arg;
2662 struct nfs4_create_res res;
2663 struct nfs_fh fh;
2664 struct nfs_fattr fattr;
2665 struct nfs_fattr dir_fattr;
2666};
2667
2668static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2669 struct qstr *name, struct iattr *sattr, u32 ftype)
2670{
2671 struct nfs4_createdata *data;
2672
2673 data = kzalloc(sizeof(*data), GFP_KERNEL);
2674 if (data != NULL) {
2675 struct nfs_server *server = NFS_SERVER(dir);
2676
2677 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2678 data->msg.rpc_argp = &data->arg;
2679 data->msg.rpc_resp = &data->res;
2680 data->arg.dir_fh = NFS_FH(dir);
2681 data->arg.server = server;
2682 data->arg.name = name;
2683 data->arg.attrs = sattr;
2684 data->arg.ftype = ftype;
2685 data->arg.bitmask = server->attr_bitmask;
2686 data->res.server = server;
2687 data->res.fh = &data->fh;
2688 data->res.fattr = &data->fattr;
2689 data->res.dir_fattr = &data->dir_fattr;
2690 nfs_fattr_init(data->res.fattr);
2691 nfs_fattr_init(data->res.dir_fattr);
2692 }
2693 return data;
2694}
2695
2696static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2697{
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002698 int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2699 &data->arg, &data->res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002700 if (status == 0) {
2701 update_changeattr(dir, &data->res.dir_cinfo);
2702 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2703 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2704 }
2705 return status;
2706}
2707
2708static void nfs4_free_createdata(struct nfs4_createdata *data)
2709{
2710 kfree(data);
2711}
2712
Chuck Lever4f390c12006-08-22 20:06:22 -04002713static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002714 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002716 struct nfs4_createdata *data;
2717 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718
Chuck Lever94a6d752006-08-22 20:06:23 -04002719 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002720 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002721
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002722 status = -ENOMEM;
2723 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2724 if (data == NULL)
2725 goto out;
2726
2727 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2728 data->arg.u.symlink.pages = &page;
2729 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002731 status = nfs4_do_create(dir, dentry, data);
2732
2733 nfs4_free_createdata(data);
2734out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 return status;
2736}
2737
Chuck Lever4f390c12006-08-22 20:06:22 -04002738static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002739 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740{
2741 struct nfs4_exception exception = { };
2742 int err;
2743 do {
2744 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002745 _nfs4_proc_symlink(dir, dentry, page,
2746 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 &exception);
2748 } while (exception.retry);
2749 return err;
2750}
2751
2752static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2753 struct iattr *sattr)
2754{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002755 struct nfs4_createdata *data;
2756 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002758 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2759 if (data == NULL)
2760 goto out;
2761
2762 status = nfs4_do_create(dir, dentry, data);
2763
2764 nfs4_free_createdata(data);
2765out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 return status;
2767}
2768
2769static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2770 struct iattr *sattr)
2771{
2772 struct nfs4_exception exception = { };
2773 int err;
2774 do {
2775 err = nfs4_handle_exception(NFS_SERVER(dir),
2776 _nfs4_proc_mkdir(dir, dentry, sattr),
2777 &exception);
2778 } while (exception.retry);
2779 return err;
2780}
2781
2782static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2783 u64 cookie, struct page *page, unsigned int count, int plus)
2784{
2785 struct inode *dir = dentry->d_inode;
2786 struct nfs4_readdir_arg args = {
2787 .fh = NFS_FH(dir),
2788 .pages = &page,
2789 .pgbase = 0,
2790 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09002791 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 };
2793 struct nfs4_readdir_res res;
2794 struct rpc_message msg = {
2795 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2796 .rpc_argp = &args,
2797 .rpc_resp = &res,
2798 .rpc_cred = cred,
2799 };
2800 int status;
2801
Harvey Harrison3110ff82008-05-02 13:42:44 -07002802 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00002803 dentry->d_parent->d_name.name,
2804 dentry->d_name.name,
2805 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2807 res.pgbase = args.pgbase;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002808 status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 if (status == 0)
2810 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustc4812992007-09-28 17:11:45 -04002811
2812 nfs_invalidate_atime(dir);
2813
Harvey Harrison3110ff82008-05-02 13:42:44 -07002814 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 return status;
2816}
2817
2818static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2819 u64 cookie, struct page *page, unsigned int count, int plus)
2820{
2821 struct nfs4_exception exception = { };
2822 int err;
2823 do {
2824 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2825 _nfs4_proc_readdir(dentry, cred, cookie,
2826 page, count, plus),
2827 &exception);
2828 } while (exception.retry);
2829 return err;
2830}
2831
2832static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2833 struct iattr *sattr, dev_t rdev)
2834{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002835 struct nfs4_createdata *data;
2836 int mode = sattr->ia_mode;
2837 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838
2839 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2840 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002841
2842 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2843 if (data == NULL)
2844 goto out;
2845
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002847 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002849 data->arg.ftype = NF4BLK;
2850 data->arg.u.device.specdata1 = MAJOR(rdev);
2851 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 }
2853 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002854 data->arg.ftype = NF4CHR;
2855 data->arg.u.device.specdata1 = MAJOR(rdev);
2856 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002859 status = nfs4_do_create(dir, dentry, data);
2860
2861 nfs4_free_createdata(data);
2862out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 return status;
2864}
2865
2866static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2867 struct iattr *sattr, dev_t rdev)
2868{
2869 struct nfs4_exception exception = { };
2870 int err;
2871 do {
2872 err = nfs4_handle_exception(NFS_SERVER(dir),
2873 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2874 &exception);
2875 } while (exception.retry);
2876 return err;
2877}
2878
2879static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2880 struct nfs_fsstat *fsstat)
2881{
2882 struct nfs4_statfs_arg args = {
2883 .fh = fhandle,
2884 .bitmask = server->attr_bitmask,
2885 };
Benny Halevy24ad1482009-04-01 09:21:56 -04002886 struct nfs4_statfs_res res = {
2887 .fsstat = fsstat,
2888 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 struct rpc_message msg = {
2890 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2891 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04002892 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 };
2894
Trond Myklebust0e574af2005-10-27 22:12:38 -04002895 nfs_fattr_init(fsstat->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002896 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897}
2898
2899static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2900{
2901 struct nfs4_exception exception = { };
2902 int err;
2903 do {
2904 err = nfs4_handle_exception(server,
2905 _nfs4_proc_statfs(server, fhandle, fsstat),
2906 &exception);
2907 } while (exception.retry);
2908 return err;
2909}
2910
2911static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2912 struct nfs_fsinfo *fsinfo)
2913{
2914 struct nfs4_fsinfo_arg args = {
2915 .fh = fhandle,
2916 .bitmask = server->attr_bitmask,
2917 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04002918 struct nfs4_fsinfo_res res = {
2919 .fsinfo = fsinfo,
2920 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 struct rpc_message msg = {
2922 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2923 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04002924 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 };
2926
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002927 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928}
2929
2930static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2931{
2932 struct nfs4_exception exception = { };
2933 int err;
2934
2935 do {
2936 err = nfs4_handle_exception(server,
2937 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2938 &exception);
2939 } while (exception.retry);
2940 return err;
2941}
2942
2943static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2944{
Trond Myklebust0e574af2005-10-27 22:12:38 -04002945 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2947}
2948
2949static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2950 struct nfs_pathconf *pathconf)
2951{
2952 struct nfs4_pathconf_arg args = {
2953 .fh = fhandle,
2954 .bitmask = server->attr_bitmask,
2955 };
Benny Halevyd45b2982009-04-01 09:21:58 -04002956 struct nfs4_pathconf_res res = {
2957 .pathconf = pathconf,
2958 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 struct rpc_message msg = {
2960 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2961 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04002962 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 };
2964
2965 /* None of the pathconf attributes are mandatory to implement */
2966 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
2967 memset(pathconf, 0, sizeof(*pathconf));
2968 return 0;
2969 }
2970
Trond Myklebust0e574af2005-10-27 22:12:38 -04002971 nfs_fattr_init(pathconf->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002972 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973}
2974
2975static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2976 struct nfs_pathconf *pathconf)
2977{
2978 struct nfs4_exception exception = { };
2979 int err;
2980
2981 do {
2982 err = nfs4_handle_exception(server,
2983 _nfs4_proc_pathconf(server, fhandle, pathconf),
2984 &exception);
2985 } while (exception.retry);
2986 return err;
2987}
2988
Trond Myklebustec06c092006-03-20 13:44:27 -05002989static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990{
Trond Myklebustec06c092006-03-20 13:44:27 -05002991 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992
Andy Adamsonf11c88a2009-04-01 09:22:25 -04002993 dprintk("--> %s\n", __func__);
2994
2995 /* nfs4_sequence_free_slot called in the read rpc_call_done */
2996 nfs4_sequence_done(server, &data->res.seq_res, task->tk_status);
2997
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002998 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Andy Adamsone608e792009-12-04 15:55:29 -05002999 nfs4_restart_rpc(task, server->nfs_client, &data->res.seq_res);
Trond Myklebustec06c092006-03-20 13:44:27 -05003000 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003002
3003 nfs_invalidate_atime(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003005 renew_lease(server, data->timestamp);
Andy Adamson36bbe342009-12-04 15:55:30 -05003006 else if (task->tk_status < 0)
3007 nfs4_sequence_free_slot(server->nfs_client, &data->res.seq_res);
3008
Trond Myklebustec06c092006-03-20 13:44:27 -05003009 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010}
3011
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003012static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 data->timestamp = jiffies;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003015 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016}
3017
Trond Myklebust788e7a82006-03-20 13:44:27 -05003018static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 struct inode *inode = data->inode;
3021
Andy Adamsondef6ed72009-04-01 09:22:26 -04003022 /* slot is freed in nfs_writeback_done */
3023 nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
3024 task->tk_status);
3025
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003026 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Andy Adamsone608e792009-12-04 15:55:29 -05003027 nfs4_restart_rpc(task, NFS_SERVER(inode)->nfs_client,
3028 &data->res.seq_res);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003029 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003031 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003033 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003034 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003035 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036}
3037
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003038static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003040 struct nfs_server *server = NFS_SERVER(data->inode);
3041
Trond Myklebusta65318b2009-03-11 14:10:28 -04003042 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003043 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 data->timestamp = jiffies;
3045
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003046 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047}
3048
Trond Myklebust788e7a82006-03-20 13:44:27 -05003049static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 struct inode *inode = data->inode;
3052
Andy Adamson21d9a852009-04-01 09:22:27 -04003053 nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
3054 task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003055 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Andy Adamsone608e792009-12-04 15:55:29 -05003056 nfs4_restart_rpc(task, NFS_SERVER(inode)->nfs_client,
3057 &data->res.seq_res);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003058 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 }
Andy Adamson21d9a852009-04-01 09:22:27 -04003060 nfs4_sequence_free_slot(NFS_SERVER(inode)->nfs_client,
3061 &data->res.seq_res);
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003062 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003063 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064}
3065
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003066static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003068 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069
Trond Myklebusta65318b2009-03-11 14:10:28 -04003070 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003071 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003072 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073}
3074
3075/*
3076 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3077 * standalone procedure for queueing an asynchronous RENEW.
3078 */
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003079static void nfs4_renew_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080{
David Howellsadfa6f92006-08-22 20:06:08 -04003081 struct nfs_client *clp = (struct nfs_client *)task->tk_msg.rpc_argp;
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003082 unsigned long timestamp = (unsigned long)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083
3084 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003085 /* Unless we're shutting down, schedule state recovery! */
3086 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
3087 nfs4_schedule_state_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 return;
3089 }
3090 spin_lock(&clp->cl_lock);
3091 if (time_before(clp->cl_last_renewal,timestamp))
3092 clp->cl_last_renewal = timestamp;
3093 spin_unlock(&clp->cl_lock);
3094}
3095
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003096static const struct rpc_call_ops nfs4_renew_ops = {
3097 .rpc_call_done = nfs4_renew_done,
3098};
3099
David Howellsadfa6f92006-08-22 20:06:08 -04003100int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101{
3102 struct rpc_message msg = {
3103 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3104 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003105 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 };
3107
3108 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003109 &nfs4_renew_ops, (void *)jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110}
3111
David Howellsadfa6f92006-08-22 20:06:08 -04003112int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113{
3114 struct rpc_message msg = {
3115 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3116 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003117 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 };
3119 unsigned long now = jiffies;
3120 int status;
3121
3122 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3123 if (status < 0)
3124 return status;
3125 spin_lock(&clp->cl_lock);
3126 if (time_before(clp->cl_last_renewal,now))
3127 clp->cl_last_renewal = now;
3128 spin_unlock(&clp->cl_lock);
3129 return 0;
3130}
3131
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003132static inline int nfs4_server_supports_acls(struct nfs_server *server)
3133{
3134 return (server->caps & NFS_CAP_ACLS)
3135 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3136 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3137}
3138
3139/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3140 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3141 * the stack.
3142 */
3143#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3144
3145static void buf_to_pages(const void *buf, size_t buflen,
3146 struct page **pages, unsigned int *pgbase)
3147{
3148 const void *p = buf;
3149
3150 *pgbase = offset_in_page(buf);
3151 p -= *pgbase;
3152 while (p < buf + buflen) {
3153 *(pages++) = virt_to_page(p);
3154 p += PAGE_CACHE_SIZE;
3155 }
3156}
3157
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003158struct nfs4_cached_acl {
3159 int cached;
3160 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003161 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003162};
3163
3164static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003165{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003166 struct nfs_inode *nfsi = NFS_I(inode);
3167
3168 spin_lock(&inode->i_lock);
3169 kfree(nfsi->nfs4_acl);
3170 nfsi->nfs4_acl = acl;
3171 spin_unlock(&inode->i_lock);
3172}
3173
3174static void nfs4_zap_acl_attr(struct inode *inode)
3175{
3176 nfs4_set_cached_acl(inode, NULL);
3177}
3178
3179static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3180{
3181 struct nfs_inode *nfsi = NFS_I(inode);
3182 struct nfs4_cached_acl *acl;
3183 int ret = -ENOENT;
3184
3185 spin_lock(&inode->i_lock);
3186 acl = nfsi->nfs4_acl;
3187 if (acl == NULL)
3188 goto out;
3189 if (buf == NULL) /* user is just asking for length */
3190 goto out_len;
3191 if (acl->cached == 0)
3192 goto out;
3193 ret = -ERANGE; /* see getxattr(2) man page */
3194 if (acl->len > buflen)
3195 goto out;
3196 memcpy(buf, acl->data, acl->len);
3197out_len:
3198 ret = acl->len;
3199out:
3200 spin_unlock(&inode->i_lock);
3201 return ret;
3202}
3203
3204static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3205{
3206 struct nfs4_cached_acl *acl;
3207
3208 if (buf && acl_len <= PAGE_SIZE) {
3209 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3210 if (acl == NULL)
3211 goto out;
3212 acl->cached = 1;
3213 memcpy(acl->data, buf, acl_len);
3214 } else {
3215 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3216 if (acl == NULL)
3217 goto out;
3218 acl->cached = 0;
3219 }
3220 acl->len = acl_len;
3221out:
3222 nfs4_set_cached_acl(inode, acl);
3223}
3224
Trond Myklebust16b42892006-08-24 12:27:15 -04003225static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003226{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003227 struct page *pages[NFS4ACL_MAXPAGES];
3228 struct nfs_getaclargs args = {
3229 .fh = NFS_FH(inode),
3230 .acl_pages = pages,
3231 .acl_len = buflen,
3232 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003233 struct nfs_getaclres res = {
3234 .acl_len = buflen,
3235 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003236 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003237 struct rpc_message msg = {
3238 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3239 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003240 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003241 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003242 struct page *localpage = NULL;
3243 int ret;
3244
3245 if (buflen < PAGE_SIZE) {
3246 /* As long as we're doing a round trip to the server anyway,
3247 * let's be prepared for a page of acl data. */
3248 localpage = alloc_page(GFP_KERNEL);
3249 resp_buf = page_address(localpage);
3250 if (localpage == NULL)
3251 return -ENOMEM;
3252 args.acl_pages[0] = localpage;
3253 args.acl_pgbase = 0;
Benny Halevy663c79b2009-04-01 09:21:59 -04003254 args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003255 } else {
3256 resp_buf = buf;
3257 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3258 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003259 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003260 if (ret)
3261 goto out_free;
Benny Halevy663c79b2009-04-01 09:21:59 -04003262 if (res.acl_len > args.acl_len)
3263 nfs4_write_cached_acl(inode, NULL, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003264 else
Benny Halevy663c79b2009-04-01 09:21:59 -04003265 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003266 if (buf) {
3267 ret = -ERANGE;
Benny Halevy663c79b2009-04-01 09:21:59 -04003268 if (res.acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003269 goto out_free;
3270 if (localpage)
Benny Halevy663c79b2009-04-01 09:21:59 -04003271 memcpy(buf, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003272 }
Benny Halevy663c79b2009-04-01 09:21:59 -04003273 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003274out_free:
3275 if (localpage)
3276 __free_page(localpage);
3277 return ret;
3278}
3279
Trond Myklebust16b42892006-08-24 12:27:15 -04003280static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3281{
3282 struct nfs4_exception exception = { };
3283 ssize_t ret;
3284 do {
3285 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3286 if (ret >= 0)
3287 break;
3288 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3289 } while (exception.retry);
3290 return ret;
3291}
3292
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003293static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3294{
3295 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003296 int ret;
3297
3298 if (!nfs4_server_supports_acls(server))
3299 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003300 ret = nfs_revalidate_inode(server, inode);
3301 if (ret < 0)
3302 return ret;
3303 ret = nfs4_read_cached_acl(inode, buf, buflen);
3304 if (ret != -ENOENT)
3305 return ret;
3306 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003307}
3308
Trond Myklebust16b42892006-08-24 12:27:15 -04003309static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003310{
3311 struct nfs_server *server = NFS_SERVER(inode);
3312 struct page *pages[NFS4ACL_MAXPAGES];
3313 struct nfs_setaclargs arg = {
3314 .fh = NFS_FH(inode),
3315 .acl_pages = pages,
3316 .acl_len = buflen,
3317 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003318 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003319 struct rpc_message msg = {
3320 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3321 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003322 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003323 };
3324 int ret;
3325
3326 if (!nfs4_server_supports_acls(server))
3327 return -EOPNOTSUPP;
Trond Myklebust642ac542005-10-18 14:20:19 -07003328 nfs_inode_return_delegation(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003329 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003330 ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003331 nfs_access_zap_cache(inode);
3332 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003333 return ret;
3334}
3335
Trond Myklebust16b42892006-08-24 12:27:15 -04003336static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3337{
3338 struct nfs4_exception exception = { };
3339 int err;
3340 do {
3341 err = nfs4_handle_exception(NFS_SERVER(inode),
3342 __nfs4_proc_set_acl(inode, buf, buflen),
3343 &exception);
3344 } while (exception.retry);
3345 return err;
3346}
3347
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348static int
Andy Adamson8328d592009-04-01 09:22:35 -04003349_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 -07003350{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 if (!clp || task->tk_status >= 0)
3352 return 0;
3353 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003354 case -NFS4ERR_ADMIN_REVOKED:
3355 case -NFS4ERR_BAD_STATEID:
3356 case -NFS4ERR_OPENMODE:
3357 if (state == NULL)
3358 break;
3359 nfs4_state_mark_reclaim_nograce(clp, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 case -NFS4ERR_STALE_CLIENTID:
3361 case -NFS4ERR_STALE_STATEID:
3362 case -NFS4ERR_EXPIRED:
Trond Myklebust5d008372008-02-22 16:34:17 -05003363 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 nfs4_schedule_state_recovery(clp);
Trond Myklebuste005e802008-12-23 15:21:48 -05003365 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
Trond Myklebustfda13932008-02-22 16:34:12 -05003366 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 task->tk_status = 0;
3368 return -EAGAIN;
Andy Adamson4745e312009-04-01 09:22:42 -04003369#if defined(CONFIG_NFS_V4_1)
3370 case -NFS4ERR_BADSESSION:
3371 case -NFS4ERR_BADSLOT:
3372 case -NFS4ERR_BAD_HIGH_SLOT:
3373 case -NFS4ERR_DEADSESSION:
3374 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3375 case -NFS4ERR_SEQ_FALSE_RETRY:
3376 case -NFS4ERR_SEQ_MISORDERED:
3377 dprintk("%s ERROR %d, Reset session\n", __func__,
3378 task->tk_status);
Andy Adamson0b9e2d42009-12-04 16:02:14 -05003379 nfs4_schedule_state_recovery(clp);
Andy Adamson4745e312009-04-01 09:22:42 -04003380 task->tk_status = 0;
3381 return -EAGAIN;
3382#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 case -NFS4ERR_DELAY:
Andy Adamson8328d592009-04-01 09:22:35 -04003384 if (server)
3385 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003386 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3388 task->tk_status = 0;
3389 return -EAGAIN;
3390 case -NFS4ERR_OLD_STATEID:
3391 task->tk_status = 0;
3392 return -EAGAIN;
3393 }
3394 task->tk_status = nfs4_map_errors(task->tk_status);
3395 return 0;
3396}
3397
Andy Adamson8328d592009-04-01 09:22:35 -04003398static int
3399nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3400{
3401 return _nfs4_async_handle_error(task, server, server->nfs_client, state);
3402}
3403
David Howellsadfa6f92006-08-22 20:06:08 -04003404int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, unsigned short port, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405{
3406 nfs4_verifier sc_verifier;
3407 struct nfs4_setclientid setclientid = {
3408 .sc_verifier = &sc_verifier,
3409 .sc_prog = program,
3410 };
3411 struct rpc_message msg = {
3412 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3413 .rpc_argp = &setclientid,
3414 .rpc_resp = clp,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003415 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 };
Al Virobc4785c2006-10-19 23:28:51 -07003417 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 int loop = 0;
3419 int status;
3420
Al Virobc4785c2006-10-19 23:28:51 -07003421 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3423 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3424
3425 for(;;) {
3426 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003427 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003428 clp->cl_ipaddr,
3429 rpc_peeraddr2str(clp->cl_rpcclient,
3430 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003431 rpc_peeraddr2str(clp->cl_rpcclient,
3432 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003433 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 clp->cl_id_uniquifier);
3435 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003436 sizeof(setclientid.sc_netid),
3437 rpc_peeraddr2str(clp->cl_rpcclient,
3438 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003440 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 clp->cl_ipaddr, port >> 8, port & 255);
3442
3443 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3444 if (status != -NFS4ERR_CLID_INUSE)
3445 break;
3446 if (signalled())
3447 break;
3448 if (loop++ & 1)
3449 ssleep(clp->cl_lease_time + 1);
3450 else
3451 if (++clp->cl_id_uniquifier == 0)
3452 break;
3453 }
3454 return status;
3455}
3456
David Howellsadfa6f92006-08-22 20:06:08 -04003457static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458{
3459 struct nfs_fsinfo fsinfo;
3460 struct rpc_message msg = {
3461 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
3462 .rpc_argp = clp,
3463 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003464 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 };
3466 unsigned long now;
3467 int status;
3468
3469 now = jiffies;
3470 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3471 if (status == 0) {
3472 spin_lock(&clp->cl_lock);
3473 clp->cl_lease_time = fsinfo.lease_time * HZ;
3474 clp->cl_last_renewal = now;
3475 spin_unlock(&clp->cl_lock);
3476 }
3477 return status;
3478}
3479
David Howellsadfa6f92006-08-22 20:06:08 -04003480int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
Trond Myklebust51581f32006-03-20 13:44:47 -05003481{
Benny Halevy77e03672008-06-24 20:25:57 +03003482 long timeout = 0;
Trond Myklebust51581f32006-03-20 13:44:47 -05003483 int err;
3484 do {
3485 err = _nfs4_proc_setclientid_confirm(clp, cred);
3486 switch (err) {
3487 case 0:
3488 return err;
3489 case -NFS4ERR_RESOURCE:
3490 /* The IBM lawyers misread another document! */
3491 case -NFS4ERR_DELAY:
3492 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3493 }
3494 } while (err == 0);
3495 return err;
3496}
3497
Trond Myklebustfe650402006-01-03 09:55:18 +01003498struct nfs4_delegreturndata {
3499 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003500 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003501 struct nfs_fh fh;
3502 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003503 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003504 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003505 int rpc_status;
3506};
3507
Trond Myklebustfe650402006-01-03 09:55:18 +01003508static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3509{
3510 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003511
3512 nfs4_sequence_done_free_slot(data->res.server, &data->res.seq_res,
3513 task->tk_status);
3514
Trond Myklebustfe650402006-01-03 09:55:18 +01003515 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003516 if (data->rpc_status == 0)
Trond Myklebustfa178f22006-01-03 09:55:38 +01003517 renew_lease(data->res.server, data->timestamp);
Trond Myklebustfe650402006-01-03 09:55:18 +01003518}
3519
3520static void nfs4_delegreturn_release(void *calldata)
3521{
Trond Myklebustfe650402006-01-03 09:55:18 +01003522 kfree(calldata);
3523}
3524
Andy Adamson938e1012009-04-01 09:22:28 -04003525#if defined(CONFIG_NFS_V4_1)
3526static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3527{
3528 struct nfs4_delegreturndata *d_data;
3529
3530 d_data = (struct nfs4_delegreturndata *)data;
3531
3532 if (nfs4_setup_sequence(d_data->res.server->nfs_client,
3533 &d_data->args.seq_args,
3534 &d_data->res.seq_res, 1, task))
3535 return;
3536 rpc_call_start(task);
3537}
3538#endif /* CONFIG_NFS_V4_1 */
3539
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003540static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003541#if defined(CONFIG_NFS_V4_1)
3542 .rpc_call_prepare = nfs4_delegreturn_prepare,
3543#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003544 .rpc_call_done = nfs4_delegreturn_done,
3545 .rpc_release = nfs4_delegreturn_release,
3546};
3547
Trond Myklebuste6f81072008-01-24 18:14:34 -05003548static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003549{
3550 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003551 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003552 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003553 struct rpc_message msg = {
3554 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3555 .rpc_cred = cred,
3556 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003557 struct rpc_task_setup task_setup_data = {
3558 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003559 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003560 .callback_ops = &nfs4_delegreturn_ops,
3561 .flags = RPC_TASK_ASYNC,
3562 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003563 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003564
Andy Adamson938e1012009-04-01 09:22:28 -04003565 data = kzalloc(sizeof(*data), GFP_KERNEL);
Trond Myklebustfe650402006-01-03 09:55:18 +01003566 if (data == NULL)
3567 return -ENOMEM;
3568 data->args.fhandle = &data->fh;
3569 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003570 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003571 nfs_copy_fh(&data->fh, NFS_FH(inode));
3572 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003573 data->res.fattr = &data->fattr;
3574 data->res.server = server;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003575 data->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003576 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003577 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003578 data->rpc_status = 0;
3579
Trond Myklebustc970aa82007-07-14 15:39:59 -04003580 task_setup_data.callback_data = data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003581 msg.rpc_argp = &data->args,
3582 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003583 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003584 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003585 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003586 if (!issync)
3587 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003588 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003589 if (status != 0)
3590 goto out;
3591 status = data->rpc_status;
3592 if (status != 0)
3593 goto out;
3594 nfs_refresh_inode(inode, &data->fattr);
3595out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003596 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003597 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598}
3599
Trond Myklebuste6f81072008-01-24 18:14:34 -05003600int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601{
3602 struct nfs_server *server = NFS_SERVER(inode);
3603 struct nfs4_exception exception = { };
3604 int err;
3605 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003606 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 switch (err) {
3608 case -NFS4ERR_STALE_STATEID:
3609 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 case 0:
3611 return 0;
3612 }
3613 err = nfs4_handle_exception(server, err, &exception);
3614 } while (exception.retry);
3615 return err;
3616}
3617
3618#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3619#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3620
3621/*
3622 * sleep, with exponential backoff, and retry the LOCK operation.
3623 */
3624static unsigned long
3625nfs4_set_lock_task_retry(unsigned long timeout)
3626{
Matthew Wilcox150030b2007-12-06 16:24:39 -05003627 schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628 timeout <<= 1;
3629 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3630 return NFS4_LOCK_MAXTIMEOUT;
3631 return timeout;
3632}
3633
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3635{
3636 struct inode *inode = state->inode;
3637 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003638 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003639 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003641 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003643 struct nfs_lockt_res res = {
3644 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 };
3646 struct rpc_message msg = {
3647 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3648 .rpc_argp = &arg,
3649 .rpc_resp = &res,
3650 .rpc_cred = state->owner->so_cred,
3651 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652 struct nfs4_lock_state *lsp;
3653 int status;
3654
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003655 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003656 status = nfs4_set_lock_state(state, request);
3657 if (status != 0)
3658 goto out;
3659 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003660 arg.lock_owner.id = lsp->ls_id.id;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003661 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003662 switch (status) {
3663 case 0:
3664 request->fl_type = F_UNLCK;
3665 break;
3666 case -NFS4ERR_DENIED:
3667 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05003669 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003670out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 return status;
3672}
3673
3674static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3675{
3676 struct nfs4_exception exception = { };
3677 int err;
3678
3679 do {
3680 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3681 _nfs4_proc_getlk(state, cmd, request),
3682 &exception);
3683 } while (exception.retry);
3684 return err;
3685}
3686
3687static int do_vfs_lock(struct file *file, struct file_lock *fl)
3688{
3689 int res = 0;
3690 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3691 case FL_POSIX:
3692 res = posix_lock_file_wait(file, fl);
3693 break;
3694 case FL_FLOCK:
3695 res = flock_lock_file_wait(file, fl);
3696 break;
3697 default:
3698 BUG();
3699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700 return res;
3701}
3702
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003703struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003704 struct nfs_locku_args arg;
3705 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003706 struct nfs4_lock_state *lsp;
3707 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003708 struct file_lock fl;
3709 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003710 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003711};
3712
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003713static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3714 struct nfs_open_context *ctx,
3715 struct nfs4_lock_state *lsp,
3716 struct nfs_seqid *seqid)
3717{
3718 struct nfs4_unlockdata *p;
3719 struct inode *inode = lsp->ls_state->inode;
3720
Andy Adamsona8936932009-04-01 09:22:23 -04003721 p = kzalloc(sizeof(*p), GFP_KERNEL);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003722 if (p == NULL)
3723 return NULL;
3724 p->arg.fh = NFS_FH(inode);
3725 p->arg.fl = &p->fl;
3726 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003727 p->res.seqid = seqid;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003728 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003729 p->arg.stateid = &lsp->ls_stateid;
3730 p->lsp = lsp;
3731 atomic_inc(&lsp->ls_count);
3732 /* Ensure we don't close file until we're done freeing locks! */
3733 p->ctx = get_nfs_open_context(ctx);
3734 memcpy(&p->fl, fl, sizeof(p->fl));
3735 p->server = NFS_SERVER(inode);
3736 return p;
3737}
3738
Trond Myklebust06f814a2006-01-03 09:55:07 +01003739static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003740{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003741 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003742 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003743 nfs4_put_lock_state(calldata->lsp);
3744 put_nfs_open_context(calldata->ctx);
3745 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003746}
3747
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003748static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003749{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003750 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003751
Andy Adamsona8936932009-04-01 09:22:23 -04003752 nfs4_sequence_done(calldata->server, &calldata->res.seq_res,
3753 task->tk_status);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003754 if (RPC_ASSASSINATED(task))
3755 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003756 switch (task->tk_status) {
3757 case 0:
3758 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003759 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003760 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01003761 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003762 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003763 case -NFS4ERR_BAD_STATEID:
3764 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003765 case -NFS4ERR_STALE_STATEID:
3766 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003767 break;
3768 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003769 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Andy Adamsoneedc0202009-04-01 09:22:41 -04003770 nfs4_restart_rpc(task,
Andy Adamsone608e792009-12-04 15:55:29 -05003771 calldata->server->nfs_client,
3772 &calldata->res.seq_res);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003773 }
Andy Adamsona8936932009-04-01 09:22:23 -04003774 nfs4_sequence_free_slot(calldata->server->nfs_client,
3775 &calldata->res.seq_res);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003776}
3777
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003778static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003779{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003780 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003782 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003783 return;
3784 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003785 /* Note: exit _without_ running nfs4_locku_done */
3786 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003787 return;
3788 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003789 calldata->timestamp = jiffies;
Andy Adamsona8936932009-04-01 09:22:23 -04003790 if (nfs4_setup_sequence(calldata->server->nfs_client,
3791 &calldata->arg.seq_args,
3792 &calldata->res.seq_res, 1, task))
3793 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003794 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795}
3796
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003797static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003798 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003799 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01003800 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003801};
3802
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003803static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3804 struct nfs_open_context *ctx,
3805 struct nfs4_lock_state *lsp,
3806 struct nfs_seqid *seqid)
3807{
3808 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003809 struct rpc_message msg = {
3810 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3811 .rpc_cred = ctx->cred,
3812 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003813 struct rpc_task_setup task_setup_data = {
3814 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04003815 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003816 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003817 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003818 .flags = RPC_TASK_ASYNC,
3819 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003820
Frank Filz137d6ac2007-07-09 15:32:29 -07003821 /* Ensure this is an unlock - when canceling a lock, the
3822 * canceled lock is passed in, and it won't be an unlock.
3823 */
3824 fl->fl_type = F_UNLCK;
3825
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003826 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3827 if (data == NULL) {
3828 nfs_free_seqid(seqid);
3829 return ERR_PTR(-ENOMEM);
3830 }
3831
Trond Myklebust5138fde2007-07-14 15:40:01 -04003832 msg.rpc_argp = &data->arg,
3833 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003834 task_setup_data.callback_data = data;
3835 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003836}
3837
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3839{
Trond Myklebust19e03c52008-12-23 15:21:44 -05003840 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003841 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003842 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01003843 struct rpc_task *task;
3844 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003845 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846
Trond Myklebust9b073572006-06-29 16:38:34 -04003847 status = nfs4_set_lock_state(state, request);
3848 /* Unlock _before_ we do the RPC call */
3849 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003850 down_read(&nfsi->rwsem);
3851 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
3852 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003853 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003854 }
3855 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003856 if (status != 0)
3857 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003858 /* Is this a delegated lock? */
3859 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04003860 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003861 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003862 seqid = nfs_alloc_seqid(&lsp->ls_seqid);
Trond Myklebust9b073572006-06-29 16:38:34 -04003863 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003864 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04003865 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04003866 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003867 status = PTR_ERR(task);
3868 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04003869 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003870 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003871 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04003872out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003873 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003874 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875}
3876
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003877struct nfs4_lockdata {
3878 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003879 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003880 struct nfs4_lock_state *lsp;
3881 struct nfs_open_context *ctx;
3882 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003883 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003884 int rpc_status;
3885 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04003886 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003887};
3888
3889static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
3890 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
3891{
3892 struct nfs4_lockdata *p;
3893 struct inode *inode = lsp->ls_state->inode;
3894 struct nfs_server *server = NFS_SERVER(inode);
3895
3896 p = kzalloc(sizeof(*p), GFP_KERNEL);
3897 if (p == NULL)
3898 return NULL;
3899
3900 p->arg.fh = NFS_FH(inode);
3901 p->arg.fl = &p->fl;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003902 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid);
3903 if (p->arg.open_seqid == NULL)
3904 goto out_free;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003905 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3906 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003907 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003908 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04003909 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003910 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003911 p->res.lock_seqid = p->arg.lock_seqid;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003912 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003913 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04003914 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003915 atomic_inc(&lsp->ls_count);
3916 p->ctx = get_nfs_open_context(ctx);
3917 memcpy(&p->fl, fl, sizeof(p->fl));
3918 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003919out_free_seqid:
3920 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003921out_free:
3922 kfree(p);
3923 return NULL;
3924}
3925
3926static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
3927{
3928 struct nfs4_lockdata *data = calldata;
3929 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930
Harvey Harrison3110ff82008-05-02 13:42:44 -07003931 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003932 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
3933 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003934 /* Do we need to do an open_to_lock_owner? */
3935 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05003936 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
3937 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003938 data->arg.open_stateid = &state->stateid;
3939 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003940 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003941 } else
3942 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003943 data->timestamp = jiffies;
Andy Adamson66179ef2009-04-01 09:22:22 -04003944 if (nfs4_setup_sequence(data->server->nfs_client, &data->arg.seq_args,
3945 &data->res.seq_res, 1, task))
3946 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003947 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003948 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003949}
3950
3951static void nfs4_lock_done(struct rpc_task *task, void *calldata)
3952{
3953 struct nfs4_lockdata *data = calldata;
3954
Harvey Harrison3110ff82008-05-02 13:42:44 -07003955 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003956
Andy Adamson66179ef2009-04-01 09:22:22 -04003957 nfs4_sequence_done_free_slot(data->server, &data->res.seq_res,
3958 task->tk_status);
3959
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003960 data->rpc_status = task->tk_status;
3961 if (RPC_ASSASSINATED(task))
3962 goto out;
3963 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003964 if (data->rpc_status == 0)
3965 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
3966 else
3967 goto out;
3968 }
3969 if (data->rpc_status == 0) {
3970 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
3971 sizeof(data->lsp->ls_stateid.data));
3972 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust88be9f92007-06-05 10:42:27 -04003973 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003974 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003975out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07003976 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003977}
3978
3979static void nfs4_lock_release(void *calldata)
3980{
3981 struct nfs4_lockdata *data = calldata;
3982
Harvey Harrison3110ff82008-05-02 13:42:44 -07003983 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003984 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003985 if (data->cancelled != 0) {
3986 struct rpc_task *task;
3987 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
3988 data->arg.lock_seqid);
3989 if (!IS_ERR(task))
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003990 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003991 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003992 } else
3993 nfs_free_seqid(data->arg.lock_seqid);
3994 nfs4_put_lock_state(data->lsp);
3995 put_nfs_open_context(data->ctx);
3996 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003997 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003998}
3999
4000static const struct rpc_call_ops nfs4_lock_ops = {
4001 .rpc_call_prepare = nfs4_lock_prepare,
4002 .rpc_call_done = nfs4_lock_done,
4003 .rpc_release = nfs4_lock_release,
4004};
4005
4006static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int reclaim)
4007{
4008 struct nfs4_lockdata *data;
4009 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004010 struct rpc_message msg = {
4011 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4012 .rpc_cred = state->owner->so_cred,
4013 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004014 struct rpc_task_setup task_setup_data = {
4015 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004016 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004017 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004018 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004019 .flags = RPC_TASK_ASYNC,
4020 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004021 int ret;
4022
Harvey Harrison3110ff82008-05-02 13:42:44 -07004023 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004024 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004025 fl->fl_u.nfs4_fl.owner);
4026 if (data == NULL)
4027 return -ENOMEM;
4028 if (IS_SETLKW(cmd))
4029 data->arg.block = 1;
4030 if (reclaim != 0)
4031 data->arg.reclaim = 1;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004032 msg.rpc_argp = &data->arg,
4033 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004034 task_setup_data.callback_data = data;
4035 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004036 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004037 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004038 ret = nfs4_wait_for_completion_rpc_task(task);
4039 if (ret == 0) {
4040 ret = data->rpc_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004041 } else
4042 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004043 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004044 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004045 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046}
4047
4048static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4049{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004050 struct nfs_server *server = NFS_SERVER(state->inode);
4051 struct nfs4_exception exception = { };
4052 int err;
4053
4054 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004055 /* Cache the lock if possible... */
4056 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4057 return 0;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004058 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
4059 if (err != -NFS4ERR_DELAY)
4060 break;
4061 nfs4_handle_exception(server, err, &exception);
4062 } while (exception.retry);
4063 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064}
4065
4066static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4067{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004068 struct nfs_server *server = NFS_SERVER(state->inode);
4069 struct nfs4_exception exception = { };
4070 int err;
4071
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004072 err = nfs4_set_lock_state(state, request);
4073 if (err != 0)
4074 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004075 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004076 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4077 return 0;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004078 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004079 switch (err) {
4080 default:
4081 goto out;
4082 case -NFS4ERR_GRACE:
4083 case -NFS4ERR_DELAY:
4084 nfs4_handle_exception(server, err, &exception);
4085 err = 0;
4086 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004087 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004088out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004089 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090}
4091
4092static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4093{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004094 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004095 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 int status;
4097
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004098 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004099 status = nfs4_set_lock_state(state, request);
4100 if (status != 0)
4101 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004102 request->fl_flags |= FL_ACCESS;
4103 status = do_vfs_lock(request->fl_file, request);
4104 if (status < 0)
4105 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004106 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004107 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004108 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004109 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004110 request->fl_flags = fl_flags & ~FL_SLEEP;
4111 status = do_vfs_lock(request->fl_file, request);
4112 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004113 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004114 status = _nfs4_do_setlk(state, cmd, request, 0);
4115 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004116 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004117 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004118 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004119 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07004120 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004121out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004122 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004123out:
4124 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 return status;
4126}
4127
4128static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4129{
4130 struct nfs4_exception exception = { };
4131 int err;
4132
4133 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004134 err = _nfs4_proc_setlk(state, cmd, request);
4135 if (err == -NFS4ERR_DENIED)
4136 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004138 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139 } while (exception.retry);
4140 return err;
4141}
4142
4143static int
4144nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4145{
4146 struct nfs_open_context *ctx;
4147 struct nfs4_state *state;
4148 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4149 int status;
4150
4151 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004152 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 state = ctx->state;
4154
4155 if (request->fl_start < 0 || request->fl_end < 0)
4156 return -EINVAL;
4157
Trond Myklebustd9531262009-07-21 19:22:38 -04004158 if (IS_GETLK(cmd)) {
4159 if (state != NULL)
4160 return nfs4_proc_getlk(state, F_GETLK, request);
4161 return 0;
4162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163
4164 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4165 return -EINVAL;
4166
Trond Myklebustd9531262009-07-21 19:22:38 -04004167 if (request->fl_type == F_UNLCK) {
4168 if (state != NULL)
4169 return nfs4_proc_unlck(state, cmd, request);
4170 return 0;
4171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172
Trond Myklebustd9531262009-07-21 19:22:38 -04004173 if (state == NULL)
4174 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175 do {
4176 status = nfs4_proc_setlk(state, cmd, request);
4177 if ((status != -EAGAIN) || IS_SETLK(cmd))
4178 break;
4179 timeout = nfs4_set_lock_task_retry(timeout);
4180 status = -ERESTARTSYS;
4181 if (signalled())
4182 break;
4183 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 return status;
4185}
4186
Trond Myklebust888e6942005-11-04 15:38:11 -05004187int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4188{
4189 struct nfs_server *server = NFS_SERVER(state->inode);
4190 struct nfs4_exception exception = { };
4191 int err;
4192
4193 err = nfs4_set_lock_state(state, fl);
4194 if (err != 0)
4195 goto out;
4196 do {
4197 err = _nfs4_do_setlk(state, F_SETLK, fl, 0);
Trond Myklebustd5122202009-06-17 13:22:58 -07004198 switch (err) {
4199 default:
4200 printk(KERN_ERR "%s: unhandled error %d.\n",
4201 __func__, err);
4202 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004203 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004204 goto out;
4205 case -NFS4ERR_EXPIRED:
4206 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004207 case -NFS4ERR_STALE_STATEID:
Trond Myklebustd5122202009-06-17 13:22:58 -07004208 nfs4_schedule_state_recovery(server->nfs_client);
4209 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004210 case -ERESTARTSYS:
4211 /*
4212 * The show must go on: exit, but mark the
4213 * stateid as needing recovery.
4214 */
4215 case -NFS4ERR_ADMIN_REVOKED:
4216 case -NFS4ERR_BAD_STATEID:
4217 case -NFS4ERR_OPENMODE:
4218 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
4219 err = 0;
4220 goto out;
4221 case -ENOMEM:
4222 case -NFS4ERR_DENIED:
4223 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4224 err = 0;
4225 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004226 case -NFS4ERR_DELAY:
4227 break;
4228 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004229 err = nfs4_handle_exception(server, err, &exception);
4230 } while (exception.retry);
4231out:
4232 return err;
4233}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004234
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004235#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4236
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004237int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
4238 size_t buflen, int flags)
4239{
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004240 struct inode *inode = dentry->d_inode;
4241
4242 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4243 return -EOPNOTSUPP;
4244
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004245 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004246}
4247
4248/* The getxattr man page suggests returning -ENODATA for unknown attributes,
4249 * and that's what we'll do for e.g. user attributes that haven't been set.
4250 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
4251 * attributes in kernel-managed attribute namespaces. */
4252ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
4253 size_t buflen)
4254{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004255 struct inode *inode = dentry->d_inode;
4256
4257 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4258 return -EOPNOTSUPP;
4259
4260 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004261}
4262
4263ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
4264{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004265 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004266
J. Bruce Fields096455a2006-03-20 23:23:42 -05004267 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4268 return 0;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004269 if (buf && buflen < len)
4270 return -ERANGE;
4271 if (buf)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004272 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
4273 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004274}
4275
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004276static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4277{
4278 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4279 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4280 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4281 return;
4282
4283 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4284 NFS_ATTR_FATTR_NLINK;
4285 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4286 fattr->nlink = 2;
4287}
4288
Trond Myklebust56659e92007-07-17 21:52:39 -04004289int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004290 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004291{
4292 struct nfs_server *server = NFS_SERVER(dir);
4293 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004294 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4295 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004296 };
4297 struct nfs4_fs_locations_arg args = {
4298 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004299 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004300 .page = page,
4301 .bitmask = bitmask,
4302 };
Benny Halevy22958462009-04-01 09:22:02 -04004303 struct nfs4_fs_locations_res res = {
4304 .fs_locations = fs_locations,
4305 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004306 struct rpc_message msg = {
4307 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4308 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004309 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004310 };
4311 int status;
4312
Harvey Harrison3110ff82008-05-02 13:42:44 -07004313 dprintk("%s: start\n", __func__);
Trond Myklebustc228fd32007-01-13 02:28:11 -05004314 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004315 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004316 fs_locations->nlocations = 0;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04004317 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004318 nfs_fixup_referral_attributes(&fs_locations->fattr);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004319 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004320 return status;
4321}
4322
Andy Adamson557134a2009-04-01 09:21:53 -04004323#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004324/*
4325 * nfs4_proc_exchange_id()
4326 *
4327 * Since the clientid has expired, all compounds using sessions
4328 * associated with the stale clientid will be returning
4329 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4330 * be in some phase of session reset.
4331 */
Andy Adamson4d643d12009-12-04 15:52:24 -05004332int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004333{
4334 nfs4_verifier verifier;
4335 struct nfs41_exchange_id_args args = {
4336 .client = clp,
4337 .flags = clp->cl_exchange_flags,
4338 };
4339 struct nfs41_exchange_id_res res = {
4340 .client = clp,
4341 };
4342 int status;
4343 struct rpc_message msg = {
4344 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4345 .rpc_argp = &args,
4346 .rpc_resp = &res,
4347 .rpc_cred = cred,
4348 };
4349 __be32 *p;
4350
4351 dprintk("--> %s\n", __func__);
4352 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004353
Alexandros Batsakis7b183d02009-12-05 13:33:25 -05004354 /* Remove server-only flags */
4355 args.flags &= ~EXCHGID4_FLAG_CONFIRMED_R;
4356
Benny Halevy99fe60d2009-04-01 09:22:29 -04004357 p = (u32 *)verifier.data;
4358 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4359 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4360 args.verifier = &verifier;
4361
4362 while (1) {
4363 args.id_len = scnprintf(args.id, sizeof(args.id),
4364 "%s/%s %u",
4365 clp->cl_ipaddr,
4366 rpc_peeraddr2str(clp->cl_rpcclient,
4367 RPC_DISPLAY_ADDR),
4368 clp->cl_id_uniquifier);
4369
4370 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4371
4372 if (status != NFS4ERR_CLID_INUSE)
4373 break;
4374
4375 if (signalled())
4376 break;
4377
4378 if (++clp->cl_id_uniquifier == 0)
4379 break;
4380 }
4381
4382 dprintk("<-- %s status= %d\n", __func__, status);
4383 return status;
4384}
4385
Andy Adamson2050f0c2009-04-01 09:22:30 -04004386struct nfs4_get_lease_time_data {
4387 struct nfs4_get_lease_time_args *args;
4388 struct nfs4_get_lease_time_res *res;
4389 struct nfs_client *clp;
4390};
4391
4392static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4393 void *calldata)
4394{
4395 int ret;
4396 struct nfs4_get_lease_time_data *data =
4397 (struct nfs4_get_lease_time_data *)calldata;
4398
4399 dprintk("--> %s\n", __func__);
4400 /* just setup sequence, do not trigger session recovery
4401 since we're invoked within one */
4402 ret = nfs41_setup_sequence(data->clp->cl_session,
4403 &data->args->la_seq_args,
4404 &data->res->lr_seq_res, 0, task);
4405
4406 BUG_ON(ret == -EAGAIN);
4407 rpc_call_start(task);
4408 dprintk("<-- %s\n", __func__);
4409}
4410
4411/*
4412 * Called from nfs4_state_manager thread for session setup, so don't recover
4413 * from sequence operation or clientid errors.
4414 */
4415static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4416{
4417 struct nfs4_get_lease_time_data *data =
4418 (struct nfs4_get_lease_time_data *)calldata;
4419
4420 dprintk("--> %s\n", __func__);
4421 nfs41_sequence_done(data->clp, &data->res->lr_seq_res, task->tk_status);
4422 switch (task->tk_status) {
4423 case -NFS4ERR_DELAY:
4424 case -NFS4ERR_GRACE:
4425 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4426 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4427 task->tk_status = 0;
Andy Adamson1d9ddde92009-12-04 15:55:27 -05004428 rpc_restart_call(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004429 return;
4430 }
4431 nfs41_sequence_free_slot(data->clp, &data->res->lr_seq_res);
4432 dprintk("<-- %s\n", __func__);
4433}
4434
4435struct rpc_call_ops nfs4_get_lease_time_ops = {
4436 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4437 .rpc_call_done = nfs4_get_lease_time_done,
4438};
4439
4440int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4441{
4442 struct rpc_task *task;
4443 struct nfs4_get_lease_time_args args;
4444 struct nfs4_get_lease_time_res res = {
4445 .lr_fsinfo = fsinfo,
4446 };
4447 struct nfs4_get_lease_time_data data = {
4448 .args = &args,
4449 .res = &res,
4450 .clp = clp,
4451 };
4452 struct rpc_message msg = {
4453 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4454 .rpc_argp = &args,
4455 .rpc_resp = &res,
4456 };
4457 struct rpc_task_setup task_setup = {
4458 .rpc_client = clp->cl_rpcclient,
4459 .rpc_message = &msg,
4460 .callback_ops = &nfs4_get_lease_time_ops,
4461 .callback_data = &data
4462 };
4463 int status;
4464
4465 res.lr_seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
4466 dprintk("--> %s\n", __func__);
4467 task = rpc_run_task(&task_setup);
4468
4469 if (IS_ERR(task))
4470 status = PTR_ERR(task);
4471 else {
4472 status = task->tk_status;
4473 rpc_put_task(task);
4474 }
4475 dprintk("<-- %s return %d\n", __func__, status);
4476
4477 return status;
4478}
4479
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004480/*
4481 * Reset a slot table
4482 */
4483static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, int max_slots,
4484 int old_max_slots, int ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004485{
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004486 int i;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004487 int ret = 0;
4488
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004489 dprintk("--> %s: max_reqs=%u, tbl %p\n", __func__, max_slots, tbl);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004490
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004491 /*
4492 * Until we have dynamic slot table adjustment, insist
4493 * upon the same slot table size
4494 */
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004495 if (max_slots != old_max_slots) {
4496 dprintk("%s reset slot table does't match old\n",
4497 __func__);
4498 ret = -EINVAL; /*XXX NFS4ERR_REQ_TOO_BIG ? */
4499 goto out;
4500 }
4501 spin_lock(&tbl->slot_tbl_lock);
4502 for (i = 0; i < max_slots; ++i)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004503 tbl->slots[i].seq_nr = ivalue;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004504 tbl->highest_used_slotid = -1;
4505 spin_unlock(&tbl->slot_tbl_lock);
4506 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4507 tbl, tbl->slots, tbl->max_slots);
4508out:
4509 dprintk("<-- %s: return %d\n", __func__, ret);
4510 return ret;
4511}
4512
Andy Adamsonfc931582009-04-01 09:22:31 -04004513/*
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004514 * Reset the forechannel and backchannel slot tables
4515 */
4516static int nfs4_reset_slot_tables(struct nfs4_session *session)
4517{
4518 int status;
4519
4520 status = nfs4_reset_slot_table(&session->fc_slot_table,
4521 session->fc_attrs.max_reqs,
4522 session->fc_slot_table.max_slots,
4523 1);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004524 if (status)
4525 return status;
Andy Adamsonea028ac2009-12-04 15:55:38 -05004526 init_completion(&session->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004527
4528 status = nfs4_reset_slot_table(&session->bc_slot_table,
4529 session->bc_attrs.max_reqs,
4530 session->bc_slot_table.max_slots,
4531 0);
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004532 return status;
4533}
4534
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004535/* Destroy the slot table */
4536static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4537{
4538 if (session->fc_slot_table.slots != NULL) {
4539 kfree(session->fc_slot_table.slots);
4540 session->fc_slot_table.slots = NULL;
4541 }
4542 if (session->bc_slot_table.slots != NULL) {
4543 kfree(session->bc_slot_table.slots);
4544 session->bc_slot_table.slots = NULL;
4545 }
4546 return;
4547}
4548
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004549/*
Andy Adamsonfc931582009-04-01 09:22:31 -04004550 * Initialize slot table
4551 */
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004552static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
4553 int max_slots, int ivalue)
Andy Adamsonfc931582009-04-01 09:22:31 -04004554{
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004555 int i;
Andy Adamsonfc931582009-04-01 09:22:31 -04004556 struct nfs4_slot *slot;
4557 int ret = -ENOMEM;
4558
4559 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
4560
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004561 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
Andy Adamsonfc931582009-04-01 09:22:31 -04004562
4563 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_KERNEL);
4564 if (!slot)
4565 goto out;
4566 for (i = 0; i < max_slots; ++i)
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004567 slot[i].seq_nr = ivalue;
Andy Adamsonfc931582009-04-01 09:22:31 -04004568 ret = 0;
4569
4570 spin_lock(&tbl->slot_tbl_lock);
4571 if (tbl->slots != NULL) {
4572 spin_unlock(&tbl->slot_tbl_lock);
4573 dprintk("%s: slot table already initialized. tbl=%p slots=%p\n",
4574 __func__, tbl, tbl->slots);
4575 WARN_ON(1);
4576 goto out_free;
4577 }
4578 tbl->max_slots = max_slots;
4579 tbl->slots = slot;
4580 tbl->highest_used_slotid = -1; /* no slot is currently used */
4581 spin_unlock(&tbl->slot_tbl_lock);
4582 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4583 tbl, tbl->slots, tbl->max_slots);
4584out:
4585 dprintk("<-- %s: return %d\n", __func__, ret);
4586 return ret;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004587
Andy Adamsonfc931582009-04-01 09:22:31 -04004588out_free:
4589 kfree(slot);
4590 goto out;
4591}
4592
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004593/*
4594 * Initialize the forechannel and backchannel tables
4595 */
4596static int nfs4_init_slot_tables(struct nfs4_session *session)
4597{
4598 int status;
4599
4600 status = nfs4_init_slot_table(&session->fc_slot_table,
4601 session->fc_attrs.max_reqs, 1);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004602 if (status)
4603 return status;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004604
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004605 status = nfs4_init_slot_table(&session->bc_slot_table,
4606 session->bc_attrs.max_reqs, 0);
4607 if (status)
4608 nfs4_destroy_slot_tables(session);
4609
4610 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04004611}
4612
4613struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4614{
4615 struct nfs4_session *session;
4616 struct nfs4_slot_table *tbl;
4617
4618 session = kzalloc(sizeof(struct nfs4_session), GFP_KERNEL);
4619 if (!session)
4620 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04004621
Andy Adamson76db6d92009-04-01 09:22:38 -04004622 /*
4623 * The create session reply races with the server back
4624 * channel probe. Mark the client NFS_CS_SESSION_INITING
4625 * so that the client back channel can find the
4626 * nfs_client struct
4627 */
4628 clp->cl_cons_state = NFS_CS_SESSION_INITING;
Andy Adamsonea028ac2009-12-04 15:55:38 -05004629 init_completion(&session->complete);
Andy Adamson76db6d92009-04-01 09:22:38 -04004630
Andy Adamson557134a2009-04-01 09:21:53 -04004631 tbl = &session->fc_slot_table;
4632 spin_lock_init(&tbl->slot_tbl_lock);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004633 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
4634
4635 tbl = &session->bc_slot_table;
4636 spin_lock_init(&tbl->slot_tbl_lock);
4637 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
4638
Andy Adamson557134a2009-04-01 09:21:53 -04004639 session->clp = clp;
4640 return session;
4641}
4642
4643void nfs4_destroy_session(struct nfs4_session *session)
4644{
Andy Adamson5a0ffe52009-04-01 09:23:18 -04004645 nfs4_proc_destroy_session(session);
4646 dprintk("%s Destroy backchannel for xprt %p\n",
4647 __func__, session->clp->cl_rpcclient->cl_xprt);
4648 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
4649 NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004650 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04004651 kfree(session);
4652}
4653
Andy Adamsonfc931582009-04-01 09:22:31 -04004654/*
4655 * Initialize the values to be used by the client in CREATE_SESSION
4656 * If nfs4_init_session set the fore channel request and response sizes,
4657 * use them.
4658 *
4659 * Set the back channel max_resp_sz_cached to zero to force the client to
4660 * always set csa_cachethis to FALSE because the current implementation
4661 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4662 */
4663static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
4664{
4665 struct nfs4_session *session = args->client->cl_session;
4666 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
4667 mxresp_sz = session->fc_attrs.max_resp_sz;
4668
4669 if (mxrqst_sz == 0)
4670 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
4671 if (mxresp_sz == 0)
4672 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
4673 /* Fore channel attributes */
4674 args->fc_attrs.headerpadsz = 0;
4675 args->fc_attrs.max_rqst_sz = mxrqst_sz;
4676 args->fc_attrs.max_resp_sz = mxresp_sz;
4677 args->fc_attrs.max_resp_sz_cached = mxresp_sz;
4678 args->fc_attrs.max_ops = NFS4_MAX_OPS;
4679 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
4680
4681 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
4682 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4683 __func__,
4684 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
4685 args->fc_attrs.max_resp_sz_cached, args->fc_attrs.max_ops,
4686 args->fc_attrs.max_reqs);
4687
4688 /* Back channel attributes */
4689 args->bc_attrs.headerpadsz = 0;
4690 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
4691 args->bc_attrs.max_resp_sz = PAGE_SIZE;
4692 args->bc_attrs.max_resp_sz_cached = 0;
4693 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
4694 args->bc_attrs.max_reqs = 1;
4695
4696 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
4697 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4698 __func__,
4699 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
4700 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
4701 args->bc_attrs.max_reqs);
4702}
4703
Andy Adamson8d353012009-04-01 09:22:32 -04004704static int _verify_channel_attr(char *chan, char *attr_name, u32 sent, u32 rcvd)
4705{
4706 if (rcvd <= sent)
4707 return 0;
4708 printk(KERN_WARNING "%s: Session INVALID: %s channel %s increased. "
4709 "sent=%u rcvd=%u\n", __func__, chan, attr_name, sent, rcvd);
4710 return -EINVAL;
4711}
4712
4713#define _verify_fore_channel_attr(_name_) \
4714 _verify_channel_attr("fore", #_name_, \
4715 args->fc_attrs._name_, \
4716 session->fc_attrs._name_)
4717
4718#define _verify_back_channel_attr(_name_) \
4719 _verify_channel_attr("back", #_name_, \
4720 args->bc_attrs._name_, \
4721 session->bc_attrs._name_)
4722
4723/*
4724 * The server is not allowed to increase the fore channel header pad size,
4725 * maximum response size, or maximum number of operations.
4726 *
4727 * The back channel attributes are only negotiatied down: We send what the
4728 * (back channel) server insists upon.
4729 */
4730static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
4731 struct nfs4_session *session)
4732{
4733 int ret = 0;
4734
4735 ret |= _verify_fore_channel_attr(headerpadsz);
4736 ret |= _verify_fore_channel_attr(max_resp_sz);
4737 ret |= _verify_fore_channel_attr(max_ops);
4738
4739 ret |= _verify_back_channel_attr(headerpadsz);
4740 ret |= _verify_back_channel_attr(max_rqst_sz);
4741 ret |= _verify_back_channel_attr(max_resp_sz);
4742 ret |= _verify_back_channel_attr(max_resp_sz_cached);
4743 ret |= _verify_back_channel_attr(max_ops);
4744 ret |= _verify_back_channel_attr(max_reqs);
4745
4746 return ret;
4747}
4748
Andy Adamsonfc931582009-04-01 09:22:31 -04004749static int _nfs4_proc_create_session(struct nfs_client *clp)
4750{
4751 struct nfs4_session *session = clp->cl_session;
4752 struct nfs41_create_session_args args = {
4753 .client = clp,
4754 .cb_program = NFS4_CALLBACK,
4755 };
4756 struct nfs41_create_session_res res = {
4757 .client = clp,
4758 };
4759 struct rpc_message msg = {
4760 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
4761 .rpc_argp = &args,
4762 .rpc_resp = &res,
4763 };
4764 int status;
4765
4766 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04004767 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04004768
4769 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4770
Andy Adamson8d353012009-04-01 09:22:32 -04004771 if (!status)
4772 /* Verify the session's negotiated channel_attrs values */
4773 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04004774 if (!status) {
4775 /* Increment the clientid slot sequence id */
4776 clp->cl_seqid++;
4777 }
4778
4779 return status;
4780}
4781
4782/*
4783 * Issues a CREATE_SESSION operation to the server.
4784 * It is the responsibility of the caller to verify the session is
4785 * expired before calling this routine.
4786 */
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004787int nfs4_proc_create_session(struct nfs_client *clp, int reset)
Andy Adamsonfc931582009-04-01 09:22:31 -04004788{
4789 int status;
4790 unsigned *ptr;
4791 struct nfs_fsinfo fsinfo;
4792 struct nfs4_session *session = clp->cl_session;
4793
4794 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
4795
4796 status = _nfs4_proc_create_session(clp);
4797 if (status)
4798 goto out;
4799
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004800 /* Init or reset the fore channel */
4801 if (reset)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004802 status = nfs4_reset_slot_tables(session);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004803 else
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004804 status = nfs4_init_slot_tables(session);
Andy Adamsonfc931582009-04-01 09:22:31 -04004805 dprintk("fore channel slot table initialization returned %d\n", status);
4806 if (status)
4807 goto out;
4808
4809 ptr = (unsigned *)&session->sess_id.data[0];
4810 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
4811 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
4812
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004813 if (reset)
4814 /* Lease time is aleady set */
4815 goto out;
4816
Andy Adamsonfc931582009-04-01 09:22:31 -04004817 /* Get the lease time */
4818 status = nfs4_proc_get_lease_time(clp, &fsinfo);
4819 if (status == 0) {
4820 /* Update lease time and schedule renewal */
4821 spin_lock(&clp->cl_lock);
4822 clp->cl_lease_time = fsinfo.lease_time * HZ;
4823 clp->cl_last_renewal = jiffies;
4824 clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
4825 spin_unlock(&clp->cl_lock);
4826
4827 nfs4_schedule_state_renewal(clp);
4828 }
4829out:
4830 dprintk("<-- %s\n", __func__);
4831 return status;
4832}
4833
Andy Adamson0f3e66c2009-04-01 09:22:34 -04004834/*
4835 * Issue the over-the-wire RPC DESTROY_SESSION.
4836 * The caller must serialize access to this routine.
4837 */
4838int nfs4_proc_destroy_session(struct nfs4_session *session)
4839{
4840 int status = 0;
4841 struct rpc_message msg;
4842
4843 dprintk("--> nfs4_proc_destroy_session\n");
4844
4845 /* session is still being setup */
4846 if (session->clp->cl_cons_state != NFS_CS_READY)
4847 return status;
4848
4849 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
4850 msg.rpc_argp = session;
4851 msg.rpc_resp = NULL;
4852 msg.rpc_cred = NULL;
4853 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4854
4855 if (status)
4856 printk(KERN_WARNING
4857 "Got error %d from the server on DESTROY_SESSION. "
4858 "Session has been destroyed regardless...\n", status);
4859
4860 dprintk("<-- nfs4_proc_destroy_session\n");
4861 return status;
4862}
4863
Trond Myklebustfccba802009-07-21 16:48:07 -04004864int nfs4_init_session(struct nfs_server *server)
4865{
4866 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05004867 struct nfs4_session *session;
Trond Myklebustfccba802009-07-21 16:48:07 -04004868 int ret;
4869
4870 if (!nfs4_has_session(clp))
4871 return 0;
4872
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05004873 session = clp->cl_session;
4874 session->fc_attrs.max_rqst_sz = server->wsize + nfs41_maxwrite_overhead;
4875 session->fc_attrs.max_resp_sz = server->rsize + nfs41_maxread_overhead;
4876
Trond Myklebustfccba802009-07-21 16:48:07 -04004877 ret = nfs4_recover_expired_lease(server);
4878 if (!ret)
4879 ret = nfs4_check_client_ready(clp);
4880 return ret;
4881}
4882
Andy Adamsonfc01cea2009-04-01 09:22:36 -04004883/*
4884 * Renew the cl_session lease.
4885 */
4886static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
4887{
4888 struct nfs4_sequence_args args;
4889 struct nfs4_sequence_res res;
4890
4891 struct rpc_message msg = {
4892 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
4893 .rpc_argp = &args,
4894 .rpc_resp = &res,
4895 .rpc_cred = cred,
4896 };
4897
4898 args.sa_cache_this = 0;
4899
4900 return nfs4_call_sync_sequence(clp, clp->cl_rpcclient, &msg, &args,
4901 &res, 0);
4902}
4903
4904void nfs41_sequence_call_done(struct rpc_task *task, void *data)
4905{
4906 struct nfs_client *clp = (struct nfs_client *)data;
4907
4908 nfs41_sequence_done(clp, task->tk_msg.rpc_resp, task->tk_status);
4909
4910 if (task->tk_status < 0) {
4911 dprintk("%s ERROR %d\n", __func__, task->tk_status);
4912
4913 if (_nfs4_async_handle_error(task, NULL, clp, NULL)
4914 == -EAGAIN) {
Andy Adamsone608e792009-12-04 15:55:29 -05004915 nfs4_restart_rpc(task, clp, task->tk_msg.rpc_resp);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04004916 return;
4917 }
4918 }
4919 nfs41_sequence_free_slot(clp, task->tk_msg.rpc_resp);
4920 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
4921
Andy Adamsonfc01cea2009-04-01 09:22:36 -04004922 kfree(task->tk_msg.rpc_argp);
4923 kfree(task->tk_msg.rpc_resp);
4924
4925 dprintk("<-- %s\n", __func__);
4926}
4927
4928static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
4929{
4930 struct nfs_client *clp;
4931 struct nfs4_sequence_args *args;
4932 struct nfs4_sequence_res *res;
4933
4934 clp = (struct nfs_client *)data;
4935 args = task->tk_msg.rpc_argp;
4936 res = task->tk_msg.rpc_resp;
4937
4938 if (nfs4_setup_sequence(clp, args, res, 0, task))
4939 return;
4940 rpc_call_start(task);
4941}
4942
4943static const struct rpc_call_ops nfs41_sequence_ops = {
4944 .rpc_call_done = nfs41_sequence_call_done,
4945 .rpc_call_prepare = nfs41_sequence_prepare,
4946};
4947
4948static int nfs41_proc_async_sequence(struct nfs_client *clp,
4949 struct rpc_cred *cred)
4950{
4951 struct nfs4_sequence_args *args;
4952 struct nfs4_sequence_res *res;
4953 struct rpc_message msg = {
4954 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
4955 .rpc_cred = cred,
4956 };
4957
4958 args = kzalloc(sizeof(*args), GFP_KERNEL);
4959 if (!args)
4960 return -ENOMEM;
4961 res = kzalloc(sizeof(*res), GFP_KERNEL);
4962 if (!res) {
4963 kfree(args);
4964 return -ENOMEM;
4965 }
4966 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
4967 msg.rpc_argp = args;
4968 msg.rpc_resp = res;
4969
4970 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
4971 &nfs41_sequence_ops, (void *)clp);
4972}
4973
Ricardo Labiagafce5c832009-12-05 16:08:41 -05004974struct nfs4_reclaim_complete_data {
4975 struct nfs_client *clp;
4976 struct nfs41_reclaim_complete_args arg;
4977 struct nfs41_reclaim_complete_res res;
4978};
4979
4980static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
4981{
4982 struct nfs4_reclaim_complete_data *calldata = data;
4983
4984 if (nfs4_setup_sequence(calldata->clp, &calldata->arg.seq_args,
4985 &calldata->res.seq_res, 0, task))
4986 return;
4987
4988 rpc_call_start(task);
4989}
4990
4991static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
4992{
4993 struct nfs4_reclaim_complete_data *calldata = data;
4994 struct nfs_client *clp = calldata->clp;
4995 struct nfs4_sequence_res *res = &calldata->res.seq_res;
4996
4997 dprintk("--> %s\n", __func__);
4998 nfs41_sequence_done(clp, res, task->tk_status);
4999 switch (task->tk_status) {
5000 case 0:
5001 case -NFS4ERR_COMPLETE_ALREADY:
5002 break;
5003 case -NFS4ERR_BADSESSION:
5004 case -NFS4ERR_DEADSESSION:
5005 /*
5006 * Handle the session error, but do not retry the operation, as
5007 * we have no way of telling whether the clientid had to be
5008 * reset before we got our reply. If reset, a new wave of
5009 * reclaim operations will follow, containing their own reclaim
5010 * complete. We don't want our retry to get on the way of
5011 * recovery by incorrectly indicating to the server that we're
5012 * done reclaiming state since the process had to be restarted.
5013 */
5014 _nfs4_async_handle_error(task, NULL, clp, NULL);
5015 break;
5016 default:
5017 if (_nfs4_async_handle_error(
5018 task, NULL, clp, NULL) == -EAGAIN) {
5019 rpc_restart_call_prepare(task);
5020 return;
5021 }
5022 }
5023 nfs41_sequence_free_slot(clp, res);
5024
5025 dprintk("<-- %s\n", __func__);
5026}
5027
5028static void nfs4_free_reclaim_complete_data(void *data)
5029{
5030 struct nfs4_reclaim_complete_data *calldata = data;
5031
5032 kfree(calldata);
5033}
5034
5035static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5036 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5037 .rpc_call_done = nfs4_reclaim_complete_done,
5038 .rpc_release = nfs4_free_reclaim_complete_data,
5039};
5040
5041/*
5042 * Issue a global reclaim complete.
5043 */
5044static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5045{
5046 struct nfs4_reclaim_complete_data *calldata;
5047 struct rpc_task *task;
5048 struct rpc_message msg = {
5049 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5050 };
5051 struct rpc_task_setup task_setup_data = {
5052 .rpc_client = clp->cl_rpcclient,
5053 .rpc_message = &msg,
5054 .callback_ops = &nfs4_reclaim_complete_call_ops,
5055 .flags = RPC_TASK_ASYNC,
5056 };
5057 int status = -ENOMEM;
5058
5059 dprintk("--> %s\n", __func__);
5060 calldata = kzalloc(sizeof(*calldata), GFP_KERNEL);
5061 if (calldata == NULL)
5062 goto out;
5063 calldata->clp = clp;
5064 calldata->arg.one_fs = 0;
5065 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
5066
5067 msg.rpc_argp = &calldata->arg;
5068 msg.rpc_resp = &calldata->res;
5069 task_setup_data.callback_data = calldata;
5070 task = rpc_run_task(&task_setup_data);
5071 if (IS_ERR(task))
5072 status = PTR_ERR(task);
5073 rpc_put_task(task);
5074out:
5075 dprintk("<-- %s status=%d\n", __func__, status);
5076 return status;
5077}
Andy Adamson557134a2009-04-01 09:21:53 -04005078#endif /* CONFIG_NFS_V4_1 */
5079
Andy Adamson591d71c2009-04-01 09:22:47 -04005080struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005081 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005082 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083 .recover_open = nfs4_open_reclaim,
5084 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04005085 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005086 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087};
5088
Andy Adamson591d71c2009-04-01 09:22:47 -04005089#if defined(CONFIG_NFS_V4_1)
5090struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
5091 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5092 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5093 .recover_open = nfs4_open_reclaim,
5094 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05005095 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005096 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005097 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04005098};
5099#endif /* CONFIG_NFS_V4_1 */
5100
5101struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005102 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005103 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104 .recover_open = nfs4_open_expired,
5105 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04005106 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005107 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108};
5109
Andy Adamson591d71c2009-04-01 09:22:47 -04005110#if defined(CONFIG_NFS_V4_1)
5111struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
5112 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5113 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5114 .recover_open = nfs4_open_expired,
5115 .recover_lock = nfs4_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05005116 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005117 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04005118};
5119#endif /* CONFIG_NFS_V4_1 */
5120
Benny Halevy29fba382009-04-01 09:22:44 -04005121struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
5122 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04005123 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005124 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04005125};
5126
5127#if defined(CONFIG_NFS_V4_1)
5128struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
5129 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04005130 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005131 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04005132};
5133#endif
5134
5135/*
5136 * Per minor version reboot and network partition recovery ops
5137 */
5138
Andy Adamson591d71c2009-04-01 09:22:47 -04005139struct nfs4_state_recovery_ops *nfs4_reboot_recovery_ops[] = {
5140 &nfs40_reboot_recovery_ops,
5141#if defined(CONFIG_NFS_V4_1)
5142 &nfs41_reboot_recovery_ops,
5143#endif
5144};
5145
5146struct nfs4_state_recovery_ops *nfs4_nograce_recovery_ops[] = {
5147 &nfs40_nograce_recovery_ops,
5148#if defined(CONFIG_NFS_V4_1)
5149 &nfs41_nograce_recovery_ops,
5150#endif
5151};
5152
Benny Halevy29fba382009-04-01 09:22:44 -04005153struct nfs4_state_maintenance_ops *nfs4_state_renewal_ops[] = {
5154 &nfs40_state_renewal_ops,
5155#if defined(CONFIG_NFS_V4_1)
5156 &nfs41_state_renewal_ops,
5157#endif
5158};
5159
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08005160static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005161 .permission = nfs_permission,
5162 .getattr = nfs_getattr,
5163 .setattr = nfs_setattr,
5164 .getxattr = nfs4_getxattr,
5165 .setxattr = nfs4_setxattr,
5166 .listxattr = nfs4_listxattr,
5167};
5168
David Howells509de812006-08-22 20:06:11 -04005169const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170 .version = 4, /* protocol version */
5171 .dentry_ops = &nfs4_dentry_operations,
5172 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005173 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174 .getroot = nfs4_proc_get_root,
5175 .getattr = nfs4_proc_getattr,
5176 .setattr = nfs4_proc_setattr,
David Howells2b3de442006-08-22 20:06:09 -04005177 .lookupfh = nfs4_proc_lookupfh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178 .lookup = nfs4_proc_lookup,
5179 .access = nfs4_proc_access,
5180 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181 .create = nfs4_proc_create,
5182 .remove = nfs4_proc_remove,
5183 .unlink_setup = nfs4_proc_unlink_setup,
5184 .unlink_done = nfs4_proc_unlink_done,
5185 .rename = nfs4_proc_rename,
5186 .link = nfs4_proc_link,
5187 .symlink = nfs4_proc_symlink,
5188 .mkdir = nfs4_proc_mkdir,
5189 .rmdir = nfs4_proc_remove,
5190 .readdir = nfs4_proc_readdir,
5191 .mknod = nfs4_proc_mknod,
5192 .statfs = nfs4_proc_statfs,
5193 .fsinfo = nfs4_proc_fsinfo,
5194 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04005195 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196 .decode_dirent = nfs4_decode_dirent,
5197 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05005198 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005200 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005201 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005202 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005204 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04005205 .close_context = nfs4_close_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005206};
5207
5208/*
5209 * Local variables:
5210 * c-basic-offset: 8
5211 * End:
5212 */