1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause AND BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2006 Pawel Jakub Dawidek <pjd@FreeBSD.org> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * 28 * Copyright (c) 1982, 1986, 1989, 1993 29 * The Regents of the University of California. All rights reserved. 30 * 31 * Redistribution and use in source and binary forms, with or without 32 * modification, are permitted provided that the following conditions 33 * are met: 34 * 1. Redistributions of source code must retain the above copyright 35 * notice, this list of conditions and the following disclaimer. 36 * 2. Redistributions in binary form must reproduce the above copyright 37 * notice, this list of conditions and the following disclaimer in the 38 * documentation and/or other materials provided with the distribution. 39 * 3. Neither the name of the University nor the names of its contributors 40 * may be used to endorse or promote products derived from this software 41 * without specific prior written permission. 42 * 43 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 46 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 53 * SUCH DAMAGE. 54 */ 55 56 #include <sys/cdefs.h> 57 __FBSDID("$FreeBSD$"); 58 59 #include <string.h> 60 #include <sys/stat.h> 61 #include <ufs/ffs/fs.h> 62 #include "fsck.h" 63 64 void 65 gjournal_check(const char *filesys) 66 { 67 struct fs *fs; 68 struct inode ip; 69 union dinode *dp; 70 struct bufarea *cgbp; 71 struct cg *cgp; 72 struct inodesc idesc; 73 uint8_t *inosused; 74 ino_t cino, ino; 75 int cg; 76 77 fs = &sblock; 78 /* Are there any unreferenced inodes in this file system? */ 79 if (fs->fs_unrefs == 0) { 80 //printf("No unreferenced inodes.\n"); 81 sbdirty(); 82 ckfini(1); 83 return; 84 } 85 86 for (cg = 0; cg < fs->fs_ncg; cg++) { 87 /* Show progress if requested. */ 88 if (got_siginfo) { 89 printf("%s: phase j: cyl group %d of %d (%d%%)\n", 90 cdevname, cg, fs->fs_ncg, cg * 100 / fs->fs_ncg); 91 got_siginfo = 0; 92 } 93 if (got_sigalarm) { 94 setproctitle("%s pj %d%%", cdevname, 95 cg * 100 / fs->fs_ncg); 96 got_sigalarm = 0; 97 } 98 cgbp = cglookup(cg); 99 cgp = cgbp->b_un.b_cg; 100 if (!check_cgmagic(cg, cgbp, 0)) { 101 rerun = 1; 102 ckfini(0); 103 return; 104 } 105 /* Are there any unreferenced inodes in this cylinder group? */ 106 if (cgp->cg_unrefs == 0) 107 continue; 108 //printf("Analizing cylinder group %d (count=%d)\n", cg, cgp->cg_unrefs); 109 /* 110 * Now go through the list of all inodes in this cylinder group 111 * to find unreferenced ones. 112 */ 113 inosused = cg_inosused(cgp); 114 for (cino = 0; cino < fs->fs_ipg; cino++) { 115 ino = fs->fs_ipg * cg + cino; 116 /* Unallocated? Skip it. */ 117 if (isclr(inosused, cino)) 118 continue; 119 ginode(ino, &ip); 120 dp = ip.i_dp; 121 /* Not a regular file nor directory? Skip it. */ 122 if (!S_ISREG(dp->dp2.di_mode) && 123 !S_ISDIR(dp->dp2.di_mode)) { 124 irelse(&ip); 125 continue; 126 } 127 /* Has reference(s)? Skip it. */ 128 if (dp->dp2.di_nlink > 0) { 129 irelse(&ip); 130 continue; 131 } 132 /* printf("Clearing inode=%d (size=%jd)\n", ino, 133 (intmax_t)dp->dp2->di_size); */ 134 /* Deallocate it. */ 135 memset(&idesc, 0, sizeof(struct inodesc)); 136 idesc.id_type = ADDR; 137 idesc.id_func = freeblock; 138 idesc.id_number = ino; 139 clri(&idesc, "UNREF", 1); 140 clrbit(inosused, cino); 141 /* Update position of last used inode. */ 142 if (ino < cgp->cg_irotor) 143 cgp->cg_irotor = ino; 144 /* Update statistics. */ 145 cgp->cg_unrefs--; 146 fs->fs_unrefs--; 147 /* Zero-fill the inode. */ 148 dp->dp2 = zino.dp2; 149 /* Write the inode back. */ 150 inodirty(&ip); 151 irelse(&ip); 152 cgdirty(cgbp); 153 if (cgp->cg_unrefs == 0) { 154 //printf("No more unreferenced inodes in cg=%d.\n", cg); 155 break; 156 } 157 } 158 /* 159 * If there are no more unreferenced inodes, there is no need to 160 * check other cylinder groups. 161 */ 162 if (fs->fs_unrefs == 0) { 163 //printf("No more unreferenced inodes (cg=%d/%d).\n", cg, 164 // fs->fs_ncg); 165 break; 166 } 167 } 168 /* Write back updated statistics and super-block. */ 169 sbdirty(); 170 ckfini(1); 171 } 172