Home | History | Annotate | Line # | Download | only in lfs
lfs_subr.c revision 1.8
      1  1.8  perseant /*	$NetBSD: lfs_subr.c,v 1.8 1999/03/10 00:20:00 perseant Exp $	*/
      2  1.2       cgd 
      3  1.8  perseant /*-
      4  1.8  perseant  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5  1.8  perseant  * All rights reserved.
      6  1.8  perseant  *
      7  1.8  perseant  * This code is derived from software contributed to The NetBSD Foundation
      8  1.8  perseant  * by Konrad E. Schroder <perseant (at) hhhh.org>.
      9  1.8  perseant  *
     10  1.8  perseant  * Redistribution and use in source and binary forms, with or without
     11  1.8  perseant  * modification, are permitted provided that the following conditions
     12  1.8  perseant  * are met:
     13  1.8  perseant  * 1. Redistributions of source code must retain the above copyright
     14  1.8  perseant  *    notice, this list of conditions and the following disclaimer.
     15  1.8  perseant  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.8  perseant  *    notice, this list of conditions and the following disclaimer in the
     17  1.8  perseant  *    documentation and/or other materials provided with the distribution.
     18  1.8  perseant  * 3. All advertising materials mentioning features or use of this software
     19  1.8  perseant  *    must display the following acknowledgement:
     20  1.8  perseant  *      This product includes software developed by the NetBSD
     21  1.8  perseant  *      Foundation, Inc. and its contributors.
     22  1.8  perseant  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.8  perseant  *    contributors may be used to endorse or promote products derived
     24  1.8  perseant  *    from this software without specific prior written permission.
     25  1.8  perseant  *
     26  1.8  perseant  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.8  perseant  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.8  perseant  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.8  perseant  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.8  perseant  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.8  perseant  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.8  perseant  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.8  perseant  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.8  perseant  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.8  perseant  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.8  perseant  * POSSIBILITY OF SUCH DAMAGE.
     37  1.8  perseant  */
     38  1.1   mycroft /*
     39  1.1   mycroft  * Copyright (c) 1991, 1993
     40  1.1   mycroft  *	The Regents of the University of California.  All rights reserved.
     41  1.1   mycroft  *
     42  1.1   mycroft  * Redistribution and use in source and binary forms, with or without
     43  1.1   mycroft  * modification, are permitted provided that the following conditions
     44  1.1   mycroft  * are met:
     45  1.1   mycroft  * 1. Redistributions of source code must retain the above copyright
     46  1.1   mycroft  *    notice, this list of conditions and the following disclaimer.
     47  1.1   mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     48  1.1   mycroft  *    notice, this list of conditions and the following disclaimer in the
     49  1.1   mycroft  *    documentation and/or other materials provided with the distribution.
     50  1.1   mycroft  * 3. All advertising materials mentioning features or use of this software
     51  1.1   mycroft  *    must display the following acknowledgement:
     52  1.1   mycroft  *	This product includes software developed by the University of
     53  1.1   mycroft  *	California, Berkeley and its contributors.
     54  1.1   mycroft  * 4. Neither the name of the University nor the names of its contributors
     55  1.1   mycroft  *    may be used to endorse or promote products derived from this software
     56  1.1   mycroft  *    without specific prior written permission.
     57  1.1   mycroft  *
     58  1.1   mycroft  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     59  1.1   mycroft  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     60  1.1   mycroft  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     61  1.1   mycroft  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     62  1.1   mycroft  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     63  1.1   mycroft  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     64  1.1   mycroft  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     65  1.1   mycroft  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     66  1.1   mycroft  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     67  1.1   mycroft  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     68  1.1   mycroft  * SUCH DAMAGE.
     69  1.1   mycroft  *
     70  1.6      fvdl  *	@(#)lfs_subr.c	8.4 (Berkeley) 5/8/95
     71  1.1   mycroft  */
     72  1.1   mycroft 
     73  1.1   mycroft #include <sys/param.h>
     74  1.3  christos #include <sys/systm.h>
     75  1.1   mycroft #include <sys/namei.h>
     76  1.1   mycroft #include <sys/vnode.h>
     77  1.1   mycroft #include <sys/buf.h>
     78  1.1   mycroft #include <sys/mount.h>
     79  1.1   mycroft #include <sys/malloc.h>
     80  1.1   mycroft #include <sys/proc.h>
     81  1.1   mycroft 
     82  1.1   mycroft #include <ufs/ufs/quota.h>
     83  1.1   mycroft #include <ufs/ufs/inode.h>
     84  1.1   mycroft #include <ufs/lfs/lfs.h>
     85  1.1   mycroft #include <ufs/lfs/lfs_extern.h>
     86  1.1   mycroft 
     87  1.1   mycroft /*
     88  1.1   mycroft  * Return buffer with the contents of block "offset" from the beginning of
     89  1.1   mycroft  * directory "ip".  If "res" is non-zero, fill it in with a pointer to the
     90  1.1   mycroft  * remaining space in the directory.
     91  1.1   mycroft  */
     92  1.1   mycroft int
     93  1.3  christos lfs_blkatoff(v)
     94  1.3  christos 	void *v;
     95  1.3  christos {
     96  1.1   mycroft 	struct vop_blkatoff_args /* {
     97  1.1   mycroft 		struct vnode *a_vp;
     98  1.1   mycroft 		off_t a_offset;
     99  1.1   mycroft 		char **a_res;
    100  1.1   mycroft 		struct buf **a_bpp;
    101  1.8  perseant 		} */ *ap = v;
    102  1.1   mycroft 	register struct lfs *fs;
    103  1.1   mycroft 	struct inode *ip;
    104  1.1   mycroft 	struct buf *bp;
    105  1.6      fvdl 	ufs_daddr_t lbn;
    106  1.1   mycroft 	int bsize, error;
    107  1.8  perseant 
    108  1.1   mycroft 	ip = VTOI(ap->a_vp);
    109  1.1   mycroft 	fs = ip->i_lfs;
    110  1.1   mycroft 	lbn = lblkno(fs, ap->a_offset);
    111  1.6      fvdl 	bsize = blksize(fs, ip, lbn);
    112  1.8  perseant 
    113  1.1   mycroft 	*ap->a_bpp = NULL;
    114  1.3  christos 	if ((error = bread(ap->a_vp, lbn, bsize, NOCRED, &bp)) != 0) {
    115  1.1   mycroft 		brelse(bp);
    116  1.1   mycroft 		return (error);
    117  1.1   mycroft 	}
    118  1.1   mycroft 	if (ap->a_res)
    119  1.1   mycroft 		*ap->a_res = (char *)bp->b_data + blkoff(fs, ap->a_offset);
    120  1.1   mycroft 	*ap->a_bpp = bp;
    121  1.1   mycroft 	return (0);
    122  1.1   mycroft }
    123  1.1   mycroft 
    124  1.1   mycroft 
    125  1.1   mycroft /*
    126  1.1   mycroft  * lfs_seglock --
    127  1.1   mycroft  *	Single thread the segment writer.
    128  1.1   mycroft  */
    129  1.1   mycroft void
    130  1.1   mycroft lfs_seglock(fs, flags)
    131  1.1   mycroft 	struct lfs *fs;
    132  1.1   mycroft 	unsigned long flags;
    133  1.1   mycroft {
    134  1.1   mycroft 	struct segment *sp;
    135  1.1   mycroft 	int s;
    136  1.8  perseant 
    137  1.7   thorpej 	if (fs->lfs_seglock) {
    138  1.1   mycroft 		if (fs->lfs_lockpid == curproc->p_pid) {
    139  1.1   mycroft 			++fs->lfs_seglock;
    140  1.1   mycroft 			fs->lfs_sp->seg_flags |= flags;
    141  1.1   mycroft 			return;
    142  1.1   mycroft 		} else while (fs->lfs_seglock)
    143  1.1   mycroft 			(void)tsleep(&fs->lfs_seglock, PRIBIO + 1,
    144  1.8  perseant 				     "lfs seglock", 0);
    145  1.7   thorpej 	}
    146  1.8  perseant 
    147  1.1   mycroft 	fs->lfs_seglock = 1;
    148  1.1   mycroft 	fs->lfs_lockpid = curproc->p_pid;
    149  1.8  perseant 
    150  1.1   mycroft 	sp = fs->lfs_sp = malloc(sizeof(struct segment), M_SEGMENT, M_WAITOK);
    151  1.1   mycroft 	sp->bpp = malloc(((LFS_SUMMARY_SIZE - sizeof(SEGSUM)) /
    152  1.8  perseant 			  sizeof(ufs_daddr_t) + 1) * sizeof(struct buf *),
    153  1.8  perseant 			 M_SEGMENT, M_WAITOK);
    154  1.1   mycroft 	sp->seg_flags = flags;
    155  1.1   mycroft 	sp->vp = NULL;
    156  1.1   mycroft 	(void) lfs_initseg(fs);
    157  1.8  perseant 
    158  1.1   mycroft 	/*
    159  1.1   mycroft 	 * Keep a cumulative count of the outstanding I/O operations.  If the
    160  1.1   mycroft 	 * disk drive catches up with us it could go to zero before we finish,
    161  1.1   mycroft 	 * so we artificially increment it by one until we've scheduled all of
    162  1.1   mycroft 	 * the writes we intend to do.
    163  1.1   mycroft 	 */
    164  1.1   mycroft 	s = splbio();
    165  1.1   mycroft 	++fs->lfs_iocount;
    166  1.1   mycroft 	splx(s);
    167  1.1   mycroft }
    168  1.8  perseant 
    169  1.1   mycroft /*
    170  1.1   mycroft  * lfs_segunlock --
    171  1.1   mycroft  *	Single thread the segment writer.
    172  1.1   mycroft  */
    173  1.1   mycroft void
    174  1.1   mycroft lfs_segunlock(fs)
    175  1.1   mycroft 	struct lfs *fs;
    176  1.1   mycroft {
    177  1.1   mycroft 	struct segment *sp;
    178  1.1   mycroft 	unsigned long sync, ckp;
    179  1.1   mycroft 	int s;
    180  1.8  perseant 
    181  1.1   mycroft 	if (fs->lfs_seglock == 1) {
    182  1.1   mycroft 
    183  1.1   mycroft 		sp = fs->lfs_sp;
    184  1.1   mycroft 		sync = sp->seg_flags & SEGM_SYNC;
    185  1.1   mycroft 		ckp = sp->seg_flags & SEGM_CKP;
    186  1.1   mycroft 		if (sp->bpp != sp->cbpp) {
    187  1.1   mycroft 			/* Free allocated segment summary */
    188  1.1   mycroft 			fs->lfs_offset -= LFS_SUMMARY_SIZE / DEV_BSIZE;
    189  1.8  perseant                         lfs_freebuf(*sp->bpp);
    190  1.1   mycroft 		} else
    191  1.5  christos 			printf ("unlock to 0 with no summary");
    192  1.8  perseant 
    193  1.1   mycroft 		free(sp->bpp, M_SEGMENT);
    194  1.1   mycroft 		free(sp, M_SEGMENT);
    195  1.1   mycroft 
    196  1.1   mycroft 		/*
    197  1.1   mycroft 		 * If the I/O count is non-zero, sleep until it reaches zero.
    198  1.1   mycroft 		 * At the moment, the user's process hangs around so we can
    199  1.1   mycroft 		 * sleep.
    200  1.1   mycroft 		 */
    201  1.1   mycroft 		s = splbio();
    202  1.1   mycroft 		--fs->lfs_iocount;
    203  1.1   mycroft 		/*
    204  1.1   mycroft 		 * We let checkpoints happen asynchronously.  That means
    205  1.1   mycroft 		 * that during recovery, we have to roll forward between
    206  1.1   mycroft 		 * the two segments described by the first and second
    207  1.1   mycroft 		 * superblocks to make sure that the checkpoint described
    208  1.1   mycroft 		 * by a superblock completed.
    209  1.1   mycroft 		 */
    210  1.8  perseant 		while (sync && fs->lfs_iocount)
    211  1.8  perseant 			(void)tsleep(&fs->lfs_iocount, PRIBIO + 1,
    212  1.8  perseant 				     "lfs vflush", 0);
    213  1.1   mycroft 		splx(s);
    214  1.1   mycroft 		if (ckp) {
    215  1.1   mycroft 			fs->lfs_nactive = 0;
    216  1.8  perseant #ifndef LFS_TOGGLE_SB
    217  1.8  perseant 			lfs_writesuper(fs,fs->lfs_sboffs[0]);
    218  1.8  perseant #else
    219  1.8  perseant 			/* If we *know* everything's on disk, write both sbs */
    220  1.8  perseant 			if(sync)
    221  1.8  perseant 				lfs_writesuper(fs,fs->lfs_sboffs[fs->lfs_activesb]);
    222  1.8  perseant 			fs->lfs_activesb = 1 - fs->lfs_activesb;
    223  1.8  perseant 			lfs_writesuper(fs,fs->lfs_sboffs[fs->lfs_activesb]);
    224  1.8  perseant #endif
    225  1.1   mycroft 		}
    226  1.1   mycroft 		--fs->lfs_seglock;
    227  1.1   mycroft 		fs->lfs_lockpid = 0;
    228  1.1   mycroft 		wakeup(&fs->lfs_seglock);
    229  1.1   mycroft 	} else if (fs->lfs_seglock == 0) {
    230  1.1   mycroft 		panic ("Seglock not held");
    231  1.1   mycroft 	} else {
    232  1.1   mycroft 		--fs->lfs_seglock;
    233  1.1   mycroft 	}
    234  1.1   mycroft }
    235