2 * Copyright (c) 2000-2002 Silicon Graphics, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
39 #include "err_protos.h"
49 lf_block_delete_orphanage(xfs_mount_t
*mp
,
51 xfs_dir_leafblock_t
*leaf
,
53 xfs_buf_t
*rootino_bp
,
56 xfs_dir_leaf_entry_t
*entry
;
59 ino_tree_node_t
*irec
;
61 xfs_dir_leaf_name_t
*namest
;
64 xfs_agino_t root_agino
;
65 xfs_agnumber_t root_agno
;
71 char fname
[MAXNAMELEN
+ 1];
74 entry
= &leaf
->entries
[0];
78 root_agno
= XFS_INO_TO_AGNO(mp
, mp
->m_sb
.sb_rootino
);
79 root_agino
= XFS_INO_TO_AGINO(mp
, mp
->m_sb
.sb_rootino
);
81 for (i
= 0; i
< INT_GET(leaf
->hdr
.count
, ARCH_CONVERT
); entry
++, i
++) {
82 namest
= XFS_DIR_LEAF_NAMESTRUCT(leaf
,
83 INT_GET(entry
->nameidx
, ARCH_CONVERT
));
84 XFS_DIR_SF_GET_DIRINO_ARCH(&namest
->inumber
, &lino
, ARCH_CONVERT
);
85 bcopy(namest
->name
, fname
, entry
->namelen
);
86 fname
[entry
->namelen
] = '\0';
88 if (fname
[0] != '/' && !strcmp(fname
, ORPHANAGE
)) {
89 agino
= XFS_INO_TO_AGINO(mp
, lino
);
90 agno
= XFS_INO_TO_AGNO(mp
, lino
);
92 old_orphanage_ino
= lino
;
94 irec
= find_inode_rec(agno
, agino
);
97 * if the orphange inode is in the tree,
98 * get it, clear it, and mark it free.
99 * the inodes in the orphanage will get
100 * reattached to the new orphanage.
103 ino_offset
= agino
- irec
->ino_startnum
;
106 * check if we have to use the root inode
107 * buffer or read one in ourselves. Note
108 * that the root inode is always the first
109 * inode of the chunk that it's in so there
110 * are two possible cases where lost+found
111 * might be in the same buffer as the root
112 * inode. One case is a large block
113 * filesystem where the two inodes are
114 * in different inode chunks but wind
115 * up in the same block (multiple chunks
116 * per block) and the second case (one or
117 * more blocks per chunk) is where the two
118 * inodes are in the same chunk. Note that
119 * inodes are allocated on disk in units
120 * of MAX(XFS_INODES_PER_CHUNK,sb_inopblock).
122 if (XFS_INO_TO_FSB(mp
, mp
->m_sb
.sb_rootino
)
123 == XFS_INO_TO_FSB(mp
, lino
) ||
124 (agno
== root_agno
&&
125 agino
< root_agino
+ XFS_INODES_PER_CHUNK
)) {
128 dino
= XFS_MAKE_IPTR(mp
, bp
, agino
-
130 mp
->m_sb
.sb_rootino
));
132 len
= (int)XFS_FSB_TO_BB(mp
,
133 MAX(1, XFS_INODES_PER_CHUNK
/
135 bp
= libxfs_readbuf(mp
->m_dev
,
136 XFS_AGB_TO_DADDR(mp
, agno
,
137 XFS_AGINO_TO_AGBNO(mp
,
138 irec
->ino_startnum
)),
142 _("couldn't read %s inode %llu\n"),
146 * get the agbno containing the first
147 * inode in the chunk. In multi-block
148 * chunks, this gets us the offset
149 * relative to the beginning of a
150 * properly aligned buffer. In
151 * multi-chunk blocks, this gets us
152 * the correct block number. Then
153 * turn the block number back into
154 * an agino and calculate the offset
155 * from there to feed to make the iptr.
156 * the last term in effect rounds down
157 * to the first agino in the buffer.
159 dino
= XFS_MAKE_IPTR(mp
, bp
,
160 agino
- XFS_OFFBNO_TO_AGINO(mp
,
161 XFS_AGINO_TO_AGBNO(mp
,
167 _(" - clearing existing \"%s\" inode\n"),
170 ino_dirty
= clear_dinode(mp
, dino
, lino
);
173 ASSERT(ino_dirty
== 0 ||
174 (ino_dirty
&& !no_modify
));
176 if (ino_dirty
&& !no_modify
)
177 libxfs_writebuf(bp
, 0);
185 if (inode_isadir(irec
, ino_offset
))
186 clear_inode_isadir(irec
, ino_offset
);
188 set_inode_free(irec
, ino_offset
);
192 * regardless of whether the inode num is good or
193 * bad, mark the entry to be junked so the
194 * createname in phase 6 will succeed.
196 namest
->name
[0] = '/';
199 _(" - marking entry \"%s\" to be deleted\n"),
209 longform_delete_orphanage(xfs_mount_t
*mp
,
212 xfs_buf_t
*rootino_bp
,
215 xfs_dir_leafblock_t
*leaf
;
225 if ((fsbno
= get_first_dblock_fsbno(mp
, ino
, dino
)) == NULLDFSBNO
)
227 _("couldn't map first leaf block of directory inode %llu\n"), ino
);
230 * cycle through the entire directory looking to delete
231 * every "lost+found" entry. make sure to catch duplicate
234 * We could probably speed this up by doing a smarter lookup
235 * to get us to the first block that contains the hashvalue
236 * of "lost+found" but what the heck. that would require a
237 * double lookup for each level. and how big can '/' get???
238 * It's probably not worth it.
243 if (fsbno
== NULLDFSBNO
)
245 bp
= libxfs_readbuf(mp
->m_dev
, XFS_FSB_TO_DADDR(mp
, fsbno
),
246 XFS_FSB_TO_BB(mp
, 1), 0);
248 do_error(_("can't read block %u (fsbno %llu) for "
249 "directory inode %llu\n"),
252 leaf
= (xfs_dir_leafblock_t
*)XFS_BUF_PTR(bp
);
254 if (INT_GET(leaf
->hdr
.info
.magic
, ARCH_CONVERT
) !=
256 do_error(_("bad magic # (0x%x) for directory "
257 "leaf block (bno %u fsbno %llu)\n"),
258 INT_GET(leaf
->hdr
.info
.magic
, ARCH_CONVERT
),
261 da_bno
= INT_GET(leaf
->hdr
.info
.forw
, ARCH_CONVERT
);
263 res
+= lf_block_delete_orphanage(mp
, ino
, leaf
, &dirty
,
264 rootino_bp
, rbuf_dirty
);
266 ASSERT(dirty
== 0 || (dirty
&& !no_modify
));
268 if (dirty
&& !no_modify
)
269 libxfs_writebuf(bp
, 0);
274 fsbno
= get_bmapi(mp
, dino
, ino
, da_bno
, XFS_DATA_FORK
);
276 } while (da_bno
!= 0);
282 * returns 1 if a deletion happened, 0 otherwise.
286 shortform_delete_orphanage(xfs_mount_t
*mp
,
288 xfs_dinode_t
*root_dino
,
289 xfs_buf_t
*rootino_bp
,
292 xfs_dir_shortform_t
*sf
;
294 xfs_dir_sf_entry_t
*sf_entry
, *next_sfe
, *tmp_sfe
;
298 xfs_agino_t root_agino
;
301 xfs_agnumber_t root_agno
;
303 ino_tree_node_t
*irec
;
311 char fname
[MAXNAMELEN
+ 1];
314 sf
= &root_dino
->di_u
.di_dirsf
;
318 ino_dir_size
= INT_GET(root_dino
->di_core
.di_size
, ARCH_CONVERT
);
319 max_size
= XFS_DFORK_DSIZE_ARCH(root_dino
, mp
, ARCH_CONVERT
);
321 root_agno
= XFS_INO_TO_AGNO(mp
, mp
->m_sb
.sb_rootino
);
322 root_agino
= XFS_INO_TO_AGINO(mp
, mp
->m_sb
.sb_rootino
);
325 * run through entries looking for "lost+found".
327 sf_entry
= next_sfe
= &sf
->list
[0];
328 for (i
= 0; i
< INT_GET(sf
->hdr
.count
, ARCH_CONVERT
) && ino_dir_size
>
329 (__psint_t
)next_sfe
- (__psint_t
)sf
; i
++) {
332 XFS_DIR_SF_GET_DIRINO_ARCH(&sf_entry
->inumber
,
333 &lino
, ARCH_CONVERT
);
334 bcopy(sf_entry
->name
, fname
, sf_entry
->namelen
);
335 fname
[sf_entry
->namelen
] = '\0';
337 if (!strcmp(ORPHANAGE
, fname
)) {
338 agno
= XFS_INO_TO_AGNO(mp
, lino
);
339 agino
= XFS_INO_TO_AGINO(mp
, lino
);
341 irec
= find_inode_rec(agno
, agino
);
344 * if the orphange inode is in the tree,
345 * get it, clear it, and mark it free.
346 * the inodes in the orphanage will get
347 * reattached to the new orphanage.
351 _(" - clearing existing \"%s\" inode\n"),
354 ino_offset
= agino
- irec
->ino_startnum
;
357 * check if we have to use the root inode
358 * buffer or read one in ourselves. Note
359 * that the root inode is always the first
360 * inode of the chunk that it's in so there
361 * are two possible cases where lost+found
362 * might be in the same buffer as the root
363 * inode. One case is a large block
364 * filesystem where the two inodes are
365 * in different inode chunks but wind
366 * up in the same block (multiple chunks
367 * per block) and the second case (one or
368 * more blocks per chunk) is where the two
369 * inodes are in the same chunk. Note that
370 * inodes are allocated on disk in units
371 * of MAX(XFS_INODES_PER_CHUNK,sb_inopblock).
373 if (XFS_INO_TO_FSB(mp
, mp
->m_sb
.sb_rootino
)
374 == XFS_INO_TO_FSB(mp
, lino
) ||
375 (agno
== root_agno
&&
376 agino
< root_agino
+ XFS_INODES_PER_CHUNK
)) {
380 dino
= XFS_MAKE_IPTR(mp
, bp
, agino
-
382 mp
->m_sb
.sb_rootino
));
384 len
= (int)XFS_FSB_TO_BB(mp
,
385 MAX(1, XFS_INODES_PER_CHUNK
/
387 bp
= libxfs_readbuf(mp
->m_dev
,
388 XFS_AGB_TO_DADDR(mp
, agno
,
389 XFS_AGINO_TO_AGBNO(mp
,
390 irec
->ino_startnum
)),
394 _("could not read %s inode %llu\n"),
398 * get the agbno containing the first
399 * inode in the chunk. In multi-block
400 * chunks, this gets us the offset
401 * relative to the beginning of a
402 * properly aligned buffer. In
403 * multi-chunk blocks, this gets us
404 * the correct block number. Then
405 * turn the block number back into
406 * an agino and calculate the offset
407 * from there to feed to make the iptr.
408 * the last term in effect rounds down
409 * to the first agino in the buffer.
411 dino
= XFS_MAKE_IPTR(mp
, bp
,
412 agino
- XFS_OFFBNO_TO_AGINO(mp
,
413 XFS_AGINO_TO_AGBNO(mp
,
418 dirty
= clear_dinode(mp
, dino
, lino
);
420 ASSERT(dirty
== 0 || (dirty
&& !no_modify
));
423 * if we read the lost+found inode in to
424 * it, get rid of it here. if the lost+found
425 * inode is in the root inode buffer, the
426 * buffer will be marked dirty anyway since
427 * the lost+found entry in the root inode is
428 * also being deleted which makes the root
429 * inode buffer automatically dirty.
433 if (dirty
&& !no_modify
)
434 libxfs_writebuf(bp
, 0);
439 if (inode_isadir(irec
, ino_offset
))
440 clear_inode_isadir(irec
, ino_offset
);
442 set_inode_free(irec
, ino_offset
);
445 do_warn(_(" - deleting existing \"%s\" entry\n"),
449 * note -- exactly the same deletion code as in
450 * process_shortform_dir()
452 tmp_elen
= XFS_DIR_SF_ENTSIZE_BYENTRY(sf_entry
);
453 INT_MOD(root_dino
->di_core
.di_size
, ARCH_CONVERT
,
456 tmp_sfe
= (xfs_dir_sf_entry_t
*)
457 ((__psint_t
) sf_entry
+ tmp_elen
);
458 tmp_len
= max_size
- ((__psint_t
) tmp_sfe
461 memmove(sf_entry
, tmp_sfe
, tmp_len
);
463 INT_MOD(sf
->hdr
.count
, ARCH_CONVERT
, -1);
465 bzero((void *) ((__psint_t
) sf_entry
+ tmp_len
),
469 * set the tmp value to the current
470 * pointer so we'll process the entry
476 * WARNING: drop the index i by one
477 * so it matches the decremented count for
478 * accurate comparisons in the loop test.
479 * mark root inode as dirty to make deletion
488 next_sfe
= (tmp_sfe
== NULL
)
489 ? (xfs_dir_sf_entry_t
*) ((__psint_t
) sf_entry
+
490 XFS_DIR_SF_ENTSIZE_BYENTRY(sf_entry
))
499 lf2_block_delete_orphanage(xfs_mount_t
*mp
,
501 xfs_dir2_data_t
*data
,
503 xfs_buf_t
*rootino_bp
,
508 ino_tree_node_t
*irec
;
512 xfs_agino_t root_agino
;
513 xfs_agnumber_t root_agno
;
518 char fname
[MAXNAMELEN
+ 1];
522 xfs_dir2_block_tail_t
*btp
;
523 xfs_dir2_data_entry_t
*dep
;
524 xfs_dir2_data_unused_t
*dup
;
526 ptr
= (char *)data
->u
;
527 if (INT_GET(data
->hdr
.magic
, ARCH_CONVERT
) == XFS_DIR2_BLOCK_MAGIC
) {
528 btp
= XFS_DIR2_BLOCK_TAIL_P(mp
, (xfs_dir2_block_t
*)data
);
529 endptr
= (char *)XFS_DIR2_BLOCK_LEAF_P_ARCH(btp
, ARCH_CONVERT
);
531 endptr
= (char *)data
+ mp
->m_dirblksize
;
535 root_agno
= XFS_INO_TO_AGNO(mp
, mp
->m_sb
.sb_rootino
);
536 root_agino
= XFS_INO_TO_AGINO(mp
, mp
->m_sb
.sb_rootino
);
538 while (ptr
< endptr
) {
539 dup
= (xfs_dir2_data_unused_t
*)ptr
;
540 if (INT_GET(dup
->freetag
, ARCH_CONVERT
) ==
541 XFS_DIR2_DATA_FREE_TAG
) {
542 if (ptr
+ INT_GET(dup
->length
, ARCH_CONVERT
) > endptr
||
543 INT_GET(dup
->length
, ARCH_CONVERT
) == 0 ||
544 (INT_GET(dup
->length
, ARCH_CONVERT
) &
545 (XFS_DIR2_DATA_ALIGN
- 1)))
547 ptr
+= INT_GET(dup
->length
, ARCH_CONVERT
);
550 dep
= (xfs_dir2_data_entry_t
*)ptr
;
551 lino
= INT_GET(dep
->inumber
, ARCH_CONVERT
);
552 bcopy(dep
->name
, fname
, dep
->namelen
);
553 fname
[dep
->namelen
] = '\0';
555 if (fname
[0] != '/' && !strcmp(fname
, ORPHANAGE
)) {
556 agino
= XFS_INO_TO_AGINO(mp
, lino
);
557 agno
= XFS_INO_TO_AGNO(mp
, lino
);
559 old_orphanage_ino
= lino
;
561 irec
= find_inode_rec(agno
, agino
);
564 * if the orphange inode is in the tree,
565 * get it, clear it, and mark it free.
566 * the inodes in the orphanage will get
567 * reattached to the new orphanage.
570 ino_offset
= agino
- irec
->ino_startnum
;
573 * check if we have to use the root inode
574 * buffer or read one in ourselves. Note
575 * that the root inode is always the first
576 * inode of the chunk that it's in so there
577 * are two possible cases where lost+found
578 * might be in the same buffer as the root
579 * inode. One case is a large block
580 * filesystem where the two inodes are
581 * in different inode chunks but wind
582 * up in the same block (multiple chunks
583 * per block) and the second case (one or
584 * more blocks per chunk) is where the two
585 * inodes are in the same chunk. Note that
586 * inodes are allocated on disk in units
587 * of MAX(XFS_INODES_PER_CHUNK,sb_inopblock).
589 if (XFS_INO_TO_FSB(mp
, mp
->m_sb
.sb_rootino
)
590 == XFS_INO_TO_FSB(mp
, lino
) ||
591 (agno
== root_agno
&&
592 agino
< root_agino
+ XFS_INODES_PER_CHUNK
)) {
595 dino
= XFS_MAKE_IPTR(mp
, bp
, agino
-
597 mp
->m_sb
.sb_rootino
));
599 len
= (int)XFS_FSB_TO_BB(mp
,
600 MAX(1, XFS_INODES_PER_CHUNK
/
602 bp
= libxfs_readbuf(mp
->m_dev
,
603 XFS_AGB_TO_DADDR(mp
, agno
,
604 XFS_AGINO_TO_AGBNO(mp
,
605 irec
->ino_startnum
)),
609 _("couldn't read %s inode %llu\n"),
613 * get the agbno containing the first
614 * inode in the chunk. In multi-block
615 * chunks, this gets us the offset
616 * relative to the beginning of a
617 * properly aligned buffer. In
618 * multi-chunk blocks, this gets us
619 * the correct block number. Then
620 * turn the block number back into
621 * an agino and calculate the offset
622 * from there to feed to make the iptr.
623 * the last term in effect rounds down
624 * to the first agino in the buffer.
626 dino
= XFS_MAKE_IPTR(mp
, bp
,
627 agino
- XFS_OFFBNO_TO_AGINO(mp
,
628 XFS_AGINO_TO_AGBNO(mp
,
634 _(" - clearing existing \"%s\" inode\n"),
637 ino_dirty
= clear_dinode(mp
, dino
, lino
);
640 ASSERT(ino_dirty
== 0 ||
641 (ino_dirty
&& !no_modify
));
643 if (ino_dirty
&& !no_modify
)
644 libxfs_writebuf(bp
, 0);
652 if (inode_isadir(irec
, ino_offset
))
653 clear_inode_isadir(irec
, ino_offset
);
655 set_inode_free(irec
, ino_offset
);
660 * regardless of whether the inode num is good or
661 * bad, mark the entry to be junked so the
662 * createname in phase 6 will succeed.
667 _(" - marking entry \"%s\" to be deleted\n"),
671 ptr
+= XFS_DIR2_DATA_ENTSIZE(dep
->namelen
);
678 longform2_delete_orphanage(xfs_mount_t
*mp
,
681 xfs_buf_t
*rootino_bp
,
684 xfs_dir2_data_t
*data
;
696 bmp
= malloc(mp
->m_dirblkfsbs
* sizeof(*bmp
));
699 _("malloc failed (%u bytes) in longform2_delete_orphanage, ino %llu\n"),
700 mp
->m_dirblkfsbs
* sizeof(*bmp
), ino
);
703 * cycle through the entire directory looking to delete
704 * every "lost+found" entry. make sure to catch duplicate
707 * We could probably speed this up by doing a smarter lookup
708 * to get us to the first block that contains the hashvalue
709 * of "lost+found" but what the heck. that would require a
710 * double lookup for each level. and how big can '/' get???
711 * It's probably not worth it.
716 da_bno
< XFS_B_TO_FSB(mp
, INT_GET(dino
->di_core
.di_size
, ARCH_CONVERT
));
717 da_bno
+= mp
->m_dirblkfsbs
) {
718 for (i
= 0; i
< mp
->m_dirblkfsbs
; i
++) {
719 fsbno
= get_bmapi(mp
, dino
, ino
, da_bno
+ i
,
721 if (fsbno
== NULLDFSBNO
)
723 bmp
[i
].startoff
= da_bno
+ i
;
724 bmp
[i
].startblock
= fsbno
;
725 bmp
[i
].blockcount
= 1;
728 if (fsbno
== NULLDFSBNO
)
730 bp
= da_read_buf(mp
, mp
->m_dirblkfsbs
, bmp
);
733 _("can't read block %u (fsbno %llu) for directory inode %llu\n"),
734 da_bno
, bmp
[0].startblock
, ino
);
736 data
= (xfs_dir2_data_t
*)bp
->data
;
738 if (INT_GET(data
->hdr
.magic
, ARCH_CONVERT
) !=
739 XFS_DIR2_DATA_MAGIC
&&
740 INT_GET(data
->hdr
.magic
, ARCH_CONVERT
) !=
741 XFS_DIR2_BLOCK_MAGIC
)
743 _("bad magic # (0x%x) for directory data block (bno %u fsbno %llu)\n"),
744 INT_GET(data
->hdr
.magic
, ARCH_CONVERT
),
745 da_bno
, bmp
[0].startblock
);
747 res
+= lf2_block_delete_orphanage(mp
, ino
, data
, &dirty
,
748 rootino_bp
, rbuf_dirty
);
750 ASSERT(dirty
== 0 || (dirty
&& !no_modify
));
752 if (dirty
&& !no_modify
)
763 * returns 1 if a deletion happened, 0 otherwise.
767 shortform2_delete_orphanage(xfs_mount_t
*mp
,
769 xfs_dinode_t
*root_dino
,
770 xfs_buf_t
*rootino_bp
,
775 xfs_dir2_sf_entry_t
*sf_entry
, *next_sfe
, *tmp_sfe
;
779 xfs_agino_t root_agino
;
782 xfs_agnumber_t root_agno
;
784 ino_tree_node_t
*irec
;
792 char fname
[MAXNAMELEN
+ 1];
795 sf
= &root_dino
->di_u
.di_dir2sf
;
798 ino_dir_size
= INT_GET(root_dino
->di_core
.di_size
, ARCH_CONVERT
);
799 max_size
= XFS_DFORK_DSIZE_ARCH(root_dino
, mp
, ARCH_CONVERT
);
802 root_agno
= XFS_INO_TO_AGNO(mp
, mp
->m_sb
.sb_rootino
);
803 root_agino
= XFS_INO_TO_AGINO(mp
, mp
->m_sb
.sb_rootino
);
806 * run through entries looking for "lost+found".
808 sf_entry
= next_sfe
= XFS_DIR2_SF_FIRSTENTRY(sf
);
809 for (i
= 0; i
< INT_GET(sf
->hdr
.count
, ARCH_CONVERT
) && ino_dir_size
>
810 (__psint_t
)next_sfe
- (__psint_t
)sf
; i
++) {
813 lino
= XFS_DIR2_SF_GET_INUMBER_ARCH(sf
,
814 XFS_DIR2_SF_INUMBERP(sf_entry
), ARCH_CONVERT
);
815 bcopy(sf_entry
->name
, fname
, sf_entry
->namelen
);
816 fname
[sf_entry
->namelen
] = '\0';
818 if (!strcmp(ORPHANAGE
, fname
)) {
819 agno
= XFS_INO_TO_AGNO(mp
, lino
);
820 agino
= XFS_INO_TO_AGINO(mp
, lino
);
822 irec
= find_inode_rec(agno
, agino
);
825 * if the orphange inode is in the tree,
826 * get it, clear it, and mark it free.
827 * the inodes in the orphanage will get
828 * reattached to the new orphanage.
832 _(" - clearing existing \"%s\" inode\n"),
835 ino_offset
= agino
- irec
->ino_startnum
;
838 * check if we have to use the root inode
839 * buffer or read one in ourselves. Note
840 * that the root inode is always the first
841 * inode of the chunk that it's in so there
842 * are two possible cases where lost+found
843 * might be in the same buffer as the root
844 * inode. One case is a large block
845 * filesystem where the two inodes are
846 * in different inode chunks but wind
847 * up in the same block (multiple chunks
848 * per block) and the second case (one or
849 * more blocks per chunk) is where the two
850 * inodes are in the same chunk. Note that
851 * inodes are allocated on disk in units
852 * of MAX(XFS_INODES_PER_CHUNK,sb_inopblock).
854 if (XFS_INO_TO_FSB(mp
, mp
->m_sb
.sb_rootino
)
855 == XFS_INO_TO_FSB(mp
, lino
) ||
856 (agno
== root_agno
&&
857 agino
< root_agino
+ XFS_INODES_PER_CHUNK
)) {
861 dino
= XFS_MAKE_IPTR(mp
, bp
, agino
-
863 mp
->m_sb
.sb_rootino
));
865 len
= (int)XFS_FSB_TO_BB(mp
,
866 MAX(1, XFS_INODES_PER_CHUNK
/
868 bp
= libxfs_readbuf(mp
->m_dev
,
869 XFS_AGB_TO_DADDR(mp
, agno
,
870 XFS_AGINO_TO_AGBNO(mp
,
871 irec
->ino_startnum
)),
875 _("could not read %s inode %llu\n"),
879 * get the agbno containing the first
880 * inode in the chunk. In multi-block
881 * chunks, this gets us the offset
882 * relative to the beginning of a
883 * properly aligned buffer. In
884 * multi-chunk blocks, this gets us
885 * the correct block number. Then
886 * turn the block number back into
887 * an agino and calculate the offset
888 * from there to feed to make the iptr.
889 * the last term in effect rounds down
890 * to the first agino in the buffer.
892 dino
= XFS_MAKE_IPTR(mp
, bp
,
893 agino
- XFS_OFFBNO_TO_AGINO(mp
,
894 XFS_AGINO_TO_AGBNO(mp
,
899 dirty
= clear_dinode(mp
, dino
, lino
);
901 ASSERT(dirty
== 0 || (dirty
&& !no_modify
));
904 * if we read the lost+found inode in to
905 * it, get rid of it here. if the lost+found
906 * inode is in the root inode buffer, the
907 * buffer will be marked dirty anyway since
908 * the lost+found entry in the root inode is
909 * also being deleted which makes the root
910 * inode buffer automatically dirty.
914 if (dirty
&& !no_modify
)
915 libxfs_writebuf(bp
, 0);
921 if (inode_isadir(irec
, ino_offset
))
922 clear_inode_isadir(irec
, ino_offset
);
924 set_inode_free(irec
, ino_offset
);
927 do_warn(_(" - deleting existing \"%s\" entry\n"),
931 * note -- exactly the same deletion code as in
932 * process_shortform_dir()
934 tmp_elen
= XFS_DIR2_SF_ENTSIZE_BYENTRY(sf
, sf_entry
);
935 INT_MOD(root_dino
->di_core
.di_size
, ARCH_CONVERT
,
938 tmp_sfe
= (xfs_dir2_sf_entry_t
*)
939 ((__psint_t
) sf_entry
+ tmp_elen
);
940 tmp_len
= max_size
- ((__psint_t
) tmp_sfe
943 memmove(sf_entry
, tmp_sfe
, tmp_len
);
945 INT_MOD(sf
->hdr
.count
, ARCH_CONVERT
, -1);
946 if (lino
> XFS_DIR2_MAX_SHORT_INUM
)
949 bzero((void *) ((__psint_t
) sf_entry
+ tmp_len
),
953 * set the tmp value to the current
954 * pointer so we'll process the entry
960 * WARNING: drop the index i by one
961 * so it matches the decremented count for
962 * accurate comparisons in the loop test.
963 * mark root inode as dirty to make deletion
972 next_sfe
= (tmp_sfe
== NULL
)
973 ? (xfs_dir2_sf_entry_t
*) ((__psint_t
) sf_entry
+
974 XFS_DIR2_SF_ENTSIZE_BYENTRY(sf
, sf_entry
))
982 delete_orphanage(xfs_mount_t
*mp
)
993 ino
= mp
->m_sb
.sb_rootino
;
996 * we know the root is in use or we wouldn't be here
998 len
= (int)XFS_FSB_TO_BB(mp
,
999 MAX(1, XFS_INODES_PER_CHUNK
/inodes_per_block
));
1000 dbp
= libxfs_readbuf(mp
->m_dev
,
1001 XFS_FSB_TO_DADDR(mp
, XFS_INO_TO_FSB(mp
, ino
)), len
, 0);
1003 do_error(_("could not read buffer for root inode %llu "
1004 "(daddr %lld, size %d)\n"), ino
,
1005 XFS_FSB_TO_DADDR(mp
, XFS_INO_TO_FSB(mp
, ino
)),
1006 XFS_FSB_TO_BB(mp
, 1));
1009 * we also know that the root inode is always the first inode
1010 * allocated in the system, therefore it'll be at the beginning
1011 * of the root inode chunk
1013 dino
= XFS_MAKE_IPTR(mp
, dbp
, 0);
1015 switch (dino
->di_core
.di_format
) {
1016 case XFS_DINODE_FMT_EXTENTS
:
1017 case XFS_DINODE_FMT_BTREE
:
1018 if (XFS_SB_VERSION_HASDIRV2(&mp
->m_sb
))
1019 res
= longform2_delete_orphanage(mp
, ino
, dino
, dbp
,
1022 res
= longform_delete_orphanage(mp
, ino
, dino
, dbp
,
1025 case XFS_DINODE_FMT_LOCAL
:
1026 if (XFS_SB_VERSION_HASDIRV2(&mp
->m_sb
))
1027 res
= shortform2_delete_orphanage(mp
, ino
, dino
, dbp
,
1030 res
= shortform_delete_orphanage(mp
, ino
, dino
, dbp
,
1032 ASSERT((res
== 0 && dirty
== 0) || (res
> 0 && dirty
== 1));
1039 switch (dino
->di_core
.di_version
) {
1040 case XFS_DINODE_VERSION_1
:
1041 INT_MOD(dino
->di_core
.di_onlink
, ARCH_CONVERT
, -res
);
1042 INT_SET(dino
->di_core
.di_nlink
, ARCH_CONVERT
,
1043 INT_GET(dino
->di_core
.di_onlink
, ARCH_CONVERT
));
1045 case XFS_DINODE_VERSION_2
:
1046 INT_MOD(dino
->di_core
.di_nlink
, ARCH_CONVERT
, -res
);
1049 do_error(_("unknown version #%d in root inode\n"),
1050 dino
->di_core
.di_version
);
1057 libxfs_writebuf(dbp
, 0);
1063 * null out quota inode fields in sb if they point to non-existent inodes.
1064 * this isn't as redundant as it looks since it's possible that the sb field
1065 * might be set but the imap and inode(s) agree that the inode is
1066 * free in which case they'd never be cleared so the fields wouldn't
1067 * be cleared by process_dinode().
1070 quotino_check(xfs_mount_t
*mp
)
1072 ino_tree_node_t
*irec
;
1074 if (mp
->m_sb
.sb_uquotino
!= NULLFSINO
&& mp
->m_sb
.sb_uquotino
!= 0) {
1075 irec
= find_inode_rec(XFS_INO_TO_AGNO(mp
, mp
->m_sb
.sb_uquotino
),
1076 XFS_INO_TO_AGINO(mp
, mp
->m_sb
.sb_uquotino
));
1078 if (irec
== NULL
|| is_inode_free(irec
,
1079 mp
->m_sb
.sb_uquotino
- irec
->ino_startnum
)) {
1080 mp
->m_sb
.sb_uquotino
= NULLFSINO
;
1086 if (mp
->m_sb
.sb_gquotino
!= NULLFSINO
&& mp
->m_sb
.sb_gquotino
!= 0) {
1087 irec
= find_inode_rec(XFS_INO_TO_AGNO(mp
, mp
->m_sb
.sb_gquotino
),
1088 XFS_INO_TO_AGINO(mp
, mp
->m_sb
.sb_gquotino
));
1090 if (irec
== NULL
|| is_inode_free(irec
,
1091 mp
->m_sb
.sb_gquotino
- irec
->ino_startnum
)) {
1092 mp
->m_sb
.sb_gquotino
= NULLFSINO
;
1100 quota_sb_check(xfs_mount_t
*mp
)
1103 * if the sb says we have quotas and we lost both,
1104 * signal a superblock downgrade. that will cause
1105 * the quota flags to get zeroed. (if we only lost
1106 * one quota inode, do nothing and complain later.)
1108 * if the sb says we have quotas but we didn't start out
1109 * with any quota inodes, signal a superblock downgrade.
1111 * The sb downgrades are so that older systems can mount
1114 * if the sb says we don't have quotas but it looks like
1115 * we do have quota inodes, then signal a superblock upgrade.
1117 * if the sb says we don't have quotas and we have no
1118 * quota inodes, then leave will enough alone.
1122 (mp
->m_sb
.sb_uquotino
== NULLFSINO
|| mp
->m_sb
.sb_uquotino
== 0) &&
1123 (mp
->m_sb
.sb_gquotino
== NULLFSINO
|| mp
->m_sb
.sb_gquotino
== 0)) {
1126 } else if (!verify_inum(mp
, mp
->m_sb
.sb_uquotino
) &&
1127 !verify_inum(mp
, mp
->m_sb
.sb_gquotino
)) {
1134 phase4(xfs_mount_t
*mp
)
1136 ino_tree_node_t
*irec
;
1138 xfs_drtbno_t rt_start
;
1139 xfs_extlen_t rt_len
;
1142 xfs_agblock_t ag_end
;
1143 xfs_agblock_t extent_start
;
1144 xfs_extlen_t extent_len
;
1145 int ag_hdr_len
= 4 * mp
->m_sb
.sb_sectsize
;
1148 int count_bcnt_extents(xfs_agnumber_t agno
);
1149 int count_bno_extents(xfs_agnumber_t agno
);
1151 ag_hdr_block
= howmany(ag_hdr_len
, mp
->m_sb
.sb_blocksize
);
1153 do_log(_("Phase 4 - check for duplicate blocks...\n"));
1154 do_log(_(" - setting up duplicate extent list...\n"));
1156 irec
= find_inode_rec(XFS_INO_TO_AGNO(mp
, mp
->m_sb
.sb_rootino
),
1157 XFS_INO_TO_AGINO(mp
, mp
->m_sb
.sb_rootino
));
1160 * we always have a root inode, even if it's free...
1161 * if the root is free, forget it, lost+found is already gone
1163 if (is_inode_free(irec
, 0) || !inode_isadir(irec
, 0)) {
1164 need_root_inode
= 1;
1166 do_warn(_("root inode would be lost\n"));
1168 do_warn(_("root inode lost\n"));
1172 * have to delete lost+found first so that blocks used
1173 * by lost+found don't show up as used
1176 do_log(_(" - clear lost+found (if it exists) ...\n"));
1177 if (!need_root_inode
)
1178 delete_orphanage(mp
);
1181 for (i
= 0; i
< mp
->m_sb
.sb_agcount
; i
++) {
1182 ag_end
= (i
< mp
->m_sb
.sb_agcount
- 1) ? mp
->m_sb
.sb_agblocks
:
1183 mp
->m_sb
.sb_dblocks
-
1184 (xfs_drfsbno_t
) mp
->m_sb
.sb_agblocks
* i
;
1185 extent_start
= extent_len
= 0;
1187 * set up duplicate extent list for this ag
1189 for (j
= ag_hdr_block
; j
< ag_end
; j
++) {
1191 bstate
= get_agbno_state(mp
, i
, j
);
1194 case XR_E_BAD_STATE
:
1197 _("unknown block state, ag %d, block %d\n"),
1199 /* fall through .. */
1207 if (extent_start
== 0)
1211 * add extent and reset extent state
1213 add_dup_extent(i
, extent_start
,
1220 if (extent_start
== 0) {
1223 } else if (extent_len
== MAXEXTLEN
) {
1224 add_dup_extent(i
, extent_start
,
1234 * catch tail-case, extent hitting the end of the ag
1236 if (extent_start
!= 0)
1237 add_dup_extent(i
, extent_start
, extent_len
);
1241 * initialize realtime bitmap
1246 for (bno
= 0; bno
< mp
->m_sb
.sb_rextents
; bno
++) {
1248 bstate
= get_rtbno_state(mp
, bno
);
1251 case XR_E_BAD_STATE
:
1253 do_warn(_("unknown rt extent state, extent %llu\n"),
1255 /* fall through .. */
1267 * add extent and reset extent state
1269 add_rt_dup_extent(rt_start
, rt_len
);
1275 if (rt_start
== 0) {
1278 } else if (rt_len
== MAXEXTLEN
) {
1282 add_rt_dup_extent(rt_start
, rt_len
);
1292 * catch tail-case, extent hitting the end of the ag
1295 add_rt_dup_extent(rt_start
, rt_len
);
1298 * initialize bitmaps for all AGs
1300 for (i
= 0; i
< mp
->m_sb
.sb_agcount
; i
++) {
1301 ag_end
= (i
< mp
->m_sb
.sb_agcount
- 1) ? mp
->m_sb
.sb_agblocks
:
1302 mp
->m_sb
.sb_dblocks
-
1303 (xfs_drfsbno_t
) mp
->m_sb
.sb_agblocks
* i
;
1305 * now reset the bitmap for all ags
1307 bzero(ba_bmap
[i
], roundup(mp
->m_sb
.sb_agblocks
/(NBBY
/XR_BB
),
1308 sizeof(__uint64_t
)));
1309 for (j
= 0; j
< ag_hdr_block
; j
++)
1310 set_agbno_state(mp
, i
, j
, XR_E_INUSE_FS
);
1312 set_bmap_rt(mp
->m_sb
.sb_rextents
);
1316 do_log(_(" - check for inodes claiming duplicate blocks...\n"));
1317 for (i
= 0; i
< mp
->m_sb
.sb_agcount
; i
++) {
1319 * ok, now process the inodes -- signal 2-pass check per inode.
1320 * first pass checks if the inode conflicts with a known
1321 * duplicate extent. if so, the inode is cleared and second
1322 * pass is skipped. second pass sets the block bitmap
1323 * for all blocks claimed by the inode. directory
1324 * and attribute processing is turned OFF since we did that
1325 * already in phase 3.
1327 do_log(_(" - agno = %d\n"), i
);
1328 process_aginodes(mp
, i
, 0, 1, 0);
1331 * now recycle the per-AG duplicate extent records
1333 release_dup_extent_tree(i
);
1337 * free up memory used to track trealtime duplicate extents
1340 free_rt_dup_extent_tree(mp
);
1343 * ensure consistency of quota inode pointers in superblock,
1344 * make sure they point to real inodes