lfs_subr.c revision 1.26 1 1.26 perseant /* $NetBSD: lfs_subr.c,v 1.26 2002/06/16 00:13:15 perseant Exp $ */
2 1.2 cgd
3 1.8 perseant /*-
4 1.17 perseant * Copyright (c) 1999, 2000 The NetBSD Foundation, Inc.
5 1.8 perseant * All rights reserved.
6 1.8 perseant *
7 1.8 perseant * This code is derived from software contributed to The NetBSD Foundation
8 1.8 perseant * by Konrad E. Schroder <perseant (at) hhhh.org>.
9 1.8 perseant *
10 1.8 perseant * Redistribution and use in source and binary forms, with or without
11 1.8 perseant * modification, are permitted provided that the following conditions
12 1.8 perseant * are met:
13 1.8 perseant * 1. Redistributions of source code must retain the above copyright
14 1.8 perseant * notice, this list of conditions and the following disclaimer.
15 1.8 perseant * 2. Redistributions in binary form must reproduce the above copyright
16 1.8 perseant * notice, this list of conditions and the following disclaimer in the
17 1.8 perseant * documentation and/or other materials provided with the distribution.
18 1.8 perseant * 3. All advertising materials mentioning features or use of this software
19 1.8 perseant * must display the following acknowledgement:
20 1.8 perseant * This product includes software developed by the NetBSD
21 1.8 perseant * Foundation, Inc. and its contributors.
22 1.8 perseant * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.8 perseant * contributors may be used to endorse or promote products derived
24 1.8 perseant * from this software without specific prior written permission.
25 1.8 perseant *
26 1.8 perseant * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.8 perseant * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.8 perseant * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.8 perseant * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.8 perseant * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.8 perseant * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.8 perseant * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.8 perseant * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.8 perseant * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.8 perseant * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.8 perseant * POSSIBILITY OF SUCH DAMAGE.
37 1.8 perseant */
38 1.1 mycroft /*
39 1.1 mycroft * Copyright (c) 1991, 1993
40 1.1 mycroft * The Regents of the University of California. All rights reserved.
41 1.1 mycroft *
42 1.1 mycroft * Redistribution and use in source and binary forms, with or without
43 1.1 mycroft * modification, are permitted provided that the following conditions
44 1.1 mycroft * are met:
45 1.1 mycroft * 1. Redistributions of source code must retain the above copyright
46 1.1 mycroft * notice, this list of conditions and the following disclaimer.
47 1.1 mycroft * 2. Redistributions in binary form must reproduce the above copyright
48 1.1 mycroft * notice, this list of conditions and the following disclaimer in the
49 1.1 mycroft * documentation and/or other materials provided with the distribution.
50 1.1 mycroft * 3. All advertising materials mentioning features or use of this software
51 1.1 mycroft * must display the following acknowledgement:
52 1.1 mycroft * This product includes software developed by the University of
53 1.1 mycroft * California, Berkeley and its contributors.
54 1.1 mycroft * 4. Neither the name of the University nor the names of its contributors
55 1.1 mycroft * may be used to endorse or promote products derived from this software
56 1.1 mycroft * without specific prior written permission.
57 1.1 mycroft *
58 1.1 mycroft * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 1.1 mycroft * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 1.1 mycroft * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 1.1 mycroft * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 1.1 mycroft * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 1.1 mycroft * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 1.1 mycroft * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 1.1 mycroft * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 1.1 mycroft * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 1.1 mycroft * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 1.1 mycroft * SUCH DAMAGE.
69 1.1 mycroft *
70 1.6 fvdl * @(#)lfs_subr.c 8.4 (Berkeley) 5/8/95
71 1.1 mycroft */
72 1.20 lukem
73 1.20 lukem #include <sys/cdefs.h>
74 1.26 perseant __KERNEL_RCSID(0, "$NetBSD: lfs_subr.c,v 1.26 2002/06/16 00:13:15 perseant Exp $");
75 1.1 mycroft
76 1.1 mycroft #include <sys/param.h>
77 1.3 christos #include <sys/systm.h>
78 1.1 mycroft #include <sys/namei.h>
79 1.1 mycroft #include <sys/vnode.h>
80 1.1 mycroft #include <sys/buf.h>
81 1.1 mycroft #include <sys/mount.h>
82 1.1 mycroft #include <sys/malloc.h>
83 1.1 mycroft #include <sys/proc.h>
84 1.1 mycroft
85 1.1 mycroft #include <ufs/ufs/inode.h>
86 1.1 mycroft #include <ufs/lfs/lfs.h>
87 1.1 mycroft #include <ufs/lfs/lfs_extern.h>
88 1.1 mycroft
89 1.1 mycroft /*
90 1.1 mycroft * Return buffer with the contents of block "offset" from the beginning of
91 1.1 mycroft * directory "ip". If "res" is non-zero, fill it in with a pointer to the
92 1.1 mycroft * remaining space in the directory.
93 1.1 mycroft */
94 1.1 mycroft int
95 1.18 perseant lfs_blkatoff(void *v)
96 1.3 christos {
97 1.1 mycroft struct vop_blkatoff_args /* {
98 1.1 mycroft struct vnode *a_vp;
99 1.1 mycroft off_t a_offset;
100 1.1 mycroft char **a_res;
101 1.1 mycroft struct buf **a_bpp;
102 1.8 perseant } */ *ap = v;
103 1.13 augustss struct lfs *fs;
104 1.1 mycroft struct inode *ip;
105 1.1 mycroft struct buf *bp;
106 1.6 fvdl ufs_daddr_t lbn;
107 1.1 mycroft int bsize, error;
108 1.8 perseant
109 1.1 mycroft ip = VTOI(ap->a_vp);
110 1.1 mycroft fs = ip->i_lfs;
111 1.1 mycroft lbn = lblkno(fs, ap->a_offset);
112 1.6 fvdl bsize = blksize(fs, ip, lbn);
113 1.8 perseant
114 1.1 mycroft *ap->a_bpp = NULL;
115 1.3 christos if ((error = bread(ap->a_vp, lbn, bsize, NOCRED, &bp)) != 0) {
116 1.1 mycroft brelse(bp);
117 1.1 mycroft return (error);
118 1.1 mycroft }
119 1.1 mycroft if (ap->a_res)
120 1.1 mycroft *ap->a_res = (char *)bp->b_data + blkoff(fs, ap->a_offset);
121 1.1 mycroft *ap->a_bpp = bp;
122 1.1 mycroft return (0);
123 1.1 mycroft }
124 1.1 mycroft
125 1.1 mycroft
126 1.1 mycroft /*
127 1.1 mycroft * lfs_seglock --
128 1.1 mycroft * Single thread the segment writer.
129 1.1 mycroft */
130 1.1 mycroft void
131 1.18 perseant lfs_seglock(struct lfs *fs, unsigned long flags)
132 1.1 mycroft {
133 1.1 mycroft struct segment *sp;
134 1.8 perseant
135 1.7 thorpej if (fs->lfs_seglock) {
136 1.1 mycroft if (fs->lfs_lockpid == curproc->p_pid) {
137 1.1 mycroft ++fs->lfs_seglock;
138 1.1 mycroft fs->lfs_sp->seg_flags |= flags;
139 1.1 mycroft return;
140 1.1 mycroft } else while (fs->lfs_seglock)
141 1.1 mycroft (void)tsleep(&fs->lfs_seglock, PRIBIO + 1,
142 1.8 perseant "lfs seglock", 0);
143 1.7 thorpej }
144 1.8 perseant
145 1.1 mycroft fs->lfs_seglock = 1;
146 1.1 mycroft fs->lfs_lockpid = curproc->p_pid;
147 1.8 perseant
148 1.1 mycroft sp = fs->lfs_sp = malloc(sizeof(struct segment), M_SEGMENT, M_WAITOK);
149 1.18 perseant sp->bpp = malloc(((fs->lfs_sumsize - SEGSUM_SIZE(fs)) /
150 1.8 perseant sizeof(ufs_daddr_t) + 1) * sizeof(struct buf *),
151 1.8 perseant M_SEGMENT, M_WAITOK);
152 1.1 mycroft sp->seg_flags = flags;
153 1.1 mycroft sp->vp = NULL;
154 1.26 perseant sp->seg_iocount = 0;
155 1.1 mycroft (void) lfs_initseg(fs);
156 1.8 perseant
157 1.1 mycroft /*
158 1.1 mycroft * Keep a cumulative count of the outstanding I/O operations. If the
159 1.1 mycroft * disk drive catches up with us it could go to zero before we finish,
160 1.1 mycroft * so we artificially increment it by one until we've scheduled all of
161 1.1 mycroft * the writes we intend to do.
162 1.1 mycroft */
163 1.1 mycroft ++fs->lfs_iocount;
164 1.1 mycroft }
165 1.8 perseant
166 1.1 mycroft /*
167 1.1 mycroft * lfs_segunlock --
168 1.1 mycroft * Single thread the segment writer.
169 1.1 mycroft */
170 1.1 mycroft void
171 1.18 perseant lfs_segunlock(struct lfs *fs)
172 1.1 mycroft {
173 1.1 mycroft struct segment *sp;
174 1.1 mycroft unsigned long sync, ckp;
175 1.22 perseant struct buf *bp;
176 1.24 perseant struct vnode *vp, *nvp;
177 1.10 perseant struct mount *mp;
178 1.10 perseant extern int lfs_dirvcount;
179 1.22 perseant #ifdef LFS_MALLOC_SUMMARY
180 1.22 perseant extern int locked_queue_count;
181 1.22 perseant extern long locked_queue_bytes;
182 1.22 perseant #endif
183 1.8 perseant
184 1.15 perseant sp = fs->lfs_sp;
185 1.15 perseant
186 1.15 perseant if (fs->lfs_seglock == 1 && !(sp->seg_flags & SEGM_PROT)) {
187 1.10 perseant
188 1.10 perseant mp = fs->lfs_ivnode->v_mount;
189 1.10 perseant /*
190 1.10 perseant * Go through and unmark all DIROP vnodes, possibly
191 1.10 perseant * calling VOP_INACTIVE (through vrele). This is
192 1.10 perseant * delayed until now in order not to accidentally
193 1.10 perseant * write a DIROP node through lfs_flush.
194 1.10 perseant */
195 1.10 perseant #ifndef LFS_NO_BACKVP_HACK
196 1.10 perseant /* BEGIN HACK */
197 1.23 perseant #define VN_OFFSET (((caddr_t)&LIST_NEXT(vp, v_mntvnodes)) - (caddr_t)vp)
198 1.23 perseant #define BACK_VP(VP) ((struct vnode *)(((caddr_t)(VP)->v_mntvnodes.le_prev) - VN_OFFSET))
199 1.23 perseant #define BEG_OF_VLIST ((struct vnode *)(((caddr_t)&LIST_FIRST(&mp->mnt_vnodelist)) - VN_OFFSET))
200 1.10 perseant
201 1.10 perseant /* Find last vnode. */
202 1.23 perseant loop: for (vp = LIST_FIRST(&mp->mnt_vnodelist);
203 1.23 perseant vp && LIST_NEXT(vp, v_mntvnodes) != NULL;
204 1.23 perseant vp = LIST_NEXT(vp, v_mntvnodes));
205 1.24 perseant for (; vp && vp != BEG_OF_VLIST; vp = nvp) {
206 1.24 perseant nvp = BACK_VP(vp);
207 1.10 perseant #else
208 1.10 perseant loop:
209 1.23 perseant for (vp = LIST_FIRST(&mp->mnt_vnodelist);
210 1.10 perseant vp != NULL;
211 1.24 perseant vp = nvp) {
212 1.24 perseant nvp = LIST_NEXT(vp, v_mntvnodes);
213 1.10 perseant #endif
214 1.22 perseant if (vp->v_mount != mp) {
215 1.22 perseant printf("lfs_segunlock: starting over\n");
216 1.10 perseant goto loop;
217 1.22 perseant }
218 1.10 perseant if (vp->v_type == VNON)
219 1.10 perseant continue;
220 1.16 perseant if (lfs_vref(vp))
221 1.16 perseant continue;
222 1.16 perseant if (VOP_ISLOCKED(vp) &&
223 1.16 perseant vp->v_lock.lk_lockholder != curproc->p_pid) {
224 1.16 perseant lfs_vunref(vp);
225 1.16 perseant continue;
226 1.16 perseant }
227 1.15 perseant if ((vp->v_flag & VDIROP) &&
228 1.15 perseant !(VTOI(vp)->i_flag & IN_ADIROP)) {
229 1.10 perseant --lfs_dirvcount;
230 1.10 perseant vp->v_flag &= ~VDIROP;
231 1.10 perseant wakeup(&lfs_dirvcount);
232 1.15 perseant fs->lfs_unlockvp = vp;
233 1.16 perseant lfs_vunref(vp);
234 1.15 perseant vrele(vp);
235 1.15 perseant fs->lfs_unlockvp = NULL;
236 1.16 perseant } else {
237 1.16 perseant lfs_vunref(vp);
238 1.10 perseant }
239 1.10 perseant }
240 1.15 perseant }
241 1.1 mycroft
242 1.15 perseant if (fs->lfs_seglock == 1) {
243 1.1 mycroft sync = sp->seg_flags & SEGM_SYNC;
244 1.1 mycroft ckp = sp->seg_flags & SEGM_CKP;
245 1.1 mycroft if (sp->bpp != sp->cbpp) {
246 1.1 mycroft /* Free allocated segment summary */
247 1.18 perseant fs->lfs_offset -= btofsb(fs, fs->lfs_sumsize);
248 1.22 perseant bp = *sp->bpp;
249 1.22 perseant #ifdef LFS_MALLOC_SUMMARY
250 1.22 perseant lfs_freebuf(bp);
251 1.22 perseant #else
252 1.22 perseant s = splbio();
253 1.22 perseant bremfree(bp);
254 1.22 perseant bp->b_flags |= B_DONE|B_INVAL;
255 1.22 perseant bp->b_flags &= ~B_DELWRI;
256 1.22 perseant reassignbuf(bp,bp->b_vp);
257 1.25 perseant splx(s);
258 1.22 perseant brelse(bp);
259 1.22 perseant #endif
260 1.1 mycroft } else
261 1.5 christos printf ("unlock to 0 with no summary");
262 1.8 perseant
263 1.1 mycroft free(sp->bpp, M_SEGMENT);
264 1.18 perseant sp->bpp = NULL;
265 1.26 perseant /* The sync case holds a reference in `sp' to be freed below */
266 1.26 perseant if (!sync)
267 1.26 perseant free(sp, M_SEGMENT);
268 1.18 perseant fs->lfs_sp = NULL;
269 1.1 mycroft
270 1.1 mycroft /*
271 1.1 mycroft * If the I/O count is non-zero, sleep until it reaches zero.
272 1.1 mycroft * At the moment, the user's process hangs around so we can
273 1.1 mycroft * sleep.
274 1.1 mycroft */
275 1.22 perseant if (--fs->lfs_iocount < LFS_THROTTLE)
276 1.22 perseant wakeup(&fs->lfs_iocount);
277 1.22 perseant if(fs->lfs_iocount == 0) {
278 1.22 perseant lfs_countlocked(&locked_queue_count,
279 1.22 perseant &locked_queue_bytes, "lfs_segunlock");
280 1.22 perseant wakeup(&locked_queue_count);
281 1.22 perseant wakeup(&fs->lfs_iocount);
282 1.22 perseant }
283 1.1 mycroft /*
284 1.26 perseant * If we're not checkpointing, we don't have to block
285 1.26 perseant * other processes to wait for a synchronous write
286 1.26 perseant * to complete.
287 1.26 perseant */
288 1.26 perseant if (!ckp) {
289 1.26 perseant --fs->lfs_seglock;
290 1.26 perseant fs->lfs_lockpid = 0;
291 1.26 perseant wakeup(&fs->lfs_seglock);
292 1.26 perseant }
293 1.26 perseant /*
294 1.1 mycroft * We let checkpoints happen asynchronously. That means
295 1.1 mycroft * that during recovery, we have to roll forward between
296 1.1 mycroft * the two segments described by the first and second
297 1.1 mycroft * superblocks to make sure that the checkpoint described
298 1.1 mycroft * by a superblock completed.
299 1.1 mycroft */
300 1.26 perseant while (ckp && sync && fs->lfs_iocount)
301 1.8 perseant (void)tsleep(&fs->lfs_iocount, PRIBIO + 1,
302 1.26 perseant "lfs_iocount", 0);
303 1.26 perseant while (sync && sp->seg_iocount) {
304 1.26 perseant (void)tsleep(&sp->seg_iocount, PRIBIO + 1,
305 1.26 perseant "seg_iocount", 0);
306 1.26 perseant /* printf("sleeping on iocount %x == %d\n", sp, sp->seg_iocount); */
307 1.26 perseant }
308 1.26 perseant if (sync)
309 1.26 perseant free(sp, M_SEGMENT);
310 1.1 mycroft if (ckp) {
311 1.1 mycroft fs->lfs_nactive = 0;
312 1.8 perseant /* If we *know* everything's on disk, write both sbs */
313 1.21 chs if (sync)
314 1.8 perseant lfs_writesuper(fs,fs->lfs_sboffs[fs->lfs_activesb]);
315 1.8 perseant fs->lfs_activesb = 1 - fs->lfs_activesb;
316 1.8 perseant lfs_writesuper(fs,fs->lfs_sboffs[fs->lfs_activesb]);
317 1.26 perseant
318 1.26 perseant --fs->lfs_seglock;
319 1.26 perseant fs->lfs_lockpid = 0;
320 1.26 perseant wakeup(&fs->lfs_seglock);
321 1.1 mycroft }
322 1.1 mycroft } else if (fs->lfs_seglock == 0) {
323 1.1 mycroft panic ("Seglock not held");
324 1.1 mycroft } else {
325 1.1 mycroft --fs->lfs_seglock;
326 1.1 mycroft }
327 1.1 mycroft }
328