1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
17 #include "err_protos.h"
22 static struct cred zerocr
;
23 static struct fsxattr zerofsx
;
24 static xfs_ino_t orphanage_ino
;
26 static struct xfs_name xfs_name_dot
= {(unsigned char *)".",
31 * Data structures used to keep track of directories where the ".."
32 * entries are updated. These must be rebuilt after the initial pass
34 typedef struct dotdot_update
{
35 struct list_head list
;
36 ino_tree_node_t
*irec
;
41 static LIST_HEAD(dotdot_update_list
);
42 static int dotdot_update
;
47 ino_tree_node_t
*irec
,
50 dotdot_update_t
*dir
= malloc(sizeof(dotdot_update_t
));
53 do_error(_("malloc failed add_dotdot_update (%zu bytes)\n"),
54 sizeof(dotdot_update_t
));
56 INIT_LIST_HEAD(&dir
->list
);
59 dir
->ino_offset
= ino_offset
;
61 list_add(&dir
->list
, &dotdot_update_list
);
65 * Data structures and routines to keep track of directory entries
66 * and whether their leaf entry has been seen. Also used for name
67 * duplicate checking and rebuilding step if required.
70 struct dir_hash_ent
*nextbyhash
; /* next in name bucket */
71 struct dir_hash_ent
*nextbyorder
; /* next in order added */
72 xfs_dahash_t hashval
; /* hash value of name */
73 uint32_t address
; /* offset of data entry */
74 xfs_ino_t inum
; /* inode num of entry */
75 short junkit
; /* name starts with / */
76 short seen
; /* have seen leaf entry */
78 unsigned char namebuf
[];
82 int size
; /* size of hash tables */
83 struct dir_hash_ent
*first
; /* ptr to first added entry */
84 struct dir_hash_ent
*last
; /* ptr to last added entry */
85 struct dir_hash_ent
**byhash
; /* ptr to name hash buckets */
87 struct radix_tree_root byaddr
;
90 #define DIR_HASH_TAB_SIZE(n) \
91 (sizeof(struct dir_hash_tab) + (sizeof(struct dir_hash_ent *) * (n)))
92 #define DIR_HASH_FUNC(t,a) ((a) % (t)->size)
95 * Track the contents of the freespace table in a directory.
97 typedef struct freetab
{
98 int naents
; /* expected number of data blocks */
99 int nents
; /* number of data blocks processed */
101 xfs_dir2_data_off_t v
;
105 #define FREETAB_SIZE(n) \
106 (offsetof(freetab_t, ents) + (sizeof(struct freetab_ent) * (n)))
108 #define DIR_HASH_CK_OK 0
109 #define DIR_HASH_CK_DUPLEAF 1
110 #define DIR_HASH_CK_BADHASH 2
111 #define DIR_HASH_CK_NODATA 3
112 #define DIR_HASH_CK_NOLEAF 4
113 #define DIR_HASH_CK_BADSTALE 5
114 #define DIR_HASH_CK_TOTAL 6
117 * Need to handle CRC and validation errors specially here. If there is a
118 * validator error, re-read without the verifier so that we get a buffer we can
119 * check and repair. Re-attach the ops to the buffer after the read so that when
120 * it is rewritten the CRC is recalculated.
122 * If the buffer was not read, we return an error. If the buffer was read but
123 * had a CRC or corruption error, we reread it without the verifier and if it is
124 * read successfully we increment *crc_error and return 0. Otherwise we
125 * return the read error.
129 struct xfs_inode
*ip
,
131 struct xfs_buf
**bpp
,
132 const struct xfs_buf_ops
*ops
,
138 error
= -libxfs_da_read_buf(NULL
, ip
, bno
, 0, bpp
, XFS_DATA_FORK
, ops
);
140 if (error
!= EFSBADCRC
&& error
!= EFSCORRUPTED
)
143 error2
= -libxfs_da_read_buf(NULL
, ip
, bno
, 0, bpp
, XFS_DATA_FORK
,
154 * Returns 0 if the name already exists (ie. a duplicate)
158 struct xfs_mount
*mp
,
159 struct dir_hash_tab
*hashtab
,
166 xfs_dahash_t hash
= 0;
168 struct dir_hash_ent
*p
;
171 struct xfs_name xname
;
178 junk
= name
[0] == '/';
182 hash
= libxfs_dir2_hashname(mp
, &xname
);
183 byhash
= DIR_HASH_FUNC(hashtab
, hash
);
186 * search hash bucket for existing name.
188 for (p
= hashtab
->byhash
[byhash
]; p
; p
= p
->nextbyhash
) {
189 if (p
->hashval
== hash
&& p
->name
.len
== namelen
) {
190 if (memcmp(p
->name
.name
, name
, namelen
) == 0) {
200 * Allocate enough space for the hash entry and the name in a single
201 * allocation so we can store our own copy of the name for later use.
203 p
= calloc(1, sizeof(*p
) + namelen
+ 1);
205 do_error(_("malloc failed in dir_hash_add (%zu bytes)\n"),
208 error
= radix_tree_insert(&hashtab
->byaddr
, addr
, p
);
209 if (error
== EEXIST
) {
210 do_warn(_("duplicate addrs %u in directory!\n"), addr
);
214 radix_tree_tag_set(&hashtab
->byaddr
, addr
, HT_UNSEEN
);
217 hashtab
->last
->nextbyorder
= p
;
220 p
->nextbyorder
= NULL
;
223 if (!(p
->junkit
= junk
)) {
225 p
->nextbyhash
= hashtab
->byhash
[byhash
];
226 hashtab
->byhash
[byhash
] = p
;
232 /* Set up the name in the region trailing the hash entry. */
233 memcpy(p
->namebuf
, name
, namelen
);
234 p
->name
.name
= p
->namebuf
;
235 p
->name
.len
= namelen
;
236 p
->name
.type
= ftype
;
242 struct dir_hash_tab
*hashtab
,
243 struct xfs_inode
*ip
,
246 static char *seevalstr
[DIR_HASH_CK_TOTAL
];
250 seevalstr
[DIR_HASH_CK_OK
] = _("ok");
251 seevalstr
[DIR_HASH_CK_DUPLEAF
] = _("duplicate leaf");
252 seevalstr
[DIR_HASH_CK_BADHASH
] = _("hash value mismatch");
253 seevalstr
[DIR_HASH_CK_NODATA
] = _("no data entry");
254 seevalstr
[DIR_HASH_CK_NOLEAF
] = _("no leaf entry");
255 seevalstr
[DIR_HASH_CK_BADSTALE
] = _("bad stale count");
259 if (seeval
== DIR_HASH_CK_OK
&&
260 radix_tree_tagged(&hashtab
->byaddr
, HT_UNSEEN
))
261 seeval
= DIR_HASH_CK_NOLEAF
;
262 if (seeval
== DIR_HASH_CK_OK
)
264 do_warn(_("bad hash table for directory inode %" PRIu64
" (%s): "),
265 ip
->i_ino
, seevalstr
[seeval
]);
267 do_warn(_("rebuilding\n"));
269 do_warn(_("would rebuild\n"));
275 struct dir_hash_tab
*hashtab
)
278 struct dir_hash_ent
*n
;
279 struct dir_hash_ent
*p
;
281 for (i
= 0; i
< hashtab
->size
; i
++) {
282 for (p
= hashtab
->byhash
[i
]; p
; p
= n
) {
284 radix_tree_delete(&hashtab
->byaddr
, p
->address
);
291 static struct dir_hash_tab
*
295 struct dir_hash_tab
*hashtab
;
298 hsize
= size
/ (16 * 4);
303 if ((hashtab
= calloc(DIR_HASH_TAB_SIZE(hsize
), 1)) == NULL
)
304 do_error(_("calloc failed in dir_hash_init\n"));
305 hashtab
->size
= hsize
;
306 hashtab
->byhash
= (struct dir_hash_ent
**)((char *)hashtab
+
307 sizeof(struct dir_hash_tab
));
308 INIT_RADIX_TREE(&hashtab
->byaddr
, 0);
314 struct dir_hash_tab
*hashtab
,
316 xfs_dir2_dataptr_t addr
)
318 struct dir_hash_ent
*p
;
320 p
= radix_tree_lookup(&hashtab
->byaddr
, addr
);
322 return DIR_HASH_CK_NODATA
;
323 if (!radix_tree_tag_get(&hashtab
->byaddr
, addr
, HT_UNSEEN
))
324 return DIR_HASH_CK_DUPLEAF
;
325 if (p
->junkit
== 0 && p
->hashval
!= hash
)
326 return DIR_HASH_CK_BADHASH
;
327 radix_tree_tag_clear(&hashtab
->byaddr
, addr
, HT_UNSEEN
);
328 return DIR_HASH_CK_OK
;
332 dir_hash_update_ftype(
333 struct dir_hash_tab
*hashtab
,
334 xfs_dir2_dataptr_t addr
,
337 struct dir_hash_ent
*p
;
339 p
= radix_tree_lookup(&hashtab
->byaddr
, addr
);
342 p
->name
.type
= ftype
;
346 * checks to make sure leafs match a data entry, and that the stale
351 struct dir_hash_tab
*hashtab
,
352 xfs_dir2_leaf_entry_t
*ents
,
360 for (i
= j
= 0; i
< count
; i
++) {
361 if (be32_to_cpu(ents
[i
].address
) == XFS_DIR2_NULL_DATAPTR
) {
365 rval
= dir_hash_see(hashtab
, be32_to_cpu(ents
[i
].hashval
),
366 be32_to_cpu(ents
[i
].address
));
367 if (rval
!= DIR_HASH_CK_OK
)
370 return j
== stale
? DIR_HASH_CK_OK
: DIR_HASH_CK_BADSTALE
;
374 * Given a block number in a fork, return the next valid block number (not a
375 * hole). If this is the last block number then NULLFILEOFF is returned.
379 struct xfs_inode
*ip
,
384 struct xfs_bmbt_irec got
;
385 struct xfs_iext_cursor icur
;
387 switch (ip
->i_df
.if_format
) {
388 case XFS_DINODE_FMT_LOCAL
:
391 case XFS_DINODE_FMT_BTREE
:
392 case XFS_DINODE_FMT_EXTENTS
:
398 if (!(ip
->i_df
.if_flags
& XFS_IFEXTENTS
)) {
399 error
= -libxfs_iread_extents(NULL
, ip
, XFS_DATA_FORK
);
405 if (!libxfs_iext_lookup_extent(ip
, &ip
->i_df
, bno
, &icur
, &got
))
408 *bnop
= got
.br_startoff
< bno
? bno
: got
.br_startoff
;
417 do_error(_("ran out of disk space!\n"));
419 do_error(_("xfs_trans_reserve returned %d\n"), err
);
423 mk_rbmino(xfs_mount_t
*mp
)
432 xfs_bmbt_irec_t map
[XFS_BMAP_MAX_NMAP
];
439 i
= -libxfs_trans_alloc_rollable(mp
, 10, &tp
);
443 error
= -libxfs_iget(mp
, tp
, mp
->m_sb
.sb_rbmino
, 0, &ip
);
446 _("couldn't iget realtime bitmap inode -- error - %d\n"),
450 memset(&ip
->i_d
, 0, sizeof(ip
->i_d
));
452 VFS_I(ip
)->i_mode
= S_IFREG
;
453 ip
->i_df
.if_format
= XFS_DINODE_FMT_EXTENTS
;
455 ip
->i_afp
->if_format
= XFS_DINODE_FMT_EXTENTS
;
457 set_nlink(VFS_I(ip
), 1); /* account for sb ptr */
459 times
= XFS_ICHGTIME_CHG
| XFS_ICHGTIME_MOD
;
460 if (xfs_sb_version_has_v3inode(&mp
->m_sb
)) {
461 VFS_I(ip
)->i_version
= 1;
462 ip
->i_d
.di_flags2
= 0;
463 times
|= XFS_ICHGTIME_CREATE
;
465 libxfs_trans_ichgtime(tp
, ip
, times
);
470 ip
->i_df
.if_flags
= XFS_IFEXTENTS
;
471 ip
->i_df
.if_bytes
= 0;
472 ip
->i_df
.if_u1
.if_root
= NULL
;
474 ip
->i_d
.di_size
= mp
->m_sb
.sb_rbmblocks
* mp
->m_sb
.sb_blocksize
;
479 libxfs_trans_ijoin(tp
, ip
, 0);
480 libxfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
481 error
= -libxfs_trans_commit(tp
);
483 do_error(_("%s: commit failed, error %d\n"), __func__
, error
);
486 * then allocate blocks for file and fill with zeroes (stolen
489 blocks
= mp
->m_sb
.sb_rbmblocks
+
490 XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
) - 1;
491 error
= -libxfs_trans_alloc_rollable(mp
, blocks
, &tp
);
495 libxfs_trans_ijoin(tp
, ip
, 0);
497 while (bno
< mp
->m_sb
.sb_rbmblocks
) {
498 nmap
= XFS_BMAP_MAX_NMAP
;
499 error
= -libxfs_bmapi_write(tp
, ip
, bno
,
500 (xfs_extlen_t
)(mp
->m_sb
.sb_rbmblocks
- bno
),
501 0, mp
->m_sb
.sb_rbmblocks
, map
, &nmap
);
504 _("couldn't allocate realtime bitmap, error = %d\n"),
507 for (i
= 0, ep
= map
; i
< nmap
; i
++, ep
++) {
508 libxfs_device_zero(mp
->m_ddev_targp
,
509 XFS_FSB_TO_DADDR(mp
, ep
->br_startblock
),
510 XFS_FSB_TO_BB(mp
, ep
->br_blockcount
));
511 bno
+= ep
->br_blockcount
;
514 error
= -libxfs_trans_commit(tp
);
517 _("allocation of the realtime bitmap failed, error = %d\n"),
524 fill_rbmino(xfs_mount_t
*mp
)
538 error
= -libxfs_trans_alloc_rollable(mp
, 10, &tp
);
542 error
= -libxfs_iget(mp
, tp
, mp
->m_sb
.sb_rbmino
, 0, &ip
);
545 _("couldn't iget realtime bitmap inode -- error - %d\n"),
549 while (bno
< mp
->m_sb
.sb_rbmblocks
) {
551 * fill the file one block at a time
554 error
= -libxfs_bmapi_write(tp
, ip
, bno
, 1, 0, 1, &map
, &nmap
);
555 if (error
|| nmap
!= 1) {
557 _("couldn't map realtime bitmap block %" PRIu64
", error = %d\n"),
561 ASSERT(map
.br_startblock
!= HOLESTARTBLOCK
);
563 error
= -libxfs_trans_read_buf(
565 XFS_FSB_TO_DADDR(mp
, map
.br_startblock
),
566 XFS_FSB_TO_BB(mp
, 1), 1, &bp
, NULL
);
570 _("can't access block %" PRIu64
" (fsbno %" PRIu64
") of realtime bitmap inode %" PRIu64
"\n"),
571 bno
, map
.br_startblock
, mp
->m_sb
.sb_rbmino
);
575 memmove(bp
->b_addr
, bmp
, mp
->m_sb
.sb_blocksize
);
577 libxfs_trans_log_buf(tp
, bp
, 0, mp
->m_sb
.sb_blocksize
- 1);
579 bmp
= (xfs_rtword_t
*)((intptr_t) bmp
+ mp
->m_sb
.sb_blocksize
);
583 libxfs_trans_ijoin(tp
, ip
, 0);
584 error
= -libxfs_trans_commit(tp
);
586 do_error(_("%s: commit failed, error %d\n"), __func__
, error
);
592 fill_rsumino(xfs_mount_t
*mp
)
601 xfs_fileoff_t end_bno
;
606 end_bno
= mp
->m_rsumsize
>> mp
->m_sb
.sb_blocklog
;
608 error
= -libxfs_trans_alloc_rollable(mp
, 10, &tp
);
612 error
= -libxfs_iget(mp
, tp
, mp
->m_sb
.sb_rsumino
, 0, &ip
);
615 _("couldn't iget realtime summary inode -- error - %d\n"),
619 while (bno
< end_bno
) {
621 * fill the file one block at a time
624 error
= -libxfs_bmapi_write(tp
, ip
, bno
, 1, 0, 1, &map
, &nmap
);
625 if (error
|| nmap
!= 1) {
627 _("couldn't map realtime summary inode block %" PRIu64
", error = %d\n"),
631 ASSERT(map
.br_startblock
!= HOLESTARTBLOCK
);
633 error
= -libxfs_trans_read_buf(
635 XFS_FSB_TO_DADDR(mp
, map
.br_startblock
),
636 XFS_FSB_TO_BB(mp
, 1), 1, &bp
, NULL
);
640 _("can't access block %" PRIu64
" (fsbno %" PRIu64
") of realtime summary inode %" PRIu64
"\n"),
641 bno
, map
.br_startblock
, mp
->m_sb
.sb_rsumino
);
646 memmove(bp
->b_addr
, smp
, mp
->m_sb
.sb_blocksize
);
648 libxfs_trans_log_buf(tp
, bp
, 0, mp
->m_sb
.sb_blocksize
- 1);
650 smp
= (xfs_suminfo_t
*)((intptr_t)smp
+ mp
->m_sb
.sb_blocksize
);
654 libxfs_trans_ijoin(tp
, ip
, 0);
655 error
= -libxfs_trans_commit(tp
);
657 do_error(_("%s: commit failed, error %d\n"), __func__
, error
);
663 mk_rsumino(xfs_mount_t
*mp
)
673 xfs_bmbt_irec_t map
[XFS_BMAP_MAX_NMAP
];
680 i
= -libxfs_trans_alloc(mp
, &M_RES(mp
)->tr_ichange
, 10, 0, 0, &tp
);
684 error
= -libxfs_iget(mp
, tp
, mp
->m_sb
.sb_rsumino
, 0, &ip
);
687 _("couldn't iget realtime summary inode -- error - %d\n"),
691 memset(&ip
->i_d
, 0, sizeof(ip
->i_d
));
693 VFS_I(ip
)->i_mode
= S_IFREG
;
694 ip
->i_df
.if_format
= XFS_DINODE_FMT_EXTENTS
;
696 ip
->i_afp
->if_format
= XFS_DINODE_FMT_EXTENTS
;
698 set_nlink(VFS_I(ip
), 1); /* account for sb ptr */
700 times
= XFS_ICHGTIME_CHG
| XFS_ICHGTIME_MOD
;
701 if (xfs_sb_version_has_v3inode(&mp
->m_sb
)) {
702 VFS_I(ip
)->i_version
= 1;
703 ip
->i_d
.di_flags2
= 0;
704 times
|= XFS_ICHGTIME_CREATE
;
706 libxfs_trans_ichgtime(tp
, ip
, times
);
711 ip
->i_df
.if_flags
= XFS_IFEXTENTS
;
712 ip
->i_df
.if_bytes
= 0;
713 ip
->i_df
.if_u1
.if_root
= NULL
;
715 ip
->i_d
.di_size
= mp
->m_rsumsize
;
720 libxfs_trans_ijoin(tp
, ip
, 0);
721 libxfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
722 error
= -libxfs_trans_commit(tp
);
724 do_error(_("%s: commit failed, error %d\n"), __func__
, error
);
727 * then allocate blocks for file and fill with zeroes (stolen
730 nsumblocks
= mp
->m_rsumsize
>> mp
->m_sb
.sb_blocklog
;
731 blocks
= nsumblocks
+ XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
) - 1;
732 error
= -libxfs_trans_alloc_rollable(mp
, blocks
, &tp
);
736 libxfs_trans_ijoin(tp
, ip
, 0);
738 while (bno
< nsumblocks
) {
739 nmap
= XFS_BMAP_MAX_NMAP
;
740 error
= -libxfs_bmapi_write(tp
, ip
, bno
,
741 (xfs_extlen_t
)(nsumblocks
- bno
),
742 0, nsumblocks
, map
, &nmap
);
745 _("couldn't allocate realtime summary inode, error = %d\n"),
748 for (i
= 0, ep
= map
; i
< nmap
; i
++, ep
++) {
749 libxfs_device_zero(mp
->m_ddev_targp
,
750 XFS_FSB_TO_DADDR(mp
, ep
->br_startblock
),
751 XFS_FSB_TO_BB(mp
, ep
->br_blockcount
));
752 bno
+= ep
->br_blockcount
;
755 error
= -libxfs_trans_commit(tp
);
758 _("allocation of the realtime summary ino failed, error = %d\n"),
765 * makes a new root directory.
768 mk_root_dir(xfs_mount_t
*mp
)
774 const mode_t mode
= 0755;
775 ino_tree_node_t
*irec
;
779 i
= -libxfs_trans_alloc(mp
, &M_RES(mp
)->tr_ichange
, 10, 0, 0, &tp
);
783 error
= -libxfs_iget(mp
, tp
, mp
->m_sb
.sb_rootino
, 0, &ip
);
785 do_error(_("could not iget root inode -- error - %d\n"), error
);
789 * take care of the core -- initialization from xfs_ialloc()
791 memset(&ip
->i_d
, 0, sizeof(ip
->i_d
));
793 VFS_I(ip
)->i_mode
= mode
|S_IFDIR
;
794 ip
->i_df
.if_format
= XFS_DINODE_FMT_EXTENTS
;
796 ip
->i_afp
->if_format
= XFS_DINODE_FMT_EXTENTS
;
798 set_nlink(VFS_I(ip
), 2); /* account for . and .. */
800 times
= XFS_ICHGTIME_CHG
| XFS_ICHGTIME_MOD
;
801 if (xfs_sb_version_has_v3inode(&mp
->m_sb
)) {
802 VFS_I(ip
)->i_version
= 1;
803 ip
->i_d
.di_flags2
= 0;
804 times
|= XFS_ICHGTIME_CREATE
;
806 libxfs_trans_ichgtime(tp
, ip
, times
);
807 libxfs_trans_ijoin(tp
, ip
, 0);
808 libxfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
813 ip
->i_df
.if_flags
= XFS_IFEXTENTS
;
814 ip
->i_df
.if_bytes
= 0;
815 ip
->i_df
.if_u1
.if_root
= NULL
;
818 * initialize the directory
820 libxfs_dir_init(tp
, ip
, ip
);
822 error
= -libxfs_trans_commit(tp
);
824 do_error(_("%s: commit failed, error %d\n"), __func__
, error
);
828 irec
= find_inode_rec(mp
, XFS_INO_TO_AGNO(mp
, mp
->m_sb
.sb_rootino
),
829 XFS_INO_TO_AGINO(mp
, mp
->m_sb
.sb_rootino
));
830 set_inode_isadir(irec
, XFS_INO_TO_AGINO(mp
, mp
->m_sb
.sb_rootino
) -
835 * orphanage name == lost+found
838 mk_orphanage(xfs_mount_t
*mp
)
844 ino_tree_node_t
*irec
;
848 const int mode
= 0755;
850 struct xfs_name xname
;
853 * check for an existing lost+found first, if it exists, return
854 * its inode. Otherwise, we can create it. Bad lost+found inodes
855 * would have been cleared in phase3 and phase4.
858 i
= -libxfs_iget(mp
, NULL
, mp
->m_sb
.sb_rootino
, 0, &pip
);
860 do_error(_("%d - couldn't iget root inode to obtain %s\n"),
863 xname
.name
= (unsigned char *)ORPHANAGE
;
864 xname
.len
= strlen(ORPHANAGE
);
865 xname
.type
= XFS_DIR3_FT_DIR
;
867 if (libxfs_dir_lookup(NULL
, pip
, &xname
, &ino
, NULL
) == 0)
871 * could not be found, create it
873 nres
= XFS_MKDIR_SPACE_RES(mp
, xname
.len
);
874 i
= -libxfs_trans_alloc(mp
, &M_RES(mp
)->tr_mkdir
, nres
, 0, 0, &tp
);
879 * use iget/ijoin instead of trans_iget because the ialloc
880 * wrapper can commit the transaction and start a new one
882 /* i = -libxfs_iget(mp, NULL, mp->m_sb.sb_rootino, 0, &pip);
884 do_error(_("%d - couldn't iget root inode to make %s\n"),
887 error
= -libxfs_dir_ialloc(&tp
, pip
, mode
|S_IFDIR
,
888 1, 0, &zerocr
, &zerofsx
, &ip
);
890 do_error(_("%s inode allocation failed %d\n"),
893 inc_nlink(VFS_I(ip
)); /* account for . */
896 irec
= find_inode_rec(mp
,
897 XFS_INO_TO_AGNO(mp
, ino
),
898 XFS_INO_TO_AGINO(mp
, ino
));
902 * This inode is allocated from a newly created inode
903 * chunk and therefore did not exist when inode chunks
904 * were processed in phase3. Add this group of inodes to
905 * the entry avl tree as if they were discovered in phase3.
907 irec
= set_inode_free_alloc(mp
, XFS_INO_TO_AGNO(mp
, ino
),
908 XFS_INO_TO_AGINO(mp
, ino
));
911 for (i
= 0; i
< XFS_INODES_PER_CHUNK
; i
++)
912 set_inode_free(irec
, i
);
915 ino_offset
= get_inode_offset(mp
, ino
, irec
);
918 * Mark the inode allocated to lost+found as used in the AVL tree
919 * so it is not skipped in phase 7
921 set_inode_used(irec
, ino_offset
);
922 add_inode_ref(irec
, ino_offset
);
923 add_inode_reached(irec
, ino_offset
);
926 * now that we know the transaction will stay around,
927 * add the root inode to it
929 libxfs_trans_ijoin(tp
, pip
, 0);
932 * create the actual entry
934 error
= -libxfs_dir_createname(tp
, pip
, &xname
, ip
->i_ino
, nres
);
937 _("can't make %s, createname error %d\n"),
941 * bump up the link count in the root directory to account
942 * for .. in the new directory, and update the irec copy of the
943 * on-disk nlink so we don't fail the link count check later.
945 inc_nlink(VFS_I(pip
));
946 irec
= find_inode_rec(mp
, XFS_INO_TO_AGNO(mp
, mp
->m_sb
.sb_rootino
),
947 XFS_INO_TO_AGINO(mp
, mp
->m_sb
.sb_rootino
));
948 add_inode_ref(irec
, 0);
949 set_inode_disk_nlinks(irec
, 0, get_inode_disk_nlinks(irec
, 0) + 1);
951 libxfs_trans_log_inode(tp
, pip
, XFS_ILOG_CORE
);
952 libxfs_dir_init(tp
, ip
, pip
);
953 libxfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
954 error
= -libxfs_trans_commit(tp
);
956 do_error(_("%s directory creation failed -- bmapf error %d\n"),
966 * move a file to the orphange.
971 xfs_ino_t ino
, /* inode # to be moved */
972 int isa_dir
) /* 1 if inode is a directory */
974 xfs_inode_t
*orphanage_ip
;
975 xfs_ino_t entry_ino_num
;
979 unsigned char fname
[MAXPATHLEN
+ 1];
982 ino_tree_node_t
*irec
;
984 struct xfs_name xname
;
987 xname
.len
= snprintf((char *)fname
, sizeof(fname
), "%llu",
988 (unsigned long long)ino
);
990 err
= -libxfs_iget(mp
, NULL
, orphanage_ino
, 0, &orphanage_ip
);
992 do_error(_("%d - couldn't iget orphanage inode\n"), err
);
994 * Make sure the filename is unique in the lost+found
997 while (libxfs_dir_lookup(NULL
, orphanage_ip
, &xname
, &entry_ino_num
,
999 xname
.len
= snprintf((char *)fname
, sizeof(fname
), "%llu.%d",
1000 (unsigned long long)ino
, ++incr
);
1002 /* Orphans may not have a proper parent, so use custom ops here */
1003 err
= -libxfs_iget(mp
, NULL
, ino
, 0, &ino_p
);
1005 do_error(_("%d - couldn't iget disconnected inode\n"), err
);
1007 xname
.type
= libxfs_mode_to_ftype(VFS_I(ino_p
)->i_mode
);
1010 irec
= find_inode_rec(mp
, XFS_INO_TO_AGNO(mp
, orphanage_ino
),
1011 XFS_INO_TO_AGINO(mp
, orphanage_ino
));
1013 ino_offset
= XFS_INO_TO_AGINO(mp
, orphanage_ino
) -
1015 nres
= XFS_DIRENTER_SPACE_RES(mp
, fnamelen
) +
1016 XFS_DIRENTER_SPACE_RES(mp
, 2);
1017 err
= -libxfs_dir_lookup(NULL
, ino_p
, &xfs_name_dotdot
,
1018 &entry_ino_num
, NULL
);
1020 ASSERT(err
== ENOENT
);
1022 err
= -libxfs_trans_alloc(mp
, &M_RES(mp
)->tr_rename
,
1026 _("space reservation failed (%d), filesystem may be out of space\n"),
1029 libxfs_trans_ijoin(tp
, orphanage_ip
, 0);
1030 libxfs_trans_ijoin(tp
, ino_p
, 0);
1032 err
= -libxfs_dir_createname(tp
, orphanage_ip
, &xname
,
1036 _("name create failed in %s (%d), filesystem may be out of space\n"),
1040 add_inode_ref(irec
, ino_offset
);
1042 inc_nlink(VFS_I(orphanage_ip
));
1043 libxfs_trans_log_inode(tp
, orphanage_ip
, XFS_ILOG_CORE
);
1045 err
= -libxfs_dir_createname(tp
, ino_p
, &xfs_name_dotdot
,
1046 orphanage_ino
, nres
);
1049 _("creation of .. entry failed (%d), filesystem may be out of space\n"),
1052 inc_nlink(VFS_I(ino_p
));
1053 libxfs_trans_log_inode(tp
, ino_p
, XFS_ILOG_CORE
);
1054 err
= -libxfs_trans_commit(tp
);
1057 _("creation of .. entry failed (%d)\n"), err
);
1059 err
= -libxfs_trans_alloc(mp
, &M_RES(mp
)->tr_rename
,
1063 _("space reservation failed (%d), filesystem may be out of space\n"),
1066 libxfs_trans_ijoin(tp
, orphanage_ip
, 0);
1067 libxfs_trans_ijoin(tp
, ino_p
, 0);
1070 err
= -libxfs_dir_createname(tp
, orphanage_ip
, &xname
,
1074 _("name create failed in %s (%d), filesystem may be out of space\n"),
1078 add_inode_ref(irec
, ino_offset
);
1080 inc_nlink(VFS_I(orphanage_ip
));
1081 libxfs_trans_log_inode(tp
, orphanage_ip
, XFS_ILOG_CORE
);
1084 * don't replace .. value if it already points
1085 * to us. that'll pop a libxfs/kernel ASSERT.
1087 if (entry_ino_num
!= orphanage_ino
) {
1088 err
= -libxfs_dir_replace(tp
, ino_p
,
1089 &xfs_name_dotdot
, orphanage_ino
,
1093 _("name replace op failed (%d), filesystem may be out of space\n"),
1097 err
= -libxfs_trans_commit(tp
);
1100 _("orphanage name replace op failed (%d)\n"), err
);
1105 * use the remove log reservation as that's
1106 * more accurate. we're only creating the
1107 * links, we're not doing the inode allocation
1108 * also accounted for in the create
1110 nres
= XFS_DIRENTER_SPACE_RES(mp
, xname
.len
);
1111 err
= -libxfs_trans_alloc(mp
, &M_RES(mp
)->tr_remove
,
1115 _("space reservation failed (%d), filesystem may be out of space\n"),
1118 libxfs_trans_ijoin(tp
, orphanage_ip
, 0);
1119 libxfs_trans_ijoin(tp
, ino_p
, 0);
1121 err
= -libxfs_dir_createname(tp
, orphanage_ip
, &xname
, ino
,
1125 _("name create failed in %s (%d), filesystem may be out of space\n"),
1129 set_nlink(VFS_I(ino_p
), 1);
1130 libxfs_trans_log_inode(tp
, ino_p
, XFS_ILOG_CORE
);
1131 err
= -libxfs_trans_commit(tp
);
1134 _("orphanage name create failed (%d)\n"), err
);
1136 libxfs_irele(ino_p
);
1137 libxfs_irele(orphanage_ip
);
1147 do_warn(msg
, iname
, ino1
, ino2
);
1150 do_warn(_(", marking entry to be junked\n"));
1154 do_warn(_(", would junk entry\n"));
1158 /* Find and invalidate all the directory's buffers. */
1161 struct xfs_trans
*tp
,
1162 struct xfs_inode
*ip
,
1165 struct xfs_iext_cursor icur
;
1166 struct xfs_bmbt_irec rec
;
1167 struct xfs_ifork
*ifp
;
1168 struct xfs_da_geometry
*geo
;
1173 if (ip
->i_df
.if_format
!= XFS_DINODE_FMT_EXTENTS
&&
1174 ip
->i_df
.if_format
!= XFS_DINODE_FMT_BTREE
)
1177 geo
= tp
->t_mountp
->m_dir_geo
;
1178 ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
1179 for_each_xfs_iext(ifp
, &icur
, &rec
) {
1180 for (dabno
= roundup(rec
.br_startoff
, geo
->fsbcount
);
1181 dabno
< rec
.br_startoff
+ rec
.br_blockcount
;
1182 dabno
+= geo
->fsbcount
) {
1184 error
= -libxfs_da_get_buf(tp
, ip
, dabno
, &bp
,
1190 libxfs_trans_binval(tp
, bp
);
1191 libxfs_trans_brelse(tp
, bp
);
1199 * Unexpected failure during the rebuild will leave the entries in
1200 * lost+found on the next run
1204 longform_dir2_rebuild(
1205 struct xfs_mount
*mp
,
1207 struct xfs_inode
*ip
,
1208 struct ino_tree_node
*irec
,
1210 struct dir_hash_tab
*hashtab
)
1214 struct xfs_trans
*tp
;
1215 xfs_fileoff_t lastblock
;
1216 struct xfs_inode pip
;
1217 struct dir_hash_ent
*p
;
1221 * trash directory completely and rebuild from scratch using the
1222 * name/inode pairs in the hash table
1225 do_warn(_("rebuilding directory inode %" PRIu64
"\n"), ino
);
1228 * first attempt to locate the parent inode, if it can't be
1229 * found, set it to the root inode and it'll be moved to the
1230 * orphanage later (the inode number here needs to be valid
1231 * for the libxfs_dir_init() call).
1233 pip
.i_ino
= get_inode_parent(irec
, ino_offset
);
1234 if (pip
.i_ino
== NULLFSINO
||
1235 libxfs_dir_ino_validate(mp
, pip
.i_ino
))
1236 pip
.i_ino
= mp
->m_sb
.sb_rootino
;
1238 nres
= XFS_REMOVE_SPACE_RES(mp
);
1239 error
= -libxfs_trans_alloc(mp
, &M_RES(mp
)->tr_remove
, nres
, 0, 0, &tp
);
1242 libxfs_trans_ijoin(tp
, ip
, 0);
1244 error
= dir_binval(tp
, ip
, XFS_DATA_FORK
);
1246 do_error(_("error %d invalidating directory %llu blocks\n"),
1247 error
, (unsigned long long)ip
->i_ino
);
1249 if ((error
= -libxfs_bmap_last_offset(ip
, &lastblock
, XFS_DATA_FORK
)))
1250 do_error(_("xfs_bmap_last_offset failed -- error - %d\n"),
1253 /* free all data, leaf, node and freespace blocks */
1255 error
= -libxfs_bunmapi(tp
, ip
, 0, lastblock
, XFS_BMAPI_METADATA
,
1258 do_warn(_("xfs_bunmapi failed -- error - %d\n"), error
);
1259 goto out_bmap_cancel
;
1261 error
= -libxfs_defer_finish(&tp
);
1263 do_warn(("defer_finish failed -- error - %d\n"), error
);
1264 goto out_bmap_cancel
;
1267 * Close out trans and start the next one in the chain.
1269 error
= -libxfs_trans_roll_inode(&tp
, ip
);
1271 goto out_bmap_cancel
;
1274 error
= -libxfs_dir_init(tp
, ip
, &pip
);
1276 do_warn(_("xfs_dir_init failed -- error - %d\n"), error
);
1277 goto out_bmap_cancel
;
1280 error
= -libxfs_trans_commit(tp
);
1283 _("dir init failed (%d)\n"), error
);
1285 if (ino
== mp
->m_sb
.sb_rootino
)
1286 need_root_dotdot
= 0;
1288 /* go through the hash list and re-add the inodes */
1290 for (p
= hashtab
->first
; p
; p
= p
->nextbyorder
) {
1292 if (p
->name
.name
[0] == '/' || (p
->name
.name
[0] == '.' &&
1293 (p
->name
.len
== 1 || (p
->name
.len
== 2 &&
1294 p
->name
.name
[1] == '.'))))
1297 nres
= XFS_CREATE_SPACE_RES(mp
, p
->name
.len
);
1298 error
= -libxfs_trans_alloc(mp
, &M_RES(mp
)->tr_create
,
1303 libxfs_trans_ijoin(tp
, ip
, 0);
1305 error
= -libxfs_dir_createname(tp
, ip
, &p
->name
, p
->inum
,
1309 _("name create failed in ino %" PRIu64
" (%d), filesystem may be out of space\n"),
1311 goto out_bmap_cancel
;
1314 error
= -libxfs_trans_commit(tp
);
1317 _("name create failed (%d) during rebuild\n"), error
);
1323 libxfs_trans_cancel(tp
);
1329 * Kill a block in a version 2 inode.
1330 * Makes its own transaction.
1344 nres
= XFS_REMOVE_SPACE_RES(mp
);
1345 error
= -libxfs_trans_alloc(mp
, &M_RES(mp
)->tr_remove
, nres
, 0, 0, &tp
);
1348 libxfs_trans_ijoin(tp
, ip
, 0);
1349 libxfs_trans_bjoin(tp
, bp
);
1350 libxfs_trans_bhold(tp
, bp
);
1351 memset(&args
, 0, sizeof(args
));
1354 args
.whichfork
= XFS_DATA_FORK
;
1355 args
.geo
= mp
->m_dir_geo
;
1356 if (da_bno
>= mp
->m_dir_geo
->leafblk
&& da_bno
< mp
->m_dir_geo
->freeblk
)
1357 error
= -libxfs_da_shrink_inode(&args
, da_bno
, bp
);
1359 error
= -libxfs_dir2_shrink_inode(&args
,
1360 xfs_dir2_da_to_db(mp
->m_dir_geo
, da_bno
), bp
);
1362 do_error(_("shrink_inode failed inode %" PRIu64
" block %u\n"),
1364 error
= -libxfs_trans_commit(tp
);
1367 _("directory shrink failed (%d)\n"), error
);
1371 * process a data block, also checks for .. entry
1372 * and corrects it to match what we think .. should be
1375 longform_dir2_entry_check_data(
1376 struct xfs_mount
*mp
,
1377 struct xfs_inode
*ip
,
1380 struct ino_tree_node
*current_irec
,
1381 int current_ino_offset
,
1383 struct dir_hash_tab
*hashtab
,
1384 freetab_t
**freetabp
,
1388 xfs_dir2_dataptr_t addr
;
1389 xfs_dir2_leaf_entry_t
*blp
;
1390 xfs_dir2_block_tail_t
*btp
;
1391 struct xfs_dir2_data_hdr
*d
;
1393 xfs_dir2_data_entry_t
*dep
;
1394 xfs_dir2_data_unused_t
*dup
;
1395 struct xfs_dir2_data_free
*bf
;
1398 char fname
[MAXNAMELEN
+ 1];
1403 ino_tree_node_t
*irec
;
1414 struct xfs_da_args da
= {
1416 .geo
= mp
->m_dir_geo
,
1421 ptr
= (char *)d
+ mp
->m_dir_geo
->data_entry_offset
;
1423 needscan
= needlog
= 0;
1425 freetab
= *freetabp
;
1427 btp
= xfs_dir2_block_tail_p(mp
->m_dir_geo
, d
);
1428 blp
= xfs_dir2_block_leaf_p(btp
);
1429 endptr
= (char *)blp
;
1430 if (endptr
> (char *)btp
)
1431 endptr
= (char *)btp
;
1432 if (xfs_sb_version_hascrc(&mp
->m_sb
))
1433 wantmagic
= XFS_DIR3_BLOCK_MAGIC
;
1435 wantmagic
= XFS_DIR2_BLOCK_MAGIC
;
1437 endptr
= (char *)d
+ mp
->m_dir_geo
->blksize
;
1438 if (xfs_sb_version_hascrc(&mp
->m_sb
))
1439 wantmagic
= XFS_DIR3_DATA_MAGIC
;
1441 wantmagic
= XFS_DIR2_DATA_MAGIC
;
1443 db
= xfs_dir2_da_to_db(mp
->m_dir_geo
, da_bno
);
1445 /* check for data block beyond expected end */
1446 if (freetab
->naents
<= db
) {
1447 struct freetab_ent e
;
1449 *freetabp
= freetab
= realloc(freetab
, FREETAB_SIZE(db
+ 1));
1451 do_error(_("realloc failed in %s (%zu bytes)\n"),
1452 __func__
, FREETAB_SIZE(db
+ 1));
1456 for (i
= freetab
->naents
; i
< db
; i
++)
1457 freetab
->ents
[i
] = e
;
1458 freetab
->naents
= db
+ 1;
1461 /* check the data block */
1462 while (ptr
< endptr
) {
1464 /* check for freespace */
1465 dup
= (xfs_dir2_data_unused_t
*)ptr
;
1466 if (XFS_DIR2_DATA_FREE_TAG
== be16_to_cpu(dup
->freetag
)) {
1468 /* check for invalid freespace length */
1469 if (ptr
+ be16_to_cpu(dup
->length
) > endptr
||
1470 be16_to_cpu(dup
->length
) == 0 ||
1471 (be16_to_cpu(dup
->length
) &
1472 (XFS_DIR2_DATA_ALIGN
- 1)))
1475 /* check for invalid tag */
1476 if (be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup
)) !=
1477 (char *)dup
- (char *)d
)
1480 /* check for block with no data entries */
1481 if ((ptr
== (char *)d
+ mp
->m_dir_geo
->data_entry_offset
) &&
1482 (ptr
+ be16_to_cpu(dup
->length
) >= endptr
)) {
1488 /* continue at the end of the freespace */
1489 ptr
+= be16_to_cpu(dup
->length
);
1494 /* validate data entry size */
1495 dep
= (xfs_dir2_data_entry_t
*)ptr
;
1496 if (ptr
+ libxfs_dir2_data_entsize(mp
, dep
->namelen
) > endptr
)
1498 if (be16_to_cpu(*libxfs_dir2_data_entry_tag_p(mp
, dep
)) !=
1499 (char *)dep
- (char *)d
)
1501 ptr
+= libxfs_dir2_data_entsize(mp
, dep
->namelen
);
1504 /* did we find an empty or corrupt block? */
1505 if (ptr
!= endptr
) {
1508 _("empty data block %u in directory inode %" PRIu64
": "),
1512 ("corrupt block %u in directory inode %" PRIu64
": "),
1516 do_warn(_("junking block\n"));
1517 dir2_kill_block(mp
, ip
, da_bno
, bp
);
1519 do_warn(_("would junk block\n"));
1521 freetab
->ents
[db
].v
= NULLDATAOFF
;
1525 /* update number of data blocks processed */
1526 if (freetab
->nents
< db
+ 1)
1527 freetab
->nents
= db
+ 1;
1529 error
= -libxfs_trans_alloc(mp
, &M_RES(mp
)->tr_remove
, 0, 0, 0, &tp
);
1533 libxfs_trans_ijoin(tp
, ip
, 0);
1534 libxfs_trans_bjoin(tp
, bp
);
1535 libxfs_trans_bhold(tp
, bp
);
1536 if (be32_to_cpu(d
->magic
) != wantmagic
) {
1538 _("bad directory block magic # %#x for directory inode %" PRIu64
" block %d: "),
1539 be32_to_cpu(d
->magic
), ip
->i_ino
, da_bno
);
1541 do_warn(_("fixing magic # to %#x\n"), wantmagic
);
1542 d
->magic
= cpu_to_be32(wantmagic
);
1545 do_warn(_("would fix magic # to %#x\n"), wantmagic
);
1548 ptr
= (char *)d
+ mp
->m_dir_geo
->data_entry_offset
;
1550 * look at each entry. reference inode pointed to by each
1551 * entry in the incore inode tree.
1552 * if not a directory, set reached flag, increment link count
1553 * if a directory and reached, mark entry as to be deleted.
1554 * if a directory, check to see if recorded parent
1555 * matches current inode #,
1556 * if so, then set reached flag, increment link count
1557 * of current and child dir inodes, push the child
1558 * directory inode onto the directory stack.
1559 * if current inode != parent, then mark entry to be deleted.
1561 while (ptr
< endptr
) {
1562 dup
= (xfs_dir2_data_unused_t
*)ptr
;
1563 if (be16_to_cpu(dup
->freetag
) == XFS_DIR2_DATA_FREE_TAG
) {
1566 _("directory inode %" PRIu64
" block %u has consecutive free entries: "),
1570 do_warn(_("joining together\n"));
1571 len
= be16_to_cpu(dup
->length
);
1572 libxfs_dir2_data_use_free(&da
, bp
, dup
,
1573 ptr
- (char *)d
, len
, &needlog
,
1575 libxfs_dir2_data_make_free(&da
, bp
,
1576 ptr
- (char *)d
, len
, &needlog
,
1579 do_warn(_("would join together\n"));
1581 ptr
+= be16_to_cpu(dup
->length
);
1585 addr
= xfs_dir2_db_off_to_dataptr(mp
->m_dir_geo
, db
,
1587 dep
= (xfs_dir2_data_entry_t
*)ptr
;
1588 ptr
+= libxfs_dir2_data_entsize(mp
, dep
->namelen
);
1589 inum
= be64_to_cpu(dep
->inumber
);
1592 * skip bogus entries (leading '/'). they'll be deleted
1593 * later. must still log it, else we leak references to
1596 if (dep
->name
[0] == '/') {
1599 libxfs_dir2_data_log_entry(&da
, bp
, dep
);
1603 memmove(fname
, dep
->name
, dep
->namelen
);
1604 fname
[dep
->namelen
] = '\0';
1605 ASSERT(inum
!= NULLFSINO
);
1607 irec
= find_inode_rec(mp
, XFS_INO_TO_AGNO(mp
, inum
),
1608 XFS_INO_TO_AGINO(mp
, inum
));
1612 _("entry \"%s\" in directory inode %" PRIu64
" points to non-existent inode %" PRIu64
""),
1613 fname
, ip
->i_ino
, inum
)) {
1615 libxfs_dir2_data_log_entry(&da
, bp
, dep
);
1619 ino_offset
= XFS_INO_TO_AGINO(mp
, inum
) - irec
->ino_startnum
;
1622 * if it's a free inode, blow out the entry.
1623 * by now, any inode that we think is free
1626 if (is_inode_free(irec
, ino_offset
)) {
1629 _("entry \"%s\" in directory inode %" PRIu64
" points to free inode %" PRIu64
),
1630 fname
, ip
->i_ino
, inum
)) {
1632 libxfs_dir2_data_log_entry(&da
, bp
, dep
);
1638 * check if this inode is lost+found dir in the root
1640 if (inum
== mp
->m_sb
.sb_rootino
&& strcmp(fname
, ORPHANAGE
) == 0) {
1642 * if it's not a directory, trash it
1644 if (!inode_isadir(irec
, ino_offset
)) {
1647 _("%s (ino %" PRIu64
") in root (%" PRIu64
") is not a directory"),
1648 ORPHANAGE
, inum
, ip
->i_ino
)) {
1650 libxfs_dir2_data_log_entry(&da
, bp
, dep
);
1655 * if this is a dup, it will be picked up below,
1656 * otherwise, mark it as the orphanage for later.
1659 orphanage_ino
= inum
;
1663 * check for duplicate names in directory.
1665 if (!dir_hash_add(mp
, hashtab
, addr
, inum
, dep
->namelen
,
1666 dep
->name
, libxfs_dir2_data_get_ftype(mp
, dep
))) {
1669 _("entry \"%s\" (ino %" PRIu64
") in dir %" PRIu64
" is a duplicate name"),
1670 fname
, inum
, ip
->i_ino
)) {
1672 libxfs_dir2_data_log_entry(&da
, bp
, dep
);
1674 if (inum
== orphanage_ino
)
1680 * if just scanning to rebuild a directory due to a ".."
1681 * update, just continue
1687 * skip the '..' entry since it's checked when the
1688 * directory is reached by something else. if it never
1689 * gets reached, it'll be moved to the orphanage and we'll
1690 * take care of it then. If it doesn't exist at all, the
1691 * directory needs to be rebuilt first before being added
1694 if (dep
->namelen
== 2 && dep
->name
[0] == '.' &&
1695 dep
->name
[1] == '.') {
1697 /* ".." should be in the first block */
1700 _("entry \"%s\" (ino %" PRIu64
") in dir %" PRIu64
" is not in the the first block"), fname
,
1703 libxfs_dir2_data_log_entry(&da
, bp
, dep
);
1708 ASSERT(no_modify
|| libxfs_verify_dir_ino(mp
, inum
));
1710 * special case the . entry. we know there's only one
1711 * '.' and only '.' points to itself because bogus entries
1712 * got trashed in phase 3 if there were > 1.
1713 * bump up link count for '.' but don't set reached
1714 * until we're actually reached by another directory
1715 * '..' is already accounted for or will be taken care
1716 * of when directory is moved to orphanage.
1718 if (ip
->i_ino
== inum
) {
1720 (dep
->name
[0] == '.' && dep
->namelen
== 1));
1721 add_inode_ref(current_irec
, current_ino_offset
);
1723 dep
!= (void *)d
+ mp
->m_dir_geo
->data_entry_offset
) {
1724 /* "." should be the first entry */
1727 _("entry \"%s\" in dir %" PRIu64
" is not the first entry"),
1728 fname
, inum
, ip
->i_ino
)) {
1730 libxfs_dir2_data_log_entry(&da
, bp
, dep
);
1737 * skip entries with bogus inumbers if we're in no modify mode
1739 if (no_modify
&& !libxfs_verify_dir_ino(mp
, inum
))
1742 /* validate ftype field if supported */
1743 if (xfs_sb_version_hasftype(&mp
->m_sb
)) {
1747 dir_ftype
= libxfs_dir2_data_get_ftype(mp
, dep
);
1748 ino_ftype
= get_inode_ftype(irec
, ino_offset
);
1750 if (dir_ftype
!= ino_ftype
) {
1753 _("would fix ftype mismatch (%d/%d) in directory/child inode %" PRIu64
"/%" PRIu64
"\n"),
1754 dir_ftype
, ino_ftype
,
1758 _("fixing ftype mismatch (%d/%d) in directory/child inode %" PRIu64
"/%" PRIu64
"\n"),
1759 dir_ftype
, ino_ftype
,
1761 libxfs_dir2_data_put_ftype(mp
, dep
, ino_ftype
);
1762 libxfs_dir2_data_log_entry(&da
, bp
, dep
);
1763 dir_hash_update_ftype(hashtab
, addr
,
1770 * check easy case first, regular inode, just bump
1771 * the link count and continue
1773 if (!inode_isadir(irec
, ino_offset
)) {
1774 add_inode_reached(irec
, ino_offset
);
1777 parent
= get_inode_parent(irec
, ino_offset
);
1778 ASSERT(parent
!= 0);
1781 * bump up the link counts in parent and child
1782 * directory but if the link doesn't agree with
1783 * the .. in the child, blow out the entry.
1784 * if the directory has already been reached,
1785 * blow away the entry also.
1787 if (is_inode_reached(irec
, ino_offset
)) {
1790 _("entry \"%s\" in dir %" PRIu64
" points to an already connected directory inode %" PRIu64
"\n"),
1791 fname
, ip
->i_ino
, inum
);
1792 } else if (parent
== ip
->i_ino
) {
1793 add_inode_reached(irec
, ino_offset
);
1794 add_inode_ref(current_irec
, current_ino_offset
);
1795 } else if (parent
== NULLFSINO
) {
1796 /* ".." was missing, but this entry refers to it,
1797 so, set it as the parent and mark for rebuild */
1799 _("entry \"%s\" in dir ino %" PRIu64
" doesn't have a .. entry, will set it in ino %" PRIu64
".\n"),
1800 fname
, ip
->i_ino
, inum
);
1801 set_inode_parent(irec
, ino_offset
, ip
->i_ino
);
1802 add_inode_reached(irec
, ino_offset
);
1803 add_inode_ref(current_irec
, current_ino_offset
);
1804 add_dotdot_update(XFS_INO_TO_AGNO(mp
, inum
), irec
,
1809 _("entry \"%s\" in dir inode %" PRIu64
" inconsistent with .. value (%" PRIu64
") in ino %" PRIu64
"\n"),
1810 fname
, ip
->i_ino
, parent
, inum
);
1813 if (inum
== orphanage_ino
)
1818 libxfs_dir2_data_log_entry(&da
, bp
, dep
);
1821 _("\twill clear entry \"%s\"\n"),
1824 do_warn(_("\twould clear entry \"%s\"\n"),
1829 *num_illegal
+= nbad
;
1831 libxfs_dir2_data_freescan(mp
, d
, &i
);
1833 libxfs_dir2_data_log_header(&da
, bp
);
1834 error
= -libxfs_trans_commit(tp
);
1837 _("directory block fixing failed (%d)\n"), error
);
1839 /* record the largest free space in the freetab for later checking */
1840 bf
= libxfs_dir2_data_bestfree_p(mp
, d
);
1841 freetab
->ents
[db
].v
= be16_to_cpu(bf
[0].length
);
1842 freetab
->ents
[db
].s
= 0;
1845 /* check v5 metadata */
1847 __check_dir3_header(
1848 struct xfs_mount
*mp
,
1857 if (be64_to_cpu(owner
) != ino
) {
1859 _("expected owner inode %" PRIu64
", got %llu, directory block %" PRIu64
"\n"),
1860 ino
, (unsigned long long)be64_to_cpu(owner
), bp
->b_bn
);
1863 /* verify block number */
1864 if (be64_to_cpu(blkno
) != bp
->b_bn
) {
1866 _("expected block %" PRIu64
", got %llu, directory inode %" PRIu64
"\n"),
1867 bp
->b_bn
, (unsigned long long)be64_to_cpu(blkno
), ino
);
1871 if (platform_uuid_compare(uuid
, &mp
->m_sb
.sb_meta_uuid
) != 0) {
1873 _("wrong FS UUID, directory inode %" PRIu64
" block %" PRIu64
"\n"),
1883 struct xfs_mount
*mp
,
1887 struct xfs_da3_blkinfo
*info
= bp
->b_addr
;
1889 return __check_dir3_header(mp
, bp
, ino
, info
->owner
, info
->blkno
,
1895 struct xfs_mount
*mp
,
1899 struct xfs_dir3_blk_hdr
*info
= bp
->b_addr
;
1901 return __check_dir3_header(mp
, bp
, ino
, info
->owner
, info
->blkno
,
1906 * Check contents of leaf-form block.
1909 longform_dir2_check_leaf(
1910 struct xfs_mount
*mp
,
1911 struct xfs_inode
*ip
,
1912 struct dir_hash_tab
*hashtab
,
1913 struct freetab
*freetab
)
1920 xfs_dir2_leaf_t
*leaf
;
1921 xfs_dir2_leaf_tail_t
*ltp
;
1923 struct xfs_dir2_leaf_entry
*ents
;
1924 struct xfs_dir3_icleaf_hdr leafhdr
;
1928 da_bno
= mp
->m_dir_geo
->leafblk
;
1929 error
= dir_read_buf(ip
, da_bno
, &bp
, &xfs_dir3_leaf1_buf_ops
, &fixit
);
1930 if (error
== EFSBADCRC
|| error
== EFSCORRUPTED
|| fixit
) {
1932 _("leaf block %u for directory inode %" PRIu64
" bad CRC\n"),
1937 _("can't read block %u for directory inode %" PRIu64
", error %d\n"),
1938 da_bno
, ip
->i_ino
, error
);
1943 libxfs_dir2_leaf_hdr_from_disk(mp
, &leafhdr
, leaf
);
1944 ents
= leafhdr
.ents
;
1945 ltp
= xfs_dir2_leaf_tail_p(mp
->m_dir_geo
, leaf
);
1946 bestsp
= xfs_dir2_leaf_bests_p(ltp
);
1947 if (!(leafhdr
.magic
== XFS_DIR2_LEAF1_MAGIC
||
1948 leafhdr
.magic
== XFS_DIR3_LEAF1_MAGIC
) ||
1949 leafhdr
.forw
|| leafhdr
.back
||
1950 leafhdr
.count
< leafhdr
.stale
||
1951 leafhdr
.count
> mp
->m_dir_geo
->leaf_max_ents
||
1952 (char *)&ents
[leafhdr
.count
] > (char *)bestsp
) {
1954 _("leaf block %u for directory inode %" PRIu64
" bad header\n"),
1956 libxfs_buf_relse(bp
);
1960 if (leafhdr
.magic
== XFS_DIR3_LEAF1_MAGIC
) {
1961 error
= check_da3_header(mp
, bp
, ip
->i_ino
);
1963 libxfs_buf_relse(bp
);
1968 seeval
= dir_hash_see_all(hashtab
, ents
, leafhdr
.count
, leafhdr
.stale
);
1969 if (dir_hash_check(hashtab
, ip
, seeval
)) {
1970 libxfs_buf_relse(bp
);
1973 badtail
= freetab
->nents
!= be32_to_cpu(ltp
->bestcount
);
1974 for (i
= 0; !badtail
&& i
< be32_to_cpu(ltp
->bestcount
); i
++) {
1975 freetab
->ents
[i
].s
= 1;
1976 badtail
= freetab
->ents
[i
].v
!= be16_to_cpu(bestsp
[i
]);
1980 _("leaf block %u for directory inode %" PRIu64
" bad tail\n"),
1982 libxfs_buf_relse(bp
);
1985 libxfs_buf_relse(bp
);
1990 * Check contents of the node blocks (leaves)
1991 * Looks for matching hash values for the data entries.
1994 longform_dir2_check_node(
1995 struct xfs_mount
*mp
,
1996 struct xfs_inode
*ip
,
1997 struct dir_hash_tab
*hashtab
,
1998 struct freetab
*freetab
)
2003 xfs_dir2_free_t
*free
;
2005 xfs_dir2_leaf_t
*leaf
;
2006 xfs_fileoff_t next_da_bno
;
2009 struct xfs_dir2_leaf_entry
*ents
;
2010 struct xfs_dir3_icleaf_hdr leafhdr
;
2011 struct xfs_dir3_icfree_hdr freehdr
;
2016 for (da_bno
= mp
->m_dir_geo
->leafblk
, next_da_bno
= 0;
2017 next_da_bno
!= NULLFILEOFF
&& da_bno
< mp
->m_dir_geo
->freeblk
;
2018 da_bno
= (xfs_dablk_t
)next_da_bno
) {
2019 next_da_bno
= da_bno
+ mp
->m_dir_geo
->fsbcount
- 1;
2020 if (bmap_next_offset(ip
, &next_da_bno
))
2024 * we need to use the da3 node verifier here as it handles the
2025 * fact that reading the leaf hash tree blocks can return either
2026 * leaf or node blocks and calls the correct verifier. If we get
2027 * a node block, then we'll skip it below based on a magic
2030 error
= dir_read_buf(ip
, da_bno
, &bp
, &xfs_da3_node_buf_ops
,
2034 _("can't read leaf block %u for directory inode %" PRIu64
", error %d\n"),
2035 da_bno
, ip
->i_ino
, error
);
2039 libxfs_dir2_leaf_hdr_from_disk(mp
, &leafhdr
, leaf
);
2040 ents
= leafhdr
.ents
;
2041 if (!(leafhdr
.magic
== XFS_DIR2_LEAFN_MAGIC
||
2042 leafhdr
.magic
== XFS_DIR3_LEAFN_MAGIC
||
2043 leafhdr
.magic
== XFS_DA_NODE_MAGIC
||
2044 leafhdr
.magic
== XFS_DA3_NODE_MAGIC
)) {
2046 _("unknown magic number %#x for block %u in directory inode %" PRIu64
"\n"),
2047 leafhdr
.magic
, da_bno
, ip
->i_ino
);
2048 libxfs_buf_relse(bp
);
2052 /* check v5 metadata */
2053 if (leafhdr
.magic
== XFS_DIR3_LEAFN_MAGIC
||
2054 leafhdr
.magic
== XFS_DA3_NODE_MAGIC
) {
2055 error
= check_da3_header(mp
, bp
, ip
->i_ino
);
2057 libxfs_buf_relse(bp
);
2063 if (leafhdr
.magic
== XFS_DA_NODE_MAGIC
||
2064 leafhdr
.magic
== XFS_DA3_NODE_MAGIC
) {
2065 libxfs_buf_relse(bp
);
2070 * If there's a validator error, we need to ensure that we got
2071 * the right ops on the buffer for when we write it back out.
2073 bp
->b_ops
= &xfs_dir3_leafn_buf_ops
;
2074 if (leafhdr
.count
> mp
->m_dir_geo
->leaf_max_ents
||
2075 leafhdr
.count
< leafhdr
.stale
) {
2077 _("leaf block %u for directory inode %" PRIu64
" bad header\n"),
2079 libxfs_buf_relse(bp
);
2082 seeval
= dir_hash_see_all(hashtab
, ents
,
2083 leafhdr
.count
, leafhdr
.stale
);
2084 libxfs_buf_relse(bp
);
2085 if (seeval
!= DIR_HASH_CK_OK
)
2088 if (dir_hash_check(hashtab
, ip
, seeval
))
2091 for (da_bno
= mp
->m_dir_geo
->freeblk
, next_da_bno
= 0;
2092 next_da_bno
!= NULLFILEOFF
;
2093 da_bno
= (xfs_dablk_t
)next_da_bno
) {
2094 next_da_bno
= da_bno
+ mp
->m_dir_geo
->fsbcount
- 1;
2095 if (bmap_next_offset(ip
, &next_da_bno
))
2098 error
= dir_read_buf(ip
, da_bno
, &bp
, &xfs_dir3_free_buf_ops
,
2102 _("can't read freespace block %u for directory inode %" PRIu64
", error %d\n"),
2103 da_bno
, ip
->i_ino
, error
);
2107 libxfs_dir2_free_hdr_from_disk(mp
, &freehdr
, free
);
2108 bests
= freehdr
.bests
;
2109 fdb
= xfs_dir2_da_to_db(mp
->m_dir_geo
, da_bno
);
2110 if (!(freehdr
.magic
== XFS_DIR2_FREE_MAGIC
||
2111 freehdr
.magic
== XFS_DIR3_FREE_MAGIC
) ||
2113 (fdb
- xfs_dir2_byte_to_db(mp
->m_dir_geo
, XFS_DIR2_FREE_OFFSET
)) *
2114 mp
->m_dir_geo
->free_max_bests
||
2115 freehdr
.nvalid
< freehdr
.nused
) {
2117 _("free block %u for directory inode %" PRIu64
" bad header\n"),
2119 libxfs_buf_relse(bp
);
2123 if (freehdr
.magic
== XFS_DIR3_FREE_MAGIC
) {
2124 error
= check_dir3_header(mp
, bp
, ip
->i_ino
);
2126 libxfs_buf_relse(bp
);
2130 for (i
= used
= 0; i
< freehdr
.nvalid
; i
++) {
2131 if (i
+ freehdr
.firstdb
>= freetab
->nents
||
2132 freetab
->ents
[i
+ freehdr
.firstdb
].v
!=
2133 be16_to_cpu(bests
[i
])) {
2135 _("free block %u entry %i for directory ino %" PRIu64
" bad\n"),
2136 da_bno
, i
, ip
->i_ino
);
2137 libxfs_buf_relse(bp
);
2140 used
+= be16_to_cpu(bests
[i
]) != NULLDATAOFF
;
2141 freetab
->ents
[i
+ freehdr
.firstdb
].s
= 1;
2143 if (used
!= freehdr
.nused
) {
2145 _("free block %u for directory inode %" PRIu64
" bad nused\n"),
2147 libxfs_buf_relse(bp
);
2150 libxfs_buf_relse(bp
);
2152 for (i
= 0; i
< freetab
->nents
; i
++) {
2153 if ((freetab
->ents
[i
].s
== 0) &&
2154 (freetab
->ents
[i
].v
!= NULLDATAOFF
)) {
2156 _("missing freetab entry %u for directory inode %" PRIu64
"\n"),
2165 * If a directory is corrupt, we need to read in as many entries as possible,
2166 * destroy the entry and create a new one with recovered name/inode pairs.
2167 * (ie. get libxfs to do all the grunt work)
2170 longform_dir2_entry_check(
2171 struct xfs_mount
*mp
,
2173 struct xfs_inode
*ip
,
2176 struct ino_tree_node
*irec
,
2178 struct dir_hash_tab
*hashtab
)
2186 xfs_fileoff_t next_da_bno
;
2189 struct xfs_da_args args
;
2192 freetab
= malloc(FREETAB_SIZE(ip
->i_d
.di_size
/ mp
->m_dir_geo
->blksize
));
2194 do_error(_("malloc failed in %s (%" PRId64
" bytes)\n"),
2196 FREETAB_SIZE(ip
->i_d
.di_size
/ mp
->m_dir_geo
->blksize
));
2199 freetab
->naents
= ip
->i_d
.di_size
/ mp
->m_dir_geo
->blksize
;
2201 for (i
= 0; i
< freetab
->naents
; i
++) {
2202 freetab
->ents
[i
].v
= NULLDATAOFF
;
2203 freetab
->ents
[i
].s
= 0;
2206 /* is this a block, leaf, or node directory? */
2208 args
.geo
= mp
->m_dir_geo
;
2209 libxfs_dir2_isblock(&args
, &isblock
);
2210 libxfs_dir2_isleaf(&args
, &isleaf
);
2212 /* check directory "data" blocks (ie. name/inode pairs) */
2213 for (da_bno
= 0, next_da_bno
= 0;
2214 next_da_bno
!= NULLFILEOFF
&& da_bno
< mp
->m_dir_geo
->leafblk
;
2215 da_bno
= (xfs_dablk_t
)next_da_bno
) {
2216 const struct xfs_buf_ops
*ops
;
2218 struct xfs_dir2_data_hdr
*d
;
2220 next_da_bno
= da_bno
+ mp
->m_dir_geo
->fsbcount
- 1;
2221 if (bmap_next_offset(ip
, &next_da_bno
)) {
2223 * if this is the first block, there isn't anything we
2224 * can recover so we just trash it.
2234 ops
= &xfs_dir3_block_buf_ops
;
2236 ops
= &xfs_dir3_data_buf_ops
;
2238 error
= dir_read_buf(ip
, da_bno
, &bp
, ops
, &fixit
);
2241 _("can't read data block %u for directory inode %" PRIu64
" error %d\n"),
2242 da_bno
, ino
, error
);
2246 * we try to read all "data" blocks, but if we are in
2247 * block form and we fail, there isn't anything else to
2248 * read, and nothing we can do but trash it.
2257 /* check v5 metadata */
2259 if (be32_to_cpu(d
->magic
) == XFS_DIR3_BLOCK_MAGIC
||
2260 be32_to_cpu(d
->magic
) == XFS_DIR3_DATA_MAGIC
) {
2261 error
= check_dir3_header(mp
, bp
, ino
);
2270 longform_dir2_entry_check_data(mp
, ip
, num_illegal
, need_dot
,
2271 irec
, ino_offset
, bp
, hashtab
,
2272 &freetab
, da_bno
, isblock
);
2276 libxfs_buf_relse(bp
);
2278 fixit
|= (*num_illegal
!= 0) || dir2_is_badino(ino
) || *need_dot
;
2280 if (!dotdot_update
) {
2281 /* check btree and freespace */
2283 struct xfs_dir2_data_hdr
*block
;
2284 xfs_dir2_block_tail_t
*btp
;
2285 xfs_dir2_leaf_entry_t
*blp
;
2288 btp
= xfs_dir2_block_tail_p(mp
->m_dir_geo
, block
);
2289 blp
= xfs_dir2_block_leaf_p(btp
);
2290 seeval
= dir_hash_see_all(hashtab
, blp
,
2291 be32_to_cpu(btp
->count
),
2292 be32_to_cpu(btp
->stale
));
2293 if (dir_hash_check(hashtab
, ip
, seeval
))
2295 } else if (isleaf
) {
2296 fixit
|= longform_dir2_check_leaf(mp
, ip
, hashtab
,
2299 fixit
|= longform_dir2_check_node(mp
, ip
, hashtab
,
2305 libxfs_buf_relse(bp
);
2307 if (!no_modify
&& (fixit
|| dotdot_update
)) {
2308 longform_dir2_rebuild(mp
, ino
, ip
, irec
, ino_offset
, hashtab
);
2312 if (fixit
|| dotdot_update
)
2314 _("would rebuild directory inode %" PRIu64
"\n"), ino
);
2321 * shortform directory v2 processing routines -- entry verification and
2322 * bad entry deletion (pruning).
2324 static struct xfs_dir2_sf_entry
*
2325 shortform_dir2_junk(
2326 struct xfs_mount
*mp
,
2327 struct xfs_dir2_sf_hdr
*sfp
,
2328 struct xfs_dir2_sf_entry
*sfep
,
2335 struct xfs_dir2_sf_entry
*next_sfep
;
2339 if (lino
== orphanage_ino
)
2342 next_elen
= libxfs_dir2_sf_entsize(mp
, sfp
, sfep
->namelen
);
2343 next_sfep
= libxfs_dir2_sf_nextentry(mp
, sfp
, sfep
);
2346 * if we are just checking, simply return the pointer to the next entry
2347 * here so that the checking loop can continue.
2350 do_warn(_("would junk entry\n"));
2355 * now move all the remaining entries down over the junked entry and
2356 * clear the newly unused bytes at the tail of the directory region.
2358 next_len
= *max_size
- ((intptr_t)next_sfep
- (intptr_t)sfp
);
2359 *max_size
-= next_elen
;
2360 *bytes_deleted
+= next_elen
;
2362 memmove(sfep
, next_sfep
, next_len
);
2363 memset((void *)((intptr_t)sfep
+ next_len
), 0, next_elen
);
2368 * WARNING: drop the index i by one so it matches the decremented count
2369 * for accurate comparisons in the loop test
2374 do_warn(_("junking entry\n"));
2381 shortform_dir2_entry_check(
2382 struct xfs_mount
*mp
,
2384 struct xfs_inode
*ip
,
2386 struct ino_tree_node
*current_irec
,
2387 int current_ino_offset
,
2388 struct dir_hash_tab
*hashtab
)
2392 struct xfs_dir2_sf_hdr
*sfp
;
2393 struct xfs_dir2_sf_entry
*sfep
;
2394 struct xfs_dir2_sf_entry
*next_sfep
;
2395 struct xfs_ifork
*ifp
;
2396 struct ino_tree_node
*irec
;
2403 char fname
[MAXNAMELEN
+ 1];
2407 sfp
= (struct xfs_dir2_sf_hdr
*) ifp
->if_u1
.if_data
;
2411 max_size
= ifp
->if_bytes
;
2412 ASSERT(ip
->i_d
.di_size
<= ifp
->if_bytes
);
2415 * if just rebuild a directory due to a "..", update and return
2417 if (dotdot_update
) {
2418 parent
= get_inode_parent(current_irec
, current_ino_offset
);
2421 _("would set .. in sf dir inode %" PRIu64
" to %" PRIu64
"\n"),
2425 _("setting .. in sf dir inode %" PRIu64
" to %" PRIu64
"\n"),
2427 libxfs_dir2_sf_put_parent_ino(sfp
, parent
);
2434 * no '.' entry in shortform dirs, just bump up ref count by 1
2435 * '..' was already (or will be) accounted for and checked when
2436 * the directory is reached or will be taken care of when the
2437 * directory is moved to orphanage.
2439 add_inode_ref(current_irec
, current_ino_offset
);
2442 * Initialise i8 counter -- the parent inode number counts as well.
2444 i8
= libxfs_dir2_sf_get_parent_ino(sfp
) > XFS_DIR2_MAX_SHORT_INUM
;
2447 * now run through entries, stop at first bad entry, don't need
2448 * to skip over '..' since that's encoded in its own field and
2449 * no need to worry about '.' since it doesn't exist.
2451 sfep
= next_sfep
= xfs_dir2_sf_firstentry(sfp
);
2453 for (i
= 0; i
< sfp
->count
&& max_size
>
2454 (intptr_t)next_sfep
- (intptr_t)sfp
;
2455 sfep
= next_sfep
, i
++) {
2458 lino
= libxfs_dir2_sf_get_ino(mp
, sfp
, sfep
);
2460 namelen
= sfep
->namelen
;
2462 ASSERT(no_modify
|| namelen
> 0);
2464 if (no_modify
&& namelen
== 0) {
2466 * if we're really lucky, this is
2467 * the last entry in which case we
2468 * can use the dir size to set the
2469 * namelen value. otherwise, forget
2470 * it because we're not going to be
2471 * able to find the next entry.
2475 if (i
== sfp
->count
- 1) {
2476 namelen
= ip
->i_d
.di_size
-
2477 ((intptr_t) &sfep
->name
[0] -
2481 * don't process the rest of the directory,
2482 * break out of processing loop
2486 } else if (no_modify
&& (intptr_t) sfep
- (intptr_t) sfp
+
2487 + libxfs_dir2_sf_entsize(mp
, sfp
, sfep
->namelen
)
2488 > ip
->i_d
.di_size
) {
2491 if (i
== sfp
->count
- 1) {
2492 namelen
= ip
->i_d
.di_size
-
2493 ((intptr_t) &sfep
->name
[0] -
2497 * don't process the rest of the directory,
2498 * break out of processing loop
2504 memmove(fname
, sfep
->name
, sfep
->namelen
);
2505 fname
[sfep
->namelen
] = '\0';
2507 ASSERT(no_modify
|| (lino
!= NULLFSINO
&& lino
!= 0));
2508 ASSERT(no_modify
|| libxfs_verify_dir_ino(mp
, lino
));
2511 * Also skip entries with bogus inode numbers if we're
2512 * in no modify mode.
2515 if (no_modify
&& !libxfs_verify_dir_ino(mp
, lino
)) {
2516 next_sfep
= libxfs_dir2_sf_nextentry(mp
, sfp
, sfep
);
2520 irec
= find_inode_rec(mp
, XFS_INO_TO_AGNO(mp
, lino
),
2521 XFS_INO_TO_AGINO(mp
, lino
));
2525 _("entry \"%s\" in shortform directory %" PRIu64
" references non-existent inode %" PRIu64
"\n"),
2527 next_sfep
= shortform_dir2_junk(mp
, sfp
, sfep
, lino
,
2528 &max_size
, &i
, &bytes_deleted
,
2533 ino_offset
= XFS_INO_TO_AGINO(mp
, lino
) - irec
->ino_startnum
;
2536 * if it's a free inode, blow out the entry.
2537 * by now, any inode that we think is free
2540 if (is_inode_free(irec
, ino_offset
)) {
2542 _("entry \"%s\" in shortform directory inode %" PRIu64
" points to free inode %" PRIu64
"\n"),
2544 next_sfep
= shortform_dir2_junk(mp
, sfp
, sfep
, lino
,
2545 &max_size
, &i
, &bytes_deleted
,
2550 * check if this inode is lost+found dir in the root
2552 if (ino
== mp
->m_sb
.sb_rootino
&& strcmp(fname
, ORPHANAGE
) == 0) {
2554 * if it's not a directory, trash it
2556 if (!inode_isadir(irec
, ino_offset
)) {
2558 _("%s (ino %" PRIu64
") in root (%" PRIu64
") is not a directory"),
2559 ORPHANAGE
, lino
, ino
);
2560 next_sfep
= shortform_dir2_junk(mp
, sfp
, sfep
,
2561 lino
, &max_size
, &i
,
2562 &bytes_deleted
, ino_dirty
);
2566 * if this is a dup, it will be picked up below,
2567 * otherwise, mark it as the orphanage for later.
2570 orphanage_ino
= lino
;
2573 * check for duplicate names in directory.
2575 if (!dir_hash_add(mp
, hashtab
, (xfs_dir2_dataptr_t
)
2576 (sfep
- xfs_dir2_sf_firstentry(sfp
)),
2577 lino
, sfep
->namelen
, sfep
->name
,
2578 libxfs_dir2_sf_get_ftype(mp
, sfep
))) {
2580 _("entry \"%s\" (ino %" PRIu64
") in dir %" PRIu64
" is a duplicate name"),
2582 next_sfep
= shortform_dir2_junk(mp
, sfp
, sfep
, lino
,
2583 &max_size
, &i
, &bytes_deleted
,
2588 if (!inode_isadir(irec
, ino_offset
)) {
2590 * check easy case first, regular inode, just bump
2593 add_inode_reached(irec
, ino_offset
);
2595 parent
= get_inode_parent(irec
, ino_offset
);
2598 * bump up the link counts in parent and child.
2599 * directory but if the link doesn't agree with
2600 * the .. in the child, blow out the entry
2602 if (is_inode_reached(irec
, ino_offset
)) {
2604 _("entry \"%s\" in directory inode %" PRIu64
2605 " references already connected inode %" PRIu64
".\n"),
2607 next_sfep
= shortform_dir2_junk(mp
, sfp
, sfep
,
2608 lino
, &max_size
, &i
,
2609 &bytes_deleted
, ino_dirty
);
2611 } else if (parent
== ino
) {
2612 add_inode_reached(irec
, ino_offset
);
2613 add_inode_ref(current_irec
, current_ino_offset
);
2614 } else if (parent
== NULLFSINO
) {
2615 /* ".." was missing, but this entry refers to it,
2616 so, set it as the parent and mark for rebuild */
2618 _("entry \"%s\" in dir ino %" PRIu64
" doesn't have a .. entry, will set it in ino %" PRIu64
".\n"),
2620 set_inode_parent(irec
, ino_offset
, ino
);
2621 add_inode_reached(irec
, ino_offset
);
2622 add_inode_ref(current_irec
, current_ino_offset
);
2623 add_dotdot_update(XFS_INO_TO_AGNO(mp
, lino
),
2627 _("entry \"%s\" in directory inode %" PRIu64
2628 " not consistent with .. value (%" PRIu64
2629 ") in inode %" PRIu64
",\n"),
2630 fname
, ino
, parent
, lino
);
2631 next_sfep
= shortform_dir2_junk(mp
, sfp
, sfep
,
2632 lino
, &max_size
, &i
,
2633 &bytes_deleted
, ino_dirty
);
2638 /* validate ftype field if supported */
2639 if (xfs_sb_version_hasftype(&mp
->m_sb
)) {
2643 dir_ftype
= libxfs_dir2_sf_get_ftype(mp
, sfep
);
2644 ino_ftype
= get_inode_ftype(irec
, ino_offset
);
2646 if (dir_ftype
!= ino_ftype
) {
2649 _("would fix ftype mismatch (%d/%d) in directory/child inode %" PRIu64
"/%" PRIu64
"\n"),
2650 dir_ftype
, ino_ftype
,
2654 _("fixing ftype mismatch (%d/%d) in directory/child inode %" PRIu64
"/%" PRIu64
"\n"),
2655 dir_ftype
, ino_ftype
,
2657 libxfs_dir2_sf_put_ftype(mp
, sfep
,
2659 dir_hash_update_ftype(hashtab
,
2660 (xfs_dir2_dataptr_t
)(sfep
- xfs_dir2_sf_firstentry(sfp
)),
2667 if (lino
> XFS_DIR2_MAX_SHORT_INUM
)
2671 * go onto next entry - we have to take entries with bad namelen
2672 * into account in no modify mode since we calculate size based
2675 ASSERT(no_modify
|| bad_sfnamelen
== 0);
2676 next_sfep
= (struct xfs_dir2_sf_entry
*)((intptr_t)sfep
+
2678 ? libxfs_dir2_sf_entsize(mp
, sfp
, namelen
)
2679 : libxfs_dir2_sf_entsize(mp
, sfp
, sfep
->namelen
)));
2682 if (sfp
->i8count
!= i8
) {
2684 do_warn(_("would fix i8count in inode %" PRIu64
"\n"),
2688 struct xfs_dir2_sf_entry
*tmp_sfep
;
2690 tmp_sfep
= next_sfep
;
2691 process_sf_dir2_fixi8(mp
, sfp
, &tmp_sfep
);
2693 (intptr_t)next_sfep
-
2695 next_sfep
= tmp_sfep
;
2699 do_warn(_("fixing i8count in inode %" PRIu64
"\n"),
2705 * sync up sizes if required
2707 if (*ino_dirty
&& bytes_deleted
> 0) {
2709 libxfs_idata_realloc(ip
, -bytes_deleted
, XFS_DATA_FORK
);
2710 ip
->i_d
.di_size
-= bytes_deleted
;
2713 if (ip
->i_d
.di_size
!= ip
->i_df
.if_bytes
) {
2714 ASSERT(ip
->i_df
.if_bytes
== (xfs_fsize_t
)
2715 ((intptr_t) next_sfep
- (intptr_t) sfp
));
2716 ip
->i_d
.di_size
= (xfs_fsize_t
)
2717 ((intptr_t) next_sfep
- (intptr_t) sfp
);
2719 _("setting size to %" PRId64
" bytes to reflect junked entries\n"),
2726 * processes all reachable inodes in directories
2730 struct xfs_mount
*mp
,
2731 xfs_agnumber_t agno
,
2732 struct ino_tree_node
*irec
,
2736 struct xfs_inode
*ip
;
2737 struct xfs_trans
*tp
;
2738 struct dir_hash_tab
*hashtab
;
2740 int dirty
, num_illegal
, error
, nres
;
2742 ino
= XFS_AGINO_TO_INO(mp
, agno
, irec
->ino_startnum
+ ino_offset
);
2745 * open up directory inode, check all entries,
2746 * then call prune_dir_entries to remove all
2747 * remaining illegal directory entries.
2750 ASSERT(!is_inode_refchecked(irec
, ino_offset
) || dotdot_update
);
2752 error
= -libxfs_iget(mp
, NULL
, ino
, 0, &ip
);
2756 _("couldn't map inode %" PRIu64
", err = %d\n"),
2760 _("couldn't map inode %" PRIu64
", err = %d\n"),
2763 * see below for what we're doing if this
2764 * is root. Why do we need to do this here?
2765 * to ensure that the root doesn't show up
2766 * as being disconnected in the no_modify case.
2768 if (mp
->m_sb
.sb_rootino
== ino
) {
2769 add_inode_reached(irec
, 0);
2770 add_inode_ref(irec
, 0);
2774 add_inode_refchecked(irec
, 0);
2778 need_dot
= dirty
= num_illegal
= 0;
2780 if (mp
->m_sb
.sb_rootino
== ino
) {
2782 * mark root inode reached and bump up
2783 * link count for root inode to account
2784 * for '..' entry since the root inode is
2785 * never reached by a parent. we know
2786 * that root's '..' is always good --
2787 * guaranteed by phase 3 and/or below.
2789 add_inode_reached(irec
, ino_offset
);
2792 add_inode_refchecked(irec
, ino_offset
);
2794 hashtab
= dir_hash_init(ip
->i_d
.di_size
);
2797 * look for bogus entries
2799 switch (ip
->i_df
.if_format
) {
2800 case XFS_DINODE_FMT_EXTENTS
:
2801 case XFS_DINODE_FMT_BTREE
:
2803 * also check for missing '.' in longform dirs.
2804 * missing .. entries are added if required when
2805 * the directory is connected to lost+found. but
2806 * we need to create '.' entries here.
2808 longform_dir2_entry_check(mp
, ino
, ip
,
2809 &num_illegal
, &need_dot
,
2814 case XFS_DINODE_FMT_LOCAL
:
2816 * using the remove reservation is overkill
2817 * since at most we'll only need to log the
2818 * inode but it's easier than wedging a
2819 * new define in ourselves.
2821 nres
= no_modify
? 0 : XFS_REMOVE_SPACE_RES(mp
);
2822 error
= -libxfs_trans_alloc(mp
, &M_RES(mp
)->tr_remove
,
2827 libxfs_trans_ijoin(tp
, ip
, 0);
2829 shortform_dir2_entry_check(mp
, ino
, ip
, &dirty
,
2833 ASSERT(dirty
== 0 || (dirty
&& !no_modify
));
2835 libxfs_trans_log_inode(tp
, ip
,
2836 XFS_ILOG_CORE
| XFS_ILOG_DDATA
);
2837 error
= -libxfs_trans_commit(tp
);
2840 _("error %d fixing shortform directory %llu\n"),
2842 (unsigned long long)ip
->i_ino
);
2844 libxfs_trans_cancel(tp
);
2851 dir_hash_done(hashtab
);
2854 * if we have to create a .. for /, do it now *before*
2855 * we delete the bogus entries, otherwise the directory
2856 * could transform into a shortform dir which would
2857 * probably cause the simulation to choke. Even
2858 * if the illegal entries get shifted around, it's ok
2859 * because the entries are structurally intact and in
2860 * in hash-value order so the simulation won't get confused
2861 * if it has to move them around.
2863 if (!no_modify
&& need_root_dotdot
&& ino
== mp
->m_sb
.sb_rootino
) {
2864 ASSERT(ip
->i_df
.if_format
!= XFS_DINODE_FMT_LOCAL
);
2866 do_warn(_("recreating root directory .. entry\n"));
2868 nres
= XFS_MKDIR_SPACE_RES(mp
, 2);
2869 error
= -libxfs_trans_alloc(mp
, &M_RES(mp
)->tr_mkdir
,
2874 libxfs_trans_ijoin(tp
, ip
, 0);
2876 error
= -libxfs_dir_createname(tp
, ip
, &xfs_name_dotdot
,
2880 _("can't make \"..\" entry in root inode %" PRIu64
", createname error %d\n"), ino
, error
);
2882 libxfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
2883 error
= -libxfs_trans_commit(tp
);
2886 _("root inode \"..\" entry recreation failed (%d)\n"), error
);
2888 need_root_dotdot
= 0;
2889 } else if (need_root_dotdot
&& ino
== mp
->m_sb
.sb_rootino
) {
2890 do_warn(_("would recreate root directory .. entry\n"));
2894 * if we need to create the '.' entry, do so only if
2895 * the directory is a longform dir. if it's been
2896 * turned into a shortform dir, then the inode is ok
2897 * since shortform dirs have no '.' entry and the inode
2898 * has already been committed by prune_lf_dir_entry().
2902 * bump up our link count but don't
2903 * bump up the inode link count. chances
2904 * are good that even though we lost '.'
2905 * the inode link counts reflect '.' so
2906 * leave the inode link count alone and if
2907 * it turns out to be wrong, we'll catch
2910 add_inode_ref(irec
, ino_offset
);
2914 _("would create missing \".\" entry in dir ino %" PRIu64
"\n"),
2916 } else if (ip
->i_df
.if_format
!= XFS_DINODE_FMT_LOCAL
) {
2918 * need to create . entry in longform dir.
2921 _("creating missing \".\" entry in dir ino %" PRIu64
"\n"), ino
);
2923 nres
= XFS_MKDIR_SPACE_RES(mp
, 1);
2924 error
= -libxfs_trans_alloc(mp
, &M_RES(mp
)->tr_mkdir
,
2929 libxfs_trans_ijoin(tp
, ip
, 0);
2931 error
= -libxfs_dir_createname(tp
, ip
, &xfs_name_dot
,
2935 _("can't make \".\" entry in dir ino %" PRIu64
", createname error %d\n"),
2938 libxfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
2939 error
= -libxfs_trans_commit(tp
);
2942 _("root inode \".\" entry recreation failed (%d)\n"), error
);
2949 * mark realtime bitmap and summary inodes as reached.
2950 * quota inode will be marked here as well
2953 mark_standalone_inodes(xfs_mount_t
*mp
)
2955 ino_tree_node_t
*irec
;
2958 irec
= find_inode_rec(mp
, XFS_INO_TO_AGNO(mp
, mp
->m_sb
.sb_rbmino
),
2959 XFS_INO_TO_AGINO(mp
, mp
->m_sb
.sb_rbmino
));
2961 offset
= XFS_INO_TO_AGINO(mp
, mp
->m_sb
.sb_rbmino
) -
2964 add_inode_reached(irec
, offset
);
2966 irec
= find_inode_rec(mp
, XFS_INO_TO_AGNO(mp
, mp
->m_sb
.sb_rsumino
),
2967 XFS_INO_TO_AGINO(mp
, mp
->m_sb
.sb_rsumino
));
2969 offset
= XFS_INO_TO_AGINO(mp
, mp
->m_sb
.sb_rsumino
) -
2972 add_inode_reached(irec
, offset
);
2975 if (mp
->m_sb
.sb_uquotino
2976 && mp
->m_sb
.sb_uquotino
!= NULLFSINO
) {
2977 irec
= find_inode_rec(mp
, XFS_INO_TO_AGNO(mp
,
2978 mp
->m_sb
.sb_uquotino
),
2979 XFS_INO_TO_AGINO(mp
, mp
->m_sb
.sb_uquotino
));
2980 offset
= XFS_INO_TO_AGINO(mp
, mp
->m_sb
.sb_uquotino
)
2981 - irec
->ino_startnum
;
2982 add_inode_reached(irec
, offset
);
2984 if (mp
->m_sb
.sb_gquotino
2985 && mp
->m_sb
.sb_gquotino
!= NULLFSINO
) {
2986 irec
= find_inode_rec(mp
, XFS_INO_TO_AGNO(mp
,
2987 mp
->m_sb
.sb_gquotino
),
2988 XFS_INO_TO_AGINO(mp
, mp
->m_sb
.sb_gquotino
));
2989 offset
= XFS_INO_TO_AGINO(mp
, mp
->m_sb
.sb_gquotino
)
2990 - irec
->ino_startnum
;
2991 add_inode_reached(irec
, offset
);
2993 if (mp
->m_sb
.sb_pquotino
2994 && mp
->m_sb
.sb_pquotino
!= NULLFSINO
) {
2995 irec
= find_inode_rec(mp
, XFS_INO_TO_AGNO(mp
,
2996 mp
->m_sb
.sb_pquotino
),
2997 XFS_INO_TO_AGINO(mp
, mp
->m_sb
.sb_pquotino
));
2998 offset
= XFS_INO_TO_AGINO(mp
, mp
->m_sb
.sb_pquotino
)
2999 - irec
->ino_startnum
;
3000 add_inode_reached(irec
, offset
);
3006 check_for_orphaned_inodes(
3008 xfs_agnumber_t agno
,
3009 ino_tree_node_t
*irec
)
3014 for (i
= 0; i
< XFS_INODES_PER_CHUNK
; i
++) {
3015 ASSERT(is_inode_confirmed(irec
, i
));
3016 if (is_inode_free(irec
, i
))
3019 if (is_inode_reached(irec
, i
))
3022 ASSERT(inode_isadir(irec
, i
) ||
3023 num_inode_references(irec
, i
) == 0);
3025 ino
= XFS_AGINO_TO_INO(mp
, agno
, i
+ irec
->ino_startnum
);
3026 if (inode_isadir(irec
, i
))
3027 do_warn(_("disconnected dir inode %" PRIu64
", "), ino
);
3029 do_warn(_("disconnected inode %" PRIu64
", "), ino
);
3032 orphanage_ino
= mk_orphanage(mp
);
3033 do_warn(_("moving to %s\n"), ORPHANAGE
);
3034 mv_orphanage(mp
, ino
, inode_isadir(irec
, i
));
3036 do_warn(_("would move to %s\n"), ORPHANAGE
);
3039 * for read-only case, even though the inode isn't
3040 * really reachable, set the flag (and bump our link
3041 * count) anyway to fool phase 7
3043 add_inode_reached(irec
, i
);
3049 struct workqueue
*wq
,
3050 xfs_agnumber_t agno
,
3053 struct ino_tree_node
*irec
= arg
;
3056 for (i
= 0; i
< XFS_INODES_PER_CHUNK
; i
++) {
3057 if (inode_isadir(irec
, i
))
3058 process_dir_inode(wq
->wq_ctx
, agno
, irec
, i
);
3064 struct workqueue
*wq
,
3065 xfs_agnumber_t agno
,
3068 struct ino_tree_node
*irec
;
3069 prefetch_args_t
*pf_args
= arg
;
3070 struct workqueue lwq
;
3071 struct xfs_mount
*mp
= wq
->wq_ctx
;
3073 wait_for_inode_prefetch(pf_args
);
3076 do_log(_(" - agno = %d\n"), agno
);
3079 * The more AGs we have in flight at once, the fewer processing threads
3080 * per AG. This means we don't overwhelm the machine with hundreds of
3081 * threads when we start acting on lots of AGs at once. We just want
3082 * enough that we can keep multiple CPUs busy across multiple AGs.
3084 workqueue_create_bound(&lwq
, mp
, ag_stride
, 1000);
3086 for (irec
= findfirst_inode_rec(agno
); irec
; irec
= next_ino_rec(irec
)) {
3087 if (irec
->ino_isa_dir
== 0)
3091 sem_post(&pf_args
->ra_count
);
3095 sem_getvalue(&pf_args
->ra_count
, &i
);
3097 "processing inode chunk %p in AG %d (sem count = %d)",
3103 queue_work(&lwq
, do_dir_inode
, agno
, irec
);
3105 destroy_work_queue(&lwq
);
3106 cleanup_inode_prefetch(pf_args
);
3110 update_missing_dotdot_entries(
3113 dotdot_update_t
*dir
;
3116 * these entries parents were updated, rebuild them again
3117 * set dotdot_update flag so processing routines do not count links
3120 while (!list_empty(&dotdot_update_list
)) {
3121 dir
= list_entry(dotdot_update_list
.prev
, struct dotdot_update
,
3123 list_del(&dir
->list
);
3124 process_dir_inode(mp
, dir
->agno
, dir
->irec
, dir
->ino_offset
);
3131 struct xfs_mount
*mp
)
3133 do_inode_prefetch(mp
, ag_stride
, traverse_function
, false, true);
3137 phase6(xfs_mount_t
*mp
)
3139 ino_tree_node_t
*irec
;
3142 memset(&zerocr
, 0, sizeof(struct cred
));
3143 memset(&zerofsx
, 0, sizeof(struct fsxattr
));
3146 do_log(_("Phase 6 - check inode connectivity...\n"));
3148 incore_ext_teardown(mp
);
3150 add_ino_ex_data(mp
);
3153 * verify existence of root directory - if we have to
3154 * make one, it's ok for the incore data structs not to
3155 * know about it since everything about it (and the other
3156 * inodes in its chunk if a new chunk was created) are ok
3158 if (need_root_inode
) {
3160 do_warn(_("reinitializing root directory\n"));
3162 need_root_inode
= 0;
3163 need_root_dotdot
= 0;
3165 do_warn(_("would reinitialize root directory\n"));
3171 do_warn(_("reinitializing realtime bitmap inode\n"));
3175 do_warn(_("would reinitialize realtime bitmap inode\n"));
3181 do_warn(_("reinitializing realtime summary inode\n"));
3185 do_warn(_("would reinitialize realtime summary inode\n"));
3191 _(" - resetting contents of realtime bitmap and summary inodes\n"));
3192 if (fill_rbmino(mp
)) {
3194 _("Warning: realtime bitmap may be inconsistent\n"));
3197 if (fill_rsumino(mp
)) {
3199 _("Warning: realtime bitmap may be inconsistent\n"));
3203 mark_standalone_inodes(mp
);
3205 do_log(_(" - traversing filesystem ...\n"));
3207 irec
= find_inode_rec(mp
, XFS_INO_TO_AGNO(mp
, mp
->m_sb
.sb_rootino
),
3208 XFS_INO_TO_AGINO(mp
, mp
->m_sb
.sb_rootino
));
3211 * we always have a root inode, even if it's free...
3212 * if the root is free, forget it, lost+found is already gone
3214 if (is_inode_free(irec
, 0) || !inode_isadir(irec
, 0)) {
3215 need_root_inode
= 1;
3219 * then process all inodes by walking incore inode tree
3224 * any directories that had updated ".." entries, rebuild them now
3226 update_missing_dotdot_entries(mp
);
3228 do_log(_(" - traversal finished ...\n"));
3229 do_log(_(" - moving disconnected inodes to %s ...\n"),
3233 * move all disconnected inodes to the orphanage
3235 for (i
= 0; i
< glob_agcount
; i
++) {
3236 irec
= findfirst_inode_rec(i
);
3237 while (irec
!= NULL
) {
3238 check_for_orphaned_inodes(mp
, i
, irec
);
3239 irec
= next_ino_rec(irec
);