inode.c revision 1.36 1 1.36 dholland /* $NetBSD: inode.c,v 1.36 2013/06/23 07:28:36 dholland Exp $ */
2 1.1 bouyer
3 1.1 bouyer /*
4 1.1 bouyer * Copyright (c) 1980, 1986, 1993
5 1.1 bouyer * The Regents of the University of California. All rights reserved.
6 1.1 bouyer *
7 1.1 bouyer * Redistribution and use in source and binary forms, with or without
8 1.1 bouyer * modification, are permitted provided that the following conditions
9 1.1 bouyer * are met:
10 1.1 bouyer * 1. Redistributions of source code must retain the above copyright
11 1.1 bouyer * notice, this list of conditions and the following disclaimer.
12 1.1 bouyer * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 bouyer * notice, this list of conditions and the following disclaimer in the
14 1.1 bouyer * documentation and/or other materials provided with the distribution.
15 1.11 agc * 3. Neither the name of the University nor the names of its contributors
16 1.11 agc * may be used to endorse or promote products derived from this software
17 1.11 agc * without specific prior written permission.
18 1.11 agc *
19 1.11 agc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.11 agc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.11 agc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.11 agc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.11 agc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.11 agc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.11 agc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.11 agc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.11 agc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.11 agc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.11 agc * SUCH DAMAGE.
30 1.11 agc */
31 1.11 agc
32 1.11 agc /*
33 1.11 agc * Copyright (c) 1997 Manuel Bouyer.
34 1.11 agc *
35 1.11 agc * Redistribution and use in source and binary forms, with or without
36 1.11 agc * modification, are permitted provided that the following conditions
37 1.11 agc * are met:
38 1.11 agc * 1. Redistributions of source code must retain the above copyright
39 1.11 agc * notice, this list of conditions and the following disclaimer.
40 1.11 agc * 2. Redistributions in binary form must reproduce the above copyright
41 1.11 agc * notice, this list of conditions and the following disclaimer in the
42 1.11 agc * documentation and/or other materials provided with the distribution.
43 1.1 bouyer *
44 1.14 bouyer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
45 1.14 bouyer * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
46 1.14 bouyer * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
47 1.14 bouyer * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
48 1.14 bouyer * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
49 1.14 bouyer * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
50 1.14 bouyer * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
51 1.14 bouyer * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
52 1.14 bouyer * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
53 1.14 bouyer * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54 1.1 bouyer */
55 1.1 bouyer
56 1.2 lukem #include <sys/cdefs.h>
57 1.1 bouyer #ifndef lint
58 1.1 bouyer #if 0
59 1.1 bouyer static char sccsid[] = "@(#)inode.c 8.5 (Berkeley) 2/8/95";
60 1.1 bouyer #else
61 1.36 dholland __RCSID("$NetBSD: inode.c,v 1.36 2013/06/23 07:28:36 dholland Exp $");
62 1.1 bouyer #endif
63 1.1 bouyer #endif /* not lint */
64 1.1 bouyer
65 1.1 bouyer #include <sys/param.h>
66 1.1 bouyer #include <sys/time.h>
67 1.1 bouyer #include <ufs/ext2fs/ext2fs_dinode.h>
68 1.1 bouyer #include <ufs/ext2fs/ext2fs_dir.h>
69 1.1 bouyer #include <ufs/ext2fs/ext2fs.h>
70 1.1 bouyer
71 1.1 bouyer #include <ufs/ufs/dinode.h> /* for IFMT & friends */
72 1.1 bouyer #ifndef SMALL
73 1.1 bouyer #include <pwd.h>
74 1.1 bouyer #endif
75 1.1 bouyer #include <stdio.h>
76 1.1 bouyer #include <stdlib.h>
77 1.1 bouyer #include <string.h>
78 1.4 kleink #include <time.h>
79 1.1 bouyer
80 1.1 bouyer #include "fsck.h"
81 1.1 bouyer #include "fsutil.h"
82 1.1 bouyer #include "extern.h"
83 1.1 bouyer
84 1.3 bouyer /*
85 1.3 bouyer * CG is stored in fs byte order in memory, so we can't use ino_to_fsba
86 1.3 bouyer * here.
87 1.3 bouyer */
88 1.3 bouyer
89 1.3 bouyer #define fsck_ino_to_fsba(fs, x) \
90 1.3 bouyer (fs2h32((fs)->e2fs_gd[ino_to_cg(fs, x)].ext2bgd_i_tables) + \
91 1.3 bouyer (((x)-1) % (fs)->e2fs.e2fs_ipg)/(fs)->e2fs_ipb)
92 1.3 bouyer
93 1.3 bouyer
94 1.1 bouyer static ino_t startinum;
95 1.1 bouyer
96 1.15 xtraeme static int iblock(struct inodesc *, long, u_int64_t);
97 1.1 bouyer
98 1.17 ws static int setlarge(void);
99 1.17 ws
100 1.32 jakllsch static int sethuge(void);
101 1.32 jakllsch
102 1.17 ws static int
103 1.17 ws setlarge(void)
104 1.17 ws {
105 1.17 ws if (sblock.e2fs.e2fs_rev < E2FS_REV1) {
106 1.17 ws pfatal("LARGE FILES UNSUPPORTED ON REVISION 0 FILESYSTEMS");
107 1.17 ws return 0;
108 1.17 ws }
109 1.17 ws if (!(sblock.e2fs.e2fs_features_rocompat & EXT2F_ROCOMPAT_LARGEFILE)) {
110 1.17 ws if (preen)
111 1.17 ws pwarn("SETTING LARGE FILE INDICATOR\n");
112 1.17 ws else if (!reply("SET LARGE FILE INDICATOR"))
113 1.17 ws return 0;
114 1.17 ws sblock.e2fs.e2fs_features_rocompat |= EXT2F_ROCOMPAT_LARGEFILE;
115 1.17 ws sbdirty();
116 1.17 ws }
117 1.17 ws return 1;
118 1.17 ws }
119 1.17 ws
120 1.32 jakllsch static int
121 1.32 jakllsch sethuge(void)
122 1.32 jakllsch {
123 1.32 jakllsch if (sblock.e2fs.e2fs_rev < E2FS_REV1) {
124 1.32 jakllsch pfatal("HUGE FILES UNSUPPORTED ON REVISION 0 FILESYSTEMS");
125 1.32 jakllsch return 0;
126 1.32 jakllsch }
127 1.32 jakllsch if (!(sblock.e2fs.e2fs_features_rocompat & EXT2F_ROCOMPAT_HUGE_FILE)) {
128 1.32 jakllsch if (preen)
129 1.32 jakllsch pwarn("SETTING HUGE FILE FEATURE\n");
130 1.32 jakllsch else if (!reply("SET HUGE FILE FEATURE"))
131 1.32 jakllsch return 0;
132 1.32 jakllsch sblock.e2fs.e2fs_features_rocompat |= EXT2F_ROCOMPAT_HUGE_FILE;
133 1.32 jakllsch sbdirty();
134 1.32 jakllsch }
135 1.32 jakllsch return 1;
136 1.32 jakllsch }
137 1.32 jakllsch
138 1.17 ws u_int64_t
139 1.17 ws inosize(struct ext2fs_dinode *dp)
140 1.17 ws {
141 1.17 ws u_int64_t size = fs2h32(dp->e2di_size);
142 1.17 ws
143 1.17 ws if ((fs2h16(dp->e2di_mode) & IFMT) == IFREG)
144 1.17 ws size |= (u_int64_t)fs2h32(dp->e2di_dacl) << 32;
145 1.25 tsutsui if (size > INT32_MAX)
146 1.17 ws (void)setlarge();
147 1.17 ws return size;
148 1.17 ws }
149 1.17 ws
150 1.17 ws void
151 1.17 ws inossize(struct ext2fs_dinode *dp, u_int64_t size)
152 1.17 ws {
153 1.17 ws if ((fs2h16(dp->e2di_mode) & IFMT) == IFREG) {
154 1.17 ws dp->e2di_dacl = h2fs32(size >> 32);
155 1.25 tsutsui if (size > INT32_MAX)
156 1.17 ws if (!setlarge())
157 1.17 ws return;
158 1.25 tsutsui } else if (size > INT32_MAX) {
159 1.17 ws pfatal("TRYING TO SET FILESIZE TO %llu ON MODE %x FILE\n",
160 1.17 ws (unsigned long long)size, fs2h16(dp->e2di_mode) & IFMT);
161 1.17 ws return;
162 1.17 ws }
163 1.17 ws dp->e2di_size = h2fs32(size);
164 1.17 ws }
165 1.17 ws
166 1.1 bouyer int
167 1.15 xtraeme ckinode(struct ext2fs_dinode *dp, struct inodesc *idesc)
168 1.1 bouyer {
169 1.2 lukem u_int32_t *ap;
170 1.2 lukem long ret, n, ndb;
171 1.1 bouyer struct ext2fs_dinode dino;
172 1.1 bouyer u_int64_t remsize, sizepb;
173 1.1 bouyer mode_t mode;
174 1.1 bouyer char pathbuf[MAXPATHLEN + 1];
175 1.1 bouyer
176 1.1 bouyer if (idesc->id_fix != IGNORE)
177 1.1 bouyer idesc->id_fix = DONTKNOW;
178 1.1 bouyer idesc->id_entryno = 0;
179 1.17 ws idesc->id_filesize = inosize(dp);
180 1.3 bouyer mode = fs2h16(dp->e2di_mode) & IFMT;
181 1.6 bouyer if (mode == IFBLK || mode == IFCHR || mode == IFIFO ||
182 1.17 ws (mode == IFLNK && (inosize(dp) < EXT2_MAXSYMLINKLEN)))
183 1.1 bouyer return (KEEPON);
184 1.1 bouyer dino = *dp;
185 1.17 ws ndb = howmany(inosize(&dino), sblock.e2fs_bsize);
186 1.33 dholland for (ap = &dino.e2di_blocks[0]; ap < &dino.e2di_blocks[EXT2FS_NDADDR];
187 1.24 tsutsui ap++,ndb--) {
188 1.1 bouyer idesc->id_numfrags = 1;
189 1.1 bouyer if (*ap == 0) {
190 1.1 bouyer if (idesc->id_type == DATA && ndb > 0) {
191 1.1 bouyer /* An empty block in a directory XXX */
192 1.10 itojun getpathname(pathbuf, sizeof(pathbuf),
193 1.10 itojun idesc->id_number, idesc->id_number);
194 1.1 bouyer pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
195 1.1 bouyer pathbuf);
196 1.1 bouyer if (reply("ADJUST LENGTH") == 1) {
197 1.1 bouyer dp = ginode(idesc->id_number);
198 1.17 ws inossize(dp,
199 1.17 ws (ap - &dino.e2di_blocks[0]) *
200 1.3 bouyer sblock.e2fs_bsize);
201 1.1 bouyer printf(
202 1.1 bouyer "YOU MUST RERUN FSCK AFTERWARDS\n");
203 1.1 bouyer rerun = 1;
204 1.1 bouyer inodirty();
205 1.1 bouyer }
206 1.1 bouyer }
207 1.1 bouyer continue;
208 1.1 bouyer }
209 1.3 bouyer idesc->id_blkno = fs2h32(*ap);
210 1.1 bouyer if (idesc->id_type == ADDR)
211 1.1 bouyer ret = (*idesc->id_func)(idesc);
212 1.1 bouyer else
213 1.1 bouyer ret = dirscan(idesc);
214 1.1 bouyer if (ret & STOP)
215 1.1 bouyer return (ret);
216 1.1 bouyer }
217 1.1 bouyer idesc->id_numfrags = 1;
218 1.33 dholland remsize = inosize(&dino) - sblock.e2fs_bsize * EXT2FS_NDADDR;
219 1.1 bouyer sizepb = sblock.e2fs_bsize;
220 1.33 dholland for (ap = &dino.e2di_blocks[EXT2FS_NDADDR], n = 1; n <= EXT2FS_NIADDR; ap++, n++) {
221 1.1 bouyer if (*ap) {
222 1.3 bouyer idesc->id_blkno = fs2h32(*ap);
223 1.1 bouyer ret = iblock(idesc, n, remsize);
224 1.1 bouyer if (ret & STOP)
225 1.1 bouyer return (ret);
226 1.1 bouyer } else {
227 1.1 bouyer if (idesc->id_type == DATA && remsize > 0) {
228 1.1 bouyer /* An empty block in a directory XXX */
229 1.10 itojun getpathname(pathbuf, sizeof(pathbuf),
230 1.10 itojun idesc->id_number, idesc->id_number);
231 1.1 bouyer pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
232 1.1 bouyer pathbuf);
233 1.1 bouyer if (reply("ADJUST LENGTH") == 1) {
234 1.1 bouyer dp = ginode(idesc->id_number);
235 1.17 ws inossize(dp, inosize(dp) - remsize);
236 1.1 bouyer remsize = 0;
237 1.1 bouyer printf(
238 1.1 bouyer "YOU MUST RERUN FSCK AFTERWARDS\n");
239 1.1 bouyer rerun = 1;
240 1.1 bouyer inodirty();
241 1.1 bouyer break;
242 1.1 bouyer }
243 1.1 bouyer }
244 1.1 bouyer }
245 1.34 dholland sizepb *= EXT2_NINDIR(&sblock);
246 1.1 bouyer remsize -= sizepb;
247 1.1 bouyer }
248 1.1 bouyer return (KEEPON);
249 1.1 bouyer }
250 1.1 bouyer
251 1.1 bouyer static int
252 1.15 xtraeme iblock(struct inodesc *idesc, long ilevel, u_int64_t isize)
253 1.1 bouyer {
254 1.9 fvdl /* XXX ondisk32 */
255 1.9 fvdl int32_t *ap;
256 1.9 fvdl int32_t *aplim;
257 1.2 lukem struct bufarea *bp;
258 1.29 lukem int i, n, (*func)(struct inodesc *);
259 1.29 lukem size_t nif;
260 1.1 bouyer u_int64_t sizepb;
261 1.1 bouyer char buf[BUFSIZ];
262 1.1 bouyer char pathbuf[MAXPATHLEN + 1];
263 1.1 bouyer struct ext2fs_dinode *dp;
264 1.1 bouyer
265 1.1 bouyer if (idesc->id_type == ADDR) {
266 1.1 bouyer func = idesc->id_func;
267 1.1 bouyer if (((n = (*func)(idesc)) & KEEPON) == 0)
268 1.1 bouyer return (n);
269 1.1 bouyer } else
270 1.1 bouyer func = dirscan;
271 1.1 bouyer if (chkrange(idesc->id_blkno, idesc->id_numfrags))
272 1.1 bouyer return (SKIP);
273 1.1 bouyer bp = getdatablk(idesc->id_blkno, sblock.e2fs_bsize);
274 1.1 bouyer ilevel--;
275 1.1 bouyer for (sizepb = sblock.e2fs_bsize, i = 0; i < ilevel; i++)
276 1.34 dholland sizepb *= EXT2_NINDIR(&sblock);
277 1.34 dholland if (isize > sizepb * EXT2_NINDIR(&sblock))
278 1.34 dholland nif = EXT2_NINDIR(&sblock);
279 1.1 bouyer else
280 1.1 bouyer nif = howmany(isize, sizepb);
281 1.1 bouyer if (idesc->id_func == pass1check &&
282 1.34 dholland nif < EXT2_NINDIR(&sblock)) {
283 1.34 dholland aplim = &bp->b_un.b_indir[EXT2_NINDIR(&sblock)];
284 1.1 bouyer for (ap = &bp->b_un.b_indir[nif]; ap < aplim; ap++) {
285 1.1 bouyer if (*ap == 0)
286 1.1 bouyer continue;
287 1.5 mycroft (void)snprintf(buf, sizeof(buf),
288 1.20 christos "PARTIALLY TRUNCATED INODE I=%llu",
289 1.20 christos (unsigned long long)idesc->id_number);
290 1.1 bouyer if (dofix(idesc, buf)) {
291 1.1 bouyer *ap = 0;
292 1.1 bouyer dirty(bp);
293 1.1 bouyer }
294 1.1 bouyer }
295 1.1 bouyer flush(fswritefd, bp);
296 1.1 bouyer }
297 1.1 bouyer aplim = &bp->b_un.b_indir[nif];
298 1.1 bouyer for (ap = bp->b_un.b_indir; ap < aplim; ap++) {
299 1.1 bouyer if (*ap) {
300 1.3 bouyer idesc->id_blkno = fs2h32(*ap);
301 1.1 bouyer if (ilevel == 0)
302 1.1 bouyer n = (*func)(idesc);
303 1.1 bouyer else
304 1.1 bouyer n = iblock(idesc, ilevel, isize);
305 1.1 bouyer if (n & STOP) {
306 1.1 bouyer bp->b_flags &= ~B_INUSE;
307 1.1 bouyer return (n);
308 1.1 bouyer }
309 1.1 bouyer } else {
310 1.1 bouyer if (idesc->id_type == DATA && isize > 0) {
311 1.1 bouyer /* An empty block in a directory XXX */
312 1.10 itojun getpathname(pathbuf, sizeof(pathbuf),
313 1.10 itojun idesc->id_number, idesc->id_number);
314 1.1 bouyer pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
315 1.1 bouyer pathbuf);
316 1.1 bouyer if (reply("ADJUST LENGTH") == 1) {
317 1.1 bouyer dp = ginode(idesc->id_number);
318 1.17 ws inossize(dp, inosize(dp) - isize);
319 1.1 bouyer isize = 0;
320 1.1 bouyer printf(
321 1.1 bouyer "YOU MUST RERUN FSCK AFTERWARDS\n");
322 1.1 bouyer rerun = 1;
323 1.1 bouyer inodirty();
324 1.1 bouyer bp->b_flags &= ~B_INUSE;
325 1.1 bouyer return(STOP);
326 1.1 bouyer }
327 1.1 bouyer }
328 1.1 bouyer }
329 1.1 bouyer isize -= sizepb;
330 1.1 bouyer }
331 1.1 bouyer bp->b_flags &= ~B_INUSE;
332 1.1 bouyer return (KEEPON);
333 1.1 bouyer }
334 1.1 bouyer
335 1.1 bouyer /*
336 1.1 bouyer * Check that a block in a legal block number.
337 1.1 bouyer * Return 0 if in range, 1 if out of range.
338 1.1 bouyer */
339 1.1 bouyer int
340 1.15 xtraeme chkrange(daddr_t blk, int cnt)
341 1.1 bouyer {
342 1.8 bouyer int c, overh;
343 1.1 bouyer
344 1.24 tsutsui if ((unsigned int)(blk + cnt) > maxfsblock)
345 1.1 bouyer return (1);
346 1.1 bouyer c = dtog(&sblock, blk);
347 1.8 bouyer overh = cgoverhead(c);
348 1.8 bouyer if (blk < sblock.e2fs.e2fs_bpg * c + overh +
349 1.8 bouyer sblock.e2fs.e2fs_first_dblock) {
350 1.8 bouyer if ((blk + cnt) > sblock.e2fs.e2fs_bpg * c + overh +
351 1.8 bouyer sblock.e2fs.e2fs_first_dblock) {
352 1.1 bouyer if (debug) {
353 1.9 fvdl printf("blk %lld < cgdmin %d;",
354 1.9 fvdl (long long)blk,
355 1.9 fvdl sblock.e2fs.e2fs_bpg * c + overh +
356 1.8 bouyer sblock.e2fs.e2fs_first_dblock);
357 1.9 fvdl printf(" blk + cnt %lld > cgsbase %d\n",
358 1.9 fvdl (long long)(blk + cnt),
359 1.9 fvdl sblock.e2fs.e2fs_bpg * c +
360 1.8 bouyer overh + sblock.e2fs.e2fs_first_dblock);
361 1.1 bouyer }
362 1.1 bouyer return (1);
363 1.1 bouyer }
364 1.1 bouyer } else {
365 1.8 bouyer if ((blk + cnt) > sblock.e2fs.e2fs_bpg * (c + 1) + overh +
366 1.8 bouyer sblock.e2fs.e2fs_first_dblock) {
367 1.1 bouyer if (debug) {
368 1.9 fvdl printf("blk %lld >= cgdmin %d;",
369 1.9 fvdl (long long)blk,
370 1.9 fvdl sblock.e2fs.e2fs_bpg * c + overh +
371 1.8 bouyer sblock.e2fs.e2fs_first_dblock);
372 1.9 fvdl printf(" blk + cnt %lld > cgdmax %d\n",
373 1.9 fvdl (long long)(blk+cnt),
374 1.9 fvdl sblock.e2fs.e2fs_bpg * (c + 1) +
375 1.8 bouyer overh + sblock.e2fs.e2fs_first_dblock);
376 1.1 bouyer }
377 1.1 bouyer return (1);
378 1.1 bouyer }
379 1.1 bouyer }
380 1.1 bouyer return (0);
381 1.1 bouyer }
382 1.1 bouyer
383 1.1 bouyer /*
384 1.1 bouyer * General purpose interface for reading inodes.
385 1.1 bouyer */
386 1.1 bouyer struct ext2fs_dinode *
387 1.15 xtraeme ginode(ino_t inumber)
388 1.1 bouyer {
389 1.1 bouyer daddr_t iblk;
390 1.28 tsutsui struct ext2fs_dinode *dp;
391 1.1 bouyer
392 1.21 tsutsui if ((inumber < EXT2_FIRSTINO &&
393 1.21 tsutsui inumber != EXT2_ROOTINO &&
394 1.21 tsutsui !(inumber == EXT2_RESIZEINO &&
395 1.21 tsutsui (sblock.e2fs.e2fs_features_compat & EXT2F_COMPAT_RESIZE) != 0))
396 1.1 bouyer || inumber > maxino)
397 1.22 lukem errexit("bad inode number %llu to ginode",
398 1.20 christos (unsigned long long)inumber);
399 1.1 bouyer if (startinum == 0 ||
400 1.1 bouyer inumber < startinum || inumber >= startinum + sblock.e2fs_ipb) {
401 1.3 bouyer iblk = fsck_ino_to_fsba(&sblock, inumber);
402 1.1 bouyer if (pbp != 0)
403 1.1 bouyer pbp->b_flags &= ~B_INUSE;
404 1.1 bouyer pbp = getdatablk(iblk, sblock.e2fs_bsize);
405 1.28 tsutsui startinum =
406 1.28 tsutsui ((inumber - 1) / sblock.e2fs_ipb) * sblock.e2fs_ipb + 1;
407 1.1 bouyer }
408 1.28 tsutsui dp = (struct ext2fs_dinode *)(pbp->b_un.b_buf +
409 1.28 tsutsui EXT2_DINODE_SIZE(&sblock) * ino_to_fsbo(&sblock, inumber));
410 1.28 tsutsui
411 1.28 tsutsui return dp;
412 1.1 bouyer }
413 1.1 bouyer
414 1.1 bouyer /*
415 1.1 bouyer * Special purpose version of ginode used to optimize first pass
416 1.1 bouyer * over all the inodes in numerical order.
417 1.1 bouyer */
418 1.1 bouyer ino_t nextino, lastinum;
419 1.1 bouyer long readcnt, readpercg, fullcnt, inobufsize, partialcnt, partialsize;
420 1.28 tsutsui char *inodebuf;
421 1.1 bouyer
422 1.1 bouyer struct ext2fs_dinode *
423 1.15 xtraeme getnextinode(ino_t inumber)
424 1.1 bouyer {
425 1.1 bouyer long size;
426 1.1 bouyer daddr_t dblk;
427 1.28 tsutsui struct ext2fs_dinode *dp;
428 1.28 tsutsui static char *bp;
429 1.1 bouyer
430 1.1 bouyer if (inumber != nextino++ || inumber > maxino)
431 1.22 lukem errexit("bad inode number %llu to nextinode",
432 1.20 christos (unsigned long long)inumber);
433 1.1 bouyer if (inumber >= lastinum) {
434 1.1 bouyer readcnt++;
435 1.35 dholland dblk = EXT2_FSBTODB(&sblock, fsck_ino_to_fsba(&sblock, lastinum));
436 1.1 bouyer if (readcnt % readpercg == 0) {
437 1.1 bouyer size = partialsize;
438 1.1 bouyer lastinum += partialcnt;
439 1.1 bouyer } else {
440 1.1 bouyer size = inobufsize;
441 1.1 bouyer lastinum += fullcnt;
442 1.1 bouyer }
443 1.28 tsutsui (void)bread(fsreadfd, inodebuf, dblk, size);
444 1.28 tsutsui bp = inodebuf;
445 1.1 bouyer }
446 1.28 tsutsui dp = (struct ext2fs_dinode *)bp;
447 1.28 tsutsui bp += EXT2_DINODE_SIZE(&sblock);
448 1.28 tsutsui
449 1.28 tsutsui return dp;
450 1.1 bouyer }
451 1.1 bouyer
452 1.1 bouyer void
453 1.15 xtraeme resetinodebuf(void)
454 1.1 bouyer {
455 1.1 bouyer
456 1.1 bouyer startinum = 0;
457 1.1 bouyer nextino = 1;
458 1.1 bouyer lastinum = 1;
459 1.1 bouyer readcnt = 0;
460 1.36 dholland inobufsize = ext2_blkroundup(&sblock, INOBUFSIZE);
461 1.28 tsutsui fullcnt = inobufsize / EXT2_DINODE_SIZE(&sblock);
462 1.1 bouyer readpercg = sblock.e2fs.e2fs_ipg / fullcnt;
463 1.1 bouyer partialcnt = sblock.e2fs.e2fs_ipg % fullcnt;
464 1.28 tsutsui partialsize = partialcnt * EXT2_DINODE_SIZE(&sblock);
465 1.1 bouyer if (partialcnt != 0) {
466 1.1 bouyer readpercg++;
467 1.1 bouyer } else {
468 1.1 bouyer partialcnt = fullcnt;
469 1.1 bouyer partialsize = inobufsize;
470 1.1 bouyer }
471 1.1 bouyer if (inodebuf == NULL &&
472 1.24 tsutsui (inodebuf = malloc((unsigned int)inobufsize)) == NULL)
473 1.22 lukem errexit("Cannot allocate space for inode buffer");
474 1.1 bouyer while (nextino < EXT2_ROOTINO)
475 1.1 bouyer (void)getnextinode(nextino);
476 1.1 bouyer }
477 1.1 bouyer
478 1.1 bouyer void
479 1.15 xtraeme freeinodebuf(void)
480 1.1 bouyer {
481 1.1 bouyer
482 1.1 bouyer if (inodebuf != NULL)
483 1.24 tsutsui free(inodebuf);
484 1.1 bouyer inodebuf = NULL;
485 1.1 bouyer }
486 1.1 bouyer
487 1.1 bouyer /*
488 1.1 bouyer * Routines to maintain information about directory inodes.
489 1.1 bouyer * This is built during the first pass and used during the
490 1.1 bouyer * second and third passes.
491 1.1 bouyer *
492 1.1 bouyer * Enter inodes into the cache.
493 1.1 bouyer */
494 1.1 bouyer void
495 1.15 xtraeme cacheino(struct ext2fs_dinode *dp, ino_t inumber)
496 1.1 bouyer {
497 1.2 lukem struct inoinfo *inp;
498 1.1 bouyer struct inoinfo **inpp;
499 1.1 bouyer unsigned int blks;
500 1.1 bouyer
501 1.17 ws blks = howmany(inosize(dp), sblock.e2fs_bsize);
502 1.33 dholland if (blks > EXT2FS_NDADDR)
503 1.33 dholland blks = EXT2FS_NDADDR + EXT2FS_NIADDR;
504 1.9 fvdl /* XXX ondisk32 */
505 1.24 tsutsui inp = malloc(sizeof(*inp) + (blks - 1) * sizeof(int32_t));
506 1.1 bouyer if (inp == NULL)
507 1.1 bouyer return;
508 1.1 bouyer inpp = &inphead[inumber % numdirs];
509 1.1 bouyer inp->i_nexthash = *inpp;
510 1.1 bouyer *inpp = inp;
511 1.1 bouyer inp->i_child = inp->i_sibling = inp->i_parentp = 0;
512 1.1 bouyer if (inumber == EXT2_ROOTINO)
513 1.1 bouyer inp->i_parent = EXT2_ROOTINO;
514 1.1 bouyer else
515 1.1 bouyer inp->i_parent = (ino_t)0;
516 1.1 bouyer inp->i_dotdot = (ino_t)0;
517 1.1 bouyer inp->i_number = inumber;
518 1.17 ws inp->i_isize = inosize(dp);
519 1.9 fvdl /* XXX ondisk32 */
520 1.9 fvdl inp->i_numblks = blks * sizeof(int32_t);
521 1.1 bouyer memcpy(&inp->i_blks[0], &dp->e2di_blocks[0], (size_t)inp->i_numblks);
522 1.1 bouyer if (inplast == listmax) {
523 1.1 bouyer listmax += 100;
524 1.1 bouyer inpsort = (struct inoinfo **)realloc((char *)inpsort,
525 1.24 tsutsui (unsigned int)listmax * sizeof(struct inoinfo *));
526 1.1 bouyer if (inpsort == NULL)
527 1.22 lukem errexit("cannot increase directory list");
528 1.1 bouyer }
529 1.1 bouyer inpsort[inplast++] = inp;
530 1.1 bouyer }
531 1.1 bouyer
532 1.1 bouyer /*
533 1.1 bouyer * Look up an inode cache structure.
534 1.1 bouyer */
535 1.1 bouyer struct inoinfo *
536 1.15 xtraeme getinoinfo(ino_t inumber)
537 1.1 bouyer {
538 1.2 lukem struct inoinfo *inp;
539 1.1 bouyer
540 1.1 bouyer for (inp = inphead[inumber % numdirs]; inp; inp = inp->i_nexthash) {
541 1.1 bouyer if (inp->i_number != inumber)
542 1.1 bouyer continue;
543 1.1 bouyer return (inp);
544 1.1 bouyer }
545 1.22 lukem errexit("cannot find inode %llu", (unsigned long long)inumber);
546 1.1 bouyer return ((struct inoinfo *)0);
547 1.1 bouyer }
548 1.1 bouyer
549 1.1 bouyer /*
550 1.1 bouyer * Clean up all the inode cache structure.
551 1.1 bouyer */
552 1.1 bouyer void
553 1.15 xtraeme inocleanup(void)
554 1.1 bouyer {
555 1.2 lukem struct inoinfo **inpp;
556 1.1 bouyer
557 1.1 bouyer if (inphead == NULL)
558 1.1 bouyer return;
559 1.1 bouyer for (inpp = &inpsort[inplast - 1]; inpp >= inpsort; inpp--)
560 1.24 tsutsui free(*inpp);
561 1.24 tsutsui free(inphead);
562 1.24 tsutsui free(inpsort);
563 1.1 bouyer inphead = inpsort = NULL;
564 1.1 bouyer }
565 1.1 bouyer
566 1.1 bouyer void
567 1.15 xtraeme inodirty(void)
568 1.1 bouyer {
569 1.1 bouyer
570 1.1 bouyer dirty(pbp);
571 1.1 bouyer }
572 1.1 bouyer
573 1.1 bouyer void
574 1.18 christos clri(struct inodesc *idesc, const char *type, int flag)
575 1.1 bouyer {
576 1.2 lukem struct ext2fs_dinode *dp;
577 1.1 bouyer
578 1.1 bouyer dp = ginode(idesc->id_number);
579 1.1 bouyer if (flag == 1) {
580 1.1 bouyer pwarn("%s %s", type,
581 1.26 tsutsui (fs2h16(dp->e2di_mode) & IFMT) == IFDIR ? "DIR" : "FILE");
582 1.1 bouyer pinode(idesc->id_number);
583 1.1 bouyer }
584 1.1 bouyer if (preen || reply("CLEAR") == 1) {
585 1.1 bouyer if (preen)
586 1.1 bouyer printf(" (CLEARED)\n");
587 1.1 bouyer n_files--;
588 1.1 bouyer (void)ckinode(dp, idesc);
589 1.1 bouyer clearinode(dp);
590 1.1 bouyer statemap[idesc->id_number] = USTATE;
591 1.1 bouyer inodirty();
592 1.1 bouyer }
593 1.1 bouyer }
594 1.1 bouyer
595 1.1 bouyer int
596 1.15 xtraeme findname(struct inodesc *idesc)
597 1.1 bouyer {
598 1.2 lukem struct ext2fs_direct *dirp = idesc->id_dirp;
599 1.7 bouyer u_int16_t namlen = dirp->e2d_namlen;
600 1.18 christos /* from utilities.c namebuf[] variable */
601 1.18 christos char *buf = __UNCONST(idesc->id_name);
602 1.18 christos if (namlen > MAXPATHLEN) {
603 1.18 christos /* XXX: Prevent overflow but don't fix */
604 1.18 christos namlen = MAXPATHLEN;
605 1.18 christos }
606 1.1 bouyer
607 1.3 bouyer if (fs2h32(dirp->e2d_ino) != idesc->id_parent)
608 1.1 bouyer return (KEEPON);
609 1.18 christos (void)memcpy(buf, dirp->e2d_name, (size_t)namlen);
610 1.18 christos buf[namlen] = '\0';
611 1.1 bouyer return (STOP|FOUND);
612 1.1 bouyer }
613 1.1 bouyer
614 1.1 bouyer int
615 1.15 xtraeme findino(struct inodesc *idesc)
616 1.1 bouyer {
617 1.2 lukem struct ext2fs_direct *dirp = idesc->id_dirp;
618 1.3 bouyer u_int32_t ino = fs2h32(dirp->e2d_ino);
619 1.1 bouyer
620 1.3 bouyer if (ino == 0)
621 1.1 bouyer return (KEEPON);
622 1.1 bouyer if (strcmp(dirp->e2d_name, idesc->id_name) == 0 &&
623 1.3 bouyer (ino == EXT2_ROOTINO || ino >= EXT2_FIRSTINO)
624 1.3 bouyer && ino <= maxino) {
625 1.3 bouyer idesc->id_parent = ino;
626 1.1 bouyer return (STOP|FOUND);
627 1.1 bouyer }
628 1.1 bouyer return (KEEPON);
629 1.1 bouyer }
630 1.1 bouyer
631 1.1 bouyer void
632 1.15 xtraeme pinode(ino_t ino)
633 1.1 bouyer {
634 1.2 lukem struct ext2fs_dinode *dp;
635 1.1 bouyer struct passwd *pw;
636 1.26 tsutsui uid_t uid;
637 1.1 bouyer
638 1.20 christos printf(" I=%llu ", (unsigned long long)ino);
639 1.1 bouyer if ((ino < EXT2_FIRSTINO && ino != EXT2_ROOTINO) || ino > maxino)
640 1.1 bouyer return;
641 1.1 bouyer dp = ginode(ino);
642 1.26 tsutsui uid = fs2h16(dp->e2di_uid);
643 1.27 tsutsui if (sblock.e2fs.e2fs_rev > E2FS_REV0)
644 1.27 tsutsui uid |= fs2h16(dp->e2di_uid_high) << 16;
645 1.1 bouyer printf(" OWNER=");
646 1.1 bouyer #ifndef SMALL
647 1.26 tsutsui if (Uflag && (pw = getpwuid(uid)) != 0)
648 1.1 bouyer printf("%s ", pw->pw_name);
649 1.1 bouyer else
650 1.1 bouyer #endif
651 1.26 tsutsui printf("%u ", (unsigned int)uid);
652 1.3 bouyer printf("MODE=%o\n", fs2h16(dp->e2di_mode));
653 1.1 bouyer if (preen)
654 1.1 bouyer printf("%s: ", cdevname());
655 1.17 ws printf("SIZE=%llu ", (long long)inosize(dp));
656 1.31 christos printf("MTIME=%s ", print_mtime(fs2h32(dp->e2di_mtime)));
657 1.1 bouyer }
658 1.1 bouyer
659 1.1 bouyer void
660 1.19 christos blkerror(ino_t ino, const char *type, daddr_t blk)
661 1.1 bouyer {
662 1.1 bouyer
663 1.20 christos pfatal("%lld %s I=%llu", (long long)blk, type, (unsigned long long)ino);
664 1.1 bouyer printf("\n");
665 1.1 bouyer switch (statemap[ino]) {
666 1.1 bouyer
667 1.1 bouyer case FSTATE:
668 1.1 bouyer statemap[ino] = FCLEAR;
669 1.1 bouyer return;
670 1.1 bouyer
671 1.1 bouyer case DSTATE:
672 1.1 bouyer statemap[ino] = DCLEAR;
673 1.1 bouyer return;
674 1.1 bouyer
675 1.1 bouyer case FCLEAR:
676 1.1 bouyer case DCLEAR:
677 1.1 bouyer return;
678 1.1 bouyer
679 1.1 bouyer default:
680 1.22 lukem errexit("BAD STATE %d TO BLKERR", statemap[ino]);
681 1.1 bouyer /* NOTREACHED */
682 1.1 bouyer }
683 1.1 bouyer }
684 1.1 bouyer
685 1.1 bouyer /*
686 1.1 bouyer * allocate an unused inode
687 1.1 bouyer */
688 1.1 bouyer ino_t
689 1.15 xtraeme allocino(ino_t request, int type)
690 1.1 bouyer {
691 1.2 lukem ino_t ino;
692 1.2 lukem struct ext2fs_dinode *dp;
693 1.1 bouyer time_t t;
694 1.1 bouyer
695 1.1 bouyer if (request == 0)
696 1.1 bouyer request = EXT2_ROOTINO;
697 1.1 bouyer else if (statemap[request] != USTATE)
698 1.1 bouyer return (0);
699 1.1 bouyer for (ino = request; ino < maxino; ino++) {
700 1.1 bouyer if ((ino > EXT2_ROOTINO) && (ino < EXT2_FIRSTINO))
701 1.1 bouyer continue;
702 1.1 bouyer if (statemap[ino] == USTATE)
703 1.1 bouyer break;
704 1.1 bouyer }
705 1.1 bouyer if (ino == maxino)
706 1.1 bouyer return (0);
707 1.1 bouyer switch (type & IFMT) {
708 1.1 bouyer case IFDIR:
709 1.1 bouyer statemap[ino] = DSTATE;
710 1.1 bouyer break;
711 1.1 bouyer case IFREG:
712 1.1 bouyer case IFLNK:
713 1.1 bouyer statemap[ino] = FSTATE;
714 1.1 bouyer break;
715 1.1 bouyer default:
716 1.1 bouyer return (0);
717 1.1 bouyer }
718 1.1 bouyer dp = ginode(ino);
719 1.3 bouyer dp->e2di_blocks[0] = h2fs32(allocblk());
720 1.1 bouyer if (dp->e2di_blocks[0] == 0) {
721 1.1 bouyer statemap[ino] = USTATE;
722 1.1 bouyer return (0);
723 1.1 bouyer }
724 1.3 bouyer dp->e2di_mode = h2fs16(type);
725 1.1 bouyer (void)time(&t);
726 1.3 bouyer dp->e2di_atime = h2fs32(t);
727 1.1 bouyer dp->e2di_mtime = dp->e2di_ctime = dp->e2di_atime;
728 1.1 bouyer dp->e2di_dtime = 0;
729 1.17 ws inossize(dp, sblock.e2fs_bsize);
730 1.32 jakllsch inosnblock(dp, btodb(sblock.e2fs_bsize));
731 1.1 bouyer n_files++;
732 1.1 bouyer inodirty();
733 1.8 bouyer typemap[ino] = E2IFTODT(type);
734 1.1 bouyer return (ino);
735 1.1 bouyer }
736 1.1 bouyer
737 1.1 bouyer /*
738 1.1 bouyer * deallocate an inode
739 1.1 bouyer */
740 1.1 bouyer void
741 1.15 xtraeme freeino(ino_t ino)
742 1.1 bouyer {
743 1.1 bouyer struct inodesc idesc;
744 1.1 bouyer struct ext2fs_dinode *dp;
745 1.1 bouyer
746 1.1 bouyer memset(&idesc, 0, sizeof(struct inodesc));
747 1.1 bouyer idesc.id_type = ADDR;
748 1.1 bouyer idesc.id_func = pass4check;
749 1.1 bouyer idesc.id_number = ino;
750 1.1 bouyer dp = ginode(ino);
751 1.1 bouyer (void)ckinode(dp, &idesc);
752 1.1 bouyer clearinode(dp);
753 1.1 bouyer inodirty();
754 1.1 bouyer statemap[ino] = USTATE;
755 1.1 bouyer n_files--;
756 1.1 bouyer }
757 1.32 jakllsch
758 1.32 jakllsch uint64_t
759 1.32 jakllsch inonblock(struct ext2fs_dinode *dp)
760 1.32 jakllsch {
761 1.32 jakllsch uint64_t nblock;
762 1.32 jakllsch
763 1.32 jakllsch /* XXX check for EXT2_HUGE_FILE without EXT2F_ROCOMPAT_HUGE_FILE? */
764 1.32 jakllsch
765 1.32 jakllsch nblock = fs2h32(dp->e2di_nblock);
766 1.32 jakllsch
767 1.32 jakllsch if ((sblock.e2fs.e2fs_features_rocompat & EXT2F_ROCOMPAT_HUGE_FILE)) {
768 1.32 jakllsch nblock |= (uint64_t)fs2h16(dp->e2di_nblock_high) << 32;
769 1.32 jakllsch if (fs2h32(dp->e2di_flags) & EXT2_HUGE_FILE) {
770 1.35 dholland nblock = EXT2_FSBTODB(&sblock, nblock);
771 1.32 jakllsch }
772 1.32 jakllsch }
773 1.32 jakllsch
774 1.32 jakllsch return nblock;
775 1.32 jakllsch }
776 1.32 jakllsch
777 1.32 jakllsch void
778 1.32 jakllsch inosnblock(struct ext2fs_dinode *dp, uint64_t nblock)
779 1.32 jakllsch {
780 1.32 jakllsch uint32_t flags;
781 1.32 jakllsch
782 1.32 jakllsch flags = fs2h32(dp->e2di_flags);
783 1.32 jakllsch
784 1.32 jakllsch if (nblock <= 0xffffffffULL) {
785 1.32 jakllsch flags &= ~EXT2_HUGE_FILE;
786 1.32 jakllsch dp->e2di_flags = h2fs32(flags);
787 1.32 jakllsch dp->e2di_nblock = h2fs32(nblock);
788 1.32 jakllsch return;
789 1.32 jakllsch }
790 1.32 jakllsch
791 1.32 jakllsch sethuge();
792 1.32 jakllsch
793 1.32 jakllsch if (nblock <= 0xffffffffffffULL) {
794 1.32 jakllsch flags &= ~EXT2_HUGE_FILE;
795 1.32 jakllsch dp->e2di_flags = h2fs32(flags);
796 1.32 jakllsch dp->e2di_nblock = h2fs32(nblock);
797 1.32 jakllsch dp->e2di_nblock_high = h2fs16((nblock >> 32));
798 1.32 jakllsch return;
799 1.32 jakllsch }
800 1.32 jakllsch
801 1.35 dholland if (EXT2_DBTOFSB(&sblock, nblock) <= 0xffffffffffffULL) {
802 1.32 jakllsch flags |= EXT2_HUGE_FILE;
803 1.32 jakllsch dp->e2di_flags = h2fs32(flags);
804 1.35 dholland dp->e2di_nblock = h2fs32(EXT2_DBTOFSB(&sblock, nblock));
805 1.35 dholland dp->e2di_nblock_high = h2fs16((EXT2_DBTOFSB(&sblock, nblock) >> 32));
806 1.32 jakllsch return;
807 1.32 jakllsch }
808 1.32 jakllsch
809 1.32 jakllsch pfatal("trying to set nblocks higher than representable");
810 1.32 jakllsch
811 1.32 jakllsch return;
812 1.32 jakllsch }
813