coalesce.c revision 1.8 1 1.8 perseant /* $NetBSD: coalesce.c,v 1.8 2003/02/25 23:12:08 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.2 perseant extern int debug, do_mmap;
66 1.1 perseant
67 1.1 perseant static int
68 1.1 perseant tossdead(const void *client, const void *a, const void *b)
69 1.1 perseant {
70 1.7 perseant return (((BLOCK_INFO *)a)->bi_daddr <= 0 ||
71 1.7 perseant ((BLOCK_INFO *)a)->bi_size == 0);
72 1.1 perseant }
73 1.1 perseant
74 1.2 perseant static int log2int(int n)
75 1.2 perseant {
76 1.2 perseant int log;
77 1.2 perseant
78 1.2 perseant log = 0;
79 1.2 perseant while (n > 0) {
80 1.2 perseant ++log;
81 1.2 perseant n /= 2;
82 1.2 perseant }
83 1.2 perseant return log - 1;
84 1.2 perseant }
85 1.2 perseant
86 1.3 perseant enum coalesce_returncodes {
87 1.3 perseant COALESCE_OK = 0,
88 1.3 perseant COALESCE_NOINODE,
89 1.3 perseant COALESCE_TOOSMALL,
90 1.3 perseant COALESCE_BADSIZE,
91 1.3 perseant COALESCE_BADBLOCKSIZE,
92 1.3 perseant COALESCE_NOMEM,
93 1.3 perseant COALESCE_BADBMAPV,
94 1.3 perseant COALESCE_NOTWORTHIT,
95 1.3 perseant COALESCE_NOTHINGLEFT,
96 1.3 perseant COALESCE_NOTHINGLEFT2,
97 1.5 yamt COALESCE_EIO,
98 1.3 perseant
99 1.3 perseant COALESCE_MAXERROR
100 1.3 perseant };
101 1.3 perseant
102 1.3 perseant char *coalesce_return[] = {
103 1.3 perseant "Successfully coalesced",
104 1.3 perseant "File not in use or inode not found",
105 1.3 perseant "Not large enough to coalesce",
106 1.3 perseant "Negative size",
107 1.3 perseant "Not enough blocks to account for size",
108 1.3 perseant "Malloc failed",
109 1.8 perseant "LFCNBMAPV failed",
110 1.3 perseant "Not broken enough to fix",
111 1.3 perseant "Too many blocks not found",
112 1.3 perseant "Too many blocks found in active segments",
113 1.5 yamt "I/O error",
114 1.3 perseant
115 1.3 perseant "No such error"
116 1.3 perseant };
117 1.3 perseant
118 1.1 perseant /*
119 1.1 perseant * Find out if this inode's data blocks are discontinuous; if they are,
120 1.7 perseant * rewrite them using markv. Return the number of inodes rewritten.
121 1.1 perseant */
122 1.1 perseant int clean_inode(struct fs_info *fsp, ino_t ino)
123 1.1 perseant {
124 1.1 perseant int i, error;
125 1.7 perseant BLOCK_INFO *bip = NULL, *tbip;
126 1.1 perseant struct dinode *dip;
127 1.2 perseant int nb, onb, noff;
128 1.6 fvdl daddr_t toff;
129 1.1 perseant struct lfs *lfsp;
130 1.1 perseant int bps;
131 1.5 yamt SEGUSE *sup;
132 1.5 yamt int retval;
133 1.1 perseant
134 1.1 perseant lfsp = &fsp->fi_lfs;
135 1.1 perseant
136 1.1 perseant dip = get_dinode(fsp, ino);
137 1.1 perseant if (dip == NULL)
138 1.3 perseant return COALESCE_NOINODE;
139 1.1 perseant
140 1.7 perseant /* Compute file block size, set up for bmapv */
141 1.4 yamt onb = nb = lblkno(lfsp, dip->di_size);
142 1.2 perseant
143 1.2 perseant /* XXX for now, don't do any file small enough to have fragments */
144 1.2 perseant if (nb < NDADDR)
145 1.3 perseant return COALESCE_TOOSMALL;
146 1.2 perseant
147 1.2 perseant /* Sanity checks */
148 1.2 perseant if (dip->di_size < 0) {
149 1.3 perseant if (debug)
150 1.3 perseant syslog(LOG_DEBUG, "ino %d, negative size (%lld)",
151 1.3 perseant ino, (long long)dip->di_size);
152 1.3 perseant return COALESCE_BADSIZE;
153 1.2 perseant }
154 1.1 perseant if (nb > dip->di_blocks) {
155 1.3 perseant if (debug)
156 1.3 perseant syslog(LOG_DEBUG, "ino %d, computed blocks %d > held blocks %d",
157 1.3 perseant ino, nb, dip->di_blocks);
158 1.3 perseant return COALESCE_BADBLOCKSIZE;
159 1.1 perseant }
160 1.2 perseant
161 1.7 perseant bip = (BLOCK_INFO *)malloc(sizeof(BLOCK_INFO) * nb);
162 1.1 perseant if (bip == NULL) {
163 1.1 perseant syslog(LOG_WARNING, "ino %d, %d blocks: %m", ino, nb);
164 1.3 perseant return COALESCE_NOMEM;
165 1.1 perseant }
166 1.1 perseant for (i = 0; i < nb; i++) {
167 1.7 perseant memset(bip + i, 0, sizeof(BLOCK_INFO));
168 1.1 perseant bip[i].bi_inode = ino;
169 1.1 perseant bip[i].bi_lbn = i;
170 1.2 perseant bip[i].bi_version = dip->di_gen;
171 1.1 perseant /* Don't set the size, but let lfs_bmap fill it in */
172 1.1 perseant }
173 1.7 perseant if ((error = lfs_bmapv_emul(ifile_fd, bip, nb)) < 0) {
174 1.8 perseant syslog(LOG_WARNING, "LFCNBMAPV: %m");
175 1.5 yamt retval = COALESCE_BADBMAPV;
176 1.5 yamt goto out;
177 1.5 yamt }
178 1.5 yamt #if 0
179 1.5 yamt for (i = 0; i < nb; i++) {
180 1.5 yamt printf("bi_size = %d, bi_ino = %d, "
181 1.5 yamt "bi_lbn = %d, bi_daddr = %d\n",
182 1.5 yamt bip[i].bi_size, bip[i].bi_inode, bip[i].bi_lbn,
183 1.5 yamt bip[i].bi_daddr);
184 1.1 perseant }
185 1.5 yamt #endif
186 1.1 perseant noff = toff = 0;
187 1.1 perseant for (i = 1; i < nb; i++) {
188 1.4 yamt if (bip[i].bi_daddr != bip[i - 1].bi_daddr + lfsp->lfs_frag)
189 1.1 perseant ++noff;
190 1.4 yamt toff += abs(bip[i].bi_daddr - bip[i - 1].bi_daddr
191 1.4 yamt - lfsp->lfs_frag) >> lfsp->lfs_fbshift;
192 1.1 perseant }
193 1.1 perseant
194 1.1 perseant /*
195 1.1 perseant * If this file is not discontinuous, there's no point in rewriting it.
196 1.1 perseant *
197 1.1 perseant * Explicitly allow a certain amount of discontinuity, since large
198 1.1 perseant * files will be broken among segments and medium-sized files
199 1.1 perseant * can have a break or two and it's okay.
200 1.1 perseant */
201 1.2 perseant if (nb <= 1 || noff == 0 || noff < log2int(nb) ||
202 1.2 perseant segtod(lfsp, noff) * 2 < nb) {
203 1.5 yamt retval = COALESCE_NOTWORTHIT;
204 1.5 yamt goto out;
205 1.1 perseant } else if (debug)
206 1.1 perseant syslog(LOG_DEBUG, "ino %d total discontinuity "
207 1.6 fvdl "%d (%lld) for %d blocks", ino, noff,
208 1.6 fvdl (long long)toff, nb);
209 1.1 perseant
210 1.1 perseant /* Search for blocks in active segments; don't move them. */
211 1.1 perseant for (i = 0; i < nb; i++) {
212 1.1 perseant if (bip[i].bi_daddr <= 0)
213 1.1 perseant continue;
214 1.1 perseant sup = SEGUSE_ENTRY(lfsp, fsp->fi_segusep,
215 1.1 perseant dtosn(lfsp, bip[i].bi_daddr));
216 1.1 perseant if (sup->su_flags & SEGUSE_ACTIVE)
217 1.1 perseant bip[i].bi_daddr = LFS_UNUSED_DADDR; /* 0 */
218 1.1 perseant }
219 1.1 perseant /*
220 1.1 perseant * Get rid of any we've marked dead. If this is an older
221 1.7 perseant * kernel that doesn't have bmapv fill in the block
222 1.1 perseant * sizes, we'll toss everything here.
223 1.1 perseant */
224 1.7 perseant toss(bip, &nb, sizeof(BLOCK_INFO), tossdead, NULL);
225 1.1 perseant if (nb && tossdead(NULL, bip + nb - 1, NULL))
226 1.1 perseant --nb;
227 1.1 perseant if (nb == 0) {
228 1.5 yamt retval = COALESCE_NOTHINGLEFT;
229 1.5 yamt goto out;
230 1.2 perseant }
231 1.2 perseant
232 1.1 perseant /*
233 1.2 perseant * We may have tossed enough blocks that it is no longer worthwhile
234 1.2 perseant * to rewrite this inode.
235 1.1 perseant */
236 1.3 perseant if (onb - nb > log2int(onb)) {
237 1.3 perseant if (debug)
238 1.3 perseant syslog(LOG_DEBUG, "too many blocks tossed, not rewriting");
239 1.3 perseant return COALESCE_NOTHINGLEFT2;
240 1.1 perseant }
241 1.1 perseant
242 1.1 perseant /*
243 1.1 perseant * We are going to rewrite this inode.
244 1.1 perseant * For any remaining blocks, read in their contents.
245 1.1 perseant */
246 1.1 perseant for (i = 0; i < nb; i++) {
247 1.1 perseant bip[i].bi_bp = malloc(bip[i].bi_size);
248 1.5 yamt if (bip[i].bi_bp == NULL) {
249 1.5 yamt syslog(LOG_WARNING, "allocate block buffer size=%d: %m",
250 1.5 yamt bip[i].bi_size);
251 1.5 yamt retval = COALESCE_NOMEM;
252 1.5 yamt goto out;
253 1.5 yamt }
254 1.5 yamt if (get_rawblock(fsp, bip[i].bi_bp, bip[i].bi_size,
255 1.5 yamt bip[i].bi_daddr) != bip[i].bi_size) {
256 1.5 yamt retval = COALESCE_EIO;
257 1.5 yamt goto out;
258 1.5 yamt }
259 1.1 perseant }
260 1.1 perseant if (debug)
261 1.1 perseant syslog(LOG_DEBUG, "ino %d markv %d blocks", ino, nb);
262 1.1 perseant
263 1.2 perseant /*
264 1.2 perseant * Write in segment-sized chunks. If at any point we'd write more
265 1.2 perseant * than half of the available segments, sleep until that's not
266 1.2 perseant * true any more.
267 1.2 perseant */
268 1.1 perseant bps = segtod(lfsp, 1);
269 1.1 perseant for (tbip = bip; tbip < bip + nb; tbip += bps) {
270 1.2 perseant while (fsp->fi_cip->clean < 4) {
271 1.7 perseant lfs_segwait_emul(ifile_fd, NULL);
272 1.2 perseant reread_fs_info(fsp, do_mmap);
273 1.2 perseant /* XXX start over? */
274 1.2 perseant }
275 1.7 perseant lfs_markv_emul(ifile_fd, tbip,
276 1.1 perseant (tbip + bps < bip + nb ? bps : nb % bps));
277 1.1 perseant }
278 1.1 perseant
279 1.5 yamt retval = COALESCE_OK;
280 1.5 yamt out:
281 1.5 yamt if (bip) {
282 1.5 yamt for (i = 0; i < onb; i++)
283 1.5 yamt if (bip[i].bi_bp)
284 1.5 yamt free(bip[i].bi_bp);
285 1.5 yamt free(bip);
286 1.5 yamt }
287 1.5 yamt return retval;
288 1.1 perseant }
289 1.1 perseant
290 1.1 perseant /*
291 1.1 perseant * Try coalescing every inode in the filesystem.
292 1.1 perseant * Return the number of inodes actually altered.
293 1.1 perseant */
294 1.1 perseant int clean_all_inodes(struct fs_info *fsp)
295 1.1 perseant {
296 1.3 perseant int i, r;
297 1.3 perseant int totals[COALESCE_MAXERROR];
298 1.1 perseant
299 1.3 perseant memset(totals, 0, sizeof(totals));
300 1.1 perseant for (i = 0; i < fsp->fi_ifile_count; i++) {
301 1.1 perseant r = clean_inode(fsp, i);
302 1.3 perseant ++totals[r];
303 1.1 perseant }
304 1.3 perseant
305 1.3 perseant for (i = 0; i < COALESCE_MAXERROR; i++)
306 1.3 perseant if (totals[i])
307 1.3 perseant syslog(LOG_DEBUG, "%s: %d", coalesce_return[i],
308 1.3 perseant totals[i]);
309 1.3 perseant
310 1.3 perseant return totals[COALESCE_OK];
311 1.1 perseant }
312 1.1 perseant
313 1.1 perseant int fork_coalesce(struct fs_info *fsp)
314 1.1 perseant {
315 1.1 perseant static pid_t childpid;
316 1.2 perseant int num;
317 1.2 perseant
318 1.2 perseant reread_fs_info(fsp, do_mmap);
319 1.1 perseant
320 1.1 perseant if (childpid) {
321 1.1 perseant if (waitpid(childpid, NULL, WNOHANG) == childpid)
322 1.1 perseant childpid = 0;
323 1.1 perseant }
324 1.1 perseant if (childpid && kill(childpid, 0) >= 0) {
325 1.1 perseant /* already running a coalesce process */
326 1.2 perseant if (debug)
327 1.2 perseant syslog(LOG_DEBUG, "coalescing already in progress");
328 1.1 perseant return 0;
329 1.1 perseant }
330 1.1 perseant childpid = fork();
331 1.1 perseant if (childpid < 0) {
332 1.1 perseant syslog(LOG_ERR, "fork: %m");
333 1.1 perseant return 0;
334 1.1 perseant } else if (childpid == 0) {
335 1.3 perseant syslog(LOG_NOTICE, "new coalescing process, pid %d", getpid());
336 1.2 perseant num = clean_all_inodes(fsp);
337 1.2 perseant syslog(LOG_NOTICE, "coalesced %d discontiguous inodes", num);
338 1.1 perseant exit(0);
339 1.1 perseant }
340 1.1 perseant return 0;
341 1.1 perseant }
342