Home | History | Annotate | Line # | Download | only in kern
vfs_trans.c revision 1.5.8.8
      1 /*	$NetBSD: vfs_trans.c,v 1.5.8.8 2007/11/07 08:22:12 hannken Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2007 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Juergen Hannken-Illjes.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: vfs_trans.c,v 1.5.8.8 2007/11/07 08:22:12 hannken Exp $");
     41 
     42 /*
     43  * File system transaction operations.
     44  */
     45 
     46 #include "opt_ddb.h"
     47 
     48 #if defined(DDB)
     49 #define _LWP_API_PRIVATE	/* Need _lwp_getspecific_by_lwp() */
     50 #endif
     51 
     52 #include <sys/param.h>
     53 #include <sys/systm.h>
     54 #include <sys/malloc.h>
     55 #include <sys/kmem.h>
     56 #include <sys/mount.h>
     57 #include <sys/rwlock.h>
     58 #include <sys/vnode.h>
     59 #define _FSTRANS_API_PRIVATE
     60 #include <sys/fstrans.h>
     61 #include <sys/proc.h>
     62 
     63 #include <miscfs/specfs/specdev.h>
     64 #include <miscfs/syncfs/syncfs.h>
     65 
     66 struct fscow_handler {
     67 	SLIST_ENTRY(fscow_handler) ch_list;
     68 	int (*ch_func)(void *, struct buf *);
     69 	void *ch_arg;
     70 };
     71 struct fstrans_lwp_info {
     72 	struct fstrans_lwp_info *fli_succ;
     73 	struct mount *fli_mount;
     74 	int fli_count;
     75 	enum fstrans_lock_type fli_lock_type;
     76 };
     77 struct fstrans_mount_info {
     78 	enum fstrans_state fmi_state;
     79 	krwlock_t fmi_shared_lock;
     80 	krwlock_t fmi_lazy_lock;
     81 	krwlock_t fmi_cow_lock;
     82 	SLIST_HEAD(, fscow_handler) fmi_cow_handler;
     83 };
     84 
     85 static specificdata_key_t lwp_data_key;
     86 static kmutex_t vfs_suspend_lock;	/* Serialize suspensions. */
     87 static kmutex_t fstrans_init_lock;
     88 
     89 POOL_INIT(fstrans_pl, sizeof(struct fstrans_lwp_info), 0, 0, 0,
     90     "fstrans", NULL, IPL_NONE);
     91 
     92 static void fstrans_lwp_dtor(void *);
     93 static struct fstrans_mount_info *fstrans_mount_init(struct mount *);
     94 
     95 /*
     96  * Initialize
     97  */
     98 void
     99 fstrans_init(void)
    100 {
    101 	int error;
    102 
    103 	error = lwp_specific_key_create(&lwp_data_key, fstrans_lwp_dtor);
    104 	KASSERT(error == 0);
    105 
    106 	mutex_init(&vfs_suspend_lock, MUTEX_DEFAULT, IPL_NONE);
    107 	mutex_init(&fstrans_init_lock, MUTEX_DEFAULT, IPL_NONE);
    108 }
    109 
    110 /*
    111  * Deallocate lwp state
    112  */
    113 static void
    114 fstrans_lwp_dtor(void *arg)
    115 {
    116 	struct fstrans_lwp_info *fli, *fli_next;
    117 
    118 	for (fli = arg; fli; fli = fli_next) {
    119 		KASSERT(fli->fli_mount == NULL);
    120 		KASSERT(fli->fli_count == 0);
    121 		fli_next = fli->fli_succ;
    122 		pool_put(&fstrans_pl, fli);
    123 	}
    124 }
    125 
    126 /*
    127  * Deallocate mount state
    128  */
    129 void
    130 fstrans_unmount(struct mount *mp)
    131 {
    132 	struct fstrans_mount_info *fmi = mp->mnt_transinfo;
    133 
    134 	KASSERT(fmi->fmi_state == FSTRANS_NORMAL);
    135 	rw_destroy(&fmi->fmi_lazy_lock);
    136 	rw_destroy(&fmi->fmi_shared_lock);
    137 	KASSERT(SLIST_EMPTY(&fmi->fmi_cow_handler));
    138 	rw_destroy(&fmi->fmi_cow_lock);
    139 	free(fmi, M_MOUNT);
    140 	mp->mnt_transinfo = NULL;
    141 }
    142 
    143 /*
    144  * Create mount info for this mount
    145  */
    146 static struct fstrans_mount_info *
    147 fstrans_mount_init(struct mount *mp)
    148 {
    149 	struct fstrans_mount_info *new;
    150 
    151 	mutex_enter(&fstrans_init_lock);
    152 
    153 	if ((new = mp->mnt_transinfo) != NULL) {
    154 		mutex_exit(&fstrans_init_lock);
    155 		return new;
    156 	}
    157 
    158 	new = malloc(sizeof(*new), M_MOUNT, M_WAITOK);
    159 	new->fmi_state = FSTRANS_NORMAL;
    160 	rw_init(&new->fmi_lazy_lock);
    161 	rw_init(&new->fmi_shared_lock);
    162 	SLIST_INIT(&new->fmi_cow_handler);
    163 	rw_init(&new->fmi_cow_lock);
    164 
    165 	mp->mnt_transinfo = new;
    166 	mutex_exit(&fstrans_init_lock);
    167 
    168 	return new;
    169 }
    170 
    171 /*
    172  * Start a transaction.  If this thread already has a transaction on this
    173  * file system increment the reference counter.
    174  * A thread with an exclusive transaction lock may get a shared or lazy one.
    175  * A thread with a shared or lazy transaction lock cannot upgrade to an
    176  * exclusive one yet.
    177  */
    178 int
    179 _fstrans_start(struct mount *mp, enum fstrans_lock_type lock_type, int wait)
    180 {
    181 	krwlock_t *lock_p;
    182 	krw_t lock_op;
    183 	struct fstrans_lwp_info *fli, *new_fli;
    184 	struct fstrans_mount_info *fmi;
    185 
    186 	ASSERT_SLEEPABLE(NULL, __func__);
    187 
    188 	if (mp == NULL || (mp->mnt_iflag & IMNT_HAS_TRANS) == 0)
    189 		return 0;
    190 
    191 	new_fli = NULL;
    192 	for (fli = lwp_getspecific(lwp_data_key); fli; fli = fli->fli_succ) {
    193 		if (fli->fli_mount == NULL && new_fli == NULL)
    194 			new_fli = fli;
    195 		if (fli->fli_mount == mp) {
    196 			KASSERT(fli->fli_count > 0);
    197 			if (fli->fli_lock_type != FSTRANS_EXCL &&
    198 			    lock_type == FSTRANS_EXCL)
    199 				panic("fstrans_start: cannot upgrade lock");
    200 			fli->fli_count += 1;
    201 			return 0;
    202 		}
    203 	}
    204 
    205 	if (new_fli == NULL) {
    206 		new_fli = pool_get(&fstrans_pl, PR_WAITOK);
    207 		new_fli->fli_mount = NULL;
    208 		new_fli->fli_count = 0;
    209 		new_fli->fli_succ = lwp_getspecific(lwp_data_key);
    210 		lwp_setspecific(lwp_data_key, new_fli);
    211 	}
    212 
    213 	KASSERT(new_fli->fli_mount == NULL);
    214 	KASSERT(new_fli->fli_count == 0);
    215 
    216 	if ((fmi = mp->mnt_transinfo) == NULL)
    217 		fmi = fstrans_mount_init(mp);
    218 
    219 	if (lock_type == FSTRANS_LAZY)
    220 		lock_p = &fmi->fmi_lazy_lock;
    221 	else
    222 		lock_p = &fmi->fmi_shared_lock;
    223 	lock_op = (lock_type == FSTRANS_EXCL ? RW_WRITER : RW_READER);
    224 
    225 	if (wait)
    226 		rw_enter(lock_p, lock_op);
    227 	else if (rw_tryenter(lock_p, lock_op) == 0)
    228 		return EBUSY;
    229 
    230 	new_fli->fli_mount = mp;
    231 	new_fli->fli_count = 1;
    232 	new_fli->fli_lock_type = lock_type;
    233 
    234 	return 0;
    235 }
    236 
    237 /*
    238  * Finish a transaction.
    239  */
    240 void
    241 fstrans_done(struct mount *mp)
    242 {
    243 	struct fstrans_lwp_info *fli;
    244 	struct fstrans_mount_info *fmi;
    245 
    246 	if (mp == NULL || (mp->mnt_iflag & IMNT_HAS_TRANS) == 0)
    247 		return;
    248 
    249 	for (fli = lwp_getspecific(lwp_data_key); fli; fli = fli->fli_succ) {
    250 		if (fli->fli_mount == mp) {
    251 			fli->fli_count -= 1;
    252 			if (fli->fli_count > 0)
    253 				return;
    254 			break;
    255 		}
    256 	}
    257 
    258 	KASSERT(fli != NULL);
    259 	KASSERT(fli->fli_mount == mp);
    260 	KASSERT(fli->fli_count == 0);
    261 	fli->fli_mount = NULL;
    262 	fmi = mp->mnt_transinfo;
    263 	KASSERT(fmi != NULL);
    264 	if (fli->fli_lock_type == FSTRANS_LAZY)
    265 		rw_exit(&fmi->fmi_lazy_lock);
    266 	else
    267 		rw_exit(&fmi->fmi_shared_lock);
    268 }
    269 
    270 /*
    271  * Check if this thread has an exclusive lock.
    272  */
    273 int
    274 fstrans_is_owner(struct mount *mp)
    275 {
    276 	struct fstrans_lwp_info *fli;
    277 
    278 	if (mp == NULL)
    279 		return 0;
    280 	if ((mp->mnt_iflag & IMNT_HAS_TRANS) == 0)
    281 		return 0;
    282 
    283 	for (fli = lwp_getspecific(lwp_data_key); fli; fli = fli->fli_succ)
    284 		if (fli->fli_mount == mp)
    285 			break;
    286 
    287 	if (fli == NULL)
    288 		return 0;
    289 
    290 	KASSERT(fli->fli_mount == mp);
    291 	KASSERT(fli->fli_count > 0);
    292 	return (fli->fli_lock_type == FSTRANS_EXCL);
    293 }
    294 
    295 /*
    296  * Set new file system state.
    297  */
    298 int
    299 fstrans_setstate(struct mount *mp, enum fstrans_state new_state)
    300 {
    301 	struct fstrans_mount_info *fmi;
    302 
    303 	if ((fmi = mp->mnt_transinfo) == NULL)
    304 		fmi = fstrans_mount_init(mp);
    305 
    306 	switch (new_state) {
    307 	case FSTRANS_SUSPENDING:
    308 		KASSERT(fmi->fmi_state == FSTRANS_NORMAL);
    309 		fstrans_start(mp, FSTRANS_EXCL);
    310 		fmi->fmi_state = FSTRANS_SUSPENDING;
    311 		break;
    312 
    313 	case FSTRANS_SUSPENDED:
    314 		KASSERT(fmi->fmi_state == FSTRANS_NORMAL ||
    315 			fmi->fmi_state == FSTRANS_SUSPENDING);
    316 		KASSERT(fmi->fmi_state == FSTRANS_NORMAL ||
    317 			fstrans_is_owner(mp));
    318 		if (fmi->fmi_state == FSTRANS_NORMAL)
    319 			fstrans_start(mp, FSTRANS_EXCL);
    320 		rw_enter(&fmi->fmi_lazy_lock, RW_WRITER);
    321 		fmi->fmi_state = FSTRANS_SUSPENDED;
    322 		break;
    323 
    324 	case FSTRANS_NORMAL:
    325 		KASSERT(fmi->fmi_state == FSTRANS_NORMAL ||
    326 			fstrans_is_owner(mp));
    327 		if (fmi->fmi_state == FSTRANS_SUSPENDED)
    328 			rw_exit(&fmi->fmi_lazy_lock);
    329 		if (fmi->fmi_state == FSTRANS_SUSPENDING ||
    330 		    fmi->fmi_state == FSTRANS_SUSPENDED) {
    331 			fmi->fmi_state = FSTRANS_NORMAL;
    332 			fstrans_done(mp);
    333 		}
    334 		break;
    335 
    336 	default:
    337 		panic("%s: illegal state %d", __func__, new_state);
    338 	}
    339 
    340 	return 0;
    341 }
    342 
    343 /*
    344  * Get current file system state
    345  */
    346 enum fstrans_state
    347 fstrans_getstate(struct mount *mp)
    348 {
    349 	struct fstrans_mount_info *fmi;
    350 
    351 	if ((fmi = mp->mnt_transinfo) == NULL)
    352 		return FSTRANS_NORMAL;
    353 
    354 	return fmi->fmi_state;
    355 }
    356 
    357 /*
    358  * Request a filesystem to suspend all operations.
    359  */
    360 int
    361 vfs_suspend(struct mount *mp, int nowait)
    362 {
    363 	int error;
    364 
    365 	if (nowait) {
    366 		if (!mutex_tryenter(&vfs_suspend_lock))
    367 			return EWOULDBLOCK;
    368 	} else
    369 		mutex_enter(&vfs_suspend_lock);
    370 
    371 	mutex_enter(&syncer_mutex);
    372 
    373 	if ((error = VFS_SUSPENDCTL(mp, SUSPEND_SUSPEND)) != 0) {
    374 		mutex_exit(&syncer_mutex);
    375 		mutex_exit(&vfs_suspend_lock);
    376 	}
    377 
    378 	return error;
    379 }
    380 
    381 /*
    382  * Request a filesystem to resume all operations.
    383  */
    384 void
    385 vfs_resume(struct mount *mp)
    386 {
    387 
    388 	VFS_SUSPENDCTL(mp, SUSPEND_RESUME);
    389 	mutex_exit(&syncer_mutex);
    390 	mutex_exit(&vfs_suspend_lock);
    391 }
    392 
    393 #if defined(DDB)
    394 void fstrans_dump(int);
    395 
    396 static void
    397 fstrans_print_lwp(struct proc *p, struct lwp *l, int verbose)
    398 {
    399 	char prefix[9];
    400 	struct fstrans_lwp_info *fli;
    401 
    402 	snprintf(prefix, sizeof(prefix), "%d.%d", p->p_pid, l->l_lid);
    403 	for (fli = _lwp_getspecific_by_lwp(l, lwp_data_key);
    404 	     fli;
    405 	     fli = fli->fli_succ) {
    406 		if (!verbose && fli->fli_count == 0)
    407 			continue;
    408 		printf("%-8s", prefix);
    409 		if (verbose)
    410 			printf(" @%p", fli);
    411 		if (fli->fli_mount != NULL)
    412 			printf(" (%s)", fli->fli_mount->mnt_stat.f_mntonname);
    413 		else
    414 			printf(" NULL");
    415 		switch (fli->fli_lock_type) {
    416 		case FSTRANS_LAZY:
    417 			printf(" lazy");
    418 			break;
    419 		case FSTRANS_SHARED:
    420 			printf(" shared");
    421 			break;
    422 		case FSTRANS_EXCL:
    423 			printf(" excl");
    424 			break;
    425 		default:
    426 			printf(" %#x", fli->fli_lock_type);
    427 			break;
    428 		}
    429 		printf(" %d\n", fli->fli_count);
    430 		prefix[0] = '\0';
    431 	}
    432 }
    433 
    434 static void
    435 fstrans_print_mount(struct mount *mp, int verbose)
    436 {
    437 	struct fstrans_mount_info *fmi;
    438 
    439 	fmi = mp->mnt_transinfo;
    440 	if (!verbose && (fmi == NULL || fmi->fmi_state == FSTRANS_NORMAL))
    441 		return;
    442 
    443 	printf("%-16s ", mp->mnt_stat.f_mntonname);
    444 	if (fmi == NULL) {
    445 		printf("(null)\n");
    446 		return;
    447 	}
    448 	switch (fmi->fmi_state) {
    449 	case FSTRANS_NORMAL:
    450 		printf("state normal\n");
    451 		break;
    452 	case FSTRANS_SUSPENDING:
    453 		printf("state suspending\n");
    454 		break;
    455 	case FSTRANS_SUSPENDED:
    456 		printf("state suspended\n");
    457 		break;
    458 	default:
    459 		printf("state %#x\n", fmi->fmi_state);
    460 		break;
    461 	}
    462 	printf("%16s r=%d w=%d\n", "lock_lazy:",
    463 	    rw_read_held(&fmi->fmi_lazy_lock),
    464 	    rw_write_held(&fmi->fmi_lazy_lock));
    465 	printf("%16s r=%d w=%d\n", "lock_shared:",
    466 	    rw_read_held(&fmi->fmi_shared_lock),
    467 	    rw_write_held(&fmi->fmi_shared_lock));
    468 }
    469 
    470 void
    471 fstrans_dump(int full)
    472 {
    473 	const struct proclist_desc *pd;
    474 	struct proc *p;
    475 	struct lwp *l;
    476 	struct mount *mp;
    477 
    478 	printf("Fstrans locks by lwp:\n");
    479 	for (pd = proclists; pd->pd_list != NULL; pd++)
    480 		LIST_FOREACH(p, pd->pd_list, p_list)
    481 			LIST_FOREACH(l, &p->p_lwps, l_sibling)
    482 				fstrans_print_lwp(p, l, full == 1);
    483 
    484 	printf("Fstrans state by mount:\n");
    485 	CIRCLEQ_FOREACH(mp, &mountlist, mnt_list)
    486 		fstrans_print_mount(mp, full == 1);
    487 }
    488 #endif /* defined(DDB) */
    489 
    490 int
    491 fscow_establish(struct mount *mp, int (*func)(void *, struct buf *), void *arg)
    492 {
    493 	struct fstrans_mount_info *fmi;
    494 	struct fscow_handler *new;
    495 
    496 	if ((fmi = mp->mnt_transinfo) == NULL)
    497 		fmi = fstrans_mount_init(mp);
    498 
    499 	if ((new = kmem_alloc(sizeof(*new), KM_SLEEP)) == NULL)
    500 		return ENOMEM;
    501 	new->ch_func = func;
    502 	new->ch_arg = arg;
    503 	rw_enter(&fmi->fmi_cow_lock, RW_WRITER);
    504 	SLIST_INSERT_HEAD(&fmi->fmi_cow_handler, new, ch_list);
    505 	rw_exit(&fmi->fmi_cow_lock);
    506 
    507 	return 0;
    508 }
    509 
    510 int
    511 fscow_disestablish(struct mount *mp, int (*func)(void *, struct buf *),
    512     void *arg)
    513 {
    514 	struct fstrans_mount_info *fmi;
    515 	struct fscow_handler *hp = NULL;
    516 
    517 	if ((fmi = mp->mnt_transinfo) == NULL)
    518 		return EINVAL;
    519 
    520 	rw_enter(&fmi->fmi_cow_lock, RW_WRITER);
    521 	SLIST_FOREACH(hp, &fmi->fmi_cow_handler, ch_list)
    522 		if (hp->ch_func == func && hp->ch_arg == arg)
    523 			break;
    524 	if (hp != NULL) {
    525 		SLIST_REMOVE(&fmi->fmi_cow_handler, hp, fscow_handler, ch_list);
    526 		kmem_free(hp, sizeof(*hp));
    527 	}
    528 	rw_exit(&fmi->fmi_cow_lock);
    529 
    530 	return hp ? 0 : EINVAL;
    531 }
    532 
    533 int
    534 fscow_run(struct buf *bp)
    535 {
    536 	int error = 0;
    537 	struct mount *mp;
    538 	struct fstrans_mount_info *fmi;
    539 	struct fscow_handler *hp;
    540 
    541 	if (bp->b_vp == NULL)
    542 		return 0;
    543 	if (bp->b_vp->v_type == VBLK)
    544 		mp = bp->b_vp->v_specmountpoint;
    545 	else
    546 		mp = bp->b_vp->v_mount;
    547 	if (mp == NULL)
    548 		return 0;
    549 
    550 	if ((fmi = mp->mnt_transinfo) == NULL)
    551 		return 0;
    552 
    553 	rw_enter(&fmi->fmi_cow_lock, RW_READER);
    554 	SLIST_FOREACH(hp, &fmi->fmi_cow_handler, ch_list)
    555 		if ((error = (*hp->ch_func)(hp->ch_arg, bp)) != 0)
    556 			break;
    557 	rw_exit(&fmi->fmi_cow_lock);
    558 
    559 	return error;
    560 }
    561