]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - repair/dino_chunks.c
Update copyright/license notices to match SGI legal prefered boilerplate.
[thirdparty/xfsprogs-dev.git] / repair / dino_chunks.c
1 /*
2 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include <libxfs.h>
20 #include "avl.h"
21 #include "globals.h"
22 #include "agheader.h"
23 #include "incore.h"
24 #include "protos.h"
25 #include "err_protos.h"
26 #include "dir.h"
27 #include "dinode.h"
28 #include "versions.h"
29
30 /*
31 * validates inode block or chunk, returns # of good inodes
32 * the dinodes are verified using verify_uncertain_dinode() which
33 * means only the basic inode info is checked, no fork checks.
34 */
35
36 int
37 check_aginode_block(xfs_mount_t *mp,
38 xfs_agnumber_t agno,
39 xfs_agblock_t agbno)
40 {
41
42 xfs_dinode_t *dino_p;
43 int i;
44 int cnt = 0;
45 xfs_buf_t *bp;
46
47 /*
48 * it's ok to read these possible inode blocks in one at
49 * a time because they don't belong to known inodes (if
50 * they did, we'd know about them courtesy of the incore inode
51 * tree and we wouldn't be here and we stale the buffers out
52 * so no one else will overlap them.
53 */
54 bp = libxfs_readbuf(mp->m_dev, XFS_AGB_TO_DADDR(mp, agno, agbno),
55 XFS_FSB_TO_BB(mp, 1), 0);
56 if (!bp) {
57 do_warn(_("cannot read agbno (%u/%u), disk block %lld\n"), agno,
58 agbno, (xfs_daddr_t)XFS_AGB_TO_DADDR(mp, agno, agbno));
59 return(0);
60 }
61
62 for (i = 0; i < mp->m_sb.sb_inopblock; i++) {
63 dino_p = XFS_MAKE_IPTR(mp, bp, i);
64 if (!verify_uncertain_dinode(mp, dino_p, agno,
65 XFS_OFFBNO_TO_AGINO(mp, agbno, i)))
66 cnt++;
67 }
68
69 libxfs_putbuf(bp);
70 return(cnt);
71 }
72
73 int
74 check_inode_block(xfs_mount_t *mp,
75 xfs_ino_t ino)
76 {
77 return(check_aginode_block(mp, XFS_INO_TO_AGNO(mp, ino),
78 XFS_INO_TO_AGBNO(mp, ino)));
79 }
80
81 /*
82 * tries to establish if the inode really exists in a valid
83 * inode chunk. returns number of new inodes if things are good
84 * and 0 if bad. start is the start of the discovered inode chunk.
85 * routine assumes that ino is a legal inode number
86 * (verified by verify_inum()). If the inode chunk turns out
87 * to be good, this routine will put the inode chunk into
88 * the good inode chunk tree if required.
89 *
90 * the verify_(ag)inode* family of routines are utility
91 * routines called by check_uncertain_aginodes() and
92 * process_uncertain_aginodes().
93 */
94 int
95 verify_inode_chunk(xfs_mount_t *mp,
96 xfs_ino_t ino,
97 xfs_ino_t *start_ino)
98 {
99 xfs_agnumber_t agno;
100 xfs_agino_t agino;
101 xfs_agino_t start_agino;
102 xfs_agblock_t agbno;
103 xfs_agblock_t start_agbno = 0;
104 xfs_agblock_t end_agbno;
105 xfs_agblock_t max_agbno;
106 xfs_agblock_t cur_agbno;
107 xfs_agblock_t chunk_start_agbno;
108 xfs_agblock_t chunk_stop_agbno;
109 ino_tree_node_t *irec_before_p = NULL;
110 ino_tree_node_t *irec_after_p = NULL;
111 ino_tree_node_t *irec_p;
112 ino_tree_node_t *irec_next_p;
113 int irec_cnt;
114 int ino_cnt = 0;
115 int num_blks;
116 int i;
117 int j;
118 int state;
119
120 agno = XFS_INO_TO_AGNO(mp, ino);
121 agino = XFS_INO_TO_AGINO(mp, ino);
122 agbno = XFS_INO_TO_AGBNO(mp, ino);
123 *start_ino = NULLFSINO;
124
125 ASSERT(XFS_IALLOC_BLOCKS(mp) > 0);
126
127 if (agno == mp->m_sb.sb_agcount - 1)
128 max_agbno = mp->m_sb.sb_dblocks -
129 (xfs_drfsbno_t) mp->m_sb.sb_agblocks * agno;
130 else
131 max_agbno = mp->m_sb.sb_agblocks;
132
133 /*
134 * is the inode beyond the end of the AG?
135 */
136 if (agbno >= max_agbno)
137 return(0);
138
139 /*
140 * check for the easy case, inodes per block >= XFS_INODES_PER_CHUNK
141 * (multiple chunks per block)
142 */
143 if (XFS_IALLOC_BLOCKS(mp) == 1) {
144 if (agbno > max_agbno)
145 return(0);
146
147 if (check_inode_block(mp, ino) == 0)
148 return(0);
149
150 switch (state = get_agbno_state(mp, agno, agbno)) {
151 case XR_E_INO:
152 do_warn(
153 _("uncertain inode block %d/%d already known\n"),
154 agno, agbno);
155 break;
156 case XR_E_UNKNOWN:
157 case XR_E_FREE1:
158 case XR_E_FREE:
159 set_agbno_state(mp, agno, agbno, XR_E_INO);
160 break;
161 case XR_E_MULT:
162 case XR_E_INUSE:
163 case XR_E_INUSE_FS:
164 case XR_E_FS_MAP:
165 /*
166 * if block is already claimed, forget it.
167 */
168 do_warn(
169 _("inode block %d/%d multiply claimed, (state %d)\n"),
170 agno, agbno, state);
171 set_agbno_state(mp, agno, agbno, XR_E_MULT);
172 return(0);
173 default:
174 do_warn(
175 _("inode block %d/%d bad state, (state %d)\n"),
176 agno, agbno, state);
177 set_agbno_state(mp, agno, agbno, XR_E_INO);
178 break;
179 }
180
181 start_agino = XFS_OFFBNO_TO_AGINO(mp, agbno, 0);
182 *start_ino = XFS_AGINO_TO_INO(mp, agno, start_agino);
183
184 /*
185 * put new inode record(s) into inode tree
186 */
187 for (j = 0; j < chunks_pblock; j++) {
188 if ((irec_p = find_inode_rec(agno, start_agino))
189 == NULL) {
190 irec_p = set_inode_free_alloc(agno,
191 start_agino);
192 for (i = 1; i < XFS_INODES_PER_CHUNK; i++)
193 set_inode_free(irec_p, i);
194 }
195 if (start_agino <= agino && agino <
196 start_agino + XFS_INODES_PER_CHUNK)
197 set_inode_used(irec_p, agino - start_agino);
198
199 start_agino += XFS_INODES_PER_CHUNK;
200 ino_cnt += XFS_INODES_PER_CHUNK;
201 }
202
203 return(ino_cnt);
204 } else if (fs_aligned_inodes) {
205 /*
206 * next easy case -- aligned inode filesystem.
207 * just check out the chunk
208 */
209 start_agbno = rounddown(XFS_INO_TO_AGBNO(mp, ino),
210 fs_ino_alignment);
211 end_agbno = start_agbno + XFS_IALLOC_BLOCKS(mp);
212
213 /*
214 * if this fs has aligned inodes but the end of the
215 * chunk is beyond the end of the ag, this is a bad
216 * chunk
217 */
218 if (end_agbno > max_agbno)
219 return(0);
220
221 /*
222 * check out all blocks in chunk
223 */
224 ino_cnt = 0;
225 for (cur_agbno = start_agbno; cur_agbno < end_agbno;
226 cur_agbno++) {
227 ino_cnt += check_aginode_block(mp, agno, cur_agbno);
228 }
229
230 /*
231 * if we lose either 2 blocks worth of inodes or >25% of
232 * the chunk, just forget it.
233 */
234 if (ino_cnt < XFS_INODES_PER_CHUNK - 2 * mp->m_sb.sb_inopblock
235 || ino_cnt < XFS_INODES_PER_CHUNK - 16)
236 return(0);
237
238 /*
239 * ok, put the record into the tree, if no conflict.
240 */
241 if (find_uncertain_inode_rec(agno,
242 XFS_OFFBNO_TO_AGINO(mp, start_agbno, 0)))
243 return(0);
244
245 start_agino = XFS_OFFBNO_TO_AGINO(mp, start_agbno, 0);
246 *start_ino = XFS_AGINO_TO_INO(mp, agno, start_agino);
247
248 irec_p = set_inode_free_alloc(agno,
249 XFS_OFFBNO_TO_AGINO(mp, start_agbno, 0));
250
251 for (i = 1; i < XFS_INODES_PER_CHUNK; i++)
252 set_inode_free(irec_p, i);
253
254 ASSERT(start_agino <= agino &&
255 start_agino + XFS_INODES_PER_CHUNK > agino);
256
257 set_inode_used(irec_p, agino - start_agino);
258
259 return(XFS_INODES_PER_CHUNK);
260 }
261
262 /*
263 * hard case -- pre-6.3 filesystem.
264 * set default start/end agbnos and ensure agbnos are legal.
265 * we're setting a range [start_agbno, end_agbno) such that
266 * a discovered inode chunk completely within that range
267 * would include the inode passed into us.
268 */
269 if (XFS_IALLOC_BLOCKS(mp) > 1) {
270 if (agino > XFS_IALLOC_INODES(mp))
271 start_agbno = agbno - XFS_IALLOC_BLOCKS(mp) + 1;
272 else
273 start_agbno = 1;
274 }
275
276 end_agbno = agbno + XFS_IALLOC_BLOCKS(mp);
277
278 if (end_agbno > max_agbno)
279 end_agbno = max_agbno;
280
281 /*
282 * search tree for known inodes within +/- 1 inode chunk range
283 */
284 irec_before_p = irec_after_p = NULL;
285
286 find_inode_rec_range(agno, XFS_OFFBNO_TO_AGINO(mp, start_agbno, 0),
287 XFS_OFFBNO_TO_AGINO(mp, end_agbno, mp->m_sb.sb_inopblock - 1),
288 &irec_before_p, &irec_after_p);
289
290 /*
291 * if we have known inode chunks in our search range, establish
292 * their start and end-points to tighten our search range. range
293 * is [start, end) -- e.g. max/end agbno is one beyond the
294 * last block to be examined. the avl routines work this way.
295 */
296 if (irec_before_p) {
297 /*
298 * only one inode record in the range, move one boundary in
299 */
300 if (irec_before_p == irec_after_p) {
301 if (irec_before_p->ino_startnum < agino)
302 start_agbno = XFS_AGINO_TO_AGBNO(mp,
303 irec_before_p->ino_startnum +
304 XFS_INODES_PER_CHUNK);
305 else
306 end_agbno = XFS_AGINO_TO_AGBNO(mp,
307 irec_before_p->ino_startnum);
308 }
309
310 /*
311 * find the start of the gap in the search range (which
312 * should contain our unknown inode). if the only irec
313 * within +/- 1 chunks starts after the inode we're
314 * looking for, skip this stuff since the end_agbno
315 * of the range has already been trimmed in to not
316 * include that irec.
317 */
318 if (irec_before_p->ino_startnum < agino) {
319 irec_p = irec_before_p;
320 irec_next_p = next_ino_rec(irec_p);
321
322 while(irec_next_p != NULL &&
323 irec_p->ino_startnum + XFS_INODES_PER_CHUNK ==
324 irec_next_p->ino_startnum) {
325 irec_p = irec_next_p;
326 irec_next_p = next_ino_rec(irec_next_p);
327 }
328
329 start_agbno = XFS_AGINO_TO_AGBNO(mp,
330 irec_p->ino_startnum) +
331 XFS_IALLOC_BLOCKS(mp);
332
333 /*
334 * we know that the inode we're trying to verify isn't
335 * in an inode chunk so the next ino_rec marks the end
336 * of the gap -- is it within the search range?
337 */
338 if (irec_next_p != NULL &&
339 agino + XFS_IALLOC_INODES(mp) >=
340 irec_next_p->ino_startnum)
341 end_agbno = XFS_AGINO_TO_AGBNO(mp,
342 irec_next_p->ino_startnum);
343 }
344
345 ASSERT(start_agbno < end_agbno);
346 }
347
348 /*
349 * if the gap is too small to contain a chunk, we lose.
350 * this means that inode chunks known to be good surround
351 * the inode in question and that the space between them
352 * is too small for a legal inode chunk
353 */
354 if (end_agbno - start_agbno < XFS_IALLOC_BLOCKS(mp))
355 return(0);
356
357 /*
358 * now grunge around the disk, start at the inode block and
359 * go in each direction until you hit a non-inode block or
360 * run into a range boundary. A non-inode block is block
361 * with *no* good inodes in it. Unfortunately, we can't
362 * co-opt bad blocks into inode chunks (which might take
363 * care of disk blocks that turn into zeroes) because the
364 * filesystem could very well allocate two inode chunks
365 * with a one block file in between and we'd zap the file.
366 * We're better off just losing the rest of the
367 * inode chunk instead.
368 */
369 for (cur_agbno = agbno; cur_agbno >= start_agbno; cur_agbno--) {
370 /*
371 * if the block has no inodes, it's a bad block so
372 * break out now without decrementing cur_agbno so
373 * chunk start blockno will be set to the last good block
374 */
375 if (!(irec_cnt = check_aginode_block(mp, agno, cur_agbno)))
376 break;
377 ino_cnt += irec_cnt;
378 }
379
380 chunk_start_agbno = cur_agbno + 1;
381
382 for (cur_agbno = agbno + 1; cur_agbno < end_agbno; cur_agbno++) {
383 /*
384 * if the block has no inodes, it's a bad block so
385 * break out now without incrementing cur_agbno so
386 * chunk start blockno will be set to the block
387 * immediately after the last good block.
388 */
389 if (!(irec_cnt = check_aginode_block(mp, agno, cur_agbno)))
390 break;
391 ino_cnt += irec_cnt;
392 }
393
394 chunk_stop_agbno = cur_agbno;
395
396 num_blks = chunk_stop_agbno - chunk_start_agbno;
397
398 if (num_blks < XFS_IALLOC_BLOCKS(mp) || ino_cnt == 0)
399 return(0);
400
401 /*
402 * XXX - later - if the entire range is selected and they're all
403 * good inodes, keep searching in either direction.
404 * until you the range of inodes end, then split into chunks
405 * for now, just take one chunk's worth starting at the lowest
406 * possible point and hopefully we'll pick the rest up later.
407 *
408 * XXX - if we were going to fix up an inode chunk for
409 * any good inodes in the chunk, this is where we would
410 * do it. For now, keep it simple and lose the rest of
411 * the chunk
412 */
413
414 if (num_blks % XFS_IALLOC_BLOCKS(mp) != 0) {
415 num_blks = rounddown(num_blks, XFS_IALLOC_BLOCKS(mp));
416 chunk_stop_agbno = chunk_start_agbno + num_blks;
417 }
418
419 /*
420 * ok, we've got a candidate inode chunk. now we have to
421 * verify that we aren't trying to use blocks that are already
422 * in use. If so, mark them as multiply claimed since odds
423 * are very low that we found this chunk by stumbling across
424 * user data -- we're probably here as a result of a directory
425 * entry or an iunlinked pointer
426 */
427 for (j = 0, cur_agbno = chunk_start_agbno;
428 cur_agbno < chunk_stop_agbno; cur_agbno++) {
429 switch (state = get_agbno_state(mp, agno, cur_agbno)) {
430 case XR_E_MULT:
431 case XR_E_INUSE:
432 case XR_E_INUSE_FS:
433 case XR_E_FS_MAP:
434 do_warn(
435 _("inode block %d/%d multiply claimed, (state %d)\n"),
436 agno, cur_agbno, state);
437 set_agbno_state(mp, agno, cur_agbno, XR_E_MULT);
438 j = 1;
439 break;
440 case XR_E_INO:
441 do_error(
442 _("uncertain inode block overlap, agbno = %d, ino = %llu\n"),
443 agbno, ino);
444 break;
445 default:
446 break;
447 }
448
449 if (j)
450 return(0);
451 }
452
453 /*
454 * ok, chunk is good. put the record into the tree if required,
455 * and fill in the bitmap. All inodes will be marked as "free"
456 * except for the one that led us to discover the chunk. That's
457 * ok because we'll override the free setting later if the
458 * contents of the inode indicate it's in use.
459 */
460 start_agino = XFS_OFFBNO_TO_AGINO(mp, chunk_start_agbno, 0);
461 *start_ino = XFS_AGINO_TO_INO(mp, agno, start_agino);
462
463 ASSERT(find_inode_rec(agno, start_agino) == NULL);
464
465 irec_p = set_inode_free_alloc(agno, start_agino);
466 for (i = 1; i < XFS_INODES_PER_CHUNK; i++)
467 set_inode_free(irec_p, i);
468
469 ASSERT(start_agino <= agino &&
470 start_agino + XFS_INODES_PER_CHUNK > agino);
471
472 set_inode_used(irec_p, agino - start_agino);
473
474 for (cur_agbno = chunk_start_agbno;
475 cur_agbno < chunk_stop_agbno; cur_agbno++) {
476 switch (state = get_agbno_state(mp, agno, cur_agbno)) {
477 case XR_E_INO:
478 do_error(
479 _("uncertain inode block %llu already known\n"),
480 XFS_AGB_TO_FSB(mp, agno, cur_agbno));
481 break;
482 case XR_E_UNKNOWN:
483 case XR_E_FREE1:
484 case XR_E_FREE:
485 set_agbno_state(mp, agno, cur_agbno, XR_E_INO);
486 break;
487 case XR_E_MULT:
488 case XR_E_INUSE:
489 case XR_E_INUSE_FS:
490 case XR_E_FS_MAP:
491 do_error(
492 _("inode block %d/%d multiply claimed, (state %d)\n"),
493 agno, cur_agbno, state);
494 break;
495 default:
496 do_warn(
497 _("inode block %d/%d bad state, (state %d)\n"),
498 agno, cur_agbno, state);
499 set_agbno_state(mp, agno, cur_agbno, XR_E_INO);
500 break;
501 }
502 }
503
504 return(ino_cnt);
505 }
506
507 /*
508 * same as above only for ag inode chunks
509 */
510 int
511 verify_aginode_chunk(xfs_mount_t *mp,
512 xfs_agnumber_t agno,
513 xfs_agino_t agino,
514 xfs_agino_t *agino_start)
515 {
516 xfs_ino_t ino;
517 int res;
518
519 res = verify_inode_chunk(mp, XFS_AGINO_TO_INO(mp, agno, agino), &ino);
520
521 if (res)
522 *agino_start = XFS_INO_TO_AGINO(mp, ino);
523 else
524 *agino_start = NULLAGINO;
525
526 return(res);
527 }
528
529 /*
530 * this does the same as the two above only it returns a pointer
531 * to the inode record in the good inode tree
532 */
533 ino_tree_node_t *
534 verify_aginode_chunk_irec(xfs_mount_t *mp,
535 xfs_agnumber_t agno,
536 xfs_agino_t agino)
537 {
538 xfs_agino_t start_agino;
539 ino_tree_node_t *irec = NULL;
540
541 if (verify_aginode_chunk(mp, agno, agino, &start_agino))
542 irec = find_inode_rec(agno, start_agino);
543
544 return(irec);
545 }
546
547
548
549 /*
550 * processes an inode allocation chunk/block, returns 1 on I/O errors,
551 * 0 otherwise
552 *
553 * *bogus is set to 1 if the entire set of inodes is bad.
554 */
555 /* ARGSUSED */
556 int
557 process_inode_chunk(xfs_mount_t *mp, xfs_agnumber_t agno, int num_inos,
558 ino_tree_node_t *first_irec, int ino_discovery,
559 int check_dups, int extra_attr_check, int *bogus)
560 {
561 xfs_ino_t parent;
562 ino_tree_node_t *ino_rec;
563 xfs_buf_t *bp;
564 xfs_dinode_t *dino;
565 int icnt;
566 int status;
567 int is_used;
568 int state;
569 int done;
570 int ino_dirty;
571 int irec_offset;
572 int ibuf_offset;
573 xfs_agino_t agino;
574 xfs_agblock_t agbno;
575 int dirty = 0;
576 int cleared = 0;
577 int isa_dir = 0;
578
579 ASSERT(first_irec != NULL);
580 ASSERT(XFS_AGINO_TO_OFFSET(mp, first_irec->ino_startnum) == 0);
581
582 *bogus = 0;
583 ASSERT(XFS_IALLOC_BLOCKS(mp) > 0);
584
585 /*
586 * get all blocks required to read in this chunk (may wind up
587 * having to process more chunks in a multi-chunk per block fs)
588 */
589 agbno = XFS_AGINO_TO_AGBNO(mp, first_irec->ino_startnum);
590
591 bp = libxfs_readbuf(mp->m_dev, XFS_AGB_TO_DADDR(mp, agno, agbno),
592 XFS_FSB_TO_BB(mp, XFS_IALLOC_BLOCKS(mp)), 0);
593 if (!bp) {
594 do_warn(_("cannot read inode %llu, disk block %lld, cnt %d\n"),
595 XFS_AGINO_TO_INO(mp, agno, first_irec->ino_startnum),
596 XFS_AGB_TO_DADDR(mp, agno, agbno),
597 (int)XFS_FSB_TO_BB(mp, XFS_IALLOC_BLOCKS(mp)));
598 return(1);
599 }
600
601 /*
602 * set up first irec
603 */
604 ino_rec = first_irec;
605 /*
606 * initialize counters
607 */
608 irec_offset = 0;
609 ibuf_offset = 0;
610 icnt = 0;
611 status = 0;
612 done = 0;
613
614 /*
615 * verify inode chunk if necessary
616 */
617 if (ino_discovery) {
618 while (!done) {
619 /*
620 * make inode pointer
621 */
622 dino = XFS_MAKE_IPTR(mp, bp, icnt);
623 agino = irec_offset + ino_rec->ino_startnum;
624
625 /*
626 * we always think that the root and realtime
627 * inodes are verified even though we may have
628 * to reset them later to keep from losing the
629 * chunk that they're in
630 */
631 if (verify_dinode(mp, dino, agno, agino) == 0 ||
632 (agno == 0 &&
633 (mp->m_sb.sb_rootino == agino ||
634 mp->m_sb.sb_rsumino == agino ||
635 mp->m_sb.sb_rbmino == agino)))
636 status++;
637
638 irec_offset++;
639 icnt++;
640
641 if (icnt == XFS_IALLOC_INODES(mp) &&
642 irec_offset == XFS_INODES_PER_CHUNK) {
643 /*
644 * done! - finished up irec and block
645 * simultaneously
646 */
647 libxfs_putbuf(bp);
648 done = 1;
649 break;
650 } else if (irec_offset == XFS_INODES_PER_CHUNK) {
651 /*
652 * get new irec (multiple chunks per block fs)
653 */
654 ino_rec = next_ino_rec(ino_rec);
655 ASSERT(ino_rec->ino_startnum == agino + 1);
656 irec_offset = 0;
657 }
658 }
659
660 /*
661 * if chunk/block is bad, blow it off. the inode records
662 * will be deleted by the caller if appropriate.
663 */
664 if (!status) {
665 *bogus = 1;
666 if (!done) /* already free'd */
667 libxfs_putbuf(bp);
668 return(0);
669 }
670
671 /*
672 * reset irec and counters
673 */
674 ino_rec = first_irec;
675
676 irec_offset = 0;
677 ibuf_offset = 0;
678 icnt = 0;
679 status = 0;
680 done = 0;
681
682 /* nathans TODO ... memory leak here?: */
683
684 /*
685 * get first block
686 */
687 bp = libxfs_readbuf(mp->m_dev,
688 XFS_AGB_TO_DADDR(mp, agno, agbno),
689 XFS_FSB_TO_BB(mp, XFS_IALLOC_BLOCKS(mp)), 0);
690 if (!bp) {
691 do_warn(_("can't read inode %llu, disk block %lld, "
692 "cnt %d\n"), XFS_AGINO_TO_INO(mp, agno, agino),
693 XFS_AGB_TO_DADDR(mp, agno, agbno),
694 (int)XFS_FSB_TO_BB(mp, XFS_IALLOC_BLOCKS(mp)));
695 return(1);
696 }
697 }
698
699 /*
700 * mark block as an inode block in the incore bitmap
701 */
702 switch (state = get_agbno_state(mp, agno, agbno)) {
703 case XR_E_INO: /* already marked */
704 break;
705 case XR_E_UNKNOWN:
706 case XR_E_FREE:
707 case XR_E_FREE1:
708 set_agbno_state(mp, agno, agbno, XR_E_INO);
709 break;
710 case XR_E_BAD_STATE:
711 do_error(_("bad state in block map %d\n"), state);
712 break;
713 default:
714 set_agbno_state(mp, agno, agbno, XR_E_MULT);
715 do_warn(_("inode block %llu multiply claimed, state was %d\n"),
716 XFS_AGB_TO_FSB(mp, agno, agbno), state);
717 break;
718 }
719
720 while (!done) {
721 /*
722 * make inode pointer
723 */
724 dino = XFS_MAKE_IPTR(mp, bp, icnt);
725 agino = irec_offset + ino_rec->ino_startnum;
726
727 is_used = 3;
728 ino_dirty = 0;
729 parent = 0;
730
731 status = process_dinode(mp, dino, agno, agino,
732 is_inode_free(ino_rec, irec_offset),
733 &ino_dirty, &cleared, &is_used,
734 ino_discovery, check_dups,
735 extra_attr_check, &isa_dir, &parent);
736
737 ASSERT(is_used != 3);
738 if (ino_dirty)
739 dirty = 1;
740 /*
741 * XXX - if we want to try and keep
742 * track of whether we need to bang on
743 * the inode maps (instead of just
744 * blindly reconstructing them like
745 * we do now, this is where to start.
746 */
747 if (is_used) {
748 if (is_inode_free(ino_rec, irec_offset)) {
749 if (verbose || no_modify ||
750 XFS_AGINO_TO_INO(mp, agno, agino) !=
751 old_orphanage_ino) {
752 do_warn(_("imap claims in-use inode "
753 "%llu is free, "),
754 XFS_AGINO_TO_INO(mp, agno,
755 agino));
756 }
757
758 if (verbose || (!no_modify &&
759 XFS_AGINO_TO_INO(mp, agno, agino) !=
760 old_orphanage_ino))
761 do_warn(_("correcting imap\n"));
762 else
763 do_warn(_("would correct imap\n"));
764 }
765 set_inode_used(ino_rec, irec_offset);
766 } else {
767 set_inode_free(ino_rec, irec_offset);
768 }
769
770 /*
771 * if we lose the root inode, or it turns into
772 * a non-directory, that allows us to double-check
773 * later whether or not we need to reinitialize it.
774 */
775 if (isa_dir) {
776 set_inode_isadir(ino_rec, irec_offset);
777 /*
778 * we always set the parent but
779 * we may as well wait until
780 * phase 4 (no inode discovery)
781 * because the parent info will
782 * be solid then.
783 */
784 if (!ino_discovery) {
785 ASSERT(parent != 0);
786 set_inode_parent(ino_rec, irec_offset, parent);
787 ASSERT(parent ==
788 get_inode_parent(ino_rec, irec_offset));
789 }
790 } else {
791 clear_inode_isadir(ino_rec, irec_offset);
792 }
793
794 if (status) {
795 if (mp->m_sb.sb_rootino ==
796 XFS_AGINO_TO_INO(mp, agno, agino)) {
797 need_root_inode = 1;
798
799 if (!no_modify) {
800 do_warn(_("cleared root inode %llu\n"),
801 XFS_AGINO_TO_INO(mp, agno,
802 agino));
803 } else {
804 do_warn(_("would clear root inode %llu\n"),
805 XFS_AGINO_TO_INO(mp, agno,
806 agino));
807 }
808 } else if (mp->m_sb.sb_rbmino ==
809 XFS_AGINO_TO_INO(mp, agno, agino)) {
810 need_rbmino = 1;
811
812 if (!no_modify) {
813 do_warn(_("cleared realtime bitmap "
814 "inode %llu\n"),
815 XFS_AGINO_TO_INO(mp, agno,
816 agino));
817 } else {
818 do_warn(_("would clear realtime bitmap "
819 "inode %llu\n"),
820 XFS_AGINO_TO_INO(mp, agno,
821 agino));
822 }
823 } else if (mp->m_sb.sb_rsumino ==
824 XFS_AGINO_TO_INO(mp, agno, agino)) {
825 need_rsumino = 1;
826
827 if (!no_modify) {
828 do_warn(_("cleared realtime summary "
829 "inode %llu\n"),
830 XFS_AGINO_TO_INO(mp, agno,
831 agino));
832 } else {
833 do_warn(_("would clear realtime summary"
834 " inode %llu\n"),
835 XFS_AGINO_TO_INO(mp, agno,
836 agino));
837 }
838 } else if (!no_modify) {
839 do_warn(_("cleared inode %llu\n"),
840 XFS_AGINO_TO_INO(mp, agno, agino));
841 } else {
842 do_warn(_("would have cleared inode %llu\n"),
843 XFS_AGINO_TO_INO(mp, agno, agino));
844 }
845 }
846
847 irec_offset++;
848 ibuf_offset++;
849 icnt++;
850
851 if (icnt == XFS_IALLOC_INODES(mp) &&
852 irec_offset == XFS_INODES_PER_CHUNK) {
853 /*
854 * done! - finished up irec and block simultaneously
855 */
856 if (dirty && !no_modify)
857 libxfs_writebuf(bp, 0);
858 else
859 libxfs_putbuf(bp);
860
861 done = 1;
862 break;
863 } else if (ibuf_offset == mp->m_sb.sb_inopblock) {
864 /*
865 * mark block as an inode block in the incore bitmap
866 * and reset inode buffer offset counter
867 */
868 ibuf_offset = 0;
869 agbno++;
870
871 switch (state = get_agbno_state(mp, agno, agbno)) {
872 case XR_E_INO: /* already marked */
873 break;
874 case XR_E_UNKNOWN:
875 case XR_E_FREE:
876 case XR_E_FREE1:
877 set_agbno_state(mp, agno, agbno, XR_E_INO);
878 break;
879 case XR_E_BAD_STATE:
880 do_error(_("bad state in block map %d\n"),
881 state);
882 break;
883 default:
884 set_agbno_state(mp, agno, agbno, XR_E_MULT);
885 do_warn(_("inode block %llu multiply claimed, "
886 "state was %d\n"),
887 XFS_AGB_TO_FSB(mp, agno, agbno), state);
888 break;
889 }
890
891 } else if (irec_offset == XFS_INODES_PER_CHUNK) {
892 /*
893 * get new irec (multiple chunks per block fs)
894 */
895 ino_rec = next_ino_rec(ino_rec);
896 ASSERT(ino_rec->ino_startnum == agino + 1);
897 irec_offset = 0;
898 }
899 }
900 return(0);
901 }
902
903 /*
904 * check all inodes mentioned in the ag's incore inode maps.
905 * the map may be incomplete. If so, we'll catch the missing
906 * inodes (hopefully) when we traverse the directory tree.
907 * check_dirs is set to 1 if directory inodes should be
908 * processed for internal consistency, parent setting and
909 * discovery of unknown inodes. this only happens
910 * in phase 3. check_dups is set to 1 if we're looking for
911 * inodes that reference duplicate blocks so we can trash
912 * the inode right then and there. this is set only in
913 * phase 4 after we've run through and set the bitmap once.
914 */
915 void
916 process_aginodes(xfs_mount_t *mp, xfs_agnumber_t agno,
917 int ino_discovery, int check_dups, int extra_attr_check)
918 {
919 int num_inos, bogus;
920 ino_tree_node_t *ino_rec, *first_ino_rec, *prev_ino_rec;
921
922 first_ino_rec = ino_rec = findfirst_inode_rec(agno);
923 while (ino_rec != NULL) {
924 /*
925 * paranoia - step through inode records until we step
926 * through a full allocation of inodes. this could
927 * be an issue in big-block filesystems where a block
928 * can hold more than one inode chunk. make sure to
929 * grab the record corresponding to the beginning of
930 * the next block before we call the processing routines.
931 */
932 num_inos = XFS_INODES_PER_CHUNK;
933 while (num_inos < XFS_IALLOC_INODES(mp) && ino_rec != NULL) {
934 ASSERT(ino_rec != NULL);
935 /*
936 * inodes chunks will always be aligned and sized
937 * correctly
938 */
939 if ((ino_rec = next_ino_rec(ino_rec)) != NULL)
940 num_inos += XFS_INODES_PER_CHUNK;
941 }
942
943 ASSERT(num_inos == XFS_IALLOC_INODES(mp));
944
945 if (process_inode_chunk(mp, agno, num_inos, first_ino_rec,
946 ino_discovery, check_dups, extra_attr_check, &bogus)) {
947 /* XXX - i/o error, we've got a problem */
948 abort();
949 }
950
951 if (!bogus)
952 first_ino_rec = ino_rec = next_ino_rec(ino_rec);
953 else {
954 /*
955 * inodes pointed to by this record are
956 * completely bogus, blow the records for
957 * this chunk out.
958 * the inode block(s) will get reclaimed
959 * in phase 4 when the block map is
960 * reconstructed after inodes claiming
961 * duplicate blocks are deleted.
962 */
963 num_inos = 0;
964 ino_rec = first_ino_rec;
965 while (num_inos < XFS_IALLOC_INODES(mp) &&
966 ino_rec != NULL) {
967 prev_ino_rec = ino_rec;
968
969 if ((ino_rec = next_ino_rec(ino_rec)) != NULL)
970 num_inos += XFS_INODES_PER_CHUNK;
971
972 get_inode_rec(agno, prev_ino_rec);
973 free_inode_rec(agno, prev_ino_rec);
974 }
975
976 first_ino_rec = ino_rec;
977 }
978 }
979 }
980
981 /*
982 * verify the uncertain inode list for an ag.
983 * Good inodes get moved into the good inode tree.
984 * returns 0 if there are no uncertain inode records to
985 * be processed, 1 otherwise. This routine destroys the
986 * the entire uncertain inode tree for the ag as a side-effect.
987 */
988 void
989 check_uncertain_aginodes(xfs_mount_t *mp, xfs_agnumber_t agno)
990 {
991 ino_tree_node_t *irec;
992 ino_tree_node_t *nrec;
993 xfs_agino_t start;
994 xfs_agino_t i;
995 xfs_agino_t agino;
996 int got_some;
997
998 nrec = NULL;
999 got_some = 0;
1000
1001 clear_uncertain_ino_cache(agno);
1002
1003 if ((irec = findfirst_uncertain_inode_rec(agno)) == NULL)
1004 return;
1005
1006 /*
1007 * the trick here is to find a contiguous range
1008 * of inodes, make sure that it doesn't overlap
1009 * with a known to exist chunk, and then make
1010 * sure it is a number of entire chunks.
1011 * we check on-disk once we have an idea of what's
1012 * going on just to double-check.
1013 *
1014 * process the uncertain inode record list and look
1015 * on disk to see if the referenced inodes are good
1016 */
1017
1018 do_warn(_("found inodes not in the inode allocation tree\n"));
1019
1020 do {
1021 /*
1022 * check every confirmed (which in this case means
1023 * inode that we really suspect to be an inode) inode
1024 */
1025 for (i = 0; i < XFS_INODES_PER_CHUNK; i++) {
1026 if (!is_inode_confirmed(irec, i))
1027 continue;
1028
1029 agino = i + irec->ino_startnum;
1030
1031 if (verify_aginum(mp, agno, agino))
1032 continue;
1033
1034 if (nrec != NULL && nrec->ino_startnum <= agino &&
1035 agino < nrec->ino_startnum +
1036 XFS_INODES_PER_CHUNK)
1037 continue;
1038
1039 if ((nrec = find_inode_rec(agno, agino)) == NULL)
1040 if (!verify_aginum(mp, agno, agino))
1041 if (verify_aginode_chunk(mp, agno,
1042 agino, &start))
1043 got_some = 1;
1044 }
1045
1046 get_uncertain_inode_rec(agno, irec);
1047 free_inode_rec(agno, irec);
1048
1049 irec = findfirst_uncertain_inode_rec(agno);
1050 } while (irec != NULL);
1051
1052 if (got_some)
1053 do_warn(_("found inodes not in the inode allocation tree\n"));
1054
1055 return;
1056 }
1057
1058 /*
1059 * verify and process the uncertain inodes for an ag.
1060 * this is different from check_ in that we can't just
1061 * move the good inodes into the good inode tree and let
1062 * process_aginodes() deal with them because this gets called
1063 * after process_aginodes() has been run on the ag inode tree.
1064 * So we have to process the inodes as well as verify since
1065 * we don't want to rerun process_aginodes() on a tree that has
1066 * mostly been processed.
1067 *
1068 * Note that if this routine does process some inodes, it can
1069 * add uncertain inodes to any ag which would require that
1070 * the routine be called again to process those newly-added
1071 * uncertain inodes.
1072 *
1073 * returns 0 if no inodes were processed and 1 if inodes
1074 * were processed (and it is possible that new uncertain
1075 * inodes were discovered).
1076 *
1077 * as a side-effect, this routine tears down the uncertain
1078 * inode tree for the ag.
1079 */
1080 int
1081 process_uncertain_aginodes(xfs_mount_t *mp, xfs_agnumber_t agno)
1082 {
1083 ino_tree_node_t *irec;
1084 ino_tree_node_t *nrec;
1085 xfs_agino_t agino;
1086 int i;
1087 int bogus;
1088 int cnt;
1089 int got_some;
1090
1091 #ifdef XR_INODE_TRACE
1092 fprintf(stderr, "in process_uncertain_aginodes, agno = %d\n", agno);
1093 #endif
1094
1095 got_some = 0;
1096
1097 clear_uncertain_ino_cache(agno);
1098
1099 if ((irec = findfirst_uncertain_inode_rec(agno)) == NULL)
1100 return(0);
1101
1102 nrec = NULL;
1103
1104 do {
1105 /*
1106 * check every confirmed inode
1107 */
1108 for (cnt = i = 0; i < XFS_INODES_PER_CHUNK; i++) {
1109 if (!is_inode_confirmed(irec, i))
1110 continue;
1111 cnt++;
1112 agino = i + irec->ino_startnum;
1113 #ifdef XR_INODE_TRACE
1114 fprintf(stderr, "ag inode = %d (0x%x)\n", agino, agino);
1115 #endif
1116 /*
1117 * skip over inodes already processed (in the
1118 * good tree), bad inode numbers, and inode numbers
1119 * pointing to bogus inodes
1120 */
1121 if (verify_aginum(mp, agno, agino))
1122 continue;
1123
1124 if (nrec != NULL && nrec->ino_startnum <= agino &&
1125 agino < nrec->ino_startnum +
1126 XFS_INODES_PER_CHUNK)
1127 continue;
1128
1129 if ((nrec = find_inode_rec(agno, agino)) != NULL)
1130 continue;
1131
1132 /*
1133 * verify the chunk. if good, it will be
1134 * added to the good inode tree.
1135 */
1136 if ((nrec = verify_aginode_chunk_irec(mp,
1137 agno, agino)) == NULL)
1138 continue;
1139
1140 got_some = 1;
1141
1142 /*
1143 * process the inode record we just added
1144 * to the good inode tree. The inode
1145 * processing may add more records to the
1146 * uncertain inode lists.
1147 */
1148 if (process_inode_chunk(mp, agno, XFS_IALLOC_INODES(mp),
1149 nrec, 1, 0, 0, &bogus)) {
1150 /* XXX - i/o error, we've got a problem */
1151 abort();
1152 }
1153 }
1154
1155 ASSERT(cnt != 0);
1156 /*
1157 * now return the uncertain inode record to the free pool
1158 * and pull another one off the list for processing
1159 */
1160 get_uncertain_inode_rec(agno, irec);
1161 free_inode_rec(agno, irec);
1162
1163 irec = findfirst_uncertain_inode_rec(agno);
1164 } while (irec != NULL);
1165
1166 if (got_some)
1167 do_warn(_("found inodes not in the inode allocation tree\n"));
1168
1169 return(1);
1170 }