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