xref: /freebsd/sbin/fsck_ffs/gjournal.c (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
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 #include <string.h>
58 #include <sys/stat.h>
59 #include <ufs/ffs/fs.h>
60 #include "fsck.h"
61 
62 void
63 gjournal_check(const char *filesys)
64 {
65 	struct fs *fs;
66 	struct inode ip;
67 	union dinode *dp;
68 	struct bufarea *cgbp;
69 	struct cg *cgp;
70 	struct inodesc idesc;
71 	uint8_t *inosused;
72 	ino_t cino, ino;
73 	int cg;
74 
75 	fs = &sblock;
76 	/* Are there any unreferenced inodes in this file system? */
77 	if (fs->fs_unrefs == 0) {
78 		sbdirty();
79 		ckfini(1);
80 		return;
81 	}
82 
83 	for (cg = 0; cg < fs->fs_ncg; cg++) {
84 		/* Show progress if requested. */
85 		if (got_siginfo) {
86 			printf("%s: phase j: cyl group %d of %d (%d%%)\n",
87 			    cdevname, cg, fs->fs_ncg, cg * 100 / fs->fs_ncg);
88 			got_siginfo = 0;
89 		}
90 		if (got_sigalarm) {
91 			setproctitle("%s pj %d%%", cdevname,
92 			     cg * 100 / fs->fs_ncg);
93 			got_sigalarm = 0;
94 		}
95 		cgbp = cglookup(cg);
96 		cgp = cgbp->b_un.b_cg;
97 		if (!check_cgmagic(cg, cgbp)) {
98 			rerun = 1;
99 			ckfini(0);
100 			return;
101 		}
102 		/* Are there any unreferenced inodes in this cylinder group? */
103 		if (cgp->cg_unrefs == 0)
104 			continue;
105 		/*
106 		 * Now go through the list of all inodes in this cylinder group
107 		 * to find unreferenced ones.
108 		 */
109 		inosused = cg_inosused(cgp);
110 		for (cino = 0; cino < fs->fs_ipg; cino++) {
111 			ino = fs->fs_ipg * cg + cino;
112 			/* Unallocated? Skip it. */
113 			if (isclr(inosused, cino))
114 				continue;
115 			ginode(ino, &ip);
116 			dp = ip.i_dp;
117 			/* Not a regular file nor directory? Skip it. */
118 			if (!S_ISREG(dp->dp2.di_mode) &&
119 			    !S_ISDIR(dp->dp2.di_mode)) {
120 				irelse(&ip);
121 				continue;
122 			}
123 			/* Has reference(s)? Skip it. */
124 			if (dp->dp2.di_nlink > 0) {
125 				irelse(&ip);
126 				continue;
127 			}
128 			/* printf("Clearing inode=%d (size=%jd)\n", ino,
129 			    (intmax_t)dp->dp2->di_size); */
130 			/* Deallocate it. */
131 			memset(&idesc, 0, sizeof(struct inodesc));
132 			idesc.id_type = ADDR;
133 			idesc.id_func = freeblock;
134 			idesc.id_number = ino;
135 			clri(&idesc, "UNREF", 1);
136 			clrbit(inosused, cino);
137 			/* Update position of last used inode. */
138 			if (ino < cgp->cg_irotor)
139 				cgp->cg_irotor = ino;
140 			/* Update statistics. */
141 			cgp->cg_unrefs--;
142 			fs->fs_unrefs--;
143 			/* Zero-fill the inode. */
144 			dp->dp2 = zino.dp2;
145 			/* Write the inode back. */
146 			inodirty(&ip);
147 			irelse(&ip);
148 			cgdirty(cgbp);
149 			if (cgp->cg_unrefs == 0)
150 				break;
151 		}
152 		/*
153 		 * If there are no more unreferenced inodes, there is no
154 		 * need to check other cylinder groups.
155 		 */
156 		if (fs->fs_unrefs == 0)
157 			break;
158 	}
159 	/* Write back updated statistics and super-block. */
160 	sbdirty();
161 	ckfini(1);
162 }
163