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