summaryrefslogtreecommitdiff
path: root/libexec/lfs_cleanerd/lfs_cleanerd.c
blob: 1adb7480d6607f85787358a6071f190a035f5de2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
/* $NetBSD: lfs_cleanerd.c,v 1.60 2019/08/30 23:37:23 brad Exp $	 */

/*-
 * Copyright (c) 2005 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Konrad E. Schroder <perseant@hhhh.org>.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * The cleaner daemon for the NetBSD Log-structured File System.
 * Only tested for use with version 2 LFSs.
 */

#include <sys/syslog.h>
#include <sys/param.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <ufs/lfs/lfs.h>

#include <assert.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <semaphore.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <util.h>

#include "bufcache.h"
#include "vnode.h"
#include "lfs_user.h"
#include "fdfs.h"
#include "cleaner.h"
#include "kernelops.h"
#include "mount_lfs.h"

/*
 * Global variables.
 */
/* XXX these top few should really be fs-specific */
int use_fs_idle;	/* Use fs idle rather than cpu idle time */
int use_bytes;		/* Use bytes written rather than segments cleaned */
double load_threshold;	/* How idle is idle (CPU idle) */
int atatime;		/* How many segments (bytes) to clean at a time */

int nfss;		/* Number of filesystems monitored by this cleanerd */
struct clfs **fsp;	/* Array of extended filesystem structures */
int segwait_timeout;	/* Time to wait in lfs_segwait() */
int do_quit;		/* Quit after one cleaning loop */
int do_coalesce;	/* Coalesce filesystem */
int do_small;		/* Use small writes through markv */
char *do_asdevice;      /* Use this as the raw device */
char *copylog_filename; /* File to use for fs debugging analysis */
int inval_segment;	/* Segment to invalidate */
int stat_report;	/* Report statistics for this period of cycles */
int debug;		/* Turn on debugging */
struct cleaner_stats {
	double	util_tot;
	double	util_sos;
	off_t	bytes_read;
	off_t	bytes_written;
	off_t	segs_cleaned;
	off_t	segs_empty;
	off_t	segs_error;
} cleaner_stats;

extern u_int32_t cksum(void *, size_t);
extern u_int32_t lfs_sb_cksum(struct dlfs *);
extern u_int32_t lfs_cksum_part(void *, size_t, u_int32_t);
extern int ulfs_getlbns(struct lfs *, struct uvnode *, daddr_t, struct indir *, int *);

/* Ugh */
#define FSMNT_SIZE MAX(sizeof(((struct dlfs *)0)->dlfs_fsmnt), \
			sizeof(((struct dlfs64 *)0)->dlfs_fsmnt))


/* Compat */
void pwarn(const char *unused, ...) { /* Does nothing */ };

/*
 * Log a message if debugging is turned on.
 */
void
dlog(const char *fmt, ...)
{
	va_list ap;

	if (debug == 0)
		return;

	va_start(ap, fmt);
	vsyslog(LOG_DEBUG, fmt, ap);
	va_end(ap);
}

/*
 * Remove the specified filesystem from the list, due to its having
 * become unmounted or other error condition.
 */
void
handle_error(struct clfs **cfsp, int n)
{
	syslog(LOG_NOTICE, "%s: detaching cleaner", lfs_sb_getfsmnt(cfsp[n]));
	free(cfsp[n]);
	if (n != nfss - 1)
		cfsp[n] = cfsp[nfss - 1];
	--nfss;
}

/*
 * Reinitialize a filesystem if, e.g., its size changed.
 */
int
reinit_fs(struct clfs *fs)
{
	char fsname[FSMNT_SIZE];

	memcpy(fsname, lfs_sb_getfsmnt(fs), sizeof(fsname));
	fsname[sizeof(fsname) - 1] = '\0';

	kops.ko_close(fs->clfs_ifilefd);
	kops.ko_close(fs->clfs_devfd);
	fd_reclaim(fs->clfs_devvp);
	fd_reclaim(fs->lfs_ivnode);
	free(fs->clfs_dev);
	free(fs->clfs_segtab);
	free(fs->clfs_segtabp);

	return init_fs(fs, fsname);
}

#ifdef REPAIR_ZERO_FINFO
/*
 * Use fsck's lfs routines to load the Ifile from an unmounted fs.
 * We interpret "fsname" as the name of the raw disk device.
 */
int
init_unmounted_fs(struct clfs *fs, char *fsname)
{
	struct lfs *disc_fs;
	int i;

	fs->clfs_dev = fsname;
	if ((fs->clfs_devfd = kops.ko_open(fs->clfs_dev, O_RDWR)) < 0) {
		syslog(LOG_ERR, "couldn't open device %s read/write",
		       fs->clfs_dev);
		return -1;
	}

	disc_fs = lfs_init(fs->clfs_devfd, 0, 0, 0, 0);

	fs->lfs_dlfs = disc_fs->lfs_dlfs; /* Structure copy */
	strncpy(fs->lfs_fsmnt, fsname, MNAMELEN);
	fs->lfs_ivnode = (struct uvnode *)disc_fs->lfs_ivnode;
	fs->clfs_devvp = fd_vget(fs->clfs_devfd, fs->lfs_fsize, fs->lfs_ssize,
				 atatime);

	/* Allocate and clear segtab */
	fs->clfs_segtab = (struct clfs_seguse *)malloc(lfs_sb_getnseg(fs) *
						sizeof(*fs->clfs_segtab));
	fs->clfs_segtabp = (struct clfs_seguse **)malloc(lfs_sb_getnseg(fs) *
						sizeof(*fs->clfs_segtabp));
	for (i = 0; i < lfs_sb_getnseg(fs); i++) {
		fs->clfs_segtabp[i] = &(fs->clfs_segtab[i]);
		fs->clfs_segtab[i].flags = 0x0;
	}
	syslog(LOG_NOTICE, "%s: unmounted cleaner starting", fsname);

	return 0;
}
#endif

/*
 * Set up the file descriptors, including the Ifile descriptor.
 * If we can't get the Ifile, this is not an LFS (or the kernel is
 * too old to support the fcntl).
 * XXX Merge this and init_unmounted_fs, switching on whether
 * XXX "fsname" is a dir or a char special device.  Should
 * XXX also be able to read unmounted devices out of fstab, the way
 * XXX fsck does.
 */
