lfs_cleanerd.c revision 1.13 1 1.13 ad /* $NetBSD: lfs_cleanerd.c,v 1.13 2007/10/08 21:41:13 ad Exp $ */
2 1.1 perseant
3 1.1 perseant /*-
4 1.1 perseant * Copyright (c) 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 * 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 /*
40 1.1 perseant * The cleaner daemon for the NetBSD Log-structured File System.
41 1.1 perseant * Only tested for use with version 2 LFSs.
42 1.1 perseant */
43 1.1 perseant
44 1.1 perseant #include <sys/syslog.h>
45 1.1 perseant #include <sys/param.h>
46 1.1 perseant #include <sys/mount.h>
47 1.1 perseant #include <sys/stat.h>
48 1.1 perseant #include <ufs/ufs/inode.h>
49 1.1 perseant #include <ufs/lfs/lfs.h>
50 1.1 perseant
51 1.1 perseant #include <assert.h>
52 1.1 perseant #include <err.h>
53 1.1 perseant #include <errno.h>
54 1.1 perseant #include <fcntl.h>
55 1.1 perseant #include <stdio.h>
56 1.1 perseant #include <stdlib.h>
57 1.1 perseant #include <string.h>
58 1.1 perseant #include <unistd.h>
59 1.1 perseant #include <time.h>
60 1.1 perseant #include <util.h>
61 1.1 perseant
62 1.1 perseant #include "bufcache.h"
63 1.1 perseant #include "vnode.h"
64 1.1 perseant #include "lfs_user.h"
65 1.1 perseant #include "fdfs.h"
66 1.1 perseant #include "cleaner.h"
67 1.1 perseant
68 1.1 perseant /*
69 1.1 perseant * Global variables.
70 1.1 perseant */
71 1.1 perseant /* XXX these top few should really be fs-specific */
72 1.1 perseant int use_fs_idle; /* Use fs idle rather than cpu idle time */
73 1.1 perseant int use_bytes; /* Use bytes written rather than segments cleaned */
74 1.1 perseant int load_threshold; /* How idle is idle (CPU idle) */
75 1.1 perseant int atatime; /* How many segments (bytes) to clean at a time */
76 1.1 perseant
77 1.1 perseant int nfss; /* Number of filesystems monitored by this cleanerd */
78 1.1 perseant struct clfs **fsp; /* Array of extended filesystem structures */
79 1.1 perseant int segwait_timeout; /* Time to wait in lfs_segwait() */
80 1.1 perseant int do_quit; /* Quit after one cleaning loop */
81 1.1 perseant int do_coalesce; /* Coalesce filesystem */
82 1.1 perseant int do_small; /* Use small writes through markv */
83 1.1 perseant char *copylog_filename; /* File to use for fs debugging analysis */
84 1.1 perseant int inval_segment; /* Segment to invalidate */
85 1.1 perseant int stat_report; /* Report statistics for this period of cycles */
86 1.1 perseant int debug; /* Turn on debugging */
87 1.1 perseant struct cleaner_stats {
88 1.1 perseant double util_tot;
89 1.1 perseant double util_sos;
90 1.1 perseant off_t bytes_read;
91 1.1 perseant off_t bytes_written;
92 1.1 perseant off_t segs_cleaned;
93 1.1 perseant off_t segs_empty;
94 1.1 perseant off_t segs_error;
95 1.1 perseant } cleaner_stats;
96 1.1 perseant
97 1.1 perseant extern u_int32_t cksum(void *, size_t);
98 1.1 perseant extern u_int32_t lfs_sb_cksum(struct dlfs *);
99 1.1 perseant extern u_int32_t lfs_cksum_part(void *, size_t, u_int32_t);
100 1.1 perseant extern int ufs_getlbns(struct lfs *, struct uvnode *, daddr_t, struct indir *, int *);
101 1.1 perseant
102 1.1 perseant /* Compat */
103 1.1 perseant void pwarn(const char *unused, ...) { /* Does nothing */ };
104 1.1 perseant
105 1.1 perseant /*
106 1.1 perseant * Log a message if debugging is turned on.
107 1.1 perseant */
108 1.1 perseant void
109 1.1 perseant dlog(char *fmt, ...)
110 1.1 perseant {
111 1.1 perseant va_list ap;
112 1.1 perseant
113 1.1 perseant if (debug == 0)
114 1.1 perseant return;
115 1.1 perseant
116 1.1 perseant va_start(ap, fmt);
117 1.1 perseant vsyslog(LOG_DEBUG, fmt, ap);
118 1.1 perseant va_end(ap);
119 1.1 perseant }
120 1.1 perseant
121 1.1 perseant /*
122 1.1 perseant * Remove the specified filesystem from the list, due to its having
123 1.1 perseant * become unmounted or other error condition.
124 1.1 perseant */
125 1.1 perseant void
126 1.1 perseant handle_error(struct clfs **fsp, int n)
127 1.1 perseant {
128 1.1 perseant syslog(LOG_NOTICE, "%s: detaching cleaner", fsp[n]->lfs_fsmnt);
129 1.1 perseant free(fsp[n]);
130 1.1 perseant if (n != nfss - 1)
131 1.1 perseant fsp[n] = fsp[nfss - 1];
132 1.1 perseant --nfss;
133 1.1 perseant }
134 1.1 perseant
135 1.1 perseant /*
136 1.1 perseant * Reinitialize a filesystem if, e.g., its size changed.
137 1.1 perseant */
138 1.1 perseant int
139 1.1 perseant reinit_fs(struct clfs *fs)
140 1.1 perseant {
141 1.1 perseant char fsname[MNAMELEN];
142 1.1 perseant
143 1.6 mrg strncpy(fsname, (char *)fs->lfs_fsmnt, MNAMELEN);
144 1.1 perseant close(fs->clfs_ifilefd);
145 1.1 perseant close(fs->clfs_devfd);
146 1.1 perseant fd_reclaim(fs->clfs_devvp);
147 1.1 perseant fd_reclaim(fs->lfs_ivnode);
148 1.1 perseant free(fs->clfs_dev);
149 1.1 perseant free(fs->clfs_segtab);
150 1.1 perseant free(fs->clfs_segtabp);
151 1.1 perseant
152 1.1 perseant return init_fs(fs, fsname);
153 1.1 perseant }
154 1.1 perseant
155 1.1 perseant #ifdef REPAIR_ZERO_FINFO
156 1.1 perseant /*
157 1.1 perseant * Use fsck's lfs routines to load the Ifile from an unmounted fs.
158 1.1 perseant * We interpret "fsname" as the name of the raw disk device.
159 1.1 perseant */
160 1.1 perseant int
161 1.1 perseant init_unmounted_fs(struct clfs *fs, char *fsname)
162 1.1 perseant {
163 1.1 perseant struct lfs *disc_fs;
164 1.1 perseant int i;
165 1.1 perseant
166 1.1 perseant fs->clfs_dev = fsname;
167 1.1 perseant if ((fs->clfs_devfd = open(fs->clfs_dev, O_RDWR)) < 0) {
168 1.1 perseant syslog(LOG_ERR, "couldn't open device %s read/write",
169 1.1 perseant fs->clfs_dev);
170 1.1 perseant return -1;
171 1.1 perseant }
172 1.1 perseant
173 1.1 perseant disc_fs = lfs_init(fs->clfs_devfd, 0, 0, 0, 0);
174 1.1 perseant
175 1.1 perseant fs->lfs_dlfs = disc_fs->lfs_dlfs; /* Structure copy */
176 1.1 perseant strncpy(fs->lfs_fsmnt, fsname, MNAMELEN);
177 1.1 perseant fs->lfs_ivnode = (struct uvnode *)disc_fs->lfs_ivnode;
178 1.1 perseant fs->clfs_devvp = fd_vget(fs->clfs_devfd, fs->lfs_fsize, fs->lfs_ssize,
179 1.1 perseant atatime);
180 1.1 perseant
181 1.1 perseant /* Allocate and clear segtab */
182 1.1 perseant fs->clfs_segtab = (struct clfs_seguse *)malloc(fs->lfs_nseg *
183 1.1 perseant sizeof(*fs->clfs_segtab));
184 1.1 perseant fs->clfs_segtabp = (struct clfs_seguse **)malloc(fs->lfs_nseg *
185 1.1 perseant sizeof(*fs->clfs_segtabp));
186 1.1 perseant for (i = 0; i < fs->lfs_nseg; i++) {
187 1.1 perseant fs->clfs_segtabp[i] = &(fs->clfs_segtab[i]);
188 1.1 perseant fs->clfs_segtab[i].flags = 0x0;
189 1.1 perseant }
190 1.1 perseant syslog(LOG_NOTICE, "%s: unmounted cleaner starting", fsname);
191 1.1 perseant
192 1.1 perseant return 0;
193 1.1 perseant }
194 1.1 perseant #endif
195 1.1 perseant
196 1.1 perseant /*
197 1.1 perseant * Set up the file descriptors, including the Ifile descriptor.
198 1.1 perseant * If we can't get the Ifile, this is not an LFS (or the kernel is
199 1.1 perseant * too old to support the fcntl).
200 1.1 perseant * XXX Merge this and init_unmounted_fs, switching on whether
201 1.1 perseant * XXX "fsname" is a dir or a char special device. Should
202 1.1 perseant * XXX also be able to read unmounted devices out of fstab, the way
203 1.1 perseant * XXX fsck does.
204 1.1 perseant */
205 1.1 perseant int
206 1.1 perseant init_fs(struct clfs *fs, char *fsname)
207 1.1 perseant {
208 1.1 perseant struct statvfs sf;
209 1.1 perseant int rootfd;
210 1.1 perseant int i;
211 1.1 perseant
212 1.1 perseant /*
213 1.1 perseant * Get the raw device from the block device.
214 1.1 perseant * XXX this is ugly. Is there a way to discover the raw device
215 1.1 perseant * XXX for a given mount point?
216 1.1 perseant */
217 1.1 perseant if (statvfs(fsname, &sf) < 0)
218 1.1 perseant return -1;
219 1.1 perseant fs->clfs_dev = malloc(strlen(sf.f_mntfromname) + 2);
220 1.7 perseant if (fs->clfs_dev == NULL) {
221 1.7 perseant syslog(LOG_ERR, "couldn't malloc device name string: %m");
222 1.7 perseant return -1;
223 1.7 perseant }
224 1.1 perseant sprintf(fs->clfs_dev, "/dev/r%s", sf.f_mntfromname + 5);
225 1.1 perseant if ((fs->clfs_devfd = open(fs->clfs_dev, O_RDONLY)) < 0) {
226 1.1 perseant syslog(LOG_ERR, "couldn't open device %s for reading",
227 1.1 perseant fs->clfs_dev);
228 1.1 perseant return -1;
229 1.1 perseant }
230 1.1 perseant
231 1.1 perseant /* Find the Ifile and open it */
232 1.1 perseant if ((rootfd = open(fsname, O_RDONLY)) < 0)
233 1.1 perseant return -2;
234 1.1 perseant if (fcntl(rootfd, LFCNIFILEFH, &fs->clfs_ifilefh) < 0)
235 1.1 perseant return -3;
236 1.10 martin if ((fs->clfs_ifilefd = fhopen(&fs->clfs_ifilefh,
237 1.10 martin sizeof(fs->clfs_ifilefh), O_RDONLY)) < 0)
238 1.1 perseant return -4;
239 1.1 perseant close(rootfd);
240 1.1 perseant
241 1.1 perseant /* Load in the superblock */
242 1.1 perseant if (pread(fs->clfs_devfd, &(fs->lfs_dlfs), sizeof(struct dlfs),
243 1.1 perseant LFS_LABELPAD) < 0)
244 1.1 perseant return -1;
245 1.1 perseant
246 1.1 perseant /* If this is not a version 2 filesystem, complain and exit */
247 1.1 perseant if (fs->lfs_version != 2) {
248 1.1 perseant syslog(LOG_ERR, "%s: not a version 2 LFS", fsname);
249 1.1 perseant return -1;
250 1.1 perseant }
251 1.1 perseant
252 1.1 perseant /* Assume fsname is the mounted name */
253 1.6 mrg strncpy((char *)fs->lfs_fsmnt, fsname, MNAMELEN);
254 1.1 perseant
255 1.1 perseant /* Set up vnodes for Ifile and raw device */
256 1.1 perseant fs->lfs_ivnode = fd_vget(fs->clfs_ifilefd, fs->lfs_bsize, 0, 0);
257 1.1 perseant fs->clfs_devvp = fd_vget(fs->clfs_devfd, fs->lfs_fsize, fs->lfs_ssize,
258 1.1 perseant atatime);
259 1.1 perseant
260 1.1 perseant /* Allocate and clear segtab */
261 1.1 perseant fs->clfs_segtab = (struct clfs_seguse *)malloc(fs->lfs_nseg *
262 1.1 perseant sizeof(*fs->clfs_segtab));
263 1.1 perseant fs->clfs_segtabp = (struct clfs_seguse **)malloc(fs->lfs_nseg *
264 1.1 perseant sizeof(*fs->clfs_segtabp));
265 1.7 perseant if (fs->clfs_segtab == NULL || fs->clfs_segtabp == NULL) {
266 1.7 perseant syslog(LOG_ERR, "%s: couldn't malloc segment table: %m",
267 1.7 perseant fs->clfs_dev);
268 1.7 perseant return -1;
269 1.7 perseant }
270 1.7 perseant
271 1.1 perseant for (i = 0; i < fs->lfs_nseg; i++) {
272 1.1 perseant fs->clfs_segtabp[i] = &(fs->clfs_segtab[i]);
273 1.1 perseant fs->clfs_segtab[i].flags = 0x0;
274 1.1 perseant }
275 1.1 perseant
276 1.1 perseant syslog(LOG_NOTICE, "%s: attaching cleaner", fsname);
277 1.1 perseant return 0;
278 1.1 perseant }
279 1.1 perseant
280 1.1 perseant /*
281 1.1 perseant * Invalidate all the currently held Ifile blocks so they will be
282 1.1 perseant * reread when we clean. Check the size while we're at it, and
283 1.1 perseant * resize the buffer cache if necessary.
284 1.1 perseant */
285 1.1 perseant void
286 1.1 perseant reload_ifile(struct clfs *fs)
287 1.1 perseant {
288 1.1 perseant struct ubuf *bp;
289 1.1 perseant struct stat st;
290 1.1 perseant int ohashmax;
291 1.1 perseant extern int hashmax;
292 1.1 perseant
293 1.2 christos while ((bp = LIST_FIRST(&fs->lfs_ivnode->v_dirtyblkhd)) != NULL) {
294 1.1 perseant bremfree(bp);
295 1.1 perseant buf_destroy(bp);
296 1.1 perseant }
297 1.2 christos while ((bp = LIST_FIRST(&fs->lfs_ivnode->v_cleanblkhd)) != NULL) {
298 1.1 perseant bremfree(bp);
299 1.1 perseant buf_destroy(bp);
300 1.1 perseant }
301 1.1 perseant
302 1.1 perseant /* If Ifile is larger than buffer cache, rehash */
303 1.1 perseant fstat(fs->clfs_ifilefd, &st);
304 1.1 perseant if (st.st_size / fs->lfs_bsize > hashmax) {
305 1.1 perseant ohashmax = hashmax;
306 1.1 perseant bufrehash(st.st_size / fs->lfs_bsize);
307 1.1 perseant dlog("%s: resized buffer hash from %d to %d",
308 1.1 perseant fs->lfs_fsmnt, ohashmax, hashmax);
309 1.1 perseant }
310 1.1 perseant }
311 1.1 perseant
312 1.1 perseant /*
313 1.1 perseant * Get IFILE entry for the given inode, store in ifpp. The buffer
314 1.1 perseant * which contains that data is returned in bpp, and must be brelse()d
315 1.1 perseant * by the caller.
316 1.1 perseant */
317 1.1 perseant void
318 1.1 perseant lfs_ientry(IFILE **ifpp, struct clfs *fs, ino_t ino, struct ubuf **bpp)
319 1.1 perseant {
320 1.1 perseant int error;
321 1.1 perseant
322 1.1 perseant error = bread(fs->lfs_ivnode, ino / fs->lfs_ifpb + fs->lfs_cleansz +
323 1.1 perseant fs->lfs_segtabsz, fs->lfs_bsize, NOCRED, bpp);
324 1.8 perseant if (error)
325 1.8 perseant syslog(LOG_ERR, "%s: ientry failed for ino %d",
326 1.8 perseant fs->lfs_fsmnt, (int)ino);
327 1.1 perseant *ifpp = (IFILE *)(*bpp)->b_data + ino % fs->lfs_ifpb;
328 1.1 perseant return;
329 1.1 perseant }
330 1.1 perseant
331 1.1 perseant #ifdef TEST_PATTERN
332 1.1 perseant /*
333 1.1 perseant * Check ROOTINO for file data. The assumption is that we are running
334 1.1 perseant * the "twofiles" test with the rest of the filesystem empty. Files
335 1.1 perseant * created by "twofiles" match the test pattern, but ROOTINO and the
336 1.1 perseant * executable itself (assumed to be inode 3) should not match.
337 1.1 perseant */
338 1.1 perseant static void
339 1.1 perseant check_test_pattern(BLOCK_INFO *bip)
340 1.1 perseant {
341 1.1 perseant int j;
342 1.1 perseant unsigned char *cp = bip->bi_bp;
343 1.1 perseant
344 1.1 perseant /* Check inode sanity */
345 1.1 perseant if (bip->bi_lbn == LFS_UNUSED_LBN) {
346 1.1 perseant assert(((struct ufs1_dinode *)bip->bi_bp)->di_inumber ==
347 1.1 perseant bip->bi_inode);
348 1.1 perseant }
349 1.1 perseant
350 1.1 perseant /* These can have the test pattern and it's all good */
351 1.1 perseant if (bip->bi_inode > 3)
352 1.1 perseant return;
353 1.1 perseant
354 1.1 perseant for (j = 0; j < bip->bi_size; j++) {
355 1.1 perseant if (cp[j] != (j & 0xff))
356 1.1 perseant break;
357 1.1 perseant }
358 1.1 perseant assert(j < bip->bi_size);
359 1.1 perseant }
360 1.1 perseant #endif /* TEST_PATTERN */
361 1.1 perseant
362 1.1 perseant /*
363 1.1 perseant * Parse the partial segment at daddr, adding its information to
364 1.1 perseant * bip. Return the address of the next partial segment to read.
365 1.1 perseant */
366 1.1 perseant int32_t
367 1.1 perseant parse_pseg(struct clfs *fs, daddr_t daddr, BLOCK_INFO **bipp, int *bic)
368 1.1 perseant {
369 1.1 perseant SEGSUM *ssp;
370 1.1 perseant IFILE *ifp;
371 1.3 perseant BLOCK_INFO *bip, *nbip;
372 1.1 perseant int32_t *iaddrp, idaddr, odaddr;
373 1.1 perseant FINFO *fip;
374 1.1 perseant struct ubuf *ifbp;
375 1.1 perseant struct ufs1_dinode *dip;
376 1.1 perseant u_int32_t ck, vers;
377 1.1 perseant int fic, inoc, obic;
378 1.1 perseant int i;
379 1.6 mrg char *cp;
380 1.1 perseant
381 1.1 perseant odaddr = daddr;
382 1.1 perseant obic = *bic;
383 1.1 perseant bip = *bipp;
384 1.1 perseant
385 1.1 perseant /*
386 1.1 perseant * Retrieve the segment header, set up the SEGSUM pointer
387 1.1 perseant * as well as the first FINFO and inode address pointer.
388 1.1 perseant */
389 1.1 perseant cp = fd_ptrget(fs->clfs_devvp, daddr);
390 1.1 perseant ssp = (SEGSUM *)cp;
391 1.4 perseant iaddrp = ((int32_t *)(cp + fs->lfs_ibsize)) - 1;
392 1.1 perseant fip = (FINFO *)(cp + sizeof(SEGSUM));
393 1.1 perseant
394 1.1 perseant /*
395 1.1 perseant * Check segment header magic and checksum
396 1.1 perseant */
397 1.1 perseant if (ssp->ss_magic != SS_MAGIC) {
398 1.1 perseant syslog(LOG_WARNING, "%s: sumsum magic number bad at 0x%x:"
399 1.1 perseant " read 0x%x, expected 0x%x", fs->lfs_fsmnt,
400 1.1 perseant (int32_t)daddr, ssp->ss_magic, SS_MAGIC);
401 1.1 perseant return 0x0;
402 1.1 perseant }
403 1.1 perseant ck = cksum(&ssp->ss_datasum, fs->lfs_sumsize - sizeof(ssp->ss_sumsum));
404 1.1 perseant if (ck != ssp->ss_sumsum) {
405 1.1 perseant syslog(LOG_WARNING, "%s: sumsum checksum mismatch at 0x%x:"
406 1.1 perseant " read 0x%x, computed 0x%x", fs->lfs_fsmnt,
407 1.1 perseant (int32_t)daddr, ssp->ss_sumsum, ck);
408 1.1 perseant return 0x0;
409 1.1 perseant }
410 1.1 perseant
411 1.1 perseant /* Initialize data sum */
412 1.1 perseant ck = 0;
413 1.1 perseant
414 1.1 perseant /* Point daddr at next block after segment summary */
415 1.1 perseant ++daddr;
416 1.1 perseant
417 1.1 perseant /*
418 1.1 perseant * Loop over file info and inode pointers. We always move daddr
419 1.1 perseant * forward here because we are also computing the data checksum
420 1.1 perseant * as we go.
421 1.1 perseant */
422 1.1 perseant fic = inoc = 0;
423 1.1 perseant while (fic < ssp->ss_nfinfo || inoc < ssp->ss_ninos) {
424 1.1 perseant /*
425 1.3 perseant * We must have either a file block or an inode block.
426 1.3 perseant * If we don't have either one, it's an error.
427 1.3 perseant */
428 1.3 perseant if (fic >= ssp->ss_nfinfo && *iaddrp != daddr) {
429 1.3 perseant syslog(LOG_WARNING, "%s: bad pseg at %x (seg %d)",
430 1.3 perseant fs->lfs_fsmnt, odaddr, dtosn(fs, odaddr));
431 1.3 perseant *bipp = bip;
432 1.3 perseant return 0x0;
433 1.3 perseant }
434 1.3 perseant
435 1.3 perseant /*
436 1.1 perseant * Note each inode from the inode blocks
437 1.1 perseant */
438 1.1 perseant if (inoc < ssp->ss_ninos && *iaddrp == daddr) {
439 1.1 perseant cp = fd_ptrget(fs->clfs_devvp, daddr);
440 1.1 perseant ck = lfs_cksum_part(cp, sizeof(u_int32_t), ck);
441 1.1 perseant dip = (struct ufs1_dinode *)cp;
442 1.1 perseant for (i = 0; i < fs->lfs_inopb; i++) {
443 1.1 perseant if (dip[i].di_inumber == 0)
444 1.1 perseant break;
445 1.1 perseant
446 1.1 perseant /*
447 1.1 perseant * Check currency before adding it
448 1.1 perseant */
449 1.1 perseant #ifndef REPAIR_ZERO_FINFO
450 1.1 perseant lfs_ientry(&ifp, fs, dip[i].di_inumber, &ifbp);
451 1.1 perseant idaddr = ifp->if_daddr;
452 1.13 ad brelse(ifbp, 0);
453 1.1 perseant if (idaddr != daddr)
454 1.1 perseant #endif
455 1.1 perseant continue;
456 1.1 perseant
457 1.1 perseant /*
458 1.1 perseant * A current inode. Add it.
459 1.1 perseant */
460 1.1 perseant ++*bic;
461 1.3 perseant nbip = (BLOCK_INFO *)realloc(bip, *bic *
462 1.3 perseant sizeof(*bip));
463 1.3 perseant if (nbip)
464 1.3 perseant bip = nbip;
465 1.3 perseant else {
466 1.3 perseant --*bic;
467 1.3 perseant *bipp = bip;
468 1.3 perseant return 0x0;
469 1.3 perseant }
470 1.1 perseant bip[*bic - 1].bi_inode = dip[i].di_inumber;
471 1.1 perseant bip[*bic - 1].bi_lbn = LFS_UNUSED_LBN;
472 1.1 perseant bip[*bic - 1].bi_daddr = daddr;
473 1.1 perseant bip[*bic - 1].bi_segcreate = ssp->ss_create;
474 1.1 perseant bip[*bic - 1].bi_version = dip[i].di_gen;
475 1.1 perseant bip[*bic - 1].bi_bp = &(dip[i]);
476 1.1 perseant bip[*bic - 1].bi_size = DINODE1_SIZE;
477 1.1 perseant }
478 1.1 perseant inoc += i;
479 1.1 perseant daddr += btofsb(fs, fs->lfs_ibsize);
480 1.1 perseant --iaddrp;
481 1.1 perseant continue;
482 1.1 perseant }
483 1.1 perseant
484 1.1 perseant /*
485 1.1 perseant * Note each file block from the finfo blocks
486 1.1 perseant */
487 1.1 perseant if (fic >= ssp->ss_nfinfo)
488 1.1 perseant continue;
489 1.1 perseant
490 1.1 perseant /* Count this finfo, whether or not we use it */
491 1.1 perseant ++fic;
492 1.1 perseant
493 1.1 perseant /*
494 1.1 perseant * If this finfo has nblocks==0, it was written wrong.
495 1.1 perseant * Kernels with this problem always wrote this zero-sized
496 1.1 perseant * finfo last, so just ignore it.
497 1.1 perseant */
498 1.1 perseant if (fip->fi_nblocks == 0) {
499 1.1 perseant #ifdef REPAIR_ZERO_FINFO
500 1.1 perseant struct ubuf *nbp;
501 1.1 perseant SEGSUM *nssp;
502 1.1 perseant
503 1.1 perseant syslog(LOG_WARNING, "fixing short FINFO at %x (seg %d)",
504 1.1 perseant odaddr, dtosn(fs, odaddr));
505 1.1 perseant bread(fs->clfs_devvp, odaddr, fs->lfs_fsize, NOCRED, &nbp);
506 1.1 perseant nssp = (SEGSUM *)nbp->b_data;
507 1.1 perseant --nssp->ss_nfinfo;
508 1.1 perseant nssp->ss_sumsum = cksum(&nssp->ss_datasum,
509 1.1 perseant fs->lfs_sumsize - sizeof(nssp->ss_sumsum));
510 1.1 perseant bwrite(nbp);
511 1.1 perseant #endif
512 1.8 perseant syslog(LOG_WARNING, "zero-length FINFO at %x (seg %d)",
513 1.8 perseant odaddr, dtosn(fs, odaddr));
514 1.1 perseant continue;
515 1.1 perseant }
516 1.1 perseant
517 1.1 perseant /*
518 1.1 perseant * Check currency before adding blocks
519 1.1 perseant */
520 1.1 perseant #ifdef REPAIR_ZERO_FINFO
521 1.1 perseant vers = -1;
522 1.1 perseant #else
523 1.1 perseant lfs_ientry(&ifp, fs, fip->fi_ino, &ifbp);
524 1.1 perseant vers = ifp->if_version;
525 1.13 ad brelse(ifbp, 0);
526 1.1 perseant #endif
527 1.1 perseant if (vers != fip->fi_version) {
528 1.1 perseant size_t size;
529 1.1 perseant
530 1.1 perseant /* Read all the blocks from the data summary */
531 1.1 perseant for (i = 0; i < fip->fi_nblocks; i++) {
532 1.1 perseant size = (i == fip->fi_nblocks - 1) ?
533 1.1 perseant fip->fi_lastlength : fs->lfs_bsize;
534 1.1 perseant cp = fd_ptrget(fs->clfs_devvp, daddr);
535 1.1 perseant ck = lfs_cksum_part(cp, sizeof(u_int32_t), ck);
536 1.1 perseant daddr += btofsb(fs, size);
537 1.1 perseant }
538 1.1 perseant fip = (FINFO *)(fip->fi_blocks + fip->fi_nblocks);
539 1.1 perseant continue;
540 1.1 perseant }
541 1.1 perseant
542 1.1 perseant /* Add all the blocks from the finfos (current or not) */
543 1.3 perseant nbip = (BLOCK_INFO *)realloc(bip, (*bic + fip->fi_nblocks) *
544 1.3 perseant sizeof(*bip));
545 1.3 perseant if (nbip)
546 1.3 perseant bip = nbip;
547 1.3 perseant else {
548 1.3 perseant *bipp = bip;
549 1.3 perseant return 0x0;
550 1.3 perseant }
551 1.3 perseant
552 1.1 perseant for (i = 0; i < fip->fi_nblocks; i++) {
553 1.1 perseant bip[*bic + i].bi_inode = fip->fi_ino;
554 1.1 perseant bip[*bic + i].bi_lbn = fip->fi_blocks[i];
555 1.1 perseant bip[*bic + i].bi_daddr = daddr;
556 1.1 perseant bip[*bic + i].bi_segcreate = ssp->ss_create;
557 1.1 perseant bip[*bic + i].bi_version = fip->fi_version;
558 1.1 perseant bip[*bic + i].bi_size = (i == fip->fi_nblocks - 1) ?
559 1.1 perseant fip->fi_lastlength : fs->lfs_bsize;
560 1.1 perseant cp = fd_ptrget(fs->clfs_devvp, daddr);
561 1.1 perseant ck = lfs_cksum_part(cp, sizeof(u_int32_t), ck);
562 1.1 perseant bip[*bic + i].bi_bp = cp;
563 1.1 perseant daddr += btofsb(fs, bip[*bic + i].bi_size);
564 1.1 perseant
565 1.1 perseant #ifdef TEST_PATTERN
566 1.1 perseant check_test_pattern(bip + *bic + i); /* XXXDEBUG */
567 1.1 perseant #endif
568 1.1 perseant }
569 1.1 perseant *bic += fip->fi_nblocks;
570 1.1 perseant fip = (FINFO *)(fip->fi_blocks + fip->fi_nblocks);
571 1.1 perseant }
572 1.1 perseant
573 1.1 perseant #ifndef REPAIR_ZERO_FINFO
574 1.1 perseant if (ssp->ss_datasum != ck) {
575 1.1 perseant syslog(LOG_WARNING, "%s: data checksum bad at 0x%x:"
576 1.1 perseant " read 0x%x, computed 0x%x", fs->lfs_fsmnt, odaddr,
577 1.1 perseant ssp->ss_datasum, ck);
578 1.1 perseant *bic = obic;
579 1.1 perseant return 0x0;
580 1.1 perseant }
581 1.1 perseant #endif
582 1.1 perseant
583 1.1 perseant *bipp = bip;
584 1.1 perseant return daddr;
585 1.1 perseant }
586 1.1 perseant
587 1.1 perseant static void
588 1.1 perseant log_segment_read(struct clfs *fs, int sn)
589 1.1 perseant {
590 1.1 perseant FILE *fp;
591 1.1 perseant char *cp;
592 1.1 perseant
593 1.1 perseant /*
594 1.1 perseant * Write the segment read, and its contents, into a log file in
595 1.1 perseant * the current directory. We don't need to log the location of
596 1.1 perseant * the segment, since that can be inferred from the segments up
597 1.1 perseant * to this point (ss_nextseg field of the previously written segment).
598 1.1 perseant *
599 1.1 perseant * We can use this info later to reconstruct the filesystem at any
600 1.1 perseant * given point in time for analysis, by replaying the log forward
601 1.1 perseant * indexed by the segment serial numbers; but it is not suitable
602 1.1 perseant * for everyday use since the copylog will be simply enormous.
603 1.1 perseant */
604 1.1 perseant cp = fd_ptrget(fs->clfs_devvp, sntod(fs, sn));
605 1.1 perseant
606 1.1 perseant fp = fopen(copylog_filename, "ab");
607 1.1 perseant if (fp != NULL) {
608 1.1 perseant if (fwrite(cp, (size_t)fs->lfs_ssize, 1, fp) < 0) {
609 1.1 perseant perror("writing segment to copy log");
610 1.1 perseant }
611 1.1 perseant }
612 1.1 perseant fclose(fp);
613 1.1 perseant }
614 1.1 perseant
615 1.1 perseant /*
616 1.1 perseant * Read a segment to populate the BLOCK_INFO structures.
617 1.1 perseant * Return the number of partial segments read and parsed.
618 1.1 perseant */
619 1.1 perseant int
620 1.1 perseant load_segment(struct clfs *fs, int sn, BLOCK_INFO **bipp, int *bic)
621 1.1 perseant {
622 1.1 perseant int32_t daddr;
623 1.1 perseant int i, npseg;
624 1.1 perseant
625 1.1 perseant daddr = sntod(fs, sn);
626 1.1 perseant if (daddr < btofsb(fs, LFS_LABELPAD))
627 1.1 perseant daddr = btofsb(fs, LFS_LABELPAD);
628 1.1 perseant for (i = 0; i < LFS_MAXNUMSB; i++) {
629 1.1 perseant if (fs->lfs_sboffs[i] == daddr) {
630 1.1 perseant daddr += btofsb(fs, LFS_SBPAD);
631 1.1 perseant break;
632 1.1 perseant }
633 1.1 perseant }
634 1.1 perseant
635 1.1 perseant /* Preload the segment buffer */
636 1.1 perseant if (fd_preload(fs->clfs_devvp, sntod(fs, sn)) < 0)
637 1.1 perseant return -1;
638 1.1 perseant
639 1.1 perseant if (copylog_filename)
640 1.1 perseant log_segment_read(fs, sn);
641 1.1 perseant
642 1.1 perseant /* Note bytes read for stats */
643 1.1 perseant cleaner_stats.segs_cleaned++;
644 1.1 perseant cleaner_stats.bytes_read += fs->lfs_ssize;
645 1.1 perseant ++fs->clfs_nactive;
646 1.1 perseant
647 1.1 perseant npseg = 0;
648 1.1 perseant while(dtosn(fs, daddr) == sn &&
649 1.1 perseant dtosn(fs, daddr + btofsb(fs, fs->lfs_bsize)) == sn) {
650 1.1 perseant daddr = parse_pseg(fs, daddr, bipp, bic);
651 1.1 perseant if (daddr == 0x0) {
652 1.1 perseant ++cleaner_stats.segs_error;
653 1.1 perseant break;
654 1.1 perseant }
655 1.1 perseant ++npseg;
656 1.1 perseant }
657 1.1 perseant
658 1.1 perseant return npseg;
659 1.1 perseant }
660 1.1 perseant
661 1.1 perseant void
662 1.1 perseant calc_cb(struct clfs *fs, int sn, struct clfs_seguse *t)
663 1.1 perseant {
664 1.1 perseant time_t now;
665 1.1 perseant int64_t age, benefit, cost;
666 1.1 perseant
667 1.1 perseant time(&now);
668 1.1 perseant age = (now < t->lastmod ? 0 : now - t->lastmod);
669 1.1 perseant
670 1.1 perseant /* Under no circumstances clean active or already-clean segments */
671 1.1 perseant if ((t->flags & SEGUSE_ACTIVE) || !(t->flags & SEGUSE_DIRTY)) {
672 1.1 perseant t->priority = 0;
673 1.1 perseant return;
674 1.1 perseant }
675 1.1 perseant
676 1.1 perseant /*
677 1.1 perseant * If the segment is empty, there is no reason to clean it.
678 1.1 perseant * Clear its error condition, if any, since we are never going to
679 1.1 perseant * try to parse this one.
680 1.1 perseant */
681 1.1 perseant if (t->nbytes == 0) {
682 1.1 perseant t->flags &= ~SEGUSE_ERROR; /* Strip error once empty */
683 1.1 perseant t->priority = 0;
684 1.1 perseant return;
685 1.1 perseant }
686 1.1 perseant
687 1.1 perseant if (t->flags & SEGUSE_ERROR) { /* No good if not already empty */
688 1.1 perseant /* No benefit */
689 1.1 perseant t->priority = 0;
690 1.1 perseant return;
691 1.1 perseant }
692 1.1 perseant
693 1.1 perseant if (t->nbytes < 0 || t->nbytes > fs->lfs_ssize) {
694 1.1 perseant /* Another type of error */
695 1.1 perseant syslog(LOG_WARNING, "segment %d: bad seguse count %d",
696 1.1 perseant sn, t->nbytes);
697 1.1 perseant t->flags |= SEGUSE_ERROR;
698 1.1 perseant t->priority = 0;
699 1.1 perseant return;
700 1.1 perseant }
701 1.1 perseant
702 1.1 perseant /*
703 1.1 perseant * The non-degenerate case. Use Rosenblum's cost-benefit algorithm.
704 1.1 perseant * Calculate the benefit from cleaning this segment (one segment,
705 1.1 perseant * minus fragmentation, dirty blocks and a segment summary block)
706 1.1 perseant * and weigh that against the cost (bytes read plus bytes written).
707 1.1 perseant * We count the summary headers as "dirty" to avoid cleaning very
708 1.1 perseant * old and very full segments.
709 1.1 perseant */
710 1.1 perseant benefit = (int64_t)fs->lfs_ssize - t->nbytes -
711 1.1 perseant (t->nsums + 1) * fs->lfs_fsize;
712 1.1 perseant if (fs->lfs_bsize > fs->lfs_fsize) /* fragmentation */
713 1.5 perseant benefit -= (fs->lfs_bsize / 2);
714 1.1 perseant if (benefit <= 0) {
715 1.1 perseant t->priority = 0;
716 1.1 perseant return;
717 1.1 perseant }
718 1.1 perseant
719 1.1 perseant cost = fs->lfs_ssize + t->nbytes;
720 1.1 perseant t->priority = (256 * benefit * age) / cost;
721 1.1 perseant
722 1.1 perseant return;
723 1.1 perseant }
724 1.1 perseant
725 1.1 perseant /*
726 1.1 perseant * Comparator for BLOCK_INFO structures. Anything not in one of the segments
727 1.1 perseant * we're looking at sorts higher; after that we sort first by inode number
728 1.1 perseant * and then by block number (unsigned, i.e., negative sorts higher) *but*
729 1.1 perseant * sort inodes before data blocks.
730 1.1 perseant */
731 1.1 perseant static int
732 1.1 perseant bi_comparator(const void *va, const void *vb)
733 1.1 perseant {
734 1.1 perseant BLOCK_INFO *a, *b;
735 1.1 perseant
736 1.1 perseant a = (BLOCK_INFO *)va;
737 1.1 perseant b = (BLOCK_INFO *)vb;
738 1.1 perseant
739 1.1 perseant /* Check for out-of-place block */
740 1.1 perseant if (a->bi_segcreate == a->bi_daddr &&
741 1.1 perseant b->bi_segcreate != b->bi_daddr)
742 1.1 perseant return -1;
743 1.1 perseant if (a->bi_segcreate != a->bi_daddr &&
744 1.1 perseant b->bi_segcreate == b->bi_daddr)
745 1.1 perseant return 1;
746 1.1 perseant if (a->bi_size <= 0 && b->bi_size > 0)
747 1.1 perseant return 1;
748 1.1 perseant if (b->bi_size <= 0 && a->bi_size > 0)
749 1.1 perseant return -1;
750 1.1 perseant
751 1.1 perseant /* Check inode number */
752 1.1 perseant if (a->bi_inode != b->bi_inode)
753 1.1 perseant return a->bi_inode - b->bi_inode;
754 1.1 perseant
755 1.1 perseant /* Check lbn */
756 1.1 perseant if (a->bi_lbn == LFS_UNUSED_LBN) /* Inodes sort lower than blocks */
757 1.1 perseant return -1;
758 1.1 perseant if (b->bi_lbn == LFS_UNUSED_LBN)
759 1.1 perseant return 1;
760 1.1 perseant if ((u_int32_t)a->bi_lbn > (u_int32_t)b->bi_lbn)
761 1.1 perseant return 1;
762 1.1 perseant else
763 1.1 perseant return -1;
764 1.8 perseant
765 1.8 perseant return 0;
766 1.1 perseant }
767 1.1 perseant
768 1.1 perseant /*
769 1.1 perseant * Comparator for sort_segments: cost-benefit equation.
770 1.1 perseant */
771 1.1 perseant static int
772 1.1 perseant cb_comparator(const void *va, const void *vb)
773 1.1 perseant {
774 1.1 perseant struct clfs_seguse *a, *b;
775 1.1 perseant
776 1.1 perseant a = *(struct clfs_seguse **)va;
777 1.1 perseant b = *(struct clfs_seguse **)vb;
778 1.1 perseant return a->priority > b->priority ? -1 : 1;
779 1.1 perseant }
780 1.1 perseant
781 1.1 perseant void
782 1.7 perseant toss_old_blocks(struct clfs *fs, BLOCK_INFO **bipp, int *bic, int *sizep)
783 1.1 perseant {
784 1.1 perseant int i, r;
785 1.1 perseant BLOCK_INFO *bip = *bipp;
786 1.1 perseant struct lfs_fcntl_markv /* {
787 1.1 perseant BLOCK_INFO *blkiov;
788 1.1 perseant int blkcnt;
789 1.1 perseant } */ lim;
790 1.1 perseant
791 1.3 perseant if (bic == 0 || bip == NULL)
792 1.3 perseant return;
793 1.3 perseant
794 1.1 perseant /*
795 1.1 perseant * Kludge: Store the disk address in segcreate so we know which
796 1.1 perseant * ones to toss.
797 1.1 perseant */
798 1.1 perseant for (i = 0; i < *bic; i++)
799 1.1 perseant bip[i].bi_segcreate = bip[i].bi_daddr;
800 1.1 perseant
801 1.1 perseant /* Sort the blocks */
802 1.1 perseant heapsort(bip, *bic, sizeof(BLOCK_INFO), bi_comparator);
803 1.1 perseant
804 1.1 perseant /* Use bmapv to locate the blocks */
805 1.1 perseant lim.blkiov = bip;
806 1.1 perseant lim.blkcnt = *bic;
807 1.1 perseant if ((r = fcntl(fs->clfs_ifilefd, LFCNBMAPV, &lim)) < 0) {
808 1.1 perseant syslog(LOG_WARNING, "%s: bmapv returned %d (%m)",
809 1.1 perseant fs->lfs_fsmnt, r);
810 1.1 perseant return;
811 1.1 perseant }
812 1.1 perseant
813 1.1 perseant /* Toss blocks not in this segment */
814 1.1 perseant heapsort(bip, *bic, sizeof(BLOCK_INFO), bi_comparator);
815 1.1 perseant
816 1.1 perseant /* Get rid of stale blocks */
817 1.7 perseant if (sizep)
818 1.7 perseant *sizep = 0;
819 1.7 perseant for (i = 0; i < *bic; i++) {
820 1.1 perseant if (bip[i].bi_segcreate != bip[i].bi_daddr)
821 1.1 perseant break;
822 1.7 perseant if (sizep)
823 1.7 perseant *sizep += bip[i].bi_size;
824 1.7 perseant }
825 1.1 perseant *bic = i; /* XXX realloc bip? */
826 1.1 perseant *bipp = bip;
827 1.1 perseant
828 1.1 perseant return;
829 1.1 perseant }
830 1.1 perseant
831 1.1 perseant /*
832 1.1 perseant * Clean a segment and mark it invalid.
833 1.1 perseant */
834 1.1 perseant int
835 1.1 perseant invalidate_segment(struct clfs *fs, int sn)
836 1.1 perseant {
837 1.1 perseant BLOCK_INFO *bip;
838 1.1 perseant int i, r, bic;
839 1.1 perseant off_t nb;
840 1.1 perseant double util;
841 1.1 perseant struct lfs_fcntl_markv /* {
842 1.1 perseant BLOCK_INFO *blkiov;
843 1.1 perseant int blkcnt;
844 1.1 perseant } */ lim;
845 1.1 perseant
846 1.1 perseant dlog("%s: inval seg %d", fs->lfs_fsmnt, sn);
847 1.1 perseant
848 1.1 perseant bip = NULL;
849 1.1 perseant bic = 0;
850 1.1 perseant fs->clfs_nactive = 0;
851 1.7 perseant if (load_segment(fs, sn, &bip, &bic) <= 0)
852 1.7 perseant return -1;
853 1.7 perseant toss_old_blocks(fs, &bip, &bic, NULL);
854 1.1 perseant
855 1.1 perseant /* Record statistics */
856 1.1 perseant for (i = nb = 0; i < bic; i++)
857 1.1 perseant nb += bip[i].bi_size;
858 1.1 perseant util = ((double)nb) / (fs->clfs_nactive * fs->lfs_ssize);
859 1.1 perseant cleaner_stats.util_tot += util;
860 1.1 perseant cleaner_stats.util_sos += util * util;
861 1.1 perseant cleaner_stats.bytes_written += nb;
862 1.1 perseant
863 1.1 perseant /*
864 1.1 perseant * Use markv to move the blocks.
865 1.1 perseant */
866 1.1 perseant lim.blkiov = bip;
867 1.1 perseant lim.blkcnt = bic;
868 1.1 perseant if ((r = fcntl(fs->clfs_ifilefd, LFCNMARKV, &lim)) < 0) {
869 1.1 perseant syslog(LOG_WARNING, "%s: markv returned %d (%m) "
870 1.1 perseant "for seg %d", fs->lfs_fsmnt, r, sn);
871 1.1 perseant return r;
872 1.1 perseant }
873 1.1 perseant
874 1.1 perseant /*
875 1.1 perseant * Finally call invalidate to invalidate the segment.
876 1.1 perseant */
877 1.1 perseant if ((r = fcntl(fs->clfs_ifilefd, LFCNINVAL, &sn)) < 0) {
878 1.1 perseant syslog(LOG_WARNING, "%s: inval returned %d (%m) "
879 1.1 perseant "for seg %d", fs->lfs_fsmnt, r, sn);
880 1.1 perseant return r;
881 1.1 perseant }
882 1.1 perseant
883 1.1 perseant return 0;
884 1.1 perseant }
885 1.1 perseant
886 1.1 perseant /*
887 1.1 perseant * Check to see if the given ino/lbn pair is represented in the BLOCK_INFO
888 1.1 perseant * array we are sending to the kernel, or if the kernel will have to add it.
889 1.1 perseant * The kernel will only add each such pair once, though, so keep track of
890 1.1 perseant * previous requests in a separate "extra" BLOCK_INFO array. Returns 1
891 1.1 perseant * if the block needs to be added, 0 if it is already represented.
892 1.1 perseant */
893 1.1 perseant static int
894 1.1 perseant check_or_add(ino_t ino, int32_t lbn, BLOCK_INFO *bip, int bic, BLOCK_INFO **ebipp, int *ebicp)
895 1.1 perseant {
896 1.1 perseant BLOCK_INFO *t, *ebip = *ebipp;
897 1.1 perseant int ebic = *ebicp;
898 1.1 perseant int k;
899 1.1 perseant
900 1.1 perseant for (k = 0; k < bic; k++) {
901 1.1 perseant if (bip[k].bi_inode != ino)
902 1.1 perseant break;
903 1.1 perseant if (bip[k].bi_lbn == lbn) {
904 1.1 perseant return 0;
905 1.1 perseant }
906 1.1 perseant }
907 1.1 perseant
908 1.1 perseant /* Look on the list of extra blocks, too */
909 1.1 perseant for (k = 0; k < ebic; k++) {
910 1.1 perseant if (ebip[k].bi_inode == ino && ebip[k].bi_lbn == lbn) {
911 1.1 perseant return 0;
912 1.1 perseant }
913 1.1 perseant }
914 1.1 perseant
915 1.1 perseant ++ebic;
916 1.1 perseant t = realloc(ebip, ebic * sizeof(BLOCK_INFO));
917 1.1 perseant if (t == NULL)
918 1.1 perseant return 1; /* Note *ebipc is not updated */
919 1.1 perseant
920 1.1 perseant ebip = t;
921 1.1 perseant ebip[ebic - 1].bi_inode = ino;
922 1.1 perseant ebip[ebic - 1].bi_lbn = lbn;
923 1.1 perseant
924 1.1 perseant *ebipp = ebip;
925 1.1 perseant *ebicp = ebic;
926 1.1 perseant return 1;
927 1.1 perseant }
928 1.1 perseant
929 1.1 perseant /*
930 1.1 perseant * Look for indirect blocks we will have to write which are not
931 1.1 perseant * contained in this collection of blocks. This constitutes
932 1.1 perseant * a hidden cleaning cost, since we are unaware of it until we
933 1.1 perseant * have already read the segments. Return the total cost, and fill
934 1.1 perseant * in *ifc with the part of that cost due to rewriting the Ifile.
935 1.1 perseant */
936 1.1 perseant static off_t
937 1.1 perseant check_hidden_cost(struct clfs *fs, BLOCK_INFO *bip, int bic, off_t *ifc)
938 1.1 perseant {
939 1.1 perseant int start;
940 1.1 perseant struct indir in[NIADDR + 1];
941 1.1 perseant int num;
942 1.1 perseant int i, j, ebic;
943 1.1 perseant BLOCK_INFO *ebip;
944 1.1 perseant int32_t lbn;
945 1.1 perseant
946 1.1 perseant start = 0;
947 1.1 perseant ebip = NULL;
948 1.1 perseant ebic = 0;
949 1.1 perseant for (i = 0; i < bic; i++) {
950 1.1 perseant if (i == 0 || bip[i].bi_inode != bip[start].bi_inode) {
951 1.1 perseant start = i;
952 1.1 perseant /*
953 1.1 perseant * Look for IFILE blocks, unless this is the Ifile.
954 1.1 perseant */
955 1.1 perseant if (bip[i].bi_inode != fs->lfs_ifile) {
956 1.1 perseant lbn = fs->lfs_cleansz + bip[i].bi_inode /
957 1.1 perseant fs->lfs_ifpb;
958 1.1 perseant *ifc += check_or_add(fs->lfs_ifile, lbn,
959 1.1 perseant bip, bic, &ebip, &ebic);
960 1.1 perseant }
961 1.1 perseant }
962 1.1 perseant if (bip[i].bi_lbn == LFS_UNUSED_LBN)
963 1.1 perseant continue;
964 1.5 perseant if (bip[i].bi_lbn < NDADDR)
965 1.5 perseant continue;
966 1.5 perseant
967 1.1 perseant ufs_getlbns((struct lfs *)fs, NULL, (daddr_t)bip[i].bi_lbn, in, &num);
968 1.1 perseant for (j = 0; j < num; j++) {
969 1.1 perseant check_or_add(bip[i].bi_inode, in[j].in_lbn,
970 1.1 perseant bip + start, bic - start, &ebip, &ebic);
971 1.1 perseant }
972 1.1 perseant }
973 1.1 perseant return ebic;
974 1.1 perseant }
975 1.1 perseant
976 1.1 perseant /*
977 1.1 perseant * Select segments to clean, add blocks from these segments to a cleaning
978 1.1 perseant * list, and send this list through lfs_markv() to move them to new
979 1.1 perseant * locations on disk.
980 1.1 perseant */
981 1.1 perseant int
982 1.1 perseant clean_fs(struct clfs *fs, CLEANERINFO *cip)
983 1.1 perseant {
984 1.7 perseant int i, j, ngood, sn, bic, r, npos;
985 1.7 perseant int bytes, totbytes;
986 1.1 perseant struct ubuf *bp;
987 1.1 perseant SEGUSE *sup;
988 1.1 perseant static BLOCK_INFO *bip;
989 1.1 perseant struct lfs_fcntl_markv /* {
990 1.1 perseant BLOCK_INFO *blkiov;
991 1.1 perseant int blkcnt;
992 1.1 perseant } */ lim;
993 1.1 perseant int mc;
994 1.1 perseant BLOCK_INFO *mbip;
995 1.1 perseant int inc;
996 1.1 perseant off_t nb;
997 1.1 perseant off_t goal;
998 1.1 perseant off_t extra, if_extra;
999 1.1 perseant double util;
1000 1.1 perseant
1001 1.1 perseant /* Read the segment table into our private structure */
1002 1.7 perseant npos = 0;
1003 1.1 perseant for (i = 0; i < fs->lfs_nseg; i+= fs->lfs_sepb) {
1004 1.1 perseant bread(fs->lfs_ivnode, fs->lfs_cleansz + i / fs->lfs_sepb,
1005 1.1 perseant fs->lfs_bsize, NOCRED, &bp);
1006 1.1 perseant for (j = 0; j < fs->lfs_sepb && i + j < fs->lfs_nseg; j++) {
1007 1.1 perseant sup = ((SEGUSE *)bp->b_data) + j;
1008 1.1 perseant fs->clfs_segtab[i + j].nbytes = sup->su_nbytes;
1009 1.1 perseant fs->clfs_segtab[i + j].nsums = sup->su_nsums;
1010 1.1 perseant fs->clfs_segtab[i + j].lastmod = sup->su_lastmod;
1011 1.1 perseant /* Keep error status but renew other flags */
1012 1.1 perseant fs->clfs_segtab[i + j].flags &= SEGUSE_ERROR;
1013 1.1 perseant fs->clfs_segtab[i + j].flags |= sup->su_flags;
1014 1.1 perseant
1015 1.1 perseant /* Compute cost-benefit coefficient */
1016 1.1 perseant calc_cb(fs, i + j, fs->clfs_segtab + i + j);
1017 1.7 perseant if (fs->clfs_segtab[i + j].priority > 0)
1018 1.7 perseant ++npos;
1019 1.1 perseant }
1020 1.13 ad brelse(bp, 0);
1021 1.1 perseant }
1022 1.1 perseant
1023 1.1 perseant /* Sort segments based on cleanliness, fulness, and condition */
1024 1.1 perseant heapsort(fs->clfs_segtabp, fs->lfs_nseg, sizeof(struct clfs_seguse *),
1025 1.1 perseant cb_comparator);
1026 1.1 perseant
1027 1.1 perseant /* If no segment is cleanable, just return */
1028 1.1 perseant if (fs->clfs_segtabp[0]->priority == 0) {
1029 1.1 perseant dlog("%s: no segment cleanable", fs->lfs_fsmnt);
1030 1.1 perseant return 0;
1031 1.1 perseant }
1032 1.1 perseant
1033 1.1 perseant /* Load some segments' blocks into bip */
1034 1.1 perseant bic = 0;
1035 1.1 perseant fs->clfs_nactive = 0;
1036 1.1 perseant ngood = 0;
1037 1.1 perseant if (use_bytes) {
1038 1.1 perseant /* Set attainable goal */
1039 1.1 perseant goal = fs->lfs_ssize * atatime;
1040 1.1 perseant if (goal > (cip->clean - 1) * fs->lfs_ssize / 2)
1041 1.1 perseant goal = MAX((cip->clean - 1) * fs->lfs_ssize,
1042 1.1 perseant fs->lfs_ssize) / 2;
1043 1.1 perseant
1044 1.7 perseant dlog("%s: cleaning with goal %" PRId64
1045 1.7 perseant " bytes (%d segs clean, %d cleanable)",
1046 1.7 perseant fs->lfs_fsmnt, goal, cip->clean, npos);
1047 1.7 perseant syslog(LOG_INFO, "%s: cleaning with goal %" PRId64
1048 1.7 perseant " bytes (%d segs clean, %d cleanable)",
1049 1.7 perseant fs->lfs_fsmnt, goal, cip->clean, npos);
1050 1.7 perseant totbytes = 0;
1051 1.7 perseant for (i = 0; i < fs->lfs_nseg && totbytes < goal; i++) {
1052 1.1 perseant if (fs->clfs_segtabp[i]->priority == 0)
1053 1.1 perseant break;
1054 1.8 perseant /* Upper bound on number of segments at once */
1055 1.8 perseant if (ngood * fs->lfs_ssize > 4 * goal)
1056 1.8 perseant break;
1057 1.1 perseant sn = (fs->clfs_segtabp[i] - fs->clfs_segtab);
1058 1.1 perseant dlog("%s: add seg %d prio %" PRIu64
1059 1.1 perseant " containing %ld bytes",
1060 1.1 perseant fs->lfs_fsmnt, sn, fs->clfs_segtabp[i]->priority,
1061 1.1 perseant fs->clfs_segtabp[i]->nbytes);
1062 1.7 perseant if ((r = load_segment(fs, sn, &bip, &bic)) > 0) {
1063 1.1 perseant ++ngood;
1064 1.7 perseant toss_old_blocks(fs, &bip, &bic, &bytes);
1065 1.7 perseant totbytes += bytes;
1066 1.7 perseant } else if (r == 0)
1067 1.7 perseant fd_release(fs->clfs_devvp);
1068 1.1 perseant else
1069 1.1 perseant break;
1070 1.1 perseant }
1071 1.1 perseant } else {
1072 1.1 perseant /* Set attainable goal */
1073 1.1 perseant goal = atatime;
1074 1.1 perseant if (goal > cip->clean - 1)
1075 1.1 perseant goal = MAX(cip->clean - 1, 1);
1076 1.1 perseant
1077 1.7 perseant dlog("%s: cleaning with goal %d segments (%d clean, %d cleanable)",
1078 1.7 perseant fs->lfs_fsmnt, (int)goal, cip->clean, npos);
1079 1.1 perseant for (i = 0; i < fs->lfs_nseg && ngood < goal; i++) {
1080 1.1 perseant if (fs->clfs_segtabp[i]->priority == 0)
1081 1.1 perseant break;
1082 1.1 perseant sn = (fs->clfs_segtabp[i] - fs->clfs_segtab);
1083 1.1 perseant dlog("%s: add seg %d prio %" PRIu64,
1084 1.1 perseant fs->lfs_fsmnt, sn, fs->clfs_segtabp[i]->priority);
1085 1.1 perseant if ((r = load_segment(fs, sn, &bip, &bic)) > 0)
1086 1.1 perseant ++ngood;
1087 1.7 perseant else if (r == 0)
1088 1.7 perseant fd_release(fs->clfs_devvp);
1089 1.7 perseant else
1090 1.1 perseant break;
1091 1.1 perseant }
1092 1.7 perseant toss_old_blocks(fs, &bip, &bic, NULL);
1093 1.1 perseant }
1094 1.1 perseant
1095 1.1 perseant /* If there is nothing to do, try again later. */
1096 1.1 perseant if (bic == 0) {
1097 1.3 perseant dlog("%s: no blocks to clean in %d cleanable segments",
1098 1.3 perseant fs->lfs_fsmnt, (int)ngood);
1099 1.1 perseant fd_release_all(fs->clfs_devvp);
1100 1.1 perseant return 0;
1101 1.1 perseant }
1102 1.1 perseant
1103 1.1 perseant /* Record statistics */
1104 1.1 perseant for (i = nb = 0; i < bic; i++)
1105 1.1 perseant nb += bip[i].bi_size;
1106 1.1 perseant util = ((double)nb) / (fs->clfs_nactive * fs->lfs_ssize);
1107 1.1 perseant cleaner_stats.util_tot += util;
1108 1.1 perseant cleaner_stats.util_sos += util * util;
1109 1.1 perseant cleaner_stats.bytes_written += nb;
1110 1.1 perseant
1111 1.1 perseant /*
1112 1.1 perseant * Check out our blocks to see if there are hidden cleaning costs.
1113 1.1 perseant * If there are, we might be cleaning ourselves deeper into a hole
1114 1.1 perseant * rather than doing anything useful.
1115 1.1 perseant * XXX do something about this.
1116 1.1 perseant */
1117 1.1 perseant if_extra = 0;
1118 1.1 perseant extra = fs->lfs_bsize * (off_t)check_hidden_cost(fs, bip, bic, &if_extra);
1119 1.1 perseant if_extra *= fs->lfs_bsize;
1120 1.1 perseant
1121 1.1 perseant /*
1122 1.1 perseant * Use markv to move the blocks.
1123 1.1 perseant */
1124 1.1 perseant if (do_small)
1125 1.1 perseant inc = MAXPHYS / fs->lfs_bsize - 1;
1126 1.1 perseant else
1127 1.1 perseant inc = LFS_MARKV_MAXBLKCNT / 2;
1128 1.1 perseant for (mc = 0, mbip = bip; mc < bic; mc += inc, mbip += inc) {
1129 1.1 perseant lim.blkiov = mbip;
1130 1.1 perseant lim.blkcnt = (bic - mc > inc ? inc : bic - mc);
1131 1.1 perseant #ifdef TEST_PATTERN
1132 1.1 perseant dlog("checking blocks %d-%d", mc, mc + lim.blkcnt - 1);
1133 1.1 perseant for (i = 0; i < lim.blkcnt; i++) {
1134 1.1 perseant check_test_pattern(mbip + i);
1135 1.1 perseant }
1136 1.1 perseant #endif /* TEST_PATTERN */
1137 1.1 perseant dlog("sending blocks %d-%d", mc, mc + lim.blkcnt - 1);
1138 1.1 perseant if ((r = fcntl(fs->clfs_ifilefd, LFCNMARKV, &lim)) < 0) {
1139 1.1 perseant syslog(LOG_WARNING, "%s: markv returned %d (%m)",
1140 1.1 perseant fs->lfs_fsmnt, r);
1141 1.1 perseant if (errno != EAGAIN && errno != ESHUTDOWN) {
1142 1.1 perseant fd_release_all(fs->clfs_devvp);
1143 1.1 perseant return r;
1144 1.1 perseant }
1145 1.1 perseant }
1146 1.1 perseant }
1147 1.1 perseant
1148 1.1 perseant /*
1149 1.1 perseant * Report progress (or lack thereof)
1150 1.1 perseant */
1151 1.1 perseant syslog(LOG_INFO, "%s: wrote %" PRId64 " dirty + %"
1152 1.1 perseant PRId64 " supporting indirect + %"
1153 1.1 perseant PRId64 " supporting Ifile = %"
1154 1.1 perseant PRId64 " bytes to clean %d segs (%" PRId64 "%% recovery)",
1155 1.1 perseant fs->lfs_fsmnt, (int64_t)nb, (int64_t)(extra - if_extra),
1156 1.3 perseant (int64_t)if_extra, (int64_t)(nb + extra), ngood,
1157 1.3 perseant (ngood ? (int64_t)(100 - (100 * (nb + extra)) /
1158 1.3 perseant (ngood * fs->lfs_ssize)) :
1159 1.3 perseant (int64_t)0));
1160 1.1 perseant if (nb + extra >= ngood * fs->lfs_ssize)
1161 1.1 perseant syslog(LOG_WARNING, "%s: cleaner not making forward progress",
1162 1.1 perseant fs->lfs_fsmnt);
1163 1.1 perseant
1164 1.1 perseant /*
1165 1.1 perseant * Finally call reclaim to prompt cleaning of the segments.
1166 1.1 perseant */
1167 1.1 perseant fcntl(fs->clfs_ifilefd, LFCNRECLAIM, NULL);
1168 1.1 perseant
1169 1.1 perseant fd_release_all(fs->clfs_devvp);
1170 1.1 perseant return 0;
1171 1.1 perseant }
1172 1.1 perseant
1173 1.1 perseant /*
1174 1.1 perseant * Read the cleanerinfo block and apply cleaning policy to determine whether
1175 1.1 perseant * the given filesystem needs to be cleaned. Returns 1 if it does, 0 if it
1176 1.1 perseant * does not, or -1 on error.
1177 1.1 perseant */
1178 1.1 perseant int
1179 1.1 perseant needs_cleaning(struct clfs *fs, CLEANERINFO *cip)
1180 1.1 perseant {
1181 1.1 perseant struct ubuf *bp;
1182 1.1 perseant struct stat st;
1183 1.1 perseant daddr_t fsb_per_seg, max_free_segs;
1184 1.1 perseant time_t now;
1185 1.1 perseant double loadavg;
1186 1.1 perseant
1187 1.1 perseant /* If this fs is "on hold", don't clean it. */
1188 1.1 perseant if (fs->clfs_onhold)
1189 1.1 perseant return 0;
1190 1.1 perseant
1191 1.1 perseant /*
1192 1.1 perseant * Read the cleanerinfo block from the Ifile. We don't want
1193 1.1 perseant * the cached information, so invalidate the buffer before
1194 1.1 perseant * handing it back.
1195 1.1 perseant */
1196 1.1 perseant if (bread(fs->lfs_ivnode, 0, fs->lfs_bsize, NOCRED, &bp)) {
1197 1.1 perseant syslog(LOG_ERR, "%s: can't read inode", fs->lfs_fsmnt);
1198 1.1 perseant return -1;
1199 1.1 perseant }
1200 1.1 perseant *cip = *(CLEANERINFO *)bp->b_data; /* Structure copy */
1201 1.13 ad brelse(bp, B_INVAL);
1202 1.1 perseant cleaner_stats.bytes_read += fs->lfs_bsize;
1203 1.1 perseant
1204 1.1 perseant /*
1205 1.1 perseant * If the number of segments changed under us, reinit.
1206 1.1 perseant * We don't have to start over from scratch, however,
1207 1.1 perseant * since we don't hold any buffers.
1208 1.1 perseant */
1209 1.1 perseant if (fs->lfs_nseg != cip->clean + cip->dirty) {
1210 1.1 perseant if (reinit_fs(fs) < 0) {
1211 1.1 perseant /* The normal case for unmount */
1212 1.1 perseant syslog(LOG_NOTICE, "%s: filesystem unmounted", fs->lfs_fsmnt);
1213 1.1 perseant return -1;
1214 1.1 perseant }
1215 1.1 perseant syslog(LOG_NOTICE, "%s: nsegs changed", fs->lfs_fsmnt);
1216 1.1 perseant }
1217 1.1 perseant
1218 1.1 perseant /* Compute theoretical "free segments" maximum based on usage */
1219 1.1 perseant fsb_per_seg = segtod(fs, 1);
1220 1.1 perseant max_free_segs = MAX(cip->bfree, 0) / fsb_per_seg + fs->lfs_minfreeseg;
1221 1.1 perseant
1222 1.1 perseant dlog("%s: bfree = %d, avail = %d, clean = %d/%d",
1223 1.1 perseant fs->lfs_fsmnt, cip->bfree, cip->avail, cip->clean, fs->lfs_nseg);
1224 1.1 perseant
1225 1.1 perseant /* If the writer is waiting on us, clean it */
1226 1.11 perseant if (cip->clean <= fs->lfs_minfreeseg ||
1227 1.11 perseant (cip->flags & LFS_CLEANER_MUST_CLEAN))
1228 1.1 perseant return 1;
1229 1.1 perseant
1230 1.1 perseant /* If there are enough segments, don't clean it */
1231 1.1 perseant if (cip->bfree - cip->avail <= fsb_per_seg &&
1232 1.1 perseant cip->avail > fsb_per_seg)
1233 1.1 perseant return 0;
1234 1.1 perseant
1235 1.1 perseant /* If we are in dire straits, clean it */
1236 1.1 perseant if (cip->bfree - cip->avail > fsb_per_seg &&
1237 1.1 perseant cip->avail <= fsb_per_seg)
1238 1.1 perseant return 1;
1239 1.1 perseant
1240 1.1 perseant /* If under busy threshold, clean regardless of load */
1241 1.1 perseant if (cip->clean < max_free_segs * BUSY_LIM)
1242 1.1 perseant return 1;
1243 1.1 perseant
1244 1.1 perseant /* Check busy status; clean if idle and under idle limit */
1245 1.1 perseant if (use_fs_idle) {
1246 1.1 perseant /* Filesystem idle */
1247 1.1 perseant time(&now);
1248 1.1 perseant if (fstat(fs->clfs_ifilefd, &st) < 0) {
1249 1.1 perseant syslog(LOG_ERR, "%s: failed to stat ifile",
1250 1.1 perseant fs->lfs_fsmnt);
1251 1.1 perseant return -1;
1252 1.1 perseant }
1253 1.1 perseant if (now - st.st_mtime > segwait_timeout &&
1254 1.1 perseant cip->clean < max_free_segs * IDLE_LIM)
1255 1.1 perseant return 1;
1256 1.1 perseant } else {
1257 1.1 perseant /* CPU idle - use one-minute load avg */
1258 1.1 perseant if (getloadavg(&loadavg, 1) == -1) {
1259 1.1 perseant syslog(LOG_ERR, "%s: failed to get load avg",
1260 1.1 perseant fs->lfs_fsmnt);
1261 1.1 perseant return -1;
1262 1.1 perseant }
1263 1.1 perseant if (loadavg < load_threshold &&
1264 1.1 perseant cip->clean < max_free_segs * IDLE_LIM)
1265 1.1 perseant return 1;
1266 1.1 perseant }
1267 1.1 perseant
1268 1.1 perseant return 0;
1269 1.1 perseant }
1270 1.1 perseant
1271 1.1 perseant /*
1272 1.1 perseant * Report statistics. If the signal was SIGUSR2, clear the statistics too.
1273 1.1 perseant * If the signal was SIGINT, exit.
1274 1.1 perseant */
1275 1.1 perseant static void
1276 1.1 perseant sig_report(int sig)
1277 1.1 perseant {
1278 1.1 perseant double avg = 0.0, stddev;
1279 1.1 perseant
1280 1.1 perseant avg = cleaner_stats.util_tot / MAX(cleaner_stats.segs_cleaned, 1.0);
1281 1.1 perseant stddev = cleaner_stats.util_sos / MAX(cleaner_stats.segs_cleaned -
1282 1.1 perseant avg * avg, 1.0);
1283 1.1 perseant syslog(LOG_INFO, "bytes read: %" PRId64, cleaner_stats.bytes_read);
1284 1.1 perseant syslog(LOG_INFO, "bytes written: %" PRId64, cleaner_stats.bytes_written);
1285 1.1 perseant syslog(LOG_INFO, "segments cleaned: %" PRId64, cleaner_stats.segs_cleaned);
1286 1.1 perseant #if 0
1287 1.1 perseant /* "Empty segments" is meaningless, since the kernel handles those */
1288 1.1 perseant syslog(LOG_INFO, "empty segments: %" PRId64, cleaner_stats.segs_empty);
1289 1.1 perseant #endif
1290 1.1 perseant syslog(LOG_INFO, "error segments: %" PRId64, cleaner_stats.segs_error);
1291 1.1 perseant syslog(LOG_INFO, "utilization total: %g", cleaner_stats.util_tot);
1292 1.1 perseant syslog(LOG_INFO, "utilization sos: %g", cleaner_stats.util_sos);
1293 1.1 perseant syslog(LOG_INFO, "utilization avg: %4.2f", avg);
1294 1.1 perseant syslog(LOG_INFO, "utilization sdev: %9.6f", stddev);
1295 1.1 perseant
1296 1.1 perseant if (debug)
1297 1.1 perseant bufstats();
1298 1.1 perseant
1299 1.1 perseant if (sig == SIGUSR2)
1300 1.1 perseant memset(&cleaner_stats, 0, sizeof(cleaner_stats));
1301 1.1 perseant if (sig == SIGINT)
1302 1.1 perseant exit(0);
1303 1.1 perseant }
1304 1.1 perseant
1305 1.1 perseant static void
1306 1.1 perseant sig_exit(int sig)
1307 1.1 perseant {
1308 1.1 perseant exit(0);
1309 1.1 perseant }
1310 1.1 perseant
1311 1.1 perseant static void
1312 1.1 perseant usage(void)
1313 1.1 perseant {
1314 1.1 perseant errx(1, "usage: lfs_cleanerd [-bcdfmqs] [-i segnum] [-l load] "
1315 1.1 perseant "[-n nsegs] [-r report_freq] [-t timeout] fs_name ...");
1316 1.1 perseant }
1317 1.1 perseant
1318 1.1 perseant /*
1319 1.1 perseant * Main.
1320 1.1 perseant */
1321 1.1 perseant int
1322 1.1 perseant main(int argc, char **argv)
1323 1.1 perseant {
1324 1.1 perseant int i, opt, error, r, loopcount;
1325 1.1 perseant struct timeval tv;
1326 1.1 perseant CLEANERINFO ci;
1327 1.1 perseant #ifndef USE_CLIENT_SERVER
1328 1.1 perseant char *cp, *pidname;
1329 1.1 perseant #endif
1330 1.1 perseant
1331 1.1 perseant /*
1332 1.1 perseant * Set up defaults
1333 1.1 perseant */
1334 1.1 perseant atatime = 1;
1335 1.1 perseant segwait_timeout = 300; /* Five minutes */
1336 1.1 perseant load_threshold = 0.2;
1337 1.1 perseant stat_report = 0;
1338 1.1 perseant inval_segment = -1;
1339 1.1 perseant copylog_filename = NULL;
1340 1.1 perseant
1341 1.1 perseant /*
1342 1.1 perseant * Parse command-line arguments
1343 1.1 perseant */
1344 1.1 perseant while ((opt = getopt(argc, argv, "bC:cdfi:l:mn:qr:st:")) != -1) {
1345 1.1 perseant switch (opt) {
1346 1.1 perseant case 'b': /* Use bytes written, not segments read */
1347 1.1 perseant use_bytes = 1;
1348 1.1 perseant break;
1349 1.1 perseant case 'C': /* copy log */
1350 1.1 perseant copylog_filename = optarg;
1351 1.1 perseant break;
1352 1.1 perseant case 'c': /* Coalesce files */
1353 1.1 perseant do_coalesce++;
1354 1.1 perseant break;
1355 1.1 perseant case 'd': /* Debug mode. */
1356 1.1 perseant debug++;
1357 1.1 perseant break;
1358 1.1 perseant case 'f': /* Use fs idle time rather than cpu idle */
1359 1.1 perseant use_fs_idle = 1;
1360 1.1 perseant break;
1361 1.1 perseant case 'i': /* Invalidate this segment */
1362 1.1 perseant inval_segment = atoi(optarg);
1363 1.1 perseant break;
1364 1.1 perseant case 'l': /* Load below which to clean */
1365 1.1 perseant load_threshold = atof(optarg);
1366 1.1 perseant break;
1367 1.1 perseant case 'm': /* [compat only] */
1368 1.1 perseant break;
1369 1.1 perseant case 'n': /* How many segs to clean at once */
1370 1.1 perseant atatime = atoi(optarg);
1371 1.1 perseant break;
1372 1.1 perseant case 'q': /* Quit after one run */
1373 1.1 perseant do_quit = 1;
1374 1.1 perseant break;
1375 1.1 perseant case 'r': /* Report every stat_report segments */
1376 1.1 perseant stat_report = atoi(optarg);
1377 1.1 perseant break;
1378 1.1 perseant case 's': /* Small writes */
1379 1.1 perseant do_small = 1;
1380 1.1 perseant break;
1381 1.1 perseant case 't': /* timeout */
1382 1.1 perseant segwait_timeout = atoi(optarg);
1383 1.1 perseant break;
1384 1.1 perseant default:
1385 1.1 perseant usage();
1386 1.1 perseant /* NOTREACHED */
1387 1.1 perseant }
1388 1.1 perseant }
1389 1.1 perseant argc -= optind;
1390 1.1 perseant argv += optind;
1391 1.1 perseant
1392 1.1 perseant if (argc < 1)
1393 1.1 perseant usage();
1394 1.1 perseant if (inval_segment >= 0 && argc != 1) {
1395 1.1 perseant errx(1, "lfs_cleanerd: may only specify one filesystem when "
1396 1.1 perseant "using -i flag");
1397 1.1 perseant }
1398 1.1 perseant
1399 1.12 tls if (do_coalesce) {
1400 1.12 tls errx(1, "lfs_cleanerd: -c disabled due to reports of file "
1401 1.12 tls "corruption; you may re-enable it by rebuilding the "
1402 1.12 tls "cleaner");
1403 1.12 tls }
1404 1.12 tls
1405 1.1 perseant /*
1406 1.1 perseant * Set up daemon mode or verbose debug mode
1407 1.1 perseant */
1408 1.1 perseant if (debug) {
1409 1.1 perseant openlog("lfs_cleanerd", LOG_NDELAY | LOG_PID | LOG_PERROR,
1410 1.1 perseant LOG_DAEMON);
1411 1.1 perseant signal(SIGINT, sig_report);
1412 1.1 perseant } else {
1413 1.1 perseant if (daemon(0, 0) == -1)
1414 1.1 perseant err(1, "lfs_cleanerd: couldn't become a daemon!");
1415 1.1 perseant openlog("lfs_cleanerd", LOG_NDELAY | LOG_PID, LOG_DAEMON);
1416 1.1 perseant signal(SIGINT, sig_exit);
1417 1.1 perseant }
1418 1.1 perseant
1419 1.1 perseant /*
1420 1.1 perseant * Look for an already-running master daemon. If there is one,
1421 1.1 perseant * send it our filesystems to add to its list and exit.
1422 1.1 perseant * If there is none, become the master.
1423 1.1 perseant */
1424 1.1 perseant #ifdef USE_CLIENT_SERVER
1425 1.1 perseant try_to_become_master(argc, argv);
1426 1.1 perseant #else
1427 1.1 perseant /* XXX think about this */
1428 1.1 perseant asprintf(&pidname, "lfs_cleanerd:m:%s", argv[0]);
1429 1.1 perseant if (pidname == NULL) {
1430 1.1 perseant syslog(LOG_ERR, "malloc failed: %m");
1431 1.1 perseant exit(1);
1432 1.1 perseant }
1433 1.1 perseant for (cp = pidname; cp != NULL; cp = strchr(cp, '/'))
1434 1.1 perseant *cp = '|';
1435 1.1 perseant pidfile(pidname);
1436 1.1 perseant #endif
1437 1.1 perseant
1438 1.1 perseant /*
1439 1.1 perseant * Signals mean daemon should report its statistics
1440 1.1 perseant */
1441 1.1 perseant memset(&cleaner_stats, 0, sizeof(cleaner_stats));
1442 1.1 perseant signal(SIGUSR1, sig_report);
1443 1.1 perseant signal(SIGUSR2, sig_report);
1444 1.1 perseant
1445 1.1 perseant /*
1446 1.1 perseant * Start up buffer cache. We only use this for the Ifile,
1447 1.1 perseant * and we will resize it if necessary, so it can start small.
1448 1.1 perseant */
1449 1.1 perseant bufinit(4);
1450 1.1 perseant
1451 1.1 perseant #ifdef REPAIR_ZERO_FINFO
1452 1.1 perseant {
1453 1.1 perseant BLOCK_INFO *bip = NULL;
1454 1.1 perseant int bic = 0;
1455 1.1 perseant
1456 1.1 perseant nfss = 1;
1457 1.1 perseant fsp = (struct clfs **)malloc(sizeof(*fsp));
1458 1.1 perseant fsp[0] = (struct clfs *)calloc(1, sizeof(**fsp));
1459 1.1 perseant
1460 1.1 perseant if (init_unmounted_fs(fsp[0], argv[0]) < 0) {
1461 1.1 perseant err(1, "init_unmounted_fs");
1462 1.1 perseant }
1463 1.1 perseant dlog("Filesystem has %d segments", fsp[0]->lfs_nseg);
1464 1.1 perseant for (i = 0; i < fsp[0]->lfs_nseg; i++) {
1465 1.1 perseant load_segment(fsp[0], i, &bip, &bic);
1466 1.1 perseant bic = 0;
1467 1.1 perseant }
1468 1.1 perseant exit(0);
1469 1.1 perseant }
1470 1.1 perseant #endif
1471 1.1 perseant
1472 1.1 perseant /*
1473 1.1 perseant * Initialize cleaning structures, open devices, etc.
1474 1.1 perseant */
1475 1.1 perseant nfss = argc;
1476 1.1 perseant fsp = (struct clfs **)malloc(nfss * sizeof(*fsp));
1477 1.7 perseant if (fsp == NULL) {
1478 1.7 perseant syslog(LOG_ERR, "couldn't allocate fs table: %m");
1479 1.7 perseant exit(1);
1480 1.7 perseant }
1481 1.1 perseant for (i = 0; i < nfss; i++) {
1482 1.1 perseant fsp[i] = (struct clfs *)calloc(1, sizeof(**fsp));
1483 1.1 perseant if ((r = init_fs(fsp[i], argv[i])) < 0) {
1484 1.1 perseant syslog(LOG_ERR, "%s: couldn't init: error code %d",
1485 1.1 perseant argv[i], r);
1486 1.1 perseant handle_error(fsp, i);
1487 1.1 perseant --i; /* Do the new #i over again */
1488 1.1 perseant }
1489 1.1 perseant }
1490 1.1 perseant
1491 1.1 perseant /*
1492 1.1 perseant * If asked to coalesce, do so and exit.
1493 1.1 perseant */
1494 1.1 perseant if (do_coalesce) {
1495 1.1 perseant for (i = 0; i < nfss; i++)
1496 1.1 perseant clean_all_inodes(fsp[i]);
1497 1.1 perseant exit(0);
1498 1.1 perseant }
1499 1.1 perseant
1500 1.1 perseant /*
1501 1.1 perseant * If asked to invalidate a segment, do that and exit.
1502 1.1 perseant */
1503 1.1 perseant if (inval_segment >= 0) {
1504 1.1 perseant invalidate_segment(fsp[0], inval_segment);
1505 1.1 perseant exit(0);
1506 1.1 perseant }
1507 1.1 perseant
1508 1.1 perseant /*
1509 1.1 perseant * Main cleaning loop.
1510 1.1 perseant */
1511 1.1 perseant loopcount = 0;
1512 1.1 perseant while (nfss > 0) {
1513 1.1 perseant int cleaned_one;
1514 1.1 perseant do {
1515 1.1 perseant #ifdef USE_CLIENT_SERVER
1516 1.1 perseant check_control_socket();
1517 1.1 perseant #endif
1518 1.1 perseant cleaned_one = 0;
1519 1.1 perseant for (i = 0; i < nfss; i++) {
1520 1.1 perseant if ((error = needs_cleaning(fsp[i], &ci)) < 0) {
1521 1.1 perseant handle_error(fsp, i);
1522 1.1 perseant continue;
1523 1.1 perseant }
1524 1.1 perseant if (error == 0) /* No need to clean */
1525 1.1 perseant continue;
1526 1.1 perseant
1527 1.1 perseant reload_ifile(fsp[i]);
1528 1.1 perseant if (clean_fs(fsp[i], &ci) < 0) {
1529 1.1 perseant handle_error(fsp, i);
1530 1.1 perseant continue;
1531 1.1 perseant }
1532 1.1 perseant ++cleaned_one;
1533 1.1 perseant }
1534 1.1 perseant ++loopcount;
1535 1.1 perseant if (stat_report && loopcount % stat_report == 0)
1536 1.1 perseant sig_report(0);
1537 1.1 perseant if (do_quit)
1538 1.1 perseant exit(0);
1539 1.1 perseant } while(cleaned_one);
1540 1.1 perseant tv.tv_sec = segwait_timeout;
1541 1.1 perseant tv.tv_usec = 0;
1542 1.1 perseant fcntl(fsp[0]->clfs_ifilefd, LFCNSEGWAITALL, &tv);
1543 1.1 perseant }
1544 1.1 perseant
1545 1.1 perseant /* NOTREACHED */
1546 1.1 perseant return 0;
1547 1.1 perseant }
1548