lfs_subr.c revision 1.31 1 /* $NetBSD: lfs_subr.c,v 1.31 2003/02/17 23:48:20 perseant Exp $ */
2
3 /*-
4 * Copyright (c) 1999, 2000, 2001, 2002, 2003 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 (at) 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/cdefs.h>
74 __KERNEL_RCSID(0, "$NetBSD: lfs_subr.c,v 1.31 2003/02/17 23:48:20 perseant Exp $");
75
76 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/namei.h>
79 #include <sys/vnode.h>
80 #include <sys/buf.h>
81 #include <sys/mount.h>
82 #include <sys/malloc.h>
83 #include <sys/proc.h>
84
85 #include <ufs/ufs/inode.h>
86 #include <ufs/lfs/lfs.h>
87 #include <ufs/lfs/lfs_extern.h>
88
89 #include <uvm/uvm.h>
90
91 /*
92 * Return buffer with the contents of block "offset" from the beginning of
93 * directory "ip". If "res" is non-zero, fill it in with a pointer to the
94 * remaining space in the directory.
95 */
96 int
97 lfs_blkatoff(void *v)
98 {
99 struct vop_blkatoff_args /* {
100 struct vnode *a_vp;
101 off_t a_offset;
102 char **a_res;
103 struct buf **a_bpp;
104 } */ *ap = v;
105 struct lfs *fs;
106 struct inode *ip;
107 struct buf *bp;
108 daddr_t lbn;
109 int bsize, error;
110
111 ip = VTOI(ap->a_vp);
112 fs = ip->i_lfs;
113 lbn = lblkno(fs, ap->a_offset);
114 bsize = blksize(fs, ip, lbn);
115
116 *ap->a_bpp = NULL;
117 if ((error = bread(ap->a_vp, lbn, bsize, NOCRED, &bp)) != 0) {
118 brelse(bp);
119 return (error);
120 }
121 if (ap->a_res)
122 *ap->a_res = (char *)bp->b_data + blkoff(fs, ap->a_offset);
123 *ap->a_bpp = bp;
124 return (0);
125 }
126
127 #ifdef LFS_DEBUG_MALLOC
128 char *lfs_res_names[LFS_NB_COUNT] = {
129 "summary",
130 "superblock",
131 "ifile block",
132 "cluster",
133 "clean",
134 };
135 #endif
136
137 int lfs_res_qty[LFS_NB_COUNT] = {
138 LFS_N_SUMMARIES,
139 LFS_N_SBLOCKS,
140 LFS_N_IBLOCKS,
141 LFS_N_CLUSTERS,
142 LFS_N_CLEAN,
143 };
144
145 void
146 lfs_setup_resblks(struct lfs *fs)
147 {
148 int i, j;
149 int maxbpp;
150
151 fs->lfs_resblk = (res_t *)malloc(LFS_N_TOTAL * sizeof(res_t), M_SEGMENT,
152 M_WAITOK);
153 for (i = 0; i < LFS_N_TOTAL; i++) {
154 fs->lfs_resblk[i].inuse = 0;
155 fs->lfs_resblk[i].p = NULL;
156 }
157 for (i = 0; i < LFS_RESHASH_WIDTH; i++)
158 LIST_INIT(fs->lfs_reshash + i);
159
160 /*
161 * These types of allocations can be larger than a page,
162 * so we can't use the pool subsystem for them.
163 */
164 for (i = 0, j = 0; j < LFS_N_SUMMARIES; j++, i++)
165 fs->lfs_resblk[i].p = malloc(fs->lfs_sumsize, M_SEGMENT,
166 M_WAITOK);
167 for (j = 0; j < LFS_N_SBLOCKS; j++, i++)
168 fs->lfs_resblk[i].p = malloc(LFS_SBPAD, M_SEGMENT, M_WAITOK);
169 for (j = 0; j < LFS_N_IBLOCKS; j++, i++)
170 fs->lfs_resblk[i].p = malloc(fs->lfs_bsize, M_SEGMENT, M_WAITOK);
171 for (j = 0; j < LFS_N_CLUSTERS; j++, i++)
172 fs->lfs_resblk[i].p = malloc(MAXPHYS, M_SEGMENT, M_WAITOK);
173 for (j = 0; j < LFS_N_CLEAN; j++, i++)
174 fs->lfs_resblk[i].p = malloc(MAXPHYS, M_SEGMENT, M_WAITOK);
175
176 /*
177 * Initialize pools for small types (XXX is BPP small?)
178 */
179 maxbpp = ((fs->lfs_sumsize - SEGSUM_SIZE(fs)) / sizeof(int32_t) + 2);
180 maxbpp = MIN(maxbpp, fs->lfs_ssize / fs->lfs_fsize + 2);
181 pool_init(&fs->lfs_bpppool, maxbpp * sizeof(struct buf *), 0, 0,
182 LFS_N_BPP, "lfsbpppl", &pool_allocator_nointr);
183 pool_init(&fs->lfs_clpool, sizeof(struct lfs_cluster), 0, 0,
184 LFS_N_CL, "lfsclpl", &pool_allocator_nointr);
185 pool_init(&fs->lfs_segpool, sizeof(struct segment), 0, 0,
186 LFS_N_SEG, "lfssegpool", &pool_allocator_nointr);
187 }
188
189 void
190 lfs_free_resblks(struct lfs *fs)
191 {
192 int i;
193
194 pool_destroy(&fs->lfs_bpppool);
195 pool_destroy(&fs->lfs_segpool);
196 pool_destroy(&fs->lfs_clpool);
197
198 for (i = 0; i < LFS_N_TOTAL; i++) {
199 while(fs->lfs_resblk[i].inuse)
200 tsleep(&fs->lfs_resblk, PRIBIO + 1, "lfs_free", 0);
201 if (fs->lfs_resblk[i].p != NULL)
202 free(fs->lfs_resblk[i].p, M_SEGMENT);
203 }
204 free(fs->lfs_resblk, M_SEGMENT);
205 }
206
207 static unsigned int
208 lfs_mhash(void *vp)
209 {
210 return (unsigned int)(((unsigned long)vp) >> 2) % LFS_RESHASH_WIDTH;
211 }
212
213 /*
214 * Return memory of the given size for the given purpose, or use one of a
215 * number of spare last-resort buffers, if malloc returns NULL.
216 */
217 void *
218 lfs_malloc(struct lfs *fs, size_t size, int type)
219 {
220 struct lfs_res_blk *re;
221 void *r;
222 int i, s, start;
223 unsigned int h;
224
225 /* If no mem allocated for this type, it just waits */
226 if (lfs_res_qty[type] == 0)
227 return malloc(size, M_SEGMENT, M_WAITOK);
228
229 /* Otherwise try a quick malloc, and if it works, great */
230 if ((r = malloc(size, M_SEGMENT, M_NOWAIT)) != NULL)
231 return r;
232
233 /*
234 * If malloc returned NULL, we are forced to use one of our
235 * reserve blocks. We have on hand at least one summary block,
236 * at least one cluster block, at least one superblock,
237 * and several indirect blocks.
238 */
239 /* skip over blocks of other types */
240 for (i = 0, start = 0; i < type; i++)
241 start += lfs_res_qty[i];
242 while (r == NULL) {
243 for (i = 0; i < lfs_res_qty[type]; i++) {
244 if (fs->lfs_resblk[start + i].inuse == 0) {
245 re = fs->lfs_resblk + start + i;
246 re->inuse = 1;
247 r = re->p;
248 h = lfs_mhash(r);
249 s = splbio();
250 LIST_INSERT_HEAD(&fs->lfs_reshash[h], re, res);
251 splx(s);
252 return r;
253 }
254 }
255 #ifdef LFS_DEBUG_MALLOC
256 printf("sleeping on %s (%d)\n", lfs_res_names[type], lfs_res_qty[type]);
257 #endif
258 tsleep(&fs->lfs_resblk, PVM, "lfs_malloc", 0);
259 #ifdef LFS_DEBUG_MALLOC
260 printf("done sleeping on %s\n", lfs_res_names[type]);
261 #endif
262 }
263 /* NOTREACHED */
264 return r;
265 }
266
267 void
268 lfs_free(struct lfs *fs, void *p, int type)
269 {
270 int s;
271 unsigned int h;
272 res_t *re;
273
274 h = lfs_mhash(p);
275 s = splbio();
276 LIST_FOREACH(re, &fs->lfs_reshash[h], res) {
277 if (re->p == p) {
278 LIST_REMOVE(re, res);
279 re->inuse = 0;
280 wakeup(&fs->lfs_resblk);
281 splx(s);
282 return;
283 }
284 }
285 splx(s);
286
287 /*
288 * If we didn't find it, free it.
289 */
290 free(p, M_SEGMENT);
291 }
292
293 /*
294 * lfs_seglock --
295 * Single thread the segment writer.
296 */
297 int
298 lfs_seglock(struct lfs *fs, unsigned long flags)
299 {
300 struct segment *sp;
301
302 if (fs->lfs_seglock) {
303 if (fs->lfs_lockpid == curproc->p_pid) {
304 ++fs->lfs_seglock;
305 fs->lfs_sp->seg_flags |= flags;
306 return 0;
307 } else if (flags & SEGM_PAGEDAEMON)
308 return EWOULDBLOCK;
309 else while (fs->lfs_seglock)
310 (void)tsleep(&fs->lfs_seglock, PRIBIO + 1,
311 "lfs seglock", 0);
312 }
313
314 fs->lfs_seglock = 1;
315 fs->lfs_lockpid = curproc->p_pid;
316
317 /* Drain fragment size changes out */
318 lockmgr(&fs->lfs_fraglock, LK_EXCLUSIVE, 0);
319
320 sp = fs->lfs_sp = pool_get(&fs->lfs_segpool, PR_WAITOK);
321 sp->bpp = pool_get(&fs->lfs_bpppool, PR_WAITOK);
322 sp->seg_flags = flags;
323 sp->vp = NULL;
324 sp->seg_iocount = 0;
325 (void) lfs_initseg(fs);
326
327 /*
328 * Keep a cumulative count of the outstanding I/O operations. If the
329 * disk drive catches up with us it could go to zero before we finish,
330 * so we artificially increment it by one until we've scheduled all of
331 * the writes we intend to do.
332 */
333 ++fs->lfs_iocount;
334 return 0;
335 }
336
337 static void lfs_unmark_dirop(struct lfs *);
338
339 static void
340 lfs_unmark_dirop(struct lfs *fs)
341 {
342 struct inode *ip, *nip;
343 struct vnode *vp;
344 extern int lfs_dirvcount;
345
346 for (ip = TAILQ_FIRST(&fs->lfs_dchainhd); ip != NULL; ip = nip) {
347 nip = TAILQ_NEXT(ip, i_lfs_dchain);
348 vp = ITOV(ip);
349
350 if (VOP_ISLOCKED(vp) &&
351 vp->v_lock.lk_lockholder != curproc->p_pid) {
352 continue;
353 }
354 if ((VTOI(vp)->i_flag & IN_ADIROP) == 0) {
355 --lfs_dirvcount;
356 vp->v_flag &= ~VDIROP;
357 TAILQ_REMOVE(&fs->lfs_dchainhd, ip, i_lfs_dchain);
358 wakeup(&lfs_dirvcount);
359 fs->lfs_unlockvp = vp;
360 vrele(vp);
361 fs->lfs_unlockvp = NULL;
362 }
363 }
364 }
365
366 #ifndef LFS_NO_AUTO_SEGCLEAN
367 static void
368 lfs_auto_segclean(struct lfs *fs)
369 {
370 int i, error;
371
372 /*
373 * Now that we've swapped lfs_activesb, but while we still
374 * hold the segment lock, run through the segment list marking
375 * the empty ones clean.
376 * XXX - do we really need to do them all at once?
377 */
378 for (i = 0; i < fs->lfs_nseg; i++) {
379 if ((fs->lfs_suflags[0][i] &
380 (SEGUSE_ACTIVE | SEGUSE_DIRTY | SEGUSE_EMPTY)) ==
381 (SEGUSE_DIRTY | SEGUSE_EMPTY) &&
382 (fs->lfs_suflags[1][i] &
383 (SEGUSE_ACTIVE | SEGUSE_DIRTY | SEGUSE_EMPTY)) ==
384 (SEGUSE_DIRTY | SEGUSE_EMPTY)) {
385
386 if ((error = lfs_do_segclean(fs, i)) != 0) {
387 #ifdef DEBUG
388 printf("lfs_auto_segclean: lfs_do_segclean returned %d for seg %d\n", error, i);
389 #endif /* DEBUG */
390 }
391 }
392 fs->lfs_suflags[1 - fs->lfs_activesb][i] =
393 fs->lfs_suflags[fs->lfs_activesb][i];
394 }
395 }
396 #endif /* LFS_AUTO_SEGCLEAN */
397
398 /*
399 * lfs_segunlock --
400 * Single thread the segment writer.
401 */
402 void
403 lfs_segunlock(struct lfs *fs)
404 {
405 struct segment *sp;
406 unsigned long sync, ckp;
407 struct buf *bp;
408 #ifdef LFS_MALLOC_SUMMARY
409 extern int locked_queue_count;
410 extern long locked_queue_bytes;
411 #endif
412
413 sp = fs->lfs_sp;
414
415 if (fs->lfs_seglock == 1) {
416 if ((sp->seg_flags & SEGM_PROT) == 0)
417 lfs_unmark_dirop(fs);
418 sync = sp->seg_flags & SEGM_SYNC;
419 ckp = sp->seg_flags & SEGM_CKP;
420 if (sp->bpp != sp->cbpp) {
421 /* Free allocated segment summary */
422 fs->lfs_offset -= btofsb(fs, fs->lfs_sumsize);
423 bp = *sp->bpp;
424 #ifdef LFS_MALLOC_SUMMARY
425 lfs_freebuf(fs, bp);
426 #else
427 s = splbio();
428 bremfree(bp);
429 bp->b_flags |= B_DONE|B_INVAL;
430 bp->b_flags &= ~B_DELWRI;
431 reassignbuf(bp,bp->b_vp);
432 splx(s);
433 brelse(bp);
434 #endif
435 } else
436 printf ("unlock to 0 with no summary");
437
438 pool_put(&fs->lfs_bpppool, sp->bpp);
439 sp->bpp = NULL;
440 /* The sync case holds a reference in `sp' to be freed below */
441 if (!sync)
442 pool_put(&fs->lfs_segpool, sp);
443 fs->lfs_sp = NULL;
444
445 /*
446 * If the I/O count is non-zero, sleep until it reaches zero.
447 * At the moment, the user's process hangs around so we can
448 * sleep.
449 */
450 if (--fs->lfs_iocount == 0) {
451 lfs_countlocked(&locked_queue_count,
452 &locked_queue_bytes, "lfs_segunlock");
453 wakeup(&locked_queue_count);
454 wakeup(&fs->lfs_iocount);
455 }
456 /*
457 * If we're not checkpointing, we don't have to block
458 * other processes to wait for a synchronous write
459 * to complete.
460 */
461 if (!ckp) {
462 --fs->lfs_seglock;
463 fs->lfs_lockpid = 0;
464 wakeup(&fs->lfs_seglock);
465 }
466 /*
467 * We let checkpoints happen asynchronously. That means
468 * that during recovery, we have to roll forward between
469 * the two segments described by the first and second
470 * superblocks to make sure that the checkpoint described
471 * by a superblock completed.
472 */
473 while (ckp && sync && fs->lfs_iocount)
474 (void)tsleep(&fs->lfs_iocount, PRIBIO + 1,
475 "lfs_iocount", 0);
476 while (sync && sp->seg_iocount) {
477 (void)tsleep(&sp->seg_iocount, PRIBIO + 1,
478 "seg_iocount", 0);
479 /* printf("sleeping on iocount %x == %d\n", sp, sp->seg_iocount); */
480 }
481 if (sync)
482 pool_put(&fs->lfs_segpool, sp);
483 if (ckp) {
484 fs->lfs_nactive = 0;
485 /* If we *know* everything's on disk, write both sbs */
486 /* XXX should wait for this one */
487 if (sync)
488 lfs_writesuper(fs, fs->lfs_sboffs[fs->lfs_activesb]);
489 lfs_writesuper(fs, fs->lfs_sboffs[1 - fs->lfs_activesb]);
490 #ifndef LFS_NO_AUTO_SEGCLEAN
491 lfs_auto_segclean(fs);
492 #endif
493 fs->lfs_activesb = 1 - fs->lfs_activesb;
494 --fs->lfs_seglock;
495 fs->lfs_lockpid = 0;
496 wakeup(&fs->lfs_seglock);
497 }
498 /* Reenable fragment size changes */
499 lockmgr(&fs->lfs_fraglock, LK_RELEASE, 0);
500 } else if (fs->lfs_seglock == 0) {
501 panic ("Seglock not held");
502 } else {
503 --fs->lfs_seglock;
504 }
505 }
506