int
init_fs(struct clfs *fs, char *fsname)
{
	char mnttmp[FSMNT_SIZE];
	struct statvfs sf;
	int rootfd;
	int i;
	void *sbuf;
	size_t mlen;

	if (do_asdevice != NULL) {
		fs->clfs_dev = strndup(do_asdevice,strlen(do_asdevice) + 2);
		if (fs->clfs_dev == NULL) {
			syslog(LOG_ERR, "couldn't malloc device name string: %m");
			return -1;
		}
	} else {
		/*
		 * Get the raw device from the block device.
		 * XXX this is ugly.  Is there a way to discover the raw device
		 * XXX for a given mount point?
		 */
		if (kops.ko_statvfs(fsname, &sf, ST_WAIT) < 0)
			return -1;
		mlen = strlen(sf.f_mntfromname) + 2;
		fs->clfs_dev = malloc(mlen);
		if (fs->clfs_dev == NULL) {
			syslog(LOG_ERR, "couldn't malloc device name string: %m");
			return -1;
		}
		if (getdiskrawname(fs->clfs_dev, mlen, sf.f_mntfromname) == NULL) {
			syslog(LOG_ERR, "couldn't convert '%s' to raw name: %m",
			    sf.f_mntfromname);
			return -1;
		}
	}
	if ((fs->clfs_devfd = kops.ko_open(fs->clfs_dev, O_RDONLY, 0)) < 0) {
		syslog(LOG_ERR, "couldn't open device %s for reading: %m",
			fs->clfs_dev);
		return -1;
	}

	/* Find the Ifile and open it */
	if ((rootfd = kops.ko_open(fsname, O_RDONLY, 0)) < 0)
		return -2;
	if (kops.ko_fcntl(rootfd, LFCNIFILEFH, &fs->clfs_ifilefh) < 0)
		return -3;
	if ((fs->clfs_ifilefd = kops.ko_fhopen(&fs->clfs_ifilefh,
	    sizeof(fs->clfs_ifilefh), O_RDONLY)) < 0)
		return -4;
	kops.ko_close(rootfd);

	sbuf = malloc(LFS_SBPAD);
	if (sbuf == NULL) {
		syslog(LOG_ERR, "couldn't malloc superblock buffer");
		return -1;
	}

	/* Load in the superblock */
	if (kops.ko_pread(fs->clfs_devfd, sbuf, LFS_SBPAD, LFS_LABELPAD) < 0) {
		free(sbuf);
		return -1;
	}

	__CTASSERT(sizeof(struct dlfs) == sizeof(struct dlfs64));
	memcpy(&fs->lfs_dlfs_u, sbuf, sizeof(struct dlfs));
	free(sbuf);

	/* If it is not LFS, complain and exit! */
	switch (fs->lfs_dlfs_u.u_32.dlfs_magic) {
	    case LFS_MAGIC:
		fs->lfs_is64 = false;
		fs->lfs_dobyteswap = false;
		break;
	    case LFS_MAGIC_SWAPPED:
		fs->lfs_is64 = false;
		fs->lfs_dobyteswap = true;
		break;
	    case LFS64_MAGIC:
		fs->lfs_is64 = true;
		fs->lfs_dobyteswap = false;
		break;
	    case LFS64_MAGIC_SWAPPED:
		fs->lfs_is64 = true;
		fs->lfs_dobyteswap = true;
		break;
	    default:
		syslog(LOG_ERR, "%s: not LFS", fsname);
		return -1;
	}
	/* XXX: can this ever need to be set? does the cleaner even care? */
	fs->lfs_hasolddirfmt = 0;

	/* If this is not a version 2 filesystem, complain and exit */
	if (lfs_sb_getversion(fs) != 2) {
		syslog(LOG_ERR, "%s: not a version 2 LFS", fsname);
		return -1;
	}

	/* Assume fsname is the mounted name */
	strncpy(mnttmp, fsname, sizeof(mnttmp));
	mnttmp[sizeof(mnttmp) - 1] = '\0';
	lfs_sb_setfsmnt(fs, mnttmp);

	/* Set up vnodes for Ifile and raw device */
	fs->lfs_ivnode = fd_vget(fs->clfs_ifilefd, lfs_sb_getbsize(fs), 0, 0);
	fs->clfs_devvp = fd_vget(fs->clfs_devfd, lfs_sb_getfsize(fs), lfs_sb_getssize(fs),
				 atatime);

	/* Allocate and clear segtab */
	fs->clfs_segtab = (struct clfs_seguse *)malloc(lfs_sb_getnseg(fs) *
						sizeof(*fs->clfs_segtab));
	fs->clfs_segtabp = (struct clfs_seguse **)malloc(lfs_sb_getnseg(fs) *
						sizeof(*fs->clfs_segtabp));
	if (fs->clfs_segtab == NULL || fs->clfs_segtabp == NULL) {
		syslog(LOG_ERR, "%s: couldn't malloc segment table: %m",
			fs->clfs_dev);
		return -1;
	}

	for (i = 0; i < lfs_sb_getnseg(fs); i++) {
		fs->clfs_segtabp[i] = &(fs->clfs_segtab[i]);
		fs->clfs_segtab[i].flags = 0x0;
	}

	syslog(LOG_NOTICE, "%s: attaching cleaner", fsname);
	return 0;
}

/*
 * Invalidate all the currently held Ifile blocks so they will be
 * reread when we clean.  Check the size while we're at it, and
 * resize the buffer cache if necessary.
 */
void
reload_ifile(struct clfs *fs)
{
	struct ubuf *bp;
	struct stat st;
	int ohashmax;
	extern int hashmax;

	while ((bp = LIST_FIRST(&fs->lfs_ivnode->v_dirtyblkhd)) != NULL) {
		bremfree(bp);
		buf_destroy(bp);
	}
	while ((bp = LIST_FIRST(&fs->lfs_ivnode->v_cleanblkhd)) != NULL) {
		bremfree(bp);
		buf_destroy(bp);
	}

	/* If Ifile is larger than buffer cache, rehash */
	fstat(fs->clfs_ifilefd, &st);
	if (st.st_size / lfs_sb_getbsize(fs) > hashmax) {
		ohashmax = hashmax;
		bufrehash(st.st_size / lfs_sb_getbsize(fs));
		dlog("%s: resized buffer hash from %d to %d",
		     lfs_sb_getfsmnt(fs), ohashmax, hashmax);
	}
}

/*
 * Get IFILE entry for the given inode, store in ifpp.	The buffer
 * which contains that data is returned in bpp, and must be brelse()d
 * by the caller.
 *
 * XXX this is cutpaste of LFS_IENTRY from lfs.h; unify the two.
 */
void
lfs_ientry(IFILE **ifpp, struct clfs *fs, ino_t ino, struct ubuf **bpp)
{
	IFILE64 *ifp64;
	IFILE32 *ifp32;
	IFILE_V1 *ifp_v1;
	int error;

	error = bread(fs->lfs_ivnode,
		      ino / lfs_sb_getifpb(fs) + lfs_sb_getcleansz(fs) +
		      lfs_sb_getsegtabsz(fs), lfs_sb_getbsize(fs), 0, bpp);
	if (error)
		syslog(LOG_ERR, "%s: ientry failed for ino %d",
			lfs_sb_getfsmnt(fs), (int)ino);
	if (fs->lfs_is64) {
		ifp64 = (IFILE64 *)(*bpp)->b_data;
		ifp64 += ino % lfs_sb_getifpb(fs);
		*ifpp = (IFILE *)ifp64;
	} else if (lfs_sb_getversion(fs) > 1) {
		ifp32 = (IFILE32 *)(*bpp)->b_data;
		ifp32 += ino % lfs_sb_getifpb(fs);
		*ifpp = (IFILE *)ifp32;
	} else {
		ifp_v1 = (IFILE_V1 *)(*bpp)->b_data;
		ifp_v1 += ino % lfs_sb_getifpb(fs);
		*ifpp = (IFILE *)ifp_v1;
	}
	return;
}

#ifdef TEST_PATTERN
/*
 * Check ULFS_ROOTINO for file data.  The assumption is that we are running
 * the "twofiles" test with the rest of the filesystem empty.  Files
 * created by "twofiles" match the test pattern, but ULFS_ROOTINO and the
 * executable itself (assumed to be inode 3) should not match.
 */
static void
check_test_pattern(BLOCK_INFO *bip)
{
	int j;
	unsigned char *cp = bip->bi_bp;

	/* Check inode sanity */
	if (bip->bi_lbn == LFS_UNUSED_LBN) {
		assert(((struct ulfs1_dinode *)bip->bi_bp)->di_inumber ==
			bip->bi_inode);
	}

	/* These can have the test pattern and it's all good */
	if (bip->bi_inode > 3)
		return;

	for (j = 0; j < bip->bi_size; j++) {
		if (cp[j] != (j & 0xff))
			break;
	}
	assert(j < bip->bi_size);
}
#endif /* TEST_PATTERN */

/*
 * Parse the partial segment at daddr, adding its information to
 * bip.	 Return the address of the next partial segment to read.
 */
