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