2008-09-17 18:49:25 +04:00
|
|
|
/* $NetBSD: vfs_trans.c,v 1.23 2008/09/17 14:49:25 hannken Exp $ */
|
2007-01-19 17:49:08 +03:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 2007 The NetBSD Foundation, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Juergen Hannken-Illjes.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
2008-09-17 18:49:25 +04:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: vfs_trans.c,v 1.23 2008/09/17 14:49:25 hannken Exp $");
|
2007-01-19 17:49:08 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* File system transaction operations.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "opt_ddb.h"
|
|
|
|
|
|
|
|
#if defined(DDB)
|
|
|
|
#define _LWP_API_PRIVATE /* Need _lwp_getspecific_by_lwp() */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
2007-10-07 17:38:53 +04:00
|
|
|
#include <sys/kmem.h>
|
2007-01-19 17:49:08 +03:00
|
|
|
#include <sys/mount.h>
|
2007-05-16 20:11:56 +04:00
|
|
|
#include <sys/rwlock.h>
|
2007-01-19 17:49:08 +03:00
|
|
|
#include <sys/vnode.h>
|
|
|
|
#define _FSTRANS_API_PRIVATE
|
|
|
|
#include <sys/fstrans.h>
|
2007-07-10 00:51:58 +04:00
|
|
|
#include <sys/proc.h>
|
2007-01-19 17:49:08 +03:00
|
|
|
|
2007-10-07 17:38:53 +04:00
|
|
|
#include <miscfs/specfs/specdev.h>
|
2007-01-19 17:49:08 +03:00
|
|
|
#include <miscfs/syncfs/syncfs.h>
|
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
struct fscow_handler {
|
|
|
|
SLIST_ENTRY(fscow_handler) ch_list;
|
|
|
|
int (*ch_func)(void *, struct buf *, bool);
|
|
|
|
void *ch_arg;
|
|
|
|
};
|
2007-01-19 17:49:08 +03:00
|
|
|
struct fstrans_lwp_info {
|
|
|
|
struct fstrans_lwp_info *fli_succ;
|
|
|
|
struct mount *fli_mount;
|
2008-05-16 13:01:56 +04:00
|
|
|
int fli_trans_cnt;
|
|
|
|
int fli_cow_cnt;
|
2007-01-19 17:49:08 +03:00
|
|
|
enum fstrans_lock_type fli_lock_type;
|
|
|
|
};
|
|
|
|
struct fstrans_mount_info {
|
|
|
|
enum fstrans_state fmi_state;
|
2007-05-16 20:11:56 +04:00
|
|
|
krwlock_t fmi_shared_lock;
|
|
|
|
krwlock_t fmi_lazy_lock;
|
2008-01-02 14:48:20 +03:00
|
|
|
krwlock_t fmi_cow_lock;
|
|
|
|
SLIST_HEAD(, fscow_handler) fmi_cow_handler;
|
2007-01-19 17:49:08 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
static specificdata_key_t lwp_data_key;
|
2007-02-15 18:40:50 +03:00
|
|
|
static kmutex_t vfs_suspend_lock; /* Serialize suspensions. */
|
2008-06-24 22:50:30 +04:00
|
|
|
static pool_cache_t fstrans_cache;
|
2007-01-19 17:49:08 +03:00
|
|
|
|
|
|
|
static void fstrans_lwp_dtor(void *);
|
2008-05-16 13:01:56 +04:00
|
|
|
static struct fstrans_lwp_info *fstrans_get_lwp_info(struct mount *);
|
2007-01-19 17:49:08 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
fstrans_init(void)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
|
|
|
error = lwp_specific_key_create(&lwp_data_key, fstrans_lwp_dtor);
|
|
|
|
KASSERT(error == 0);
|
2007-03-13 00:33:07 +03:00
|
|
|
|
2007-02-15 18:40:50 +03:00
|
|
|
mutex_init(&vfs_suspend_lock, MUTEX_DEFAULT, IPL_NONE);
|
2008-06-24 22:50:30 +04:00
|
|
|
fstrans_cache = pool_cache_init(sizeof(struct fstrans_lwp_info), 0, 0,
|
|
|
|
0, "fstrans", NULL, IPL_NONE, NULL, NULL, NULL);
|
2007-01-19 17:49:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Deallocate lwp state
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
fstrans_lwp_dtor(void *arg)
|
|
|
|
{
|
|
|
|
struct fstrans_lwp_info *fli, *fli_next;
|
|
|
|
|
|
|
|
for (fli = arg; fli; fli = fli_next) {
|
2008-05-16 13:01:56 +04:00
|
|
|
KASSERT(fli->fli_trans_cnt == 0);
|
|
|
|
KASSERT(fli->fli_cow_cnt == 0);
|
2007-01-19 17:49:08 +03:00
|
|
|
fli_next = fli->fli_succ;
|
2008-06-24 22:50:30 +04:00
|
|
|
pool_cache_put(fstrans_cache, fli);
|
2007-01-19 17:49:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2008-01-02 14:48:20 +03:00
|
|
|
* Allocate mount state
|
2007-01-19 17:49:08 +03:00
|
|
|
*/
|
2008-01-02 14:48:20 +03:00
|
|
|
int
|
|
|
|
fstrans_mount(struct mount *mp)
|
2007-01-19 17:49:08 +03:00
|
|
|
{
|
2008-01-02 14:48:20 +03:00
|
|
|
struct fstrans_mount_info *new;
|
2007-01-19 17:49:08 +03:00
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
if ((new = kmem_alloc(sizeof(*new), KM_SLEEP)) == NULL)
|
|
|
|
return ENOMEM;
|
|
|
|
new->fmi_state = FSTRANS_NORMAL;
|
|
|
|
rw_init(&new->fmi_lazy_lock);
|
|
|
|
rw_init(&new->fmi_shared_lock);
|
|
|
|
SLIST_INIT(&new->fmi_cow_handler);
|
|
|
|
rw_init(&new->fmi_cow_lock);
|
|
|
|
|
|
|
|
mp->mnt_transinfo = new;
|
|
|
|
mp->mnt_iflag |= IMNT_HAS_TRANS;
|
|
|
|
|
|
|
|
return 0;
|
2007-01-19 17:49:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2008-01-02 14:48:20 +03:00
|
|
|
* Deallocate mount state
|
2007-01-19 17:49:08 +03:00
|
|
|
*/
|
2008-01-02 14:48:20 +03:00
|
|
|
void
|
|
|
|
fstrans_unmount(struct mount *mp)
|
2007-01-19 17:49:08 +03:00
|
|
|
{
|
2008-01-02 14:48:20 +03:00
|
|
|
struct fstrans_mount_info *fmi;
|
2008-09-17 18:49:25 +04:00
|
|
|
struct fscow_handler *hp;
|
2007-01-19 17:49:08 +03:00
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
if ((fmi = mp->mnt_transinfo) == NULL)
|
|
|
|
return;
|
2007-01-19 17:49:08 +03:00
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
KASSERT(fmi->fmi_state == FSTRANS_NORMAL);
|
|
|
|
rw_destroy(&fmi->fmi_lazy_lock);
|
|
|
|
rw_destroy(&fmi->fmi_shared_lock);
|
2008-09-17 18:49:25 +04:00
|
|
|
rw_enter(&fmi->fmi_cow_lock, RW_WRITER);
|
|
|
|
while ((hp = SLIST_FIRST(&fmi->fmi_cow_handler)) != NULL) {
|
|
|
|
SLIST_REMOVE(&fmi->fmi_cow_handler, hp, fscow_handler, ch_list);
|
|
|
|
kmem_free(hp, sizeof(*hp));
|
|
|
|
}
|
|
|
|
rw_exit(&fmi->fmi_cow_lock);
|
2008-01-02 14:48:20 +03:00
|
|
|
rw_destroy(&fmi->fmi_cow_lock);
|
|
|
|
kmem_free(fmi, sizeof(*fmi));
|
|
|
|
mp->mnt_iflag &= ~IMNT_HAS_TRANS;
|
|
|
|
mp->mnt_transinfo = NULL;
|
2007-01-19 17:49:08 +03:00
|
|
|
}
|
|
|
|
|
2008-05-16 13:01:56 +04:00
|
|
|
/*
|
|
|
|
* Retrieve the per lwp info for this mount
|
|
|
|
*/
|
|
|
|
static struct fstrans_lwp_info *
|
|
|
|
fstrans_get_lwp_info(struct mount *mp)
|
|
|
|
{
|
|
|
|
struct fstrans_lwp_info *fli, *new_fli;
|
|
|
|
|
|
|
|
new_fli = NULL;
|
|
|
|
for (fli = lwp_getspecific(lwp_data_key); fli; fli = fli->fli_succ) {
|
|
|
|
if (fli->fli_mount == mp)
|
|
|
|
return fli;
|
|
|
|
else if (fli->fli_trans_cnt == 0 && fli->fli_cow_cnt == 0 &&
|
|
|
|
new_fli == NULL)
|
|
|
|
new_fli = fli;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (new_fli == NULL) {
|
2008-06-24 22:50:30 +04:00
|
|
|
new_fli = pool_cache_get(fstrans_cache, PR_WAITOK);
|
2008-05-16 13:01:56 +04:00
|
|
|
new_fli->fli_trans_cnt = 0;
|
|
|
|
new_fli->fli_cow_cnt = 0;
|
|
|
|
new_fli->fli_succ = lwp_getspecific(lwp_data_key);
|
|
|
|
lwp_setspecific(lwp_data_key, new_fli);
|
|
|
|
}
|
|
|
|
|
|
|
|
KASSERT(new_fli->fli_trans_cnt == 0);
|
|
|
|
KASSERT(new_fli->fli_cow_cnt == 0);
|
|
|
|
|
|
|
|
new_fli->fli_mount = mp;
|
|
|
|
|
|
|
|
return new_fli;
|
|
|
|
}
|
|
|
|
|
2007-01-19 17:49:08 +03:00
|
|
|
/*
|
|
|
|
* Start a transaction. If this thread already has a transaction on this
|
|
|
|
* file system increment the reference counter.
|
|
|
|
* A thread with an exclusive transaction lock may get a shared or lazy one.
|
|
|
|
* A thread with a shared or lazy transaction lock cannot upgrade to an
|
|
|
|
* exclusive one yet.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
_fstrans_start(struct mount *mp, enum fstrans_lock_type lock_type, int wait)
|
|
|
|
{
|
2007-05-16 20:11:56 +04:00
|
|
|
krwlock_t *lock_p;
|
|
|
|
krw_t lock_op;
|
2008-05-16 13:01:56 +04:00
|
|
|
struct fstrans_lwp_info *fli;
|
2007-01-19 17:49:08 +03:00
|
|
|
struct fstrans_mount_info *fmi;
|
|
|
|
|
2008-03-17 11:27:50 +03:00
|
|
|
ASSERT_SLEEPABLE();
|
2007-01-19 17:49:08 +03:00
|
|
|
|
|
|
|
if (mp == NULL || (mp->mnt_iflag & IMNT_HAS_TRANS) == 0)
|
|
|
|
return 0;
|
|
|
|
|
2008-05-16 13:01:56 +04:00
|
|
|
fli = fstrans_get_lwp_info(mp);
|
2007-01-19 17:49:08 +03:00
|
|
|
|
2008-05-16 13:01:56 +04:00
|
|
|
if (fli->fli_trans_cnt > 0) {
|
|
|
|
if (fli->fli_lock_type != FSTRANS_EXCL &&
|
|
|
|
lock_type == FSTRANS_EXCL)
|
|
|
|
panic("fstrans_start: cannot upgrade lock");
|
|
|
|
fli->fli_trans_cnt += 1;
|
|
|
|
return 0;
|
2007-01-19 17:49:08 +03:00
|
|
|
}
|
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
fmi = mp->mnt_transinfo;
|
2007-01-19 17:49:08 +03:00
|
|
|
|
2007-01-29 18:42:50 +03:00
|
|
|
if (lock_type == FSTRANS_LAZY)
|
2007-05-16 20:11:56 +04:00
|
|
|
lock_p = &fmi->fmi_lazy_lock;
|
2007-01-19 17:49:08 +03:00
|
|
|
else
|
2007-05-16 20:11:56 +04:00
|
|
|
lock_p = &fmi->fmi_shared_lock;
|
|
|
|
lock_op = (lock_type == FSTRANS_EXCL ? RW_WRITER : RW_READER);
|
|
|
|
|
|
|
|
if (wait)
|
|
|
|
rw_enter(lock_p, lock_op);
|
|
|
|
else if (rw_tryenter(lock_p, lock_op) == 0)
|
|
|
|
return EBUSY;
|
2007-01-19 17:49:08 +03:00
|
|
|
|
2008-05-16 13:01:56 +04:00
|
|
|
fli->fli_trans_cnt = 1;
|
|
|
|
fli->fli_lock_type = lock_type;
|
2007-01-19 17:49:08 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Finish a transaction.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
fstrans_done(struct mount *mp)
|
|
|
|
{
|
|
|
|
struct fstrans_lwp_info *fli;
|
|
|
|
struct fstrans_mount_info *fmi;
|
|
|
|
|
|
|
|
if (mp == NULL || (mp->mnt_iflag & IMNT_HAS_TRANS) == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (fli = lwp_getspecific(lwp_data_key); fli; fli = fli->fli_succ) {
|
|
|
|
if (fli->fli_mount == mp) {
|
2008-05-16 13:01:56 +04:00
|
|
|
fli->fli_trans_cnt -= 1;
|
|
|
|
if (fli->fli_trans_cnt > 0)
|
2007-01-19 17:49:08 +03:00
|
|
|
return;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
KASSERT(fli != NULL);
|
|
|
|
KASSERT(fli->fli_mount == mp);
|
2008-05-16 13:01:56 +04:00
|
|
|
KASSERT(fli->fli_trans_cnt == 0);
|
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
fmi = mp->mnt_transinfo;
|
2007-01-19 17:49:08 +03:00
|
|
|
KASSERT(fmi != NULL);
|
2007-01-29 18:42:50 +03:00
|
|
|
if (fli->fli_lock_type == FSTRANS_LAZY)
|
2007-05-16 20:11:56 +04:00
|
|
|
rw_exit(&fmi->fmi_lazy_lock);
|
2007-01-19 17:49:08 +03:00
|
|
|
else
|
2007-05-16 20:11:56 +04:00
|
|
|
rw_exit(&fmi->fmi_shared_lock);
|
2007-01-19 17:49:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if this thread has an exclusive lock.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
fstrans_is_owner(struct mount *mp)
|
|
|
|
{
|
|
|
|
struct fstrans_lwp_info *fli;
|
|
|
|
|
|
|
|
if (mp == NULL)
|
|
|
|
return 0;
|
|
|
|
if ((mp->mnt_iflag & IMNT_HAS_TRANS) == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (fli = lwp_getspecific(lwp_data_key); fli; fli = fli->fli_succ)
|
|
|
|
if (fli->fli_mount == mp)
|
|
|
|
break;
|
|
|
|
|
2008-05-16 13:01:56 +04:00
|
|
|
if (fli == NULL || fli->fli_trans_cnt == 0)
|
2007-01-19 17:49:08 +03:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
KASSERT(fli->fli_mount == mp);
|
2008-05-16 13:01:56 +04:00
|
|
|
KASSERT(fli->fli_trans_cnt > 0);
|
2007-01-29 18:42:50 +03:00
|
|
|
return (fli->fli_lock_type == FSTRANS_EXCL);
|
2007-01-19 17:49:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set new file system state.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
fstrans_setstate(struct mount *mp, enum fstrans_state new_state)
|
|
|
|
{
|
|
|
|
struct fstrans_mount_info *fmi;
|
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
fmi = mp->mnt_transinfo;
|
2007-01-19 17:49:08 +03:00
|
|
|
|
|
|
|
switch (new_state) {
|
2007-01-29 18:42:50 +03:00
|
|
|
case FSTRANS_SUSPENDING:
|
|
|
|
KASSERT(fmi->fmi_state == FSTRANS_NORMAL);
|
2007-05-17 11:26:21 +04:00
|
|
|
fstrans_start(mp, FSTRANS_EXCL);
|
2007-01-29 18:42:50 +03:00
|
|
|
fmi->fmi_state = FSTRANS_SUSPENDING;
|
2007-01-19 17:49:08 +03:00
|
|
|
break;
|
|
|
|
|
2007-01-29 18:42:50 +03:00
|
|
|
case FSTRANS_SUSPENDED:
|
|
|
|
KASSERT(fmi->fmi_state == FSTRANS_NORMAL ||
|
|
|
|
fmi->fmi_state == FSTRANS_SUSPENDING);
|
|
|
|
KASSERT(fmi->fmi_state == FSTRANS_NORMAL ||
|
2007-01-19 17:49:08 +03:00
|
|
|
fstrans_is_owner(mp));
|
2007-05-17 11:26:21 +04:00
|
|
|
if (fmi->fmi_state == FSTRANS_NORMAL)
|
|
|
|
fstrans_start(mp, FSTRANS_EXCL);
|
2007-05-16 20:11:56 +04:00
|
|
|
rw_enter(&fmi->fmi_lazy_lock, RW_WRITER);
|
2007-01-29 18:42:50 +03:00
|
|
|
fmi->fmi_state = FSTRANS_SUSPENDED;
|
2007-01-19 17:49:08 +03:00
|
|
|
break;
|
|
|
|
|
2007-01-29 18:42:50 +03:00
|
|
|
case FSTRANS_NORMAL:
|
|
|
|
KASSERT(fmi->fmi_state == FSTRANS_NORMAL ||
|
2007-01-19 17:49:08 +03:00
|
|
|
fstrans_is_owner(mp));
|
2007-01-29 18:42:50 +03:00
|
|
|
if (fmi->fmi_state == FSTRANS_SUSPENDED)
|
2007-05-16 20:11:56 +04:00
|
|
|
rw_exit(&fmi->fmi_lazy_lock);
|
2007-01-29 18:42:50 +03:00
|
|
|
if (fmi->fmi_state == FSTRANS_SUSPENDING ||
|
|
|
|
fmi->fmi_state == FSTRANS_SUSPENDED) {
|
|
|
|
fmi->fmi_state = FSTRANS_NORMAL;
|
2007-01-19 17:49:08 +03:00
|
|
|
fstrans_done(mp);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
panic("%s: illegal state %d", __func__, new_state);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get current file system state
|
|
|
|
*/
|
|
|
|
enum fstrans_state
|
|
|
|
fstrans_getstate(struct mount *mp)
|
|
|
|
{
|
|
|
|
struct fstrans_mount_info *fmi;
|
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
fmi = mp->mnt_transinfo;
|
2007-01-19 17:49:08 +03:00
|
|
|
|
|
|
|
return fmi->fmi_state;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Request a filesystem to suspend all operations.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
vfs_suspend(struct mount *mp, int nowait)
|
|
|
|
{
|
2007-02-15 18:40:50 +03:00
|
|
|
int error;
|
2007-01-19 17:49:08 +03:00
|
|
|
|
2007-02-15 18:40:50 +03:00
|
|
|
if (nowait) {
|
|
|
|
if (!mutex_tryenter(&vfs_suspend_lock))
|
|
|
|
return EWOULDBLOCK;
|
|
|
|
} else
|
|
|
|
mutex_enter(&vfs_suspend_lock);
|
2007-01-19 17:49:08 +03:00
|
|
|
|
2007-02-10 18:51:02 +03:00
|
|
|
mutex_enter(&syncer_mutex);
|
2007-01-19 17:49:08 +03:00
|
|
|
|
|
|
|
if ((error = VFS_SUSPENDCTL(mp, SUSPEND_SUSPEND)) != 0) {
|
2007-02-10 18:51:02 +03:00
|
|
|
mutex_exit(&syncer_mutex);
|
2007-02-15 18:40:50 +03:00
|
|
|
mutex_exit(&vfs_suspend_lock);
|
2007-01-19 17:49:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Request a filesystem to resume all operations.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
vfs_resume(struct mount *mp)
|
|
|
|
{
|
|
|
|
|
|
|
|
VFS_SUSPENDCTL(mp, SUSPEND_RESUME);
|
2007-02-10 18:51:02 +03:00
|
|
|
mutex_exit(&syncer_mutex);
|
2007-02-15 18:40:50 +03:00
|
|
|
mutex_exit(&vfs_suspend_lock);
|
2007-01-19 17:49:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(DDB)
|
|
|
|
void fstrans_dump(int);
|
|
|
|
|
|
|
|
static void
|
|
|
|
fstrans_print_lwp(struct proc *p, struct lwp *l, int verbose)
|
|
|
|
{
|
|
|
|
char prefix[9];
|
|
|
|
struct fstrans_lwp_info *fli;
|
|
|
|
|
|
|
|
snprintf(prefix, sizeof(prefix), "%d.%d", p->p_pid, l->l_lid);
|
|
|
|
for (fli = _lwp_getspecific_by_lwp(l, lwp_data_key);
|
|
|
|
fli;
|
|
|
|
fli = fli->fli_succ) {
|
2008-05-16 13:01:56 +04:00
|
|
|
if (!verbose && fli->fli_trans_cnt == 0)
|
2007-01-19 17:49:08 +03:00
|
|
|
continue;
|
|
|
|
printf("%-8s", prefix);
|
|
|
|
if (verbose)
|
|
|
|
printf(" @%p", fli);
|
|
|
|
if (fli->fli_mount != NULL)
|
|
|
|
printf(" (%s)", fli->fli_mount->mnt_stat.f_mntonname);
|
|
|
|
else
|
|
|
|
printf(" NULL");
|
|
|
|
switch (fli->fli_lock_type) {
|
2007-01-29 18:42:50 +03:00
|
|
|
case FSTRANS_LAZY:
|
2007-01-19 17:49:08 +03:00
|
|
|
printf(" lazy");
|
|
|
|
break;
|
2007-01-29 18:42:50 +03:00
|
|
|
case FSTRANS_SHARED:
|
2007-01-19 17:49:08 +03:00
|
|
|
printf(" shared");
|
|
|
|
break;
|
2007-01-29 18:42:50 +03:00
|
|
|
case FSTRANS_EXCL:
|
2007-01-19 17:49:08 +03:00
|
|
|
printf(" excl");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
printf(" %#x", fli->fli_lock_type);
|
|
|
|
break;
|
|
|
|
}
|
2008-05-16 13:01:56 +04:00
|
|
|
printf(" %d\n", fli->fli_trans_cnt);
|
2007-01-19 17:49:08 +03:00
|
|
|
prefix[0] = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
fstrans_print_mount(struct mount *mp, int verbose)
|
|
|
|
{
|
|
|
|
struct fstrans_mount_info *fmi;
|
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
fmi = mp->mnt_transinfo;
|
2007-01-29 18:42:50 +03:00
|
|
|
if (!verbose && (fmi == NULL || fmi->fmi_state == FSTRANS_NORMAL))
|
2007-01-19 17:49:08 +03:00
|
|
|
return;
|
|
|
|
|
|
|
|
printf("%-16s ", mp->mnt_stat.f_mntonname);
|
|
|
|
if (fmi == NULL) {
|
|
|
|
printf("(null)\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
switch (fmi->fmi_state) {
|
2007-01-29 18:42:50 +03:00
|
|
|
case FSTRANS_NORMAL:
|
2007-01-19 17:49:08 +03:00
|
|
|
printf("state normal\n");
|
|
|
|
break;
|
2007-01-29 18:42:50 +03:00
|
|
|
case FSTRANS_SUSPENDING:
|
2007-01-19 17:49:08 +03:00
|
|
|
printf("state suspending\n");
|
|
|
|
break;
|
2007-01-29 18:42:50 +03:00
|
|
|
case FSTRANS_SUSPENDED:
|
2007-01-19 17:49:08 +03:00
|
|
|
printf("state suspended\n");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
printf("state %#x\n", fmi->fmi_state);
|
|
|
|
break;
|
|
|
|
}
|
2007-05-16 20:11:56 +04:00
|
|
|
printf("%16s r=%d w=%d\n", "lock_lazy:",
|
|
|
|
rw_read_held(&fmi->fmi_lazy_lock),
|
|
|
|
rw_write_held(&fmi->fmi_lazy_lock));
|
|
|
|
printf("%16s r=%d w=%d\n", "lock_shared:",
|
|
|
|
rw_read_held(&fmi->fmi_shared_lock),
|
|
|
|
rw_write_held(&fmi->fmi_shared_lock));
|
2007-01-19 17:49:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
fstrans_dump(int full)
|
|
|
|
{
|
|
|
|
const struct proclist_desc *pd;
|
|
|
|
struct proc *p;
|
|
|
|
struct lwp *l;
|
|
|
|
struct mount *mp;
|
|
|
|
|
|
|
|
printf("Fstrans locks by lwp:\n");
|
|
|
|
for (pd = proclists; pd->pd_list != NULL; pd++)
|
|
|
|
LIST_FOREACH(p, pd->pd_list, p_list)
|
|
|
|
LIST_FOREACH(l, &p->p_lwps, l_sibling)
|
|
|
|
fstrans_print_lwp(p, l, full == 1);
|
|
|
|
|
|
|
|
printf("Fstrans state by mount:\n");
|
|
|
|
CIRCLEQ_FOREACH(mp, &mountlist, mnt_list)
|
|
|
|
fstrans_print_mount(mp, full == 1);
|
|
|
|
}
|
|
|
|
#endif /* defined(DDB) */
|
2007-10-07 17:38:53 +04:00
|
|
|
|
|
|
|
int
|
2007-12-02 16:56:15 +03:00
|
|
|
fscow_establish(struct mount *mp, int (*func)(void *, struct buf *, bool),
|
|
|
|
void *arg)
|
2007-10-07 17:38:53 +04:00
|
|
|
{
|
2008-01-02 14:48:20 +03:00
|
|
|
struct fstrans_mount_info *fmi;
|
2007-10-07 17:38:53 +04:00
|
|
|
struct fscow_handler *new;
|
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
if ((mp->mnt_iflag & IMNT_HAS_TRANS) == 0)
|
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
fmi = mp->mnt_transinfo;
|
2007-10-07 17:38:53 +04:00
|
|
|
|
|
|
|
if ((new = kmem_alloc(sizeof(*new), KM_SLEEP)) == NULL)
|
|
|
|
return ENOMEM;
|
|
|
|
new->ch_func = func;
|
|
|
|
new->ch_arg = arg;
|
2008-01-02 14:48:20 +03:00
|
|
|
rw_enter(&fmi->fmi_cow_lock, RW_WRITER);
|
|
|
|
SLIST_INSERT_HEAD(&fmi->fmi_cow_handler, new, ch_list);
|
|
|
|
rw_exit(&fmi->fmi_cow_lock);
|
2007-10-07 17:38:53 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2007-12-02 16:56:15 +03:00
|
|
|
fscow_disestablish(struct mount *mp, int (*func)(void *, struct buf *, bool),
|
2007-10-07 17:38:53 +04:00
|
|
|
void *arg)
|
|
|
|
{
|
2008-01-02 14:48:20 +03:00
|
|
|
struct fstrans_mount_info *fmi;
|
2007-10-07 17:38:53 +04:00
|
|
|
struct fscow_handler *hp = NULL;
|
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
if ((mp->mnt_iflag & IMNT_HAS_TRANS) == 0)
|
2007-10-07 17:38:53 +04:00
|
|
|
return EINVAL;
|
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
fmi = mp->mnt_transinfo;
|
|
|
|
|
|
|
|
rw_enter(&fmi->fmi_cow_lock, RW_WRITER);
|
|
|
|
SLIST_FOREACH(hp, &fmi->fmi_cow_handler, ch_list)
|
2007-10-07 17:38:53 +04:00
|
|
|
if (hp->ch_func == func && hp->ch_arg == arg)
|
|
|
|
break;
|
|
|
|
if (hp != NULL) {
|
2008-01-02 14:48:20 +03:00
|
|
|
SLIST_REMOVE(&fmi->fmi_cow_handler, hp, fscow_handler, ch_list);
|
2007-10-07 17:38:53 +04:00
|
|
|
kmem_free(hp, sizeof(*hp));
|
|
|
|
}
|
2008-01-02 14:48:20 +03:00
|
|
|
rw_exit(&fmi->fmi_cow_lock);
|
2007-10-07 17:38:53 +04:00
|
|
|
|
|
|
|
return hp ? 0 : EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2007-12-02 16:56:15 +03:00
|
|
|
fscow_run(struct buf *bp, bool data_valid)
|
2007-10-07 17:38:53 +04:00
|
|
|
{
|
|
|
|
int error = 0;
|
|
|
|
struct mount *mp;
|
2008-05-16 13:01:56 +04:00
|
|
|
struct fstrans_lwp_info *fli;
|
2008-01-02 14:48:20 +03:00
|
|
|
struct fstrans_mount_info *fmi;
|
2007-10-07 17:38:53 +04:00
|
|
|
struct fscow_handler *hp;
|
|
|
|
|
2008-02-02 19:51:34 +03:00
|
|
|
if ((bp->b_flags & B_COWDONE))
|
2007-12-02 16:56:15 +03:00
|
|
|
goto done;
|
2007-10-07 17:38:53 +04:00
|
|
|
if (bp->b_vp == NULL)
|
2007-12-02 16:56:15 +03:00
|
|
|
goto done;
|
2007-10-07 17:38:53 +04:00
|
|
|
if (bp->b_vp->v_type == VBLK)
|
|
|
|
mp = bp->b_vp->v_specmountpoint;
|
|
|
|
else
|
|
|
|
mp = bp->b_vp->v_mount;
|
2008-01-02 14:48:20 +03:00
|
|
|
if (mp == NULL || (mp->mnt_iflag & IMNT_HAS_TRANS) == 0)
|
2007-12-02 16:56:15 +03:00
|
|
|
goto done;
|
2007-10-07 17:38:53 +04:00
|
|
|
|
2008-05-16 13:01:56 +04:00
|
|
|
fli = fstrans_get_lwp_info(mp);
|
2008-01-02 14:48:20 +03:00
|
|
|
fmi = mp->mnt_transinfo;
|
2007-10-07 17:38:53 +04:00
|
|
|
|
2008-05-16 13:01:56 +04:00
|
|
|
if (fli->fli_cow_cnt++ == 0)
|
|
|
|
rw_enter(&fmi->fmi_cow_lock, RW_READER);
|
|
|
|
|
2008-01-02 14:48:20 +03:00
|
|
|
SLIST_FOREACH(hp, &fmi->fmi_cow_handler, ch_list)
|
2007-12-02 16:56:15 +03:00
|
|
|
if ((error = (*hp->ch_func)(hp->ch_arg, bp, data_valid)) != 0)
|
2007-10-07 17:38:53 +04:00
|
|
|
break;
|
2008-05-16 13:01:56 +04:00
|
|
|
|
|
|
|
if (--fli->fli_cow_cnt == 0)
|
|
|
|
rw_exit(&fmi->fmi_cow_lock);
|
2008-01-02 14:48:20 +03:00
|
|
|
|
|
|
|
done:
|
2008-02-02 19:51:34 +03:00
|
|
|
if (error == 0)
|
|
|
|
bp->b_flags |= B_COWDONE;
|
2007-12-02 16:56:15 +03:00
|
|
|
|
2007-10-07 17:38:53 +04:00
|
|
|
return error;
|
|
|
|
}
|