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