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