/*-1* SPDX-License-Identifier: BSD-3-Clause AND BSD-2-Clause2*3* Copyright (c) 2006 Pawel Jakub Dawidek <[email protected]>4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND16* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE19* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25* SUCH DAMAGE.26*27* Copyright (c) 1982, 1986, 1989, 199328* The Regents of the University of California. All rights reserved.29*30* Redistribution and use in source and binary forms, with or without31* modification, are permitted provided that the following conditions32* are met:33* 1. Redistributions of source code must retain the above copyright34* notice, this list of conditions and the following disclaimer.35* 2. Redistributions in binary form must reproduce the above copyright36* notice, this list of conditions and the following disclaimer in the37* documentation and/or other materials provided with the distribution.38* 3. Neither the name of the University nor the names of its contributors39* may be used to endorse or promote products derived from this software40* without specific prior written permission.41*42* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND43* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE44* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE45* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE46* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL47* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS48* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)49* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT50* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY51* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF52* SUCH DAMAGE.53*/5455#include <sys/cdefs.h>56#include <string.h>57#include <sys/stat.h>58#include <ufs/ffs/fs.h>59#include "fsck.h"6061void62gjournal_check(const char *filesys)63{64struct fs *fs;65struct inode ip;66union dinode *dp;67struct bufarea *cgbp;68struct cg *cgp;69struct inodesc idesc;70uint8_t *inosused;71ino_t cino, ino;72int cg;7374fs = &sblock;75/* Are there any unreferenced inodes in this file system? */76if (fs->fs_unrefs == 0) {77sbdirty();78ckfini(1);79return;80}8182for (cg = 0; cg < fs->fs_ncg; cg++) {83/* Show progress if requested. */84if (got_siginfo) {85printf("%s: phase j: cyl group %d of %d (%d%%)\n",86cdevname, cg, fs->fs_ncg, cg * 100 / fs->fs_ncg);87got_siginfo = 0;88}89if (got_sigalarm) {90setproctitle("%s pj %d%%", cdevname,91cg * 100 / fs->fs_ncg);92got_sigalarm = 0;93}94cgbp = cglookup(cg);95cgp = cgbp->b_un.b_cg;96if (!check_cgmagic(cg, cgbp)) {97rerun = 1;98ckfini(0);99return;100}101/* Are there any unreferenced inodes in this cylinder group? */102if (cgp->cg_unrefs == 0)103continue;104/*105* Now go through the list of all inodes in this cylinder group106* to find unreferenced ones.107*/108inosused = cg_inosused(cgp);109for (cino = 0; cino < fs->fs_ipg; cino++) {110ino = fs->fs_ipg * cg + cino;111/* Unallocated? Skip it. */112if (isclr(inosused, cino))113continue;114ginode(ino, &ip);115dp = ip.i_dp;116/* Not a regular file nor directory? Skip it. */117if (!S_ISREG(dp->dp2.di_mode) &&118!S_ISDIR(dp->dp2.di_mode)) {119irelse(&ip);120continue;121}122/* Has reference(s)? Skip it. */123if (dp->dp2.di_nlink > 0) {124irelse(&ip);125continue;126}127/* printf("Clearing inode=%d (size=%jd)\n", ino,128(intmax_t)dp->dp2->di_size); */129/* Deallocate it. */130memset(&idesc, 0, sizeof(struct inodesc));131idesc.id_type = ADDR;132idesc.id_func = freeblock;133idesc.id_number = ino;134clri(&idesc, "UNREF", 1);135clrbit(inosused, cino);136/* Update position of last used inode. */137if (ino < cgp->cg_irotor)138cgp->cg_irotor = ino;139/* Update statistics. */140cgp->cg_unrefs--;141fs->fs_unrefs--;142/* Zero-fill the inode. */143dp->dp2 = zino.dp2;144/* Write the inode back. */145inodirty(&ip);146irelse(&ip);147cgdirty(cgbp);148if (cgp->cg_unrefs == 0)149break;150}151/*152* If there are no more unreferenced inodes, there is no153* need to check other cylinder groups.154*/155if (fs->fs_unrefs == 0)156break;157}158/* Write back updated statistics and super-block. */159sbdirty();160ckfini(1);161}162163164