blob: b6308f6740ec3164f1598ea324f1779313375ece [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>
39#include <linux/utsname.h>
40#include <linux/delay.h>
41#include <linux/errno.h>
42#include <linux/string.h>
43#include <linux/sunrpc/clnt.h>
44#include <linux/nfs.h>
45#include <linux/nfs4.h>
46#include <linux/nfs_fs.h>
47#include <linux/nfs_page.h>
48#include <linux/smp_lock.h>
49#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070050#include <linux/mount.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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57#define NFSDBG_FACILITY NFSDBG_PROC
58
Trond Myklebust2066fe82006-09-15 08:30:46 -040059#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#define NFS4_POLL_RETRY_MAX (15*HZ)
61
Trond Myklebustcdd4e682006-01-03 09:55:12 +010062struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010063static int _nfs4_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050065static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Trond Myklebust99367812007-07-17 21:52:41 -040066static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
67static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Linus Torvalds1da177e2005-04-16 15:20:36 -070069/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050070static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
72 if (err < -1000) {
73 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -070074 __func__, -err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 return -EIO;
76 }
77 return err;
78}
79
80/*
81 * This is our standard bitmap for GETATTR requests.
82 */
83const u32 nfs4_fattr_bitmap[2] = {
84 FATTR4_WORD0_TYPE
85 | FATTR4_WORD0_CHANGE
86 | FATTR4_WORD0_SIZE
87 | FATTR4_WORD0_FSID
88 | FATTR4_WORD0_FILEID,
89 FATTR4_WORD1_MODE
90 | FATTR4_WORD1_NUMLINKS
91 | FATTR4_WORD1_OWNER
92 | FATTR4_WORD1_OWNER_GROUP
93 | FATTR4_WORD1_RAWDEV
94 | FATTR4_WORD1_SPACE_USED
95 | FATTR4_WORD1_TIME_ACCESS
96 | FATTR4_WORD1_TIME_METADATA
97 | FATTR4_WORD1_TIME_MODIFY
98};
99
100const u32 nfs4_statfs_bitmap[2] = {
101 FATTR4_WORD0_FILES_AVAIL
102 | FATTR4_WORD0_FILES_FREE
103 | FATTR4_WORD0_FILES_TOTAL,
104 FATTR4_WORD1_SPACE_AVAIL
105 | FATTR4_WORD1_SPACE_FREE
106 | FATTR4_WORD1_SPACE_TOTAL
107};
108
Trond Myklebust4ce79712005-06-22 17:16:21 +0000109const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 FATTR4_WORD0_MAXLINK
111 | FATTR4_WORD0_MAXNAME,
112 0
113};
114
115const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
116 | FATTR4_WORD0_MAXREAD
117 | FATTR4_WORD0_MAXWRITE
118 | FATTR4_WORD0_LEASE_TIME,
119 0
120};
121
Manoj Naik830b8e32006-06-09 09:34:25 -0400122const u32 nfs4_fs_locations_bitmap[2] = {
123 FATTR4_WORD0_TYPE
124 | FATTR4_WORD0_CHANGE
125 | FATTR4_WORD0_SIZE
126 | FATTR4_WORD0_FSID
127 | FATTR4_WORD0_FILEID
128 | FATTR4_WORD0_FS_LOCATIONS,
129 FATTR4_WORD1_MODE
130 | FATTR4_WORD1_NUMLINKS
131 | FATTR4_WORD1_OWNER
132 | FATTR4_WORD1_OWNER_GROUP
133 | FATTR4_WORD1_RAWDEV
134 | FATTR4_WORD1_SPACE_USED
135 | FATTR4_WORD1_TIME_ACCESS
136 | FATTR4_WORD1_TIME_METADATA
137 | FATTR4_WORD1_TIME_MODIFY
138 | FATTR4_WORD1_MOUNTED_ON_FILEID
139};
140
Al Virobc4785c2006-10-19 23:28:51 -0700141static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 struct nfs4_readdir_arg *readdir)
143{
Al Viro0dbb4c62006-10-19 23:28:49 -0700144 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 BUG_ON(readdir->count < 80);
147 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000148 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
150 return;
151 }
152
153 readdir->cookie = 0;
154 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
155 if (cookie == 2)
156 return;
157
158 /*
159 * NFSv4 servers do not return entries for '.' and '..'
160 * Therefore, we fake these entries here. We let '.'
161 * have cookie 0 and '..' have cookie 1. Note that
162 * when talking to the server, we always send cookie 0
163 * instead of 1 or 2.
164 */
Al Viro0dbb4c62006-10-19 23:28:49 -0700165 start = p = kmap_atomic(*readdir->pages, KM_USER0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 if (cookie == 0) {
168 *p++ = xdr_one; /* next */
169 *p++ = xdr_zero; /* cookie, first word */
170 *p++ = xdr_one; /* cookie, second word */
171 *p++ = xdr_one; /* entry len */
172 memcpy(p, ".\0\0\0", 4); /* entry */
173 p++;
174 *p++ = xdr_one; /* bitmap length */
175 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
176 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400177 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 }
179
180 *p++ = xdr_one; /* next */
181 *p++ = xdr_zero; /* cookie, first word */
182 *p++ = xdr_two; /* cookie, second word */
183 *p++ = xdr_two; /* entry len */
184 memcpy(p, "..\0\0", 4); /* entry */
185 p++;
186 *p++ = xdr_one; /* bitmap length */
187 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
188 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400189 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191 readdir->pgbase = (char *)p - (char *)start;
192 readdir->count -= readdir->pgbase;
193 kunmap_atomic(start, KM_USER0);
194}
195
Trond Myklebust65de8722008-12-23 15:21:44 -0500196static int nfs4_wait_clnt_recover(struct nfs_client *clp)
197{
198 int res;
199
200 might_sleep();
201
Trond Myklebuste005e802008-12-23 15:21:48 -0500202 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400203 nfs_wait_bit_killable, TASK_KILLABLE);
Trond Myklebust65de8722008-12-23 15:21:44 -0500204 return res;
205}
206
207static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
208{
209 int res = 0;
210
211 might_sleep();
212
213 if (*timeout <= 0)
214 *timeout = NFS4_POLL_RETRY_MIN;
215 if (*timeout > NFS4_POLL_RETRY_MAX)
216 *timeout = NFS4_POLL_RETRY_MAX;
217 schedule_timeout_killable(*timeout);
218 if (fatal_signal_pending(current))
219 res = -ERESTARTSYS;
220 *timeout <<= 1;
221 return res;
222}
223
224/* This is the error handling routine for processes that are allowed
225 * to sleep.
226 */
227static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
228{
229 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500230 struct nfs4_state *state = exception->state;
Trond Myklebust65de8722008-12-23 15:21:44 -0500231 int ret = errorcode;
232
233 exception->retry = 0;
234 switch(errorcode) {
235 case 0:
236 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500237 case -NFS4ERR_ADMIN_REVOKED:
238 case -NFS4ERR_BAD_STATEID:
239 case -NFS4ERR_OPENMODE:
240 if (state == NULL)
241 break;
242 nfs4_state_mark_reclaim_nograce(clp, state);
Trond Myklebust65de8722008-12-23 15:21:44 -0500243 case -NFS4ERR_STALE_CLIENTID:
244 case -NFS4ERR_STALE_STATEID:
245 case -NFS4ERR_EXPIRED:
246 nfs4_schedule_state_recovery(clp);
247 ret = nfs4_wait_clnt_recover(clp);
248 if (ret == 0)
249 exception->retry = 1;
250 break;
251 case -NFS4ERR_FILE_OPEN:
252 case -NFS4ERR_GRACE:
253 case -NFS4ERR_DELAY:
254 ret = nfs4_delay(server->client, &exception->timeout);
255 if (ret != 0)
256 break;
257 case -NFS4ERR_OLD_STATEID:
258 exception->retry = 1;
259 }
260 /* We failed to handle the error */
261 return nfs4_map_errors(ret);
262}
263
264
Trond Myklebust26e976a2006-01-03 09:55:21 +0100265static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
David Howells7539bba2006-08-22 20:06:09 -0400267 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 spin_lock(&clp->cl_lock);
269 if (time_before(clp->cl_last_renewal,timestamp))
270 clp->cl_last_renewal = timestamp;
271 spin_unlock(&clp->cl_lock);
272}
273
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400274#if defined(CONFIG_NFS_V4_1)
275
Benny Halevy510b8172009-04-01 09:22:14 -0400276/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400277 * nfs4_free_slot - free a slot and efficiently update slot table.
278 *
279 * freeing a slot is trivially done by clearing its respective bit
280 * in the bitmap.
281 * If the freed slotid equals highest_used_slotid we want to update it
282 * so that the server would be able to size down the slot table if needed,
283 * otherwise we know that the highest_used_slotid is still in use.
284 * When updating highest_used_slotid there may be "holes" in the bitmap
285 * so we need to scan down from highest_used_slotid to 0 looking for the now
286 * highest slotid in use.
287 * If none found, highest_used_slotid is set to -1.
288 */
289static void
290nfs4_free_slot(struct nfs4_slot_table *tbl, u8 free_slotid)
291{
292 int slotid = free_slotid;
293
294 spin_lock(&tbl->slot_tbl_lock);
295 /* clear used bit in bitmap */
296 __clear_bit(slotid, tbl->used_slots);
297
298 /* update highest_used_slotid when it is freed */
299 if (slotid == tbl->highest_used_slotid) {
300 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
301 if (slotid >= 0 && slotid < tbl->max_slots)
302 tbl->highest_used_slotid = slotid;
303 else
304 tbl->highest_used_slotid = -1;
305 }
306 rpc_wake_up_next(&tbl->slot_tbl_waitq);
307 spin_unlock(&tbl->slot_tbl_lock);
308 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
309 free_slotid, tbl->highest_used_slotid);
310}
311
312/*
Benny Halevy510b8172009-04-01 09:22:14 -0400313 * nfs4_find_slot - efficiently look for a free slot
314 *
315 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
316 * If found, we mark the slot as used, update the highest_used_slotid,
317 * and respectively set up the sequence operation args.
318 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400319 *
320 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400321 */
322static u8
323nfs4_find_slot(struct nfs4_slot_table *tbl, struct rpc_task *task)
324{
325 int slotid;
326 u8 ret_id = NFS4_MAX_SLOT_TABLE;
327 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
328
Benny Halevy510b8172009-04-01 09:22:14 -0400329 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
330 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
331 tbl->max_slots);
332 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
333 if (slotid >= tbl->max_slots)
334 goto out;
335 __set_bit(slotid, tbl->used_slots);
336 if (slotid > tbl->highest_used_slotid)
337 tbl->highest_used_slotid = slotid;
338 ret_id = slotid;
339out:
340 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
341 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400342 return ret_id;
343}
344
Andy Adamsonce5039c2009-04-01 09:22:13 -0400345static int nfs41_setup_sequence(struct nfs4_session *session,
346 struct nfs4_sequence_args *args,
347 struct nfs4_sequence_res *res,
348 int cache_reply,
349 struct rpc_task *task)
350{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400351 struct nfs4_slot *slot;
352 struct nfs4_slot_table *tbl;
353 u8 slotid;
354
355 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400356 /* slot already allocated? */
357 if (res->sr_slotid != NFS4_MAX_SLOT_TABLE)
358 return 0;
359
360 memset(res, 0, sizeof(*res));
361 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400362 tbl = &session->fc_slot_table;
363
364 spin_lock(&tbl->slot_tbl_lock);
365 slotid = nfs4_find_slot(tbl, task);
366 if (slotid == NFS4_MAX_SLOT_TABLE) {
367 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
368 spin_unlock(&tbl->slot_tbl_lock);
369 dprintk("<-- %s: no free slots\n", __func__);
370 return -EAGAIN;
371 }
372 spin_unlock(&tbl->slot_tbl_lock);
373
374 slot = tbl->slots + slotid;
375 args->sa_slotid = slotid;
376 args->sa_cache_this = cache_reply;
377
378 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
379
380 res->sr_slotid = slotid;
381 res->sr_renewal_time = jiffies;
382 /*
383 * sr_status is only set in decode_sequence, and so will remain
384 * set to 1 if an rpc level failure occurs.
385 */
386 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400387 return 0;
388}
389
390int nfs4_setup_sequence(struct nfs_client *clp,
391 struct nfs4_sequence_args *args,
392 struct nfs4_sequence_res *res,
393 int cache_reply,
394 struct rpc_task *task)
395{
396 int ret = 0;
397
398 dprintk("--> %s clp %p session %p sr_slotid %d\n",
399 __func__, clp, clp->cl_session, res->sr_slotid);
400
401 if (!nfs4_has_session(clp))
402 goto out;
403 ret = nfs41_setup_sequence(clp->cl_session, args, res, cache_reply,
404 task);
405 if (ret != -EAGAIN) {
406 /* terminate rpc task */
407 task->tk_status = ret;
408 task->tk_action = NULL;
409 }
410out:
411 dprintk("<-- %s status=%d\n", __func__, ret);
412 return ret;
413}
414
415struct nfs41_call_sync_data {
416 struct nfs_client *clp;
417 struct nfs4_sequence_args *seq_args;
418 struct nfs4_sequence_res *seq_res;
419 int cache_reply;
420};
421
422static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
423{
424 struct nfs41_call_sync_data *data = calldata;
425
426 dprintk("--> %s data->clp->cl_session %p\n", __func__,
427 data->clp->cl_session);
428 if (nfs4_setup_sequence(data->clp, data->seq_args,
429 data->seq_res, data->cache_reply, task))
430 return;
431 rpc_call_start(task);
432}
433
434struct rpc_call_ops nfs41_call_sync_ops = {
435 .rpc_call_prepare = nfs41_call_sync_prepare,
436};
437
438static int nfs4_call_sync_sequence(struct nfs_client *clp,
439 struct rpc_clnt *clnt,
440 struct rpc_message *msg,
441 struct nfs4_sequence_args *args,
442 struct nfs4_sequence_res *res,
443 int cache_reply)
444{
445 int ret;
446 struct rpc_task *task;
447 struct nfs41_call_sync_data data = {
448 .clp = clp,
449 .seq_args = args,
450 .seq_res = res,
451 .cache_reply = cache_reply,
452 };
453 struct rpc_task_setup task_setup = {
454 .rpc_client = clnt,
455 .rpc_message = msg,
456 .callback_ops = &nfs41_call_sync_ops,
457 .callback_data = &data
458 };
459
460 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
461 task = rpc_run_task(&task_setup);
462 if (IS_ERR(task))
463 ret = PTR_ERR(task);
464 else {
465 ret = task->tk_status;
466 rpc_put_task(task);
467 }
468 return ret;
469}
470
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400471int _nfs4_call_sync_session(struct nfs_server *server,
472 struct rpc_message *msg,
473 struct nfs4_sequence_args *args,
474 struct nfs4_sequence_res *res,
475 int cache_reply)
476{
Andy Adamsonce5039c2009-04-01 09:22:13 -0400477 return nfs4_call_sync_sequence(server->nfs_client, server->client,
478 msg, args, res, cache_reply);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400479}
480
481#endif /* CONFIG_NFS_V4_1 */
482
483int _nfs4_call_sync(struct nfs_server *server,
484 struct rpc_message *msg,
485 struct nfs4_sequence_args *args,
486 struct nfs4_sequence_res *res,
487 int cache_reply)
488{
Benny Halevyf3752972009-04-01 09:22:04 -0400489 args->sa_session = res->sr_session = NULL;
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400490 return rpc_call_sync(server->client, msg, 0);
491}
492
493#define nfs4_call_sync(server, msg, args, res, cache_reply) \
494 (server)->nfs_client->cl_call_sync((server), (msg), &(args)->seq_args, \
495 &(res)->seq_res, (cache_reply))
496
Trond Myklebust38478b22006-05-25 01:40:57 -0400497static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
Trond Myklebust38478b22006-05-25 01:40:57 -0400499 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Trond Myklebust38478b22006-05-25 01:40:57 -0400501 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400502 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
503 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400504 nfs_force_lookup_revalidate(dir);
Trond Myklebust40d24702007-10-08 09:24:22 -0400505 nfsi->change_attr = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400506 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507}
508
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100509struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400510 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100511 struct nfs_openargs o_arg;
512 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100513 struct nfs_open_confirmargs c_arg;
514 struct nfs_open_confirmres c_res;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100515 struct nfs_fattr f_attr;
516 struct nfs_fattr dir_attr;
Trond Myklebustad389da2007-06-05 12:30:00 -0400517 struct path path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100518 struct dentry *dir;
519 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400520 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100521 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100522 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400523 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100524 int rpc_status;
525 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100526};
527
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400528
529static void nfs4_init_opendata_res(struct nfs4_opendata *p)
530{
531 p->o_res.f_attr = &p->f_attr;
532 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400533 p->o_res.seqid = p->o_arg.seqid;
534 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400535 p->o_res.server = p->o_arg.server;
536 nfs_fattr_init(&p->f_attr);
537 nfs_fattr_init(&p->dir_attr);
538}
539
Trond Myklebustad389da2007-06-05 12:30:00 -0400540static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500541 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100542 const struct iattr *attrs)
543{
Trond Myklebustad389da2007-06-05 12:30:00 -0400544 struct dentry *parent = dget_parent(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100545 struct inode *dir = parent->d_inode;
546 struct nfs_server *server = NFS_SERVER(dir);
547 struct nfs4_opendata *p;
548
549 p = kzalloc(sizeof(*p), GFP_KERNEL);
550 if (p == NULL)
551 goto err;
552 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
553 if (p->o_arg.seqid == NULL)
554 goto err_free;
Trond Myklebustad389da2007-06-05 12:30:00 -0400555 p->path.mnt = mntget(path->mnt);
556 p->path.dentry = dget(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100557 p->dir = parent;
558 p->owner = sp;
559 atomic_inc(&sp->so_count);
560 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500561 p->o_arg.open_flags = flags;
562 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400563 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400564 p->o_arg.id = sp->so_owner_id.id;
Trond Myklebustad389da2007-06-05 12:30:00 -0400565 p->o_arg.name = &p->path.dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100566 p->o_arg.server = server;
567 p->o_arg.bitmask = server->attr_bitmask;
568 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Andy Adamson5f7dbd52009-04-01 09:22:05 -0400569 p->o_res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100570 if (flags & O_EXCL) {
571 u32 *s = (u32 *) p->o_arg.u.verifier.data;
572 s[0] = jiffies;
573 s[1] = current->pid;
574 } else if (flags & O_CREAT) {
575 p->o_arg.u.attrs = &p->attrs;
576 memcpy(&p->attrs, attrs, sizeof(p->attrs));
577 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100578 p->c_arg.fh = &p->o_res.fh;
579 p->c_arg.stateid = &p->o_res.stateid;
580 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400581 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400582 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100583 return p;
584err_free:
585 kfree(p);
586err:
587 dput(parent);
588 return NULL;
589}
590
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400591static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100592{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400593 struct nfs4_opendata *p = container_of(kref,
594 struct nfs4_opendata, kref);
595
596 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400597 if (p->state != NULL)
598 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400599 nfs4_put_state_owner(p->owner);
600 dput(p->dir);
Jan Blunck31f31db12008-05-02 13:42:45 -0700601 path_put(&p->path);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400602 kfree(p);
603}
604
605static void nfs4_opendata_put(struct nfs4_opendata *p)
606{
607 if (p != NULL)
608 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100609}
610
Trond Myklebust06f814a2006-01-03 09:55:07 +0100611static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
612{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100613 int ret;
614
Trond Myklebust06f814a2006-01-03 09:55:07 +0100615 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100616 return ret;
617}
618
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500619static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400620{
621 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500622
623 if (open_mode & O_EXCL)
624 goto out;
625 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400626 case FMODE_READ:
627 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400628 break;
629 case FMODE_WRITE:
630 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400631 break;
632 case FMODE_READ|FMODE_WRITE:
633 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0;
634 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500635out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400636 return ret;
637}
638
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500639static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400640{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500641 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400642 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500643 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400644 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500645 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400646 return 1;
647}
648
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500649static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100650{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500651 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100652 case FMODE_WRITE:
653 state->n_wronly++;
654 break;
655 case FMODE_READ:
656 state->n_rdonly++;
657 break;
658 case FMODE_READ|FMODE_WRITE:
659 state->n_rdwr++;
660 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500661 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100662}
663
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500664static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400665{
666 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
667 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
668 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500669 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400670 case FMODE_READ:
671 set_bit(NFS_O_RDONLY_STATE, &state->flags);
672 break;
673 case FMODE_WRITE:
674 set_bit(NFS_O_WRONLY_STATE, &state->flags);
675 break;
676 case FMODE_READ|FMODE_WRITE:
677 set_bit(NFS_O_RDWR_STATE, &state->flags);
678 }
679}
680
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500681static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400682{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400683 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500684 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400685 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400686}
687
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500688static 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 -0700689{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400690 /*
691 * Protect the call to nfs4_state_set_mode_locked and
692 * serialise the stateid update
693 */
694 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400695 if (deleg_stateid != NULL) {
696 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
697 set_bit(NFS_DELEGATED_STATE, &state->flags);
698 }
699 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500700 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400701 write_sequnlock(&state->seqlock);
702 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500703 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700704 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705}
706
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500707static 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 -0500708{
709 struct nfs_inode *nfsi = NFS_I(state->inode);
710 struct nfs_delegation *deleg_cur;
711 int ret = 0;
712
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500713 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500714
715 rcu_read_lock();
716 deleg_cur = rcu_dereference(nfsi->delegation);
717 if (deleg_cur == NULL)
718 goto no_delegation;
719
720 spin_lock(&deleg_cur->lock);
721 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500722 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500723 goto no_delegation_unlock;
724
725 if (delegation == NULL)
726 delegation = &deleg_cur->stateid;
727 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
728 goto no_delegation_unlock;
729
Trond Myklebustb7391f42008-12-23 15:21:52 -0500730 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500731 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500732 ret = 1;
733no_delegation_unlock:
734 spin_unlock(&deleg_cur->lock);
735no_delegation:
736 rcu_read_unlock();
737
738 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500739 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500740 ret = 1;
741 }
742
743 return ret;
744}
745
746
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500747static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400748{
749 struct nfs_delegation *delegation;
750
751 rcu_read_lock();
752 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500753 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -0400754 rcu_read_unlock();
755 return;
756 }
757 rcu_read_unlock();
758 nfs_inode_return_delegation(inode);
759}
760
Trond Myklebust6ee41262007-07-08 14:11:36 -0400761static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400762{
763 struct nfs4_state *state = opendata->state;
764 struct nfs_inode *nfsi = NFS_I(state->inode);
765 struct nfs_delegation *delegation;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500766 int open_mode = opendata->o_arg.open_flags & O_EXCL;
767 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400768 nfs4_stateid stateid;
769 int ret = -EAGAIN;
770
Trond Myklebustaac00a82007-07-05 19:02:21 -0400771 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500772 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400773 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500774 if (can_open_cached(state, fmode, open_mode)) {
775 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400776 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400777 goto out_return_state;
778 }
779 spin_unlock(&state->owner->so_lock);
780 }
Trond Myklebust34310432008-12-23 15:21:38 -0500781 rcu_read_lock();
782 delegation = rcu_dereference(nfsi->delegation);
783 if (delegation == NULL ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500784 !can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -0500785 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -0400786 break;
Trond Myklebust34310432008-12-23 15:21:38 -0500787 }
Trond Myklebustaac00a82007-07-05 19:02:21 -0400788 /* Save the delegation */
789 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
790 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -0400791 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400792 if (ret != 0)
793 goto out;
794 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -0500795
796 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500797 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -0500798 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400799 }
Trond Myklebustaac00a82007-07-05 19:02:21 -0400800out:
801 return ERR_PTR(ret);
802out_return_state:
803 atomic_inc(&state->count);
804 return state;
805}
806
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100807static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
808{
809 struct inode *inode;
810 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -0400811 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400812 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100813
Trond Myklebustaac00a82007-07-05 19:02:21 -0400814 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400815 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400816 goto out;
817 }
818
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400819 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100820 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400821 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100822 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400823 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -0500824 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400825 goto err;
826 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100827 state = nfs4_get_open_state(inode, data->owner);
828 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400829 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -0400830 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -0400831 int delegation_flags = 0;
832
Trond Myklebust003707c2007-07-05 18:07:55 -0400833 rcu_read_lock();
834 delegation = rcu_dereference(NFS_I(inode)->delegation);
835 if (delegation)
836 delegation_flags = delegation->flags;
837 rcu_read_unlock();
Trond Myklebust15c831b2008-12-23 15:21:39 -0500838 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -0400839 nfs_inode_set_delegation(state->inode,
840 data->owner->so_cred,
841 &data->o_res);
842 else
843 nfs_inode_reclaim_delegation(state->inode,
844 data->owner->so_cred,
845 &data->o_res);
846 }
Trond Myklebust34310432008-12-23 15:21:38 -0500847
848 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500849 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100850 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400851out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100852 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400853err_put_inode:
854 iput(inode);
855err:
856 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100857}
858
Trond Myklebust864472e2006-01-03 09:55:15 +0100859static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
860{
861 struct nfs_inode *nfsi = NFS_I(state->inode);
862 struct nfs_open_context *ctx;
863
864 spin_lock(&state->inode->i_lock);
865 list_for_each_entry(ctx, &nfsi->open_files, list) {
866 if (ctx->state != state)
867 continue;
868 get_nfs_open_context(ctx);
869 spin_unlock(&state->inode->i_lock);
870 return ctx;
871 }
872 spin_unlock(&state->inode->i_lock);
873 return ERR_PTR(-ENOENT);
874}
875
Trond Myklebust6f220ed2007-07-17 21:50:45 -0400876static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
877{
878 struct nfs4_opendata *opendata;
879
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500880 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL);
Trond Myklebust6f220ed2007-07-17 21:50:45 -0400881 if (opendata == NULL)
882 return ERR_PTR(-ENOMEM);
883 opendata->state = state;
884 atomic_inc(&state->count);
885 return opendata;
886}
887
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500888static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +0100889{
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400890 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +0100891 int ret;
892
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500893 opendata->o_arg.open_flags = 0;
894 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400895 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
896 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
897 nfs4_init_opendata_res(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +0100898 ret = _nfs4_proc_open(opendata);
899 if (ret != 0)
900 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400901 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400902 if (IS_ERR(newstate))
903 return PTR_ERR(newstate);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500904 nfs4_close_state(&opendata->path, newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400905 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +0100906 return 0;
907}
908
909static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
910{
Trond Myklebust864472e2006-01-03 09:55:15 +0100911 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +0100912 int ret;
913
914 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400915 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +0100916 smp_rmb();
917 if (state->n_rdwr != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400918 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +0100919 if (ret != 0)
920 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400921 if (newstate != state)
922 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +0100923 }
924 if (state->n_wronly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400925 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +0100926 if (ret != 0)
927 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400928 if (newstate != state)
929 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +0100930 }
931 if (state->n_rdonly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400932 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +0100933 if (ret != 0)
934 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400935 if (newstate != state)
936 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +0100937 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -0400938 /*
939 * We may have performed cached opens for all three recoveries.
940 * Check if we need to update the current stateid.
941 */
942 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
943 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400944 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -0400945 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
946 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400947 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -0400948 }
Trond Myklebust864472e2006-01-03 09:55:15 +0100949 return 0;
950}
951
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952/*
953 * OPEN_RECLAIM:
954 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 */
Trond Myklebust539cd032007-06-05 11:46:42 -0400956static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -0400958 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +0100959 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500960 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 int status;
962
Trond Myklebust6f220ed2007-07-17 21:50:45 -0400963 opendata = nfs4_open_recoverdata_alloc(ctx, state);
964 if (IS_ERR(opendata))
965 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +0100966 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
967 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -0400968 rcu_read_lock();
969 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -0500970 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -0400971 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -0400972 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +0100973 opendata->o_arg.u.delegation_type = delegation_type;
974 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400975 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 return status;
977}
978
Trond Myklebust539cd032007-06-05 11:46:42 -0400979static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980{
981 struct nfs_server *server = NFS_SERVER(state->inode);
982 struct nfs4_exception exception = { };
983 int err;
984 do {
Trond Myklebust539cd032007-06-05 11:46:42 -0400985 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust202b50d2005-06-22 17:16:29 +0000986 if (err != -NFS4ERR_DELAY)
987 break;
988 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 } while (exception.retry);
990 return err;
991}
992
Trond Myklebust864472e2006-01-03 09:55:15 +0100993static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
994{
995 struct nfs_open_context *ctx;
996 int ret;
997
998 ctx = nfs4_state_find_open_context(state);
999 if (IS_ERR(ctx))
1000 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001001 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001002 put_nfs_open_context(ctx);
1003 return ret;
1004}
1005
Trond Myklebust13437e12007-07-06 15:10:43 -04001006static 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 -07001007{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001008 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001009 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001011 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1012 if (IS_ERR(opendata))
1013 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001014 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -04001015 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001016 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +01001017 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001018 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001019 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020}
1021
Trond Myklebust13437e12007-07-06 15:10:43 -04001022int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023{
1024 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001025 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 int err;
1027 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001028 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 switch (err) {
1030 case 0:
1031 return err;
1032 case -NFS4ERR_STALE_CLIENTID:
1033 case -NFS4ERR_STALE_STATEID:
1034 case -NFS4ERR_EXPIRED:
1035 /* Don't recall a delegation if it was lost */
David Howells7539bba2006-08-22 20:06:09 -04001036 nfs4_schedule_state_recovery(server->nfs_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 return err;
1038 }
1039 err = nfs4_handle_exception(server, err, &exception);
1040 } while (exception.retry);
1041 return err;
1042}
1043
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001044static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1045{
1046 struct nfs4_opendata *data = calldata;
1047
1048 data->rpc_status = task->tk_status;
1049 if (RPC_ASSASSINATED(task))
1050 return;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001051 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001052 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1053 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -05001054 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001055 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001056 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001057 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001058}
1059
1060static void nfs4_open_confirm_release(void *calldata)
1061{
1062 struct nfs4_opendata *data = calldata;
1063 struct nfs4_state *state = NULL;
1064
1065 /* If this request hasn't been cancelled, do nothing */
1066 if (data->cancelled == 0)
1067 goto out_free;
1068 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001069 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001070 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001071 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001072 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001073 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001074out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001075 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001076}
1077
1078static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001079 .rpc_call_done = nfs4_open_confirm_done,
1080 .rpc_release = nfs4_open_confirm_release,
1081};
1082
1083/*
1084 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1085 */
1086static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1087{
1088 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1089 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001090 struct rpc_message msg = {
1091 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1092 .rpc_argp = &data->c_arg,
1093 .rpc_resp = &data->c_res,
1094 .rpc_cred = data->owner->so_cred,
1095 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001096 struct rpc_task_setup task_setup_data = {
1097 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001098 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001099 .callback_ops = &nfs4_open_confirm_ops,
1100 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001101 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001102 .flags = RPC_TASK_ASYNC,
1103 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 int status;
1105
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001106 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001107 data->rpc_done = 0;
1108 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001109 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001110 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001111 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001112 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001113 status = nfs4_wait_for_completion_rpc_task(task);
1114 if (status != 0) {
1115 data->cancelled = 1;
1116 smp_wmb();
1117 } else
1118 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001119 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 return status;
1121}
1122
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001123static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001125 struct nfs4_opendata *data = calldata;
1126 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001127
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001128 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1129 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001130 /*
1131 * Check if we still need to send an OPEN call, or if we can use
1132 * a delegation instead.
1133 */
1134 if (data->state != NULL) {
1135 struct nfs_delegation *delegation;
1136
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001137 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001138 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001139 rcu_read_lock();
1140 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1141 if (delegation != NULL &&
Trond Myklebust15c831b2008-12-23 15:21:39 -05001142 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001143 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001144 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001145 }
1146 rcu_read_unlock();
1147 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001148 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001149 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001150 data->o_arg.clientid = sp->so_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001151 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001152 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001153 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1154 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001155 data->timestamp = jiffies;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001156 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001157 return;
1158out_no_action:
1159 task->tk_action = NULL;
1160
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001161}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001163static void nfs4_open_done(struct rpc_task *task, void *calldata)
1164{
1165 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001167 data->rpc_status = task->tk_status;
1168 if (RPC_ASSASSINATED(task))
1169 return;
1170 if (task->tk_status == 0) {
1171 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001172 case S_IFREG:
1173 break;
1174 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001175 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001176 break;
1177 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001178 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001179 break;
1180 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001181 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001182 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001183 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001184 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1185 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001186 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001187 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001188}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001189
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001190static void nfs4_open_release(void *calldata)
1191{
1192 struct nfs4_opendata *data = calldata;
1193 struct nfs4_state *state = NULL;
1194
1195 /* If this request hasn't been cancelled, do nothing */
1196 if (data->cancelled == 0)
1197 goto out_free;
1198 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001199 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001200 goto out_free;
1201 /* In case we need an open_confirm, no cleanup! */
1202 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1203 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001204 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001205 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001206 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001207out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001208 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001209}
1210
1211static const struct rpc_call_ops nfs4_open_ops = {
1212 .rpc_call_prepare = nfs4_open_prepare,
1213 .rpc_call_done = nfs4_open_done,
1214 .rpc_release = nfs4_open_release,
1215};
1216
1217/*
1218 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1219 */
1220static int _nfs4_proc_open(struct nfs4_opendata *data)
1221{
1222 struct inode *dir = data->dir->d_inode;
1223 struct nfs_server *server = NFS_SERVER(dir);
1224 struct nfs_openargs *o_arg = &data->o_arg;
1225 struct nfs_openres *o_res = &data->o_res;
1226 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001227 struct rpc_message msg = {
1228 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1229 .rpc_argp = o_arg,
1230 .rpc_resp = o_res,
1231 .rpc_cred = data->owner->so_cred,
1232 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001233 struct rpc_task_setup task_setup_data = {
1234 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001235 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001236 .callback_ops = &nfs4_open_ops,
1237 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001238 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001239 .flags = RPC_TASK_ASYNC,
1240 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001241 int status;
1242
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001243 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001244 data->rpc_done = 0;
1245 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001246 data->cancelled = 0;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001247 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001248 if (IS_ERR(task))
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001249 return PTR_ERR(task);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001250 status = nfs4_wait_for_completion_rpc_task(task);
1251 if (status != 0) {
1252 data->cancelled = 1;
1253 smp_wmb();
1254 } else
1255 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001256 rpc_put_task(task);
Trond Myklebust3e309912007-07-07 13:19:59 -04001257 if (status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001258 return status;
1259
Trond Myklebust99367812007-07-17 21:52:41 -04001260 if (o_res->fh.size == 0)
1261 _nfs4_proc_lookup(dir, o_arg->name, &o_res->fh, o_res->f_attr);
1262
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001263 if (o_arg->open_flags & O_CREAT) {
1264 update_changeattr(dir, &o_res->cinfo);
1265 nfs_post_op_update_inode(dir, o_res->dir_attr);
1266 } else
1267 nfs_refresh_inode(dir, o_res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001269 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001271 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 }
1273 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001274 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001275 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276}
1277
Trond Myklebust10afec92007-05-14 17:16:04 -04001278static int nfs4_recover_expired_lease(struct nfs_server *server)
Trond Myklebust58d97142006-01-03 09:55:24 +01001279{
David Howells7539bba2006-08-22 20:06:09 -04001280 struct nfs_client *clp = server->nfs_client;
Trond Myklebust6b309542006-09-14 14:03:14 -04001281 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001282
Trond Myklebust6b309542006-09-14 14:03:14 -04001283 for (;;) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001284 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001285 if (ret != 0)
1286 return ret;
Trond Myklebuste598d842008-12-23 15:21:42 -05001287 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1288 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001289 break;
Trond Myklebust58d97142006-01-03 09:55:24 +01001290 nfs4_schedule_state_recovery(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001291 }
1292 return 0;
Trond Myklebust58d97142006-01-03 09:55:24 +01001293}
1294
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295/*
1296 * OPEN_EXPIRED:
1297 * reclaim state on the server after a network partition.
1298 * Assumes caller holds the appropriate lock
1299 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001300static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001302 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001303 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001305 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1306 if (IS_ERR(opendata))
1307 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001308 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001309 if (ret == -ESTALE)
Trond Myklebust539cd032007-06-05 11:46:42 -04001310 d_drop(ctx->path.dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001311 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001312 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313}
1314
Trond Myklebust539cd032007-06-05 11:46:42 -04001315static inline int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001316{
Trond Myklebust539cd032007-06-05 11:46:42 -04001317 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001318 struct nfs4_exception exception = { };
1319 int err;
1320
1321 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001322 err = _nfs4_open_expired(ctx, state);
Trond Myklebust027b6ca2008-12-23 16:04:13 -05001323 if (err != -NFS4ERR_DELAY)
1324 break;
1325 nfs4_handle_exception(server, err, &exception);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001326 } while (exception.retry);
1327 return err;
1328}
1329
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1331{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001333 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
Trond Myklebust864472e2006-01-03 09:55:15 +01001335 ctx = nfs4_state_find_open_context(state);
1336 if (IS_ERR(ctx))
1337 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001338 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001339 put_nfs_open_context(ctx);
1340 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341}
1342
1343/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001344 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1345 * fields corresponding to attributes that were used to store the verifier.
1346 * Make sure we clobber those fields in the later setattr call
1347 */
1348static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1349{
1350 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1351 !(sattr->ia_valid & ATTR_ATIME_SET))
1352 sattr->ia_valid |= ATTR_ATIME;
1353
1354 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1355 !(sattr->ia_valid & ATTR_MTIME_SET))
1356 sattr->ia_valid |= ATTR_MTIME;
1357}
1358
1359/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001360 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001362static 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 -07001363{
1364 struct nfs4_state_owner *sp;
1365 struct nfs4_state *state = NULL;
1366 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001367 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001368 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
1370 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 status = -ENOMEM;
1372 if (!(sp = nfs4_get_state_owner(server, cred))) {
1373 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1374 goto out_err;
1375 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001376 status = nfs4_recover_expired_lease(server);
1377 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001378 goto err_put_state_owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001379 if (path->dentry->d_inode != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001380 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001381 status = -ENOMEM;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001382 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001383 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001384 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
Trond Myklebustaac00a82007-07-05 19:02:21 -04001386 if (path->dentry->d_inode != NULL)
1387 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1388
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001389 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001391 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001393 if (opendata->o_arg.open_flags & O_EXCL)
1394 nfs4_exclusive_attrset(opendata, sattr);
1395
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001396 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001397 status = PTR_ERR(state);
1398 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001399 goto err_opendata_put;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001400 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 *res = state;
1403 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001404err_opendata_put:
1405 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001406err_put_state_owner:
1407 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 *res = NULL;
1410 return status;
1411}
1412
1413
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001414static 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 -07001415{
1416 struct nfs4_exception exception = { };
1417 struct nfs4_state *res;
1418 int status;
1419
1420 do {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001421 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 if (status == 0)
1423 break;
1424 /* NOTE: BAD_SEQID means the server and client disagree about the
1425 * book-keeping w.r.t. state-changing operations
1426 * (OPEN/CLOSE/LOCK/LOCKU...)
1427 * It is actually a sign of a bug on the client or on the server.
1428 *
1429 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001430 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 * have unhashed the old state_owner for us, and that we can
1432 * therefore safely retry using a new one. We should still warn
1433 * the user though...
1434 */
1435 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001436 printk(KERN_WARNING "NFS: v4 server %s "
1437 " returned a bad sequence-id error!\n",
1438 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 exception.retry = 1;
1440 continue;
1441 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001442 /*
1443 * BAD_STATEID on OPEN means that the server cancelled our
1444 * state before it received the OPEN_CONFIRM.
1445 * Recover by retrying the request as per the discussion
1446 * on Page 181 of RFC3530.
1447 */
1448 if (status == -NFS4ERR_BAD_STATEID) {
1449 exception.retry = 1;
1450 continue;
1451 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001452 if (status == -EAGAIN) {
1453 /* We must have found a delegation */
1454 exception.retry = 1;
1455 continue;
1456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1458 status, &exception));
1459 } while (exception.retry);
1460 return res;
1461}
1462
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001463static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1464 struct nfs_fattr *fattr, struct iattr *sattr,
1465 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001467 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001469 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 .iap = sattr,
1471 .server = server,
1472 .bitmask = server->attr_bitmask,
1473 };
1474 struct nfs_setattrres res = {
1475 .fattr = fattr,
1476 .server = server,
1477 };
1478 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001479 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1480 .rpc_argp = &arg,
1481 .rpc_resp = &res,
1482 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001484 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001485 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Trond Myklebust0e574af2005-10-27 22:12:38 -04001487 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001489 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1490 /* Use that stateid */
1491 } else if (state != NULL) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001492 nfs4_copy_stateid(&arg.stateid, state, current->files);
1493 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1495
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001496 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001497 if (status == 0 && state != NULL)
1498 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001499 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500}
1501
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001502static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1503 struct nfs_fattr *fattr, struct iattr *sattr,
1504 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001506 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 struct nfs4_exception exception = { };
1508 int err;
1509 do {
1510 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001511 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 &exception);
1513 } while (exception.retry);
1514 return err;
1515}
1516
1517struct nfs4_closedata {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001518 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 struct inode *inode;
1520 struct nfs4_state *state;
1521 struct nfs_closeargs arg;
1522 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001523 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001524 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525};
1526
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001527static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001528{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001529 struct nfs4_closedata *calldata = data;
1530 struct nfs4_state_owner *sp = calldata->state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -07001531
1532 nfs4_put_open_state(calldata->state);
1533 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001534 nfs4_put_state_owner(sp);
Jan Blunck31f31db12008-05-02 13:42:45 -07001535 path_put(&calldata->path);
Trond Myklebust95121352005-10-18 14:20:12 -07001536 kfree(calldata);
1537}
1538
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001539static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001541 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 struct nfs_server *server = NFS_SERVER(calldata->inode);
1544
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001545 if (RPC_ASSASSINATED(task))
1546 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 /* hmm. we are done with the inode, and in the process of freeing
1548 * the state_owner. we keep this around to process errors
1549 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 switch (task->tk_status) {
1551 case 0:
Trond Myklebust45328c32007-07-26 17:47:34 -04001552 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001553 renew_lease(server, calldata->timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 break;
1555 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001556 case -NFS4ERR_OLD_STATEID:
1557 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001559 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001560 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001562 if (nfs4_async_handle_error(task, server, state) == -EAGAIN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 rpc_restart_call(task);
1564 return;
1565 }
1566 }
Trond Myklebust516a6af2005-10-27 22:12:41 -04001567 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568}
1569
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001570static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001572 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001573 struct nfs4_state *state = calldata->state;
Trond Myklebust003707c2007-07-05 18:07:55 -04001574 int clear_rd, clear_wr, clear_rdwr;
Trond Myklebust95121352005-10-18 14:20:12 -07001575
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001576 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001577 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001578
Trond Myklebust003707c2007-07-05 18:07:55 -04001579 clear_rd = clear_wr = clear_rdwr = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001580 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001581 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001582 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001583 if (state->n_rdonly == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001584 clear_rd |= test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1585 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1586 }
1587 if (state->n_wronly == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001588 clear_wr |= test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1589 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1590 }
Trond Myklebuste7616922006-01-03 09:55:13 +01001591 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001592 spin_unlock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001593 if (!clear_rd && !clear_wr && !clear_rdwr) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001594 /* Note: exit _without_ calling nfs4_close_done */
1595 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001596 return;
1597 }
Trond Myklebust516a6af2005-10-27 22:12:41 -04001598 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust45328c32007-07-26 17:47:34 -04001599 if (test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001600 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001601 calldata->arg.fmode = FMODE_READ;
Trond Myklebust45328c32007-07-26 17:47:34 -04001602 } else if (test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001603 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001604 calldata->arg.fmode = FMODE_WRITE;
Trond Myklebust45328c32007-07-26 17:47:34 -04001605 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001606 calldata->timestamp = jiffies;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001607 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608}
1609
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001610static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001611 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001612 .rpc_call_done = nfs4_close_done,
1613 .rpc_release = nfs4_free_closedata,
1614};
1615
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616/*
1617 * It is possible for data to be read/written from a mem-mapped file
1618 * after the sys_close call (which hits the vfs layer as a flush).
1619 * This means that we can't safely call nfsv4 close on a file until
1620 * the inode is cleared. This in turn means that we are not good
1621 * NFSv4 citizens - we do not indicate to the server to update the file's
1622 * share state even when we are done with one of the three share
1623 * stateid's in the inode.
1624 *
1625 * NOTE: Caller must be holding the sp->so_owner semaphore!
1626 */
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001627int nfs4_do_close(struct path *path, struct nfs4_state *state, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001629 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04001631 struct nfs4_state_owner *sp = state->owner;
1632 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001633 struct rpc_message msg = {
1634 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1635 .rpc_cred = state->owner->so_cred,
1636 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001637 struct rpc_task_setup task_setup_data = {
1638 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001639 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001640 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05001641 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001642 .flags = RPC_TASK_ASYNC,
1643 };
Trond Myklebust95121352005-10-18 14:20:12 -07001644 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
Trond Myklebust95121352005-10-18 14:20:12 -07001646 calldata = kmalloc(sizeof(*calldata), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07001648 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001649 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001651 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04001652 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 /* Serialization for the sequence id */
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001654 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001655 if (calldata->arg.seqid == NULL)
1656 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001657 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04001658 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001659 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04001660 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001661 calldata->res.server = server;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04001662 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001663 calldata->path.mnt = mntget(path->mnt);
1664 calldata->path.dentry = dget(path->dentry);
Trond Myklebust95121352005-10-18 14:20:12 -07001665
Trond Myklebust5138fde2007-07-14 15:40:01 -04001666 msg.rpc_argp = &calldata->arg,
1667 msg.rpc_resp = &calldata->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001668 task_setup_data.callback_data = calldata;
1669 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001670 if (IS_ERR(task))
1671 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001672 status = 0;
1673 if (wait)
1674 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001675 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001676 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07001677out_free_calldata:
1678 kfree(calldata);
1679out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04001680 nfs4_put_open_state(state);
1681 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07001682 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683}
1684
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001685static 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 -07001686{
1687 struct file *filp;
Trond Myklebust1b45c462007-07-03 13:04:56 -04001688 int ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001689
Trond Myklebust1b45c462007-07-03 13:04:56 -04001690 /* If the open_intent is for execute, we have an extra check to make */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001691 if (fmode & FMODE_EXEC) {
Trond Myklebustaf22f942007-08-10 17:45:10 -04001692 ret = nfs_may_open(state->inode,
Trond Myklebust1b45c462007-07-03 13:04:56 -04001693 state->owner->so_cred,
1694 nd->intent.open.flags);
1695 if (ret < 0)
1696 goto out_close;
1697 }
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001698 filp = lookup_instantiate_filp(nd, path->dentry, NULL);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001699 if (!IS_ERR(filp)) {
1700 struct nfs_open_context *ctx;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04001701 ctx = nfs_file_open_context(filp);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001702 ctx->state = state;
Trond Myklebust95cf9592006-04-18 13:14:06 -04001703 return 0;
1704 }
Trond Myklebust1b45c462007-07-03 13:04:56 -04001705 ret = PTR_ERR(filp);
1706out_close:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001707 nfs4_close_sync(path, state, fmode & (FMODE_READ|FMODE_WRITE));
Trond Myklebust1b45c462007-07-03 13:04:56 -04001708 return ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001709}
1710
1711struct dentry *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1713{
Trond Myklebustad389da2007-06-05 12:30:00 -04001714 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08001715 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001716 .dentry = dentry,
1717 };
Jan Blunck4ac91372008-02-14 19:34:32 -08001718 struct dentry *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 struct iattr attr;
1720 struct rpc_cred *cred;
1721 struct nfs4_state *state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001722 struct dentry *res;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001723 fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
1725 if (nd->flags & LOOKUP_CREATE) {
1726 attr.ia_mode = nd->intent.open.create_mode;
1727 attr.ia_valid = ATTR_MODE;
1728 if (!IS_POSIXACL(dir))
Al Viroce3b0f82009-03-29 19:08:22 -04001729 attr.ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 } else {
1731 attr.ia_valid = 0;
1732 BUG_ON(nd->intent.open.flags & O_CREAT);
1733 }
1734
Trond Myklebust98a8e322008-03-12 12:25:28 -04001735 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 if (IS_ERR(cred))
Trond Myklebust02a913a2005-10-18 14:20:17 -07001737 return (struct dentry *)cred;
Trond Myklebust565277f2007-10-15 18:17:53 -04001738 parent = dentry->d_parent;
1739 /* Protect against concurrent sillydeletes */
1740 nfs_block_sillyrename(parent);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001741 state = nfs4_do_open(dir, &path, fmode, nd->intent.open.flags, &attr, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001743 if (IS_ERR(state)) {
Trond Myklebustd75340c2007-10-01 21:42:01 -04001744 if (PTR_ERR(state) == -ENOENT) {
Trond Myklebust02a913a2005-10-18 14:20:17 -07001745 d_add(dentry, NULL);
Trond Myklebustd75340c2007-10-01 21:42:01 -04001746 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1747 }
Trond Myklebust565277f2007-10-15 18:17:53 -04001748 nfs_unblock_sillyrename(parent);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001749 return (struct dentry *)state;
1750 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001751 res = d_add_unique(dentry, igrab(state->inode));
Trond Myklebust02a913a2005-10-18 14:20:17 -07001752 if (res != NULL)
Trond Myklebustdeee9362007-08-27 11:33:00 -04001753 path.dentry = res;
Trond Myklebustd75340c2007-10-01 21:42:01 -04001754 nfs_set_verifier(path.dentry, nfs_save_change_attribute(dir));
Trond Myklebust565277f2007-10-15 18:17:53 -04001755 nfs_unblock_sillyrename(parent);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001756 nfs4_intent_set_file(nd, &path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001757 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758}
1759
1760int
Trond Myklebust02a913a2005-10-18 14:20:17 -07001761nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762{
Trond Myklebustad389da2007-06-05 12:30:00 -04001763 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08001764 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001765 .dentry = dentry,
1766 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 struct rpc_cred *cred;
1768 struct nfs4_state *state;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001769 fmode_t fmode = openflags & (FMODE_READ | FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
Trond Myklebust98a8e322008-03-12 12:25:28 -04001771 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 if (IS_ERR(cred))
1773 return PTR_ERR(cred);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001774 state = nfs4_do_open(dir, &path, fmode, openflags, NULL, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001776 if (IS_ERR(state)) {
1777 switch (PTR_ERR(state)) {
1778 case -EPERM:
1779 case -EACCES:
1780 case -EDQUOT:
1781 case -ENOSPC:
1782 case -EROFS:
1783 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
1784 return 1;
Chuck Leverb87c0ad2006-10-19 23:28:42 -07001785 default:
1786 goto out_drop;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001787 }
Trond Myklebust02a913a2005-10-18 14:20:17 -07001788 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001789 if (state->inode == dentry->d_inode) {
Trond Myklebustd75340c2007-10-01 21:42:01 -04001790 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001791 nfs4_intent_set_file(nd, &path, state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 return 1;
1793 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001794 nfs4_close_sync(&path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001795out_drop:
1796 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 return 0;
1798}
1799
Trond Myklebust7fe5c392009-03-19 15:35:50 -04001800void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
1801{
1802 if (ctx->state == NULL)
1803 return;
1804 if (is_sync)
1805 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
1806 else
1807 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
1808}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
1810static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1811{
Benny Halevy43652ad2009-04-01 09:21:54 -04001812 struct nfs4_server_caps_arg args = {
1813 .fhandle = fhandle,
1814 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 struct nfs4_server_caps_res res = {};
1816 struct rpc_message msg = {
1817 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04001818 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 .rpc_resp = &res,
1820 };
1821 int status;
1822
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001823 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 if (status == 0) {
1825 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
1826 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
1827 server->caps |= NFS_CAP_ACLS;
1828 if (res.has_links != 0)
1829 server->caps |= NFS_CAP_HARDLINKS;
1830 if (res.has_symlinks != 0)
1831 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebusta65318b2009-03-11 14:10:28 -04001832 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
1833 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
1834 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 server->acl_bitmask = res.acl_bitmask;
1836 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001837
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 return status;
1839}
1840
Trond Myklebust55a97592006-06-09 09:34:19 -04001841int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842{
1843 struct nfs4_exception exception = { };
1844 int err;
1845 do {
1846 err = nfs4_handle_exception(server,
1847 _nfs4_server_capabilities(server, fhandle),
1848 &exception);
1849 } while (exception.retry);
1850 return err;
1851}
1852
1853static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1854 struct nfs_fsinfo *info)
1855{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 struct nfs4_lookup_root_arg args = {
1857 .bitmask = nfs4_fattr_bitmap,
1858 };
1859 struct nfs4_lookup_res res = {
1860 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04001861 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 .fh = fhandle,
1863 };
1864 struct rpc_message msg = {
1865 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
1866 .rpc_argp = &args,
1867 .rpc_resp = &res,
1868 };
Trond Myklebust0e574af2005-10-27 22:12:38 -04001869 nfs_fattr_init(info->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001870 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871}
1872
1873static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1874 struct nfs_fsinfo *info)
1875{
1876 struct nfs4_exception exception = { };
1877 int err;
1878 do {
1879 err = nfs4_handle_exception(server,
1880 _nfs4_lookup_root(server, fhandle, info),
1881 &exception);
1882 } while (exception.retry);
1883 return err;
1884}
1885
David Howells54ceac42006-08-22 20:06:13 -04001886/*
1887 * get the file handle for the "/" directory on the server
1888 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04001890 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 int status;
1893
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 status = nfs4_lookup_root(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 if (status == 0)
1896 status = nfs4_server_capabilities(server, fhandle);
1897 if (status == 0)
1898 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08001899 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900}
1901
Manoj Naik6b97fd32006-06-09 09:34:29 -04001902/*
1903 * Get locations and (maybe) other attributes of a referral.
1904 * Note that we'll actually follow the referral later when
1905 * we detect fsid mismatch in inode revalidation
1906 */
Trond Myklebust56659e92007-07-17 21:52:39 -04001907static 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 -04001908{
1909 int status = -ENOMEM;
1910 struct page *page = NULL;
1911 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04001912
1913 page = alloc_page(GFP_KERNEL);
1914 if (page == NULL)
1915 goto out;
1916 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
1917 if (locations == NULL)
1918 goto out;
1919
Trond Myklebustc228fd32007-01-13 02:28:11 -05001920 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04001921 if (status != 0)
1922 goto out;
1923 /* Make sure server returned a different fsid for the referral */
1924 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -07001925 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 -04001926 status = -EIO;
1927 goto out;
1928 }
1929
1930 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
1931 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
1932 if (!fattr->mode)
1933 fattr->mode = S_IFDIR;
1934 memset(fhandle, 0, sizeof(struct nfs_fh));
1935out:
1936 if (page)
1937 __free_page(page);
1938 if (locations)
1939 kfree(locations);
1940 return status;
1941}
1942
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1944{
1945 struct nfs4_getattr_arg args = {
1946 .fh = fhandle,
1947 .bitmask = server->attr_bitmask,
1948 };
1949 struct nfs4_getattr_res res = {
1950 .fattr = fattr,
1951 .server = server,
1952 };
1953 struct rpc_message msg = {
1954 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
1955 .rpc_argp = &args,
1956 .rpc_resp = &res,
1957 };
1958
Trond Myklebust0e574af2005-10-27 22:12:38 -04001959 nfs_fattr_init(fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001960 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961}
1962
1963static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1964{
1965 struct nfs4_exception exception = { };
1966 int err;
1967 do {
1968 err = nfs4_handle_exception(server,
1969 _nfs4_proc_getattr(server, fhandle, fattr),
1970 &exception);
1971 } while (exception.retry);
1972 return err;
1973}
1974
1975/*
1976 * The file is not closed if it is opened due to the a request to change
1977 * the size of the file. The open call will not be needed once the
1978 * VFS layer lookup-intents are implemented.
1979 *
1980 * Close is called when the inode is destroyed.
1981 * If we haven't opened the file for O_WRONLY, we
1982 * need to in the size_change case to obtain a stateid.
1983 *
1984 * Got race?
1985 * Because OPEN is always done by name in nfsv4, it is
1986 * possible that we opened a different file by the same
1987 * name. We can recognize this race condition, but we
1988 * can't do anything about it besides returning an error.
1989 *
1990 * This will be fixed with VFS changes (lookup-intent).
1991 */
1992static int
1993nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
1994 struct iattr *sattr)
1995{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001996 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001997 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05001998 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 int status;
2000
Trond Myklebust0e574af2005-10-27 22:12:38 -04002001 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
Trond Myklebustd5308382005-11-04 15:33:38 -05002003 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002004 if (sattr->ia_valid & ATTR_FILE) {
2005 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002006
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002007 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002008 if (ctx) {
2009 cred = ctx->cred;
2010 state = ctx->state;
2011 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002012 }
2013
2014 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002015 if (status == 0)
2016 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 return status;
2018}
2019
Trond Myklebust56659e92007-07-17 21:52:39 -04002020static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
2021 const struct qstr *name, struct nfs_fh *fhandle,
David Howells2b3de442006-08-22 20:06:09 -04002022 struct nfs_fattr *fattr)
2023{
2024 int status;
2025 struct nfs4_lookup_arg args = {
2026 .bitmask = server->attr_bitmask,
2027 .dir_fh = dirfh,
2028 .name = name,
2029 };
2030 struct nfs4_lookup_res res = {
2031 .server = server,
2032 .fattr = fattr,
2033 .fh = fhandle,
2034 };
2035 struct rpc_message msg = {
2036 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2037 .rpc_argp = &args,
2038 .rpc_resp = &res,
2039 };
2040
2041 nfs_fattr_init(fattr);
2042
2043 dprintk("NFS call lookupfh %s\n", name->name);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002044 status = nfs4_call_sync(server, &msg, &args, &res, 0);
David Howells2b3de442006-08-22 20:06:09 -04002045 dprintk("NFS reply lookupfh: %d\n", status);
David Howells2b3de442006-08-22 20:06:09 -04002046 return status;
2047}
2048
2049static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2050 struct qstr *name, struct nfs_fh *fhandle,
2051 struct nfs_fattr *fattr)
2052{
2053 struct nfs4_exception exception = { };
2054 int err;
2055 do {
Trond Myklebust4e56e082007-07-01 18:13:52 -04002056 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2057 /* FIXME: !!!! */
2058 if (err == -NFS4ERR_MOVED) {
2059 err = -EREMOTE;
2060 break;
2061 }
2062 err = nfs4_handle_exception(server, err, &exception);
David Howells2b3de442006-08-22 20:06:09 -04002063 } while (exception.retry);
2064 return err;
2065}
2066
Trond Myklebust56659e92007-07-17 21:52:39 -04002067static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2069{
Trond Myklebust4e56e082007-07-01 18:13:52 -04002070 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
2072 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust4e56e082007-07-01 18:13:52 -04002073 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002074 if (status == -NFS4ERR_MOVED)
2075 status = nfs4_get_referral(dir, name, fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 dprintk("NFS reply lookup: %d\n", status);
2077 return status;
2078}
2079
2080static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2081{
2082 struct nfs4_exception exception = { };
2083 int err;
2084 do {
2085 err = nfs4_handle_exception(NFS_SERVER(dir),
2086 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2087 &exception);
2088 } while (exception.retry);
2089 return err;
2090}
2091
2092static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2093{
Trond Myklebust76b32992007-08-10 17:45:11 -04002094 struct nfs_server *server = NFS_SERVER(inode);
2095 struct nfs_fattr fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 struct nfs4_accessargs args = {
2097 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04002098 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002100 struct nfs4_accessres res = {
2101 .server = server,
2102 .fattr = &fattr,
2103 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 struct rpc_message msg = {
2105 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2106 .rpc_argp = &args,
2107 .rpc_resp = &res,
2108 .rpc_cred = entry->cred,
2109 };
2110 int mode = entry->mask;
2111 int status;
2112
2113 /*
2114 * Determine which access bits we want to ask for...
2115 */
2116 if (mode & MAY_READ)
2117 args.access |= NFS4_ACCESS_READ;
2118 if (S_ISDIR(inode->i_mode)) {
2119 if (mode & MAY_WRITE)
2120 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2121 if (mode & MAY_EXEC)
2122 args.access |= NFS4_ACCESS_LOOKUP;
2123 } else {
2124 if (mode & MAY_WRITE)
2125 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2126 if (mode & MAY_EXEC)
2127 args.access |= NFS4_ACCESS_EXECUTE;
2128 }
Trond Myklebust76b32992007-08-10 17:45:11 -04002129 nfs_fattr_init(&fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002130 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 if (!status) {
2132 entry->mask = 0;
2133 if (res.access & NFS4_ACCESS_READ)
2134 entry->mask |= MAY_READ;
2135 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2136 entry->mask |= MAY_WRITE;
2137 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2138 entry->mask |= MAY_EXEC;
Trond Myklebust76b32992007-08-10 17:45:11 -04002139 nfs_refresh_inode(inode, &fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 }
2141 return status;
2142}
2143
2144static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2145{
2146 struct nfs4_exception exception = { };
2147 int err;
2148 do {
2149 err = nfs4_handle_exception(NFS_SERVER(inode),
2150 _nfs4_proc_access(inode, entry),
2151 &exception);
2152 } while (exception.retry);
2153 return err;
2154}
2155
2156/*
2157 * TODO: For the time being, we don't try to get any attributes
2158 * along with any of the zero-copy operations READ, READDIR,
2159 * READLINK, WRITE.
2160 *
2161 * In the case of the first three, we want to put the GETATTR
2162 * after the read-type operation -- this is because it is hard
2163 * to predict the length of a GETATTR response in v4, and thus
2164 * align the READ data correctly. This means that the GETATTR
2165 * may end up partially falling into the page cache, and we should
2166 * shift it into the 'tail' of the xdr_buf before processing.
2167 * To do this efficiently, we need to know the total length
2168 * of data received, which doesn't seem to be available outside
2169 * of the RPC layer.
2170 *
2171 * In the case of WRITE, we also want to put the GETATTR after
2172 * the operation -- in this case because we want to make sure
2173 * we get the post-operation mtime and size. This means that
2174 * we can't use xdr_encode_pages() as written: we need a variant
2175 * of it which would leave room in the 'tail' iovec.
2176 *
2177 * Both of these changes to the XDR layer would in fact be quite
2178 * minor, but I decided to leave them for a subsequent patch.
2179 */
2180static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2181 unsigned int pgbase, unsigned int pglen)
2182{
2183 struct nfs4_readlink args = {
2184 .fh = NFS_FH(inode),
2185 .pgbase = pgbase,
2186 .pglen = pglen,
2187 .pages = &page,
2188 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002189 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 struct rpc_message msg = {
2191 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2192 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002193 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 };
2195
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002196 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197}
2198
2199static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2200 unsigned int pgbase, unsigned int pglen)
2201{
2202 struct nfs4_exception exception = { };
2203 int err;
2204 do {
2205 err = nfs4_handle_exception(NFS_SERVER(inode),
2206 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2207 &exception);
2208 } while (exception.retry);
2209 return err;
2210}
2211
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212/*
2213 * Got race?
2214 * We will need to arrange for the VFS layer to provide an atomic open.
2215 * Until then, this create/open method is prone to inefficiency and race
2216 * conditions due to the lookup, create, and open VFS calls from sys_open()
2217 * placed on the wire.
2218 *
2219 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2220 * The file will be opened again in the subsequent VFS open call
2221 * (nfs4_proc_file_open).
2222 *
2223 * The open for read will just hang around to be used by any process that
2224 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2225 */
2226
2227static int
2228nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebust02a913a2005-10-18 14:20:17 -07002229 int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230{
Trond Myklebustad389da2007-06-05 12:30:00 -04002231 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08002232 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04002233 .dentry = dentry,
2234 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 struct nfs4_state *state;
2236 struct rpc_cred *cred;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002237 fmode_t fmode = flags & (FMODE_READ | FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 int status = 0;
2239
Trond Myklebust98a8e322008-03-12 12:25:28 -04002240 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 if (IS_ERR(cred)) {
2242 status = PTR_ERR(cred);
2243 goto out;
2244 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002245 state = nfs4_do_open(dir, &path, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002246 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 if (IS_ERR(state)) {
2248 status = PTR_ERR(state);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002249 goto out_putcred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002251 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002252 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 if (flags & O_EXCL) {
2254 struct nfs_fattr fattr;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002255 status = nfs4_do_setattr(state->inode, cred, &fattr, sattr, state);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002256 if (status == 0)
Trond Myklebust65e43082005-08-16 11:49:44 -04002257 nfs_setattr_update_inode(state->inode, sattr);
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002258 nfs_post_op_update_inode(state->inode, &fattr);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002259 }
Trond Myklebustad389da2007-06-05 12:30:00 -04002260 if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002261 status = nfs4_intent_set_file(nd, &path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002262 else
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002263 nfs4_close_sync(&path, state, fmode);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002264out_putcred:
2265 put_rpccred(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266out:
2267 return status;
2268}
2269
2270static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2271{
Trond Myklebust16e42952005-10-27 22:12:44 -04002272 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002273 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002275 .name.len = name->len,
2276 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002277 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002279 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002280 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002281 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002283 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2284 .rpc_argp = &args,
2285 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 };
2287 int status;
2288
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002289 nfs_fattr_init(&res.dir_attr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002290 status = nfs4_call_sync(server, &msg, &args, &res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002291 if (status == 0) {
2292 update_changeattr(dir, &res.cinfo);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002293 nfs_post_op_update_inode(dir, &res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 return status;
2296}
2297
2298static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2299{
2300 struct nfs4_exception exception = { };
2301 int err;
2302 do {
2303 err = nfs4_handle_exception(NFS_SERVER(dir),
2304 _nfs4_proc_remove(dir, name),
2305 &exception);
2306 } while (exception.retry);
2307 return err;
2308}
2309
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002310static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002312 struct nfs_server *server = NFS_SERVER(dir);
2313 struct nfs_removeargs *args = msg->rpc_argp;
2314 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
Trond Myklebusta65318b2009-03-11 14:10:28 -04002316 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002317 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319}
2320
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002321static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002323 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2324
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002325 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002326 return 0;
2327 update_changeattr(dir, &res->cinfo);
2328 nfs_post_op_update_inode(dir, &res->dir_attr);
2329 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330}
2331
2332static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2333 struct inode *new_dir, struct qstr *new_name)
2334{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002335 struct nfs_server *server = NFS_SERVER(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 struct nfs4_rename_arg arg = {
2337 .old_dir = NFS_FH(old_dir),
2338 .new_dir = NFS_FH(new_dir),
2339 .old_name = old_name,
2340 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002341 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 };
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002343 struct nfs_fattr old_fattr, new_fattr;
2344 struct nfs4_rename_res res = {
2345 .server = server,
2346 .old_fattr = &old_fattr,
2347 .new_fattr = &new_fattr,
2348 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 struct rpc_message msg = {
2350 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2351 .rpc_argp = &arg,
2352 .rpc_resp = &res,
2353 };
2354 int status;
2355
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002356 nfs_fattr_init(res.old_fattr);
2357 nfs_fattr_init(res.new_fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002358 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359
2360 if (!status) {
2361 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002362 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002364 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 }
2366 return status;
2367}
2368
2369static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2370 struct inode *new_dir, struct qstr *new_name)
2371{
2372 struct nfs4_exception exception = { };
2373 int err;
2374 do {
2375 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2376 _nfs4_proc_rename(old_dir, old_name,
2377 new_dir, new_name),
2378 &exception);
2379 } while (exception.retry);
2380 return err;
2381}
2382
2383static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2384{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002385 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 struct nfs4_link_arg arg = {
2387 .fh = NFS_FH(inode),
2388 .dir_fh = NFS_FH(dir),
2389 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002390 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002392 struct nfs_fattr fattr, dir_attr;
2393 struct nfs4_link_res res = {
2394 .server = server,
2395 .fattr = &fattr,
2396 .dir_attr = &dir_attr,
2397 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 struct rpc_message msg = {
2399 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2400 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002401 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 };
2403 int status;
2404
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002405 nfs_fattr_init(res.fattr);
2406 nfs_fattr_init(res.dir_attr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002407 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002408 if (!status) {
2409 update_changeattr(dir, &res.cinfo);
2410 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002411 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413
2414 return status;
2415}
2416
2417static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2418{
2419 struct nfs4_exception exception = { };
2420 int err;
2421 do {
2422 err = nfs4_handle_exception(NFS_SERVER(inode),
2423 _nfs4_proc_link(inode, dir, name),
2424 &exception);
2425 } while (exception.retry);
2426 return err;
2427}
2428
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002429struct nfs4_createdata {
2430 struct rpc_message msg;
2431 struct nfs4_create_arg arg;
2432 struct nfs4_create_res res;
2433 struct nfs_fh fh;
2434 struct nfs_fattr fattr;
2435 struct nfs_fattr dir_fattr;
2436};
2437
2438static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2439 struct qstr *name, struct iattr *sattr, u32 ftype)
2440{
2441 struct nfs4_createdata *data;
2442
2443 data = kzalloc(sizeof(*data), GFP_KERNEL);
2444 if (data != NULL) {
2445 struct nfs_server *server = NFS_SERVER(dir);
2446
2447 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2448 data->msg.rpc_argp = &data->arg;
2449 data->msg.rpc_resp = &data->res;
2450 data->arg.dir_fh = NFS_FH(dir);
2451 data->arg.server = server;
2452 data->arg.name = name;
2453 data->arg.attrs = sattr;
2454 data->arg.ftype = ftype;
2455 data->arg.bitmask = server->attr_bitmask;
2456 data->res.server = server;
2457 data->res.fh = &data->fh;
2458 data->res.fattr = &data->fattr;
2459 data->res.dir_fattr = &data->dir_fattr;
2460 nfs_fattr_init(data->res.fattr);
2461 nfs_fattr_init(data->res.dir_fattr);
2462 }
2463 return data;
2464}
2465
2466static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2467{
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002468 int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2469 &data->arg, &data->res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002470 if (status == 0) {
2471 update_changeattr(dir, &data->res.dir_cinfo);
2472 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2473 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2474 }
2475 return status;
2476}
2477
2478static void nfs4_free_createdata(struct nfs4_createdata *data)
2479{
2480 kfree(data);
2481}
2482
Chuck Lever4f390c12006-08-22 20:06:22 -04002483static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002484 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002486 struct nfs4_createdata *data;
2487 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488
Chuck Lever94a6d752006-08-22 20:06:23 -04002489 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002490 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002491
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002492 status = -ENOMEM;
2493 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2494 if (data == NULL)
2495 goto out;
2496
2497 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2498 data->arg.u.symlink.pages = &page;
2499 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002501 status = nfs4_do_create(dir, dentry, data);
2502
2503 nfs4_free_createdata(data);
2504out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 return status;
2506}
2507
Chuck Lever4f390c12006-08-22 20:06:22 -04002508static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002509 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510{
2511 struct nfs4_exception exception = { };
2512 int err;
2513 do {
2514 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002515 _nfs4_proc_symlink(dir, dentry, page,
2516 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 &exception);
2518 } while (exception.retry);
2519 return err;
2520}
2521
2522static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2523 struct iattr *sattr)
2524{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002525 struct nfs4_createdata *data;
2526 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002528 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2529 if (data == NULL)
2530 goto out;
2531
2532 status = nfs4_do_create(dir, dentry, data);
2533
2534 nfs4_free_createdata(data);
2535out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 return status;
2537}
2538
2539static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2540 struct iattr *sattr)
2541{
2542 struct nfs4_exception exception = { };
2543 int err;
2544 do {
2545 err = nfs4_handle_exception(NFS_SERVER(dir),
2546 _nfs4_proc_mkdir(dir, dentry, sattr),
2547 &exception);
2548 } while (exception.retry);
2549 return err;
2550}
2551
2552static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2553 u64 cookie, struct page *page, unsigned int count, int plus)
2554{
2555 struct inode *dir = dentry->d_inode;
2556 struct nfs4_readdir_arg args = {
2557 .fh = NFS_FH(dir),
2558 .pages = &page,
2559 .pgbase = 0,
2560 .count = count,
Trond Myklebusta65318b2009-03-11 14:10:28 -04002561 .bitmask = NFS_SERVER(dentry->d_inode)->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 };
2563 struct nfs4_readdir_res res;
2564 struct rpc_message msg = {
2565 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2566 .rpc_argp = &args,
2567 .rpc_resp = &res,
2568 .rpc_cred = cred,
2569 };
2570 int status;
2571
Harvey Harrison3110ff82008-05-02 13:42:44 -07002572 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00002573 dentry->d_parent->d_name.name,
2574 dentry->d_name.name,
2575 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2577 res.pgbase = args.pgbase;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002578 status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 if (status == 0)
2580 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustc4812992007-09-28 17:11:45 -04002581
2582 nfs_invalidate_atime(dir);
2583
Harvey Harrison3110ff82008-05-02 13:42:44 -07002584 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 return status;
2586}
2587
2588static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2589 u64 cookie, struct page *page, unsigned int count, int plus)
2590{
2591 struct nfs4_exception exception = { };
2592 int err;
2593 do {
2594 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2595 _nfs4_proc_readdir(dentry, cred, cookie,
2596 page, count, plus),
2597 &exception);
2598 } while (exception.retry);
2599 return err;
2600}
2601
2602static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2603 struct iattr *sattr, dev_t rdev)
2604{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002605 struct nfs4_createdata *data;
2606 int mode = sattr->ia_mode;
2607 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608
2609 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2610 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002611
2612 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2613 if (data == NULL)
2614 goto out;
2615
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002617 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002619 data->arg.ftype = NF4BLK;
2620 data->arg.u.device.specdata1 = MAJOR(rdev);
2621 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 }
2623 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002624 data->arg.ftype = NF4CHR;
2625 data->arg.u.device.specdata1 = MAJOR(rdev);
2626 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002629 status = nfs4_do_create(dir, dentry, data);
2630
2631 nfs4_free_createdata(data);
2632out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 return status;
2634}
2635
2636static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2637 struct iattr *sattr, dev_t rdev)
2638{
2639 struct nfs4_exception exception = { };
2640 int err;
2641 do {
2642 err = nfs4_handle_exception(NFS_SERVER(dir),
2643 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2644 &exception);
2645 } while (exception.retry);
2646 return err;
2647}
2648
2649static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2650 struct nfs_fsstat *fsstat)
2651{
2652 struct nfs4_statfs_arg args = {
2653 .fh = fhandle,
2654 .bitmask = server->attr_bitmask,
2655 };
Benny Halevy24ad1482009-04-01 09:21:56 -04002656 struct nfs4_statfs_res res = {
2657 .fsstat = fsstat,
2658 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 struct rpc_message msg = {
2660 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2661 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04002662 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 };
2664
Trond Myklebust0e574af2005-10-27 22:12:38 -04002665 nfs_fattr_init(fsstat->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002666 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667}
2668
2669static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2670{
2671 struct nfs4_exception exception = { };
2672 int err;
2673 do {
2674 err = nfs4_handle_exception(server,
2675 _nfs4_proc_statfs(server, fhandle, fsstat),
2676 &exception);
2677 } while (exception.retry);
2678 return err;
2679}
2680
2681static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2682 struct nfs_fsinfo *fsinfo)
2683{
2684 struct nfs4_fsinfo_arg args = {
2685 .fh = fhandle,
2686 .bitmask = server->attr_bitmask,
2687 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04002688 struct nfs4_fsinfo_res res = {
2689 .fsinfo = fsinfo,
2690 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 struct rpc_message msg = {
2692 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2693 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04002694 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 };
2696
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002697 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698}
2699
2700static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2701{
2702 struct nfs4_exception exception = { };
2703 int err;
2704
2705 do {
2706 err = nfs4_handle_exception(server,
2707 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2708 &exception);
2709 } while (exception.retry);
2710 return err;
2711}
2712
2713static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2714{
Trond Myklebust0e574af2005-10-27 22:12:38 -04002715 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2717}
2718
2719static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2720 struct nfs_pathconf *pathconf)
2721{
2722 struct nfs4_pathconf_arg args = {
2723 .fh = fhandle,
2724 .bitmask = server->attr_bitmask,
2725 };
Benny Halevyd45b2982009-04-01 09:21:58 -04002726 struct nfs4_pathconf_res res = {
2727 .pathconf = pathconf,
2728 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 struct rpc_message msg = {
2730 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2731 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04002732 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 };
2734
2735 /* None of the pathconf attributes are mandatory to implement */
2736 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
2737 memset(pathconf, 0, sizeof(*pathconf));
2738 return 0;
2739 }
2740
Trond Myklebust0e574af2005-10-27 22:12:38 -04002741 nfs_fattr_init(pathconf->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002742 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743}
2744
2745static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2746 struct nfs_pathconf *pathconf)
2747{
2748 struct nfs4_exception exception = { };
2749 int err;
2750
2751 do {
2752 err = nfs4_handle_exception(server,
2753 _nfs4_proc_pathconf(server, fhandle, pathconf),
2754 &exception);
2755 } while (exception.retry);
2756 return err;
2757}
2758
Trond Myklebustec06c092006-03-20 13:44:27 -05002759static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760{
Trond Myklebustec06c092006-03-20 13:44:27 -05002761 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002763 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 rpc_restart_call(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05002765 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 }
Trond Myklebust8850df92007-09-28 17:20:07 -04002767
2768 nfs_invalidate_atime(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05002770 renew_lease(server, data->timestamp);
2771 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772}
2773
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002774static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 data->timestamp = jiffies;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002777 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778}
2779
Trond Myklebust788e7a82006-03-20 13:44:27 -05002780static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 struct inode *inode = data->inode;
2783
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002784 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 rpc_restart_call(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05002786 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002788 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04002790 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002791 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05002792 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793}
2794
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002795static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002797 struct nfs_server *server = NFS_SERVER(data->inode);
2798
Trond Myklebusta65318b2009-03-11 14:10:28 -04002799 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002800 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 data->timestamp = jiffies;
2802
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002803 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804}
2805
Trond Myklebust788e7a82006-03-20 13:44:27 -05002806static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 struct inode *inode = data->inode;
2809
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002810 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 rpc_restart_call(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05002812 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04002814 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05002815 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816}
2817
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002818static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819{
Trond Myklebust788e7a82006-03-20 13:44:27 -05002820 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821
Trond Myklebusta65318b2009-03-11 14:10:28 -04002822 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002823 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002824 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825}
2826
2827/*
2828 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
2829 * standalone procedure for queueing an asynchronous RENEW.
2830 */
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002831static void nfs4_renew_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832{
David Howellsadfa6f92006-08-22 20:06:08 -04002833 struct nfs_client *clp = (struct nfs_client *)task->tk_msg.rpc_argp;
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002834 unsigned long timestamp = (unsigned long)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835
2836 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04002837 /* Unless we're shutting down, schedule state recovery! */
2838 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
2839 nfs4_schedule_state_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 return;
2841 }
2842 spin_lock(&clp->cl_lock);
2843 if (time_before(clp->cl_last_renewal,timestamp))
2844 clp->cl_last_renewal = timestamp;
2845 spin_unlock(&clp->cl_lock);
2846}
2847
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002848static const struct rpc_call_ops nfs4_renew_ops = {
2849 .rpc_call_done = nfs4_renew_done,
2850};
2851
David Howellsadfa6f92006-08-22 20:06:08 -04002852int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853{
2854 struct rpc_message msg = {
2855 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2856 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002857 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 };
2859
2860 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002861 &nfs4_renew_ops, (void *)jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862}
2863
David Howellsadfa6f92006-08-22 20:06:08 -04002864int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865{
2866 struct rpc_message msg = {
2867 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2868 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002869 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 };
2871 unsigned long now = jiffies;
2872 int status;
2873
2874 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2875 if (status < 0)
2876 return status;
2877 spin_lock(&clp->cl_lock);
2878 if (time_before(clp->cl_last_renewal,now))
2879 clp->cl_last_renewal = now;
2880 spin_unlock(&clp->cl_lock);
2881 return 0;
2882}
2883
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002884static inline int nfs4_server_supports_acls(struct nfs_server *server)
2885{
2886 return (server->caps & NFS_CAP_ACLS)
2887 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
2888 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
2889}
2890
2891/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
2892 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
2893 * the stack.
2894 */
2895#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
2896
2897static void buf_to_pages(const void *buf, size_t buflen,
2898 struct page **pages, unsigned int *pgbase)
2899{
2900 const void *p = buf;
2901
2902 *pgbase = offset_in_page(buf);
2903 p -= *pgbase;
2904 while (p < buf + buflen) {
2905 *(pages++) = virt_to_page(p);
2906 p += PAGE_CACHE_SIZE;
2907 }
2908}
2909
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002910struct nfs4_cached_acl {
2911 int cached;
2912 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00002913 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002914};
2915
2916static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002917{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002918 struct nfs_inode *nfsi = NFS_I(inode);
2919
2920 spin_lock(&inode->i_lock);
2921 kfree(nfsi->nfs4_acl);
2922 nfsi->nfs4_acl = acl;
2923 spin_unlock(&inode->i_lock);
2924}
2925
2926static void nfs4_zap_acl_attr(struct inode *inode)
2927{
2928 nfs4_set_cached_acl(inode, NULL);
2929}
2930
2931static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
2932{
2933 struct nfs_inode *nfsi = NFS_I(inode);
2934 struct nfs4_cached_acl *acl;
2935 int ret = -ENOENT;
2936
2937 spin_lock(&inode->i_lock);
2938 acl = nfsi->nfs4_acl;
2939 if (acl == NULL)
2940 goto out;
2941 if (buf == NULL) /* user is just asking for length */
2942 goto out_len;
2943 if (acl->cached == 0)
2944 goto out;
2945 ret = -ERANGE; /* see getxattr(2) man page */
2946 if (acl->len > buflen)
2947 goto out;
2948 memcpy(buf, acl->data, acl->len);
2949out_len:
2950 ret = acl->len;
2951out:
2952 spin_unlock(&inode->i_lock);
2953 return ret;
2954}
2955
2956static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
2957{
2958 struct nfs4_cached_acl *acl;
2959
2960 if (buf && acl_len <= PAGE_SIZE) {
2961 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
2962 if (acl == NULL)
2963 goto out;
2964 acl->cached = 1;
2965 memcpy(acl->data, buf, acl_len);
2966 } else {
2967 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
2968 if (acl == NULL)
2969 goto out;
2970 acl->cached = 0;
2971 }
2972 acl->len = acl_len;
2973out:
2974 nfs4_set_cached_acl(inode, acl);
2975}
2976
Trond Myklebust16b4289c2006-08-24 12:27:15 -04002977static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002978{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002979 struct page *pages[NFS4ACL_MAXPAGES];
2980 struct nfs_getaclargs args = {
2981 .fh = NFS_FH(inode),
2982 .acl_pages = pages,
2983 .acl_len = buflen,
2984 };
Benny Halevy663c79b2009-04-01 09:21:59 -04002985 struct nfs_getaclres res = {
2986 .acl_len = buflen,
2987 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002988 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002989 struct rpc_message msg = {
2990 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
2991 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04002992 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002993 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002994 struct page *localpage = NULL;
2995 int ret;
2996
2997 if (buflen < PAGE_SIZE) {
2998 /* As long as we're doing a round trip to the server anyway,
2999 * let's be prepared for a page of acl data. */
3000 localpage = alloc_page(GFP_KERNEL);
3001 resp_buf = page_address(localpage);
3002 if (localpage == NULL)
3003 return -ENOMEM;
3004 args.acl_pages[0] = localpage;
3005 args.acl_pgbase = 0;
Benny Halevy663c79b2009-04-01 09:21:59 -04003006 args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003007 } else {
3008 resp_buf = buf;
3009 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3010 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003011 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003012 if (ret)
3013 goto out_free;
Benny Halevy663c79b2009-04-01 09:21:59 -04003014 if (res.acl_len > args.acl_len)
3015 nfs4_write_cached_acl(inode, NULL, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003016 else
Benny Halevy663c79b2009-04-01 09:21:59 -04003017 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003018 if (buf) {
3019 ret = -ERANGE;
Benny Halevy663c79b2009-04-01 09:21:59 -04003020 if (res.acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003021 goto out_free;
3022 if (localpage)
Benny Halevy663c79b2009-04-01 09:21:59 -04003023 memcpy(buf, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003024 }
Benny Halevy663c79b2009-04-01 09:21:59 -04003025 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003026out_free:
3027 if (localpage)
3028 __free_page(localpage);
3029 return ret;
3030}
3031
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003032static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3033{
3034 struct nfs4_exception exception = { };
3035 ssize_t ret;
3036 do {
3037 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3038 if (ret >= 0)
3039 break;
3040 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3041 } while (exception.retry);
3042 return ret;
3043}
3044
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003045static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3046{
3047 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003048 int ret;
3049
3050 if (!nfs4_server_supports_acls(server))
3051 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003052 ret = nfs_revalidate_inode(server, inode);
3053 if (ret < 0)
3054 return ret;
Trond Myklebustf41f7412008-06-11 17:39:04 -04003055 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3056 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003057 ret = nfs4_read_cached_acl(inode, buf, buflen);
3058 if (ret != -ENOENT)
3059 return ret;
3060 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003061}
3062
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003063static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003064{
3065 struct nfs_server *server = NFS_SERVER(inode);
3066 struct page *pages[NFS4ACL_MAXPAGES];
3067 struct nfs_setaclargs arg = {
3068 .fh = NFS_FH(inode),
3069 .acl_pages = pages,
3070 .acl_len = buflen,
3071 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003072 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003073 struct rpc_message msg = {
3074 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3075 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003076 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003077 };
3078 int ret;
3079
3080 if (!nfs4_server_supports_acls(server))
3081 return -EOPNOTSUPP;
Trond Myklebust642ac542005-10-18 14:20:19 -07003082 nfs_inode_return_delegation(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003083 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003084 ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003085 nfs_access_zap_cache(inode);
3086 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003087 return ret;
3088}
3089
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003090static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3091{
3092 struct nfs4_exception exception = { };
3093 int err;
3094 do {
3095 err = nfs4_handle_exception(NFS_SERVER(inode),
3096 __nfs4_proc_set_acl(inode, buf, buflen),
3097 &exception);
3098 } while (exception.retry);
3099 return err;
3100}
3101
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102static int
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003103nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104{
David Howells7539bba2006-08-22 20:06:09 -04003105 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106
3107 if (!clp || task->tk_status >= 0)
3108 return 0;
3109 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003110 case -NFS4ERR_ADMIN_REVOKED:
3111 case -NFS4ERR_BAD_STATEID:
3112 case -NFS4ERR_OPENMODE:
3113 if (state == NULL)
3114 break;
3115 nfs4_state_mark_reclaim_nograce(clp, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 case -NFS4ERR_STALE_CLIENTID:
3117 case -NFS4ERR_STALE_STATEID:
3118 case -NFS4ERR_EXPIRED:
Trond Myklebust5d008372008-02-22 16:34:17 -05003119 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 nfs4_schedule_state_recovery(clp);
Trond Myklebuste005e802008-12-23 15:21:48 -05003121 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
Trond Myklebustfda13932008-02-22 16:34:12 -05003122 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 task->tk_status = 0;
3124 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 case -NFS4ERR_DELAY:
Trond Myklebust2e96d282008-06-11 16:42:05 -04003126 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003127 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3129 task->tk_status = 0;
3130 return -EAGAIN;
3131 case -NFS4ERR_OLD_STATEID:
3132 task->tk_status = 0;
3133 return -EAGAIN;
3134 }
3135 task->tk_status = nfs4_map_errors(task->tk_status);
3136 return 0;
3137}
3138
David Howellsadfa6f92006-08-22 20:06:08 -04003139int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, unsigned short port, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140{
3141 nfs4_verifier sc_verifier;
3142 struct nfs4_setclientid setclientid = {
3143 .sc_verifier = &sc_verifier,
3144 .sc_prog = program,
3145 };
3146 struct rpc_message msg = {
3147 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3148 .rpc_argp = &setclientid,
3149 .rpc_resp = clp,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003150 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 };
Al Virobc4785c2006-10-19 23:28:51 -07003152 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 int loop = 0;
3154 int status;
3155
Al Virobc4785c2006-10-19 23:28:51 -07003156 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3158 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3159
3160 for(;;) {
3161 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003162 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003163 clp->cl_ipaddr,
3164 rpc_peeraddr2str(clp->cl_rpcclient,
3165 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003166 rpc_peeraddr2str(clp->cl_rpcclient,
3167 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003168 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 clp->cl_id_uniquifier);
3170 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003171 sizeof(setclientid.sc_netid),
3172 rpc_peeraddr2str(clp->cl_rpcclient,
3173 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003175 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 clp->cl_ipaddr, port >> 8, port & 255);
3177
3178 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3179 if (status != -NFS4ERR_CLID_INUSE)
3180 break;
3181 if (signalled())
3182 break;
3183 if (loop++ & 1)
3184 ssleep(clp->cl_lease_time + 1);
3185 else
3186 if (++clp->cl_id_uniquifier == 0)
3187 break;
3188 }
3189 return status;
3190}
3191
David Howellsadfa6f92006-08-22 20:06:08 -04003192static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193{
3194 struct nfs_fsinfo fsinfo;
3195 struct rpc_message msg = {
3196 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
3197 .rpc_argp = clp,
3198 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003199 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 };
3201 unsigned long now;
3202 int status;
3203
3204 now = jiffies;
3205 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3206 if (status == 0) {
3207 spin_lock(&clp->cl_lock);
3208 clp->cl_lease_time = fsinfo.lease_time * HZ;
3209 clp->cl_last_renewal = now;
3210 spin_unlock(&clp->cl_lock);
3211 }
3212 return status;
3213}
3214
David Howellsadfa6f92006-08-22 20:06:08 -04003215int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
Trond Myklebust51581f32006-03-20 13:44:47 -05003216{
Benny Halevy77e03672008-06-24 20:25:57 +03003217 long timeout = 0;
Trond Myklebust51581f32006-03-20 13:44:47 -05003218 int err;
3219 do {
3220 err = _nfs4_proc_setclientid_confirm(clp, cred);
3221 switch (err) {
3222 case 0:
3223 return err;
3224 case -NFS4ERR_RESOURCE:
3225 /* The IBM lawyers misread another document! */
3226 case -NFS4ERR_DELAY:
3227 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3228 }
3229 } while (err == 0);
3230 return err;
3231}
3232
Trond Myklebustfe650402006-01-03 09:55:18 +01003233struct nfs4_delegreturndata {
3234 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003235 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003236 struct nfs_fh fh;
3237 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003238 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003239 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003240 int rpc_status;
3241};
3242
Trond Myklebustfe650402006-01-03 09:55:18 +01003243static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3244{
3245 struct nfs4_delegreturndata *data = calldata;
3246 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003247 if (data->rpc_status == 0)
Trond Myklebustfa178f22006-01-03 09:55:38 +01003248 renew_lease(data->res.server, data->timestamp);
Trond Myklebustfe650402006-01-03 09:55:18 +01003249}
3250
3251static void nfs4_delegreturn_release(void *calldata)
3252{
Trond Myklebustfe650402006-01-03 09:55:18 +01003253 kfree(calldata);
3254}
3255
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003256static const struct rpc_call_ops nfs4_delegreturn_ops = {
Trond Myklebustfe650402006-01-03 09:55:18 +01003257 .rpc_call_done = nfs4_delegreturn_done,
3258 .rpc_release = nfs4_delegreturn_release,
3259};
3260
Trond Myklebuste6f81072008-01-24 18:14:34 -05003261static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003262{
3263 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003264 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003265 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003266 struct rpc_message msg = {
3267 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3268 .rpc_cred = cred,
3269 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003270 struct rpc_task_setup task_setup_data = {
3271 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003272 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003273 .callback_ops = &nfs4_delegreturn_ops,
3274 .flags = RPC_TASK_ASYNC,
3275 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003276 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003277
3278 data = kmalloc(sizeof(*data), GFP_KERNEL);
3279 if (data == NULL)
3280 return -ENOMEM;
3281 data->args.fhandle = &data->fh;
3282 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003283 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003284 nfs_copy_fh(&data->fh, NFS_FH(inode));
3285 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003286 data->res.fattr = &data->fattr;
3287 data->res.server = server;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003288 data->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003289 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003290 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003291 data->rpc_status = 0;
3292
Trond Myklebustc970aa82007-07-14 15:39:59 -04003293 task_setup_data.callback_data = data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003294 msg.rpc_argp = &data->args,
3295 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003296 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003297 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003298 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003299 if (!issync)
3300 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003301 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003302 if (status != 0)
3303 goto out;
3304 status = data->rpc_status;
3305 if (status != 0)
3306 goto out;
3307 nfs_refresh_inode(inode, &data->fattr);
3308out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003309 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003310 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311}
3312
Trond Myklebuste6f81072008-01-24 18:14:34 -05003313int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314{
3315 struct nfs_server *server = NFS_SERVER(inode);
3316 struct nfs4_exception exception = { };
3317 int err;
3318 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003319 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 switch (err) {
3321 case -NFS4ERR_STALE_STATEID:
3322 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 case 0:
3324 return 0;
3325 }
3326 err = nfs4_handle_exception(server, err, &exception);
3327 } while (exception.retry);
3328 return err;
3329}
3330
3331#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3332#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3333
3334/*
3335 * sleep, with exponential backoff, and retry the LOCK operation.
3336 */
3337static unsigned long
3338nfs4_set_lock_task_retry(unsigned long timeout)
3339{
Matthew Wilcox150030b2007-12-06 16:24:39 -05003340 schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 timeout <<= 1;
3342 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3343 return NFS4_LOCK_MAXTIMEOUT;
3344 return timeout;
3345}
3346
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3348{
3349 struct inode *inode = state->inode;
3350 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003351 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003352 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003354 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003356 struct nfs_lockt_res res = {
3357 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 };
3359 struct rpc_message msg = {
3360 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3361 .rpc_argp = &arg,
3362 .rpc_resp = &res,
3363 .rpc_cred = state->owner->so_cred,
3364 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 struct nfs4_lock_state *lsp;
3366 int status;
3367
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003368 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003369 status = nfs4_set_lock_state(state, request);
3370 if (status != 0)
3371 goto out;
3372 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003373 arg.lock_owner.id = lsp->ls_id.id;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003374 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003375 switch (status) {
3376 case 0:
3377 request->fl_type = F_UNLCK;
3378 break;
3379 case -NFS4ERR_DENIED:
3380 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05003382 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003383out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 return status;
3385}
3386
3387static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3388{
3389 struct nfs4_exception exception = { };
3390 int err;
3391
3392 do {
3393 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3394 _nfs4_proc_getlk(state, cmd, request),
3395 &exception);
3396 } while (exception.retry);
3397 return err;
3398}
3399
3400static int do_vfs_lock(struct file *file, struct file_lock *fl)
3401{
3402 int res = 0;
3403 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3404 case FL_POSIX:
3405 res = posix_lock_file_wait(file, fl);
3406 break;
3407 case FL_FLOCK:
3408 res = flock_lock_file_wait(file, fl);
3409 break;
3410 default:
3411 BUG();
3412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 return res;
3414}
3415
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003416struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003417 struct nfs_locku_args arg;
3418 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003419 struct nfs4_lock_state *lsp;
3420 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003421 struct file_lock fl;
3422 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003423 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003424};
3425
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003426static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3427 struct nfs_open_context *ctx,
3428 struct nfs4_lock_state *lsp,
3429 struct nfs_seqid *seqid)
3430{
3431 struct nfs4_unlockdata *p;
3432 struct inode *inode = lsp->ls_state->inode;
3433
3434 p = kmalloc(sizeof(*p), GFP_KERNEL);
3435 if (p == NULL)
3436 return NULL;
3437 p->arg.fh = NFS_FH(inode);
3438 p->arg.fl = &p->fl;
3439 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003440 p->res.seqid = seqid;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003441 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003442 p->arg.stateid = &lsp->ls_stateid;
3443 p->lsp = lsp;
3444 atomic_inc(&lsp->ls_count);
3445 /* Ensure we don't close file until we're done freeing locks! */
3446 p->ctx = get_nfs_open_context(ctx);
3447 memcpy(&p->fl, fl, sizeof(p->fl));
3448 p->server = NFS_SERVER(inode);
3449 return p;
3450}
3451
Trond Myklebust06f814a2006-01-03 09:55:07 +01003452static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003453{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003454 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003455 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003456 nfs4_put_lock_state(calldata->lsp);
3457 put_nfs_open_context(calldata->ctx);
3458 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003459}
3460
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003461static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003462{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003463 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003464
Trond Myklebust06f814a2006-01-03 09:55:07 +01003465 if (RPC_ASSASSINATED(task))
3466 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003467 switch (task->tk_status) {
3468 case 0:
3469 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003470 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003471 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01003472 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003473 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003474 case -NFS4ERR_BAD_STATEID:
3475 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003476 case -NFS4ERR_STALE_STATEID:
3477 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003478 break;
3479 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003480 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003481 rpc_restart_call(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003482 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003483}
3484
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003485static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003486{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003487 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003489 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003490 return;
3491 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003492 /* Note: exit _without_ running nfs4_locku_done */
3493 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003494 return;
3495 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003496 calldata->timestamp = jiffies;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003497 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498}
3499
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003500static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003501 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003502 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01003503 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003504};
3505
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003506static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3507 struct nfs_open_context *ctx,
3508 struct nfs4_lock_state *lsp,
3509 struct nfs_seqid *seqid)
3510{
3511 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003512 struct rpc_message msg = {
3513 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3514 .rpc_cred = ctx->cred,
3515 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003516 struct rpc_task_setup task_setup_data = {
3517 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04003518 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003519 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003520 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003521 .flags = RPC_TASK_ASYNC,
3522 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003523
Frank Filz137d6ac2007-07-09 15:32:29 -07003524 /* Ensure this is an unlock - when canceling a lock, the
3525 * canceled lock is passed in, and it won't be an unlock.
3526 */
3527 fl->fl_type = F_UNLCK;
3528
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003529 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3530 if (data == NULL) {
3531 nfs_free_seqid(seqid);
3532 return ERR_PTR(-ENOMEM);
3533 }
3534
Trond Myklebust5138fde2007-07-14 15:40:01 -04003535 msg.rpc_argp = &data->arg,
3536 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003537 task_setup_data.callback_data = data;
3538 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003539}
3540
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3542{
Trond Myklebust19e03c52008-12-23 15:21:44 -05003543 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003544 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003545 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01003546 struct rpc_task *task;
3547 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003548 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549
Trond Myklebust9b073572006-06-29 16:38:34 -04003550 status = nfs4_set_lock_state(state, request);
3551 /* Unlock _before_ we do the RPC call */
3552 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003553 down_read(&nfsi->rwsem);
3554 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
3555 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003556 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003557 }
3558 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003559 if (status != 0)
3560 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003561 /* Is this a delegated lock? */
3562 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04003563 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003564 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003565 seqid = nfs_alloc_seqid(&lsp->ls_seqid);
Trond Myklebust9b073572006-06-29 16:38:34 -04003566 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003567 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04003568 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04003569 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003570 status = PTR_ERR(task);
3571 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04003572 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003573 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003574 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04003575out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003576 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003577 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578}
3579
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003580struct nfs4_lockdata {
3581 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003582 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003583 struct nfs4_lock_state *lsp;
3584 struct nfs_open_context *ctx;
3585 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003586 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003587 int rpc_status;
3588 int cancelled;
3589};
3590
3591static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
3592 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
3593{
3594 struct nfs4_lockdata *p;
3595 struct inode *inode = lsp->ls_state->inode;
3596 struct nfs_server *server = NFS_SERVER(inode);
3597
3598 p = kzalloc(sizeof(*p), GFP_KERNEL);
3599 if (p == NULL)
3600 return NULL;
3601
3602 p->arg.fh = NFS_FH(inode);
3603 p->arg.fl = &p->fl;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003604 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid);
3605 if (p->arg.open_seqid == NULL)
3606 goto out_free;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003607 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3608 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003609 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003610 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04003611 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003612 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003613 p->res.lock_seqid = p->arg.lock_seqid;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003614 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003615 p->lsp = lsp;
3616 atomic_inc(&lsp->ls_count);
3617 p->ctx = get_nfs_open_context(ctx);
3618 memcpy(&p->fl, fl, sizeof(p->fl));
3619 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003620out_free_seqid:
3621 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003622out_free:
3623 kfree(p);
3624 return NULL;
3625}
3626
3627static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
3628{
3629 struct nfs4_lockdata *data = calldata;
3630 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631
Harvey Harrison3110ff82008-05-02 13:42:44 -07003632 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003633 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
3634 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003635 /* Do we need to do an open_to_lock_owner? */
3636 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05003637 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
3638 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003639 data->arg.open_stateid = &state->stateid;
3640 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003641 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003642 } else
3643 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003644 data->timestamp = jiffies;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003645 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003646 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003647}
3648
3649static void nfs4_lock_done(struct rpc_task *task, void *calldata)
3650{
3651 struct nfs4_lockdata *data = calldata;
3652
Harvey Harrison3110ff82008-05-02 13:42:44 -07003653 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003654
3655 data->rpc_status = task->tk_status;
3656 if (RPC_ASSASSINATED(task))
3657 goto out;
3658 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003659 if (data->rpc_status == 0)
3660 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
3661 else
3662 goto out;
3663 }
3664 if (data->rpc_status == 0) {
3665 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
3666 sizeof(data->lsp->ls_stateid.data));
3667 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust88be9f92007-06-05 10:42:27 -04003668 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003669 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003670out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07003671 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003672}
3673
3674static void nfs4_lock_release(void *calldata)
3675{
3676 struct nfs4_lockdata *data = calldata;
3677
Harvey Harrison3110ff82008-05-02 13:42:44 -07003678 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003679 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003680 if (data->cancelled != 0) {
3681 struct rpc_task *task;
3682 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
3683 data->arg.lock_seqid);
3684 if (!IS_ERR(task))
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003685 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003686 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003687 } else
3688 nfs_free_seqid(data->arg.lock_seqid);
3689 nfs4_put_lock_state(data->lsp);
3690 put_nfs_open_context(data->ctx);
3691 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003692 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003693}
3694
3695static const struct rpc_call_ops nfs4_lock_ops = {
3696 .rpc_call_prepare = nfs4_lock_prepare,
3697 .rpc_call_done = nfs4_lock_done,
3698 .rpc_release = nfs4_lock_release,
3699};
3700
3701static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int reclaim)
3702{
3703 struct nfs4_lockdata *data;
3704 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003705 struct rpc_message msg = {
3706 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
3707 .rpc_cred = state->owner->so_cred,
3708 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003709 struct rpc_task_setup task_setup_data = {
3710 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04003711 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003712 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003713 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003714 .flags = RPC_TASK_ASYNC,
3715 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003716 int ret;
3717
Harvey Harrison3110ff82008-05-02 13:42:44 -07003718 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04003719 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003720 fl->fl_u.nfs4_fl.owner);
3721 if (data == NULL)
3722 return -ENOMEM;
3723 if (IS_SETLKW(cmd))
3724 data->arg.block = 1;
3725 if (reclaim != 0)
3726 data->arg.reclaim = 1;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003727 msg.rpc_argp = &data->arg,
3728 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003729 task_setup_data.callback_data = data;
3730 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003731 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003732 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003733 ret = nfs4_wait_for_completion_rpc_task(task);
3734 if (ret == 0) {
3735 ret = data->rpc_status;
3736 if (ret == -NFS4ERR_DENIED)
3737 ret = -EAGAIN;
3738 } else
3739 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003740 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003741 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003742 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743}
3744
3745static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
3746{
Trond Myklebust202b50d2005-06-22 17:16:29 +00003747 struct nfs_server *server = NFS_SERVER(state->inode);
3748 struct nfs4_exception exception = { };
3749 int err;
3750
3751 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04003752 /* Cache the lock if possible... */
3753 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3754 return 0;
Trond Myklebust202b50d2005-06-22 17:16:29 +00003755 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
3756 if (err != -NFS4ERR_DELAY)
3757 break;
3758 nfs4_handle_exception(server, err, &exception);
3759 } while (exception.retry);
3760 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761}
3762
3763static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
3764{
Trond Myklebust202b50d2005-06-22 17:16:29 +00003765 struct nfs_server *server = NFS_SERVER(state->inode);
3766 struct nfs4_exception exception = { };
3767 int err;
3768
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003769 err = nfs4_set_lock_state(state, request);
3770 if (err != 0)
3771 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00003772 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04003773 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3774 return 0;
Trond Myklebust202b50d2005-06-22 17:16:29 +00003775 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
3776 if (err != -NFS4ERR_DELAY)
3777 break;
3778 nfs4_handle_exception(server, err, &exception);
3779 } while (exception.retry);
3780 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781}
3782
3783static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3784{
Trond Myklebust19e03c52008-12-23 15:21:44 -05003785 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04003786 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 int status;
3788
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003789 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003790 status = nfs4_set_lock_state(state, request);
3791 if (status != 0)
3792 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04003793 request->fl_flags |= FL_ACCESS;
3794 status = do_vfs_lock(request->fl_file, request);
3795 if (status < 0)
3796 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003797 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04003798 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04003799 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04003800 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05003801 request->fl_flags = fl_flags & ~FL_SLEEP;
3802 status = do_vfs_lock(request->fl_file, request);
3803 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04003804 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003805 status = _nfs4_do_setlk(state, cmd, request, 0);
3806 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04003807 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003808 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04003809 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003810 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07003811 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04003812out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05003813 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04003814out:
3815 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816 return status;
3817}
3818
3819static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3820{
3821 struct nfs4_exception exception = { };
3822 int err;
3823
3824 do {
3825 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3826 _nfs4_proc_setlk(state, cmd, request),
3827 &exception);
3828 } while (exception.retry);
3829 return err;
3830}
3831
3832static int
3833nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
3834{
3835 struct nfs_open_context *ctx;
3836 struct nfs4_state *state;
3837 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
3838 int status;
3839
3840 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04003841 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842 state = ctx->state;
3843
3844 if (request->fl_start < 0 || request->fl_end < 0)
3845 return -EINVAL;
3846
3847 if (IS_GETLK(cmd))
3848 return nfs4_proc_getlk(state, F_GETLK, request);
3849
3850 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
3851 return -EINVAL;
3852
3853 if (request->fl_type == F_UNLCK)
3854 return nfs4_proc_unlck(state, cmd, request);
3855
3856 do {
3857 status = nfs4_proc_setlk(state, cmd, request);
3858 if ((status != -EAGAIN) || IS_SETLK(cmd))
3859 break;
3860 timeout = nfs4_set_lock_task_retry(timeout);
3861 status = -ERESTARTSYS;
3862 if (signalled())
3863 break;
3864 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 return status;
3866}
3867
Trond Myklebust888e6942005-11-04 15:38:11 -05003868int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
3869{
3870 struct nfs_server *server = NFS_SERVER(state->inode);
3871 struct nfs4_exception exception = { };
3872 int err;
3873
3874 err = nfs4_set_lock_state(state, fl);
3875 if (err != 0)
3876 goto out;
3877 do {
3878 err = _nfs4_do_setlk(state, F_SETLK, fl, 0);
3879 if (err != -NFS4ERR_DELAY)
3880 break;
3881 err = nfs4_handle_exception(server, err, &exception);
3882 } while (exception.retry);
3883out:
3884 return err;
3885}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003886
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003887#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
3888
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003889int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
3890 size_t buflen, int flags)
3891{
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003892 struct inode *inode = dentry->d_inode;
3893
3894 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3895 return -EOPNOTSUPP;
3896
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003897 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003898}
3899
3900/* The getxattr man page suggests returning -ENODATA for unknown attributes,
3901 * and that's what we'll do for e.g. user attributes that haven't been set.
3902 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
3903 * attributes in kernel-managed attribute namespaces. */
3904ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
3905 size_t buflen)
3906{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003907 struct inode *inode = dentry->d_inode;
3908
3909 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3910 return -EOPNOTSUPP;
3911
3912 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003913}
3914
3915ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
3916{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003917 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003918
J. Bruce Fields096455a2006-03-20 23:23:42 -05003919 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
3920 return 0;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003921 if (buf && buflen < len)
3922 return -ERANGE;
3923 if (buf)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003924 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
3925 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003926}
3927
Trond Myklebust69aaaae2009-03-11 14:10:28 -04003928static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
3929{
3930 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
3931 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
3932 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
3933 return;
3934
3935 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
3936 NFS_ATTR_FATTR_NLINK;
3937 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3938 fattr->nlink = 2;
3939}
3940
Trond Myklebust56659e92007-07-17 21:52:39 -04003941int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04003942 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04003943{
3944 struct nfs_server *server = NFS_SERVER(dir);
3945 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04003946 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
3947 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
Trond Myklebust683b57b2006-06-09 09:34:22 -04003948 };
3949 struct nfs4_fs_locations_arg args = {
3950 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05003951 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04003952 .page = page,
3953 .bitmask = bitmask,
3954 };
Benny Halevy22958462009-04-01 09:22:02 -04003955 struct nfs4_fs_locations_res res = {
3956 .fs_locations = fs_locations,
3957 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04003958 struct rpc_message msg = {
3959 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
3960 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04003961 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04003962 };
3963 int status;
3964
Harvey Harrison3110ff82008-05-02 13:42:44 -07003965 dprintk("%s: start\n", __func__);
Trond Myklebustc228fd32007-01-13 02:28:11 -05003966 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04003967 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04003968 fs_locations->nlocations = 0;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003969 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Trond Myklebust69aaaae2009-03-11 14:10:28 -04003970 nfs_fixup_referral_attributes(&fs_locations->fattr);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003971 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04003972 return status;
3973}
3974
Andy Adamson557134a2009-04-01 09:21:53 -04003975#ifdef CONFIG_NFS_V4_1
3976/* Destroy the slot table */
3977static void nfs4_destroy_slot_table(struct nfs4_session *session)
3978{
3979 if (session->fc_slot_table.slots == NULL)
3980 return;
3981 kfree(session->fc_slot_table.slots);
3982 session->fc_slot_table.slots = NULL;
3983 return;
3984}
3985
3986struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
3987{
3988 struct nfs4_session *session;
3989 struct nfs4_slot_table *tbl;
3990
3991 session = kzalloc(sizeof(struct nfs4_session), GFP_KERNEL);
3992 if (!session)
3993 return NULL;
3994 tbl = &session->fc_slot_table;
3995 spin_lock_init(&tbl->slot_tbl_lock);
3996 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "Slot table");
3997 session->clp = clp;
3998 return session;
3999}
4000
4001void nfs4_destroy_session(struct nfs4_session *session)
4002{
4003 nfs4_destroy_slot_table(session);
4004 kfree(session);
4005}
4006
4007#endif /* CONFIG_NFS_V4_1 */
4008
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05004010 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05004011 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 .recover_open = nfs4_open_reclaim,
4013 .recover_lock = nfs4_lock_reclaim,
4014};
4015
Trond Myklebustb79a4a12008-12-23 15:21:41 -05004016struct nfs4_state_recovery_ops nfs4_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05004017 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05004018 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 .recover_open = nfs4_open_expired,
4020 .recover_lock = nfs4_lock_expired,
4021};
4022
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08004023static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004024 .permission = nfs_permission,
4025 .getattr = nfs_getattr,
4026 .setattr = nfs_setattr,
4027 .getxattr = nfs4_getxattr,
4028 .setxattr = nfs4_setxattr,
4029 .listxattr = nfs4_listxattr,
4030};
4031
David Howells509de812006-08-22 20:06:11 -04004032const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033 .version = 4, /* protocol version */
4034 .dentry_ops = &nfs4_dentry_operations,
4035 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004036 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037 .getroot = nfs4_proc_get_root,
4038 .getattr = nfs4_proc_getattr,
4039 .setattr = nfs4_proc_setattr,
David Howells2b3de442006-08-22 20:06:09 -04004040 .lookupfh = nfs4_proc_lookupfh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041 .lookup = nfs4_proc_lookup,
4042 .access = nfs4_proc_access,
4043 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 .create = nfs4_proc_create,
4045 .remove = nfs4_proc_remove,
4046 .unlink_setup = nfs4_proc_unlink_setup,
4047 .unlink_done = nfs4_proc_unlink_done,
4048 .rename = nfs4_proc_rename,
4049 .link = nfs4_proc_link,
4050 .symlink = nfs4_proc_symlink,
4051 .mkdir = nfs4_proc_mkdir,
4052 .rmdir = nfs4_proc_remove,
4053 .readdir = nfs4_proc_readdir,
4054 .mknod = nfs4_proc_mknod,
4055 .statfs = nfs4_proc_statfs,
4056 .fsinfo = nfs4_proc_fsinfo,
4057 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04004058 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059 .decode_dirent = nfs4_decode_dirent,
4060 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05004061 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05004063 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05004065 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004067 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04004068 .close_context = nfs4_close_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069};
4070
4071/*
4072 * Local variables:
4073 * c-basic-offset: 8
4074 * End:
4075 */