dir.c revision 1.11 1 1.11 agc /* $NetBSD: dir.c,v 1.11 2003/08/07 10:04:22 agc 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.8 perseant bread(vp, idesc->id_blkno, 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.8 perseant bread(vp, idesc->id_blkno, 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.8 perseant struct inode *ip;
287 1.1 perseant
288 1.1 perseant pwarn("%s ", errmesg);
289 1.1 perseant pinode(ino);
290 1.1 perseant printf("\n");
291 1.10 itojun getpathname(pathbuf, sizeof(pathbuf), cwd, ino);
292 1.4 perseant if (ino < ROOTINO || ino >= maxino) {
293 1.1 perseant pfatal("NAME=%s\n", pathbuf);
294 1.1 perseant return;
295 1.1 perseant }
296 1.8 perseant vp = vget(fs, ino);
297 1.8 perseant ip = VTOI(vp);
298 1.8 perseant if (vp == NULL)
299 1.3 perseant pfatal("INO is NULL\n");
300 1.3 perseant else {
301 1.8 perseant if (ftypeok(VTOD(vp)))
302 1.3 perseant pfatal("%s=%s\n",
303 1.9 fvdl (ip->i_ffs1_mode & IFMT) == IFDIR ?
304 1.8 perseant "DIR" : "FILE", pathbuf);
305 1.3 perseant else
306 1.3 perseant pfatal("NAME=%s\n", pathbuf);
307 1.3 perseant }
308 1.1 perseant }
309 1.1 perseant
310 1.1 perseant void
311 1.8 perseant adjust(struct inodesc *idesc, short lcnt)
312 1.1 perseant {
313 1.8 perseant struct uvnode *vp;
314 1.9 fvdl struct ufs1_dinode *dp;
315 1.1 perseant
316 1.8 perseant vp = vget(fs, idesc->id_number);
317 1.8 perseant dp = VTOD(vp);
318 1.1 perseant if (dp->di_nlink == lcnt) {
319 1.8 perseant if (linkup(idesc->id_number, (ino_t) 0) == 0)
320 1.1 perseant clri(idesc, "UNREF", 0);
321 1.1 perseant } else {
322 1.1 perseant pwarn("LINK COUNT %s", (lfdir == idesc->id_number) ? lfname :
323 1.8 perseant ((dp->di_mode & IFMT) == IFDIR ? "DIR" : "FILE"));
324 1.1 perseant pinode(idesc->id_number);
325 1.1 perseant printf(" COUNT %d SHOULD BE %d",
326 1.8 perseant dp->di_nlink, dp->di_nlink - lcnt);
327 1.1 perseant if (preen) {
328 1.1 perseant if (lcnt < 0) {
329 1.1 perseant printf("\n");
330 1.1 perseant pfatal("LINK COUNT INCREASING");
331 1.1 perseant }
332 1.1 perseant printf(" (ADJUSTED)\n");
333 1.1 perseant }
334 1.1 perseant if (preen || reply("ADJUST") == 1) {
335 1.1 perseant dp->di_nlink -= lcnt;
336 1.8 perseant inodirty(VTOI(vp));
337 1.1 perseant }
338 1.1 perseant }
339 1.1 perseant }
340 1.1 perseant
341 1.1 perseant static int
342 1.8 perseant mkentry(struct inodesc *idesc)
343 1.1 perseant {
344 1.8 perseant struct direct *dirp = idesc->id_dirp;
345 1.8 perseant struct direct newent;
346 1.8 perseant int newlen, oldlen;
347 1.1 perseant
348 1.1 perseant newent.d_namlen = strlen(idesc->id_name);
349 1.1 perseant newlen = DIRSIZ(0, &newent, 0);
350 1.1 perseant if (dirp->d_ino != 0)
351 1.1 perseant oldlen = DIRSIZ(0, dirp, 0);
352 1.1 perseant else
353 1.1 perseant oldlen = 0;
354 1.1 perseant if (dirp->d_reclen - oldlen < newlen)
355 1.1 perseant return (KEEPON);
356 1.1 perseant newent.d_reclen = dirp->d_reclen - oldlen;
357 1.1 perseant dirp->d_reclen = oldlen;
358 1.8 perseant dirp = (struct direct *) (((char *) dirp) + oldlen);
359 1.1 perseant dirp->d_ino = idesc->id_parent; /* ino to be entered is in id_parent */
360 1.1 perseant dirp->d_reclen = newent.d_reclen;
361 1.8 perseant dirp->d_type = typemap[idesc->id_parent];
362 1.1 perseant dirp->d_namlen = newent.d_namlen;
363 1.8 perseant memcpy(dirp->d_name, idesc->id_name, (size_t) dirp->d_namlen + 1);
364 1.3 perseant return (ALTERED | STOP);
365 1.1 perseant }
366 1.1 perseant
367 1.1 perseant static int
368 1.8 perseant chgino(struct inodesc *idesc)
369 1.1 perseant {
370 1.8 perseant struct direct *dirp = idesc->id_dirp;
371 1.1 perseant
372 1.8 perseant if (memcmp(dirp->d_name, idesc->id_name, (int) dirp->d_namlen + 1))
373 1.1 perseant return (KEEPON);
374 1.1 perseant dirp->d_ino = idesc->id_parent;
375 1.8 perseant dirp->d_type = typemap[idesc->id_parent];
376 1.3 perseant return (ALTERED | STOP);
377 1.1 perseant }
378 1.1 perseant
379 1.1 perseant int
380 1.3 perseant linkup(ino_t orphan, ino_t parentdir)
381 1.1 perseant {
382 1.9 fvdl struct ufs1_dinode *dp;
383 1.8 perseant int lostdir;
384 1.8 perseant ino_t oldlfdir;
385 1.8 perseant struct inodesc idesc;
386 1.8 perseant char tempname[BUFSIZ];
387 1.8 perseant struct uvnode *vp;
388 1.1 perseant
389 1.1 perseant memset(&idesc, 0, sizeof(struct inodesc));
390 1.8 perseant vp = vget(fs, orphan);
391 1.8 perseant dp = VTOD(vp);
392 1.1 perseant lostdir = (dp->di_mode & IFMT) == IFDIR;
393 1.1 perseant pwarn("UNREF %s ", lostdir ? "DIR" : "FILE");
394 1.1 perseant pinode(orphan);
395 1.1 perseant if (preen && dp->di_size == 0)
396 1.1 perseant return (0);
397 1.1 perseant if (preen)
398 1.1 perseant printf(" (RECONNECTED)\n");
399 1.3 perseant else if (reply("RECONNECT") == 0)
400 1.3 perseant return (0);
401 1.1 perseant if (lfdir == 0) {
402 1.1 perseant dp = ginode(ROOTINO);
403 1.1 perseant idesc.id_name = lfname;
404 1.1 perseant idesc.id_type = DATA;
405 1.1 perseant idesc.id_func = findino;
406 1.1 perseant idesc.id_number = ROOTINO;
407 1.1 perseant if ((ckinode(dp, &idesc) & FOUND) != 0) {
408 1.1 perseant lfdir = idesc.id_parent;
409 1.1 perseant } else {
410 1.1 perseant pwarn("NO lost+found DIRECTORY");
411 1.1 perseant if (preen || reply("CREATE")) {
412 1.8 perseant lfdir = allocdir(ROOTINO, (ino_t) 0, lfmode);
413 1.1 perseant if (lfdir != 0) {
414 1.1 perseant if (makeentry(ROOTINO, lfdir, lfname) != 0) {
415 1.1 perseant if (preen)
416 1.1 perseant printf(" (CREATED)\n");
417 1.1 perseant } else {
418 1.1 perseant freedir(lfdir, ROOTINO);
419 1.1 perseant lfdir = 0;
420 1.1 perseant if (preen)
421 1.1 perseant printf("\n");
422 1.1 perseant }
423 1.1 perseant }
424 1.1 perseant }
425 1.1 perseant }
426 1.1 perseant if (lfdir == 0) {
427 1.1 perseant pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY");
428 1.1 perseant printf("\n\n");
429 1.1 perseant return (0);
430 1.1 perseant }
431 1.1 perseant }
432 1.8 perseant vp = vget(fs, lfdir);
433 1.8 perseant dp = VTOD(vp);
434 1.1 perseant if ((dp->di_mode & IFMT) != IFDIR) {
435 1.1 perseant pfatal("lost+found IS NOT A DIRECTORY");
436 1.1 perseant if (reply("REALLOCATE") == 0)
437 1.1 perseant return (0);
438 1.1 perseant oldlfdir = lfdir;
439 1.8 perseant if ((lfdir = allocdir(ROOTINO, (ino_t) 0, lfmode)) == 0) {
440 1.1 perseant pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
441 1.1 perseant return (0);
442 1.1 perseant }
443 1.1 perseant if ((changeino(ROOTINO, lfname, lfdir) & ALTERED) == 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.8 perseant inodirty(VTOI(vp));
448 1.1 perseant idesc.id_type = ADDR;
449 1.1 perseant idesc.id_func = pass4check;
450 1.1 perseant idesc.id_number = oldlfdir;
451 1.1 perseant adjust(&idesc, lncntp[oldlfdir] + 1);
452 1.1 perseant lncntp[oldlfdir] = 0;
453 1.8 perseant vp = vget(fs, lfdir);
454 1.8 perseant dp = VTOD(vp);
455 1.1 perseant }
456 1.1 perseant if (statemap[lfdir] != DFOUND) {
457 1.1 perseant pfatal("SORRY. NO lost+found DIRECTORY\n\n");
458 1.1 perseant return (0);
459 1.1 perseant }
460 1.8 perseant (void) lftempname(tempname, orphan);
461 1.1 perseant if (makeentry(lfdir, orphan, tempname) == 0) {
462 1.1 perseant pfatal("SORRY. NO SPACE IN lost+found DIRECTORY");
463 1.1 perseant printf("\n\n");
464 1.1 perseant return (0);
465 1.1 perseant }
466 1.1 perseant lncntp[orphan]--;
467 1.1 perseant if (lostdir) {
468 1.1 perseant if ((changeino(orphan, "..", lfdir) & ALTERED) == 0 &&
469 1.8 perseant parentdir != (ino_t) - 1)
470 1.8 perseant (void) makeentry(orphan, lfdir, "..");
471 1.8 perseant vp = vget(fs, lfdir);
472 1.9 fvdl VTOI(vp)->i_ffs1_nlink++;
473 1.8 perseant inodirty(VTOI(vp));
474 1.1 perseant lncntp[lfdir]++;
475 1.1 perseant pwarn("DIR I=%u CONNECTED. ", orphan);
476 1.8 perseant if (parentdir != (ino_t) - 1)
477 1.1 perseant printf("PARENT WAS I=%u\n", parentdir);
478 1.1 perseant if (preen == 0)
479 1.1 perseant printf("\n");
480 1.1 perseant }
481 1.1 perseant return (1);
482 1.1 perseant }
483 1.1 perseant
484 1.1 perseant /*
485 1.1 perseant * fix an entry in a directory.
486 1.1 perseant */
487 1.1 perseant int
488 1.3 perseant changeino(ino_t dir, char *name, ino_t newnum)
489 1.1 perseant {
490 1.8 perseant struct inodesc idesc;
491 1.1 perseant
492 1.1 perseant memset(&idesc, 0, sizeof(struct inodesc));
493 1.1 perseant idesc.id_type = DATA;
494 1.1 perseant idesc.id_func = chgino;
495 1.1 perseant idesc.id_number = dir;
496 1.1 perseant idesc.id_fix = DONTKNOW;
497 1.1 perseant idesc.id_name = name;
498 1.1 perseant idesc.id_parent = newnum; /* new value for name */
499 1.8 perseant
500 1.1 perseant return (ckinode(ginode(dir), &idesc));
501 1.1 perseant }
502 1.1 perseant
503 1.1 perseant /*
504 1.1 perseant * make an entry in a directory
505 1.1 perseant */
506 1.1 perseant int
507 1.3 perseant makeentry(ino_t parent, ino_t ino, char *name)
508 1.3 perseant {
509 1.9 fvdl struct ufs1_dinode *dp;
510 1.8 perseant struct inodesc idesc;
511 1.8 perseant char pathbuf[MAXPATHLEN + 1];
512 1.8 perseant struct uvnode *vp;
513 1.3 perseant
514 1.1 perseant if (parent < ROOTINO || parent >= maxino ||
515 1.1 perseant ino < ROOTINO || ino >= maxino)
516 1.1 perseant return (0);
517 1.1 perseant memset(&idesc, 0, sizeof(struct inodesc));
518 1.1 perseant idesc.id_type = DATA;
519 1.1 perseant idesc.id_func = mkentry;
520 1.1 perseant idesc.id_number = parent;
521 1.1 perseant idesc.id_parent = ino; /* this is the inode to enter */
522 1.1 perseant idesc.id_fix = DONTKNOW;
523 1.1 perseant idesc.id_name = name;
524 1.8 perseant vp = vget(fs, parent);
525 1.8 perseant dp = VTOD(vp);
526 1.1 perseant if (dp->di_size % DIRBLKSIZ) {
527 1.1 perseant dp->di_size = roundup(dp->di_size, DIRBLKSIZ);
528 1.8 perseant inodirty(VTOI(vp));
529 1.1 perseant }
530 1.1 perseant if ((ckinode(dp, &idesc) & ALTERED) != 0)
531 1.1 perseant return (1);
532 1.10 itojun getpathname(pathbuf, sizeof(pathbuf), parent, parent);
533 1.8 perseant vp = vget(fs, parent);
534 1.8 perseant dp = VTOD(vp);
535 1.8 perseant if (expanddir(vp, dp, pathbuf) == 0)
536 1.1 perseant return (0);
537 1.1 perseant return (ckinode(dp, &idesc) & ALTERED);
538 1.1 perseant }
539 1.1 perseant
540 1.1 perseant /*
541 1.1 perseant * Attempt to expand the size of a directory
542 1.1 perseant */
543 1.1 perseant static int
544 1.9 fvdl expanddir(struct uvnode *vp, struct ufs1_dinode *dp, char *name)
545 1.1 perseant {
546 1.8 perseant daddr_t lastbn, newblk;
547 1.8 perseant struct ubuf *bp;
548 1.8 perseant char *cp, firstblk[DIRBLKSIZ];
549 1.1 perseant
550 1.8 perseant lastbn = lblkno(fs, dp->di_size);
551 1.1 perseant if (lastbn >= NDADDR - 1 || dp->di_db[lastbn] == 0 || dp->di_size == 0)
552 1.1 perseant return (0);
553 1.1 perseant dp->di_db[lastbn + 1] = dp->di_db[lastbn];
554 1.8 perseant dp->di_db[lastbn] = 0;
555 1.8 perseant bp = getblk(vp, lastbn, fs->lfs_bsize);
556 1.8 perseant VOP_BWRITE(bp);
557 1.8 perseant dp->di_size += fs->lfs_bsize;
558 1.8 perseant dp->di_blocks += btofsb(fs, fs->lfs_bsize);
559 1.8 perseant bread(vp, dp->di_db[lastbn + 1],
560 1.8 perseant (long) dblksize(fs, dp, lastbn + 1), NOCRED, &bp);
561 1.8 perseant if (bp->b_flags & B_ERROR)
562 1.1 perseant goto bad;
563 1.8 perseant memcpy(firstblk, bp->b_data, DIRBLKSIZ);
564 1.8 perseant bread(vp, newblk, fs->lfs_bsize, NOCRED, &bp);
565 1.8 perseant if (bp->b_flags & B_ERROR)
566 1.1 perseant goto bad;
567 1.8 perseant memcpy(bp->b_data, firstblk, DIRBLKSIZ);
568 1.8 perseant for (cp = &bp->b_data[DIRBLKSIZ];
569 1.8 perseant cp < &bp->b_data[fs->lfs_bsize];
570 1.8 perseant cp += DIRBLKSIZ)
571 1.1 perseant memcpy(cp, &emptydir, sizeof emptydir);
572 1.8 perseant VOP_BWRITE(bp);
573 1.8 perseant bread(vp, dp->di_db[lastbn + 1],
574 1.8 perseant (long) dblksize(fs, dp, lastbn + 1), NOCRED, &bp);
575 1.8 perseant if (bp->b_flags & B_ERROR)
576 1.1 perseant goto bad;
577 1.8 perseant memcpy(bp->b_data, &emptydir, sizeof emptydir);
578 1.1 perseant pwarn("NO SPACE LEFT IN %s", name);
579 1.1 perseant if (preen)
580 1.1 perseant printf(" (EXPANDED)\n");
581 1.1 perseant else if (reply("EXPAND") == 0)
582 1.1 perseant goto bad;
583 1.8 perseant VOP_BWRITE(bp);
584 1.8 perseant inodirty(VTOI(vp));
585 1.1 perseant return (1);
586 1.1 perseant bad:
587 1.1 perseant dp->di_db[lastbn] = dp->di_db[lastbn + 1];
588 1.1 perseant dp->di_db[lastbn + 1] = 0;
589 1.8 perseant dp->di_size -= fs->lfs_bsize;
590 1.8 perseant dp->di_blocks -= btofsb(fs, fs->lfs_bsize);
591 1.8 perseant freeblk(newblk, fs->lfs_frag);
592 1.1 perseant return (0);
593 1.1 perseant }
594 1.1 perseant
595 1.1 perseant /*
596 1.1 perseant * allocate a new directory
597 1.1 perseant */
598 1.1 perseant int
599 1.3 perseant allocdir(ino_t parent, ino_t request, int mode)
600 1.3 perseant {
601 1.8 perseant ino_t ino;
602 1.8 perseant char *cp;
603 1.9 fvdl struct ufs1_dinode *dp;
604 1.8 perseant struct ubuf *bp;
605 1.1 perseant struct dirtemplate *dirp;
606 1.8 perseant struct uvnode *vp;
607 1.1 perseant
608 1.3 perseant ino = allocino(request, IFDIR | mode);
609 1.8 perseant dirp = &dirhead;
610 1.1 perseant dirp->dot_ino = ino;
611 1.1 perseant dirp->dotdot_ino = parent;
612 1.8 perseant vp = vget(fs, ino);
613 1.8 perseant dp = VTOD(vp);
614 1.8 perseant bread(vp, dp->di_db[0], fs->lfs_fsize, NOCRED, &bp);
615 1.8 perseant if (bp->b_flags & B_ERROR) {
616 1.8 perseant brelse(bp);
617 1.1 perseant freeino(ino);
618 1.1 perseant return (0);
619 1.1 perseant }
620 1.8 perseant memcpy(bp->b_data, dirp, sizeof(struct dirtemplate));
621 1.8 perseant for (cp = &bp->b_data[DIRBLKSIZ];
622 1.8 perseant cp < &bp->b_data[fs->lfs_fsize];
623 1.8 perseant cp += DIRBLKSIZ)
624 1.1 perseant memcpy(cp, &emptydir, sizeof emptydir);
625 1.8 perseant VOP_BWRITE(bp);
626 1.1 perseant dp->di_nlink = 2;
627 1.8 perseant inodirty(VTOI(vp));
628 1.1 perseant if (ino == ROOTINO) {
629 1.1 perseant lncntp[ino] = dp->di_nlink;
630 1.1 perseant cacheino(dp, ino);
631 1.3 perseant return (ino);
632 1.1 perseant }
633 1.1 perseant if (statemap[parent] != DSTATE && statemap[parent] != DFOUND) {
634 1.1 perseant freeino(ino);
635 1.1 perseant return (0);
636 1.1 perseant }
637 1.1 perseant cacheino(dp, ino);
638 1.1 perseant statemap[ino] = statemap[parent];
639 1.1 perseant if (statemap[ino] == DSTATE) {
640 1.1 perseant lncntp[ino] = dp->di_nlink;
641 1.1 perseant lncntp[parent]++;
642 1.1 perseant }
643 1.8 perseant vp = vget(fs, parent);
644 1.8 perseant dp = VTOD(vp);
645 1.1 perseant dp->di_nlink++;
646 1.8 perseant inodirty(VTOI(vp));
647 1.1 perseant return (ino);
648 1.1 perseant }
649 1.1 perseant
650 1.1 perseant /*
651 1.1 perseant * free a directory inode
652 1.1 perseant */
653 1.1 perseant static void
654 1.3 perseant freedir(ino_t ino, ino_t parent)
655 1.1 perseant {
656 1.8 perseant struct uvnode *vp;
657 1.1 perseant
658 1.1 perseant if (ino != parent) {
659 1.8 perseant vp = vget(fs, parent);
660 1.9 fvdl VTOI(vp)->i_ffs1_nlink--;
661 1.8 perseant inodirty(VTOI(vp));
662 1.1 perseant }
663 1.1 perseant freeino(ino);
664 1.1 perseant }
665 1.1 perseant
666 1.1 perseant /*
667 1.1 perseant * generate a temporary name for the lost+found directory.
668 1.1 perseant */
669 1.1 perseant static int
670 1.3 perseant lftempname(char *bufp, ino_t ino)
671 1.3 perseant {
672 1.8 perseant ino_t in;
673 1.8 perseant char *cp;
674 1.8 perseant int namlen;
675 1.1 perseant
676 1.1 perseant cp = bufp + 2;
677 1.1 perseant for (in = maxino; in > 0; in /= 10)
678 1.1 perseant cp++;
679 1.1 perseant *--cp = 0;
680 1.1 perseant namlen = cp - bufp;
681 1.1 perseant in = ino;
682 1.1 perseant while (cp > bufp) {
683 1.1 perseant *--cp = (in % 10) + '0';
684 1.1 perseant in /= 10;
685 1.1 perseant }
686 1.1 perseant *cp = '#';
687 1.1 perseant return (namlen);
688 1.1 perseant }
689