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