nfs41: get_lease_time
[linux-2.6/cjktty.git] / fs / nfs / nfs4proc.c
blobeafc99afd356fbf5d11bb57e2537539393ab0e17
1 /*
2 * fs/nfs/nfs4proc.c
4 * Client-side procedure declarations for NFSv4.
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
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.
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.
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>
50 #include <linux/mount.h>
51 #include <linux/module.h>
53 #include "nfs4_fs.h"
54 #include "delegation.h"
55 #include "internal.h"
56 #include "iostat.h"
58 #define NFSDBG_FACILITY NFSDBG_PROC
60 #define NFS4_POLL_RETRY_MIN (HZ/10)
61 #define NFS4_POLL_RETRY_MAX (15*HZ)
63 struct nfs4_opendata;
64 static int _nfs4_proc_open(struct nfs4_opendata *data);
65 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
66 static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
67 static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
68 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
70 /* Prevent leaks of NFSv4 errors into userland */
71 static int nfs4_map_errors(int err)
73 if (err < -1000) {
74 dprintk("%s could not handle NFSv4 error %d\n",
75 __func__, -err);
76 return -EIO;
78 return err;
82 * This is our standard bitmap for GETATTR requests.
84 const u32 nfs4_fattr_bitmap[2] = {
85 FATTR4_WORD0_TYPE
86 | FATTR4_WORD0_CHANGE
87 | FATTR4_WORD0_SIZE
88 | FATTR4_WORD0_FSID
89 | FATTR4_WORD0_FILEID,
90 FATTR4_WORD1_MODE
91 | FATTR4_WORD1_NUMLINKS
92 | FATTR4_WORD1_OWNER
93 | FATTR4_WORD1_OWNER_GROUP
94 | FATTR4_WORD1_RAWDEV
95 | FATTR4_WORD1_SPACE_USED
96 | FATTR4_WORD1_TIME_ACCESS
97 | FATTR4_WORD1_TIME_METADATA
98 | FATTR4_WORD1_TIME_MODIFY
101 const u32 nfs4_statfs_bitmap[2] = {
102 FATTR4_WORD0_FILES_AVAIL
103 | FATTR4_WORD0_FILES_FREE
104 | FATTR4_WORD0_FILES_TOTAL,
105 FATTR4_WORD1_SPACE_AVAIL
106 | FATTR4_WORD1_SPACE_FREE
107 | FATTR4_WORD1_SPACE_TOTAL
110 const u32 nfs4_pathconf_bitmap[2] = {
111 FATTR4_WORD0_MAXLINK
112 | FATTR4_WORD0_MAXNAME,
116 const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
117 | FATTR4_WORD0_MAXREAD
118 | FATTR4_WORD0_MAXWRITE
119 | FATTR4_WORD0_LEASE_TIME,
123 const u32 nfs4_fs_locations_bitmap[2] = {
124 FATTR4_WORD0_TYPE
125 | FATTR4_WORD0_CHANGE
126 | FATTR4_WORD0_SIZE
127 | FATTR4_WORD0_FSID
128 | FATTR4_WORD0_FILEID
129 | FATTR4_WORD0_FS_LOCATIONS,
130 FATTR4_WORD1_MODE
131 | FATTR4_WORD1_NUMLINKS
132 | FATTR4_WORD1_OWNER
133 | FATTR4_WORD1_OWNER_GROUP
134 | FATTR4_WORD1_RAWDEV
135 | FATTR4_WORD1_SPACE_USED
136 | FATTR4_WORD1_TIME_ACCESS
137 | FATTR4_WORD1_TIME_METADATA
138 | FATTR4_WORD1_TIME_MODIFY
139 | FATTR4_WORD1_MOUNTED_ON_FILEID
142 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
143 struct nfs4_readdir_arg *readdir)
145 __be32 *start, *p;
147 BUG_ON(readdir->count < 80);
148 if (cookie > 2) {
149 readdir->cookie = cookie;
150 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
151 return;
154 readdir->cookie = 0;
155 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
156 if (cookie == 2)
157 return;
160 * NFSv4 servers do not return entries for '.' and '..'
161 * Therefore, we fake these entries here. We let '.'
162 * have cookie 0 and '..' have cookie 1. Note that
163 * when talking to the server, we always send cookie 0
164 * instead of 1 or 2.
166 start = p = kmap_atomic(*readdir->pages, KM_USER0);
168 if (cookie == 0) {
169 *p++ = xdr_one; /* next */
170 *p++ = xdr_zero; /* cookie, first word */
171 *p++ = xdr_one; /* cookie, second word */
172 *p++ = xdr_one; /* entry len */
173 memcpy(p, ".\0\0\0", 4); /* entry */
174 p++;
175 *p++ = xdr_one; /* bitmap length */
176 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
177 *p++ = htonl(8); /* attribute buffer length */
178 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
181 *p++ = xdr_one; /* next */
182 *p++ = xdr_zero; /* cookie, first word */
183 *p++ = xdr_two; /* cookie, second word */
184 *p++ = xdr_two; /* entry len */
185 memcpy(p, "..\0\0", 4); /* entry */
186 p++;
187 *p++ = xdr_one; /* bitmap length */
188 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
189 *p++ = htonl(8); /* attribute buffer length */
190 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
192 readdir->pgbase = (char *)p - (char *)start;
193 readdir->count -= readdir->pgbase;
194 kunmap_atomic(start, KM_USER0);
197 static int nfs4_wait_clnt_recover(struct nfs_client *clp)
199 int res;
201 might_sleep();
203 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
204 nfs_wait_bit_killable, TASK_KILLABLE);
205 return res;
208 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
210 int res = 0;
212 might_sleep();
214 if (*timeout <= 0)
215 *timeout = NFS4_POLL_RETRY_MIN;
216 if (*timeout > NFS4_POLL_RETRY_MAX)
217 *timeout = NFS4_POLL_RETRY_MAX;
218 schedule_timeout_killable(*timeout);
219 if (fatal_signal_pending(current))
220 res = -ERESTARTSYS;
221 *timeout <<= 1;
222 return res;
225 /* This is the error handling routine for processes that are allowed
226 * to sleep.
228 static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
230 struct nfs_client *clp = server->nfs_client;
231 struct nfs4_state *state = exception->state;
232 int ret = errorcode;
234 exception->retry = 0;
235 switch(errorcode) {
236 case 0:
237 return 0;
238 case -NFS4ERR_ADMIN_REVOKED:
239 case -NFS4ERR_BAD_STATEID:
240 case -NFS4ERR_OPENMODE:
241 if (state == NULL)
242 break;
243 nfs4_state_mark_reclaim_nograce(clp, state);
244 case -NFS4ERR_STALE_CLIENTID:
245 case -NFS4ERR_STALE_STATEID:
246 case -NFS4ERR_EXPIRED:
247 nfs4_schedule_state_recovery(clp);
248 ret = nfs4_wait_clnt_recover(clp);
249 if (ret == 0)
250 exception->retry = 1;
251 break;
252 case -NFS4ERR_FILE_OPEN:
253 case -NFS4ERR_GRACE:
254 case -NFS4ERR_DELAY:
255 ret = nfs4_delay(server->client, &exception->timeout);
256 if (ret != 0)
257 break;
258 case -NFS4ERR_OLD_STATEID:
259 exception->retry = 1;
261 /* We failed to handle the error */
262 return nfs4_map_errors(ret);
266 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
268 struct nfs_client *clp = server->nfs_client;
269 spin_lock(&clp->cl_lock);
270 if (time_before(clp->cl_last_renewal,timestamp))
271 clp->cl_last_renewal = timestamp;
272 spin_unlock(&clp->cl_lock);
275 #if defined(CONFIG_NFS_V4_1)
278 * nfs4_free_slot - free a slot and efficiently update slot table.
280 * freeing a slot is trivially done by clearing its respective bit
281 * in the bitmap.
282 * If the freed slotid equals highest_used_slotid we want to update it
283 * so that the server would be able to size down the slot table if needed,
284 * otherwise we know that the highest_used_slotid is still in use.
285 * When updating highest_used_slotid there may be "holes" in the bitmap
286 * so we need to scan down from highest_used_slotid to 0 looking for the now
287 * highest slotid in use.
288 * If none found, highest_used_slotid is set to -1.
290 static void
291 nfs4_free_slot(struct nfs4_slot_table *tbl, u8 free_slotid)
293 int slotid = free_slotid;
295 spin_lock(&tbl->slot_tbl_lock);
296 /* clear used bit in bitmap */
297 __clear_bit(slotid, tbl->used_slots);
299 /* update highest_used_slotid when it is freed */
300 if (slotid == tbl->highest_used_slotid) {
301 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
302 if (slotid >= 0 && slotid < tbl->max_slots)
303 tbl->highest_used_slotid = slotid;
304 else
305 tbl->highest_used_slotid = -1;
307 rpc_wake_up_next(&tbl->slot_tbl_waitq);
308 spin_unlock(&tbl->slot_tbl_lock);
309 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
310 free_slotid, tbl->highest_used_slotid);
313 void nfs41_sequence_free_slot(const struct nfs_client *clp,
314 struct nfs4_sequence_res *res)
316 struct nfs4_slot_table *tbl;
318 if (!nfs4_has_session(clp)) {
319 dprintk("%s: No session\n", __func__);
320 return;
322 tbl = &clp->cl_session->fc_slot_table;
323 if (res->sr_slotid == NFS4_MAX_SLOT_TABLE) {
324 dprintk("%s: No slot\n", __func__);
325 /* just wake up the next guy waiting since
326 * we may have not consumed a slot after all */
327 rpc_wake_up_next(&tbl->slot_tbl_waitq);
328 return;
330 nfs4_free_slot(tbl, res->sr_slotid);
331 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
334 static void nfs41_sequence_done(struct nfs_client *clp,
335 struct nfs4_sequence_res *res,
336 int rpc_status)
338 unsigned long timestamp;
339 struct nfs4_slot_table *tbl;
340 struct nfs4_slot *slot;
343 * sr_status remains 1 if an RPC level error occurred. The server
344 * may or may not have processed the sequence operation..
345 * Proceed as if the server received and processed the sequence
346 * operation.
348 if (res->sr_status == 1)
349 res->sr_status = NFS_OK;
351 /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
352 if (res->sr_slotid == NFS4_MAX_SLOT_TABLE)
353 goto out;
355 tbl = &clp->cl_session->fc_slot_table;
356 slot = tbl->slots + res->sr_slotid;
358 if (res->sr_status == 0) {
359 /* Update the slot's sequence and clientid lease timer */
360 ++slot->seq_nr;
361 timestamp = res->sr_renewal_time;
362 spin_lock(&clp->cl_lock);
363 if (time_before(clp->cl_last_renewal, timestamp))
364 clp->cl_last_renewal = timestamp;
365 spin_unlock(&clp->cl_lock);
366 return;
368 out:
369 /* The session may be reset by one of the error handlers. */
370 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
371 nfs41_sequence_free_slot(clp, res);
375 * nfs4_find_slot - efficiently look for a free slot
377 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
378 * If found, we mark the slot as used, update the highest_used_slotid,
379 * and respectively set up the sequence operation args.
380 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
382 * Note: must be called with under the slot_tbl_lock.
384 static u8
385 nfs4_find_slot(struct nfs4_slot_table *tbl, struct rpc_task *task)
387 int slotid;
388 u8 ret_id = NFS4_MAX_SLOT_TABLE;
389 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
391 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
392 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
393 tbl->max_slots);
394 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
395 if (slotid >= tbl->max_slots)
396 goto out;
397 __set_bit(slotid, tbl->used_slots);
398 if (slotid > tbl->highest_used_slotid)
399 tbl->highest_used_slotid = slotid;
400 ret_id = slotid;
401 out:
402 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
403 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
404 return ret_id;
407 static int nfs41_setup_sequence(struct nfs4_session *session,
408 struct nfs4_sequence_args *args,
409 struct nfs4_sequence_res *res,
410 int cache_reply,
411 struct rpc_task *task)
413 struct nfs4_slot *slot;
414 struct nfs4_slot_table *tbl;
415 u8 slotid;
417 dprintk("--> %s\n", __func__);
418 /* slot already allocated? */
419 if (res->sr_slotid != NFS4_MAX_SLOT_TABLE)
420 return 0;
422 memset(res, 0, sizeof(*res));
423 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
424 tbl = &session->fc_slot_table;
426 spin_lock(&tbl->slot_tbl_lock);
427 slotid = nfs4_find_slot(tbl, task);
428 if (slotid == NFS4_MAX_SLOT_TABLE) {
429 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
430 spin_unlock(&tbl->slot_tbl_lock);
431 dprintk("<-- %s: no free slots\n", __func__);
432 return -EAGAIN;
434 spin_unlock(&tbl->slot_tbl_lock);
436 slot = tbl->slots + slotid;
437 args->sa_session = session;
438 args->sa_slotid = slotid;
439 args->sa_cache_this = cache_reply;
441 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
443 res->sr_session = session;
444 res->sr_slotid = slotid;
445 res->sr_renewal_time = jiffies;
447 * sr_status is only set in decode_sequence, and so will remain
448 * set to 1 if an rpc level failure occurs.
450 res->sr_status = 1;
451 return 0;
454 int nfs4_setup_sequence(struct nfs_client *clp,
455 struct nfs4_sequence_args *args,
456 struct nfs4_sequence_res *res,
457 int cache_reply,
458 struct rpc_task *task)
460 int ret = 0;
462 dprintk("--> %s clp %p session %p sr_slotid %d\n",
463 __func__, clp, clp->cl_session, res->sr_slotid);
465 if (!nfs4_has_session(clp))
466 goto out;
467 ret = nfs41_setup_sequence(clp->cl_session, args, res, cache_reply,
468 task);
469 if (ret != -EAGAIN) {
470 /* terminate rpc task */
471 task->tk_status = ret;
472 task->tk_action = NULL;
474 out:
475 dprintk("<-- %s status=%d\n", __func__, ret);
476 return ret;
479 struct nfs41_call_sync_data {
480 struct nfs_client *clp;
481 struct nfs4_sequence_args *seq_args;
482 struct nfs4_sequence_res *seq_res;
483 int cache_reply;
486 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
488 struct nfs41_call_sync_data *data = calldata;
490 dprintk("--> %s data->clp->cl_session %p\n", __func__,
491 data->clp->cl_session);
492 if (nfs4_setup_sequence(data->clp, data->seq_args,
493 data->seq_res, data->cache_reply, task))
494 return;
495 rpc_call_start(task);
498 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
500 struct nfs41_call_sync_data *data = calldata;
502 nfs41_sequence_done(data->clp, data->seq_res, task->tk_status);
503 nfs41_sequence_free_slot(data->clp, data->seq_res);
506 struct rpc_call_ops nfs41_call_sync_ops = {
507 .rpc_call_prepare = nfs41_call_sync_prepare,
508 .rpc_call_done = nfs41_call_sync_done,
511 static int nfs4_call_sync_sequence(struct nfs_client *clp,
512 struct rpc_clnt *clnt,
513 struct rpc_message *msg,
514 struct nfs4_sequence_args *args,
515 struct nfs4_sequence_res *res,
516 int cache_reply)
518 int ret;
519 struct rpc_task *task;
520 struct nfs41_call_sync_data data = {
521 .clp = clp,
522 .seq_args = args,
523 .seq_res = res,
524 .cache_reply = cache_reply,
526 struct rpc_task_setup task_setup = {
527 .rpc_client = clnt,
528 .rpc_message = msg,
529 .callback_ops = &nfs41_call_sync_ops,
530 .callback_data = &data
533 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
534 task = rpc_run_task(&task_setup);
535 if (IS_ERR(task))
536 ret = PTR_ERR(task);
537 else {
538 ret = task->tk_status;
539 rpc_put_task(task);
541 return ret;
544 int _nfs4_call_sync_session(struct nfs_server *server,
545 struct rpc_message *msg,
546 struct nfs4_sequence_args *args,
547 struct nfs4_sequence_res *res,
548 int cache_reply)
550 return nfs4_call_sync_sequence(server->nfs_client, server->client,
551 msg, args, res, cache_reply);
554 #endif /* CONFIG_NFS_V4_1 */
556 int _nfs4_call_sync(struct nfs_server *server,
557 struct rpc_message *msg,
558 struct nfs4_sequence_args *args,
559 struct nfs4_sequence_res *res,
560 int cache_reply)
562 args->sa_session = res->sr_session = NULL;
563 return rpc_call_sync(server->client, msg, 0);
566 #define nfs4_call_sync(server, msg, args, res, cache_reply) \
567 (server)->nfs_client->cl_call_sync((server), (msg), &(args)->seq_args, \
568 &(res)->seq_res, (cache_reply))
570 static void nfs4_sequence_done(const struct nfs_server *server,
571 struct nfs4_sequence_res *res, int rpc_status)
573 #ifdef CONFIG_NFS_V4_1
574 if (nfs4_has_session(server->nfs_client))
575 nfs41_sequence_done(server->nfs_client, res, rpc_status);
576 #endif /* CONFIG_NFS_V4_1 */
579 /* no restart, therefore free slot here */
580 static void nfs4_sequence_done_free_slot(const struct nfs_server *server,
581 struct nfs4_sequence_res *res,
582 int rpc_status)
584 nfs4_sequence_done(server, res, rpc_status);
585 nfs4_sequence_free_slot(server->nfs_client, res);
588 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
590 struct nfs_inode *nfsi = NFS_I(dir);
592 spin_lock(&dir->i_lock);
593 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
594 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
595 nfs_force_lookup_revalidate(dir);
596 nfsi->change_attr = cinfo->after;
597 spin_unlock(&dir->i_lock);
600 struct nfs4_opendata {
601 struct kref kref;
602 struct nfs_openargs o_arg;
603 struct nfs_openres o_res;
604 struct nfs_open_confirmargs c_arg;
605 struct nfs_open_confirmres c_res;
606 struct nfs_fattr f_attr;
607 struct nfs_fattr dir_attr;
608 struct path path;
609 struct dentry *dir;
610 struct nfs4_state_owner *owner;
611 struct nfs4_state *state;
612 struct iattr attrs;
613 unsigned long timestamp;
614 unsigned int rpc_done : 1;
615 int rpc_status;
616 int cancelled;
620 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
622 p->o_res.f_attr = &p->f_attr;
623 p->o_res.dir_attr = &p->dir_attr;
624 p->o_res.seqid = p->o_arg.seqid;
625 p->c_res.seqid = p->c_arg.seqid;
626 p->o_res.server = p->o_arg.server;
627 nfs_fattr_init(&p->f_attr);
628 nfs_fattr_init(&p->dir_attr);
631 static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
632 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
633 const struct iattr *attrs)
635 struct dentry *parent = dget_parent(path->dentry);
636 struct inode *dir = parent->d_inode;
637 struct nfs_server *server = NFS_SERVER(dir);
638 struct nfs4_opendata *p;
640 p = kzalloc(sizeof(*p), GFP_KERNEL);
641 if (p == NULL)
642 goto err;
643 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
644 if (p->o_arg.seqid == NULL)
645 goto err_free;
646 p->path.mnt = mntget(path->mnt);
647 p->path.dentry = dget(path->dentry);
648 p->dir = parent;
649 p->owner = sp;
650 atomic_inc(&sp->so_count);
651 p->o_arg.fh = NFS_FH(dir);
652 p->o_arg.open_flags = flags;
653 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
654 p->o_arg.clientid = server->nfs_client->cl_clientid;
655 p->o_arg.id = sp->so_owner_id.id;
656 p->o_arg.name = &p->path.dentry->d_name;
657 p->o_arg.server = server;
658 p->o_arg.bitmask = server->attr_bitmask;
659 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
660 p->o_res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
661 if (flags & O_EXCL) {
662 u32 *s = (u32 *) p->o_arg.u.verifier.data;
663 s[0] = jiffies;
664 s[1] = current->pid;
665 } else if (flags & O_CREAT) {
666 p->o_arg.u.attrs = &p->attrs;
667 memcpy(&p->attrs, attrs, sizeof(p->attrs));
669 p->c_arg.fh = &p->o_res.fh;
670 p->c_arg.stateid = &p->o_res.stateid;
671 p->c_arg.seqid = p->o_arg.seqid;
672 nfs4_init_opendata_res(p);
673 kref_init(&p->kref);
674 return p;
675 err_free:
676 kfree(p);
677 err:
678 dput(parent);
679 return NULL;
682 static void nfs4_opendata_free(struct kref *kref)
684 struct nfs4_opendata *p = container_of(kref,
685 struct nfs4_opendata, kref);
687 nfs_free_seqid(p->o_arg.seqid);
688 if (p->state != NULL)
689 nfs4_put_open_state(p->state);
690 nfs4_put_state_owner(p->owner);
691 dput(p->dir);
692 path_put(&p->path);
693 kfree(p);
696 static void nfs4_opendata_put(struct nfs4_opendata *p)
698 if (p != NULL)
699 kref_put(&p->kref, nfs4_opendata_free);
702 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
704 int ret;
706 ret = rpc_wait_for_completion_task(task);
707 return ret;
710 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
712 int ret = 0;
714 if (open_mode & O_EXCL)
715 goto out;
716 switch (mode & (FMODE_READ|FMODE_WRITE)) {
717 case FMODE_READ:
718 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0;
719 break;
720 case FMODE_WRITE:
721 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0;
722 break;
723 case FMODE_READ|FMODE_WRITE:
724 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0;
726 out:
727 return ret;
730 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
732 if ((delegation->type & fmode) != fmode)
733 return 0;
734 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
735 return 0;
736 nfs_mark_delegation_referenced(delegation);
737 return 1;
740 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
742 switch (fmode) {
743 case FMODE_WRITE:
744 state->n_wronly++;
745 break;
746 case FMODE_READ:
747 state->n_rdonly++;
748 break;
749 case FMODE_READ|FMODE_WRITE:
750 state->n_rdwr++;
752 nfs4_state_set_mode_locked(state, state->state | fmode);
755 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
757 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
758 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
759 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
760 switch (fmode) {
761 case FMODE_READ:
762 set_bit(NFS_O_RDONLY_STATE, &state->flags);
763 break;
764 case FMODE_WRITE:
765 set_bit(NFS_O_WRONLY_STATE, &state->flags);
766 break;
767 case FMODE_READ|FMODE_WRITE:
768 set_bit(NFS_O_RDWR_STATE, &state->flags);
772 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
774 write_seqlock(&state->seqlock);
775 nfs_set_open_stateid_locked(state, stateid, fmode);
776 write_sequnlock(&state->seqlock);
779 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
782 * Protect the call to nfs4_state_set_mode_locked and
783 * serialise the stateid update
785 write_seqlock(&state->seqlock);
786 if (deleg_stateid != NULL) {
787 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
788 set_bit(NFS_DELEGATED_STATE, &state->flags);
790 if (open_stateid != NULL)
791 nfs_set_open_stateid_locked(state, open_stateid, fmode);
792 write_sequnlock(&state->seqlock);
793 spin_lock(&state->owner->so_lock);
794 update_open_stateflags(state, fmode);
795 spin_unlock(&state->owner->so_lock);
798 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
800 struct nfs_inode *nfsi = NFS_I(state->inode);
801 struct nfs_delegation *deleg_cur;
802 int ret = 0;
804 fmode &= (FMODE_READ|FMODE_WRITE);
806 rcu_read_lock();
807 deleg_cur = rcu_dereference(nfsi->delegation);
808 if (deleg_cur == NULL)
809 goto no_delegation;
811 spin_lock(&deleg_cur->lock);
812 if (nfsi->delegation != deleg_cur ||
813 (deleg_cur->type & fmode) != fmode)
814 goto no_delegation_unlock;
816 if (delegation == NULL)
817 delegation = &deleg_cur->stateid;
818 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
819 goto no_delegation_unlock;
821 nfs_mark_delegation_referenced(deleg_cur);
822 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
823 ret = 1;
824 no_delegation_unlock:
825 spin_unlock(&deleg_cur->lock);
826 no_delegation:
827 rcu_read_unlock();
829 if (!ret && open_stateid != NULL) {
830 __update_open_stateid(state, open_stateid, NULL, fmode);
831 ret = 1;
834 return ret;
838 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
840 struct nfs_delegation *delegation;
842 rcu_read_lock();
843 delegation = rcu_dereference(NFS_I(inode)->delegation);
844 if (delegation == NULL || (delegation->type & fmode) == fmode) {
845 rcu_read_unlock();
846 return;
848 rcu_read_unlock();
849 nfs_inode_return_delegation(inode);
852 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
854 struct nfs4_state *state = opendata->state;
855 struct nfs_inode *nfsi = NFS_I(state->inode);
856 struct nfs_delegation *delegation;
857 int open_mode = opendata->o_arg.open_flags & O_EXCL;
858 fmode_t fmode = opendata->o_arg.fmode;
859 nfs4_stateid stateid;
860 int ret = -EAGAIN;
862 for (;;) {
863 if (can_open_cached(state, fmode, open_mode)) {
864 spin_lock(&state->owner->so_lock);
865 if (can_open_cached(state, fmode, open_mode)) {
866 update_open_stateflags(state, fmode);
867 spin_unlock(&state->owner->so_lock);
868 goto out_return_state;
870 spin_unlock(&state->owner->so_lock);
872 rcu_read_lock();
873 delegation = rcu_dereference(nfsi->delegation);
874 if (delegation == NULL ||
875 !can_open_delegated(delegation, fmode)) {
876 rcu_read_unlock();
877 break;
879 /* Save the delegation */
880 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
881 rcu_read_unlock();
882 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
883 if (ret != 0)
884 goto out;
885 ret = -EAGAIN;
887 /* Try to update the stateid using the delegation */
888 if (update_open_stateid(state, NULL, &stateid, fmode))
889 goto out_return_state;
891 out:
892 return ERR_PTR(ret);
893 out_return_state:
894 atomic_inc(&state->count);
895 return state;
898 static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
900 struct inode *inode;
901 struct nfs4_state *state = NULL;
902 struct nfs_delegation *delegation;
903 int ret;
905 if (!data->rpc_done) {
906 state = nfs4_try_open_cached(data);
907 goto out;
910 ret = -EAGAIN;
911 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
912 goto err;
913 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
914 ret = PTR_ERR(inode);
915 if (IS_ERR(inode))
916 goto err;
917 ret = -ENOMEM;
918 state = nfs4_get_open_state(inode, data->owner);
919 if (state == NULL)
920 goto err_put_inode;
921 if (data->o_res.delegation_type != 0) {
922 int delegation_flags = 0;
924 rcu_read_lock();
925 delegation = rcu_dereference(NFS_I(inode)->delegation);
926 if (delegation)
927 delegation_flags = delegation->flags;
928 rcu_read_unlock();
929 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
930 nfs_inode_set_delegation(state->inode,
931 data->owner->so_cred,
932 &data->o_res);
933 else
934 nfs_inode_reclaim_delegation(state->inode,
935 data->owner->so_cred,
936 &data->o_res);
939 update_open_stateid(state, &data->o_res.stateid, NULL,
940 data->o_arg.fmode);
941 iput(inode);
942 out:
943 return state;
944 err_put_inode:
945 iput(inode);
946 err:
947 return ERR_PTR(ret);
950 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
952 struct nfs_inode *nfsi = NFS_I(state->inode);
953 struct nfs_open_context *ctx;
955 spin_lock(&state->inode->i_lock);
956 list_for_each_entry(ctx, &nfsi->open_files, list) {
957 if (ctx->state != state)
958 continue;
959 get_nfs_open_context(ctx);
960 spin_unlock(&state->inode->i_lock);
961 return ctx;
963 spin_unlock(&state->inode->i_lock);
964 return ERR_PTR(-ENOENT);
967 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
969 struct nfs4_opendata *opendata;
971 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL);
972 if (opendata == NULL)
973 return ERR_PTR(-ENOMEM);
974 opendata->state = state;
975 atomic_inc(&state->count);
976 return opendata;
979 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
981 struct nfs4_state *newstate;
982 int ret;
984 opendata->o_arg.open_flags = 0;
985 opendata->o_arg.fmode = fmode;
986 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
987 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
988 nfs4_init_opendata_res(opendata);
989 ret = _nfs4_proc_open(opendata);
990 if (ret != 0)
991 return ret;
992 newstate = nfs4_opendata_to_nfs4_state(opendata);
993 if (IS_ERR(newstate))
994 return PTR_ERR(newstate);
995 nfs4_close_state(&opendata->path, newstate, fmode);
996 *res = newstate;
997 return 0;
1000 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1002 struct nfs4_state *newstate;
1003 int ret;
1005 /* memory barrier prior to reading state->n_* */
1006 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1007 smp_rmb();
1008 if (state->n_rdwr != 0) {
1009 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1010 if (ret != 0)
1011 return ret;
1012 if (newstate != state)
1013 return -ESTALE;
1015 if (state->n_wronly != 0) {
1016 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1017 if (ret != 0)
1018 return ret;
1019 if (newstate != state)
1020 return -ESTALE;
1022 if (state->n_rdonly != 0) {
1023 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1024 if (ret != 0)
1025 return ret;
1026 if (newstate != state)
1027 return -ESTALE;
1030 * We may have performed cached opens for all three recoveries.
1031 * Check if we need to update the current stateid.
1033 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1034 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
1035 write_seqlock(&state->seqlock);
1036 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1037 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
1038 write_sequnlock(&state->seqlock);
1040 return 0;
1044 * OPEN_RECLAIM:
1045 * reclaim state on the server after a reboot.
1047 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1049 struct nfs_delegation *delegation;
1050 struct nfs4_opendata *opendata;
1051 fmode_t delegation_type = 0;
1052 int status;
1054 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1055 if (IS_ERR(opendata))
1056 return PTR_ERR(opendata);
1057 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1058 opendata->o_arg.fh = NFS_FH(state->inode);
1059 rcu_read_lock();
1060 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1061 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1062 delegation_type = delegation->type;
1063 rcu_read_unlock();
1064 opendata->o_arg.u.delegation_type = delegation_type;
1065 status = nfs4_open_recover(opendata, state);
1066 nfs4_opendata_put(opendata);
1067 return status;
1070 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1072 struct nfs_server *server = NFS_SERVER(state->inode);
1073 struct nfs4_exception exception = { };
1074 int err;
1075 do {
1076 err = _nfs4_do_open_reclaim(ctx, state);
1077 if (err != -NFS4ERR_DELAY)
1078 break;
1079 nfs4_handle_exception(server, err, &exception);
1080 } while (exception.retry);
1081 return err;
1084 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1086 struct nfs_open_context *ctx;
1087 int ret;
1089 ctx = nfs4_state_find_open_context(state);
1090 if (IS_ERR(ctx))
1091 return PTR_ERR(ctx);
1092 ret = nfs4_do_open_reclaim(ctx, state);
1093 put_nfs_open_context(ctx);
1094 return ret;
1097 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1099 struct nfs4_opendata *opendata;
1100 int ret;
1102 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1103 if (IS_ERR(opendata))
1104 return PTR_ERR(opendata);
1105 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
1106 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
1107 sizeof(opendata->o_arg.u.delegation.data));
1108 ret = nfs4_open_recover(opendata, state);
1109 nfs4_opendata_put(opendata);
1110 return ret;
1113 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1115 struct nfs4_exception exception = { };
1116 struct nfs_server *server = NFS_SERVER(state->inode);
1117 int err;
1118 do {
1119 err = _nfs4_open_delegation_recall(ctx, state, stateid);
1120 switch (err) {
1121 case 0:
1122 return err;
1123 case -NFS4ERR_STALE_CLIENTID:
1124 case -NFS4ERR_STALE_STATEID:
1125 case -NFS4ERR_EXPIRED:
1126 /* Don't recall a delegation if it was lost */
1127 nfs4_schedule_state_recovery(server->nfs_client);
1128 return err;
1130 err = nfs4_handle_exception(server, err, &exception);
1131 } while (exception.retry);
1132 return err;
1135 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1137 struct nfs4_opendata *data = calldata;
1139 data->rpc_status = task->tk_status;
1140 if (RPC_ASSASSINATED(task))
1141 return;
1142 if (data->rpc_status == 0) {
1143 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1144 sizeof(data->o_res.stateid.data));
1145 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1146 renew_lease(data->o_res.server, data->timestamp);
1147 data->rpc_done = 1;
1151 static void nfs4_open_confirm_release(void *calldata)
1153 struct nfs4_opendata *data = calldata;
1154 struct nfs4_state *state = NULL;
1156 /* If this request hasn't been cancelled, do nothing */
1157 if (data->cancelled == 0)
1158 goto out_free;
1159 /* In case of error, no cleanup! */
1160 if (!data->rpc_done)
1161 goto out_free;
1162 state = nfs4_opendata_to_nfs4_state(data);
1163 if (!IS_ERR(state))
1164 nfs4_close_state(&data->path, state, data->o_arg.fmode);
1165 out_free:
1166 nfs4_opendata_put(data);
1169 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1170 .rpc_call_done = nfs4_open_confirm_done,
1171 .rpc_release = nfs4_open_confirm_release,
1175 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1177 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1179 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1180 struct rpc_task *task;
1181 struct rpc_message msg = {
1182 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1183 .rpc_argp = &data->c_arg,
1184 .rpc_resp = &data->c_res,
1185 .rpc_cred = data->owner->so_cred,
1187 struct rpc_task_setup task_setup_data = {
1188 .rpc_client = server->client,
1189 .rpc_message = &msg,
1190 .callback_ops = &nfs4_open_confirm_ops,
1191 .callback_data = data,
1192 .workqueue = nfsiod_workqueue,
1193 .flags = RPC_TASK_ASYNC,
1195 int status;
1197 kref_get(&data->kref);
1198 data->rpc_done = 0;
1199 data->rpc_status = 0;
1200 data->timestamp = jiffies;
1201 task = rpc_run_task(&task_setup_data);
1202 if (IS_ERR(task))
1203 return PTR_ERR(task);
1204 status = nfs4_wait_for_completion_rpc_task(task);
1205 if (status != 0) {
1206 data->cancelled = 1;
1207 smp_wmb();
1208 } else
1209 status = data->rpc_status;
1210 rpc_put_task(task);
1211 return status;
1214 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1216 struct nfs4_opendata *data = calldata;
1217 struct nfs4_state_owner *sp = data->owner;
1219 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1220 return;
1222 * Check if we still need to send an OPEN call, or if we can use
1223 * a delegation instead.
1225 if (data->state != NULL) {
1226 struct nfs_delegation *delegation;
1228 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1229 goto out_no_action;
1230 rcu_read_lock();
1231 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1232 if (delegation != NULL &&
1233 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
1234 rcu_read_unlock();
1235 goto out_no_action;
1237 rcu_read_unlock();
1239 /* Update sequence id. */
1240 data->o_arg.id = sp->so_owner_id.id;
1241 data->o_arg.clientid = sp->so_client->cl_clientid;
1242 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
1243 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1244 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1246 data->timestamp = jiffies;
1247 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
1248 &data->o_arg.seq_args,
1249 &data->o_res.seq_res, 1, task))
1250 return;
1251 rpc_call_start(task);
1252 return;
1253 out_no_action:
1254 task->tk_action = NULL;
1258 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1260 struct nfs4_opendata *data = calldata;
1262 data->rpc_status = task->tk_status;
1264 nfs4_sequence_done_free_slot(data->o_arg.server, &data->o_res.seq_res,
1265 task->tk_status);
1267 if (RPC_ASSASSINATED(task))
1268 return;
1269 if (task->tk_status == 0) {
1270 switch (data->o_res.f_attr->mode & S_IFMT) {
1271 case S_IFREG:
1272 break;
1273 case S_IFLNK:
1274 data->rpc_status = -ELOOP;
1275 break;
1276 case S_IFDIR:
1277 data->rpc_status = -EISDIR;
1278 break;
1279 default:
1280 data->rpc_status = -ENOTDIR;
1282 renew_lease(data->o_res.server, data->timestamp);
1283 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1284 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1286 data->rpc_done = 1;
1289 static void nfs4_open_release(void *calldata)
1291 struct nfs4_opendata *data = calldata;
1292 struct nfs4_state *state = NULL;
1294 /* If this request hasn't been cancelled, do nothing */
1295 if (data->cancelled == 0)
1296 goto out_free;
1297 /* In case of error, no cleanup! */
1298 if (data->rpc_status != 0 || !data->rpc_done)
1299 goto out_free;
1300 /* In case we need an open_confirm, no cleanup! */
1301 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1302 goto out_free;
1303 state = nfs4_opendata_to_nfs4_state(data);
1304 if (!IS_ERR(state))
1305 nfs4_close_state(&data->path, state, data->o_arg.fmode);
1306 out_free:
1307 nfs4_opendata_put(data);
1310 static const struct rpc_call_ops nfs4_open_ops = {
1311 .rpc_call_prepare = nfs4_open_prepare,
1312 .rpc_call_done = nfs4_open_done,
1313 .rpc_release = nfs4_open_release,
1317 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1319 static int _nfs4_proc_open(struct nfs4_opendata *data)
1321 struct inode *dir = data->dir->d_inode;
1322 struct nfs_server *server = NFS_SERVER(dir);
1323 struct nfs_openargs *o_arg = &data->o_arg;
1324 struct nfs_openres *o_res = &data->o_res;
1325 struct rpc_task *task;
1326 struct rpc_message msg = {
1327 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1328 .rpc_argp = o_arg,
1329 .rpc_resp = o_res,
1330 .rpc_cred = data->owner->so_cred,
1332 struct rpc_task_setup task_setup_data = {
1333 .rpc_client = server->client,
1334 .rpc_message = &msg,
1335 .callback_ops = &nfs4_open_ops,
1336 .callback_data = data,
1337 .workqueue = nfsiod_workqueue,
1338 .flags = RPC_TASK_ASYNC,
1340 int status;
1342 kref_get(&data->kref);
1343 data->rpc_done = 0;
1344 data->rpc_status = 0;
1345 data->cancelled = 0;
1346 task = rpc_run_task(&task_setup_data);
1347 if (IS_ERR(task))
1348 return PTR_ERR(task);
1349 status = nfs4_wait_for_completion_rpc_task(task);
1350 if (status != 0) {
1351 data->cancelled = 1;
1352 smp_wmb();
1353 } else
1354 status = data->rpc_status;
1355 rpc_put_task(task);
1356 if (status != 0 || !data->rpc_done)
1357 return status;
1359 if (o_res->fh.size == 0)
1360 _nfs4_proc_lookup(dir, o_arg->name, &o_res->fh, o_res->f_attr);
1362 if (o_arg->open_flags & O_CREAT) {
1363 update_changeattr(dir, &o_res->cinfo);
1364 nfs_post_op_update_inode(dir, o_res->dir_attr);
1365 } else
1366 nfs_refresh_inode(dir, o_res->dir_attr);
1367 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1368 status = _nfs4_proc_open_confirm(data);
1369 if (status != 0)
1370 return status;
1372 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1373 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1374 return 0;
1377 static int nfs4_recover_expired_lease(struct nfs_server *server)
1379 struct nfs_client *clp = server->nfs_client;
1380 int ret;
1382 for (;;) {
1383 ret = nfs4_wait_clnt_recover(clp);
1384 if (ret != 0)
1385 return ret;
1386 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1387 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1388 break;
1389 nfs4_schedule_state_recovery(clp);
1391 return 0;
1395 * OPEN_EXPIRED:
1396 * reclaim state on the server after a network partition.
1397 * Assumes caller holds the appropriate lock
1399 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1401 struct nfs4_opendata *opendata;
1402 int ret;
1404 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1405 if (IS_ERR(opendata))
1406 return PTR_ERR(opendata);
1407 ret = nfs4_open_recover(opendata, state);
1408 if (ret == -ESTALE)
1409 d_drop(ctx->path.dentry);
1410 nfs4_opendata_put(opendata);
1411 return ret;
1414 static inline int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1416 struct nfs_server *server = NFS_SERVER(state->inode);
1417 struct nfs4_exception exception = { };
1418 int err;
1420 do {
1421 err = _nfs4_open_expired(ctx, state);
1422 if (err != -NFS4ERR_DELAY)
1423 break;
1424 nfs4_handle_exception(server, err, &exception);
1425 } while (exception.retry);
1426 return err;
1429 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1431 struct nfs_open_context *ctx;
1432 int ret;
1434 ctx = nfs4_state_find_open_context(state);
1435 if (IS_ERR(ctx))
1436 return PTR_ERR(ctx);
1437 ret = nfs4_do_open_expired(ctx, state);
1438 put_nfs_open_context(ctx);
1439 return ret;
1443 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1444 * fields corresponding to attributes that were used to store the verifier.
1445 * Make sure we clobber those fields in the later setattr call
1447 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1449 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1450 !(sattr->ia_valid & ATTR_ATIME_SET))
1451 sattr->ia_valid |= ATTR_ATIME;
1453 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1454 !(sattr->ia_valid & ATTR_MTIME_SET))
1455 sattr->ia_valid |= ATTR_MTIME;
1459 * Returns a referenced nfs4_state
1461 static 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)
1463 struct nfs4_state_owner *sp;
1464 struct nfs4_state *state = NULL;
1465 struct nfs_server *server = NFS_SERVER(dir);
1466 struct nfs4_opendata *opendata;
1467 int status;
1469 /* Protect against reboot recovery conflicts */
1470 status = -ENOMEM;
1471 if (!(sp = nfs4_get_state_owner(server, cred))) {
1472 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1473 goto out_err;
1475 status = nfs4_recover_expired_lease(server);
1476 if (status != 0)
1477 goto err_put_state_owner;
1478 if (path->dentry->d_inode != NULL)
1479 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
1480 status = -ENOMEM;
1481 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr);
1482 if (opendata == NULL)
1483 goto err_put_state_owner;
1485 if (path->dentry->d_inode != NULL)
1486 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1488 status = _nfs4_proc_open(opendata);
1489 if (status != 0)
1490 goto err_opendata_put;
1492 if (opendata->o_arg.open_flags & O_EXCL)
1493 nfs4_exclusive_attrset(opendata, sattr);
1495 state = nfs4_opendata_to_nfs4_state(opendata);
1496 status = PTR_ERR(state);
1497 if (IS_ERR(state))
1498 goto err_opendata_put;
1499 nfs4_opendata_put(opendata);
1500 nfs4_put_state_owner(sp);
1501 *res = state;
1502 return 0;
1503 err_opendata_put:
1504 nfs4_opendata_put(opendata);
1505 err_put_state_owner:
1506 nfs4_put_state_owner(sp);
1507 out_err:
1508 *res = NULL;
1509 return status;
1513 static struct nfs4_state *nfs4_do_open(struct inode *dir, struct path *path, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred)
1515 struct nfs4_exception exception = { };
1516 struct nfs4_state *res;
1517 int status;
1519 do {
1520 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
1521 if (status == 0)
1522 break;
1523 /* NOTE: BAD_SEQID means the server and client disagree about the
1524 * book-keeping w.r.t. state-changing operations
1525 * (OPEN/CLOSE/LOCK/LOCKU...)
1526 * It is actually a sign of a bug on the client or on the server.
1528 * If we receive a BAD_SEQID error in the particular case of
1529 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1530 * have unhashed the old state_owner for us, and that we can
1531 * therefore safely retry using a new one. We should still warn
1532 * the user though...
1534 if (status == -NFS4ERR_BAD_SEQID) {
1535 printk(KERN_WARNING "NFS: v4 server %s "
1536 " returned a bad sequence-id error!\n",
1537 NFS_SERVER(dir)->nfs_client->cl_hostname);
1538 exception.retry = 1;
1539 continue;
1542 * BAD_STATEID on OPEN means that the server cancelled our
1543 * state before it received the OPEN_CONFIRM.
1544 * Recover by retrying the request as per the discussion
1545 * on Page 181 of RFC3530.
1547 if (status == -NFS4ERR_BAD_STATEID) {
1548 exception.retry = 1;
1549 continue;
1551 if (status == -EAGAIN) {
1552 /* We must have found a delegation */
1553 exception.retry = 1;
1554 continue;
1556 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1557 status, &exception));
1558 } while (exception.retry);
1559 return res;
1562 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1563 struct nfs_fattr *fattr, struct iattr *sattr,
1564 struct nfs4_state *state)
1566 struct nfs_server *server = NFS_SERVER(inode);
1567 struct nfs_setattrargs arg = {
1568 .fh = NFS_FH(inode),
1569 .iap = sattr,
1570 .server = server,
1571 .bitmask = server->attr_bitmask,
1573 struct nfs_setattrres res = {
1574 .fattr = fattr,
1575 .server = server,
1577 struct rpc_message msg = {
1578 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1579 .rpc_argp = &arg,
1580 .rpc_resp = &res,
1581 .rpc_cred = cred,
1583 unsigned long timestamp = jiffies;
1584 int status;
1586 nfs_fattr_init(fattr);
1588 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1589 /* Use that stateid */
1590 } else if (state != NULL) {
1591 nfs4_copy_stateid(&arg.stateid, state, current->files);
1592 } else
1593 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1595 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
1596 if (status == 0 && state != NULL)
1597 renew_lease(server, timestamp);
1598 return status;
1601 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1602 struct nfs_fattr *fattr, struct iattr *sattr,
1603 struct nfs4_state *state)
1605 struct nfs_server *server = NFS_SERVER(inode);
1606 struct nfs4_exception exception = { };
1607 int err;
1608 do {
1609 err = nfs4_handle_exception(server,
1610 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
1611 &exception);
1612 } while (exception.retry);
1613 return err;
1616 struct nfs4_closedata {
1617 struct path path;
1618 struct inode *inode;
1619 struct nfs4_state *state;
1620 struct nfs_closeargs arg;
1621 struct nfs_closeres res;
1622 struct nfs_fattr fattr;
1623 unsigned long timestamp;
1626 static void nfs4_free_closedata(void *data)
1628 struct nfs4_closedata *calldata = data;
1629 struct nfs4_state_owner *sp = calldata->state->owner;
1631 nfs4_put_open_state(calldata->state);
1632 nfs_free_seqid(calldata->arg.seqid);
1633 nfs4_put_state_owner(sp);
1634 path_put(&calldata->path);
1635 kfree(calldata);
1638 static void nfs4_close_done(struct rpc_task *task, void *data)
1640 struct nfs4_closedata *calldata = data;
1641 struct nfs4_state *state = calldata->state;
1642 struct nfs_server *server = NFS_SERVER(calldata->inode);
1644 nfs4_sequence_done(server, &calldata->res.seq_res, task->tk_status);
1645 if (RPC_ASSASSINATED(task))
1646 return;
1647 /* hmm. we are done with the inode, and in the process of freeing
1648 * the state_owner. we keep this around to process errors
1650 switch (task->tk_status) {
1651 case 0:
1652 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
1653 renew_lease(server, calldata->timestamp);
1654 break;
1655 case -NFS4ERR_STALE_STATEID:
1656 case -NFS4ERR_OLD_STATEID:
1657 case -NFS4ERR_BAD_STATEID:
1658 case -NFS4ERR_EXPIRED:
1659 if (calldata->arg.fmode == 0)
1660 break;
1661 default:
1662 if (nfs4_async_handle_error(task, server, state) == -EAGAIN) {
1663 rpc_restart_call(task);
1664 return;
1667 nfs4_sequence_free_slot(server->nfs_client, &calldata->res.seq_res);
1668 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
1671 static void nfs4_close_prepare(struct rpc_task *task, void *data)
1673 struct nfs4_closedata *calldata = data;
1674 struct nfs4_state *state = calldata->state;
1675 int clear_rd, clear_wr, clear_rdwr;
1677 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
1678 return;
1680 clear_rd = clear_wr = clear_rdwr = 0;
1681 spin_lock(&state->owner->so_lock);
1682 /* Calculate the change in open mode */
1683 if (state->n_rdwr == 0) {
1684 if (state->n_rdonly == 0) {
1685 clear_rd |= test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1686 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1688 if (state->n_wronly == 0) {
1689 clear_wr |= test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1690 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1693 spin_unlock(&state->owner->so_lock);
1694 if (!clear_rd && !clear_wr && !clear_rdwr) {
1695 /* Note: exit _without_ calling nfs4_close_done */
1696 task->tk_action = NULL;
1697 return;
1699 nfs_fattr_init(calldata->res.fattr);
1700 if (test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0) {
1701 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1702 calldata->arg.fmode = FMODE_READ;
1703 } else if (test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0) {
1704 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1705 calldata->arg.fmode = FMODE_WRITE;
1707 calldata->timestamp = jiffies;
1708 if (nfs4_setup_sequence((NFS_SERVER(calldata->inode))->nfs_client,
1709 &calldata->arg.seq_args, &calldata->res.seq_res,
1710 1, task))
1711 return;
1712 rpc_call_start(task);
1715 static const struct rpc_call_ops nfs4_close_ops = {
1716 .rpc_call_prepare = nfs4_close_prepare,
1717 .rpc_call_done = nfs4_close_done,
1718 .rpc_release = nfs4_free_closedata,
1722 * It is possible for data to be read/written from a mem-mapped file
1723 * after the sys_close call (which hits the vfs layer as a flush).
1724 * This means that we can't safely call nfsv4 close on a file until
1725 * the inode is cleared. This in turn means that we are not good
1726 * NFSv4 citizens - we do not indicate to the server to update the file's
1727 * share state even when we are done with one of the three share
1728 * stateid's in the inode.
1730 * NOTE: Caller must be holding the sp->so_owner semaphore!
1732 int nfs4_do_close(struct path *path, struct nfs4_state *state, int wait)
1734 struct nfs_server *server = NFS_SERVER(state->inode);
1735 struct nfs4_closedata *calldata;
1736 struct nfs4_state_owner *sp = state->owner;
1737 struct rpc_task *task;
1738 struct rpc_message msg = {
1739 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1740 .rpc_cred = state->owner->so_cred,
1742 struct rpc_task_setup task_setup_data = {
1743 .rpc_client = server->client,
1744 .rpc_message = &msg,
1745 .callback_ops = &nfs4_close_ops,
1746 .workqueue = nfsiod_workqueue,
1747 .flags = RPC_TASK_ASYNC,
1749 int status = -ENOMEM;
1751 calldata = kzalloc(sizeof(*calldata), GFP_KERNEL);
1752 if (calldata == NULL)
1753 goto out;
1754 calldata->inode = state->inode;
1755 calldata->state = state;
1756 calldata->arg.fh = NFS_FH(state->inode);
1757 calldata->arg.stateid = &state->open_stateid;
1758 /* Serialization for the sequence id */
1759 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
1760 if (calldata->arg.seqid == NULL)
1761 goto out_free_calldata;
1762 calldata->arg.fmode = 0;
1763 calldata->arg.bitmask = server->cache_consistency_bitmask;
1764 calldata->res.fattr = &calldata->fattr;
1765 calldata->res.seqid = calldata->arg.seqid;
1766 calldata->res.server = server;
1767 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
1768 calldata->path.mnt = mntget(path->mnt);
1769 calldata->path.dentry = dget(path->dentry);
1771 msg.rpc_argp = &calldata->arg,
1772 msg.rpc_resp = &calldata->res,
1773 task_setup_data.callback_data = calldata;
1774 task = rpc_run_task(&task_setup_data);
1775 if (IS_ERR(task))
1776 return PTR_ERR(task);
1777 status = 0;
1778 if (wait)
1779 status = rpc_wait_for_completion_task(task);
1780 rpc_put_task(task);
1781 return status;
1782 out_free_calldata:
1783 kfree(calldata);
1784 out:
1785 nfs4_put_open_state(state);
1786 nfs4_put_state_owner(sp);
1787 return status;
1790 static int nfs4_intent_set_file(struct nameidata *nd, struct path *path, struct nfs4_state *state, fmode_t fmode)
1792 struct file *filp;
1793 int ret;
1795 /* If the open_intent is for execute, we have an extra check to make */
1796 if (fmode & FMODE_EXEC) {
1797 ret = nfs_may_open(state->inode,
1798 state->owner->so_cred,
1799 nd->intent.open.flags);
1800 if (ret < 0)
1801 goto out_close;
1803 filp = lookup_instantiate_filp(nd, path->dentry, NULL);
1804 if (!IS_ERR(filp)) {
1805 struct nfs_open_context *ctx;
1806 ctx = nfs_file_open_context(filp);
1807 ctx->state = state;
1808 return 0;
1810 ret = PTR_ERR(filp);
1811 out_close:
1812 nfs4_close_sync(path, state, fmode & (FMODE_READ|FMODE_WRITE));
1813 return ret;
1816 struct dentry *
1817 nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1819 struct path path = {
1820 .mnt = nd->path.mnt,
1821 .dentry = dentry,
1823 struct dentry *parent;
1824 struct iattr attr;
1825 struct rpc_cred *cred;
1826 struct nfs4_state *state;
1827 struct dentry *res;
1828 fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
1830 if (nd->flags & LOOKUP_CREATE) {
1831 attr.ia_mode = nd->intent.open.create_mode;
1832 attr.ia_valid = ATTR_MODE;
1833 if (!IS_POSIXACL(dir))
1834 attr.ia_mode &= ~current_umask();
1835 } else {
1836 attr.ia_valid = 0;
1837 BUG_ON(nd->intent.open.flags & O_CREAT);
1840 cred = rpc_lookup_cred();
1841 if (IS_ERR(cred))
1842 return (struct dentry *)cred;
1843 parent = dentry->d_parent;
1844 /* Protect against concurrent sillydeletes */
1845 nfs_block_sillyrename(parent);
1846 state = nfs4_do_open(dir, &path, fmode, nd->intent.open.flags, &attr, cred);
1847 put_rpccred(cred);
1848 if (IS_ERR(state)) {
1849 if (PTR_ERR(state) == -ENOENT) {
1850 d_add(dentry, NULL);
1851 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1853 nfs_unblock_sillyrename(parent);
1854 return (struct dentry *)state;
1856 res = d_add_unique(dentry, igrab(state->inode));
1857 if (res != NULL)
1858 path.dentry = res;
1859 nfs_set_verifier(path.dentry, nfs_save_change_attribute(dir));
1860 nfs_unblock_sillyrename(parent);
1861 nfs4_intent_set_file(nd, &path, state, fmode);
1862 return res;
1866 nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
1868 struct path path = {
1869 .mnt = nd->path.mnt,
1870 .dentry = dentry,
1872 struct rpc_cred *cred;
1873 struct nfs4_state *state;
1874 fmode_t fmode = openflags & (FMODE_READ | FMODE_WRITE);
1876 cred = rpc_lookup_cred();
1877 if (IS_ERR(cred))
1878 return PTR_ERR(cred);
1879 state = nfs4_do_open(dir, &path, fmode, openflags, NULL, cred);
1880 put_rpccred(cred);
1881 if (IS_ERR(state)) {
1882 switch (PTR_ERR(state)) {
1883 case -EPERM:
1884 case -EACCES:
1885 case -EDQUOT:
1886 case -ENOSPC:
1887 case -EROFS:
1888 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
1889 return 1;
1890 default:
1891 goto out_drop;
1894 if (state->inode == dentry->d_inode) {
1895 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1896 nfs4_intent_set_file(nd, &path, state, fmode);
1897 return 1;
1899 nfs4_close_sync(&path, state, fmode);
1900 out_drop:
1901 d_drop(dentry);
1902 return 0;
1905 void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
1907 if (ctx->state == NULL)
1908 return;
1909 if (is_sync)
1910 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
1911 else
1912 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
1915 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1917 struct nfs4_server_caps_arg args = {
1918 .fhandle = fhandle,
1920 struct nfs4_server_caps_res res = {};
1921 struct rpc_message msg = {
1922 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
1923 .rpc_argp = &args,
1924 .rpc_resp = &res,
1926 int status;
1928 status = nfs4_call_sync(server, &msg, &args, &res, 0);
1929 if (status == 0) {
1930 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
1931 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
1932 server->caps |= NFS_CAP_ACLS;
1933 if (res.has_links != 0)
1934 server->caps |= NFS_CAP_HARDLINKS;
1935 if (res.has_symlinks != 0)
1936 server->caps |= NFS_CAP_SYMLINKS;
1937 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
1938 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
1939 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
1940 server->acl_bitmask = res.acl_bitmask;
1943 return status;
1946 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1948 struct nfs4_exception exception = { };
1949 int err;
1950 do {
1951 err = nfs4_handle_exception(server,
1952 _nfs4_server_capabilities(server, fhandle),
1953 &exception);
1954 } while (exception.retry);
1955 return err;
1958 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1959 struct nfs_fsinfo *info)
1961 struct nfs4_lookup_root_arg args = {
1962 .bitmask = nfs4_fattr_bitmap,
1964 struct nfs4_lookup_res res = {
1965 .server = server,
1966 .fattr = info->fattr,
1967 .fh = fhandle,
1969 struct rpc_message msg = {
1970 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
1971 .rpc_argp = &args,
1972 .rpc_resp = &res,
1974 nfs_fattr_init(info->fattr);
1975 return nfs4_call_sync(server, &msg, &args, &res, 0);
1978 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1979 struct nfs_fsinfo *info)
1981 struct nfs4_exception exception = { };
1982 int err;
1983 do {
1984 err = nfs4_handle_exception(server,
1985 _nfs4_lookup_root(server, fhandle, info),
1986 &exception);
1987 } while (exception.retry);
1988 return err;
1992 * get the file handle for the "/" directory on the server
1994 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
1995 struct nfs_fsinfo *info)
1997 int status;
1999 status = nfs4_lookup_root(server, fhandle, info);
2000 if (status == 0)
2001 status = nfs4_server_capabilities(server, fhandle);
2002 if (status == 0)
2003 status = nfs4_do_fsinfo(server, fhandle, info);
2004 return nfs4_map_errors(status);
2008 * Get locations and (maybe) other attributes of a referral.
2009 * Note that we'll actually follow the referral later when
2010 * we detect fsid mismatch in inode revalidation
2012 static int nfs4_get_referral(struct inode *dir, const struct qstr *name, struct nfs_fattr *fattr, struct nfs_fh *fhandle)
2014 int status = -ENOMEM;
2015 struct page *page = NULL;
2016 struct nfs4_fs_locations *locations = NULL;
2018 page = alloc_page(GFP_KERNEL);
2019 if (page == NULL)
2020 goto out;
2021 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2022 if (locations == NULL)
2023 goto out;
2025 status = nfs4_proc_fs_locations(dir, name, locations, page);
2026 if (status != 0)
2027 goto out;
2028 /* Make sure server returned a different fsid for the referral */
2029 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
2030 dprintk("%s: server did not return a different fsid for a referral at %s\n", __func__, name->name);
2031 status = -EIO;
2032 goto out;
2035 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2036 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2037 if (!fattr->mode)
2038 fattr->mode = S_IFDIR;
2039 memset(fhandle, 0, sizeof(struct nfs_fh));
2040 out:
2041 if (page)
2042 __free_page(page);
2043 if (locations)
2044 kfree(locations);
2045 return status;
2048 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2050 struct nfs4_getattr_arg args = {
2051 .fh = fhandle,
2052 .bitmask = server->attr_bitmask,
2054 struct nfs4_getattr_res res = {
2055 .fattr = fattr,
2056 .server = server,
2058 struct rpc_message msg = {
2059 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2060 .rpc_argp = &args,
2061 .rpc_resp = &res,
2064 nfs_fattr_init(fattr);
2065 return nfs4_call_sync(server, &msg, &args, &res, 0);
2068 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2070 struct nfs4_exception exception = { };
2071 int err;
2072 do {
2073 err = nfs4_handle_exception(server,
2074 _nfs4_proc_getattr(server, fhandle, fattr),
2075 &exception);
2076 } while (exception.retry);
2077 return err;
2081 * The file is not closed if it is opened due to the a request to change
2082 * the size of the file. The open call will not be needed once the
2083 * VFS layer lookup-intents are implemented.
2085 * Close is called when the inode is destroyed.
2086 * If we haven't opened the file for O_WRONLY, we
2087 * need to in the size_change case to obtain a stateid.
2089 * Got race?
2090 * Because OPEN is always done by name in nfsv4, it is
2091 * possible that we opened a different file by the same
2092 * name. We can recognize this race condition, but we
2093 * can't do anything about it besides returning an error.
2095 * This will be fixed with VFS changes (lookup-intent).
2097 static int
2098 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2099 struct iattr *sattr)
2101 struct inode *inode = dentry->d_inode;
2102 struct rpc_cred *cred = NULL;
2103 struct nfs4_state *state = NULL;
2104 int status;
2106 nfs_fattr_init(fattr);
2108 /* Search for an existing open(O_WRITE) file */
2109 if (sattr->ia_valid & ATTR_FILE) {
2110 struct nfs_open_context *ctx;
2112 ctx = nfs_file_open_context(sattr->ia_file);
2113 if (ctx) {
2114 cred = ctx->cred;
2115 state = ctx->state;
2119 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2120 if (status == 0)
2121 nfs_setattr_update_inode(inode, sattr);
2122 return status;
2125 static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
2126 const struct qstr *name, struct nfs_fh *fhandle,
2127 struct nfs_fattr *fattr)
2129 int status;
2130 struct nfs4_lookup_arg args = {
2131 .bitmask = server->attr_bitmask,
2132 .dir_fh = dirfh,
2133 .name = name,
2135 struct nfs4_lookup_res res = {
2136 .server = server,
2137 .fattr = fattr,
2138 .fh = fhandle,
2140 struct rpc_message msg = {
2141 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2142 .rpc_argp = &args,
2143 .rpc_resp = &res,
2146 nfs_fattr_init(fattr);
2148 dprintk("NFS call lookupfh %s\n", name->name);
2149 status = nfs4_call_sync(server, &msg, &args, &res, 0);
2150 dprintk("NFS reply lookupfh: %d\n", status);
2151 return status;
2154 static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2155 struct qstr *name, struct nfs_fh *fhandle,
2156 struct nfs_fattr *fattr)
2158 struct nfs4_exception exception = { };
2159 int err;
2160 do {
2161 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2162 /* FIXME: !!!! */
2163 if (err == -NFS4ERR_MOVED) {
2164 err = -EREMOTE;
2165 break;
2167 err = nfs4_handle_exception(server, err, &exception);
2168 } while (exception.retry);
2169 return err;
2172 static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
2173 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2175 int status;
2177 dprintk("NFS call lookup %s\n", name->name);
2178 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
2179 if (status == -NFS4ERR_MOVED)
2180 status = nfs4_get_referral(dir, name, fattr, fhandle);
2181 dprintk("NFS reply lookup: %d\n", status);
2182 return status;
2185 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2187 struct nfs4_exception exception = { };
2188 int err;
2189 do {
2190 err = nfs4_handle_exception(NFS_SERVER(dir),
2191 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2192 &exception);
2193 } while (exception.retry);
2194 return err;
2197 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2199 struct nfs_server *server = NFS_SERVER(inode);
2200 struct nfs_fattr fattr;
2201 struct nfs4_accessargs args = {
2202 .fh = NFS_FH(inode),
2203 .bitmask = server->attr_bitmask,
2205 struct nfs4_accessres res = {
2206 .server = server,
2207 .fattr = &fattr,
2209 struct rpc_message msg = {
2210 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2211 .rpc_argp = &args,
2212 .rpc_resp = &res,
2213 .rpc_cred = entry->cred,
2215 int mode = entry->mask;
2216 int status;
2219 * Determine which access bits we want to ask for...
2221 if (mode & MAY_READ)
2222 args.access |= NFS4_ACCESS_READ;
2223 if (S_ISDIR(inode->i_mode)) {
2224 if (mode & MAY_WRITE)
2225 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2226 if (mode & MAY_EXEC)
2227 args.access |= NFS4_ACCESS_LOOKUP;
2228 } else {
2229 if (mode & MAY_WRITE)
2230 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2231 if (mode & MAY_EXEC)
2232 args.access |= NFS4_ACCESS_EXECUTE;
2234 nfs_fattr_init(&fattr);
2235 status = nfs4_call_sync(server, &msg, &args, &res, 0);
2236 if (!status) {
2237 entry->mask = 0;
2238 if (res.access & NFS4_ACCESS_READ)
2239 entry->mask |= MAY_READ;
2240 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2241 entry->mask |= MAY_WRITE;
2242 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2243 entry->mask |= MAY_EXEC;
2244 nfs_refresh_inode(inode, &fattr);
2246 return status;
2249 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2251 struct nfs4_exception exception = { };
2252 int err;
2253 do {
2254 err = nfs4_handle_exception(NFS_SERVER(inode),
2255 _nfs4_proc_access(inode, entry),
2256 &exception);
2257 } while (exception.retry);
2258 return err;
2262 * TODO: For the time being, we don't try to get any attributes
2263 * along with any of the zero-copy operations READ, READDIR,
2264 * READLINK, WRITE.
2266 * In the case of the first three, we want to put the GETATTR
2267 * after the read-type operation -- this is because it is hard
2268 * to predict the length of a GETATTR response in v4, and thus
2269 * align the READ data correctly. This means that the GETATTR
2270 * may end up partially falling into the page cache, and we should
2271 * shift it into the 'tail' of the xdr_buf before processing.
2272 * To do this efficiently, we need to know the total length
2273 * of data received, which doesn't seem to be available outside
2274 * of the RPC layer.
2276 * In the case of WRITE, we also want to put the GETATTR after
2277 * the operation -- in this case because we want to make sure
2278 * we get the post-operation mtime and size. This means that
2279 * we can't use xdr_encode_pages() as written: we need a variant
2280 * of it which would leave room in the 'tail' iovec.
2282 * Both of these changes to the XDR layer would in fact be quite
2283 * minor, but I decided to leave them for a subsequent patch.
2285 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2286 unsigned int pgbase, unsigned int pglen)
2288 struct nfs4_readlink args = {
2289 .fh = NFS_FH(inode),
2290 .pgbase = pgbase,
2291 .pglen = pglen,
2292 .pages = &page,
2294 struct nfs4_readlink_res res;
2295 struct rpc_message msg = {
2296 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2297 .rpc_argp = &args,
2298 .rpc_resp = &res,
2301 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
2304 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2305 unsigned int pgbase, unsigned int pglen)
2307 struct nfs4_exception exception = { };
2308 int err;
2309 do {
2310 err = nfs4_handle_exception(NFS_SERVER(inode),
2311 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2312 &exception);
2313 } while (exception.retry);
2314 return err;
2318 * Got race?
2319 * We will need to arrange for the VFS layer to provide an atomic open.
2320 * Until then, this create/open method is prone to inefficiency and race
2321 * conditions due to the lookup, create, and open VFS calls from sys_open()
2322 * placed on the wire.
2324 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2325 * The file will be opened again in the subsequent VFS open call
2326 * (nfs4_proc_file_open).
2328 * The open for read will just hang around to be used by any process that
2329 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2332 static int
2333 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
2334 int flags, struct nameidata *nd)
2336 struct path path = {
2337 .mnt = nd->path.mnt,
2338 .dentry = dentry,
2340 struct nfs4_state *state;
2341 struct rpc_cred *cred;
2342 fmode_t fmode = flags & (FMODE_READ | FMODE_WRITE);
2343 int status = 0;
2345 cred = rpc_lookup_cred();
2346 if (IS_ERR(cred)) {
2347 status = PTR_ERR(cred);
2348 goto out;
2350 state = nfs4_do_open(dir, &path, fmode, flags, sattr, cred);
2351 d_drop(dentry);
2352 if (IS_ERR(state)) {
2353 status = PTR_ERR(state);
2354 goto out_putcred;
2356 d_add(dentry, igrab(state->inode));
2357 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2358 if (flags & O_EXCL) {
2359 struct nfs_fattr fattr;
2360 status = nfs4_do_setattr(state->inode, cred, &fattr, sattr, state);
2361 if (status == 0)
2362 nfs_setattr_update_inode(state->inode, sattr);
2363 nfs_post_op_update_inode(state->inode, &fattr);
2365 if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
2366 status = nfs4_intent_set_file(nd, &path, state, fmode);
2367 else
2368 nfs4_close_sync(&path, state, fmode);
2369 out_putcred:
2370 put_rpccred(cred);
2371 out:
2372 return status;
2375 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2377 struct nfs_server *server = NFS_SERVER(dir);
2378 struct nfs_removeargs args = {
2379 .fh = NFS_FH(dir),
2380 .name.len = name->len,
2381 .name.name = name->name,
2382 .bitmask = server->attr_bitmask,
2384 struct nfs_removeres res = {
2385 .server = server,
2387 struct rpc_message msg = {
2388 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2389 .rpc_argp = &args,
2390 .rpc_resp = &res,
2392 int status;
2394 nfs_fattr_init(&res.dir_attr);
2395 status = nfs4_call_sync(server, &msg, &args, &res, 1);
2396 if (status == 0) {
2397 update_changeattr(dir, &res.cinfo);
2398 nfs_post_op_update_inode(dir, &res.dir_attr);
2400 return status;
2403 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2405 struct nfs4_exception exception = { };
2406 int err;
2407 do {
2408 err = nfs4_handle_exception(NFS_SERVER(dir),
2409 _nfs4_proc_remove(dir, name),
2410 &exception);
2411 } while (exception.retry);
2412 return err;
2415 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
2417 struct nfs_server *server = NFS_SERVER(dir);
2418 struct nfs_removeargs *args = msg->rpc_argp;
2419 struct nfs_removeres *res = msg->rpc_resp;
2421 args->bitmask = server->cache_consistency_bitmask;
2422 res->server = server;
2423 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
2426 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
2428 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2430 nfs4_sequence_done(res->server, &res->seq_res, task->tk_status);
2431 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2432 return 0;
2433 nfs4_sequence_free_slot(res->server->nfs_client, &res->seq_res);
2434 update_changeattr(dir, &res->cinfo);
2435 nfs_post_op_update_inode(dir, &res->dir_attr);
2436 return 1;
2439 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2440 struct inode *new_dir, struct qstr *new_name)
2442 struct nfs_server *server = NFS_SERVER(old_dir);
2443 struct nfs4_rename_arg arg = {
2444 .old_dir = NFS_FH(old_dir),
2445 .new_dir = NFS_FH(new_dir),
2446 .old_name = old_name,
2447 .new_name = new_name,
2448 .bitmask = server->attr_bitmask,
2450 struct nfs_fattr old_fattr, new_fattr;
2451 struct nfs4_rename_res res = {
2452 .server = server,
2453 .old_fattr = &old_fattr,
2454 .new_fattr = &new_fattr,
2456 struct rpc_message msg = {
2457 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2458 .rpc_argp = &arg,
2459 .rpc_resp = &res,
2461 int status;
2463 nfs_fattr_init(res.old_fattr);
2464 nfs_fattr_init(res.new_fattr);
2465 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
2467 if (!status) {
2468 update_changeattr(old_dir, &res.old_cinfo);
2469 nfs_post_op_update_inode(old_dir, res.old_fattr);
2470 update_changeattr(new_dir, &res.new_cinfo);
2471 nfs_post_op_update_inode(new_dir, res.new_fattr);
2473 return status;
2476 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2477 struct inode *new_dir, struct qstr *new_name)
2479 struct nfs4_exception exception = { };
2480 int err;
2481 do {
2482 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2483 _nfs4_proc_rename(old_dir, old_name,
2484 new_dir, new_name),
2485 &exception);
2486 } while (exception.retry);
2487 return err;
2490 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2492 struct nfs_server *server = NFS_SERVER(inode);
2493 struct nfs4_link_arg arg = {
2494 .fh = NFS_FH(inode),
2495 .dir_fh = NFS_FH(dir),
2496 .name = name,
2497 .bitmask = server->attr_bitmask,
2499 struct nfs_fattr fattr, dir_attr;
2500 struct nfs4_link_res res = {
2501 .server = server,
2502 .fattr = &fattr,
2503 .dir_attr = &dir_attr,
2505 struct rpc_message msg = {
2506 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2507 .rpc_argp = &arg,
2508 .rpc_resp = &res,
2510 int status;
2512 nfs_fattr_init(res.fattr);
2513 nfs_fattr_init(res.dir_attr);
2514 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
2515 if (!status) {
2516 update_changeattr(dir, &res.cinfo);
2517 nfs_post_op_update_inode(dir, res.dir_attr);
2518 nfs_post_op_update_inode(inode, res.fattr);
2521 return status;
2524 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2526 struct nfs4_exception exception = { };
2527 int err;
2528 do {
2529 err = nfs4_handle_exception(NFS_SERVER(inode),
2530 _nfs4_proc_link(inode, dir, name),
2531 &exception);
2532 } while (exception.retry);
2533 return err;
2536 struct nfs4_createdata {
2537 struct rpc_message msg;
2538 struct nfs4_create_arg arg;
2539 struct nfs4_create_res res;
2540 struct nfs_fh fh;
2541 struct nfs_fattr fattr;
2542 struct nfs_fattr dir_fattr;
2545 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2546 struct qstr *name, struct iattr *sattr, u32 ftype)
2548 struct nfs4_createdata *data;
2550 data = kzalloc(sizeof(*data), GFP_KERNEL);
2551 if (data != NULL) {
2552 struct nfs_server *server = NFS_SERVER(dir);
2554 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2555 data->msg.rpc_argp = &data->arg;
2556 data->msg.rpc_resp = &data->res;
2557 data->arg.dir_fh = NFS_FH(dir);
2558 data->arg.server = server;
2559 data->arg.name = name;
2560 data->arg.attrs = sattr;
2561 data->arg.ftype = ftype;
2562 data->arg.bitmask = server->attr_bitmask;
2563 data->res.server = server;
2564 data->res.fh = &data->fh;
2565 data->res.fattr = &data->fattr;
2566 data->res.dir_fattr = &data->dir_fattr;
2567 nfs_fattr_init(data->res.fattr);
2568 nfs_fattr_init(data->res.dir_fattr);
2570 return data;
2573 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2575 int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2576 &data->arg, &data->res, 1);
2577 if (status == 0) {
2578 update_changeattr(dir, &data->res.dir_cinfo);
2579 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2580 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2582 return status;
2585 static void nfs4_free_createdata(struct nfs4_createdata *data)
2587 kfree(data);
2590 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2591 struct page *page, unsigned int len, struct iattr *sattr)
2593 struct nfs4_createdata *data;
2594 int status = -ENAMETOOLONG;
2596 if (len > NFS4_MAXPATHLEN)
2597 goto out;
2599 status = -ENOMEM;
2600 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2601 if (data == NULL)
2602 goto out;
2604 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2605 data->arg.u.symlink.pages = &page;
2606 data->arg.u.symlink.len = len;
2608 status = nfs4_do_create(dir, dentry, data);
2610 nfs4_free_createdata(data);
2611 out:
2612 return status;
2615 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2616 struct page *page, unsigned int len, struct iattr *sattr)
2618 struct nfs4_exception exception = { };
2619 int err;
2620 do {
2621 err = nfs4_handle_exception(NFS_SERVER(dir),
2622 _nfs4_proc_symlink(dir, dentry, page,
2623 len, sattr),
2624 &exception);
2625 } while (exception.retry);
2626 return err;
2629 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2630 struct iattr *sattr)
2632 struct nfs4_createdata *data;
2633 int status = -ENOMEM;
2635 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2636 if (data == NULL)
2637 goto out;
2639 status = nfs4_do_create(dir, dentry, data);
2641 nfs4_free_createdata(data);
2642 out:
2643 return status;
2646 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2647 struct iattr *sattr)
2649 struct nfs4_exception exception = { };
2650 int err;
2651 do {
2652 err = nfs4_handle_exception(NFS_SERVER(dir),
2653 _nfs4_proc_mkdir(dir, dentry, sattr),
2654 &exception);
2655 } while (exception.retry);
2656 return err;
2659 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2660 u64 cookie, struct page *page, unsigned int count, int plus)
2662 struct inode *dir = dentry->d_inode;
2663 struct nfs4_readdir_arg args = {
2664 .fh = NFS_FH(dir),
2665 .pages = &page,
2666 .pgbase = 0,
2667 .count = count,
2668 .bitmask = NFS_SERVER(dentry->d_inode)->cache_consistency_bitmask,
2670 struct nfs4_readdir_res res;
2671 struct rpc_message msg = {
2672 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2673 .rpc_argp = &args,
2674 .rpc_resp = &res,
2675 .rpc_cred = cred,
2677 int status;
2679 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
2680 dentry->d_parent->d_name.name,
2681 dentry->d_name.name,
2682 (unsigned long long)cookie);
2683 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2684 res.pgbase = args.pgbase;
2685 status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
2686 if (status == 0)
2687 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
2689 nfs_invalidate_atime(dir);
2691 dprintk("%s: returns %d\n", __func__, status);
2692 return status;
2695 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2696 u64 cookie, struct page *page, unsigned int count, int plus)
2698 struct nfs4_exception exception = { };
2699 int err;
2700 do {
2701 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2702 _nfs4_proc_readdir(dentry, cred, cookie,
2703 page, count, plus),
2704 &exception);
2705 } while (exception.retry);
2706 return err;
2709 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2710 struct iattr *sattr, dev_t rdev)
2712 struct nfs4_createdata *data;
2713 int mode = sattr->ia_mode;
2714 int status = -ENOMEM;
2716 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2717 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
2719 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2720 if (data == NULL)
2721 goto out;
2723 if (S_ISFIFO(mode))
2724 data->arg.ftype = NF4FIFO;
2725 else if (S_ISBLK(mode)) {
2726 data->arg.ftype = NF4BLK;
2727 data->arg.u.device.specdata1 = MAJOR(rdev);
2728 data->arg.u.device.specdata2 = MINOR(rdev);
2730 else if (S_ISCHR(mode)) {
2731 data->arg.ftype = NF4CHR;
2732 data->arg.u.device.specdata1 = MAJOR(rdev);
2733 data->arg.u.device.specdata2 = MINOR(rdev);
2736 status = nfs4_do_create(dir, dentry, data);
2738 nfs4_free_createdata(data);
2739 out:
2740 return status;
2743 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2744 struct iattr *sattr, dev_t rdev)
2746 struct nfs4_exception exception = { };
2747 int err;
2748 do {
2749 err = nfs4_handle_exception(NFS_SERVER(dir),
2750 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2751 &exception);
2752 } while (exception.retry);
2753 return err;
2756 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2757 struct nfs_fsstat *fsstat)
2759 struct nfs4_statfs_arg args = {
2760 .fh = fhandle,
2761 .bitmask = server->attr_bitmask,
2763 struct nfs4_statfs_res res = {
2764 .fsstat = fsstat,
2766 struct rpc_message msg = {
2767 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2768 .rpc_argp = &args,
2769 .rpc_resp = &res,
2772 nfs_fattr_init(fsstat->fattr);
2773 return nfs4_call_sync(server, &msg, &args, &res, 0);
2776 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2778 struct nfs4_exception exception = { };
2779 int err;
2780 do {
2781 err = nfs4_handle_exception(server,
2782 _nfs4_proc_statfs(server, fhandle, fsstat),
2783 &exception);
2784 } while (exception.retry);
2785 return err;
2788 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2789 struct nfs_fsinfo *fsinfo)
2791 struct nfs4_fsinfo_arg args = {
2792 .fh = fhandle,
2793 .bitmask = server->attr_bitmask,
2795 struct nfs4_fsinfo_res res = {
2796 .fsinfo = fsinfo,
2798 struct rpc_message msg = {
2799 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2800 .rpc_argp = &args,
2801 .rpc_resp = &res,
2804 return nfs4_call_sync(server, &msg, &args, &res, 0);
2807 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2809 struct nfs4_exception exception = { };
2810 int err;
2812 do {
2813 err = nfs4_handle_exception(server,
2814 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2815 &exception);
2816 } while (exception.retry);
2817 return err;
2820 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2822 nfs_fattr_init(fsinfo->fattr);
2823 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2826 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2827 struct nfs_pathconf *pathconf)
2829 struct nfs4_pathconf_arg args = {
2830 .fh = fhandle,
2831 .bitmask = server->attr_bitmask,
2833 struct nfs4_pathconf_res res = {
2834 .pathconf = pathconf,
2836 struct rpc_message msg = {
2837 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2838 .rpc_argp = &args,
2839 .rpc_resp = &res,
2842 /* None of the pathconf attributes are mandatory to implement */
2843 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
2844 memset(pathconf, 0, sizeof(*pathconf));
2845 return 0;
2848 nfs_fattr_init(pathconf->fattr);
2849 return nfs4_call_sync(server, &msg, &args, &res, 0);
2852 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2853 struct nfs_pathconf *pathconf)
2855 struct nfs4_exception exception = { };
2856 int err;
2858 do {
2859 err = nfs4_handle_exception(server,
2860 _nfs4_proc_pathconf(server, fhandle, pathconf),
2861 &exception);
2862 } while (exception.retry);
2863 return err;
2866 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
2868 struct nfs_server *server = NFS_SERVER(data->inode);
2870 dprintk("--> %s\n", __func__);
2872 /* nfs4_sequence_free_slot called in the read rpc_call_done */
2873 nfs4_sequence_done(server, &data->res.seq_res, task->tk_status);
2875 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
2876 rpc_restart_call(task);
2877 return -EAGAIN;
2880 nfs_invalidate_atime(data->inode);
2881 if (task->tk_status > 0)
2882 renew_lease(server, data->timestamp);
2883 return 0;
2886 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
2888 data->timestamp = jiffies;
2889 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
2892 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
2894 struct inode *inode = data->inode;
2896 /* slot is freed in nfs_writeback_done */
2897 nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
2898 task->tk_status);
2900 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
2901 rpc_restart_call(task);
2902 return -EAGAIN;
2904 if (task->tk_status >= 0) {
2905 renew_lease(NFS_SERVER(inode), data->timestamp);
2906 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
2908 return 0;
2911 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
2913 struct nfs_server *server = NFS_SERVER(data->inode);
2915 data->args.bitmask = server->cache_consistency_bitmask;
2916 data->res.server = server;
2917 data->timestamp = jiffies;
2919 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
2922 static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
2924 struct inode *inode = data->inode;
2926 nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
2927 task->tk_status);
2928 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
2929 rpc_restart_call(task);
2930 return -EAGAIN;
2932 nfs4_sequence_free_slot(NFS_SERVER(inode)->nfs_client,
2933 &data->res.seq_res);
2934 nfs_refresh_inode(inode, data->res.fattr);
2935 return 0;
2938 static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
2940 struct nfs_server *server = NFS_SERVER(data->inode);
2942 data->args.bitmask = server->cache_consistency_bitmask;
2943 data->res.server = server;
2944 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
2948 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
2949 * standalone procedure for queueing an asynchronous RENEW.
2951 static void nfs4_renew_done(struct rpc_task *task, void *data)
2953 struct nfs_client *clp = (struct nfs_client *)task->tk_msg.rpc_argp;
2954 unsigned long timestamp = (unsigned long)data;
2956 if (task->tk_status < 0) {
2957 /* Unless we're shutting down, schedule state recovery! */
2958 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
2959 nfs4_schedule_state_recovery(clp);
2960 return;
2962 spin_lock(&clp->cl_lock);
2963 if (time_before(clp->cl_last_renewal,timestamp))
2964 clp->cl_last_renewal = timestamp;
2965 spin_unlock(&clp->cl_lock);
2968 static const struct rpc_call_ops nfs4_renew_ops = {
2969 .rpc_call_done = nfs4_renew_done,
2972 int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
2974 struct rpc_message msg = {
2975 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2976 .rpc_argp = clp,
2977 .rpc_cred = cred,
2980 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
2981 &nfs4_renew_ops, (void *)jiffies);
2984 int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
2986 struct rpc_message msg = {
2987 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2988 .rpc_argp = clp,
2989 .rpc_cred = cred,
2991 unsigned long now = jiffies;
2992 int status;
2994 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2995 if (status < 0)
2996 return status;
2997 spin_lock(&clp->cl_lock);
2998 if (time_before(clp->cl_last_renewal,now))
2999 clp->cl_last_renewal = now;
3000 spin_unlock(&clp->cl_lock);
3001 return 0;
3004 static inline int nfs4_server_supports_acls(struct nfs_server *server)
3006 return (server->caps & NFS_CAP_ACLS)
3007 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3008 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3011 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3012 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3013 * the stack.
3015 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3017 static void buf_to_pages(const void *buf, size_t buflen,
3018 struct page **pages, unsigned int *pgbase)
3020 const void *p = buf;
3022 *pgbase = offset_in_page(buf);
3023 p -= *pgbase;
3024 while (p < buf + buflen) {
3025 *(pages++) = virt_to_page(p);
3026 p += PAGE_CACHE_SIZE;
3030 struct nfs4_cached_acl {
3031 int cached;
3032 size_t len;
3033 char data[0];
3036 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
3038 struct nfs_inode *nfsi = NFS_I(inode);
3040 spin_lock(&inode->i_lock);
3041 kfree(nfsi->nfs4_acl);
3042 nfsi->nfs4_acl = acl;
3043 spin_unlock(&inode->i_lock);
3046 static void nfs4_zap_acl_attr(struct inode *inode)
3048 nfs4_set_cached_acl(inode, NULL);
3051 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3053 struct nfs_inode *nfsi = NFS_I(inode);
3054 struct nfs4_cached_acl *acl;
3055 int ret = -ENOENT;
3057 spin_lock(&inode->i_lock);
3058 acl = nfsi->nfs4_acl;
3059 if (acl == NULL)
3060 goto out;
3061 if (buf == NULL) /* user is just asking for length */
3062 goto out_len;
3063 if (acl->cached == 0)
3064 goto out;
3065 ret = -ERANGE; /* see getxattr(2) man page */
3066 if (acl->len > buflen)
3067 goto out;
3068 memcpy(buf, acl->data, acl->len);
3069 out_len:
3070 ret = acl->len;
3071 out:
3072 spin_unlock(&inode->i_lock);
3073 return ret;
3076 static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3078 struct nfs4_cached_acl *acl;
3080 if (buf && acl_len <= PAGE_SIZE) {
3081 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3082 if (acl == NULL)
3083 goto out;
3084 acl->cached = 1;
3085 memcpy(acl->data, buf, acl_len);
3086 } else {
3087 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3088 if (acl == NULL)
3089 goto out;
3090 acl->cached = 0;
3092 acl->len = acl_len;
3093 out:
3094 nfs4_set_cached_acl(inode, acl);
3097 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3099 struct page *pages[NFS4ACL_MAXPAGES];
3100 struct nfs_getaclargs args = {
3101 .fh = NFS_FH(inode),
3102 .acl_pages = pages,
3103 .acl_len = buflen,
3105 struct nfs_getaclres res = {
3106 .acl_len = buflen,
3108 void *resp_buf;
3109 struct rpc_message msg = {
3110 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3111 .rpc_argp = &args,
3112 .rpc_resp = &res,
3114 struct page *localpage = NULL;
3115 int ret;
3117 if (buflen < PAGE_SIZE) {
3118 /* As long as we're doing a round trip to the server anyway,
3119 * let's be prepared for a page of acl data. */
3120 localpage = alloc_page(GFP_KERNEL);
3121 resp_buf = page_address(localpage);
3122 if (localpage == NULL)
3123 return -ENOMEM;
3124 args.acl_pages[0] = localpage;
3125 args.acl_pgbase = 0;
3126 args.acl_len = PAGE_SIZE;
3127 } else {
3128 resp_buf = buf;
3129 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3131 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
3132 if (ret)
3133 goto out_free;
3134 if (res.acl_len > args.acl_len)
3135 nfs4_write_cached_acl(inode, NULL, res.acl_len);
3136 else
3137 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
3138 if (buf) {
3139 ret = -ERANGE;
3140 if (res.acl_len > buflen)
3141 goto out_free;
3142 if (localpage)
3143 memcpy(buf, resp_buf, res.acl_len);
3145 ret = res.acl_len;
3146 out_free:
3147 if (localpage)
3148 __free_page(localpage);
3149 return ret;
3152 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3154 struct nfs4_exception exception = { };
3155 ssize_t ret;
3156 do {
3157 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3158 if (ret >= 0)
3159 break;
3160 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3161 } while (exception.retry);
3162 return ret;
3165 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3167 struct nfs_server *server = NFS_SERVER(inode);
3168 int ret;
3170 if (!nfs4_server_supports_acls(server))
3171 return -EOPNOTSUPP;
3172 ret = nfs_revalidate_inode(server, inode);
3173 if (ret < 0)
3174 return ret;
3175 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3176 nfs_zap_acl_cache(inode);
3177 ret = nfs4_read_cached_acl(inode, buf, buflen);
3178 if (ret != -ENOENT)
3179 return ret;
3180 return nfs4_get_acl_uncached(inode, buf, buflen);
3183 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3185 struct nfs_server *server = NFS_SERVER(inode);
3186 struct page *pages[NFS4ACL_MAXPAGES];
3187 struct nfs_setaclargs arg = {
3188 .fh = NFS_FH(inode),
3189 .acl_pages = pages,
3190 .acl_len = buflen,
3192 struct nfs_setaclres res;
3193 struct rpc_message msg = {
3194 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3195 .rpc_argp = &arg,
3196 .rpc_resp = &res,
3198 int ret;
3200 if (!nfs4_server_supports_acls(server))
3201 return -EOPNOTSUPP;
3202 nfs_inode_return_delegation(inode);
3203 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3204 ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
3205 nfs_access_zap_cache(inode);
3206 nfs_zap_acl_cache(inode);
3207 return ret;
3210 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3212 struct nfs4_exception exception = { };
3213 int err;
3214 do {
3215 err = nfs4_handle_exception(NFS_SERVER(inode),
3216 __nfs4_proc_set_acl(inode, buf, buflen),
3217 &exception);
3218 } while (exception.retry);
3219 return err;
3222 static int
3223 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3225 struct nfs_client *clp = server->nfs_client;
3227 if (!clp || task->tk_status >= 0)
3228 return 0;
3229 switch(task->tk_status) {
3230 case -NFS4ERR_ADMIN_REVOKED:
3231 case -NFS4ERR_BAD_STATEID:
3232 case -NFS4ERR_OPENMODE:
3233 if (state == NULL)
3234 break;
3235 nfs4_state_mark_reclaim_nograce(clp, state);
3236 case -NFS4ERR_STALE_CLIENTID:
3237 case -NFS4ERR_STALE_STATEID:
3238 case -NFS4ERR_EXPIRED:
3239 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3240 nfs4_schedule_state_recovery(clp);
3241 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3242 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3243 task->tk_status = 0;
3244 return -EAGAIN;
3245 case -NFS4ERR_DELAY:
3246 nfs_inc_server_stats(server, NFSIOS_DELAY);
3247 case -NFS4ERR_GRACE:
3248 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3249 task->tk_status = 0;
3250 return -EAGAIN;
3251 case -NFS4ERR_OLD_STATEID:
3252 task->tk_status = 0;
3253 return -EAGAIN;
3255 task->tk_status = nfs4_map_errors(task->tk_status);
3256 return 0;
3259 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, unsigned short port, struct rpc_cred *cred)
3261 nfs4_verifier sc_verifier;
3262 struct nfs4_setclientid setclientid = {
3263 .sc_verifier = &sc_verifier,
3264 .sc_prog = program,
3266 struct rpc_message msg = {
3267 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3268 .rpc_argp = &setclientid,
3269 .rpc_resp = clp,
3270 .rpc_cred = cred,
3272 __be32 *p;
3273 int loop = 0;
3274 int status;
3276 p = (__be32*)sc_verifier.data;
3277 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3278 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3280 for(;;) {
3281 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
3282 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
3283 clp->cl_ipaddr,
3284 rpc_peeraddr2str(clp->cl_rpcclient,
3285 RPC_DISPLAY_ADDR),
3286 rpc_peeraddr2str(clp->cl_rpcclient,
3287 RPC_DISPLAY_PROTO),
3288 clp->cl_rpcclient->cl_auth->au_ops->au_name,
3289 clp->cl_id_uniquifier);
3290 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
3291 sizeof(setclientid.sc_netid),
3292 rpc_peeraddr2str(clp->cl_rpcclient,
3293 RPC_DISPLAY_NETID));
3294 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
3295 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
3296 clp->cl_ipaddr, port >> 8, port & 255);
3298 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3299 if (status != -NFS4ERR_CLID_INUSE)
3300 break;
3301 if (signalled())
3302 break;
3303 if (loop++ & 1)
3304 ssleep(clp->cl_lease_time + 1);
3305 else
3306 if (++clp->cl_id_uniquifier == 0)
3307 break;
3309 return status;
3312 static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
3314 struct nfs_fsinfo fsinfo;
3315 struct rpc_message msg = {
3316 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
3317 .rpc_argp = clp,
3318 .rpc_resp = &fsinfo,
3319 .rpc_cred = cred,
3321 unsigned long now;
3322 int status;
3324 now = jiffies;
3325 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3326 if (status == 0) {
3327 spin_lock(&clp->cl_lock);
3328 clp->cl_lease_time = fsinfo.lease_time * HZ;
3329 clp->cl_last_renewal = now;
3330 spin_unlock(&clp->cl_lock);
3332 return status;
3335 int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
3337 long timeout = 0;
3338 int err;
3339 do {
3340 err = _nfs4_proc_setclientid_confirm(clp, cred);
3341 switch (err) {
3342 case 0:
3343 return err;
3344 case -NFS4ERR_RESOURCE:
3345 /* The IBM lawyers misread another document! */
3346 case -NFS4ERR_DELAY:
3347 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3349 } while (err == 0);
3350 return err;
3353 struct nfs4_delegreturndata {
3354 struct nfs4_delegreturnargs args;
3355 struct nfs4_delegreturnres res;
3356 struct nfs_fh fh;
3357 nfs4_stateid stateid;
3358 unsigned long timestamp;
3359 struct nfs_fattr fattr;
3360 int rpc_status;
3363 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3365 struct nfs4_delegreturndata *data = calldata;
3367 nfs4_sequence_done_free_slot(data->res.server, &data->res.seq_res,
3368 task->tk_status);
3370 data->rpc_status = task->tk_status;
3371 if (data->rpc_status == 0)
3372 renew_lease(data->res.server, data->timestamp);
3375 static void nfs4_delegreturn_release(void *calldata)
3377 kfree(calldata);
3380 #if defined(CONFIG_NFS_V4_1)
3381 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3383 struct nfs4_delegreturndata *d_data;
3385 d_data = (struct nfs4_delegreturndata *)data;
3387 if (nfs4_setup_sequence(d_data->res.server->nfs_client,
3388 &d_data->args.seq_args,
3389 &d_data->res.seq_res, 1, task))
3390 return;
3391 rpc_call_start(task);
3393 #endif /* CONFIG_NFS_V4_1 */
3395 static const struct rpc_call_ops nfs4_delegreturn_ops = {
3396 #if defined(CONFIG_NFS_V4_1)
3397 .rpc_call_prepare = nfs4_delegreturn_prepare,
3398 #endif /* CONFIG_NFS_V4_1 */
3399 .rpc_call_done = nfs4_delegreturn_done,
3400 .rpc_release = nfs4_delegreturn_release,
3403 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3405 struct nfs4_delegreturndata *data;
3406 struct nfs_server *server = NFS_SERVER(inode);
3407 struct rpc_task *task;
3408 struct rpc_message msg = {
3409 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3410 .rpc_cred = cred,
3412 struct rpc_task_setup task_setup_data = {
3413 .rpc_client = server->client,
3414 .rpc_message = &msg,
3415 .callback_ops = &nfs4_delegreturn_ops,
3416 .flags = RPC_TASK_ASYNC,
3418 int status = 0;
3420 data = kzalloc(sizeof(*data), GFP_KERNEL);
3421 if (data == NULL)
3422 return -ENOMEM;
3423 data->args.fhandle = &data->fh;
3424 data->args.stateid = &data->stateid;
3425 data->args.bitmask = server->attr_bitmask;
3426 nfs_copy_fh(&data->fh, NFS_FH(inode));
3427 memcpy(&data->stateid, stateid, sizeof(data->stateid));
3428 data->res.fattr = &data->fattr;
3429 data->res.server = server;
3430 data->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
3431 nfs_fattr_init(data->res.fattr);
3432 data->timestamp = jiffies;
3433 data->rpc_status = 0;
3435 task_setup_data.callback_data = data;
3436 msg.rpc_argp = &data->args,
3437 msg.rpc_resp = &data->res,
3438 task = rpc_run_task(&task_setup_data);
3439 if (IS_ERR(task))
3440 return PTR_ERR(task);
3441 if (!issync)
3442 goto out;
3443 status = nfs4_wait_for_completion_rpc_task(task);
3444 if (status != 0)
3445 goto out;
3446 status = data->rpc_status;
3447 if (status != 0)
3448 goto out;
3449 nfs_refresh_inode(inode, &data->fattr);
3450 out:
3451 rpc_put_task(task);
3452 return status;
3455 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3457 struct nfs_server *server = NFS_SERVER(inode);
3458 struct nfs4_exception exception = { };
3459 int err;
3460 do {
3461 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
3462 switch (err) {
3463 case -NFS4ERR_STALE_STATEID:
3464 case -NFS4ERR_EXPIRED:
3465 case 0:
3466 return 0;
3468 err = nfs4_handle_exception(server, err, &exception);
3469 } while (exception.retry);
3470 return err;
3473 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3474 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3477 * sleep, with exponential backoff, and retry the LOCK operation.
3479 static unsigned long
3480 nfs4_set_lock_task_retry(unsigned long timeout)
3482 schedule_timeout_killable(timeout);
3483 timeout <<= 1;
3484 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3485 return NFS4_LOCK_MAXTIMEOUT;
3486 return timeout;
3489 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3491 struct inode *inode = state->inode;
3492 struct nfs_server *server = NFS_SERVER(inode);
3493 struct nfs_client *clp = server->nfs_client;
3494 struct nfs_lockt_args arg = {
3495 .fh = NFS_FH(inode),
3496 .fl = request,
3498 struct nfs_lockt_res res = {
3499 .denied = request,
3501 struct rpc_message msg = {
3502 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3503 .rpc_argp = &arg,
3504 .rpc_resp = &res,
3505 .rpc_cred = state->owner->so_cred,
3507 struct nfs4_lock_state *lsp;
3508 int status;
3510 arg.lock_owner.clientid = clp->cl_clientid;
3511 status = nfs4_set_lock_state(state, request);
3512 if (status != 0)
3513 goto out;
3514 lsp = request->fl_u.nfs4_fl.owner;
3515 arg.lock_owner.id = lsp->ls_id.id;
3516 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
3517 switch (status) {
3518 case 0:
3519 request->fl_type = F_UNLCK;
3520 break;
3521 case -NFS4ERR_DENIED:
3522 status = 0;
3524 request->fl_ops->fl_release_private(request);
3525 out:
3526 return status;
3529 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3531 struct nfs4_exception exception = { };
3532 int err;
3534 do {
3535 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3536 _nfs4_proc_getlk(state, cmd, request),
3537 &exception);
3538 } while (exception.retry);
3539 return err;
3542 static int do_vfs_lock(struct file *file, struct file_lock *fl)
3544 int res = 0;
3545 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3546 case FL_POSIX:
3547 res = posix_lock_file_wait(file, fl);
3548 break;
3549 case FL_FLOCK:
3550 res = flock_lock_file_wait(file, fl);
3551 break;
3552 default:
3553 BUG();
3555 return res;
3558 struct nfs4_unlockdata {
3559 struct nfs_locku_args arg;
3560 struct nfs_locku_res res;
3561 struct nfs4_lock_state *lsp;
3562 struct nfs_open_context *ctx;
3563 struct file_lock fl;
3564 const struct nfs_server *server;
3565 unsigned long timestamp;
3568 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3569 struct nfs_open_context *ctx,
3570 struct nfs4_lock_state *lsp,
3571 struct nfs_seqid *seqid)
3573 struct nfs4_unlockdata *p;
3574 struct inode *inode = lsp->ls_state->inode;
3576 p = kzalloc(sizeof(*p), GFP_KERNEL);
3577 if (p == NULL)
3578 return NULL;
3579 p->arg.fh = NFS_FH(inode);
3580 p->arg.fl = &p->fl;
3581 p->arg.seqid = seqid;
3582 p->res.seqid = seqid;
3583 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
3584 p->arg.stateid = &lsp->ls_stateid;
3585 p->lsp = lsp;
3586 atomic_inc(&lsp->ls_count);
3587 /* Ensure we don't close file until we're done freeing locks! */
3588 p->ctx = get_nfs_open_context(ctx);
3589 memcpy(&p->fl, fl, sizeof(p->fl));
3590 p->server = NFS_SERVER(inode);
3591 return p;
3594 static void nfs4_locku_release_calldata(void *data)
3596 struct nfs4_unlockdata *calldata = data;
3597 nfs_free_seqid(calldata->arg.seqid);
3598 nfs4_put_lock_state(calldata->lsp);
3599 put_nfs_open_context(calldata->ctx);
3600 kfree(calldata);
3603 static void nfs4_locku_done(struct rpc_task *task, void *data)
3605 struct nfs4_unlockdata *calldata = data;
3607 nfs4_sequence_done(calldata->server, &calldata->res.seq_res,
3608 task->tk_status);
3609 if (RPC_ASSASSINATED(task))
3610 return;
3611 switch (task->tk_status) {
3612 case 0:
3613 memcpy(calldata->lsp->ls_stateid.data,
3614 calldata->res.stateid.data,
3615 sizeof(calldata->lsp->ls_stateid.data));
3616 renew_lease(calldata->server, calldata->timestamp);
3617 break;
3618 case -NFS4ERR_BAD_STATEID:
3619 case -NFS4ERR_OLD_STATEID:
3620 case -NFS4ERR_STALE_STATEID:
3621 case -NFS4ERR_EXPIRED:
3622 break;
3623 default:
3624 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
3625 rpc_restart_call(task);
3627 nfs4_sequence_free_slot(calldata->server->nfs_client,
3628 &calldata->res.seq_res);
3631 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
3633 struct nfs4_unlockdata *calldata = data;
3635 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3636 return;
3637 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
3638 /* Note: exit _without_ running nfs4_locku_done */
3639 task->tk_action = NULL;
3640 return;
3642 calldata->timestamp = jiffies;
3643 if (nfs4_setup_sequence(calldata->server->nfs_client,
3644 &calldata->arg.seq_args,
3645 &calldata->res.seq_res, 1, task))
3646 return;
3647 rpc_call_start(task);
3650 static const struct rpc_call_ops nfs4_locku_ops = {
3651 .rpc_call_prepare = nfs4_locku_prepare,
3652 .rpc_call_done = nfs4_locku_done,
3653 .rpc_release = nfs4_locku_release_calldata,
3656 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3657 struct nfs_open_context *ctx,
3658 struct nfs4_lock_state *lsp,
3659 struct nfs_seqid *seqid)
3661 struct nfs4_unlockdata *data;
3662 struct rpc_message msg = {
3663 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3664 .rpc_cred = ctx->cred,
3666 struct rpc_task_setup task_setup_data = {
3667 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
3668 .rpc_message = &msg,
3669 .callback_ops = &nfs4_locku_ops,
3670 .workqueue = nfsiod_workqueue,
3671 .flags = RPC_TASK_ASYNC,
3674 /* Ensure this is an unlock - when canceling a lock, the
3675 * canceled lock is passed in, and it won't be an unlock.
3677 fl->fl_type = F_UNLCK;
3679 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3680 if (data == NULL) {
3681 nfs_free_seqid(seqid);
3682 return ERR_PTR(-ENOMEM);
3685 msg.rpc_argp = &data->arg,
3686 msg.rpc_resp = &data->res,
3687 task_setup_data.callback_data = data;
3688 return rpc_run_task(&task_setup_data);
3691 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3693 struct nfs_inode *nfsi = NFS_I(state->inode);
3694 struct nfs_seqid *seqid;
3695 struct nfs4_lock_state *lsp;
3696 struct rpc_task *task;
3697 int status = 0;
3698 unsigned char fl_flags = request->fl_flags;
3700 status = nfs4_set_lock_state(state, request);
3701 /* Unlock _before_ we do the RPC call */
3702 request->fl_flags |= FL_EXISTS;
3703 down_read(&nfsi->rwsem);
3704 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
3705 up_read(&nfsi->rwsem);
3706 goto out;
3708 up_read(&nfsi->rwsem);
3709 if (status != 0)
3710 goto out;
3711 /* Is this a delegated lock? */
3712 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
3713 goto out;
3714 lsp = request->fl_u.nfs4_fl.owner;
3715 seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3716 status = -ENOMEM;
3717 if (seqid == NULL)
3718 goto out;
3719 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
3720 status = PTR_ERR(task);
3721 if (IS_ERR(task))
3722 goto out;
3723 status = nfs4_wait_for_completion_rpc_task(task);
3724 rpc_put_task(task);
3725 out:
3726 request->fl_flags = fl_flags;
3727 return status;
3730 struct nfs4_lockdata {
3731 struct nfs_lock_args arg;
3732 struct nfs_lock_res res;
3733 struct nfs4_lock_state *lsp;
3734 struct nfs_open_context *ctx;
3735 struct file_lock fl;
3736 unsigned long timestamp;
3737 int rpc_status;
3738 int cancelled;
3739 struct nfs_server *server;
3742 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
3743 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
3745 struct nfs4_lockdata *p;
3746 struct inode *inode = lsp->ls_state->inode;
3747 struct nfs_server *server = NFS_SERVER(inode);
3749 p = kzalloc(sizeof(*p), GFP_KERNEL);
3750 if (p == NULL)
3751 return NULL;
3753 p->arg.fh = NFS_FH(inode);
3754 p->arg.fl = &p->fl;
3755 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid);
3756 if (p->arg.open_seqid == NULL)
3757 goto out_free;
3758 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3759 if (p->arg.lock_seqid == NULL)
3760 goto out_free_seqid;
3761 p->arg.lock_stateid = &lsp->ls_stateid;
3762 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
3763 p->arg.lock_owner.id = lsp->ls_id.id;
3764 p->res.lock_seqid = p->arg.lock_seqid;
3765 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
3766 p->lsp = lsp;
3767 p->server = server;
3768 atomic_inc(&lsp->ls_count);
3769 p->ctx = get_nfs_open_context(ctx);
3770 memcpy(&p->fl, fl, sizeof(p->fl));
3771 return p;
3772 out_free_seqid:
3773 nfs_free_seqid(p->arg.open_seqid);
3774 out_free:
3775 kfree(p);
3776 return NULL;
3779 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
3781 struct nfs4_lockdata *data = calldata;
3782 struct nfs4_state *state = data->lsp->ls_state;
3784 dprintk("%s: begin!\n", __func__);
3785 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
3786 return;
3787 /* Do we need to do an open_to_lock_owner? */
3788 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
3789 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
3790 return;
3791 data->arg.open_stateid = &state->stateid;
3792 data->arg.new_lock_owner = 1;
3793 data->res.open_seqid = data->arg.open_seqid;
3794 } else
3795 data->arg.new_lock_owner = 0;
3796 data->timestamp = jiffies;
3797 if (nfs4_setup_sequence(data->server->nfs_client, &data->arg.seq_args,
3798 &data->res.seq_res, 1, task))
3799 return;
3800 rpc_call_start(task);
3801 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
3804 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
3806 struct nfs4_lockdata *data = calldata;
3808 dprintk("%s: begin!\n", __func__);
3810 nfs4_sequence_done_free_slot(data->server, &data->res.seq_res,
3811 task->tk_status);
3813 data->rpc_status = task->tk_status;
3814 if (RPC_ASSASSINATED(task))
3815 goto out;
3816 if (data->arg.new_lock_owner != 0) {
3817 if (data->rpc_status == 0)
3818 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
3819 else
3820 goto out;
3822 if (data->rpc_status == 0) {
3823 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
3824 sizeof(data->lsp->ls_stateid.data));
3825 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
3826 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
3828 out:
3829 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
3832 static void nfs4_lock_release(void *calldata)
3834 struct nfs4_lockdata *data = calldata;
3836 dprintk("%s: begin!\n", __func__);
3837 nfs_free_seqid(data->arg.open_seqid);
3838 if (data->cancelled != 0) {
3839 struct rpc_task *task;
3840 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
3841 data->arg.lock_seqid);
3842 if (!IS_ERR(task))
3843 rpc_put_task(task);
3844 dprintk("%s: cancelling lock!\n", __func__);
3845 } else
3846 nfs_free_seqid(data->arg.lock_seqid);
3847 nfs4_put_lock_state(data->lsp);
3848 put_nfs_open_context(data->ctx);
3849 kfree(data);
3850 dprintk("%s: done!\n", __func__);
3853 static const struct rpc_call_ops nfs4_lock_ops = {
3854 .rpc_call_prepare = nfs4_lock_prepare,
3855 .rpc_call_done = nfs4_lock_done,
3856 .rpc_release = nfs4_lock_release,
3859 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int reclaim)
3861 struct nfs4_lockdata *data;
3862 struct rpc_task *task;
3863 struct rpc_message msg = {
3864 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
3865 .rpc_cred = state->owner->so_cred,
3867 struct rpc_task_setup task_setup_data = {
3868 .rpc_client = NFS_CLIENT(state->inode),
3869 .rpc_message = &msg,
3870 .callback_ops = &nfs4_lock_ops,
3871 .workqueue = nfsiod_workqueue,
3872 .flags = RPC_TASK_ASYNC,
3874 int ret;
3876 dprintk("%s: begin!\n", __func__);
3877 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
3878 fl->fl_u.nfs4_fl.owner);
3879 if (data == NULL)
3880 return -ENOMEM;
3881 if (IS_SETLKW(cmd))
3882 data->arg.block = 1;
3883 if (reclaim != 0)
3884 data->arg.reclaim = 1;
3885 msg.rpc_argp = &data->arg,
3886 msg.rpc_resp = &data->res,
3887 task_setup_data.callback_data = data;
3888 task = rpc_run_task(&task_setup_data);
3889 if (IS_ERR(task))
3890 return PTR_ERR(task);
3891 ret = nfs4_wait_for_completion_rpc_task(task);
3892 if (ret == 0) {
3893 ret = data->rpc_status;
3894 if (ret == -NFS4ERR_DENIED)
3895 ret = -EAGAIN;
3896 } else
3897 data->cancelled = 1;
3898 rpc_put_task(task);
3899 dprintk("%s: done, ret = %d!\n", __func__, ret);
3900 return ret;
3903 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
3905 struct nfs_server *server = NFS_SERVER(state->inode);
3906 struct nfs4_exception exception = { };
3907 int err;
3909 do {
3910 /* Cache the lock if possible... */
3911 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3912 return 0;
3913 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
3914 if (err != -NFS4ERR_DELAY)
3915 break;
3916 nfs4_handle_exception(server, err, &exception);
3917 } while (exception.retry);
3918 return err;
3921 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
3923 struct nfs_server *server = NFS_SERVER(state->inode);
3924 struct nfs4_exception exception = { };
3925 int err;
3927 err = nfs4_set_lock_state(state, request);
3928 if (err != 0)
3929 return err;
3930 do {
3931 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3932 return 0;
3933 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
3934 if (err != -NFS4ERR_DELAY)
3935 break;
3936 nfs4_handle_exception(server, err, &exception);
3937 } while (exception.retry);
3938 return err;
3941 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3943 struct nfs_inode *nfsi = NFS_I(state->inode);
3944 unsigned char fl_flags = request->fl_flags;
3945 int status;
3947 /* Is this a delegated open? */
3948 status = nfs4_set_lock_state(state, request);
3949 if (status != 0)
3950 goto out;
3951 request->fl_flags |= FL_ACCESS;
3952 status = do_vfs_lock(request->fl_file, request);
3953 if (status < 0)
3954 goto out;
3955 down_read(&nfsi->rwsem);
3956 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
3957 /* Yes: cache locks! */
3958 /* ...but avoid races with delegation recall... */
3959 request->fl_flags = fl_flags & ~FL_SLEEP;
3960 status = do_vfs_lock(request->fl_file, request);
3961 goto out_unlock;
3963 status = _nfs4_do_setlk(state, cmd, request, 0);
3964 if (status != 0)
3965 goto out_unlock;
3966 /* Note: we always want to sleep here! */
3967 request->fl_flags = fl_flags | FL_SLEEP;
3968 if (do_vfs_lock(request->fl_file, request) < 0)
3969 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
3970 out_unlock:
3971 up_read(&nfsi->rwsem);
3972 out:
3973 request->fl_flags = fl_flags;
3974 return status;
3977 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3979 struct nfs4_exception exception = { };
3980 int err;
3982 do {
3983 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3984 _nfs4_proc_setlk(state, cmd, request),
3985 &exception);
3986 } while (exception.retry);
3987 return err;
3990 static int
3991 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
3993 struct nfs_open_context *ctx;
3994 struct nfs4_state *state;
3995 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
3996 int status;
3998 /* verify open state */
3999 ctx = nfs_file_open_context(filp);
4000 state = ctx->state;
4002 if (request->fl_start < 0 || request->fl_end < 0)
4003 return -EINVAL;
4005 if (IS_GETLK(cmd))
4006 return nfs4_proc_getlk(state, F_GETLK, request);
4008 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4009 return -EINVAL;
4011 if (request->fl_type == F_UNLCK)
4012 return nfs4_proc_unlck(state, cmd, request);
4014 do {
4015 status = nfs4_proc_setlk(state, cmd, request);
4016 if ((status != -EAGAIN) || IS_SETLK(cmd))
4017 break;
4018 timeout = nfs4_set_lock_task_retry(timeout);
4019 status = -ERESTARTSYS;
4020 if (signalled())
4021 break;
4022 } while(status < 0);
4023 return status;
4026 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4028 struct nfs_server *server = NFS_SERVER(state->inode);
4029 struct nfs4_exception exception = { };
4030 int err;
4032 err = nfs4_set_lock_state(state, fl);
4033 if (err != 0)
4034 goto out;
4035 do {
4036 err = _nfs4_do_setlk(state, F_SETLK, fl, 0);
4037 if (err != -NFS4ERR_DELAY)
4038 break;
4039 err = nfs4_handle_exception(server, err, &exception);
4040 } while (exception.retry);
4041 out:
4042 return err;
4045 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4047 int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
4048 size_t buflen, int flags)
4050 struct inode *inode = dentry->d_inode;
4052 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4053 return -EOPNOTSUPP;
4055 return nfs4_proc_set_acl(inode, buf, buflen);
4058 /* The getxattr man page suggests returning -ENODATA for unknown attributes,
4059 * and that's what we'll do for e.g. user attributes that haven't been set.
4060 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
4061 * attributes in kernel-managed attribute namespaces. */
4062 ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
4063 size_t buflen)
4065 struct inode *inode = dentry->d_inode;
4067 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4068 return -EOPNOTSUPP;
4070 return nfs4_proc_get_acl(inode, buf, buflen);
4073 ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
4075 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
4077 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4078 return 0;
4079 if (buf && buflen < len)
4080 return -ERANGE;
4081 if (buf)
4082 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
4083 return len;
4086 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4088 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4089 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4090 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4091 return;
4093 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4094 NFS_ATTR_FATTR_NLINK;
4095 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4096 fattr->nlink = 2;
4099 int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
4100 struct nfs4_fs_locations *fs_locations, struct page *page)
4102 struct nfs_server *server = NFS_SERVER(dir);
4103 u32 bitmask[2] = {
4104 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4105 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
4107 struct nfs4_fs_locations_arg args = {
4108 .dir_fh = NFS_FH(dir),
4109 .name = name,
4110 .page = page,
4111 .bitmask = bitmask,
4113 struct nfs4_fs_locations_res res = {
4114 .fs_locations = fs_locations,
4116 struct rpc_message msg = {
4117 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4118 .rpc_argp = &args,
4119 .rpc_resp = &res,
4121 int status;
4123 dprintk("%s: start\n", __func__);
4124 nfs_fattr_init(&fs_locations->fattr);
4125 fs_locations->server = server;
4126 fs_locations->nlocations = 0;
4127 status = nfs4_call_sync(server, &msg, &args, &res, 0);
4128 nfs_fixup_referral_attributes(&fs_locations->fattr);
4129 dprintk("%s: returned status = %d\n", __func__, status);
4130 return status;
4133 #ifdef CONFIG_NFS_V4_1
4135 * nfs4_proc_exchange_id()
4137 * Since the clientid has expired, all compounds using sessions
4138 * associated with the stale clientid will be returning
4139 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4140 * be in some phase of session reset.
4142 static int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
4144 nfs4_verifier verifier;
4145 struct nfs41_exchange_id_args args = {
4146 .client = clp,
4147 .flags = clp->cl_exchange_flags,
4149 struct nfs41_exchange_id_res res = {
4150 .client = clp,
4152 int status;
4153 struct rpc_message msg = {
4154 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4155 .rpc_argp = &args,
4156 .rpc_resp = &res,
4157 .rpc_cred = cred,
4159 __be32 *p;
4161 dprintk("--> %s\n", __func__);
4162 BUG_ON(clp == NULL);
4163 p = (u32 *)verifier.data;
4164 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4165 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4166 args.verifier = &verifier;
4168 while (1) {
4169 args.id_len = scnprintf(args.id, sizeof(args.id),
4170 "%s/%s %u",
4171 clp->cl_ipaddr,
4172 rpc_peeraddr2str(clp->cl_rpcclient,
4173 RPC_DISPLAY_ADDR),
4174 clp->cl_id_uniquifier);
4176 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4178 if (status != NFS4ERR_CLID_INUSE)
4179 break;
4181 if (signalled())
4182 break;
4184 if (++clp->cl_id_uniquifier == 0)
4185 break;
4188 dprintk("<-- %s status= %d\n", __func__, status);
4189 return status;
4192 struct nfs4_get_lease_time_data {
4193 struct nfs4_get_lease_time_args *args;
4194 struct nfs4_get_lease_time_res *res;
4195 struct nfs_client *clp;
4198 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4199 void *calldata)
4201 int ret;
4202 struct nfs4_get_lease_time_data *data =
4203 (struct nfs4_get_lease_time_data *)calldata;
4205 dprintk("--> %s\n", __func__);
4206 /* just setup sequence, do not trigger session recovery
4207 since we're invoked within one */
4208 ret = nfs41_setup_sequence(data->clp->cl_session,
4209 &data->args->la_seq_args,
4210 &data->res->lr_seq_res, 0, task);
4212 BUG_ON(ret == -EAGAIN);
4213 rpc_call_start(task);
4214 dprintk("<-- %s\n", __func__);
4218 * Called from nfs4_state_manager thread for session setup, so don't recover
4219 * from sequence operation or clientid errors.
4221 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4223 struct nfs4_get_lease_time_data *data =
4224 (struct nfs4_get_lease_time_data *)calldata;
4226 dprintk("--> %s\n", __func__);
4227 nfs41_sequence_done(data->clp, &data->res->lr_seq_res, task->tk_status);
4228 switch (task->tk_status) {
4229 case -NFS4ERR_DELAY:
4230 case -NFS4ERR_GRACE:
4231 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4232 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4233 task->tk_status = 0;
4234 rpc_restart_call(task);
4235 return;
4237 nfs41_sequence_free_slot(data->clp, &data->res->lr_seq_res);
4238 dprintk("<-- %s\n", __func__);
4241 struct rpc_call_ops nfs4_get_lease_time_ops = {
4242 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4243 .rpc_call_done = nfs4_get_lease_time_done,
4246 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4248 struct rpc_task *task;
4249 struct nfs4_get_lease_time_args args;
4250 struct nfs4_get_lease_time_res res = {
4251 .lr_fsinfo = fsinfo,
4253 struct nfs4_get_lease_time_data data = {
4254 .args = &args,
4255 .res = &res,
4256 .clp = clp,
4258 struct rpc_message msg = {
4259 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4260 .rpc_argp = &args,
4261 .rpc_resp = &res,
4263 struct rpc_task_setup task_setup = {
4264 .rpc_client = clp->cl_rpcclient,
4265 .rpc_message = &msg,
4266 .callback_ops = &nfs4_get_lease_time_ops,
4267 .callback_data = &data
4269 int status;
4271 res.lr_seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
4272 dprintk("--> %s\n", __func__);
4273 task = rpc_run_task(&task_setup);
4275 if (IS_ERR(task))
4276 status = PTR_ERR(task);
4277 else {
4278 status = task->tk_status;
4279 rpc_put_task(task);
4281 dprintk("<-- %s return %d\n", __func__, status);
4283 return status;
4286 /* Destroy the slot table */
4287 static void nfs4_destroy_slot_table(struct nfs4_session *session)
4289 if (session->fc_slot_table.slots == NULL)
4290 return;
4291 kfree(session->fc_slot_table.slots);
4292 session->fc_slot_table.slots = NULL;
4293 return;
4296 struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4298 struct nfs4_session *session;
4299 struct nfs4_slot_table *tbl;
4301 session = kzalloc(sizeof(struct nfs4_session), GFP_KERNEL);
4302 if (!session)
4303 return NULL;
4304 tbl = &session->fc_slot_table;
4305 spin_lock_init(&tbl->slot_tbl_lock);
4306 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "Slot table");
4307 session->clp = clp;
4308 return session;
4311 void nfs4_destroy_session(struct nfs4_session *session)
4313 nfs4_destroy_slot_table(session);
4314 kfree(session);
4317 #endif /* CONFIG_NFS_V4_1 */
4319 struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops = {
4320 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
4321 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
4322 .recover_open = nfs4_open_reclaim,
4323 .recover_lock = nfs4_lock_reclaim,
4326 struct nfs4_state_recovery_ops nfs4_nograce_recovery_ops = {
4327 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
4328 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
4329 .recover_open = nfs4_open_expired,
4330 .recover_lock = nfs4_lock_expired,
4333 static const struct inode_operations nfs4_file_inode_operations = {
4334 .permission = nfs_permission,
4335 .getattr = nfs_getattr,
4336 .setattr = nfs_setattr,
4337 .getxattr = nfs4_getxattr,
4338 .setxattr = nfs4_setxattr,
4339 .listxattr = nfs4_listxattr,
4342 const struct nfs_rpc_ops nfs_v4_clientops = {
4343 .version = 4, /* protocol version */
4344 .dentry_ops = &nfs4_dentry_operations,
4345 .dir_inode_ops = &nfs4_dir_inode_operations,
4346 .file_inode_ops = &nfs4_file_inode_operations,
4347 .getroot = nfs4_proc_get_root,
4348 .getattr = nfs4_proc_getattr,
4349 .setattr = nfs4_proc_setattr,
4350 .lookupfh = nfs4_proc_lookupfh,
4351 .lookup = nfs4_proc_lookup,
4352 .access = nfs4_proc_access,
4353 .readlink = nfs4_proc_readlink,
4354 .create = nfs4_proc_create,
4355 .remove = nfs4_proc_remove,
4356 .unlink_setup = nfs4_proc_unlink_setup,
4357 .unlink_done = nfs4_proc_unlink_done,
4358 .rename = nfs4_proc_rename,
4359 .link = nfs4_proc_link,
4360 .symlink = nfs4_proc_symlink,
4361 .mkdir = nfs4_proc_mkdir,
4362 .rmdir = nfs4_proc_remove,
4363 .readdir = nfs4_proc_readdir,
4364 .mknod = nfs4_proc_mknod,
4365 .statfs = nfs4_proc_statfs,
4366 .fsinfo = nfs4_proc_fsinfo,
4367 .pathconf = nfs4_proc_pathconf,
4368 .set_capabilities = nfs4_server_capabilities,
4369 .decode_dirent = nfs4_decode_dirent,
4370 .read_setup = nfs4_proc_read_setup,
4371 .read_done = nfs4_read_done,
4372 .write_setup = nfs4_proc_write_setup,
4373 .write_done = nfs4_write_done,
4374 .commit_setup = nfs4_proc_commit_setup,
4375 .commit_done = nfs4_commit_done,
4376 .lock = nfs4_proc_lock,
4377 .clear_acl_cache = nfs4_zap_acl_attr,
4378 .close_context = nfs4_close_context,
4382 * Local variables:
4383 * c-basic-offset: 8
4384 * End: