inode.c revision 1.56 1 1.56 christos /* $NetBSD: inode.c,v 1.56 2005/08/19 02:07:19 christos Exp $ */
2 1.13 cgd
3 1.1 cgd /*
4 1.9 mycroft * Copyright (c) 1980, 1986, 1993
5 1.9 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.40 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 cgd * may be used to endorse or promote products derived from this software
17 1.1 cgd * without specific prior written permission.
18 1.1 cgd *
19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 cgd * SUCH DAMAGE.
30 1.1 cgd */
31 1.1 cgd
32 1.24 lukem #include <sys/cdefs.h>
33 1.1 cgd #ifndef lint
34 1.13 cgd #if 0
35 1.26 lukem static char sccsid[] = "@(#)inode.c 8.8 (Berkeley) 4/28/95";
36 1.13 cgd #else
37 1.56 christos __RCSID("$NetBSD: inode.c,v 1.56 2005/08/19 02:07:19 christos Exp $");
38 1.13 cgd #endif
39 1.1 cgd #endif /* not lint */
40 1.1 cgd
41 1.1 cgd #include <sys/param.h>
42 1.7 cgd #include <sys/time.h>
43 1.26 lukem
44 1.9 mycroft #include <ufs/ufs/dinode.h>
45 1.9 mycroft #include <ufs/ufs/dir.h>
46 1.9 mycroft #include <ufs/ffs/fs.h>
47 1.27 bouyer #include <ufs/ffs/ffs_extern.h>
48 1.32 fvdl #include <ufs/ufs/ufs_bswap.h>
49 1.28 kleink
50 1.6 cgd #ifndef SMALL
51 1.26 lukem #include <err.h>
52 1.1 cgd #include <pwd.h>
53 1.6 cgd #endif
54 1.12 cgd #include <stdio.h>
55 1.1 cgd #include <stdlib.h>
56 1.1 cgd #include <string.h>
57 1.28 kleink #include <time.h>
58 1.12 cgd
59 1.1 cgd #include "fsck.h"
60 1.22 christos #include "fsutil.h"
61 1.12 cgd #include "extern.h"
62 1.1 cgd
63 1.1 cgd static ino_t startinum;
64 1.1 cgd
65 1.51 xtraeme static int iblock(struct inodesc *, long, u_int64_t);
66 1.37 fvdl static void swap_dinode1(union dinode *, int);
67 1.37 fvdl static void swap_dinode2(union dinode *, int);
68 1.12 cgd
69 1.12 cgd int
70 1.50 xtraeme ckinode(union dinode *dp, struct inodesc *idesc)
71 1.1 cgd {
72 1.37 fvdl int ret, offset, i;
73 1.37 fvdl union dinode dino;
74 1.27 bouyer u_int64_t sizepb;
75 1.27 bouyer int64_t remsize;
76 1.37 fvdl daddr_t ndb;
77 1.9 mycroft mode_t mode;
78 1.23 thorpej char pathbuf[MAXPATHLEN + 1];
79 1.1 cgd
80 1.1 cgd if (idesc->id_fix != IGNORE)
81 1.1 cgd idesc->id_fix = DONTKNOW;
82 1.1 cgd idesc->id_entryno = 0;
83 1.37 fvdl idesc->id_filesize = iswap64(DIP(dp, size));
84 1.37 fvdl mode = iswap16(DIP(dp, mode)) & IFMT;
85 1.9 mycroft if (mode == IFBLK || mode == IFCHR || (mode == IFLNK &&
86 1.27 bouyer (idesc->id_filesize < sblock->fs_maxsymlinklen ||
87 1.35 dbj (isappleufs && (idesc->id_filesize < APPLEUFS_MAXSYMLINKLEN)) ||
88 1.37 fvdl (sblock->fs_maxsymlinklen == 0 && DIP(dp, blocks) == 0))))
89 1.1 cgd return (KEEPON);
90 1.37 fvdl if (is_ufs2)
91 1.37 fvdl dino.dp2 = dp->dp2;
92 1.37 fvdl else
93 1.37 fvdl dino.dp1 = dp->dp1;
94 1.37 fvdl ndb = howmany(iswap64(DIP(&dino, size)), sblock->fs_bsize);
95 1.37 fvdl for (i = 0; i < NDADDR; i++) {
96 1.27 bouyer if (--ndb == 0 &&
97 1.37 fvdl (offset = blkoff(sblock, iswap64(DIP(&dino, size)))) != 0)
98 1.1 cgd idesc->id_numfrags =
99 1.27 bouyer numfrags(sblock, fragroundup(sblock, offset));
100 1.1 cgd else
101 1.27 bouyer idesc->id_numfrags = sblock->fs_frag;
102 1.37 fvdl if (DIP(&dino, db[i]) == 0) {
103 1.23 thorpej if (idesc->id_type == DATA && ndb >= 0) {
104 1.23 thorpej /* An empty block in a directory XXX */
105 1.27 bouyer markclean = 0;
106 1.39 itojun getpathname(pathbuf, sizeof(pathbuf),
107 1.39 itojun idesc->id_number, idesc->id_number);
108 1.53 christos pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
109 1.23 thorpej pathbuf);
110 1.23 thorpej if (reply("ADJUST LENGTH") == 1) {
111 1.23 thorpej dp = ginode(idesc->id_number);
112 1.37 fvdl DIP(dp, size) = iswap64(i *
113 1.27 bouyer sblock->fs_bsize);
114 1.23 thorpej printf(
115 1.23 thorpej "YOU MUST RERUN FSCK AFTERWARDS\n");
116 1.23 thorpej rerun = 1;
117 1.23 thorpej inodirty();
118 1.23 thorpej }
119 1.23 thorpej }
120 1.1 cgd continue;
121 1.23 thorpej }
122 1.37 fvdl if (is_ufs2)
123 1.37 fvdl idesc->id_blkno = iswap64(dino.dp2.di_db[i]);
124 1.37 fvdl else
125 1.37 fvdl idesc->id_blkno = iswap32(dino.dp1.di_db[i]);
126 1.47 hannken if (idesc->id_type != DATA)
127 1.1 cgd ret = (*idesc->id_func)(idesc);
128 1.1 cgd else
129 1.1 cgd ret = dirscan(idesc);
130 1.1 cgd if (ret & STOP)
131 1.1 cgd return (ret);
132 1.1 cgd }
133 1.27 bouyer idesc->id_numfrags = sblock->fs_frag;
134 1.37 fvdl remsize = iswap64(DIP(&dino, size)) - sblock->fs_bsize * NDADDR;
135 1.27 bouyer sizepb = sblock->fs_bsize;
136 1.37 fvdl for (i = 0; i < NIADDR; i++) {
137 1.37 fvdl if (DIP(&dino, ib[i])) {
138 1.37 fvdl if (is_ufs2)
139 1.37 fvdl idesc->id_blkno = iswap64(dino.dp2.di_ib[i]);
140 1.37 fvdl else
141 1.37 fvdl idesc->id_blkno = iswap32(dino.dp1.di_ib[i]);
142 1.37 fvdl ret = iblock(idesc, i + 1, remsize);
143 1.1 cgd if (ret & STOP)
144 1.1 cgd return (ret);
145 1.23 thorpej } else {
146 1.23 thorpej if (idesc->id_type == DATA && remsize > 0) {
147 1.23 thorpej /* An empty block in a directory XXX */
148 1.27 bouyer markclean = 0;
149 1.39 itojun getpathname(pathbuf, sizeof(pathbuf),
150 1.39 itojun idesc->id_number, idesc->id_number);
151 1.23 thorpej pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
152 1.23 thorpej pathbuf);
153 1.23 thorpej if (reply("ADJUST LENGTH") == 1) {
154 1.23 thorpej dp = ginode(idesc->id_number);
155 1.37 fvdl DIP(dp, size) =
156 1.37 fvdl iswap64(iswap64(DIP(dp, size))
157 1.37 fvdl - remsize);
158 1.23 thorpej remsize = 0;
159 1.23 thorpej printf(
160 1.23 thorpej "YOU MUST RERUN FSCK AFTERWARDS\n");
161 1.23 thorpej rerun = 1;
162 1.23 thorpej inodirty();
163 1.23 thorpej break;
164 1.23 thorpej }
165 1.23 thorpej }
166 1.1 cgd }
167 1.27 bouyer sizepb *= NINDIR(sblock);
168 1.9 mycroft remsize -= sizepb;
169 1.1 cgd }
170 1.1 cgd return (KEEPON);
171 1.1 cgd }
172 1.1 cgd
173 1.21 christos static int
174 1.50 xtraeme iblock(struct inodesc *idesc, long ilevel, u_int64_t isize)
175 1.1 cgd {
176 1.24 lukem struct bufarea *bp;
177 1.50 xtraeme int i, n, (*func) (struct inodesc *), nif;
178 1.19 mycroft u_int64_t sizepb;
179 1.1 cgd char buf[BUFSIZ];
180 1.23 thorpej char pathbuf[MAXPATHLEN + 1];
181 1.37 fvdl union dinode *dp;
182 1.1 cgd
183 1.47 hannken if (idesc->id_type != DATA) {
184 1.1 cgd func = idesc->id_func;
185 1.1 cgd if (((n = (*func)(idesc)) & KEEPON) == 0)
186 1.1 cgd return (n);
187 1.1 cgd } else
188 1.1 cgd func = dirscan;
189 1.1 cgd if (chkrange(idesc->id_blkno, idesc->id_numfrags))
190 1.1 cgd return (SKIP);
191 1.27 bouyer bp = getdatablk(idesc->id_blkno, sblock->fs_bsize);
192 1.1 cgd ilevel--;
193 1.27 bouyer for (sizepb = sblock->fs_bsize, i = 0; i < ilevel; i++)
194 1.27 bouyer sizepb *= NINDIR(sblock);
195 1.37 fvdl if (howmany(isize, sizepb) > NINDIR(sblock))
196 1.27 bouyer nif = NINDIR(sblock);
197 1.18 mycroft else
198 1.18 mycroft nif = howmany(isize, sizepb);
199 1.27 bouyer if (do_blkswap) { /* swap byte order of the whole blk */
200 1.37 fvdl if (is_ufs2) {
201 1.37 fvdl for (i = 0; i < nif; i++)
202 1.37 fvdl bp->b_un.b_indir2[i] =
203 1.37 fvdl bswap64(bp->b_un.b_indir2[i]);
204 1.37 fvdl } else {
205 1.37 fvdl for (i = 0; i < nif; i++)
206 1.37 fvdl bp->b_un.b_indir1[i] =
207 1.38 fvdl bswap32(bp->b_un.b_indir1[i]);
208 1.37 fvdl }
209 1.27 bouyer dirty(bp);
210 1.27 bouyer flush(fswritefd, bp);
211 1.27 bouyer }
212 1.27 bouyer if (idesc->id_func == pass1check && nif < NINDIR(sblock)) {
213 1.37 fvdl for (i = nif; i < NINDIR(sblock); i++) {
214 1.37 fvdl if (IBLK(bp, i) == 0)
215 1.1 cgd continue;
216 1.29 mycroft (void)snprintf(buf, sizeof(buf),
217 1.56 christos "PARTIALLY TRUNCATED INODE I=%llu",
218 1.56 christos (unsigned long long)idesc->id_number);
219 1.1 cgd if (dofix(idesc, buf)) {
220 1.37 fvdl IBLK(bp, i) = 0;
221 1.1 cgd dirty(bp);
222 1.27 bouyer } else
223 1.27 bouyer markclean= 0;
224 1.1 cgd }
225 1.1 cgd flush(fswritefd, bp);
226 1.1 cgd }
227 1.37 fvdl for (i = 0; i < nif; i++) {
228 1.37 fvdl if (IBLK(bp, i)) {
229 1.37 fvdl if (is_ufs2)
230 1.37 fvdl idesc->id_blkno = iswap64(bp->b_un.b_indir2[i]);
231 1.37 fvdl else
232 1.37 fvdl idesc->id_blkno = iswap32(bp->b_un.b_indir1[i]);
233 1.9 mycroft if (ilevel == 0)
234 1.9 mycroft n = (*func)(idesc);
235 1.1 cgd else
236 1.9 mycroft n = iblock(idesc, ilevel, isize);
237 1.1 cgd if (n & STOP) {
238 1.1 cgd bp->b_flags &= ~B_INUSE;
239 1.1 cgd return (n);
240 1.23 thorpej }
241 1.23 thorpej } else {
242 1.23 thorpej if (idesc->id_type == DATA && isize > 0) {
243 1.23 thorpej /* An empty block in a directory XXX */
244 1.27 bouyer markclean= 0;
245 1.39 itojun getpathname(pathbuf, sizeof(pathbuf),
246 1.39 itojun idesc->id_number, idesc->id_number);
247 1.23 thorpej pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
248 1.23 thorpej pathbuf);
249 1.23 thorpej if (reply("ADJUST LENGTH") == 1) {
250 1.23 thorpej dp = ginode(idesc->id_number);
251 1.37 fvdl DIP(dp, size) =
252 1.37 fvdl iswap64(iswap64(DIP(dp, size))
253 1.37 fvdl - isize);
254 1.23 thorpej isize = 0;
255 1.23 thorpej printf(
256 1.23 thorpej "YOU MUST RERUN FSCK AFTERWARDS\n");
257 1.23 thorpej rerun = 1;
258 1.23 thorpej inodirty();
259 1.23 thorpej bp->b_flags &= ~B_INUSE;
260 1.23 thorpej return(STOP);
261 1.23 thorpej }
262 1.1 cgd }
263 1.1 cgd }
264 1.9 mycroft isize -= sizepb;
265 1.1 cgd }
266 1.1 cgd bp->b_flags &= ~B_INUSE;
267 1.1 cgd return (KEEPON);
268 1.1 cgd }
269 1.1 cgd
270 1.1 cgd /*
271 1.1 cgd * Check that a block in a legal block number.
272 1.1 cgd * Return 0 if in range, 1 if out of range.
273 1.1 cgd */
274 1.12 cgd int
275 1.50 xtraeme chkrange(daddr_t blk, int cnt)
276 1.1 cgd {
277 1.24 lukem int c;
278 1.1 cgd
279 1.46 dbj if (cnt <= 0 || blk <= 0 || blk > maxfsblock ||
280 1.46 dbj cnt - 1 > maxfsblock - blk)
281 1.1 cgd return (1);
282 1.37 fvdl if (cnt > sblock->fs_frag ||
283 1.37 fvdl fragnum(sblock, blk) + cnt > sblock->fs_frag) {
284 1.37 fvdl if (debug)
285 1.37 fvdl printf("bad size: blk %lld, offset %d, size %d\n",
286 1.37 fvdl (long long)blk, (int)fragnum(sblock, blk), cnt);
287 1.37 fvdl }
288 1.27 bouyer c = dtog(sblock, blk);
289 1.27 bouyer if (blk < cgdmin(sblock, c)) {
290 1.27 bouyer if ((blk + cnt) > cgsblock(sblock, c)) {
291 1.1 cgd if (debug) {
292 1.36 fvdl printf("blk %lld < cgdmin %lld;",
293 1.36 fvdl (long long)blk,
294 1.36 fvdl (long long)cgdmin(sblock, c));
295 1.36 fvdl printf(" blk + cnt %lld > cgsbase %lld\n",
296 1.36 fvdl (long long)(blk + cnt),
297 1.36 fvdl (long long)cgsblock(sblock, c));
298 1.1 cgd }
299 1.1 cgd return (1);
300 1.1 cgd }
301 1.1 cgd } else {
302 1.27 bouyer if ((blk + cnt) > cgbase(sblock, c+1)) {
303 1.1 cgd if (debug) {
304 1.36 fvdl printf("blk %lld >= cgdmin %lld;",
305 1.36 fvdl (long long)blk,
306 1.36 fvdl (long long)cgdmin(sblock, c));
307 1.36 fvdl printf(" blk + cnt %lld > sblock->fs_fpg %d\n",
308 1.36 fvdl (long long)(blk+cnt), sblock->fs_fpg);
309 1.1 cgd }
310 1.1 cgd return (1);
311 1.1 cgd }
312 1.1 cgd }
313 1.1 cgd return (0);
314 1.1 cgd }
315 1.1 cgd
316 1.1 cgd /*
317 1.1 cgd * General purpose interface for reading inodes.
318 1.1 cgd */
319 1.37 fvdl union dinode *
320 1.50 xtraeme ginode(ino_t inumber)
321 1.1 cgd {
322 1.36 fvdl daddr_t iblk;
323 1.30 thorpej int blkoff;
324 1.1 cgd
325 1.1 cgd if (inumber < ROOTINO || inumber > maxino)
326 1.56 christos errx(EEXIT, "bad inode number %llu to ginode",
327 1.56 christos (unsigned long long)inumber);
328 1.1 cgd if (startinum == 0 ||
329 1.27 bouyer inumber < startinum || inumber >= startinum + INOPB(sblock)) {
330 1.27 bouyer iblk = ino_to_fsba(sblock, inumber);
331 1.1 cgd if (pbp != 0)
332 1.1 cgd pbp->b_flags &= ~B_INUSE;
333 1.27 bouyer pbp = getdatablk(iblk, sblock->fs_bsize);
334 1.27 bouyer startinum = (inumber / INOPB(sblock)) * INOPB(sblock);
335 1.1 cgd }
336 1.37 fvdl if (is_ufs2) {
337 1.37 fvdl blkoff = (inumber % INOPB(sblock)) * DINODE2_SIZE;
338 1.37 fvdl return ((union dinode *)((caddr_t)pbp->b_un.b_buf + blkoff));
339 1.37 fvdl }
340 1.37 fvdl blkoff = (inumber % INOPB(sblock)) * DINODE1_SIZE;
341 1.37 fvdl return ((union dinode *)((caddr_t)pbp->b_un.b_buf + blkoff));
342 1.37 fvdl }
343 1.37 fvdl
344 1.37 fvdl static void
345 1.37 fvdl swap_dinode1(union dinode *dp, int n)
346 1.37 fvdl {
347 1.37 fvdl int i, j;
348 1.37 fvdl struct ufs1_dinode *dp1;
349 1.45 dbj int32_t maxsymlinklen = sblock->fs_maxsymlinklen;
350 1.45 dbj if (isappleufs)
351 1.45 dbj maxsymlinklen = APPLEUFS_MAXSYMLINKLEN;
352 1.37 fvdl
353 1.37 fvdl dp1 = (struct ufs1_dinode *)&dp->dp1;
354 1.37 fvdl for (i = 0; i < n; i++, dp1++) {
355 1.37 fvdl ffs_dinode1_swap(dp1, dp1);
356 1.42 dbj if (((iswap16(dp1->di_mode) & IFMT) != IFLNK) ||
357 1.44 dbj doinglevel2 ||
358 1.45 dbj (maxsymlinklen < 0) ||
359 1.45 dbj (iswap64(dp1->di_size) > maxsymlinklen)) {
360 1.37 fvdl for (j = 0; j < (NDADDR + NIADDR); j++)
361 1.37 fvdl dp1->di_db[j] = bswap32(dp1->di_db[j]);
362 1.37 fvdl }
363 1.37 fvdl }
364 1.37 fvdl }
365 1.37 fvdl
366 1.37 fvdl static void
367 1.37 fvdl swap_dinode2(union dinode *dp, int n)
368 1.37 fvdl {
369 1.37 fvdl int i, j;
370 1.37 fvdl struct ufs2_dinode *dp2;
371 1.37 fvdl
372 1.37 fvdl dp2 = (struct ufs2_dinode *)&dp->dp2;
373 1.37 fvdl for (i = 0; i < n; i++, dp2++) {
374 1.37 fvdl ffs_dinode2_swap(dp2, dp2);
375 1.37 fvdl if ((iswap16(dp2->di_mode) & IFMT) != IFLNK) {
376 1.37 fvdl for (j = 0; j < (NDADDR + NIADDR + NXADDR); j++)
377 1.37 fvdl dp2->di_extb[j] = bswap64(dp2->di_extb[j]);
378 1.37 fvdl }
379 1.37 fvdl }
380 1.1 cgd }
381 1.1 cgd
382 1.1 cgd /*
383 1.1 cgd * Special purpose version of ginode used to optimize first pass
384 1.1 cgd * over all the inodes in numerical order.
385 1.1 cgd */
386 1.37 fvdl ino_t nextino, lastinum, lastvalidinum;
387 1.1 cgd long readcnt, readpercg, fullcnt, inobufsize, partialcnt, partialsize;
388 1.37 fvdl union dinode *inodebuf;
389 1.1 cgd
390 1.37 fvdl union dinode *
391 1.50 xtraeme getnextinode(ino_t inumber)
392 1.1 cgd {
393 1.1 cgd long size;
394 1.36 fvdl daddr_t dblk;
395 1.37 fvdl static union dinode *dp;
396 1.37 fvdl union dinode *ret;
397 1.1 cgd
398 1.52 christos if (inumber != nextino++ || inumber > lastvalidinum)
399 1.56 christos errx(EEXIT, "bad inode number %llu to nextinode",
400 1.56 christos (unsigned long long)inumber);
401 1.37 fvdl
402 1.1 cgd if (inumber >= lastinum) {
403 1.1 cgd readcnt++;
404 1.27 bouyer dblk = fsbtodb(sblock, ino_to_fsba(sblock, lastinum));
405 1.1 cgd if (readcnt % readpercg == 0) {
406 1.1 cgd size = partialsize;
407 1.1 cgd lastinum += partialcnt;
408 1.1 cgd } else {
409 1.1 cgd size = inobufsize;
410 1.1 cgd lastinum += fullcnt;
411 1.1 cgd }
412 1.37 fvdl (void)bread(fsreadfd, (caddr_t)inodebuf, dblk, size);
413 1.27 bouyer if (doswap) {
414 1.37 fvdl if (is_ufs2)
415 1.37 fvdl swap_dinode2(inodebuf, lastinum - inumber);
416 1.37 fvdl else
417 1.37 fvdl swap_dinode1(inodebuf, lastinum - inumber);
418 1.27 bouyer bwrite(fswritefd, (char *)inodebuf, dblk, size);
419 1.27 bouyer }
420 1.37 fvdl dp = (union dinode *)inodebuf;
421 1.1 cgd }
422 1.37 fvdl ret = dp;
423 1.37 fvdl dp = (union dinode *)
424 1.37 fvdl ((char *)dp + (is_ufs2 ? DINODE2_SIZE : DINODE1_SIZE));
425 1.37 fvdl return ret;
426 1.1 cgd }
427 1.1 cgd
428 1.12 cgd void
429 1.50 xtraeme setinodebuf(ino_t inum)
430 1.1 cgd {
431 1.1 cgd
432 1.37 fvdl if (inum % sblock->fs_ipg != 0)
433 1.56 christos errx(EEXIT, "bad inode number %llu to setinodebuf",
434 1.56 christos (unsigned long long)inum);
435 1.37 fvdl
436 1.37 fvdl lastvalidinum = inum + sblock->fs_ipg - 1;
437 1.1 cgd startinum = 0;
438 1.37 fvdl nextino = inum;
439 1.37 fvdl lastinum = inum;
440 1.1 cgd readcnt = 0;
441 1.37 fvdl if (inodebuf != NULL)
442 1.37 fvdl return;
443 1.27 bouyer inobufsize = blkroundup(sblock, INOBUFSIZE);
444 1.37 fvdl fullcnt = inobufsize / (is_ufs2 ? DINODE2_SIZE : DINODE1_SIZE);
445 1.27 bouyer readpercg = sblock->fs_ipg / fullcnt;
446 1.27 bouyer partialcnt = sblock->fs_ipg % fullcnt;
447 1.37 fvdl partialsize = partialcnt * (is_ufs2 ? DINODE2_SIZE : DINODE1_SIZE);
448 1.1 cgd if (partialcnt != 0) {
449 1.1 cgd readpercg++;
450 1.1 cgd } else {
451 1.1 cgd partialcnt = fullcnt;
452 1.1 cgd partialsize = inobufsize;
453 1.1 cgd }
454 1.1 cgd if (inodebuf == NULL &&
455 1.37 fvdl (inodebuf = malloc((unsigned)inobufsize)) == NULL)
456 1.26 lukem errx(EEXIT, "Cannot allocate space for inode buffer");
457 1.1 cgd }
458 1.1 cgd
459 1.12 cgd void
460 1.50 xtraeme freeinodebuf(void)
461 1.1 cgd {
462 1.1 cgd
463 1.1 cgd if (inodebuf != NULL)
464 1.1 cgd free((char *)inodebuf);
465 1.1 cgd inodebuf = NULL;
466 1.1 cgd }
467 1.1 cgd
468 1.1 cgd /*
469 1.1 cgd * Routines to maintain information about directory inodes.
470 1.1 cgd * This is built during the first pass and used during the
471 1.1 cgd * second and third passes.
472 1.1 cgd *
473 1.1 cgd * Enter inodes into the cache.
474 1.1 cgd */
475 1.12 cgd void
476 1.50 xtraeme cacheino(union dinode *dp, ino_t inumber)
477 1.1 cgd {
478 1.24 lukem struct inoinfo *inp;
479 1.41 itojun struct inoinfo **inpp, **ninpsort;
480 1.37 fvdl unsigned int blks;
481 1.37 fvdl int i;
482 1.37 fvdl int64_t size;
483 1.1 cgd
484 1.37 fvdl size = iswap64(DIP(dp, size));
485 1.37 fvdl blks = howmany(size, sblock->fs_bsize);
486 1.1 cgd if (blks > NDADDR)
487 1.1 cgd blks = NDADDR + NIADDR;
488 1.37 fvdl
489 1.37 fvdl inp = (struct inoinfo *) malloc(sizeof(*inp) + (blks - 1)
490 1.37 fvdl * sizeof (int64_t));
491 1.1 cgd if (inp == NULL)
492 1.1 cgd return;
493 1.37 fvdl inpp = &inphead[inumber % dirhash];
494 1.1 cgd inp->i_nexthash = *inpp;
495 1.1 cgd *inpp = inp;
496 1.48 mycroft inp->i_child = inp->i_sibling = 0;
497 1.14 mycroft if (inumber == ROOTINO)
498 1.14 mycroft inp->i_parent = ROOTINO;
499 1.14 mycroft else
500 1.14 mycroft inp->i_parent = (ino_t)0;
501 1.1 cgd inp->i_dotdot = (ino_t)0;
502 1.1 cgd inp->i_number = inumber;
503 1.37 fvdl inp->i_isize = size;
504 1.37 fvdl inp->i_numblks = blks;
505 1.37 fvdl for (i = 0; i < (blks < NDADDR ? blks : NDADDR); i++)
506 1.37 fvdl inp->i_blks[i] = DIP(dp, db[i]);
507 1.37 fvdl if (blks > NDADDR)
508 1.37 fvdl for (i = 0; i < NIADDR; i++)
509 1.37 fvdl inp->i_blks[NDADDR + i] = DIP(dp, ib[i]);
510 1.1 cgd if (inplast == listmax) {
511 1.41 itojun ninpsort = (struct inoinfo **)realloc((char *)inpsort,
512 1.41 itojun (unsigned)(listmax + 100) * sizeof(struct inoinfo *));
513 1.1 cgd if (inpsort == NULL)
514 1.26 lukem errx(EEXIT, "cannot increase directory list");
515 1.41 itojun inpsort = ninpsort;
516 1.41 itojun listmax += 100;
517 1.1 cgd }
518 1.1 cgd inpsort[inplast++] = inp;
519 1.1 cgd }
520 1.1 cgd
521 1.1 cgd /*
522 1.1 cgd * Look up an inode cache structure.
523 1.1 cgd */
524 1.1 cgd struct inoinfo *
525 1.50 xtraeme getinoinfo(ino_t inumber)
526 1.1 cgd {
527 1.24 lukem struct inoinfo *inp;
528 1.1 cgd
529 1.37 fvdl for (inp = inphead[inumber % dirhash]; inp; inp = inp->i_nexthash) {
530 1.1 cgd if (inp->i_number != inumber)
531 1.1 cgd continue;
532 1.1 cgd return (inp);
533 1.1 cgd }
534 1.56 christos errx(EEXIT, "cannot find inode %llu", (unsigned long long)inumber);
535 1.1 cgd return ((struct inoinfo *)0);
536 1.1 cgd }
537 1.1 cgd
538 1.1 cgd /*
539 1.1 cgd * Clean up all the inode cache structure.
540 1.1 cgd */
541 1.12 cgd void
542 1.50 xtraeme inocleanup(void)
543 1.1 cgd {
544 1.24 lukem struct inoinfo **inpp;
545 1.1 cgd
546 1.1 cgd if (inphead == NULL)
547 1.1 cgd return;
548 1.1 cgd for (inpp = &inpsort[inplast - 1]; inpp >= inpsort; inpp--)
549 1.1 cgd free((char *)(*inpp));
550 1.1 cgd free((char *)inphead);
551 1.1 cgd free((char *)inpsort);
552 1.1 cgd inphead = inpsort = NULL;
553 1.1 cgd }
554 1.9 mycroft
555 1.12 cgd void
556 1.50 xtraeme inodirty(void)
557 1.1 cgd {
558 1.1 cgd
559 1.1 cgd dirty(pbp);
560 1.1 cgd }
561 1.1 cgd
562 1.12 cgd void
563 1.55 christos clri(struct inodesc *idesc, const char *type, int flag)
564 1.1 cgd {
565 1.37 fvdl union dinode *dp;
566 1.1 cgd
567 1.1 cgd dp = ginode(idesc->id_number);
568 1.1 cgd if (flag == 1) {
569 1.1 cgd pwarn("%s %s", type,
570 1.37 fvdl (iswap16(DIP(dp, mode)) & IFMT) == IFDIR ? "DIR" : "FILE");
571 1.1 cgd pinode(idesc->id_number);
572 1.1 cgd }
573 1.1 cgd if (preen || reply("CLEAR") == 1) {
574 1.1 cgd if (preen)
575 1.1 cgd printf(" (CLEARED)\n");
576 1.1 cgd n_files--;
577 1.1 cgd (void)ckinode(dp, idesc);
578 1.1 cgd clearinode(dp);
579 1.37 fvdl inoinfo(idesc->id_number)->ino_state = USTATE;
580 1.1 cgd inodirty();
581 1.27 bouyer } else
582 1.27 bouyer markclean= 0;
583 1.1 cgd }
584 1.1 cgd
585 1.12 cgd int
586 1.50 xtraeme findname(struct inodesc *idesc)
587 1.1 cgd {
588 1.24 lukem struct direct *dirp = idesc->id_dirp;
589 1.55 christos size_t len;
590 1.55 christos char *buf;
591 1.1 cgd
592 1.37 fvdl if (iswap32(dirp->d_ino) != idesc->id_parent || idesc->id_entryno < 2) {
593 1.37 fvdl idesc->id_entryno++;
594 1.1 cgd return (KEEPON);
595 1.37 fvdl }
596 1.55 christos if ((len = dirp->d_namlen + 1) > MAXPATHLEN) {
597 1.55 christos /* XXX: We don't fix but we ignore */
598 1.55 christos len = MAXPATHLEN;
599 1.55 christos }
600 1.55 christos /* this is namebuf from utilities.c */
601 1.55 christos buf = __UNCONST(idesc->id_name);
602 1.55 christos (void)memcpy(buf, dirp->d_name, (size_t)dirp->d_namlen + 1);
603 1.1 cgd return (STOP|FOUND);
604 1.1 cgd }
605 1.1 cgd
606 1.12 cgd int
607 1.50 xtraeme findino(struct inodesc *idesc)
608 1.1 cgd {
609 1.24 lukem struct direct *dirp = idesc->id_dirp;
610 1.1 cgd
611 1.1 cgd if (dirp->d_ino == 0)
612 1.1 cgd return (KEEPON);
613 1.1 cgd if (strcmp(dirp->d_name, idesc->id_name) == 0 &&
614 1.27 bouyer iswap32(dirp->d_ino) >= ROOTINO && iswap32(dirp->d_ino) <= maxino) {
615 1.27 bouyer idesc->id_parent = iswap32(dirp->d_ino);
616 1.1 cgd return (STOP|FOUND);
617 1.1 cgd }
618 1.1 cgd return (KEEPON);
619 1.1 cgd }
620 1.1 cgd
621 1.37 fvdl int
622 1.50 xtraeme clearentry(struct inodesc *idesc)
623 1.37 fvdl {
624 1.37 fvdl struct direct *dirp = idesc->id_dirp;
625 1.37 fvdl
626 1.37 fvdl if (dirp->d_ino != idesc->id_parent || idesc->id_entryno < 2) {
627 1.37 fvdl idesc->id_entryno++;
628 1.37 fvdl return (KEEPON);
629 1.37 fvdl }
630 1.37 fvdl dirp->d_ino = 0;
631 1.37 fvdl return (STOP|FOUND|ALTERED);
632 1.37 fvdl }
633 1.37 fvdl
634 1.12 cgd void
635 1.50 xtraeme pinode(ino_t ino)
636 1.1 cgd {
637 1.37 fvdl union dinode *dp;
638 1.24 lukem char *p;
639 1.1 cgd struct passwd *pw;
640 1.17 thorpej time_t t;
641 1.1 cgd
642 1.56 christos printf(" I=%llu ", (unsigned long long)ino);
643 1.1 cgd if (ino < ROOTINO || ino > maxino)
644 1.1 cgd return;
645 1.1 cgd dp = ginode(ino);
646 1.1 cgd printf(" OWNER=");
647 1.6 cgd #ifndef SMALL
648 1.37 fvdl if ((pw = getpwuid((int)iswap32(DIP(dp, uid)))) != 0)
649 1.1 cgd printf("%s ", pw->pw_name);
650 1.1 cgd else
651 1.6 cgd #endif
652 1.37 fvdl printf("%u ", (unsigned)iswap32(DIP(dp, uid)));
653 1.37 fvdl printf("MODE=%o\n", iswap16(DIP(dp, mode)));
654 1.1 cgd if (preen)
655 1.21 christos printf("%s: ", cdevname());
656 1.37 fvdl printf("SIZE=%llu ", (unsigned long long)iswap64(DIP(dp, size)));
657 1.37 fvdl t = iswap32(DIP(dp, mtime));
658 1.17 thorpej p = ctime(&t);
659 1.1 cgd printf("MTIME=%12.12s %4.4s ", &p[4], &p[20]);
660 1.1 cgd }
661 1.1 cgd
662 1.12 cgd void
663 1.55 christos blkerror(ino_t ino, const char *type, daddr_t blk)
664 1.1 cgd {
665 1.37 fvdl struct inostat *info;
666 1.1 cgd
667 1.56 christos pfatal("%lld %s I=%llu", (long long)blk, type, (unsigned long long)ino);
668 1.1 cgd printf("\n");
669 1.37 fvdl info = inoinfo(ino);
670 1.37 fvdl switch (info->ino_state) {
671 1.1 cgd
672 1.1 cgd case FSTATE:
673 1.37 fvdl info->ino_state = FCLEAR;
674 1.1 cgd return;
675 1.1 cgd
676 1.1 cgd case DSTATE:
677 1.37 fvdl info->ino_state = DCLEAR;
678 1.1 cgd return;
679 1.1 cgd
680 1.1 cgd case FCLEAR:
681 1.1 cgd case DCLEAR:
682 1.1 cgd return;
683 1.1 cgd
684 1.1 cgd default:
685 1.37 fvdl errx(EEXIT, "BAD STATE %d TO BLKERR", info->ino_state);
686 1.1 cgd /* NOTREACHED */
687 1.1 cgd }
688 1.1 cgd }
689 1.1 cgd
690 1.1 cgd /*
691 1.1 cgd * allocate an unused inode
692 1.1 cgd */
693 1.1 cgd ino_t
694 1.50 xtraeme allocino(ino_t request, int type)
695 1.1 cgd {
696 1.24 lukem ino_t ino;
697 1.37 fvdl union dinode *dp;
698 1.37 fvdl struct ufs1_dinode *dp1;
699 1.37 fvdl struct ufs2_dinode *dp2;
700 1.16 thorpej time_t t;
701 1.32 fvdl struct cg *cgp = cgrp;
702 1.32 fvdl int cg;
703 1.54 lukem struct inostat *info = NULL;
704 1.1 cgd
705 1.1 cgd if (request == 0)
706 1.1 cgd request = ROOTINO;
707 1.37 fvdl else if (inoinfo(request)->ino_state != USTATE)
708 1.1 cgd return (0);
709 1.37 fvdl for (ino = request; ino < maxino; ino++) {
710 1.37 fvdl info = inoinfo(ino);
711 1.37 fvdl if (info->ino_state == USTATE)
712 1.1 cgd break;
713 1.37 fvdl }
714 1.1 cgd if (ino == maxino)
715 1.1 cgd return (0);
716 1.32 fvdl cg = ino_to_cg(sblock, ino);
717 1.49 dbj /* If necessary, extend the inoinfo array. grow exponentially */
718 1.49 dbj if ((ino % sblock->fs_ipg) >= inostathead[cg].il_numalloced) {
719 1.49 dbj unsigned long newalloced, i;
720 1.49 dbj newalloced = MIN(sblock->fs_ipg,
721 1.49 dbj MAX(2 * inostathead[cg].il_numalloced, 10));
722 1.49 dbj info = calloc(newalloced, sizeof(struct inostat));
723 1.49 dbj if (info == NULL) {
724 1.49 dbj pwarn("cannot alloc %lu bytes to extend inoinfo\n",
725 1.49 dbj sizeof(struct inostat) * newalloced);
726 1.49 dbj return 0;
727 1.49 dbj }
728 1.49 dbj memmove(info, inostathead[cg].il_stat,
729 1.49 dbj inostathead[cg].il_numalloced * sizeof(*info));
730 1.49 dbj for (i = inostathead[cg].il_numalloced; i < newalloced; i++) {
731 1.49 dbj info[i].ino_state = USTATE;
732 1.49 dbj }
733 1.49 dbj if (inostathead[cg].il_numalloced)
734 1.49 dbj free(inostathead[cg].il_stat);
735 1.49 dbj inostathead[cg].il_stat = info;
736 1.49 dbj inostathead[cg].il_numalloced = newalloced;
737 1.49 dbj info = inoinfo(ino);
738 1.49 dbj }
739 1.32 fvdl getblk(&cgblk, cgtod(sblock, cg), sblock->fs_cgsize);
740 1.32 fvdl memcpy(cgp, cgblk.b_un.b_cg, sblock->fs_cgsize);
741 1.32 fvdl if ((doswap && !needswap) || (!doswap && needswap))
742 1.37 fvdl ffs_cg_swap(cgblk.b_un.b_cg, cgp, sblock);
743 1.32 fvdl if (!cg_chkmagic(cgp, 0))
744 1.32 fvdl pfatal("CG %d: ALLOCINO: BAD MAGIC NUMBER\n", cg);
745 1.32 fvdl if (doswap)
746 1.32 fvdl cgdirty();
747 1.32 fvdl setbit(cg_inosused(cgp, 0), ino % sblock->fs_ipg);
748 1.32 fvdl cgp->cg_cs.cs_nifree--;
749 1.1 cgd switch (type & IFMT) {
750 1.1 cgd case IFDIR:
751 1.37 fvdl info->ino_state = DSTATE;
752 1.32 fvdl cgp->cg_cs.cs_ndir++;
753 1.1 cgd break;
754 1.1 cgd case IFREG:
755 1.1 cgd case IFLNK:
756 1.37 fvdl info->ino_state = FSTATE;
757 1.1 cgd break;
758 1.1 cgd default:
759 1.1 cgd return (0);
760 1.1 cgd }
761 1.32 fvdl cgdirty();
762 1.1 cgd dp = ginode(ino);
763 1.37 fvdl if (is_ufs2) {
764 1.37 fvdl dp2 = &dp->dp2;
765 1.37 fvdl dp2->di_db[0] = iswap64(allocblk(1));
766 1.37 fvdl if (dp2->di_db[0] == 0) {
767 1.37 fvdl info->ino_state = USTATE;
768 1.37 fvdl return (0);
769 1.37 fvdl }
770 1.37 fvdl dp2->di_mode = iswap16(type);
771 1.37 fvdl dp2->di_flags = 0;
772 1.37 fvdl (void)time(&t);
773 1.37 fvdl dp2->di_atime = iswap64(t);
774 1.37 fvdl dp2->di_mtime = dp2->di_ctime = dp2->di_atime;
775 1.37 fvdl dp2->di_size = iswap64(sblock->fs_fsize);
776 1.37 fvdl dp2->di_blocks = iswap64(btodb(sblock->fs_fsize));
777 1.37 fvdl } else {
778 1.37 fvdl dp1 = &dp->dp1;
779 1.37 fvdl dp1->di_db[0] = iswap32(allocblk(1));
780 1.37 fvdl if (dp1->di_db[0] == 0) {
781 1.37 fvdl info->ino_state = USTATE;
782 1.37 fvdl return (0);
783 1.37 fvdl }
784 1.37 fvdl dp1->di_mode = iswap16(type);
785 1.37 fvdl dp1->di_flags = 0;
786 1.37 fvdl (void)time(&t);
787 1.37 fvdl dp1->di_atime = iswap32(t);
788 1.37 fvdl dp1->di_mtime = dp1->di_ctime = dp1->di_atime;
789 1.37 fvdl dp1->di_size = iswap64(sblock->fs_fsize);
790 1.37 fvdl dp1->di_blocks = iswap32(btodb(sblock->fs_fsize));
791 1.1 cgd }
792 1.1 cgd n_files++;
793 1.1 cgd inodirty();
794 1.9 mycroft if (newinofmt)
795 1.37 fvdl info->ino_type = IFTODT(type);
796 1.1 cgd return (ino);
797 1.1 cgd }
798 1.1 cgd
799 1.1 cgd /*
800 1.1 cgd * deallocate an inode
801 1.1 cgd */
802 1.12 cgd void
803 1.50 xtraeme freeino(ino_t ino)
804 1.1 cgd {
805 1.1 cgd struct inodesc idesc;
806 1.37 fvdl union dinode *dp;
807 1.1 cgd
808 1.11 mycroft memset(&idesc, 0, sizeof(struct inodesc));
809 1.1 cgd idesc.id_type = ADDR;
810 1.1 cgd idesc.id_func = pass4check;
811 1.1 cgd idesc.id_number = ino;
812 1.1 cgd dp = ginode(ino);
813 1.1 cgd (void)ckinode(dp, &idesc);
814 1.1 cgd clearinode(dp);
815 1.1 cgd inodirty();
816 1.37 fvdl inoinfo(ino)->ino_state = USTATE;
817 1.1 cgd n_files--;
818 1.1 cgd }
819