]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/fsck_ffs/inode.c
MFC of 346185
[FreeBSD/FreeBSD.git] / sbin / fsck_ffs / inode.c
1 /*
2  * Copyright (c) 1980, 1986, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #if 0
31 #ifndef lint
32 static const char sccsid[] = "@(#)inode.c       8.8 (Berkeley) 4/28/95";
33 #endif /* not lint */
34 #endif
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37
38 #include <sys/param.h>
39 #include <sys/stdint.h>
40 #include <sys/sysctl.h>
41
42 #include <ufs/ufs/dinode.h>
43 #include <ufs/ufs/dir.h>
44 #include <ufs/ffs/fs.h>
45
46 #include <err.h>
47 #include <pwd.h>
48 #include <string.h>
49 #include <time.h>
50
51 #include "fsck.h"
52
53 static ino_t startinum;
54
55 static int iblock(struct inodesc *, off_t isize, int type);
56
57 int
58 ckinode(union dinode *dp, struct inodesc *idesc)
59 {
60         off_t remsize, sizepb;
61         int i, offset, ret;
62         union dinode dino;
63         ufs2_daddr_t ndb;
64         mode_t mode;
65         char pathbuf[MAXPATHLEN + 1];
66
67         if (idesc->id_fix != IGNORE)
68                 idesc->id_fix = DONTKNOW;
69         idesc->id_lbn = -1;
70         idesc->id_lballoc = -1;
71         idesc->id_level = 0;
72         idesc->id_entryno = 0;
73         idesc->id_filesize = DIP(dp, di_size);
74         mode = DIP(dp, di_mode) & IFMT;
75         if (mode == IFBLK || mode == IFCHR || (mode == IFLNK &&
76             DIP(dp, di_size) < (unsigned)sblock.fs_maxsymlinklen))
77                 return (KEEPON);
78         if (sblock.fs_magic == FS_UFS1_MAGIC)
79                 dino.dp1 = dp->dp1;
80         else
81                 dino.dp2 = dp->dp2;
82         ndb = howmany(DIP(&dino, di_size), sblock.fs_bsize);
83         for (i = 0; i < NDADDR; i++) {
84                 idesc->id_lbn++;
85                 if (--ndb == 0 &&
86                     (offset = blkoff(&sblock, DIP(&dino, di_size))) != 0)
87                         idesc->id_numfrags =
88                                 numfrags(&sblock, fragroundup(&sblock, offset));
89                 else
90                         idesc->id_numfrags = sblock.fs_frag;
91                 if (DIP(&dino, di_db[i]) == 0) {
92                         if (idesc->id_type == DATA && ndb >= 0) {
93                                 /* An empty block in a directory XXX */
94                                 getpathname(pathbuf, idesc->id_number,
95                                                 idesc->id_number);
96                                 pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
97                                         pathbuf);
98                                 if (reply("ADJUST LENGTH") == 1) {
99                                         dp = ginode(idesc->id_number);
100                                         DIP_SET(dp, di_size,
101                                             i * sblock.fs_bsize);
102                                         printf(
103                                             "YOU MUST RERUN FSCK AFTERWARDS\n");
104                                         rerun = 1;
105                                         inodirty(dp);
106
107                                 }
108                         }
109                         continue;
110                 }
111                 idesc->id_blkno = DIP(&dino, di_db[i]);
112                 if (idesc->id_type != DATA)
113                         ret = (*idesc->id_func)(idesc);
114                 else
115                         ret = dirscan(idesc);
116                 if (ret & STOP)
117                         return (ret);
118         }
119         idesc->id_numfrags = sblock.fs_frag;
120         remsize = DIP(&dino, di_size) - sblock.fs_bsize * NDADDR;
121         sizepb = sblock.fs_bsize;
122         for (i = 0; i < NIADDR; i++) {
123                 sizepb *= NINDIR(&sblock);
124                 idesc->id_level = i + 1;
125                 if (DIP(&dino, di_ib[i])) {
126                         idesc->id_blkno = DIP(&dino, di_ib[i]);
127                         ret = iblock(idesc, remsize, BT_LEVEL1 + i);
128                         if (ret & STOP)
129                                 return (ret);
130                 } else if (remsize > 0) {
131                         idesc->id_lbn += sizepb / sblock.fs_bsize;
132                         if (idesc->id_type == DATA) {
133                                 /* An empty block in a directory XXX */
134                                 getpathname(pathbuf, idesc->id_number,
135                                                 idesc->id_number);
136                                 pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
137                                         pathbuf);
138                                 if (reply("ADJUST LENGTH") == 1) {
139                                         dp = ginode(idesc->id_number);
140                                         DIP_SET(dp, di_size,
141                                             DIP(dp, di_size) - remsize);
142                                         remsize = 0;
143                                         printf(
144                                             "YOU MUST RERUN FSCK AFTERWARDS\n");
145                                         rerun = 1;
146                                         inodirty(dp);
147                                         break;
148                                 }
149                         }
150                 }
151                 remsize -= sizepb;
152         }
153         return (KEEPON);
154 }
155
156 static int
157 iblock(struct inodesc *idesc, off_t isize, int type)
158 {
159         struct bufarea *bp;
160         int i, n, (*func)(struct inodesc *), nif;
161         off_t sizepb;
162         char buf[BUFSIZ];
163         char pathbuf[MAXPATHLEN + 1];
164         union dinode *dp;
165
166         if (idesc->id_type != DATA) {
167                 func = idesc->id_func;
168                 if (((n = (*func)(idesc)) & KEEPON) == 0)
169                         return (n);
170         } else
171                 func = dirscan;
172         if (chkrange(idesc->id_blkno, idesc->id_numfrags))
173                 return (SKIP);
174         bp = getdatablk(idesc->id_blkno, sblock.fs_bsize, type);
175         idesc->id_level--;
176         for (sizepb = sblock.fs_bsize, i = 0; i < idesc->id_level; i++)
177                 sizepb *= NINDIR(&sblock);
178         if (howmany(isize, sizepb) > NINDIR(&sblock))
179                 nif = NINDIR(&sblock);
180         else
181                 nif = howmany(isize, sizepb);
182         if (idesc->id_func == pass1check && nif < NINDIR(&sblock)) {
183                 for (i = nif; i < NINDIR(&sblock); i++) {
184                         if (IBLK(bp, i) == 0)
185                                 continue;
186                         (void)sprintf(buf, "PARTIALLY TRUNCATED INODE I=%lu",
187                             (u_long)idesc->id_number);
188                         if (preen) {
189                                 pfatal("%s", buf);
190                         } else if (dofix(idesc, buf)) {
191                                 IBLK_SET(bp, i, 0);
192                                 dirty(bp);
193                         }
194                 }
195                 flush(fswritefd, bp);
196         }
197         for (i = 0; i < nif; i++) {
198                 if (IBLK(bp, i)) {
199                         idesc->id_blkno = IBLK(bp, i);
200                         if (idesc->id_level == 0) {
201                                 idesc->id_lbn++;
202                                 n = (*func)(idesc);
203                         } else {
204                                 n = iblock(idesc, isize, type);
205                                 idesc->id_level++;
206                         }
207                         if (n & STOP) {
208                                 bp->b_flags &= ~B_INUSE;
209                                 return (n);
210                         }
211                 } else {
212                         idesc->id_lbn += sizepb / sblock.fs_bsize;
213                         if (idesc->id_type == DATA && isize > 0) {
214                                 /* An empty block in a directory XXX */
215                                 getpathname(pathbuf, idesc->id_number,
216                                                 idesc->id_number);
217                                 pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
218                                         pathbuf);
219                                 if (reply("ADJUST LENGTH") == 1) {
220                                         dp = ginode(idesc->id_number);
221                                         DIP_SET(dp, di_size,
222                                             DIP(dp, di_size) - isize);
223                                         isize = 0;
224                                         printf(
225                                             "YOU MUST RERUN FSCK AFTERWARDS\n");
226                                         rerun = 1;
227                                         inodirty(dp);
228                                         bp->b_flags &= ~B_INUSE;
229                                         return(STOP);
230                                 }
231                         }
232                 }
233                 isize -= sizepb;
234         }
235         bp->b_flags &= ~B_INUSE;
236         return (KEEPON);
237 }
238
239 /*
240  * Check that a block in a legal block number.
241  * Return 0 if in range, 1 if out of range.
242  */
243 int
244 chkrange(ufs2_daddr_t blk, int cnt)
245 {
246         int c;
247
248         if (cnt <= 0 || blk <= 0 || blk > maxfsblock ||
249             cnt - 1 > maxfsblock - blk)
250                 return (1);
251         if (cnt > sblock.fs_frag ||
252             fragnum(&sblock, blk) + cnt > sblock.fs_frag) {
253                 if (debug)
254                         printf("bad size: blk %ld, offset %i, size %d\n",
255                             (long)blk, (int)fragnum(&sblock, blk), cnt);
256                 return (1);
257         }
258         c = dtog(&sblock, blk);
259         if (blk < cgdmin(&sblock, c)) {
260                 if ((blk + cnt) > cgsblock(&sblock, c)) {
261                         if (debug) {
262                                 printf("blk %ld < cgdmin %ld;",
263                                     (long)blk, (long)cgdmin(&sblock, c));
264                                 printf(" blk + cnt %ld > cgsbase %ld\n",
265                                     (long)(blk + cnt),
266                                     (long)cgsblock(&sblock, c));
267                         }
268                         return (1);
269                 }
270         } else {
271                 if ((blk + cnt) > cgbase(&sblock, c+1)) {
272                         if (debug)  {
273                                 printf("blk %ld >= cgdmin %ld;",
274                                     (long)blk, (long)cgdmin(&sblock, c));
275                                 printf(" blk + cnt %ld > sblock.fs_fpg %ld\n",
276                                     (long)(blk + cnt), (long)sblock.fs_fpg);
277                         }
278                         return (1);
279                 }
280         }
281         return (0);
282 }
283
284 /*
285  * General purpose interface for reading inodes.
286  */
287 union dinode *
288 ginode(ino_t inumber)
289 {
290         ufs2_daddr_t iblk;
291
292         if (inumber < ROOTINO || inumber > maxino)
293                 errx(EEXIT, "bad inode number %ju to ginode",
294                     (uintmax_t)inumber);
295         if (startinum == 0 ||
296             inumber < startinum || inumber >= startinum + INOPB(&sblock)) {
297                 iblk = ino_to_fsba(&sblock, inumber);
298                 if (pbp != NULL)
299                         pbp->b_flags &= ~B_INUSE;
300                 pbp = getdatablk(iblk, sblock.fs_bsize, BT_INODES);
301                 startinum = rounddown(inumber, INOPB(&sblock));
302         }
303         if (sblock.fs_magic == FS_UFS1_MAGIC)
304                 return ((union dinode *)
305                     &pbp->b_un.b_dinode1[inumber % INOPB(&sblock)]);
306         return ((union dinode *)&pbp->b_un.b_dinode2[inumber % INOPB(&sblock)]);
307 }
308
309 /*
310  * Special purpose version of ginode used to optimize first pass
311  * over all the inodes in numerical order.
312  */
313 static ino_t nextino, lastinum, lastvalidinum;
314 static long readcount, readpercg, fullcnt, inobufsize, partialcnt, partialsize;
315 static struct bufarea inobuf;
316
317 union dinode *
318 getnextinode(ino_t inumber, int rebuildcg)
319 {
320         int j;
321         long size;
322         mode_t mode;
323         ufs2_daddr_t ndb, blk;
324         union dinode *dp;
325         static caddr_t nextinop;
326
327         if (inumber != nextino++ || inumber > lastvalidinum)
328                 errx(EEXIT, "bad inode number %ju to nextinode",
329                     (uintmax_t)inumber);
330         if (inumber >= lastinum) {
331                 readcount++;
332                 blk = ino_to_fsba(&sblock, lastinum);
333                 if (readcount % readpercg == 0) {
334                         size = partialsize;
335                         lastinum += partialcnt;
336                 } else {
337                         size = inobufsize;
338                         lastinum += fullcnt;
339                 }
340                 /*
341                  * If getblk encounters an error, it will already have zeroed
342                  * out the buffer, so we do not need to do so here.
343                  */
344                 getblk(&inobuf, blk, size);
345                 nextinop = inobuf.b_un.b_buf;
346         }
347         dp = (union dinode *)nextinop;
348         if (rebuildcg && nextinop == inobuf.b_un.b_buf) {
349                 /*
350                  * Try to determine if we have reached the end of the
351                  * allocated inodes.
352                  */
353                 mode = DIP(dp, di_mode) & IFMT;
354                 if (mode == 0) {
355                         if (memcmp(dp->dp2.di_db, ufs2_zino.di_db,
356                                 NDADDR * sizeof(ufs2_daddr_t)) ||
357                               memcmp(dp->dp2.di_ib, ufs2_zino.di_ib,
358                                 NIADDR * sizeof(ufs2_daddr_t)) ||
359                               dp->dp2.di_mode || dp->dp2.di_size)
360                                 return (NULL);
361                         goto inodegood;
362                 }
363                 if (!ftypeok(dp))
364                         return (NULL);
365                 ndb = howmany(DIP(dp, di_size), sblock.fs_bsize);
366                 if (ndb < 0)
367                         return (NULL);
368                 if (mode == IFBLK || mode == IFCHR)
369                         ndb++;
370                 if (mode == IFLNK) {
371                         /*
372                          * Fake ndb value so direct/indirect block checks below
373                          * will detect any garbage after symlink string.
374                          */
375                         if (DIP(dp, di_size) < (off_t)sblock.fs_maxsymlinklen) {
376                                 ndb = howmany(DIP(dp, di_size),
377                                     sizeof(ufs2_daddr_t));
378                                 if (ndb > NDADDR) {
379                                         j = ndb - NDADDR;
380                                         for (ndb = 1; j > 1; j--)
381                                                 ndb *= NINDIR(&sblock);
382                                         ndb += NDADDR;
383                                 }
384                         }
385                 }
386                 for (j = ndb; ndb < NDADDR && j < NDADDR; j++)
387                         if (DIP(dp, di_db[j]) != 0)
388                                 return (NULL);
389                 for (j = 0, ndb -= NDADDR; ndb > 0; j++)
390                         ndb /= NINDIR(&sblock);
391                 for (; j < NIADDR; j++)
392                         if (DIP(dp, di_ib[j]) != 0)
393                                 return (NULL);
394         }
395 inodegood:
396         if (sblock.fs_magic == FS_UFS1_MAGIC)
397                 nextinop += sizeof(struct ufs1_dinode);
398         else
399                 nextinop += sizeof(struct ufs2_dinode);
400         return (dp);
401 }
402
403 void
404 setinodebuf(ino_t inum)
405 {
406
407         if (inum % sblock.fs_ipg != 0)
408                 errx(EEXIT, "bad inode number %ju to setinodebuf",
409                     (uintmax_t)inum);
410         lastvalidinum = inum + sblock.fs_ipg - 1;
411         startinum = 0;
412         nextino = inum;
413         lastinum = inum;
414         readcount = 0;
415         if (inobuf.b_un.b_buf != NULL)
416                 return;
417         inobufsize = blkroundup(&sblock, INOBUFSIZE);
418         fullcnt = inobufsize / ((sblock.fs_magic == FS_UFS1_MAGIC) ?
419             sizeof(struct ufs1_dinode) : sizeof(struct ufs2_dinode));
420         readpercg = sblock.fs_ipg / fullcnt;
421         partialcnt = sblock.fs_ipg % fullcnt;
422         partialsize = partialcnt * ((sblock.fs_magic == FS_UFS1_MAGIC) ?
423             sizeof(struct ufs1_dinode) : sizeof(struct ufs2_dinode));
424         if (partialcnt != 0) {
425                 readpercg++;
426         } else {
427                 partialcnt = fullcnt;
428                 partialsize = inobufsize;
429         }
430         initbarea(&inobuf, BT_INODES);
431         if ((inobuf.b_un.b_buf = Malloc((unsigned)inobufsize)) == NULL)
432                 errx(EEXIT, "cannot allocate space for inode buffer");
433 }
434
435 void
436 freeinodebuf(void)
437 {
438
439         if (inobuf.b_un.b_buf != NULL)
440                 free((char *)inobuf.b_un.b_buf);
441         inobuf.b_un.b_buf = NULL;
442 }
443
444 /*
445  * Routines to maintain information about directory inodes.
446  * This is built during the first pass and used during the
447  * second and third passes.
448  *
449  * Enter inodes into the cache.
450  */
451 void
452 cacheino(union dinode *dp, ino_t inumber)
453 {
454         struct inoinfo *inp, **inpp;
455         int i, blks;
456
457         if (howmany(DIP(dp, di_size), sblock.fs_bsize) > NDADDR)
458                 blks = NDADDR + NIADDR;
459         else if (DIP(dp, di_size) > 0)
460                 blks = howmany(DIP(dp, di_size), sblock.fs_bsize);
461         else
462                 blks = 1;
463         inp = (struct inoinfo *)
464                 Malloc(sizeof(*inp) + (blks - 1) * sizeof(ufs2_daddr_t));
465         if (inp == NULL)
466                 errx(EEXIT, "cannot increase directory list");
467         inpp = &inphead[inumber % dirhash];
468         inp->i_nexthash = *inpp;
469         *inpp = inp;
470         inp->i_parent = inumber == ROOTINO ? ROOTINO : (ino_t)0;
471         inp->i_dotdot = (ino_t)0;
472         inp->i_number = inumber;
473         inp->i_isize = DIP(dp, di_size);
474         inp->i_numblks = blks;
475         for (i = 0; i < MIN(blks, NDADDR); i++)
476                 inp->i_blks[i] = DIP(dp, di_db[i]);
477         if (blks > NDADDR)
478                 for (i = 0; i < NIADDR; i++)
479                         inp->i_blks[NDADDR + i] = DIP(dp, di_ib[i]);
480         if (inplast == listmax) {
481                 listmax += 100;
482                 inpsort = (struct inoinfo **)reallocarray((char *)inpsort,
483                     listmax, sizeof(struct inoinfo *));
484                 if (inpsort == NULL)
485                         errx(EEXIT, "cannot increase directory list");
486         }
487         inpsort[inplast++] = inp;
488 }
489
490 /*
491  * Look up an inode cache structure.
492  */
493 struct inoinfo *
494 getinoinfo(ino_t inumber)
495 {
496         struct inoinfo *inp;
497
498         for (inp = inphead[inumber % dirhash]; inp; inp = inp->i_nexthash) {
499                 if (inp->i_number != inumber)
500                         continue;
501                 return (inp);
502         }
503         errx(EEXIT, "cannot find inode %ju", (uintmax_t)inumber);
504         return ((struct inoinfo *)0);
505 }
506
507 /*
508  * Clean up all the inode cache structure.
509  */
510 void
511 inocleanup(void)
512 {
513         struct inoinfo **inpp;
514
515         if (inphead == NULL)
516                 return;
517         for (inpp = &inpsort[inplast - 1]; inpp >= inpsort; inpp--)
518                 free((char *)(*inpp));
519         free((char *)inphead);
520         free((char *)inpsort);
521         inphead = inpsort = NULL;
522 }
523
524 void
525 inodirty(union dinode *dp)
526 {
527
528         dirty(pbp);
529 }
530
531 void
532 clri(struct inodesc *idesc, const char *type, int flag)
533 {
534         union dinode *dp;
535
536         dp = ginode(idesc->id_number);
537         if (flag == 1) {
538                 pwarn("%s %s", type,
539                     (DIP(dp, di_mode) & IFMT) == IFDIR ? "DIR" : "FILE");
540                 pinode(idesc->id_number);
541         }
542         if (preen || reply("CLEAR") == 1) {
543                 if (preen)
544                         printf(" (CLEARED)\n");
545                 n_files--;
546                 if (bkgrdflag == 0) {
547                         (void)ckinode(dp, idesc);
548                         inoinfo(idesc->id_number)->ino_state = USTATE;
549                         clearinode(dp);
550                         inodirty(dp);
551                 } else {
552                         cmd.value = idesc->id_number;
553                         cmd.size = -DIP(dp, di_nlink);
554                         if (debug)
555                                 printf("adjrefcnt ino %ld amt %lld\n",
556                                     (long)cmd.value, (long long)cmd.size);
557                         if (sysctl(adjrefcnt, MIBSIZE, 0, 0,
558                             &cmd, sizeof cmd) == -1)
559                                 rwerror("ADJUST INODE", cmd.value);
560                 }
561         }
562 }
563
564 int
565 findname(struct inodesc *idesc)
566 {
567         struct direct *dirp = idesc->id_dirp;
568
569         if (dirp->d_ino != idesc->id_parent || idesc->id_entryno < 2) {
570                 idesc->id_entryno++;
571                 return (KEEPON);
572         }
573         memmove(idesc->id_name, dirp->d_name, (size_t)dirp->d_namlen + 1);
574         return (STOP|FOUND);
575 }
576
577 int
578 findino(struct inodesc *idesc)
579 {
580         struct direct *dirp = idesc->id_dirp;
581
582         if (dirp->d_ino == 0)
583                 return (KEEPON);
584         if (strcmp(dirp->d_name, idesc->id_name) == 0 &&
585             dirp->d_ino >= ROOTINO && dirp->d_ino <= maxino) {
586                 idesc->id_parent = dirp->d_ino;
587                 return (STOP|FOUND);
588         }
589         return (KEEPON);
590 }
591
592 int
593 clearentry(struct inodesc *idesc)
594 {
595         struct direct *dirp = idesc->id_dirp;
596
597         if (dirp->d_ino != idesc->id_parent || idesc->id_entryno < 2) {
598                 idesc->id_entryno++;
599                 return (KEEPON);
600         }
601         dirp->d_ino = 0;
602         return (STOP|FOUND|ALTERED);
603 }
604
605 void
606 pinode(ino_t ino)
607 {
608         union dinode *dp;
609         char *p;
610         struct passwd *pw;
611         time_t t;
612
613         printf(" I=%lu ", (u_long)ino);
614         if (ino < ROOTINO || ino > maxino)
615                 return;
616         dp = ginode(ino);
617         printf(" OWNER=");
618         if ((pw = getpwuid((int)DIP(dp, di_uid))) != NULL)
619                 printf("%s ", pw->pw_name);
620         else
621                 printf("%u ", (unsigned)DIP(dp, di_uid));
622         printf("MODE=%o\n", DIP(dp, di_mode));
623         if (preen)
624                 printf("%s: ", cdevname);
625         printf("SIZE=%ju ", (uintmax_t)DIP(dp, di_size));
626         t = DIP(dp, di_mtime);
627         p = ctime(&t);
628         printf("MTIME=%12.12s %4.4s ", &p[4], &p[20]);
629 }
630
631 void
632 blkerror(ino_t ino, const char *type, ufs2_daddr_t blk)
633 {
634
635         pfatal("%jd %s I=%ju", (intmax_t)blk, type, (uintmax_t)ino);
636         printf("\n");
637         switch (inoinfo(ino)->ino_state) {
638
639         case FSTATE:
640         case FZLINK:
641                 inoinfo(ino)->ino_state = FCLEAR;
642                 return;
643
644         case DSTATE:
645         case DZLINK:
646                 inoinfo(ino)->ino_state = DCLEAR;
647                 return;
648
649         case FCLEAR:
650         case DCLEAR:
651                 return;
652
653         default:
654                 errx(EEXIT, "BAD STATE %d TO BLKERR", inoinfo(ino)->ino_state);
655                 /* NOTREACHED */
656         }
657 }
658
659 /*
660  * allocate an unused inode
661  */
662 ino_t
663 allocino(ino_t request, int type)
664 {
665         ino_t ino;
666         union dinode *dp;
667         struct bufarea *cgbp;
668         struct cg *cgp;
669         int cg;
670
671         if (request == 0)
672                 request = ROOTINO;
673         else if (inoinfo(request)->ino_state != USTATE)
674                 return (0);
675         for (ino = request; ino < maxino; ino++)
676                 if (inoinfo(ino)->ino_state == USTATE)
677                         break;
678         if (ino == maxino)
679                 return (0);
680         cg = ino_to_cg(&sblock, ino);
681         cgbp = cgget(cg);
682         cgp = cgbp->b_un.b_cg;
683         if (!check_cgmagic(cg, cgbp))
684                 return (0);
685         setbit(cg_inosused(cgp), ino % sblock.fs_ipg);
686         cgp->cg_cs.cs_nifree--;
687         switch (type & IFMT) {
688         case IFDIR:
689                 inoinfo(ino)->ino_state = DSTATE;
690                 cgp->cg_cs.cs_ndir++;
691                 break;
692         case IFREG:
693         case IFLNK:
694                 inoinfo(ino)->ino_state = FSTATE;
695                 break;
696         default:
697                 return (0);
698         }
699         dirty(cgbp);
700         dp = ginode(ino);
701         DIP_SET(dp, di_db[0], allocblk((long)1));
702         if (DIP(dp, di_db[0]) == 0) {
703                 inoinfo(ino)->ino_state = USTATE;
704                 return (0);
705         }
706         DIP_SET(dp, di_mode, type);
707         DIP_SET(dp, di_flags, 0);
708         DIP_SET(dp, di_atime, time(NULL));
709         DIP_SET(dp, di_ctime, DIP(dp, di_atime));
710         DIP_SET(dp, di_mtime, DIP(dp, di_ctime));
711         DIP_SET(dp, di_mtimensec, 0);
712         DIP_SET(dp, di_ctimensec, 0);
713         DIP_SET(dp, di_atimensec, 0);
714         DIP_SET(dp, di_size, sblock.fs_fsize);
715         DIP_SET(dp, di_blocks, btodb(sblock.fs_fsize));
716         n_files++;
717         inodirty(dp);
718         inoinfo(ino)->ino_type = IFTODT(type);
719         return (ino);
720 }
721
722 /*
723  * deallocate an inode
724  */
725 void
726 freeino(ino_t ino)
727 {
728         struct inodesc idesc;
729         union dinode *dp;
730
731         memset(&idesc, 0, sizeof(struct inodesc));
732         idesc.id_type = ADDR;
733         idesc.id_func = pass4check;
734         idesc.id_number = ino;
735         dp = ginode(ino);
736         (void)ckinode(dp, &idesc);
737         clearinode(dp);
738         inodirty(dp);
739         inoinfo(ino)->ino_state = USTATE;
740         n_files--;
741 }