coalesce.c revision 1.1 1 1.1 perseant /* $NetBSD: coalesce.c,v 1.1 2002/06/06 00:56:50 perseant Exp $ */
2 1.1 perseant
3 1.1 perseant /*-
4 1.1 perseant * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 1.1 perseant * All rights reserved.
6 1.1 perseant *
7 1.1 perseant * This code is derived from software contributed to The NetBSD Foundation
8 1.1 perseant * by Konrad E. Schroder <perseant (at) hhhh.org>.
9 1.1 perseant *
10 1.1 perseant * Redistribution and use in source and binary forms, with or without
11 1.1 perseant * modification, are permitted provided that the following conditions
12 1.1 perseant * are met:
13 1.1 perseant * 1. Redistributions of source code must retain the above copyright
14 1.1 perseant * notice, this list of conditions and the following disclaimer.
15 1.1 perseant * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 perseant * notice, this list of conditions and the following disclaimer in the
17 1.1 perseant * documentation and/or other materials provided with the distribution.
18 1.1 perseant * 3. All advertising materials mentioning features or use of this software
19 1.1 perseant * must display the following acknowledgement:
20 1.1 perseant * This product includes software developed by the NetBSD
21 1.1 perseant * Foundation, Inc. and its contributors.
22 1.1 perseant * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 perseant * contributors may be used to endorse or promote products derived
24 1.1 perseant * from this software without specific prior written permission.
25 1.1 perseant *
26 1.1 perseant * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 perseant * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 perseant * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 perseant * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 perseant * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 perseant * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 perseant * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 perseant * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 perseant * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 perseant * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 perseant * POSSIBILITY OF SUCH DAMAGE.
37 1.1 perseant */
38 1.1 perseant
39 1.1 perseant #include <sys/param.h>
40 1.1 perseant #include <sys/mount.h>
41 1.1 perseant #include <sys/time.h>
42 1.1 perseant #include <sys/resource.h>
43 1.1 perseant #include <sys/types.h>
44 1.1 perseant #include <sys/wait.h>
45 1.1 perseant #include <sys/mman.h>
46 1.1 perseant
47 1.1 perseant #include <ufs/ufs/dinode.h>
48 1.1 perseant #include <ufs/lfs/lfs.h>
49 1.1 perseant
50 1.1 perseant #include <fcntl.h>
51 1.1 perseant #include <signal.h>
52 1.1 perseant #include <stdio.h>
53 1.1 perseant #include <stdlib.h>
54 1.1 perseant #include <string.h>
55 1.1 perseant #include <time.h>
56 1.1 perseant #include <unistd.h>
57 1.1 perseant #include <util.h>
58 1.1 perseant #include <errno.h>
59 1.1 perseant #include <err.h>
60 1.1 perseant
61 1.1 perseant #include <syslog.h>
62 1.1 perseant
63 1.1 perseant #include "clean.h"
64 1.1 perseant
65 1.1 perseant int lfs_bmapv(fsid_t *, BLOCK_INFO_15 *, int);
66 1.1 perseant int lfs_markv(fsid_t *, BLOCK_INFO_15 *, int);
67 1.1 perseant
68 1.1 perseant extern int debug;
69 1.1 perseant
70 1.1 perseant static int
71 1.1 perseant tossdead(const void *client, const void *a, const void *b)
72 1.1 perseant {
73 1.1 perseant return (((BLOCK_INFO_15 *)a)->bi_daddr == LFS_UNUSED_DADDR ||
74 1.1 perseant ((BLOCK_INFO_15 *)a)->bi_size == 0);
75 1.1 perseant }
76 1.1 perseant
77 1.1 perseant /*
78 1.1 perseant * Find out if this inode's data blocks are discontinuous; if they are,
79 1.1 perseant * rewrite them using lfs_markv. Return the number of inodes rewritten.
80 1.1 perseant */
81 1.1 perseant int clean_inode(struct fs_info *fsp, ino_t ino)
82 1.1 perseant {
83 1.1 perseant int i, error;
84 1.1 perseant IFILE *ifp;
85 1.1 perseant BLOCK_INFO_15 *bip, *tbip;
86 1.1 perseant struct dinode *dip;
87 1.1 perseant int nb, noff;
88 1.1 perseant ufs_daddr_t toff;
89 1.1 perseant struct lfs *lfsp;
90 1.1 perseant int bps;
91 1.1 perseant SEGUSE *sup;
92 1.1 perseant
93 1.1 perseant lfsp = &fsp->fi_lfs;
94 1.1 perseant
95 1.1 perseant dip = get_dinode(fsp, ino);
96 1.1 perseant if (dip == NULL)
97 1.1 perseant return 0;
98 1.1 perseant
99 1.1 perseant /* Compute file block size, set up for lfs_bmapv */
100 1.1 perseant nb = btofsb(lfsp, dip->di_size);
101 1.1 perseant if (nb > dip->di_blocks) {
102 1.1 perseant syslog(LOG_WARNING, "ino %d, computed blocks %d > held blocks %d",
103 1.1 perseant ino, nb, dip->di_blocks);
104 1.1 perseant return -1;
105 1.1 perseant }
106 1.1 perseant bip = (BLOCK_INFO_15 *)malloc(sizeof(BLOCK_INFO_15) * nb);
107 1.1 perseant if (bip == NULL) {
108 1.1 perseant syslog(LOG_WARNING, "ino %d, %d blocks: %m", ino, nb);
109 1.1 perseant return -1;
110 1.1 perseant }
111 1.1 perseant for (i = 0; i < nb; i++) {
112 1.1 perseant memset(bip + i, 0, sizeof(BLOCK_INFO_15));
113 1.1 perseant bip[i].bi_inode = ino;
114 1.1 perseant bip[i].bi_lbn = i;
115 1.1 perseant bip[i].bi_version = ifp->if_version;
116 1.1 perseant /* Don't set the size, but let lfs_bmap fill it in */
117 1.1 perseant }
118 1.1 perseant if ((error = lfs_bmapv(&fsp->fi_statfsp->f_fsid, bip, nb)) < 0) {
119 1.1 perseant syslog(LOG_WARNING, "lfs_bmapv");
120 1.1 perseant free(bip);
121 1.1 perseant return -1;
122 1.1 perseant }
123 1.1 perseant noff = toff = 0;
124 1.1 perseant for (i = 1; i < nb; i++) {
125 1.1 perseant if (bip[i].bi_daddr != bip[i - 1].bi_daddr + 1)
126 1.1 perseant ++noff;
127 1.1 perseant toff += abs(bip[i].bi_daddr - bip[i - 1].bi_daddr - 1);
128 1.1 perseant }
129 1.1 perseant
130 1.1 perseant /*
131 1.1 perseant * If this file is not discontinuous, there's no point in rewriting it.
132 1.1 perseant *
133 1.1 perseant * Explicitly allow a certain amount of discontinuity, since large
134 1.1 perseant * files will be broken among segments and medium-sized files
135 1.1 perseant * can have a break or two and it's okay.
136 1.1 perseant */
137 1.1 perseant if (nb <= 1 || noff == 0 || (1 << (noff + 1)) < nb ||
138 1.1 perseant segtod(lfsp, noff) * 2< nb) {
139 1.1 perseant free(bip);
140 1.1 perseant return 0;
141 1.1 perseant } else if (debug)
142 1.1 perseant syslog(LOG_DEBUG, "ino %d total discontinuity "
143 1.1 perseant "%d (%d) for %d blocks", ino, noff, toff, nb);
144 1.1 perseant
145 1.1 perseant /* Search for blocks in active segments; don't move them. */
146 1.1 perseant for (i = 0; i < nb; i++) {
147 1.1 perseant if (bip[i].bi_daddr <= 0)
148 1.1 perseant continue;
149 1.1 perseant sup = SEGUSE_ENTRY(lfsp, fsp->fi_segusep,
150 1.1 perseant dtosn(lfsp, bip[i].bi_daddr));
151 1.1 perseant if (sup->su_flags & SEGUSE_ACTIVE)
152 1.1 perseant bip[i].bi_daddr = LFS_UNUSED_DADDR; /* 0 */
153 1.1 perseant }
154 1.1 perseant /*
155 1.1 perseant * Get rid of any we've marked dead. If this is an older
156 1.1 perseant * kernel that doesn't have lfs_bmapv fill in the block
157 1.1 perseant * sizes, we'll toss everything here.
158 1.1 perseant */
159 1.1 perseant toss(bip, &nb, sizeof(BLOCK_INFO_15), tossdead, NULL);
160 1.1 perseant if (nb && tossdead(NULL, bip + nb - 1, NULL))
161 1.1 perseant --nb;
162 1.1 perseant if (nb == 0) {
163 1.1 perseant free(bip);
164 1.1 perseant return 0;
165 1.1 perseant }
166 1.1 perseant
167 1.1 perseant /*
168 1.1 perseant * If we're going to write more blocks than half of the available
169 1.1 perseant * segments, don't do it.
170 1.1 perseant */
171 1.1 perseant if (segtod(lfsp, fsp->fi_cip->clean - 1) / 2 < nb) {
172 1.1 perseant syslog(LOG_WARNING, "not rewriting ino %d, "
173 1.1 perseant "not enough free segments", ino);
174 1.1 perseant free(bip);
175 1.1 perseant return 0;
176 1.1 perseant }
177 1.1 perseant
178 1.1 perseant /*
179 1.1 perseant * We are going to rewrite this inode.
180 1.1 perseant * For any remaining blocks, read in their contents.
181 1.1 perseant */
182 1.1 perseant for (i = 0; i < nb; i++) {
183 1.1 perseant bip[i].bi_bp = malloc(bip[i].bi_size);
184 1.1 perseant get_rawblock(fsp, bip[i].bi_bp, bip[i].bi_size, bip[i].bi_daddr);
185 1.1 perseant }
186 1.1 perseant if (debug)
187 1.1 perseant syslog(LOG_DEBUG, "ino %d markv %d blocks", ino, nb);
188 1.1 perseant
189 1.1 perseant /* Write in segment-sized chunks */
190 1.1 perseant bps = segtod(lfsp, 1);
191 1.1 perseant for (tbip = bip; tbip < bip + nb; tbip += bps) {
192 1.1 perseant lfs_markv(&fsp->fi_statfsp->f_fsid, tbip,
193 1.1 perseant (tbip + bps < bip + nb ? bps : nb % bps));
194 1.1 perseant }
195 1.1 perseant
196 1.1 perseant for (i = 0; i < nb; i++)
197 1.1 perseant if (bip[i].bi_bp)
198 1.1 perseant free(bip[i].bi_bp);
199 1.1 perseant free(bip);
200 1.1 perseant return 1;
201 1.1 perseant }
202 1.1 perseant
203 1.1 perseant /*
204 1.1 perseant * Try coalescing every inode in the filesystem.
205 1.1 perseant * Return the number of inodes actually altered.
206 1.1 perseant */
207 1.1 perseant int clean_all_inodes(struct fs_info *fsp)
208 1.1 perseant {
209 1.1 perseant int i;
210 1.1 perseant int r, tot;
211 1.1 perseant
212 1.1 perseant tot = 0;
213 1.1 perseant for (i = 0; i < fsp->fi_ifile_count; i++) {
214 1.1 perseant r = clean_inode(fsp, i);
215 1.1 perseant if (r > 0)
216 1.1 perseant tot += r;
217 1.1 perseant }
218 1.1 perseant return tot;
219 1.1 perseant }
220 1.1 perseant
221 1.1 perseant int fork_coalesce(struct fs_info *fsp)
222 1.1 perseant {
223 1.1 perseant static pid_t childpid;
224 1.1 perseant
225 1.1 perseant if (childpid) {
226 1.1 perseant if (waitpid(childpid, NULL, WNOHANG) == childpid)
227 1.1 perseant childpid = 0;
228 1.1 perseant }
229 1.1 perseant if (childpid && kill(childpid, 0) >= 0) {
230 1.1 perseant /* already running a coalesce process */
231 1.1 perseant return 0;
232 1.1 perseant }
233 1.1 perseant childpid = fork();
234 1.1 perseant if (childpid < 0) {
235 1.1 perseant syslog(LOG_ERR, "fork: %m");
236 1.1 perseant return 0;
237 1.1 perseant } else if (childpid == 0) {
238 1.1 perseant clean_all_inodes(fsp);
239 1.1 perseant exit(0);
240 1.1 perseant }
241 1.1 perseant return 0;
242 1.1 perseant }
243