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