1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause AND BSD-2-Clause 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 sbdirty(); 81 ckfini(1); 82 return; 83 } 84 85 for (cg = 0; cg < fs->fs_ncg; cg++) { 86 /* Show progress if requested. */ 87 if (got_siginfo) { 88 printf("%s: phase j: cyl group %d of %d (%d%%)\n", 89 cdevname, cg, fs->fs_ncg, cg * 100 / fs->fs_ncg); 90 got_siginfo = 0; 91 } 92 if (got_sigalarm) { 93 setproctitle("%s pj %d%%", cdevname, 94 cg * 100 / fs->fs_ncg); 95 got_sigalarm = 0; 96 } 97 cgbp = cglookup(cg); 98 cgp = cgbp->b_un.b_cg; 99 if (!check_cgmagic(cg, cgbp)) { 100 rerun = 1; 101 ckfini(0); 102 return; 103 } 104 /* Are there any unreferenced inodes in this cylinder group? */ 105 if (cgp->cg_unrefs == 0) 106 continue; 107 /* 108 * Now go through the list of all inodes in this cylinder group 109 * to find unreferenced ones. 110 */ 111 inosused = cg_inosused(cgp); 112 for (cino = 0; cino < fs->fs_ipg; cino++) { 113 ino = fs->fs_ipg * cg + cino; 114 /* Unallocated? Skip it. */ 115 if (isclr(inosused, cino)) 116 continue; 117 ginode(ino, &ip); 118 dp = ip.i_dp; 119 /* Not a regular file nor directory? Skip it. */ 120 if (!S_ISREG(dp->dp2.di_mode) && 121 !S_ISDIR(dp->dp2.di_mode)) { 122 irelse(&ip); 123 continue; 124 } 125 /* Has reference(s)? Skip it. */ 126 if (dp->dp2.di_nlink > 0) { 127 irelse(&ip); 128 continue; 129 } 130 /* printf("Clearing inode=%d (size=%jd)\n", ino, 131 (intmax_t)dp->dp2->di_size); */ 132 /* Deallocate it. */ 133 memset(&idesc, 0, sizeof(struct inodesc)); 134 idesc.id_type = ADDR; 135 idesc.id_func = freeblock; 136 idesc.id_number = ino; 137 clri(&idesc, "UNREF", 1); 138 clrbit(inosused, cino); 139 /* Update position of last used inode. */ 140 if (ino < cgp->cg_irotor) 141 cgp->cg_irotor = ino; 142 /* Update statistics. */ 143 cgp->cg_unrefs--; 144 fs->fs_unrefs--; 145 /* Zero-fill the inode. */ 146 dp->dp2 = zino.dp2; 147 /* Write the inode back. */ 148 inodirty(&ip); 149 irelse(&ip); 150 cgdirty(cgbp); 151 if (cgp->cg_unrefs == 0) 152 break; 153 } 154 /* 155 * If there are no more unreferenced inodes, there is no 156 * need to check other cylinder groups. 157 */ 158 if (fs->fs_unrefs == 0) 159 break; 160 } 161 /* Write back updated statistics and super-block. */ 162 sbdirty(); 163 ckfini(1); 164 } 165