static daddr_t
parse_pseg(struct clfs *fs, daddr_t daddr, BLOCK_INFO **bipp, int *bic)
{
	SEGSUM *ssp;
	IFILE *ifp;
	BLOCK_INFO *bip, *nbip;
	daddr_t idaddr, odaddr;
	FINFO *fip;
	IINFO *iip;
	struct ubuf *ifbp;
	union lfs_dinode *dip;
	u_int32_t ck, vers;
	int fic, inoc, obic;
	size_t sumstart;
	int i;
	char *cp;

	odaddr = daddr;
	obic = *bic;
	bip = *bipp;

	/*
	 * Retrieve the segment header, set up the SEGSUM pointer
	 * as well as the first FINFO and inode address pointer.
	 */
	cp = fd_ptrget(fs->clfs_devvp, daddr);
	ssp = (SEGSUM *)cp;
	iip = SEGSUM_IINFOSTART(fs, cp);
	fip = SEGSUM_FINFOBASE(fs, cp);

	/*
	 * Check segment header magic and checksum
	 */
	if (lfs_ss_getmagic(fs, ssp) != SS_MAGIC) {
		syslog(LOG_WARNING, "%s: sumsum magic number bad at 0x%jx:"
		       " read 0x%x, expected 0x%x", lfs_sb_getfsmnt(fs),
		       (intmax_t)daddr, lfs_ss_getmagic(fs, ssp), SS_MAGIC);
		return 0x0;
	}
	sumstart = lfs_ss_getsumstart(fs);
	ck = cksum((char *)ssp + sumstart, lfs_sb_getsumsize(fs) - sumstart);
	if (ck != lfs_ss_getsumsum(fs, ssp)) {
		syslog(LOG_WARNING, "%s: sumsum checksum mismatch at 0x%jx:"
		       " read 0x%x, computed 0x%x", lfs_sb_getfsmnt(fs),
		       (intmax_t)daddr, lfs_ss_getsumsum(fs, ssp), ck);
		return 0x0;
	}

	/* Initialize data sum */
	ck = 0;

	/* Point daddr at next block after segment summary */
	++daddr;

	/*
	 * Loop over file info and inode pointers.  We always move daddr
	 * forward here because we are also computing the data checksum
	 * as we go.
	 */
	fic = inoc = 0;
	while (fic < lfs_ss_getnfinfo(fs, ssp) || inoc < lfs_ss_getninos(fs, ssp)) {
		/*
		 * We must have either a file block or an inode block.
		 * If we don't have either one, it's an error.
		 */
		if (fic >= lfs_ss_getnfinfo(fs, ssp) && lfs_ii_getblock(fs, iip) != daddr) {
			syslog(LOG_WARNING, "%s: bad pseg at %jx (seg %d)",
			       lfs_sb_getfsmnt(fs), (intmax_t)odaddr, lfs_dtosn(fs, odaddr));
			*bipp = bip;
			return 0x0;
		}

		/*
		 * Note each inode from the inode blocks
		 */
		if (inoc < lfs_ss_getninos(fs, ssp) && lfs_ii_getblock(fs, iip) == daddr) {
			cp = fd_ptrget(fs->clfs_devvp, daddr);
			ck = lfs_cksum_part(cp, sizeof(u_int32_t), ck);
			for (i = 0; i < lfs_sb_getinopb(fs); i++) {
				dip = DINO_IN_BLOCK(fs, cp, i);
				if (lfs_dino_getinumber(fs, dip) == 0)
					break;

				/*
				 * Check currency before adding it
				 */
#ifndef REPAIR_ZERO_FINFO
				lfs_ientry(&ifp, fs, lfs_dino_getinumber(fs, dip), &ifbp);
				idaddr = lfs_if_getdaddr(fs, ifp);
				brelse(ifbp, 0);
				if (idaddr != daddr)
#endif
					continue;

				/*
				 * A current inode.  Add it.
				 */
				++*bic;
				nbip = (BLOCK_INFO *)realloc(bip, *bic *
							     sizeof(*bip));
				if (nbip)
					bip = nbip;
				else {
					--*bic;
					*bipp = bip;
					return 0x0;
				}
				bip[*bic - 1].bi_inode = lfs_dino_getinumber(fs, dip);
				bip[*bic - 1].bi_lbn = LFS_UNUSED_LBN;
				bip[*bic - 1].bi_daddr = daddr;
				bip[*bic - 1].bi_segcreate = lfs_ss_getcreate(fs, ssp);
				bip[*bic - 1].bi_version = lfs_dino_getgen(fs, dip);
				bip[*bic - 1].bi_bp = dip;
				bip[*bic - 1].bi_size = DINOSIZE(fs);
			}
			inoc += i;
			daddr += lfs_btofsb(fs, lfs_sb_getibsize(fs));
			iip = NEXTLOWER_IINFO(fs, iip);
			continue;
		}

		/*
		 * Note each file block from the finfo blocks
		 */
		if (fic >= lfs_ss_getnfinfo(fs, ssp))
			continue;

		/* Count this finfo, whether or not we use it */
		++fic;

		/*
		 * If this finfo has nblocks==0, it was written wrong.
		 * Kernels with this problem always wrote this zero-sized
		 * finfo last, so just ignore it.
		 */
		if (lfs_fi_getnblocks(fs, fip) == 0) {
#ifdef REPAIR_ZERO_FINFO
			struct ubuf *nbp;
			SEGSUM *nssp;

			syslog(LOG_WARNING, "fixing short FINFO at %jx (seg %d)",
			       (intmax_t)odaddr, lfs_dtosn(fs, odaddr));
			bread(fs->clfs_devvp, odaddr, lfs_sb_getfsize(fs),
			    0, &nbp);
			nssp = (SEGSUM *)nbp->b_data;
			--nssp->ss_nfinfo;
			nssp->ss_sumsum = cksum(&nssp->ss_datasum,
				lfs_sb_getsumsize(fs) - sizeof(nssp->ss_sumsum));
			bwrite(nbp);
#endif
			syslog(LOG_WARNING, "zero-length FINFO at %jx (seg %d)",
			       (intmax_t)odaddr, lfs_dtosn(fs, odaddr));
			continue;
		}

		/*
		 * Check currency before adding blocks
		 */
#ifdef REPAIR_ZERO_FINFO
		vers = -1;
#else
		lfs_ientry(&ifp, fs, lfs_fi_getino(fs, fip), &ifbp);
		vers = lfs_if_getversion(fs, ifp);
		brelse(ifbp, 0);
#endif
		if (vers != lfs_fi_getversion(fs, fip)) {
			size_t size;

			/* Read all the blocks from the data summary */
			for (i = 0; i < lfs_fi_getnblocks(fs, fip); i++) {
				size = (i == lfs_fi_getnblocks(fs, fip) - 1) ?
					lfs_fi_getlastlength(fs, fip) : lfs_sb_getbsize(fs);
				cp = fd_ptrget(fs->clfs_devvp, daddr);
				ck = lfs_cksum_part(cp, sizeof(u_int32_t), ck);
				daddr += lfs_btofsb(fs, size);
			}
			fip = NEXT_FINFO(fs, fip);
			continue;
		}

		/* Add all the blocks from the finfos (current or not) */
		nbip = (BLOCK_INFO *)realloc(bip, (*bic + lfs_fi_getnblocks(fs, fip)) *
					     sizeof(*bip));
		if (nbip)
			bip = nbip;
		else {
			*bipp = bip;
			return 0x0;
		}

		for (i = 0; i < lfs_fi_getnblocks(fs, fip); i++) {
			bip[*bic + i].bi_inode = lfs_fi_getino(fs, fip);
			bip[*bic + i].bi_lbn = lfs_fi_getblock(fs, fip, i);
			bip[*bic + i].bi_daddr = daddr;
			bip[*bic + i].bi_segcreate = lfs_ss_getcreate(fs, ssp);
			bip[*bic + i].bi_version = lfs_fi_getversion(fs, fip);
			bip[*bic + i].bi_size = (i == lfs_fi_getnblocks(fs, fip) - 1) ?
				lfs_fi_getlastlength(fs, fip) : lfs_sb_getbsize(fs);
			cp = fd_ptrget(fs->clfs_devvp, daddr);
			ck = lfs_cksum_part(cp, sizeof(u_int32_t), ck);
			bip[*bic + i].bi_bp = cp;
			daddr += lfs_btofsb(fs, bip[*bic + i].bi_size);

#ifdef TEST_PATTERN
			check_test_pattern(bip + *bic + i); /* XXXDEBUG */
#endif
		}
		*bic += lfs_fi_getnblocks(fs, fip);
		fip = NEXT_FINFO(fs, fip);
	}

#ifndef REPAIR_ZERO_FINFO
	if (lfs_ss_getdatasum(fs, ssp) != ck) {
		syslog(LOG_WARNING, "%s: data checksum bad at 0x%jx:"
		       " read 0x%x, computed 0x%x", lfs_sb_getfsmnt(fs),
		       (intmax_t)odaddr,
		       lfs_ss_getdatasum(fs, ssp), ck);
		*bic = obic;
		return 0x0;
	}
#endif

	*bipp = bip;
	return daddr;
}

