inode.c revision 1.4 1 1.4 kleink /* $NetBSD: inode.c,v 1.4 1998/04/01 15:22:57 kleink 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.4 kleink __RCSID("$NetBSD: inode.c,v 1.4 1998/04/01 15:22:57 kleink 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.1 bouyer if (mode == IFBLK || mode == IFCHR || (mode == IFLNK &&
97 1.3 bouyer (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.1 bouyer (void)sprintf(buf, "PARTIALLY TRUNCATED INODE I=%u",
203 1.1 bouyer 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.2 lukem int c;
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.1 bouyer if (blk < sblock.e2fs.e2fs_bpg * c + cgoverhead +
264 1.1 bouyer sblock.e2fs.e2fs_first_dblock) {
265 1.1 bouyer if ((blk + cnt) > sblock.e2fs.e2fs_bpg * c + cgoverhead +
266 1.1 bouyer sblock.e2fs.e2fs_first_dblock) {
267 1.1 bouyer if (debug) {
268 1.1 bouyer printf("blk %d < cgdmin %d;",
269 1.1 bouyer blk, sblock.e2fs.e2fs_bpg * c + cgoverhead +
270 1.1 bouyer sblock.e2fs.e2fs_first_dblock);
271 1.1 bouyer printf(" blk + cnt %d > cgsbase %d\n",
272 1.1 bouyer blk + cnt, sblock.e2fs.e2fs_bpg * c + cgoverhead +
273 1.1 bouyer sblock.e2fs.e2fs_first_dblock);
274 1.1 bouyer }
275 1.1 bouyer return (1);
276 1.1 bouyer }
277 1.1 bouyer } else {
278 1.1 bouyer if ((blk + cnt) > sblock.e2fs.e2fs_bpg * (c + 1) + cgoverhead +
279 1.1 bouyer sblock.e2fs.e2fs_first_dblock) {
280 1.1 bouyer if (debug) {
281 1.1 bouyer printf("blk %d >= cgdmin %d;",
282 1.1 bouyer blk, sblock.e2fs.e2fs_bpg * c + cgoverhead +
283 1.1 bouyer sblock.e2fs.e2fs_first_dblock);
284 1.1 bouyer printf(" blk + cnt %d > cgdmax %d\n",
285 1.1 bouyer blk+cnt, sblock.e2fs.e2fs_bpg * (c + 1) + cgoverhead +
286 1.1 bouyer sblock.e2fs.e2fs_first_dblock);
287 1.1 bouyer }
288 1.1 bouyer return (1);
289 1.1 bouyer }
290 1.1 bouyer }
291 1.1 bouyer return (0);
292 1.1 bouyer }
293 1.1 bouyer
294 1.1 bouyer /*
295 1.1 bouyer * General purpose interface for reading inodes.
296 1.1 bouyer */
297 1.1 bouyer struct ext2fs_dinode *
298 1.1 bouyer ginode(inumber)
299 1.1 bouyer ino_t inumber;
300 1.1 bouyer {
301 1.1 bouyer daddr_t iblk;
302 1.1 bouyer
303 1.1 bouyer if ((inumber < EXT2_FIRSTINO && inumber != EXT2_ROOTINO)
304 1.1 bouyer || inumber > maxino)
305 1.1 bouyer errexit("bad inode number %d to ginode\n", inumber);
306 1.1 bouyer if (startinum == 0 ||
307 1.1 bouyer inumber < startinum || inumber >= startinum + sblock.e2fs_ipb) {
308 1.3 bouyer iblk = fsck_ino_to_fsba(&sblock, inumber);
309 1.1 bouyer if (pbp != 0)
310 1.1 bouyer pbp->b_flags &= ~B_INUSE;
311 1.1 bouyer pbp = getdatablk(iblk, sblock.e2fs_bsize);
312 1.1 bouyer startinum = ((inumber -1) / sblock.e2fs_ipb) * sblock.e2fs_ipb + 1;
313 1.1 bouyer }
314 1.1 bouyer return (&pbp->b_un.b_dinode[(inumber-1) % sblock.e2fs_ipb]);
315 1.1 bouyer }
316 1.1 bouyer
317 1.1 bouyer /*
318 1.1 bouyer * Special purpose version of ginode used to optimize first pass
319 1.1 bouyer * over all the inodes in numerical order.
320 1.1 bouyer */
321 1.1 bouyer ino_t nextino, lastinum;
322 1.1 bouyer long readcnt, readpercg, fullcnt, inobufsize, partialcnt, partialsize;
323 1.1 bouyer struct ext2fs_dinode *inodebuf;
324 1.1 bouyer
325 1.1 bouyer struct ext2fs_dinode *
326 1.1 bouyer getnextinode(inumber)
327 1.1 bouyer ino_t inumber;
328 1.1 bouyer {
329 1.1 bouyer long size;
330 1.1 bouyer daddr_t dblk;
331 1.1 bouyer static struct ext2fs_dinode *dp;
332 1.1 bouyer
333 1.1 bouyer if (inumber != nextino++ || inumber > maxino)
334 1.1 bouyer errexit("bad inode number %d to nextinode\n", inumber);
335 1.1 bouyer if (inumber >= lastinum) {
336 1.1 bouyer readcnt++;
337 1.3 bouyer dblk = fsbtodb(&sblock, fsck_ino_to_fsba(&sblock, lastinum));
338 1.1 bouyer if (readcnt % readpercg == 0) {
339 1.1 bouyer size = partialsize;
340 1.1 bouyer lastinum += partialcnt;
341 1.1 bouyer } else {
342 1.1 bouyer size = inobufsize;
343 1.1 bouyer lastinum += fullcnt;
344 1.1 bouyer }
345 1.1 bouyer (void)bread(fsreadfd, (char *)inodebuf, dblk, size);
346 1.1 bouyer dp = inodebuf;
347 1.1 bouyer }
348 1.1 bouyer return (dp++);
349 1.1 bouyer }
350 1.1 bouyer
351 1.1 bouyer void
352 1.1 bouyer resetinodebuf()
353 1.1 bouyer {
354 1.1 bouyer
355 1.1 bouyer startinum = 0;
356 1.1 bouyer nextino = 1;
357 1.1 bouyer lastinum = 1;
358 1.1 bouyer readcnt = 0;
359 1.1 bouyer inobufsize = blkroundup(&sblock, INOBUFSIZE);
360 1.1 bouyer fullcnt = inobufsize / sizeof(struct ext2fs_dinode);
361 1.1 bouyer readpercg = sblock.e2fs.e2fs_ipg / fullcnt;
362 1.1 bouyer partialcnt = sblock.e2fs.e2fs_ipg % fullcnt;
363 1.1 bouyer partialsize = partialcnt * sizeof(struct ext2fs_dinode);
364 1.1 bouyer if (partialcnt != 0) {
365 1.1 bouyer readpercg++;
366 1.1 bouyer } else {
367 1.1 bouyer partialcnt = fullcnt;
368 1.1 bouyer partialsize = inobufsize;
369 1.1 bouyer }
370 1.1 bouyer if (inodebuf == NULL &&
371 1.1 bouyer (inodebuf = (struct ext2fs_dinode *)malloc((unsigned)inobufsize)) ==
372 1.1 bouyer NULL)
373 1.1 bouyer errexit("Cannot allocate space for inode buffer\n");
374 1.1 bouyer while (nextino < EXT2_ROOTINO)
375 1.1 bouyer (void)getnextinode(nextino);
376 1.1 bouyer }
377 1.1 bouyer
378 1.1 bouyer void
379 1.1 bouyer freeinodebuf()
380 1.1 bouyer {
381 1.1 bouyer
382 1.1 bouyer if (inodebuf != NULL)
383 1.1 bouyer free((char *)inodebuf);
384 1.1 bouyer inodebuf = NULL;
385 1.1 bouyer }
386 1.1 bouyer
387 1.1 bouyer /*
388 1.1 bouyer * Routines to maintain information about directory inodes.
389 1.1 bouyer * This is built during the first pass and used during the
390 1.1 bouyer * second and third passes.
391 1.1 bouyer *
392 1.1 bouyer * Enter inodes into the cache.
393 1.1 bouyer */
394 1.1 bouyer void
395 1.1 bouyer cacheino(dp, inumber)
396 1.2 lukem struct ext2fs_dinode *dp;
397 1.1 bouyer ino_t inumber;
398 1.1 bouyer {
399 1.2 lukem struct inoinfo *inp;
400 1.1 bouyer struct inoinfo **inpp;
401 1.1 bouyer unsigned int blks;
402 1.1 bouyer
403 1.3 bouyer blks = howmany(fs2h32(dp->e2di_size), sblock.e2fs_bsize);
404 1.1 bouyer if (blks > NDADDR)
405 1.1 bouyer blks = NDADDR + NIADDR;
406 1.1 bouyer inp = (struct inoinfo *)
407 1.1 bouyer malloc(sizeof(*inp) + (blks - 1) * sizeof(daddr_t));
408 1.1 bouyer if (inp == NULL)
409 1.1 bouyer return;
410 1.1 bouyer inpp = &inphead[inumber % numdirs];
411 1.1 bouyer inp->i_nexthash = *inpp;
412 1.1 bouyer *inpp = inp;
413 1.1 bouyer inp->i_child = inp->i_sibling = inp->i_parentp = 0;
414 1.1 bouyer if (inumber == EXT2_ROOTINO)
415 1.1 bouyer inp->i_parent = EXT2_ROOTINO;
416 1.1 bouyer else
417 1.1 bouyer inp->i_parent = (ino_t)0;
418 1.1 bouyer inp->i_dotdot = (ino_t)0;
419 1.1 bouyer inp->i_number = inumber;
420 1.3 bouyer inp->i_isize = fs2h32(dp->e2di_size);
421 1.1 bouyer inp->i_numblks = blks * sizeof(daddr_t);
422 1.1 bouyer memcpy(&inp->i_blks[0], &dp->e2di_blocks[0], (size_t)inp->i_numblks);
423 1.1 bouyer if (inplast == listmax) {
424 1.1 bouyer listmax += 100;
425 1.1 bouyer inpsort = (struct inoinfo **)realloc((char *)inpsort,
426 1.1 bouyer (unsigned)listmax * sizeof(struct inoinfo *));
427 1.1 bouyer if (inpsort == NULL)
428 1.1 bouyer errexit("cannot increase directory list\n");
429 1.1 bouyer }
430 1.1 bouyer inpsort[inplast++] = inp;
431 1.1 bouyer }
432 1.1 bouyer
433 1.1 bouyer /*
434 1.1 bouyer * Look up an inode cache structure.
435 1.1 bouyer */
436 1.1 bouyer struct inoinfo *
437 1.1 bouyer getinoinfo(inumber)
438 1.1 bouyer ino_t inumber;
439 1.1 bouyer {
440 1.2 lukem struct inoinfo *inp;
441 1.1 bouyer
442 1.1 bouyer for (inp = inphead[inumber % numdirs]; inp; inp = inp->i_nexthash) {
443 1.1 bouyer if (inp->i_number != inumber)
444 1.1 bouyer continue;
445 1.1 bouyer return (inp);
446 1.1 bouyer }
447 1.1 bouyer errexit("cannot find inode %d\n", inumber);
448 1.1 bouyer return ((struct inoinfo *)0);
449 1.1 bouyer }
450 1.1 bouyer
451 1.1 bouyer /*
452 1.1 bouyer * Clean up all the inode cache structure.
453 1.1 bouyer */
454 1.1 bouyer void
455 1.1 bouyer inocleanup()
456 1.1 bouyer {
457 1.2 lukem struct inoinfo **inpp;
458 1.1 bouyer
459 1.1 bouyer if (inphead == NULL)
460 1.1 bouyer return;
461 1.1 bouyer for (inpp = &inpsort[inplast - 1]; inpp >= inpsort; inpp--)
462 1.1 bouyer free((char *)(*inpp));
463 1.1 bouyer free((char *)inphead);
464 1.1 bouyer free((char *)inpsort);
465 1.1 bouyer inphead = inpsort = NULL;
466 1.1 bouyer }
467 1.1 bouyer
468 1.1 bouyer void
469 1.1 bouyer inodirty()
470 1.1 bouyer {
471 1.1 bouyer
472 1.1 bouyer dirty(pbp);
473 1.1 bouyer }
474 1.1 bouyer
475 1.1 bouyer void
476 1.1 bouyer clri(idesc, type, flag)
477 1.2 lukem struct inodesc *idesc;
478 1.1 bouyer char *type;
479 1.1 bouyer int flag;
480 1.1 bouyer {
481 1.2 lukem struct ext2fs_dinode *dp;
482 1.1 bouyer
483 1.1 bouyer dp = ginode(idesc->id_number);
484 1.1 bouyer if (flag == 1) {
485 1.1 bouyer pwarn("%s %s", type,
486 1.1 bouyer (dp->e2di_mode & IFMT) == IFDIR ? "DIR" : "FILE");
487 1.1 bouyer pinode(idesc->id_number);
488 1.1 bouyer }
489 1.1 bouyer if (preen || reply("CLEAR") == 1) {
490 1.1 bouyer if (preen)
491 1.1 bouyer printf(" (CLEARED)\n");
492 1.1 bouyer n_files--;
493 1.1 bouyer (void)ckinode(dp, idesc);
494 1.1 bouyer clearinode(dp);
495 1.1 bouyer statemap[idesc->id_number] = USTATE;
496 1.1 bouyer inodirty();
497 1.1 bouyer }
498 1.1 bouyer }
499 1.1 bouyer
500 1.1 bouyer int
501 1.1 bouyer findname(idesc)
502 1.1 bouyer struct inodesc *idesc;
503 1.1 bouyer {
504 1.2 lukem struct ext2fs_direct *dirp = idesc->id_dirp;
505 1.3 bouyer u_int16_t namlen = fs2h16(dirp->e2d_namlen);
506 1.1 bouyer
507 1.3 bouyer if (fs2h32(dirp->e2d_ino) != idesc->id_parent)
508 1.1 bouyer return (KEEPON);
509 1.3 bouyer memcpy(idesc->id_name, dirp->e2d_name, (size_t)namlen);
510 1.3 bouyer idesc->id_name[namlen] = '\0';
511 1.1 bouyer return (STOP|FOUND);
512 1.1 bouyer }
513 1.1 bouyer
514 1.1 bouyer int
515 1.1 bouyer findino(idesc)
516 1.1 bouyer struct inodesc *idesc;
517 1.1 bouyer {
518 1.2 lukem struct ext2fs_direct *dirp = idesc->id_dirp;
519 1.3 bouyer u_int32_t ino = fs2h32(dirp->e2d_ino);
520 1.1 bouyer
521 1.3 bouyer if (ino == 0)
522 1.1 bouyer return (KEEPON);
523 1.1 bouyer if (strcmp(dirp->e2d_name, idesc->id_name) == 0 &&
524 1.3 bouyer (ino == EXT2_ROOTINO || ino >= EXT2_FIRSTINO)
525 1.3 bouyer && ino <= maxino) {
526 1.3 bouyer idesc->id_parent = ino;
527 1.1 bouyer return (STOP|FOUND);
528 1.1 bouyer }
529 1.1 bouyer return (KEEPON);
530 1.1 bouyer }
531 1.1 bouyer
532 1.1 bouyer void
533 1.1 bouyer pinode(ino)
534 1.1 bouyer ino_t ino;
535 1.1 bouyer {
536 1.2 lukem struct ext2fs_dinode *dp;
537 1.2 lukem char *p;
538 1.1 bouyer struct passwd *pw;
539 1.1 bouyer time_t t;
540 1.1 bouyer
541 1.1 bouyer printf(" I=%u ", ino);
542 1.1 bouyer if ((ino < EXT2_FIRSTINO && ino != EXT2_ROOTINO) || ino > maxino)
543 1.1 bouyer return;
544 1.1 bouyer dp = ginode(ino);
545 1.1 bouyer printf(" OWNER=");
546 1.1 bouyer #ifndef SMALL
547 1.1 bouyer if ((pw = getpwuid((int)dp->e2di_uid)) != 0)
548 1.1 bouyer printf("%s ", pw->pw_name);
549 1.1 bouyer else
550 1.1 bouyer #endif
551 1.3 bouyer printf("%u ", (unsigned)fs2h16(dp->e2di_uid));
552 1.3 bouyer printf("MODE=%o\n", fs2h16(dp->e2di_mode));
553 1.1 bouyer if (preen)
554 1.1 bouyer printf("%s: ", cdevname());
555 1.3 bouyer printf("SIZE=%u ", fs2h32(dp->e2di_size));
556 1.3 bouyer t = fs2h32(dp->e2di_mtime);
557 1.1 bouyer p = ctime(&t);
558 1.1 bouyer printf("MTIME=%12.12s %4.4s ", &p[4], &p[20]);
559 1.1 bouyer }
560 1.1 bouyer
561 1.1 bouyer void
562 1.1 bouyer blkerror(ino, type, blk)
563 1.1 bouyer ino_t ino;
564 1.1 bouyer char *type;
565 1.1 bouyer daddr_t blk;
566 1.1 bouyer {
567 1.1 bouyer
568 1.1 bouyer pfatal("%d %s I=%u", blk, type, ino);
569 1.1 bouyer printf("\n");
570 1.1 bouyer switch (statemap[ino]) {
571 1.1 bouyer
572 1.1 bouyer case FSTATE:
573 1.1 bouyer statemap[ino] = FCLEAR;
574 1.1 bouyer return;
575 1.1 bouyer
576 1.1 bouyer case DSTATE:
577 1.1 bouyer statemap[ino] = DCLEAR;
578 1.1 bouyer return;
579 1.1 bouyer
580 1.1 bouyer case FCLEAR:
581 1.1 bouyer case DCLEAR:
582 1.1 bouyer return;
583 1.1 bouyer
584 1.1 bouyer default:
585 1.1 bouyer errexit("BAD STATE %d TO BLKERR\n", statemap[ino]);
586 1.1 bouyer /* NOTREACHED */
587 1.1 bouyer }
588 1.1 bouyer }
589 1.1 bouyer
590 1.1 bouyer /*
591 1.1 bouyer * allocate an unused inode
592 1.1 bouyer */
593 1.1 bouyer ino_t
594 1.1 bouyer allocino(request, type)
595 1.1 bouyer ino_t request;
596 1.1 bouyer int type;
597 1.1 bouyer {
598 1.2 lukem ino_t ino;
599 1.2 lukem struct ext2fs_dinode *dp;
600 1.1 bouyer time_t t;
601 1.1 bouyer
602 1.1 bouyer if (request == 0)
603 1.1 bouyer request = EXT2_ROOTINO;
604 1.1 bouyer else if (statemap[request] != USTATE)
605 1.1 bouyer return (0);
606 1.1 bouyer for (ino = request; ino < maxino; ino++) {
607 1.1 bouyer if ((ino > EXT2_ROOTINO) && (ino < EXT2_FIRSTINO))
608 1.1 bouyer continue;
609 1.1 bouyer if (statemap[ino] == USTATE)
610 1.1 bouyer break;
611 1.1 bouyer }
612 1.1 bouyer if (ino == maxino)
613 1.1 bouyer return (0);
614 1.1 bouyer switch (type & IFMT) {
615 1.1 bouyer case IFDIR:
616 1.1 bouyer statemap[ino] = DSTATE;
617 1.1 bouyer break;
618 1.1 bouyer case IFREG:
619 1.1 bouyer case IFLNK:
620 1.1 bouyer statemap[ino] = FSTATE;
621 1.1 bouyer break;
622 1.1 bouyer default:
623 1.1 bouyer return (0);
624 1.1 bouyer }
625 1.1 bouyer dp = ginode(ino);
626 1.3 bouyer dp->e2di_blocks[0] = h2fs32(allocblk());
627 1.1 bouyer if (dp->e2di_blocks[0] == 0) {
628 1.1 bouyer statemap[ino] = USTATE;
629 1.1 bouyer return (0);
630 1.1 bouyer }
631 1.3 bouyer dp->e2di_mode = h2fs16(type);
632 1.1 bouyer (void)time(&t);
633 1.3 bouyer dp->e2di_atime = h2fs32(t);
634 1.1 bouyer dp->e2di_mtime = dp->e2di_ctime = dp->e2di_atime;
635 1.1 bouyer dp->e2di_dtime = 0;
636 1.3 bouyer dp->e2di_size = h2fs32(sblock.e2fs_bsize);
637 1.3 bouyer dp->e2di_nblock = h2fs32(btodb(sblock.e2fs_bsize));
638 1.1 bouyer n_files++;
639 1.1 bouyer inodirty();
640 1.1 bouyer return (ino);
641 1.1 bouyer }
642 1.1 bouyer
643 1.1 bouyer /*
644 1.1 bouyer * deallocate an inode
645 1.1 bouyer */
646 1.1 bouyer void
647 1.1 bouyer freeino(ino)
648 1.1 bouyer ino_t ino;
649 1.1 bouyer {
650 1.1 bouyer struct inodesc idesc;
651 1.1 bouyer struct ext2fs_dinode *dp;
652 1.1 bouyer
653 1.1 bouyer memset(&idesc, 0, sizeof(struct inodesc));
654 1.1 bouyer idesc.id_type = ADDR;
655 1.1 bouyer idesc.id_func = pass4check;
656 1.1 bouyer idesc.id_number = ino;
657 1.1 bouyer dp = ginode(ino);
658 1.1 bouyer (void)ckinode(dp, &idesc);
659 1.1 bouyer clearinode(dp);
660 1.1 bouyer inodirty();
661 1.1 bouyer statemap[ino] = USTATE;
662 1.1 bouyer n_files--;
663 1.1 bouyer }
664