Home | History | Annotate | Line # | Download | only in lfs
lfs_subr.c revision 1.1.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  *	@(#)lfs_subr.c	8.2 (Berkeley) 9/21/93
     34  */
     35 
     36 #include <sys/param.h>
     37 #include <sys/namei.h>
     38 #include <sys/vnode.h>
     39 #include <sys/buf.h>
     40 #include <sys/mount.h>
     41 #include <sys/malloc.h>
     42 #include <sys/proc.h>
     43 
     44 #include <ufs/ufs/quota.h>
     45 #include <ufs/ufs/inode.h>
     46 #include <ufs/lfs/lfs.h>
     47 #include <ufs/lfs/lfs_extern.h>
     48 
     49 /*
     50  * Return buffer with the contents of block "offset" from the beginning of
     51  * directory "ip".  If "res" is non-zero, fill it in with a pointer to the
     52  * remaining space in the directory.
     53  */
     54 int
     55 lfs_blkatoff(ap)
     56 	struct vop_blkatoff_args /* {
     57 		struct vnode *a_vp;
     58 		off_t a_offset;
     59 		char **a_res;
     60 		struct buf **a_bpp;
     61 	} */ *ap;
     62 {
     63 	register struct lfs *fs;
     64 	struct inode *ip;
     65 	struct buf *bp;
     66 	daddr_t lbn;
     67 	int bsize, error;
     68 
     69 	ip = VTOI(ap->a_vp);
     70 	fs = ip->i_lfs;
     71 	lbn = lblkno(fs, ap->a_offset);
     72 	bsize = blksize(fs);
     73 
     74 	*ap->a_bpp = NULL;
     75 	if (error = bread(ap->a_vp, lbn, bsize, NOCRED, &bp)) {
     76 		brelse(bp);
     77 		return (error);
     78 	}
     79 	if (ap->a_res)
     80 		*ap->a_res = (char *)bp->b_data + blkoff(fs, ap->a_offset);
     81 	*ap->a_bpp = bp;
     82 	return (0);
     83 }
     84 
     85 
     86 /*
     87  * lfs_seglock --
     88  *	Single thread the segment writer.
     89  */
     90 void
     91 lfs_seglock(fs, flags)
     92 	struct lfs *fs;
     93 	unsigned long flags;
     94 {
     95 	struct segment *sp;
     96 	int s;
     97 
     98 	if (fs->lfs_seglock)
     99 		if (fs->lfs_lockpid == curproc->p_pid) {
    100 			++fs->lfs_seglock;
    101 			fs->lfs_sp->seg_flags |= flags;
    102 			return;
    103 		} else while (fs->lfs_seglock)
    104 			(void)tsleep(&fs->lfs_seglock, PRIBIO + 1,
    105 			    "lfs seglock", 0);
    106 
    107 	fs->lfs_seglock = 1;
    108 	fs->lfs_lockpid = curproc->p_pid;
    109 
    110 	sp = fs->lfs_sp = malloc(sizeof(struct segment), M_SEGMENT, M_WAITOK);
    111 	sp->bpp = malloc(((LFS_SUMMARY_SIZE - sizeof(SEGSUM)) /
    112 	    sizeof(daddr_t) + 1) * sizeof(struct buf *), M_SEGMENT, M_WAITOK);
    113 	sp->seg_flags = flags;
    114 	sp->vp = NULL;
    115 	(void) lfs_initseg(fs);
    116 
    117 	/*
    118 	 * Keep a cumulative count of the outstanding I/O operations.  If the
    119 	 * disk drive catches up with us it could go to zero before we finish,
    120 	 * so we artificially increment it by one until we've scheduled all of
    121 	 * the writes we intend to do.
    122 	 */
    123 	s = splbio();
    124 	++fs->lfs_iocount;
    125 	splx(s);
    126 }
    127 /*
    128  * lfs_segunlock --
    129  *	Single thread the segment writer.
    130  */
    131 void
    132 lfs_segunlock(fs)
    133 	struct lfs *fs;
    134 {
    135 	struct segment *sp;
    136 	unsigned long sync, ckp;
    137 	int s;
    138 
    139 	if (fs->lfs_seglock == 1) {
    140 
    141 		sp = fs->lfs_sp;
    142 		sync = sp->seg_flags & SEGM_SYNC;
    143 		ckp = sp->seg_flags & SEGM_CKP;
    144 		if (sp->bpp != sp->cbpp) {
    145 			/* Free allocated segment summary */
    146 			fs->lfs_offset -= LFS_SUMMARY_SIZE / DEV_BSIZE;
    147 			brelvp(*sp->bpp);
    148 			free((*sp->bpp)->b_data, M_SEGMENT);
    149 			free(*sp->bpp, M_SEGMENT);
    150 		} else
    151 			printf ("unlock to 0 with no summary");
    152 		free(sp->bpp, M_SEGMENT);
    153 		free(sp, M_SEGMENT);
    154 
    155 		/*
    156 		 * If the I/O count is non-zero, sleep until it reaches zero.
    157 		 * At the moment, the user's process hangs around so we can
    158 		 * sleep.
    159 		 */
    160 		s = splbio();
    161 		--fs->lfs_iocount;
    162 		/*
    163 		 * We let checkpoints happen asynchronously.  That means
    164 		 * that during recovery, we have to roll forward between
    165 		 * the two segments described by the first and second
    166 		 * superblocks to make sure that the checkpoint described
    167 		 * by a superblock completed.
    168 		 */
    169 		if (sync && fs->lfs_iocount)
    170 		    (void)tsleep(&fs->lfs_iocount, PRIBIO + 1, "lfs vflush", 0);
    171 		splx(s);
    172 		if (ckp) {
    173 			fs->lfs_nactive = 0;
    174 			lfs_writesuper(fs);
    175 		}
    176 		--fs->lfs_seglock;
    177 		fs->lfs_lockpid = 0;
    178 		wakeup(&fs->lfs_seglock);
    179 	} else if (fs->lfs_seglock == 0) {
    180 		panic ("Seglock not held");
    181 	} else {
    182 		--fs->lfs_seglock;
    183 	}
    184 }
    185