static void
log_segment_read(struct clfs *fs, int sn)
{
        FILE *fp;
	char *cp;
	
        /*
         * Write the segment read, and its contents, into a log file in
         * the current directory.  We don't need to log the location of
         * the segment, since that can be inferred from the segments up
	 * to this point (ss_nextseg field of the previously written segment).
	 *
	 * We can use this info later to reconstruct the filesystem at any
	 * given point in time for analysis, by replaying the log forward
	 * indexed by the segment serial numbers; but it is not suitable
	 * for everyday use since the copylog will be simply enormous.
         */
	cp = fd_ptrget(fs->clfs_devvp, lfs_sntod(fs, sn));

        fp = fopen(copylog_filename, "ab");
        if (fp != NULL) {
                if (fwrite(cp, (size_t)lfs_sb_getssize(fs), 1, fp) != 1) {
                        perror("writing segment to copy log");
                }
        }
        fclose(fp);
}

/*
 * Read a segment to populate the BLOCK_INFO structures.
 * Return the number of partial segments read and parsed.
 */
int
load_segment(struct clfs *fs, int sn, BLOCK_INFO **bipp, int *bic)
{
	daddr_t daddr;
	int i, npseg;

	daddr = lfs_sntod(fs, sn);
	if (daddr < lfs_btofsb(fs, LFS_LABELPAD))
		daddr = lfs_btofsb(fs, LFS_LABELPAD);
	for (i = 0; i < LFS_MAXNUMSB; i++) {
		if (lfs_sb_getsboff(fs, i) == daddr) {
			daddr += lfs_btofsb(fs, LFS_SBPAD);
			break;
		}
	}

	/* Preload the segment buffer */
	if (fd_preload(fs->clfs_devvp, lfs_sntod(fs, sn)) < 0)
		return -1;

	if (copylog_filename)
		log_segment_read(fs, sn);

	/* Note bytes read for stats */
	cleaner_stats.segs_cleaned++;
	cleaner_stats.bytes_read += lfs_sb_getssize(fs);
	++fs->clfs_nactive;

	npseg = 0;
	while(lfs_dtosn(fs, daddr) == sn &&
	      lfs_dtosn(fs, daddr + lfs_btofsb(fs, lfs_sb_getbsize(fs))) == sn) {
		daddr = parse_pseg(fs, daddr, bipp, bic);
		if (daddr == 0x0) {
			++cleaner_stats.segs_error;
			break;
		}
		++npseg;
	}

	return npseg;
}

void
calc_cb(struct clfs *fs, int sn, struct clfs_seguse *t)
{
	time_t now;
	int64_t age, benefit, cost;

	time(&now);
	age = (now < t->lastmod ? 0 : now - t->lastmod);

	/* Under no circumstances clean active or already-clean segments */
	if ((t->flags & SEGUSE_ACTIVE) || !(t->flags & SEGUSE_DIRTY)) {
		t->priority = 0;
		return;
	}

	/*
	 * If the segment is empty, there is no reason to clean it.
	 * Clear its error condition, if any, since we are never going to
	 * try to parse this one.
	 */
	if (t->nbytes == 0) {
		t->flags &= ~SEGUSE_ERROR; /* Strip error once empty */
		t->priority = 0;
		return;
	}

	if (t->flags & SEGUSE_ERROR) {	/* No good if not already empty */
		/* No benefit */
		t->priority = 0;
		return;
	}

	if (t->nbytes > lfs_sb_getssize(fs)) {
		/* Another type of error */
		syslog(LOG_WARNING, "segment %d: bad seguse count %d",
		       sn, t->nbytes);
		t->flags |= SEGUSE_ERROR;
		t->priority = 0;
		return;
	}

	/*
	 * The non-degenerate case.  Use Rosenblum's cost-benefit algorithm.
	 * Calculate the benefit from cleaning this segment (one segment,
	 * minus fragmentation, dirty blocks and a segment summary block)
	 * and weigh that against the cost (bytes read plus bytes written).
	 * We count the summary headers as "dirty" to avoid cleaning very
	 * old and very full segments.
	 */
	benefit = (int64_t)lfs_sb_getssize(fs) - t->nbytes -
		  (t->nsums + 1) * lfs_sb_getfsize(fs);
	if (lfs_sb_getbsize(fs) > lfs_sb_getfsize(fs)) /* fragmentation */
		benefit -= (lfs_sb_getbsize(fs) / 2);
	if (benefit <= 0) {
		t->priority = 0;
		return;
	}

	cost = lfs_sb_getssize(fs) + t->nbytes;
	t->priority = (256 * benefit * age) / cost;

	return;
}

/*
 * Comparator for BLOCK_INFO structures.  Anything not in one of the segments
 * we're looking at sorts higher; after that we sort first by inode number
 * and then by block number (unsigned, i.e., negative sorts higher) *but*
 * sort inodes before data blocks.
 */
static int
bi_comparator(const void *va, const void *vb)
{
	const BLOCK_INFO *a, *b;

	a = (const BLOCK_INFO *)va;
	b = (const BLOCK_INFO *)vb;

	/* Check for out-of-place block */
	if (a->bi_segcreate == a->bi_daddr &&
	    b->bi_segcreate != b->bi_daddr)
		return -1;
	if (a->bi_segcreate != a->bi_daddr &&
	    b->bi_segcreate == b->bi_daddr)
		return 1;
	if (a->bi_size <= 0 && b->bi_size > 0)
		return 1;
	if (b->bi_size <= 0 && a->bi_size > 0)
		return -1;

	/* Check inode number */
	if (a->bi_inode != b->bi_inode)
		return a->bi_inode - b->bi_inode;

	/* Check lbn */
	if (a->bi_lbn == LFS_UNUSED_LBN) /* Inodes sort lower than blocks */
		return -1;
	if (b->bi_lbn == LFS_UNUSED_LBN)
		return 1;
	if ((u_int64_t)a->bi_lbn > (u_int64_t)b->bi_lbn)
		return 1;
	else
		return -1;

	return 0;
}

/*
 * Comparator for sort_segments: cost-benefit equation.
 */
static int
cb_comparator(const void *va, const void *vb)
{
	const struct clfs_seguse *a, *b;

	a = *(const struct clfs_seguse * const *)va;
	b = *(const struct clfs_seguse * const *)vb;
	return a->priority > b->priority ? -1 : 1;
}

