1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
16 #include "err_protos.h"
21 static struct cred zerocr
;
22 static struct fsxattr zerofsx
;
23 static xfs_ino_t orphanage_ino
;
25 static struct xfs_name xfs_name_dot
= {(unsigned char *)".",
30 * Data structures used to keep track of directories where the ".."
31 * entries are updated. These must be rebuilt after the initial pass
33 typedef struct dotdot_update
{
34 struct list_head list
;
35 ino_tree_node_t
*irec
;
40 static LIST_HEAD(dotdot_update_list
);
41 static int dotdot_update
;
46 ino_tree_node_t
*irec
,
49 dotdot_update_t
*dir
= malloc(sizeof(dotdot_update_t
));
52 do_error(_("malloc failed add_dotdot_update (%zu bytes)\n"),
53 sizeof(dotdot_update_t
));
55 INIT_LIST_HEAD(&dir
->list
);
58 dir
->ino_offset
= ino_offset
;
60 list_add(&dir
->list
, &dotdot_update_list
);
64 * Data structures and routines to keep track of directory entries
65 * and whether their leaf entry has been seen. Also used for name
66 * duplicate checking and rebuilding step if required.
68 typedef struct dir_hash_ent
{
69 struct dir_hash_ent
*nextbyaddr
; /* next in addr bucket */
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 */
80 typedef struct dir_hash_tab
{
81 int size
; /* size of hash tables */
82 int names_duped
; /* 1 = ent names malloced */
83 dir_hash_ent_t
*first
; /* ptr to first added entry */
84 dir_hash_ent_t
*last
; /* ptr to last added entry */
85 dir_hash_ent_t
**byhash
; /* ptr to name hash buckets */
86 dir_hash_ent_t
**byaddr
; /* ptr to addr hash buckets */
89 #define DIR_HASH_TAB_SIZE(n) \
90 (sizeof(dir_hash_tab_t) + (sizeof(dir_hash_ent_t *) * (n) * 2))
91 #define DIR_HASH_FUNC(t,a) ((a) % (t)->size)
94 * Track the contents of the freespace table in a directory.
96 typedef struct freetab
{
97 int naents
; /* expected number of data blocks */
98 int nents
; /* number of data blocks processed */
100 xfs_dir2_data_off_t v
;
104 #define FREETAB_SIZE(n) \
105 (offsetof(freetab_t, ents) + (sizeof(struct freetab_ent) * (n)))
107 #define DIR_HASH_CK_OK 0
108 #define DIR_HASH_CK_DUPLEAF 1
109 #define DIR_HASH_CK_BADHASH 2
110 #define DIR_HASH_CK_NODATA 3
111 #define DIR_HASH_CK_NOLEAF 4
112 #define DIR_HASH_CK_BADSTALE 5
113 #define DIR_HASH_CK_TOTAL 6
116 * Need to handle CRC and validation errors specially here. If there is a
117 * validator error, re-read without the verifier so that we get a buffer we can
118 * check and repair. Re-attach the ops to the buffer after the read so that when
119 * it is rewritten the CRC is recalculated.
121 * If the buffer was not read, we return an error. If the buffer was read but
122 * had a CRC or corruption error, we reread it without the verifier and if it is
123 * read successfully we increment *crc_error and return 0. Otherwise we
124 * return the read error.
128 struct xfs_inode
*ip
,
130 struct xfs_buf
**bpp
,
131 const struct xfs_buf_ops
*ops
,
137 error
= -libxfs_da_read_buf(NULL
, ip
, bno
, 0, bpp
, XFS_DATA_FORK
, ops
);
139 if (error
!= EFSBADCRC
&& error
!= EFSCORRUPTED
)
142 error2
= -libxfs_da_read_buf(NULL
, ip
, bno
, 0, bpp
, XFS_DATA_FORK
,
153 * Returns 0 if the name already exists (ie. a duplicate)
158 dir_hash_tab_t
*hashtab
,
165 xfs_dahash_t hash
= 0;
171 struct xfs_name xname
;
173 ASSERT(!hashtab
->names_duped
);
179 junk
= name
[0] == '/';
180 byaddr
= DIR_HASH_FUNC(hashtab
, addr
);
184 hash
= libxfs_dir2_hashname(mp
, &xname
);
185 byhash
= DIR_HASH_FUNC(hashtab
, hash
);
188 * search hash bucket for existing name.
190 for (p
= hashtab
->byhash
[byhash
]; p
; p
= p
->nextbyhash
) {
191 if (p
->hashval
== hash
&& p
->name
.len
== namelen
) {
192 if (memcmp(p
->name
.name
, name
, namelen
) == 0) {
201 if ((p
= malloc(sizeof(*p
))) == NULL
)
202 do_error(_("malloc failed in dir_hash_add (%zu bytes)\n"),
205 p
->nextbyaddr
= hashtab
->byaddr
[byaddr
];
206 hashtab
->byaddr
[byaddr
] = p
;
208 hashtab
->last
->nextbyorder
= p
;
211 p
->nextbyorder
= NULL
;
214 if (!(p
->junkit
= junk
)) {
216 p
->nextbyhash
= hashtab
->byhash
[byhash
];
217 hashtab
->byhash
[byhash
] = p
;
228 * checks to see if any data entries are not in the leaf blocks
232 dir_hash_tab_t
*hashtab
)
237 for (i
= 0; i
< hashtab
->size
; i
++) {
238 for (p
= hashtab
->byaddr
[i
]; p
; p
= p
->nextbyaddr
) {
248 dir_hash_tab_t
*hashtab
,
252 static char *seevalstr
[DIR_HASH_CK_TOTAL
];
256 seevalstr
[DIR_HASH_CK_OK
] = _("ok");
257 seevalstr
[DIR_HASH_CK_DUPLEAF
] = _("duplicate leaf");
258 seevalstr
[DIR_HASH_CK_BADHASH
] = _("hash value mismatch");
259 seevalstr
[DIR_HASH_CK_NODATA
] = _("no data entry");
260 seevalstr
[DIR_HASH_CK_NOLEAF
] = _("no leaf entry");
261 seevalstr
[DIR_HASH_CK_BADSTALE
] = _("bad stale count");
265 if (seeval
== DIR_HASH_CK_OK
&& dir_hash_unseen(hashtab
))
266 seeval
= DIR_HASH_CK_NOLEAF
;
267 if (seeval
== DIR_HASH_CK_OK
)
269 do_warn(_("bad hash table for directory inode %" PRIu64
" (%s): "),
270 ip
->i_ino
, seevalstr
[seeval
]);
272 do_warn(_("rebuilding\n"));
274 do_warn(_("would rebuild\n"));
280 dir_hash_tab_t
*hashtab
)
286 for (i
= 0; i
< hashtab
->size
; i
++) {
287 for (p
= hashtab
->byaddr
[i
]; p
; p
= n
) {
289 if (hashtab
->names_duped
)
290 free((void *)p
->name
.name
);
297 static dir_hash_tab_t
*
301 dir_hash_tab_t
*hashtab
;
304 hsize
= size
/ (16 * 4);
309 if ((hashtab
= calloc(DIR_HASH_TAB_SIZE(hsize
), 1)) == NULL
)
310 do_error(_("calloc failed in dir_hash_init\n"));
311 hashtab
->size
= hsize
;
312 hashtab
->byhash
= (dir_hash_ent_t
**)((char *)hashtab
+
313 sizeof(dir_hash_tab_t
));
314 hashtab
->byaddr
= (dir_hash_ent_t
**)((char *)hashtab
+
315 sizeof(dir_hash_tab_t
) + sizeof(dir_hash_ent_t
*) * hsize
);
321 dir_hash_tab_t
*hashtab
,
323 xfs_dir2_dataptr_t addr
)
328 i
= DIR_HASH_FUNC(hashtab
, addr
);
329 for (p
= hashtab
->byaddr
[i
]; p
; p
= p
->nextbyaddr
) {
330 if (p
->address
!= addr
)
333 return DIR_HASH_CK_DUPLEAF
;
334 if (p
->junkit
== 0 && p
->hashval
!= hash
)
335 return DIR_HASH_CK_BADHASH
;
337 return DIR_HASH_CK_OK
;
339 return DIR_HASH_CK_NODATA
;
343 dir_hash_update_ftype(
344 dir_hash_tab_t
*hashtab
,
345 xfs_dir2_dataptr_t addr
,
351 i
= DIR_HASH_FUNC(hashtab
, addr
);
352 for (p
= hashtab
->byaddr
[i
]; p
; p
= p
->nextbyaddr
) {
353 if (p
->address
!= addr
)
355 p
->name
.type
= ftype
;
360 * checks to make sure leafs match a data entry, and that the stale
365 dir_hash_tab_t
*hashtab
,
366 xfs_dir2_leaf_entry_t
*ents
,
374 for (i
= j
= 0; i
< count
; i
++) {
375 if (be32_to_cpu(ents
[i
].address
) == XFS_DIR2_NULL_DATAPTR
) {
379 rval
= dir_hash_see(hashtab
, be32_to_cpu(ents
[i
].hashval
),
380 be32_to_cpu(ents
[i
].address
));
381 if (rval
!= DIR_HASH_CK_OK
)
384 return j
== stale
? DIR_HASH_CK_OK
: DIR_HASH_CK_BADSTALE
;
388 * Convert name pointers into locally allocated memory.
389 * This must only be done after all the entries have been added.
392 dir_hash_dup_names(dir_hash_tab_t
*hashtab
)
397 if (hashtab
->names_duped
)
400 for (p
= hashtab
->first
; p
; p
= p
->nextbyorder
) {
401 name
= malloc(p
->name
.len
);
402 memcpy(name
, p
->name
.name
, p
->name
.len
);
405 hashtab
->names_duped
= 1;
409 * Given a block number in a fork, return the next valid block number (not a
410 * hole). If this is the last block number then NULLFILEOFF is returned.
414 struct xfs_inode
*ip
,
419 struct xfs_bmbt_irec got
;
420 struct xfs_iext_cursor icur
;
422 switch (ip
->i_df
.if_format
) {
423 case XFS_DINODE_FMT_LOCAL
:
426 case XFS_DINODE_FMT_BTREE
:
427 case XFS_DINODE_FMT_EXTENTS
:
433 if (!(ip
->i_df
.if_flags
& XFS_IFEXTENTS
)) {
434 error
= -libxfs_iread_extents(NULL
, ip
, XFS_DATA_FORK
);
440 if (!libxfs_iext_lookup_extent(ip
, &ip
->i_df
, bno
, &icur
, &got
))
443 *bnop
= got
.br_startoff
< bno
? bno
: got
.br_startoff
;
452 do_error(_("ran out of disk space!\n"));
454 do_error(_("xfs_trans_reserve returned %d\n"), err
);
458 mk_rbmino(xfs_mount_t
*mp
)
467 xfs_bmbt_irec_t map
[XFS_BMAP_MAX_NMAP
];
474 i
= -libxfs_trans_alloc_rollable(mp
, 10, &tp
);
478 error
= -libxfs_iget(mp
, tp
, mp
->m_sb
.sb_rbmino
, 0, &ip
);
481 _("couldn't iget realtime bitmap inode -- error - %d\n"),
485 memset(&ip
->i_d
, 0, sizeof(ip
->i_d
));
487 VFS_I(ip
)->i_mode
= S_IFREG
;
488 ip
->i_df
.if_format
= XFS_DINODE_FMT_EXTENTS
;
490 ip
->i_afp
->if_format
= XFS_DINODE_FMT_EXTENTS
;
492 set_nlink(VFS_I(ip
), 1); /* account for sb ptr */
494 times
= XFS_ICHGTIME_CHG
| XFS_ICHGTIME_MOD
;
495 if (xfs_sb_version_has_v3inode(&mp
->m_sb
)) {
496 VFS_I(ip
)->i_version
= 1;
497 ip
->i_d
.di_flags2
= 0;
498 times
|= XFS_ICHGTIME_CREATE
;
500 libxfs_trans_ichgtime(tp
, ip
, times
);
505 ip
->i_df
.if_flags
= XFS_IFEXTENTS
;
506 ip
->i_df
.if_bytes
= 0;
507 ip
->i_df
.if_u1
.if_root
= NULL
;
509 ip
->i_d
.di_size
= mp
->m_sb
.sb_rbmblocks
* mp
->m_sb
.sb_blocksize
;
514 libxfs_trans_ijoin(tp
, ip
, 0);
515 libxfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
516 error
= -libxfs_trans_commit(tp
);
518 do_error(_("%s: commit failed, error %d\n"), __func__
, error
);
521 * then allocate blocks for file and fill with zeroes (stolen
524 blocks
= mp
->m_sb
.sb_rbmblocks
+
525 XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
) - 1;
526 error
= -libxfs_trans_alloc_rollable(mp
, blocks
, &tp
);
530 libxfs_trans_ijoin(tp
, ip
, 0);
532 while (bno
< mp
->m_sb
.sb_rbmblocks
) {
533 nmap
= XFS_BMAP_MAX_NMAP
;
534 error
= -libxfs_bmapi_write(tp
, ip
, bno
,
535 (xfs_extlen_t
)(mp
->m_sb
.sb_rbmblocks
- bno
),
536 0, mp
->m_sb
.sb_rbmblocks
, map
, &nmap
);
539 _("couldn't allocate realtime bitmap, error = %d\n"),
542 for (i
= 0, ep
= map
; i
< nmap
; i
++, ep
++) {
543 libxfs_device_zero(mp
->m_ddev_targp
,
544 XFS_FSB_TO_DADDR(mp
, ep
->br_startblock
),
545 XFS_FSB_TO_BB(mp
, ep
->br_blockcount
));
546 bno
+= ep
->br_blockcount
;
549 error
= -libxfs_trans_commit(tp
);
552 _("allocation of the realtime bitmap failed, error = %d\n"),
559 fill_rbmino(xfs_mount_t
*mp
)
573 error
= -libxfs_trans_alloc_rollable(mp
, 10, &tp
);
577 error
= -libxfs_iget(mp
, tp
, mp
->m_sb
.sb_rbmino
, 0, &ip
);
580 _("couldn't iget realtime bitmap inode -- error - %d\n"),
584 while (bno
< mp
->m_sb
.sb_rbmblocks
) {
586 * fill the file one block at a time
589 error
= -libxfs_bmapi_write(tp
, ip
, bno
, 1, 0, 1, &map
, &nmap
);
590 if (error
|| nmap
!= 1) {
592 _("couldn't map realtime bitmap block %" PRIu64
", error = %d\n"),
596 ASSERT(map
.br_startblock
!= HOLESTARTBLOCK
);
598 error
= -libxfs_trans_read_buf(
600 XFS_FSB_TO_DADDR(mp
, map
.br_startblock
),
601 XFS_FSB_TO_BB(mp
, 1), 1, &bp
, NULL
);
605 _("can't access block %" PRIu64
" (fsbno %" PRIu64
") of realtime bitmap inode %" PRIu64
"\n"),
606 bno
, map
.br_startblock
, mp
->m_sb
.sb_rbmino
);
610 memmove(bp
->b_addr
, bmp
, mp
->m_sb
.sb_blocksize
);
612 libxfs_trans_log_buf(tp
, bp
, 0, mp
->m_sb
.sb_blocksize
- 1);
614 bmp
= (xfs_rtword_t
*)((intptr_t) bmp
+ mp
->m_sb
.sb_blocksize
);
618 libxfs_trans_ijoin(tp
, ip
, 0);
619 error
= -libxfs_trans_commit(tp
);
621 do_error(_("%s: commit failed, error %d\n"), __func__
, error
);
627 fill_rsumino(xfs_mount_t
*mp
)
636 xfs_fileoff_t end_bno
;
641 end_bno
= mp
->m_rsumsize
>> mp
->m_sb
.sb_blocklog
;
643 error
= -libxfs_trans_alloc_rollable(mp
, 10, &tp
);
647 error
= -libxfs_iget(mp
, tp
, mp
->m_sb
.sb_rsumino
, 0, &ip
);
650 _("couldn't iget realtime summary inode -- error - %d\n"),
654 while (bno
< end_bno
) {
656 * fill the file one block at a time
659 error
= -libxfs_bmapi_write(tp
, ip
, bno
, 1, 0, 1, &map
, &nmap
);
660 if (error
|| nmap
!= 1) {
662 _("couldn't map realtime summary inode block %" PRIu64
", error = %d\n"),
666 ASSERT(map
.br_startblock
!= HOLESTARTBLOCK
);
668 error
= -libxfs_trans_read_buf(
670 XFS_FSB_TO_DADDR(mp
, map
.br_startblock
),
671 XFS_FSB_TO_BB(mp
, 1), 1, &bp
, NULL
);
675 _("can't access block %" PRIu64
" (fsbno %" PRIu64
") of realtime summary inode %" PRIu64
"\n"),
676 bno
, map
.br_startblock
, mp
->m_sb
.sb_rsumino
);
681 memmove(bp
->b_addr
, smp
, mp
->m_sb
.sb_blocksize
);
683 libxfs_trans_log_buf(tp
, bp
, 0, mp
->m_sb
.sb_blocksize
- 1);
685 smp
= (xfs_suminfo_t
*)((intptr_t)smp
+ mp
->m_sb
.sb_blocksize
);
689 libxfs_trans_ijoin(tp
, ip
, 0);
690 error
= -libxfs_trans_commit(tp
);
692 do_error(_("%s: commit failed, error %d\n"), __func__
, error
);
698 mk_rsumino(xfs_mount_t
*mp
)
708 xfs_bmbt_irec_t map
[XFS_BMAP_MAX_NMAP
];
715 i
= -libxfs_trans_alloc(mp
, &M_RES(mp
)->tr_ichange
, 10, 0, 0, &tp
);
719 error
= -libxfs_iget(mp
, tp
, mp
->m_sb
.sb_rsumino
, 0, &ip
);
722 _("couldn't iget realtime summary inode -- error - %d\n"),
726 memset(&ip
->i_d
, 0, sizeof(ip
->i_d
));
728 VFS_I(ip
)->i_mode
= S_IFREG
;
729 ip
->i_df
.if_format
= XFS_DINODE_FMT_EXTENTS
;
731 ip
->i_afp
->if_format
= XFS_DINODE_FMT_EXTENTS
;
733 set_nlink(VFS_I(ip
), 1); /* account for sb ptr */
735 times
= XFS_ICHGTIME_CHG
| XFS_ICHGTIME_MOD
;
736 if (xfs_sb_version_has_v3inode(&mp
->m_sb
)) {
737 VFS_I(ip
)->i_version
= 1;
738 ip
->i_d
.di_flags2
= 0;
739 times
|= XFS_ICHGTIME_CREATE
;
741 libxfs_trans_ichgtime(tp
, ip
, times
);
746 ip
->i_df
.if_flags
= XFS_IFEXTENTS
;
747 ip
->i_df
.if_bytes
= 0;
748 ip
->i_df
.if_u1
.if_root
= NULL
;
750 ip
->i_d
.di_size
= mp
->m_rsumsize
;
755 libxfs_trans_ijoin(tp
, ip
, 0);
756 libxfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
757 error
= -libxfs_trans_commit(tp
);
759 do_error(_("%s: commit failed, error %d\n"), __func__
, error
);
762 * then allocate blocks for file and fill with zeroes (stolen
765 nsumblocks
= mp
->m_rsumsize
>> mp
->m_sb
.sb_blocklog
;
766 blocks
= nsumblocks
+ XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
) - 1;
767 error
= -libxfs_trans_alloc_rollable(mp
, blocks
, &tp
);
771 libxfs_trans_ijoin(tp
, ip
, 0);
773 while (bno
< nsumblocks
) {
774 nmap
= XFS_BMAP_MAX_NMAP
;
775 error
= -libxfs_bmapi_write(tp
, ip
, bno
,
776 (xfs_extlen_t
)(nsumblocks
- bno
),
777 0, nsumblocks
, map
, &nmap
);
780 _("couldn't allocate realtime summary inode, error = %d\n"),
783 for (i
= 0, ep
= map
; i
< nmap
; i
++, ep
++) {
784 libxfs_device_zero(mp
->m_ddev_targp
,
785 XFS_FSB_TO_DADDR(mp
, ep
->br_startblock
),
786 XFS_FSB_TO_BB(mp
, ep
->br_blockcount
));
787 bno
+= ep
->br_blockcount
;
790 error
= -libxfs_trans_commit(tp
);
793 _("allocation of the realtime summary ino failed, error = %d\n"),
800 * makes a new root directory.
803 mk_root_dir(xfs_mount_t
*mp
)
809 const mode_t mode
= 0755;
810 ino_tree_node_t
*irec
;
814 i
= -libxfs_trans_alloc(mp
, &M_RES(mp
)->tr_ichange
, 10, 0, 0, &tp
);
818 error
= -libxfs_iget(mp
, tp
, mp
->m_sb
.sb_rootino
, 0, &ip
);
820 do_error(_("could not iget root inode -- error - %d\n"), error
);
824 * take care of the core -- initialization from xfs_ialloc()
826 memset(&ip
->i_d
, 0, sizeof(ip
->i_d
));
828 VFS_I(ip
)->i_mode
= mode
|S_IFDIR
;
829 ip
->i_df
.if_format
= XFS_DINODE_FMT_EXTENTS
;
831 ip
->i_afp
->if_format
= XFS_DINODE_FMT_EXTENTS
;
833 set_nlink(VFS_I(ip
), 2); /* account for . and .. */
835 times
= XFS_ICHGTIME_CHG
| XFS_ICHGTIME_MOD
;
836 if (xfs_sb_version_has_v3inode(&mp
->m_sb
)) {
837 VFS_I(ip
)->i_version
= 1;
838 ip
->i_d
.di_flags2
= 0;
839 times
|= XFS_ICHGTIME_CREATE
;
841 libxfs_trans_ichgtime(tp
, ip
, times
);
842 libxfs_trans_ijoin(tp
, ip
, 0);
843 libxfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
848 ip
->i_df
.if_flags
= XFS_IFEXTENTS
;
849 ip
->i_df
.if_bytes
= 0;
850 ip
->i_df
.if_u1
.if_root
= NULL
;
853 * initialize the directory
855 libxfs_dir_init(tp
, ip
, ip
);
857 error
= -libxfs_trans_commit(tp
);
859 do_error(_("%s: commit failed, error %d\n"), __func__
, error
);
863 irec
= find_inode_rec(mp
, XFS_INO_TO_AGNO(mp
, mp
->m_sb
.sb_rootino
),
864 XFS_INO_TO_AGINO(mp
, mp
->m_sb
.sb_rootino
));
865 set_inode_isadir(irec
, XFS_INO_TO_AGINO(mp
, mp
->m_sb
.sb_rootino
) -
870 * orphanage name == lost+found
873 mk_orphanage(xfs_mount_t
*mp
)
879 ino_tree_node_t
*irec
;
883 const int mode
= 0755;
885 struct xfs_name xname
;
888 * check for an existing lost+found first, if it exists, return
889 * its inode. Otherwise, we can create it. Bad lost+found inodes
890 * would have been cleared in phase3 and phase4.
893 i
= -libxfs_iget(mp
, NULL
, mp
->m_sb
.sb_rootino
, 0, &pip
);
895 do_error(_("%d - couldn't iget root inode to obtain %s\n"),
898 xname
.name
= (unsigned char *)ORPHANAGE
;
899 xname
.len
= strlen(ORPHANAGE
);
900 xname
.type
= XFS_DIR3_FT_DIR
;
902 if (libxfs_dir_lookup(NULL
, pip
, &xname
, &ino
, NULL
) == 0)
906 * could not be found, create it
908 nres
= XFS_MKDIR_SPACE_RES(mp
, xname
.len
);
909 i
= -libxfs_trans_alloc(mp
, &M_RES(mp
)->tr_mkdir
, nres
, 0, 0, &tp
);
914 * use iget/ijoin instead of trans_iget because the ialloc
915 * wrapper can commit the transaction and start a new one
917 /* i = -libxfs_iget(mp, NULL, mp->m_sb.sb_rootino, 0, &pip);
919 do_error(_("%d - couldn't iget root inode to make %s\n"),
922 error
= -libxfs_dir_ialloc(&tp
, pip
, mode
|S_IFDIR
,
923 1, 0, &zerocr
, &zerofsx
, &ip
);
925 do_error(_("%s inode allocation failed %d\n"),
928 inc_nlink(VFS_I(ip
)); /* account for . */
931 irec
= find_inode_rec(mp
,
932 XFS_INO_TO_AGNO(mp
, ino
),
933 XFS_INO_TO_AGINO(mp
, ino
));
937 * This inode is allocated from a newly created inode
938 * chunk and therefore did not exist when inode chunks
939 * were processed in phase3. Add this group of inodes to
940 * the entry avl tree as if they were discovered in phase3.
942 irec
= set_inode_free_alloc(mp
, XFS_INO_TO_AGNO(mp
, ino
),
943 XFS_INO_TO_AGINO(mp
, ino
));
946 for (i
= 0; i
< XFS_INODES_PER_CHUNK
; i
++)
947 set_inode_free(irec
, i
);
950 ino_offset
= get_inode_offset(mp
, ino
, irec
);
953 * Mark the inode allocated to lost+found as used in the AVL tree
954 * so it is not skipped in phase 7
956 set_inode_used(irec
, ino_offset
);
957 add_inode_ref(irec
, ino_offset
);
958 add_inode_reached(irec
, ino_offset
);
961 * now that we know the transaction will stay around,
962 * add the root inode to it
964 libxfs_trans_ijoin(tp
, pip
, 0);
967 * create the actual entry
969 error
= -libxfs_dir_createname(tp
, pip
, &xname
, ip
->i_ino
, nres
);
972 _("can't make %s, createname error %d\n"),
976 * bump up the link count in the root directory to account
977 * for .. in the new directory, and update the irec copy of the
978 * on-disk nlink so we don't fail the link count check later.
980 inc_nlink(VFS_I(pip
));
981 irec
= find_inode_rec(mp
, XFS_INO_TO_AGNO(mp
, mp
->m_sb
.sb_rootino
),
982 XFS_INO_TO_AGINO(mp
, mp
->m_sb
.sb_rootino
));
983 add_inode_ref(irec
, 0);
984 set_inode_disk_nlinks(irec
, 0, get_inode_disk_nlinks(irec
, 0) + 1);
986 libxfs_trans_log_inode(tp
, pip
, XFS_ILOG_CORE
);
987 libxfs_dir_init(tp
, ip
, pip
);
988 libxfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
989 error
= -libxfs_trans_commit(tp
);
991 do_error(_("%s directory creation failed -- bmapf error %d\n"),
1001 * move a file to the orphange.
1006 xfs_ino_t ino
, /* inode # to be moved */
1007 int isa_dir
) /* 1 if inode is a directory */
1009 xfs_inode_t
*orphanage_ip
;
1010 xfs_ino_t entry_ino_num
;
1014 unsigned char fname
[MAXPATHLEN
+ 1];
1017 ino_tree_node_t
*irec
;
1019 struct xfs_name xname
;
1022 xname
.len
= snprintf((char *)fname
, sizeof(fname
), "%llu",
1023 (unsigned long long)ino
);
1025 err
= -libxfs_iget(mp
, NULL
, orphanage_ino
, 0, &orphanage_ip
);
1027 do_error(_("%d - couldn't iget orphanage inode\n"), err
);
1029 * Make sure the filename is unique in the lost+found
1032 while (libxfs_dir_lookup(NULL
, orphanage_ip
, &xname
, &entry_ino_num
,
1034 xname
.len
= snprintf((char *)fname
, sizeof(fname
), "%llu.%d",
1035 (unsigned long long)ino
, ++incr
);
1037 /* Orphans may not have a proper parent, so use custom ops here */
1038 err
= -libxfs_iget(mp
, NULL
, ino
, 0, &ino_p
);
1040 do_error(_("%d - couldn't iget disconnected inode\n"), err
);
1042 xname
.type
= libxfs_mode_to_ftype(VFS_I(ino_p
)->i_mode
);
1045 irec
= find_inode_rec(mp
, XFS_INO_TO_AGNO(mp
, orphanage_ino
),
1046 XFS_INO_TO_AGINO(mp
, orphanage_ino
));
1048 ino_offset
= XFS_INO_TO_AGINO(mp
, orphanage_ino
) -
1050 nres
= XFS_DIRENTER_SPACE_RES(mp
, fnamelen
) +
1051 XFS_DIRENTER_SPACE_RES(mp
, 2);
1052 err
= -libxfs_dir_lookup(NULL
, ino_p
, &xfs_name_dotdot
,
1053 &entry_ino_num
, NULL
);
1055 ASSERT(err
== ENOENT
);
1057 err
= -libxfs_trans_alloc(mp
, &M_RES(mp
)->tr_rename
,
1061 _("space reservation failed (%d), filesystem may be out of space\n"),
1064 libxfs_trans_ijoin(tp
, orphanage_ip
, 0);
1065 libxfs_trans_ijoin(tp
, ino_p
, 0);
1067 err
= -libxfs_dir_createname(tp
, orphanage_ip
, &xname
,
1071 _("name create failed in %s (%d), filesystem may be out of space\n"),
1075 add_inode_ref(irec
, ino_offset
);
1077 inc_nlink(VFS_I(orphanage_ip
));
1078 libxfs_trans_log_inode(tp
, orphanage_ip
, XFS_ILOG_CORE
);
1080 err
= -libxfs_dir_createname(tp
, ino_p
, &xfs_name_dotdot
,
1081 orphanage_ino
, nres
);
1084 _("creation of .. entry failed (%d), filesystem may be out of space\n"),
1087 inc_nlink(VFS_I(ino_p
));
1088 libxfs_trans_log_inode(tp
, ino_p
, XFS_ILOG_CORE
);
1089 err
= -libxfs_trans_commit(tp
);
1092 _("creation of .. entry failed (%d)\n"), err
);
1094 err
= -libxfs_trans_alloc(mp
, &M_RES(mp
)->tr_rename
,
1098 _("space reservation failed (%d), filesystem may be out of space\n"),
1101 libxfs_trans_ijoin(tp
, orphanage_ip
, 0);
1102 libxfs_trans_ijoin(tp
, ino_p
, 0);
1105 err
= -libxfs_dir_createname(tp
, orphanage_ip
, &xname
,
1109 _("name create failed in %s (%d), filesystem may be out of space\n"),
1113 add_inode_ref(irec
, ino_offset
);
1115 inc_nlink(VFS_I(orphanage_ip
));
1116 libxfs_trans_log_inode(tp
, orphanage_ip
, XFS_ILOG_CORE
);
1119 * don't replace .. value if it already points
1120 * to us. that'll pop a libxfs/kernel ASSERT.
1122 if (entry_ino_num
!= orphanage_ino
) {
1123 err
= -libxfs_dir_replace(tp
, ino_p
,
1124 &xfs_name_dotdot
, orphanage_ino
,
1128 _("name replace op failed (%d), filesystem may be out of space\n"),
1132 err
= -libxfs_trans_commit(tp
);
1135 _("orphanage name replace op failed (%d)\n"), err
);
1140 * use the remove log reservation as that's
1141 * more accurate. we're only creating the
1142 * links, we're not doing the inode allocation
1143 * also accounted for in the create
1145 nres
= XFS_DIRENTER_SPACE_RES(mp
, xname
.len
);
1146 err
= -libxfs_trans_alloc(mp
, &M_RES(mp
)->tr_remove
,
1150 _("space reservation failed (%d), filesystem may be out of space\n"),
1153 libxfs_trans_ijoin(tp
, orphanage_ip
, 0);
1154 libxfs_trans_ijoin(tp
, ino_p
, 0);
1156 err
= -libxfs_dir_createname(tp
, orphanage_ip
, &xname
, ino
,
1160 _("name create failed in %s (%d), filesystem may be out of space\n"),
1164 set_nlink(VFS_I(ino_p
), 1);
1165 libxfs_trans_log_inode(tp
, ino_p
, XFS_ILOG_CORE
);
1166 err
= -libxfs_trans_commit(tp
);
1169 _("orphanage name create failed (%d)\n"), err
);
1171 libxfs_irele(ino_p
);
1172 libxfs_irele(orphanage_ip
);
1182 do_warn(msg
, iname
, ino1
, ino2
);
1185 do_warn(_(", marking entry to be junked\n"));
1189 do_warn(_(", would junk entry\n"));
1193 /* Find and invalidate all the directory's buffers. */
1196 struct xfs_trans
*tp
,
1197 struct xfs_inode
*ip
,
1200 struct xfs_iext_cursor icur
;
1201 struct xfs_bmbt_irec rec
;
1202 struct xfs_ifork
*ifp
;
1203 struct xfs_da_geometry
*geo
;
1208 if (ip
->i_df
.if_format
!= XFS_DINODE_FMT_EXTENTS
&&
1209 ip
->i_df
.if_format
!= XFS_DINODE_FMT_BTREE
)
1212 geo
= tp
->t_mountp
->m_dir_geo
;
1213 ifp
= XFS_IFORK_PTR(ip
, XFS_DATA_FORK
);
1214 for_each_xfs_iext(ifp
, &icur
, &rec
) {
1215 for (dabno
= roundup(rec
.br_startoff
, geo
->fsbcount
);
1216 dabno
< rec
.br_startoff
+ rec
.br_blockcount
;
1217 dabno
+= geo
->fsbcount
) {
1219 error
= -libxfs_da_get_buf(tp
, ip
, dabno
, &bp
,
1225 libxfs_trans_binval(tp
, bp
);
1226 libxfs_trans_brelse(tp
, bp
);
1234 * Unexpected failure during the rebuild will leave the entries in
1235 * lost+found on the next run
1239 longform_dir2_rebuild(
1243 ino_tree_node_t
*irec
,
1245 dir_hash_tab_t
*hashtab
)
1250 xfs_fileoff_t lastblock
;
1256 * trash directory completely and rebuild from scratch using the
1257 * name/inode pairs in the hash table
1260 do_warn(_("rebuilding directory inode %" PRIu64
"\n"), ino
);
1263 * first attempt to locate the parent inode, if it can't be
1264 * found, set it to the root inode and it'll be moved to the
1265 * orphanage later (the inode number here needs to be valid
1266 * for the libxfs_dir_init() call).
1268 pip
.i_ino
= get_inode_parent(irec
, ino_offset
);
1269 if (pip
.i_ino
== NULLFSINO
||
1270 libxfs_dir_ino_validate(mp
, pip
.i_ino
))
1271 pip
.i_ino
= mp
->m_sb
.sb_rootino
;
1273 nres
= XFS_REMOVE_SPACE_RES(mp
);
1274 error
= -libxfs_trans_alloc(mp
, &M_RES(mp
)->tr_remove
, nres
, 0, 0, &tp
);
1277 libxfs_trans_ijoin(tp
, ip
, 0);
1279 error
= dir_binval(tp
, ip
, XFS_DATA_FORK
);
1281 do_error(_("error %d invalidating directory %llu blocks\n"),
1282 error
, (unsigned long long)ip
->i_ino
);
1284 if ((error
= -libxfs_bmap_last_offset(ip
, &lastblock
, XFS_DATA_FORK
)))
1285 do_error(_("xfs_bmap_last_offset failed -- error - %d\n"),
1288 /* free all data, leaf, node and freespace blocks */
1290 error
= -libxfs_bunmapi(tp
, ip
, 0, lastblock
, XFS_BMAPI_METADATA
,
1293 do_warn(_("xfs_bunmapi failed -- error - %d\n"), error
);
1294 goto out_bmap_cancel
;
1296 error
= -libxfs_defer_finish(&tp
);
1298 do_warn(("defer_finish failed -- error - %d\n"), error
);
1299 goto out_bmap_cancel
;
1302 * Close out trans and start the next one in the chain.
1304 error
= -libxfs_trans_roll_inode(&tp
, ip
);
1306 goto out_bmap_cancel
;
1309 error
= -libxfs_dir_init(tp
, ip
, &pip
);
1311 do_warn(_("xfs_dir_init failed -- error - %d\n"), error
);
1312 goto out_bmap_cancel
;
1315 error
= -libxfs_trans_commit(tp
);
1318 _("dir init failed (%d)\n"), error
);
1320 if (ino
== mp
->m_sb
.sb_rootino
)
1321 need_root_dotdot
= 0;
1323 /* go through the hash list and re-add the inodes */
1325 for (p
= hashtab
->first
; p
; p
= p
->nextbyorder
) {
1327 if (p
->name
.name
[0] == '/' || (p
->name
.name
[0] == '.' &&
1328 (p
->name
.len
== 1 || (p
->name
.len
== 2 &&
1329 p
->name
.name
[1] == '.'))))
1332 nres
= XFS_CREATE_SPACE_RES(mp
, p
->name
.len
);
1333 error
= -libxfs_trans_alloc(mp
, &M_RES(mp
)->tr_create
,
1338 libxfs_trans_ijoin(tp
, ip
, 0);
1340 error
= -libxfs_dir_createname(tp
, ip
, &p
->name
, p
->inum
,
1344 _("name create failed in ino %" PRIu64
" (%d), filesystem may be out of space\n"),
1346 goto out_bmap_cancel
;
1349 error
= -libxfs_trans_commit(tp
);
1352 _("name create failed (%d) during rebuild\n"), error
);
1358 libxfs_trans_cancel(tp
);
1364 * Kill a block in a version 2 inode.
1365 * Makes its own transaction.
1379 nres
= XFS_REMOVE_SPACE_RES(mp
);
1380 error
= -libxfs_trans_alloc(mp
, &M_RES(mp
)->tr_remove
, nres
, 0, 0, &tp
);
1383 libxfs_trans_ijoin(tp
, ip
, 0);
1384 libxfs_trans_bjoin(tp
, bp
);
1385 memset(&args
, 0, sizeof(args
));
1388 args
.whichfork
= XFS_DATA_FORK
;
1389 args
.geo
= mp
->m_dir_geo
;
1390 if (da_bno
>= mp
->m_dir_geo
->leafblk
&& da_bno
< mp
->m_dir_geo
->freeblk
)
1391 error
= -libxfs_da_shrink_inode(&args
, da_bno
, bp
);
1393 error
= -libxfs_dir2_shrink_inode(&args
,
1394 xfs_dir2_da_to_db(mp
->m_dir_geo
, da_bno
), bp
);
1396 do_error(_("shrink_inode failed inode %" PRIu64
" block %u\n"),
1398 error
= -libxfs_trans_commit(tp
);
1401 _("directory shrink failed (%d)\n"), error
);
1405 * process a data block, also checks for .. entry
1406 * and corrects it to match what we think .. should be
1409 longform_dir2_entry_check_data(
1414 ino_tree_node_t
*current_irec
,
1415 int current_ino_offset
,
1416 struct xfs_buf
**bpp
,
1417 dir_hash_tab_t
*hashtab
,
1418 freetab_t
**freetabp
,
1422 xfs_dir2_dataptr_t addr
;
1423 xfs_dir2_leaf_entry_t
*blp
;
1425 xfs_dir2_block_tail_t
*btp
;
1426 struct xfs_dir2_data_hdr
*d
;
1428 xfs_dir2_data_entry_t
*dep
;
1429 xfs_dir2_data_unused_t
*dup
;
1430 struct xfs_dir2_data_free
*bf
;
1433 char fname
[MAXNAMELEN
+ 1];
1438 ino_tree_node_t
*irec
;
1449 struct xfs_da_args da
= {
1451 .geo
= mp
->m_dir_geo
,
1457 ptr
= (char *)d
+ mp
->m_dir_geo
->data_entry_offset
;
1459 needscan
= needlog
= 0;
1461 freetab
= *freetabp
;
1463 btp
= xfs_dir2_block_tail_p(mp
->m_dir_geo
, d
);
1464 blp
= xfs_dir2_block_leaf_p(btp
);
1465 endptr
= (char *)blp
;
1466 if (endptr
> (char *)btp
)
1467 endptr
= (char *)btp
;
1468 if (xfs_sb_version_hascrc(&mp
->m_sb
))
1469 wantmagic
= XFS_DIR3_BLOCK_MAGIC
;
1471 wantmagic
= XFS_DIR2_BLOCK_MAGIC
;
1473 endptr
= (char *)d
+ mp
->m_dir_geo
->blksize
;
1474 if (xfs_sb_version_hascrc(&mp
->m_sb
))
1475 wantmagic
= XFS_DIR3_DATA_MAGIC
;
1477 wantmagic
= XFS_DIR2_DATA_MAGIC
;
1479 db
= xfs_dir2_da_to_db(mp
->m_dir_geo
, da_bno
);
1481 /* check for data block beyond expected end */
1482 if (freetab
->naents
<= db
) {
1483 struct freetab_ent e
;
1485 *freetabp
= freetab
= realloc(freetab
, FREETAB_SIZE(db
+ 1));
1487 do_error(_("realloc failed in %s (%zu bytes)\n"),
1488 __func__
, FREETAB_SIZE(db
+ 1));
1492 for (i
= freetab
->naents
; i
< db
; i
++)
1493 freetab
->ents
[i
] = e
;
1494 freetab
->naents
= db
+ 1;
1497 /* check the data block */
1498 while (ptr
< endptr
) {
1500 /* check for freespace */
1501 dup
= (xfs_dir2_data_unused_t
*)ptr
;
1502 if (XFS_DIR2_DATA_FREE_TAG
== be16_to_cpu(dup
->freetag
)) {
1504 /* check for invalid freespace length */
1505 if (ptr
+ be16_to_cpu(dup
->length
) > endptr
||
1506 be16_to_cpu(dup
->length
) == 0 ||
1507 (be16_to_cpu(dup
->length
) &
1508 (XFS_DIR2_DATA_ALIGN
- 1)))
1511 /* check for invalid tag */
1512 if (be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup
)) !=
1513 (char *)dup
- (char *)d
)
1516 /* check for block with no data entries */
1517 if ((ptr
== (char *)d
+ mp
->m_dir_geo
->data_entry_offset
) &&
1518 (ptr
+ be16_to_cpu(dup
->length
) >= endptr
)) {
1524 /* continue at the end of the freespace */
1525 ptr
+= be16_to_cpu(dup
->length
);
1530 /* validate data entry size */
1531 dep
= (xfs_dir2_data_entry_t
*)ptr
;
1532 if (ptr
+ libxfs_dir2_data_entsize(mp
, dep
->namelen
) > endptr
)
1534 if (be16_to_cpu(*libxfs_dir2_data_entry_tag_p(mp
, dep
)) !=
1535 (char *)dep
- (char *)d
)
1537 ptr
+= libxfs_dir2_data_entsize(mp
, dep
->namelen
);
1540 /* did we find an empty or corrupt block? */
1541 if (ptr
!= endptr
) {
1544 _("empty data block %u in directory inode %" PRIu64
": "),
1548 ("corrupt block %u in directory inode %" PRIu64
": "),
1552 do_warn(_("junking block\n"));
1553 dir2_kill_block(mp
, ip
, da_bno
, bp
);
1555 do_warn(_("would junk block\n"));
1556 libxfs_buf_relse(bp
);
1558 freetab
->ents
[db
].v
= NULLDATAOFF
;
1563 /* update number of data blocks processed */
1564 if (freetab
->nents
< db
+ 1)
1565 freetab
->nents
= db
+ 1;
1567 error
= -libxfs_trans_alloc(mp
, &M_RES(mp
)->tr_remove
, 0, 0, 0, &tp
);
1571 libxfs_trans_ijoin(tp
, ip
, 0);
1572 libxfs_trans_bjoin(tp
, bp
);
1573 libxfs_trans_bhold(tp
, bp
);
1574 if (be32_to_cpu(d
->magic
) != wantmagic
) {
1576 _("bad directory block magic # %#x for directory inode %" PRIu64
" block %d: "),
1577 be32_to_cpu(d
->magic
), ip
->i_ino
, da_bno
);
1579 do_warn(_("fixing magic # to %#x\n"), wantmagic
);
1580 d
->magic
= cpu_to_be32(wantmagic
);
1583 do_warn(_("would fix magic # to %#x\n"), wantmagic
);
1586 ptr
= (char *)d
+ mp
->m_dir_geo
->data_entry_offset
;
1588 * look at each entry. reference inode pointed to by each
1589 * entry in the incore inode tree.
1590 * if not a directory, set reached flag, increment link count
1591 * if a directory and reached, mark entry as to be deleted.
1592 * if a directory, check to see if recorded parent
1593 * matches current inode #,
1594 * if so, then set reached flag, increment link count
1595 * of current and child dir inodes, push the child
1596 * directory inode onto the directory stack.
1597 * if current inode != parent, then mark entry to be deleted.
1599 while (ptr
< endptr
) {
1600 dup
= (xfs_dir2_data_unused_t
*)ptr
;
1601 if (be16_to_cpu(dup
->freetag
) == XFS_DIR2_DATA_FREE_TAG
) {
1604 _("directory inode %" PRIu64
" block %u has consecutive free entries: "),
1608 do_warn(_("joining together\n"));
1609 len
= be16_to_cpu(dup
->length
);
1610 libxfs_dir2_data_use_free(&da
, bp
, dup
,
1611 ptr
- (char *)d
, len
, &needlog
,
1613 libxfs_dir2_data_make_free(&da
, bp
,
1614 ptr
- (char *)d
, len
, &needlog
,
1617 do_warn(_("would join together\n"));
1619 ptr
+= be16_to_cpu(dup
->length
);
1623 addr
= xfs_dir2_db_off_to_dataptr(mp
->m_dir_geo
, db
,
1625 dep
= (xfs_dir2_data_entry_t
*)ptr
;
1626 ptr
+= libxfs_dir2_data_entsize(mp
, dep
->namelen
);
1627 inum
= be64_to_cpu(dep
->inumber
);
1630 * skip bogus entries (leading '/'). they'll be deleted
1631 * later. must still log it, else we leak references to
1634 if (dep
->name
[0] == '/') {
1637 libxfs_dir2_data_log_entry(&da
, bp
, dep
);
1641 memmove(fname
, dep
->name
, dep
->namelen
);
1642 fname
[dep
->namelen
] = '\0';
1643 ASSERT(inum
!= NULLFSINO
);
1645 irec
= find_inode_rec(mp
, XFS_INO_TO_AGNO(mp
, inum
),
1646 XFS_INO_TO_AGINO(mp
, inum
));
1650 _("entry \"%s\" in directory inode %" PRIu64
" points to non-existent inode %" PRIu64
""),
1651 fname
, ip
->i_ino
, inum
)) {
1653 libxfs_dir2_data_log_entry(&da
, bp
, dep
);
1657 ino_offset
= XFS_INO_TO_AGINO(mp
, inum
) - irec
->ino_startnum
;
1660 * if it's a free inode, blow out the entry.
1661 * by now, any inode that we think is free
1664 if (is_inode_free(irec
, ino_offset
)) {
1667 _("entry \"%s\" in directory inode %" PRIu64
" points to free inode %" PRIu64
),
1668 fname
, ip
->i_ino
, inum
)) {
1670 libxfs_dir2_data_log_entry(&da
, bp
, dep
);
1676 * check if this inode is lost+found dir in the root
1678 if (inum
== mp
->m_sb
.sb_rootino
&& strcmp(fname
, ORPHANAGE
) == 0) {
1680 * if it's not a directory, trash it
1682 if (!inode_isadir(irec
, ino_offset
)) {
1685 _("%s (ino %" PRIu64
") in root (%" PRIu64
") is not a directory"),
1686 ORPHANAGE
, inum
, ip
->i_ino
)) {
1688 libxfs_dir2_data_log_entry(&da
, bp
, dep
);
1693 * if this is a dup, it will be picked up below,
1694 * otherwise, mark it as the orphanage for later.
1697 orphanage_ino
= inum
;
1701 * check for duplicate names in directory.
1703 if (!dir_hash_add(mp
, hashtab
, addr
, inum
, dep
->namelen
,
1704 dep
->name
, libxfs_dir2_data_get_ftype(mp
, dep
))) {
1707 _("entry \"%s\" (ino %" PRIu64
") in dir %" PRIu64
" is a duplicate name"),
1708 fname
, inum
, ip
->i_ino
)) {
1710 libxfs_dir2_data_log_entry(&da
, bp
, dep
);
1712 if (inum
== orphanage_ino
)
1718 * if just scanning to rebuild a directory due to a ".."
1719 * update, just continue
1725 * skip the '..' entry since it's checked when the
1726 * directory is reached by something else. if it never
1727 * gets reached, it'll be moved to the orphanage and we'll
1728 * take care of it then. If it doesn't exist at all, the
1729 * directory needs to be rebuilt first before being added
1732 if (dep
->namelen
== 2 && dep
->name
[0] == '.' &&
1733 dep
->name
[1] == '.') {
1735 /* ".." should be in the first block */
1738 _("entry \"%s\" (ino %" PRIu64
") in dir %" PRIu64
" is not in the the first block"), fname
,
1741 libxfs_dir2_data_log_entry(&da
, bp
, dep
);
1746 ASSERT(no_modify
|| libxfs_verify_dir_ino(mp
, inum
));
1748 * special case the . entry. we know there's only one
1749 * '.' and only '.' points to itself because bogus entries
1750 * got trashed in phase 3 if there were > 1.
1751 * bump up link count for '.' but don't set reached
1752 * until we're actually reached by another directory
1753 * '..' is already accounted for or will be taken care
1754 * of when directory is moved to orphanage.
1756 if (ip
->i_ino
== inum
) {
1758 (dep
->name
[0] == '.' && dep
->namelen
== 1));
1759 add_inode_ref(current_irec
, current_ino_offset
);
1761 dep
!= (void *)d
+ mp
->m_dir_geo
->data_entry_offset
) {
1762 /* "." should be the first entry */
1765 _("entry \"%s\" in dir %" PRIu64
" is not the first entry"),
1766 fname
, inum
, ip
->i_ino
)) {
1768 libxfs_dir2_data_log_entry(&da
, bp
, dep
);
1775 * skip entries with bogus inumbers if we're in no modify mode
1777 if (no_modify
&& !libxfs_verify_dir_ino(mp
, inum
))
1780 /* validate ftype field if supported */
1781 if (xfs_sb_version_hasftype(&mp
->m_sb
)) {
1785 dir_ftype
= libxfs_dir2_data_get_ftype(mp
, dep
);
1786 ino_ftype
= get_inode_ftype(irec
, ino_offset
);
1788 if (dir_ftype
!= ino_ftype
) {
1791 _("would fix ftype mismatch (%d/%d) in directory/child inode %" PRIu64
"/%" PRIu64
"\n"),
1792 dir_ftype
, ino_ftype
,
1796 _("fixing ftype mismatch (%d/%d) in directory/child inode %" PRIu64
"/%" PRIu64
"\n"),
1797 dir_ftype
, ino_ftype
,
1799 libxfs_dir2_data_put_ftype(mp
, dep
, ino_ftype
);
1800 libxfs_dir2_data_log_entry(&da
, bp
, dep
);
1801 dir_hash_update_ftype(hashtab
, addr
,
1808 * check easy case first, regular inode, just bump
1809 * the link count and continue
1811 if (!inode_isadir(irec
, ino_offset
)) {
1812 add_inode_reached(irec
, ino_offset
);
1815 parent
= get_inode_parent(irec
, ino_offset
);
1816 ASSERT(parent
!= 0);
1819 * bump up the link counts in parent and child
1820 * directory but if the link doesn't agree with
1821 * the .. in the child, blow out the entry.
1822 * if the directory has already been reached,
1823 * blow away the entry also.
1825 if (is_inode_reached(irec
, ino_offset
)) {
1828 _("entry \"%s\" in dir %" PRIu64
" points to an already connected directory inode %" PRIu64
"\n"),
1829 fname
, ip
->i_ino
, inum
);
1830 } else if (parent
== ip
->i_ino
) {
1831 add_inode_reached(irec
, ino_offset
);
1832 add_inode_ref(current_irec
, current_ino_offset
);
1833 } else if (parent
== NULLFSINO
) {
1834 /* ".." was missing, but this entry refers to it,
1835 so, set it as the parent and mark for rebuild */
1837 _("entry \"%s\" in dir ino %" PRIu64
" doesn't have a .. entry, will set it in ino %" PRIu64
".\n"),
1838 fname
, ip
->i_ino
, inum
);
1839 set_inode_parent(irec
, ino_offset
, ip
->i_ino
);
1840 add_inode_reached(irec
, ino_offset
);
1841 add_inode_ref(current_irec
, current_ino_offset
);
1842 add_dotdot_update(XFS_INO_TO_AGNO(mp
, inum
), irec
,
1847 _("entry \"%s\" in dir inode %" PRIu64
" inconsistent with .. value (%" PRIu64
") in ino %" PRIu64
"\n"),
1848 fname
, ip
->i_ino
, parent
, inum
);
1851 if (inum
== orphanage_ino
)
1856 libxfs_dir2_data_log_entry(&da
, bp
, dep
);
1859 _("\twill clear entry \"%s\"\n"),
1862 do_warn(_("\twould clear entry \"%s\"\n"),
1867 *num_illegal
+= nbad
;
1869 libxfs_dir2_data_freescan(mp
, d
, &i
);
1871 libxfs_dir2_data_log_header(&da
, bp
);
1872 error
= -libxfs_trans_commit(tp
);
1875 _("directory block fixing failed (%d)\n"), error
);
1877 /* record the largest free space in the freetab for later checking */
1878 bf
= libxfs_dir2_data_bestfree_p(mp
, d
);
1879 freetab
->ents
[db
].v
= be16_to_cpu(bf
[0].length
);
1880 freetab
->ents
[db
].s
= 0;
1883 /* check v5 metadata */
1885 __check_dir3_header(
1886 struct xfs_mount
*mp
,
1895 if (be64_to_cpu(owner
) != ino
) {
1897 _("expected owner inode %" PRIu64
", got %llu, directory block %" PRIu64
"\n"),
1898 ino
, (unsigned long long)be64_to_cpu(owner
), bp
->b_bn
);
1901 /* verify block number */
1902 if (be64_to_cpu(blkno
) != bp
->b_bn
) {
1904 _("expected block %" PRIu64
", got %llu, directory inode %" PRIu64
"\n"),
1905 bp
->b_bn
, (unsigned long long)be64_to_cpu(blkno
), ino
);
1909 if (platform_uuid_compare(uuid
, &mp
->m_sb
.sb_meta_uuid
) != 0) {
1911 _("wrong FS UUID, directory inode %" PRIu64
" block %" PRIu64
"\n"),
1921 struct xfs_mount
*mp
,
1925 struct xfs_da3_blkinfo
*info
= bp
->b_addr
;
1927 return __check_dir3_header(mp
, bp
, ino
, info
->owner
, info
->blkno
,
1933 struct xfs_mount
*mp
,
1937 struct xfs_dir3_blk_hdr
*info
= bp
->b_addr
;
1939 return __check_dir3_header(mp
, bp
, ino
, info
->owner
, info
->blkno
,
1944 * Check contents of leaf-form block.
1947 longform_dir2_check_leaf(
1950 dir_hash_tab_t
*hashtab
,
1958 xfs_dir2_leaf_t
*leaf
;
1959 xfs_dir2_leaf_tail_t
*ltp
;
1961 struct xfs_dir2_leaf_entry
*ents
;
1962 struct xfs_dir3_icleaf_hdr leafhdr
;
1966 da_bno
= mp
->m_dir_geo
->leafblk
;
1967 error
= dir_read_buf(ip
, da_bno
, &bp
, &xfs_dir3_leaf1_buf_ops
, &fixit
);
1968 if (error
== EFSBADCRC
|| error
== EFSCORRUPTED
|| fixit
) {
1970 _("leaf block %u for directory inode %" PRIu64
" bad CRC\n"),
1975 _("can't read block %u for directory inode %" PRIu64
", error %d\n"),
1976 da_bno
, ip
->i_ino
, error
);
1981 libxfs_dir2_leaf_hdr_from_disk(mp
, &leafhdr
, leaf
);
1982 ents
= leafhdr
.ents
;
1983 ltp
= xfs_dir2_leaf_tail_p(mp
->m_dir_geo
, leaf
);
1984 bestsp
= xfs_dir2_leaf_bests_p(ltp
);
1985 if (!(leafhdr
.magic
== XFS_DIR2_LEAF1_MAGIC
||
1986 leafhdr
.magic
== XFS_DIR3_LEAF1_MAGIC
) ||
1987 leafhdr
.forw
|| leafhdr
.back
||
1988 leafhdr
.count
< leafhdr
.stale
||
1989 leafhdr
.count
> mp
->m_dir_geo
->leaf_max_ents
||
1990 (char *)&ents
[leafhdr
.count
] > (char *)bestsp
) {
1992 _("leaf block %u for directory inode %" PRIu64
" bad header\n"),
1994 libxfs_buf_relse(bp
);
1998 if (leafhdr
.magic
== XFS_DIR3_LEAF1_MAGIC
) {
1999 error
= check_da3_header(mp
, bp
, ip
->i_ino
);
2001 libxfs_buf_relse(bp
);
2006 seeval
= dir_hash_see_all(hashtab
, ents
, leafhdr
.count
, leafhdr
.stale
);
2007 if (dir_hash_check(hashtab
, ip
, seeval
)) {
2008 libxfs_buf_relse(bp
);
2011 badtail
= freetab
->nents
!= be32_to_cpu(ltp
->bestcount
);
2012 for (i
= 0; !badtail
&& i
< be32_to_cpu(ltp
->bestcount
); i
++) {
2013 freetab
->ents
[i
].s
= 1;
2014 badtail
= freetab
->ents
[i
].v
!= be16_to_cpu(bestsp
[i
]);
2018 _("leaf block %u for directory inode %" PRIu64
" bad tail\n"),
2020 libxfs_buf_relse(bp
);
2023 libxfs_buf_relse(bp
);
2028 * Check contents of the node blocks (leaves)
2029 * Looks for matching hash values for the data entries.
2032 longform_dir2_check_node(
2035 dir_hash_tab_t
*hashtab
,
2041 xfs_dir2_free_t
*free
;
2043 xfs_dir2_leaf_t
*leaf
;
2044 xfs_fileoff_t next_da_bno
;
2047 struct xfs_dir2_leaf_entry
*ents
;
2048 struct xfs_dir3_icleaf_hdr leafhdr
;
2049 struct xfs_dir3_icfree_hdr freehdr
;
2054 for (da_bno
= mp
->m_dir_geo
->leafblk
, next_da_bno
= 0;
2055 next_da_bno
!= NULLFILEOFF
&& da_bno
< mp
->m_dir_geo
->freeblk
;
2056 da_bno
= (xfs_dablk_t
)next_da_bno
) {
2057 next_da_bno
= da_bno
+ mp
->m_dir_geo
->fsbcount
- 1;
2058 if (bmap_next_offset(ip
, &next_da_bno
))
2062 * we need to use the da3 node verifier here as it handles the
2063 * fact that reading the leaf hash tree blocks can return either
2064 * leaf or node blocks and calls the correct verifier. If we get
2065 * a node block, then we'll skip it below based on a magic
2068 error
= dir_read_buf(ip
, da_bno
, &bp
, &xfs_da3_node_buf_ops
,
2072 _("can't read leaf block %u for directory inode %" PRIu64
", error %d\n"),
2073 da_bno
, ip
->i_ino
, error
);
2077 libxfs_dir2_leaf_hdr_from_disk(mp
, &leafhdr
, leaf
);
2078 ents
= leafhdr
.ents
;
2079 if (!(leafhdr
.magic
== XFS_DIR2_LEAFN_MAGIC
||
2080 leafhdr
.magic
== XFS_DIR3_LEAFN_MAGIC
||
2081 leafhdr
.magic
== XFS_DA_NODE_MAGIC
||
2082 leafhdr
.magic
== XFS_DA3_NODE_MAGIC
)) {
2084 _("unknown magic number %#x for block %u in directory inode %" PRIu64
"\n"),
2085 leafhdr
.magic
, da_bno
, ip
->i_ino
);
2086 libxfs_buf_relse(bp
);
2090 /* check v5 metadata */
2091 if (leafhdr
.magic
== XFS_DIR3_LEAFN_MAGIC
||
2092 leafhdr
.magic
== XFS_DA3_NODE_MAGIC
) {
2093 error
= check_da3_header(mp
, bp
, ip
->i_ino
);
2095 libxfs_buf_relse(bp
);
2101 if (leafhdr
.magic
== XFS_DA_NODE_MAGIC
||
2102 leafhdr
.magic
== XFS_DA3_NODE_MAGIC
) {
2103 libxfs_buf_relse(bp
);
2108 * If there's a validator error, we need to ensure that we got
2109 * the right ops on the buffer for when we write it back out.
2111 bp
->b_ops
= &xfs_dir3_leafn_buf_ops
;
2112 if (leafhdr
.count
> mp
->m_dir_geo
->leaf_max_ents
||
2113 leafhdr
.count
< leafhdr
.stale
) {
2115 _("leaf block %u for directory inode %" PRIu64
" bad header\n"),
2117 libxfs_buf_relse(bp
);
2120 seeval
= dir_hash_see_all(hashtab
, ents
,
2121 leafhdr
.count
, leafhdr
.stale
);
2122 libxfs_buf_relse(bp
);
2123 if (seeval
!= DIR_HASH_CK_OK
)
2126 if (dir_hash_check(hashtab
, ip
, seeval
))
2129 for (da_bno
= mp
->m_dir_geo
->freeblk
, next_da_bno
= 0;
2130 next_da_bno
!= NULLFILEOFF
;
2131 da_bno
= (xfs_dablk_t
)next_da_bno
) {
2132 next_da_bno
= da_bno
+ mp
->m_dir_geo
->fsbcount
- 1;
2133 if (bmap_next_offset(ip
, &next_da_bno
))
2136 error
= dir_read_buf(ip
, da_bno
, &bp
, &xfs_dir3_free_buf_ops
,
2140 _("can't read freespace block %u for directory inode %" PRIu64
", error %d\n"),
2141 da_bno
, ip
->i_ino
, error
);
2145 libxfs_dir2_free_hdr_from_disk(mp
, &freehdr
, free
);
2146 bests
= freehdr
.bests
;
2147 fdb
= xfs_dir2_da_to_db(mp
->m_dir_geo
, da_bno
);
2148 if (!(freehdr
.magic
== XFS_DIR2_FREE_MAGIC
||
2149 freehdr
.magic
== XFS_DIR3_FREE_MAGIC
) ||
2151 (fdb
- xfs_dir2_byte_to_db(mp
->m_dir_geo
, XFS_DIR2_FREE_OFFSET
)) *
2152 mp
->m_dir_geo
->free_max_bests
||
2153 freehdr
.nvalid
< freehdr
.nused
) {
2155 _("free block %u for directory inode %" PRIu64
" bad header\n"),
2157 libxfs_buf_relse(bp
);
2161 if (freehdr
.magic
== XFS_DIR3_FREE_MAGIC
) {
2162 error
= check_dir3_header(mp
, bp
, ip
->i_ino
);
2164 libxfs_buf_relse(bp
);
2168 for (i
= used
= 0; i
< freehdr
.nvalid
; i
++) {
2169 if (i
+ freehdr
.firstdb
>= freetab
->nents
||
2170 freetab
->ents
[i
+ freehdr
.firstdb
].v
!=
2171 be16_to_cpu(bests
[i
])) {
2173 _("free block %u entry %i for directory ino %" PRIu64
" bad\n"),
2174 da_bno
, i
, ip
->i_ino
);
2175 libxfs_buf_relse(bp
);
2178 used
+= be16_to_cpu(bests
[i
]) != NULLDATAOFF
;
2179 freetab
->ents
[i
+ freehdr
.firstdb
].s
= 1;
2181 if (used
!= freehdr
.nused
) {
2183 _("free block %u for directory inode %" PRIu64
" bad nused\n"),
2185 libxfs_buf_relse(bp
);
2188 libxfs_buf_relse(bp
);
2190 for (i
= 0; i
< freetab
->nents
; i
++) {
2191 if ((freetab
->ents
[i
].s
== 0) &&
2192 (freetab
->ents
[i
].v
!= NULLDATAOFF
)) {
2194 _("missing freetab entry %u for directory inode %" PRIu64
"\n"),
2203 * If a directory is corrupt, we need to read in as many entries as possible,
2204 * destroy the entry and create a new one with recovered name/inode pairs.
2205 * (ie. get libxfs to do all the grunt work)
2208 longform_dir2_entry_check(xfs_mount_t
*mp
,
2213 ino_tree_node_t
*irec
,
2215 dir_hash_tab_t
*hashtab
)
2217 struct xfs_buf
**bplist
;
2224 xfs_fileoff_t next_da_bno
;
2228 struct xfs_da_args args
;
2231 freetab
= malloc(FREETAB_SIZE(ip
->i_d
.di_size
/ mp
->m_dir_geo
->blksize
));
2233 do_error(_("malloc failed in %s (%" PRId64
" bytes)\n"),
2235 FREETAB_SIZE(ip
->i_d
.di_size
/ mp
->m_dir_geo
->blksize
));
2238 freetab
->naents
= ip
->i_d
.di_size
/ mp
->m_dir_geo
->blksize
;
2240 for (i
= 0; i
< freetab
->naents
; i
++) {
2241 freetab
->ents
[i
].v
= NULLDATAOFF
;
2242 freetab
->ents
[i
].s
= 0;
2244 num_bps
= freetab
->naents
;
2245 bplist
= calloc(num_bps
, sizeof(struct xfs_buf
*));
2247 do_error(_("calloc failed in %s (%zu bytes)\n"),
2248 __func__
, num_bps
* sizeof(struct xfs_buf
*));
2250 /* is this a block, leaf, or node directory? */
2252 args
.geo
= mp
->m_dir_geo
;
2253 libxfs_dir2_isblock(&args
, &isblock
);
2254 libxfs_dir2_isleaf(&args
, &isleaf
);
2256 /* check directory "data" blocks (ie. name/inode pairs) */
2257 for (da_bno
= 0, next_da_bno
= 0;
2258 next_da_bno
!= NULLFILEOFF
&& da_bno
< mp
->m_dir_geo
->leafblk
;
2259 da_bno
= (xfs_dablk_t
)next_da_bno
) {
2260 const struct xfs_buf_ops
*ops
;
2262 struct xfs_dir2_data_hdr
*d
;
2264 next_da_bno
= da_bno
+ mp
->m_dir_geo
->fsbcount
- 1;
2265 if (bmap_next_offset(ip
, &next_da_bno
)) {
2267 * if this is the first block, there isn't anything we
2268 * can recover so we just trash it.
2277 db
= xfs_dir2_da_to_db(mp
->m_dir_geo
, da_bno
);
2278 if (db
>= num_bps
) {
2279 int last_size
= num_bps
;
2281 /* more data blocks than expected */
2283 bplist
= realloc(bplist
, num_bps
* sizeof(struct xfs_buf
*));
2285 do_error(_("realloc failed in %s (%zu bytes)\n"),
2287 num_bps
* sizeof(struct xfs_buf
*));
2288 /* Initialize the new elements */
2289 for (i
= last_size
; i
< num_bps
; i
++)
2294 ops
= &xfs_dir3_block_buf_ops
;
2296 ops
= &xfs_dir3_data_buf_ops
;
2298 error
= dir_read_buf(ip
, da_bno
, &bplist
[db
], ops
, &fixit
);
2301 _("can't read data block %u for directory inode %" PRIu64
" error %d\n"),
2302 da_bno
, ino
, error
);
2306 * we try to read all "data" blocks, but if we are in
2307 * block form and we fail, there isn't anything else to
2308 * read, and nothing we can do but trash it.
2317 /* check v5 metadata */
2318 d
= bplist
[db
]->b_addr
;
2319 if (be32_to_cpu(d
->magic
) == XFS_DIR3_BLOCK_MAGIC
||
2320 be32_to_cpu(d
->magic
) == XFS_DIR3_DATA_MAGIC
) {
2321 struct xfs_buf
*bp
= bplist
[db
];
2323 error
= check_dir3_header(mp
, bp
, ino
);
2330 longform_dir2_entry_check_data(mp
, ip
, num_illegal
, need_dot
,
2331 irec
, ino_offset
, &bplist
[db
], hashtab
,
2332 &freetab
, da_bno
, isblock
);
2334 fixit
|= (*num_illegal
!= 0) || dir2_is_badino(ino
) || *need_dot
;
2336 if (!dotdot_update
) {
2337 /* check btree and freespace */
2339 struct xfs_dir2_data_hdr
*block
;
2340 xfs_dir2_block_tail_t
*btp
;
2341 xfs_dir2_leaf_entry_t
*blp
;
2343 block
= bplist
[0]->b_addr
;
2344 btp
= xfs_dir2_block_tail_p(mp
->m_dir_geo
, block
);
2345 blp
= xfs_dir2_block_leaf_p(btp
);
2346 seeval
= dir_hash_see_all(hashtab
, blp
,
2347 be32_to_cpu(btp
->count
),
2348 be32_to_cpu(btp
->stale
));
2349 if (dir_hash_check(hashtab
, ip
, seeval
))
2351 } else if (isleaf
) {
2352 fixit
|= longform_dir2_check_leaf(mp
, ip
, hashtab
,
2355 fixit
|= longform_dir2_check_node(mp
, ip
, hashtab
,
2360 if (!no_modify
&& (fixit
|| dotdot_update
)) {
2361 dir_hash_dup_names(hashtab
);
2362 for (i
= 0; i
< num_bps
; i
++)
2364 libxfs_buf_relse(bplist
[i
]);
2365 longform_dir2_rebuild(mp
, ino
, ip
, irec
, ino_offset
, hashtab
);
2369 if (fixit
|| dotdot_update
)
2371 _("would rebuild directory inode %" PRIu64
"\n"), ino
);
2372 for (i
= 0; i
< num_bps
; i
++)
2374 libxfs_buf_relse(bplist
[i
]);
2382 * shortform directory v2 processing routines -- entry verification and
2383 * bad entry deletion (pruning).
2385 static struct xfs_dir2_sf_entry
*
2386 shortform_dir2_junk(
2387 struct xfs_mount
*mp
,
2388 struct xfs_dir2_sf_hdr
*sfp
,
2389 struct xfs_dir2_sf_entry
*sfep
,
2396 struct xfs_dir2_sf_entry
*next_sfep
;
2400 if (lino
== orphanage_ino
)
2403 next_elen
= libxfs_dir2_sf_entsize(mp
, sfp
, sfep
->namelen
);
2404 next_sfep
= libxfs_dir2_sf_nextentry(mp
, sfp
, sfep
);
2407 * if we are just checking, simply return the pointer to the next entry
2408 * here so that the checking loop can continue.
2411 do_warn(_("would junk entry\n"));
2416 * now move all the remaining entries down over the junked entry and
2417 * clear the newly unused bytes at the tail of the directory region.
2419 next_len
= *max_size
- ((intptr_t)next_sfep
- (intptr_t)sfp
);
2420 *max_size
-= next_elen
;
2421 *bytes_deleted
+= next_elen
;
2423 memmove(sfep
, next_sfep
, next_len
);
2424 memset((void *)((intptr_t)sfep
+ next_len
), 0, next_elen
);
2429 * WARNING: drop the index i by one so it matches the decremented count
2430 * for accurate comparisons in the loop test
2435 do_warn(_("junking entry\n"));
2442 shortform_dir2_entry_check(xfs_mount_t
*mp
,
2446 ino_tree_node_t
*current_irec
,
2447 int current_ino_offset
,
2448 dir_hash_tab_t
*hashtab
)
2452 struct xfs_dir2_sf_hdr
*sfp
;
2453 struct xfs_dir2_sf_entry
*sfep
;
2454 struct xfs_dir2_sf_entry
*next_sfep
;
2455 struct xfs_ifork
*ifp
;
2456 struct ino_tree_node
*irec
;
2463 char fname
[MAXNAMELEN
+ 1];
2467 sfp
= (struct xfs_dir2_sf_hdr
*) ifp
->if_u1
.if_data
;
2471 max_size
= ifp
->if_bytes
;
2472 ASSERT(ip
->i_d
.di_size
<= ifp
->if_bytes
);
2475 * if just rebuild a directory due to a "..", update and return
2477 if (dotdot_update
) {
2478 parent
= get_inode_parent(current_irec
, current_ino_offset
);
2481 _("would set .. in sf dir inode %" PRIu64
" to %" PRIu64
"\n"),
2485 _("setting .. in sf dir inode %" PRIu64
" to %" PRIu64
"\n"),
2487 libxfs_dir2_sf_put_parent_ino(sfp
, parent
);
2494 * no '.' entry in shortform dirs, just bump up ref count by 1
2495 * '..' was already (or will be) accounted for and checked when
2496 * the directory is reached or will be taken care of when the
2497 * directory is moved to orphanage.
2499 add_inode_ref(current_irec
, current_ino_offset
);
2502 * Initialise i8 counter -- the parent inode number counts as well.
2504 i8
= libxfs_dir2_sf_get_parent_ino(sfp
) > XFS_DIR2_MAX_SHORT_INUM
;
2507 * now run through entries, stop at first bad entry, don't need
2508 * to skip over '..' since that's encoded in its own field and
2509 * no need to worry about '.' since it doesn't exist.
2511 sfep
= next_sfep
= xfs_dir2_sf_firstentry(sfp
);
2513 for (i
= 0; i
< sfp
->count
&& max_size
>
2514 (intptr_t)next_sfep
- (intptr_t)sfp
;
2515 sfep
= next_sfep
, i
++) {
2518 lino
= libxfs_dir2_sf_get_ino(mp
, sfp
, sfep
);
2520 namelen
= sfep
->namelen
;
2522 ASSERT(no_modify
|| namelen
> 0);
2524 if (no_modify
&& namelen
== 0) {
2526 * if we're really lucky, this is
2527 * the last entry in which case we
2528 * can use the dir size to set the
2529 * namelen value. otherwise, forget
2530 * it because we're not going to be
2531 * able to find the next entry.
2535 if (i
== sfp
->count
- 1) {
2536 namelen
= ip
->i_d
.di_size
-
2537 ((intptr_t) &sfep
->name
[0] -
2541 * don't process the rest of the directory,
2542 * break out of processing loop
2546 } else if (no_modify
&& (intptr_t) sfep
- (intptr_t) sfp
+
2547 + libxfs_dir2_sf_entsize(mp
, sfp
, sfep
->namelen
)
2548 > ip
->i_d
.di_size
) {
2551 if (i
== sfp
->count
- 1) {
2552 namelen
= ip
->i_d
.di_size
-
2553 ((intptr_t) &sfep
->name
[0] -
2557 * don't process the rest of the directory,
2558 * break out of processing loop
2564 memmove(fname
, sfep
->name
, sfep
->namelen
);
2565 fname
[sfep
->namelen
] = '\0';
2567 ASSERT(no_modify
|| (lino
!= NULLFSINO
&& lino
!= 0));
2568 ASSERT(no_modify
|| libxfs_verify_dir_ino(mp
, lino
));
2571 * Also skip entries with bogus inode numbers if we're
2572 * in no modify mode.
2575 if (no_modify
&& !libxfs_verify_dir_ino(mp
, lino
)) {
2576 next_sfep
= libxfs_dir2_sf_nextentry(mp
, sfp
, sfep
);
2580 irec
= find_inode_rec(mp
, XFS_INO_TO_AGNO(mp
, lino
),
2581 XFS_INO_TO_AGINO(mp
, lino
));
2585 _("entry \"%s\" in shortform directory %" PRIu64
" references non-existent inode %" PRIu64
"\n"),
2587 next_sfep
= shortform_dir2_junk(mp
, sfp
, sfep
, lino
,
2588 &max_size
, &i
, &bytes_deleted
,
2593 ino_offset
= XFS_INO_TO_AGINO(mp
, lino
) - irec
->ino_startnum
;
2596 * if it's a free inode, blow out the entry.
2597 * by now, any inode that we think is free
2600 if (is_inode_free(irec
, ino_offset
)) {
2602 _("entry \"%s\" in shortform directory inode %" PRIu64
" points to free inode %" PRIu64
"\n"),
2604 next_sfep
= shortform_dir2_junk(mp
, sfp
, sfep
, lino
,
2605 &max_size
, &i
, &bytes_deleted
,
2610 * check if this inode is lost+found dir in the root
2612 if (ino
== mp
->m_sb
.sb_rootino
&& strcmp(fname
, ORPHANAGE
) == 0) {
2614 * if it's not a directory, trash it
2616 if (!inode_isadir(irec
, ino_offset
)) {
2618 _("%s (ino %" PRIu64
") in root (%" PRIu64
") is not a directory"),
2619 ORPHANAGE
, lino
, ino
);
2620 next_sfep
= shortform_dir2_junk(mp
, sfp
, sfep
,
2621 lino
, &max_size
, &i
,
2622 &bytes_deleted
, ino_dirty
);
2626 * if this is a dup, it will be picked up below,
2627 * otherwise, mark it as the orphanage for later.
2630 orphanage_ino
= lino
;
2633 * check for duplicate names in directory.
2635 if (!dir_hash_add(mp
, hashtab
, (xfs_dir2_dataptr_t
)
2636 (sfep
- xfs_dir2_sf_firstentry(sfp
)),
2637 lino
, sfep
->namelen
, sfep
->name
,
2638 libxfs_dir2_sf_get_ftype(mp
, sfep
))) {
2640 _("entry \"%s\" (ino %" PRIu64
") in dir %" PRIu64
" is a duplicate name"),
2642 next_sfep
= shortform_dir2_junk(mp
, sfp
, sfep
, lino
,
2643 &max_size
, &i
, &bytes_deleted
,
2648 if (!inode_isadir(irec
, ino_offset
)) {
2650 * check easy case first, regular inode, just bump
2653 add_inode_reached(irec
, ino_offset
);
2655 parent
= get_inode_parent(irec
, ino_offset
);
2658 * bump up the link counts in parent and child.
2659 * directory but if the link doesn't agree with
2660 * the .. in the child, blow out the entry
2662 if (is_inode_reached(irec
, ino_offset
)) {
2664 _("entry \"%s\" in directory inode %" PRIu64
2665 " references already connected inode %" PRIu64
".\n"),
2667 next_sfep
= shortform_dir2_junk(mp
, sfp
, sfep
,
2668 lino
, &max_size
, &i
,
2669 &bytes_deleted
, ino_dirty
);
2671 } else if (parent
== ino
) {
2672 add_inode_reached(irec
, ino_offset
);
2673 add_inode_ref(current_irec
, current_ino_offset
);
2674 } else if (parent
== NULLFSINO
) {
2675 /* ".." was missing, but this entry refers to it,
2676 so, set it as the parent and mark for rebuild */
2678 _("entry \"%s\" in dir ino %" PRIu64
" doesn't have a .. entry, will set it in ino %" PRIu64
".\n"),
2680 set_inode_parent(irec
, ino_offset
, ino
);
2681 add_inode_reached(irec
, ino_offset
);
2682 add_inode_ref(current_irec
, current_ino_offset
);
2683 add_dotdot_update(XFS_INO_TO_AGNO(mp
, lino
),
2687 _("entry \"%s\" in directory inode %" PRIu64
2688 " not consistent with .. value (%" PRIu64
2689 ") in inode %" PRIu64
",\n"),
2690 fname
, ino
, parent
, lino
);
2691 next_sfep
= shortform_dir2_junk(mp
, sfp
, sfep
,
2692 lino
, &max_size
, &i
,
2693 &bytes_deleted
, ino_dirty
);
2698 /* validate ftype field if supported */
2699 if (xfs_sb_version_hasftype(&mp
->m_sb
)) {
2703 dir_ftype
= libxfs_dir2_sf_get_ftype(mp
, sfep
);
2704 ino_ftype
= get_inode_ftype(irec
, ino_offset
);
2706 if (dir_ftype
!= ino_ftype
) {
2709 _("would fix ftype mismatch (%d/%d) in directory/child inode %" PRIu64
"/%" PRIu64
"\n"),
2710 dir_ftype
, ino_ftype
,
2714 _("fixing ftype mismatch (%d/%d) in directory/child inode %" PRIu64
"/%" PRIu64
"\n"),
2715 dir_ftype
, ino_ftype
,
2717 libxfs_dir2_sf_put_ftype(mp
, sfep
,
2719 dir_hash_update_ftype(hashtab
,
2720 (xfs_dir2_dataptr_t
)(sfep
- xfs_dir2_sf_firstentry(sfp
)),
2727 if (lino
> XFS_DIR2_MAX_SHORT_INUM
)
2731 * go onto next entry - we have to take entries with bad namelen
2732 * into account in no modify mode since we calculate size based
2735 ASSERT(no_modify
|| bad_sfnamelen
== 0);
2736 next_sfep
= (struct xfs_dir2_sf_entry
*)((intptr_t)sfep
+
2738 ? libxfs_dir2_sf_entsize(mp
, sfp
, namelen
)
2739 : libxfs_dir2_sf_entsize(mp
, sfp
, sfep
->namelen
)));
2742 if (sfp
->i8count
!= i8
) {
2744 do_warn(_("would fix i8count in inode %" PRIu64
"\n"),
2748 struct xfs_dir2_sf_entry
*tmp_sfep
;
2750 tmp_sfep
= next_sfep
;
2751 process_sf_dir2_fixi8(mp
, sfp
, &tmp_sfep
);
2753 (intptr_t)next_sfep
-
2755 next_sfep
= tmp_sfep
;
2759 do_warn(_("fixing i8count in inode %" PRIu64
"\n"),
2765 * sync up sizes if required
2767 if (*ino_dirty
&& bytes_deleted
> 0) {
2769 libxfs_idata_realloc(ip
, -bytes_deleted
, XFS_DATA_FORK
);
2770 ip
->i_d
.di_size
-= bytes_deleted
;
2773 if (ip
->i_d
.di_size
!= ip
->i_df
.if_bytes
) {
2774 ASSERT(ip
->i_df
.if_bytes
== (xfs_fsize_t
)
2775 ((intptr_t) next_sfep
- (intptr_t) sfp
));
2776 ip
->i_d
.di_size
= (xfs_fsize_t
)
2777 ((intptr_t) next_sfep
- (intptr_t) sfp
);
2779 _("setting size to %" PRId64
" bytes to reflect junked entries\n"),
2786 * processes all reachable inodes in directories
2791 xfs_agnumber_t agno
,
2792 ino_tree_node_t
*irec
,
2798 dir_hash_tab_t
*hashtab
;
2800 int dirty
, num_illegal
, error
, nres
;
2802 ino
= XFS_AGINO_TO_INO(mp
, agno
, irec
->ino_startnum
+ ino_offset
);
2805 * open up directory inode, check all entries,
2806 * then call prune_dir_entries to remove all
2807 * remaining illegal directory entries.
2810 ASSERT(!is_inode_refchecked(irec
, ino_offset
) || dotdot_update
);
2812 error
= -libxfs_iget(mp
, NULL
, ino
, 0, &ip
);
2816 _("couldn't map inode %" PRIu64
", err = %d\n"),
2820 _("couldn't map inode %" PRIu64
", err = %d\n"),
2823 * see below for what we're doing if this
2824 * is root. Why do we need to do this here?
2825 * to ensure that the root doesn't show up
2826 * as being disconnected in the no_modify case.
2828 if (mp
->m_sb
.sb_rootino
== ino
) {
2829 add_inode_reached(irec
, 0);
2830 add_inode_ref(irec
, 0);
2834 add_inode_refchecked(irec
, 0);
2838 need_dot
= dirty
= num_illegal
= 0;
2840 if (mp
->m_sb
.sb_rootino
== ino
) {
2842 * mark root inode reached and bump up
2843 * link count for root inode to account
2844 * for '..' entry since the root inode is
2845 * never reached by a parent. we know
2846 * that root's '..' is always good --
2847 * guaranteed by phase 3 and/or below.
2849 add_inode_reached(irec
, ino_offset
);
2852 add_inode_refchecked(irec
, ino_offset
);
2854 hashtab
= dir_hash_init(ip
->i_d
.di_size
);
2857 * look for bogus entries
2859 switch (ip
->i_df
.if_format
) {
2860 case XFS_DINODE_FMT_EXTENTS
:
2861 case XFS_DINODE_FMT_BTREE
:
2863 * also check for missing '.' in longform dirs.
2864 * missing .. entries are added if required when
2865 * the directory is connected to lost+found. but
2866 * we need to create '.' entries here.
2868 longform_dir2_entry_check(mp
, ino
, ip
,
2869 &num_illegal
, &need_dot
,
2874 case XFS_DINODE_FMT_LOCAL
:
2876 * using the remove reservation is overkill
2877 * since at most we'll only need to log the
2878 * inode but it's easier than wedging a
2879 * new define in ourselves.
2881 nres
= no_modify
? 0 : XFS_REMOVE_SPACE_RES(mp
);
2882 error
= -libxfs_trans_alloc(mp
, &M_RES(mp
)->tr_remove
,
2887 libxfs_trans_ijoin(tp
, ip
, 0);
2889 shortform_dir2_entry_check(mp
, ino
, ip
, &dirty
,
2893 ASSERT(dirty
== 0 || (dirty
&& !no_modify
));
2895 libxfs_trans_log_inode(tp
, ip
,
2896 XFS_ILOG_CORE
| XFS_ILOG_DDATA
);
2897 error
= -libxfs_trans_commit(tp
);
2900 _("error %d fixing shortform directory %llu\n"),
2902 (unsigned long long)ip
->i_ino
);
2904 libxfs_trans_cancel(tp
);
2911 dir_hash_done(hashtab
);
2914 * if we have to create a .. for /, do it now *before*
2915 * we delete the bogus entries, otherwise the directory
2916 * could transform into a shortform dir which would
2917 * probably cause the simulation to choke. Even
2918 * if the illegal entries get shifted around, it's ok
2919 * because the entries are structurally intact and in
2920 * in hash-value order so the simulation won't get confused
2921 * if it has to move them around.
2923 if (!no_modify
&& need_root_dotdot
&& ino
== mp
->m_sb
.sb_rootino
) {
2924 ASSERT(ip
->i_df
.if_format
!= XFS_DINODE_FMT_LOCAL
);
2926 do_warn(_("recreating root directory .. entry\n"));
2928 nres
= XFS_MKDIR_SPACE_RES(mp
, 2);
2929 error
= -libxfs_trans_alloc(mp
, &M_RES(mp
)->tr_mkdir
,
2934 libxfs_trans_ijoin(tp
, ip
, 0);
2936 error
= -libxfs_dir_createname(tp
, ip
, &xfs_name_dotdot
,
2940 _("can't make \"..\" entry in root inode %" PRIu64
", createname error %d\n"), ino
, error
);
2942 libxfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
2943 error
= -libxfs_trans_commit(tp
);
2946 _("root inode \"..\" entry recreation failed (%d)\n"), error
);
2948 need_root_dotdot
= 0;
2949 } else if (need_root_dotdot
&& ino
== mp
->m_sb
.sb_rootino
) {
2950 do_warn(_("would recreate root directory .. entry\n"));
2954 * if we need to create the '.' entry, do so only if
2955 * the directory is a longform dir. if it's been
2956 * turned into a shortform dir, then the inode is ok
2957 * since shortform dirs have no '.' entry and the inode
2958 * has already been committed by prune_lf_dir_entry().
2962 * bump up our link count but don't
2963 * bump up the inode link count. chances
2964 * are good that even though we lost '.'
2965 * the inode link counts reflect '.' so
2966 * leave the inode link count alone and if
2967 * it turns out to be wrong, we'll catch
2970 add_inode_ref(irec
, ino_offset
);
2974 _("would create missing \".\" entry in dir ino %" PRIu64
"\n"),
2976 } else if (ip
->i_df
.if_format
!= XFS_DINODE_FMT_LOCAL
) {
2978 * need to create . entry in longform dir.
2981 _("creating missing \".\" entry in dir ino %" PRIu64
"\n"), ino
);
2983 nres
= XFS_MKDIR_SPACE_RES(mp
, 1);
2984 error
= -libxfs_trans_alloc(mp
, &M_RES(mp
)->tr_mkdir
,
2989 libxfs_trans_ijoin(tp
, ip
, 0);
2991 error
= -libxfs_dir_createname(tp
, ip
, &xfs_name_dot
,
2995 _("can't make \".\" entry in dir ino %" PRIu64
", createname error %d\n"),
2998 libxfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
2999 error
= -libxfs_trans_commit(tp
);
3002 _("root inode \".\" entry recreation failed (%d)\n"), error
);
3009 * mark realtime bitmap and summary inodes as reached.
3010 * quota inode will be marked here as well
3013 mark_standalone_inodes(xfs_mount_t
*mp
)
3015 ino_tree_node_t
*irec
;
3018 irec
= find_inode_rec(mp
, XFS_INO_TO_AGNO(mp
, mp
->m_sb
.sb_rbmino
),
3019 XFS_INO_TO_AGINO(mp
, mp
->m_sb
.sb_rbmino
));
3021 offset
= XFS_INO_TO_AGINO(mp
, mp
->m_sb
.sb_rbmino
) -
3024 add_inode_reached(irec
, offset
);
3026 irec
= find_inode_rec(mp
, XFS_INO_TO_AGNO(mp
, mp
->m_sb
.sb_rsumino
),
3027 XFS_INO_TO_AGINO(mp
, mp
->m_sb
.sb_rsumino
));
3029 offset
= XFS_INO_TO_AGINO(mp
, mp
->m_sb
.sb_rsumino
) -
3032 add_inode_reached(irec
, offset
);
3035 if (mp
->m_sb
.sb_uquotino
3036 && mp
->m_sb
.sb_uquotino
!= NULLFSINO
) {
3037 irec
= find_inode_rec(mp
, XFS_INO_TO_AGNO(mp
,
3038 mp
->m_sb
.sb_uquotino
),
3039 XFS_INO_TO_AGINO(mp
, mp
->m_sb
.sb_uquotino
));
3040 offset
= XFS_INO_TO_AGINO(mp
, mp
->m_sb
.sb_uquotino
)
3041 - irec
->ino_startnum
;
3042 add_inode_reached(irec
, offset
);
3044 if (mp
->m_sb
.sb_gquotino
3045 && mp
->m_sb
.sb_gquotino
!= NULLFSINO
) {
3046 irec
= find_inode_rec(mp
, XFS_INO_TO_AGNO(mp
,
3047 mp
->m_sb
.sb_gquotino
),
3048 XFS_INO_TO_AGINO(mp
, mp
->m_sb
.sb_gquotino
));
3049 offset
= XFS_INO_TO_AGINO(mp
, mp
->m_sb
.sb_gquotino
)
3050 - irec
->ino_startnum
;
3051 add_inode_reached(irec
, offset
);
3053 if (mp
->m_sb
.sb_pquotino
3054 && mp
->m_sb
.sb_pquotino
!= NULLFSINO
) {
3055 irec
= find_inode_rec(mp
, XFS_INO_TO_AGNO(mp
,
3056 mp
->m_sb
.sb_pquotino
),
3057 XFS_INO_TO_AGINO(mp
, mp
->m_sb
.sb_pquotino
));
3058 offset
= XFS_INO_TO_AGINO(mp
, mp
->m_sb
.sb_pquotino
)
3059 - irec
->ino_startnum
;
3060 add_inode_reached(irec
, offset
);
3066 check_for_orphaned_inodes(
3068 xfs_agnumber_t agno
,
3069 ino_tree_node_t
*irec
)
3074 for (i
= 0; i
< XFS_INODES_PER_CHUNK
; i
++) {
3075 ASSERT(is_inode_confirmed(irec
, i
));
3076 if (is_inode_free(irec
, i
))
3079 if (is_inode_reached(irec
, i
))
3082 ASSERT(inode_isadir(irec
, i
) ||
3083 num_inode_references(irec
, i
) == 0);
3085 ino
= XFS_AGINO_TO_INO(mp
, agno
, i
+ irec
->ino_startnum
);
3086 if (inode_isadir(irec
, i
))
3087 do_warn(_("disconnected dir inode %" PRIu64
", "), ino
);
3089 do_warn(_("disconnected inode %" PRIu64
", "), ino
);
3092 orphanage_ino
= mk_orphanage(mp
);
3093 do_warn(_("moving to %s\n"), ORPHANAGE
);
3094 mv_orphanage(mp
, ino
, inode_isadir(irec
, i
));
3096 do_warn(_("would move to %s\n"), ORPHANAGE
);
3099 * for read-only case, even though the inode isn't
3100 * really reachable, set the flag (and bump our link
3101 * count) anyway to fool phase 7
3103 add_inode_reached(irec
, i
);
3109 struct workqueue
*wq
,
3110 xfs_agnumber_t agno
,
3113 ino_tree_node_t
*irec
;
3115 prefetch_args_t
*pf_args
= arg
;
3117 wait_for_inode_prefetch(pf_args
);
3120 do_log(_(" - agno = %d\n"), agno
);
3122 for (irec
= findfirst_inode_rec(agno
); irec
; irec
= next_ino_rec(irec
)) {
3123 if (irec
->ino_isa_dir
== 0)
3127 sem_post(&pf_args
->ra_count
);
3129 sem_getvalue(&pf_args
->ra_count
, &i
);
3131 "processing inode chunk %p in AG %d (sem count = %d)",
3136 for (i
= 0; i
< XFS_INODES_PER_CHUNK
; i
++) {
3137 if (inode_isadir(irec
, i
))
3138 process_dir_inode(wq
->wq_ctx
, agno
, irec
, i
);
3141 cleanup_inode_prefetch(pf_args
);
3145 update_missing_dotdot_entries(
3148 dotdot_update_t
*dir
;
3151 * these entries parents were updated, rebuild them again
3152 * set dotdot_update flag so processing routines do not count links
3155 while (!list_empty(&dotdot_update_list
)) {
3156 dir
= list_entry(dotdot_update_list
.prev
, struct dotdot_update
,
3158 list_del(&dir
->list
);
3159 process_dir_inode(mp
, dir
->agno
, dir
->irec
, dir
->ino_offset
);
3166 struct xfs_mount
*mp
)
3168 do_inode_prefetch(mp
, 0, traverse_function
, false, true);
3172 phase6(xfs_mount_t
*mp
)
3174 ino_tree_node_t
*irec
;
3177 memset(&zerocr
, 0, sizeof(struct cred
));
3178 memset(&zerofsx
, 0, sizeof(struct fsxattr
));
3181 do_log(_("Phase 6 - check inode connectivity...\n"));
3183 incore_ext_teardown(mp
);
3185 add_ino_ex_data(mp
);
3188 * verify existence of root directory - if we have to
3189 * make one, it's ok for the incore data structs not to
3190 * know about it since everything about it (and the other
3191 * inodes in its chunk if a new chunk was created) are ok
3193 if (need_root_inode
) {
3195 do_warn(_("reinitializing root directory\n"));
3197 need_root_inode
= 0;
3198 need_root_dotdot
= 0;
3200 do_warn(_("would reinitialize root directory\n"));
3206 do_warn(_("reinitializing realtime bitmap inode\n"));
3210 do_warn(_("would reinitialize realtime bitmap inode\n"));
3216 do_warn(_("reinitializing realtime summary inode\n"));
3220 do_warn(_("would reinitialize realtime summary inode\n"));
3226 _(" - resetting contents of realtime bitmap and summary inodes\n"));
3227 if (fill_rbmino(mp
)) {
3229 _("Warning: realtime bitmap may be inconsistent\n"));
3232 if (fill_rsumino(mp
)) {
3234 _("Warning: realtime bitmap may be inconsistent\n"));
3238 mark_standalone_inodes(mp
);
3240 do_log(_(" - traversing filesystem ...\n"));
3242 irec
= find_inode_rec(mp
, XFS_INO_TO_AGNO(mp
, mp
->m_sb
.sb_rootino
),
3243 XFS_INO_TO_AGINO(mp
, mp
->m_sb
.sb_rootino
));
3246 * we always have a root inode, even if it's free...
3247 * if the root is free, forget it, lost+found is already gone
3249 if (is_inode_free(irec
, 0) || !inode_isadir(irec
, 0)) {
3250 need_root_inode
= 1;
3254 * then process all inodes by walking incore inode tree
3259 * any directories that had updated ".." entries, rebuild them now
3261 update_missing_dotdot_entries(mp
);
3263 do_log(_(" - traversal finished ...\n"));
3264 do_log(_(" - moving disconnected inodes to %s ...\n"),
3268 * move all disconnected inodes to the orphanage
3270 for (i
= 0; i
< glob_agcount
; i
++) {
3271 irec
= findfirst_inode_rec(i
);
3272 while (irec
!= NULL
) {
3273 check_for_orphaned_inodes(mp
, i
, irec
);
3274 irec
= next_ino_rec(irec
);