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