void
toss_old_blocks(struct clfs *fs, BLOCK_INFO **bipp, blkcnt_t *bic, int *sizep)
{
	blkcnt_t i;
	int r;
	BLOCK_INFO *bip = *bipp;
	struct lfs_fcntl_markv /* {
		BLOCK_INFO *blkiov;
		int blkcnt;
	} */ lim;

	if (bic == 0 || bip == NULL)
		return;

	/*
	 * Kludge: Store the disk address in segcreate so we know which
	 * ones to toss.
	 */
	for (i = 0; i < *bic; i++)
		bip[i].bi_segcreate = bip[i].bi_daddr;

	/*
	 * XXX: blkcnt_t is 64 bits, so *bic might overflow size_t
	 * (the argument type of heapsort's number argument) on a
	 * 32-bit platform. However, if so we won't have got this far
	 * because we'll have failed trying to allocate the array. So
	 * while *bic here might cause a 64->32 truncation, it's safe.
	 */
	/* Sort the blocks */
	heapsort(bip, *bic, sizeof(BLOCK_INFO), bi_comparator);

	/* Use bmapv to locate the blocks */
	lim.blkiov = bip;
	lim.blkcnt = *bic;
	if ((r = kops.ko_fcntl(fs->clfs_ifilefd, LFCNBMAPV, &lim)) < 0) {
		syslog(LOG_WARNING, "%s: bmapv returned %d (%m)",
		       lfs_sb_getfsmnt(fs), r);
		return;
	}

	/* Toss blocks not in this segment */
	heapsort(bip, *bic, sizeof(BLOCK_INFO), bi_comparator);

	/* Get rid of stale blocks */
	if (sizep)
		*sizep = 0;
	for (i = 0; i < *bic; i++) {
		if (bip[i].bi_segcreate != bip[i].bi_daddr)
			break;
		if (sizep)
			*sizep += bip[i].bi_size;
	}
	*bic = i; /* XXX should we shrink bip? */
	*bipp = bip;

	return;
}

/*
 * Clean a segment and mark it invalid.
 */
int
invalidate_segment(struct clfs *fs, int sn)
{
	BLOCK_INFO *bip;
	int i, r, bic;
	blkcnt_t widebic;
	off_t nb;
	double util;
	struct lfs_fcntl_markv /* {
		BLOCK_INFO *blkiov;
		int blkcnt;
	} */ lim;

	dlog("%s: inval seg %d", lfs_sb_getfsmnt(fs), sn);

	bip = NULL;
	bic = 0;
	fs->clfs_nactive = 0;
	if (load_segment(fs, sn, &bip, &bic) <= 0)
		return -1;
	widebic = bic;
	toss_old_blocks(fs, &bip, &widebic, NULL);
	bic = widebic;

	/* Record statistics */
	for (i = nb = 0; i < bic; i++)
		nb += bip[i].bi_size;
	util = ((double)nb) / (fs->clfs_nactive * lfs_sb_getssize(fs));
	cleaner_stats.util_tot += util;
	cleaner_stats.util_sos += util * util;
	cleaner_stats.bytes_written += nb;

	/*
	 * Use markv to move the blocks.
	 */
	lim.blkiov = bip;
	lim.blkcnt = bic;
	if ((r = kops.ko_fcntl(fs->clfs_ifilefd, LFCNMARKV, &lim)) < 0) {
		syslog(LOG_WARNING, "%s: markv returned %d (%m) "
		       "for seg %d", lfs_sb_getfsmnt(fs), r, sn);
		return r;
	}

	/*
	 * Finally call invalidate to invalidate the segment.
	 */
	if ((r = kops.ko_fcntl(fs->clfs_ifilefd, LFCNINVAL, &sn)) < 0) {
		syslog(LOG_WARNING, "%s: inval returned %d (%m) "
		       "for seg %d", lfs_sb_getfsmnt(fs), r, sn);
		return r;
	}

	return 0;
}

/*
 * Check to see if the given ino/lbn pair is represented in the BLOCK_INFO
 * array we are sending to the kernel, or if the kernel will have to add it.
 * The kernel will only add each such pair once, though, so keep track of
 * previous requests in a separate "extra" BLOCK_INFO array.  Returns 1
 * if the block needs to be added, 0 if it is already represented.
 */
static int
check_or_add(ino_t ino, daddr_t lbn, BLOCK_INFO *bip, int bic, BLOCK_INFO **ebipp, int *ebicp)
{
	BLOCK_INFO *t, *ebip = *ebipp;
	int ebic = *ebicp;
	int k;

	for (k = 0; k < bic; k++) {
		if (bip[k].bi_inode != ino)
			break;
		if (bip[k].bi_lbn == lbn) {
			return 0;
		}
	}

	/* Look on the list of extra blocks, too */
	for (k = 0; k < ebic; k++) {
		if (ebip[k].bi_inode == ino && ebip[k].bi_lbn == lbn) {
			return 0;
		}
	}

	++ebic;
	t = realloc(ebip, ebic * sizeof(BLOCK_INFO));
	if (t == NULL)
		return 1; /* Note *ebicp is unchanged */

	ebip = t;
	ebip[ebic - 1].bi_inode = ino;
	ebip[ebic - 1].bi_lbn = lbn;

	*ebipp = ebip;
	*ebicp = ebic;
	return 1;
}

/*
 * Look for indirect blocks we will have to write which are not
 * contained in this collection of blocks.  This constitutes
 * a hidden cleaning cost, since we are unaware of it until we
 * have already read the segments.  Return the total cost, and fill
 * in *ifc with the part of that cost due to rewriting the Ifile.
 */
static off_t
check_hidden_cost(struct clfs *fs, BLOCK_INFO *bip, int bic, off_t *ifc)
{
	int start;
	struct indir in[ULFS_NIADDR + 1];
	int num;
	int i, j, ebic;
	BLOCK_INFO *ebip;
	daddr_t lbn;

	start = 0;
	ebip = NULL;
	ebic = 0;
	for (i = 0; i < bic; i++) {
		if (i == 0 || bip[i].bi_inode != bip[start].bi_inode) {
			start = i;
			/*
			 * Look for IFILE blocks, unless this is the Ifile.
			 */
			if (bip[i].bi_inode != LFS_IFILE_INUM) {
				lbn = lfs_sb_getcleansz(fs) + bip[i].bi_inode /
							lfs_sb_getifpb(fs);
				*ifc += check_or_add(LFS_IFILE_INUM, lbn,
						     bip, bic, &ebip, &ebic);
			}
		}
		if (bip[i].bi_lbn == LFS_UNUSED_LBN)
			continue;
		if (bip[i].bi_lbn < ULFS_NDADDR)
			continue;

		/* XXX the struct lfs cast is completely wrong/unsafe */
		ulfs_getlbns((struct lfs *)fs, NULL, (daddr_t)bip[i].bi_lbn, in, &num);
		for (j = 0; j < num; j++) {
			check_or_add(bip[i].bi_inode, in[j].in_lbn,
				     bip + start, bic - start, &ebip, &ebic);
		}
	}
	return ebic;
}

/*
 * Select segments to clean, add blocks from these segments to a cleaning
 * list, and send this list through lfs_markv() to move them to new
 * locations on disk.
 */
