blob: a34fada51446b9e753c0a8727354216e006a35f4 [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 Adamsonb0df8062009-04-01 09:22:18 -0400333static void nfs41_sequence_done(struct nfs_client *clp,
334 struct nfs4_sequence_res *res,
335 int rpc_status)
336{
337 unsigned long timestamp;
338 struct nfs4_slot_table *tbl;
339 struct nfs4_slot *slot;
340
341 /*
342 * sr_status remains 1 if an RPC level error occurred. The server
343 * may or may not have processed the sequence operation..
344 * Proceed as if the server received and processed the sequence
345 * operation.
346 */
347 if (res->sr_status == 1)
348 res->sr_status = NFS_OK;
349
350 /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
351 if (res->sr_slotid == NFS4_MAX_SLOT_TABLE)
352 goto out;
353
354 tbl = &clp->cl_session->fc_slot_table;
355 slot = tbl->slots + res->sr_slotid;
356
357 if (res->sr_status == 0) {
358 /* Update the slot's sequence and clientid lease timer */
359 ++slot->seq_nr;
360 timestamp = res->sr_renewal_time;
361 spin_lock(&clp->cl_lock);
362 if (time_before(clp->cl_last_renewal, timestamp))
363 clp->cl_last_renewal = timestamp;
364 spin_unlock(&clp->cl_lock);
365 return;
366 }
367out:
368 /* The session may be reset by one of the error handlers. */
369 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
370 nfs41_sequence_free_slot(clp, res);
371}
372
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400373/*
Benny Halevy510b8172009-04-01 09:22:14 -0400374 * nfs4_find_slot - efficiently look for a free slot
375 *
376 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
377 * If found, we mark the slot as used, update the highest_used_slotid,
378 * and respectively set up the sequence operation args.
379 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400380 *
381 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400382 */
383static u8
384nfs4_find_slot(struct nfs4_slot_table *tbl, struct rpc_task *task)
385{
386 int slotid;
387 u8 ret_id = NFS4_MAX_SLOT_TABLE;
388 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
389
Benny Halevy510b8172009-04-01 09:22:14 -0400390 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
391 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
392 tbl->max_slots);
393 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
394 if (slotid >= tbl->max_slots)
395 goto out;
396 __set_bit(slotid, tbl->used_slots);
397 if (slotid > tbl->highest_used_slotid)
398 tbl->highest_used_slotid = slotid;
399 ret_id = slotid;
400out:
401 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
402 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400403 return ret_id;
404}
405
Andy Adamsonce5039c2009-04-01 09:22:13 -0400406static int nfs41_setup_sequence(struct nfs4_session *session,
407 struct nfs4_sequence_args *args,
408 struct nfs4_sequence_res *res,
409 int cache_reply,
410 struct rpc_task *task)
411{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400412 struct nfs4_slot *slot;
413 struct nfs4_slot_table *tbl;
414 u8 slotid;
415
416 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400417 /* slot already allocated? */
418 if (res->sr_slotid != NFS4_MAX_SLOT_TABLE)
419 return 0;
420
421 memset(res, 0, sizeof(*res));
422 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400423 tbl = &session->fc_slot_table;
424
425 spin_lock(&tbl->slot_tbl_lock);
426 slotid = nfs4_find_slot(tbl, task);
427 if (slotid == NFS4_MAX_SLOT_TABLE) {
428 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
429 spin_unlock(&tbl->slot_tbl_lock);
430 dprintk("<-- %s: no free slots\n", __func__);
431 return -EAGAIN;
432 }
433 spin_unlock(&tbl->slot_tbl_lock);
434
435 slot = tbl->slots + slotid;
436 args->sa_slotid = slotid;
437 args->sa_cache_this = cache_reply;
438
439 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
440
441 res->sr_slotid = slotid;
442 res->sr_renewal_time = jiffies;
443 /*
444 * sr_status is only set in decode_sequence, and so will remain
445 * set to 1 if an rpc level failure occurs.
446 */
447 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400448 return 0;
449}
450
451int nfs4_setup_sequence(struct nfs_client *clp,
452 struct nfs4_sequence_args *args,
453 struct nfs4_sequence_res *res,
454 int cache_reply,
455 struct rpc_task *task)
456{
457 int ret = 0;
458
459 dprintk("--> %s clp %p session %p sr_slotid %d\n",
460 __func__, clp, clp->cl_session, res->sr_slotid);
461
462 if (!nfs4_has_session(clp))
463 goto out;
464 ret = nfs41_setup_sequence(clp->cl_session, args, res, cache_reply,
465 task);
466 if (ret != -EAGAIN) {
467 /* terminate rpc task */
468 task->tk_status = ret;
469 task->tk_action = NULL;
470 }
471out:
472 dprintk("<-- %s status=%d\n", __func__, ret);
473 return ret;
474}
475
476struct nfs41_call_sync_data {
477 struct nfs_client *clp;
478 struct nfs4_sequence_args *seq_args;
479 struct nfs4_sequence_res *seq_res;
480 int cache_reply;
481};
482
483static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
484{
485 struct nfs41_call_sync_data *data = calldata;
486
487 dprintk("--> %s data->clp->cl_session %p\n", __func__,
488 data->clp->cl_session);
489 if (nfs4_setup_sequence(data->clp, data->seq_args,
490 data->seq_res, data->cache_reply, task))
491 return;
492 rpc_call_start(task);
493}
494
Andy Adamson69ab40c2009-04-01 09:22:19 -0400495static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
496{
497 struct nfs41_call_sync_data *data = calldata;
498
499 nfs41_sequence_done(data->clp, data->seq_res, task->tk_status);
500 nfs41_sequence_free_slot(data->clp, data->seq_res);
501}
502
Andy Adamsonce5039c2009-04-01 09:22:13 -0400503struct rpc_call_ops nfs41_call_sync_ops = {
504 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400505 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400506};
507
508static int nfs4_call_sync_sequence(struct nfs_client *clp,
509 struct rpc_clnt *clnt,
510 struct rpc_message *msg,
511 struct nfs4_sequence_args *args,
512 struct nfs4_sequence_res *res,
513 int cache_reply)
514{
515 int ret;
516 struct rpc_task *task;
517 struct nfs41_call_sync_data data = {
518 .clp = clp,
519 .seq_args = args,
520 .seq_res = res,
521 .cache_reply = cache_reply,
522 };
523 struct rpc_task_setup task_setup = {
524 .rpc_client = clnt,
525 .rpc_message = msg,
526 .callback_ops = &nfs41_call_sync_ops,
527 .callback_data = &data
528 };
529
530 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
531 task = rpc_run_task(&task_setup);
532 if (IS_ERR(task))
533 ret = PTR_ERR(task);
534 else {
535 ret = task->tk_status;
536 rpc_put_task(task);
537 }
538 return ret;
539}
540
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400541int _nfs4_call_sync_session(struct nfs_server *server,
542 struct rpc_message *msg,
543 struct nfs4_sequence_args *args,
544 struct nfs4_sequence_res *res,
545 int cache_reply)
546{
Andy Adamsonce5039c2009-04-01 09:22:13 -0400547 return nfs4_call_sync_sequence(server->nfs_client, server->client,
548 msg, args, res, cache_reply);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400549}
550
551#endif /* CONFIG_NFS_V4_1 */
552
553int _nfs4_call_sync(struct nfs_server *server,
554 struct rpc_message *msg,
555 struct nfs4_sequence_args *args,
556 struct nfs4_sequence_res *res,
557 int cache_reply)
558{
Benny Halevyf3752972009-04-01 09:22:04 -0400559 args->sa_session = res->sr_session = NULL;
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400560 return rpc_call_sync(server->client, msg, 0);
561}
562
563#define nfs4_call_sync(server, msg, args, res, cache_reply) \
564 (server)->nfs_client->cl_call_sync((server), (msg), &(args)->seq_args, \
565 &(res)->seq_res, (cache_reply))
566
Andy Adamsonb0df8062009-04-01 09:22:18 -0400567static void nfs4_sequence_done(const struct nfs_server *server,
568 struct nfs4_sequence_res *res, int rpc_status)
569{
570#ifdef CONFIG_NFS_V4_1
571 if (nfs4_has_session(server->nfs_client))
572 nfs41_sequence_done(server->nfs_client, res, rpc_status);
573#endif /* CONFIG_NFS_V4_1 */
574}
575
576/* no restart, therefore free slot here */
577static void nfs4_sequence_done_free_slot(const struct nfs_server *server,
578 struct nfs4_sequence_res *res,
579 int rpc_status)
580{
581 nfs4_sequence_done(server, res, rpc_status);
582 nfs4_sequence_free_slot(server->nfs_client, res);
583}
584
Trond Myklebust38478b22006-05-25 01:40:57 -0400585static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
Trond Myklebust38478b22006-05-25 01:40:57 -0400587 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Trond Myklebust38478b22006-05-25 01:40:57 -0400589 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400590 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
591 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400592 nfs_force_lookup_revalidate(dir);
Trond Myklebust40d24702007-10-08 09:24:22 -0400593 nfsi->change_attr = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400594 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595}
596
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100597struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400598 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100599 struct nfs_openargs o_arg;
600 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100601 struct nfs_open_confirmargs c_arg;
602 struct nfs_open_confirmres c_res;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100603 struct nfs_fattr f_attr;
604 struct nfs_fattr dir_attr;
Trond Myklebustad389da2007-06-05 12:30:00 -0400605 struct path path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100606 struct dentry *dir;
607 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400608 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100609 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100610 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400611 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100612 int rpc_status;
613 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100614};
615
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400616
617static void nfs4_init_opendata_res(struct nfs4_opendata *p)
618{
619 p->o_res.f_attr = &p->f_attr;
620 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400621 p->o_res.seqid = p->o_arg.seqid;
622 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400623 p->o_res.server = p->o_arg.server;
624 nfs_fattr_init(&p->f_attr);
625 nfs_fattr_init(&p->dir_attr);
626}
627
Trond Myklebustad389da2007-06-05 12:30:00 -0400628static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500629 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100630 const struct iattr *attrs)
631{
Trond Myklebustad389da2007-06-05 12:30:00 -0400632 struct dentry *parent = dget_parent(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100633 struct inode *dir = parent->d_inode;
634 struct nfs_server *server = NFS_SERVER(dir);
635 struct nfs4_opendata *p;
636
637 p = kzalloc(sizeof(*p), GFP_KERNEL);
638 if (p == NULL)
639 goto err;
640 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
641 if (p->o_arg.seqid == NULL)
642 goto err_free;
Trond Myklebustad389da2007-06-05 12:30:00 -0400643 p->path.mnt = mntget(path->mnt);
644 p->path.dentry = dget(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100645 p->dir = parent;
646 p->owner = sp;
647 atomic_inc(&sp->so_count);
648 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500649 p->o_arg.open_flags = flags;
650 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400651 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400652 p->o_arg.id = sp->so_owner_id.id;
Trond Myklebustad389da2007-06-05 12:30:00 -0400653 p->o_arg.name = &p->path.dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100654 p->o_arg.server = server;
655 p->o_arg.bitmask = server->attr_bitmask;
656 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Andy Adamson5f7dbd52009-04-01 09:22:05 -0400657 p->o_res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100658 if (flags & O_EXCL) {
659 u32 *s = (u32 *) p->o_arg.u.verifier.data;
660 s[0] = jiffies;
661 s[1] = current->pid;
662 } else if (flags & O_CREAT) {
663 p->o_arg.u.attrs = &p->attrs;
664 memcpy(&p->attrs, attrs, sizeof(p->attrs));
665 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100666 p->c_arg.fh = &p->o_res.fh;
667 p->c_arg.stateid = &p->o_res.stateid;
668 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400669 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400670 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100671 return p;
672err_free:
673 kfree(p);
674err:
675 dput(parent);
676 return NULL;
677}
678
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400679static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100680{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400681 struct nfs4_opendata *p = container_of(kref,
682 struct nfs4_opendata, kref);
683
684 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400685 if (p->state != NULL)
686 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400687 nfs4_put_state_owner(p->owner);
688 dput(p->dir);
Jan Blunck31f31db12008-05-02 13:42:45 -0700689 path_put(&p->path);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400690 kfree(p);
691}
692
693static void nfs4_opendata_put(struct nfs4_opendata *p)
694{
695 if (p != NULL)
696 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100697}
698
Trond Myklebust06f814a2006-01-03 09:55:07 +0100699static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
700{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100701 int ret;
702
Trond Myklebust06f814a2006-01-03 09:55:07 +0100703 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100704 return ret;
705}
706
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500707static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400708{
709 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500710
711 if (open_mode & O_EXCL)
712 goto out;
713 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400714 case FMODE_READ:
715 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400716 break;
717 case FMODE_WRITE:
718 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400719 break;
720 case FMODE_READ|FMODE_WRITE:
721 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0;
722 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500723out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400724 return ret;
725}
726
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500727static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400728{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500729 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400730 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500731 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400732 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500733 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400734 return 1;
735}
736
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500737static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100738{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500739 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100740 case FMODE_WRITE:
741 state->n_wronly++;
742 break;
743 case FMODE_READ:
744 state->n_rdonly++;
745 break;
746 case FMODE_READ|FMODE_WRITE:
747 state->n_rdwr++;
748 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500749 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100750}
751
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500752static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400753{
754 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
755 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
756 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500757 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400758 case FMODE_READ:
759 set_bit(NFS_O_RDONLY_STATE, &state->flags);
760 break;
761 case FMODE_WRITE:
762 set_bit(NFS_O_WRONLY_STATE, &state->flags);
763 break;
764 case FMODE_READ|FMODE_WRITE:
765 set_bit(NFS_O_RDWR_STATE, &state->flags);
766 }
767}
768
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500769static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400770{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400771 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500772 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400773 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400774}
775
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500776static 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 -0700777{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400778 /*
779 * Protect the call to nfs4_state_set_mode_locked and
780 * serialise the stateid update
781 */
782 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400783 if (deleg_stateid != NULL) {
784 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
785 set_bit(NFS_DELEGATED_STATE, &state->flags);
786 }
787 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500788 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400789 write_sequnlock(&state->seqlock);
790 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500791 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700792 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793}
794
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500795static 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 -0500796{
797 struct nfs_inode *nfsi = NFS_I(state->inode);
798 struct nfs_delegation *deleg_cur;
799 int ret = 0;
800
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500801 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500802
803 rcu_read_lock();
804 deleg_cur = rcu_dereference(nfsi->delegation);
805 if (deleg_cur == NULL)
806 goto no_delegation;
807
808 spin_lock(&deleg_cur->lock);
809 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500810 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500811 goto no_delegation_unlock;
812
813 if (delegation == NULL)
814 delegation = &deleg_cur->stateid;
815 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
816 goto no_delegation_unlock;
817
Trond Myklebustb7391f42008-12-23 15:21:52 -0500818 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500819 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500820 ret = 1;
821no_delegation_unlock:
822 spin_unlock(&deleg_cur->lock);
823no_delegation:
824 rcu_read_unlock();
825
826 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500827 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500828 ret = 1;
829 }
830
831 return ret;
832}
833
834
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500835static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400836{
837 struct nfs_delegation *delegation;
838
839 rcu_read_lock();
840 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500841 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -0400842 rcu_read_unlock();
843 return;
844 }
845 rcu_read_unlock();
846 nfs_inode_return_delegation(inode);
847}
848
Trond Myklebust6ee41262007-07-08 14:11:36 -0400849static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400850{
851 struct nfs4_state *state = opendata->state;
852 struct nfs_inode *nfsi = NFS_I(state->inode);
853 struct nfs_delegation *delegation;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500854 int open_mode = opendata->o_arg.open_flags & O_EXCL;
855 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400856 nfs4_stateid stateid;
857 int ret = -EAGAIN;
858
Trond Myklebustaac00a82007-07-05 19:02:21 -0400859 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500860 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400861 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500862 if (can_open_cached(state, fmode, open_mode)) {
863 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400864 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400865 goto out_return_state;
866 }
867 spin_unlock(&state->owner->so_lock);
868 }
Trond Myklebust34310432008-12-23 15:21:38 -0500869 rcu_read_lock();
870 delegation = rcu_dereference(nfsi->delegation);
871 if (delegation == NULL ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500872 !can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -0500873 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -0400874 break;
Trond Myklebust34310432008-12-23 15:21:38 -0500875 }
Trond Myklebustaac00a82007-07-05 19:02:21 -0400876 /* Save the delegation */
877 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
878 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -0400879 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400880 if (ret != 0)
881 goto out;
882 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -0500883
884 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500885 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -0500886 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400887 }
Trond Myklebustaac00a82007-07-05 19:02:21 -0400888out:
889 return ERR_PTR(ret);
890out_return_state:
891 atomic_inc(&state->count);
892 return state;
893}
894
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100895static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
896{
897 struct inode *inode;
898 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -0400899 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400900 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100901
Trond Myklebustaac00a82007-07-05 19:02:21 -0400902 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400903 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400904 goto out;
905 }
906
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400907 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100908 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400909 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100910 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400911 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -0500912 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400913 goto err;
914 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100915 state = nfs4_get_open_state(inode, data->owner);
916 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400917 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -0400918 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -0400919 int delegation_flags = 0;
920
Trond Myklebust003707c2007-07-05 18:07:55 -0400921 rcu_read_lock();
922 delegation = rcu_dereference(NFS_I(inode)->delegation);
923 if (delegation)
924 delegation_flags = delegation->flags;
925 rcu_read_unlock();
Trond Myklebust15c831b2008-12-23 15:21:39 -0500926 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -0400927 nfs_inode_set_delegation(state->inode,
928 data->owner->so_cred,
929 &data->o_res);
930 else
931 nfs_inode_reclaim_delegation(state->inode,
932 data->owner->so_cred,
933 &data->o_res);
934 }
Trond Myklebust34310432008-12-23 15:21:38 -0500935
936 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500937 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100938 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400939out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100940 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400941err_put_inode:
942 iput(inode);
943err:
944 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100945}
946
Trond Myklebust864472e2006-01-03 09:55:15 +0100947static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
948{
949 struct nfs_inode *nfsi = NFS_I(state->inode);
950 struct nfs_open_context *ctx;
951
952 spin_lock(&state->inode->i_lock);
953 list_for_each_entry(ctx, &nfsi->open_files, list) {
954 if (ctx->state != state)
955 continue;
956 get_nfs_open_context(ctx);
957 spin_unlock(&state->inode->i_lock);
958 return ctx;
959 }
960 spin_unlock(&state->inode->i_lock);
961 return ERR_PTR(-ENOENT);
962}
963
Trond Myklebust6f220ed2007-07-17 21:50:45 -0400964static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
965{
966 struct nfs4_opendata *opendata;
967
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500968 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL);
Trond Myklebust6f220ed2007-07-17 21:50:45 -0400969 if (opendata == NULL)
970 return ERR_PTR(-ENOMEM);
971 opendata->state = state;
972 atomic_inc(&state->count);
973 return opendata;
974}
975
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500976static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +0100977{
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400978 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +0100979 int ret;
980
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500981 opendata->o_arg.open_flags = 0;
982 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400983 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
984 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
985 nfs4_init_opendata_res(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +0100986 ret = _nfs4_proc_open(opendata);
987 if (ret != 0)
988 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400989 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400990 if (IS_ERR(newstate))
991 return PTR_ERR(newstate);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500992 nfs4_close_state(&opendata->path, newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400993 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +0100994 return 0;
995}
996
997static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
998{
Trond Myklebust864472e2006-01-03 09:55:15 +0100999 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001000 int ret;
1001
1002 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001003 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001004 smp_rmb();
1005 if (state->n_rdwr != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001006 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001007 if (ret != 0)
1008 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001009 if (newstate != state)
1010 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001011 }
1012 if (state->n_wronly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001013 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001014 if (ret != 0)
1015 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001016 if (newstate != state)
1017 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001018 }
1019 if (state->n_rdonly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001020 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001021 if (ret != 0)
1022 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001023 if (newstate != state)
1024 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001025 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001026 /*
1027 * We may have performed cached opens for all three recoveries.
1028 * Check if we need to update the current stateid.
1029 */
1030 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1031 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001032 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001033 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1034 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001035 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001036 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001037 return 0;
1038}
1039
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040/*
1041 * OPEN_RECLAIM:
1042 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001044static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001046 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001047 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001048 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 int status;
1050
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001051 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1052 if (IS_ERR(opendata))
1053 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001054 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1055 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001056 rcu_read_lock();
1057 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001058 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001059 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001060 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001061 opendata->o_arg.u.delegation_type = delegation_type;
1062 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001063 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 return status;
1065}
1066
Trond Myklebust539cd032007-06-05 11:46:42 -04001067static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068{
1069 struct nfs_server *server = NFS_SERVER(state->inode);
1070 struct nfs4_exception exception = { };
1071 int err;
1072 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001073 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001074 if (err != -NFS4ERR_DELAY)
1075 break;
1076 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 } while (exception.retry);
1078 return err;
1079}
1080
Trond Myklebust864472e2006-01-03 09:55:15 +01001081static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1082{
1083 struct nfs_open_context *ctx;
1084 int ret;
1085
1086 ctx = nfs4_state_find_open_context(state);
1087 if (IS_ERR(ctx))
1088 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001089 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001090 put_nfs_open_context(ctx);
1091 return ret;
1092}
1093
Trond Myklebust13437e12007-07-06 15:10:43 -04001094static 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 -07001095{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001096 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001097 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001099 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1100 if (IS_ERR(opendata))
1101 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001102 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -04001103 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001104 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +01001105 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001106 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001107 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108}
1109
Trond Myklebust13437e12007-07-06 15:10:43 -04001110int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111{
1112 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001113 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 int err;
1115 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001116 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 switch (err) {
1118 case 0:
1119 return err;
1120 case -NFS4ERR_STALE_CLIENTID:
1121 case -NFS4ERR_STALE_STATEID:
1122 case -NFS4ERR_EXPIRED:
1123 /* Don't recall a delegation if it was lost */
David Howells7539bba2006-08-22 20:06:09 -04001124 nfs4_schedule_state_recovery(server->nfs_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 return err;
1126 }
1127 err = nfs4_handle_exception(server, err, &exception);
1128 } while (exception.retry);
1129 return err;
1130}
1131
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001132static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1133{
1134 struct nfs4_opendata *data = calldata;
1135
1136 data->rpc_status = task->tk_status;
1137 if (RPC_ASSASSINATED(task))
1138 return;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001139 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001140 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1141 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -05001142 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001143 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001144 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001145 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001146}
1147
1148static void nfs4_open_confirm_release(void *calldata)
1149{
1150 struct nfs4_opendata *data = calldata;
1151 struct nfs4_state *state = NULL;
1152
1153 /* If this request hasn't been cancelled, do nothing */
1154 if (data->cancelled == 0)
1155 goto out_free;
1156 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001157 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001158 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001159 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001160 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001161 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001162out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001163 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001164}
1165
1166static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001167 .rpc_call_done = nfs4_open_confirm_done,
1168 .rpc_release = nfs4_open_confirm_release,
1169};
1170
1171/*
1172 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1173 */
1174static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1175{
1176 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1177 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001178 struct rpc_message msg = {
1179 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1180 .rpc_argp = &data->c_arg,
1181 .rpc_resp = &data->c_res,
1182 .rpc_cred = data->owner->so_cred,
1183 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001184 struct rpc_task_setup task_setup_data = {
1185 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001186 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001187 .callback_ops = &nfs4_open_confirm_ops,
1188 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001189 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001190 .flags = RPC_TASK_ASYNC,
1191 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 int status;
1193
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001194 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001195 data->rpc_done = 0;
1196 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001197 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001198 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001199 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001200 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001201 status = nfs4_wait_for_completion_rpc_task(task);
1202 if (status != 0) {
1203 data->cancelled = 1;
1204 smp_wmb();
1205 } else
1206 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001207 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 return status;
1209}
1210
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001211static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001213 struct nfs4_opendata *data = calldata;
1214 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001215
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001216 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1217 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001218 /*
1219 * Check if we still need to send an OPEN call, or if we can use
1220 * a delegation instead.
1221 */
1222 if (data->state != NULL) {
1223 struct nfs_delegation *delegation;
1224
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001225 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001226 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001227 rcu_read_lock();
1228 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1229 if (delegation != NULL &&
Trond Myklebust15c831b2008-12-23 15:21:39 -05001230 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001231 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001232 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001233 }
1234 rcu_read_unlock();
1235 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001236 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001237 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001238 data->o_arg.clientid = sp->so_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001239 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001240 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001241 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1242 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001243 data->timestamp = jiffies;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001244 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001245 return;
1246out_no_action:
1247 task->tk_action = NULL;
1248
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001249}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001251static void nfs4_open_done(struct rpc_task *task, void *calldata)
1252{
1253 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001255 data->rpc_status = task->tk_status;
1256 if (RPC_ASSASSINATED(task))
1257 return;
1258 if (task->tk_status == 0) {
1259 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001260 case S_IFREG:
1261 break;
1262 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001263 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001264 break;
1265 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001266 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001267 break;
1268 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001269 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001270 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001271 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001272 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1273 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001274 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001275 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001276}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001277
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001278static void nfs4_open_release(void *calldata)
1279{
1280 struct nfs4_opendata *data = calldata;
1281 struct nfs4_state *state = NULL;
1282
1283 /* If this request hasn't been cancelled, do nothing */
1284 if (data->cancelled == 0)
1285 goto out_free;
1286 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001287 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001288 goto out_free;
1289 /* In case we need an open_confirm, no cleanup! */
1290 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1291 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001292 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001293 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001294 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001295out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001296 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001297}
1298
1299static const struct rpc_call_ops nfs4_open_ops = {
1300 .rpc_call_prepare = nfs4_open_prepare,
1301 .rpc_call_done = nfs4_open_done,
1302 .rpc_release = nfs4_open_release,
1303};
1304
1305/*
1306 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1307 */
1308static int _nfs4_proc_open(struct nfs4_opendata *data)
1309{
1310 struct inode *dir = data->dir->d_inode;
1311 struct nfs_server *server = NFS_SERVER(dir);
1312 struct nfs_openargs *o_arg = &data->o_arg;
1313 struct nfs_openres *o_res = &data->o_res;
1314 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001315 struct rpc_message msg = {
1316 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1317 .rpc_argp = o_arg,
1318 .rpc_resp = o_res,
1319 .rpc_cred = data->owner->so_cred,
1320 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001321 struct rpc_task_setup task_setup_data = {
1322 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001323 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001324 .callback_ops = &nfs4_open_ops,
1325 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001326 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001327 .flags = RPC_TASK_ASYNC,
1328 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001329 int status;
1330
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001331 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001332 data->rpc_done = 0;
1333 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001334 data->cancelled = 0;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001335 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001336 if (IS_ERR(task))
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001337 return PTR_ERR(task);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001338 status = nfs4_wait_for_completion_rpc_task(task);
1339 if (status != 0) {
1340 data->cancelled = 1;
1341 smp_wmb();
1342 } else
1343 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001344 rpc_put_task(task);
Trond Myklebust3e309912007-07-07 13:19:59 -04001345 if (status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001346 return status;
1347
Trond Myklebust99367812007-07-17 21:52:41 -04001348 if (o_res->fh.size == 0)
1349 _nfs4_proc_lookup(dir, o_arg->name, &o_res->fh, o_res->f_attr);
1350
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001351 if (o_arg->open_flags & O_CREAT) {
1352 update_changeattr(dir, &o_res->cinfo);
1353 nfs_post_op_update_inode(dir, o_res->dir_attr);
1354 } else
1355 nfs_refresh_inode(dir, o_res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001357 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001359 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 }
1361 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001362 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001363 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364}
1365
Trond Myklebust10afec92007-05-14 17:16:04 -04001366static int nfs4_recover_expired_lease(struct nfs_server *server)
Trond Myklebust58d97142006-01-03 09:55:24 +01001367{
David Howells7539bba2006-08-22 20:06:09 -04001368 struct nfs_client *clp = server->nfs_client;
Trond Myklebust6b309542006-09-14 14:03:14 -04001369 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001370
Trond Myklebust6b309542006-09-14 14:03:14 -04001371 for (;;) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001372 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001373 if (ret != 0)
1374 return ret;
Trond Myklebuste598d842008-12-23 15:21:42 -05001375 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1376 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001377 break;
Trond Myklebust58d97142006-01-03 09:55:24 +01001378 nfs4_schedule_state_recovery(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001379 }
1380 return 0;
Trond Myklebust58d97142006-01-03 09:55:24 +01001381}
1382
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383/*
1384 * OPEN_EXPIRED:
1385 * reclaim state on the server after a network partition.
1386 * Assumes caller holds the appropriate lock
1387 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001388static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001390 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001391 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001393 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1394 if (IS_ERR(opendata))
1395 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001396 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001397 if (ret == -ESTALE)
Trond Myklebust539cd032007-06-05 11:46:42 -04001398 d_drop(ctx->path.dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001399 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001400 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401}
1402
Trond Myklebust539cd032007-06-05 11:46:42 -04001403static inline int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001404{
Trond Myklebust539cd032007-06-05 11:46:42 -04001405 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001406 struct nfs4_exception exception = { };
1407 int err;
1408
1409 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001410 err = _nfs4_open_expired(ctx, state);
Trond Myklebust027b6ca2008-12-23 16:04:13 -05001411 if (err != -NFS4ERR_DELAY)
1412 break;
1413 nfs4_handle_exception(server, err, &exception);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001414 } while (exception.retry);
1415 return err;
1416}
1417
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1419{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001421 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Trond Myklebust864472e2006-01-03 09:55:15 +01001423 ctx = nfs4_state_find_open_context(state);
1424 if (IS_ERR(ctx))
1425 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001426 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001427 put_nfs_open_context(ctx);
1428 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429}
1430
1431/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001432 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1433 * fields corresponding to attributes that were used to store the verifier.
1434 * Make sure we clobber those fields in the later setattr call
1435 */
1436static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1437{
1438 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1439 !(sattr->ia_valid & ATTR_ATIME_SET))
1440 sattr->ia_valid |= ATTR_ATIME;
1441
1442 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1443 !(sattr->ia_valid & ATTR_MTIME_SET))
1444 sattr->ia_valid |= ATTR_MTIME;
1445}
1446
1447/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001448 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001450static 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 -07001451{
1452 struct nfs4_state_owner *sp;
1453 struct nfs4_state *state = NULL;
1454 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001455 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001456 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
1458 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 status = -ENOMEM;
1460 if (!(sp = nfs4_get_state_owner(server, cred))) {
1461 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1462 goto out_err;
1463 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001464 status = nfs4_recover_expired_lease(server);
1465 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001466 goto err_put_state_owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001467 if (path->dentry->d_inode != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001468 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001469 status = -ENOMEM;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001470 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001471 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001472 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
Trond Myklebustaac00a82007-07-05 19:02:21 -04001474 if (path->dentry->d_inode != NULL)
1475 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1476
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001477 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001479 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001481 if (opendata->o_arg.open_flags & O_EXCL)
1482 nfs4_exclusive_attrset(opendata, sattr);
1483
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001484 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001485 status = PTR_ERR(state);
1486 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001487 goto err_opendata_put;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001488 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 *res = state;
1491 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001492err_opendata_put:
1493 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001494err_put_state_owner:
1495 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 *res = NULL;
1498 return status;
1499}
1500
1501
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001502static 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 -07001503{
1504 struct nfs4_exception exception = { };
1505 struct nfs4_state *res;
1506 int status;
1507
1508 do {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001509 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 if (status == 0)
1511 break;
1512 /* NOTE: BAD_SEQID means the server and client disagree about the
1513 * book-keeping w.r.t. state-changing operations
1514 * (OPEN/CLOSE/LOCK/LOCKU...)
1515 * It is actually a sign of a bug on the client or on the server.
1516 *
1517 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001518 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 * have unhashed the old state_owner for us, and that we can
1520 * therefore safely retry using a new one. We should still warn
1521 * the user though...
1522 */
1523 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001524 printk(KERN_WARNING "NFS: v4 server %s "
1525 " returned a bad sequence-id error!\n",
1526 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 exception.retry = 1;
1528 continue;
1529 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001530 /*
1531 * BAD_STATEID on OPEN means that the server cancelled our
1532 * state before it received the OPEN_CONFIRM.
1533 * Recover by retrying the request as per the discussion
1534 * on Page 181 of RFC3530.
1535 */
1536 if (status == -NFS4ERR_BAD_STATEID) {
1537 exception.retry = 1;
1538 continue;
1539 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001540 if (status == -EAGAIN) {
1541 /* We must have found a delegation */
1542 exception.retry = 1;
1543 continue;
1544 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1546 status, &exception));
1547 } while (exception.retry);
1548 return res;
1549}
1550
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001551static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1552 struct nfs_fattr *fattr, struct iattr *sattr,
1553 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001555 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001557 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 .iap = sattr,
1559 .server = server,
1560 .bitmask = server->attr_bitmask,
1561 };
1562 struct nfs_setattrres res = {
1563 .fattr = fattr,
1564 .server = server,
1565 };
1566 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001567 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1568 .rpc_argp = &arg,
1569 .rpc_resp = &res,
1570 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001572 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001573 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
Trond Myklebust0e574af2005-10-27 22:12:38 -04001575 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001577 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1578 /* Use that stateid */
1579 } else if (state != NULL) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001580 nfs4_copy_stateid(&arg.stateid, state, current->files);
1581 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1583
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001584 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001585 if (status == 0 && state != NULL)
1586 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001587 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588}
1589
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001590static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1591 struct nfs_fattr *fattr, struct iattr *sattr,
1592 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001594 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 struct nfs4_exception exception = { };
1596 int err;
1597 do {
1598 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001599 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 &exception);
1601 } while (exception.retry);
1602 return err;
1603}
1604
1605struct nfs4_closedata {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001606 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 struct inode *inode;
1608 struct nfs4_state *state;
1609 struct nfs_closeargs arg;
1610 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001611 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001612 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613};
1614
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001615static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001616{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001617 struct nfs4_closedata *calldata = data;
1618 struct nfs4_state_owner *sp = calldata->state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -07001619
1620 nfs4_put_open_state(calldata->state);
1621 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001622 nfs4_put_state_owner(sp);
Jan Blunck31f31db12008-05-02 13:42:45 -07001623 path_put(&calldata->path);
Trond Myklebust95121352005-10-18 14:20:12 -07001624 kfree(calldata);
1625}
1626
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001627static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001629 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 struct nfs_server *server = NFS_SERVER(calldata->inode);
1632
Andy Adamson19ddab02009-04-01 09:22:20 -04001633 nfs4_sequence_done(server, &calldata->res.seq_res, task->tk_status);
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001634 if (RPC_ASSASSINATED(task))
1635 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 /* hmm. we are done with the inode, and in the process of freeing
1637 * the state_owner. we keep this around to process errors
1638 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 switch (task->tk_status) {
1640 case 0:
Trond Myklebust45328c32007-07-26 17:47:34 -04001641 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001642 renew_lease(server, calldata->timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 break;
1644 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001645 case -NFS4ERR_OLD_STATEID:
1646 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001648 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001649 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001651 if (nfs4_async_handle_error(task, server, state) == -EAGAIN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 rpc_restart_call(task);
1653 return;
1654 }
1655 }
Andy Adamson19ddab02009-04-01 09:22:20 -04001656 nfs4_sequence_free_slot(server->nfs_client, &calldata->res.seq_res);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001657 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658}
1659
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001660static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001662 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001663 struct nfs4_state *state = calldata->state;
Trond Myklebust003707c2007-07-05 18:07:55 -04001664 int clear_rd, clear_wr, clear_rdwr;
Trond Myklebust95121352005-10-18 14:20:12 -07001665
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001666 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001667 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001668
Trond Myklebust003707c2007-07-05 18:07:55 -04001669 clear_rd = clear_wr = clear_rdwr = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001670 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001671 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001672 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001673 if (state->n_rdonly == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001674 clear_rd |= test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1675 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1676 }
1677 if (state->n_wronly == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001678 clear_wr |= test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1679 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1680 }
Trond Myklebuste7616922006-01-03 09:55:13 +01001681 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001682 spin_unlock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001683 if (!clear_rd && !clear_wr && !clear_rdwr) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001684 /* Note: exit _without_ calling nfs4_close_done */
1685 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001686 return;
1687 }
Trond Myklebust516a6af2005-10-27 22:12:41 -04001688 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust45328c32007-07-26 17:47:34 -04001689 if (test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001690 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001691 calldata->arg.fmode = FMODE_READ;
Trond Myklebust45328c32007-07-26 17:47:34 -04001692 } else if (test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001693 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001694 calldata->arg.fmode = FMODE_WRITE;
Trond Myklebust45328c32007-07-26 17:47:34 -04001695 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001696 calldata->timestamp = jiffies;
Andy Adamson19ddab02009-04-01 09:22:20 -04001697 if (nfs4_setup_sequence((NFS_SERVER(calldata->inode))->nfs_client,
1698 &calldata->arg.seq_args, &calldata->res.seq_res,
1699 1, task))
1700 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001701 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702}
1703
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001704static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001705 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001706 .rpc_call_done = nfs4_close_done,
1707 .rpc_release = nfs4_free_closedata,
1708};
1709
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710/*
1711 * It is possible for data to be read/written from a mem-mapped file
1712 * after the sys_close call (which hits the vfs layer as a flush).
1713 * This means that we can't safely call nfsv4 close on a file until
1714 * the inode is cleared. This in turn means that we are not good
1715 * NFSv4 citizens - we do not indicate to the server to update the file's
1716 * share state even when we are done with one of the three share
1717 * stateid's in the inode.
1718 *
1719 * NOTE: Caller must be holding the sp->so_owner semaphore!
1720 */
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001721int nfs4_do_close(struct path *path, struct nfs4_state *state, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001723 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04001725 struct nfs4_state_owner *sp = state->owner;
1726 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001727 struct rpc_message msg = {
1728 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1729 .rpc_cred = state->owner->so_cred,
1730 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001731 struct rpc_task_setup task_setup_data = {
1732 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001733 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001734 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05001735 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001736 .flags = RPC_TASK_ASYNC,
1737 };
Trond Myklebust95121352005-10-18 14:20:12 -07001738 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
Andy Adamson19ddab02009-04-01 09:22:20 -04001740 calldata = kzalloc(sizeof(*calldata), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07001742 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001743 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001745 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04001746 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 /* Serialization for the sequence id */
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001748 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001749 if (calldata->arg.seqid == NULL)
1750 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001751 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04001752 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001753 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04001754 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001755 calldata->res.server = server;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04001756 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001757 calldata->path.mnt = mntget(path->mnt);
1758 calldata->path.dentry = dget(path->dentry);
Trond Myklebust95121352005-10-18 14:20:12 -07001759
Trond Myklebust5138fde2007-07-14 15:40:01 -04001760 msg.rpc_argp = &calldata->arg,
1761 msg.rpc_resp = &calldata->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001762 task_setup_data.callback_data = calldata;
1763 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001764 if (IS_ERR(task))
1765 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001766 status = 0;
1767 if (wait)
1768 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001769 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001770 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07001771out_free_calldata:
1772 kfree(calldata);
1773out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04001774 nfs4_put_open_state(state);
1775 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07001776 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777}
1778
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001779static 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 -07001780{
1781 struct file *filp;
Trond Myklebust1b45c462007-07-03 13:04:56 -04001782 int ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001783
Trond Myklebust1b45c462007-07-03 13:04:56 -04001784 /* If the open_intent is for execute, we have an extra check to make */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001785 if (fmode & FMODE_EXEC) {
Trond Myklebustaf22f942007-08-10 17:45:10 -04001786 ret = nfs_may_open(state->inode,
Trond Myklebust1b45c462007-07-03 13:04:56 -04001787 state->owner->so_cred,
1788 nd->intent.open.flags);
1789 if (ret < 0)
1790 goto out_close;
1791 }
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001792 filp = lookup_instantiate_filp(nd, path->dentry, NULL);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001793 if (!IS_ERR(filp)) {
1794 struct nfs_open_context *ctx;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04001795 ctx = nfs_file_open_context(filp);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001796 ctx->state = state;
Trond Myklebust95cf9592006-04-18 13:14:06 -04001797 return 0;
1798 }
Trond Myklebust1b45c462007-07-03 13:04:56 -04001799 ret = PTR_ERR(filp);
1800out_close:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001801 nfs4_close_sync(path, state, fmode & (FMODE_READ|FMODE_WRITE));
Trond Myklebust1b45c462007-07-03 13:04:56 -04001802 return ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001803}
1804
1805struct dentry *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1807{
Trond Myklebustad389da2007-06-05 12:30:00 -04001808 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08001809 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001810 .dentry = dentry,
1811 };
Jan Blunck4ac91372008-02-14 19:34:32 -08001812 struct dentry *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 struct iattr attr;
1814 struct rpc_cred *cred;
1815 struct nfs4_state *state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001816 struct dentry *res;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001817 fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
1819 if (nd->flags & LOOKUP_CREATE) {
1820 attr.ia_mode = nd->intent.open.create_mode;
1821 attr.ia_valid = ATTR_MODE;
1822 if (!IS_POSIXACL(dir))
Al Viroce3b0f82009-03-29 19:08:22 -04001823 attr.ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 } else {
1825 attr.ia_valid = 0;
1826 BUG_ON(nd->intent.open.flags & O_CREAT);
1827 }
1828
Trond Myklebust98a8e322008-03-12 12:25:28 -04001829 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 if (IS_ERR(cred))
Trond Myklebust02a913a2005-10-18 14:20:17 -07001831 return (struct dentry *)cred;
Trond Myklebust565277f2007-10-15 18:17:53 -04001832 parent = dentry->d_parent;
1833 /* Protect against concurrent sillydeletes */
1834 nfs_block_sillyrename(parent);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001835 state = nfs4_do_open(dir, &path, fmode, nd->intent.open.flags, &attr, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001837 if (IS_ERR(state)) {
Trond Myklebustd75340c2007-10-01 21:42:01 -04001838 if (PTR_ERR(state) == -ENOENT) {
Trond Myklebust02a913a2005-10-18 14:20:17 -07001839 d_add(dentry, NULL);
Trond Myklebustd75340c2007-10-01 21:42:01 -04001840 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1841 }
Trond Myklebust565277f2007-10-15 18:17:53 -04001842 nfs_unblock_sillyrename(parent);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001843 return (struct dentry *)state;
1844 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001845 res = d_add_unique(dentry, igrab(state->inode));
Trond Myklebust02a913a2005-10-18 14:20:17 -07001846 if (res != NULL)
Trond Myklebustdeee9362007-08-27 11:33:00 -04001847 path.dentry = res;
Trond Myklebustd75340c2007-10-01 21:42:01 -04001848 nfs_set_verifier(path.dentry, nfs_save_change_attribute(dir));
Trond Myklebust565277f2007-10-15 18:17:53 -04001849 nfs_unblock_sillyrename(parent);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001850 nfs4_intent_set_file(nd, &path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001851 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852}
1853
1854int
Trond Myklebust02a913a2005-10-18 14:20:17 -07001855nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856{
Trond Myklebustad389da2007-06-05 12:30:00 -04001857 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08001858 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001859 .dentry = dentry,
1860 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 struct rpc_cred *cred;
1862 struct nfs4_state *state;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001863 fmode_t fmode = openflags & (FMODE_READ | FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
Trond Myklebust98a8e322008-03-12 12:25:28 -04001865 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 if (IS_ERR(cred))
1867 return PTR_ERR(cred);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001868 state = nfs4_do_open(dir, &path, fmode, openflags, NULL, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001870 if (IS_ERR(state)) {
1871 switch (PTR_ERR(state)) {
1872 case -EPERM:
1873 case -EACCES:
1874 case -EDQUOT:
1875 case -ENOSPC:
1876 case -EROFS:
1877 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
1878 return 1;
Chuck Leverb87c0ad2006-10-19 23:28:42 -07001879 default:
1880 goto out_drop;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001881 }
Trond Myklebust02a913a2005-10-18 14:20:17 -07001882 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001883 if (state->inode == dentry->d_inode) {
Trond Myklebustd75340c2007-10-01 21:42:01 -04001884 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001885 nfs4_intent_set_file(nd, &path, state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 return 1;
1887 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001888 nfs4_close_sync(&path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001889out_drop:
1890 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 return 0;
1892}
1893
Trond Myklebust7fe5c392009-03-19 15:35:50 -04001894void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
1895{
1896 if (ctx->state == NULL)
1897 return;
1898 if (is_sync)
1899 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
1900 else
1901 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
1902}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
1904static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1905{
Benny Halevy43652ad2009-04-01 09:21:54 -04001906 struct nfs4_server_caps_arg args = {
1907 .fhandle = fhandle,
1908 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 struct nfs4_server_caps_res res = {};
1910 struct rpc_message msg = {
1911 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04001912 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 .rpc_resp = &res,
1914 };
1915 int status;
1916
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001917 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 if (status == 0) {
1919 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
1920 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
1921 server->caps |= NFS_CAP_ACLS;
1922 if (res.has_links != 0)
1923 server->caps |= NFS_CAP_HARDLINKS;
1924 if (res.has_symlinks != 0)
1925 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebusta65318b2009-03-11 14:10:28 -04001926 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
1927 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
1928 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 server->acl_bitmask = res.acl_bitmask;
1930 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001931
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 return status;
1933}
1934
Trond Myklebust55a97592006-06-09 09:34:19 -04001935int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936{
1937 struct nfs4_exception exception = { };
1938 int err;
1939 do {
1940 err = nfs4_handle_exception(server,
1941 _nfs4_server_capabilities(server, fhandle),
1942 &exception);
1943 } while (exception.retry);
1944 return err;
1945}
1946
1947static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1948 struct nfs_fsinfo *info)
1949{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 struct nfs4_lookup_root_arg args = {
1951 .bitmask = nfs4_fattr_bitmap,
1952 };
1953 struct nfs4_lookup_res res = {
1954 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04001955 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 .fh = fhandle,
1957 };
1958 struct rpc_message msg = {
1959 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
1960 .rpc_argp = &args,
1961 .rpc_resp = &res,
1962 };
Trond Myklebust0e574af2005-10-27 22:12:38 -04001963 nfs_fattr_init(info->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001964 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965}
1966
1967static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1968 struct nfs_fsinfo *info)
1969{
1970 struct nfs4_exception exception = { };
1971 int err;
1972 do {
1973 err = nfs4_handle_exception(server,
1974 _nfs4_lookup_root(server, fhandle, info),
1975 &exception);
1976 } while (exception.retry);
1977 return err;
1978}
1979
David Howells54ceac42006-08-22 20:06:13 -04001980/*
1981 * get the file handle for the "/" directory on the server
1982 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04001984 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 int status;
1987
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 status = nfs4_lookup_root(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 if (status == 0)
1990 status = nfs4_server_capabilities(server, fhandle);
1991 if (status == 0)
1992 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08001993 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994}
1995
Manoj Naik6b97fd32006-06-09 09:34:29 -04001996/*
1997 * Get locations and (maybe) other attributes of a referral.
1998 * Note that we'll actually follow the referral later when
1999 * we detect fsid mismatch in inode revalidation
2000 */
Trond Myklebust56659e92007-07-17 21:52:39 -04002001static 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 -04002002{
2003 int status = -ENOMEM;
2004 struct page *page = NULL;
2005 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002006
2007 page = alloc_page(GFP_KERNEL);
2008 if (page == NULL)
2009 goto out;
2010 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2011 if (locations == NULL)
2012 goto out;
2013
Trond Myklebustc228fd32007-01-13 02:28:11 -05002014 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002015 if (status != 0)
2016 goto out;
2017 /* Make sure server returned a different fsid for the referral */
2018 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -07002019 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 -04002020 status = -EIO;
2021 goto out;
2022 }
2023
2024 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2025 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2026 if (!fattr->mode)
2027 fattr->mode = S_IFDIR;
2028 memset(fhandle, 0, sizeof(struct nfs_fh));
2029out:
2030 if (page)
2031 __free_page(page);
2032 if (locations)
2033 kfree(locations);
2034 return status;
2035}
2036
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2038{
2039 struct nfs4_getattr_arg args = {
2040 .fh = fhandle,
2041 .bitmask = server->attr_bitmask,
2042 };
2043 struct nfs4_getattr_res res = {
2044 .fattr = fattr,
2045 .server = server,
2046 };
2047 struct rpc_message msg = {
2048 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2049 .rpc_argp = &args,
2050 .rpc_resp = &res,
2051 };
2052
Trond Myklebust0e574af2005-10-27 22:12:38 -04002053 nfs_fattr_init(fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002054 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055}
2056
2057static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2058{
2059 struct nfs4_exception exception = { };
2060 int err;
2061 do {
2062 err = nfs4_handle_exception(server,
2063 _nfs4_proc_getattr(server, fhandle, fattr),
2064 &exception);
2065 } while (exception.retry);
2066 return err;
2067}
2068
2069/*
2070 * The file is not closed if it is opened due to the a request to change
2071 * the size of the file. The open call will not be needed once the
2072 * VFS layer lookup-intents are implemented.
2073 *
2074 * Close is called when the inode is destroyed.
2075 * If we haven't opened the file for O_WRONLY, we
2076 * need to in the size_change case to obtain a stateid.
2077 *
2078 * Got race?
2079 * Because OPEN is always done by name in nfsv4, it is
2080 * possible that we opened a different file by the same
2081 * name. We can recognize this race condition, but we
2082 * can't do anything about it besides returning an error.
2083 *
2084 * This will be fixed with VFS changes (lookup-intent).
2085 */
2086static int
2087nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2088 struct iattr *sattr)
2089{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002090 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002091 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002092 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 int status;
2094
Trond Myklebust0e574af2005-10-27 22:12:38 -04002095 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
Trond Myklebustd5308382005-11-04 15:33:38 -05002097 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002098 if (sattr->ia_valid & ATTR_FILE) {
2099 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002100
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002101 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002102 if (ctx) {
2103 cred = ctx->cred;
2104 state = ctx->state;
2105 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002106 }
2107
2108 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002109 if (status == 0)
2110 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 return status;
2112}
2113
Trond Myklebust56659e92007-07-17 21:52:39 -04002114static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
2115 const struct qstr *name, struct nfs_fh *fhandle,
David Howells2b3de442006-08-22 20:06:09 -04002116 struct nfs_fattr *fattr)
2117{
2118 int status;
2119 struct nfs4_lookup_arg args = {
2120 .bitmask = server->attr_bitmask,
2121 .dir_fh = dirfh,
2122 .name = name,
2123 };
2124 struct nfs4_lookup_res res = {
2125 .server = server,
2126 .fattr = fattr,
2127 .fh = fhandle,
2128 };
2129 struct rpc_message msg = {
2130 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2131 .rpc_argp = &args,
2132 .rpc_resp = &res,
2133 };
2134
2135 nfs_fattr_init(fattr);
2136
2137 dprintk("NFS call lookupfh %s\n", name->name);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002138 status = nfs4_call_sync(server, &msg, &args, &res, 0);
David Howells2b3de442006-08-22 20:06:09 -04002139 dprintk("NFS reply lookupfh: %d\n", status);
David Howells2b3de442006-08-22 20:06:09 -04002140 return status;
2141}
2142
2143static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2144 struct qstr *name, struct nfs_fh *fhandle,
2145 struct nfs_fattr *fattr)
2146{
2147 struct nfs4_exception exception = { };
2148 int err;
2149 do {
Trond Myklebust4e56e082007-07-01 18:13:52 -04002150 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2151 /* FIXME: !!!! */
2152 if (err == -NFS4ERR_MOVED) {
2153 err = -EREMOTE;
2154 break;
2155 }
2156 err = nfs4_handle_exception(server, err, &exception);
David Howells2b3de442006-08-22 20:06:09 -04002157 } while (exception.retry);
2158 return err;
2159}
2160
Trond Myklebust56659e92007-07-17 21:52:39 -04002161static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2163{
Trond Myklebust4e56e082007-07-01 18:13:52 -04002164 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
2166 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust4e56e082007-07-01 18:13:52 -04002167 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002168 if (status == -NFS4ERR_MOVED)
2169 status = nfs4_get_referral(dir, name, fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 dprintk("NFS reply lookup: %d\n", status);
2171 return status;
2172}
2173
2174static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2175{
2176 struct nfs4_exception exception = { };
2177 int err;
2178 do {
2179 err = nfs4_handle_exception(NFS_SERVER(dir),
2180 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2181 &exception);
2182 } while (exception.retry);
2183 return err;
2184}
2185
2186static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2187{
Trond Myklebust76b32992007-08-10 17:45:11 -04002188 struct nfs_server *server = NFS_SERVER(inode);
2189 struct nfs_fattr fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 struct nfs4_accessargs args = {
2191 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04002192 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002194 struct nfs4_accessres res = {
2195 .server = server,
2196 .fattr = &fattr,
2197 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 struct rpc_message msg = {
2199 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2200 .rpc_argp = &args,
2201 .rpc_resp = &res,
2202 .rpc_cred = entry->cred,
2203 };
2204 int mode = entry->mask;
2205 int status;
2206
2207 /*
2208 * Determine which access bits we want to ask for...
2209 */
2210 if (mode & MAY_READ)
2211 args.access |= NFS4_ACCESS_READ;
2212 if (S_ISDIR(inode->i_mode)) {
2213 if (mode & MAY_WRITE)
2214 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2215 if (mode & MAY_EXEC)
2216 args.access |= NFS4_ACCESS_LOOKUP;
2217 } else {
2218 if (mode & MAY_WRITE)
2219 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2220 if (mode & MAY_EXEC)
2221 args.access |= NFS4_ACCESS_EXECUTE;
2222 }
Trond Myklebust76b32992007-08-10 17:45:11 -04002223 nfs_fattr_init(&fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002224 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 if (!status) {
2226 entry->mask = 0;
2227 if (res.access & NFS4_ACCESS_READ)
2228 entry->mask |= MAY_READ;
2229 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2230 entry->mask |= MAY_WRITE;
2231 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2232 entry->mask |= MAY_EXEC;
Trond Myklebust76b32992007-08-10 17:45:11 -04002233 nfs_refresh_inode(inode, &fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 }
2235 return status;
2236}
2237
2238static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2239{
2240 struct nfs4_exception exception = { };
2241 int err;
2242 do {
2243 err = nfs4_handle_exception(NFS_SERVER(inode),
2244 _nfs4_proc_access(inode, entry),
2245 &exception);
2246 } while (exception.retry);
2247 return err;
2248}
2249
2250/*
2251 * TODO: For the time being, we don't try to get any attributes
2252 * along with any of the zero-copy operations READ, READDIR,
2253 * READLINK, WRITE.
2254 *
2255 * In the case of the first three, we want to put the GETATTR
2256 * after the read-type operation -- this is because it is hard
2257 * to predict the length of a GETATTR response in v4, and thus
2258 * align the READ data correctly. This means that the GETATTR
2259 * may end up partially falling into the page cache, and we should
2260 * shift it into the 'tail' of the xdr_buf before processing.
2261 * To do this efficiently, we need to know the total length
2262 * of data received, which doesn't seem to be available outside
2263 * of the RPC layer.
2264 *
2265 * In the case of WRITE, we also want to put the GETATTR after
2266 * the operation -- in this case because we want to make sure
2267 * we get the post-operation mtime and size. This means that
2268 * we can't use xdr_encode_pages() as written: we need a variant
2269 * of it which would leave room in the 'tail' iovec.
2270 *
2271 * Both of these changes to the XDR layer would in fact be quite
2272 * minor, but I decided to leave them for a subsequent patch.
2273 */
2274static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2275 unsigned int pgbase, unsigned int pglen)
2276{
2277 struct nfs4_readlink args = {
2278 .fh = NFS_FH(inode),
2279 .pgbase = pgbase,
2280 .pglen = pglen,
2281 .pages = &page,
2282 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002283 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 struct rpc_message msg = {
2285 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2286 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002287 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 };
2289
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002290 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291}
2292
2293static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2294 unsigned int pgbase, unsigned int pglen)
2295{
2296 struct nfs4_exception exception = { };
2297 int err;
2298 do {
2299 err = nfs4_handle_exception(NFS_SERVER(inode),
2300 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2301 &exception);
2302 } while (exception.retry);
2303 return err;
2304}
2305
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306/*
2307 * Got race?
2308 * We will need to arrange for the VFS layer to provide an atomic open.
2309 * Until then, this create/open method is prone to inefficiency and race
2310 * conditions due to the lookup, create, and open VFS calls from sys_open()
2311 * placed on the wire.
2312 *
2313 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2314 * The file will be opened again in the subsequent VFS open call
2315 * (nfs4_proc_file_open).
2316 *
2317 * The open for read will just hang around to be used by any process that
2318 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2319 */
2320
2321static int
2322nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebust02a913a2005-10-18 14:20:17 -07002323 int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324{
Trond Myklebustad389da2007-06-05 12:30:00 -04002325 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08002326 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04002327 .dentry = dentry,
2328 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 struct nfs4_state *state;
2330 struct rpc_cred *cred;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002331 fmode_t fmode = flags & (FMODE_READ | FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 int status = 0;
2333
Trond Myklebust98a8e322008-03-12 12:25:28 -04002334 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 if (IS_ERR(cred)) {
2336 status = PTR_ERR(cred);
2337 goto out;
2338 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002339 state = nfs4_do_open(dir, &path, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002340 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 if (IS_ERR(state)) {
2342 status = PTR_ERR(state);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002343 goto out_putcred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002345 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002346 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 if (flags & O_EXCL) {
2348 struct nfs_fattr fattr;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002349 status = nfs4_do_setattr(state->inode, cred, &fattr, sattr, state);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002350 if (status == 0)
Trond Myklebust65e43082005-08-16 11:49:44 -04002351 nfs_setattr_update_inode(state->inode, sattr);
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002352 nfs_post_op_update_inode(state->inode, &fattr);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002353 }
Trond Myklebustad389da2007-06-05 12:30:00 -04002354 if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002355 status = nfs4_intent_set_file(nd, &path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002356 else
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002357 nfs4_close_sync(&path, state, fmode);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002358out_putcred:
2359 put_rpccred(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360out:
2361 return status;
2362}
2363
2364static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2365{
Trond Myklebust16e42952005-10-27 22:12:44 -04002366 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002367 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002369 .name.len = name->len,
2370 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002371 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002373 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002374 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002375 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002377 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2378 .rpc_argp = &args,
2379 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 };
2381 int status;
2382
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002383 nfs_fattr_init(&res.dir_attr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002384 status = nfs4_call_sync(server, &msg, &args, &res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002385 if (status == 0) {
2386 update_changeattr(dir, &res.cinfo);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002387 nfs_post_op_update_inode(dir, &res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 return status;
2390}
2391
2392static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2393{
2394 struct nfs4_exception exception = { };
2395 int err;
2396 do {
2397 err = nfs4_handle_exception(NFS_SERVER(dir),
2398 _nfs4_proc_remove(dir, name),
2399 &exception);
2400 } while (exception.retry);
2401 return err;
2402}
2403
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002404static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002406 struct nfs_server *server = NFS_SERVER(dir);
2407 struct nfs_removeargs *args = msg->rpc_argp;
2408 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409
Trond Myklebusta65318b2009-03-11 14:10:28 -04002410 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002411 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413}
2414
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002415static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002417 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2418
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002419 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002420 return 0;
2421 update_changeattr(dir, &res->cinfo);
2422 nfs_post_op_update_inode(dir, &res->dir_attr);
2423 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424}
2425
2426static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2427 struct inode *new_dir, struct qstr *new_name)
2428{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002429 struct nfs_server *server = NFS_SERVER(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 struct nfs4_rename_arg arg = {
2431 .old_dir = NFS_FH(old_dir),
2432 .new_dir = NFS_FH(new_dir),
2433 .old_name = old_name,
2434 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002435 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 };
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002437 struct nfs_fattr old_fattr, new_fattr;
2438 struct nfs4_rename_res res = {
2439 .server = server,
2440 .old_fattr = &old_fattr,
2441 .new_fattr = &new_fattr,
2442 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 struct rpc_message msg = {
2444 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2445 .rpc_argp = &arg,
2446 .rpc_resp = &res,
2447 };
2448 int status;
2449
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002450 nfs_fattr_init(res.old_fattr);
2451 nfs_fattr_init(res.new_fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002452 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453
2454 if (!status) {
2455 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002456 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002458 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 }
2460 return status;
2461}
2462
2463static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2464 struct inode *new_dir, struct qstr *new_name)
2465{
2466 struct nfs4_exception exception = { };
2467 int err;
2468 do {
2469 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2470 _nfs4_proc_rename(old_dir, old_name,
2471 new_dir, new_name),
2472 &exception);
2473 } while (exception.retry);
2474 return err;
2475}
2476
2477static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2478{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002479 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 struct nfs4_link_arg arg = {
2481 .fh = NFS_FH(inode),
2482 .dir_fh = NFS_FH(dir),
2483 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002484 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002486 struct nfs_fattr fattr, dir_attr;
2487 struct nfs4_link_res res = {
2488 .server = server,
2489 .fattr = &fattr,
2490 .dir_attr = &dir_attr,
2491 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 struct rpc_message msg = {
2493 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2494 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002495 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 };
2497 int status;
2498
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002499 nfs_fattr_init(res.fattr);
2500 nfs_fattr_init(res.dir_attr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002501 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002502 if (!status) {
2503 update_changeattr(dir, &res.cinfo);
2504 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002505 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507
2508 return status;
2509}
2510
2511static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2512{
2513 struct nfs4_exception exception = { };
2514 int err;
2515 do {
2516 err = nfs4_handle_exception(NFS_SERVER(inode),
2517 _nfs4_proc_link(inode, dir, name),
2518 &exception);
2519 } while (exception.retry);
2520 return err;
2521}
2522
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002523struct nfs4_createdata {
2524 struct rpc_message msg;
2525 struct nfs4_create_arg arg;
2526 struct nfs4_create_res res;
2527 struct nfs_fh fh;
2528 struct nfs_fattr fattr;
2529 struct nfs_fattr dir_fattr;
2530};
2531
2532static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2533 struct qstr *name, struct iattr *sattr, u32 ftype)
2534{
2535 struct nfs4_createdata *data;
2536
2537 data = kzalloc(sizeof(*data), GFP_KERNEL);
2538 if (data != NULL) {
2539 struct nfs_server *server = NFS_SERVER(dir);
2540
2541 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2542 data->msg.rpc_argp = &data->arg;
2543 data->msg.rpc_resp = &data->res;
2544 data->arg.dir_fh = NFS_FH(dir);
2545 data->arg.server = server;
2546 data->arg.name = name;
2547 data->arg.attrs = sattr;
2548 data->arg.ftype = ftype;
2549 data->arg.bitmask = server->attr_bitmask;
2550 data->res.server = server;
2551 data->res.fh = &data->fh;
2552 data->res.fattr = &data->fattr;
2553 data->res.dir_fattr = &data->dir_fattr;
2554 nfs_fattr_init(data->res.fattr);
2555 nfs_fattr_init(data->res.dir_fattr);
2556 }
2557 return data;
2558}
2559
2560static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2561{
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002562 int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2563 &data->arg, &data->res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002564 if (status == 0) {
2565 update_changeattr(dir, &data->res.dir_cinfo);
2566 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2567 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2568 }
2569 return status;
2570}
2571
2572static void nfs4_free_createdata(struct nfs4_createdata *data)
2573{
2574 kfree(data);
2575}
2576
Chuck Lever4f390c12006-08-22 20:06:22 -04002577static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002578 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002580 struct nfs4_createdata *data;
2581 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582
Chuck Lever94a6d752006-08-22 20:06:23 -04002583 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002584 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002585
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002586 status = -ENOMEM;
2587 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2588 if (data == NULL)
2589 goto out;
2590
2591 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2592 data->arg.u.symlink.pages = &page;
2593 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002595 status = nfs4_do_create(dir, dentry, data);
2596
2597 nfs4_free_createdata(data);
2598out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 return status;
2600}
2601
Chuck Lever4f390c12006-08-22 20:06:22 -04002602static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002603 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604{
2605 struct nfs4_exception exception = { };
2606 int err;
2607 do {
2608 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002609 _nfs4_proc_symlink(dir, dentry, page,
2610 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 &exception);
2612 } while (exception.retry);
2613 return err;
2614}
2615
2616static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2617 struct iattr *sattr)
2618{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002619 struct nfs4_createdata *data;
2620 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002622 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2623 if (data == NULL)
2624 goto out;
2625
2626 status = nfs4_do_create(dir, dentry, data);
2627
2628 nfs4_free_createdata(data);
2629out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 return status;
2631}
2632
2633static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2634 struct iattr *sattr)
2635{
2636 struct nfs4_exception exception = { };
2637 int err;
2638 do {
2639 err = nfs4_handle_exception(NFS_SERVER(dir),
2640 _nfs4_proc_mkdir(dir, dentry, sattr),
2641 &exception);
2642 } while (exception.retry);
2643 return err;
2644}
2645
2646static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2647 u64 cookie, struct page *page, unsigned int count, int plus)
2648{
2649 struct inode *dir = dentry->d_inode;
2650 struct nfs4_readdir_arg args = {
2651 .fh = NFS_FH(dir),
2652 .pages = &page,
2653 .pgbase = 0,
2654 .count = count,
Trond Myklebusta65318b2009-03-11 14:10:28 -04002655 .bitmask = NFS_SERVER(dentry->d_inode)->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 };
2657 struct nfs4_readdir_res res;
2658 struct rpc_message msg = {
2659 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2660 .rpc_argp = &args,
2661 .rpc_resp = &res,
2662 .rpc_cred = cred,
2663 };
2664 int status;
2665
Harvey Harrison3110ff82008-05-02 13:42:44 -07002666 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00002667 dentry->d_parent->d_name.name,
2668 dentry->d_name.name,
2669 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2671 res.pgbase = args.pgbase;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002672 status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 if (status == 0)
2674 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustc4812992007-09-28 17:11:45 -04002675
2676 nfs_invalidate_atime(dir);
2677
Harvey Harrison3110ff82008-05-02 13:42:44 -07002678 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 return status;
2680}
2681
2682static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2683 u64 cookie, struct page *page, unsigned int count, int plus)
2684{
2685 struct nfs4_exception exception = { };
2686 int err;
2687 do {
2688 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2689 _nfs4_proc_readdir(dentry, cred, cookie,
2690 page, count, plus),
2691 &exception);
2692 } while (exception.retry);
2693 return err;
2694}
2695
2696static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2697 struct iattr *sattr, dev_t rdev)
2698{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002699 struct nfs4_createdata *data;
2700 int mode = sattr->ia_mode;
2701 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702
2703 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2704 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002705
2706 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2707 if (data == NULL)
2708 goto out;
2709
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002711 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002713 data->arg.ftype = NF4BLK;
2714 data->arg.u.device.specdata1 = MAJOR(rdev);
2715 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 }
2717 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002718 data->arg.ftype = NF4CHR;
2719 data->arg.u.device.specdata1 = MAJOR(rdev);
2720 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002723 status = nfs4_do_create(dir, dentry, data);
2724
2725 nfs4_free_createdata(data);
2726out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 return status;
2728}
2729
2730static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2731 struct iattr *sattr, dev_t rdev)
2732{
2733 struct nfs4_exception exception = { };
2734 int err;
2735 do {
2736 err = nfs4_handle_exception(NFS_SERVER(dir),
2737 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2738 &exception);
2739 } while (exception.retry);
2740 return err;
2741}
2742
2743static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2744 struct nfs_fsstat *fsstat)
2745{
2746 struct nfs4_statfs_arg args = {
2747 .fh = fhandle,
2748 .bitmask = server->attr_bitmask,
2749 };
Benny Halevy24ad1482009-04-01 09:21:56 -04002750 struct nfs4_statfs_res res = {
2751 .fsstat = fsstat,
2752 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 struct rpc_message msg = {
2754 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2755 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04002756 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 };
2758
Trond Myklebust0e574af2005-10-27 22:12:38 -04002759 nfs_fattr_init(fsstat->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002760 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761}
2762
2763static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2764{
2765 struct nfs4_exception exception = { };
2766 int err;
2767 do {
2768 err = nfs4_handle_exception(server,
2769 _nfs4_proc_statfs(server, fhandle, fsstat),
2770 &exception);
2771 } while (exception.retry);
2772 return err;
2773}
2774
2775static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2776 struct nfs_fsinfo *fsinfo)
2777{
2778 struct nfs4_fsinfo_arg args = {
2779 .fh = fhandle,
2780 .bitmask = server->attr_bitmask,
2781 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04002782 struct nfs4_fsinfo_res res = {
2783 .fsinfo = fsinfo,
2784 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 struct rpc_message msg = {
2786 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2787 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04002788 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 };
2790
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002791 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792}
2793
2794static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2795{
2796 struct nfs4_exception exception = { };
2797 int err;
2798
2799 do {
2800 err = nfs4_handle_exception(server,
2801 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2802 &exception);
2803 } while (exception.retry);
2804 return err;
2805}
2806
2807static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2808{
Trond Myklebust0e574af2005-10-27 22:12:38 -04002809 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2811}
2812
2813static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2814 struct nfs_pathconf *pathconf)
2815{
2816 struct nfs4_pathconf_arg args = {
2817 .fh = fhandle,
2818 .bitmask = server->attr_bitmask,
2819 };
Benny Halevyd45b2982009-04-01 09:21:58 -04002820 struct nfs4_pathconf_res res = {
2821 .pathconf = pathconf,
2822 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 struct rpc_message msg = {
2824 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2825 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04002826 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 };
2828
2829 /* None of the pathconf attributes are mandatory to implement */
2830 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
2831 memset(pathconf, 0, sizeof(*pathconf));
2832 return 0;
2833 }
2834
Trond Myklebust0e574af2005-10-27 22:12:38 -04002835 nfs_fattr_init(pathconf->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002836 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837}
2838
2839static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2840 struct nfs_pathconf *pathconf)
2841{
2842 struct nfs4_exception exception = { };
2843 int err;
2844
2845 do {
2846 err = nfs4_handle_exception(server,
2847 _nfs4_proc_pathconf(server, fhandle, pathconf),
2848 &exception);
2849 } while (exception.retry);
2850 return err;
2851}
2852
Trond Myklebustec06c092006-03-20 13:44:27 -05002853static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854{
Trond Myklebustec06c092006-03-20 13:44:27 -05002855 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002857 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 rpc_restart_call(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05002859 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 }
Trond Myklebust8850df92007-09-28 17:20:07 -04002861
2862 nfs_invalidate_atime(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05002864 renew_lease(server, data->timestamp);
2865 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866}
2867
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002868static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 data->timestamp = jiffies;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002871 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872}
2873
Trond Myklebust788e7a82006-03-20 13:44:27 -05002874static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 struct inode *inode = data->inode;
2877
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002878 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 rpc_restart_call(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05002880 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002882 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04002884 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002885 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05002886 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887}
2888
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002889static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002891 struct nfs_server *server = NFS_SERVER(data->inode);
2892
Trond Myklebusta65318b2009-03-11 14:10:28 -04002893 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002894 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 data->timestamp = jiffies;
2896
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002897 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898}
2899
Trond Myklebust788e7a82006-03-20 13:44:27 -05002900static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 struct inode *inode = data->inode;
2903
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002904 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 rpc_restart_call(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05002906 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04002908 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05002909 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910}
2911
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002912static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913{
Trond Myklebust788e7a82006-03-20 13:44:27 -05002914 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915
Trond Myklebusta65318b2009-03-11 14:10:28 -04002916 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002917 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04002918 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919}
2920
2921/*
2922 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
2923 * standalone procedure for queueing an asynchronous RENEW.
2924 */
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002925static void nfs4_renew_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926{
David Howellsadfa6f92006-08-22 20:06:08 -04002927 struct nfs_client *clp = (struct nfs_client *)task->tk_msg.rpc_argp;
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002928 unsigned long timestamp = (unsigned long)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929
2930 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04002931 /* Unless we're shutting down, schedule state recovery! */
2932 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
2933 nfs4_schedule_state_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 return;
2935 }
2936 spin_lock(&clp->cl_lock);
2937 if (time_before(clp->cl_last_renewal,timestamp))
2938 clp->cl_last_renewal = timestamp;
2939 spin_unlock(&clp->cl_lock);
2940}
2941
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002942static const struct rpc_call_ops nfs4_renew_ops = {
2943 .rpc_call_done = nfs4_renew_done,
2944};
2945
David Howellsadfa6f92006-08-22 20:06:08 -04002946int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947{
2948 struct rpc_message msg = {
2949 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2950 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002951 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 };
2953
2954 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002955 &nfs4_renew_ops, (void *)jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956}
2957
David Howellsadfa6f92006-08-22 20:06:08 -04002958int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959{
2960 struct rpc_message msg = {
2961 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2962 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002963 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 };
2965 unsigned long now = jiffies;
2966 int status;
2967
2968 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2969 if (status < 0)
2970 return status;
2971 spin_lock(&clp->cl_lock);
2972 if (time_before(clp->cl_last_renewal,now))
2973 clp->cl_last_renewal = now;
2974 spin_unlock(&clp->cl_lock);
2975 return 0;
2976}
2977
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002978static inline int nfs4_server_supports_acls(struct nfs_server *server)
2979{
2980 return (server->caps & NFS_CAP_ACLS)
2981 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
2982 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
2983}
2984
2985/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
2986 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
2987 * the stack.
2988 */
2989#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
2990
2991static void buf_to_pages(const void *buf, size_t buflen,
2992 struct page **pages, unsigned int *pgbase)
2993{
2994 const void *p = buf;
2995
2996 *pgbase = offset_in_page(buf);
2997 p -= *pgbase;
2998 while (p < buf + buflen) {
2999 *(pages++) = virt_to_page(p);
3000 p += PAGE_CACHE_SIZE;
3001 }
3002}
3003
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003004struct nfs4_cached_acl {
3005 int cached;
3006 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003007 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003008};
3009
3010static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003011{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003012 struct nfs_inode *nfsi = NFS_I(inode);
3013
3014 spin_lock(&inode->i_lock);
3015 kfree(nfsi->nfs4_acl);
3016 nfsi->nfs4_acl = acl;
3017 spin_unlock(&inode->i_lock);
3018}
3019
3020static void nfs4_zap_acl_attr(struct inode *inode)
3021{
3022 nfs4_set_cached_acl(inode, NULL);
3023}
3024
3025static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3026{
3027 struct nfs_inode *nfsi = NFS_I(inode);
3028 struct nfs4_cached_acl *acl;
3029 int ret = -ENOENT;
3030
3031 spin_lock(&inode->i_lock);
3032 acl = nfsi->nfs4_acl;
3033 if (acl == NULL)
3034 goto out;
3035 if (buf == NULL) /* user is just asking for length */
3036 goto out_len;
3037 if (acl->cached == 0)
3038 goto out;
3039 ret = -ERANGE; /* see getxattr(2) man page */
3040 if (acl->len > buflen)
3041 goto out;
3042 memcpy(buf, acl->data, acl->len);
3043out_len:
3044 ret = acl->len;
3045out:
3046 spin_unlock(&inode->i_lock);
3047 return ret;
3048}
3049
3050static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3051{
3052 struct nfs4_cached_acl *acl;
3053
3054 if (buf && acl_len <= PAGE_SIZE) {
3055 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3056 if (acl == NULL)
3057 goto out;
3058 acl->cached = 1;
3059 memcpy(acl->data, buf, acl_len);
3060 } else {
3061 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3062 if (acl == NULL)
3063 goto out;
3064 acl->cached = 0;
3065 }
3066 acl->len = acl_len;
3067out:
3068 nfs4_set_cached_acl(inode, acl);
3069}
3070
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003071static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003072{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003073 struct page *pages[NFS4ACL_MAXPAGES];
3074 struct nfs_getaclargs args = {
3075 .fh = NFS_FH(inode),
3076 .acl_pages = pages,
3077 .acl_len = buflen,
3078 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003079 struct nfs_getaclres res = {
3080 .acl_len = buflen,
3081 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003082 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003083 struct rpc_message msg = {
3084 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3085 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003086 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003087 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003088 struct page *localpage = NULL;
3089 int ret;
3090
3091 if (buflen < PAGE_SIZE) {
3092 /* As long as we're doing a round trip to the server anyway,
3093 * let's be prepared for a page of acl data. */
3094 localpage = alloc_page(GFP_KERNEL);
3095 resp_buf = page_address(localpage);
3096 if (localpage == NULL)
3097 return -ENOMEM;
3098 args.acl_pages[0] = localpage;
3099 args.acl_pgbase = 0;
Benny Halevy663c79b2009-04-01 09:21:59 -04003100 args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003101 } else {
3102 resp_buf = buf;
3103 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3104 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003105 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003106 if (ret)
3107 goto out_free;
Benny Halevy663c79b2009-04-01 09:21:59 -04003108 if (res.acl_len > args.acl_len)
3109 nfs4_write_cached_acl(inode, NULL, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003110 else
Benny Halevy663c79b2009-04-01 09:21:59 -04003111 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003112 if (buf) {
3113 ret = -ERANGE;
Benny Halevy663c79b2009-04-01 09:21:59 -04003114 if (res.acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003115 goto out_free;
3116 if (localpage)
Benny Halevy663c79b2009-04-01 09:21:59 -04003117 memcpy(buf, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003118 }
Benny Halevy663c79b2009-04-01 09:21:59 -04003119 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003120out_free:
3121 if (localpage)
3122 __free_page(localpage);
3123 return ret;
3124}
3125
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003126static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3127{
3128 struct nfs4_exception exception = { };
3129 ssize_t ret;
3130 do {
3131 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3132 if (ret >= 0)
3133 break;
3134 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3135 } while (exception.retry);
3136 return ret;
3137}
3138
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003139static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3140{
3141 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003142 int ret;
3143
3144 if (!nfs4_server_supports_acls(server))
3145 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003146 ret = nfs_revalidate_inode(server, inode);
3147 if (ret < 0)
3148 return ret;
Trond Myklebustf41f7412008-06-11 17:39:04 -04003149 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3150 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003151 ret = nfs4_read_cached_acl(inode, buf, buflen);
3152 if (ret != -ENOENT)
3153 return ret;
3154 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003155}
3156
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003157static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003158{
3159 struct nfs_server *server = NFS_SERVER(inode);
3160 struct page *pages[NFS4ACL_MAXPAGES];
3161 struct nfs_setaclargs arg = {
3162 .fh = NFS_FH(inode),
3163 .acl_pages = pages,
3164 .acl_len = buflen,
3165 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003166 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003167 struct rpc_message msg = {
3168 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3169 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003170 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003171 };
3172 int ret;
3173
3174 if (!nfs4_server_supports_acls(server))
3175 return -EOPNOTSUPP;
Trond Myklebust642ac542005-10-18 14:20:19 -07003176 nfs_inode_return_delegation(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003177 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003178 ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003179 nfs_access_zap_cache(inode);
3180 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003181 return ret;
3182}
3183
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003184static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3185{
3186 struct nfs4_exception exception = { };
3187 int err;
3188 do {
3189 err = nfs4_handle_exception(NFS_SERVER(inode),
3190 __nfs4_proc_set_acl(inode, buf, buflen),
3191 &exception);
3192 } while (exception.retry);
3193 return err;
3194}
3195
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196static int
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003197nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198{
David Howells7539bba2006-08-22 20:06:09 -04003199 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200
3201 if (!clp || task->tk_status >= 0)
3202 return 0;
3203 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003204 case -NFS4ERR_ADMIN_REVOKED:
3205 case -NFS4ERR_BAD_STATEID:
3206 case -NFS4ERR_OPENMODE:
3207 if (state == NULL)
3208 break;
3209 nfs4_state_mark_reclaim_nograce(clp, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 case -NFS4ERR_STALE_CLIENTID:
3211 case -NFS4ERR_STALE_STATEID:
3212 case -NFS4ERR_EXPIRED:
Trond Myklebust5d008372008-02-22 16:34:17 -05003213 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 nfs4_schedule_state_recovery(clp);
Trond Myklebuste005e802008-12-23 15:21:48 -05003215 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
Trond Myklebustfda13932008-02-22 16:34:12 -05003216 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 task->tk_status = 0;
3218 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 case -NFS4ERR_DELAY:
Trond Myklebust2e96d282008-06-11 16:42:05 -04003220 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003221 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3223 task->tk_status = 0;
3224 return -EAGAIN;
3225 case -NFS4ERR_OLD_STATEID:
3226 task->tk_status = 0;
3227 return -EAGAIN;
3228 }
3229 task->tk_status = nfs4_map_errors(task->tk_status);
3230 return 0;
3231}
3232
David Howellsadfa6f92006-08-22 20:06:08 -04003233int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, unsigned short port, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234{
3235 nfs4_verifier sc_verifier;
3236 struct nfs4_setclientid setclientid = {
3237 .sc_verifier = &sc_verifier,
3238 .sc_prog = program,
3239 };
3240 struct rpc_message msg = {
3241 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3242 .rpc_argp = &setclientid,
3243 .rpc_resp = clp,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003244 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 };
Al Virobc4785c2006-10-19 23:28:51 -07003246 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 int loop = 0;
3248 int status;
3249
Al Virobc4785c2006-10-19 23:28:51 -07003250 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3252 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3253
3254 for(;;) {
3255 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003256 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003257 clp->cl_ipaddr,
3258 rpc_peeraddr2str(clp->cl_rpcclient,
3259 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003260 rpc_peeraddr2str(clp->cl_rpcclient,
3261 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003262 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 clp->cl_id_uniquifier);
3264 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003265 sizeof(setclientid.sc_netid),
3266 rpc_peeraddr2str(clp->cl_rpcclient,
3267 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003269 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 clp->cl_ipaddr, port >> 8, port & 255);
3271
3272 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3273 if (status != -NFS4ERR_CLID_INUSE)
3274 break;
3275 if (signalled())
3276 break;
3277 if (loop++ & 1)
3278 ssleep(clp->cl_lease_time + 1);
3279 else
3280 if (++clp->cl_id_uniquifier == 0)
3281 break;
3282 }
3283 return status;
3284}
3285
David Howellsadfa6f92006-08-22 20:06:08 -04003286static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287{
3288 struct nfs_fsinfo fsinfo;
3289 struct rpc_message msg = {
3290 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
3291 .rpc_argp = clp,
3292 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003293 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 };
3295 unsigned long now;
3296 int status;
3297
3298 now = jiffies;
3299 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3300 if (status == 0) {
3301 spin_lock(&clp->cl_lock);
3302 clp->cl_lease_time = fsinfo.lease_time * HZ;
3303 clp->cl_last_renewal = now;
3304 spin_unlock(&clp->cl_lock);
3305 }
3306 return status;
3307}
3308
David Howellsadfa6f92006-08-22 20:06:08 -04003309int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
Trond Myklebust51581f32006-03-20 13:44:47 -05003310{
Benny Halevy77e03672008-06-24 20:25:57 +03003311 long timeout = 0;
Trond Myklebust51581f32006-03-20 13:44:47 -05003312 int err;
3313 do {
3314 err = _nfs4_proc_setclientid_confirm(clp, cred);
3315 switch (err) {
3316 case 0:
3317 return err;
3318 case -NFS4ERR_RESOURCE:
3319 /* The IBM lawyers misread another document! */
3320 case -NFS4ERR_DELAY:
3321 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3322 }
3323 } while (err == 0);
3324 return err;
3325}
3326
Trond Myklebustfe650402006-01-03 09:55:18 +01003327struct nfs4_delegreturndata {
3328 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003329 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003330 struct nfs_fh fh;
3331 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003332 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003333 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003334 int rpc_status;
3335};
3336
Trond Myklebustfe650402006-01-03 09:55:18 +01003337static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3338{
3339 struct nfs4_delegreturndata *data = calldata;
3340 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003341 if (data->rpc_status == 0)
Trond Myklebustfa178f22006-01-03 09:55:38 +01003342 renew_lease(data->res.server, data->timestamp);
Trond Myklebustfe650402006-01-03 09:55:18 +01003343}
3344
3345static void nfs4_delegreturn_release(void *calldata)
3346{
Trond Myklebustfe650402006-01-03 09:55:18 +01003347 kfree(calldata);
3348}
3349
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003350static const struct rpc_call_ops nfs4_delegreturn_ops = {
Trond Myklebustfe650402006-01-03 09:55:18 +01003351 .rpc_call_done = nfs4_delegreturn_done,
3352 .rpc_release = nfs4_delegreturn_release,
3353};
3354
Trond Myklebuste6f81072008-01-24 18:14:34 -05003355static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003356{
3357 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003358 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003359 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003360 struct rpc_message msg = {
3361 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3362 .rpc_cred = cred,
3363 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003364 struct rpc_task_setup task_setup_data = {
3365 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003366 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003367 .callback_ops = &nfs4_delegreturn_ops,
3368 .flags = RPC_TASK_ASYNC,
3369 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003370 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003371
3372 data = kmalloc(sizeof(*data), GFP_KERNEL);
3373 if (data == NULL)
3374 return -ENOMEM;
3375 data->args.fhandle = &data->fh;
3376 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003377 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003378 nfs_copy_fh(&data->fh, NFS_FH(inode));
3379 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003380 data->res.fattr = &data->fattr;
3381 data->res.server = server;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003382 data->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003383 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003384 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003385 data->rpc_status = 0;
3386
Trond Myklebustc970aa82007-07-14 15:39:59 -04003387 task_setup_data.callback_data = data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003388 msg.rpc_argp = &data->args,
3389 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003390 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003391 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003392 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003393 if (!issync)
3394 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003395 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003396 if (status != 0)
3397 goto out;
3398 status = data->rpc_status;
3399 if (status != 0)
3400 goto out;
3401 nfs_refresh_inode(inode, &data->fattr);
3402out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003403 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003404 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405}
3406
Trond Myklebuste6f81072008-01-24 18:14:34 -05003407int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408{
3409 struct nfs_server *server = NFS_SERVER(inode);
3410 struct nfs4_exception exception = { };
3411 int err;
3412 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003413 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 switch (err) {
3415 case -NFS4ERR_STALE_STATEID:
3416 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 case 0:
3418 return 0;
3419 }
3420 err = nfs4_handle_exception(server, err, &exception);
3421 } while (exception.retry);
3422 return err;
3423}
3424
3425#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3426#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3427
3428/*
3429 * sleep, with exponential backoff, and retry the LOCK operation.
3430 */
3431static unsigned long
3432nfs4_set_lock_task_retry(unsigned long timeout)
3433{
Matthew Wilcox150030b2007-12-06 16:24:39 -05003434 schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 timeout <<= 1;
3436 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3437 return NFS4_LOCK_MAXTIMEOUT;
3438 return timeout;
3439}
3440
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3442{
3443 struct inode *inode = state->inode;
3444 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003445 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003446 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003448 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003450 struct nfs_lockt_res res = {
3451 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 };
3453 struct rpc_message msg = {
3454 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3455 .rpc_argp = &arg,
3456 .rpc_resp = &res,
3457 .rpc_cred = state->owner->so_cred,
3458 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 struct nfs4_lock_state *lsp;
3460 int status;
3461
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003462 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003463 status = nfs4_set_lock_state(state, request);
3464 if (status != 0)
3465 goto out;
3466 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003467 arg.lock_owner.id = lsp->ls_id.id;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003468 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003469 switch (status) {
3470 case 0:
3471 request->fl_type = F_UNLCK;
3472 break;
3473 case -NFS4ERR_DENIED:
3474 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05003476 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003477out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 return status;
3479}
3480
3481static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3482{
3483 struct nfs4_exception exception = { };
3484 int err;
3485
3486 do {
3487 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3488 _nfs4_proc_getlk(state, cmd, request),
3489 &exception);
3490 } while (exception.retry);
3491 return err;
3492}
3493
3494static int do_vfs_lock(struct file *file, struct file_lock *fl)
3495{
3496 int res = 0;
3497 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3498 case FL_POSIX:
3499 res = posix_lock_file_wait(file, fl);
3500 break;
3501 case FL_FLOCK:
3502 res = flock_lock_file_wait(file, fl);
3503 break;
3504 default:
3505 BUG();
3506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 return res;
3508}
3509
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003510struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003511 struct nfs_locku_args arg;
3512 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003513 struct nfs4_lock_state *lsp;
3514 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003515 struct file_lock fl;
3516 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003517 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003518};
3519
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003520static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3521 struct nfs_open_context *ctx,
3522 struct nfs4_lock_state *lsp,
3523 struct nfs_seqid *seqid)
3524{
3525 struct nfs4_unlockdata *p;
3526 struct inode *inode = lsp->ls_state->inode;
3527
3528 p = kmalloc(sizeof(*p), GFP_KERNEL);
3529 if (p == NULL)
3530 return NULL;
3531 p->arg.fh = NFS_FH(inode);
3532 p->arg.fl = &p->fl;
3533 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003534 p->res.seqid = seqid;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003535 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003536 p->arg.stateid = &lsp->ls_stateid;
3537 p->lsp = lsp;
3538 atomic_inc(&lsp->ls_count);
3539 /* Ensure we don't close file until we're done freeing locks! */
3540 p->ctx = get_nfs_open_context(ctx);
3541 memcpy(&p->fl, fl, sizeof(p->fl));
3542 p->server = NFS_SERVER(inode);
3543 return p;
3544}
3545
Trond Myklebust06f814a2006-01-03 09:55:07 +01003546static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003547{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003548 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003549 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003550 nfs4_put_lock_state(calldata->lsp);
3551 put_nfs_open_context(calldata->ctx);
3552 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003553}
3554
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003555static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003556{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003557 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003558
Trond Myklebust06f814a2006-01-03 09:55:07 +01003559 if (RPC_ASSASSINATED(task))
3560 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003561 switch (task->tk_status) {
3562 case 0:
3563 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003564 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003565 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01003566 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003567 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003568 case -NFS4ERR_BAD_STATEID:
3569 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003570 case -NFS4ERR_STALE_STATEID:
3571 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003572 break;
3573 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003574 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003575 rpc_restart_call(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003576 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003577}
3578
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003579static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003580{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003581 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003583 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003584 return;
3585 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003586 /* Note: exit _without_ running nfs4_locku_done */
3587 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003588 return;
3589 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003590 calldata->timestamp = jiffies;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003591 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592}
3593
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003594static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003595 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003596 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01003597 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003598};
3599
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003600static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3601 struct nfs_open_context *ctx,
3602 struct nfs4_lock_state *lsp,
3603 struct nfs_seqid *seqid)
3604{
3605 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003606 struct rpc_message msg = {
3607 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3608 .rpc_cred = ctx->cred,
3609 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003610 struct rpc_task_setup task_setup_data = {
3611 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04003612 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003613 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003614 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003615 .flags = RPC_TASK_ASYNC,
3616 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003617
Frank Filz137d6ac2007-07-09 15:32:29 -07003618 /* Ensure this is an unlock - when canceling a lock, the
3619 * canceled lock is passed in, and it won't be an unlock.
3620 */
3621 fl->fl_type = F_UNLCK;
3622
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003623 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3624 if (data == NULL) {
3625 nfs_free_seqid(seqid);
3626 return ERR_PTR(-ENOMEM);
3627 }
3628
Trond Myklebust5138fde2007-07-14 15:40:01 -04003629 msg.rpc_argp = &data->arg,
3630 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003631 task_setup_data.callback_data = data;
3632 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003633}
3634
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3636{
Trond Myklebust19e03c52008-12-23 15:21:44 -05003637 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003638 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003639 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01003640 struct rpc_task *task;
3641 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003642 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643
Trond Myklebust9b073572006-06-29 16:38:34 -04003644 status = nfs4_set_lock_state(state, request);
3645 /* Unlock _before_ we do the RPC call */
3646 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003647 down_read(&nfsi->rwsem);
3648 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
3649 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003650 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003651 }
3652 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003653 if (status != 0)
3654 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003655 /* Is this a delegated lock? */
3656 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04003657 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003658 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003659 seqid = nfs_alloc_seqid(&lsp->ls_seqid);
Trond Myklebust9b073572006-06-29 16:38:34 -04003660 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003661 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04003662 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04003663 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003664 status = PTR_ERR(task);
3665 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04003666 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003667 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003668 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04003669out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003670 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003671 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672}
3673
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003674struct nfs4_lockdata {
3675 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003676 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003677 struct nfs4_lock_state *lsp;
3678 struct nfs_open_context *ctx;
3679 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003680 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003681 int rpc_status;
3682 int cancelled;
3683};
3684
3685static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
3686 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
3687{
3688 struct nfs4_lockdata *p;
3689 struct inode *inode = lsp->ls_state->inode;
3690 struct nfs_server *server = NFS_SERVER(inode);
3691
3692 p = kzalloc(sizeof(*p), GFP_KERNEL);
3693 if (p == NULL)
3694 return NULL;
3695
3696 p->arg.fh = NFS_FH(inode);
3697 p->arg.fl = &p->fl;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003698 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid);
3699 if (p->arg.open_seqid == NULL)
3700 goto out_free;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003701 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3702 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003703 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003704 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04003705 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003706 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003707 p->res.lock_seqid = p->arg.lock_seqid;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003708 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003709 p->lsp = lsp;
3710 atomic_inc(&lsp->ls_count);
3711 p->ctx = get_nfs_open_context(ctx);
3712 memcpy(&p->fl, fl, sizeof(p->fl));
3713 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003714out_free_seqid:
3715 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003716out_free:
3717 kfree(p);
3718 return NULL;
3719}
3720
3721static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
3722{
3723 struct nfs4_lockdata *data = calldata;
3724 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725
Harvey Harrison3110ff82008-05-02 13:42:44 -07003726 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003727 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
3728 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003729 /* Do we need to do an open_to_lock_owner? */
3730 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05003731 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
3732 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003733 data->arg.open_stateid = &state->stateid;
3734 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003735 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003736 } else
3737 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003738 data->timestamp = jiffies;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003739 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003740 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003741}
3742
3743static void nfs4_lock_done(struct rpc_task *task, void *calldata)
3744{
3745 struct nfs4_lockdata *data = calldata;
3746
Harvey Harrison3110ff82008-05-02 13:42:44 -07003747 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003748
3749 data->rpc_status = task->tk_status;
3750 if (RPC_ASSASSINATED(task))
3751 goto out;
3752 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003753 if (data->rpc_status == 0)
3754 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
3755 else
3756 goto out;
3757 }
3758 if (data->rpc_status == 0) {
3759 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
3760 sizeof(data->lsp->ls_stateid.data));
3761 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust88be9f92007-06-05 10:42:27 -04003762 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003763 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003764out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07003765 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003766}
3767
3768static void nfs4_lock_release(void *calldata)
3769{
3770 struct nfs4_lockdata *data = calldata;
3771
Harvey Harrison3110ff82008-05-02 13:42:44 -07003772 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003773 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003774 if (data->cancelled != 0) {
3775 struct rpc_task *task;
3776 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
3777 data->arg.lock_seqid);
3778 if (!IS_ERR(task))
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003779 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003780 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003781 } else
3782 nfs_free_seqid(data->arg.lock_seqid);
3783 nfs4_put_lock_state(data->lsp);
3784 put_nfs_open_context(data->ctx);
3785 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003786 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003787}
3788
3789static const struct rpc_call_ops nfs4_lock_ops = {
3790 .rpc_call_prepare = nfs4_lock_prepare,
3791 .rpc_call_done = nfs4_lock_done,
3792 .rpc_release = nfs4_lock_release,
3793};
3794
3795static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int reclaim)
3796{
3797 struct nfs4_lockdata *data;
3798 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003799 struct rpc_message msg = {
3800 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
3801 .rpc_cred = state->owner->so_cred,
3802 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003803 struct rpc_task_setup task_setup_data = {
3804 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04003805 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003806 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003807 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003808 .flags = RPC_TASK_ASYNC,
3809 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003810 int ret;
3811
Harvey Harrison3110ff82008-05-02 13:42:44 -07003812 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04003813 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003814 fl->fl_u.nfs4_fl.owner);
3815 if (data == NULL)
3816 return -ENOMEM;
3817 if (IS_SETLKW(cmd))
3818 data->arg.block = 1;
3819 if (reclaim != 0)
3820 data->arg.reclaim = 1;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003821 msg.rpc_argp = &data->arg,
3822 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003823 task_setup_data.callback_data = data;
3824 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003825 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003826 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003827 ret = nfs4_wait_for_completion_rpc_task(task);
3828 if (ret == 0) {
3829 ret = data->rpc_status;
3830 if (ret == -NFS4ERR_DENIED)
3831 ret = -EAGAIN;
3832 } else
3833 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003834 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003835 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003836 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837}
3838
3839static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
3840{
Trond Myklebust202b50d2005-06-22 17:16:29 +00003841 struct nfs_server *server = NFS_SERVER(state->inode);
3842 struct nfs4_exception exception = { };
3843 int err;
3844
3845 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04003846 /* Cache the lock if possible... */
3847 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3848 return 0;
Trond Myklebust202b50d2005-06-22 17:16:29 +00003849 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
3850 if (err != -NFS4ERR_DELAY)
3851 break;
3852 nfs4_handle_exception(server, err, &exception);
3853 } while (exception.retry);
3854 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855}
3856
3857static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
3858{
Trond Myklebust202b50d2005-06-22 17:16:29 +00003859 struct nfs_server *server = NFS_SERVER(state->inode);
3860 struct nfs4_exception exception = { };
3861 int err;
3862
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003863 err = nfs4_set_lock_state(state, request);
3864 if (err != 0)
3865 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00003866 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04003867 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3868 return 0;
Trond Myklebust202b50d2005-06-22 17:16:29 +00003869 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
3870 if (err != -NFS4ERR_DELAY)
3871 break;
3872 nfs4_handle_exception(server, err, &exception);
3873 } while (exception.retry);
3874 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875}
3876
3877static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3878{
Trond Myklebust19e03c52008-12-23 15:21:44 -05003879 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04003880 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 int status;
3882
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003883 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003884 status = nfs4_set_lock_state(state, request);
3885 if (status != 0)
3886 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04003887 request->fl_flags |= FL_ACCESS;
3888 status = do_vfs_lock(request->fl_file, request);
3889 if (status < 0)
3890 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003891 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04003892 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04003893 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04003894 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05003895 request->fl_flags = fl_flags & ~FL_SLEEP;
3896 status = do_vfs_lock(request->fl_file, request);
3897 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04003898 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003899 status = _nfs4_do_setlk(state, cmd, request, 0);
3900 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04003901 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003902 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04003903 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003904 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07003905 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04003906out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05003907 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04003908out:
3909 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910 return status;
3911}
3912
3913static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3914{
3915 struct nfs4_exception exception = { };
3916 int err;
3917
3918 do {
3919 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3920 _nfs4_proc_setlk(state, cmd, request),
3921 &exception);
3922 } while (exception.retry);
3923 return err;
3924}
3925
3926static int
3927nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
3928{
3929 struct nfs_open_context *ctx;
3930 struct nfs4_state *state;
3931 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
3932 int status;
3933
3934 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04003935 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936 state = ctx->state;
3937
3938 if (request->fl_start < 0 || request->fl_end < 0)
3939 return -EINVAL;
3940
3941 if (IS_GETLK(cmd))
3942 return nfs4_proc_getlk(state, F_GETLK, request);
3943
3944 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
3945 return -EINVAL;
3946
3947 if (request->fl_type == F_UNLCK)
3948 return nfs4_proc_unlck(state, cmd, request);
3949
3950 do {
3951 status = nfs4_proc_setlk(state, cmd, request);
3952 if ((status != -EAGAIN) || IS_SETLK(cmd))
3953 break;
3954 timeout = nfs4_set_lock_task_retry(timeout);
3955 status = -ERESTARTSYS;
3956 if (signalled())
3957 break;
3958 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 return status;
3960}
3961
Trond Myklebust888e6942005-11-04 15:38:11 -05003962int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
3963{
3964 struct nfs_server *server = NFS_SERVER(state->inode);
3965 struct nfs4_exception exception = { };
3966 int err;
3967
3968 err = nfs4_set_lock_state(state, fl);
3969 if (err != 0)
3970 goto out;
3971 do {
3972 err = _nfs4_do_setlk(state, F_SETLK, fl, 0);
3973 if (err != -NFS4ERR_DELAY)
3974 break;
3975 err = nfs4_handle_exception(server, err, &exception);
3976 } while (exception.retry);
3977out:
3978 return err;
3979}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003980
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003981#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
3982
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003983int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
3984 size_t buflen, int flags)
3985{
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003986 struct inode *inode = dentry->d_inode;
3987
3988 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3989 return -EOPNOTSUPP;
3990
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003991 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003992}
3993
3994/* The getxattr man page suggests returning -ENODATA for unknown attributes,
3995 * and that's what we'll do for e.g. user attributes that haven't been set.
3996 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
3997 * attributes in kernel-managed attribute namespaces. */
3998ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
3999 size_t buflen)
4000{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004001 struct inode *inode = dentry->d_inode;
4002
4003 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4004 return -EOPNOTSUPP;
4005
4006 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004007}
4008
4009ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
4010{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004011 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004012
J. Bruce Fields096455a2006-03-20 23:23:42 -05004013 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4014 return 0;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004015 if (buf && buflen < len)
4016 return -ERANGE;
4017 if (buf)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004018 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
4019 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004020}
4021
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004022static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4023{
4024 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4025 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4026 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4027 return;
4028
4029 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4030 NFS_ATTR_FATTR_NLINK;
4031 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4032 fattr->nlink = 2;
4033}
4034
Trond Myklebust56659e92007-07-17 21:52:39 -04004035int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004036 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004037{
4038 struct nfs_server *server = NFS_SERVER(dir);
4039 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004040 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4041 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004042 };
4043 struct nfs4_fs_locations_arg args = {
4044 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004045 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004046 .page = page,
4047 .bitmask = bitmask,
4048 };
Benny Halevy22958462009-04-01 09:22:02 -04004049 struct nfs4_fs_locations_res res = {
4050 .fs_locations = fs_locations,
4051 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004052 struct rpc_message msg = {
4053 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4054 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004055 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004056 };
4057 int status;
4058
Harvey Harrison3110ff82008-05-02 13:42:44 -07004059 dprintk("%s: start\n", __func__);
Trond Myklebustc228fd32007-01-13 02:28:11 -05004060 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004061 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004062 fs_locations->nlocations = 0;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04004063 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004064 nfs_fixup_referral_attributes(&fs_locations->fattr);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004065 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004066 return status;
4067}
4068
Andy Adamson557134a2009-04-01 09:21:53 -04004069#ifdef CONFIG_NFS_V4_1
4070/* Destroy the slot table */
4071static void nfs4_destroy_slot_table(struct nfs4_session *session)
4072{
4073 if (session->fc_slot_table.slots == NULL)
4074 return;
4075 kfree(session->fc_slot_table.slots);
4076 session->fc_slot_table.slots = NULL;
4077 return;
4078}
4079
4080struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4081{
4082 struct nfs4_session *session;
4083 struct nfs4_slot_table *tbl;
4084
4085 session = kzalloc(sizeof(struct nfs4_session), GFP_KERNEL);
4086 if (!session)
4087 return NULL;
4088 tbl = &session->fc_slot_table;
4089 spin_lock_init(&tbl->slot_tbl_lock);
4090 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "Slot table");
4091 session->clp = clp;
4092 return session;
4093}
4094
4095void nfs4_destroy_session(struct nfs4_session *session)
4096{
4097 nfs4_destroy_slot_table(session);
4098 kfree(session);
4099}
4100
4101#endif /* CONFIG_NFS_V4_1 */
4102
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05004104 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05004105 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106 .recover_open = nfs4_open_reclaim,
4107 .recover_lock = nfs4_lock_reclaim,
4108};
4109
Trond Myklebustb79a4a12008-12-23 15:21:41 -05004110struct nfs4_state_recovery_ops nfs4_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05004111 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05004112 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113 .recover_open = nfs4_open_expired,
4114 .recover_lock = nfs4_lock_expired,
4115};
4116
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08004117static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004118 .permission = nfs_permission,
4119 .getattr = nfs_getattr,
4120 .setattr = nfs_setattr,
4121 .getxattr = nfs4_getxattr,
4122 .setxattr = nfs4_setxattr,
4123 .listxattr = nfs4_listxattr,
4124};
4125
David Howells509de812006-08-22 20:06:11 -04004126const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 .version = 4, /* protocol version */
4128 .dentry_ops = &nfs4_dentry_operations,
4129 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004130 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 .getroot = nfs4_proc_get_root,
4132 .getattr = nfs4_proc_getattr,
4133 .setattr = nfs4_proc_setattr,
David Howells2b3de442006-08-22 20:06:09 -04004134 .lookupfh = nfs4_proc_lookupfh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135 .lookup = nfs4_proc_lookup,
4136 .access = nfs4_proc_access,
4137 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138 .create = nfs4_proc_create,
4139 .remove = nfs4_proc_remove,
4140 .unlink_setup = nfs4_proc_unlink_setup,
4141 .unlink_done = nfs4_proc_unlink_done,
4142 .rename = nfs4_proc_rename,
4143 .link = nfs4_proc_link,
4144 .symlink = nfs4_proc_symlink,
4145 .mkdir = nfs4_proc_mkdir,
4146 .rmdir = nfs4_proc_remove,
4147 .readdir = nfs4_proc_readdir,
4148 .mknod = nfs4_proc_mknod,
4149 .statfs = nfs4_proc_statfs,
4150 .fsinfo = nfs4_proc_fsinfo,
4151 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04004152 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 .decode_dirent = nfs4_decode_dirent,
4154 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05004155 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05004157 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05004159 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004161 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04004162 .close_context = nfs4_close_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163};
4164
4165/*
4166 * Local variables:
4167 * c-basic-offset: 8
4168 * End:
4169 */