blob: 861b103c9e3c67577903ee0c2251b98acad77b3e [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
Andy Adamson13615872009-04-01 09:22:17 -0400312void nfs41_sequence_free_slot(const struct nfs_client *clp,
313 struct nfs4_sequence_res *res)
314{
315 struct nfs4_slot_table *tbl;
316
317 if (!nfs4_has_session(clp)) {
318 dprintk("%s: No session\n", __func__);
319 return;
320 }
321 tbl = &clp->cl_session->fc_slot_table;
322 if (res->sr_slotid == NFS4_MAX_SLOT_TABLE) {
323 dprintk("%s: No slot\n", __func__);
324 /* just wake up the next guy waiting since
325 * we may have not consumed a slot after all */
326 rpc_wake_up_next(&tbl->slot_tbl_waitq);
327 return;
328 }
329 nfs4_free_slot(tbl, res->sr_slotid);
330 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
331}
332
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400333/*
Benny Halevy510b8172009-04-01 09:22:14 -0400334 * nfs4_find_slot - efficiently look for a free slot
335 *
336 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
337 * If found, we mark the slot as used, update the highest_used_slotid,
338 * and respectively set up the sequence operation args.
339 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400340 *
341 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400342 */
343static u8
344nfs4_find_slot(struct nfs4_slot_table *tbl, struct rpc_task *task)
345{
346 int slotid;
347 u8 ret_id = NFS4_MAX_SLOT_TABLE;
348 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
349
Benny Halevy510b8172009-04-01 09:22:14 -0400350 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
351 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
352 tbl->max_slots);
353 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
354 if (slotid >= tbl->max_slots)
355 goto out;
356 __set_bit(slotid, tbl->used_slots);
357 if (slotid > tbl->highest_used_slotid)
358 tbl->highest_used_slotid = slotid;
359 ret_id = slotid;
360out:
361 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
362 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400363 return ret_id;
364}
365
Andy Adamsonce5039c2009-04-01 09:22:13 -0400366static int nfs41_setup_sequence(struct nfs4_session *session,
367 struct nfs4_sequence_args *args,
368 struct nfs4_sequence_res *res,
369 int cache_reply,
370 struct rpc_task *task)
371{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400372 struct nfs4_slot *slot;
373 struct nfs4_slot_table *tbl;
374 u8 slotid;
375
376 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400377 /* slot already allocated? */
378 if (res->sr_slotid != NFS4_MAX_SLOT_TABLE)
379 return 0;
380
381 memset(res, 0, sizeof(*res));
382 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400383 tbl = &session->fc_slot_table;
384
385 spin_lock(&tbl->slot_tbl_lock);
386 slotid = nfs4_find_slot(tbl, task);
387 if (slotid == NFS4_MAX_SLOT_TABLE) {
388 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
389 spin_unlock(&tbl->slot_tbl_lock);
390 dprintk("<-- %s: no free slots\n", __func__);
391 return -EAGAIN;
392 }
393 spin_unlock(&tbl->slot_tbl_lock);
394
395 slot = tbl->slots + slotid;
396 args->sa_slotid = slotid;
397 args->sa_cache_this = cache_reply;
398
399 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
400
401 res->sr_slotid = slotid;
402 res->sr_renewal_time = jiffies;
403 /*
404 * sr_status is only set in decode_sequence, and so will remain
405 * set to 1 if an rpc level failure occurs.
406 */
407 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400408 return 0;
409}
410
411int nfs4_setup_sequence(struct nfs_client *clp,
412 struct nfs4_sequence_args *args,
413 struct nfs4_sequence_res *res,
414 int cache_reply,
415 struct rpc_task *task)
416{
417 int ret = 0;
418
419 dprintk("--> %s clp %p session %p sr_slotid %d\n",
420 __func__, clp, clp->cl_session, res->sr_slotid);
421
422 if (!nfs4_has_session(clp))
423 goto out;
424 ret = nfs41_setup_sequence(clp->cl_session, args, res, cache_reply,
425 task);
426 if (ret != -EAGAIN) {
427 /* terminate rpc task */
428 task->tk_status = ret;
429 task->tk_action = NULL;
430 }
431out:
432 dprintk("<-- %s status=%d\n", __func__, ret);
433 return ret;
434}
435
436struct nfs41_call_sync_data {
437 struct nfs_client *clp;
438 struct nfs4_sequence_args *seq_args;
439 struct nfs4_sequence_res *seq_res;
440 int cache_reply;
441};
442
443static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
444{
445 struct nfs41_call_sync_data *data = calldata;
446
447 dprintk("--> %s data->clp->cl_session %p\n", __func__,
448 data->clp->cl_session);
449 if (nfs4_setup_sequence(data->clp, data->seq_args,
450 data->seq_res, data->cache_reply, task))
451 return;
452 rpc_call_start(task);
453}
454
455struct rpc_call_ops nfs41_call_sync_ops = {
456 .rpc_call_prepare = nfs41_call_sync_prepare,
457};
458
459static int nfs4_call_sync_sequence(struct nfs_client *clp,
460 struct rpc_clnt *clnt,
461 struct rpc_message *msg,
462 struct nfs4_sequence_args *args,
463 struct nfs4_sequence_res *res,
464 int cache_reply)
465{
466 int ret;
467 struct rpc_task *task;
468 struct nfs41_call_sync_data data = {
469 .clp = clp,
470 .seq_args = args,
471 .seq_res = res,
472 .cache_reply = cache_reply,
473 };
474 struct rpc_task_setup task_setup = {
475 .rpc_client = clnt,
476 .rpc_message = msg,
477 .callback_ops = &nfs41_call_sync_ops,
478 .callback_data = &data
479 };
480
481 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
482 task = rpc_run_task(&task_setup);
483 if (IS_ERR(task))
484 ret = PTR_ERR(task);
485 else {
486 ret = task->tk_status;
487 rpc_put_task(task);
488 }
489 return ret;
490}
491
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400492int _nfs4_call_sync_session(struct nfs_server *server,
493 struct rpc_message *msg,
494 struct nfs4_sequence_args *args,
495 struct nfs4_sequence_res *res,
496 int cache_reply)
497{
Andy Adamsonce5039c2009-04-01 09:22:13 -0400498 return nfs4_call_sync_sequence(server->nfs_client, server->client,
499 msg, args, res, cache_reply);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400500}
501
502#endif /* CONFIG_NFS_V4_1 */
503
504int _nfs4_call_sync(struct nfs_server *server,
505 struct rpc_message *msg,
506 struct nfs4_sequence_args *args,
507 struct nfs4_sequence_res *res,
508 int cache_reply)
509{
Benny Halevyf3752972009-04-01 09:22:04 -0400510 args->sa_session = res->sr_session = NULL;
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400511 return rpc_call_sync(server->client, msg, 0);
512}
513
514#define nfs4_call_sync(server, msg, args, res, cache_reply) \
515 (server)->nfs_client->cl_call_sync((server), (msg), &(args)->seq_args, \
516 &(res)->seq_res, (cache_reply))
517
Trond Myklebust38478b22006-05-25 01:40:57 -0400518static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
Trond Myklebust38478b22006-05-25 01:40:57 -0400520 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Trond Myklebust38478b22006-05-25 01:40:57 -0400522 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400523 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
524 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400525 nfs_force_lookup_revalidate(dir);
Trond Myklebust40d24702007-10-08 09:24:22 -0400526 nfsi->change_attr = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400527 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528}
529
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100530struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400531 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100532 struct nfs_openargs o_arg;
533 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100534 struct nfs_open_confirmargs c_arg;
535 struct nfs_open_confirmres c_res;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100536 struct nfs_fattr f_attr;
537 struct nfs_fattr dir_attr;
Trond Myklebustad389da2007-06-05 12:30:00 -0400538 struct path path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100539 struct dentry *dir;
540 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400541 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100542 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100543 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400544 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100545 int rpc_status;
546 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100547};
548
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400549
550static void nfs4_init_opendata_res(struct nfs4_opendata *p)
551{
552 p->o_res.f_attr = &p->f_attr;
553 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400554 p->o_res.seqid = p->o_arg.seqid;
555 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400556 p->o_res.server = p->o_arg.server;
557 nfs_fattr_init(&p->f_attr);
558 nfs_fattr_init(&p->dir_attr);
559}
560
Trond Myklebustad389da2007-06-05 12:30:00 -0400561static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500562 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100563 const struct iattr *attrs)
564{
Trond Myklebustad389da2007-06-05 12:30:00 -0400565 struct dentry *parent = dget_parent(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100566 struct inode *dir = parent->d_inode;
567 struct nfs_server *server = NFS_SERVER(dir);
568 struct nfs4_opendata *p;
569
570 p = kzalloc(sizeof(*p), GFP_KERNEL);
571 if (p == NULL)
572 goto err;
573 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
574 if (p->o_arg.seqid == NULL)
575 goto err_free;
Trond Myklebustad389da2007-06-05 12:30:00 -0400576 p->path.mnt = mntget(path->mnt);
577 p->path.dentry = dget(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100578 p->dir = parent;
579 p->owner = sp;
580 atomic_inc(&sp->so_count);
581 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500582 p->o_arg.open_flags = flags;
583 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400584 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400585 p->o_arg.id = sp->so_owner_id.id;
Trond Myklebustad389da2007-06-05 12:30:00 -0400586 p->o_arg.name = &p->path.dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100587 p->o_arg.server = server;
588 p->o_arg.bitmask = server->attr_bitmask;
589 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Andy Adamson5f7dbd52009-04-01 09:22:05 -0400590 p->o_res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100591 if (flags & O_EXCL) {
592 u32 *s = (u32 *) p->o_arg.u.verifier.data;
593 s[0] = jiffies;
594 s[1] = current->pid;
595 } else if (flags & O_CREAT) {
596 p->o_arg.u.attrs = &p->attrs;
597 memcpy(&p->attrs, attrs, sizeof(p->attrs));
598 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100599 p->c_arg.fh = &p->o_res.fh;
600 p->c_arg.stateid = &p->o_res.stateid;
601 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400602 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400603 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100604 return p;
605err_free:
606 kfree(p);
607err:
608 dput(parent);
609 return NULL;
610}
611
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400612static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100613{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400614 struct nfs4_opendata *p = container_of(kref,
615 struct nfs4_opendata, kref);
616
617 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400618 if (p->state != NULL)
619 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400620 nfs4_put_state_owner(p->owner);
621 dput(p->dir);
Jan Blunck31f31db2008-05-02 13:42:45 -0700622 path_put(&p->path);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400623 kfree(p);
624}
625
626static void nfs4_opendata_put(struct nfs4_opendata *p)
627{
628 if (p != NULL)
629 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100630}
631
Trond Myklebust06f814a2006-01-03 09:55:07 +0100632static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
633{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100634 int ret;
635
Trond Myklebust06f814a2006-01-03 09:55:07 +0100636 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100637 return ret;
638}
639
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500640static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400641{
642 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500643
644 if (open_mode & O_EXCL)
645 goto out;
646 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400647 case FMODE_READ:
648 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400649 break;
650 case FMODE_WRITE:
651 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400652 break;
653 case FMODE_READ|FMODE_WRITE:
654 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0;
655 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500656out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400657 return ret;
658}
659
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500660static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400661{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500662 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400663 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500664 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400665 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500666 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400667 return 1;
668}
669
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500670static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100671{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500672 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100673 case FMODE_WRITE:
674 state->n_wronly++;
675 break;
676 case FMODE_READ:
677 state->n_rdonly++;
678 break;
679 case FMODE_READ|FMODE_WRITE:
680 state->n_rdwr++;
681 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500682 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100683}
684
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500685static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400686{
687 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
688 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
689 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500690 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400691 case FMODE_READ:
692 set_bit(NFS_O_RDONLY_STATE, &state->flags);
693 break;
694 case FMODE_WRITE:
695 set_bit(NFS_O_WRONLY_STATE, &state->flags);
696 break;
697 case FMODE_READ|FMODE_WRITE:
698 set_bit(NFS_O_RDWR_STATE, &state->flags);
699 }
700}
701
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500702static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400703{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400704 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500705 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400706 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400707}
708
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500709static 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 -0700710{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400711 /*
712 * Protect the call to nfs4_state_set_mode_locked and
713 * serialise the stateid update
714 */
715 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400716 if (deleg_stateid != NULL) {
717 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
718 set_bit(NFS_DELEGATED_STATE, &state->flags);
719 }
720 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500721 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400722 write_sequnlock(&state->seqlock);
723 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500724 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700725 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726}
727
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500728static 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 -0500729{
730 struct nfs_inode *nfsi = NFS_I(state->inode);
731 struct nfs_delegation *deleg_cur;
732 int ret = 0;
733
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500734 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500735
736 rcu_read_lock();
737 deleg_cur = rcu_dereference(nfsi->delegation);
738 if (deleg_cur == NULL)
739 goto no_delegation;
740
741 spin_lock(&deleg_cur->lock);
742 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500743 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500744 goto no_delegation_unlock;
745
746 if (delegation == NULL)
747 delegation = &deleg_cur->stateid;
748 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
749 goto no_delegation_unlock;
750
Trond Myklebustb7391f42008-12-23 15:21:52 -0500751 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500752 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500753 ret = 1;
754no_delegation_unlock:
755 spin_unlock(&deleg_cur->lock);
756no_delegation:
757 rcu_read_unlock();
758
759 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500760 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500761 ret = 1;
762 }
763
764 return ret;
765}
766
767
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500768static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400769{
770 struct nfs_delegation *delegation;
771
772 rcu_read_lock();
773 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500774 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -0400775 rcu_read_unlock();
776 return;
777 }
778 rcu_read_unlock();
779 nfs_inode_return_delegation(inode);
780}
781
Trond Myklebust6ee41262007-07-08 14:11:36 -0400782static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400783{
784 struct nfs4_state *state = opendata->state;
785 struct nfs_inode *nfsi = NFS_I(state->inode);
786 struct nfs_delegation *delegation;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500787 int open_mode = opendata->o_arg.open_flags & O_EXCL;
788 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400789 nfs4_stateid stateid;
790 int ret = -EAGAIN;
791
Trond Myklebustaac00a82007-07-05 19:02:21 -0400792 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500793 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400794 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500795 if (can_open_cached(state, fmode, open_mode)) {
796 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400797 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400798 goto out_return_state;
799 }
800 spin_unlock(&state->owner->so_lock);
801 }
Trond Myklebust34310432008-12-23 15:21:38 -0500802 rcu_read_lock();
803 delegation = rcu_dereference(nfsi->delegation);
804 if (delegation == NULL ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500805 !can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -0500806 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -0400807 break;
Trond Myklebust34310432008-12-23 15:21:38 -0500808 }
Trond Myklebustaac00a82007-07-05 19:02:21 -0400809 /* Save the delegation */
810 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
811 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -0400812 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400813 if (ret != 0)
814 goto out;
815 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -0500816
817 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500818 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -0500819 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400820 }
Trond Myklebustaac00a82007-07-05 19:02:21 -0400821out:
822 return ERR_PTR(ret);
823out_return_state:
824 atomic_inc(&state->count);
825 return state;
826}
827
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100828static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
829{
830 struct inode *inode;
831 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -0400832 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400833 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100834
Trond Myklebustaac00a82007-07-05 19:02:21 -0400835 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400836 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400837 goto out;
838 }
839
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400840 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100841 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400842 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100843 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400844 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -0500845 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400846 goto err;
847 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100848 state = nfs4_get_open_state(inode, data->owner);
849 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400850 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -0400851 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -0400852 int delegation_flags = 0;
853
Trond Myklebust003707c2007-07-05 18:07:55 -0400854 rcu_read_lock();
855 delegation = rcu_dereference(NFS_I(inode)->delegation);
856 if (delegation)
857 delegation_flags = delegation->flags;
858 rcu_read_unlock();
Trond Myklebust15c831b2008-12-23 15:21:39 -0500859 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -0400860 nfs_inode_set_delegation(state->inode,
861 data->owner->so_cred,
862 &data->o_res);
863 else
864 nfs_inode_reclaim_delegation(state->inode,
865 data->owner->so_cred,
866 &data->o_res);
867 }
Trond Myklebust34310432008-12-23 15:21:38 -0500868
869 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500870 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100871 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400872out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100873 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400874err_put_inode:
875 iput(inode);
876err:
877 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100878}
879
Trond Myklebust864472e2006-01-03 09:55:15 +0100880static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
881{
882 struct nfs_inode *nfsi = NFS_I(state->inode);
883 struct nfs_open_context *ctx;
884
885 spin_lock(&state->inode->i_lock);
886 list_for_each_entry(ctx, &nfsi->open_files, list) {
887 if (ctx->state != state)
888 continue;
889 get_nfs_open_context(ctx);
890 spin_unlock(&state->inode->i_lock);
891 return ctx;
892 }
893 spin_unlock(&state->inode->i_lock);
894 return ERR_PTR(-ENOENT);
895}
896
Trond Myklebust6f220ed2007-07-17 21:50:45 -0400897static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
898{
899 struct nfs4_opendata *opendata;
900
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500901 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL);
Trond Myklebust6f220ed2007-07-17 21:50:45 -0400902 if (opendata == NULL)
903 return ERR_PTR(-ENOMEM);
904 opendata->state = state;
905 atomic_inc(&state->count);
906 return opendata;
907}
908
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500909static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +0100910{
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400911 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +0100912 int ret;
913
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500914 opendata->o_arg.open_flags = 0;
915 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400916 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
917 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
918 nfs4_init_opendata_res(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +0100919 ret = _nfs4_proc_open(opendata);
920 if (ret != 0)
921 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400922 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400923 if (IS_ERR(newstate))
924 return PTR_ERR(newstate);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500925 nfs4_close_state(&opendata->path, newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400926 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +0100927 return 0;
928}
929
930static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
931{
Trond Myklebust864472e2006-01-03 09:55:15 +0100932 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +0100933 int ret;
934
935 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400936 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +0100937 smp_rmb();
938 if (state->n_rdwr != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400939 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +0100940 if (ret != 0)
941 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400942 if (newstate != state)
943 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +0100944 }
945 if (state->n_wronly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400946 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +0100947 if (ret != 0)
948 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400949 if (newstate != state)
950 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +0100951 }
952 if (state->n_rdonly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400953 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +0100954 if (ret != 0)
955 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400956 if (newstate != state)
957 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +0100958 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -0400959 /*
960 * We may have performed cached opens for all three recoveries.
961 * Check if we need to update the current stateid.
962 */
963 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
964 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400965 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -0400966 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
967 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400968 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -0400969 }
Trond Myklebust864472e2006-01-03 09:55:15 +0100970 return 0;
971}
972
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973/*
974 * OPEN_RECLAIM:
975 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 */
Trond Myklebust539cd032007-06-05 11:46:42 -0400977static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -0400979 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +0100980 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500981 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 int status;
983
Trond Myklebust6f220ed2007-07-17 21:50:45 -0400984 opendata = nfs4_open_recoverdata_alloc(ctx, state);
985 if (IS_ERR(opendata))
986 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +0100987 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
988 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -0400989 rcu_read_lock();
990 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -0500991 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -0400992 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -0400993 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +0100994 opendata->o_arg.u.delegation_type = delegation_type;
995 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400996 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 return status;
998}
999
Trond Myklebust539cd032007-06-05 11:46:42 -04001000static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001{
1002 struct nfs_server *server = NFS_SERVER(state->inode);
1003 struct nfs4_exception exception = { };
1004 int err;
1005 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001006 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001007 if (err != -NFS4ERR_DELAY)
1008 break;
1009 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 } while (exception.retry);
1011 return err;
1012}
1013
Trond Myklebust864472e2006-01-03 09:55:15 +01001014static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1015{
1016 struct nfs_open_context *ctx;
1017 int ret;
1018
1019 ctx = nfs4_state_find_open_context(state);
1020 if (IS_ERR(ctx))
1021 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001022 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001023 put_nfs_open_context(ctx);
1024 return ret;
1025}
1026
Trond Myklebust13437e12007-07-06 15:10:43 -04001027static 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 -07001028{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001029 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001030 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001032 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1033 if (IS_ERR(opendata))
1034 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001035 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -04001036 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001037 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +01001038 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001039 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001040 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041}
1042
Trond Myklebust13437e12007-07-06 15:10:43 -04001043int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
1045 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001046 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 int err;
1048 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001049 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 switch (err) {
1051 case 0:
1052 return err;
1053 case -NFS4ERR_STALE_CLIENTID:
1054 case -NFS4ERR_STALE_STATEID:
1055 case -NFS4ERR_EXPIRED:
1056 /* Don't recall a delegation if it was lost */
David Howells7539bba2006-08-22 20:06:09 -04001057 nfs4_schedule_state_recovery(server->nfs_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 return err;
1059 }
1060 err = nfs4_handle_exception(server, err, &exception);
1061 } while (exception.retry);
1062 return err;
1063}
1064
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001065static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1066{
1067 struct nfs4_opendata *data = calldata;
1068
1069 data->rpc_status = task->tk_status;
1070 if (RPC_ASSASSINATED(task))
1071 return;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001072 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001073 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1074 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -05001075 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001076 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001077 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001078 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001079}
1080
1081static void nfs4_open_confirm_release(void *calldata)
1082{
1083 struct nfs4_opendata *data = calldata;
1084 struct nfs4_state *state = NULL;
1085
1086 /* If this request hasn't been cancelled, do nothing */
1087 if (data->cancelled == 0)
1088 goto out_free;
1089 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001090 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001091 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001092 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001093 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001094 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001095out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001096 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001097}
1098
1099static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001100 .rpc_call_done = nfs4_open_confirm_done,
1101 .rpc_release = nfs4_open_confirm_release,
1102};
1103
1104/*
1105 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1106 */
1107static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1108{
1109 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1110 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001111 struct rpc_message msg = {
1112 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1113 .rpc_argp = &data->c_arg,
1114 .rpc_resp = &data->c_res,
1115 .rpc_cred = data->owner->so_cred,
1116 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001117 struct rpc_task_setup task_setup_data = {
1118 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001119 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001120 .callback_ops = &nfs4_open_confirm_ops,
1121 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001122 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001123 .flags = RPC_TASK_ASYNC,
1124 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 int status;
1126
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001127 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001128 data->rpc_done = 0;
1129 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001130 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001131 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001132 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001133 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001134 status = nfs4_wait_for_completion_rpc_task(task);
1135 if (status != 0) {
1136 data->cancelled = 1;
1137 smp_wmb();
1138 } else
1139 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001140 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 return status;
1142}
1143
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001144static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001146 struct nfs4_opendata *data = calldata;
1147 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001148
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001149 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1150 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001151 /*
1152 * Check if we still need to send an OPEN call, or if we can use
1153 * a delegation instead.
1154 */
1155 if (data->state != NULL) {
1156 struct nfs_delegation *delegation;
1157
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001158 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001159 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001160 rcu_read_lock();
1161 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1162 if (delegation != NULL &&
Trond Myklebust15c831b2008-12-23 15:21:39 -05001163 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001164 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001165 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001166 }
1167 rcu_read_unlock();
1168 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001169 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001170 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001171 data->o_arg.clientid = sp->so_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001172 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001173 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001174 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1175 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001176 data->timestamp = jiffies;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001177 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001178 return;
1179out_no_action:
1180 task->tk_action = NULL;
1181
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001182}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001184static void nfs4_open_done(struct rpc_task *task, void *calldata)
1185{
1186 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001188 data->rpc_status = task->tk_status;
1189 if (RPC_ASSASSINATED(task))
1190 return;
1191 if (task->tk_status == 0) {
1192 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001193 case S_IFREG:
1194 break;
1195 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001196 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001197 break;
1198 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001199 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001200 break;
1201 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001202 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001203 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001204 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001205 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1206 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001207 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001208 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001209}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001210
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001211static void nfs4_open_release(void *calldata)
1212{
1213 struct nfs4_opendata *data = calldata;
1214 struct nfs4_state *state = NULL;
1215
1216 /* If this request hasn't been cancelled, do nothing */
1217 if (data->cancelled == 0)
1218 goto out_free;
1219 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001220 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001221 goto out_free;
1222 /* In case we need an open_confirm, no cleanup! */
1223 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1224 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001225 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001226 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001227 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001228out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001229 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001230}
1231
1232static const struct rpc_call_ops nfs4_open_ops = {
1233 .rpc_call_prepare = nfs4_open_prepare,
1234 .rpc_call_done = nfs4_open_done,
1235 .rpc_release = nfs4_open_release,
1236};
1237
1238/*
1239 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1240 */
1241static int _nfs4_proc_open(struct nfs4_opendata *data)
1242{
1243 struct inode *dir = data->dir->d_inode;
1244 struct nfs_server *server = NFS_SERVER(dir);
1245 struct nfs_openargs *o_arg = &data->o_arg;
1246 struct nfs_openres *o_res = &data->o_res;
1247 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001248 struct rpc_message msg = {
1249 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1250 .rpc_argp = o_arg,
1251 .rpc_resp = o_res,
1252 .rpc_cred = data->owner->so_cred,
1253 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001254 struct rpc_task_setup task_setup_data = {
1255 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001256 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001257 .callback_ops = &nfs4_open_ops,
1258 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001259 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001260 .flags = RPC_TASK_ASYNC,
1261 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001262 int status;
1263
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001264 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001265 data->rpc_done = 0;
1266 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001267 data->cancelled = 0;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001268 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001269 if (IS_ERR(task))
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001270 return PTR_ERR(task);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001271 status = nfs4_wait_for_completion_rpc_task(task);
1272 if (status != 0) {
1273 data->cancelled = 1;
1274 smp_wmb();
1275 } else
1276 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001277 rpc_put_task(task);
Trond Myklebust3e309912007-07-07 13:19:59 -04001278 if (status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001279 return status;
1280
Trond Myklebust99367812007-07-17 21:52:41 -04001281 if (o_res->fh.size == 0)
1282 _nfs4_proc_lookup(dir, o_arg->name, &o_res->fh, o_res->f_attr);
1283
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001284 if (o_arg->open_flags & O_CREAT) {
1285 update_changeattr(dir, &o_res->cinfo);
1286 nfs_post_op_update_inode(dir, o_res->dir_attr);
1287 } else
1288 nfs_refresh_inode(dir, o_res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001290 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001292 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 }
1294 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001295 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001296 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297}
1298
Trond Myklebust10afec92007-05-14 17:16:04 -04001299static int nfs4_recover_expired_lease(struct nfs_server *server)
Trond Myklebust58d97142006-01-03 09:55:24 +01001300{
David Howells7539bba2006-08-22 20:06:09 -04001301 struct nfs_client *clp = server->nfs_client;
Trond Myklebust6b309542006-09-14 14:03:14 -04001302 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001303
Trond Myklebust6b309542006-09-14 14:03:14 -04001304 for (;;) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001305 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001306 if (ret != 0)
1307 return ret;
Trond Myklebuste598d842008-12-23 15:21:42 -05001308 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1309 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001310 break;
Trond Myklebust58d97142006-01-03 09:55:24 +01001311 nfs4_schedule_state_recovery(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001312 }
1313 return 0;
Trond Myklebust58d97142006-01-03 09:55:24 +01001314}
1315
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316/*
1317 * OPEN_EXPIRED:
1318 * reclaim state on the server after a network partition.
1319 * Assumes caller holds the appropriate lock
1320 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001321static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001323 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001324 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001326 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1327 if (IS_ERR(opendata))
1328 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001329 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001330 if (ret == -ESTALE)
Trond Myklebust539cd032007-06-05 11:46:42 -04001331 d_drop(ctx->path.dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001332 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001333 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334}
1335
Trond Myklebust539cd032007-06-05 11:46:42 -04001336static inline int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001337{
Trond Myklebust539cd032007-06-05 11:46:42 -04001338 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001339 struct nfs4_exception exception = { };
1340 int err;
1341
1342 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001343 err = _nfs4_open_expired(ctx, state);
Trond Myklebust027b6ca2008-12-23 16:04:13 -05001344 if (err != -NFS4ERR_DELAY)
1345 break;
1346 nfs4_handle_exception(server, err, &exception);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001347 } while (exception.retry);
1348 return err;
1349}
1350
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1352{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001354 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
Trond Myklebust864472e2006-01-03 09:55:15 +01001356 ctx = nfs4_state_find_open_context(state);
1357 if (IS_ERR(ctx))
1358 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001359 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001360 put_nfs_open_context(ctx);
1361 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362}
1363
1364/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001365 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1366 * fields corresponding to attributes that were used to store the verifier.
1367 * Make sure we clobber those fields in the later setattr call
1368 */
1369static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1370{
1371 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1372 !(sattr->ia_valid & ATTR_ATIME_SET))
1373 sattr->ia_valid |= ATTR_ATIME;
1374
1375 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1376 !(sattr->ia_valid & ATTR_MTIME_SET))
1377 sattr->ia_valid |= ATTR_MTIME;
1378}
1379
1380/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001381 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001383static 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 -07001384{
1385 struct nfs4_state_owner *sp;
1386 struct nfs4_state *state = NULL;
1387 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001388 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001389 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
1391 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 status = -ENOMEM;
1393 if (!(sp = nfs4_get_state_owner(server, cred))) {
1394 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1395 goto out_err;
1396 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001397 status = nfs4_recover_expired_lease(server);
1398 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001399 goto err_put_state_owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001400 if (path->dentry->d_inode != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001401 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001402 status = -ENOMEM;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001403 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001404 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001405 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
Trond Myklebustaac00a82007-07-05 19:02:21 -04001407 if (path->dentry->d_inode != NULL)
1408 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1409
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001410 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001412 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001414 if (opendata->o_arg.open_flags & O_EXCL)
1415 nfs4_exclusive_attrset(opendata, sattr);
1416
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001417 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001418 status = PTR_ERR(state);
1419 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001420 goto err_opendata_put;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001421 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 *res = state;
1424 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001425err_opendata_put:
1426 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001427err_put_state_owner:
1428 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 *res = NULL;
1431 return status;
1432}
1433
1434
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001435static 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 -07001436{
1437 struct nfs4_exception exception = { };
1438 struct nfs4_state *res;
1439 int status;
1440
1441 do {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001442 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 if (status == 0)
1444 break;
1445 /* NOTE: BAD_SEQID means the server and client disagree about the
1446 * book-keeping w.r.t. state-changing operations
1447 * (OPEN/CLOSE/LOCK/LOCKU...)
1448 * It is actually a sign of a bug on the client or on the server.
1449 *
1450 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001451 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 * have unhashed the old state_owner for us, and that we can
1453 * therefore safely retry using a new one. We should still warn
1454 * the user though...
1455 */
1456 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001457 printk(KERN_WARNING "NFS: v4 server %s "
1458 " returned a bad sequence-id error!\n",
1459 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 exception.retry = 1;
1461 continue;
1462 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001463 /*
1464 * BAD_STATEID on OPEN means that the server cancelled our
1465 * state before it received the OPEN_CONFIRM.
1466 * Recover by retrying the request as per the discussion
1467 * on Page 181 of RFC3530.
1468 */
1469 if (status == -NFS4ERR_BAD_STATEID) {
1470 exception.retry = 1;
1471 continue;
1472 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001473 if (status == -EAGAIN) {
1474 /* We must have found a delegation */
1475 exception.retry = 1;
1476 continue;
1477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1479 status, &exception));
1480 } while (exception.retry);
1481 return res;
1482}
1483
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001484static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1485 struct nfs_fattr *fattr, struct iattr *sattr,
1486 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001488 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001490 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 .iap = sattr,
1492 .server = server,
1493 .bitmask = server->attr_bitmask,
1494 };
1495 struct nfs_setattrres res = {
1496 .fattr = fattr,
1497 .server = server,
1498 };
1499 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001500 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1501 .rpc_argp = &arg,
1502 .rpc_resp = &res,
1503 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001505 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001506 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Trond Myklebust0e574af2005-10-27 22:12:38 -04001508 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001510 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1511 /* Use that stateid */
1512 } else if (state != NULL) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001513 nfs4_copy_stateid(&arg.stateid, state, current->files);
1514 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1516
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001517 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001518 if (status == 0 && state != NULL)
1519 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001520 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521}
1522
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001523static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1524 struct nfs_fattr *fattr, struct iattr *sattr,
1525 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001527 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 struct nfs4_exception exception = { };
1529 int err;
1530 do {
1531 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001532 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 &exception);
1534 } while (exception.retry);
1535 return err;
1536}
1537
1538struct nfs4_closedata {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001539 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 struct inode *inode;
1541 struct nfs4_state *state;
1542 struct nfs_closeargs arg;
1543 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001544 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001545 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546};
1547
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001548static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001549{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001550 struct nfs4_closedata *calldata = data;
1551 struct nfs4_state_owner *sp = calldata->state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -07001552
1553 nfs4_put_open_state(calldata->state);
1554 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001555 nfs4_put_state_owner(sp);
Jan Blunck31f31db2008-05-02 13:42:45 -07001556 path_put(&calldata->path);
Trond Myklebust95121352005-10-18 14:20:12 -07001557 kfree(calldata);
1558}
1559
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001560static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001562 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 struct nfs_server *server = NFS_SERVER(calldata->inode);
1565
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001566 if (RPC_ASSASSINATED(task))
1567 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 /* hmm. we are done with the inode, and in the process of freeing
1569 * the state_owner. we keep this around to process errors
1570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 switch (task->tk_status) {
1572 case 0:
Trond Myklebust45328c32007-07-26 17:47:34 -04001573 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001574 renew_lease(server, calldata->timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 break;
1576 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001577 case -NFS4ERR_OLD_STATEID:
1578 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001580 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001581 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001583 if (nfs4_async_handle_error(task, server, state) == -EAGAIN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 rpc_restart_call(task);
1585 return;
1586 }
1587 }
Trond Myklebust516a6af2005-10-27 22:12:41 -04001588 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589}
1590
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001591static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001593 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001594 struct nfs4_state *state = calldata->state;
Trond Myklebust003707c2007-07-05 18:07:55 -04001595 int clear_rd, clear_wr, clear_rdwr;
Trond Myklebust95121352005-10-18 14:20:12 -07001596
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001597 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001598 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001599
Trond Myklebust003707c2007-07-05 18:07:55 -04001600 clear_rd = clear_wr = clear_rdwr = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001601 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001602 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001603 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001604 if (state->n_rdonly == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001605 clear_rd |= test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1606 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1607 }
1608 if (state->n_wronly == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001609 clear_wr |= test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1610 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1611 }
Trond Myklebuste7616922006-01-03 09:55:13 +01001612 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001613 spin_unlock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001614 if (!clear_rd && !clear_wr && !clear_rdwr) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001615 /* Note: exit _without_ calling nfs4_close_done */
1616 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001617 return;
1618 }
Trond Myklebust516a6af2005-10-27 22:12:41 -04001619 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust45328c32007-07-26 17:47:34 -04001620 if (test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001621 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001622 calldata->arg.fmode = FMODE_READ;
Trond Myklebust45328c32007-07-26 17:47:34 -04001623 } else if (test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001624 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001625 calldata->arg.fmode = FMODE_WRITE;
Trond Myklebust45328c32007-07-26 17:47:34 -04001626 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001627 calldata->timestamp = jiffies;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001628 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629}
1630
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001631static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001632 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001633 .rpc_call_done = nfs4_close_done,
1634 .rpc_release = nfs4_free_closedata,
1635};
1636
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637/*
1638 * It is possible for data to be read/written from a mem-mapped file
1639 * after the sys_close call (which hits the vfs layer as a flush).
1640 * This means that we can't safely call nfsv4 close on a file until
1641 * the inode is cleared. This in turn means that we are not good
1642 * NFSv4 citizens - we do not indicate to the server to update the file's
1643 * share state even when we are done with one of the three share
1644 * stateid's in the inode.
1645 *
1646 * NOTE: Caller must be holding the sp->so_owner semaphore!
1647 */
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001648int nfs4_do_close(struct path *path, struct nfs4_state *state, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001650 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04001652 struct nfs4_state_owner *sp = state->owner;
1653 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001654 struct rpc_message msg = {
1655 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1656 .rpc_cred = state->owner->so_cred,
1657 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001658 struct rpc_task_setup task_setup_data = {
1659 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001660 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001661 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05001662 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001663 .flags = RPC_TASK_ASYNC,
1664 };
Trond Myklebust95121352005-10-18 14:20:12 -07001665 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
Trond Myklebust95121352005-10-18 14:20:12 -07001667 calldata = kmalloc(sizeof(*calldata), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07001669 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001670 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001672 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04001673 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 /* Serialization for the sequence id */
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001675 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001676 if (calldata->arg.seqid == NULL)
1677 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001678 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04001679 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001680 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04001681 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001682 calldata->res.server = server;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04001683 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001684 calldata->path.mnt = mntget(path->mnt);
1685 calldata->path.dentry = dget(path->dentry);
Trond Myklebust95121352005-10-18 14:20:12 -07001686
Trond Myklebust5138fde2007-07-14 15:40:01 -04001687 msg.rpc_argp = &calldata->arg,
1688 msg.rpc_resp = &calldata->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001689 task_setup_data.callback_data = calldata;
1690 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001691 if (IS_ERR(task))
1692 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001693 status = 0;
1694 if (wait)
1695 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001696 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001697 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07001698out_free_calldata:
1699 kfree(calldata);
1700out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04001701 nfs4_put_open_state(state);
1702 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07001703 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704}
1705
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001706static 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 -07001707{
1708 struct file *filp;
Trond Myklebust1b45c462007-07-03 13:04:56 -04001709 int ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001710
Trond Myklebust1b45c462007-07-03 13:04:56 -04001711 /* If the open_intent is for execute, we have an extra check to make */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001712 if (fmode & FMODE_EXEC) {
Trond Myklebustaf22f942007-08-10 17:45:10 -04001713 ret = nfs_may_open(state->inode,
Trond Myklebust1b45c462007-07-03 13:04:56 -04001714 state->owner->so_cred,
1715 nd->intent.open.flags);
1716 if (ret < 0)
1717 goto out_close;
1718 }
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001719 filp = lookup_instantiate_filp(nd, path->dentry, NULL);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001720 if (!IS_ERR(filp)) {
1721 struct nfs_open_context *ctx;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04001722 ctx = nfs_file_open_context(filp);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001723 ctx->state = state;
Trond Myklebust95cf9592006-04-18 13:14:06 -04001724 return 0;
1725 }
Trond Myklebust1b45c462007-07-03 13:04:56 -04001726 ret = PTR_ERR(filp);
1727out_close:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001728 nfs4_close_sync(path, state, fmode & (FMODE_READ|FMODE_WRITE));
Trond Myklebust1b45c462007-07-03 13:04:56 -04001729 return ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001730}
1731
1732struct dentry *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1734{
Trond Myklebustad389da2007-06-05 12:30:00 -04001735 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08001736 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001737 .dentry = dentry,
1738 };
Jan Blunck4ac91372008-02-14 19:34:32 -08001739 struct dentry *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 struct iattr attr;
1741 struct rpc_cred *cred;
1742 struct nfs4_state *state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001743 struct dentry *res;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001744 fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
1746 if (nd->flags & LOOKUP_CREATE) {
1747 attr.ia_mode = nd->intent.open.create_mode;
1748 attr.ia_valid = ATTR_MODE;
1749 if (!IS_POSIXACL(dir))
Al Viroce3b0f82009-03-29 19:08:22 -04001750 attr.ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 } else {
1752 attr.ia_valid = 0;
1753 BUG_ON(nd->intent.open.flags & O_CREAT);
1754 }
1755
Trond Myklebust98a8e322008-03-12 12:25:28 -04001756 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 if (IS_ERR(cred))
Trond Myklebust02a913a2005-10-18 14:20:17 -07001758 return (struct dentry *)cred;
Trond Myklebust565277f2007-10-15 18:17:53 -04001759 parent = dentry->d_parent;
1760 /* Protect against concurrent sillydeletes */
1761 nfs_block_sillyrename(parent);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001762 state = nfs4_do_open(dir, &path, fmode, nd->intent.open.flags, &attr, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001764 if (IS_ERR(state)) {
Trond Myklebustd75340c2007-10-01 21:42:01 -04001765 if (PTR_ERR(state) == -ENOENT) {
Trond Myklebust02a913a2005-10-18 14:20:17 -07001766 d_add(dentry, NULL);
Trond Myklebustd75340c2007-10-01 21:42:01 -04001767 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1768 }
Trond Myklebust565277f2007-10-15 18:17:53 -04001769 nfs_unblock_sillyrename(parent);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001770 return (struct dentry *)state;
1771 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001772 res = d_add_unique(dentry, igrab(state->inode));
Trond Myklebust02a913a2005-10-18 14:20:17 -07001773 if (res != NULL)
Trond Myklebustdeee9362007-08-27 11:33:00 -04001774 path.dentry = res;
Trond Myklebustd75340c2007-10-01 21:42:01 -04001775 nfs_set_verifier(path.dentry, nfs_save_change_attribute(dir));
Trond Myklebust565277f2007-10-15 18:17:53 -04001776 nfs_unblock_sillyrename(parent);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001777 nfs4_intent_set_file(nd, &path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001778 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779}
1780
1781int
Trond Myklebust02a913a2005-10-18 14:20:17 -07001782nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783{
Trond Myklebustad389da2007-06-05 12:30:00 -04001784 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08001785 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001786 .dentry = dentry,
1787 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 struct rpc_cred *cred;
1789 struct nfs4_state *state;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001790 fmode_t fmode = openflags & (FMODE_READ | FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
Trond Myklebust98a8e322008-03-12 12:25:28 -04001792 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 if (IS_ERR(cred))
1794 return PTR_ERR(cred);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001795 state = nfs4_do_open(dir, &path, fmode, openflags, NULL, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001797 if (IS_ERR(state)) {
1798 switch (PTR_ERR(state)) {
1799 case -EPERM:
1800 case -EACCES:
1801 case -EDQUOT:
1802 case -ENOSPC:
1803 case -EROFS:
1804 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
1805 return 1;
Chuck Leverb87c0ad2006-10-19 23:28:42 -07001806 default:
1807 goto out_drop;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001808 }
Trond Myklebust02a913a2005-10-18 14:20:17 -07001809 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001810 if (state->inode == dentry->d_inode) {
Trond Myklebustd75340c2007-10-01 21:42:01 -04001811 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001812 nfs4_intent_set_file(nd, &path, state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 return 1;
1814 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001815 nfs4_close_sync(&path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001816out_drop:
1817 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 return 0;
1819}
1820
Trond Myklebust7fe5c392009-03-19 15:35:50 -04001821void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
1822{
1823 if (ctx->state == NULL)
1824 return;
1825 if (is_sync)
1826 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
1827 else
1828 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
1829}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
1831static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1832{
Benny Halevy43652ad2009-04-01 09:21:54 -04001833 struct nfs4_server_caps_arg args = {
1834 .fhandle = fhandle,
1835 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 struct nfs4_server_caps_res res = {};
1837 struct rpc_message msg = {
1838 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04001839 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 .rpc_resp = &res,
1841 };
1842 int status;
1843
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001844 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 if (status == 0) {
1846 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
1847 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
1848 server->caps |= NFS_CAP_ACLS;
1849 if (res.has_links != 0)
1850 server->caps |= NFS_CAP_HARDLINKS;
1851 if (res.has_symlinks != 0)
1852 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebusta65318b2009-03-11 14:10:28 -04001853 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
1854 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
1855 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 server->acl_bitmask = res.acl_bitmask;
1857 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001858
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 return status;
1860}
1861
Trond Myklebust55a97592006-06-09 09:34:19 -04001862int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863{
1864 struct nfs4_exception exception = { };
1865 int err;
1866 do {
1867 err = nfs4_handle_exception(server,
1868 _nfs4_server_capabilities(server, fhandle),
1869 &exception);
1870 } while (exception.retry);
1871 return err;
1872}
1873
1874static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1875 struct nfs_fsinfo *info)
1876{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 struct nfs4_lookup_root_arg args = {
1878 .bitmask = nfs4_fattr_bitmap,
1879 };
1880 struct nfs4_lookup_res res = {
1881 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04001882 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 .fh = fhandle,
1884 };
1885 struct rpc_message msg = {
1886 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
1887 .rpc_argp = &args,
1888 .rpc_resp = &res,
1889 };
Trond Myklebust0e574af2005-10-27 22:12:38 -04001890 nfs_fattr_init(info->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001891 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892}
1893
1894static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1895 struct nfs_fsinfo *info)
1896{
1897 struct nfs4_exception exception = { };
1898 int err;
1899 do {
1900 err = nfs4_handle_exception(server,
1901 _nfs4_lookup_root(server, fhandle, info),
1902 &exception);
1903 } while (exception.retry);
1904 return err;
1905}
1906
David Howells54ceac42006-08-22 20:06:13 -04001907/*
1908 * get the file handle for the "/" directory on the server
1909 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04001911 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 int status;
1914
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 status = nfs4_lookup_root(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 if (status == 0)
1917 status = nfs4_server_capabilities(server, fhandle);
1918 if (status == 0)
1919 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08001920 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921}
1922
Manoj Naik6b97fd32006-06-09 09:34:29 -04001923/*
1924 * Get locations and (maybe) other attributes of a referral.
1925 * Note that we'll actually follow the referral later when
1926 * we detect fsid mismatch in inode revalidation
1927 */
Trond Myklebust56659e92007-07-17 21:52:39 -04001928static 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 -04001929{
1930 int status = -ENOMEM;
1931 struct page *page = NULL;
1932 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04001933
1934 page = alloc_page(GFP_KERNEL);
1935 if (page == NULL)
1936 goto out;
1937 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
1938 if (locations == NULL)
1939 goto out;
1940
Trond Myklebustc228fd32007-01-13 02:28:11 -05001941 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04001942 if (status != 0)
1943 goto out;
1944 /* Make sure server returned a different fsid for the referral */
1945 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -07001946 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 -04001947 status = -EIO;
1948 goto out;
1949 }
1950
1951 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
1952 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
1953 if (!fattr->mode)
1954 fattr->mode = S_IFDIR;
1955 memset(fhandle, 0, sizeof(struct nfs_fh));
1956out:
1957 if (page)
1958 __free_page(page);
1959 if (locations)
1960 kfree(locations);
1961 return status;
1962}
1963
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1965{
1966 struct nfs4_getattr_arg args = {
1967 .fh = fhandle,
1968 .bitmask = server->attr_bitmask,
1969 };
1970 struct nfs4_getattr_res res = {
1971 .fattr = fattr,
1972 .server = server,
1973 };
1974 struct rpc_message msg = {
1975 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
1976 .rpc_argp = &args,
1977 .rpc_resp = &res,
1978 };
1979
Trond Myklebust0e574af2005-10-27 22:12:38 -04001980 nfs_fattr_init(fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001981 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982}
1983
1984static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1985{
1986 struct nfs4_exception exception = { };
1987 int err;
1988 do {
1989 err = nfs4_handle_exception(server,
1990 _nfs4_proc_getattr(server, fhandle, fattr),
1991 &exception);
1992 } while (exception.retry);
1993 return err;
1994}
1995
1996/*
1997 * The file is not closed if it is opened due to the a request to change
1998 * the size of the file. The open call will not be needed once the
1999 * VFS layer lookup-intents are implemented.
2000 *
2001 * Close is called when the inode is destroyed.
2002 * If we haven't opened the file for O_WRONLY, we
2003 * need to in the size_change case to obtain a stateid.
2004 *
2005 * Got race?
2006 * Because OPEN is always done by name in nfsv4, it is
2007 * possible that we opened a different file by the same
2008 * name. We can recognize this race condition, but we
2009 * can't do anything about it besides returning an error.
2010 *
2011 * This will be fixed with VFS changes (lookup-intent).
2012 */
2013static int
2014nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2015 struct iattr *sattr)
2016{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002017 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002018 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002019 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 int status;
2021
Trond Myklebust0e574af2005-10-27 22:12:38 -04002022 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023
Trond Myklebustd5308382005-11-04 15:33:38 -05002024 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002025 if (sattr->ia_valid & ATTR_FILE) {
2026 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002027
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002028 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002029 if (ctx) {
2030 cred = ctx->cred;
2031 state = ctx->state;
2032 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002033 }
2034
2035 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002036 if (status == 0)
2037 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 return status;
2039}
2040
Trond Myklebust56659e92007-07-17 21:52:39 -04002041static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
2042 const struct qstr *name, struct nfs_fh *fhandle,
David Howells2b3de442006-08-22 20:06:09 -04002043 struct nfs_fattr *fattr)
2044{
2045 int status;
2046 struct nfs4_lookup_arg args = {
2047 .bitmask = server->attr_bitmask,
2048 .dir_fh = dirfh,
2049 .name = name,
2050 };
2051 struct nfs4_lookup_res res = {
2052 .server = server,
2053 .fattr = fattr,
2054 .fh = fhandle,
2055 };
2056 struct rpc_message msg = {
2057 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2058 .rpc_argp = &args,
2059 .rpc_resp = &res,
2060 };
2061
2062 nfs_fattr_init(fattr);
2063
2064 dprintk("NFS call lookupfh %s\n", name->name);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002065 status = nfs4_call_sync(server, &msg, &args, &res, 0);
David Howells2b3de442006-08-22 20:06:09 -04002066 dprintk("NFS reply lookupfh: %d\n", status);
David Howells2b3de442006-08-22 20:06:09 -04002067 return status;
2068}
2069
2070static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2071 struct qstr *name, struct nfs_fh *fhandle,
2072 struct nfs_fattr *fattr)
2073{
2074 struct nfs4_exception exception = { };
2075 int err;
2076 do {
Trond Myklebust4e56e082007-07-01 18:13:52 -04002077 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2078 /* FIXME: !!!! */
2079 if (err == -NFS4ERR_MOVED) {
2080 err = -EREMOTE;
2081 break;
2082 }
2083 err = nfs4_handle_exception(server, err, &exception);
David Howells2b3de442006-08-22 20:06:09 -04002084 } while (exception.retry);
2085 return err;
2086}
2087
Trond Myklebust56659e92007-07-17 21:52:39 -04002088static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2090{
Trond Myklebust4e56e082007-07-01 18:13:52 -04002091 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
2093 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust4e56e082007-07-01 18:13:52 -04002094 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002095 if (status == -NFS4ERR_MOVED)
2096 status = nfs4_get_referral(dir, name, fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 dprintk("NFS reply lookup: %d\n", status);
2098 return status;
2099}
2100
2101static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2102{
2103 struct nfs4_exception exception = { };
2104 int err;
2105 do {
2106 err = nfs4_handle_exception(NFS_SERVER(dir),
2107 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2108 &exception);
2109 } while (exception.retry);
2110 return err;
2111}
2112
2113static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2114{
Trond Myklebust76b32992007-08-10 17:45:11 -04002115 struct nfs_server *server = NFS_SERVER(inode);
2116 struct nfs_fattr fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 struct nfs4_accessargs args = {
2118 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04002119 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002121 struct nfs4_accessres res = {
2122 .server = server,
2123 .fattr = &fattr,
2124 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 struct rpc_message msg = {
2126 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2127 .rpc_argp = &args,
2128 .rpc_resp = &res,
2129 .rpc_cred = entry->cred,
2130 };
2131 int mode = entry->mask;
2132 int status;
2133
2134 /*
2135 * Determine which access bits we want to ask for...
2136 */
2137 if (mode & MAY_READ)
2138 args.access |= NFS4_ACCESS_READ;
2139 if (S_ISDIR(inode->i_mode)) {
2140 if (mode & MAY_WRITE)
2141 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2142 if (mode & MAY_EXEC)
2143 args.access |= NFS4_ACCESS_LOOKUP;
2144 } else {
2145 if (mode & MAY_WRITE)
2146 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2147 if (mode & MAY_EXEC)
2148 args.access |= NFS4_ACCESS_EXECUTE;
2149 }
Trond Myklebust76b32992007-08-10 17:45:11 -04002150 nfs_fattr_init(&fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002151 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 if (!status) {
2153 entry->mask = 0;
2154 if (res.access & NFS4_ACCESS_READ)
2155 entry->mask |= MAY_READ;
2156 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2157 entry->mask |= MAY_WRITE;
2158 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2159 entry->mask |= MAY_EXEC;
Trond Myklebust76b32992007-08-10 17:45:11 -04002160 nfs_refresh_inode(inode, &fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 }
2162 return status;
2163}
2164
2165static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2166{
2167 struct nfs4_exception exception = { };
2168 int err;
2169 do {
2170 err = nfs4_handle_exception(NFS_SERVER(inode),
2171 _nfs4_proc_access(inode, entry),
2172 &exception);
2173 } while (exception.retry);
2174 return err;
2175}
2176
2177/*
2178 * TODO: For the time being, we don't try to get any attributes
2179 * along with any of the zero-copy operations READ, READDIR,
2180 * READLINK, WRITE.
2181 *
2182 * In the case of the first three, we want to put the GETATTR
2183 * after the read-type operation -- this is because it is hard
2184 * to predict the length of a GETATTR response in v4, and thus
2185 * align the READ data correctly. This means that the GETATTR
2186 * may end up partially falling into the page cache, and we should
2187 * shift it into the 'tail' of the xdr_buf before processing.
2188 * To do this efficiently, we need to know the total length
2189 * of data received, which doesn't seem to be available outside
2190 * of the RPC layer.
2191 *
2192 * In the case of WRITE, we also want to put the GETATTR after
2193 * the operation -- in this case because we want to make sure
2194 * we get the post-operation mtime and size. This means that
2195 * we can't use xdr_encode_pages() as written: we need a variant
2196 * of it which would leave room in the 'tail' iovec.
2197 *
2198 * Both of these changes to the XDR layer would in fact be quite
2199 * minor, but I decided to leave them for a subsequent patch.
2200 */
2201static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2202 unsigned int pgbase, unsigned int pglen)
2203{
2204 struct nfs4_readlink args = {
2205 .fh = NFS_FH(inode),
2206 .pgbase = pgbase,
2207 .pglen = pglen,
2208 .pages = &page,
2209 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002210 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 struct rpc_message msg = {
2212 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2213 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002214 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 };
2216
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002217 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218}
2219
2220static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2221 unsigned int pgbase, unsigned int pglen)
2222{
2223 struct nfs4_exception exception = { };
2224 int err;
2225 do {
2226 err = nfs4_handle_exception(NFS_SERVER(inode),
2227 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2228 &exception);
2229 } while (exception.retry);
2230 return err;
2231}
2232
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233/*
2234 * Got race?
2235 * We will need to arrange for the VFS layer to provide an atomic open.
2236 * Until then, this create/open method is prone to inefficiency and race
2237 * conditions due to the lookup, create, and open VFS calls from sys_open()
2238 * placed on the wire.
2239 *
2240 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2241 * The file will be opened again in the subsequent VFS open call
2242 * (nfs4_proc_file_open).
2243 *
2244 * The open for read will just hang around to be used by any process that
2245 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2246 */
2247
2248static int
2249nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebust02a913a2005-10-18 14:20:17 -07002250 int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251{
Trond Myklebustad389da2007-06-05 12:30:00 -04002252 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08002253 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04002254 .dentry = dentry,
2255 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 struct nfs4_state *state;
2257 struct rpc_cred *cred;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002258 fmode_t fmode = flags & (FMODE_READ | FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 int status = 0;
2260
Trond Myklebust98a8e322008-03-12 12:25:28 -04002261 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 if (IS_ERR(cred)) {
2263 status = PTR_ERR(cred);
2264 goto out;
2265 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002266 state = nfs4_do_open(dir, &path, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002267 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 if (IS_ERR(state)) {
2269 status = PTR_ERR(state);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002270 goto out_putcred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002272 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002273 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 if (flags & O_EXCL) {
2275 struct nfs_fattr fattr;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002276 status = nfs4_do_setattr(state->inode, cred, &fattr, sattr, state);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002277 if (status == 0)
Trond Myklebust65e43082005-08-16 11:49:44 -04002278 nfs_setattr_update_inode(state->inode, sattr);
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002279 nfs_post_op_update_inode(state->inode, &fattr);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002280 }
Trond Myklebustad389da2007-06-05 12:30:00 -04002281 if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002282 status = nfs4_intent_set_file(nd, &path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002283 else
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002284 nfs4_close_sync(&path, state, fmode);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002285out_putcred:
2286 put_rpccred(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287out:
2288 return status;
2289}
2290
2291static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2292{
Trond Myklebust16e42952005-10-27 22:12:44 -04002293 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002294 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002296 .name.len = name->len,
2297 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002298 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002300 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002301 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002302 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002304 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2305 .rpc_argp = &args,
2306 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 };
2308 int status;
2309
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002310 nfs_fattr_init(&res.dir_attr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002311 status = nfs4_call_sync(server, &msg, &args, &res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002312 if (status == 0) {
2313 update_changeattr(dir, &res.cinfo);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002314 nfs_post_op_update_inode(dir, &res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 return status;
2317}
2318
2319static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2320{
2321 struct nfs4_exception exception = { };
2322 int err;
2323 do {
2324 err = nfs4_handle_exception(NFS_SERVER(dir),
2325 _nfs4_proc_remove(dir, name),
2326 &exception);
2327 } while (exception.retry);
2328 return err;
2329}
2330
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002331static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002333 struct nfs_server *server = NFS_SERVER(dir);
2334 struct nfs_removeargs *args = msg->rpc_argp;
2335 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336
Trond Myklebusta65318b2009-03-11 14:10:28 -04002337 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002338 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340}
2341
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002342static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002344 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2345
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002346 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002347 return 0;
2348 update_changeattr(dir, &res->cinfo);
2349 nfs_post_op_update_inode(dir, &res->dir_attr);
2350 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351}
2352
2353static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2354 struct inode *new_dir, struct qstr *new_name)
2355{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002356 struct nfs_server *server = NFS_SERVER(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 struct nfs4_rename_arg arg = {
2358 .old_dir = NFS_FH(old_dir),
2359 .new_dir = NFS_FH(new_dir),
2360 .old_name = old_name,
2361 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002362 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 };
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002364 struct nfs_fattr old_fattr, new_fattr;
2365 struct nfs4_rename_res res = {
2366 .server = server,
2367 .old_fattr = &old_fattr,
2368 .new_fattr = &new_fattr,
2369 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 struct rpc_message msg = {
2371 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2372 .rpc_argp = &arg,
2373 .rpc_resp = &res,
2374 };
2375 int status;
2376
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002377 nfs_fattr_init(res.old_fattr);
2378 nfs_fattr_init(res.new_fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002379 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380
2381 if (!status) {
2382 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002383 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002385 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 }
2387 return status;
2388}
2389
2390static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2391 struct inode *new_dir, struct qstr *new_name)
2392{
2393 struct nfs4_exception exception = { };
2394 int err;
2395 do {
2396 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2397 _nfs4_proc_rename(old_dir, old_name,
2398 new_dir, new_name),
2399 &exception);
2400 } while (exception.retry);
2401 return err;
2402}
2403
2404static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2405{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002406 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 struct nfs4_link_arg arg = {
2408 .fh = NFS_FH(inode),
2409 .dir_fh = NFS_FH(dir),
2410 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002411 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002413 struct nfs_fattr fattr, dir_attr;
2414 struct nfs4_link_res res = {
2415 .server = server,
2416 .fattr = &fattr,
2417 .dir_attr = &dir_attr,
2418 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 struct rpc_message msg = {
2420 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2421 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002422 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 };
2424 int status;
2425
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002426 nfs_fattr_init(res.fattr);
2427 nfs_fattr_init(res.dir_attr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002428 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002429 if (!status) {
2430 update_changeattr(dir, &res.cinfo);
2431 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002432 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434
2435 return status;
2436}
2437
2438static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2439{
2440 struct nfs4_exception exception = { };
2441 int err;
2442 do {
2443 err = nfs4_handle_exception(NFS_SERVER(inode),
2444 _nfs4_proc_link(inode, dir, name),
2445 &exception);
2446 } while (exception.retry);
2447 return err;
2448}
2449
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002450struct nfs4_createdata {
2451 struct rpc_message msg;
2452 struct nfs4_create_arg arg;
2453 struct nfs4_create_res res;
2454 struct nfs_fh fh;
2455 struct nfs_fattr fattr;
2456 struct nfs_fattr dir_fattr;
2457};
2458
2459static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2460 struct qstr *name, struct iattr *sattr, u32 ftype)
2461{
2462 struct nfs4_createdata *data;
2463
2464 data = kzalloc(sizeof(*data), GFP_KERNEL);
2465 if (data != NULL) {
2466 struct nfs_server *server = NFS_SERVER(dir);
2467
2468 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2469 data->msg.rpc_argp = &data->arg;
2470 data->msg.rpc_resp = &data->res;
2471 data->arg.dir_fh = NFS_FH(dir);
2472 data->arg.server = server;
2473 data->arg.name = name;
2474 data->arg.attrs = sattr;
2475 data->arg.ftype = ftype;
2476 data->arg.bitmask = server->attr_bitmask;
2477 data->res.server = server;
2478 data->res.fh = &data->fh;
2479 data->res.fattr = &data->fattr;
2480 data->res.dir_fattr = &data->dir_fattr;
2481 nfs_fattr_init(data->res.fattr);
2482 nfs_fattr_init(data->res.dir_fattr);
2483 }
2484 return data;
2485}
2486
2487static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2488{
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002489 int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2490 &data->arg, &data->res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002491 if (status == 0) {
2492 update_changeattr(dir, &data->res.dir_cinfo);
2493 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2494 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2495 }
2496 return status;
2497}
2498
2499static void nfs4_free_createdata(struct nfs4_createdata *data)
2500{
2501 kfree(data);
2502}
2503
Chuck Lever4f390c12006-08-22 20:06:22 -04002504static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002505 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002507 struct nfs4_createdata *data;
2508 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509
Chuck Lever94a6d752006-08-22 20:06:23 -04002510 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002511 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002512
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002513 status = -ENOMEM;
2514 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2515 if (data == NULL)
2516 goto out;
2517
2518 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2519 data->arg.u.symlink.pages = &page;
2520 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002522 status = nfs4_do_create(dir, dentry, data);
2523
2524 nfs4_free_createdata(data);
2525out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 return status;
2527}
2528
Chuck Lever4f390c12006-08-22 20:06:22 -04002529static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002530 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531{
2532 struct nfs4_exception exception = { };
2533 int err;
2534 do {
2535 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002536 _nfs4_proc_symlink(dir, dentry, page,
2537 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 &exception);
2539 } while (exception.retry);
2540 return err;
2541}
2542
2543static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2544 struct iattr *sattr)
2545{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002546 struct nfs4_createdata *data;
2547 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002549 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2550 if (data == NULL)
2551 goto out;
2552
2553 status = nfs4_do_create(dir, dentry, data);
2554
2555 nfs4_free_createdata(data);
2556out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 return status;
2558}
2559
2560static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2561 struct iattr *sattr)
2562{
2563 struct nfs4_exception exception = { };
2564 int err;
2565 do {
2566 err = nfs4_handle_exception(NFS_SERVER(dir),
2567 _nfs4_proc_mkdir(dir, dentry, sattr),
2568 &exception);
2569 } while (exception.retry);
2570 return err;
2571}
2572
2573static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2574 u64 cookie, struct page *page, unsigned int count, int plus)
2575{
2576 struct inode *dir = dentry->d_inode;
2577 struct nfs4_readdir_arg args = {
2578 .fh = NFS_FH(dir),
2579 .pages = &page,
2580 .pgbase = 0,
2581 .count = count,
Trond Myklebusta65318b2009-03-11 14:10:28 -04002582 .bitmask = NFS_SERVER(dentry->d_inode)->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 };
2584 struct nfs4_readdir_res res;
2585 struct rpc_message msg = {
2586 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2587 .rpc_argp = &args,
2588 .rpc_resp = &res,
2589 .rpc_cred = cred,
2590 };
2591 int status;
2592
Harvey Harrison3110ff82008-05-02 13:42:44 -07002593 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00002594 dentry->d_parent->d_name.name,
2595 dentry->d_name.name,
2596 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2598 res.pgbase = args.pgbase;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002599 status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 if (status == 0)
2601 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustc4812992007-09-28 17:11:45 -04002602
2603 nfs_invalidate_atime(dir);
2604
Harvey Harrison3110ff82008-05-02 13:42:44 -07002605 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 return status;
2607}
2608
2609static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2610 u64 cookie, struct page *page, unsigned int count, int plus)
2611{
2612 struct nfs4_exception exception = { };
2613 int err;
2614 do {
2615 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2616 _nfs4_proc_readdir(dentry, cred, cookie,
2617 page, count, plus),
2618 &exception);
2619 } while (exception.retry);
2620 return err;
2621}
2622
2623static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2624 struct iattr *sattr, dev_t rdev)
2625{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002626 struct nfs4_createdata *data;
2627 int mode = sattr->ia_mode;
2628 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629
2630 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2631 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002632
2633 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2634 if (data == NULL)
2635 goto out;
2636
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002638 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002640 data->arg.ftype = NF4BLK;
2641 data->arg.u.device.specdata1 = MAJOR(rdev);
2642 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 }
2644 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002645 data->arg.ftype = NF4CHR;
2646 data->arg.u.device.specdata1 = MAJOR(rdev);
2647 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002650 status = nfs4_do_create(dir, dentry, data);
2651
2652 nfs4_free_createdata(data);
2653out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 return status;
2655}
2656
2657static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2658 struct iattr *sattr, dev_t rdev)
2659{
2660 struct nfs4_exception exception = { };
2661 int err;
2662 do {
2663 err = nfs4_handle_exception(NFS_SERVER(dir),
2664 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2665 &exception);
2666 } while (exception.retry);
2667 return err;
2668}
2669
2670static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2671 struct nfs_fsstat *fsstat)
2672{
2673 struct nfs4_statfs_arg args = {
2674 .fh = fhandle,
2675 .bitmask = server->attr_bitmask,
2676 };
Benny Halevy24ad1482009-04-01 09:21:56 -04002677 struct nfs4_statfs_res res = {
2678 .fsstat = fsstat,
2679 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 struct rpc_message msg = {
2681 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2682 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04002683 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 };
2685
Trond Myklebust0e574af2005-10-27 22:12:38 -04002686 nfs_fattr_init(fsstat->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002687 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688}
2689
2690static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2691{
2692 struct nfs4_exception exception = { };
2693 int err;
2694 do {
2695 err = nfs4_handle_exception(server,
2696 _nfs4_proc_statfs(server, fhandle, fsstat),
2697 &exception);
2698 } while (exception.retry);
2699 return err;
2700}
2701
2702static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2703 struct nfs_fsinfo *fsinfo)
2704{
2705 struct nfs4_fsinfo_arg args = {
2706 .fh = fhandle,
2707 .bitmask = server->attr_bitmask,
2708 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04002709 struct nfs4_fsinfo_res res = {
2710 .fsinfo = fsinfo,
2711 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 struct rpc_message msg = {
2713 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2714 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04002715 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 };
2717
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002718 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719}
2720
2721static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2722{
2723 struct nfs4_exception exception = { };
2724 int err;
2725
2726 do {
2727 err = nfs4_handle_exception(server,
2728 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2729 &exception);
2730 } while (exception.retry);
2731 return err;
2732}
2733
2734static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2735{
Trond Myklebust0e574af2005-10-27 22:12:38 -04002736 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2738}
2739
2740static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2741 struct nfs_pathconf *pathconf)
2742{
2743 struct nfs4_pathconf_arg args = {
2744 .fh = fhandle,
2745 .bitmask = server->attr_bitmask,
2746 };
Benny Halevyd45b2982009-04-01 09:21:58 -04002747 struct nfs4_pathconf_res res = {
2748 .pathconf = pathconf,
2749 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 struct rpc_message msg = {
2751 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2752 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04002753 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 };
2755
2756 /* None of the pathconf attributes are mandatory to implement */
2757 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
2758 memset(pathconf, 0, sizeof(*pathconf));
2759 return 0;
2760 }
2761
Trond Myklebust0e574af2005-10-27 22:12:38 -04002762 nfs_fattr_init(pathconf->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002763 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764}
2765
2766static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2767 struct nfs_pathconf *pathconf)
2768{
2769 struct nfs4_exception exception = { };
2770 int err;
2771
2772 do {
2773 err = nfs4_handle_exception(server,
2774 _nfs4_proc_pathconf(server, fhandle, pathconf),
2775 &exception);
2776 } while (exception.retry);
2777 return err;
2778}
2779
Trond Myklebustec06c092006-03-20 13:44:27 -05002780static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781{
Trond Myklebustec06c092006-03-20 13:44:27 -05002782 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002784 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 rpc_restart_call(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05002786 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 }
Trond Myklebust8850df92007-09-28 17:20:07 -04002788
2789 nfs_invalidate_atime(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05002791 renew_lease(server, data->timestamp);
2792 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793}
2794
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002795static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 data->timestamp = jiffies;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002798 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799}
2800
Trond Myklebust788e7a82006-03-20 13:44:27 -05002801static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 struct inode *inode = data->inode;
2804
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002805 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 rpc_restart_call(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05002807 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002809 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04002811 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002812 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05002813 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814}
2815
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002816static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002818 struct nfs_server *server = NFS_SERVER(data->inode);
2819
Trond Myklebusta65318b2009-03-11 14:10:28 -04002820 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002821 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 data->timestamp = jiffies;
2823
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002824 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825}
2826
Trond Myklebust788e7a82006-03-20 13:44:27 -05002827static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 struct inode *inode = data->inode;
2830
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002831 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 rpc_restart_call(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05002833 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04002835 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05002836 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837}
2838
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002839static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840{
Trond Myklebust788e7a82006-03-20 13:44:27 -05002841 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842
Trond Myklebusta65318b2009-03-11 14:10:28 -04002843 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002844 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002845 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846}
2847
2848/*
2849 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
2850 * standalone procedure for queueing an asynchronous RENEW.
2851 */
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002852static void nfs4_renew_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853{
David Howellsadfa6f92006-08-22 20:06:08 -04002854 struct nfs_client *clp = (struct nfs_client *)task->tk_msg.rpc_argp;
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002855 unsigned long timestamp = (unsigned long)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856
2857 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04002858 /* Unless we're shutting down, schedule state recovery! */
2859 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
2860 nfs4_schedule_state_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 return;
2862 }
2863 spin_lock(&clp->cl_lock);
2864 if (time_before(clp->cl_last_renewal,timestamp))
2865 clp->cl_last_renewal = timestamp;
2866 spin_unlock(&clp->cl_lock);
2867}
2868
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002869static const struct rpc_call_ops nfs4_renew_ops = {
2870 .rpc_call_done = nfs4_renew_done,
2871};
2872
David Howellsadfa6f92006-08-22 20:06:08 -04002873int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874{
2875 struct rpc_message msg = {
2876 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2877 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002878 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 };
2880
2881 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002882 &nfs4_renew_ops, (void *)jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883}
2884
David Howellsadfa6f92006-08-22 20:06:08 -04002885int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886{
2887 struct rpc_message msg = {
2888 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2889 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002890 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 };
2892 unsigned long now = jiffies;
2893 int status;
2894
2895 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2896 if (status < 0)
2897 return status;
2898 spin_lock(&clp->cl_lock);
2899 if (time_before(clp->cl_last_renewal,now))
2900 clp->cl_last_renewal = now;
2901 spin_unlock(&clp->cl_lock);
2902 return 0;
2903}
2904
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002905static inline int nfs4_server_supports_acls(struct nfs_server *server)
2906{
2907 return (server->caps & NFS_CAP_ACLS)
2908 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
2909 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
2910}
2911
2912/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
2913 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
2914 * the stack.
2915 */
2916#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
2917
2918static void buf_to_pages(const void *buf, size_t buflen,
2919 struct page **pages, unsigned int *pgbase)
2920{
2921 const void *p = buf;
2922
2923 *pgbase = offset_in_page(buf);
2924 p -= *pgbase;
2925 while (p < buf + buflen) {
2926 *(pages++) = virt_to_page(p);
2927 p += PAGE_CACHE_SIZE;
2928 }
2929}
2930
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002931struct nfs4_cached_acl {
2932 int cached;
2933 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00002934 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002935};
2936
2937static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002938{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002939 struct nfs_inode *nfsi = NFS_I(inode);
2940
2941 spin_lock(&inode->i_lock);
2942 kfree(nfsi->nfs4_acl);
2943 nfsi->nfs4_acl = acl;
2944 spin_unlock(&inode->i_lock);
2945}
2946
2947static void nfs4_zap_acl_attr(struct inode *inode)
2948{
2949 nfs4_set_cached_acl(inode, NULL);
2950}
2951
2952static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
2953{
2954 struct nfs_inode *nfsi = NFS_I(inode);
2955 struct nfs4_cached_acl *acl;
2956 int ret = -ENOENT;
2957
2958 spin_lock(&inode->i_lock);
2959 acl = nfsi->nfs4_acl;
2960 if (acl == NULL)
2961 goto out;
2962 if (buf == NULL) /* user is just asking for length */
2963 goto out_len;
2964 if (acl->cached == 0)
2965 goto out;
2966 ret = -ERANGE; /* see getxattr(2) man page */
2967 if (acl->len > buflen)
2968 goto out;
2969 memcpy(buf, acl->data, acl->len);
2970out_len:
2971 ret = acl->len;
2972out:
2973 spin_unlock(&inode->i_lock);
2974 return ret;
2975}
2976
2977static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
2978{
2979 struct nfs4_cached_acl *acl;
2980
2981 if (buf && acl_len <= PAGE_SIZE) {
2982 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
2983 if (acl == NULL)
2984 goto out;
2985 acl->cached = 1;
2986 memcpy(acl->data, buf, acl_len);
2987 } else {
2988 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
2989 if (acl == NULL)
2990 goto out;
2991 acl->cached = 0;
2992 }
2993 acl->len = acl_len;
2994out:
2995 nfs4_set_cached_acl(inode, acl);
2996}
2997
Trond Myklebust16b42892006-08-24 12:27:15 -04002998static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002999{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003000 struct page *pages[NFS4ACL_MAXPAGES];
3001 struct nfs_getaclargs args = {
3002 .fh = NFS_FH(inode),
3003 .acl_pages = pages,
3004 .acl_len = buflen,
3005 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003006 struct nfs_getaclres res = {
3007 .acl_len = buflen,
3008 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003009 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003010 struct rpc_message msg = {
3011 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3012 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003013 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003014 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003015 struct page *localpage = NULL;
3016 int ret;
3017
3018 if (buflen < PAGE_SIZE) {
3019 /* As long as we're doing a round trip to the server anyway,
3020 * let's be prepared for a page of acl data. */
3021 localpage = alloc_page(GFP_KERNEL);
3022 resp_buf = page_address(localpage);
3023 if (localpage == NULL)
3024 return -ENOMEM;
3025 args.acl_pages[0] = localpage;
3026 args.acl_pgbase = 0;
Benny Halevy663c79b2009-04-01 09:21:59 -04003027 args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003028 } else {
3029 resp_buf = buf;
3030 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3031 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003032 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003033 if (ret)
3034 goto out_free;
Benny Halevy663c79b2009-04-01 09:21:59 -04003035 if (res.acl_len > args.acl_len)
3036 nfs4_write_cached_acl(inode, NULL, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003037 else
Benny Halevy663c79b2009-04-01 09:21:59 -04003038 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003039 if (buf) {
3040 ret = -ERANGE;
Benny Halevy663c79b2009-04-01 09:21:59 -04003041 if (res.acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003042 goto out_free;
3043 if (localpage)
Benny Halevy663c79b2009-04-01 09:21:59 -04003044 memcpy(buf, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003045 }
Benny Halevy663c79b2009-04-01 09:21:59 -04003046 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003047out_free:
3048 if (localpage)
3049 __free_page(localpage);
3050 return ret;
3051}
3052
Trond Myklebust16b42892006-08-24 12:27:15 -04003053static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3054{
3055 struct nfs4_exception exception = { };
3056 ssize_t ret;
3057 do {
3058 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3059 if (ret >= 0)
3060 break;
3061 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3062 } while (exception.retry);
3063 return ret;
3064}
3065
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003066static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3067{
3068 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003069 int ret;
3070
3071 if (!nfs4_server_supports_acls(server))
3072 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003073 ret = nfs_revalidate_inode(server, inode);
3074 if (ret < 0)
3075 return ret;
Trond Myklebustf41f7412008-06-11 17:39:04 -04003076 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3077 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003078 ret = nfs4_read_cached_acl(inode, buf, buflen);
3079 if (ret != -ENOENT)
3080 return ret;
3081 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003082}
3083
Trond Myklebust16b42892006-08-24 12:27:15 -04003084static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003085{
3086 struct nfs_server *server = NFS_SERVER(inode);
3087 struct page *pages[NFS4ACL_MAXPAGES];
3088 struct nfs_setaclargs arg = {
3089 .fh = NFS_FH(inode),
3090 .acl_pages = pages,
3091 .acl_len = buflen,
3092 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003093 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003094 struct rpc_message msg = {
3095 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3096 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003097 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003098 };
3099 int ret;
3100
3101 if (!nfs4_server_supports_acls(server))
3102 return -EOPNOTSUPP;
Trond Myklebust642ac542005-10-18 14:20:19 -07003103 nfs_inode_return_delegation(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003104 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003105 ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003106 nfs_access_zap_cache(inode);
3107 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003108 return ret;
3109}
3110
Trond Myklebust16b42892006-08-24 12:27:15 -04003111static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3112{
3113 struct nfs4_exception exception = { };
3114 int err;
3115 do {
3116 err = nfs4_handle_exception(NFS_SERVER(inode),
3117 __nfs4_proc_set_acl(inode, buf, buflen),
3118 &exception);
3119 } while (exception.retry);
3120 return err;
3121}
3122
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123static int
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003124nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125{
David Howells7539bba2006-08-22 20:06:09 -04003126 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127
3128 if (!clp || task->tk_status >= 0)
3129 return 0;
3130 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003131 case -NFS4ERR_ADMIN_REVOKED:
3132 case -NFS4ERR_BAD_STATEID:
3133 case -NFS4ERR_OPENMODE:
3134 if (state == NULL)
3135 break;
3136 nfs4_state_mark_reclaim_nograce(clp, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 case -NFS4ERR_STALE_CLIENTID:
3138 case -NFS4ERR_STALE_STATEID:
3139 case -NFS4ERR_EXPIRED:
Trond Myklebust5d008372008-02-22 16:34:17 -05003140 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 nfs4_schedule_state_recovery(clp);
Trond Myklebuste005e802008-12-23 15:21:48 -05003142 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
Trond Myklebustfda13932008-02-22 16:34:12 -05003143 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 task->tk_status = 0;
3145 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 case -NFS4ERR_DELAY:
Trond Myklebust2e96d282008-06-11 16:42:05 -04003147 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003148 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3150 task->tk_status = 0;
3151 return -EAGAIN;
3152 case -NFS4ERR_OLD_STATEID:
3153 task->tk_status = 0;
3154 return -EAGAIN;
3155 }
3156 task->tk_status = nfs4_map_errors(task->tk_status);
3157 return 0;
3158}
3159
David Howellsadfa6f92006-08-22 20:06:08 -04003160int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, unsigned short port, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161{
3162 nfs4_verifier sc_verifier;
3163 struct nfs4_setclientid setclientid = {
3164 .sc_verifier = &sc_verifier,
3165 .sc_prog = program,
3166 };
3167 struct rpc_message msg = {
3168 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3169 .rpc_argp = &setclientid,
3170 .rpc_resp = clp,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003171 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 };
Al Virobc4785c2006-10-19 23:28:51 -07003173 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 int loop = 0;
3175 int status;
3176
Al Virobc4785c2006-10-19 23:28:51 -07003177 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3179 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3180
3181 for(;;) {
3182 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003183 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003184 clp->cl_ipaddr,
3185 rpc_peeraddr2str(clp->cl_rpcclient,
3186 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003187 rpc_peeraddr2str(clp->cl_rpcclient,
3188 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003189 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 clp->cl_id_uniquifier);
3191 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003192 sizeof(setclientid.sc_netid),
3193 rpc_peeraddr2str(clp->cl_rpcclient,
3194 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003196 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 clp->cl_ipaddr, port >> 8, port & 255);
3198
3199 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3200 if (status != -NFS4ERR_CLID_INUSE)
3201 break;
3202 if (signalled())
3203 break;
3204 if (loop++ & 1)
3205 ssleep(clp->cl_lease_time + 1);
3206 else
3207 if (++clp->cl_id_uniquifier == 0)
3208 break;
3209 }
3210 return status;
3211}
3212
David Howellsadfa6f92006-08-22 20:06:08 -04003213static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214{
3215 struct nfs_fsinfo fsinfo;
3216 struct rpc_message msg = {
3217 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
3218 .rpc_argp = clp,
3219 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003220 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 };
3222 unsigned long now;
3223 int status;
3224
3225 now = jiffies;
3226 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3227 if (status == 0) {
3228 spin_lock(&clp->cl_lock);
3229 clp->cl_lease_time = fsinfo.lease_time * HZ;
3230 clp->cl_last_renewal = now;
3231 spin_unlock(&clp->cl_lock);
3232 }
3233 return status;
3234}
3235
David Howellsadfa6f92006-08-22 20:06:08 -04003236int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
Trond Myklebust51581f32006-03-20 13:44:47 -05003237{
Benny Halevy77e03672008-06-24 20:25:57 +03003238 long timeout = 0;
Trond Myklebust51581f32006-03-20 13:44:47 -05003239 int err;
3240 do {
3241 err = _nfs4_proc_setclientid_confirm(clp, cred);
3242 switch (err) {
3243 case 0:
3244 return err;
3245 case -NFS4ERR_RESOURCE:
3246 /* The IBM lawyers misread another document! */
3247 case -NFS4ERR_DELAY:
3248 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3249 }
3250 } while (err == 0);
3251 return err;
3252}
3253
Trond Myklebustfe650402006-01-03 09:55:18 +01003254struct nfs4_delegreturndata {
3255 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003256 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003257 struct nfs_fh fh;
3258 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003259 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003260 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003261 int rpc_status;
3262};
3263
Trond Myklebustfe650402006-01-03 09:55:18 +01003264static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3265{
3266 struct nfs4_delegreturndata *data = calldata;
3267 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003268 if (data->rpc_status == 0)
Trond Myklebustfa178f22006-01-03 09:55:38 +01003269 renew_lease(data->res.server, data->timestamp);
Trond Myklebustfe650402006-01-03 09:55:18 +01003270}
3271
3272static void nfs4_delegreturn_release(void *calldata)
3273{
Trond Myklebustfe650402006-01-03 09:55:18 +01003274 kfree(calldata);
3275}
3276
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003277static const struct rpc_call_ops nfs4_delegreturn_ops = {
Trond Myklebustfe650402006-01-03 09:55:18 +01003278 .rpc_call_done = nfs4_delegreturn_done,
3279 .rpc_release = nfs4_delegreturn_release,
3280};
3281
Trond Myklebuste6f81072008-01-24 18:14:34 -05003282static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003283{
3284 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003285 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003286 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003287 struct rpc_message msg = {
3288 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3289 .rpc_cred = cred,
3290 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003291 struct rpc_task_setup task_setup_data = {
3292 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003293 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003294 .callback_ops = &nfs4_delegreturn_ops,
3295 .flags = RPC_TASK_ASYNC,
3296 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003297 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003298
3299 data = kmalloc(sizeof(*data), GFP_KERNEL);
3300 if (data == NULL)
3301 return -ENOMEM;
3302 data->args.fhandle = &data->fh;
3303 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003304 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003305 nfs_copy_fh(&data->fh, NFS_FH(inode));
3306 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003307 data->res.fattr = &data->fattr;
3308 data->res.server = server;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003309 data->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003310 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003311 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003312 data->rpc_status = 0;
3313
Trond Myklebustc970aa82007-07-14 15:39:59 -04003314 task_setup_data.callback_data = data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003315 msg.rpc_argp = &data->args,
3316 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003317 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003318 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003319 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003320 if (!issync)
3321 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003322 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003323 if (status != 0)
3324 goto out;
3325 status = data->rpc_status;
3326 if (status != 0)
3327 goto out;
3328 nfs_refresh_inode(inode, &data->fattr);
3329out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003330 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003331 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332}
3333
Trond Myklebuste6f81072008-01-24 18:14:34 -05003334int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335{
3336 struct nfs_server *server = NFS_SERVER(inode);
3337 struct nfs4_exception exception = { };
3338 int err;
3339 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003340 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 switch (err) {
3342 case -NFS4ERR_STALE_STATEID:
3343 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 case 0:
3345 return 0;
3346 }
3347 err = nfs4_handle_exception(server, err, &exception);
3348 } while (exception.retry);
3349 return err;
3350}
3351
3352#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3353#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3354
3355/*
3356 * sleep, with exponential backoff, and retry the LOCK operation.
3357 */
3358static unsigned long
3359nfs4_set_lock_task_retry(unsigned long timeout)
3360{
Matthew Wilcox150030b2007-12-06 16:24:39 -05003361 schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 timeout <<= 1;
3363 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3364 return NFS4_LOCK_MAXTIMEOUT;
3365 return timeout;
3366}
3367
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3369{
3370 struct inode *inode = state->inode;
3371 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003372 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003373 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003375 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003377 struct nfs_lockt_res res = {
3378 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 };
3380 struct rpc_message msg = {
3381 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3382 .rpc_argp = &arg,
3383 .rpc_resp = &res,
3384 .rpc_cred = state->owner->so_cred,
3385 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 struct nfs4_lock_state *lsp;
3387 int status;
3388
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003389 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003390 status = nfs4_set_lock_state(state, request);
3391 if (status != 0)
3392 goto out;
3393 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003394 arg.lock_owner.id = lsp->ls_id.id;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003395 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003396 switch (status) {
3397 case 0:
3398 request->fl_type = F_UNLCK;
3399 break;
3400 case -NFS4ERR_DENIED:
3401 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05003403 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003404out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 return status;
3406}
3407
3408static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3409{
3410 struct nfs4_exception exception = { };
3411 int err;
3412
3413 do {
3414 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3415 _nfs4_proc_getlk(state, cmd, request),
3416 &exception);
3417 } while (exception.retry);
3418 return err;
3419}
3420
3421static int do_vfs_lock(struct file *file, struct file_lock *fl)
3422{
3423 int res = 0;
3424 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3425 case FL_POSIX:
3426 res = posix_lock_file_wait(file, fl);
3427 break;
3428 case FL_FLOCK:
3429 res = flock_lock_file_wait(file, fl);
3430 break;
3431 default:
3432 BUG();
3433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 return res;
3435}
3436
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003437struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003438 struct nfs_locku_args arg;
3439 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003440 struct nfs4_lock_state *lsp;
3441 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003442 struct file_lock fl;
3443 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003444 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003445};
3446
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003447static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3448 struct nfs_open_context *ctx,
3449 struct nfs4_lock_state *lsp,
3450 struct nfs_seqid *seqid)
3451{
3452 struct nfs4_unlockdata *p;
3453 struct inode *inode = lsp->ls_state->inode;
3454
3455 p = kmalloc(sizeof(*p), GFP_KERNEL);
3456 if (p == NULL)
3457 return NULL;
3458 p->arg.fh = NFS_FH(inode);
3459 p->arg.fl = &p->fl;
3460 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003461 p->res.seqid = seqid;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003462 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003463 p->arg.stateid = &lsp->ls_stateid;
3464 p->lsp = lsp;
3465 atomic_inc(&lsp->ls_count);
3466 /* Ensure we don't close file until we're done freeing locks! */
3467 p->ctx = get_nfs_open_context(ctx);
3468 memcpy(&p->fl, fl, sizeof(p->fl));
3469 p->server = NFS_SERVER(inode);
3470 return p;
3471}
3472
Trond Myklebust06f814a2006-01-03 09:55:07 +01003473static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003474{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003475 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003476 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003477 nfs4_put_lock_state(calldata->lsp);
3478 put_nfs_open_context(calldata->ctx);
3479 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003480}
3481
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003482static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003483{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003484 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003485
Trond Myklebust06f814a2006-01-03 09:55:07 +01003486 if (RPC_ASSASSINATED(task))
3487 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003488 switch (task->tk_status) {
3489 case 0:
3490 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003491 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003492 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01003493 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003494 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003495 case -NFS4ERR_BAD_STATEID:
3496 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003497 case -NFS4ERR_STALE_STATEID:
3498 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003499 break;
3500 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003501 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003502 rpc_restart_call(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003503 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003504}
3505
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003506static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003507{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003508 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003510 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003511 return;
3512 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003513 /* Note: exit _without_ running nfs4_locku_done */
3514 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003515 return;
3516 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003517 calldata->timestamp = jiffies;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003518 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519}
3520
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003521static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003522 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003523 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01003524 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003525};
3526
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003527static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3528 struct nfs_open_context *ctx,
3529 struct nfs4_lock_state *lsp,
3530 struct nfs_seqid *seqid)
3531{
3532 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003533 struct rpc_message msg = {
3534 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3535 .rpc_cred = ctx->cred,
3536 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003537 struct rpc_task_setup task_setup_data = {
3538 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04003539 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003540 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003541 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003542 .flags = RPC_TASK_ASYNC,
3543 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003544
Frank Filz137d6ac2007-07-09 15:32:29 -07003545 /* Ensure this is an unlock - when canceling a lock, the
3546 * canceled lock is passed in, and it won't be an unlock.
3547 */
3548 fl->fl_type = F_UNLCK;
3549
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003550 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3551 if (data == NULL) {
3552 nfs_free_seqid(seqid);
3553 return ERR_PTR(-ENOMEM);
3554 }
3555
Trond Myklebust5138fde2007-07-14 15:40:01 -04003556 msg.rpc_argp = &data->arg,
3557 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003558 task_setup_data.callback_data = data;
3559 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003560}
3561
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3563{
Trond Myklebust19e03c52008-12-23 15:21:44 -05003564 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003565 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003566 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01003567 struct rpc_task *task;
3568 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003569 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570
Trond Myklebust9b073572006-06-29 16:38:34 -04003571 status = nfs4_set_lock_state(state, request);
3572 /* Unlock _before_ we do the RPC call */
3573 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003574 down_read(&nfsi->rwsem);
3575 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
3576 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003577 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003578 }
3579 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003580 if (status != 0)
3581 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003582 /* Is this a delegated lock? */
3583 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04003584 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003585 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003586 seqid = nfs_alloc_seqid(&lsp->ls_seqid);
Trond Myklebust9b073572006-06-29 16:38:34 -04003587 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003588 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04003589 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04003590 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003591 status = PTR_ERR(task);
3592 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04003593 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003594 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003595 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04003596out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003597 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003598 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599}
3600
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003601struct nfs4_lockdata {
3602 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003603 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003604 struct nfs4_lock_state *lsp;
3605 struct nfs_open_context *ctx;
3606 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003607 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003608 int rpc_status;
3609 int cancelled;
3610};
3611
3612static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
3613 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
3614{
3615 struct nfs4_lockdata *p;
3616 struct inode *inode = lsp->ls_state->inode;
3617 struct nfs_server *server = NFS_SERVER(inode);
3618
3619 p = kzalloc(sizeof(*p), GFP_KERNEL);
3620 if (p == NULL)
3621 return NULL;
3622
3623 p->arg.fh = NFS_FH(inode);
3624 p->arg.fl = &p->fl;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003625 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid);
3626 if (p->arg.open_seqid == NULL)
3627 goto out_free;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003628 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3629 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003630 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003631 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04003632 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003633 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003634 p->res.lock_seqid = p->arg.lock_seqid;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003635 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003636 p->lsp = lsp;
3637 atomic_inc(&lsp->ls_count);
3638 p->ctx = get_nfs_open_context(ctx);
3639 memcpy(&p->fl, fl, sizeof(p->fl));
3640 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003641out_free_seqid:
3642 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003643out_free:
3644 kfree(p);
3645 return NULL;
3646}
3647
3648static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
3649{
3650 struct nfs4_lockdata *data = calldata;
3651 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652
Harvey Harrison3110ff82008-05-02 13:42:44 -07003653 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003654 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
3655 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003656 /* Do we need to do an open_to_lock_owner? */
3657 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05003658 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
3659 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003660 data->arg.open_stateid = &state->stateid;
3661 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003662 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003663 } else
3664 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003665 data->timestamp = jiffies;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003666 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003667 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003668}
3669
3670static void nfs4_lock_done(struct rpc_task *task, void *calldata)
3671{
3672 struct nfs4_lockdata *data = calldata;
3673
Harvey Harrison3110ff82008-05-02 13:42:44 -07003674 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003675
3676 data->rpc_status = task->tk_status;
3677 if (RPC_ASSASSINATED(task))
3678 goto out;
3679 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003680 if (data->rpc_status == 0)
3681 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
3682 else
3683 goto out;
3684 }
3685 if (data->rpc_status == 0) {
3686 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
3687 sizeof(data->lsp->ls_stateid.data));
3688 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust88be9f92007-06-05 10:42:27 -04003689 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003690 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003691out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07003692 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003693}
3694
3695static void nfs4_lock_release(void *calldata)
3696{
3697 struct nfs4_lockdata *data = calldata;
3698
Harvey Harrison3110ff82008-05-02 13:42:44 -07003699 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003700 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003701 if (data->cancelled != 0) {
3702 struct rpc_task *task;
3703 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
3704 data->arg.lock_seqid);
3705 if (!IS_ERR(task))
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003706 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003707 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003708 } else
3709 nfs_free_seqid(data->arg.lock_seqid);
3710 nfs4_put_lock_state(data->lsp);
3711 put_nfs_open_context(data->ctx);
3712 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003713 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003714}
3715
3716static const struct rpc_call_ops nfs4_lock_ops = {
3717 .rpc_call_prepare = nfs4_lock_prepare,
3718 .rpc_call_done = nfs4_lock_done,
3719 .rpc_release = nfs4_lock_release,
3720};
3721
3722static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int reclaim)
3723{
3724 struct nfs4_lockdata *data;
3725 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003726 struct rpc_message msg = {
3727 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
3728 .rpc_cred = state->owner->so_cred,
3729 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003730 struct rpc_task_setup task_setup_data = {
3731 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04003732 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003733 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003734 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003735 .flags = RPC_TASK_ASYNC,
3736 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003737 int ret;
3738
Harvey Harrison3110ff82008-05-02 13:42:44 -07003739 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04003740 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003741 fl->fl_u.nfs4_fl.owner);
3742 if (data == NULL)
3743 return -ENOMEM;
3744 if (IS_SETLKW(cmd))
3745 data->arg.block = 1;
3746 if (reclaim != 0)
3747 data->arg.reclaim = 1;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003748 msg.rpc_argp = &data->arg,
3749 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003750 task_setup_data.callback_data = data;
3751 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003752 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003753 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003754 ret = nfs4_wait_for_completion_rpc_task(task);
3755 if (ret == 0) {
3756 ret = data->rpc_status;
3757 if (ret == -NFS4ERR_DENIED)
3758 ret = -EAGAIN;
3759 } else
3760 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003761 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003762 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003763 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764}
3765
3766static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
3767{
Trond Myklebust202b50d2005-06-22 17:16:29 +00003768 struct nfs_server *server = NFS_SERVER(state->inode);
3769 struct nfs4_exception exception = { };
3770 int err;
3771
3772 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04003773 /* Cache the lock if possible... */
3774 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3775 return 0;
Trond Myklebust202b50d2005-06-22 17:16:29 +00003776 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
3777 if (err != -NFS4ERR_DELAY)
3778 break;
3779 nfs4_handle_exception(server, err, &exception);
3780 } while (exception.retry);
3781 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782}
3783
3784static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
3785{
Trond Myklebust202b50d2005-06-22 17:16:29 +00003786 struct nfs_server *server = NFS_SERVER(state->inode);
3787 struct nfs4_exception exception = { };
3788 int err;
3789
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003790 err = nfs4_set_lock_state(state, request);
3791 if (err != 0)
3792 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00003793 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04003794 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3795 return 0;
Trond Myklebust202b50d2005-06-22 17:16:29 +00003796 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
3797 if (err != -NFS4ERR_DELAY)
3798 break;
3799 nfs4_handle_exception(server, err, &exception);
3800 } while (exception.retry);
3801 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802}
3803
3804static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3805{
Trond Myklebust19e03c52008-12-23 15:21:44 -05003806 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04003807 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 int status;
3809
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003810 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003811 status = nfs4_set_lock_state(state, request);
3812 if (status != 0)
3813 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04003814 request->fl_flags |= FL_ACCESS;
3815 status = do_vfs_lock(request->fl_file, request);
3816 if (status < 0)
3817 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003818 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04003819 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04003820 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04003821 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05003822 request->fl_flags = fl_flags & ~FL_SLEEP;
3823 status = do_vfs_lock(request->fl_file, request);
3824 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04003825 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003826 status = _nfs4_do_setlk(state, cmd, request, 0);
3827 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04003828 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003829 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04003830 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003831 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07003832 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04003833out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05003834 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04003835out:
3836 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 return status;
3838}
3839
3840static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3841{
3842 struct nfs4_exception exception = { };
3843 int err;
3844
3845 do {
3846 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3847 _nfs4_proc_setlk(state, cmd, request),
3848 &exception);
3849 } while (exception.retry);
3850 return err;
3851}
3852
3853static int
3854nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
3855{
3856 struct nfs_open_context *ctx;
3857 struct nfs4_state *state;
3858 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
3859 int status;
3860
3861 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04003862 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863 state = ctx->state;
3864
3865 if (request->fl_start < 0 || request->fl_end < 0)
3866 return -EINVAL;
3867
3868 if (IS_GETLK(cmd))
3869 return nfs4_proc_getlk(state, F_GETLK, request);
3870
3871 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
3872 return -EINVAL;
3873
3874 if (request->fl_type == F_UNLCK)
3875 return nfs4_proc_unlck(state, cmd, request);
3876
3877 do {
3878 status = nfs4_proc_setlk(state, cmd, request);
3879 if ((status != -EAGAIN) || IS_SETLK(cmd))
3880 break;
3881 timeout = nfs4_set_lock_task_retry(timeout);
3882 status = -ERESTARTSYS;
3883 if (signalled())
3884 break;
3885 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886 return status;
3887}
3888
Trond Myklebust888e6942005-11-04 15:38:11 -05003889int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
3890{
3891 struct nfs_server *server = NFS_SERVER(state->inode);
3892 struct nfs4_exception exception = { };
3893 int err;
3894
3895 err = nfs4_set_lock_state(state, fl);
3896 if (err != 0)
3897 goto out;
3898 do {
3899 err = _nfs4_do_setlk(state, F_SETLK, fl, 0);
3900 if (err != -NFS4ERR_DELAY)
3901 break;
3902 err = nfs4_handle_exception(server, err, &exception);
3903 } while (exception.retry);
3904out:
3905 return err;
3906}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003907
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003908#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
3909
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003910int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
3911 size_t buflen, int flags)
3912{
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003913 struct inode *inode = dentry->d_inode;
3914
3915 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3916 return -EOPNOTSUPP;
3917
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003918 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003919}
3920
3921/* The getxattr man page suggests returning -ENODATA for unknown attributes,
3922 * and that's what we'll do for e.g. user attributes that haven't been set.
3923 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
3924 * attributes in kernel-managed attribute namespaces. */
3925ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
3926 size_t buflen)
3927{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003928 struct inode *inode = dentry->d_inode;
3929
3930 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3931 return -EOPNOTSUPP;
3932
3933 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003934}
3935
3936ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
3937{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003938 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003939
J. Bruce Fields096455a2006-03-20 23:23:42 -05003940 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
3941 return 0;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003942 if (buf && buflen < len)
3943 return -ERANGE;
3944 if (buf)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003945 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
3946 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003947}
3948
Trond Myklebust69aaaae2009-03-11 14:10:28 -04003949static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
3950{
3951 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
3952 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
3953 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
3954 return;
3955
3956 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
3957 NFS_ATTR_FATTR_NLINK;
3958 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3959 fattr->nlink = 2;
3960}
3961
Trond Myklebust56659e92007-07-17 21:52:39 -04003962int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04003963 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04003964{
3965 struct nfs_server *server = NFS_SERVER(dir);
3966 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04003967 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
3968 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
Trond Myklebust683b57b2006-06-09 09:34:22 -04003969 };
3970 struct nfs4_fs_locations_arg args = {
3971 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05003972 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04003973 .page = page,
3974 .bitmask = bitmask,
3975 };
Benny Halevy22958462009-04-01 09:22:02 -04003976 struct nfs4_fs_locations_res res = {
3977 .fs_locations = fs_locations,
3978 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04003979 struct rpc_message msg = {
3980 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
3981 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04003982 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04003983 };
3984 int status;
3985
Harvey Harrison3110ff82008-05-02 13:42:44 -07003986 dprintk("%s: start\n", __func__);
Trond Myklebustc228fd32007-01-13 02:28:11 -05003987 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04003988 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04003989 fs_locations->nlocations = 0;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003990 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Trond Myklebust69aaaae2009-03-11 14:10:28 -04003991 nfs_fixup_referral_attributes(&fs_locations->fattr);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003992 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04003993 return status;
3994}
3995
Andy Adamson557134a2009-04-01 09:21:53 -04003996#ifdef CONFIG_NFS_V4_1
3997/* Destroy the slot table */
3998static void nfs4_destroy_slot_table(struct nfs4_session *session)
3999{
4000 if (session->fc_slot_table.slots == NULL)
4001 return;
4002 kfree(session->fc_slot_table.slots);
4003 session->fc_slot_table.slots = NULL;
4004 return;
4005}
4006
4007struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4008{
4009 struct nfs4_session *session;
4010 struct nfs4_slot_table *tbl;
4011
4012 session = kzalloc(sizeof(struct nfs4_session), GFP_KERNEL);
4013 if (!session)
4014 return NULL;
4015 tbl = &session->fc_slot_table;
4016 spin_lock_init(&tbl->slot_tbl_lock);
4017 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "Slot table");
4018 session->clp = clp;
4019 return session;
4020}
4021
4022void nfs4_destroy_session(struct nfs4_session *session)
4023{
4024 nfs4_destroy_slot_table(session);
4025 kfree(session);
4026}
4027
4028#endif /* CONFIG_NFS_V4_1 */
4029
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05004031 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05004032 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033 .recover_open = nfs4_open_reclaim,
4034 .recover_lock = nfs4_lock_reclaim,
4035};
4036
Trond Myklebustb79a4a12008-12-23 15:21:41 -05004037struct nfs4_state_recovery_ops nfs4_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05004038 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05004039 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 .recover_open = nfs4_open_expired,
4041 .recover_lock = nfs4_lock_expired,
4042};
4043
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08004044static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004045 .permission = nfs_permission,
4046 .getattr = nfs_getattr,
4047 .setattr = nfs_setattr,
4048 .getxattr = nfs4_getxattr,
4049 .setxattr = nfs4_setxattr,
4050 .listxattr = nfs4_listxattr,
4051};
4052
David Howells509de812006-08-22 20:06:11 -04004053const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 .version = 4, /* protocol version */
4055 .dentry_ops = &nfs4_dentry_operations,
4056 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004057 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058 .getroot = nfs4_proc_get_root,
4059 .getattr = nfs4_proc_getattr,
4060 .setattr = nfs4_proc_setattr,
David Howells2b3de442006-08-22 20:06:09 -04004061 .lookupfh = nfs4_proc_lookupfh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062 .lookup = nfs4_proc_lookup,
4063 .access = nfs4_proc_access,
4064 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 .create = nfs4_proc_create,
4066 .remove = nfs4_proc_remove,
4067 .unlink_setup = nfs4_proc_unlink_setup,
4068 .unlink_done = nfs4_proc_unlink_done,
4069 .rename = nfs4_proc_rename,
4070 .link = nfs4_proc_link,
4071 .symlink = nfs4_proc_symlink,
4072 .mkdir = nfs4_proc_mkdir,
4073 .rmdir = nfs4_proc_remove,
4074 .readdir = nfs4_proc_readdir,
4075 .mknod = nfs4_proc_mknod,
4076 .statfs = nfs4_proc_statfs,
4077 .fsinfo = nfs4_proc_fsinfo,
4078 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04004079 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080 .decode_dirent = nfs4_decode_dirent,
4081 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05004082 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05004084 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05004086 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004088 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04004089 .close_context = nfs4_close_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090};
4091
4092/*
4093 * Local variables:
4094 * c-basic-offset: 8
4095 * End:
4096 */