lfs_subr.c revision 1.65 1 1.65 christos /* $NetBSD: lfs_subr.c,v 1.65 2006/11/16 01:33:53 christos Exp $ */
2 1.2 cgd
3 1.8 perseant /*-
4 1.31 perseant * Copyright (c) 1999, 2000, 2001, 2002, 2003 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.33 perseant * This product includes software developed by the NetBSD
21 1.33 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.43 agc * 3. Neither the name of the University nor the names of its contributors
51 1.1 mycroft * may be used to endorse or promote products derived from this software
52 1.1 mycroft * without specific prior written permission.
53 1.1 mycroft *
54 1.1 mycroft * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 1.1 mycroft * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 1.1 mycroft * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 1.1 mycroft * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 1.1 mycroft * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 1.1 mycroft * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 1.1 mycroft * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 1.1 mycroft * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 1.1 mycroft * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 1.1 mycroft * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 1.1 mycroft * SUCH DAMAGE.
65 1.1 mycroft *
66 1.6 fvdl * @(#)lfs_subr.c 8.4 (Berkeley) 5/8/95
67 1.1 mycroft */
68 1.20 lukem
69 1.20 lukem #include <sys/cdefs.h>
70 1.65 christos __KERNEL_RCSID(0, "$NetBSD: lfs_subr.c,v 1.65 2006/11/16 01:33:53 christos Exp $");
71 1.1 mycroft
72 1.1 mycroft #include <sys/param.h>
73 1.3 christos #include <sys/systm.h>
74 1.1 mycroft #include <sys/namei.h>
75 1.1 mycroft #include <sys/vnode.h>
76 1.1 mycroft #include <sys/buf.h>
77 1.1 mycroft #include <sys/mount.h>
78 1.1 mycroft #include <sys/malloc.h>
79 1.1 mycroft #include <sys/proc.h>
80 1.61 perseant #include <sys/kauth.h>
81 1.1 mycroft
82 1.1 mycroft #include <ufs/ufs/inode.h>
83 1.1 mycroft #include <ufs/lfs/lfs.h>
84 1.1 mycroft #include <ufs/lfs/lfs_extern.h>
85 1.1 mycroft
86 1.31 perseant #include <uvm/uvm.h>
87 1.31 perseant
88 1.50 perseant #ifdef DEBUG
89 1.53 christos const char *lfs_res_names[LFS_NB_COUNT] = {
90 1.31 perseant "summary",
91 1.31 perseant "superblock",
92 1.52 perseant "file block",
93 1.31 perseant "cluster",
94 1.31 perseant "clean",
95 1.52 perseant "blkiov",
96 1.31 perseant };
97 1.31 perseant #endif
98 1.31 perseant
99 1.31 perseant int lfs_res_qty[LFS_NB_COUNT] = {
100 1.31 perseant LFS_N_SUMMARIES,
101 1.31 perseant LFS_N_SBLOCKS,
102 1.31 perseant LFS_N_IBLOCKS,
103 1.31 perseant LFS_N_CLUSTERS,
104 1.31 perseant LFS_N_CLEAN,
105 1.52 perseant LFS_N_BLKIOV,
106 1.31 perseant };
107 1.31 perseant
108 1.31 perseant void
109 1.31 perseant lfs_setup_resblks(struct lfs *fs)
110 1.31 perseant {
111 1.31 perseant int i, j;
112 1.31 perseant int maxbpp;
113 1.31 perseant
114 1.51 perseant ASSERT_NO_SEGLOCK(fs);
115 1.31 perseant fs->lfs_resblk = (res_t *)malloc(LFS_N_TOTAL * sizeof(res_t), M_SEGMENT,
116 1.33 perseant M_WAITOK);
117 1.31 perseant for (i = 0; i < LFS_N_TOTAL; i++) {
118 1.31 perseant fs->lfs_resblk[i].inuse = 0;
119 1.31 perseant fs->lfs_resblk[i].p = NULL;
120 1.31 perseant }
121 1.31 perseant for (i = 0; i < LFS_RESHASH_WIDTH; i++)
122 1.31 perseant LIST_INIT(fs->lfs_reshash + i);
123 1.31 perseant
124 1.31 perseant /*
125 1.31 perseant * These types of allocations can be larger than a page,
126 1.31 perseant * so we can't use the pool subsystem for them.
127 1.31 perseant */
128 1.31 perseant for (i = 0, j = 0; j < LFS_N_SUMMARIES; j++, i++)
129 1.34 perseant fs->lfs_resblk[i].size = fs->lfs_sumsize;
130 1.31 perseant for (j = 0; j < LFS_N_SBLOCKS; j++, i++)
131 1.34 perseant fs->lfs_resblk[i].size = LFS_SBPAD;
132 1.31 perseant for (j = 0; j < LFS_N_IBLOCKS; j++, i++)
133 1.34 perseant fs->lfs_resblk[i].size = fs->lfs_bsize;
134 1.31 perseant for (j = 0; j < LFS_N_CLUSTERS; j++, i++)
135 1.34 perseant fs->lfs_resblk[i].size = MAXPHYS;
136 1.31 perseant for (j = 0; j < LFS_N_CLEAN; j++, i++)
137 1.34 perseant fs->lfs_resblk[i].size = MAXPHYS;
138 1.52 perseant for (j = 0; j < LFS_N_BLKIOV; j++, i++)
139 1.52 perseant fs->lfs_resblk[i].size = LFS_MARKV_MAXBLKCNT * sizeof(BLOCK_INFO);
140 1.34 perseant
141 1.34 perseant for (i = 0; i < LFS_N_TOTAL; i++) {
142 1.34 perseant fs->lfs_resblk[i].p = malloc(fs->lfs_resblk[i].size,
143 1.34 perseant M_SEGMENT, M_WAITOK);
144 1.34 perseant }
145 1.31 perseant
146 1.31 perseant /*
147 1.31 perseant * Initialize pools for small types (XXX is BPP small?)
148 1.31 perseant */
149 1.46 simonb pool_init(&fs->lfs_clpool, sizeof(struct lfs_cluster), 0, 0, 0,
150 1.46 simonb "lfsclpl", &pool_allocator_nointr);
151 1.46 simonb pool_init(&fs->lfs_segpool, sizeof(struct segment), 0, 0, 0,
152 1.46 simonb "lfssegpool", &pool_allocator_nointr);
153 1.31 perseant maxbpp = ((fs->lfs_sumsize - SEGSUM_SIZE(fs)) / sizeof(int32_t) + 2);
154 1.47 yamt maxbpp = MIN(maxbpp, segsize(fs) / fs->lfs_fsize + 2);
155 1.46 simonb pool_init(&fs->lfs_bpppool, maxbpp * sizeof(struct buf *), 0, 0, 0,
156 1.46 simonb "lfsbpppl", &pool_allocator_nointr);
157 1.31 perseant }
158 1.31 perseant
159 1.31 perseant void
160 1.31 perseant lfs_free_resblks(struct lfs *fs)
161 1.31 perseant {
162 1.31 perseant int i;
163 1.31 perseant
164 1.31 perseant pool_destroy(&fs->lfs_bpppool);
165 1.31 perseant pool_destroy(&fs->lfs_segpool);
166 1.31 perseant pool_destroy(&fs->lfs_clpool);
167 1.31 perseant
168 1.51 perseant simple_lock(&fs->lfs_interlock);
169 1.31 perseant for (i = 0; i < LFS_N_TOTAL; i++) {
170 1.39 perseant while (fs->lfs_resblk[i].inuse)
171 1.51 perseant ltsleep(&fs->lfs_resblk, PRIBIO + 1, "lfs_free", 0,
172 1.51 perseant &fs->lfs_interlock);
173 1.31 perseant if (fs->lfs_resblk[i].p != NULL)
174 1.31 perseant free(fs->lfs_resblk[i].p, M_SEGMENT);
175 1.31 perseant }
176 1.31 perseant free(fs->lfs_resblk, M_SEGMENT);
177 1.51 perseant simple_unlock(&fs->lfs_interlock);
178 1.31 perseant }
179 1.31 perseant
180 1.31 perseant static unsigned int
181 1.31 perseant lfs_mhash(void *vp)
182 1.31 perseant {
183 1.31 perseant return (unsigned int)(((unsigned long)vp) >> 2) % LFS_RESHASH_WIDTH;
184 1.31 perseant }
185 1.31 perseant
186 1.31 perseant /*
187 1.31 perseant * Return memory of the given size for the given purpose, or use one of a
188 1.31 perseant * number of spare last-resort buffers, if malloc returns NULL.
189 1.49 perry */
190 1.31 perseant void *
191 1.31 perseant lfs_malloc(struct lfs *fs, size_t size, int type)
192 1.31 perseant {
193 1.31 perseant struct lfs_res_blk *re;
194 1.31 perseant void *r;
195 1.31 perseant int i, s, start;
196 1.31 perseant unsigned int h;
197 1.31 perseant
198 1.51 perseant ASSERT_MAYBE_SEGLOCK(fs);
199 1.34 perseant r = NULL;
200 1.34 perseant
201 1.31 perseant /* If no mem allocated for this type, it just waits */
202 1.34 perseant if (lfs_res_qty[type] == 0) {
203 1.34 perseant r = malloc(size, M_SEGMENT, M_WAITOK);
204 1.34 perseant return r;
205 1.34 perseant }
206 1.31 perseant
207 1.31 perseant /* Otherwise try a quick malloc, and if it works, great */
208 1.34 perseant if ((r = malloc(size, M_SEGMENT, M_NOWAIT)) != NULL) {
209 1.31 perseant return r;
210 1.34 perseant }
211 1.31 perseant
212 1.31 perseant /*
213 1.31 perseant * If malloc returned NULL, we are forced to use one of our
214 1.31 perseant * reserve blocks. We have on hand at least one summary block,
215 1.31 perseant * at least one cluster block, at least one superblock,
216 1.31 perseant * and several indirect blocks.
217 1.31 perseant */
218 1.51 perseant
219 1.51 perseant simple_lock(&fs->lfs_interlock);
220 1.31 perseant /* skip over blocks of other types */
221 1.31 perseant for (i = 0, start = 0; i < type; i++)
222 1.31 perseant start += lfs_res_qty[i];
223 1.31 perseant while (r == NULL) {
224 1.31 perseant for (i = 0; i < lfs_res_qty[type]; i++) {
225 1.31 perseant if (fs->lfs_resblk[start + i].inuse == 0) {
226 1.31 perseant re = fs->lfs_resblk + start + i;
227 1.31 perseant re->inuse = 1;
228 1.31 perseant r = re->p;
229 1.34 perseant KASSERT(re->size >= size);
230 1.31 perseant h = lfs_mhash(r);
231 1.31 perseant s = splbio();
232 1.31 perseant LIST_INSERT_HEAD(&fs->lfs_reshash[h], re, res);
233 1.31 perseant splx(s);
234 1.51 perseant simple_unlock(&fs->lfs_interlock);
235 1.31 perseant return r;
236 1.31 perseant }
237 1.31 perseant }
238 1.51 perseant DLOG((DLOG_MALLOC, "sleeping on %s (%d)\n",
239 1.51 perseant lfs_res_names[type], lfs_res_qty[type]));
240 1.51 perseant ltsleep(&fs->lfs_resblk, PVM, "lfs_malloc", 0,
241 1.51 perseant &fs->lfs_interlock);
242 1.51 perseant DLOG((DLOG_MALLOC, "done sleeping on %s\n",
243 1.51 perseant lfs_res_names[type]));
244 1.31 perseant }
245 1.31 perseant /* NOTREACHED */
246 1.51 perseant simple_unlock(&fs->lfs_interlock);
247 1.31 perseant return r;
248 1.31 perseant }
249 1.31 perseant
250 1.31 perseant void
251 1.65 christos lfs_free(struct lfs *fs, void *p, int type)
252 1.31 perseant {
253 1.31 perseant int s;
254 1.31 perseant unsigned int h;
255 1.31 perseant res_t *re;
256 1.32 yamt #ifdef DEBUG
257 1.32 yamt int i;
258 1.32 yamt #endif
259 1.31 perseant
260 1.51 perseant ASSERT_MAYBE_SEGLOCK(fs);
261 1.31 perseant h = lfs_mhash(p);
262 1.51 perseant simple_lock(&fs->lfs_interlock);
263 1.31 perseant s = splbio();
264 1.31 perseant LIST_FOREACH(re, &fs->lfs_reshash[h], res) {
265 1.31 perseant if (re->p == p) {
266 1.32 yamt KASSERT(re->inuse == 1);
267 1.31 perseant LIST_REMOVE(re, res);
268 1.31 perseant re->inuse = 0;
269 1.31 perseant wakeup(&fs->lfs_resblk);
270 1.31 perseant splx(s);
271 1.51 perseant simple_unlock(&fs->lfs_interlock);
272 1.31 perseant return;
273 1.31 perseant }
274 1.31 perseant }
275 1.32 yamt #ifdef DEBUG
276 1.32 yamt for (i = 0; i < LFS_N_TOTAL; i++) {
277 1.32 yamt if (fs->lfs_resblk[i].p == p)
278 1.34 perseant panic("lfs_free: inconsistent reserved block");
279 1.32 yamt }
280 1.32 yamt #endif
281 1.31 perseant splx(s);
282 1.51 perseant simple_unlock(&fs->lfs_interlock);
283 1.51 perseant
284 1.31 perseant /*
285 1.31 perseant * If we didn't find it, free it.
286 1.31 perseant */
287 1.31 perseant free(p, M_SEGMENT);
288 1.31 perseant }
289 1.1 mycroft
290 1.1 mycroft /*
291 1.1 mycroft * lfs_seglock --
292 1.1 mycroft * Single thread the segment writer.
293 1.1 mycroft */
294 1.31 perseant int
295 1.18 perseant lfs_seglock(struct lfs *fs, unsigned long flags)
296 1.1 mycroft {
297 1.1 mycroft struct segment *sp;
298 1.49 perry
299 1.38 perseant simple_lock(&fs->lfs_interlock);
300 1.7 thorpej if (fs->lfs_seglock) {
301 1.58 perseant if (fs->lfs_lockpid == curproc->p_pid &&
302 1.58 perseant fs->lfs_locklwp == curlwp->l_lid) {
303 1.38 perseant simple_unlock(&fs->lfs_interlock);
304 1.1 mycroft ++fs->lfs_seglock;
305 1.1 mycroft fs->lfs_sp->seg_flags |= flags;
306 1.31 perseant return 0;
307 1.38 perseant } else if (flags & SEGM_PAGEDAEMON) {
308 1.38 perseant simple_unlock(&fs->lfs_interlock);
309 1.31 perseant return EWOULDBLOCK;
310 1.51 perseant } else {
311 1.51 perseant while (fs->lfs_seglock) {
312 1.51 perseant (void)ltsleep(&fs->lfs_seglock, PRIBIO + 1,
313 1.51 perseant "lfs seglock", 0, &fs->lfs_interlock);
314 1.51 perseant }
315 1.51 perseant }
316 1.7 thorpej }
317 1.49 perry
318 1.1 mycroft fs->lfs_seglock = 1;
319 1.1 mycroft fs->lfs_lockpid = curproc->p_pid;
320 1.58 perseant fs->lfs_locklwp = curlwp->l_lid;
321 1.38 perseant simple_unlock(&fs->lfs_interlock);
322 1.36 perseant fs->lfs_cleanind = 0;
323 1.36 perseant
324 1.51 perseant #ifdef DEBUG
325 1.51 perseant LFS_ENTER_LOG("seglock", __FILE__, __LINE__, 0, flags, curproc->p_pid);
326 1.51 perseant #endif
327 1.27 perseant /* Drain fragment size changes out */
328 1.27 perseant lockmgr(&fs->lfs_fraglock, LK_EXCLUSIVE, 0);
329 1.27 perseant
330 1.31 perseant sp = fs->lfs_sp = pool_get(&fs->lfs_segpool, PR_WAITOK);
331 1.31 perseant sp->bpp = pool_get(&fs->lfs_bpppool, PR_WAITOK);
332 1.1 mycroft sp->seg_flags = flags;
333 1.1 mycroft sp->vp = NULL;
334 1.26 perseant sp->seg_iocount = 0;
335 1.1 mycroft (void) lfs_initseg(fs);
336 1.49 perry
337 1.1 mycroft /*
338 1.1 mycroft * Keep a cumulative count of the outstanding I/O operations. If the
339 1.1 mycroft * disk drive catches up with us it could go to zero before we finish,
340 1.1 mycroft * so we artificially increment it by one until we've scheduled all of
341 1.1 mycroft * the writes we intend to do.
342 1.1 mycroft */
343 1.51 perseant simple_lock(&fs->lfs_interlock);
344 1.1 mycroft ++fs->lfs_iocount;
345 1.51 perseant simple_unlock(&fs->lfs_interlock);
346 1.31 perseant return 0;
347 1.1 mycroft }
348 1.8 perseant
349 1.31 perseant static void lfs_unmark_dirop(struct lfs *);
350 1.31 perseant
351 1.31 perseant static void
352 1.31 perseant lfs_unmark_dirop(struct lfs *fs)
353 1.31 perseant {
354 1.31 perseant struct inode *ip, *nip;
355 1.31 perseant struct vnode *vp;
356 1.40 perseant int doit;
357 1.31 perseant
358 1.51 perseant ASSERT_NO_SEGLOCK(fs);
359 1.40 perseant simple_lock(&fs->lfs_interlock);
360 1.40 perseant doit = !(fs->lfs_flags & LFS_UNDIROP);
361 1.40 perseant if (doit)
362 1.40 perseant fs->lfs_flags |= LFS_UNDIROP;
363 1.51 perseant if (!doit) {
364 1.51 perseant simple_unlock(&fs->lfs_interlock);
365 1.40 perseant return;
366 1.51 perseant }
367 1.40 perseant
368 1.31 perseant for (ip = TAILQ_FIRST(&fs->lfs_dchainhd); ip != NULL; ip = nip) {
369 1.31 perseant nip = TAILQ_NEXT(ip, i_lfs_dchain);
370 1.51 perseant simple_unlock(&fs->lfs_interlock);
371 1.31 perseant vp = ITOV(ip);
372 1.31 perseant
373 1.51 perseant simple_lock(&vp->v_interlock);
374 1.62 perseant if (VOP_ISLOCKED(vp) == LK_EXCLOTHER) {
375 1.51 perseant simple_lock(&fs->lfs_interlock);
376 1.51 perseant simple_unlock(&vp->v_interlock);
377 1.31 perseant continue;
378 1.31 perseant }
379 1.61 perseant if ((VTOI(vp)->i_flag & (IN_ADIROP | IN_ALLMOD)) == 0) {
380 1.51 perseant simple_lock(&fs->lfs_interlock);
381 1.51 perseant simple_lock(&lfs_subsys_lock);
382 1.31 perseant --lfs_dirvcount;
383 1.51 perseant simple_unlock(&lfs_subsys_lock);
384 1.59 perseant --fs->lfs_dirvcount;
385 1.31 perseant vp->v_flag &= ~VDIROP;
386 1.31 perseant TAILQ_REMOVE(&fs->lfs_dchainhd, ip, i_lfs_dchain);
387 1.51 perseant simple_unlock(&fs->lfs_interlock);
388 1.31 perseant wakeup(&lfs_dirvcount);
389 1.51 perseant simple_unlock(&vp->v_interlock);
390 1.51 perseant simple_lock(&fs->lfs_interlock);
391 1.31 perseant fs->lfs_unlockvp = vp;
392 1.51 perseant simple_unlock(&fs->lfs_interlock);
393 1.31 perseant vrele(vp);
394 1.51 perseant simple_lock(&fs->lfs_interlock);
395 1.31 perseant fs->lfs_unlockvp = NULL;
396 1.51 perseant simple_unlock(&fs->lfs_interlock);
397 1.51 perseant } else
398 1.51 perseant simple_unlock(&vp->v_interlock);
399 1.51 perseant simple_lock(&fs->lfs_interlock);
400 1.31 perseant }
401 1.40 perseant
402 1.40 perseant fs->lfs_flags &= ~LFS_UNDIROP;
403 1.40 perseant simple_unlock(&fs->lfs_interlock);
404 1.51 perseant wakeup(&fs->lfs_flags);
405 1.31 perseant }
406 1.31 perseant
407 1.31 perseant static void
408 1.31 perseant lfs_auto_segclean(struct lfs *fs)
409 1.31 perseant {
410 1.48 perseant int i, error, s, waited;
411 1.31 perseant
412 1.51 perseant ASSERT_SEGLOCK(fs);
413 1.31 perseant /*
414 1.31 perseant * Now that we've swapped lfs_activesb, but while we still
415 1.31 perseant * hold the segment lock, run through the segment list marking
416 1.31 perseant * the empty ones clean.
417 1.31 perseant * XXX - do we really need to do them all at once?
418 1.31 perseant */
419 1.48 perseant waited = 0;
420 1.31 perseant for (i = 0; i < fs->lfs_nseg; i++) {
421 1.31 perseant if ((fs->lfs_suflags[0][i] &
422 1.31 perseant (SEGUSE_ACTIVE | SEGUSE_DIRTY | SEGUSE_EMPTY)) ==
423 1.31 perseant (SEGUSE_DIRTY | SEGUSE_EMPTY) &&
424 1.31 perseant (fs->lfs_suflags[1][i] &
425 1.31 perseant (SEGUSE_ACTIVE | SEGUSE_DIRTY | SEGUSE_EMPTY)) ==
426 1.31 perseant (SEGUSE_DIRTY | SEGUSE_EMPTY)) {
427 1.31 perseant
428 1.48 perseant /* Make sure the sb is written before we clean */
429 1.51 perseant simple_lock(&fs->lfs_interlock);
430 1.48 perseant s = splbio();
431 1.48 perseant while (waited == 0 && fs->lfs_sbactive)
432 1.51 perseant ltsleep(&fs->lfs_sbactive, PRIBIO+1, "lfs asb",
433 1.51 perseant 0, &fs->lfs_interlock);
434 1.48 perseant splx(s);
435 1.51 perseant simple_unlock(&fs->lfs_interlock);
436 1.48 perseant waited = 1;
437 1.48 perseant
438 1.31 perseant if ((error = lfs_do_segclean(fs, i)) != 0) {
439 1.50 perseant DLOG((DLOG_CLEAN, "lfs_auto_segclean: lfs_do_segclean returned %d for seg %d\n", error, i));
440 1.31 perseant }
441 1.31 perseant }
442 1.31 perseant fs->lfs_suflags[1 - fs->lfs_activesb][i] =
443 1.31 perseant fs->lfs_suflags[fs->lfs_activesb][i];
444 1.31 perseant }
445 1.31 perseant }
446 1.31 perseant
447 1.1 mycroft /*
448 1.1 mycroft * lfs_segunlock --
449 1.1 mycroft * Single thread the segment writer.
450 1.1 mycroft */
451 1.1 mycroft void
452 1.18 perseant lfs_segunlock(struct lfs *fs)
453 1.1 mycroft {
454 1.1 mycroft struct segment *sp;
455 1.1 mycroft unsigned long sync, ckp;
456 1.22 perseant struct buf *bp;
457 1.40 perseant int do_unmark_dirop = 0;
458 1.49 perry
459 1.15 perseant sp = fs->lfs_sp;
460 1.15 perseant
461 1.38 perseant simple_lock(&fs->lfs_interlock);
462 1.51 perseant LOCK_ASSERT(LFS_SEGLOCK_HELD(fs));
463 1.15 perseant if (fs->lfs_seglock == 1) {
464 1.57 perseant if ((sp->seg_flags & (SEGM_PROT | SEGM_CLEAN)) == 0 &&
465 1.57 perseant LFS_STARVED_FOR_SEGS(fs) == 0)
466 1.40 perseant do_unmark_dirop = 1;
467 1.38 perseant simple_unlock(&fs->lfs_interlock);
468 1.1 mycroft sync = sp->seg_flags & SEGM_SYNC;
469 1.1 mycroft ckp = sp->seg_flags & SEGM_CKP;
470 1.57 perseant
471 1.57 perseant /* We should have a segment summary, and nothing else */
472 1.57 perseant KASSERT(sp->cbpp == sp->bpp + 1);
473 1.57 perseant
474 1.57 perseant /* Free allocated segment summary */
475 1.57 perseant fs->lfs_offset -= btofsb(fs, fs->lfs_sumsize);
476 1.57 perseant bp = *sp->bpp;
477 1.57 perseant lfs_freebuf(fs, bp);
478 1.8 perseant
479 1.31 perseant pool_put(&fs->lfs_bpppool, sp->bpp);
480 1.18 perseant sp->bpp = NULL;
481 1.36 perseant
482 1.36 perseant /*
483 1.36 perseant * If we're not sync, we're done with sp, get rid of it.
484 1.36 perseant * Otherwise, we keep a local copy around but free
485 1.36 perseant * fs->lfs_sp so another process can use it (we have to
486 1.36 perseant * wait but they don't have to wait for us).
487 1.36 perseant */
488 1.26 perseant if (!sync)
489 1.31 perseant pool_put(&fs->lfs_segpool, sp);
490 1.18 perseant fs->lfs_sp = NULL;
491 1.1 mycroft
492 1.1 mycroft /*
493 1.1 mycroft * If the I/O count is non-zero, sleep until it reaches zero.
494 1.1 mycroft * At the moment, the user's process hangs around so we can
495 1.1 mycroft * sleep.
496 1.1 mycroft */
497 1.51 perseant simple_lock(&fs->lfs_interlock);
498 1.63 christos if (--fs->lfs_iocount == 0) {
499 1.44 yamt LFS_DEBUG_COUNTLOCKED("lfs_segunlock");
500 1.63 christos }
501 1.36 perseant if (fs->lfs_iocount <= 1)
502 1.22 perseant wakeup(&fs->lfs_iocount);
503 1.51 perseant simple_unlock(&fs->lfs_interlock);
504 1.1 mycroft /*
505 1.26 perseant * If we're not checkpointing, we don't have to block
506 1.26 perseant * other processes to wait for a synchronous write
507 1.26 perseant * to complete.
508 1.26 perseant */
509 1.26 perseant if (!ckp) {
510 1.51 perseant #ifdef DEBUG
511 1.51 perseant LFS_ENTER_LOG("segunlock_std", __FILE__, __LINE__, 0, 0, curproc->p_pid);
512 1.51 perseant #endif
513 1.38 perseant simple_lock(&fs->lfs_interlock);
514 1.26 perseant --fs->lfs_seglock;
515 1.26 perseant fs->lfs_lockpid = 0;
516 1.58 perseant fs->lfs_locklwp = 0;
517 1.38 perseant simple_unlock(&fs->lfs_interlock);
518 1.26 perseant wakeup(&fs->lfs_seglock);
519 1.26 perseant }
520 1.26 perseant /*
521 1.1 mycroft * We let checkpoints happen asynchronously. That means
522 1.1 mycroft * that during recovery, we have to roll forward between
523 1.1 mycroft * the two segments described by the first and second
524 1.1 mycroft * superblocks to make sure that the checkpoint described
525 1.1 mycroft * by a superblock completed.
526 1.1 mycroft */
527 1.51 perseant simple_lock(&fs->lfs_interlock);
528 1.26 perseant while (ckp && sync && fs->lfs_iocount)
529 1.51 perseant (void)ltsleep(&fs->lfs_iocount, PRIBIO + 1,
530 1.51 perseant "lfs_iocount", 0, &fs->lfs_interlock);
531 1.26 perseant while (sync && sp->seg_iocount) {
532 1.51 perseant (void)ltsleep(&sp->seg_iocount, PRIBIO + 1,
533 1.51 perseant "seg_iocount", 0, &fs->lfs_interlock);
534 1.50 perseant DLOG((DLOG_SEG, "sleeping on iocount %x == %d\n", sp, sp->seg_iocount));
535 1.26 perseant }
536 1.51 perseant simple_unlock(&fs->lfs_interlock);
537 1.26 perseant if (sync)
538 1.31 perseant pool_put(&fs->lfs_segpool, sp);
539 1.36 perseant
540 1.1 mycroft if (ckp) {
541 1.1 mycroft fs->lfs_nactive = 0;
542 1.8 perseant /* If we *know* everything's on disk, write both sbs */
543 1.33 perseant /* XXX should wait for this one */
544 1.21 chs if (sync)
545 1.31 perseant lfs_writesuper(fs, fs->lfs_sboffs[fs->lfs_activesb]);
546 1.31 perseant lfs_writesuper(fs, fs->lfs_sboffs[1 - fs->lfs_activesb]);
547 1.48 perseant if (!(fs->lfs_ivnode->v_mount->mnt_iflag & IMNT_UNMOUNT)) {
548 1.35 perseant lfs_auto_segclean(fs);
549 1.48 perseant /* If sync, we can clean the remainder too */
550 1.48 perseant if (sync)
551 1.48 perseant lfs_auto_segclean(fs);
552 1.48 perseant }
553 1.8 perseant fs->lfs_activesb = 1 - fs->lfs_activesb;
554 1.51 perseant #ifdef DEBUG
555 1.51 perseant LFS_ENTER_LOG("segunlock_ckp", __FILE__, __LINE__, 0, 0, curproc->p_pid);
556 1.51 perseant #endif
557 1.38 perseant simple_lock(&fs->lfs_interlock);
558 1.26 perseant --fs->lfs_seglock;
559 1.26 perseant fs->lfs_lockpid = 0;
560 1.58 perseant fs->lfs_locklwp = 0;
561 1.38 perseant simple_unlock(&fs->lfs_interlock);
562 1.26 perseant wakeup(&fs->lfs_seglock);
563 1.1 mycroft }
564 1.27 perseant /* Reenable fragment size changes */
565 1.27 perseant lockmgr(&fs->lfs_fraglock, LK_RELEASE, 0);
566 1.40 perseant if (do_unmark_dirop)
567 1.40 perseant lfs_unmark_dirop(fs);
568 1.1 mycroft } else if (fs->lfs_seglock == 0) {
569 1.38 perseant simple_unlock(&fs->lfs_interlock);
570 1.1 mycroft panic ("Seglock not held");
571 1.1 mycroft } else {
572 1.1 mycroft --fs->lfs_seglock;
573 1.38 perseant simple_unlock(&fs->lfs_interlock);
574 1.1 mycroft }
575 1.41 yamt }
576 1.41 yamt
577 1.41 yamt /*
578 1.41 yamt * drain dirops and start writer.
579 1.41 yamt */
580 1.41 yamt int
581 1.41 yamt lfs_writer_enter(struct lfs *fs, const char *wmesg)
582 1.41 yamt {
583 1.41 yamt int error = 0;
584 1.41 yamt
585 1.51 perseant ASSERT_MAYBE_SEGLOCK(fs);
586 1.41 yamt simple_lock(&fs->lfs_interlock);
587 1.41 yamt
588 1.41 yamt /* disallow dirops during flush */
589 1.41 yamt fs->lfs_writer++;
590 1.41 yamt
591 1.41 yamt while (fs->lfs_dirops > 0) {
592 1.49 perry ++fs->lfs_diropwait;
593 1.41 yamt error = ltsleep(&fs->lfs_writer, PRIBIO+1, wmesg, 0,
594 1.51 perseant &fs->lfs_interlock);
595 1.49 perry --fs->lfs_diropwait;
596 1.41 yamt }
597 1.41 yamt
598 1.41 yamt if (error)
599 1.41 yamt fs->lfs_writer--;
600 1.41 yamt
601 1.41 yamt simple_unlock(&fs->lfs_interlock);
602 1.41 yamt
603 1.41 yamt return error;
604 1.41 yamt }
605 1.41 yamt
606 1.41 yamt void
607 1.41 yamt lfs_writer_leave(struct lfs *fs)
608 1.41 yamt {
609 1.41 yamt boolean_t dowakeup;
610 1.41 yamt
611 1.51 perseant ASSERT_MAYBE_SEGLOCK(fs);
612 1.41 yamt simple_lock(&fs->lfs_interlock);
613 1.41 yamt dowakeup = !(--fs->lfs_writer);
614 1.41 yamt simple_unlock(&fs->lfs_interlock);
615 1.41 yamt if (dowakeup)
616 1.41 yamt wakeup(&fs->lfs_dirops);
617 1.1 mycroft }
618 1.57 perseant
619 1.57 perseant /*
620 1.57 perseant * Unlock, wait for the cleaner, then relock to where we were before.
621 1.57 perseant * To be used only at a fairly high level, to address a paucity of free
622 1.57 perseant * segments propagated back from lfs_gop_write().
623 1.57 perseant */
624 1.57 perseant void
625 1.57 perseant lfs_segunlock_relock(struct lfs *fs)
626 1.57 perseant {
627 1.57 perseant int n = fs->lfs_seglock;
628 1.57 perseant u_int16_t seg_flags;
629 1.61 perseant CLEANERINFO *cip;
630 1.61 perseant struct buf *bp;
631 1.57 perseant
632 1.57 perseant if (n == 0)
633 1.57 perseant return;
634 1.57 perseant
635 1.57 perseant /* Write anything we've already gathered to disk */
636 1.57 perseant lfs_writeseg(fs, fs->lfs_sp);
637 1.57 perseant
638 1.61 perseant /* Tell cleaner */
639 1.61 perseant LFS_CLEANERINFO(cip, fs, bp);
640 1.61 perseant cip->flags |= LFS_CLEANER_MUST_CLEAN;
641 1.61 perseant LFS_SYNC_CLEANERINFO(cip, fs, bp, 1);
642 1.61 perseant
643 1.57 perseant /* Save segment flags for later */
644 1.57 perseant seg_flags = fs->lfs_sp->seg_flags;
645 1.57 perseant
646 1.57 perseant fs->lfs_sp->seg_flags |= SEGM_PROT; /* Don't unmark dirop nodes */
647 1.57 perseant while(fs->lfs_seglock)
648 1.57 perseant lfs_segunlock(fs);
649 1.57 perseant
650 1.57 perseant /* Wait for the cleaner */
651 1.60 perseant lfs_wakeup_cleaner(fs);
652 1.57 perseant simple_lock(&fs->lfs_interlock);
653 1.57 perseant while (LFS_STARVED_FOR_SEGS(fs))
654 1.57 perseant ltsleep(&fs->lfs_avail, PRIBIO, "relock", 0,
655 1.57 perseant &fs->lfs_interlock);
656 1.57 perseant simple_unlock(&fs->lfs_interlock);
657 1.57 perseant
658 1.57 perseant /* Put the segment lock back the way it was. */
659 1.57 perseant while(n--)
660 1.57 perseant lfs_seglock(fs, seg_flags);
661 1.57 perseant
662 1.61 perseant /* Cleaner can relax now */
663 1.61 perseant LFS_CLEANERINFO(cip, fs, bp);
664 1.61 perseant cip->flags &= ~LFS_CLEANER_MUST_CLEAN;
665 1.61 perseant LFS_SYNC_CLEANERINFO(cip, fs, bp, 1);
666 1.61 perseant
667 1.57 perseant return;
668 1.57 perseant }
669 1.60 perseant
670 1.60 perseant /*
671 1.60 perseant * Wake up the cleaner, provided that nowrap is not set.
672 1.60 perseant */
673 1.60 perseant void
674 1.60 perseant lfs_wakeup_cleaner(struct lfs *fs)
675 1.60 perseant {
676 1.60 perseant if (fs->lfs_nowrap > 0)
677 1.60 perseant return;
678 1.60 perseant
679 1.60 perseant wakeup(&fs->lfs_nextseg);
680 1.60 perseant wakeup(&lfs_allclean_wakeup);
681 1.60 perseant }
682