coalesce.c revision 1.27 1 1.27 dholland /* $NetBSD: coalesce.c,v 1.27 2015/07/28 05:09:34 dholland Exp $ */
2 1.1 perseant
3 1.1 perseant /*-
4 1.11 perseant * Copyright (c) 2002, 2005 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 *
19 1.1 perseant * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 perseant * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 perseant * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 perseant * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 perseant * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 perseant * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 perseant * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 perseant * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 perseant * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 perseant * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 perseant * POSSIBILITY OF SUCH DAMAGE.
30 1.1 perseant */
31 1.1 perseant
32 1.1 perseant #include <sys/param.h>
33 1.1 perseant #include <sys/mount.h>
34 1.1 perseant #include <sys/time.h>
35 1.1 perseant #include <sys/resource.h>
36 1.1 perseant #include <sys/types.h>
37 1.1 perseant #include <sys/wait.h>
38 1.1 perseant #include <sys/mman.h>
39 1.1 perseant
40 1.1 perseant #include <ufs/lfs/lfs.h>
41 1.27 dholland #include <ufs/lfs/lfs_accessors.h>
42 1.1 perseant
43 1.1 perseant #include <fcntl.h>
44 1.1 perseant #include <signal.h>
45 1.1 perseant #include <stdio.h>
46 1.1 perseant #include <stdlib.h>
47 1.1 perseant #include <string.h>
48 1.1 perseant #include <time.h>
49 1.1 perseant #include <unistd.h>
50 1.1 perseant #include <util.h>
51 1.1 perseant #include <errno.h>
52 1.1 perseant #include <err.h>
53 1.26 dholland #include <assert.h>
54 1.1 perseant
55 1.1 perseant #include <syslog.h>
56 1.1 perseant
57 1.11 perseant #include "bufcache.h"
58 1.11 perseant #include "vnode.h"
59 1.11 perseant #include "cleaner.h"
60 1.18 pooka #include "kernelops.h"
61 1.1 perseant
62 1.2 perseant extern int debug, do_mmap;
63 1.1 perseant
64 1.11 perseant int log2int(int n)
65 1.2 perseant {
66 1.2 perseant int log;
67 1.2 perseant
68 1.2 perseant log = 0;
69 1.2 perseant while (n > 0) {
70 1.2 perseant ++log;
71 1.11 perseant n >>= 1;
72 1.2 perseant }
73 1.2 perseant return log - 1;
74 1.2 perseant }
75 1.2 perseant
76 1.3 perseant enum coalesce_returncodes {
77 1.3 perseant COALESCE_OK = 0,
78 1.3 perseant COALESCE_NOINODE,
79 1.3 perseant COALESCE_TOOSMALL,
80 1.3 perseant COALESCE_BADSIZE,
81 1.3 perseant COALESCE_BADBLOCKSIZE,
82 1.3 perseant COALESCE_NOMEM,
83 1.3 perseant COALESCE_BADBMAPV,
84 1.11 perseant COALESCE_BADMARKV,
85 1.3 perseant COALESCE_NOTWORTHIT,
86 1.3 perseant COALESCE_NOTHINGLEFT,
87 1.5 yamt COALESCE_EIO,
88 1.3 perseant
89 1.3 perseant COALESCE_MAXERROR
90 1.3 perseant };
91 1.3 perseant
92 1.17 lukem const char *coalesce_return[] = {
93 1.3 perseant "Successfully coalesced",
94 1.3 perseant "File not in use or inode not found",
95 1.3 perseant "Not large enough to coalesce",
96 1.3 perseant "Negative size",
97 1.3 perseant "Not enough blocks to account for size",
98 1.3 perseant "Malloc failed",
99 1.8 perseant "LFCNBMAPV failed",
100 1.3 perseant "Not broken enough to fix",
101 1.3 perseant "Too many blocks not found",
102 1.3 perseant "Too many blocks found in active segments",
103 1.5 yamt "I/O error",
104 1.3 perseant
105 1.3 perseant "No such error"
106 1.3 perseant };
107 1.3 perseant
108 1.21 dholland static struct ulfs1_dinode *
109 1.11 perseant get_dinode(struct clfs *fs, ino_t ino)
110 1.11 perseant {
111 1.11 perseant IFILE *ifp;
112 1.11 perseant daddr_t daddr;
113 1.11 perseant struct ubuf *bp;
114 1.21 dholland struct ulfs1_dinode *dip, *r;
115 1.11 perseant
116 1.11 perseant lfs_ientry(&ifp, fs, ino, &bp);
117 1.11 perseant daddr = ifp->if_daddr;
118 1.14 ad brelse(bp, 0);
119 1.11 perseant
120 1.11 perseant if (daddr == 0x0)
121 1.11 perseant return NULL;
122 1.11 perseant
123 1.26 dholland bread(fs->clfs_devvp, daddr, lfs_sb_getibsize(fs), 0, &bp);
124 1.21 dholland for (dip = (struct ulfs1_dinode *)bp->b_data;
125 1.26 dholland dip < (struct ulfs1_dinode *)(bp->b_data + lfs_sb_getibsize(fs)); dip++)
126 1.11 perseant if (dip->di_inumber == ino) {
127 1.21 dholland r = (struct ulfs1_dinode *)malloc(sizeof(*r));
128 1.19 perseant if (r == NULL)
129 1.19 perseant break;
130 1.11 perseant memcpy(r, dip, sizeof(*r));
131 1.14 ad brelse(bp, 0);
132 1.11 perseant return r;
133 1.11 perseant }
134 1.14 ad brelse(bp, 0);
135 1.11 perseant return NULL;
136 1.11 perseant }
137 1.11 perseant
138 1.1 perseant /*
139 1.1 perseant * Find out if this inode's data blocks are discontinuous; if they are,
140 1.7 perseant * rewrite them using markv. Return the number of inodes rewritten.
141 1.1 perseant */
142 1.11 perseant static int
143 1.11 perseant clean_inode(struct clfs *fs, ino_t ino)
144 1.1 perseant {
145 1.7 perseant BLOCK_INFO *bip = NULL, *tbip;
146 1.11 perseant CLEANERINFO cip;
147 1.11 perseant struct ubuf *bp;
148 1.21 dholland struct ulfs1_dinode *dip;
149 1.11 perseant struct clfs_seguse *sup;
150 1.11 perseant struct lfs_fcntl_markv /* {
151 1.11 perseant BLOCK_INFO *blkiov;
152 1.11 perseant int blkcnt;
153 1.11 perseant } */ lim;
154 1.11 perseant daddr_t toff;
155 1.11 perseant int i;
156 1.2 perseant int nb, onb, noff;
157 1.11 perseant int retval;
158 1.1 perseant int bps;
159 1.1 perseant
160 1.11 perseant dip = get_dinode(fs, ino);
161 1.1 perseant if (dip == NULL)
162 1.3 perseant return COALESCE_NOINODE;
163 1.1 perseant
164 1.7 perseant /* Compute file block size, set up for bmapv */
165 1.23 christos onb = nb = lfs_lblkno(fs, dip->di_size);
166 1.2 perseant
167 1.2 perseant /* XXX for now, don't do any file small enough to have fragments */
168 1.21 dholland if (nb < ULFS_NDADDR) {
169 1.12 christos free(dip);
170 1.3 perseant return COALESCE_TOOSMALL;
171 1.12 christos }
172 1.2 perseant
173 1.2 perseant /* Sanity checks */
174 1.17 lukem #if 0 /* di_size is uint64_t -- this is a noop */
175 1.2 perseant if (dip->di_size < 0) {
176 1.11 perseant dlog("ino %d, negative size (%" PRId64 ")", ino, dip->di_size);
177 1.12 christos free(dip);
178 1.3 perseant return COALESCE_BADSIZE;
179 1.2 perseant }
180 1.17 lukem #endif
181 1.1 perseant if (nb > dip->di_blocks) {
182 1.11 perseant dlog("ino %d, computed blocks %d > held blocks %d", ino, nb,
183 1.11 perseant dip->di_blocks);
184 1.12 christos free(dip);
185 1.3 perseant return COALESCE_BADBLOCKSIZE;
186 1.1 perseant }
187 1.2 perseant
188 1.7 perseant bip = (BLOCK_INFO *)malloc(sizeof(BLOCK_INFO) * nb);
189 1.1 perseant if (bip == NULL) {
190 1.10 christos syslog(LOG_WARNING, "ino %llu, %d blocks: %m",
191 1.10 christos (unsigned long long)ino, nb);
192 1.12 christos free(dip);
193 1.3 perseant return COALESCE_NOMEM;
194 1.1 perseant }
195 1.1 perseant for (i = 0; i < nb; i++) {
196 1.7 perseant memset(bip + i, 0, sizeof(BLOCK_INFO));
197 1.1 perseant bip[i].bi_inode = ino;
198 1.1 perseant bip[i].bi_lbn = i;
199 1.2 perseant bip[i].bi_version = dip->di_gen;
200 1.1 perseant /* Don't set the size, but let lfs_bmap fill it in */
201 1.1 perseant }
202 1.11 perseant lim.blkiov = bip;
203 1.11 perseant lim.blkcnt = nb;
204 1.18 pooka if (kops.ko_fcntl(fs->clfs_ifilefd, LFCNBMAPV, &lim) < 0) {
205 1.11 perseant syslog(LOG_WARNING, "%s: coalesce: LFCNBMAPV: %m",
206 1.26 dholland lfs_sb_getfsmnt(fs));
207 1.5 yamt retval = COALESCE_BADBMAPV;
208 1.5 yamt goto out;
209 1.5 yamt }
210 1.5 yamt #if 0
211 1.5 yamt for (i = 0; i < nb; i++) {
212 1.5 yamt printf("bi_size = %d, bi_ino = %d, "
213 1.5 yamt "bi_lbn = %d, bi_daddr = %d\n",
214 1.5 yamt bip[i].bi_size, bip[i].bi_inode, bip[i].bi_lbn,
215 1.5 yamt bip[i].bi_daddr);
216 1.1 perseant }
217 1.5 yamt #endif
218 1.1 perseant noff = toff = 0;
219 1.1 perseant for (i = 1; i < nb; i++) {
220 1.25 dholland if (bip[i].bi_daddr != bip[i - 1].bi_daddr + clfs_sb_getfrag(fs))
221 1.1 perseant ++noff;
222 1.4 yamt toff += abs(bip[i].bi_daddr - bip[i - 1].bi_daddr
223 1.26 dholland - clfs_sb_getfrag(fs)) >> lfs_sb_getfbshift(fs);
224 1.1 perseant }
225 1.1 perseant
226 1.1 perseant /*
227 1.1 perseant * If this file is not discontinuous, there's no point in rewriting it.
228 1.11 perseant *
229 1.11 perseant * Explicitly allow a certain amount of discontinuity, since large
230 1.11 perseant * files will be broken among segments and medium-sized files
231 1.11 perseant * can have a break or two and it's okay.
232 1.1 perseant */
233 1.2 perseant if (nb <= 1 || noff == 0 || noff < log2int(nb) ||
234 1.23 christos lfs_segtod(fs, noff) * 2 < nb) {
235 1.5 yamt retval = COALESCE_NOTWORTHIT;
236 1.5 yamt goto out;
237 1.1 perseant } else if (debug)
238 1.10 christos syslog(LOG_DEBUG, "ino %llu total discontinuity "
239 1.10 christos "%d (%lld) for %d blocks", (unsigned long long)ino,
240 1.10 christos noff, (long long)toff, nb);
241 1.1 perseant
242 1.1 perseant /* Search for blocks in active segments; don't move them. */
243 1.1 perseant for (i = 0; i < nb; i++) {
244 1.1 perseant if (bip[i].bi_daddr <= 0)
245 1.1 perseant continue;
246 1.23 christos sup = &fs->clfs_segtab[lfs_dtosn(fs, bip[i].bi_daddr)];
247 1.11 perseant if (sup->flags & SEGUSE_ACTIVE)
248 1.1 perseant bip[i].bi_daddr = LFS_UNUSED_DADDR; /* 0 */
249 1.1 perseant }
250 1.11 perseant
251 1.11 perseant /*
252 1.11 perseant * Get rid of any blocks we've marked dead. If this is an older
253 1.11 perseant * kernel that doesn't have bmapv fill in the block sizes, we'll
254 1.11 perseant * toss everything here.
255 1.1 perseant */
256 1.11 perseant onb = nb;
257 1.13 perseant toss_old_blocks(fs, &bip, &nb, NULL);
258 1.11 perseant nb = i;
259 1.2 perseant
260 1.1 perseant /*
261 1.2 perseant * We may have tossed enough blocks that it is no longer worthwhile
262 1.2 perseant * to rewrite this inode.
263 1.1 perseant */
264 1.11 perseant if (nb == 0 || onb - nb > log2int(onb)) {
265 1.3 perseant if (debug)
266 1.3 perseant syslog(LOG_DEBUG, "too many blocks tossed, not rewriting");
267 1.11 perseant retval = COALESCE_NOTHINGLEFT;
268 1.11 perseant goto out;
269 1.1 perseant }
270 1.1 perseant
271 1.11 perseant /*
272 1.1 perseant * We are going to rewrite this inode.
273 1.1 perseant * For any remaining blocks, read in their contents.
274 1.1 perseant */
275 1.1 perseant for (i = 0; i < nb; i++) {
276 1.1 perseant bip[i].bi_bp = malloc(bip[i].bi_size);
277 1.5 yamt if (bip[i].bi_bp == NULL) {
278 1.5 yamt syslog(LOG_WARNING, "allocate block buffer size=%d: %m",
279 1.5 yamt bip[i].bi_size);
280 1.5 yamt retval = COALESCE_NOMEM;
281 1.5 yamt goto out;
282 1.5 yamt }
283 1.11 perseant
284 1.18 pooka if (kops.ko_pread(fs->clfs_devfd, bip[i].bi_bp, bip[i].bi_size,
285 1.23 christos lfs_fsbtob(fs, bip[i].bi_daddr)) < 0) {
286 1.5 yamt retval = COALESCE_EIO;
287 1.5 yamt goto out;
288 1.5 yamt }
289 1.1 perseant }
290 1.1 perseant if (debug)
291 1.10 christos syslog(LOG_DEBUG, "ino %llu markv %d blocks",
292 1.10 christos (unsigned long long)ino, nb);
293 1.1 perseant
294 1.2 perseant /*
295 1.2 perseant * Write in segment-sized chunks. If at any point we'd write more
296 1.2 perseant * than half of the available segments, sleep until that's not
297 1.2 perseant * true any more.
298 1.2 perseant */
299 1.23 christos bps = lfs_segtod(fs, 1);
300 1.1 perseant for (tbip = bip; tbip < bip + nb; tbip += bps) {
301 1.11 perseant do {
302 1.25 dholland bread(fs->lfs_ivnode, 0, clfs_sb_getbsize(fs), 0, &bp);
303 1.11 perseant cip = *(CLEANERINFO *)bp->b_data;
304 1.14 ad brelse(bp, B_INVAL);
305 1.11 perseant
306 1.11 perseant if (cip.clean < 4) /* XXX magic number 4 */
307 1.18 pooka kops.ko_fcntl(fs->clfs_ifilefd,
308 1.18 pooka LFCNSEGWAIT, NULL);
309 1.11 perseant } while(cip.clean < 4);
310 1.11 perseant
311 1.11 perseant lim.blkiov = tbip;
312 1.11 perseant lim.blkcnt = (tbip + bps < bip + nb ? bps : nb % bps);
313 1.18 pooka if (kops.ko_fcntl(fs->clfs_ifilefd, LFCNMARKV, &lim) < 0) {
314 1.11 perseant retval = COALESCE_BADMARKV;
315 1.11 perseant goto out;
316 1.2 perseant }
317 1.1 perseant }
318 1.1 perseant
319 1.5 yamt retval = COALESCE_OK;
320 1.5 yamt out:
321 1.11 perseant free(dip);
322 1.5 yamt if (bip) {
323 1.5 yamt for (i = 0; i < onb; i++)
324 1.5 yamt if (bip[i].bi_bp)
325 1.5 yamt free(bip[i].bi_bp);
326 1.5 yamt free(bip);
327 1.5 yamt }
328 1.5 yamt return retval;
329 1.1 perseant }
330 1.1 perseant
331 1.1 perseant /*
332 1.1 perseant * Try coalescing every inode in the filesystem.
333 1.1 perseant * Return the number of inodes actually altered.
334 1.1 perseant */
335 1.11 perseant int clean_all_inodes(struct clfs *fs)
336 1.1 perseant {
337 1.11 perseant int i, r, maxino;
338 1.3 perseant int totals[COALESCE_MAXERROR];
339 1.11 perseant struct stat st;
340 1.1 perseant
341 1.3 perseant memset(totals, 0, sizeof(totals));
342 1.11 perseant
343 1.11 perseant fstat(fs->clfs_ifilefd, &st);
344 1.26 dholland maxino = lfs_sb_getifpb(fs) * (st.st_size >> lfs_sb_getbshift(fs)) -
345 1.25 dholland lfs_sb_getsegtabsz(fs) - lfs_sb_getcleansz(fs);
346 1.11 perseant
347 1.11 perseant for (i = 0; i < maxino; i++) {
348 1.11 perseant r = clean_inode(fs, i);
349 1.3 perseant ++totals[r];
350 1.1 perseant }
351 1.3 perseant
352 1.3 perseant for (i = 0; i < COALESCE_MAXERROR; i++)
353 1.3 perseant if (totals[i])
354 1.3 perseant syslog(LOG_DEBUG, "%s: %d", coalesce_return[i],
355 1.11 perseant totals[i]);
356 1.11 perseant
357 1.3 perseant return totals[COALESCE_OK];
358 1.1 perseant }
359 1.1 perseant
360 1.11 perseant /*
361 1.11 perseant * Fork a child process to coalesce this fs.
362 1.11 perseant */
363 1.11 perseant int
364 1.11 perseant fork_coalesce(struct clfs *fs)
365 1.1 perseant {
366 1.1 perseant static pid_t childpid;
367 1.2 perseant int num;
368 1.2 perseant
369 1.11 perseant /*
370 1.11 perseant * If already running a coalescing child, don't start a new one.
371 1.11 perseant */
372 1.1 perseant if (childpid) {
373 1.11 perseant if (waitpid(childpid, NULL, WNOHANG) == childpid)
374 1.1 perseant childpid = 0;
375 1.1 perseant }
376 1.1 perseant if (childpid && kill(childpid, 0) >= 0) {
377 1.1 perseant /* already running a coalesce process */
378 1.2 perseant if (debug)
379 1.2 perseant syslog(LOG_DEBUG, "coalescing already in progress");
380 1.1 perseant return 0;
381 1.1 perseant }
382 1.11 perseant
383 1.11 perseant /*
384 1.11 perseant * Fork a child and let the child coalease
385 1.11 perseant */
386 1.1 perseant childpid = fork();
387 1.1 perseant if (childpid < 0) {
388 1.26 dholland syslog(LOG_ERR, "%s: fork to coaleasce: %m", lfs_sb_getfsmnt(fs));
389 1.1 perseant return 0;
390 1.1 perseant } else if (childpid == 0) {
391 1.11 perseant syslog(LOG_NOTICE, "%s: new coalescing process, pid %d",
392 1.26 dholland lfs_sb_getfsmnt(fs), getpid());
393 1.11 perseant num = clean_all_inodes(fs);
394 1.11 perseant syslog(LOG_NOTICE, "%s: coalesced %d discontiguous inodes",
395 1.26 dholland lfs_sb_getfsmnt(fs), num);
396 1.1 perseant exit(0);
397 1.1 perseant }
398 1.11 perseant
399 1.1 perseant return 0;
400 1.1 perseant }
401