inode.c revision 1.13 1 1.13 bouyer /* $NetBSD: inode.c,v 1.13 2003/10/05 20:54:45 bouyer 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 * 3. All advertising materials mentioning features or use of this software
44 1.1 bouyer * must display the following acknowledgement:
45 1.12 bouyer * This product includes software developed by Manuel Bouyer.
46 1.12 bouyer * 4. The name of the author may not be used to endorse or promote products
47 1.12 bouyer * derived from this software without specific prior written permission.
48 1.1 bouyer *
49 1.1 bouyer * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 1.1 bouyer * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 1.1 bouyer * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 1.1 bouyer * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 1.1 bouyer * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 1.1 bouyer * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 1.1 bouyer * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 1.1 bouyer * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 1.1 bouyer * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 1.1 bouyer * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 1.1 bouyer * SUCH DAMAGE.
60 1.1 bouyer */
61 1.1 bouyer
62 1.2 lukem #include <sys/cdefs.h>
63 1.1 bouyer #ifndef lint
64 1.1 bouyer #if 0
65 1.1 bouyer static char sccsid[] = "@(#)inode.c 8.5 (Berkeley) 2/8/95";
66 1.1 bouyer #else
67 1.13 bouyer __RCSID("$NetBSD: inode.c,v 1.13 2003/10/05 20:54:45 bouyer Exp $");
68 1.1 bouyer #endif
69 1.1 bouyer #endif /* not lint */
70 1.1 bouyer
71 1.1 bouyer #include <sys/param.h>
72 1.1 bouyer #include <sys/time.h>
73 1.1 bouyer #include <ufs/ext2fs/ext2fs_dinode.h>
74 1.1 bouyer #include <ufs/ext2fs/ext2fs_dir.h>
75 1.1 bouyer #include <ufs/ext2fs/ext2fs.h>
76 1.1 bouyer
77 1.1 bouyer #include <ufs/ufs/dinode.h> /* for IFMT & friends */
78 1.1 bouyer #ifndef SMALL
79 1.1 bouyer #include <pwd.h>
80 1.1 bouyer #endif
81 1.1 bouyer #include <stdio.h>
82 1.1 bouyer #include <stdlib.h>
83 1.1 bouyer #include <string.h>
84 1.4 kleink #include <time.h>
85 1.1 bouyer
86 1.1 bouyer #include "fsck.h"
87 1.1 bouyer #include "fsutil.h"
88 1.1 bouyer #include "extern.h"
89 1.1 bouyer
90 1.3 bouyer /*
91 1.3 bouyer * CG is stored in fs byte order in memory, so we can't use ino_to_fsba
92 1.3 bouyer * here.
93 1.3 bouyer */
94 1.3 bouyer
95 1.3 bouyer #define fsck_ino_to_fsba(fs, x) \
96 1.3 bouyer (fs2h32((fs)->e2fs_gd[ino_to_cg(fs, x)].ext2bgd_i_tables) + \
97 1.3 bouyer (((x)-1) % (fs)->e2fs.e2fs_ipg)/(fs)->e2fs_ipb)
98 1.3 bouyer
99 1.3 bouyer
100 1.1 bouyer static ino_t startinum;
101 1.1 bouyer
102 1.1 bouyer static int iblock __P((struct inodesc *, long, u_int64_t));
103 1.1 bouyer
104 1.1 bouyer int
105 1.1 bouyer ckinode(dp, idesc)
106 1.1 bouyer struct ext2fs_dinode *dp;
107 1.2 lukem struct inodesc *idesc;
108 1.1 bouyer {
109 1.2 lukem u_int32_t *ap;
110 1.2 lukem long ret, n, ndb;
111 1.1 bouyer struct ext2fs_dinode dino;
112 1.1 bouyer u_int64_t remsize, sizepb;
113 1.1 bouyer mode_t mode;
114 1.1 bouyer char pathbuf[MAXPATHLEN + 1];
115 1.1 bouyer
116 1.1 bouyer if (idesc->id_fix != IGNORE)
117 1.1 bouyer idesc->id_fix = DONTKNOW;
118 1.1 bouyer idesc->id_entryno = 0;
119 1.3 bouyer idesc->id_filesize = fs2h32(dp->e2di_size);
120 1.3 bouyer mode = fs2h16(dp->e2di_mode) & IFMT;
121 1.6 bouyer if (mode == IFBLK || mode == IFCHR || mode == IFIFO ||
122 1.6 bouyer (mode == IFLNK && (fs2h32(dp->e2di_size) < EXT2_MAXSYMLINKLEN)))
123 1.1 bouyer return (KEEPON);
124 1.1 bouyer dino = *dp;
125 1.3 bouyer ndb = howmany(fs2h32(dino.e2di_size), sblock.e2fs_bsize);
126 1.1 bouyer for (ap = &dino.e2di_blocks[0]; ap < &dino.e2di_blocks[NDADDR];
127 1.1 bouyer ap++,ndb--) {
128 1.1 bouyer idesc->id_numfrags = 1;
129 1.1 bouyer if (*ap == 0) {
130 1.1 bouyer if (idesc->id_type == DATA && ndb > 0) {
131 1.1 bouyer /* An empty block in a directory XXX */
132 1.10 itojun getpathname(pathbuf, sizeof(pathbuf),
133 1.10 itojun idesc->id_number, idesc->id_number);
134 1.1 bouyer pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
135 1.1 bouyer pathbuf);
136 1.1 bouyer if (reply("ADJUST LENGTH") == 1) {
137 1.1 bouyer dp = ginode(idesc->id_number);
138 1.3 bouyer dp->e2di_size = h2fs32((ap - &dino.e2di_blocks[0]) *
139 1.3 bouyer sblock.e2fs_bsize);
140 1.1 bouyer printf(
141 1.1 bouyer "YOU MUST RERUN FSCK AFTERWARDS\n");
142 1.1 bouyer rerun = 1;
143 1.1 bouyer inodirty();
144 1.1 bouyer }
145 1.1 bouyer }
146 1.1 bouyer continue;
147 1.1 bouyer }
148 1.3 bouyer idesc->id_blkno = fs2h32(*ap);
149 1.1 bouyer if (idesc->id_type == ADDR)
150 1.1 bouyer ret = (*idesc->id_func)(idesc);
151 1.1 bouyer else
152 1.1 bouyer ret = dirscan(idesc);
153 1.1 bouyer if (ret & STOP)
154 1.1 bouyer return (ret);
155 1.1 bouyer }
156 1.1 bouyer idesc->id_numfrags = 1;
157 1.3 bouyer remsize = fs2h32(dino.e2di_size) - sblock.e2fs_bsize * NDADDR;
158 1.1 bouyer sizepb = sblock.e2fs_bsize;
159 1.1 bouyer for (ap = &dino.e2di_blocks[NDADDR], n = 1; n <= NIADDR; ap++, n++) {
160 1.1 bouyer if (*ap) {
161 1.3 bouyer idesc->id_blkno = fs2h32(*ap);
162 1.1 bouyer ret = iblock(idesc, n, remsize);
163 1.1 bouyer if (ret & STOP)
164 1.1 bouyer return (ret);
165 1.1 bouyer } else {
166 1.1 bouyer if (idesc->id_type == DATA && remsize > 0) {
167 1.1 bouyer /* An empty block in a directory XXX */
168 1.10 itojun getpathname(pathbuf, sizeof(pathbuf),
169 1.10 itojun idesc->id_number, idesc->id_number);
170 1.1 bouyer pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
171 1.1 bouyer pathbuf);
172 1.1 bouyer if (reply("ADJUST LENGTH") == 1) {
173 1.1 bouyer dp = ginode(idesc->id_number);
174 1.3 bouyer dp->e2di_size = h2fs32(fs2h32(dp->e2di_size) - remsize);
175 1.1 bouyer remsize = 0;
176 1.1 bouyer printf(
177 1.1 bouyer "YOU MUST RERUN FSCK AFTERWARDS\n");
178 1.1 bouyer rerun = 1;
179 1.1 bouyer inodirty();
180 1.1 bouyer break;
181 1.1 bouyer }
182 1.1 bouyer }
183 1.1 bouyer }
184 1.1 bouyer sizepb *= NINDIR(&sblock);
185 1.1 bouyer remsize -= sizepb;
186 1.1 bouyer }
187 1.1 bouyer return (KEEPON);
188 1.1 bouyer }
189 1.1 bouyer
190 1.1 bouyer static int
191 1.1 bouyer iblock(idesc, ilevel, isize)
192 1.1 bouyer struct inodesc *idesc;
193 1.1 bouyer long ilevel;
194 1.1 bouyer u_int64_t isize;
195 1.1 bouyer {
196 1.9 fvdl /* XXX ondisk32 */
197 1.9 fvdl int32_t *ap;
198 1.9 fvdl int32_t *aplim;
199 1.2 lukem struct bufarea *bp;
200 1.1 bouyer int i, n, (*func) __P((struct inodesc *)), nif;
201 1.1 bouyer u_int64_t sizepb;
202 1.1 bouyer char buf[BUFSIZ];
203 1.1 bouyer char pathbuf[MAXPATHLEN + 1];
204 1.1 bouyer struct ext2fs_dinode *dp;
205 1.1 bouyer
206 1.1 bouyer if (idesc->id_type == ADDR) {
207 1.1 bouyer func = idesc->id_func;
208 1.1 bouyer if (((n = (*func)(idesc)) & KEEPON) == 0)
209 1.1 bouyer return (n);
210 1.1 bouyer } else
211 1.1 bouyer func = dirscan;
212 1.1 bouyer if (chkrange(idesc->id_blkno, idesc->id_numfrags))
213 1.1 bouyer return (SKIP);
214 1.1 bouyer bp = getdatablk(idesc->id_blkno, sblock.e2fs_bsize);
215 1.1 bouyer ilevel--;
216 1.1 bouyer for (sizepb = sblock.e2fs_bsize, i = 0; i < ilevel; i++)
217 1.1 bouyer sizepb *= NINDIR(&sblock);
218 1.1 bouyer if (isize > sizepb * NINDIR(&sblock))
219 1.1 bouyer nif = NINDIR(&sblock);
220 1.1 bouyer else
221 1.1 bouyer nif = howmany(isize, sizepb);
222 1.1 bouyer if (idesc->id_func == pass1check &&
223 1.1 bouyer nif < NINDIR(&sblock)) {
224 1.1 bouyer aplim = &bp->b_un.b_indir[NINDIR(&sblock)];
225 1.1 bouyer for (ap = &bp->b_un.b_indir[nif]; ap < aplim; ap++) {
226 1.1 bouyer if (*ap == 0)
227 1.1 bouyer continue;
228 1.5 mycroft (void)snprintf(buf, sizeof(buf),
229 1.5 mycroft "PARTIALLY TRUNCATED INODE I=%u", idesc->id_number);
230 1.1 bouyer if (dofix(idesc, buf)) {
231 1.1 bouyer *ap = 0;
232 1.1 bouyer dirty(bp);
233 1.1 bouyer }
234 1.1 bouyer }
235 1.1 bouyer flush(fswritefd, bp);
236 1.1 bouyer }
237 1.1 bouyer aplim = &bp->b_un.b_indir[nif];
238 1.1 bouyer for (ap = bp->b_un.b_indir; ap < aplim; ap++) {
239 1.1 bouyer if (*ap) {
240 1.3 bouyer idesc->id_blkno = fs2h32(*ap);
241 1.1 bouyer if (ilevel == 0)
242 1.1 bouyer n = (*func)(idesc);
243 1.1 bouyer else
244 1.1 bouyer n = iblock(idesc, ilevel, isize);
245 1.1 bouyer if (n & STOP) {
246 1.1 bouyer bp->b_flags &= ~B_INUSE;
247 1.1 bouyer return (n);
248 1.1 bouyer }
249 1.1 bouyer } else {
250 1.1 bouyer if (idesc->id_type == DATA && isize > 0) {
251 1.1 bouyer /* An empty block in a directory XXX */
252 1.10 itojun getpathname(pathbuf, sizeof(pathbuf),
253 1.10 itojun idesc->id_number, idesc->id_number);
254 1.1 bouyer pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
255 1.1 bouyer pathbuf);
256 1.1 bouyer if (reply("ADJUST LENGTH") == 1) {
257 1.1 bouyer dp = ginode(idesc->id_number);
258 1.3 bouyer dp->e2di_size = h2fs32(fs2h32(dp->e2di_size) - isize);
259 1.1 bouyer isize = 0;
260 1.1 bouyer printf(
261 1.1 bouyer "YOU MUST RERUN FSCK AFTERWARDS\n");
262 1.1 bouyer rerun = 1;
263 1.1 bouyer inodirty();
264 1.1 bouyer bp->b_flags &= ~B_INUSE;
265 1.1 bouyer return(STOP);
266 1.1 bouyer }
267 1.1 bouyer }
268 1.1 bouyer }
269 1.1 bouyer isize -= sizepb;
270 1.1 bouyer }
271 1.1 bouyer bp->b_flags &= ~B_INUSE;
272 1.1 bouyer return (KEEPON);
273 1.1 bouyer }
274 1.1 bouyer
275 1.1 bouyer /*
276 1.1 bouyer * Check that a block in a legal block number.
277 1.1 bouyer * Return 0 if in range, 1 if out of range.
278 1.1 bouyer */
279 1.1 bouyer int
280 1.1 bouyer chkrange(blk, cnt)
281 1.1 bouyer daddr_t blk;
282 1.1 bouyer int cnt;
283 1.1 bouyer {
284 1.8 bouyer int c, overh;
285 1.1 bouyer
286 1.1 bouyer if ((unsigned)(blk + cnt) > maxfsblock)
287 1.1 bouyer return (1);
288 1.1 bouyer c = dtog(&sblock, blk);
289 1.8 bouyer overh = cgoverhead(c);
290 1.8 bouyer if (blk < sblock.e2fs.e2fs_bpg * c + overh +
291 1.8 bouyer sblock.e2fs.e2fs_first_dblock) {
292 1.8 bouyer if ((blk + cnt) > sblock.e2fs.e2fs_bpg * c + overh +
293 1.8 bouyer sblock.e2fs.e2fs_first_dblock) {
294 1.1 bouyer if (debug) {
295 1.9 fvdl printf("blk %lld < cgdmin %d;",
296 1.9 fvdl (long long)blk,
297 1.9 fvdl sblock.e2fs.e2fs_bpg * c + overh +
298 1.8 bouyer sblock.e2fs.e2fs_first_dblock);
299 1.9 fvdl printf(" blk + cnt %lld > cgsbase %d\n",
300 1.9 fvdl (long long)(blk + cnt),
301 1.9 fvdl sblock.e2fs.e2fs_bpg * c +
302 1.8 bouyer overh + sblock.e2fs.e2fs_first_dblock);
303 1.1 bouyer }
304 1.1 bouyer return (1);
305 1.1 bouyer }
306 1.1 bouyer } else {
307 1.8 bouyer if ((blk + cnt) > sblock.e2fs.e2fs_bpg * (c + 1) + overh +
308 1.8 bouyer sblock.e2fs.e2fs_first_dblock) {
309 1.1 bouyer if (debug) {
310 1.9 fvdl printf("blk %lld >= cgdmin %d;",
311 1.9 fvdl (long long)blk,
312 1.9 fvdl sblock.e2fs.e2fs_bpg * c + overh +
313 1.8 bouyer sblock.e2fs.e2fs_first_dblock);
314 1.9 fvdl printf(" blk + cnt %lld > cgdmax %d\n",
315 1.9 fvdl (long long)(blk+cnt),
316 1.9 fvdl sblock.e2fs.e2fs_bpg * (c + 1) +
317 1.8 bouyer overh + sblock.e2fs.e2fs_first_dblock);
318 1.1 bouyer }
319 1.1 bouyer return (1);
320 1.1 bouyer }
321 1.1 bouyer }
322 1.1 bouyer return (0);
323 1.1 bouyer }
324 1.1 bouyer
325 1.1 bouyer /*
326 1.1 bouyer * General purpose interface for reading inodes.
327 1.1 bouyer */
328 1.1 bouyer struct ext2fs_dinode *
329 1.1 bouyer ginode(inumber)
330 1.1 bouyer ino_t inumber;
331 1.1 bouyer {
332 1.1 bouyer daddr_t iblk;
333 1.1 bouyer
334 1.1 bouyer if ((inumber < EXT2_FIRSTINO && inumber != EXT2_ROOTINO)
335 1.1 bouyer || inumber > maxino)
336 1.1 bouyer errexit("bad inode number %d to ginode\n", inumber);
337 1.1 bouyer if (startinum == 0 ||
338 1.1 bouyer inumber < startinum || inumber >= startinum + sblock.e2fs_ipb) {
339 1.3 bouyer iblk = fsck_ino_to_fsba(&sblock, inumber);
340 1.1 bouyer if (pbp != 0)
341 1.1 bouyer pbp->b_flags &= ~B_INUSE;
342 1.1 bouyer pbp = getdatablk(iblk, sblock.e2fs_bsize);
343 1.1 bouyer startinum = ((inumber -1) / sblock.e2fs_ipb) * sblock.e2fs_ipb + 1;
344 1.1 bouyer }
345 1.1 bouyer return (&pbp->b_un.b_dinode[(inumber-1) % sblock.e2fs_ipb]);
346 1.1 bouyer }
347 1.1 bouyer
348 1.1 bouyer /*
349 1.1 bouyer * Special purpose version of ginode used to optimize first pass
350 1.1 bouyer * over all the inodes in numerical order.
351 1.1 bouyer */
352 1.1 bouyer ino_t nextino, lastinum;
353 1.1 bouyer long readcnt, readpercg, fullcnt, inobufsize, partialcnt, partialsize;
354 1.1 bouyer struct ext2fs_dinode *inodebuf;
355 1.1 bouyer
356 1.1 bouyer struct ext2fs_dinode *
357 1.1 bouyer getnextinode(inumber)
358 1.1 bouyer ino_t inumber;
359 1.1 bouyer {
360 1.1 bouyer long size;
361 1.1 bouyer daddr_t dblk;
362 1.1 bouyer static struct ext2fs_dinode *dp;
363 1.1 bouyer
364 1.1 bouyer if (inumber != nextino++ || inumber > maxino)
365 1.1 bouyer errexit("bad inode number %d to nextinode\n", inumber);
366 1.1 bouyer if (inumber >= lastinum) {
367 1.1 bouyer readcnt++;
368 1.3 bouyer dblk = fsbtodb(&sblock, fsck_ino_to_fsba(&sblock, lastinum));
369 1.1 bouyer if (readcnt % readpercg == 0) {
370 1.1 bouyer size = partialsize;
371 1.1 bouyer lastinum += partialcnt;
372 1.1 bouyer } else {
373 1.1 bouyer size = inobufsize;
374 1.1 bouyer lastinum += fullcnt;
375 1.1 bouyer }
376 1.1 bouyer (void)bread(fsreadfd, (char *)inodebuf, dblk, size);
377 1.1 bouyer dp = inodebuf;
378 1.1 bouyer }
379 1.1 bouyer return (dp++);
380 1.1 bouyer }
381 1.1 bouyer
382 1.1 bouyer void
383 1.1 bouyer resetinodebuf()
384 1.1 bouyer {
385 1.1 bouyer
386 1.1 bouyer startinum = 0;
387 1.1 bouyer nextino = 1;
388 1.1 bouyer lastinum = 1;
389 1.1 bouyer readcnt = 0;
390 1.1 bouyer inobufsize = blkroundup(&sblock, INOBUFSIZE);
391 1.1 bouyer fullcnt = inobufsize / sizeof(struct ext2fs_dinode);
392 1.1 bouyer readpercg = sblock.e2fs.e2fs_ipg / fullcnt;
393 1.1 bouyer partialcnt = sblock.e2fs.e2fs_ipg % fullcnt;
394 1.1 bouyer partialsize = partialcnt * sizeof(struct ext2fs_dinode);
395 1.1 bouyer if (partialcnt != 0) {
396 1.1 bouyer readpercg++;
397 1.1 bouyer } else {
398 1.1 bouyer partialcnt = fullcnt;
399 1.1 bouyer partialsize = inobufsize;
400 1.1 bouyer }
401 1.1 bouyer if (inodebuf == NULL &&
402 1.1 bouyer (inodebuf = (struct ext2fs_dinode *)malloc((unsigned)inobufsize)) ==
403 1.1 bouyer NULL)
404 1.1 bouyer errexit("Cannot allocate space for inode buffer\n");
405 1.1 bouyer while (nextino < EXT2_ROOTINO)
406 1.1 bouyer (void)getnextinode(nextino);
407 1.1 bouyer }
408 1.1 bouyer
409 1.1 bouyer void
410 1.1 bouyer freeinodebuf()
411 1.1 bouyer {
412 1.1 bouyer
413 1.1 bouyer if (inodebuf != NULL)
414 1.1 bouyer free((char *)inodebuf);
415 1.1 bouyer inodebuf = NULL;
416 1.1 bouyer }
417 1.1 bouyer
418 1.1 bouyer /*
419 1.1 bouyer * Routines to maintain information about directory inodes.
420 1.1 bouyer * This is built during the first pass and used during the
421 1.1 bouyer * second and third passes.
422 1.1 bouyer *
423 1.1 bouyer * Enter inodes into the cache.
424 1.1 bouyer */
425 1.1 bouyer void
426 1.1 bouyer cacheino(dp, inumber)
427 1.2 lukem struct ext2fs_dinode *dp;
428 1.1 bouyer ino_t inumber;
429 1.1 bouyer {
430 1.2 lukem struct inoinfo *inp;
431 1.1 bouyer struct inoinfo **inpp;
432 1.1 bouyer unsigned int blks;
433 1.1 bouyer
434 1.3 bouyer blks = howmany(fs2h32(dp->e2di_size), sblock.e2fs_bsize);
435 1.1 bouyer if (blks > NDADDR)
436 1.1 bouyer blks = NDADDR + NIADDR;
437 1.9 fvdl /* XXX ondisk32 */
438 1.1 bouyer inp = (struct inoinfo *)
439 1.9 fvdl malloc(sizeof(*inp) + (blks - 1) * sizeof(int32_t));
440 1.1 bouyer if (inp == NULL)
441 1.1 bouyer return;
442 1.1 bouyer inpp = &inphead[inumber % numdirs];
443 1.1 bouyer inp->i_nexthash = *inpp;
444 1.1 bouyer *inpp = inp;
445 1.1 bouyer inp->i_child = inp->i_sibling = inp->i_parentp = 0;
446 1.1 bouyer if (inumber == EXT2_ROOTINO)
447 1.1 bouyer inp->i_parent = EXT2_ROOTINO;
448 1.1 bouyer else
449 1.1 bouyer inp->i_parent = (ino_t)0;
450 1.1 bouyer inp->i_dotdot = (ino_t)0;
451 1.1 bouyer inp->i_number = inumber;
452 1.3 bouyer inp->i_isize = fs2h32(dp->e2di_size);
453 1.9 fvdl /* XXX ondisk32 */
454 1.9 fvdl inp->i_numblks = blks * sizeof(int32_t);
455 1.1 bouyer memcpy(&inp->i_blks[0], &dp->e2di_blocks[0], (size_t)inp->i_numblks);
456 1.1 bouyer if (inplast == listmax) {
457 1.1 bouyer listmax += 100;
458 1.1 bouyer inpsort = (struct inoinfo **)realloc((char *)inpsort,
459 1.1 bouyer (unsigned)listmax * sizeof(struct inoinfo *));
460 1.1 bouyer if (inpsort == NULL)
461 1.1 bouyer errexit("cannot increase directory list\n");
462 1.1 bouyer }
463 1.1 bouyer inpsort[inplast++] = inp;
464 1.1 bouyer }
465 1.1 bouyer
466 1.1 bouyer /*
467 1.1 bouyer * Look up an inode cache structure.
468 1.1 bouyer */
469 1.1 bouyer struct inoinfo *
470 1.1 bouyer getinoinfo(inumber)
471 1.1 bouyer ino_t inumber;
472 1.1 bouyer {
473 1.2 lukem struct inoinfo *inp;
474 1.1 bouyer
475 1.1 bouyer for (inp = inphead[inumber % numdirs]; inp; inp = inp->i_nexthash) {
476 1.1 bouyer if (inp->i_number != inumber)
477 1.1 bouyer continue;
478 1.1 bouyer return (inp);
479 1.1 bouyer }
480 1.1 bouyer errexit("cannot find inode %d\n", inumber);
481 1.1 bouyer return ((struct inoinfo *)0);
482 1.1 bouyer }
483 1.1 bouyer
484 1.1 bouyer /*
485 1.1 bouyer * Clean up all the inode cache structure.
486 1.1 bouyer */
487 1.1 bouyer void
488 1.1 bouyer inocleanup()
489 1.1 bouyer {
490 1.2 lukem struct inoinfo **inpp;
491 1.1 bouyer
492 1.1 bouyer if (inphead == NULL)
493 1.1 bouyer return;
494 1.1 bouyer for (inpp = &inpsort[inplast - 1]; inpp >= inpsort; inpp--)
495 1.1 bouyer free((char *)(*inpp));
496 1.1 bouyer free((char *)inphead);
497 1.1 bouyer free((char *)inpsort);
498 1.1 bouyer inphead = inpsort = NULL;
499 1.1 bouyer }
500 1.1 bouyer
501 1.1 bouyer void
502 1.1 bouyer inodirty()
503 1.1 bouyer {
504 1.1 bouyer
505 1.1 bouyer dirty(pbp);
506 1.1 bouyer }
507 1.1 bouyer
508 1.1 bouyer void
509 1.1 bouyer clri(idesc, type, flag)
510 1.2 lukem struct inodesc *idesc;
511 1.1 bouyer char *type;
512 1.1 bouyer int flag;
513 1.1 bouyer {
514 1.2 lukem struct ext2fs_dinode *dp;
515 1.1 bouyer
516 1.1 bouyer dp = ginode(idesc->id_number);
517 1.1 bouyer if (flag == 1) {
518 1.1 bouyer pwarn("%s %s", type,
519 1.1 bouyer (dp->e2di_mode & IFMT) == IFDIR ? "DIR" : "FILE");
520 1.1 bouyer pinode(idesc->id_number);
521 1.1 bouyer }
522 1.1 bouyer if (preen || reply("CLEAR") == 1) {
523 1.1 bouyer if (preen)
524 1.1 bouyer printf(" (CLEARED)\n");
525 1.1 bouyer n_files--;
526 1.1 bouyer (void)ckinode(dp, idesc);
527 1.1 bouyer clearinode(dp);
528 1.1 bouyer statemap[idesc->id_number] = USTATE;
529 1.1 bouyer inodirty();
530 1.1 bouyer }
531 1.1 bouyer }
532 1.1 bouyer
533 1.1 bouyer int
534 1.1 bouyer findname(idesc)
535 1.1 bouyer struct inodesc *idesc;
536 1.1 bouyer {
537 1.2 lukem struct ext2fs_direct *dirp = idesc->id_dirp;
538 1.7 bouyer u_int16_t namlen = dirp->e2d_namlen;
539 1.1 bouyer
540 1.3 bouyer if (fs2h32(dirp->e2d_ino) != idesc->id_parent)
541 1.1 bouyer return (KEEPON);
542 1.3 bouyer memcpy(idesc->id_name, dirp->e2d_name, (size_t)namlen);
543 1.3 bouyer idesc->id_name[namlen] = '\0';
544 1.1 bouyer return (STOP|FOUND);
545 1.1 bouyer }
546 1.1 bouyer
547 1.1 bouyer int
548 1.1 bouyer findino(idesc)
549 1.1 bouyer struct inodesc *idesc;
550 1.1 bouyer {
551 1.2 lukem struct ext2fs_direct *dirp = idesc->id_dirp;
552 1.3 bouyer u_int32_t ino = fs2h32(dirp->e2d_ino);
553 1.1 bouyer
554 1.3 bouyer if (ino == 0)
555 1.1 bouyer return (KEEPON);
556 1.1 bouyer if (strcmp(dirp->e2d_name, idesc->id_name) == 0 &&
557 1.3 bouyer (ino == EXT2_ROOTINO || ino >= EXT2_FIRSTINO)
558 1.3 bouyer && ino <= maxino) {
559 1.3 bouyer idesc->id_parent = ino;
560 1.1 bouyer return (STOP|FOUND);
561 1.1 bouyer }
562 1.1 bouyer return (KEEPON);
563 1.1 bouyer }
564 1.1 bouyer
565 1.1 bouyer void
566 1.1 bouyer pinode(ino)
567 1.1 bouyer ino_t ino;
568 1.1 bouyer {
569 1.2 lukem struct ext2fs_dinode *dp;
570 1.2 lukem char *p;
571 1.1 bouyer struct passwd *pw;
572 1.1 bouyer time_t t;
573 1.1 bouyer
574 1.1 bouyer printf(" I=%u ", ino);
575 1.1 bouyer if ((ino < EXT2_FIRSTINO && ino != EXT2_ROOTINO) || ino > maxino)
576 1.1 bouyer return;
577 1.1 bouyer dp = ginode(ino);
578 1.1 bouyer printf(" OWNER=");
579 1.1 bouyer #ifndef SMALL
580 1.1 bouyer if ((pw = getpwuid((int)dp->e2di_uid)) != 0)
581 1.1 bouyer printf("%s ", pw->pw_name);
582 1.1 bouyer else
583 1.1 bouyer #endif
584 1.3 bouyer printf("%u ", (unsigned)fs2h16(dp->e2di_uid));
585 1.3 bouyer printf("MODE=%o\n", fs2h16(dp->e2di_mode));
586 1.1 bouyer if (preen)
587 1.1 bouyer printf("%s: ", cdevname());
588 1.3 bouyer printf("SIZE=%u ", fs2h32(dp->e2di_size));
589 1.3 bouyer t = fs2h32(dp->e2di_mtime);
590 1.1 bouyer p = ctime(&t);
591 1.1 bouyer printf("MTIME=%12.12s %4.4s ", &p[4], &p[20]);
592 1.1 bouyer }
593 1.1 bouyer
594 1.1 bouyer void
595 1.1 bouyer blkerror(ino, type, blk)
596 1.1 bouyer ino_t ino;
597 1.1 bouyer char *type;
598 1.1 bouyer daddr_t blk;
599 1.1 bouyer {
600 1.1 bouyer
601 1.9 fvdl pfatal("%lld %s I=%u", (long long)blk, type, ino);
602 1.1 bouyer printf("\n");
603 1.1 bouyer switch (statemap[ino]) {
604 1.1 bouyer
605 1.1 bouyer case FSTATE:
606 1.1 bouyer statemap[ino] = FCLEAR;
607 1.1 bouyer return;
608 1.1 bouyer
609 1.1 bouyer case DSTATE:
610 1.1 bouyer statemap[ino] = DCLEAR;
611 1.1 bouyer return;
612 1.1 bouyer
613 1.1 bouyer case FCLEAR:
614 1.1 bouyer case DCLEAR:
615 1.1 bouyer return;
616 1.1 bouyer
617 1.1 bouyer default:
618 1.1 bouyer errexit("BAD STATE %d TO BLKERR\n", statemap[ino]);
619 1.1 bouyer /* NOTREACHED */
620 1.1 bouyer }
621 1.1 bouyer }
622 1.1 bouyer
623 1.1 bouyer /*
624 1.1 bouyer * allocate an unused inode
625 1.1 bouyer */
626 1.1 bouyer ino_t
627 1.1 bouyer allocino(request, type)
628 1.1 bouyer ino_t request;
629 1.1 bouyer int type;
630 1.1 bouyer {
631 1.2 lukem ino_t ino;
632 1.2 lukem struct ext2fs_dinode *dp;
633 1.1 bouyer time_t t;
634 1.1 bouyer
635 1.1 bouyer if (request == 0)
636 1.1 bouyer request = EXT2_ROOTINO;
637 1.1 bouyer else if (statemap[request] != USTATE)
638 1.1 bouyer return (0);
639 1.1 bouyer for (ino = request; ino < maxino; ino++) {
640 1.1 bouyer if ((ino > EXT2_ROOTINO) && (ino < EXT2_FIRSTINO))
641 1.1 bouyer continue;
642 1.1 bouyer if (statemap[ino] == USTATE)
643 1.1 bouyer break;
644 1.1 bouyer }
645 1.1 bouyer if (ino == maxino)
646 1.1 bouyer return (0);
647 1.1 bouyer switch (type & IFMT) {
648 1.1 bouyer case IFDIR:
649 1.1 bouyer statemap[ino] = DSTATE;
650 1.1 bouyer break;
651 1.1 bouyer case IFREG:
652 1.1 bouyer case IFLNK:
653 1.1 bouyer statemap[ino] = FSTATE;
654 1.1 bouyer break;
655 1.1 bouyer default:
656 1.1 bouyer return (0);
657 1.1 bouyer }
658 1.1 bouyer dp = ginode(ino);
659 1.3 bouyer dp->e2di_blocks[0] = h2fs32(allocblk());
660 1.1 bouyer if (dp->e2di_blocks[0] == 0) {
661 1.1 bouyer statemap[ino] = USTATE;
662 1.1 bouyer return (0);
663 1.1 bouyer }
664 1.3 bouyer dp->e2di_mode = h2fs16(type);
665 1.1 bouyer (void)time(&t);
666 1.3 bouyer dp->e2di_atime = h2fs32(t);
667 1.1 bouyer dp->e2di_mtime = dp->e2di_ctime = dp->e2di_atime;
668 1.1 bouyer dp->e2di_dtime = 0;
669 1.3 bouyer dp->e2di_size = h2fs32(sblock.e2fs_bsize);
670 1.3 bouyer dp->e2di_nblock = h2fs32(btodb(sblock.e2fs_bsize));
671 1.1 bouyer n_files++;
672 1.1 bouyer inodirty();
673 1.8 bouyer typemap[ino] = E2IFTODT(type);
674 1.1 bouyer return (ino);
675 1.1 bouyer }
676 1.1 bouyer
677 1.1 bouyer /*
678 1.1 bouyer * deallocate an inode
679 1.1 bouyer */
680 1.1 bouyer void
681 1.1 bouyer freeino(ino)
682 1.1 bouyer ino_t ino;
683 1.1 bouyer {
684 1.1 bouyer struct inodesc idesc;
685 1.1 bouyer struct ext2fs_dinode *dp;
686 1.1 bouyer
687 1.1 bouyer memset(&idesc, 0, sizeof(struct inodesc));
688 1.1 bouyer idesc.id_type = ADDR;
689 1.1 bouyer idesc.id_func = pass4check;
690 1.1 bouyer idesc.id_number = ino;
691 1.1 bouyer dp = ginode(ino);
692 1.1 bouyer (void)ckinode(dp, &idesc);
693 1.1 bouyer clearinode(dp);
694 1.1 bouyer inodirty();
695 1.1 bouyer statemap[ino] = USTATE;
696 1.1 bouyer n_files--;
697 1.1 bouyer }
698