1 /*        $NetBSD: dir.c,v 1.28 2013/06/23 07:28:36 dholland Exp $    */
2 
3 /*
4  * Copyright (c) 1980, 1986, 1993
5  *        The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 /*
33  * Copyright (c) 1997 Manuel Bouyer.
34  *
35  * Redistribution and use in source and binary forms, with or without
36  * modification, are permitted provided that the following conditions
37  * are met:
38  * 1. Redistributions of source code must retain the above copyright
39  *    notice, this list of conditions and the following disclaimer.
40  * 2. Redistributions in binary form must reproduce the above copyright
41  *    notice, this list of conditions and the following disclaimer in the
42  *    documentation and/or other materials provided with the distribution.
43  *
44  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
45  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
46  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
47  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
48  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
49  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
50  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
51  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
52  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
53  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54  */
55 
56 #include <sys/cdefs.h>
57 #ifndef lint
58 #if 0
59 static char sccsid[] = "@(#)dir.c       8.5 (Berkeley) 12/8/94";
60 #else
61 __RCSID("$NetBSD: dir.c,v 1.28 2013/06/23 07:28:36 dholland Exp $");
62 #endif
63 #endif /* not lint */
64 
65 #include <sys/param.h>
66 #include <sys/time.h>
67 #include <ufs/ufs/dir.h>
68 #include <ufs/ext2fs/ext2fs_dinode.h>
69 #include <ufs/ext2fs/ext2fs_dir.h>
70 #include <ufs/ext2fs/ext2fs.h>
71 
72 #include <ufs/ufs/dinode.h> /* for IFMT & friends */
73 
74 #include <stdio.h>
75 #include <stdlib.h>
76 #include <string.h>
77 #include <err.h>
78 
79 #include "fsck.h"
80 #include "fsutil.h"
81 #include "extern.h"
82 
83 const char          *lfname = "lost+found";
84 int       lfmode = 01700;
85 struct    ext2fs_dirtemplate emptydir = {
86           .dot_ino = 0,
87           .dot_reclen = UFS_DIRBLKSIZ,
88 };
89 struct    ext2fs_dirtemplate dirhead = {
90           .dot_ino = 0,
91           .dot_reclen = 12,
92           .dot_namlen = 1,
93           .dot_type = EXT2_FT_DIR,
94           .dot_name = ".",
95           .dotdot_ino = 0,
96           .dotdot_reclen = UFS_DIRBLKSIZ - 12,
97           .dotdot_namlen = 2,
98           .dotdot_type = EXT2_FT_DIR,
99           .dotdot_name = "..",
100 };
101 #undef UFS_DIRBLKSIZ
102 
103 static int expanddir(struct ext2fs_dinode *, char *);
104 static void freedir(ino_t, ino_t);
105 static struct ext2fs_direct *fsck_readdir(struct inodesc *);
106 static struct bufarea *getdirblk(daddr_t, long);
107 static int lftempname(char *, ino_t);
108 static int mkentry(struct inodesc *);
109 static int chgino(struct  inodesc *);
110 
111 /*
112  * Propagate connected state through the tree.
113  */
114 void
propagate(void)115 propagate(void)
116 {
117           struct inoinfo **inpp, *inp, *pinp;
118           struct inoinfo **inpend;
119 
120           /*
121            * Create a list of children for each directory.
122            */
123           inpend = &inpsort[inplast];
124           for (inpp = inpsort; inpp < inpend; inpp++) {
125                     inp = *inpp;
126                     if (inp->i_parent == 0 ||
127                         inp->i_number == EXT2_ROOTINO)
128                               continue;
129                     pinp = getinoinfo(inp->i_parent);
130                     inp->i_parentp = pinp;
131                     inp->i_sibling = pinp->i_child;
132                     pinp->i_child = inp;
133           }
134           inp = getinoinfo(EXT2_ROOTINO);
135           while (inp) {
136                     statemap[inp->i_number] = DFOUND;
137                     if (inp->i_child &&
138                         statemap[inp->i_child->i_number] == DSTATE)
139                               inp = inp->i_child;
140                     else if (inp->i_sibling)
141                               inp = inp->i_sibling;
142                     else
143                               inp = inp->i_parentp;
144           }
145 }
146 
147 /*
148  * Scan each entry in a directory block.
149  */
150 int
dirscan(struct inodesc * idesc)151 dirscan(struct inodesc *idesc)
152 {
153           struct ext2fs_direct *dp;
154           struct bufarea *bp;
155           int dsize, n;
156           long blksiz;
157           char *dbuf = NULL;
158 
159           if ((dbuf = malloc(sblock.e2fs_bsize)) == NULL)
160                     err(8, "Can't allocate directory block");
161 
162           if (idesc->id_type != DATA)
163                     errexit("wrong type to dirscan %d", idesc->id_type);
164           if (idesc->id_entryno == 0 &&
165               (idesc->id_filesize & (sblock.e2fs_bsize - 1)) != 0)
166                     idesc->id_filesize = roundup(idesc->id_filesize, sblock.e2fs_bsize);
167           blksiz = idesc->id_numfrags * sblock.e2fs_bsize;
168           if (chkrange(idesc->id_blkno, idesc->id_numfrags)) {
169                     idesc->id_filesize -= blksiz;
170                     free(dbuf);
171                     return (SKIP);
172           }
173           idesc->id_loc = 0;
174           for (dp = fsck_readdir(idesc); dp != NULL; dp = fsck_readdir(idesc)) {
175                     dsize = fs2h16(dp->e2d_reclen);
176                     memcpy(dbuf, dp, (size_t)dsize);
177                     idesc->id_dirp = (struct ext2fs_direct *)dbuf;
178                     if ((n = (*idesc->id_func)(idesc)) & ALTERED) {
179                               bp = getdirblk(idesc->id_blkno, blksiz);
180                               memcpy(bp->b_un.b_buf + idesc->id_loc - dsize, dbuf,
181                                   (size_t)dsize);
182                               dirty(bp);
183                               sbdirty();
184                     }
185                     if (n & STOP) {
186                               free(dbuf);
187                               return (n);
188                     }
189           }
190           free(dbuf);
191           return (idesc->id_filesize > 0 ? KEEPON : STOP);
192 }
193 
194 /*
195  * get next entry in a directory.
196  */
197 static struct ext2fs_direct *
fsck_readdir(struct inodesc * idesc)198 fsck_readdir(struct inodesc *idesc)
199 {
200           struct ext2fs_direct *dp, *ndp;
201           struct bufarea *bp;
202           long size, blksiz, fix, dploc;
203 
204           blksiz = idesc->id_numfrags * sblock.e2fs_bsize;
205           bp = getdirblk(idesc->id_blkno, blksiz);
206           if (idesc->id_loc % sblock.e2fs_bsize == 0 && idesc->id_filesize > 0 &&
207               idesc->id_loc < blksiz) {
208                     dp = (struct ext2fs_direct *)(bp->b_un.b_buf + idesc->id_loc);
209                     if (dircheck(idesc, dp))
210                               goto dpok;
211                     if (idesc->id_fix == IGNORE)
212                               return (0);
213                     fix = dofix(idesc, "DIRECTORY CORRUPTED");
214                     bp = getdirblk(idesc->id_blkno, blksiz);
215                     dp = (struct ext2fs_direct *)(bp->b_un.b_buf + idesc->id_loc);
216                     dp->e2d_reclen = h2fs16(sblock.e2fs_bsize);
217                     dp->e2d_ino = 0;
218                     dp->e2d_namlen = 0;
219                     dp->e2d_type = 0;
220                     dp->e2d_name[0] = '\0';
221                     if (fix)
222                               dirty(bp);
223                     idesc->id_loc += sblock.e2fs_bsize;
224                     idesc->id_filesize -= sblock.e2fs_bsize;
225                     return (dp);
226           }
227 dpok:
228           if (idesc->id_filesize <= 0 || idesc->id_loc >= blksiz)
229                     return NULL;
230           dploc = idesc->id_loc;
231           dp = (struct ext2fs_direct *)(bp->b_un.b_buf + dploc);
232           idesc->id_loc += fs2h16(dp->e2d_reclen);
233           idesc->id_filesize -= fs2h16(dp->e2d_reclen);
234           if ((idesc->id_loc % sblock.e2fs_bsize) == 0)
235                     return (dp);
236           ndp = (struct ext2fs_direct *)(bp->b_un.b_buf + idesc->id_loc);
237           if (idesc->id_loc < blksiz && idesc->id_filesize > 0 &&
238               dircheck(idesc, ndp) == 0) {
239                     size = sblock.e2fs_bsize - (idesc->id_loc % sblock.e2fs_bsize);
240                     idesc->id_loc += size;
241                     idesc->id_filesize -= size;
242                     if (idesc->id_fix == IGNORE)
243                               return (0);
244                     fix = dofix(idesc, "DIRECTORY CORRUPTED");
245                     bp = getdirblk(idesc->id_blkno, blksiz);
246                     dp = (struct ext2fs_direct *)(bp->b_un.b_buf + dploc);
247                     dp->e2d_reclen = h2fs16(fs2h16(dp->e2d_reclen) + size);
248                     if (fix)
249                               dirty(bp);
250           }
251           return (dp);
252 }
253 
254 /*
255  * Verify that a directory entry is valid.
256  * This is a superset of the checks made in the kernel.
257  */
258 int
dircheck(struct inodesc * idesc,struct ext2fs_direct * dp)259 dircheck(struct inodesc *idesc, struct ext2fs_direct *dp)
260 {
261           int size;
262           char *cp;
263           int spaceleft;
264           u_int16_t reclen = fs2h16(dp->e2d_reclen);
265 
266           spaceleft = sblock.e2fs_bsize - (idesc->id_loc % sblock.e2fs_bsize);
267           if (fs2h32(dp->e2d_ino) > maxino ||
268               reclen == 0 ||
269               reclen > spaceleft ||
270               (reclen & 0x3) != 0)
271                     return (0);
272           if (dp->e2d_ino == 0)
273                     return (1);
274           if (sblock.e2fs.e2fs_rev < E2FS_REV1 ||
275               (sblock.e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE) == 0)
276                     if (dp->e2d_type != 0)
277                               return (1);
278           size = EXT2FS_DIRSIZ(dp->e2d_namlen);
279           if (reclen < size ||
280               idesc->id_filesize < size /* ||
281               dp->e2d_namlen > EXT2FS_MAXNAMLEN */)
282                     return (0);
283           for (cp = dp->e2d_name, size = 0; size < dp->e2d_namlen; size++)
284                     if (*cp == '\0' || (*cp++ == '/'))
285                               return (0);
286           return (1);
287 }
288 
289 void
direrror(ino_t ino,const char * errmesg)290 direrror(ino_t ino, const char *errmesg)
291 {
292 
293           fileerror(ino, ino, errmesg);
294 }
295 
296 void
fileerror(ino_t cwd,ino_t ino,const char * errmesg)297 fileerror(ino_t cwd, ino_t ino, const char *errmesg)
298 {
299           struct ext2fs_dinode *dp;
300           char pathbuf[MAXPATHLEN + 1];
301 
302           pwarn("%s ", errmesg);
303           pinode(ino);
304           printf("\n");
305           getpathname(pathbuf, sizeof(pathbuf), cwd, ino);
306           if ((ino < EXT2_FIRSTINO && ino != EXT2_ROOTINO) || ino > maxino) {
307                     pfatal("NAME=%s\n", pathbuf);
308                     return;
309           }
310           dp = ginode(ino);
311           if (ftypeok(dp))
312                     pfatal("%s=%s\n",
313                         (fs2h16(dp->e2di_mode) & IFMT) == IFDIR ? "DIR" : "FILE", pathbuf);
314           else
315                     pfatal("NAME=%s\n", pathbuf);
316 }
317 
318 void
adjust(struct inodesc * idesc,short lcnt)319 adjust(struct inodesc *idesc, short lcnt)
320 {
321           struct ext2fs_dinode *dp;
322 
323           dp = ginode(idesc->id_number);
324           if (fs2h16(dp->e2di_nlink) == lcnt) {
325                     if (linkup(idesc->id_number, (ino_t)0) == 0)
326                               clri(idesc, "UNREF", 0);
327           } else {
328                     pwarn("LINK COUNT %s", (lfdir == idesc->id_number) ? lfname :
329                               ((fs2h16(dp->e2di_mode) & IFMT) == IFDIR ? "DIR" : "FILE"));
330                     pinode(idesc->id_number);
331                     printf(" COUNT %d SHOULD BE %d",
332                               fs2h16(dp->e2di_nlink), fs2h16(dp->e2di_nlink) - lcnt);
333                     if (preen) {
334                               if (lcnt < 0) {
335                                         printf("\n");
336                                         pfatal("LINK COUNT INCREASING");
337                               }
338                               printf(" (ADJUSTED)\n");
339                     }
340                     if (preen || reply("ADJUST") == 1) {
341                               dp->e2di_nlink = h2fs16(fs2h16(dp->e2di_nlink) - lcnt);
342                               inodirty();
343                     }
344           }
345 }
346 
347 static int
mkentry(struct inodesc * idesc)348 mkentry(struct inodesc *idesc)
349 {
350           struct ext2fs_direct *dirp = idesc->id_dirp;
351           struct ext2fs_direct newent;
352           int newlen, oldlen;
353 
354           newent.e2d_type = 0;          /* XXX gcc */
355           newent.e2d_namlen = strlen(idesc->id_name);
356           if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
357               (sblock.e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE))
358                     newent.e2d_type = inot2ext2dt(typemap[idesc->id_parent]);
359           newlen = EXT2FS_DIRSIZ(newent.e2d_namlen);
360           if (dirp->e2d_ino != 0)
361                     oldlen = EXT2FS_DIRSIZ(dirp->e2d_namlen);
362           else
363                     oldlen = 0;
364           if (fs2h16(dirp->e2d_reclen) - oldlen < newlen)
365                     return (KEEPON);
366           newent.e2d_reclen = h2fs16(fs2h16(dirp->e2d_reclen) - oldlen);
367           dirp->e2d_reclen = h2fs16(oldlen);
368           dirp = (struct ext2fs_direct *)(((char *)dirp) + oldlen);
369           dirp->e2d_ino = h2fs32(idesc->id_parent); /* ino to be entered is in id_parent */
370           dirp->e2d_reclen = newent.e2d_reclen;
371           dirp->e2d_namlen = newent.e2d_namlen;
372           dirp->e2d_type = newent.e2d_type;
373           memcpy(dirp->e2d_name, idesc->id_name, (size_t)(dirp->e2d_namlen));
374           return (ALTERED|STOP);
375 }
376 
377 static int
chgino(struct inodesc * idesc)378 chgino(struct inodesc *idesc)
379 {
380           struct ext2fs_direct *dirp = idesc->id_dirp;
381           u_int16_t namlen = dirp->e2d_namlen;
382 
383           if (strlen(idesc->id_name) != namlen ||
384                     strncmp(dirp->e2d_name, idesc->id_name, (int)namlen))
385                     return (KEEPON);
386           dirp->e2d_ino = h2fs32(idesc->id_parent);
387           if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
388               (sblock.e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE))
389                     dirp->e2d_type = inot2ext2dt(typemap[idesc->id_parent]);
390           else
391                     dirp->e2d_type = 0;
392           return (ALTERED|STOP);
393 }
394 
395 int
linkup(ino_t orphan,ino_t parentdir)396 linkup(ino_t orphan, ino_t parentdir)
397 {
398           struct ext2fs_dinode *dp;
399           int lostdir;
400           ino_t oldlfdir;
401           struct inodesc idesc;
402           char tempname[BUFSIZ];
403 
404           memset(&idesc, 0, sizeof(struct inodesc));
405           dp = ginode(orphan);
406           lostdir = (fs2h16(dp->e2di_mode) & IFMT) == IFDIR;
407           pwarn("UNREF %s ", lostdir ? "DIR" : "FILE");
408           pinode(orphan);
409           if (preen && inosize(dp) == 0)
410                     return (0);
411           if (preen)
412                     printf(" (RECONNECTED)\n");
413           else
414                     if (reply("RECONNECT") == 0)
415                               return (0);
416           if (lfdir == 0) {
417                     dp = ginode(EXT2_ROOTINO);
418                     idesc.id_name = lfname;
419                     idesc.id_type = DATA;
420                     idesc.id_func = findino;
421                     idesc.id_number = EXT2_ROOTINO;
422                     if ((ckinode(dp, &idesc) & FOUND) != 0) {
423                               lfdir = idesc.id_parent;
424                     } else {
425                               pwarn("NO lost+found DIRECTORY");
426                               if (preen || reply("CREATE")) {
427                                         lfdir = allocdir(EXT2_ROOTINO, (ino_t)0, lfmode);
428                                         if (lfdir != 0) {
429                                                   if (makeentry(EXT2_ROOTINO, lfdir, lfname) != 0) {
430                                                             if (preen)
431                                                                       printf(" (CREATED)\n");
432                                                   } else {
433                                                             freedir(lfdir, EXT2_ROOTINO);
434                                                             lfdir = 0;
435                                                             if (preen)
436                                                                       printf("\n");
437                                                   }
438                                         }
439                               }
440                     }
441                     if (lfdir == 0) {
442                               pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY");
443                               printf("\n\n");
444                               return (0);
445                     }
446           }
447           dp = ginode(lfdir);
448           if ((fs2h16(dp->e2di_mode) & IFMT) != IFDIR) {
449                     pfatal("lost+found IS NOT A DIRECTORY");
450                     if (reply("REALLOCATE") == 0)
451                               return (0);
452                     oldlfdir = lfdir;
453                     if ((lfdir = allocdir(EXT2_ROOTINO, (ino_t)0, lfmode)) == 0) {
454                               pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
455                               return (0);
456                     }
457                     if ((changeino(EXT2_ROOTINO, lfname, lfdir) & ALTERED) == 0) {
458                               pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
459                               return (0);
460                     }
461                     inodirty();
462                     idesc.id_type = ADDR;
463                     idesc.id_func = pass4check;
464                     idesc.id_number = oldlfdir;
465                     adjust(&idesc, lncntp[oldlfdir] + 1);
466                     lncntp[oldlfdir] = 0;
467                     dp = ginode(lfdir);
468           }
469           if (statemap[lfdir] != DFOUND) {
470                     pfatal("SORRY. NO lost+found DIRECTORY\n\n");
471                     return (0);
472           }
473           (void)lftempname(tempname, orphan);
474           if (makeentry(lfdir, orphan, tempname) == 0) {
475                     pfatal("SORRY. NO SPACE IN lost+found DIRECTORY");
476                     printf("\n\n");
477                     return (0);
478           }
479           lncntp[orphan]--;
480           if (lostdir) {
481                     if ((changeino(orphan, "..", lfdir) & ALTERED) == 0 &&
482                         parentdir != (ino_t)-1)
483                               (void)makeentry(orphan, lfdir, "..");
484                     dp = ginode(lfdir);
485                     dp->e2di_nlink = h2fs16(fs2h16(dp->e2di_nlink) +1);
486                     inodirty();
487                     lncntp[lfdir]++;
488                     pwarn("DIR I=%llu CONNECTED. ", (unsigned long long)orphan);
489                     if (parentdir != (ino_t)-1)
490                               printf("PARENT WAS I=%llu\n",
491                                   (unsigned long long)parentdir);
492                     if (preen == 0)
493                               printf("\n");
494           }
495           return (1);
496 }
497 
498 /*
499  * fix an entry in a directory.
500  */
501 int
changeino(ino_t dir,const char * name,ino_t newnum)502 changeino(ino_t dir, const char *name, ino_t newnum)
503 {
504           struct inodesc idesc;
505 
506           memset(&idesc, 0, sizeof(struct inodesc));
507           idesc.id_type = DATA;
508           idesc.id_func = chgino;
509           idesc.id_number = dir;
510           idesc.id_fix = DONTKNOW;
511           idesc.id_name = name;
512           idesc.id_parent = newnum;     /* new value for name */
513           return (ckinode(ginode(dir), &idesc));
514 }
515 
516 /*
517  * make an entry in a directory
518  */
519 int
makeentry(ino_t parent,ino_t ino,const char * name)520 makeentry(ino_t parent, ino_t ino, const char *name)
521 {
522           struct ext2fs_dinode *dp;
523           struct inodesc idesc;
524           char pathbuf[MAXPATHLEN + 1];
525 
526           if ((parent < EXT2_FIRSTINO && parent != EXT2_ROOTINO)
527                     || parent >= maxino ||
528               (ino < EXT2_FIRSTINO && ino < EXT2_ROOTINO) || ino >= maxino)
529                     return (0);
530           memset(&idesc, 0, sizeof(struct inodesc));
531           idesc.id_type = DATA;
532           idesc.id_func = mkentry;
533           idesc.id_number = parent;
534           idesc.id_parent = ino;        /* this is the inode to enter */
535           idesc.id_fix = DONTKNOW;
536           idesc.id_name = name;
537           dp = ginode(parent);
538           if (inosize(dp) % sblock.e2fs_bsize) {
539                     inossize(dp, roundup(inosize(dp), sblock.e2fs_bsize));
540                     inodirty();
541           }
542           if ((ckinode(dp, &idesc) & ALTERED) != 0)
543                     return (1);
544           getpathname(pathbuf, sizeof(pathbuf), parent, parent);
545           dp = ginode(parent);
546           if (expanddir(dp, pathbuf) == 0)
547                     return (0);
548           return (ckinode(dp, &idesc) & ALTERED);
549 }
550 
551 /*
552  * Attempt to expand the size of a directory
553  */
554 static int
expanddir(struct ext2fs_dinode * dp,char * name)555 expanddir(struct ext2fs_dinode *dp, char *name)
556 {
557           daddr_t lastbn, newblk;
558           struct bufarea *bp;
559           char *firstblk;
560 
561           lastbn = ext2_lblkno(&sblock, inosize(dp));
562           if (lastbn >= EXT2FS_NDADDR - 1 || fs2h32(dp->e2di_blocks[lastbn]) == 0 ||
563                     inosize(dp) == 0) {
564                     return (0);
565           }
566           if ((newblk = allocblk()) == 0) {
567                     return (0);
568           }
569           dp->e2di_blocks[lastbn + 1] = dp->e2di_blocks[lastbn];
570           dp->e2di_blocks[lastbn] = h2fs32(newblk);
571           inossize(dp, inosize(dp) + sblock.e2fs_bsize);
572           inosnblock(dp, inonblock(dp) + btodb(sblock.e2fs_bsize));
573           bp = getdirblk(fs2h32(dp->e2di_blocks[lastbn + 1]),
574                     sblock.e2fs_bsize);
575           if (bp->b_errs)
576                     goto bad;
577           if ((firstblk = malloc(sblock.e2fs_bsize)) == NULL)
578                     err(8, "cannot allocate first block");
579           memcpy(firstblk, bp->b_un.b_buf, sblock.e2fs_bsize);
580           bp = getdirblk(newblk, sblock.e2fs_bsize);
581           if (bp->b_errs) {
582                     free(firstblk);
583                     goto bad;
584           }
585           memcpy(bp->b_un.b_buf, firstblk, sblock.e2fs_bsize);
586           free(firstblk);
587           dirty(bp);
588           bp = getdirblk(fs2h32(dp->e2di_blocks[lastbn + 1]),
589                     sblock.e2fs_bsize);
590           if (bp->b_errs)
591                     goto bad;
592           emptydir.dot_reclen = h2fs16(sblock.e2fs_bsize);
593           memcpy(bp->b_un.b_buf, &emptydir, sizeof emptydir);
594           pwarn("NO SPACE LEFT IN %s", name);
595           if (preen)
596                     printf(" (EXPANDED)\n");
597           else if (reply("EXPAND") == 0)
598                     goto bad;
599           dirty(bp);
600           inodirty();
601           return (1);
602 bad:
603           dp->e2di_blocks[lastbn] = dp->e2di_blocks[lastbn + 1];
604           dp->e2di_blocks[lastbn + 1] = 0;
605           inossize(dp, inosize(dp) - sblock.e2fs_bsize);
606           inosnblock(dp, inonblock(dp) - btodb(sblock.e2fs_bsize));
607           freeblk(newblk);
608           return (0);
609 }
610 
611 /*
612  * allocate a new directory
613  */
614 int
allocdir(ino_t parent,ino_t request,int mode)615 allocdir(ino_t parent, ino_t request, int mode)
616 {
617           ino_t ino;
618           struct ext2fs_dinode *dp;
619           struct bufarea *bp;
620           struct ext2fs_dirtemplate *dirp;
621 
622           ino = allocino(request, IFDIR|mode);
623           dirhead.dot_reclen = h2fs16(12); /* XXX */
624           dirhead.dotdot_reclen = h2fs16(sblock.e2fs_bsize - 12); /* XXX */
625           dirhead.dot_namlen = 1;
626           if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
627               (sblock.e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE))
628                     dirhead.dot_type = EXT2_FT_DIR;
629           else
630                     dirhead.dot_type = 0;
631           dirhead.dotdot_namlen = 2;
632           if (sblock.e2fs.e2fs_rev > E2FS_REV0 &&
633               (sblock.e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE))
634                     dirhead.dotdot_type = EXT2_FT_DIR;
635           else
636                     dirhead.dotdot_type = 0;
637           dirp = &dirhead;
638           dirp->dot_ino = h2fs32(ino);
639           dirp->dotdot_ino = h2fs32(parent);
640           dp = ginode(ino);
641           bp = getdirblk(fs2h32(dp->e2di_blocks[0]), sblock.e2fs_bsize);
642           if (bp->b_errs) {
643                     freeino(ino);
644                     return (0);
645           }
646           memcpy(bp->b_un.b_buf, dirp, sizeof(struct ext2fs_dirtemplate));
647           dirty(bp);
648           dp->e2di_nlink = h2fs16(2);
649           inodirty();
650           if (ino == EXT2_ROOTINO) {
651                     lncntp[ino] = fs2h16(dp->e2di_nlink);
652                     cacheino(dp, ino);
653                     return(ino);
654           }
655           if (statemap[parent] != DSTATE && statemap[parent] != DFOUND) {
656                     freeino(ino);
657                     return (0);
658           }
659           cacheino(dp, ino);
660           statemap[ino] = statemap[parent];
661           if (statemap[ino] == DSTATE) {
662                     lncntp[ino] = fs2h16(dp->e2di_nlink);
663                     lncntp[parent]++;
664           }
665           dp = ginode(parent);
666           dp->e2di_nlink = h2fs16(fs2h16(dp->e2di_nlink) + 1);
667           inodirty();
668           return (ino);
669 }
670 
671 /*
672  * free a directory inode
673  */
674 static void
freedir(ino_t ino,ino_t parent)675 freedir(ino_t ino, ino_t parent)
676 {
677           struct ext2fs_dinode *dp;
678 
679           if (ino != parent) {
680                     dp = ginode(parent);
681                     dp->e2di_nlink = h2fs16(fs2h16(dp->e2di_nlink) - 1);
682                     inodirty();
683           }
684           freeino(ino);
685 }
686 
687 /*
688  * generate a temporary name for the lost+found directory.
689  */
690 static int
lftempname(char * bufp,ino_t ino)691 lftempname(char *bufp, ino_t ino)
692 {
693           ino_t in;
694           char *cp;
695           int namlen;
696 
697           cp = bufp + 2;
698           for (in = maxino; in > 0; in /= 10)
699                     cp++;
700           *--cp = 0;
701           namlen = cp - bufp;
702           in = ino;
703           while (cp > bufp) {
704                     *--cp = (in % 10) + '0';
705                     in /= 10;
706           }
707           *cp = '#';
708           return (namlen);
709 }
710 
711 /*
712  * Get a directory block.
713  * Insure that it is held until another is requested.
714  */
715 static struct bufarea *
getdirblk(daddr_t blkno,long size)716 getdirblk(daddr_t blkno, long size)
717 {
718 
719           if (pdirbp != 0)
720                     pdirbp->b_flags &= ~B_INUSE;
721           pdirbp = getdatablk(blkno, size);
722           return (pdirbp);
723 }
724