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