blob: 637a6b476bd66652545f2d6ad9ff1c65e0bde4a6 [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);
408 return;
409 }
410out:
411 /* The session may be reset by one of the error handlers. */
412 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
413 nfs41_sequence_free_slot(clp, res);
414}
415
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400416/*
Benny Halevy510b8172009-04-01 09:22:14 -0400417 * nfs4_find_slot - efficiently look for a free slot
418 *
419 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
420 * If found, we mark the slot as used, update the highest_used_slotid,
421 * and respectively set up the sequence operation args.
422 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400423 *
424 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400425 */
426static u8
427nfs4_find_slot(struct nfs4_slot_table *tbl, struct rpc_task *task)
428{
429 int slotid;
430 u8 ret_id = NFS4_MAX_SLOT_TABLE;
431 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
432
Benny Halevy510b8172009-04-01 09:22:14 -0400433 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
434 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
435 tbl->max_slots);
436 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
437 if (slotid >= tbl->max_slots)
438 goto out;
439 __set_bit(slotid, tbl->used_slots);
440 if (slotid > tbl->highest_used_slotid)
441 tbl->highest_used_slotid = slotid;
442 ret_id = slotid;
443out:
444 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
445 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400446 return ret_id;
447}
448
Andy Adamsonce5039c2009-04-01 09:22:13 -0400449static int nfs41_setup_sequence(struct nfs4_session *session,
450 struct nfs4_sequence_args *args,
451 struct nfs4_sequence_res *res,
452 int cache_reply,
453 struct rpc_task *task)
454{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400455 struct nfs4_slot *slot;
456 struct nfs4_slot_table *tbl;
457 u8 slotid;
458
459 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400460 /* slot already allocated? */
461 if (res->sr_slotid != NFS4_MAX_SLOT_TABLE)
462 return 0;
463
464 memset(res, 0, sizeof(*res));
465 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400466 tbl = &session->fc_slot_table;
467
468 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson0b9e2d42009-12-04 16:02:14 -0500469 if (test_bit(NFS4CLNT_SESSION_DRAINING, &session->clp->cl_state)) {
Andy Adamsonea028ac2009-12-04 15:55:38 -0500470 /*
471 * The state manager will wait until the slot table is empty.
472 * Schedule the reset thread
473 */
474 dprintk("%s Schedule Session Reset\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500475 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
476 nfs4_schedule_state_manager(session->clp);
Andy Adamsonb069d942009-04-01 09:22:43 -0400477 spin_unlock(&tbl->slot_tbl_lock);
Andy Adamson05f0d232009-12-04 15:55:37 -0500478 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400479 }
480
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400481 slotid = nfs4_find_slot(tbl, task);
482 if (slotid == NFS4_MAX_SLOT_TABLE) {
483 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
484 spin_unlock(&tbl->slot_tbl_lock);
485 dprintk("<-- %s: no free slots\n", __func__);
486 return -EAGAIN;
487 }
488 spin_unlock(&tbl->slot_tbl_lock);
489
490 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400491 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400492 args->sa_slotid = slotid;
493 args->sa_cache_this = cache_reply;
494
495 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
496
Benny Halevy99fe60d2009-04-01 09:22:29 -0400497 res->sr_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400498 res->sr_slotid = slotid;
499 res->sr_renewal_time = jiffies;
500 /*
501 * sr_status is only set in decode_sequence, and so will remain
502 * set to 1 if an rpc level failure occurs.
503 */
504 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400505 return 0;
506}
507
508int nfs4_setup_sequence(struct nfs_client *clp,
509 struct nfs4_sequence_args *args,
510 struct nfs4_sequence_res *res,
511 int cache_reply,
512 struct rpc_task *task)
513{
514 int ret = 0;
515
516 dprintk("--> %s clp %p session %p sr_slotid %d\n",
517 __func__, clp, clp->cl_session, res->sr_slotid);
518
519 if (!nfs4_has_session(clp))
520 goto out;
521 ret = nfs41_setup_sequence(clp->cl_session, args, res, cache_reply,
522 task);
Andy Adamson2628edd2009-12-04 15:55:35 -0500523 if (ret && ret != -EAGAIN) {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400524 /* terminate rpc task */
525 task->tk_status = ret;
526 task->tk_action = NULL;
527 }
528out:
529 dprintk("<-- %s status=%d\n", __func__, ret);
530 return ret;
531}
532
533struct nfs41_call_sync_data {
534 struct nfs_client *clp;
535 struct nfs4_sequence_args *seq_args;
536 struct nfs4_sequence_res *seq_res;
537 int cache_reply;
538};
539
540static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
541{
542 struct nfs41_call_sync_data *data = calldata;
543
544 dprintk("--> %s data->clp->cl_session %p\n", __func__,
545 data->clp->cl_session);
546 if (nfs4_setup_sequence(data->clp, data->seq_args,
547 data->seq_res, data->cache_reply, task))
548 return;
549 rpc_call_start(task);
550}
551
Andy Adamson69ab40c2009-04-01 09:22:19 -0400552static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
553{
554 struct nfs41_call_sync_data *data = calldata;
555
556 nfs41_sequence_done(data->clp, data->seq_res, task->tk_status);
557 nfs41_sequence_free_slot(data->clp, data->seq_res);
558}
559
Andy Adamsonce5039c2009-04-01 09:22:13 -0400560struct rpc_call_ops nfs41_call_sync_ops = {
561 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400562 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400563};
564
565static int nfs4_call_sync_sequence(struct nfs_client *clp,
566 struct rpc_clnt *clnt,
567 struct rpc_message *msg,
568 struct nfs4_sequence_args *args,
569 struct nfs4_sequence_res *res,
570 int cache_reply)
571{
572 int ret;
573 struct rpc_task *task;
574 struct nfs41_call_sync_data data = {
575 .clp = clp,
576 .seq_args = args,
577 .seq_res = res,
578 .cache_reply = cache_reply,
579 };
580 struct rpc_task_setup task_setup = {
581 .rpc_client = clnt,
582 .rpc_message = msg,
583 .callback_ops = &nfs41_call_sync_ops,
584 .callback_data = &data
585 };
586
587 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
588 task = rpc_run_task(&task_setup);
589 if (IS_ERR(task))
590 ret = PTR_ERR(task);
591 else {
592 ret = task->tk_status;
593 rpc_put_task(task);
594 }
595 return ret;
596}
597
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400598int _nfs4_call_sync_session(struct nfs_server *server,
599 struct rpc_message *msg,
600 struct nfs4_sequence_args *args,
601 struct nfs4_sequence_res *res,
602 int cache_reply)
603{
Andy Adamsonce5039c2009-04-01 09:22:13 -0400604 return nfs4_call_sync_sequence(server->nfs_client, server->client,
605 msg, args, res, cache_reply);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400606}
607
608#endif /* CONFIG_NFS_V4_1 */
609
610int _nfs4_call_sync(struct nfs_server *server,
611 struct rpc_message *msg,
612 struct nfs4_sequence_args *args,
613 struct nfs4_sequence_res *res,
614 int cache_reply)
615{
Benny Halevyf3752972009-04-01 09:22:04 -0400616 args->sa_session = res->sr_session = NULL;
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400617 return rpc_call_sync(server->client, msg, 0);
618}
619
620#define nfs4_call_sync(server, msg, args, res, cache_reply) \
621 (server)->nfs_client->cl_call_sync((server), (msg), &(args)->seq_args, \
622 &(res)->seq_res, (cache_reply))
623
Andy Adamsonb0df8062009-04-01 09:22:18 -0400624static void nfs4_sequence_done(const struct nfs_server *server,
625 struct nfs4_sequence_res *res, int rpc_status)
626{
627#ifdef CONFIG_NFS_V4_1
628 if (nfs4_has_session(server->nfs_client))
629 nfs41_sequence_done(server->nfs_client, res, rpc_status);
630#endif /* CONFIG_NFS_V4_1 */
631}
632
Andy Adamsone608e792009-12-04 15:55:29 -0500633void nfs4_restart_rpc(struct rpc_task *task, const struct nfs_client *clp,
634 struct nfs4_sequence_res *res)
635{
636#ifdef CONFIG_NFS_V4_1
637 if (nfs4_has_session(clp)) {
638 nfs41_sequence_free_slot(clp, res);
639 rpc_restart_call_prepare(task);
640 return;
641 }
642#endif /* CONFIG_NFS_V4_1 */
643 rpc_restart_call(task);
644}
645
Andy Adamsonb0df8062009-04-01 09:22:18 -0400646/* no restart, therefore free slot here */
647static void nfs4_sequence_done_free_slot(const struct nfs_server *server,
648 struct nfs4_sequence_res *res,
649 int rpc_status)
650{
651 nfs4_sequence_done(server, res, rpc_status);
652 nfs4_sequence_free_slot(server->nfs_client, res);
653}
654
Trond Myklebust38478b22006-05-25 01:40:57 -0400655static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
Trond Myklebust38478b22006-05-25 01:40:57 -0400657 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Trond Myklebust38478b22006-05-25 01:40:57 -0400659 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400660 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
661 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400662 nfs_force_lookup_revalidate(dir);
Trond Myklebust40d24702007-10-08 09:24:22 -0400663 nfsi->change_attr = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400664 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665}
666
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100667struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400668 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100669 struct nfs_openargs o_arg;
670 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100671 struct nfs_open_confirmargs c_arg;
672 struct nfs_open_confirmres c_res;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100673 struct nfs_fattr f_attr;
674 struct nfs_fattr dir_attr;
Trond Myklebustad389da2007-06-05 12:30:00 -0400675 struct path path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100676 struct dentry *dir;
677 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400678 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100679 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100680 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400681 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100682 int rpc_status;
683 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100684};
685
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400686
687static void nfs4_init_opendata_res(struct nfs4_opendata *p)
688{
689 p->o_res.f_attr = &p->f_attr;
690 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400691 p->o_res.seqid = p->o_arg.seqid;
692 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400693 p->o_res.server = p->o_arg.server;
694 nfs_fattr_init(&p->f_attr);
695 nfs_fattr_init(&p->dir_attr);
Benny Halevy578e4582009-06-18 22:01:23 -0400696 p->o_res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400697}
698
Trond Myklebustad389da2007-06-05 12:30:00 -0400699static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500700 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100701 const struct iattr *attrs)
702{
Trond Myklebustad389da2007-06-05 12:30:00 -0400703 struct dentry *parent = dget_parent(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100704 struct inode *dir = parent->d_inode;
705 struct nfs_server *server = NFS_SERVER(dir);
706 struct nfs4_opendata *p;
707
708 p = kzalloc(sizeof(*p), GFP_KERNEL);
709 if (p == NULL)
710 goto err;
711 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
712 if (p->o_arg.seqid == NULL)
713 goto err_free;
Trond Myklebustad389da2007-06-05 12:30:00 -0400714 p->path.mnt = mntget(path->mnt);
715 p->path.dentry = dget(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100716 p->dir = parent;
717 p->owner = sp;
718 atomic_inc(&sp->so_count);
719 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500720 p->o_arg.open_flags = flags;
721 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400722 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400723 p->o_arg.id = sp->so_owner_id.id;
Trond Myklebustad389da2007-06-05 12:30:00 -0400724 p->o_arg.name = &p->path.dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100725 p->o_arg.server = server;
726 p->o_arg.bitmask = server->attr_bitmask;
727 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100728 if (flags & O_EXCL) {
729 u32 *s = (u32 *) p->o_arg.u.verifier.data;
730 s[0] = jiffies;
731 s[1] = current->pid;
732 } else if (flags & O_CREAT) {
733 p->o_arg.u.attrs = &p->attrs;
734 memcpy(&p->attrs, attrs, sizeof(p->attrs));
735 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100736 p->c_arg.fh = &p->o_res.fh;
737 p->c_arg.stateid = &p->o_res.stateid;
738 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400739 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400740 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100741 return p;
742err_free:
743 kfree(p);
744err:
745 dput(parent);
746 return NULL;
747}
748
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400749static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100750{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400751 struct nfs4_opendata *p = container_of(kref,
752 struct nfs4_opendata, kref);
753
754 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400755 if (p->state != NULL)
756 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400757 nfs4_put_state_owner(p->owner);
758 dput(p->dir);
Jan Blunck31f31db12008-05-02 13:42:45 -0700759 path_put(&p->path);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400760 kfree(p);
761}
762
763static void nfs4_opendata_put(struct nfs4_opendata *p)
764{
765 if (p != NULL)
766 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100767}
768
Trond Myklebust06f814a2006-01-03 09:55:07 +0100769static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
770{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100771 int ret;
772
Trond Myklebust06f814a2006-01-03 09:55:07 +0100773 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100774 return ret;
775}
776
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500777static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400778{
779 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500780
781 if (open_mode & O_EXCL)
782 goto out;
783 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400784 case FMODE_READ:
785 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400786 break;
787 case FMODE_WRITE:
788 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400789 break;
790 case FMODE_READ|FMODE_WRITE:
791 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0;
792 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500793out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400794 return ret;
795}
796
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500797static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400798{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500799 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400800 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500801 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400802 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500803 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400804 return 1;
805}
806
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500807static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100808{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500809 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100810 case FMODE_WRITE:
811 state->n_wronly++;
812 break;
813 case FMODE_READ:
814 state->n_rdonly++;
815 break;
816 case FMODE_READ|FMODE_WRITE:
817 state->n_rdwr++;
818 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500819 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100820}
821
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500822static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400823{
824 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
825 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
826 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500827 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400828 case FMODE_READ:
829 set_bit(NFS_O_RDONLY_STATE, &state->flags);
830 break;
831 case FMODE_WRITE:
832 set_bit(NFS_O_WRONLY_STATE, &state->flags);
833 break;
834 case FMODE_READ|FMODE_WRITE:
835 set_bit(NFS_O_RDWR_STATE, &state->flags);
836 }
837}
838
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500839static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400840{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400841 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500842 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400843 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400844}
845
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500846static 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 -0700847{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400848 /*
849 * Protect the call to nfs4_state_set_mode_locked and
850 * serialise the stateid update
851 */
852 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400853 if (deleg_stateid != NULL) {
854 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
855 set_bit(NFS_DELEGATED_STATE, &state->flags);
856 }
857 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500858 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400859 write_sequnlock(&state->seqlock);
860 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500861 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700862 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863}
864
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500865static 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 -0500866{
867 struct nfs_inode *nfsi = NFS_I(state->inode);
868 struct nfs_delegation *deleg_cur;
869 int ret = 0;
870
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500871 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500872
873 rcu_read_lock();
874 deleg_cur = rcu_dereference(nfsi->delegation);
875 if (deleg_cur == NULL)
876 goto no_delegation;
877
878 spin_lock(&deleg_cur->lock);
879 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500880 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500881 goto no_delegation_unlock;
882
883 if (delegation == NULL)
884 delegation = &deleg_cur->stateid;
885 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
886 goto no_delegation_unlock;
887
Trond Myklebustb7391f42008-12-23 15:21:52 -0500888 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500889 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500890 ret = 1;
891no_delegation_unlock:
892 spin_unlock(&deleg_cur->lock);
893no_delegation:
894 rcu_read_unlock();
895
896 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500897 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500898 ret = 1;
899 }
900
901 return ret;
902}
903
904
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500905static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400906{
907 struct nfs_delegation *delegation;
908
909 rcu_read_lock();
910 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500911 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -0400912 rcu_read_unlock();
913 return;
914 }
915 rcu_read_unlock();
916 nfs_inode_return_delegation(inode);
917}
918
Trond Myklebust6ee41262007-07-08 14:11:36 -0400919static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400920{
921 struct nfs4_state *state = opendata->state;
922 struct nfs_inode *nfsi = NFS_I(state->inode);
923 struct nfs_delegation *delegation;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500924 int open_mode = opendata->o_arg.open_flags & O_EXCL;
925 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400926 nfs4_stateid stateid;
927 int ret = -EAGAIN;
928
Trond Myklebustaac00a82007-07-05 19:02:21 -0400929 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500930 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400931 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500932 if (can_open_cached(state, fmode, open_mode)) {
933 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400934 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400935 goto out_return_state;
936 }
937 spin_unlock(&state->owner->so_lock);
938 }
Trond Myklebust34310432008-12-23 15:21:38 -0500939 rcu_read_lock();
940 delegation = rcu_dereference(nfsi->delegation);
941 if (delegation == NULL ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500942 !can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -0500943 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -0400944 break;
Trond Myklebust34310432008-12-23 15:21:38 -0500945 }
Trond Myklebustaac00a82007-07-05 19:02:21 -0400946 /* Save the delegation */
947 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
948 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -0400949 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400950 if (ret != 0)
951 goto out;
952 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -0500953
954 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500955 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -0500956 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400957 }
Trond Myklebustaac00a82007-07-05 19:02:21 -0400958out:
959 return ERR_PTR(ret);
960out_return_state:
961 atomic_inc(&state->count);
962 return state;
963}
964
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100965static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
966{
967 struct inode *inode;
968 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -0400969 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400970 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100971
Trond Myklebustaac00a82007-07-05 19:02:21 -0400972 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400973 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400974 goto out;
975 }
976
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400977 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100978 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400979 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100980 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400981 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -0500982 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400983 goto err;
984 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100985 state = nfs4_get_open_state(inode, data->owner);
986 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400987 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -0400988 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -0400989 int delegation_flags = 0;
990
Trond Myklebust003707c2007-07-05 18:07:55 -0400991 rcu_read_lock();
992 delegation = rcu_dereference(NFS_I(inode)->delegation);
993 if (delegation)
994 delegation_flags = delegation->flags;
995 rcu_read_unlock();
Trond Myklebust15c831b2008-12-23 15:21:39 -0500996 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -0400997 nfs_inode_set_delegation(state->inode,
998 data->owner->so_cred,
999 &data->o_res);
1000 else
1001 nfs_inode_reclaim_delegation(state->inode,
1002 data->owner->so_cred,
1003 &data->o_res);
1004 }
Trond Myklebust34310432008-12-23 15:21:38 -05001005
1006 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001007 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001008 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001009out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001010 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001011err_put_inode:
1012 iput(inode);
1013err:
1014 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001015}
1016
Trond Myklebust864472e2006-01-03 09:55:15 +01001017static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1018{
1019 struct nfs_inode *nfsi = NFS_I(state->inode);
1020 struct nfs_open_context *ctx;
1021
1022 spin_lock(&state->inode->i_lock);
1023 list_for_each_entry(ctx, &nfsi->open_files, list) {
1024 if (ctx->state != state)
1025 continue;
1026 get_nfs_open_context(ctx);
1027 spin_unlock(&state->inode->i_lock);
1028 return ctx;
1029 }
1030 spin_unlock(&state->inode->i_lock);
1031 return ERR_PTR(-ENOENT);
1032}
1033
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001034static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1035{
1036 struct nfs4_opendata *opendata;
1037
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001038 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001039 if (opendata == NULL)
1040 return ERR_PTR(-ENOMEM);
1041 opendata->state = state;
1042 atomic_inc(&state->count);
1043 return opendata;
1044}
1045
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001046static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001047{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001048 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001049 int ret;
1050
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001051 opendata->o_arg.open_flags = 0;
1052 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001053 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1054 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1055 nfs4_init_opendata_res(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001056 ret = _nfs4_proc_open(opendata);
1057 if (ret != 0)
1058 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001059 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001060 if (IS_ERR(newstate))
1061 return PTR_ERR(newstate);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001062 nfs4_close_state(&opendata->path, newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001063 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001064 return 0;
1065}
1066
1067static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1068{
Trond Myklebust864472e2006-01-03 09:55:15 +01001069 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001070 int ret;
1071
1072 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001073 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001074 smp_rmb();
1075 if (state->n_rdwr != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001076 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001077 if (ret != 0)
1078 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001079 if (newstate != state)
1080 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001081 }
1082 if (state->n_wronly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001083 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001084 if (ret != 0)
1085 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001086 if (newstate != state)
1087 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001088 }
1089 if (state->n_rdonly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001090 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001091 if (ret != 0)
1092 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001093 if (newstate != state)
1094 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001095 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001096 /*
1097 * We may have performed cached opens for all three recoveries.
1098 * Check if we need to update the current stateid.
1099 */
1100 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1101 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001102 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001103 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1104 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001105 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001106 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001107 return 0;
1108}
1109
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110/*
1111 * OPEN_RECLAIM:
1112 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001114static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001116 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001117 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001118 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 int status;
1120
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001121 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1122 if (IS_ERR(opendata))
1123 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001124 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1125 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001126 rcu_read_lock();
1127 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001128 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001129 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001130 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001131 opendata->o_arg.u.delegation_type = delegation_type;
1132 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001133 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 return status;
1135}
1136
Trond Myklebust539cd032007-06-05 11:46:42 -04001137static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138{
1139 struct nfs_server *server = NFS_SERVER(state->inode);
1140 struct nfs4_exception exception = { };
1141 int err;
1142 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001143 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001144 if (err != -NFS4ERR_DELAY)
1145 break;
1146 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 } while (exception.retry);
1148 return err;
1149}
1150
Trond Myklebust864472e2006-01-03 09:55:15 +01001151static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1152{
1153 struct nfs_open_context *ctx;
1154 int ret;
1155
1156 ctx = nfs4_state_find_open_context(state);
1157 if (IS_ERR(ctx))
1158 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001159 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001160 put_nfs_open_context(ctx);
1161 return ret;
1162}
1163
Trond Myklebust13437e12007-07-06 15:10:43 -04001164static 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 -07001165{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001166 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001167 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001169 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1170 if (IS_ERR(opendata))
1171 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001172 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -04001173 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001174 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +01001175 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001176 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001177 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178}
1179
Trond Myklebust13437e12007-07-06 15:10:43 -04001180int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181{
1182 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001183 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 int err;
1185 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001186 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 switch (err) {
1188 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001189 case -ENOENT:
1190 case -ESTALE:
1191 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 case -NFS4ERR_STALE_CLIENTID:
1193 case -NFS4ERR_STALE_STATEID:
1194 case -NFS4ERR_EXPIRED:
1195 /* Don't recall a delegation if it was lost */
David Howells7539bba2006-08-22 20:06:09 -04001196 nfs4_schedule_state_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001197 goto out;
1198 case -ERESTARTSYS:
1199 /*
1200 * The show must go on: exit, but mark the
1201 * stateid as needing recovery.
1202 */
1203 case -NFS4ERR_ADMIN_REVOKED:
1204 case -NFS4ERR_BAD_STATEID:
1205 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
1206 case -ENOMEM:
1207 err = 0;
1208 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 }
1210 err = nfs4_handle_exception(server, err, &exception);
1211 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001212out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 return err;
1214}
1215
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001216static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1217{
1218 struct nfs4_opendata *data = calldata;
1219
1220 data->rpc_status = task->tk_status;
1221 if (RPC_ASSASSINATED(task))
1222 return;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001223 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001224 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1225 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -05001226 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001227 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001228 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001229 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001230}
1231
1232static void nfs4_open_confirm_release(void *calldata)
1233{
1234 struct nfs4_opendata *data = calldata;
1235 struct nfs4_state *state = NULL;
1236
1237 /* If this request hasn't been cancelled, do nothing */
1238 if (data->cancelled == 0)
1239 goto out_free;
1240 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001241 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001242 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001243 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001244 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001245 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001246out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001247 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001248}
1249
1250static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001251 .rpc_call_done = nfs4_open_confirm_done,
1252 .rpc_release = nfs4_open_confirm_release,
1253};
1254
1255/*
1256 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1257 */
1258static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1259{
1260 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1261 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001262 struct rpc_message msg = {
1263 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1264 .rpc_argp = &data->c_arg,
1265 .rpc_resp = &data->c_res,
1266 .rpc_cred = data->owner->so_cred,
1267 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001268 struct rpc_task_setup task_setup_data = {
1269 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001270 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001271 .callback_ops = &nfs4_open_confirm_ops,
1272 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001273 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001274 .flags = RPC_TASK_ASYNC,
1275 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 int status;
1277
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001278 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001279 data->rpc_done = 0;
1280 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001281 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001282 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001283 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001284 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001285 status = nfs4_wait_for_completion_rpc_task(task);
1286 if (status != 0) {
1287 data->cancelled = 1;
1288 smp_wmb();
1289 } else
1290 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001291 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 return status;
1293}
1294
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001295static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001297 struct nfs4_opendata *data = calldata;
1298 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001299
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001300 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1301 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001302 /*
1303 * Check if we still need to send an OPEN call, or if we can use
1304 * a delegation instead.
1305 */
1306 if (data->state != NULL) {
1307 struct nfs_delegation *delegation;
1308
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001309 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001310 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001311 rcu_read_lock();
1312 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1313 if (delegation != NULL &&
Trond Myklebust15c831b2008-12-23 15:21:39 -05001314 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001315 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001316 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001317 }
1318 rcu_read_unlock();
1319 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001320 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001321 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001322 data->o_arg.clientid = sp->so_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001323 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001324 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001325 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1326 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001327 data->timestamp = jiffies;
Andy Adamsond8985282009-04-01 09:22:21 -04001328 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
1329 &data->o_arg.seq_args,
1330 &data->o_res.seq_res, 1, task))
1331 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001332 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001333 return;
1334out_no_action:
1335 task->tk_action = NULL;
1336
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001337}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001339static void nfs4_open_done(struct rpc_task *task, void *calldata)
1340{
1341 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001343 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001344
1345 nfs4_sequence_done_free_slot(data->o_arg.server, &data->o_res.seq_res,
1346 task->tk_status);
1347
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001348 if (RPC_ASSASSINATED(task))
1349 return;
1350 if (task->tk_status == 0) {
1351 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001352 case S_IFREG:
1353 break;
1354 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001355 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001356 break;
1357 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001358 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001359 break;
1360 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001361 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001362 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001363 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001364 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1365 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001366 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001367 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001368}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001369
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001370static void nfs4_open_release(void *calldata)
1371{
1372 struct nfs4_opendata *data = calldata;
1373 struct nfs4_state *state = NULL;
1374
1375 /* If this request hasn't been cancelled, do nothing */
1376 if (data->cancelled == 0)
1377 goto out_free;
1378 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001379 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001380 goto out_free;
1381 /* In case we need an open_confirm, no cleanup! */
1382 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1383 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001384 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001385 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001386 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001387out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001388 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001389}
1390
1391static const struct rpc_call_ops nfs4_open_ops = {
1392 .rpc_call_prepare = nfs4_open_prepare,
1393 .rpc_call_done = nfs4_open_done,
1394 .rpc_release = nfs4_open_release,
1395};
1396
1397/*
1398 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1399 */
1400static int _nfs4_proc_open(struct nfs4_opendata *data)
1401{
1402 struct inode *dir = data->dir->d_inode;
1403 struct nfs_server *server = NFS_SERVER(dir);
1404 struct nfs_openargs *o_arg = &data->o_arg;
1405 struct nfs_openres *o_res = &data->o_res;
1406 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001407 struct rpc_message msg = {
1408 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1409 .rpc_argp = o_arg,
1410 .rpc_resp = o_res,
1411 .rpc_cred = data->owner->so_cred,
1412 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001413 struct rpc_task_setup task_setup_data = {
1414 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001415 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001416 .callback_ops = &nfs4_open_ops,
1417 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001418 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001419 .flags = RPC_TASK_ASYNC,
1420 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001421 int status;
1422
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001423 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001424 data->rpc_done = 0;
1425 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001426 data->cancelled = 0;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001427 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001428 if (IS_ERR(task))
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001429 return PTR_ERR(task);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001430 status = nfs4_wait_for_completion_rpc_task(task);
1431 if (status != 0) {
1432 data->cancelled = 1;
1433 smp_wmb();
1434 } else
1435 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001436 rpc_put_task(task);
Trond Myklebust3e309912007-07-07 13:19:59 -04001437 if (status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001438 return status;
1439
Trond Myklebust99367812007-07-17 21:52:41 -04001440 if (o_res->fh.size == 0)
1441 _nfs4_proc_lookup(dir, o_arg->name, &o_res->fh, o_res->f_attr);
1442
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001443 if (o_arg->open_flags & O_CREAT) {
1444 update_changeattr(dir, &o_res->cinfo);
1445 nfs_post_op_update_inode(dir, o_res->dir_attr);
1446 } else
1447 nfs_refresh_inode(dir, o_res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001449 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001451 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 }
1453 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001454 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001455 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456}
1457
Trond Myklebust10afec92007-05-14 17:16:04 -04001458static int nfs4_recover_expired_lease(struct nfs_server *server)
Trond Myklebust58d97142006-01-03 09:55:24 +01001459{
David Howells7539bba2006-08-22 20:06:09 -04001460 struct nfs_client *clp = server->nfs_client;
Trond Myklebusta78cb572009-08-09 15:06:19 -04001461 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001462 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001463
Trond Myklebusta78cb572009-08-09 15:06:19 -04001464 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001465 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001466 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001467 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001468 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1469 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001470 break;
Trond Myklebust58d97142006-01-03 09:55:24 +01001471 nfs4_schedule_state_recovery(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001472 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001473 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001474 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001475}
1476
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477/*
1478 * OPEN_EXPIRED:
1479 * reclaim state on the server after a network partition.
1480 * Assumes caller holds the appropriate lock
1481 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001482static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001484 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001485 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001487 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1488 if (IS_ERR(opendata))
1489 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001490 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001491 if (ret == -ESTALE)
Trond Myklebust539cd032007-06-05 11:46:42 -04001492 d_drop(ctx->path.dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001493 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001494 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495}
1496
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001497static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001498{
Trond Myklebust539cd032007-06-05 11:46:42 -04001499 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001500 struct nfs4_exception exception = { };
1501 int err;
1502
1503 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001504 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001505 switch (err) {
1506 default:
1507 goto out;
1508 case -NFS4ERR_GRACE:
1509 case -NFS4ERR_DELAY:
1510 nfs4_handle_exception(server, err, &exception);
1511 err = 0;
1512 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001513 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001514out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001515 return err;
1516}
1517
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1519{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001521 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
Trond Myklebust864472e2006-01-03 09:55:15 +01001523 ctx = nfs4_state_find_open_context(state);
1524 if (IS_ERR(ctx))
1525 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001526 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001527 put_nfs_open_context(ctx);
1528 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529}
1530
1531/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001532 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1533 * fields corresponding to attributes that were used to store the verifier.
1534 * Make sure we clobber those fields in the later setattr call
1535 */
1536static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1537{
1538 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1539 !(sattr->ia_valid & ATTR_ATIME_SET))
1540 sattr->ia_valid |= ATTR_ATIME;
1541
1542 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1543 !(sattr->ia_valid & ATTR_MTIME_SET))
1544 sattr->ia_valid |= ATTR_MTIME;
1545}
1546
1547/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001548 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001550static 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 -07001551{
1552 struct nfs4_state_owner *sp;
1553 struct nfs4_state *state = NULL;
1554 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001555 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001556 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
1558 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 status = -ENOMEM;
1560 if (!(sp = nfs4_get_state_owner(server, cred))) {
1561 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1562 goto out_err;
1563 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001564 status = nfs4_recover_expired_lease(server);
1565 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001566 goto err_put_state_owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001567 if (path->dentry->d_inode != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001568 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001569 status = -ENOMEM;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001570 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001571 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001572 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
Trond Myklebustaac00a82007-07-05 19:02:21 -04001574 if (path->dentry->d_inode != NULL)
1575 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1576
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001577 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001579 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001581 if (opendata->o_arg.open_flags & O_EXCL)
1582 nfs4_exclusive_attrset(opendata, sattr);
1583
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001584 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001585 status = PTR_ERR(state);
1586 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001587 goto err_opendata_put;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001588 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 *res = state;
1591 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001592err_opendata_put:
1593 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001594err_put_state_owner:
1595 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 *res = NULL;
1598 return status;
1599}
1600
1601
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001602static 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 -07001603{
1604 struct nfs4_exception exception = { };
1605 struct nfs4_state *res;
1606 int status;
1607
1608 do {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001609 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 if (status == 0)
1611 break;
1612 /* NOTE: BAD_SEQID means the server and client disagree about the
1613 * book-keeping w.r.t. state-changing operations
1614 * (OPEN/CLOSE/LOCK/LOCKU...)
1615 * It is actually a sign of a bug on the client or on the server.
1616 *
1617 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001618 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 * have unhashed the old state_owner for us, and that we can
1620 * therefore safely retry using a new one. We should still warn
1621 * the user though...
1622 */
1623 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001624 printk(KERN_WARNING "NFS: v4 server %s "
1625 " returned a bad sequence-id error!\n",
1626 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 exception.retry = 1;
1628 continue;
1629 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001630 /*
1631 * BAD_STATEID on OPEN means that the server cancelled our
1632 * state before it received the OPEN_CONFIRM.
1633 * Recover by retrying the request as per the discussion
1634 * on Page 181 of RFC3530.
1635 */
1636 if (status == -NFS4ERR_BAD_STATEID) {
1637 exception.retry = 1;
1638 continue;
1639 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001640 if (status == -EAGAIN) {
1641 /* We must have found a delegation */
1642 exception.retry = 1;
1643 continue;
1644 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1646 status, &exception));
1647 } while (exception.retry);
1648 return res;
1649}
1650
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001651static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1652 struct nfs_fattr *fattr, struct iattr *sattr,
1653 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001655 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001657 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 .iap = sattr,
1659 .server = server,
1660 .bitmask = server->attr_bitmask,
1661 };
1662 struct nfs_setattrres res = {
1663 .fattr = fattr,
1664 .server = server,
1665 };
1666 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001667 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1668 .rpc_argp = &arg,
1669 .rpc_resp = &res,
1670 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001672 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001673 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
Trond Myklebust0e574af2005-10-27 22:12:38 -04001675 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001677 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1678 /* Use that stateid */
1679 } else if (state != NULL) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001680 nfs4_copy_stateid(&arg.stateid, state, current->files);
1681 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1683
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001684 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001685 if (status == 0 && state != NULL)
1686 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001687 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688}
1689
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001690static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1691 struct nfs_fattr *fattr, struct iattr *sattr,
1692 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001694 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 struct nfs4_exception exception = { };
1696 int err;
1697 do {
1698 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001699 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 &exception);
1701 } while (exception.retry);
1702 return err;
1703}
1704
1705struct nfs4_closedata {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001706 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 struct inode *inode;
1708 struct nfs4_state *state;
1709 struct nfs_closeargs arg;
1710 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001711 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001712 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713};
1714
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001715static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001716{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001717 struct nfs4_closedata *calldata = data;
1718 struct nfs4_state_owner *sp = calldata->state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -07001719
1720 nfs4_put_open_state(calldata->state);
1721 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001722 nfs4_put_state_owner(sp);
Jan Blunck31f31db12008-05-02 13:42:45 -07001723 path_put(&calldata->path);
Trond Myklebust95121352005-10-18 14:20:12 -07001724 kfree(calldata);
1725}
1726
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001727static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001729 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 struct nfs_server *server = NFS_SERVER(calldata->inode);
1732
Andy Adamson19ddab02009-04-01 09:22:20 -04001733 nfs4_sequence_done(server, &calldata->res.seq_res, task->tk_status);
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001734 if (RPC_ASSASSINATED(task))
1735 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 /* hmm. we are done with the inode, and in the process of freeing
1737 * the state_owner. we keep this around to process errors
1738 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 switch (task->tk_status) {
1740 case 0:
Trond Myklebust45328c32007-07-26 17:47:34 -04001741 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001742 renew_lease(server, calldata->timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 break;
1744 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001745 case -NFS4ERR_OLD_STATEID:
1746 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001748 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001749 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001751 if (nfs4_async_handle_error(task, server, state) == -EAGAIN) {
Andy Adamsone608e792009-12-04 15:55:29 -05001752 nfs4_restart_rpc(task, server->nfs_client,
1753 &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 return;
1755 }
1756 }
Andy Adamson19ddab02009-04-01 09:22:20 -04001757 nfs4_sequence_free_slot(server->nfs_client, &calldata->res.seq_res);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001758 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759}
1760
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001761static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001763 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001764 struct nfs4_state *state = calldata->state;
Trond Myklebust003707c2007-07-05 18:07:55 -04001765 int clear_rd, clear_wr, clear_rdwr;
Trond Myklebust95121352005-10-18 14:20:12 -07001766
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001767 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001768 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001769
Trond Myklebust003707c2007-07-05 18:07:55 -04001770 clear_rd = clear_wr = clear_rdwr = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001771 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001772 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001773 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001774 if (state->n_rdonly == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001775 clear_rd |= test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1776 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1777 }
1778 if (state->n_wronly == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001779 clear_wr |= test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1780 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1781 }
Trond Myklebuste7616922006-01-03 09:55:13 +01001782 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001783 spin_unlock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001784 if (!clear_rd && !clear_wr && !clear_rdwr) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001785 /* Note: exit _without_ calling nfs4_close_done */
1786 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001787 return;
1788 }
Trond Myklebust516a6af2005-10-27 22:12:41 -04001789 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust45328c32007-07-26 17:47:34 -04001790 if (test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001791 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001792 calldata->arg.fmode = FMODE_READ;
Trond Myklebust45328c32007-07-26 17:47:34 -04001793 } else if (test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001794 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001795 calldata->arg.fmode = FMODE_WRITE;
Trond Myklebust45328c32007-07-26 17:47:34 -04001796 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001797 calldata->timestamp = jiffies;
Andy Adamson19ddab02009-04-01 09:22:20 -04001798 if (nfs4_setup_sequence((NFS_SERVER(calldata->inode))->nfs_client,
1799 &calldata->arg.seq_args, &calldata->res.seq_res,
1800 1, task))
1801 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001802 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803}
1804
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001805static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001806 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001807 .rpc_call_done = nfs4_close_done,
1808 .rpc_release = nfs4_free_closedata,
1809};
1810
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811/*
1812 * It is possible for data to be read/written from a mem-mapped file
1813 * after the sys_close call (which hits the vfs layer as a flush).
1814 * This means that we can't safely call nfsv4 close on a file until
1815 * the inode is cleared. This in turn means that we are not good
1816 * NFSv4 citizens - we do not indicate to the server to update the file's
1817 * share state even when we are done with one of the three share
1818 * stateid's in the inode.
1819 *
1820 * NOTE: Caller must be holding the sp->so_owner semaphore!
1821 */
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001822int nfs4_do_close(struct path *path, struct nfs4_state *state, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001824 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04001826 struct nfs4_state_owner *sp = state->owner;
1827 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001828 struct rpc_message msg = {
1829 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1830 .rpc_cred = state->owner->so_cred,
1831 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001832 struct rpc_task_setup task_setup_data = {
1833 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001834 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001835 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05001836 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001837 .flags = RPC_TASK_ASYNC,
1838 };
Trond Myklebust95121352005-10-18 14:20:12 -07001839 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
Andy Adamson19ddab02009-04-01 09:22:20 -04001841 calldata = kzalloc(sizeof(*calldata), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07001843 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001844 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001846 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04001847 calldata->arg.stateid = &state->open_stateid;
Benny Halevy34dc1ad2009-04-01 09:22:52 -04001848 if (nfs4_has_session(server->nfs_client))
1849 memset(calldata->arg.stateid->data, 0, 4); /* clear seqid */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 /* Serialization for the sequence id */
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001851 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001852 if (calldata->arg.seqid == NULL)
1853 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001854 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04001855 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001856 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04001857 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001858 calldata->res.server = server;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04001859 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001860 calldata->path.mnt = mntget(path->mnt);
1861 calldata->path.dentry = dget(path->dentry);
Trond Myklebust95121352005-10-18 14:20:12 -07001862
Trond Myklebust5138fde2007-07-14 15:40:01 -04001863 msg.rpc_argp = &calldata->arg,
1864 msg.rpc_resp = &calldata->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001865 task_setup_data.callback_data = calldata;
1866 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001867 if (IS_ERR(task))
1868 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001869 status = 0;
1870 if (wait)
1871 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001872 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001873 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07001874out_free_calldata:
1875 kfree(calldata);
1876out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04001877 nfs4_put_open_state(state);
1878 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07001879 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880}
1881
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001882static 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 -07001883{
1884 struct file *filp;
Trond Myklebust1b45c462007-07-03 13:04:56 -04001885 int ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001886
Trond Myklebust1b45c462007-07-03 13:04:56 -04001887 /* If the open_intent is for execute, we have an extra check to make */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001888 if (fmode & FMODE_EXEC) {
Trond Myklebustaf22f942007-08-10 17:45:10 -04001889 ret = nfs_may_open(state->inode,
Trond Myklebust1b45c462007-07-03 13:04:56 -04001890 state->owner->so_cred,
1891 nd->intent.open.flags);
1892 if (ret < 0)
1893 goto out_close;
1894 }
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001895 filp = lookup_instantiate_filp(nd, path->dentry, NULL);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001896 if (!IS_ERR(filp)) {
1897 struct nfs_open_context *ctx;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04001898 ctx = nfs_file_open_context(filp);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001899 ctx->state = state;
Trond Myklebust95cf9592006-04-18 13:14:06 -04001900 return 0;
1901 }
Trond Myklebust1b45c462007-07-03 13:04:56 -04001902 ret = PTR_ERR(filp);
1903out_close:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001904 nfs4_close_sync(path, state, fmode & (FMODE_READ|FMODE_WRITE));
Trond Myklebust1b45c462007-07-03 13:04:56 -04001905 return ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001906}
1907
1908struct dentry *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1910{
Trond Myklebustad389da2007-06-05 12:30:00 -04001911 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08001912 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001913 .dentry = dentry,
1914 };
Jan Blunck4ac91372008-02-14 19:34:32 -08001915 struct dentry *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 struct iattr attr;
1917 struct rpc_cred *cred;
1918 struct nfs4_state *state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001919 struct dentry *res;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001920 fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
1922 if (nd->flags & LOOKUP_CREATE) {
1923 attr.ia_mode = nd->intent.open.create_mode;
1924 attr.ia_valid = ATTR_MODE;
1925 if (!IS_POSIXACL(dir))
Al Viroce3b0f82009-03-29 19:08:22 -04001926 attr.ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 } else {
1928 attr.ia_valid = 0;
1929 BUG_ON(nd->intent.open.flags & O_CREAT);
1930 }
1931
Trond Myklebust98a8e322008-03-12 12:25:28 -04001932 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 if (IS_ERR(cred))
Trond Myklebust02a913a2005-10-18 14:20:17 -07001934 return (struct dentry *)cred;
Trond Myklebust565277f2007-10-15 18:17:53 -04001935 parent = dentry->d_parent;
1936 /* Protect against concurrent sillydeletes */
1937 nfs_block_sillyrename(parent);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001938 state = nfs4_do_open(dir, &path, fmode, nd->intent.open.flags, &attr, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001940 if (IS_ERR(state)) {
Trond Myklebustd75340c2007-10-01 21:42:01 -04001941 if (PTR_ERR(state) == -ENOENT) {
Trond Myklebust02a913a2005-10-18 14:20:17 -07001942 d_add(dentry, NULL);
Trond Myklebustd75340c2007-10-01 21:42:01 -04001943 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1944 }
Trond Myklebust565277f2007-10-15 18:17:53 -04001945 nfs_unblock_sillyrename(parent);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001946 return (struct dentry *)state;
1947 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001948 res = d_add_unique(dentry, igrab(state->inode));
Trond Myklebust02a913a2005-10-18 14:20:17 -07001949 if (res != NULL)
Trond Myklebustdeee9362007-08-27 11:33:00 -04001950 path.dentry = res;
Trond Myklebustd75340c2007-10-01 21:42:01 -04001951 nfs_set_verifier(path.dentry, nfs_save_change_attribute(dir));
Trond Myklebust565277f2007-10-15 18:17:53 -04001952 nfs_unblock_sillyrename(parent);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001953 nfs4_intent_set_file(nd, &path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001954 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955}
1956
1957int
Trond Myklebust02a913a2005-10-18 14:20:17 -07001958nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959{
Trond Myklebustad389da2007-06-05 12:30:00 -04001960 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08001961 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001962 .dentry = dentry,
1963 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 struct rpc_cred *cred;
1965 struct nfs4_state *state;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001966 fmode_t fmode = openflags & (FMODE_READ | FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
Trond Myklebust98a8e322008-03-12 12:25:28 -04001968 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 if (IS_ERR(cred))
1970 return PTR_ERR(cred);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001971 state = nfs4_do_open(dir, &path, fmode, openflags, NULL, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001973 if (IS_ERR(state)) {
1974 switch (PTR_ERR(state)) {
1975 case -EPERM:
1976 case -EACCES:
1977 case -EDQUOT:
1978 case -ENOSPC:
1979 case -EROFS:
1980 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
1981 return 1;
Chuck Leverb87c0ad2006-10-19 23:28:42 -07001982 default:
1983 goto out_drop;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001984 }
Trond Myklebust02a913a2005-10-18 14:20:17 -07001985 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001986 if (state->inode == dentry->d_inode) {
Trond Myklebustd75340c2007-10-01 21:42:01 -04001987 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001988 nfs4_intent_set_file(nd, &path, state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 return 1;
1990 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001991 nfs4_close_sync(&path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001992out_drop:
1993 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 return 0;
1995}
1996
Trond Myklebust1185a552009-12-03 15:54:02 -05001997static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04001998{
1999 if (ctx->state == NULL)
2000 return;
2001 if (is_sync)
2002 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
2003 else
2004 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
2005}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006
2007static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2008{
Benny Halevy43652ad2009-04-01 09:21:54 -04002009 struct nfs4_server_caps_arg args = {
2010 .fhandle = fhandle,
2011 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 struct nfs4_server_caps_res res = {};
2013 struct rpc_message msg = {
2014 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002015 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 .rpc_resp = &res,
2017 };
2018 int status;
2019
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002020 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 if (status == 0) {
2022 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002023 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2024 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2025 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2026 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2027 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2029 server->caps |= NFS_CAP_ACLS;
2030 if (res.has_links != 0)
2031 server->caps |= NFS_CAP_HARDLINKS;
2032 if (res.has_symlinks != 0)
2033 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002034 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2035 server->caps |= NFS_CAP_FILEID;
2036 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2037 server->caps |= NFS_CAP_MODE;
2038 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2039 server->caps |= NFS_CAP_NLINK;
2040 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2041 server->caps |= NFS_CAP_OWNER;
2042 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2043 server->caps |= NFS_CAP_OWNER_GROUP;
2044 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2045 server->caps |= NFS_CAP_ATIME;
2046 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2047 server->caps |= NFS_CAP_CTIME;
2048 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2049 server->caps |= NFS_CAP_MTIME;
2050
Trond Myklebusta65318b2009-03-11 14:10:28 -04002051 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2052 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2053 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 server->acl_bitmask = res.acl_bitmask;
2055 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002056
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 return status;
2058}
2059
Trond Myklebust55a97592006-06-09 09:34:19 -04002060int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061{
2062 struct nfs4_exception exception = { };
2063 int err;
2064 do {
2065 err = nfs4_handle_exception(server,
2066 _nfs4_server_capabilities(server, fhandle),
2067 &exception);
2068 } while (exception.retry);
2069 return err;
2070}
2071
2072static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2073 struct nfs_fsinfo *info)
2074{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 struct nfs4_lookup_root_arg args = {
2076 .bitmask = nfs4_fattr_bitmap,
2077 };
2078 struct nfs4_lookup_res res = {
2079 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002080 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 .fh = fhandle,
2082 };
2083 struct rpc_message msg = {
2084 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2085 .rpc_argp = &args,
2086 .rpc_resp = &res,
2087 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002088
Trond Myklebust0e574af2005-10-27 22:12:38 -04002089 nfs_fattr_init(info->fattr);
Trond Myklebustfccba802009-07-21 16:48:07 -04002090 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091}
2092
2093static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2094 struct nfs_fsinfo *info)
2095{
2096 struct nfs4_exception exception = { };
2097 int err;
2098 do {
2099 err = nfs4_handle_exception(server,
2100 _nfs4_lookup_root(server, fhandle, info),
2101 &exception);
2102 } while (exception.retry);
2103 return err;
2104}
2105
David Howells54ceac42006-08-22 20:06:13 -04002106/*
2107 * get the file handle for the "/" directory on the server
2108 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002110 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 int status;
2113
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 status = nfs4_lookup_root(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 if (status == 0)
2116 status = nfs4_server_capabilities(server, fhandle);
2117 if (status == 0)
2118 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002119 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120}
2121
Manoj Naik6b97fd32006-06-09 09:34:29 -04002122/*
2123 * Get locations and (maybe) other attributes of a referral.
2124 * Note that we'll actually follow the referral later when
2125 * we detect fsid mismatch in inode revalidation
2126 */
Trond Myklebust56659e92007-07-17 21:52:39 -04002127static 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 -04002128{
2129 int status = -ENOMEM;
2130 struct page *page = NULL;
2131 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002132
2133 page = alloc_page(GFP_KERNEL);
2134 if (page == NULL)
2135 goto out;
2136 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2137 if (locations == NULL)
2138 goto out;
2139
Trond Myklebustc228fd32007-01-13 02:28:11 -05002140 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002141 if (status != 0)
2142 goto out;
2143 /* Make sure server returned a different fsid for the referral */
2144 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -07002145 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 -04002146 status = -EIO;
2147 goto out;
2148 }
2149
2150 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2151 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2152 if (!fattr->mode)
2153 fattr->mode = S_IFDIR;
2154 memset(fhandle, 0, sizeof(struct nfs_fh));
2155out:
2156 if (page)
2157 __free_page(page);
2158 if (locations)
2159 kfree(locations);
2160 return status;
2161}
2162
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2164{
2165 struct nfs4_getattr_arg args = {
2166 .fh = fhandle,
2167 .bitmask = server->attr_bitmask,
2168 };
2169 struct nfs4_getattr_res res = {
2170 .fattr = fattr,
2171 .server = server,
2172 };
2173 struct rpc_message msg = {
2174 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2175 .rpc_argp = &args,
2176 .rpc_resp = &res,
2177 };
2178
Trond Myklebust0e574af2005-10-27 22:12:38 -04002179 nfs_fattr_init(fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002180 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181}
2182
2183static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2184{
2185 struct nfs4_exception exception = { };
2186 int err;
2187 do {
2188 err = nfs4_handle_exception(server,
2189 _nfs4_proc_getattr(server, fhandle, fattr),
2190 &exception);
2191 } while (exception.retry);
2192 return err;
2193}
2194
2195/*
2196 * The file is not closed if it is opened due to the a request to change
2197 * the size of the file. The open call will not be needed once the
2198 * VFS layer lookup-intents are implemented.
2199 *
2200 * Close is called when the inode is destroyed.
2201 * If we haven't opened the file for O_WRONLY, we
2202 * need to in the size_change case to obtain a stateid.
2203 *
2204 * Got race?
2205 * Because OPEN is always done by name in nfsv4, it is
2206 * possible that we opened a different file by the same
2207 * name. We can recognize this race condition, but we
2208 * can't do anything about it besides returning an error.
2209 *
2210 * This will be fixed with VFS changes (lookup-intent).
2211 */
2212static int
2213nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2214 struct iattr *sattr)
2215{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002216 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002217 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002218 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 int status;
2220
Trond Myklebust0e574af2005-10-27 22:12:38 -04002221 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222
Trond Myklebustd5308382005-11-04 15:33:38 -05002223 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002224 if (sattr->ia_valid & ATTR_FILE) {
2225 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002226
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002227 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002228 if (ctx) {
2229 cred = ctx->cred;
2230 state = ctx->state;
2231 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002232 }
2233
2234 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002235 if (status == 0)
2236 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 return status;
2238}
2239
Trond Myklebust56659e92007-07-17 21:52:39 -04002240static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
2241 const struct qstr *name, struct nfs_fh *fhandle,
David Howells2b3de442006-08-22 20:06:09 -04002242 struct nfs_fattr *fattr)
2243{
2244 int status;
2245 struct nfs4_lookup_arg args = {
2246 .bitmask = server->attr_bitmask,
2247 .dir_fh = dirfh,
2248 .name = name,
2249 };
2250 struct nfs4_lookup_res res = {
2251 .server = server,
2252 .fattr = fattr,
2253 .fh = fhandle,
2254 };
2255 struct rpc_message msg = {
2256 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2257 .rpc_argp = &args,
2258 .rpc_resp = &res,
2259 };
2260
2261 nfs_fattr_init(fattr);
2262
2263 dprintk("NFS call lookupfh %s\n", name->name);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002264 status = nfs4_call_sync(server, &msg, &args, &res, 0);
David Howells2b3de442006-08-22 20:06:09 -04002265 dprintk("NFS reply lookupfh: %d\n", status);
David Howells2b3de442006-08-22 20:06:09 -04002266 return status;
2267}
2268
2269static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2270 struct qstr *name, struct nfs_fh *fhandle,
2271 struct nfs_fattr *fattr)
2272{
2273 struct nfs4_exception exception = { };
2274 int err;
2275 do {
Trond Myklebust4e56e082007-07-01 18:13:52 -04002276 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2277 /* FIXME: !!!! */
2278 if (err == -NFS4ERR_MOVED) {
2279 err = -EREMOTE;
2280 break;
2281 }
2282 err = nfs4_handle_exception(server, err, &exception);
David Howells2b3de442006-08-22 20:06:09 -04002283 } while (exception.retry);
2284 return err;
2285}
2286
Trond Myklebust56659e92007-07-17 21:52:39 -04002287static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2289{
Trond Myklebust4e56e082007-07-01 18:13:52 -04002290 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291
2292 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust4e56e082007-07-01 18:13:52 -04002293 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002294 if (status == -NFS4ERR_MOVED)
2295 status = nfs4_get_referral(dir, name, fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 dprintk("NFS reply lookup: %d\n", status);
2297 return status;
2298}
2299
2300static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2301{
2302 struct nfs4_exception exception = { };
2303 int err;
2304 do {
2305 err = nfs4_handle_exception(NFS_SERVER(dir),
2306 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2307 &exception);
2308 } while (exception.retry);
2309 return err;
2310}
2311
2312static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2313{
Trond Myklebust76b32992007-08-10 17:45:11 -04002314 struct nfs_server *server = NFS_SERVER(inode);
2315 struct nfs_fattr fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 struct nfs4_accessargs args = {
2317 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04002318 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002320 struct nfs4_accessres res = {
2321 .server = server,
2322 .fattr = &fattr,
2323 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 struct rpc_message msg = {
2325 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2326 .rpc_argp = &args,
2327 .rpc_resp = &res,
2328 .rpc_cred = entry->cred,
2329 };
2330 int mode = entry->mask;
2331 int status;
2332
2333 /*
2334 * Determine which access bits we want to ask for...
2335 */
2336 if (mode & MAY_READ)
2337 args.access |= NFS4_ACCESS_READ;
2338 if (S_ISDIR(inode->i_mode)) {
2339 if (mode & MAY_WRITE)
2340 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2341 if (mode & MAY_EXEC)
2342 args.access |= NFS4_ACCESS_LOOKUP;
2343 } else {
2344 if (mode & MAY_WRITE)
2345 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2346 if (mode & MAY_EXEC)
2347 args.access |= NFS4_ACCESS_EXECUTE;
2348 }
Trond Myklebust76b32992007-08-10 17:45:11 -04002349 nfs_fattr_init(&fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002350 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 if (!status) {
2352 entry->mask = 0;
2353 if (res.access & NFS4_ACCESS_READ)
2354 entry->mask |= MAY_READ;
2355 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2356 entry->mask |= MAY_WRITE;
2357 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2358 entry->mask |= MAY_EXEC;
Trond Myklebust76b32992007-08-10 17:45:11 -04002359 nfs_refresh_inode(inode, &fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 }
2361 return status;
2362}
2363
2364static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2365{
2366 struct nfs4_exception exception = { };
2367 int err;
2368 do {
2369 err = nfs4_handle_exception(NFS_SERVER(inode),
2370 _nfs4_proc_access(inode, entry),
2371 &exception);
2372 } while (exception.retry);
2373 return err;
2374}
2375
2376/*
2377 * TODO: For the time being, we don't try to get any attributes
2378 * along with any of the zero-copy operations READ, READDIR,
2379 * READLINK, WRITE.
2380 *
2381 * In the case of the first three, we want to put the GETATTR
2382 * after the read-type operation -- this is because it is hard
2383 * to predict the length of a GETATTR response in v4, and thus
2384 * align the READ data correctly. This means that the GETATTR
2385 * may end up partially falling into the page cache, and we should
2386 * shift it into the 'tail' of the xdr_buf before processing.
2387 * To do this efficiently, we need to know the total length
2388 * of data received, which doesn't seem to be available outside
2389 * of the RPC layer.
2390 *
2391 * In the case of WRITE, we also want to put the GETATTR after
2392 * the operation -- in this case because we want to make sure
2393 * we get the post-operation mtime and size. This means that
2394 * we can't use xdr_encode_pages() as written: we need a variant
2395 * of it which would leave room in the 'tail' iovec.
2396 *
2397 * Both of these changes to the XDR layer would in fact be quite
2398 * minor, but I decided to leave them for a subsequent patch.
2399 */
2400static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2401 unsigned int pgbase, unsigned int pglen)
2402{
2403 struct nfs4_readlink args = {
2404 .fh = NFS_FH(inode),
2405 .pgbase = pgbase,
2406 .pglen = pglen,
2407 .pages = &page,
2408 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002409 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 struct rpc_message msg = {
2411 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2412 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002413 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 };
2415
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002416 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417}
2418
2419static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2420 unsigned int pgbase, unsigned int pglen)
2421{
2422 struct nfs4_exception exception = { };
2423 int err;
2424 do {
2425 err = nfs4_handle_exception(NFS_SERVER(inode),
2426 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2427 &exception);
2428 } while (exception.retry);
2429 return err;
2430}
2431
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432/*
2433 * Got race?
2434 * We will need to arrange for the VFS layer to provide an atomic open.
2435 * Until then, this create/open method is prone to inefficiency and race
2436 * conditions due to the lookup, create, and open VFS calls from sys_open()
2437 * placed on the wire.
2438 *
2439 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2440 * The file will be opened again in the subsequent VFS open call
2441 * (nfs4_proc_file_open).
2442 *
2443 * The open for read will just hang around to be used by any process that
2444 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2445 */
2446
2447static int
2448nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebust02a913a2005-10-18 14:20:17 -07002449 int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450{
Trond Myklebustad389da2007-06-05 12:30:00 -04002451 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08002452 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04002453 .dentry = dentry,
2454 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 struct nfs4_state *state;
2456 struct rpc_cred *cred;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002457 fmode_t fmode = flags & (FMODE_READ | FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 int status = 0;
2459
Trond Myklebust98a8e322008-03-12 12:25:28 -04002460 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 if (IS_ERR(cred)) {
2462 status = PTR_ERR(cred);
2463 goto out;
2464 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002465 state = nfs4_do_open(dir, &path, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002466 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 if (IS_ERR(state)) {
2468 status = PTR_ERR(state);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002469 goto out_putcred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002471 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002472 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 if (flags & O_EXCL) {
2474 struct nfs_fattr fattr;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002475 status = nfs4_do_setattr(state->inode, cred, &fattr, sattr, state);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002476 if (status == 0)
Trond Myklebust65e43082005-08-16 11:49:44 -04002477 nfs_setattr_update_inode(state->inode, sattr);
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002478 nfs_post_op_update_inode(state->inode, &fattr);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002479 }
Trond Myklebustad389da2007-06-05 12:30:00 -04002480 if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002481 status = nfs4_intent_set_file(nd, &path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002482 else
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002483 nfs4_close_sync(&path, state, fmode);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002484out_putcred:
2485 put_rpccred(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486out:
2487 return status;
2488}
2489
2490static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2491{
Trond Myklebust16e42952005-10-27 22:12:44 -04002492 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002493 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002495 .name.len = name->len,
2496 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002497 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002499 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002500 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002501 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002503 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2504 .rpc_argp = &args,
2505 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 };
2507 int status;
2508
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002509 nfs_fattr_init(&res.dir_attr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002510 status = nfs4_call_sync(server, &msg, &args, &res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002511 if (status == 0) {
2512 update_changeattr(dir, &res.cinfo);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002513 nfs_post_op_update_inode(dir, &res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 return status;
2516}
2517
2518static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2519{
2520 struct nfs4_exception exception = { };
2521 int err;
2522 do {
2523 err = nfs4_handle_exception(NFS_SERVER(dir),
2524 _nfs4_proc_remove(dir, name),
2525 &exception);
2526 } while (exception.retry);
2527 return err;
2528}
2529
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002530static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002532 struct nfs_server *server = NFS_SERVER(dir);
2533 struct nfs_removeargs *args = msg->rpc_argp;
2534 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535
Trond Myklebusta65318b2009-03-11 14:10:28 -04002536 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002537 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539}
2540
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002541static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002543 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2544
Andy Adamson472cfbd2009-04-01 09:22:24 -04002545 nfs4_sequence_done(res->server, &res->seq_res, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002546 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002547 return 0;
Andy Adamson472cfbd2009-04-01 09:22:24 -04002548 nfs4_sequence_free_slot(res->server->nfs_client, &res->seq_res);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002549 update_changeattr(dir, &res->cinfo);
2550 nfs_post_op_update_inode(dir, &res->dir_attr);
2551 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552}
2553
2554static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2555 struct inode *new_dir, struct qstr *new_name)
2556{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002557 struct nfs_server *server = NFS_SERVER(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 struct nfs4_rename_arg arg = {
2559 .old_dir = NFS_FH(old_dir),
2560 .new_dir = NFS_FH(new_dir),
2561 .old_name = old_name,
2562 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002563 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 };
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002565 struct nfs_fattr old_fattr, new_fattr;
2566 struct nfs4_rename_res res = {
2567 .server = server,
2568 .old_fattr = &old_fattr,
2569 .new_fattr = &new_fattr,
2570 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 struct rpc_message msg = {
2572 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2573 .rpc_argp = &arg,
2574 .rpc_resp = &res,
2575 };
2576 int status;
2577
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002578 nfs_fattr_init(res.old_fattr);
2579 nfs_fattr_init(res.new_fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002580 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581
2582 if (!status) {
2583 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002584 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002586 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 }
2588 return status;
2589}
2590
2591static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2592 struct inode *new_dir, struct qstr *new_name)
2593{
2594 struct nfs4_exception exception = { };
2595 int err;
2596 do {
2597 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2598 _nfs4_proc_rename(old_dir, old_name,
2599 new_dir, new_name),
2600 &exception);
2601 } while (exception.retry);
2602 return err;
2603}
2604
2605static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2606{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002607 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 struct nfs4_link_arg arg = {
2609 .fh = NFS_FH(inode),
2610 .dir_fh = NFS_FH(dir),
2611 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002612 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002614 struct nfs_fattr fattr, dir_attr;
2615 struct nfs4_link_res res = {
2616 .server = server,
2617 .fattr = &fattr,
2618 .dir_attr = &dir_attr,
2619 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 struct rpc_message msg = {
2621 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2622 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002623 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 };
2625 int status;
2626
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002627 nfs_fattr_init(res.fattr);
2628 nfs_fattr_init(res.dir_attr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002629 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002630 if (!status) {
2631 update_changeattr(dir, &res.cinfo);
2632 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002633 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002634 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635
2636 return status;
2637}
2638
2639static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2640{
2641 struct nfs4_exception exception = { };
2642 int err;
2643 do {
2644 err = nfs4_handle_exception(NFS_SERVER(inode),
2645 _nfs4_proc_link(inode, dir, name),
2646 &exception);
2647 } while (exception.retry);
2648 return err;
2649}
2650
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002651struct nfs4_createdata {
2652 struct rpc_message msg;
2653 struct nfs4_create_arg arg;
2654 struct nfs4_create_res res;
2655 struct nfs_fh fh;
2656 struct nfs_fattr fattr;
2657 struct nfs_fattr dir_fattr;
2658};
2659
2660static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2661 struct qstr *name, struct iattr *sattr, u32 ftype)
2662{
2663 struct nfs4_createdata *data;
2664
2665 data = kzalloc(sizeof(*data), GFP_KERNEL);
2666 if (data != NULL) {
2667 struct nfs_server *server = NFS_SERVER(dir);
2668
2669 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2670 data->msg.rpc_argp = &data->arg;
2671 data->msg.rpc_resp = &data->res;
2672 data->arg.dir_fh = NFS_FH(dir);
2673 data->arg.server = server;
2674 data->arg.name = name;
2675 data->arg.attrs = sattr;
2676 data->arg.ftype = ftype;
2677 data->arg.bitmask = server->attr_bitmask;
2678 data->res.server = server;
2679 data->res.fh = &data->fh;
2680 data->res.fattr = &data->fattr;
2681 data->res.dir_fattr = &data->dir_fattr;
2682 nfs_fattr_init(data->res.fattr);
2683 nfs_fattr_init(data->res.dir_fattr);
2684 }
2685 return data;
2686}
2687
2688static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2689{
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002690 int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2691 &data->arg, &data->res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002692 if (status == 0) {
2693 update_changeattr(dir, &data->res.dir_cinfo);
2694 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2695 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2696 }
2697 return status;
2698}
2699
2700static void nfs4_free_createdata(struct nfs4_createdata *data)
2701{
2702 kfree(data);
2703}
2704
Chuck Lever4f390c12006-08-22 20:06:22 -04002705static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002706 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002708 struct nfs4_createdata *data;
2709 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710
Chuck Lever94a6d752006-08-22 20:06:23 -04002711 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002712 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002713
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002714 status = -ENOMEM;
2715 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2716 if (data == NULL)
2717 goto out;
2718
2719 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2720 data->arg.u.symlink.pages = &page;
2721 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002723 status = nfs4_do_create(dir, dentry, data);
2724
2725 nfs4_free_createdata(data);
2726out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 return status;
2728}
2729
Chuck Lever4f390c12006-08-22 20:06:22 -04002730static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002731 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732{
2733 struct nfs4_exception exception = { };
2734 int err;
2735 do {
2736 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002737 _nfs4_proc_symlink(dir, dentry, page,
2738 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 &exception);
2740 } while (exception.retry);
2741 return err;
2742}
2743
2744static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2745 struct iattr *sattr)
2746{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002747 struct nfs4_createdata *data;
2748 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002750 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2751 if (data == NULL)
2752 goto out;
2753
2754 status = nfs4_do_create(dir, dentry, data);
2755
2756 nfs4_free_createdata(data);
2757out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 return status;
2759}
2760
2761static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2762 struct iattr *sattr)
2763{
2764 struct nfs4_exception exception = { };
2765 int err;
2766 do {
2767 err = nfs4_handle_exception(NFS_SERVER(dir),
2768 _nfs4_proc_mkdir(dir, dentry, sattr),
2769 &exception);
2770 } while (exception.retry);
2771 return err;
2772}
2773
2774static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2775 u64 cookie, struct page *page, unsigned int count, int plus)
2776{
2777 struct inode *dir = dentry->d_inode;
2778 struct nfs4_readdir_arg args = {
2779 .fh = NFS_FH(dir),
2780 .pages = &page,
2781 .pgbase = 0,
2782 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09002783 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 };
2785 struct nfs4_readdir_res res;
2786 struct rpc_message msg = {
2787 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2788 .rpc_argp = &args,
2789 .rpc_resp = &res,
2790 .rpc_cred = cred,
2791 };
2792 int status;
2793
Harvey Harrison3110ff82008-05-02 13:42:44 -07002794 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00002795 dentry->d_parent->d_name.name,
2796 dentry->d_name.name,
2797 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2799 res.pgbase = args.pgbase;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002800 status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 if (status == 0)
2802 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustc4812992007-09-28 17:11:45 -04002803
2804 nfs_invalidate_atime(dir);
2805
Harvey Harrison3110ff82008-05-02 13:42:44 -07002806 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 return status;
2808}
2809
2810static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2811 u64 cookie, struct page *page, unsigned int count, int plus)
2812{
2813 struct nfs4_exception exception = { };
2814 int err;
2815 do {
2816 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2817 _nfs4_proc_readdir(dentry, cred, cookie,
2818 page, count, plus),
2819 &exception);
2820 } while (exception.retry);
2821 return err;
2822}
2823
2824static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2825 struct iattr *sattr, dev_t rdev)
2826{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002827 struct nfs4_createdata *data;
2828 int mode = sattr->ia_mode;
2829 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830
2831 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2832 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002833
2834 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2835 if (data == NULL)
2836 goto out;
2837
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002839 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002841 data->arg.ftype = NF4BLK;
2842 data->arg.u.device.specdata1 = MAJOR(rdev);
2843 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 }
2845 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002846 data->arg.ftype = NF4CHR;
2847 data->arg.u.device.specdata1 = MAJOR(rdev);
2848 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002851 status = nfs4_do_create(dir, dentry, data);
2852
2853 nfs4_free_createdata(data);
2854out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 return status;
2856}
2857
2858static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2859 struct iattr *sattr, dev_t rdev)
2860{
2861 struct nfs4_exception exception = { };
2862 int err;
2863 do {
2864 err = nfs4_handle_exception(NFS_SERVER(dir),
2865 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2866 &exception);
2867 } while (exception.retry);
2868 return err;
2869}
2870
2871static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2872 struct nfs_fsstat *fsstat)
2873{
2874 struct nfs4_statfs_arg args = {
2875 .fh = fhandle,
2876 .bitmask = server->attr_bitmask,
2877 };
Benny Halevy24ad1482009-04-01 09:21:56 -04002878 struct nfs4_statfs_res res = {
2879 .fsstat = fsstat,
2880 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 struct rpc_message msg = {
2882 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2883 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04002884 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 };
2886
Trond Myklebust0e574af2005-10-27 22:12:38 -04002887 nfs_fattr_init(fsstat->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002888 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889}
2890
2891static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2892{
2893 struct nfs4_exception exception = { };
2894 int err;
2895 do {
2896 err = nfs4_handle_exception(server,
2897 _nfs4_proc_statfs(server, fhandle, fsstat),
2898 &exception);
2899 } while (exception.retry);
2900 return err;
2901}
2902
2903static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2904 struct nfs_fsinfo *fsinfo)
2905{
2906 struct nfs4_fsinfo_arg args = {
2907 .fh = fhandle,
2908 .bitmask = server->attr_bitmask,
2909 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04002910 struct nfs4_fsinfo_res res = {
2911 .fsinfo = fsinfo,
2912 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 struct rpc_message msg = {
2914 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2915 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04002916 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 };
2918
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002919 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920}
2921
2922static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2923{
2924 struct nfs4_exception exception = { };
2925 int err;
2926
2927 do {
2928 err = nfs4_handle_exception(server,
2929 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2930 &exception);
2931 } while (exception.retry);
2932 return err;
2933}
2934
2935static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2936{
Trond Myklebust0e574af2005-10-27 22:12:38 -04002937 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2939}
2940
2941static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2942 struct nfs_pathconf *pathconf)
2943{
2944 struct nfs4_pathconf_arg args = {
2945 .fh = fhandle,
2946 .bitmask = server->attr_bitmask,
2947 };
Benny Halevyd45b2982009-04-01 09:21:58 -04002948 struct nfs4_pathconf_res res = {
2949 .pathconf = pathconf,
2950 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 struct rpc_message msg = {
2952 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2953 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04002954 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 };
2956
2957 /* None of the pathconf attributes are mandatory to implement */
2958 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
2959 memset(pathconf, 0, sizeof(*pathconf));
2960 return 0;
2961 }
2962
Trond Myklebust0e574af2005-10-27 22:12:38 -04002963 nfs_fattr_init(pathconf->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002964 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965}
2966
2967static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2968 struct nfs_pathconf *pathconf)
2969{
2970 struct nfs4_exception exception = { };
2971 int err;
2972
2973 do {
2974 err = nfs4_handle_exception(server,
2975 _nfs4_proc_pathconf(server, fhandle, pathconf),
2976 &exception);
2977 } while (exception.retry);
2978 return err;
2979}
2980
Trond Myklebustec06c092006-03-20 13:44:27 -05002981static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982{
Trond Myklebustec06c092006-03-20 13:44:27 -05002983 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984
Andy Adamsonf11c88a2009-04-01 09:22:25 -04002985 dprintk("--> %s\n", __func__);
2986
2987 /* nfs4_sequence_free_slot called in the read rpc_call_done */
2988 nfs4_sequence_done(server, &data->res.seq_res, task->tk_status);
2989
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002990 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Andy Adamsone608e792009-12-04 15:55:29 -05002991 nfs4_restart_rpc(task, server->nfs_client, &data->res.seq_res);
Trond Myklebustec06c092006-03-20 13:44:27 -05002992 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 }
Trond Myklebust8850df92007-09-28 17:20:07 -04002994
2995 nfs_invalidate_atime(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05002997 renew_lease(server, data->timestamp);
Andy Adamson36bbe342009-12-04 15:55:30 -05002998 else if (task->tk_status < 0)
2999 nfs4_sequence_free_slot(server->nfs_client, &data->res.seq_res);
3000
Trond Myklebustec06c092006-03-20 13:44:27 -05003001 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002}
3003
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003004static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 data->timestamp = jiffies;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003007 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008}
3009
Trond Myklebust788e7a82006-03-20 13:44:27 -05003010static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 struct inode *inode = data->inode;
3013
Andy Adamsondef6ed72009-04-01 09:22:26 -04003014 /* slot is freed in nfs_writeback_done */
3015 nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
3016 task->tk_status);
3017
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003018 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Andy Adamsone608e792009-12-04 15:55:29 -05003019 nfs4_restart_rpc(task, NFS_SERVER(inode)->nfs_client,
3020 &data->res.seq_res);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003021 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003023 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003025 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003026 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003027 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028}
3029
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003030static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003032 struct nfs_server *server = NFS_SERVER(data->inode);
3033
Trond Myklebusta65318b2009-03-11 14:10:28 -04003034 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003035 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 data->timestamp = jiffies;
3037
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003038 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039}
3040
Trond Myklebust788e7a82006-03-20 13:44:27 -05003041static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 struct inode *inode = data->inode;
3044
Andy Adamson21d9a852009-04-01 09:22:27 -04003045 nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
3046 task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003047 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Andy Adamsone608e792009-12-04 15:55:29 -05003048 nfs4_restart_rpc(task, NFS_SERVER(inode)->nfs_client,
3049 &data->res.seq_res);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003050 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 }
Andy Adamson21d9a852009-04-01 09:22:27 -04003052 nfs4_sequence_free_slot(NFS_SERVER(inode)->nfs_client,
3053 &data->res.seq_res);
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003054 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003055 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056}
3057
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003058static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003060 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061
Trond Myklebusta65318b2009-03-11 14:10:28 -04003062 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003063 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003064 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065}
3066
3067/*
3068 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3069 * standalone procedure for queueing an asynchronous RENEW.
3070 */
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003071static void nfs4_renew_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072{
David Howellsadfa6f92006-08-22 20:06:08 -04003073 struct nfs_client *clp = (struct nfs_client *)task->tk_msg.rpc_argp;
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003074 unsigned long timestamp = (unsigned long)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075
3076 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003077 /* Unless we're shutting down, schedule state recovery! */
3078 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
3079 nfs4_schedule_state_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 return;
3081 }
3082 spin_lock(&clp->cl_lock);
3083 if (time_before(clp->cl_last_renewal,timestamp))
3084 clp->cl_last_renewal = timestamp;
3085 spin_unlock(&clp->cl_lock);
3086}
3087
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003088static const struct rpc_call_ops nfs4_renew_ops = {
3089 .rpc_call_done = nfs4_renew_done,
3090};
3091
David Howellsadfa6f92006-08-22 20:06:08 -04003092int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093{
3094 struct rpc_message msg = {
3095 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3096 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003097 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 };
3099
3100 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003101 &nfs4_renew_ops, (void *)jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102}
3103
David Howellsadfa6f92006-08-22 20:06:08 -04003104int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105{
3106 struct rpc_message msg = {
3107 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3108 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003109 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 };
3111 unsigned long now = jiffies;
3112 int status;
3113
3114 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3115 if (status < 0)
3116 return status;
3117 spin_lock(&clp->cl_lock);
3118 if (time_before(clp->cl_last_renewal,now))
3119 clp->cl_last_renewal = now;
3120 spin_unlock(&clp->cl_lock);
3121 return 0;
3122}
3123
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003124static inline int nfs4_server_supports_acls(struct nfs_server *server)
3125{
3126 return (server->caps & NFS_CAP_ACLS)
3127 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3128 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3129}
3130
3131/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3132 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3133 * the stack.
3134 */
3135#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3136
3137static void buf_to_pages(const void *buf, size_t buflen,
3138 struct page **pages, unsigned int *pgbase)
3139{
3140 const void *p = buf;
3141
3142 *pgbase = offset_in_page(buf);
3143 p -= *pgbase;
3144 while (p < buf + buflen) {
3145 *(pages++) = virt_to_page(p);
3146 p += PAGE_CACHE_SIZE;
3147 }
3148}
3149
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003150struct nfs4_cached_acl {
3151 int cached;
3152 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003153 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003154};
3155
3156static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003157{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003158 struct nfs_inode *nfsi = NFS_I(inode);
3159
3160 spin_lock(&inode->i_lock);
3161 kfree(nfsi->nfs4_acl);
3162 nfsi->nfs4_acl = acl;
3163 spin_unlock(&inode->i_lock);
3164}
3165
3166static void nfs4_zap_acl_attr(struct inode *inode)
3167{
3168 nfs4_set_cached_acl(inode, NULL);
3169}
3170
3171static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3172{
3173 struct nfs_inode *nfsi = NFS_I(inode);
3174 struct nfs4_cached_acl *acl;
3175 int ret = -ENOENT;
3176
3177 spin_lock(&inode->i_lock);
3178 acl = nfsi->nfs4_acl;
3179 if (acl == NULL)
3180 goto out;
3181 if (buf == NULL) /* user is just asking for length */
3182 goto out_len;
3183 if (acl->cached == 0)
3184 goto out;
3185 ret = -ERANGE; /* see getxattr(2) man page */
3186 if (acl->len > buflen)
3187 goto out;
3188 memcpy(buf, acl->data, acl->len);
3189out_len:
3190 ret = acl->len;
3191out:
3192 spin_unlock(&inode->i_lock);
3193 return ret;
3194}
3195
3196static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3197{
3198 struct nfs4_cached_acl *acl;
3199
3200 if (buf && acl_len <= PAGE_SIZE) {
3201 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3202 if (acl == NULL)
3203 goto out;
3204 acl->cached = 1;
3205 memcpy(acl->data, buf, acl_len);
3206 } else {
3207 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3208 if (acl == NULL)
3209 goto out;
3210 acl->cached = 0;
3211 }
3212 acl->len = acl_len;
3213out:
3214 nfs4_set_cached_acl(inode, acl);
3215}
3216
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003217static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003218{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003219 struct page *pages[NFS4ACL_MAXPAGES];
3220 struct nfs_getaclargs args = {
3221 .fh = NFS_FH(inode),
3222 .acl_pages = pages,
3223 .acl_len = buflen,
3224 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003225 struct nfs_getaclres res = {
3226 .acl_len = buflen,
3227 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003228 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003229 struct rpc_message msg = {
3230 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3231 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003232 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003233 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003234 struct page *localpage = NULL;
3235 int ret;
3236
3237 if (buflen < PAGE_SIZE) {
3238 /* As long as we're doing a round trip to the server anyway,
3239 * let's be prepared for a page of acl data. */
3240 localpage = alloc_page(GFP_KERNEL);
3241 resp_buf = page_address(localpage);
3242 if (localpage == NULL)
3243 return -ENOMEM;
3244 args.acl_pages[0] = localpage;
3245 args.acl_pgbase = 0;
Benny Halevy663c79b2009-04-01 09:21:59 -04003246 args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003247 } else {
3248 resp_buf = buf;
3249 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3250 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003251 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003252 if (ret)
3253 goto out_free;
Benny Halevy663c79b2009-04-01 09:21:59 -04003254 if (res.acl_len > args.acl_len)
3255 nfs4_write_cached_acl(inode, NULL, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003256 else
Benny Halevy663c79b2009-04-01 09:21:59 -04003257 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003258 if (buf) {
3259 ret = -ERANGE;
Benny Halevy663c79b2009-04-01 09:21:59 -04003260 if (res.acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003261 goto out_free;
3262 if (localpage)
Benny Halevy663c79b2009-04-01 09:21:59 -04003263 memcpy(buf, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003264 }
Benny Halevy663c79b2009-04-01 09:21:59 -04003265 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003266out_free:
3267 if (localpage)
3268 __free_page(localpage);
3269 return ret;
3270}
3271
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003272static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3273{
3274 struct nfs4_exception exception = { };
3275 ssize_t ret;
3276 do {
3277 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3278 if (ret >= 0)
3279 break;
3280 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3281 } while (exception.retry);
3282 return ret;
3283}
3284
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003285static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3286{
3287 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003288 int ret;
3289
3290 if (!nfs4_server_supports_acls(server))
3291 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003292 ret = nfs_revalidate_inode(server, inode);
3293 if (ret < 0)
3294 return ret;
3295 ret = nfs4_read_cached_acl(inode, buf, buflen);
3296 if (ret != -ENOENT)
3297 return ret;
3298 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003299}
3300
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003301static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003302{
3303 struct nfs_server *server = NFS_SERVER(inode);
3304 struct page *pages[NFS4ACL_MAXPAGES];
3305 struct nfs_setaclargs arg = {
3306 .fh = NFS_FH(inode),
3307 .acl_pages = pages,
3308 .acl_len = buflen,
3309 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003310 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003311 struct rpc_message msg = {
3312 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3313 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003314 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003315 };
3316 int ret;
3317
3318 if (!nfs4_server_supports_acls(server))
3319 return -EOPNOTSUPP;
Trond Myklebust642ac542005-10-18 14:20:19 -07003320 nfs_inode_return_delegation(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003321 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003322 ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003323 nfs_access_zap_cache(inode);
3324 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003325 return ret;
3326}
3327
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003328static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3329{
3330 struct nfs4_exception exception = { };
3331 int err;
3332 do {
3333 err = nfs4_handle_exception(NFS_SERVER(inode),
3334 __nfs4_proc_set_acl(inode, buf, buflen),
3335 &exception);
3336 } while (exception.retry);
3337 return err;
3338}
3339
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340static int
Andy Adamson8328d592009-04-01 09:22:35 -04003341_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 -07003342{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 if (!clp || task->tk_status >= 0)
3344 return 0;
3345 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003346 case -NFS4ERR_ADMIN_REVOKED:
3347 case -NFS4ERR_BAD_STATEID:
3348 case -NFS4ERR_OPENMODE:
3349 if (state == NULL)
3350 break;
3351 nfs4_state_mark_reclaim_nograce(clp, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 case -NFS4ERR_STALE_CLIENTID:
3353 case -NFS4ERR_STALE_STATEID:
3354 case -NFS4ERR_EXPIRED:
Trond Myklebust5d008372008-02-22 16:34:17 -05003355 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 nfs4_schedule_state_recovery(clp);
Trond Myklebuste005e802008-12-23 15:21:48 -05003357 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
Trond Myklebustfda13932008-02-22 16:34:12 -05003358 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 task->tk_status = 0;
3360 return -EAGAIN;
Andy Adamson4745e312009-04-01 09:22:42 -04003361#if defined(CONFIG_NFS_V4_1)
3362 case -NFS4ERR_BADSESSION:
3363 case -NFS4ERR_BADSLOT:
3364 case -NFS4ERR_BAD_HIGH_SLOT:
3365 case -NFS4ERR_DEADSESSION:
3366 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3367 case -NFS4ERR_SEQ_FALSE_RETRY:
3368 case -NFS4ERR_SEQ_MISORDERED:
3369 dprintk("%s ERROR %d, Reset session\n", __func__,
3370 task->tk_status);
Andy Adamson0b9e2d42009-12-04 16:02:14 -05003371 nfs4_schedule_state_recovery(clp);
Andy Adamson4745e312009-04-01 09:22:42 -04003372 task->tk_status = 0;
3373 return -EAGAIN;
3374#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 case -NFS4ERR_DELAY:
Andy Adamson8328d592009-04-01 09:22:35 -04003376 if (server)
3377 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003378 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3380 task->tk_status = 0;
3381 return -EAGAIN;
3382 case -NFS4ERR_OLD_STATEID:
3383 task->tk_status = 0;
3384 return -EAGAIN;
3385 }
3386 task->tk_status = nfs4_map_errors(task->tk_status);
3387 return 0;
3388}
3389
Andy Adamson8328d592009-04-01 09:22:35 -04003390static int
3391nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3392{
3393 return _nfs4_async_handle_error(task, server, server->nfs_client, state);
3394}
3395
David Howellsadfa6f92006-08-22 20:06:08 -04003396int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, unsigned short port, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397{
3398 nfs4_verifier sc_verifier;
3399 struct nfs4_setclientid setclientid = {
3400 .sc_verifier = &sc_verifier,
3401 .sc_prog = program,
3402 };
3403 struct rpc_message msg = {
3404 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3405 .rpc_argp = &setclientid,
3406 .rpc_resp = clp,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003407 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 };
Al Virobc4785c2006-10-19 23:28:51 -07003409 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 int loop = 0;
3411 int status;
3412
Al Virobc4785c2006-10-19 23:28:51 -07003413 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3415 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3416
3417 for(;;) {
3418 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003419 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003420 clp->cl_ipaddr,
3421 rpc_peeraddr2str(clp->cl_rpcclient,
3422 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003423 rpc_peeraddr2str(clp->cl_rpcclient,
3424 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003425 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 clp->cl_id_uniquifier);
3427 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003428 sizeof(setclientid.sc_netid),
3429 rpc_peeraddr2str(clp->cl_rpcclient,
3430 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003432 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 clp->cl_ipaddr, port >> 8, port & 255);
3434
3435 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3436 if (status != -NFS4ERR_CLID_INUSE)
3437 break;
3438 if (signalled())
3439 break;
3440 if (loop++ & 1)
3441 ssleep(clp->cl_lease_time + 1);
3442 else
3443 if (++clp->cl_id_uniquifier == 0)
3444 break;
3445 }
3446 return status;
3447}
3448
David Howellsadfa6f92006-08-22 20:06:08 -04003449static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450{
3451 struct nfs_fsinfo fsinfo;
3452 struct rpc_message msg = {
3453 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
3454 .rpc_argp = clp,
3455 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003456 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 };
3458 unsigned long now;
3459 int status;
3460
3461 now = jiffies;
3462 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3463 if (status == 0) {
3464 spin_lock(&clp->cl_lock);
3465 clp->cl_lease_time = fsinfo.lease_time * HZ;
3466 clp->cl_last_renewal = now;
3467 spin_unlock(&clp->cl_lock);
3468 }
3469 return status;
3470}
3471
David Howellsadfa6f92006-08-22 20:06:08 -04003472int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
Trond Myklebust51581f32006-03-20 13:44:47 -05003473{
Benny Halevy77e03672008-06-24 20:25:57 +03003474 long timeout = 0;
Trond Myklebust51581f32006-03-20 13:44:47 -05003475 int err;
3476 do {
3477 err = _nfs4_proc_setclientid_confirm(clp, cred);
3478 switch (err) {
3479 case 0:
3480 return err;
3481 case -NFS4ERR_RESOURCE:
3482 /* The IBM lawyers misread another document! */
3483 case -NFS4ERR_DELAY:
3484 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3485 }
3486 } while (err == 0);
3487 return err;
3488}
3489
Trond Myklebustfe650402006-01-03 09:55:18 +01003490struct nfs4_delegreturndata {
3491 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003492 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003493 struct nfs_fh fh;
3494 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003495 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003496 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003497 int rpc_status;
3498};
3499
Trond Myklebustfe650402006-01-03 09:55:18 +01003500static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3501{
3502 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003503
3504 nfs4_sequence_done_free_slot(data->res.server, &data->res.seq_res,
3505 task->tk_status);
3506
Trond Myklebustfe650402006-01-03 09:55:18 +01003507 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003508 if (data->rpc_status == 0)
Trond Myklebustfa178f22006-01-03 09:55:38 +01003509 renew_lease(data->res.server, data->timestamp);
Trond Myklebustfe650402006-01-03 09:55:18 +01003510}
3511
3512static void nfs4_delegreturn_release(void *calldata)
3513{
Trond Myklebustfe650402006-01-03 09:55:18 +01003514 kfree(calldata);
3515}
3516
Andy Adamson938e1012009-04-01 09:22:28 -04003517#if defined(CONFIG_NFS_V4_1)
3518static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3519{
3520 struct nfs4_delegreturndata *d_data;
3521
3522 d_data = (struct nfs4_delegreturndata *)data;
3523
3524 if (nfs4_setup_sequence(d_data->res.server->nfs_client,
3525 &d_data->args.seq_args,
3526 &d_data->res.seq_res, 1, task))
3527 return;
3528 rpc_call_start(task);
3529}
3530#endif /* CONFIG_NFS_V4_1 */
3531
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003532static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003533#if defined(CONFIG_NFS_V4_1)
3534 .rpc_call_prepare = nfs4_delegreturn_prepare,
3535#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003536 .rpc_call_done = nfs4_delegreturn_done,
3537 .rpc_release = nfs4_delegreturn_release,
3538};
3539
Trond Myklebuste6f81072008-01-24 18:14:34 -05003540static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003541{
3542 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003543 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003544 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003545 struct rpc_message msg = {
3546 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3547 .rpc_cred = cred,
3548 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003549 struct rpc_task_setup task_setup_data = {
3550 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003551 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003552 .callback_ops = &nfs4_delegreturn_ops,
3553 .flags = RPC_TASK_ASYNC,
3554 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003555 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003556
Andy Adamson938e1012009-04-01 09:22:28 -04003557 data = kzalloc(sizeof(*data), GFP_KERNEL);
Trond Myklebustfe650402006-01-03 09:55:18 +01003558 if (data == NULL)
3559 return -ENOMEM;
3560 data->args.fhandle = &data->fh;
3561 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003562 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003563 nfs_copy_fh(&data->fh, NFS_FH(inode));
3564 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003565 data->res.fattr = &data->fattr;
3566 data->res.server = server;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003567 data->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003568 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003569 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003570 data->rpc_status = 0;
3571
Trond Myklebustc970aa82007-07-14 15:39:59 -04003572 task_setup_data.callback_data = data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003573 msg.rpc_argp = &data->args,
3574 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003575 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003576 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003577 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003578 if (!issync)
3579 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003580 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003581 if (status != 0)
3582 goto out;
3583 status = data->rpc_status;
3584 if (status != 0)
3585 goto out;
3586 nfs_refresh_inode(inode, &data->fattr);
3587out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003588 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003589 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590}
3591
Trond Myklebuste6f81072008-01-24 18:14:34 -05003592int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593{
3594 struct nfs_server *server = NFS_SERVER(inode);
3595 struct nfs4_exception exception = { };
3596 int err;
3597 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003598 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599 switch (err) {
3600 case -NFS4ERR_STALE_STATEID:
3601 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 case 0:
3603 return 0;
3604 }
3605 err = nfs4_handle_exception(server, err, &exception);
3606 } while (exception.retry);
3607 return err;
3608}
3609
3610#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3611#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3612
3613/*
3614 * sleep, with exponential backoff, and retry the LOCK operation.
3615 */
3616static unsigned long
3617nfs4_set_lock_task_retry(unsigned long timeout)
3618{
Matthew Wilcox150030b2007-12-06 16:24:39 -05003619 schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 timeout <<= 1;
3621 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3622 return NFS4_LOCK_MAXTIMEOUT;
3623 return timeout;
3624}
3625
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3627{
3628 struct inode *inode = state->inode;
3629 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003630 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003631 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003633 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003635 struct nfs_lockt_res res = {
3636 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637 };
3638 struct rpc_message msg = {
3639 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3640 .rpc_argp = &arg,
3641 .rpc_resp = &res,
3642 .rpc_cred = state->owner->so_cred,
3643 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 struct nfs4_lock_state *lsp;
3645 int status;
3646
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003647 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003648 status = nfs4_set_lock_state(state, request);
3649 if (status != 0)
3650 goto out;
3651 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003652 arg.lock_owner.id = lsp->ls_id.id;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003653 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003654 switch (status) {
3655 case 0:
3656 request->fl_type = F_UNLCK;
3657 break;
3658 case -NFS4ERR_DENIED:
3659 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05003661 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003662out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 return status;
3664}
3665
3666static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3667{
3668 struct nfs4_exception exception = { };
3669 int err;
3670
3671 do {
3672 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3673 _nfs4_proc_getlk(state, cmd, request),
3674 &exception);
3675 } while (exception.retry);
3676 return err;
3677}
3678
3679static int do_vfs_lock(struct file *file, struct file_lock *fl)
3680{
3681 int res = 0;
3682 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3683 case FL_POSIX:
3684 res = posix_lock_file_wait(file, fl);
3685 break;
3686 case FL_FLOCK:
3687 res = flock_lock_file_wait(file, fl);
3688 break;
3689 default:
3690 BUG();
3691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 return res;
3693}
3694
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003695struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003696 struct nfs_locku_args arg;
3697 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003698 struct nfs4_lock_state *lsp;
3699 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003700 struct file_lock fl;
3701 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003702 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003703};
3704
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003705static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3706 struct nfs_open_context *ctx,
3707 struct nfs4_lock_state *lsp,
3708 struct nfs_seqid *seqid)
3709{
3710 struct nfs4_unlockdata *p;
3711 struct inode *inode = lsp->ls_state->inode;
3712
Andy Adamsona8936932009-04-01 09:22:23 -04003713 p = kzalloc(sizeof(*p), GFP_KERNEL);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003714 if (p == NULL)
3715 return NULL;
3716 p->arg.fh = NFS_FH(inode);
3717 p->arg.fl = &p->fl;
3718 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003719 p->res.seqid = seqid;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003720 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003721 p->arg.stateid = &lsp->ls_stateid;
3722 p->lsp = lsp;
3723 atomic_inc(&lsp->ls_count);
3724 /* Ensure we don't close file until we're done freeing locks! */
3725 p->ctx = get_nfs_open_context(ctx);
3726 memcpy(&p->fl, fl, sizeof(p->fl));
3727 p->server = NFS_SERVER(inode);
3728 return p;
3729}
3730
Trond Myklebust06f814a2006-01-03 09:55:07 +01003731static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003732{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003733 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003734 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003735 nfs4_put_lock_state(calldata->lsp);
3736 put_nfs_open_context(calldata->ctx);
3737 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003738}
3739
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003740static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003741{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003742 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003743
Andy Adamsona8936932009-04-01 09:22:23 -04003744 nfs4_sequence_done(calldata->server, &calldata->res.seq_res,
3745 task->tk_status);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003746 if (RPC_ASSASSINATED(task))
3747 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003748 switch (task->tk_status) {
3749 case 0:
3750 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003751 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003752 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01003753 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003754 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003755 case -NFS4ERR_BAD_STATEID:
3756 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003757 case -NFS4ERR_STALE_STATEID:
3758 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003759 break;
3760 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003761 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Andy Adamsoneedc0202009-04-01 09:22:41 -04003762 nfs4_restart_rpc(task,
Andy Adamsone608e792009-12-04 15:55:29 -05003763 calldata->server->nfs_client,
3764 &calldata->res.seq_res);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003765 }
Andy Adamsona8936932009-04-01 09:22:23 -04003766 nfs4_sequence_free_slot(calldata->server->nfs_client,
3767 &calldata->res.seq_res);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003768}
3769
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003770static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003771{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003772 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003774 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003775 return;
3776 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003777 /* Note: exit _without_ running nfs4_locku_done */
3778 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003779 return;
3780 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003781 calldata->timestamp = jiffies;
Andy Adamsona8936932009-04-01 09:22:23 -04003782 if (nfs4_setup_sequence(calldata->server->nfs_client,
3783 &calldata->arg.seq_args,
3784 &calldata->res.seq_res, 1, task))
3785 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003786 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787}
3788
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003789static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003790 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003791 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01003792 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003793};
3794
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003795static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3796 struct nfs_open_context *ctx,
3797 struct nfs4_lock_state *lsp,
3798 struct nfs_seqid *seqid)
3799{
3800 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003801 struct rpc_message msg = {
3802 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3803 .rpc_cred = ctx->cred,
3804 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003805 struct rpc_task_setup task_setup_data = {
3806 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04003807 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003808 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003809 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003810 .flags = RPC_TASK_ASYNC,
3811 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003812
Frank Filz137d6ac2007-07-09 15:32:29 -07003813 /* Ensure this is an unlock - when canceling a lock, the
3814 * canceled lock is passed in, and it won't be an unlock.
3815 */
3816 fl->fl_type = F_UNLCK;
3817
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003818 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3819 if (data == NULL) {
3820 nfs_free_seqid(seqid);
3821 return ERR_PTR(-ENOMEM);
3822 }
3823
Trond Myklebust5138fde2007-07-14 15:40:01 -04003824 msg.rpc_argp = &data->arg,
3825 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003826 task_setup_data.callback_data = data;
3827 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003828}
3829
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3831{
Trond Myklebust19e03c52008-12-23 15:21:44 -05003832 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003833 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003834 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01003835 struct rpc_task *task;
3836 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003837 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838
Trond Myklebust9b073572006-06-29 16:38:34 -04003839 status = nfs4_set_lock_state(state, request);
3840 /* Unlock _before_ we do the RPC call */
3841 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003842 down_read(&nfsi->rwsem);
3843 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
3844 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003845 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003846 }
3847 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003848 if (status != 0)
3849 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003850 /* Is this a delegated lock? */
3851 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04003852 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003853 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003854 seqid = nfs_alloc_seqid(&lsp->ls_seqid);
Trond Myklebust9b073572006-06-29 16:38:34 -04003855 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003856 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04003857 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04003858 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003859 status = PTR_ERR(task);
3860 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04003861 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003862 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003863 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04003864out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003865 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003866 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867}
3868
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003869struct nfs4_lockdata {
3870 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003871 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003872 struct nfs4_lock_state *lsp;
3873 struct nfs_open_context *ctx;
3874 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003875 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003876 int rpc_status;
3877 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04003878 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003879};
3880
3881static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
3882 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
3883{
3884 struct nfs4_lockdata *p;
3885 struct inode *inode = lsp->ls_state->inode;
3886 struct nfs_server *server = NFS_SERVER(inode);
3887
3888 p = kzalloc(sizeof(*p), GFP_KERNEL);
3889 if (p == NULL)
3890 return NULL;
3891
3892 p->arg.fh = NFS_FH(inode);
3893 p->arg.fl = &p->fl;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003894 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid);
3895 if (p->arg.open_seqid == NULL)
3896 goto out_free;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003897 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3898 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003899 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003900 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04003901 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003902 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003903 p->res.lock_seqid = p->arg.lock_seqid;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003904 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003905 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04003906 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003907 atomic_inc(&lsp->ls_count);
3908 p->ctx = get_nfs_open_context(ctx);
3909 memcpy(&p->fl, fl, sizeof(p->fl));
3910 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003911out_free_seqid:
3912 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003913out_free:
3914 kfree(p);
3915 return NULL;
3916}
3917
3918static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
3919{
3920 struct nfs4_lockdata *data = calldata;
3921 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922
Harvey Harrison3110ff82008-05-02 13:42:44 -07003923 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003924 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
3925 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003926 /* Do we need to do an open_to_lock_owner? */
3927 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05003928 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
3929 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003930 data->arg.open_stateid = &state->stateid;
3931 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003932 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003933 } else
3934 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003935 data->timestamp = jiffies;
Andy Adamson66179ef2009-04-01 09:22:22 -04003936 if (nfs4_setup_sequence(data->server->nfs_client, &data->arg.seq_args,
3937 &data->res.seq_res, 1, task))
3938 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003939 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003940 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003941}
3942
3943static void nfs4_lock_done(struct rpc_task *task, void *calldata)
3944{
3945 struct nfs4_lockdata *data = calldata;
3946
Harvey Harrison3110ff82008-05-02 13:42:44 -07003947 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003948
Andy Adamson66179ef2009-04-01 09:22:22 -04003949 nfs4_sequence_done_free_slot(data->server, &data->res.seq_res,
3950 task->tk_status);
3951
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003952 data->rpc_status = task->tk_status;
3953 if (RPC_ASSASSINATED(task))
3954 goto out;
3955 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003956 if (data->rpc_status == 0)
3957 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
3958 else
3959 goto out;
3960 }
3961 if (data->rpc_status == 0) {
3962 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
3963 sizeof(data->lsp->ls_stateid.data));
3964 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust88be9f92007-06-05 10:42:27 -04003965 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003966 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003967out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07003968 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003969}
3970
3971static void nfs4_lock_release(void *calldata)
3972{
3973 struct nfs4_lockdata *data = calldata;
3974
Harvey Harrison3110ff82008-05-02 13:42:44 -07003975 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003976 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003977 if (data->cancelled != 0) {
3978 struct rpc_task *task;
3979 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
3980 data->arg.lock_seqid);
3981 if (!IS_ERR(task))
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003982 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003983 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003984 } else
3985 nfs_free_seqid(data->arg.lock_seqid);
3986 nfs4_put_lock_state(data->lsp);
3987 put_nfs_open_context(data->ctx);
3988 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003989 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003990}
3991
3992static const struct rpc_call_ops nfs4_lock_ops = {
3993 .rpc_call_prepare = nfs4_lock_prepare,
3994 .rpc_call_done = nfs4_lock_done,
3995 .rpc_release = nfs4_lock_release,
3996};
3997
3998static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int reclaim)
3999{
4000 struct nfs4_lockdata *data;
4001 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004002 struct rpc_message msg = {
4003 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4004 .rpc_cred = state->owner->so_cred,
4005 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004006 struct rpc_task_setup task_setup_data = {
4007 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004008 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004009 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004010 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004011 .flags = RPC_TASK_ASYNC,
4012 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004013 int ret;
4014
Harvey Harrison3110ff82008-05-02 13:42:44 -07004015 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004016 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004017 fl->fl_u.nfs4_fl.owner);
4018 if (data == NULL)
4019 return -ENOMEM;
4020 if (IS_SETLKW(cmd))
4021 data->arg.block = 1;
4022 if (reclaim != 0)
4023 data->arg.reclaim = 1;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004024 msg.rpc_argp = &data->arg,
4025 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004026 task_setup_data.callback_data = data;
4027 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004028 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004029 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004030 ret = nfs4_wait_for_completion_rpc_task(task);
4031 if (ret == 0) {
4032 ret = data->rpc_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004033 } else
4034 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004035 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004036 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004037 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038}
4039
4040static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4041{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004042 struct nfs_server *server = NFS_SERVER(state->inode);
4043 struct nfs4_exception exception = { };
4044 int err;
4045
4046 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004047 /* Cache the lock if possible... */
4048 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4049 return 0;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004050 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
4051 if (err != -NFS4ERR_DELAY)
4052 break;
4053 nfs4_handle_exception(server, err, &exception);
4054 } while (exception.retry);
4055 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056}
4057
4058static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4059{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004060 struct nfs_server *server = NFS_SERVER(state->inode);
4061 struct nfs4_exception exception = { };
4062 int err;
4063
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004064 err = nfs4_set_lock_state(state, request);
4065 if (err != 0)
4066 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004067 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004068 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4069 return 0;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004070 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004071 switch (err) {
4072 default:
4073 goto out;
4074 case -NFS4ERR_GRACE:
4075 case -NFS4ERR_DELAY:
4076 nfs4_handle_exception(server, err, &exception);
4077 err = 0;
4078 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004079 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004080out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004081 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082}
4083
4084static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4085{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004086 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004087 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088 int status;
4089
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004090 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004091 status = nfs4_set_lock_state(state, request);
4092 if (status != 0)
4093 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004094 request->fl_flags |= FL_ACCESS;
4095 status = do_vfs_lock(request->fl_file, request);
4096 if (status < 0)
4097 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004098 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004099 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004100 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004101 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004102 request->fl_flags = fl_flags & ~FL_SLEEP;
4103 status = do_vfs_lock(request->fl_file, request);
4104 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004105 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004106 status = _nfs4_do_setlk(state, cmd, request, 0);
4107 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004108 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004109 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004110 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004111 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07004112 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004113out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004114 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004115out:
4116 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117 return status;
4118}
4119
4120static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4121{
4122 struct nfs4_exception exception = { };
4123 int err;
4124
4125 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004126 err = _nfs4_proc_setlk(state, cmd, request);
4127 if (err == -NFS4ERR_DENIED)
4128 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004130 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 } while (exception.retry);
4132 return err;
4133}
4134
4135static int
4136nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4137{
4138 struct nfs_open_context *ctx;
4139 struct nfs4_state *state;
4140 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4141 int status;
4142
4143 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004144 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145 state = ctx->state;
4146
4147 if (request->fl_start < 0 || request->fl_end < 0)
4148 return -EINVAL;
4149
Trond Myklebustd9531262009-07-21 19:22:38 -04004150 if (IS_GETLK(cmd)) {
4151 if (state != NULL)
4152 return nfs4_proc_getlk(state, F_GETLK, request);
4153 return 0;
4154 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155
4156 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4157 return -EINVAL;
4158
Trond Myklebustd9531262009-07-21 19:22:38 -04004159 if (request->fl_type == F_UNLCK) {
4160 if (state != NULL)
4161 return nfs4_proc_unlck(state, cmd, request);
4162 return 0;
4163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164
Trond Myklebustd9531262009-07-21 19:22:38 -04004165 if (state == NULL)
4166 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 do {
4168 status = nfs4_proc_setlk(state, cmd, request);
4169 if ((status != -EAGAIN) || IS_SETLK(cmd))
4170 break;
4171 timeout = nfs4_set_lock_task_retry(timeout);
4172 status = -ERESTARTSYS;
4173 if (signalled())
4174 break;
4175 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 return status;
4177}
4178
Trond Myklebust888e6942005-11-04 15:38:11 -05004179int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4180{
4181 struct nfs_server *server = NFS_SERVER(state->inode);
4182 struct nfs4_exception exception = { };
4183 int err;
4184
4185 err = nfs4_set_lock_state(state, fl);
4186 if (err != 0)
4187 goto out;
4188 do {
4189 err = _nfs4_do_setlk(state, F_SETLK, fl, 0);
Trond Myklebustd5122202009-06-17 13:22:58 -07004190 switch (err) {
4191 default:
4192 printk(KERN_ERR "%s: unhandled error %d.\n",
4193 __func__, err);
4194 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004195 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004196 goto out;
4197 case -NFS4ERR_EXPIRED:
4198 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004199 case -NFS4ERR_STALE_STATEID:
Trond Myklebustd5122202009-06-17 13:22:58 -07004200 nfs4_schedule_state_recovery(server->nfs_client);
4201 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004202 case -ERESTARTSYS:
4203 /*
4204 * The show must go on: exit, but mark the
4205 * stateid as needing recovery.
4206 */
4207 case -NFS4ERR_ADMIN_REVOKED:
4208 case -NFS4ERR_BAD_STATEID:
4209 case -NFS4ERR_OPENMODE:
4210 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
4211 err = 0;
4212 goto out;
4213 case -ENOMEM:
4214 case -NFS4ERR_DENIED:
4215 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4216 err = 0;
4217 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004218 case -NFS4ERR_DELAY:
4219 break;
4220 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004221 err = nfs4_handle_exception(server, err, &exception);
4222 } while (exception.retry);
4223out:
4224 return err;
4225}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004226
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004227#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4228
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004229int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
4230 size_t buflen, int flags)
4231{
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004232 struct inode *inode = dentry->d_inode;
4233
4234 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4235 return -EOPNOTSUPP;
4236
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004237 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004238}
4239
4240/* The getxattr man page suggests returning -ENODATA for unknown attributes,
4241 * and that's what we'll do for e.g. user attributes that haven't been set.
4242 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
4243 * attributes in kernel-managed attribute namespaces. */
4244ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
4245 size_t buflen)
4246{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004247 struct inode *inode = dentry->d_inode;
4248
4249 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4250 return -EOPNOTSUPP;
4251
4252 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004253}
4254
4255ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
4256{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004257 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004258
J. Bruce Fields096455a2006-03-20 23:23:42 -05004259 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4260 return 0;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004261 if (buf && buflen < len)
4262 return -ERANGE;
4263 if (buf)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004264 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
4265 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004266}
4267
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004268static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4269{
4270 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4271 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4272 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4273 return;
4274
4275 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4276 NFS_ATTR_FATTR_NLINK;
4277 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4278 fattr->nlink = 2;
4279}
4280
Trond Myklebust56659e92007-07-17 21:52:39 -04004281int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004282 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004283{
4284 struct nfs_server *server = NFS_SERVER(dir);
4285 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004286 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4287 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004288 };
4289 struct nfs4_fs_locations_arg args = {
4290 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004291 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004292 .page = page,
4293 .bitmask = bitmask,
4294 };
Benny Halevy22958462009-04-01 09:22:02 -04004295 struct nfs4_fs_locations_res res = {
4296 .fs_locations = fs_locations,
4297 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004298 struct rpc_message msg = {
4299 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4300 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004301 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004302 };
4303 int status;
4304
Harvey Harrison3110ff82008-05-02 13:42:44 -07004305 dprintk("%s: start\n", __func__);
Trond Myklebustc228fd32007-01-13 02:28:11 -05004306 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004307 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004308 fs_locations->nlocations = 0;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04004309 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004310 nfs_fixup_referral_attributes(&fs_locations->fattr);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004311 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004312 return status;
4313}
4314
Andy Adamson557134a2009-04-01 09:21:53 -04004315#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004316/*
4317 * nfs4_proc_exchange_id()
4318 *
4319 * Since the clientid has expired, all compounds using sessions
4320 * associated with the stale clientid will be returning
4321 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4322 * be in some phase of session reset.
4323 */
Andy Adamson4d643d12009-12-04 15:52:24 -05004324int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004325{
4326 nfs4_verifier verifier;
4327 struct nfs41_exchange_id_args args = {
4328 .client = clp,
4329 .flags = clp->cl_exchange_flags,
4330 };
4331 struct nfs41_exchange_id_res res = {
4332 .client = clp,
4333 };
4334 int status;
4335 struct rpc_message msg = {
4336 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4337 .rpc_argp = &args,
4338 .rpc_resp = &res,
4339 .rpc_cred = cred,
4340 };
4341 __be32 *p;
4342
4343 dprintk("--> %s\n", __func__);
4344 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004345
Benny Halevy99fe60d2009-04-01 09:22:29 -04004346 p = (u32 *)verifier.data;
4347 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4348 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4349 args.verifier = &verifier;
4350
4351 while (1) {
4352 args.id_len = scnprintf(args.id, sizeof(args.id),
4353 "%s/%s %u",
4354 clp->cl_ipaddr,
4355 rpc_peeraddr2str(clp->cl_rpcclient,
4356 RPC_DISPLAY_ADDR),
4357 clp->cl_id_uniquifier);
4358
4359 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4360
4361 if (status != NFS4ERR_CLID_INUSE)
4362 break;
4363
4364 if (signalled())
4365 break;
4366
4367 if (++clp->cl_id_uniquifier == 0)
4368 break;
4369 }
4370
4371 dprintk("<-- %s status= %d\n", __func__, status);
4372 return status;
4373}
4374
Andy Adamson2050f0c2009-04-01 09:22:30 -04004375struct nfs4_get_lease_time_data {
4376 struct nfs4_get_lease_time_args *args;
4377 struct nfs4_get_lease_time_res *res;
4378 struct nfs_client *clp;
4379};
4380
4381static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4382 void *calldata)
4383{
4384 int ret;
4385 struct nfs4_get_lease_time_data *data =
4386 (struct nfs4_get_lease_time_data *)calldata;
4387
4388 dprintk("--> %s\n", __func__);
4389 /* just setup sequence, do not trigger session recovery
4390 since we're invoked within one */
4391 ret = nfs41_setup_sequence(data->clp->cl_session,
4392 &data->args->la_seq_args,
4393 &data->res->lr_seq_res, 0, task);
4394
4395 BUG_ON(ret == -EAGAIN);
4396 rpc_call_start(task);
4397 dprintk("<-- %s\n", __func__);
4398}
4399
4400/*
4401 * Called from nfs4_state_manager thread for session setup, so don't recover
4402 * from sequence operation or clientid errors.
4403 */
4404static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4405{
4406 struct nfs4_get_lease_time_data *data =
4407 (struct nfs4_get_lease_time_data *)calldata;
4408
4409 dprintk("--> %s\n", __func__);
4410 nfs41_sequence_done(data->clp, &data->res->lr_seq_res, task->tk_status);
4411 switch (task->tk_status) {
4412 case -NFS4ERR_DELAY:
4413 case -NFS4ERR_GRACE:
4414 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4415 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4416 task->tk_status = 0;
Andy Adamson1d9ddde2009-12-04 15:55:27 -05004417 rpc_restart_call(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004418 return;
4419 }
4420 nfs41_sequence_free_slot(data->clp, &data->res->lr_seq_res);
4421 dprintk("<-- %s\n", __func__);
4422}
4423
4424struct rpc_call_ops nfs4_get_lease_time_ops = {
4425 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4426 .rpc_call_done = nfs4_get_lease_time_done,
4427};
4428
4429int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4430{
4431 struct rpc_task *task;
4432 struct nfs4_get_lease_time_args args;
4433 struct nfs4_get_lease_time_res res = {
4434 .lr_fsinfo = fsinfo,
4435 };
4436 struct nfs4_get_lease_time_data data = {
4437 .args = &args,
4438 .res = &res,
4439 .clp = clp,
4440 };
4441 struct rpc_message msg = {
4442 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4443 .rpc_argp = &args,
4444 .rpc_resp = &res,
4445 };
4446 struct rpc_task_setup task_setup = {
4447 .rpc_client = clp->cl_rpcclient,
4448 .rpc_message = &msg,
4449 .callback_ops = &nfs4_get_lease_time_ops,
4450 .callback_data = &data
4451 };
4452 int status;
4453
4454 res.lr_seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
4455 dprintk("--> %s\n", __func__);
4456 task = rpc_run_task(&task_setup);
4457
4458 if (IS_ERR(task))
4459 status = PTR_ERR(task);
4460 else {
4461 status = task->tk_status;
4462 rpc_put_task(task);
4463 }
4464 dprintk("<-- %s return %d\n", __func__, status);
4465
4466 return status;
4467}
4468
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004469/*
4470 * Reset a slot table
4471 */
4472static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, int max_slots,
4473 int old_max_slots, int ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004474{
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004475 int i;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004476 int ret = 0;
4477
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004478 dprintk("--> %s: max_reqs=%u, tbl %p\n", __func__, max_slots, tbl);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004479
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004480 /*
4481 * Until we have dynamic slot table adjustment, insist
4482 * upon the same slot table size
4483 */
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004484 if (max_slots != old_max_slots) {
4485 dprintk("%s reset slot table does't match old\n",
4486 __func__);
4487 ret = -EINVAL; /*XXX NFS4ERR_REQ_TOO_BIG ? */
4488 goto out;
4489 }
4490 spin_lock(&tbl->slot_tbl_lock);
4491 for (i = 0; i < max_slots; ++i)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004492 tbl->slots[i].seq_nr = ivalue;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004493 tbl->highest_used_slotid = -1;
4494 spin_unlock(&tbl->slot_tbl_lock);
4495 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4496 tbl, tbl->slots, tbl->max_slots);
4497out:
4498 dprintk("<-- %s: return %d\n", __func__, ret);
4499 return ret;
4500}
4501
Andy Adamsonfc931582009-04-01 09:22:31 -04004502/*
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004503 * Reset the forechannel and backchannel slot tables
4504 */
4505static int nfs4_reset_slot_tables(struct nfs4_session *session)
4506{
4507 int status;
4508
4509 status = nfs4_reset_slot_table(&session->fc_slot_table,
4510 session->fc_attrs.max_reqs,
4511 session->fc_slot_table.max_slots,
4512 1);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004513 if (status)
4514 return status;
Andy Adamsonea028ac2009-12-04 15:55:38 -05004515 init_completion(&session->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004516
4517 status = nfs4_reset_slot_table(&session->bc_slot_table,
4518 session->bc_attrs.max_reqs,
4519 session->bc_slot_table.max_slots,
4520 0);
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004521 return status;
4522}
4523
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004524/* Destroy the slot table */
4525static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4526{
4527 if (session->fc_slot_table.slots != NULL) {
4528 kfree(session->fc_slot_table.slots);
4529 session->fc_slot_table.slots = NULL;
4530 }
4531 if (session->bc_slot_table.slots != NULL) {
4532 kfree(session->bc_slot_table.slots);
4533 session->bc_slot_table.slots = NULL;
4534 }
4535 return;
4536}
4537
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004538/*
Andy Adamsonfc931582009-04-01 09:22:31 -04004539 * Initialize slot table
4540 */
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004541static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
4542 int max_slots, int ivalue)
Andy Adamsonfc931582009-04-01 09:22:31 -04004543{
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004544 int i;
Andy Adamsonfc931582009-04-01 09:22:31 -04004545 struct nfs4_slot *slot;
4546 int ret = -ENOMEM;
4547
4548 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
4549
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004550 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
Andy Adamsonfc931582009-04-01 09:22:31 -04004551
4552 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_KERNEL);
4553 if (!slot)
4554 goto out;
4555 for (i = 0; i < max_slots; ++i)
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004556 slot[i].seq_nr = ivalue;
Andy Adamsonfc931582009-04-01 09:22:31 -04004557 ret = 0;
4558
4559 spin_lock(&tbl->slot_tbl_lock);
4560 if (tbl->slots != NULL) {
4561 spin_unlock(&tbl->slot_tbl_lock);
4562 dprintk("%s: slot table already initialized. tbl=%p slots=%p\n",
4563 __func__, tbl, tbl->slots);
4564 WARN_ON(1);
4565 goto out_free;
4566 }
4567 tbl->max_slots = max_slots;
4568 tbl->slots = slot;
4569 tbl->highest_used_slotid = -1; /* no slot is currently used */
4570 spin_unlock(&tbl->slot_tbl_lock);
4571 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4572 tbl, tbl->slots, tbl->max_slots);
4573out:
4574 dprintk("<-- %s: return %d\n", __func__, ret);
4575 return ret;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004576
Andy Adamsonfc931582009-04-01 09:22:31 -04004577out_free:
4578 kfree(slot);
4579 goto out;
4580}
4581
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004582/*
4583 * Initialize the forechannel and backchannel tables
4584 */
4585static int nfs4_init_slot_tables(struct nfs4_session *session)
4586{
4587 int status;
4588
4589 status = nfs4_init_slot_table(&session->fc_slot_table,
4590 session->fc_attrs.max_reqs, 1);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004591 if (status)
4592 return status;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004593
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004594 status = nfs4_init_slot_table(&session->bc_slot_table,
4595 session->bc_attrs.max_reqs, 0);
4596 if (status)
4597 nfs4_destroy_slot_tables(session);
4598
4599 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04004600}
4601
4602struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4603{
4604 struct nfs4_session *session;
4605 struct nfs4_slot_table *tbl;
4606
4607 session = kzalloc(sizeof(struct nfs4_session), GFP_KERNEL);
4608 if (!session)
4609 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04004610
Andy Adamson76db6d92009-04-01 09:22:38 -04004611 /*
4612 * The create session reply races with the server back
4613 * channel probe. Mark the client NFS_CS_SESSION_INITING
4614 * so that the client back channel can find the
4615 * nfs_client struct
4616 */
4617 clp->cl_cons_state = NFS_CS_SESSION_INITING;
Andy Adamsonea028ac2009-12-04 15:55:38 -05004618 init_completion(&session->complete);
Andy Adamson76db6d92009-04-01 09:22:38 -04004619
Andy Adamson557134a2009-04-01 09:21:53 -04004620 tbl = &session->fc_slot_table;
4621 spin_lock_init(&tbl->slot_tbl_lock);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004622 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
4623
4624 tbl = &session->bc_slot_table;
4625 spin_lock_init(&tbl->slot_tbl_lock);
4626 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
4627
Andy Adamson557134a2009-04-01 09:21:53 -04004628 session->clp = clp;
4629 return session;
4630}
4631
4632void nfs4_destroy_session(struct nfs4_session *session)
4633{
Andy Adamson5a0ffe52009-04-01 09:23:18 -04004634 nfs4_proc_destroy_session(session);
4635 dprintk("%s Destroy backchannel for xprt %p\n",
4636 __func__, session->clp->cl_rpcclient->cl_xprt);
4637 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
4638 NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004639 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04004640 kfree(session);
4641}
4642
Andy Adamsonfc931582009-04-01 09:22:31 -04004643/*
4644 * Initialize the values to be used by the client in CREATE_SESSION
4645 * If nfs4_init_session set the fore channel request and response sizes,
4646 * use them.
4647 *
4648 * Set the back channel max_resp_sz_cached to zero to force the client to
4649 * always set csa_cachethis to FALSE because the current implementation
4650 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4651 */
4652static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
4653{
4654 struct nfs4_session *session = args->client->cl_session;
4655 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
4656 mxresp_sz = session->fc_attrs.max_resp_sz;
4657
4658 if (mxrqst_sz == 0)
4659 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
4660 if (mxresp_sz == 0)
4661 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
4662 /* Fore channel attributes */
4663 args->fc_attrs.headerpadsz = 0;
4664 args->fc_attrs.max_rqst_sz = mxrqst_sz;
4665 args->fc_attrs.max_resp_sz = mxresp_sz;
4666 args->fc_attrs.max_resp_sz_cached = mxresp_sz;
4667 args->fc_attrs.max_ops = NFS4_MAX_OPS;
4668 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
4669
4670 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
4671 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4672 __func__,
4673 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
4674 args->fc_attrs.max_resp_sz_cached, args->fc_attrs.max_ops,
4675 args->fc_attrs.max_reqs);
4676
4677 /* Back channel attributes */
4678 args->bc_attrs.headerpadsz = 0;
4679 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
4680 args->bc_attrs.max_resp_sz = PAGE_SIZE;
4681 args->bc_attrs.max_resp_sz_cached = 0;
4682 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
4683 args->bc_attrs.max_reqs = 1;
4684
4685 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
4686 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4687 __func__,
4688 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
4689 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
4690 args->bc_attrs.max_reqs);
4691}
4692
Andy Adamson8d353012009-04-01 09:22:32 -04004693static int _verify_channel_attr(char *chan, char *attr_name, u32 sent, u32 rcvd)
4694{
4695 if (rcvd <= sent)
4696 return 0;
4697 printk(KERN_WARNING "%s: Session INVALID: %s channel %s increased. "
4698 "sent=%u rcvd=%u\n", __func__, chan, attr_name, sent, rcvd);
4699 return -EINVAL;
4700}
4701
4702#define _verify_fore_channel_attr(_name_) \
4703 _verify_channel_attr("fore", #_name_, \
4704 args->fc_attrs._name_, \
4705 session->fc_attrs._name_)
4706
4707#define _verify_back_channel_attr(_name_) \
4708 _verify_channel_attr("back", #_name_, \
4709 args->bc_attrs._name_, \
4710 session->bc_attrs._name_)
4711
4712/*
4713 * The server is not allowed to increase the fore channel header pad size,
4714 * maximum response size, or maximum number of operations.
4715 *
4716 * The back channel attributes are only negotiatied down: We send what the
4717 * (back channel) server insists upon.
4718 */
4719static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
4720 struct nfs4_session *session)
4721{
4722 int ret = 0;
4723
4724 ret |= _verify_fore_channel_attr(headerpadsz);
4725 ret |= _verify_fore_channel_attr(max_resp_sz);
4726 ret |= _verify_fore_channel_attr(max_ops);
4727
4728 ret |= _verify_back_channel_attr(headerpadsz);
4729 ret |= _verify_back_channel_attr(max_rqst_sz);
4730 ret |= _verify_back_channel_attr(max_resp_sz);
4731 ret |= _verify_back_channel_attr(max_resp_sz_cached);
4732 ret |= _verify_back_channel_attr(max_ops);
4733 ret |= _verify_back_channel_attr(max_reqs);
4734
4735 return ret;
4736}
4737
Andy Adamsonfc931582009-04-01 09:22:31 -04004738static int _nfs4_proc_create_session(struct nfs_client *clp)
4739{
4740 struct nfs4_session *session = clp->cl_session;
4741 struct nfs41_create_session_args args = {
4742 .client = clp,
4743 .cb_program = NFS4_CALLBACK,
4744 };
4745 struct nfs41_create_session_res res = {
4746 .client = clp,
4747 };
4748 struct rpc_message msg = {
4749 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
4750 .rpc_argp = &args,
4751 .rpc_resp = &res,
4752 };
4753 int status;
4754
4755 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04004756 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04004757
4758 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4759
Andy Adamson8d353012009-04-01 09:22:32 -04004760 if (!status)
4761 /* Verify the session's negotiated channel_attrs values */
4762 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04004763 if (!status) {
4764 /* Increment the clientid slot sequence id */
4765 clp->cl_seqid++;
4766 }
4767
4768 return status;
4769}
4770
4771/*
4772 * Issues a CREATE_SESSION operation to the server.
4773 * It is the responsibility of the caller to verify the session is
4774 * expired before calling this routine.
4775 */
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004776int nfs4_proc_create_session(struct nfs_client *clp, int reset)
Andy Adamsonfc931582009-04-01 09:22:31 -04004777{
4778 int status;
4779 unsigned *ptr;
4780 struct nfs_fsinfo fsinfo;
4781 struct nfs4_session *session = clp->cl_session;
4782
4783 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
4784
4785 status = _nfs4_proc_create_session(clp);
4786 if (status)
4787 goto out;
4788
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004789 /* Init or reset the fore channel */
4790 if (reset)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004791 status = nfs4_reset_slot_tables(session);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004792 else
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004793 status = nfs4_init_slot_tables(session);
Andy Adamsonfc931582009-04-01 09:22:31 -04004794 dprintk("fore channel slot table initialization returned %d\n", status);
4795 if (status)
4796 goto out;
4797
4798 ptr = (unsigned *)&session->sess_id.data[0];
4799 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
4800 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
4801
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004802 if (reset)
4803 /* Lease time is aleady set */
4804 goto out;
4805
Andy Adamsonfc931582009-04-01 09:22:31 -04004806 /* Get the lease time */
4807 status = nfs4_proc_get_lease_time(clp, &fsinfo);
4808 if (status == 0) {
4809 /* Update lease time and schedule renewal */
4810 spin_lock(&clp->cl_lock);
4811 clp->cl_lease_time = fsinfo.lease_time * HZ;
4812 clp->cl_last_renewal = jiffies;
4813 clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
4814 spin_unlock(&clp->cl_lock);
4815
4816 nfs4_schedule_state_renewal(clp);
4817 }
4818out:
4819 dprintk("<-- %s\n", __func__);
4820 return status;
4821}
4822
Andy Adamson0f3e66c2009-04-01 09:22:34 -04004823/*
4824 * Issue the over-the-wire RPC DESTROY_SESSION.
4825 * The caller must serialize access to this routine.
4826 */
4827int nfs4_proc_destroy_session(struct nfs4_session *session)
4828{
4829 int status = 0;
4830 struct rpc_message msg;
4831
4832 dprintk("--> nfs4_proc_destroy_session\n");
4833
4834 /* session is still being setup */
4835 if (session->clp->cl_cons_state != NFS_CS_READY)
4836 return status;
4837
4838 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
4839 msg.rpc_argp = session;
4840 msg.rpc_resp = NULL;
4841 msg.rpc_cred = NULL;
4842 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4843
4844 if (status)
4845 printk(KERN_WARNING
4846 "Got error %d from the server on DESTROY_SESSION. "
4847 "Session has been destroyed regardless...\n", status);
4848
4849 dprintk("<-- nfs4_proc_destroy_session\n");
4850 return status;
4851}
4852
Trond Myklebustfccba802009-07-21 16:48:07 -04004853int nfs4_init_session(struct nfs_server *server)
4854{
4855 struct nfs_client *clp = server->nfs_client;
4856 int ret;
4857
4858 if (!nfs4_has_session(clp))
4859 return 0;
4860
4861 clp->cl_session->fc_attrs.max_rqst_sz = server->wsize;
4862 clp->cl_session->fc_attrs.max_resp_sz = server->rsize;
4863 ret = nfs4_recover_expired_lease(server);
4864 if (!ret)
4865 ret = nfs4_check_client_ready(clp);
4866 return ret;
4867}
4868
Andy Adamsonfc01cea2009-04-01 09:22:36 -04004869/*
4870 * Renew the cl_session lease.
4871 */
4872static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
4873{
4874 struct nfs4_sequence_args args;
4875 struct nfs4_sequence_res res;
4876
4877 struct rpc_message msg = {
4878 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
4879 .rpc_argp = &args,
4880 .rpc_resp = &res,
4881 .rpc_cred = cred,
4882 };
4883
4884 args.sa_cache_this = 0;
4885
4886 return nfs4_call_sync_sequence(clp, clp->cl_rpcclient, &msg, &args,
4887 &res, 0);
4888}
4889
4890void nfs41_sequence_call_done(struct rpc_task *task, void *data)
4891{
4892 struct nfs_client *clp = (struct nfs_client *)data;
4893
4894 nfs41_sequence_done(clp, task->tk_msg.rpc_resp, task->tk_status);
4895
4896 if (task->tk_status < 0) {
4897 dprintk("%s ERROR %d\n", __func__, task->tk_status);
4898
4899 if (_nfs4_async_handle_error(task, NULL, clp, NULL)
4900 == -EAGAIN) {
Andy Adamsone608e792009-12-04 15:55:29 -05004901 nfs4_restart_rpc(task, clp, task->tk_msg.rpc_resp);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04004902 return;
4903 }
4904 }
4905 nfs41_sequence_free_slot(clp, task->tk_msg.rpc_resp);
4906 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
4907
Andy Adamsonfc01cea2009-04-01 09:22:36 -04004908 kfree(task->tk_msg.rpc_argp);
4909 kfree(task->tk_msg.rpc_resp);
4910
4911 dprintk("<-- %s\n", __func__);
4912}
4913
4914static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
4915{
4916 struct nfs_client *clp;
4917 struct nfs4_sequence_args *args;
4918 struct nfs4_sequence_res *res;
4919
4920 clp = (struct nfs_client *)data;
4921 args = task->tk_msg.rpc_argp;
4922 res = task->tk_msg.rpc_resp;
4923
4924 if (nfs4_setup_sequence(clp, args, res, 0, task))
4925 return;
4926 rpc_call_start(task);
4927}
4928
4929static const struct rpc_call_ops nfs41_sequence_ops = {
4930 .rpc_call_done = nfs41_sequence_call_done,
4931 .rpc_call_prepare = nfs41_sequence_prepare,
4932};
4933
4934static int nfs41_proc_async_sequence(struct nfs_client *clp,
4935 struct rpc_cred *cred)
4936{
4937 struct nfs4_sequence_args *args;
4938 struct nfs4_sequence_res *res;
4939 struct rpc_message msg = {
4940 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
4941 .rpc_cred = cred,
4942 };
4943
4944 args = kzalloc(sizeof(*args), GFP_KERNEL);
4945 if (!args)
4946 return -ENOMEM;
4947 res = kzalloc(sizeof(*res), GFP_KERNEL);
4948 if (!res) {
4949 kfree(args);
4950 return -ENOMEM;
4951 }
4952 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
4953 msg.rpc_argp = args;
4954 msg.rpc_resp = res;
4955
4956 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
4957 &nfs41_sequence_ops, (void *)clp);
4958}
4959
Andy Adamson557134a2009-04-01 09:21:53 -04004960#endif /* CONFIG_NFS_V4_1 */
4961
Andy Adamson591d71c2009-04-01 09:22:47 -04004962struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05004963 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05004964 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965 .recover_open = nfs4_open_reclaim,
4966 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04004967 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04004968 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969};
4970
Andy Adamson591d71c2009-04-01 09:22:47 -04004971#if defined(CONFIG_NFS_V4_1)
4972struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
4973 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
4974 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
4975 .recover_open = nfs4_open_reclaim,
4976 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05004977 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04004978 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04004979};
4980#endif /* CONFIG_NFS_V4_1 */
4981
4982struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05004983 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05004984 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985 .recover_open = nfs4_open_expired,
4986 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04004987 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04004988 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989};
4990
Andy Adamson591d71c2009-04-01 09:22:47 -04004991#if defined(CONFIG_NFS_V4_1)
4992struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
4993 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
4994 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
4995 .recover_open = nfs4_open_expired,
4996 .recover_lock = nfs4_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05004997 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04004998 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04004999};
5000#endif /* CONFIG_NFS_V4_1 */
5001
Benny Halevy29fba382009-04-01 09:22:44 -04005002struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
5003 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04005004 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005005 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04005006};
5007
5008#if defined(CONFIG_NFS_V4_1)
5009struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
5010 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04005011 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005012 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04005013};
5014#endif
5015
5016/*
5017 * Per minor version reboot and network partition recovery ops
5018 */
5019
Andy Adamson591d71c2009-04-01 09:22:47 -04005020struct nfs4_state_recovery_ops *nfs4_reboot_recovery_ops[] = {
5021 &nfs40_reboot_recovery_ops,
5022#if defined(CONFIG_NFS_V4_1)
5023 &nfs41_reboot_recovery_ops,
5024#endif
5025};
5026
5027struct nfs4_state_recovery_ops *nfs4_nograce_recovery_ops[] = {
5028 &nfs40_nograce_recovery_ops,
5029#if defined(CONFIG_NFS_V4_1)
5030 &nfs41_nograce_recovery_ops,
5031#endif
5032};
5033
Benny Halevy29fba382009-04-01 09:22:44 -04005034struct nfs4_state_maintenance_ops *nfs4_state_renewal_ops[] = {
5035 &nfs40_state_renewal_ops,
5036#if defined(CONFIG_NFS_V4_1)
5037 &nfs41_state_renewal_ops,
5038#endif
5039};
5040
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08005041static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005042 .permission = nfs_permission,
5043 .getattr = nfs_getattr,
5044 .setattr = nfs_setattr,
5045 .getxattr = nfs4_getxattr,
5046 .setxattr = nfs4_setxattr,
5047 .listxattr = nfs4_listxattr,
5048};
5049
David Howells509de812006-08-22 20:06:11 -04005050const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051 .version = 4, /* protocol version */
5052 .dentry_ops = &nfs4_dentry_operations,
5053 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005054 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005055 .getroot = nfs4_proc_get_root,
5056 .getattr = nfs4_proc_getattr,
5057 .setattr = nfs4_proc_setattr,
David Howells2b3de442006-08-22 20:06:09 -04005058 .lookupfh = nfs4_proc_lookupfh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059 .lookup = nfs4_proc_lookup,
5060 .access = nfs4_proc_access,
5061 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062 .create = nfs4_proc_create,
5063 .remove = nfs4_proc_remove,
5064 .unlink_setup = nfs4_proc_unlink_setup,
5065 .unlink_done = nfs4_proc_unlink_done,
5066 .rename = nfs4_proc_rename,
5067 .link = nfs4_proc_link,
5068 .symlink = nfs4_proc_symlink,
5069 .mkdir = nfs4_proc_mkdir,
5070 .rmdir = nfs4_proc_remove,
5071 .readdir = nfs4_proc_readdir,
5072 .mknod = nfs4_proc_mknod,
5073 .statfs = nfs4_proc_statfs,
5074 .fsinfo = nfs4_proc_fsinfo,
5075 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04005076 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077 .decode_dirent = nfs4_decode_dirent,
5078 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05005079 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005081 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005083 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005085 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04005086 .close_context = nfs4_close_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087};
5088
5089/*
5090 * Local variables:
5091 * c-basic-offset: 8
5092 * End:
5093 */