4 * Client-side procedure declarations for NFSv4.
6 * Copyright (c) 2002 The Regents of the University of Michigan.
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
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.
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>
54 #include "delegation.h"
58 #define NFSDBG_FACILITY NFSDBG_PROC
60 #define NFS4_POLL_RETRY_MIN (HZ/10)
61 #define NFS4_POLL_RETRY_MAX (15*HZ)
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)
74 dprintk("%s could not handle NFSv4 error %d\n",
82 * This is our standard bitmap for GETATTR requests.
84 const u32 nfs4_fattr_bitmap[2] = {
89 | FATTR4_WORD0_FILEID,
91 | FATTR4_WORD1_NUMLINKS
93 | FATTR4_WORD1_OWNER_GROUP
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] = {
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] = {
125 | FATTR4_WORD0_CHANGE
128 | FATTR4_WORD0_FILEID
129 | FATTR4_WORD0_FS_LOCATIONS,
131 | FATTR4_WORD1_NUMLINKS
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)
147 BUG_ON(readdir->count < 80);
149 readdir->cookie = cookie;
150 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
155 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
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
166 start = p = kmap_atomic(*readdir->pages, KM_USER0);
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 */
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 */
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)
203 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
204 nfs_wait_bit_killable, TASK_KILLABLE);
208 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
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))
225 /* This is the error handling routine for processes that are allowed
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;
234 exception->retry = 0;
238 case -NFS4ERR_ADMIN_REVOKED:
239 case -NFS4ERR_BAD_STATEID:
240 case -NFS4ERR_OPENMODE:
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);
250 exception->retry = 1;
252 case -NFS4ERR_FILE_OPEN:
255 ret = nfs4_delay(server->client, &exception->timeout);
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
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.
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;
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__);
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);
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,
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
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)
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 */
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);
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.
385 nfs4_find_slot(struct nfs4_slot_table *tbl, struct rpc_task *task)
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,
394 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
395 if (slotid >= tbl->max_slots)
397 __set_bit(slotid, tbl->used_slots);
398 if (slotid > tbl->highest_used_slotid)
399 tbl->highest_used_slotid = slotid;
402 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
403 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
407 static int nfs41_setup_sequence(struct nfs4_session *session,
408 struct nfs4_sequence_args *args,
409 struct nfs4_sequence_res *res,
411 struct rpc_task *task)
413 struct nfs4_slot *slot;
414 struct nfs4_slot_table *tbl;
417 dprintk("--> %s\n", __func__);
418 /* slot already allocated? */
419 if (res->sr_slotid != NFS4_MAX_SLOT_TABLE)
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__);
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.
454 int nfs4_setup_sequence(struct nfs_client *clp,
455 struct nfs4_sequence_args *args,
456 struct nfs4_sequence_res *res,
458 struct rpc_task *task)
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))
467 ret = nfs41_setup_sequence(clp->cl_session, args, res, cache_reply,
469 if (ret != -EAGAIN) {
470 /* terminate rpc task */
471 task->tk_status = ret;
472 task->tk_action = NULL;
475 dprintk("<-- %s status=%d\n", __func__, 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;
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))
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,
519 struct rpc_task *task;
520 struct nfs41_call_sync_data data = {
524 .cache_reply = cache_reply,
526 struct rpc_task_setup task_setup = {
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);
538 ret = task->tk_status;
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,
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,
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,
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 {
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;
610 struct nfs4_state_owner *owner;
611 struct nfs4_state *state;
613 unsigned long timestamp;
614 unsigned int rpc_done : 1;
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);
643 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
644 if (p->o_arg.seqid == NULL)
646 p->path.mnt = mntget(path->mnt);
647 p->path.dentry = dget(path->dentry);
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;
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);
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);
696 static void nfs4_opendata_put(struct nfs4_opendata *p)
699 kref_put(&p->kref, nfs4_opendata_free);
702 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
706 ret = rpc_wait_for_completion_task(task);
710 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
714 if (open_mode & O_EXCL)
716 switch (mode & (FMODE_READ|FMODE_WRITE)) {
718 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0;
721 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0;
723 case FMODE_READ|FMODE_WRITE:
724 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0;
730 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
732 if ((delegation->type & fmode) != fmode)
734 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
736 nfs_mark_delegation_referenced(delegation);
740 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
749 case FMODE_READ|FMODE_WRITE:
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));
762 set_bit(NFS_O_RDONLY_STATE, &state->flags);
765 set_bit(NFS_O_WRONLY_STATE, &state->flags);
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;
804 fmode &= (FMODE_READ|FMODE_WRITE);
807 deleg_cur = rcu_dereference(nfsi->delegation);
808 if (deleg_cur == NULL)
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);
824 no_delegation_unlock:
825 spin_unlock(&deleg_cur->lock);
829 if (!ret && open_stateid != NULL) {
830 __update_open_stateid(state, open_stateid, NULL, fmode);
838 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
840 struct nfs_delegation *delegation;
843 delegation = rcu_dereference(NFS_I(inode)->delegation);
844 if (delegation == NULL || (delegation->type & fmode) == fmode) {
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;
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);
873 delegation = rcu_dereference(nfsi->delegation);
874 if (delegation == NULL ||
875 !can_open_delegated(delegation, fmode)) {
879 /* Save the delegation */
880 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
882 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
887 /* Try to update the stateid using the delegation */
888 if (update_open_stateid(state, NULL, &stateid, fmode))
889 goto out_return_state;
894 atomic_inc(&state->count);
898 static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
901 struct nfs4_state *state = NULL;
902 struct nfs_delegation *delegation;
905 if (!data->rpc_done) {
906 state = nfs4_try_open_cached(data);
911 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
913 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
914 ret = PTR_ERR(inode);
918 state = nfs4_get_open_state(inode, data->owner);
921 if (data->o_res.delegation_type != 0) {
922 int delegation_flags = 0;
925 delegation = rcu_dereference(NFS_I(inode)->delegation);
927 delegation_flags = delegation->flags;
929 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
930 nfs_inode_set_delegation(state->inode,
931 data->owner->so_cred,
934 nfs_inode_reclaim_delegation(state->inode,
935 data->owner->so_cred,
939 update_open_stateid(state, &data->o_res.stateid, NULL,
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)
959 get_nfs_open_context(ctx);
960 spin_unlock(&state->inode->i_lock);
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);
979 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
981 struct nfs4_state *newstate;
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);
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);
1000 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1002 struct nfs4_state *newstate;
1005 /* memory barrier prior to reading state->n_* */
1006 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1008 if (state->n_rdwr != 0) {
1009 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1012 if (newstate != state)
1015 if (state->n_wronly != 0) {
1016 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1019 if (newstate != state)
1022 if (state->n_rdonly != 0) {
1023 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1026 if (newstate != state)
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);
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;
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);
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;
1064 opendata->o_arg.u.delegation_type = delegation_type;
1065 status = nfs4_open_recover(opendata, state);
1066 nfs4_opendata_put(opendata);
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 = { };
1076 err = _nfs4_do_open_reclaim(ctx, state);
1077 if (err != -NFS4ERR_DELAY)
1079 nfs4_handle_exception(server, err, &exception);
1080 } while (exception.retry);
1084 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1086 struct nfs_open_context *ctx;
1089 ctx = nfs4_state_find_open_context(state);
1091 return PTR_ERR(ctx);
1092 ret = nfs4_do_open_reclaim(ctx, state);
1093 put_nfs_open_context(ctx);
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;
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);
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);
1119 err = _nfs4_open_delegation_recall(ctx, state, stateid);
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);
1130 err = nfs4_handle_exception(server, err, &exception);
1131 } while (exception.retry);
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))
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);
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)
1159 /* In case of error, no cleanup! */
1160 if (!data->rpc_done)
1162 state = nfs4_opendata_to_nfs4_state(data);
1164 nfs4_close_state(&data->path, state, data->o_arg.fmode);
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,
1197 kref_get(&data->kref);
1199 data->rpc_status = 0;
1200 data->timestamp = jiffies;
1201 task = rpc_run_task(&task_setup_data);
1203 return PTR_ERR(task);
1204 status = nfs4_wait_for_completion_rpc_task(task);
1206 data->cancelled = 1;
1209 status = data->rpc_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)
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))
1231 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1232 if (delegation != NULL &&
1233 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
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))
1251 rpc_call_start(task);
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,
1267 if (RPC_ASSASSINATED(task))
1269 if (task->tk_status == 0) {
1270 switch (data->o_res.f_attr->mode & S_IFMT) {
1274 data->rpc_status = -ELOOP;
1277 data->rpc_status = -EISDIR;
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);
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)
1297 /* In case of error, no cleanup! */
1298 if (data->rpc_status != 0 || !data->rpc_done)
1300 /* In case we need an open_confirm, no cleanup! */
1301 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1303 state = nfs4_opendata_to_nfs4_state(data);
1305 nfs4_close_state(&data->path, state, data->o_arg.fmode);
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],
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,
1342 kref_get(&data->kref);
1344 data->rpc_status = 0;
1345 data->cancelled = 0;
1346 task = rpc_run_task(&task_setup_data);
1348 return PTR_ERR(task);
1349 status = nfs4_wait_for_completion_rpc_task(task);
1351 data->cancelled = 1;
1354 status = data->rpc_status;
1356 if (status != 0 || !data->rpc_done)
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);
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);
1372 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1373 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1377 static int nfs4_recover_expired_lease(struct nfs_server *server)
1379 struct nfs_client *clp = server->nfs_client;
1383 ret = nfs4_wait_clnt_recover(clp);
1386 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1387 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1389 nfs4_schedule_state_recovery(clp);
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;
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);
1409 d_drop(ctx->path.dentry);
1410 nfs4_opendata_put(opendata);
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 = { };
1421 err = _nfs4_open_expired(ctx, state);
1422 if (err != -NFS4ERR_DELAY)
1424 nfs4_handle_exception(server, err, &exception);
1425 } while (exception.retry);
1429 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1431 struct nfs_open_context *ctx;
1434 ctx = nfs4_state_find_open_context(state);
1436 return PTR_ERR(ctx);
1437 ret = nfs4_do_open_expired(ctx, state);
1438 put_nfs_open_context(ctx);
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;
1469 /* Protect against reboot recovery conflicts */
1471 if (!(sp = nfs4_get_state_owner(server, cred))) {
1472 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1475 status = nfs4_recover_expired_lease(server);
1477 goto err_put_state_owner;
1478 if (path->dentry->d_inode != NULL)
1479 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
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);
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);
1498 goto err_opendata_put;
1499 nfs4_opendata_put(opendata);
1500 nfs4_put_state_owner(sp);
1504 nfs4_opendata_put(opendata);
1505 err_put_state_owner:
1506 nfs4_put_state_owner(sp);
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;
1520 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
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;
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;
1551 if (status == -EAGAIN) {
1552 /* We must have found a delegation */
1553 exception.retry = 1;
1556 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1557 status, &exception));
1558 } while (exception.retry);
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),
1571 .bitmask = server->attr_bitmask,
1573 struct nfs_setattrres res = {
1577 struct rpc_message msg = {
1578 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1583 unsigned long timestamp = jiffies;
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);
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);
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 = { };
1609 err = nfs4_handle_exception(server,
1610 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
1612 } while (exception.retry);
1616 struct nfs4_closedata {
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);
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))
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) {
1652 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
1653 renew_lease(server, calldata->timestamp);
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)
1662 if (nfs4_async_handle_error(task, server, state) == -EAGAIN) {
1663 rpc_restart_call(task);
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)
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;
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,
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)
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);
1776 return PTR_ERR(task);
1779 status = rpc_wait_for_completion_task(task);
1785 nfs4_put_open_state(state);
1786 nfs4_put_state_owner(sp);
1790 static int nfs4_intent_set_file(struct nameidata *nd, struct path *path, struct nfs4_state *state, fmode_t fmode)
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);
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);
1810 ret = PTR_ERR(filp);
1812 nfs4_close_sync(path, state, fmode & (FMODE_READ|FMODE_WRITE));
1817 nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1819 struct path path = {
1820 .mnt = nd->path.mnt,
1823 struct dentry *parent;
1825 struct rpc_cred *cred;
1826 struct nfs4_state *state;
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();
1837 BUG_ON(nd->intent.open.flags & O_CREAT);
1840 cred = rpc_lookup_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);
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));
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);
1866 nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
1868 struct path path = {
1869 .mnt = nd->path.mnt,
1872 struct rpc_cred *cred;
1873 struct nfs4_state *state;
1874 fmode_t fmode = openflags & (FMODE_READ | FMODE_WRITE);
1876 cred = rpc_lookup_cred();
1878 return PTR_ERR(cred);
1879 state = nfs4_do_open(dir, &path, fmode, openflags, NULL, cred);
1881 if (IS_ERR(state)) {
1882 switch (PTR_ERR(state)) {
1888 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
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);
1899 nfs4_close_sync(&path, state, fmode);
1905 void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
1907 if (ctx->state == NULL)
1910 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
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 = {
1920 struct nfs4_server_caps_res res = {};
1921 struct rpc_message msg = {
1922 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
1928 status = nfs4_call_sync(server, &msg, &args, &res, 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;
1946 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1948 struct nfs4_exception exception = { };
1951 err = nfs4_handle_exception(server,
1952 _nfs4_server_capabilities(server, fhandle),
1954 } while (exception.retry);
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 = {
1966 .fattr = info->fattr,
1969 struct rpc_message msg = {
1970 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
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 = { };
1984 err = nfs4_handle_exception(server,
1985 _nfs4_lookup_root(server, fhandle, info),
1987 } while (exception.retry);
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)
1999 status = nfs4_lookup_root(server, fhandle, info);
2001 status = nfs4_server_capabilities(server, fhandle);
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);
2021 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2022 if (locations == NULL)
2025 status = nfs4_proc_fs_locations(dir, name, locations, page);
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);
2035 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2036 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2038 fattr->mode = S_IFDIR;
2039 memset(fhandle, 0, sizeof(struct nfs_fh));
2048 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2050 struct nfs4_getattr_arg args = {
2052 .bitmask = server->attr_bitmask,
2054 struct nfs4_getattr_res res = {
2058 struct rpc_message msg = {
2059 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
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 = { };
2073 err = nfs4_handle_exception(server,
2074 _nfs4_proc_getattr(server, fhandle, fattr),
2076 } while (exception.retry);
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.
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).
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;
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);
2119 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2121 nfs_setattr_update_inode(inode, sattr);
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)
2130 struct nfs4_lookup_arg args = {
2131 .bitmask = server->attr_bitmask,
2135 struct nfs4_lookup_res res = {
2140 struct rpc_message msg = {
2141 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
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);
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 = { };
2161 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2163 if (err == -NFS4ERR_MOVED) {
2167 err = nfs4_handle_exception(server, err, &exception);
2168 } while (exception.retry);
2172 static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
2173 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
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);
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 = { };
2190 err = nfs4_handle_exception(NFS_SERVER(dir),
2191 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2193 } while (exception.retry);
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 = {
2209 struct rpc_message msg = {
2210 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2213 .rpc_cred = entry->cred,
2215 int mode = entry->mask;
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;
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);
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);
2249 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2251 struct nfs4_exception exception = { };
2254 err = nfs4_handle_exception(NFS_SERVER(inode),
2255 _nfs4_proc_access(inode, entry),
2257 } while (exception.retry);
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,
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
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),
2294 struct nfs4_readlink_res res;
2295 struct rpc_message msg = {
2296 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
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 = { };
2310 err = nfs4_handle_exception(NFS_SERVER(inode),
2311 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2313 } while (exception.retry);
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.
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,
2340 struct nfs4_state *state;
2341 struct rpc_cred *cred;
2342 fmode_t fmode = flags & (FMODE_READ | FMODE_WRITE);
2345 cred = rpc_lookup_cred();
2347 status = PTR_ERR(cred);
2350 state = nfs4_do_open(dir, &path, fmode, flags, sattr, cred);
2352 if (IS_ERR(state)) {
2353 status = PTR_ERR(state);
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);
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);
2368 nfs4_close_sync(&path, state, fmode);
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 = {
2380 .name.len = name->len,
2381 .name.name = name->name,
2382 .bitmask = server->attr_bitmask,
2384 struct nfs_removeres res = {
2387 struct rpc_message msg = {
2388 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2394 nfs_fattr_init(&res.dir_attr);
2395 status = nfs4_call_sync(server, &msg, &args, &res, 1);
2397 update_changeattr(dir, &res.cinfo);
2398 nfs_post_op_update_inode(dir, &res.dir_attr);
2403 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2405 struct nfs4_exception exception = { };
2408 err = nfs4_handle_exception(NFS_SERVER(dir),
2409 _nfs4_proc_remove(dir, name),
2411 } while (exception.retry);
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)
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);
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 = {
2453 .old_fattr = &old_fattr,
2454 .new_fattr = &new_fattr,
2456 struct rpc_message msg = {
2457 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2463 nfs_fattr_init(res.old_fattr);
2464 nfs_fattr_init(res.new_fattr);
2465 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
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);
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 = { };
2482 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2483 _nfs4_proc_rename(old_dir, old_name,
2486 } while (exception.retry);
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),
2497 .bitmask = server->attr_bitmask,
2499 struct nfs_fattr fattr, dir_attr;
2500 struct nfs4_link_res res = {
2503 .dir_attr = &dir_attr,
2505 struct rpc_message msg = {
2506 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2512 nfs_fattr_init(res.fattr);
2513 nfs_fattr_init(res.dir_attr);
2514 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
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);
2524 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2526 struct nfs4_exception exception = { };
2529 err = nfs4_handle_exception(NFS_SERVER(inode),
2530 _nfs4_proc_link(inode, dir, name),
2532 } while (exception.retry);
2536 struct nfs4_createdata {
2537 struct rpc_message msg;
2538 struct nfs4_create_arg arg;
2539 struct nfs4_create_res res;
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);
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);
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);
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);
2585 static void nfs4_free_createdata(struct nfs4_createdata *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)
2600 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
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);
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 = { };
2621 err = nfs4_handle_exception(NFS_SERVER(dir),
2622 _nfs4_proc_symlink(dir, dentry, page,
2625 } while (exception.retry);
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);
2639 status = nfs4_do_create(dir, dentry, data);
2641 nfs4_free_createdata(data);
2646 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2647 struct iattr *sattr)
2649 struct nfs4_exception exception = { };
2652 err = nfs4_handle_exception(NFS_SERVER(dir),
2653 _nfs4_proc_mkdir(dir, dentry, sattr),
2655 } while (exception.retry);
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 = {
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],
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);
2687 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
2689 nfs_invalidate_atime(dir);
2691 dprintk("%s: returns %d\n", __func__, 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 = { };
2701 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2702 _nfs4_proc_readdir(dentry, cred, cookie,
2705 } while (exception.retry);
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);
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);
2743 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2744 struct iattr *sattr, dev_t rdev)
2746 struct nfs4_exception exception = { };
2749 err = nfs4_handle_exception(NFS_SERVER(dir),
2750 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2752 } while (exception.retry);
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 = {
2761 .bitmask = server->attr_bitmask,
2763 struct nfs4_statfs_res res = {
2766 struct rpc_message msg = {
2767 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
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 = { };
2781 err = nfs4_handle_exception(server,
2782 _nfs4_proc_statfs(server, fhandle, fsstat),
2784 } while (exception.retry);
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 = {
2793 .bitmask = server->attr_bitmask,
2795 struct nfs4_fsinfo_res res = {
2798 struct rpc_message msg = {
2799 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
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 = { };
2813 err = nfs4_handle_exception(server,
2814 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2816 } while (exception.retry);
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 = {
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],
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));
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 = { };
2859 err = nfs4_handle_exception(server,
2860 _nfs4_proc_pathconf(server, fhandle, pathconf),
2862 } while (exception.retry);
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);
2880 nfs_invalidate_atime(data->inode);
2881 if (task->tk_status > 0)
2882 renew_lease(server, data->timestamp);
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,
2900 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
2901 rpc_restart_call(task);
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);
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,
2928 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
2929 rpc_restart_call(task);
2932 nfs4_sequence_free_slot(NFS_SERVER(inode)->nfs_client,
2933 &data->res.seq_res);
2934 nfs_refresh_inode(inode, data->res.fattr);
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);
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],
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],
2991 unsigned long now = jiffies;
2994 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
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);
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
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);
3024 while (p < buf + buflen) {
3025 *(pages++) = virt_to_page(p);
3026 p += PAGE_CACHE_SIZE;
3030 struct nfs4_cached_acl {
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;
3057 spin_lock(&inode->i_lock);
3058 acl = nfsi->nfs4_acl;
3061 if (buf == NULL) /* user is just asking for length */
3063 if (acl->cached == 0)
3065 ret = -ERANGE; /* see getxattr(2) man page */
3066 if (acl->len > buflen)
3068 memcpy(buf, acl->data, acl->len);
3072 spin_unlock(&inode->i_lock);
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);
3085 memcpy(acl->data, buf, acl_len);
3087 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
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),
3105 struct nfs_getaclres res = {
3109 struct rpc_message msg = {
3110 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3114 struct page *localpage = NULL;
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)
3124 args.acl_pages[0] = localpage;
3125 args.acl_pgbase = 0;
3126 args.acl_len = PAGE_SIZE;
3129 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3131 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
3134 if (res.acl_len > args.acl_len)
3135 nfs4_write_cached_acl(inode, NULL, res.acl_len);
3137 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
3140 if (res.acl_len > buflen)
3143 memcpy(buf, resp_buf, res.acl_len);
3148 __free_page(localpage);
3152 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3154 struct nfs4_exception exception = { };
3157 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3160 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3161 } while (exception.retry);
3165 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3167 struct nfs_server *server = NFS_SERVER(inode);
3170 if (!nfs4_server_supports_acls(server))
3172 ret = nfs_revalidate_inode(server, inode);
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);
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),
3192 struct nfs_setaclres res;
3193 struct rpc_message msg = {
3194 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3200 if (!nfs4_server_supports_acls(server))
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);
3210 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3212 struct nfs4_exception exception = { };
3215 err = nfs4_handle_exception(NFS_SERVER(inode),
3216 __nfs4_proc_set_acl(inode, buf, buflen),
3218 } while (exception.retry);
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)
3229 switch(task->tk_status) {
3230 case -NFS4ERR_ADMIN_REVOKED:
3231 case -NFS4ERR_BAD_STATEID:
3232 case -NFS4ERR_OPENMODE:
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;
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;
3251 case -NFS4ERR_OLD_STATEID:
3252 task->tk_status = 0;
3255 task->tk_status = nfs4_map_errors(task->tk_status);
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,
3266 struct rpc_message msg = {
3267 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3268 .rpc_argp = &setclientid,
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);
3281 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
3282 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
3284 rpc_peeraddr2str(clp->cl_rpcclient,
3286 rpc_peeraddr2str(clp->cl_rpcclient,
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)
3304 ssleep(clp->cl_lease_time + 1);
3306 if (++clp->cl_id_uniquifier == 0)
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],
3318 .rpc_resp = &fsinfo,
3325 status = rpc_call_sync(clp->cl_rpcclient, &msg, 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);
3335 int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
3340 err = _nfs4_proc_setclientid_confirm(clp, cred);
3344 case -NFS4ERR_RESOURCE:
3345 /* The IBM lawyers misread another document! */
3346 case -NFS4ERR_DELAY:
3347 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3353 struct nfs4_delegreturndata {
3354 struct nfs4_delegreturnargs args;
3355 struct nfs4_delegreturnres res;
3357 nfs4_stateid stateid;
3358 unsigned long timestamp;
3359 struct nfs_fattr fattr;
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,
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)
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))
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],
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,
3420 data = kzalloc(sizeof(*data), GFP_KERNEL);
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);
3440 return PTR_ERR(task);
3443 status = nfs4_wait_for_completion_rpc_task(task);
3446 status = data->rpc_status;
3449 nfs_refresh_inode(inode, &data->fattr);
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 = { };
3461 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
3463 case -NFS4ERR_STALE_STATEID:
3464 case -NFS4ERR_EXPIRED:
3468 err = nfs4_handle_exception(server, err, &exception);
3469 } while (exception.retry);
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);
3484 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3485 return NFS4_LOCK_MAXTIMEOUT;
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),
3498 struct nfs_lockt_res res = {
3501 struct rpc_message msg = {
3502 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3505 .rpc_cred = state->owner->so_cred,
3507 struct nfs4_lock_state *lsp;
3510 arg.lock_owner.clientid = clp->cl_clientid;
3511 status = nfs4_set_lock_state(state, request);
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);
3519 request->fl_type = F_UNLCK;
3521 case -NFS4ERR_DENIED:
3524 request->fl_ops->fl_release_private(request);
3529 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3531 struct nfs4_exception exception = { };
3535 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3536 _nfs4_proc_getlk(state, cmd, request),
3538 } while (exception.retry);
3542 static int do_vfs_lock(struct file *file, struct file_lock *fl)
3545 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3547 res = posix_lock_file_wait(file, fl);
3550 res = flock_lock_file_wait(file, fl);
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);
3579 p->arg.fh = NFS_FH(inode);
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;
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);
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);
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,
3609 if (RPC_ASSASSINATED(task))
3611 switch (task->tk_status) {
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);
3618 case -NFS4ERR_BAD_STATEID:
3619 case -NFS4ERR_OLD_STATEID:
3620 case -NFS4ERR_STALE_STATEID:
3621 case -NFS4ERR_EXPIRED:
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)
3637 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
3638 /* Note: exit _without_ running nfs4_locku_done */
3639 task->tk_action = NULL;
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))
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);
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;
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);
3708 up_read(&nfsi->rwsem);
3711 /* Is this a delegated lock? */
3712 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
3714 lsp = request->fl_u.nfs4_fl.owner;
3715 seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3719 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
3720 status = PTR_ERR(task);
3723 status = nfs4_wait_for_completion_rpc_task(task);
3726 request->fl_flags = fl_flags;
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;
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);
3753 p->arg.fh = NFS_FH(inode);
3755 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid);
3756 if (p->arg.open_seqid == NULL)
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;
3768 atomic_inc(&lsp->ls_count);
3769 p->ctx = get_nfs_open_context(ctx);
3770 memcpy(&p->fl, fl, sizeof(p->fl));
3773 nfs_free_seqid(p->arg.open_seqid);
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)
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)
3791 data->arg.open_stateid = &state->stateid;
3792 data->arg.new_lock_owner = 1;
3793 data->res.open_seqid = data->arg.open_seqid;
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))
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,
3813 data->rpc_status = task->tk_status;
3814 if (RPC_ASSASSINATED(task))
3816 if (data->arg.new_lock_owner != 0) {
3817 if (data->rpc_status == 0)
3818 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
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);
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);
3844 dprintk("%s: cancelling lock!\n", __func__);
3846 nfs_free_seqid(data->arg.lock_seqid);
3847 nfs4_put_lock_state(data->lsp);
3848 put_nfs_open_context(data->ctx);
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,
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);
3882 data->arg.block = 1;
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);
3890 return PTR_ERR(task);
3891 ret = nfs4_wait_for_completion_rpc_task(task);
3893 ret = data->rpc_status;
3894 if (ret == -NFS4ERR_DENIED)
3897 data->cancelled = 1;
3899 dprintk("%s: done, ret = %d!\n", __func__, 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 = { };
3910 /* Cache the lock if possible... */
3911 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3913 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
3914 if (err != -NFS4ERR_DELAY)
3916 nfs4_handle_exception(server, err, &exception);
3917 } while (exception.retry);
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 = { };
3927 err = nfs4_set_lock_state(state, request);
3931 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
3933 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
3934 if (err != -NFS4ERR_DELAY)
3936 nfs4_handle_exception(server, err, &exception);
3937 } while (exception.retry);
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;
3947 /* Is this a delegated open? */
3948 status = nfs4_set_lock_state(state, request);
3951 request->fl_flags |= FL_ACCESS;
3952 status = do_vfs_lock(request->fl_file, request);
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);
3963 status = _nfs4_do_setlk(state, cmd, request, 0);
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__);
3971 up_read(&nfsi->rwsem);
3973 request->fl_flags = fl_flags;
3977 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3979 struct nfs4_exception exception = { };
3983 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3984 _nfs4_proc_setlk(state, cmd, request),
3986 } while (exception.retry);
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;
3998 /* verify open state */
3999 ctx = nfs_file_open_context(filp);
4002 if (request->fl_start < 0 || request->fl_end < 0)
4006 return nfs4_proc_getlk(state, F_GETLK, request);
4008 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4011 if (request->fl_type == F_UNLCK)
4012 return nfs4_proc_unlck(state, cmd, request);
4015 status = nfs4_proc_setlk(state, cmd, request);
4016 if ((status != -EAGAIN) || IS_SETLK(cmd))
4018 timeout = nfs4_set_lock_task_retry(timeout);
4019 status = -ERESTARTSYS;
4022 } while(status < 0);
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 = { };
4032 err = nfs4_set_lock_state(state, fl);
4036 err = _nfs4_do_setlk(state, F_SETLK, fl, 0);
4037 if (err != -NFS4ERR_DELAY)
4039 err = nfs4_handle_exception(server, err, &exception);
4040 } while (exception.retry);
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)
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,
4065 struct inode *inode = dentry->d_inode;
4067 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
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)))
4079 if (buf && buflen < len)
4082 memcpy(buf, XATTR_NAME_NFSV4_ACL, 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)))
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;
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);
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),
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],
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);
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 = {
4147 .flags = clp->cl_exchange_flags,
4149 struct nfs41_exchange_id_res res = {
4153 struct rpc_message msg = {
4154 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
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;
4169 args.id_len = scnprintf(args.id, sizeof(args.id),
4172 rpc_peeraddr2str(clp->cl_rpcclient,
4174 clp->cl_id_uniquifier);
4176 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4178 if (status != NFS4ERR_CLID_INUSE)
4184 if (++clp->cl_id_uniquifier == 0)
4188 dprintk("<-- %s status= %d\n", __func__, 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,
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);
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 = {
4258 struct rpc_message msg = {
4259 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
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
4271 res.lr_seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
4272 dprintk("--> %s\n", __func__);
4273 task = rpc_run_task(&task_setup);
4276 status = PTR_ERR(task);
4278 status = task->tk_status;
4281 dprintk("<-- %s return %d\n", __func__, 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)
4291 kfree(session->fc_slot_table.slots);
4292 session->fc_slot_table.slots = NULL;
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);
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");
4311 void nfs4_destroy_session(struct nfs4_session *session)
4313 nfs4_destroy_slot_table(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,