2 * Copyright (c) 2006 Pawel Jakub Dawidek <pjd@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * Copyright (c) 1982, 1986, 1989, 1993
27 * The Regents of the University of California. All rights reserved.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 4. Neither the name of the University nor the names of its contributors
38 * may be used to endorse or promote products derived from this software
39 * without specific prior written permission.
41 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 #include <sys/cdefs.h>
55 __FBSDID("$FreeBSD$");
57 #include <sys/param.h>
58 #include <sys/disklabel.h>
59 #include <sys/mount.h>
62 #include <ufs/ufs/ufsmount.h>
63 #include <ufs/ufs/dinode.h>
64 #include <ufs/ffs/fs.h>
79 char cgcu_buf[MAXBSIZE];
83 LIST_ENTRY(cgchain) cgc_next;
85 #define cgc_cg cgc_union.cgcu_cg
87 #define MAX_CACHED_CGS 1024
88 static unsigned ncgs = 0;
89 static LIST_HEAD(, cgchain) cglist = LIST_HEAD_INITIALIZER(cglist);
91 static const char *devnam;
92 static struct uufsd *disk = NULL;
93 static struct fs *fs = NULL;
94 struct ufs2_dinode ufs2_zino;
96 static void putcgs(void);
99 * Return cylinder group from the cache or load it if it is not in the
101 * Don't cache more than MAX_CACHED_CGS cylinder groups.
103 static struct cgchain *
108 assert(disk != NULL && fs != NULL);
109 LIST_FOREACH(cgc, &cglist, cgc_next) {
110 if (cgc->cgc_cg.cg_cgx == cg) {
111 //printf("%s: Found cg=%d\n", __func__, cg);
116 * Our cache is full? Let's clean it up.
118 if (ncgs >= MAX_CACHED_CGS) {
119 //printf("%s: Flushing CGs.\n", __func__);
122 cgc = malloc(sizeof(*cgc));
125 * Cannot allocate memory?
126 * Let's put all currently loaded and not busy cylinder groups
127 * on disk and try again.
129 //printf("%s: No memory, flushing CGs.\n", __func__);
131 cgc = malloc(sizeof(*cgc));
133 err(1, "malloc(%zu)", sizeof(*cgc));
135 if (cgread1(disk, cg) == -1)
136 err(1, "cgread1(%d)", cg);
137 bcopy(&disk->d_cg, &cgc->cgc_cg, sizeof(cgc->cgc_union));
140 LIST_INSERT_HEAD(&cglist, cgc, cgc_next);
142 //printf("%s: Read cg=%d\n", __func__, cg);
147 * Mark cylinder group as dirty - it will be written back on putcgs().
150 dirtycg(struct cgchain *cgc)
157 * Mark cylinder group as busy - it will not be freed on putcgs().
160 busycg(struct cgchain *cgc)
167 * Unmark the given cylinder group as busy.
170 unbusycg(struct cgchain *cgc)
177 * Write back all dirty cylinder groups.
178 * Free all non-busy cylinder groups.
183 struct cgchain *cgc, *cgc2;
185 assert(disk != NULL && fs != NULL);
186 LIST_FOREACH_SAFE(cgc, &cglist, cgc_next, cgc2) {
189 LIST_REMOVE(cgc, cgc_next);
191 if (cgc->cgc_dirty) {
192 bcopy(&cgc->cgc_cg, &disk->d_cg,
193 sizeof(cgc->cgc_union));
194 if (cgwrite1(disk, cgc->cgc_cg.cg_cgx) == -1)
195 err(1, "cgwrite1(%d)", cgc->cgc_cg.cg_cgx);
196 //printf("%s: Wrote cg=%d\n", __func__,
197 // cgc->cgc_cg.cg_cgx);
205 * Free all non-busy cylinder groups without storing the dirty ones.
212 assert(disk != NULL && fs != NULL);
213 while ((cgc = LIST_FIRST(&cglist)) != NULL) {
216 LIST_REMOVE(cgc, cgc_next);
217 //printf("%s: Canceled cg=%d\n", __func__, cgc->cgc_cg.cg_cgx);
224 * Open the given provider, load superblock.
231 disk = malloc(sizeof(*disk));
233 err(1, "malloc(%zu)", sizeof(*disk));
234 if (ufs_disk_fillout(disk, devnam) == -1) {
235 err(1, "ufs_disk_fillout(%s) failed: %s", devnam,
242 * Mark file system as clean, write the super-block back, close the disk.
249 if (sbwrite(disk, 0) == -1)
250 err(1, "sbwrite(%s)", devnam);
251 if (ufs_disk_close(disk) == -1)
252 err(1, "ufs_disk_close(%s)", devnam);
259 blkfree(ufs2_daddr_t bno, long size)
263 ufs1_daddr_t fragno, cgbno;
264 int i, cg, blk, frags, bbase;
271 cgbno = dtogd(fs, bno);
272 blksfree = cg_blksfree(cgp);
273 if (size == fs->fs_bsize) {
274 fragno = fragstoblks(fs, cgbno);
275 if (!ffs_isfreeblock(fs, blksfree, fragno))
276 assert(!"blkfree: freeing free block");
277 ffs_setblock(fs, blksfree, fragno);
278 ffs_clusteracct(fs, cgp, fragno, 1);
279 cgp->cg_cs.cs_nbfree++;
280 fs->fs_cstotal.cs_nbfree++;
281 fs->fs_cs(fs, cg).cs_nbfree++;
283 bbase = cgbno - fragnum(fs, cgbno);
285 * decrement the counts associated with the old frags
287 blk = blkmap(fs, blksfree, bbase);
288 ffs_fragacct(fs, blk, cgp->cg_frsum, -1);
290 * deallocate the fragment
292 frags = numfrags(fs, size);
293 for (i = 0; i < frags; i++) {
294 if (isset(blksfree, cgbno + i))
295 assert(!"blkfree: freeing free frag");
296 setbit(blksfree, cgbno + i);
298 cgp->cg_cs.cs_nffree += i;
299 fs->fs_cstotal.cs_nffree += i;
300 fs->fs_cs(fs, cg).cs_nffree += i;
302 * add back in counts associated with the new frags
304 blk = blkmap(fs, blksfree, bbase);
305 ffs_fragacct(fs, blk, cgp->cg_frsum, 1);
307 * if a complete block has been reassembled, account for it
309 fragno = fragstoblks(fs, bbase);
310 if (ffs_isblock(fs, blksfree, fragno)) {
311 cgp->cg_cs.cs_nffree -= fs->fs_frag;
312 fs->fs_cstotal.cs_nffree -= fs->fs_frag;
313 fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
314 ffs_clusteracct(fs, cgp, fragno, 1);
315 cgp->cg_cs.cs_nbfree++;
316 fs->fs_cstotal.cs_nbfree++;
317 fs->fs_cs(fs, cg).cs_nbfree++;
323 * Recursively free all indirect blocks.
326 freeindir(ufs2_daddr_t blk, int level)
328 char sblks[MAXBSIZE];
332 if (bread(disk, fsbtodb(fs, blk), (void *)&sblks, (size_t)fs->fs_bsize) == -1)
333 err(1, "bread: %s", disk->d_error);
334 blks = (ufs2_daddr_t *)&sblks;
335 for (i = 0; i < NINDIR(fs); i++) {
339 blkfree(blks[i], fs->fs_bsize);
341 freeindir(blks[i], level - 1);
343 blkfree(blk, fs->fs_bsize);
346 #define dblksize(fs, dino, lbn) \
347 ((dino)->di_size >= smalllblktosize(fs, (lbn) + 1) \
349 : fragroundup(fs, blkoff(fs, (dino)->di_size)))
352 * Free all blocks associated with the given inode.
355 clear_inode(struct ufs2_dinode *dino)
358 int extblocks, i, level;
363 if (fs->fs_magic == FS_UFS2_MAGIC && dino->di_extsize > 0)
364 extblocks = btodb(fragroundup(fs, dino->di_extsize));
365 /* deallocate external attributes blocks */
367 osize = dino->di_extsize;
368 dino->di_blocks -= extblocks;
369 dino->di_extsize = 0;
370 for (i = 0; i < NXADDR; i++) {
371 if (dino->di_extb[i] == 0)
373 blkfree(dino->di_extb[i], sblksize(fs, osize, i));
376 #define SINGLE 0 /* index of single indirect block */
377 #define DOUBLE 1 /* index of double indirect block */
378 #define TRIPLE 2 /* index of triple indirect block */
379 /* deallocate indirect blocks */
380 for (level = SINGLE; level <= TRIPLE; level++) {
381 if (dino->di_ib[level] == 0)
383 freeindir(dino->di_ib[level], level);
385 /* deallocate direct blocks and fragments */
386 for (i = 0; i < NDADDR; i++) {
390 bsize = dblksize(fs, dino, i);
396 gjournal_check(const char *filesys)
398 struct ufs2_dinode *dino;
402 uint8_t *inosused, *blksfree;
408 /* Are there any unreferenced inodes in this file system? */
409 if (fs->fs_unrefs == 0) {
410 //printf("No unreferenced inodes.\n");
415 for (cg = 0; cg < fs->fs_ncg; cg++) {
416 /* Show progress if requested. */
418 printf("%s: phase j: cyl group %d of %d (%d%%)\n",
419 cdevname, cg, fs->fs_ncg, cg * 100 / fs->fs_ncg);
423 setproctitle("%s pj %d%%", cdevname,
424 cg * 100 / fs->fs_ncg);
429 /* Are there any unreferenced inodes in this cylinder group? */
430 if (cgp->cg_unrefs == 0)
432 //printf("Analizing cylinder group %d (count=%d)\n", cg, cgp->cg_unrefs);
434 * We are going to modify this cylinder group, so we want it to
438 /* We don't want it to be freed in the meantime. */
440 inosused = cg_inosused(cgp);
441 blksfree = cg_blksfree(cgp);
443 * Now go through the list of all inodes in this cylinder group
444 * to find unreferenced ones.
446 for (cino = 0; cino < fs->fs_ipg; cino++) {
447 ino = fs->fs_ipg * cg + cino;
448 /* Unallocated? Skip it. */
449 if (isclr(inosused, cino))
451 if (getino(disk, &p, ino, &mode) == -1)
452 err(1, "getino(cg=%d ino=%d)", cg, ino);
454 /* Not a regular file nor directory? Skip it. */
455 if (!S_ISREG(dino->di_mode) && !S_ISDIR(dino->di_mode))
457 /* Has reference(s)? Skip it. */
458 if (dino->di_nlink > 0)
460 //printf("Clearing inode=%d (size=%jd)\n", ino, (intmax_t)dino->di_size);
461 /* Free inode's blocks. */
464 clrbit(inosused, cino);
465 /* Update position of last used inode. */
466 if (ino < cgp->cg_irotor)
467 cgp->cg_irotor = ino;
468 /* Update statistics. */
469 cgp->cg_cs.cs_nifree++;
470 fs->fs_cs(fs, cg).cs_nifree++;
471 fs->fs_cstotal.cs_nifree++;
474 /* If this is directory, update related statistics. */
475 if (S_ISDIR(dino->di_mode)) {
476 cgp->cg_cs.cs_ndir--;
477 fs->fs_cs(fs, cg).cs_ndir--;
478 fs->fs_cstotal.cs_ndir--;
480 /* Zero-fill the inode. */
482 /* Write the inode back. */
483 if (putino(disk) == -1)
484 err(1, "putino(cg=%d ino=%d)", cg, ino);
485 if (cgp->cg_unrefs == 0) {
486 //printf("No more unreferenced inodes in cg=%d.\n", cg);
491 * We don't need this cylinder group anymore, so feel free to
496 * If there are no more unreferenced inodes, there is no need to
497 * check other cylinder groups.
499 if (fs->fs_unrefs == 0) {
500 //printf("No more unreferenced inodes (cg=%d/%d).\n", cg,
505 /* Write back modified cylinder groups. */
507 /* Write back updated statistics and super-block. */