static int
clean_fs(struct clfs *fs, const CLEANERINFO64 *cip)
{
	int i, j, ngood, sn, bic, r, npos;
	blkcnt_t widebic;
	int bytes, totbytes;
	struct ubuf *bp;
	SEGUSE *sup;
	static BLOCK_INFO *bip;
	struct lfs_fcntl_markv /* {
		BLOCK_INFO *blkiov;
		int blkcnt;
	} */ lim;
	int mc;
	BLOCK_INFO *mbip;
	int inc;
	off_t nb;
	off_t goal;
	off_t extra, if_extra;
	double util;

	/* Read the segment table into our private structure */
	npos = 0;
	for (i = 0; i < lfs_sb_getnseg(fs); i+= lfs_sb_getsepb(fs)) {
		bread(fs->lfs_ivnode,
		      lfs_sb_getcleansz(fs) + i / lfs_sb_getsepb(fs),
		      lfs_sb_getbsize(fs), 0, &bp);
		for (j = 0; j < lfs_sb_getsepb(fs) && i + j < lfs_sb_getnseg(fs); j++) {
			sup = ((SEGUSE *)bp->b_data) + j;
			fs->clfs_segtab[i + j].nbytes  = sup->su_nbytes;
			fs->clfs_segtab[i + j].nsums = sup->su_nsums;
			fs->clfs_segtab[i + j].lastmod = sup->su_lastmod;
			/* Keep error status but renew other flags */
			fs->clfs_segtab[i + j].flags  &= SEGUSE_ERROR;
			fs->clfs_segtab[i + j].flags  |= sup->su_flags;

			/* Compute cost-benefit coefficient */
			calc_cb(fs, i + j, fs->clfs_segtab + i + j);
			if (fs->clfs_segtab[i + j].priority > 0)
				++npos;
		}
		brelse(bp, 0);
	}

	/* Sort segments based on cleanliness, fulness, and condition */
	heapsort(fs->clfs_segtabp, lfs_sb_getnseg(fs), sizeof(struct clfs_seguse *),
		 cb_comparator);

	/* If no segment is cleanable, just return */
	if (fs->clfs_segtabp[0]->priority == 0) {
		dlog("%s: no segment cleanable", lfs_sb_getfsmnt(fs));
		return 0;
	}

	/* Load some segments' blocks into bip */
	bic = 0;
	fs->clfs_nactive = 0;
	ngood = 0;
	if (use_bytes) {
		/* Set attainable goal */
		goal = lfs_sb_getssize(fs) * atatime;
		if (goal > (cip->clean - 1) * lfs_sb_getssize(fs) / 2)
			goal = MAX((cip->clean - 1) * lfs_sb_getssize(fs),
				   lfs_sb_getssize(fs)) / 2;

		dlog("%s: cleaning with goal %" PRId64
		     " bytes (%d segs clean, %d cleanable)",
		     lfs_sb_getfsmnt(fs), goal, cip->clean, npos);
		syslog(LOG_INFO, "%s: cleaning with goal %" PRId64
		       " bytes (%d segs clean, %d cleanable)",
		       lfs_sb_getfsmnt(fs), goal, cip->clean, npos);
		totbytes = 0;
		for (i = 0; i < lfs_sb_getnseg(fs) && totbytes < goal; i++) {
			if (fs->clfs_segtabp[i]->priority == 0)
				break;
			/* Upper bound on number of segments at once */
			if (ngood * lfs_sb_getssize(fs) > 4 * goal)
				break;
			sn = (fs->clfs_segtabp[i] - fs->clfs_segtab);
			dlog("%s: add seg %d prio %" PRIu64
			     " containing %ld bytes",
			     lfs_sb_getfsmnt(fs), sn, fs->clfs_segtabp[i]->priority,
			     fs->clfs_segtabp[i]->nbytes);
			if ((r = load_segment(fs, sn, &bip, &bic)) > 0) {
				++ngood;
				widebic = bic;
				toss_old_blocks(fs, &bip, &widebic, &bytes);
				bic = widebic;
				totbytes += bytes;
			} else if (r == 0)
				fd_release(fs->clfs_devvp);
			else
				break;
		}
	} else {
		/* Set attainable goal */
		goal = atatime;
		if (goal > cip->clean - 1)
			goal = MAX(cip->clean - 1, 1);

		dlog("%s: cleaning with goal %d segments (%d clean, %d cleanable)",
		       lfs_sb_getfsmnt(fs), (int)goal, cip->clean, npos);
		for (i = 0; i < lfs_sb_getnseg(fs) && ngood < goal; i++) {
			if (fs->clfs_segtabp[i]->priority == 0)
				break;
			sn = (fs->clfs_segtabp[i] - fs->clfs_segtab);
			dlog("%s: add seg %d prio %" PRIu64,
			     lfs_sb_getfsmnt(fs), sn, fs->clfs_segtabp[i]->priority);
			if ((r = load_segment(fs, sn, &bip, &bic)) > 0)
				++ngood;
			else if (r == 0)
				fd_release(fs->clfs_devvp);
			else
				break;
		}
		widebic = bic;
		toss_old_blocks(fs, &bip, &widebic, NULL);
		bic = widebic;
	}

	/* If there is nothing to do, try again later. */
	if (bic == 0) {
		dlog("%s: no blocks to clean in %d cleanable segments",
		       lfs_sb_getfsmnt(fs), (int)ngood);
		fd_release_all(fs->clfs_devvp);
		return 0;
	}

	/* Record statistics */
	for (i = nb = 0; i < bic; i++)
		nb += bip[i].bi_size;
	util = ((double)nb) / (fs->clfs_nactive * lfs_sb_getssize(fs));
	cleaner_stats.util_tot += util;
	cleaner_stats.util_sos += util * util;
	cleaner_stats.bytes_written += nb;

	/*
	 * Check out our blocks to see if there are hidden cleaning costs.
	 * If there are, we might be cleaning ourselves deeper into a hole
	 * rather than doing anything useful.
	 * XXX do something about this.
	 */
	if_extra = 0;
	extra = lfs_sb_getbsize(fs) * (off_t)check_hidden_cost(fs, bip, bic, &if_extra);
	if_extra *= lfs_sb_getbsize(fs);

	/*
	 * Use markv to move the blocks.
	 */
	if (do_small) 
		inc = MAXPHYS / lfs_sb_getbsize(fs) - 1;
	else
		inc = LFS_MARKV_MAXBLKCNT / 2;
	for (mc = 0, mbip = bip; mc < bic; mc += inc, mbip += inc) {
		lim.blkiov = mbip;
		lim.blkcnt = (bic - mc > inc ? inc : bic - mc);
#ifdef TEST_PATTERN
		dlog("checking blocks %d-%d", mc, mc + lim.blkcnt - 1);
		for (i = 0; i < lim.blkcnt; i++) {
			check_test_pattern(mbip + i);
		}
#endif /* TEST_PATTERN */
		dlog("sending blocks %d-%d", mc, mc + lim.blkcnt - 1);
		if ((r = kops.ko_fcntl(fs->clfs_ifilefd, LFCNMARKV, &lim))<0) {
			int oerrno = errno;
			syslog(LOG_WARNING, "%s: markv returned %d (errno %d, %m)",
			       lfs_sb_getfsmnt(fs), r, errno);
			if (oerrno != EAGAIN && oerrno != ESHUTDOWN) {
				syslog(LOG_DEBUG, "%s: errno %d, returning",
				       lfs_sb_getfsmnt(fs), oerrno);
				fd_release_all(fs->clfs_devvp);
				return r;
			}
			if (oerrno == ESHUTDOWN) {
				syslog(LOG_NOTICE, "%s: filesystem unmounted",
				       lfs_sb_getfsmnt(fs));
				fd_release_all(fs->clfs_devvp);
				return r;
			}
		}
	}

	/*
	 * Report progress (or lack thereof)
	 */
	syslog(LOG_INFO, "%s: wrote %" PRId64 " dirty + %"
	       PRId64 " supporting indirect + %"
	       PRId64 " supporting Ifile = %"
	       PRId64 " bytes to clean %d segs (%" PRId64 "%% recovery)",
	       lfs_sb_getfsmnt(fs), (int64_t)nb, (int64_t)(extra - if_extra),
	       (int64_t)if_extra, (int64_t)(nb + extra), ngood,
	       (ngood ? (int64_t)(100 - (100 * (nb + extra)) /
					 (ngood * lfs_sb_getssize(fs))) :
		(int64_t)0));
	if (nb + extra >= ngood * lfs_sb_getssize(fs))
		syslog(LOG_WARNING, "%s: cleaner not making forward progress",
		       lfs_sb_getfsmnt(fs));

	/*
	 * Finally call reclaim to prompt cleaning of the segments.
	 */
	kops.ko_fcntl(fs->clfs_ifilefd, LFCNRECLAIM, NULL);

	fd_release_all(fs->clfs_devvp);
	return 0;
}

