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