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