/*
 * Read the cleanerinfo block and apply cleaning policy to determine whether
 * the given filesystem needs to be cleaned.  Returns 1 if it does, 0 if it
 * does not, or -1 on error.
 */
static int
needs_cleaning(struct clfs *fs, CLEANERINFO64 *cip)
{
	CLEANERINFO *cipu;
	struct ubuf *bp;
	struct stat st;
	daddr_t fsb_per_seg, max_free_segs;
	time_t now;
	double loadavg;

	/* If this fs is "on hold", don't clean it. */
	if (fs->clfs_onhold) {
#if defined(__GNUC__) && \
    (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)) && \
    defined(__OPTIMIZE_SIZE__)
	/*
	 * XXX: Work around apparent bug with GCC >= 4.8 and -Os: it
	 * claims that ci.clean is uninitialized in clean_fs (at one
	 * of the several uses of it, which is neither the first nor
	 * last use) -- this doesn't happen with plain -O2.
	 *
	 * Hopefully in the future further rearrangements will allow
	 * removing this hack.
	 */
		cip->clean = 0;
#endif
		return 0;
	}

	/*
	 * Read the cleanerinfo block from the Ifile.  We don't want
	 * the cached information, so invalidate the buffer before
	 * handing it back.
	 */
	if (bread(fs->lfs_ivnode, 0, lfs_sb_getbsize(fs), 0, &bp)) {
		syslog(LOG_ERR, "%s: can't read inode", lfs_sb_getfsmnt(fs));
		return -1;
	}
	cipu = (CLEANERINFO *)bp->b_data;
	if (fs->lfs_is64) {
		/* Structure copy */
		*cip = cipu->u_64;
	} else {
		/* Copy the fields and promote to 64 bit */
		cip->clean = cipu->u_32.clean;
		cip->dirty = cipu->u_32.dirty;
		cip->bfree = cipu->u_32.bfree;
		cip->avail = cipu->u_32.avail;
		cip->free_head = cipu->u_32.free_head;
		cip->free_tail = cipu->u_32.free_tail;
		cip->flags = cipu->u_32.flags;
	}
	brelse(bp, B_INVAL);
	cleaner_stats.bytes_read += lfs_sb_getbsize(fs);

	/*
	 * If the number of segments changed under us, reinit.
	 * We don't have to start over from scratch, however,
	 * since we don't hold any buffers.
	 */
	if (lfs_sb_getnseg(fs) != cip->clean + cip->dirty) {
		if (reinit_fs(fs) < 0) {
			/* The normal case for unmount */
			syslog(LOG_NOTICE, "%s: filesystem unmounted", lfs_sb_getfsmnt(fs));
			return -1;
		}
		syslog(LOG_NOTICE, "%s: nsegs changed", lfs_sb_getfsmnt(fs));
	}

	/* Compute theoretical "free segments" maximum based on usage */
	fsb_per_seg = lfs_segtod(fs, 1);
	max_free_segs = MAX(cip->bfree, 0) / fsb_per_seg + lfs_sb_getminfreeseg(fs);

	dlog("%s: bfree = %d, avail = %d, clean = %d/%d",
	     lfs_sb_getfsmnt(fs), cip->bfree, cip->avail, cip->clean,
	     lfs_sb_getnseg(fs));

	/* If the writer is waiting on us, clean it */
	if (cip->clean <= lfs_sb_getminfreeseg(fs) ||
	    (cip->flags & LFS_CLEANER_MUST_CLEAN))
		return 1;

	/* If there are enough segments, don't clean it */
	if (cip->bfree - cip->avail <= fsb_per_seg &&
	    cip->avail > fsb_per_seg)
		return 0;

	/* If we are in dire straits, clean it */
	if (cip->bfree - cip->avail > fsb_per_seg &&
	    cip->avail <= fsb_per_seg)
		return 1;

	/* If under busy threshold, clean regardless of load */
	if (cip->clean < max_free_segs * BUSY_LIM)
		return 1;

	/* Check busy status; clean if idle and under idle limit */
	if (use_fs_idle) {
		/* Filesystem idle */
		time(&now);
		if (fstat(fs->clfs_ifilefd, &st) < 0) {
			syslog(LOG_ERR, "%s: failed to stat ifile",
			       lfs_sb_getfsmnt(fs));
			return -1;
		}
		if (now - st.st_mtime > segwait_timeout &&
		    cip->clean < max_free_segs * IDLE_LIM)
			return 1;
	} else {
		/* CPU idle - use one-minute load avg */
		if (getloadavg(&loadavg, 1) == -1) {
			syslog(LOG_ERR, "%s: failed to get load avg",
			       lfs_sb_getfsmnt(fs));
			return -1;
		}
		if (loadavg < load_threshold &&
		    cip->clean < max_free_segs * IDLE_LIM)
			return 1;
	}

	return 0;
}

/*
 * Report statistics.  If the signal was SIGUSR2, clear the statistics too.
 * If the signal was SIGINT, exit.
 */
static void
sig_report(int sig)
{
	double avg = 0.0, stddev;

	avg = cleaner_stats.util_tot / MAX(cleaner_stats.segs_cleaned, 1.0);
	stddev = cleaner_stats.util_sos / MAX(cleaner_stats.segs_cleaned -
					      avg * avg, 1.0);
	syslog(LOG_INFO, "bytes read:	     %" PRId64, cleaner_stats.bytes_read);
	syslog(LOG_INFO, "bytes written:     %" PRId64, cleaner_stats.bytes_written);
	syslog(LOG_INFO, "segments cleaned:  %" PRId64, cleaner_stats.segs_cleaned);
#if 0
	/* "Empty segments" is meaningless, since the kernel handles those */
	syslog(LOG_INFO, "empty segments:    %" PRId64, cleaner_stats.segs_empty);
#endif
	syslog(LOG_INFO, "error segments:    %" PRId64, cleaner_stats.segs_error);
	syslog(LOG_INFO, "utilization total: %g", cleaner_stats.util_tot);
	syslog(LOG_INFO, "utilization sos:   %g", cleaner_stats.util_sos);
	syslog(LOG_INFO, "utilization avg:   %4.2f", avg);
	syslog(LOG_INFO, "utilization sdev:  %9.6f", stddev);

	if (debug)
		bufstats();

	if (sig == SIGUSR2)
		memset(&cleaner_stats, 0, sizeof(cleaner_stats));
	if (sig == SIGINT)
		exit(0);
}

static void
sig_exit(int sig)
{
	exit(0);
}

static void
usage(void)
{
	fprintf(stderr, "usage: lfs_cleanerd [-bcdfmqsJ] [-i segnum] [-l load] "
	     "[-n nsegs] [-r report_freq] [-t timeout] fs_name ...");
}

#ifndef LFS_CLEANER_AS_LIB
/*
 * Main.
 */
int
main(int argc, char **argv)
{

	return lfs_cleaner_main(argc, argv);
}
#endif

