Home | History | Annotate | Line # | Download | only in kern
vfs_trans.c revision 1.9
      1 /*	$NetBSD: vfs_trans.c,v 1.9 2007/05/17 07:26:22 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.9 2007/05/17 07:26:22 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/mount.h>
     56 #include <sys/rwlock.h>
     57 #include <sys/vnode.h>
     58 #define _FSTRANS_API_PRIVATE
     59 #include <sys/fstrans.h>
     60 
     61 #include <miscfs/syncfs/syncfs.h>
     62 
     63 struct fstrans_lwp_info {
     64 	struct fstrans_lwp_info *fli_succ;
     65 	struct mount *fli_mount;
     66 	int fli_count;
     67 	enum fstrans_lock_type fli_lock_type;
     68 };
     69 struct fstrans_mount_info {
     70 	enum fstrans_state fmi_state;
     71 	krwlock_t fmi_shared_lock;
     72 	krwlock_t fmi_lazy_lock;
     73 };
     74 
     75 static specificdata_key_t lwp_data_key;
     76 static specificdata_key_t mount_data_key;
     77 static kmutex_t vfs_suspend_lock;	/* Serialize suspensions. */
     78 static kmutex_t fstrans_init_lock;
     79 
     80 POOL_INIT(fstrans_pl, sizeof(struct fstrans_lwp_info), 0, 0, 0,
     81     "fstrans", NULL, IPL_NONE);
     82 
     83 static void fstrans_lwp_dtor(void *);
     84 static void fstrans_mount_dtor(void *);
     85 static struct fstrans_mount_info *fstrans_mount_init(struct mount *);
     86 
     87 /*
     88  * Initialize
     89  */
     90 void
     91 fstrans_init(void)
     92 {
     93 	int error;
     94 
     95 	error = lwp_specific_key_create(&lwp_data_key, fstrans_lwp_dtor);
     96 	KASSERT(error == 0);
     97 	error = mount_specific_key_create(&mount_data_key, fstrans_mount_dtor);
     98 	KASSERT(error == 0);
     99 
    100 	mutex_init(&vfs_suspend_lock, MUTEX_DEFAULT, IPL_NONE);
    101 	mutex_init(&fstrans_init_lock, MUTEX_DEFAULT, IPL_NONE);
    102 }
    103 
    104 /*
    105  * Deallocate lwp state
    106  */
    107 static void
    108 fstrans_lwp_dtor(void *arg)
    109 {
    110 	struct fstrans_lwp_info *fli, *fli_next;
    111 
    112 	for (fli = arg; fli; fli = fli_next) {
    113 		KASSERT(fli->fli_mount == NULL);
    114 		KASSERT(fli->fli_count == 0);
    115 		fli_next = fli->fli_succ;
    116 		pool_put(&fstrans_pl, fli);
    117 	}
    118 }
    119 
    120 /*
    121  * Deallocate mount state
    122  */
    123 static void
    124 fstrans_mount_dtor(void *arg)
    125 {
    126 	struct fstrans_mount_info *fmi = arg;
    127 
    128 	KASSERT(fmi->fmi_state == FSTRANS_NORMAL);
    129 	rw_destroy(&fmi->fmi_lazy_lock);
    130 	rw_destroy(&fmi->fmi_shared_lock);
    131 	free(fmi, M_MOUNT);
    132 }
    133 
    134 /*
    135  * Create mount info for this mount
    136  */
    137 static struct fstrans_mount_info *
    138 fstrans_mount_init(struct mount *mp)
    139 {
    140 	struct fstrans_mount_info *new;
    141 
    142 	mutex_enter(&fstrans_init_lock);
    143 
    144 	if ((new = mount_getspecific(mp, mount_data_key)) != NULL) {
    145 		mutex_exit(&fstrans_init_lock);
    146 		return new;
    147 	}
    148 
    149 	new = malloc(sizeof(*new), M_MOUNT, M_WAITOK);
    150 	new->fmi_state = FSTRANS_NORMAL;
    151 	rw_init(&new->fmi_lazy_lock);
    152 	rw_init(&new->fmi_shared_lock);
    153 
    154 	mount_setspecific(mp, mount_data_key, new);
    155 	mutex_exit(&fstrans_init_lock);
    156 
    157 	return new;
    158 }
    159 
    160 /*
    161  * Start a transaction.  If this thread already has a transaction on this
    162  * file system increment the reference counter.
    163  * A thread with an exclusive transaction lock may get a shared or lazy one.
    164  * A thread with a shared or lazy transaction lock cannot upgrade to an
    165  * exclusive one yet.
    166  */
    167 int
    168 _fstrans_start(struct mount *mp, enum fstrans_lock_type lock_type, int wait)
    169 {
    170 	krwlock_t *lock_p;
    171 	krw_t lock_op;
    172 	struct fstrans_lwp_info *fli, *new_fli;
    173 	struct fstrans_mount_info *fmi;
    174 
    175 	ASSERT_SLEEPABLE(NULL, __func__);
    176 
    177 	if (mp == NULL || (mp->mnt_iflag & IMNT_HAS_TRANS) == 0)
    178 		return 0;
    179 
    180 	new_fli = NULL;
    181 	for (fli = lwp_getspecific(lwp_data_key); fli; fli = fli->fli_succ) {
    182 		if (fli->fli_mount == NULL && new_fli == NULL)
    183 			new_fli = fli;
    184 		if (fli->fli_mount == mp) {
    185 			KASSERT(fli->fli_count > 0);
    186 			if (fli->fli_lock_type != FSTRANS_EXCL &&
    187 			    lock_type == FSTRANS_EXCL)
    188 				panic("fstrans_start: cannot upgrade lock");
    189 			fli->fli_count += 1;
    190 			return 0;
    191 		}
    192 	}
    193 
    194 	if (new_fli == NULL) {
    195 		new_fli = pool_get(&fstrans_pl, PR_WAITOK);
    196 		new_fli->fli_mount = NULL;
    197 		new_fli->fli_count = 0;
    198 		new_fli->fli_succ = lwp_getspecific(lwp_data_key);
    199 		lwp_setspecific(lwp_data_key, new_fli);
    200 	}
    201 
    202 	KASSERT(new_fli->fli_mount == NULL);
    203 	KASSERT(new_fli->fli_count == 0);
    204 
    205 	if ((fmi = mount_getspecific(mp, mount_data_key)) == NULL)
    206 		fmi = fstrans_mount_init(mp);
    207 
    208 	if (lock_type == FSTRANS_LAZY)
    209 		lock_p = &fmi->fmi_lazy_lock;
    210 	else
    211 		lock_p = &fmi->fmi_shared_lock;
    212 	lock_op = (lock_type == FSTRANS_EXCL ? RW_WRITER : RW_READER);
    213 
    214 	if (wait)
    215 		rw_enter(lock_p, lock_op);
    216 	else if (rw_tryenter(lock_p, lock_op) == 0)
    217 		return EBUSY;
    218 
    219 	new_fli->fli_mount = mp;
    220 	new_fli->fli_count = 1;
    221 	new_fli->fli_lock_type = lock_type;
    222 
    223 	return 0;
    224 }
    225 
    226 /*
    227  * Finish a transaction.
    228  */
    229 void
    230 fstrans_done(struct mount *mp)
    231 {
    232 	struct fstrans_lwp_info *fli;
    233 	struct fstrans_mount_info *fmi;
    234 
    235 	if (mp == NULL || (mp->mnt_iflag & IMNT_HAS_TRANS) == 0)
    236 		return;
    237 
    238 	for (fli = lwp_getspecific(lwp_data_key); fli; fli = fli->fli_succ) {
    239 		if (fli->fli_mount == mp) {
    240 			fli->fli_count -= 1;
    241 			if (fli->fli_count > 0)
    242 				return;
    243 			break;
    244 		}
    245 	}
    246 
    247 	KASSERT(fli != NULL);
    248 	KASSERT(fli->fli_mount == mp);
    249 	KASSERT(fli->fli_count == 0);
    250 	fli->fli_mount = NULL;
    251 	fmi = mount_getspecific(mp, mount_data_key);
    252 	KASSERT(fmi != NULL);
    253 	if (fli->fli_lock_type == FSTRANS_LAZY)
    254 		rw_exit(&fmi->fmi_lazy_lock);
    255 	else
    256 		rw_exit(&fmi->fmi_shared_lock);
    257 }
    258 
    259 /*
    260  * Check if this thread has an exclusive lock.
    261  */
    262 int
    263 fstrans_is_owner(struct mount *mp)
    264 {
    265 	struct fstrans_lwp_info *fli;
    266 
    267 	if (mp == NULL)
    268 		return 0;
    269 	if ((mp->mnt_iflag & IMNT_HAS_TRANS) == 0)
    270 		return 0;
    271 
    272 	for (fli = lwp_getspecific(lwp_data_key); fli; fli = fli->fli_succ)
    273 		if (fli->fli_mount == mp)
    274 			break;
    275 
    276 	if (fli == NULL)
    277 		return 0;
    278 
    279 	KASSERT(fli->fli_mount == mp);
    280 	KASSERT(fli->fli_count > 0);
    281 	return (fli->fli_lock_type == FSTRANS_EXCL);
    282 }
    283 
    284 /*
    285  * Set new file system state.
    286  */
    287 int
    288 fstrans_setstate(struct mount *mp, enum fstrans_state new_state)
    289 {
    290 	struct fstrans_mount_info *fmi;
    291 
    292 	if ((fmi = mount_getspecific(mp, mount_data_key)) == NULL)
    293 		fmi = fstrans_mount_init(mp);
    294 
    295 	switch (new_state) {
    296 	case FSTRANS_SUSPENDING:
    297 		KASSERT(fmi->fmi_state == FSTRANS_NORMAL);
    298 		fstrans_start(mp, FSTRANS_EXCL);
    299 		fmi->fmi_state = FSTRANS_SUSPENDING;
    300 		break;
    301 
    302 	case FSTRANS_SUSPENDED:
    303 		KASSERT(fmi->fmi_state == FSTRANS_NORMAL ||
    304 			fmi->fmi_state == FSTRANS_SUSPENDING);
    305 		KASSERT(fmi->fmi_state == FSTRANS_NORMAL ||
    306 			fstrans_is_owner(mp));
    307 		if (fmi->fmi_state == FSTRANS_NORMAL)
    308 			fstrans_start(mp, FSTRANS_EXCL);
    309 		rw_enter(&fmi->fmi_lazy_lock, RW_WRITER);
    310 		fmi->fmi_state = FSTRANS_SUSPENDED;
    311 		break;
    312 
    313 	case FSTRANS_NORMAL:
    314 		KASSERT(fmi->fmi_state == FSTRANS_NORMAL ||
    315 			fstrans_is_owner(mp));
    316 		if (fmi->fmi_state == FSTRANS_SUSPENDED)
    317 			rw_exit(&fmi->fmi_lazy_lock);
    318 		if (fmi->fmi_state == FSTRANS_SUSPENDING ||
    319 		    fmi->fmi_state == FSTRANS_SUSPENDED) {
    320 			fmi->fmi_state = FSTRANS_NORMAL;
    321 			fstrans_done(mp);
    322 		}
    323 		break;
    324 
    325 	default:
    326 		panic("%s: illegal state %d", __func__, new_state);
    327 	}
    328 
    329 	return 0;
    330 }
    331 
    332 /*
    333  * Get current file system state
    334  */
    335 enum fstrans_state
    336 fstrans_getstate(struct mount *mp)
    337 {
    338 	struct fstrans_mount_info *fmi;
    339 
    340 	if ((fmi = mount_getspecific(mp, mount_data_key)) == NULL)
    341 		return FSTRANS_NORMAL;
    342 
    343 	return fmi->fmi_state;
    344 }
    345 
    346 /*
    347  * Request a filesystem to suspend all operations.
    348  */
    349 int
    350 vfs_suspend(struct mount *mp, int nowait)
    351 {
    352 	int error;
    353 
    354 	if (nowait) {
    355 		if (!mutex_tryenter(&vfs_suspend_lock))
    356 			return EWOULDBLOCK;
    357 	} else
    358 		mutex_enter(&vfs_suspend_lock);
    359 
    360 	mutex_enter(&syncer_mutex);
    361 
    362 	if ((error = VFS_SUSPENDCTL(mp, SUSPEND_SUSPEND)) != 0) {
    363 		mutex_exit(&syncer_mutex);
    364 		mutex_exit(&vfs_suspend_lock);
    365 	}
    366 
    367 	return error;
    368 }
    369 
    370 /*
    371  * Request a filesystem to resume all operations.
    372  */
    373 void
    374 vfs_resume(struct mount *mp)
    375 {
    376 
    377 	VFS_SUSPENDCTL(mp, SUSPEND_RESUME);
    378 	mutex_exit(&syncer_mutex);
    379 	mutex_exit(&vfs_suspend_lock);
    380 }
    381 
    382 /*
    383  * Default vfs_suspendctl routine for file systems that do not support it.
    384  */
    385 /*ARGSUSED*/
    386 int
    387 vfs_stdsuspendctl(struct mount *mp __unused, int mode __unused)
    388 {
    389 	return EOPNOTSUPP;
    390 }
    391 
    392 #if defined(DDB)
    393 void fstrans_dump(int);
    394 
    395 static void
    396 fstrans_print_lwp(struct proc *p, struct lwp *l, int verbose)
    397 {
    398 	char prefix[9];
    399 	struct fstrans_lwp_info *fli;
    400 
    401 	snprintf(prefix, sizeof(prefix), "%d.%d", p->p_pid, l->l_lid);
    402 	for (fli = _lwp_getspecific_by_lwp(l, lwp_data_key);
    403 	     fli;
    404 	     fli = fli->fli_succ) {
    405 		if (!verbose && fli->fli_count == 0)
    406 			continue;
    407 		printf("%-8s", prefix);
    408 		if (verbose)
    409 			printf(" @%p", fli);
    410 		if (fli->fli_mount != NULL)
    411 			printf(" (%s)", fli->fli_mount->mnt_stat.f_mntonname);
    412 		else
    413 			printf(" NULL");
    414 		switch (fli->fli_lock_type) {
    415 		case FSTRANS_LAZY:
    416 			printf(" lazy");
    417 			break;
    418 		case FSTRANS_SHARED:
    419 			printf(" shared");
    420 			break;
    421 		case FSTRANS_EXCL:
    422 			printf(" excl");
    423 			break;
    424 		default:
    425 			printf(" %#x", fli->fli_lock_type);
    426 			break;
    427 		}
    428 		printf(" %d\n", fli->fli_count);
    429 		prefix[0] = '\0';
    430 	}
    431 }
    432 
    433 static void
    434 fstrans_print_mount(struct mount *mp, int verbose)
    435 {
    436 	struct fstrans_mount_info *fmi;
    437 
    438 	fmi = mount_getspecific(mp, mount_data_key);
    439 	if (!verbose && (fmi == NULL || fmi->fmi_state == FSTRANS_NORMAL))
    440 		return;
    441 
    442 	printf("%-16s ", mp->mnt_stat.f_mntonname);
    443 	if (fmi == NULL) {
    444 		printf("(null)\n");
    445 		return;
    446 	}
    447 	switch (fmi->fmi_state) {
    448 	case FSTRANS_NORMAL:
    449 		printf("state normal\n");
    450 		break;
    451 	case FSTRANS_SUSPENDING:
    452 		printf("state suspending\n");
    453 		break;
    454 	case FSTRANS_SUSPENDED:
    455 		printf("state suspended\n");
    456 		break;
    457 	default:
    458 		printf("state %#x\n", fmi->fmi_state);
    459 		break;
    460 	}
    461 	printf("%16s r=%d w=%d\n", "lock_lazy:",
    462 	    rw_read_held(&fmi->fmi_lazy_lock),
    463 	    rw_write_held(&fmi->fmi_lazy_lock));
    464 	printf("%16s r=%d w=%d\n", "lock_shared:",
    465 	    rw_read_held(&fmi->fmi_shared_lock),
    466 	    rw_write_held(&fmi->fmi_shared_lock));
    467 }
    468 
    469 void
    470 fstrans_dump(int full)
    471 {
    472 	const struct proclist_desc *pd;
    473 	struct proc *p;
    474 	struct lwp *l;
    475 	struct mount *mp;
    476 
    477 	printf("Fstrans locks by lwp:\n");
    478 	for (pd = proclists; pd->pd_list != NULL; pd++)
    479 		LIST_FOREACH(p, pd->pd_list, p_list)
    480 			LIST_FOREACH(l, &p->p_lwps, l_sibling)
    481 				fstrans_print_lwp(p, l, full == 1);
    482 
    483 	printf("Fstrans state by mount:\n");
    484 	CIRCLEQ_FOREACH(mp, &mountlist, mnt_list)
    485 		fstrans_print_mount(mp, full == 1);
    486 }
    487 #endif /* defined(DDB) */
    488