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