int
lfs_cleaner_main(int argc, char **argv)
{
	int i, opt, error, r, loopcount, nodetach;
	struct timeval tv;
#ifdef LFS_CLEANER_AS_LIB
	sem_t *semaddr = NULL;
#endif
	CLEANERINFO64 ci;
#ifndef USE_CLIENT_SERVER
	char *cp, *pidname;
#endif

	/*
	 * Set up defaults
	 */
	atatime	 = 1;
	segwait_timeout = 300; /* Five minutes */
	load_threshold	= 0.2;
	stat_report	= 0;
	inval_segment	= -1;
	copylog_filename = NULL;
	nodetach        = 0;
	do_asdevice     = NULL;

	/*
	 * Parse command-line arguments
	 */
	while ((opt = getopt(argc, argv, "bC:cdDfi:J:l:mn:qr:sS:t:")) != -1) {
		switch (opt) {
		    case 'b':	/* Use bytes written, not segments read */
			    use_bytes = 1;
			    break;
		    case 'C':	/* copy log */
			    copylog_filename = optarg;
			    break;
		    case 'c':	/* Coalesce files */
			    do_coalesce++;
			    break;
		    case 'd':	/* Debug mode. */
			    nodetach++;
			    debug++;
			    break;
		    case 'D':	/* stay-on-foreground */
			    nodetach++;
			    break;
		    case 'f':	/* Use fs idle time rather than cpu idle */
			    use_fs_idle = 1;
			    break;
		    case 'i':	/* Invalidate this segment */
			    inval_segment = atoi(optarg);
			    break;
		    case 'l':	/* Load below which to clean */
			    load_threshold = atof(optarg);
			    break;
		    case 'm':	/* [compat only] */
			    break;
		    case 'n':	/* How many segs to clean at once */
			    atatime = atoi(optarg);
			    break;
		    case 'q':	/* Quit after one run */
			    do_quit = 1;
			    break;
		    case 'r':	/* Report every stat_report segments */
			    stat_report = atoi(optarg);
			    break;
		    case 's':	/* Small writes */
			    do_small = 1;
			    break;
#ifdef LFS_CLEANER_AS_LIB
		    case 'S':	/* semaphore */
			    semaddr = (void*)(uintptr_t)strtoull(optarg,NULL,0);
			    break;
#endif
		    case 't':	/* timeout */
			    segwait_timeout = atoi(optarg);
			    break;
		    case 'J': /* do as a device */
			    do_asdevice = optarg;
			    break;
		    default:
			    usage();
			    /* NOTREACHED */
		}
	}
	argc -= optind;
	argv += optind;

	if (argc < 1)
		usage();
	if (inval_segment >= 0 && argc != 1) {
		errx(1, "lfs_cleanerd: may only specify one filesystem when "
		     "using -i flag");
	}

	if (do_coalesce) {
		errx(1, "lfs_cleanerd: -c disabled due to reports of file "
		     "corruption; you may re-enable it by rebuilding the "
		     "cleaner");
	}

	/*
	 * Set up daemon mode or foreground mode
	 */
	if (nodetach) {
		openlog("lfs_cleanerd", LOG_NDELAY | LOG_PID | LOG_PERROR,
			LOG_DAEMON);
		signal(SIGINT, sig_report);
	} else {
		if (daemon(0, 0) == -1)
			err(1, "lfs_cleanerd: couldn't become a daemon!");
		openlog("lfs_cleanerd", LOG_NDELAY | LOG_PID, LOG_DAEMON);
		signal(SIGINT, sig_exit);
	}

	/*
	 * Look for an already-running master daemon.  If there is one,
	 * send it our filesystems to add to its list and exit.
	 * If there is none, become the master.
	 */
#ifdef USE_CLIENT_SERVER
	try_to_become_master(argc, argv);
#else
	/* XXX think about this */
	asprintf(&pidname, "lfs_cleanerd:m:%s", argv[0]);
	if (pidname == NULL) {
		syslog(LOG_ERR, "malloc failed: %m");
		exit(1);
	}
	for (cp = pidname; cp != NULL; cp = strchr(cp, '/'))
		*cp = '|';
	pidfile(pidname);
#endif

	/*
	 * Signals mean daemon should report its statistics
	 */
	memset(&cleaner_stats, 0, sizeof(cleaner_stats));
	signal(SIGUSR1, sig_report);
	signal(SIGUSR2, sig_report);

	/*
	 * Start up buffer cache.  We only use this for the Ifile,
	 * and we will resize it if necessary, so it can start small.
	 */
	bufinit(4);

#ifdef REPAIR_ZERO_FINFO
	{
		BLOCK_INFO *bip = NULL;
		int bic = 0;

		nfss = 1;
		fsp = (struct clfs **)malloc(sizeof(*fsp));
		fsp[0] = (struct clfs *)calloc(1, sizeof(**fsp));

		if (init_unmounted_fs(fsp[0], argv[0]) < 0) {
			err(1, "init_unmounted_fs");
		}
		dlog("Filesystem has %d segments", fsp[0]->lfs_nseg);
		for (i = 0; i < fsp[0]->lfs_nseg; i++) {
			load_segment(fsp[0], i, &bip, &bic);
			bic = 0;
		}
		exit(0);
	}
#endif

	/*
	 * Initialize cleaning structures, open devices, etc.
	 */
	nfss = argc;
	fsp = (struct clfs **)malloc(nfss * sizeof(*fsp));
	if (fsp == NULL) {
		syslog(LOG_ERR, "couldn't allocate fs table: %m");
		exit(1);
	}
	for (i = 0; i < nfss; i++) {
		fsp[i] = (struct clfs *)calloc(1, sizeof(**fsp));
		if ((r = init_fs(fsp[i], argv[i])) < 0) {
			syslog(LOG_ERR, "%s: couldn't init: error code %d",
			       argv[i], r);
			handle_error(fsp, i);
			--i; /* Do the new #i over again */
		}
	}

	/*
	 * If asked to coalesce, do so and exit.
	 */
	if (do_coalesce) {
		for (i = 0; i < nfss; i++)
			clean_all_inodes(fsp[i]);
		exit(0);
	}

	/*
	 * If asked to invalidate a segment, do that and exit.
	 */
	if (inval_segment >= 0) {
		invalidate_segment(fsp[0], inval_segment);
		exit(0);
	}

	/*
	 * Main cleaning loop.
	 */
	loopcount = 0;
#ifdef LFS_CLEANER_AS_LIB
	if (semaddr)
		sem_post(semaddr);
#endif
	error = 0;
	while (nfss > 0) {
		int cleaned_one;
		do {
#ifdef USE_CLIENT_SERVER
			check_control_socket();
#endif
			cleaned_one = 0;
			for (i = 0; i < nfss; i++) {
				if ((error = needs_cleaning(fsp[i], &ci)) < 0) {
					syslog(LOG_DEBUG, "%s: needs_cleaning returned %d",
					       getprogname(), error);
					handle_error(fsp, i);
					continue;
				}
				if (error == 0) /* No need to clean */
					continue;
				
				reload_ifile(fsp[i]);
				if ((error = clean_fs(fsp[i], &ci)) < 0) {
					syslog(LOG_DEBUG, "%s: clean_fs returned %d",
					       getprogname(), error);
					handle_error(fsp, i);
					continue;
				}
				++cleaned_one;
			}
			++loopcount;
			if (stat_report && loopcount % stat_report == 0)
				sig_report(0);
			if (do_quit)
				exit(0);
		} while(cleaned_one);
		tv.tv_sec = segwait_timeout;
		tv.tv_usec = 0;
		/* XXX: why couldn't others work if fsp socket is shutdown? */
		error = kops.ko_fcntl(fsp[0]->clfs_ifilefd,LFCNSEGWAITALL,&tv);
		if (error) {
			if (errno == ESHUTDOWN) {
				for (i = 0; i < nfss; i++) {
					syslog(LOG_INFO, "%s: shutdown",
					       getprogname());
					handle_error(fsp, i);
					assert(nfss == 0);
				}
			} else {
#ifdef LFS_CLEANER_AS_LIB
				error = ESHUTDOWN;
				break;
#else
				err(1, "LFCNSEGWAITALL");
#endif
			}
		}
	}

	/* NOTREACHED */
	return error;
}