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