2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "err_protos.h"
33 static struct cred zerocr
;
34 static struct fsxattr zerofsx
;
35 static xfs_ino_t orphanage_ino
;
37 static struct xfs_name xfs_name_dot
= {(unsigned char *)".",
42 * Data structures used to keep track of directories where the ".."
43 * entries are updated. These must be rebuilt after the initial pass
45 typedef struct dotdot_update
{
46 struct list_head list
;
47 ino_tree_node_t
*irec
;
52 static LIST_HEAD(dotdot_update_list
);
53 static int dotdot_update
;
58 ino_tree_node_t
*irec
,
61 dotdot_update_t
*dir
= malloc(sizeof(dotdot_update_t
));
64 do_error(_("malloc failed add_dotdot_update (%zu bytes)\n"),
65 sizeof(dotdot_update_t
));
67 INIT_LIST_HEAD(&dir
->list
);
70 dir
->ino_offset
= ino_offset
;
72 list_add(&dir
->list
, &dotdot_update_list
);
76 * Data structures and routines to keep track of directory entries
77 * and whether their leaf entry has been seen. Also used for name
78 * duplicate checking and rebuilding step if required.
80 typedef struct dir_hash_ent
{
81 struct dir_hash_ent
*nextbyaddr
; /* next in addr bucket */
82 struct dir_hash_ent
*nextbyhash
; /* next in name bucket */
83 struct dir_hash_ent
*nextbyorder
; /* next in order added */
84 xfs_dahash_t hashval
; /* hash value of name */
85 __uint32_t address
; /* offset of data entry */
86 xfs_ino_t inum
; /* inode num of entry */
87 short junkit
; /* name starts with / */
88 short seen
; /* have seen leaf entry */
92 typedef struct dir_hash_tab
{
93 int size
; /* size of hash tables */
94 int names_duped
; /* 1 = ent names malloced */
95 dir_hash_ent_t
*first
; /* ptr to first added entry */
96 dir_hash_ent_t
*last
; /* ptr to last added entry */
97 dir_hash_ent_t
**byhash
; /* ptr to name hash buckets */
98 dir_hash_ent_t
**byaddr
; /* ptr to addr hash buckets */
101 #define DIR_HASH_TAB_SIZE(n) \
102 (sizeof(dir_hash_tab_t) + (sizeof(dir_hash_ent_t *) * (n) * 2))
103 #define DIR_HASH_FUNC(t,a) ((a) % (t)->size)
106 * Track the contents of the freespace table in a directory.
108 typedef struct freetab
{
109 int naents
; /* expected number of data blocks */
110 int nents
; /* number of data blocks processed */
112 xfs_dir2_data_off_t v
;
116 #define FREETAB_SIZE(n) \
117 (offsetof(freetab_t, ents) + (sizeof(struct freetab_ent) * (n)))
119 #define DIR_HASH_CK_OK 0
120 #define DIR_HASH_CK_DUPLEAF 1
121 #define DIR_HASH_CK_BADHASH 2
122 #define DIR_HASH_CK_NODATA 3
123 #define DIR_HASH_CK_NOLEAF 4
124 #define DIR_HASH_CK_BADSTALE 5
125 #define DIR_HASH_CK_TOTAL 6
128 * Returns 0 if the name already exists (ie. a duplicate)
133 dir_hash_tab_t
*hashtab
,
140 xfs_dahash_t hash
= 0;
146 struct xfs_name xname
;
148 ASSERT(!hashtab
->names_duped
);
154 junk
= name
[0] == '/';
155 byaddr
= DIR_HASH_FUNC(hashtab
, addr
);
159 hash
= mp
->m_dirnameops
->hashname(&xname
);
160 byhash
= DIR_HASH_FUNC(hashtab
, hash
);
163 * search hash bucket for existing name.
165 for (p
= hashtab
->byhash
[byhash
]; p
; p
= p
->nextbyhash
) {
166 if (p
->hashval
== hash
&& p
->name
.len
== namelen
) {
167 if (memcmp(p
->name
.name
, name
, namelen
) == 0) {
176 if ((p
= malloc(sizeof(*p
))) == NULL
)
177 do_error(_("malloc failed in dir_hash_add (%zu bytes)\n"),
180 p
->nextbyaddr
= hashtab
->byaddr
[byaddr
];
181 hashtab
->byaddr
[byaddr
] = p
;
183 hashtab
->last
->nextbyorder
= p
;
186 p
->nextbyorder
= NULL
;
189 if (!(p
->junkit
= junk
)) {
191 p
->nextbyhash
= hashtab
->byhash
[byhash
];
192 hashtab
->byhash
[byhash
] = p
;
203 * checks to see if any data entries are not in the leaf blocks
207 dir_hash_tab_t
*hashtab
)
212 for (i
= 0; i
< hashtab
->size
; i
++) {
213 for (p
= hashtab
->byaddr
[i
]; p
; p
= p
->nextbyaddr
) {
223 dir_hash_tab_t
*hashtab
,
227 static char *seevalstr
[DIR_HASH_CK_TOTAL
];
231 seevalstr
[DIR_HASH_CK_OK
] = _("ok");
232 seevalstr
[DIR_HASH_CK_DUPLEAF
] = _("duplicate leaf");
233 seevalstr
[DIR_HASH_CK_BADHASH
] = _("hash value mismatch");
234 seevalstr
[DIR_HASH_CK_NODATA
] = _("no data entry");
235 seevalstr
[DIR_HASH_CK_NOLEAF
] = _("no leaf entry");
236 seevalstr
[DIR_HASH_CK_BADSTALE
] = _("bad stale count");
240 if (seeval
== DIR_HASH_CK_OK
&& dir_hash_unseen(hashtab
))
241 seeval
= DIR_HASH_CK_NOLEAF
;
242 if (seeval
== DIR_HASH_CK_OK
)
244 do_warn(_("bad hash table for directory inode %" PRIu64
" (%s): "),
245 ip
->i_ino
, seevalstr
[seeval
]);
247 do_warn(_("rebuilding\n"));
249 do_warn(_("would rebuild\n"));
255 dir_hash_tab_t
*hashtab
)
261 for (i
= 0; i
< hashtab
->size
; i
++) {
262 for (p
= hashtab
->byaddr
[i
]; p
; p
= n
) {
264 if (hashtab
->names_duped
)
265 free((void *)p
->name
.name
);
272 static dir_hash_tab_t
*
276 dir_hash_tab_t
*hashtab
;
279 hsize
= size
/ (16 * 4);
284 if ((hashtab
= calloc(DIR_HASH_TAB_SIZE(hsize
), 1)) == NULL
)
285 do_error(_("calloc failed in dir_hash_init\n"));
286 hashtab
->size
= hsize
;
287 hashtab
->byhash
= (dir_hash_ent_t
**)((char *)hashtab
+
288 sizeof(dir_hash_tab_t
));
289 hashtab
->byaddr
= (dir_hash_ent_t
**)((char *)hashtab
+
290 sizeof(dir_hash_tab_t
) + sizeof(dir_hash_ent_t
*) * hsize
);
296 dir_hash_tab_t
*hashtab
,
298 xfs_dir2_dataptr_t addr
)
303 i
= DIR_HASH_FUNC(hashtab
, addr
);
304 for (p
= hashtab
->byaddr
[i
]; p
; p
= p
->nextbyaddr
) {
305 if (p
->address
!= addr
)
308 return DIR_HASH_CK_DUPLEAF
;
309 if (p
->junkit
== 0 && p
->hashval
!= hash
)
310 return DIR_HASH_CK_BADHASH
;
312 return DIR_HASH_CK_OK
;
314 return DIR_HASH_CK_NODATA
;
318 dir_hash_update_ftype(
319 dir_hash_tab_t
*hashtab
,
320 xfs_dir2_dataptr_t addr
,
326 i
= DIR_HASH_FUNC(hashtab
, addr
);
327 for (p
= hashtab
->byaddr
[i
]; p
; p
= p
->nextbyaddr
) {
328 if (p
->address
!= addr
)
330 p
->name
.type
= ftype
;
335 * checks to make sure leafs match a data entry, and that the stale
340 dir_hash_tab_t
*hashtab
,
341 xfs_dir2_leaf_entry_t
*ents
,
349 for (i
= j
= 0; i
< count
; i
++) {
350 if (be32_to_cpu(ents
[i
].address
) == XFS_DIR2_NULL_DATAPTR
) {
354 rval
= dir_hash_see(hashtab
, be32_to_cpu(ents
[i
].hashval
),
355 be32_to_cpu(ents
[i
].address
));
356 if (rval
!= DIR_HASH_CK_OK
)
359 return j
== stale
? DIR_HASH_CK_OK
: DIR_HASH_CK_BADSTALE
;
363 * Convert name pointers into locally allocated memory.
364 * This must only be done after all the entries have been added.
367 dir_hash_dup_names(dir_hash_tab_t
*hashtab
)
372 if (hashtab
->names_duped
)
375 for (p
= hashtab
->first
; p
; p
= p
->nextbyorder
) {
376 name
= malloc(p
->name
.len
);
377 memcpy(name
, p
->name
.name
, p
->name
.len
);
380 hashtab
->names_duped
= 1;
384 * Given a block number in a fork, return the next valid block number
386 * If this is the last block number then NULLFILEOFF is returned.
388 * This was originally in the kernel, but only used in xfs_repair.
392 xfs_trans_t
*tp
, /* transaction pointer */
393 xfs_inode_t
*ip
, /* incore inode */
394 xfs_fileoff_t
*bnop
, /* current block */
395 int whichfork
) /* data or attr fork */
397 xfs_fileoff_t bno
; /* current block */
398 int eof
; /* hit end of file */
399 int error
; /* error return value */
400 xfs_bmbt_irec_t got
; /* current extent value */
401 xfs_ifork_t
*ifp
; /* inode fork pointer */
402 xfs_extnum_t lastx
; /* last extent used */
403 xfs_bmbt_irec_t prev
; /* previous extent value */
405 if (XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_BTREE
&&
406 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_EXTENTS
&&
407 XFS_IFORK_FORMAT(ip
, whichfork
) != XFS_DINODE_FMT_LOCAL
)
409 if (XFS_IFORK_FORMAT(ip
, whichfork
) == XFS_DINODE_FMT_LOCAL
) {
413 ifp
= XFS_IFORK_PTR(ip
, whichfork
);
414 if (!(ifp
->if_flags
& XFS_IFEXTENTS
) &&
415 (error
= xfs_iread_extents(tp
, ip
, whichfork
)))
418 xfs_bmap_search_extents(ip
, bno
, whichfork
, &eof
, &lastx
, &got
, &prev
);
422 *bnop
= got
.br_startoff
< bno
? bno
: got
.br_startoff
;
432 do_error(_("ran out of disk space!\n"));
434 do_error(_("xfs_trans_reserve returned %d\n"), err
);
438 mk_rbmino(xfs_mount_t
*mp
)
448 xfs_bmap_free_t flist
;
450 xfs_bmbt_irec_t map
[XFS_BMAP_MAX_NMAP
];
453 struct xfs_trans_res tres
= {0};
458 tp
= libxfs_trans_alloc(mp
, 0);
460 i
= libxfs_trans_reserve(tp
, &tres
, 10, 0);
464 error
= libxfs_trans_iget(mp
, tp
, mp
->m_sb
.sb_rbmino
, 0, 0, &ip
);
467 _("couldn't iget realtime bitmap inode -- error - %d\n"),
471 vers
= xfs_sb_version_hascrc(&mp
->m_sb
) ? 3 : 1;
472 memset(&ip
->i_d
, 0, xfs_icdinode_size(vers
));
474 ip
->i_d
.di_magic
= XFS_DINODE_MAGIC
;
475 ip
->i_d
.di_mode
= S_IFREG
;
476 ip
->i_d
.di_version
= vers
;
477 ip
->i_d
.di_format
= XFS_DINODE_FMT_EXTENTS
;
478 ip
->i_d
.di_aformat
= XFS_DINODE_FMT_EXTENTS
;
480 ip
->i_d
.di_nlink
= 1; /* account for sb ptr */
482 times
= XFS_ICHGTIME_CHG
| XFS_ICHGTIME_MOD
;
483 if (ip
->i_d
.di_version
== 3) {
485 ip
->i_d
.di_changecount
= 1;
487 ip
->i_d
.di_flags2
= 0;
488 ip
->i_d
.di_ino
= mp
->m_sb
.sb_rbmino
;
489 memset(&(ip
->i_d
.di_pad2
[0]), 0, sizeof(ip
->i_d
.di_pad2
));
490 platform_uuid_copy(&ip
->i_d
.di_uuid
, &mp
->m_sb
.sb_uuid
);
491 times
|= XFS_ICHGTIME_CREATE
;
493 libxfs_trans_ichgtime(tp
, ip
, times
);
498 ip
->i_df
.if_flags
= XFS_IFEXTENTS
;
499 ip
->i_df
.if_bytes
= ip
->i_df
.if_real_bytes
= 0;
500 ip
->i_df
.if_u1
.if_extents
= NULL
;
502 ip
->i_d
.di_size
= mp
->m_sb
.sb_rbmblocks
* mp
->m_sb
.sb_blocksize
;
507 libxfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
508 libxfs_trans_ihold(tp
, ip
);
509 libxfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
|XFS_TRANS_SYNC
);
512 * then allocate blocks for file and fill with zeroes (stolen
515 tp
= libxfs_trans_alloc(mp
, 0);
516 error
= libxfs_trans_reserve(tp
, &tres
, mp
->m_sb
.sb_rbmblocks
+
517 (XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
) - 1), 0);
521 libxfs_trans_ijoin(tp
, ip
, 0);
523 xfs_bmap_init(&flist
, &first
);
524 while (bno
< mp
->m_sb
.sb_rbmblocks
) {
525 nmap
= XFS_BMAP_MAX_NMAP
;
526 error
= libxfs_bmapi_write(tp
, ip
, bno
,
527 (xfs_extlen_t
)(mp
->m_sb
.sb_rbmblocks
- bno
),
528 0, &first
, mp
->m_sb
.sb_rbmblocks
,
532 _("couldn't allocate realtime bitmap, error = %d\n"),
535 for (i
= 0, ep
= map
; i
< nmap
; i
++, ep
++) {
536 libxfs_device_zero(mp
->m_ddev_targp
,
537 XFS_FSB_TO_DADDR(mp
, ep
->br_startblock
),
538 XFS_FSB_TO_BB(mp
, ep
->br_blockcount
));
539 bno
+= ep
->br_blockcount
;
542 error
= libxfs_bmap_finish(&tp
, &flist
, &committed
);
545 _("allocation of the realtime bitmap failed, error = %d\n"),
548 libxfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
|XFS_TRANS_SYNC
);
552 fill_rbmino(xfs_mount_t
*mp
)
563 struct xfs_trans_res tres
= {0};
568 tp
= libxfs_trans_alloc(mp
, 0);
570 error
= libxfs_trans_reserve(tp
, &tres
, 10, 0);
574 error
= libxfs_trans_iget(mp
, tp
, mp
->m_sb
.sb_rbmino
, 0, 0, &ip
);
577 _("couldn't iget realtime bitmap inode -- error - %d\n"),
582 while (bno
< mp
->m_sb
.sb_rbmblocks
) {
584 * fill the file one block at a time
587 error
= libxfs_bmapi_write(tp
, ip
, bno
, 1, 0,
588 &first
, 1, &map
, &nmap
, NULL
);
589 if (error
|| nmap
!= 1) {
591 _("couldn't map realtime bitmap block %" PRIu64
", error = %d\n"),
595 ASSERT(map
.br_startblock
!= HOLESTARTBLOCK
);
597 error
= libxfs_trans_read_buf(
599 XFS_FSB_TO_DADDR(mp
, map
.br_startblock
),
600 XFS_FSB_TO_BB(mp
, 1), 1, &bp
, NULL
);
604 _("can't access block %" PRIu64
" (fsbno %" PRIu64
") of realtime bitmap inode %" PRIu64
"\n"),
605 bno
, map
.br_startblock
, mp
->m_sb
.sb_rbmino
);
609 memmove(XFS_BUF_PTR(bp
), bmp
, mp
->m_sb
.sb_blocksize
);
611 libxfs_trans_log_buf(tp
, bp
, 0, mp
->m_sb
.sb_blocksize
- 1);
613 bmp
= (xfs_rtword_t
*)((__psint_t
) bmp
+ mp
->m_sb
.sb_blocksize
);
617 libxfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
|XFS_TRANS_SYNC
);
622 fill_rsumino(xfs_mount_t
*mp
)
632 xfs_dfiloff_t end_bno
;
634 struct xfs_trans_res tres
= {0};
638 end_bno
= mp
->m_rsumsize
>> mp
->m_sb
.sb_blocklog
;
640 tp
= libxfs_trans_alloc(mp
, 0);
642 error
= libxfs_trans_reserve(tp
, &tres
, 10, 0);
646 error
= libxfs_trans_iget(mp
, tp
, mp
->m_sb
.sb_rsumino
, 0, 0, &ip
);
649 _("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,
660 &first
, 1, &map
, &nmap
, NULL
);
661 if (error
|| nmap
!= 1) {
663 _("couldn't map realtime summary inode block %" PRIu64
", error = %d\n"),
667 ASSERT(map
.br_startblock
!= HOLESTARTBLOCK
);
669 error
= libxfs_trans_read_buf(
671 XFS_FSB_TO_DADDR(mp
, map
.br_startblock
),
672 XFS_FSB_TO_BB(mp
, 1), 1, &bp
, NULL
);
676 _("can't access block %" PRIu64
" (fsbno %" PRIu64
") of realtime summary inode %" PRIu64
"\n"),
677 bno
, map
.br_startblock
, mp
->m_sb
.sb_rsumino
);
681 memmove(XFS_BUF_PTR(bp
), 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
*)((__psint_t
)smp
+ mp
->m_sb
.sb_blocksize
);
689 libxfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
|XFS_TRANS_SYNC
);
694 mk_rsumino(xfs_mount_t
*mp
)
705 xfs_bmap_free_t flist
;
707 xfs_bmbt_irec_t map
[XFS_BMAP_MAX_NMAP
];
710 struct xfs_trans_res tres
= {0};
715 tp
= libxfs_trans_alloc(mp
, 0);
717 i
= libxfs_trans_reserve(tp
, &M_RES(mp
)->tr_ichange
, 10, 0);
721 error
= libxfs_trans_iget(mp
, tp
, mp
->m_sb
.sb_rsumino
, 0, 0, &ip
);
724 _("couldn't iget realtime summary inode -- error - %d\n"),
728 vers
= xfs_sb_version_hascrc(&mp
->m_sb
) ? 3 : 1;
729 memset(&ip
->i_d
, 0, xfs_icdinode_size(vers
));
731 ip
->i_d
.di_magic
= XFS_DINODE_MAGIC
;
732 ip
->i_d
.di_mode
= S_IFREG
;
733 ip
->i_d
.di_version
= vers
;
734 ip
->i_d
.di_format
= XFS_DINODE_FMT_EXTENTS
;
735 ip
->i_d
.di_aformat
= XFS_DINODE_FMT_EXTENTS
;
737 ip
->i_d
.di_nlink
= 1; /* account for sb ptr */
739 times
= XFS_ICHGTIME_CHG
| XFS_ICHGTIME_MOD
;
740 if (ip
->i_d
.di_version
== 3) {
742 ip
->i_d
.di_changecount
= 1;
744 ip
->i_d
.di_flags2
= 0;
745 ip
->i_d
.di_ino
= mp
->m_sb
.sb_rsumino
;
746 memset(&(ip
->i_d
.di_pad2
[0]), 0, sizeof(ip
->i_d
.di_pad2
));
747 platform_uuid_copy(&ip
->i_d
.di_uuid
, &mp
->m_sb
.sb_uuid
);
748 times
|= XFS_ICHGTIME_CREATE
;
750 libxfs_trans_ichgtime(tp
, ip
, times
);
755 ip
->i_df
.if_flags
= XFS_IFEXTENTS
;
756 ip
->i_df
.if_bytes
= ip
->i_df
.if_real_bytes
= 0;
757 ip
->i_df
.if_u1
.if_extents
= NULL
;
759 ip
->i_d
.di_size
= mp
->m_rsumsize
;
764 libxfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
765 libxfs_trans_ihold(tp
, ip
);
766 libxfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
|XFS_TRANS_SYNC
);
769 * then allocate blocks for file and fill with zeroes (stolen
772 tp
= libxfs_trans_alloc(mp
, 0);
773 xfs_bmap_init(&flist
, &first
);
775 nsumblocks
= mp
->m_rsumsize
>> mp
->m_sb
.sb_blocklog
;
776 tres
.tr_logres
= BBTOB(128);
777 tres
.tr_logcount
= XFS_DEFAULT_PERM_LOG_COUNT
;
778 tres
.tr_logflags
= XFS_TRANS_PERM_LOG_RES
;
779 error
= libxfs_trans_reserve(tp
, &tres
, mp
->m_sb
.sb_rbmblocks
+
780 (XFS_BM_MAXLEVELS(mp
, XFS_DATA_FORK
) - 1), 0);
784 libxfs_trans_ijoin(tp
, ip
, 0);
786 xfs_bmap_init(&flist
, &first
);
787 while (bno
< nsumblocks
) {
788 nmap
= XFS_BMAP_MAX_NMAP
;
789 error
= libxfs_bmapi_write(tp
, ip
, bno
,
790 (xfs_extlen_t
)(nsumblocks
- bno
),
791 0, &first
, nsumblocks
, map
, &nmap
, &flist
);
794 _("couldn't allocate realtime summary inode, error = %d\n"),
797 for (i
= 0, ep
= map
; i
< nmap
; i
++, ep
++) {
798 libxfs_device_zero(mp
->m_ddev_targp
,
799 XFS_FSB_TO_DADDR(mp
, ep
->br_startblock
),
800 XFS_FSB_TO_BB(mp
, ep
->br_blockcount
));
801 bno
+= ep
->br_blockcount
;
804 error
= libxfs_bmap_finish(&tp
, &flist
, &committed
);
807 _("allocation of the realtime summary ino failed, error = %d\n"),
810 libxfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
|XFS_TRANS_SYNC
);
814 * makes a new root directory.
817 mk_root_dir(xfs_mount_t
*mp
)
823 const mode_t mode
= 0755;
824 ino_tree_node_t
*irec
;
828 ASSERT(xfs_sb_version_hasdirv2(&mp
->m_sb
));
830 tp
= libxfs_trans_alloc(mp
, 0);
833 i
= libxfs_trans_reserve(tp
, &M_RES(mp
)->tr_ichange
, 10, 0);
837 error
= libxfs_trans_iget(mp
, tp
, mp
->m_sb
.sb_rootino
, 0, 0, &ip
);
839 do_error(_("could not iget root inode -- error - %d\n"), error
);
843 * take care of the core -- initialization from xfs_ialloc()
845 vers
= xfs_sb_version_hascrc(&mp
->m_sb
) ? 3 : 1;
846 memset(&ip
->i_d
, 0, xfs_icdinode_size(vers
));
848 ip
->i_d
.di_magic
= XFS_DINODE_MAGIC
;
849 ip
->i_d
.di_mode
= (__uint16_t
) mode
|S_IFDIR
;
850 ip
->i_d
.di_version
= vers
;
851 ip
->i_d
.di_format
= XFS_DINODE_FMT_EXTENTS
;
852 ip
->i_d
.di_aformat
= XFS_DINODE_FMT_EXTENTS
;
854 ip
->i_d
.di_nlink
= 1; /* account for . */
856 times
= XFS_ICHGTIME_CHG
| XFS_ICHGTIME_MOD
;
857 if (ip
->i_d
.di_version
== 3) {
859 ip
->i_d
.di_changecount
= 1;
861 ip
->i_d
.di_flags2
= 0;
862 ip
->i_d
.di_ino
= mp
->m_sb
.sb_rootino
;
863 memset(&(ip
->i_d
.di_pad2
[0]), 0, sizeof(ip
->i_d
.di_pad2
));
864 platform_uuid_copy(&ip
->i_d
.di_uuid
, &mp
->m_sb
.sb_uuid
);
865 times
|= XFS_ICHGTIME_CREATE
;
867 libxfs_trans_ichgtime(tp
, ip
, times
);
869 libxfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
874 ip
->i_df
.if_flags
= XFS_IFEXTENTS
;
875 ip
->i_df
.if_bytes
= ip
->i_df
.if_real_bytes
= 0;
876 ip
->i_df
.if_u1
.if_extents
= NULL
;
879 * initialize the directory
881 libxfs_dir_init(tp
, ip
, ip
);
883 libxfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
|XFS_TRANS_SYNC
);
885 irec
= find_inode_rec(mp
, XFS_INO_TO_AGNO(mp
, mp
->m_sb
.sb_rootino
),
886 XFS_INO_TO_AGINO(mp
, mp
->m_sb
.sb_rootino
));
887 set_inode_isadir(irec
, XFS_INO_TO_AGINO(mp
, mp
->m_sb
.sb_rootino
) -
892 * orphanage name == lost+found
895 mk_orphanage(xfs_mount_t
*mp
)
902 ino_tree_node_t
*irec
;
907 xfs_bmap_free_t flist
;
908 const int mode
= 0755;
910 struct xfs_name xname
;
912 ASSERT(xfs_sb_version_hasdirv2(&mp
->m_sb
));
915 * check for an existing lost+found first, if it exists, return
916 * its inode. Otherwise, we can create it. Bad lost+found inodes
917 * would have been cleared in phase3 and phase4.
920 if ((i
= libxfs_iget(mp
, NULL
, mp
->m_sb
.sb_rootino
, 0, &pip
, 0)))
921 do_error(_("%d - couldn't iget root inode to obtain %s\n"),
924 xname
.name
= (unsigned char *)ORPHANAGE
;
925 xname
.len
= strlen(ORPHANAGE
);
926 xname
.type
= XFS_DIR3_FT_DIR
;
928 if (libxfs_dir_lookup(NULL
, pip
, &xname
, &ino
, NULL
) == 0)
932 * could not be found, create it
935 tp
= libxfs_trans_alloc(mp
, 0);
936 xfs_bmap_init(&flist
, &first
);
938 nres
= XFS_MKDIR_SPACE_RES(mp
, xname
.len
);
939 i
= libxfs_trans_reserve(tp
, &M_RES(mp
)->tr_mkdir
, nres
, 0);
944 * use iget/ijoin instead of trans_iget because the ialloc
945 * wrapper can commit the transaction and start a new one
947 /* if ((i = libxfs_iget(mp, NULL, mp->m_sb.sb_rootino, 0, &pip, 0)))
948 do_error(_("%d - couldn't iget root inode to make %s\n"),
951 error
= libxfs_inode_alloc(&tp
, pip
, mode
|S_IFDIR
,
952 1, 0, &zerocr
, &zerofsx
, &ip
);
954 do_error(_("%s inode allocation failed %d\n"),
957 ip
->i_d
.di_nlink
++; /* account for . */
960 irec
= find_inode_rec(mp
,
961 XFS_INO_TO_AGNO(mp
, ino
),
962 XFS_INO_TO_AGINO(mp
, ino
));
966 * This inode is allocated from a newly created inode
967 * chunk and therefore did not exist when inode chunks
968 * were processed in phase3. Add this group of inodes to
969 * the entry avl tree as if they were discovered in phase3.
971 irec
= set_inode_free_alloc(mp
, XFS_INO_TO_AGNO(mp
, ino
),
972 XFS_INO_TO_AGINO(mp
, ino
));
975 for (i
= 0; i
< XFS_INODES_PER_CHUNK
; i
++)
976 set_inode_free(irec
, i
);
979 ino_offset
= get_inode_offset(mp
, ino
, irec
);
982 * Mark the inode allocated to lost+found as used in the AVL tree
983 * so it is not skipped in phase 7
985 set_inode_used(irec
, ino_offset
);
986 add_inode_ref(irec
, ino_offset
);
989 * now that we know the transaction will stay around,
990 * add the root inode to it
992 libxfs_trans_ijoin(tp
, pip
, 0);
995 * create the actual entry
997 error
= libxfs_dir_createname(tp
, pip
, &xname
, ip
->i_ino
, &first
,
1001 _("can't make %s, createname error %d\n"),
1005 * bump up the link count in the root directory to account
1006 * for .. in the new directory
1008 pip
->i_d
.di_nlink
++;
1009 add_inode_ref(find_inode_rec(mp
,
1010 XFS_INO_TO_AGNO(mp
, mp
->m_sb
.sb_rootino
),
1011 XFS_INO_TO_AGINO(mp
, mp
->m_sb
.sb_rootino
)), 0);
1015 libxfs_trans_log_inode(tp
, pip
, XFS_ILOG_CORE
);
1016 libxfs_dir_init(tp
, ip
, pip
);
1017 libxfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
1019 error
= libxfs_bmap_finish(&tp
, &flist
, &committed
);
1021 do_error(_("%s directory creation failed -- bmapf error %d\n"),
1026 libxfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
|XFS_TRANS_SYNC
);
1027 add_inode_reached(irec
,ino_offset
);
1033 * move a file to the orphange.
1038 xfs_ino_t ino
, /* inode # to be moved */
1039 int isa_dir
) /* 1 if inode is a directory */
1041 xfs_inode_t
*orphanage_ip
;
1042 xfs_ino_t entry_ino_num
;
1045 xfs_fsblock_t first
;
1046 xfs_bmap_free_t flist
;
1049 unsigned char fname
[MAXPATHLEN
+ 1];
1052 ino_tree_node_t
*irec
;
1054 struct xfs_name xname
;
1056 ASSERT(xfs_sb_version_hasdirv2(&mp
->m_sb
));
1059 xname
.len
= snprintf((char *)fname
, sizeof(fname
), "%llu",
1060 (unsigned long long)ino
);
1061 /* XXX use xfs_mode_to_ftype[] when userspace gains it */
1062 xname
.type
= XFS_DIR3_FT_UNKNOWN
;
1064 err
= libxfs_iget(mp
, NULL
, orphanage_ino
, 0, &orphanage_ip
, 0);
1066 do_error(_("%d - couldn't iget orphanage inode\n"), err
);
1068 * Make sure the filename is unique in the lost+found
1071 while (libxfs_dir_lookup(NULL
, orphanage_ip
, &xname
, &entry_ino_num
,
1073 xname
.len
= snprintf((char *)fname
, sizeof(fname
), "%llu.%d",
1074 (unsigned long long)ino
, ++incr
);
1076 tp
= libxfs_trans_alloc(mp
, 0);
1078 if ((err
= libxfs_iget(mp
, NULL
, ino
, 0, &ino_p
, 0)))
1079 do_error(_("%d - couldn't iget disconnected inode\n"), err
);
1082 irec
= find_inode_rec(mp
, XFS_INO_TO_AGNO(mp
, orphanage_ino
),
1083 XFS_INO_TO_AGINO(mp
, orphanage_ino
));
1085 ino_offset
= XFS_INO_TO_AGINO(mp
, orphanage_ino
) -
1087 nres
= XFS_DIRENTER_SPACE_RES(mp
, fnamelen
) +
1088 XFS_DIRENTER_SPACE_RES(mp
, 2);
1089 err
= libxfs_dir_lookup(tp
, ino_p
, &xfs_name_dotdot
,
1090 &entry_ino_num
, NULL
);
1092 ASSERT(err
== ENOENT
);
1094 err
= libxfs_trans_reserve(tp
, &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);
1104 xfs_bmap_init(&flist
, &first
);
1105 err
= libxfs_dir_createname(tp
, orphanage_ip
, &xname
,
1106 ino
, &first
, &flist
, nres
);
1109 _("name create failed in %s (%d), filesystem may be out of space\n"),
1113 add_inode_ref(irec
, ino_offset
);
1115 orphanage_ip
->i_d
.di_nlink
++;
1116 libxfs_trans_log_inode(tp
, orphanage_ip
, XFS_ILOG_CORE
);
1118 err
= libxfs_dir_createname(tp
, ino_p
, &xfs_name_dotdot
,
1119 orphanage_ino
, &first
, &flist
, nres
);
1122 _("creation of .. entry failed (%d), filesystem may be out of space\n"),
1125 ino_p
->i_d
.di_nlink
++;
1126 libxfs_trans_log_inode(tp
, ino_p
, XFS_ILOG_CORE
);
1128 err
= libxfs_bmap_finish(&tp
, &flist
, &committed
);
1131 _("bmap finish failed (err - %d), filesystem may be out of space\n"),
1134 libxfs_trans_commit(tp
,
1135 XFS_TRANS_RELEASE_LOG_RES
|XFS_TRANS_SYNC
);
1137 err
= libxfs_trans_reserve(tp
, &M_RES(mp
)->tr_rename
,
1141 _("space reservation failed (%d), filesystem may be out of space\n"),
1144 libxfs_trans_ijoin(tp
, orphanage_ip
, 0);
1145 libxfs_trans_ijoin(tp
, ino_p
, 0);
1147 xfs_bmap_init(&flist
, &first
);
1149 err
= libxfs_dir_createname(tp
, orphanage_ip
, &xname
,
1150 ino
, &first
, &flist
, nres
);
1153 _("name create failed in %s (%d), filesystem may be out of space\n"),
1157 add_inode_ref(irec
, ino_offset
);
1159 orphanage_ip
->i_d
.di_nlink
++;
1160 libxfs_trans_log_inode(tp
, orphanage_ip
, XFS_ILOG_CORE
);
1163 * don't replace .. value if it already points
1164 * to us. that'll pop a libxfs/kernel ASSERT.
1166 if (entry_ino_num
!= orphanage_ino
) {
1167 err
= libxfs_dir_replace(tp
, ino_p
,
1168 &xfs_name_dotdot
, orphanage_ino
,
1169 &first
, &flist
, nres
);
1172 _("name replace op failed (%d), filesystem may be out of space\n"),
1176 err
= libxfs_bmap_finish(&tp
, &flist
, &committed
);
1179 _("bmap finish failed (%d), filesystem may be out of space\n"),
1182 libxfs_trans_commit(tp
,
1183 XFS_TRANS_RELEASE_LOG_RES
|XFS_TRANS_SYNC
);
1188 * use the remove log reservation as that's
1189 * more accurate. we're only creating the
1190 * links, we're not doing the inode allocation
1191 * also accounted for in the create
1193 nres
= XFS_DIRENTER_SPACE_RES(mp
, xname
.len
);
1194 err
= libxfs_trans_reserve(tp
, &M_RES(mp
)->tr_remove
,
1198 _("space reservation failed (%d), filesystem may be out of space\n"),
1201 libxfs_trans_ijoin(tp
, orphanage_ip
, 0);
1202 libxfs_trans_ijoin(tp
, ino_p
, 0);
1204 xfs_bmap_init(&flist
, &first
);
1205 err
= libxfs_dir_createname(tp
, orphanage_ip
, &xname
, ino
,
1206 &first
, &flist
, nres
);
1209 _("name create failed in %s (%d), filesystem may be out of space\n"),
1213 ino_p
->i_d
.di_nlink
= 1;
1214 libxfs_trans_log_inode(tp
, ino_p
, XFS_ILOG_CORE
);
1216 err
= libxfs_bmap_finish(&tp
, &flist
, &committed
);
1219 _("bmap finish failed (%d), filesystem may be out of space\n"),
1222 libxfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
|XFS_TRANS_SYNC
);
1233 do_warn(msg
, iname
, ino1
, ino2
);
1236 do_warn(_(", marking entry to be junked\n"));
1240 do_warn(_(", would junk entry\n"));
1245 * Unexpected failure during the rebuild will leave the entries in
1246 * lost+found on the next run
1250 longform_dir2_rebuild(
1254 ino_tree_node_t
*irec
,
1256 dir_hash_tab_t
*hashtab
)
1261 xfs_fileoff_t lastblock
;
1262 xfs_fsblock_t firstblock
;
1263 xfs_bmap_free_t flist
;
1270 * trash directory completely and rebuild from scratch using the
1271 * name/inode pairs in the hash table
1274 do_warn(_("rebuilding directory inode %" PRIu64
"\n"), ino
);
1277 * first attempt to locate the parent inode, if it can't be
1278 * found, set it to the root inode and it'll be moved to the
1279 * orphanage later (the inode number here needs to be valid
1280 * for the libxfs_dir_init() call).
1282 pip
.i_ino
= get_inode_parent(irec
, ino_offset
);
1283 if (pip
.i_ino
== NULLFSINO
)
1284 pip
.i_ino
= mp
->m_sb
.sb_rootino
;
1286 xfs_bmap_init(&flist
, &firstblock
);
1288 tp
= libxfs_trans_alloc(mp
, 0);
1289 nres
= XFS_REMOVE_SPACE_RES(mp
);
1290 error
= libxfs_trans_reserve(tp
, &M_RES(mp
)->tr_remove
, nres
, 0);
1293 libxfs_trans_ijoin(tp
, ip
, 0);
1294 libxfs_trans_ihold(tp
, ip
);
1296 if ((error
= libxfs_bmap_last_offset(tp
, ip
, &lastblock
,
1298 do_error(_("xfs_bmap_last_offset failed -- error - %d\n"),
1301 /* free all data, leaf, node and freespace blocks */
1302 error
= libxfs_bunmapi(tp
, ip
, 0, lastblock
, XFS_BMAPI_METADATA
, 0,
1303 &firstblock
, &flist
, &done
);
1305 do_warn(_("xfs_bunmapi failed -- error - %d\n"), error
);
1306 goto out_bmap_cancel
;
1311 libxfs_dir_init(tp
, ip
, &pip
);
1313 error
= libxfs_bmap_finish(&tp
, &flist
, &committed
);
1315 libxfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
|XFS_TRANS_SYNC
);
1317 /* go through the hash list and re-add the inodes */
1319 for (p
= hashtab
->first
; p
; p
= p
->nextbyorder
) {
1321 if (p
->name
.name
[0] == '/' || (p
->name
.name
[0] == '.' &&
1322 (p
->name
.len
== 1 || (p
->name
.len
== 2 &&
1323 p
->name
.name
[1] == '.'))))
1326 tp
= libxfs_trans_alloc(mp
, 0);
1327 nres
= XFS_CREATE_SPACE_RES(mp
, p
->name
.len
);
1328 error
= libxfs_trans_reserve(tp
, &M_RES(mp
)->tr_create
,
1333 libxfs_trans_ijoin(tp
, ip
, 0);
1334 libxfs_trans_ihold(tp
, ip
);
1336 xfs_bmap_init(&flist
, &firstblock
);
1337 error
= libxfs_dir_createname(tp
, ip
, &p
->name
, p
->inum
,
1338 &firstblock
, &flist
, nres
);
1341 _("name create failed in ino %" PRIu64
" (%d), filesystem may be out of space\n"),
1343 goto out_bmap_cancel
;
1346 error
= libxfs_bmap_finish(&tp
, &flist
, &committed
);
1349 _("bmap finish failed (%d), filesystem may be out of space\n"),
1351 goto out_bmap_cancel
;
1354 libxfs_trans_commit(tp
,
1355 XFS_TRANS_RELEASE_LOG_RES
|XFS_TRANS_SYNC
);
1361 libxfs_bmap_cancel(&flist
);
1362 libxfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
| XFS_TRANS_ABORT
);
1368 * Kill a block in a version 2 inode.
1369 * Makes its own transaction.
1381 xfs_fsblock_t firstblock
;
1382 xfs_bmap_free_t flist
;
1386 tp
= libxfs_trans_alloc(mp
, 0);
1387 nres
= XFS_REMOVE_SPACE_RES(mp
);
1388 error
= libxfs_trans_reserve(tp
, &M_RES(mp
)->tr_remove
, nres
, 0);
1391 libxfs_trans_ijoin(tp
, ip
, 0);
1392 libxfs_trans_ihold(tp
, ip
);
1393 libxfs_trans_bjoin(tp
, bp
);
1394 memset(&args
, 0, sizeof(args
));
1395 xfs_bmap_init(&flist
, &firstblock
);
1398 args
.firstblock
= &firstblock
;
1399 args
.flist
= &flist
;
1400 args
.whichfork
= XFS_DATA_FORK
;
1401 if (da_bno
>= mp
->m_dirleafblk
&& da_bno
< mp
->m_dirfreeblk
)
1402 error
= libxfs_da_shrink_inode(&args
, da_bno
, bp
);
1404 error
= libxfs_dir2_shrink_inode(&args
,
1405 xfs_dir2_da_to_db(mp
, da_bno
), bp
);
1407 do_error(_("shrink_inode failed inode %" PRIu64
" block %u\n"),
1409 libxfs_bmap_finish(&tp
, &flist
, &committed
);
1410 libxfs_trans_commit(tp
, 0);
1414 * process a data block, also checks for .. entry
1415 * and corrects it to match what we think .. should be
1418 longform_dir2_entry_check_data(
1423 ino_tree_node_t
*current_irec
,
1424 int current_ino_offset
,
1425 struct xfs_buf
**bpp
,
1426 dir_hash_tab_t
*hashtab
,
1427 freetab_t
**freetabp
,
1431 xfs_dir2_dataptr_t addr
;
1432 xfs_dir2_leaf_entry_t
*blp
;
1434 xfs_dir2_block_tail_t
*btp
;
1436 struct xfs_dir2_data_hdr
*d
;
1438 xfs_dir2_data_entry_t
*dep
;
1439 xfs_dir2_data_unused_t
*dup
;
1440 struct xfs_dir2_data_free
*bf
;
1443 xfs_fsblock_t firstblock
;
1444 xfs_bmap_free_t flist
;
1445 char fname
[MAXNAMELEN
+ 1];
1450 ino_tree_node_t
*irec
;
1464 ptr
= (char *)xfs_dir3_data_entry_p(d
);
1466 needscan
= needlog
= 0;
1468 freetab
= *freetabp
;
1470 btp
= xfs_dir2_block_tail_p(mp
, (struct xfs_dir2_data_hdr
*)d
);
1471 blp
= xfs_dir2_block_leaf_p(btp
);
1472 endptr
= (char *)blp
;
1473 if (endptr
> (char *)btp
)
1474 endptr
= (char *)btp
;
1475 if (xfs_sb_version_hascrc(&mp
->m_sb
))
1476 wantmagic
= XFS_DIR3_BLOCK_MAGIC
;
1478 wantmagic
= XFS_DIR2_BLOCK_MAGIC
;
1480 endptr
= (char *)d
+ mp
->m_dirblksize
;
1481 if (xfs_sb_version_hascrc(&mp
->m_sb
))
1482 wantmagic
= XFS_DIR3_DATA_MAGIC
;
1484 wantmagic
= XFS_DIR2_DATA_MAGIC
;
1486 db
= xfs_dir2_da_to_db(mp
, da_bno
);
1488 /* check for data block beyond expected end */
1489 if (freetab
->naents
<= db
) {
1490 struct freetab_ent e
;
1492 *freetabp
= freetab
= realloc(freetab
, FREETAB_SIZE(db
+ 1));
1495 _("realloc failed in longform_dir2_entry_check_data (%zu bytes)\n"),
1496 FREETAB_SIZE(db
+ 1));
1500 for (i
= freetab
->naents
; i
< db
; i
++)
1501 freetab
->ents
[i
] = e
;
1502 freetab
->naents
= db
+ 1;
1505 /* check the data block */
1506 while (ptr
< endptr
) {
1508 /* check for freespace */
1509 dup
= (xfs_dir2_data_unused_t
*)ptr
;
1510 if (XFS_DIR2_DATA_FREE_TAG
== be16_to_cpu(dup
->freetag
)) {
1512 /* check for invalid freespace length */
1513 if (ptr
+ be16_to_cpu(dup
->length
) > endptr
||
1514 be16_to_cpu(dup
->length
) == 0 ||
1515 (be16_to_cpu(dup
->length
) &
1516 (XFS_DIR2_DATA_ALIGN
- 1)))
1519 /* check for invalid tag */
1520 if (be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup
)) !=
1521 (char *)dup
- (char *)d
)
1524 /* check for block with no data entries */
1525 if ((ptr
== (char *)xfs_dir3_data_entry_p(d
)) &&
1526 (ptr
+ be16_to_cpu(dup
->length
) >= endptr
)) {
1532 /* continue at the end of the freespace */
1533 ptr
+= be16_to_cpu(dup
->length
);
1538 /* validate data entry size */
1539 dep
= (xfs_dir2_data_entry_t
*)ptr
;
1540 if (ptr
+ xfs_dir3_data_entsize(mp
, dep
->namelen
) > endptr
)
1542 if (be16_to_cpu(*xfs_dir3_data_entry_tag_p(mp
, dep
)) !=
1543 (char *)dep
- (char *)d
)
1545 ptr
+= xfs_dir3_data_entsize(mp
, dep
->namelen
);
1548 /* did we find an empty or corrupt block? */
1549 if (ptr
!= endptr
) {
1552 _("empty data block %u in directory inode %" PRIu64
": "),
1556 ("corrupt block %u in directory inode %" PRIu64
": "),
1560 do_warn(_("junking block\n"));
1561 dir2_kill_block(mp
, ip
, da_bno
, bp
);
1563 do_warn(_("would junk block\n"));
1566 freetab
->ents
[db
].v
= NULLDATAOFF
;
1571 /* update number of data blocks processed */
1572 if (freetab
->nents
< db
+ 1)
1573 freetab
->nents
= db
+ 1;
1575 tp
= libxfs_trans_alloc(mp
, 0);
1576 error
= libxfs_trans_reserve(tp
, &M_RES(mp
)->tr_remove
, 0, 0);
1579 libxfs_trans_ijoin(tp
, ip
, 0);
1580 libxfs_trans_ihold(tp
, ip
);
1581 libxfs_trans_bjoin(tp
, bp
);
1582 libxfs_trans_bhold(tp
, bp
);
1583 xfs_bmap_init(&flist
, &firstblock
);
1584 if (be32_to_cpu(d
->magic
) != wantmagic
) {
1586 _("bad directory block magic # %#x for directory inode %" PRIu64
" block %d: "),
1587 be32_to_cpu(d
->magic
), ip
->i_ino
, da_bno
);
1589 do_warn(_("fixing magic # to %#x\n"), wantmagic
);
1590 d
->magic
= cpu_to_be32(wantmagic
);
1593 do_warn(_("would fix magic # to %#x\n"), wantmagic
);
1596 ptr
= (char *)xfs_dir3_data_entry_p(d
);
1598 * look at each entry. reference inode pointed to by each
1599 * entry in the incore inode tree.
1600 * if not a directory, set reached flag, increment link count
1601 * if a directory and reached, mark entry as to be deleted.
1602 * if a directory, check to see if recorded parent
1603 * matches current inode #,
1604 * if so, then set reached flag, increment link count
1605 * of current and child dir inodes, push the child
1606 * directory inode onto the directory stack.
1607 * if current inode != parent, then mark entry to be deleted.
1609 while (ptr
< endptr
) {
1610 dup
= (xfs_dir2_data_unused_t
*)ptr
;
1611 if (be16_to_cpu(dup
->freetag
) == XFS_DIR2_DATA_FREE_TAG
) {
1614 _("directory inode %" PRIu64
" block %u has consecutive free entries: "),
1617 do_warn(_("joining together\n"));
1618 len
= be16_to_cpu(dup
->length
);
1619 libxfs_dir2_data_use_free(tp
, bp
, dup
,
1620 ptr
- (char *)d
, len
, &needlog
,
1622 libxfs_dir2_data_make_free(tp
, bp
,
1623 ptr
- (char *)d
, len
, &needlog
,
1626 do_warn(_("would join together\n"));
1628 ptr
+= be16_to_cpu(dup
->length
);
1632 addr
= xfs_dir2_db_off_to_dataptr(mp
, db
, ptr
- (char *)d
);
1633 dep
= (xfs_dir2_data_entry_t
*)ptr
;
1634 ptr
+= xfs_dir3_data_entsize(mp
, dep
->namelen
);
1635 inum
= be64_to_cpu(dep
->inumber
);
1638 * skip bogus entries (leading '/'). they'll be deleted
1639 * later. must still log it, else we leak references to
1642 if (dep
->name
[0] == '/') {
1645 libxfs_dir2_data_log_entry(tp
, bp
, dep
);
1649 memmove(fname
, dep
->name
, dep
->namelen
);
1650 fname
[dep
->namelen
] = '\0';
1651 ASSERT(inum
!= NULLFSINO
);
1653 irec
= find_inode_rec(mp
, XFS_INO_TO_AGNO(mp
, inum
),
1654 XFS_INO_TO_AGINO(mp
, inum
));
1658 _("entry \"%s\" in directory inode %" PRIu64
" points to non-existent inode %" PRIu64
""),
1659 fname
, ip
->i_ino
, inum
)) {
1661 libxfs_dir2_data_log_entry(tp
, bp
, dep
);
1665 ino_offset
= XFS_INO_TO_AGINO(mp
, inum
) - irec
->ino_startnum
;
1668 * if it's a free inode, blow out the entry.
1669 * by now, any inode that we think is free
1672 if (is_inode_free(irec
, ino_offset
)) {
1675 _("entry \"%s\" in directory inode %" PRIu64
" points to free inode %" PRIu64
),
1676 fname
, ip
->i_ino
, inum
)) {
1678 libxfs_dir2_data_log_entry(tp
, bp
, dep
);
1684 * check if this inode is lost+found dir in the root
1686 if (inum
== mp
->m_sb
.sb_rootino
&& strcmp(fname
, ORPHANAGE
) == 0) {
1688 * if it's not a directory, trash it
1690 if (!inode_isadir(irec
, ino_offset
)) {
1693 _("%s (ino %" PRIu64
") in root (%" PRIu64
") is not a directory"),
1694 ORPHANAGE
, inum
, ip
->i_ino
)) {
1696 libxfs_dir2_data_log_entry(tp
, bp
, dep
);
1701 * if this is a dup, it will be picked up below,
1702 * otherwise, mark it as the orphanage for later.
1705 orphanage_ino
= inum
;
1709 * check for duplicate names in directory.
1711 if (!dir_hash_add(mp
, hashtab
, addr
, inum
, dep
->namelen
,
1712 dep
->name
, xfs_dir3_dirent_get_ftype(mp
, dep
))) {
1715 _("entry \"%s\" (ino %" PRIu64
") in dir %" PRIu64
" is a duplicate name"),
1716 fname
, inum
, ip
->i_ino
)) {
1718 libxfs_dir2_data_log_entry(tp
, bp
, dep
);
1720 if (inum
== orphanage_ino
)
1726 * if just scanning to rebuild a directory due to a ".."
1727 * update, just continue
1733 * skip the '..' entry since it's checked when the
1734 * directory is reached by something else. if it never
1735 * gets reached, it'll be moved to the orphanage and we'll
1736 * take care of it then. If it doesn't exist at all, the
1737 * directory needs to be rebuilt first before being added
1740 if (dep
->namelen
== 2 && dep
->name
[0] == '.' &&
1741 dep
->name
[1] == '.') {
1743 /* ".." should be in the first block */
1746 _("entry \"%s\" (ino %" PRIu64
") in dir %" PRIu64
" is not in the the first block"), fname
,
1749 libxfs_dir2_data_log_entry(tp
, bp
, dep
);
1754 ASSERT(no_modify
|| !verify_inum(mp
, inum
));
1756 * special case the . entry. we know there's only one
1757 * '.' and only '.' points to itself because bogus entries
1758 * got trashed in phase 3 if there were > 1.
1759 * bump up link count for '.' but don't set reached
1760 * until we're actually reached by another directory
1761 * '..' is already accounted for or will be taken care
1762 * of when directory is moved to orphanage.
1764 if (ip
->i_ino
== inum
) {
1765 ASSERT(dep
->name
[0] == '.' && dep
->namelen
== 1);
1766 add_inode_ref(current_irec
, current_ino_offset
);
1768 dep
!= xfs_dir3_data_entry_p(d
)) {
1769 /* "." should be the first entry */
1772 _("entry \"%s\" in dir %" PRIu64
" is not the first entry"),
1773 fname
, inum
, ip
->i_ino
)) {
1775 libxfs_dir2_data_log_entry(tp
, bp
, dep
);
1782 * skip entries with bogus inumbers if we're in no modify mode
1784 if (no_modify
&& verify_inum(mp
, inum
))
1787 /* validate ftype field if supported */
1788 if (xfs_sb_version_hasftype(&mp
->m_sb
)) {
1789 __uint8_t dir_ftype
;
1790 __uint8_t ino_ftype
;
1792 dir_ftype
= xfs_dir3_dirent_get_ftype(mp
, dep
);
1793 ino_ftype
= get_inode_ftype(irec
, ino_offset
);
1795 if (dir_ftype
!= ino_ftype
) {
1798 _("would fix ftype mismatch (%d/%d) in directory/child inode %" PRIu64
"/%" PRIu64
"\n"),
1799 dir_ftype
, ino_ftype
,
1803 _("fixing ftype mismatch (%d/%d) in directory/child inode %" PRIu64
"/%" PRIu64
"\n"),
1804 dir_ftype
, ino_ftype
,
1806 xfs_dir3_dirent_put_ftype(mp
, dep
,
1808 libxfs_dir2_data_log_entry(tp
, bp
, dep
);
1809 dir_hash_update_ftype(hashtab
, addr
,
1816 * check easy case first, regular inode, just bump
1817 * the link count and continue
1819 if (!inode_isadir(irec
, ino_offset
)) {
1820 add_inode_reached(irec
, ino_offset
);
1823 parent
= get_inode_parent(irec
, ino_offset
);
1824 ASSERT(parent
!= 0);
1827 * bump up the link counts in parent and child
1828 * directory but if the link doesn't agree with
1829 * the .. in the child, blow out the entry.
1830 * if the directory has already been reached,
1831 * blow away the entry also.
1833 if (is_inode_reached(irec
, ino_offset
)) {
1836 _("entry \"%s\" in dir %" PRIu64
" points to an already connected directory inode %" PRIu64
"\n"),
1837 fname
, ip
->i_ino
, inum
);
1838 } else if (parent
== ip
->i_ino
) {
1839 add_inode_reached(irec
, ino_offset
);
1840 add_inode_ref(current_irec
, current_ino_offset
);
1841 } else if (parent
== NULLFSINO
) {
1842 /* ".." was missing, but this entry refers to it,
1843 so, set it as the parent and mark for rebuild */
1845 _("entry \"%s\" in dir ino %" PRIu64
" doesn't have a .. entry, will set it in ino %" PRIu64
".\n"),
1846 fname
, ip
->i_ino
, inum
);
1847 set_inode_parent(irec
, ino_offset
, ip
->i_ino
);
1848 add_inode_reached(irec
, ino_offset
);
1849 add_inode_ref(current_irec
, current_ino_offset
);
1850 add_dotdot_update(XFS_INO_TO_AGNO(mp
, inum
), irec
,
1855 _("entry \"%s\" in dir inode %" PRIu64
" inconsistent with .. value (%" PRIu64
") in ino %" PRIu64
"\n"),
1856 fname
, ip
->i_ino
, parent
, inum
);
1859 if (inum
== orphanage_ino
)
1865 libxfs_dir2_data_log_entry(tp
, bp
, dep
);
1868 _("\twill clear entry \"%s\"\n"),
1871 do_warn(_("\twould clear entry \"%s\"\n"),
1876 *num_illegal
+= nbad
;
1878 libxfs_dir2_data_freescan(mp
, d
, &needlog
);
1880 libxfs_dir2_data_log_header(tp
, bp
);
1881 libxfs_bmap_finish(&tp
, &flist
, &committed
);
1882 libxfs_trans_commit(tp
, 0);
1884 /* record the largest free space in the freetab for later checking */
1885 bf
= xfs_dir3_data_bestfree_p(d
);
1886 freetab
->ents
[db
].v
= be16_to_cpu(bf
[0].length
);
1887 freetab
->ents
[db
].s
= 0;
1891 * Check contents of leaf-form block.
1894 longform_dir2_check_leaf(
1897 dir_hash_tab_t
*hashtab
,
1905 xfs_dir2_leaf_t
*leaf
;
1906 xfs_dir2_leaf_tail_t
*ltp
;
1908 struct xfs_dir2_leaf_entry
*ents
;
1909 struct xfs_dir3_icleaf_hdr leafhdr
;
1911 da_bno
= mp
->m_dirleafblk
;
1912 if (libxfs_da_read_buf(NULL
, ip
, da_bno
, -1, &bp
, XFS_DATA_FORK
,
1913 &xfs_dir3_leaf1_buf_ops
)) {
1915 _("can't read block %u for directory inode %" PRIu64
"\n"),
1920 xfs_dir3_leaf_hdr_from_disk(&leafhdr
, leaf
);
1921 ents
= xfs_dir3_leaf_ents_p(leaf
);
1922 ltp
= xfs_dir2_leaf_tail_p(mp
, leaf
);
1923 bestsp
= xfs_dir2_leaf_bests_p(ltp
);
1924 if (!(leafhdr
.magic
== XFS_DIR2_LEAF1_MAGIC
||
1925 leafhdr
.magic
== XFS_DIR3_LEAF1_MAGIC
) ||
1926 leafhdr
.forw
|| leafhdr
.back
||
1927 leafhdr
.count
< leafhdr
.stale
||
1929 xfs_dir3_max_leaf_ents(mp
, leaf
) ||
1930 (char *)&ents
[leafhdr
.count
] > (char *)bestsp
) {
1932 _("leaf block %u for directory inode %" PRIu64
" bad header\n"),
1937 seeval
= dir_hash_see_all(hashtab
, ents
, leafhdr
.count
, leafhdr
.stale
);
1938 if (dir_hash_check(hashtab
, ip
, seeval
)) {
1942 badtail
= freetab
->nents
!= be32_to_cpu(ltp
->bestcount
);
1943 for (i
= 0; !badtail
&& i
< be32_to_cpu(ltp
->bestcount
); i
++) {
1944 freetab
->ents
[i
].s
= 1;
1945 badtail
= freetab
->ents
[i
].v
!= be16_to_cpu(bestsp
[i
]);
1949 _("leaf block %u for directory inode %" PRIu64
" bad tail\n"),
1959 * Check contents of the node blocks (leaves)
1960 * Looks for matching hash values for the data entries.
1963 longform_dir2_check_node(
1966 dir_hash_tab_t
*hashtab
,
1972 xfs_dir2_free_t
*free
;
1974 xfs_dir2_leaf_t
*leaf
;
1975 xfs_fileoff_t next_da_bno
;
1978 struct xfs_dir2_leaf_entry
*ents
;
1979 struct xfs_dir3_icleaf_hdr leafhdr
;
1980 struct xfs_dir3_icfree_hdr freehdr
;
1983 for (da_bno
= mp
->m_dirleafblk
, next_da_bno
= 0;
1984 next_da_bno
!= NULLFILEOFF
&& da_bno
< mp
->m_dirfreeblk
;
1985 da_bno
= (xfs_dablk_t
)next_da_bno
) {
1986 next_da_bno
= da_bno
+ mp
->m_dirblkfsbs
- 1;
1987 if (bmap_next_offset(NULL
, ip
, &next_da_bno
, XFS_DATA_FORK
))
1991 * we need to use the da3 node verifier here as it handles the
1992 * fact that reading the leaf hash tree blocks can return either
1993 * leaf or node blocks and calls the correct verifier. If we get
1994 * a node block, then we'll skip it below based on a magic
1997 if (libxfs_da_read_buf(NULL
, ip
, da_bno
, -1, &bp
,
1998 XFS_DATA_FORK
, &xfs_da3_node_buf_ops
)) {
2000 _("can't read leaf block %u for directory inode %" PRIu64
"\n"),
2005 xfs_dir3_leaf_hdr_from_disk(&leafhdr
, leaf
);
2006 ents
= xfs_dir3_leaf_ents_p(leaf
);
2007 if (!(leafhdr
.magic
== XFS_DIR2_LEAFN_MAGIC
||
2008 leafhdr
.magic
== XFS_DIR3_LEAFN_MAGIC
)) {
2009 if (leafhdr
.magic
== XFS_DA_NODE_MAGIC
||
2010 leafhdr
.magic
== XFS_DA3_NODE_MAGIC
) {
2015 _("unknown magic number %#x for block %u in directory inode %" PRIu64
"\n"),
2016 leafhdr
.magic
, da_bno
, ip
->i_ino
);
2020 if (leafhdr
.count
> xfs_dir3_max_leaf_ents(mp
, leaf
) ||
2021 leafhdr
.count
< leafhdr
.stale
) {
2023 _("leaf block %u for directory inode %" PRIu64
" bad header\n"),
2028 seeval
= dir_hash_see_all(hashtab
, ents
,
2029 leafhdr
.count
, leafhdr
.stale
);
2031 if (seeval
!= DIR_HASH_CK_OK
)
2034 if (dir_hash_check(hashtab
, ip
, seeval
))
2037 for (da_bno
= mp
->m_dirfreeblk
, next_da_bno
= 0;
2038 next_da_bno
!= NULLFILEOFF
;
2039 da_bno
= (xfs_dablk_t
)next_da_bno
) {
2040 next_da_bno
= da_bno
+ mp
->m_dirblkfsbs
- 1;
2041 if (bmap_next_offset(NULL
, ip
, &next_da_bno
, XFS_DATA_FORK
))
2043 if (libxfs_da_read_buf(NULL
, ip
, da_bno
, -1, &bp
,
2044 XFS_DATA_FORK
, &xfs_dir3_free_buf_ops
)) {
2046 _("can't read freespace block %u for directory inode %" PRIu64
"\n"),
2051 xfs_dir3_free_hdr_from_disk(&freehdr
, free
);
2052 bests
= xfs_dir3_free_bests_p(mp
, free
);
2053 fdb
= xfs_dir2_da_to_db(mp
, da_bno
);
2054 if (!(freehdr
.magic
== XFS_DIR2_FREE_MAGIC
||
2055 freehdr
.magic
== XFS_DIR3_FREE_MAGIC
) ||
2057 (fdb
- XFS_DIR2_FREE_FIRSTDB(mp
)) *
2058 xfs_dir3_free_max_bests(mp
) ||
2059 freehdr
.nvalid
< freehdr
.nused
) {
2061 _("free block %u for directory inode %" PRIu64
" bad header\n"),
2066 for (i
= used
= 0; i
< freehdr
.nvalid
; i
++) {
2067 if (i
+ freehdr
.firstdb
>= freetab
->nents
||
2068 freetab
->ents
[i
+ freehdr
.firstdb
].v
!=
2069 be16_to_cpu(bests
[i
])) {
2071 _("free block %u entry %i for directory ino %" PRIu64
" bad\n"),
2072 da_bno
, i
, ip
->i_ino
);
2076 used
+= be16_to_cpu(bests
[i
]) != NULLDATAOFF
;
2077 freetab
->ents
[i
+ freehdr
.firstdb
].s
= 1;
2079 if (used
!= freehdr
.nused
) {
2081 _("free block %u for directory inode %" PRIu64
" bad nused\n"),
2088 for (i
= 0; i
< freetab
->nents
; i
++) {
2089 if ((freetab
->ents
[i
].s
== 0) &&
2090 (freetab
->ents
[i
].v
!= NULLDATAOFF
)) {
2092 _("missing freetab entry %u for directory inode %" PRIu64
"\n"),
2101 * If a directory is corrupt, we need to read in as many entries as possible,
2102 * destroy the entry and create a new one with recovered name/inode pairs.
2103 * (ie. get libxfs to do all the grunt work)
2106 longform_dir2_entry_check(xfs_mount_t
*mp
,
2111 ino_tree_node_t
*irec
,
2113 dir_hash_tab_t
*hashtab
)
2115 struct xfs_buf
**bplist
;
2122 xfs_fileoff_t next_da_bno
;
2128 freetab
= malloc(FREETAB_SIZE(ip
->i_d
.di_size
/ mp
->m_dirblksize
));
2131 _("malloc failed in longform_dir2_entry_check (%" PRId64
" bytes)\n"),
2132 FREETAB_SIZE(ip
->i_d
.di_size
/ mp
->m_dirblksize
));
2135 freetab
->naents
= ip
->i_d
.di_size
/ mp
->m_dirblksize
;
2137 for (i
= 0; i
< freetab
->naents
; i
++) {
2138 freetab
->ents
[i
].v
= NULLDATAOFF
;
2139 freetab
->ents
[i
].s
= 0;
2141 num_bps
= freetab
->naents
;
2142 bplist
= calloc(num_bps
, sizeof(struct xfs_buf
*));
2143 /* is this a block, leaf, or node directory? */
2144 libxfs_dir2_isblock(NULL
, ip
, &isblock
);
2145 libxfs_dir2_isleaf(NULL
, ip
, &isleaf
);
2147 /* check directory "data" blocks (ie. name/inode pairs) */
2148 for (da_bno
= 0, next_da_bno
= 0;
2149 next_da_bno
!= NULLFILEOFF
&& da_bno
< mp
->m_dirleafblk
;
2150 da_bno
= (xfs_dablk_t
)next_da_bno
) {
2151 const struct xfs_buf_ops
*ops
;
2153 next_da_bno
= da_bno
+ mp
->m_dirblkfsbs
- 1;
2154 if (bmap_next_offset(NULL
, ip
, &next_da_bno
, XFS_DATA_FORK
))
2156 db
= xfs_dir2_da_to_db(mp
, da_bno
);
2157 if (db
>= num_bps
) {
2158 /* more data blocks than expected */
2160 bplist
= realloc(bplist
, num_bps
* sizeof(struct xfs_buf
*));
2163 _("realloc failed in longform_dir2_entry_check (%zu bytes)\n"),
2164 num_bps
* sizeof(struct xfs_buf
*));
2168 ops
= &xfs_dir3_block_buf_ops
;
2170 ops
= &xfs_dir3_data_buf_ops
;
2171 if (libxfs_da_read_buf(NULL
, ip
, da_bno
, -1, &bplist
[db
],
2172 XFS_DATA_FORK
, ops
)) {
2174 _("can't read data block %u for directory inode %" PRIu64
"\n"),
2179 * we try to read all "data" blocks, but if we are in
2180 * block form and we fail, there isn't anything else to
2181 * read, and nothing we can do but trash it.
2189 longform_dir2_entry_check_data(mp
, ip
, num_illegal
, need_dot
,
2190 irec
, ino_offset
, &bplist
[db
], hashtab
,
2191 &freetab
, da_bno
, isblock
);
2193 fixit
= (*num_illegal
!= 0) || dir2_is_badino(ino
) || *need_dot
;
2195 if (!dotdot_update
) {
2196 /* check btree and freespace */
2198 struct xfs_dir2_data_hdr
*block
;
2199 xfs_dir2_block_tail_t
*btp
;
2200 xfs_dir2_leaf_entry_t
*blp
;
2202 block
= bplist
[0]->b_addr
;
2203 btp
= xfs_dir2_block_tail_p(mp
, block
);
2204 blp
= xfs_dir2_block_leaf_p(btp
);
2205 seeval
= dir_hash_see_all(hashtab
, blp
,
2206 be32_to_cpu(btp
->count
),
2207 be32_to_cpu(btp
->stale
));
2208 if (dir_hash_check(hashtab
, ip
, seeval
))
2210 } else if (isleaf
) {
2211 fixit
|= longform_dir2_check_leaf(mp
, ip
, hashtab
,
2214 fixit
|= longform_dir2_check_node(mp
, ip
, hashtab
,
2219 if (!no_modify
&& (fixit
|| dotdot_update
)) {
2220 dir_hash_dup_names(hashtab
);
2221 for (i
= 0; i
< freetab
->naents
; i
++)
2223 libxfs_putbuf(bplist
[i
]);
2224 longform_dir2_rebuild(mp
, ino
, ip
, irec
, ino_offset
, hashtab
);
2228 for (i
= 0; i
< freetab
->naents
; i
++)
2230 libxfs_putbuf(bplist
[i
]);
2238 * shortform directory v2 processing routines -- entry verification and
2239 * bad entry deletion (pruning).
2241 static struct xfs_dir2_sf_entry
*
2242 shortform_dir2_junk(
2243 struct xfs_mount
*mp
,
2244 struct xfs_dir2_sf_hdr
*sfp
,
2245 struct xfs_dir2_sf_entry
*sfep
,
2252 struct xfs_dir2_sf_entry
*next_sfep
;
2256 if (lino
== orphanage_ino
)
2259 next_elen
= xfs_dir3_sf_entsize(mp
, sfp
, sfep
->namelen
);
2260 next_sfep
= (xfs_dir2_sf_entry_t
*)((__psint_t
)sfep
+ next_elen
);
2263 * if we are just checking, simply return the pointer to the next entry
2264 * here so that the checking loop can continue.
2267 do_warn(_("would junk entry\n"));
2272 * now move all the remaining entries down over the junked entry and
2273 * clear the newly unused bytes at the tail of the directory region.
2275 next_len
= *max_size
- ((__psint_t
)next_sfep
- (__psint_t
)sfp
);
2276 *max_size
-= next_elen
;
2277 *bytes_deleted
+= next_elen
;
2279 memmove(sfep
, next_sfep
, next_len
);
2280 memset((void *)((__psint_t
)sfep
+ next_len
), 0, next_elen
);
2285 * WARNING: drop the index i by one so it matches the decremented count
2286 * for accurate comparisons in the loop test
2291 do_warn(_("junking entry\n"));
2298 shortform_dir2_entry_check(xfs_mount_t
*mp
,
2302 ino_tree_node_t
*current_irec
,
2303 int current_ino_offset
,
2304 dir_hash_tab_t
*hashtab
)
2308 struct xfs_dir2_sf_hdr
*sfp
;
2309 struct xfs_dir2_sf_entry
*sfep
;
2310 struct xfs_dir2_sf_entry
*next_sfep
;
2311 struct xfs_ifork
*ifp
;
2312 struct ino_tree_node
*irec
;
2319 char fname
[MAXNAMELEN
+ 1];
2323 sfp
= (struct xfs_dir2_sf_hdr
*) ifp
->if_u1
.if_data
;
2327 max_size
= ifp
->if_bytes
;
2328 ASSERT(ip
->i_d
.di_size
<= ifp
->if_bytes
);
2331 * if just rebuild a directory due to a "..", update and return
2333 if (dotdot_update
) {
2334 parent
= get_inode_parent(current_irec
, current_ino_offset
);
2337 _("would set .. in sf dir inode %" PRIu64
" to %" PRIu64
"\n"),
2341 _("setting .. in sf dir inode %" PRIu64
" to %" PRIu64
"\n"),
2343 xfs_dir2_sf_put_parent_ino(sfp
, parent
);
2350 * no '.' entry in shortform dirs, just bump up ref count by 1
2351 * '..' was already (or will be) accounted for and checked when
2352 * the directory is reached or will be taken care of when the
2353 * directory is moved to orphanage.
2355 add_inode_ref(current_irec
, current_ino_offset
);
2358 * Initialise i8 counter -- the parent inode number counts as well.
2360 i8
= xfs_dir2_sf_get_parent_ino(sfp
) > XFS_DIR2_MAX_SHORT_INUM
;
2363 * now run through entries, stop at first bad entry, don't need
2364 * to skip over '..' since that's encoded in its own field and
2365 * no need to worry about '.' since it doesn't exist.
2367 sfep
= next_sfep
= xfs_dir2_sf_firstentry(sfp
);
2369 for (i
= 0; i
< sfp
->count
&& max_size
>
2370 (__psint_t
)next_sfep
- (__psint_t
)sfp
;
2371 sfep
= next_sfep
, i
++) {
2374 lino
= xfs_dir3_sfe_get_ino(mp
, sfp
, sfep
);
2376 namelen
= sfep
->namelen
;
2378 ASSERT(no_modify
|| namelen
> 0);
2380 if (no_modify
&& namelen
== 0) {
2382 * if we're really lucky, this is
2383 * the last entry in which case we
2384 * can use the dir size to set the
2385 * namelen value. otherwise, forget
2386 * it because we're not going to be
2387 * able to find the next entry.
2391 if (i
== sfp
->count
- 1) {
2392 namelen
= ip
->i_d
.di_size
-
2393 ((__psint_t
) &sfep
->name
[0] -
2397 * don't process the rest of the directory,
2398 * break out of processing loop
2402 } else if (no_modify
&& (__psint_t
) sfep
- (__psint_t
) sfp
+
2403 + xfs_dir3_sf_entsize(mp
, sfp
, sfep
->namelen
)
2404 > ip
->i_d
.di_size
) {
2407 if (i
== sfp
->count
- 1) {
2408 namelen
= ip
->i_d
.di_size
-
2409 ((__psint_t
) &sfep
->name
[0] -
2413 * don't process the rest of the directory,
2414 * break out of processing loop
2420 memmove(fname
, sfep
->name
, sfep
->namelen
);
2421 fname
[sfep
->namelen
] = '\0';
2423 ASSERT(no_modify
|| (lino
!= NULLFSINO
&& lino
!= 0));
2424 ASSERT(no_modify
|| !verify_inum(mp
, lino
));
2427 * Also skip entries with bogus inode numbers if we're
2428 * in no modify mode.
2431 if (no_modify
&& verify_inum(mp
, lino
)) {
2432 next_sfep
= (xfs_dir2_sf_entry_t
*)((__psint_t
)sfep
+
2433 xfs_dir3_sf_entsize(mp
, sfp
, sfep
->namelen
));
2437 irec
= find_inode_rec(mp
, XFS_INO_TO_AGNO(mp
, lino
),
2438 XFS_INO_TO_AGINO(mp
, lino
));
2442 _("entry \"%s\" in shortform directory %" PRIu64
" references non-existent inode %" PRIu64
"\n"),
2444 next_sfep
= shortform_dir2_junk(mp
, sfp
, sfep
, lino
,
2445 &max_size
, &i
, &bytes_deleted
,
2450 ino_offset
= XFS_INO_TO_AGINO(mp
, lino
) - irec
->ino_startnum
;
2453 * if it's a free inode, blow out the entry.
2454 * by now, any inode that we think is free
2457 if (is_inode_free(irec
, ino_offset
)) {
2459 _("entry \"%s\" in shortform directory inode %" PRIu64
" points to free inode %" PRIu64
"\n"),
2461 next_sfep
= shortform_dir2_junk(mp
, sfp
, sfep
, lino
,
2462 &max_size
, &i
, &bytes_deleted
,
2467 * check if this inode is lost+found dir in the root
2469 if (ino
== mp
->m_sb
.sb_rootino
&& strcmp(fname
, ORPHANAGE
) == 0) {
2471 * if it's not a directory, trash it
2473 if (!inode_isadir(irec
, ino_offset
)) {
2475 _("%s (ino %" PRIu64
") in root (%" PRIu64
") is not a directory"),
2476 ORPHANAGE
, lino
, ino
);
2477 next_sfep
= shortform_dir2_junk(mp
, sfp
, sfep
,
2478 lino
, &max_size
, &i
,
2479 &bytes_deleted
, ino_dirty
);
2483 * if this is a dup, it will be picked up below,
2484 * otherwise, mark it as the orphanage for later.
2487 orphanage_ino
= lino
;
2490 * check for duplicate names in directory.
2492 if (!dir_hash_add(mp
, hashtab
, (xfs_dir2_dataptr_t
)
2493 (sfep
- xfs_dir2_sf_firstentry(sfp
)),
2494 lino
, sfep
->namelen
, sfep
->name
,
2495 xfs_dir3_sfe_get_ftype(mp
, sfp
, sfep
))) {
2497 _("entry \"%s\" (ino %" PRIu64
") in dir %" PRIu64
" is a duplicate name"),
2499 next_sfep
= shortform_dir2_junk(mp
, sfp
, sfep
, lino
,
2500 &max_size
, &i
, &bytes_deleted
,
2505 if (!inode_isadir(irec
, ino_offset
)) {
2507 * check easy case first, regular inode, just bump
2510 add_inode_reached(irec
, ino_offset
);
2512 parent
= get_inode_parent(irec
, ino_offset
);
2515 * bump up the link counts in parent and child.
2516 * directory but if the link doesn't agree with
2517 * the .. in the child, blow out the entry
2519 if (is_inode_reached(irec
, ino_offset
)) {
2521 _("entry \"%s\" in directory inode %" PRIu64
2522 " references already connected inode %" PRIu64
".\n"),
2524 next_sfep
= shortform_dir2_junk(mp
, sfp
, sfep
,
2525 lino
, &max_size
, &i
,
2526 &bytes_deleted
, ino_dirty
);
2528 } else if (parent
== ino
) {
2529 add_inode_reached(irec
, ino_offset
);
2530 add_inode_ref(current_irec
, current_ino_offset
);
2531 } else if (parent
== NULLFSINO
) {
2532 /* ".." was missing, but this entry refers to it,
2533 so, set it as the parent and mark for rebuild */
2535 _("entry \"%s\" in dir ino %" PRIu64
" doesn't have a .. entry, will set it in ino %" PRIu64
".\n"),
2537 set_inode_parent(irec
, ino_offset
, ino
);
2538 add_inode_reached(irec
, ino_offset
);
2539 add_inode_ref(current_irec
, current_ino_offset
);
2540 add_dotdot_update(XFS_INO_TO_AGNO(mp
, lino
),
2544 _("entry \"%s\" in directory inode %" PRIu64
2545 " not consistent with .. value (%" PRIu64
2546 ") in inode %" PRIu64
",\n"),
2547 fname
, ino
, parent
, lino
);
2548 next_sfep
= shortform_dir2_junk(mp
, sfp
, sfep
,
2549 lino
, &max_size
, &i
,
2550 &bytes_deleted
, ino_dirty
);
2555 /* validate ftype field if supported */
2556 if (xfs_sb_version_hasftype(&mp
->m_sb
)) {
2557 __uint8_t dir_ftype
;
2558 __uint8_t ino_ftype
;
2560 dir_ftype
= xfs_dir3_sfe_get_ftype(mp
, sfp
, sfep
);
2561 ino_ftype
= get_inode_ftype(irec
, ino_offset
);
2563 if (dir_ftype
!= ino_ftype
) {
2566 _("would fix ftype mismatch (%d/%d) in directory/child inode %" PRIu64
"/%" PRIu64
"\n"),
2567 dir_ftype
, ino_ftype
,
2571 _("fixing ftype mismatch (%d/%d) in directory/child inode %" PRIu64
"/%" PRIu64
"\n"),
2572 dir_ftype
, ino_ftype
,
2574 xfs_dir3_sfe_put_ftype(mp
, sfp
, sfep
,
2576 dir_hash_update_ftype(hashtab
,
2577 (xfs_dir2_dataptr_t
)(sfep
- xfs_dir2_sf_firstentry(sfp
)),
2584 if (lino
> XFS_DIR2_MAX_SHORT_INUM
)
2588 * go onto next entry - we have to take entries with bad namelen
2589 * into account in no modify mode since we calculate size based
2592 ASSERT(no_modify
|| bad_sfnamelen
== 0);
2593 next_sfep
= (struct xfs_dir2_sf_entry
*)((__psint_t
)sfep
+
2595 ? xfs_dir3_sf_entsize(mp
, sfp
, namelen
)
2596 : xfs_dir3_sf_entsize(mp
, sfp
, sfep
->namelen
)));
2599 if (sfp
->i8count
!= i8
) {
2601 do_warn(_("would fix i8count in inode %" PRIu64
"\n"),
2605 struct xfs_dir2_sf_entry
*tmp_sfep
;
2607 tmp_sfep
= next_sfep
;
2608 process_sf_dir2_fixi8(mp
, sfp
, &tmp_sfep
);
2610 (__psint_t
)next_sfep
-
2611 (__psint_t
)tmp_sfep
;
2612 next_sfep
= tmp_sfep
;
2616 do_warn(_("fixing i8count in inode %" PRIu64
"\n"),
2622 * sync up sizes if required
2624 if (*ino_dirty
&& bytes_deleted
> 0) {
2626 libxfs_idata_realloc(ip
, -bytes_deleted
, XFS_DATA_FORK
);
2627 ip
->i_d
.di_size
-= bytes_deleted
;
2630 if (ip
->i_d
.di_size
!= ip
->i_df
.if_bytes
) {
2631 ASSERT(ip
->i_df
.if_bytes
== (xfs_fsize_t
)
2632 ((__psint_t
) next_sfep
- (__psint_t
) sfp
));
2633 ip
->i_d
.di_size
= (xfs_fsize_t
)
2634 ((__psint_t
) next_sfep
- (__psint_t
) sfp
);
2636 _("setting size to %" PRId64
" bytes to reflect junked entries\n"),
2643 * processes all reachable inodes in directories
2648 xfs_agnumber_t agno
,
2649 ino_tree_node_t
*irec
,
2653 xfs_bmap_free_t flist
;
2654 xfs_fsblock_t first
;
2657 dir_hash_tab_t
*hashtab
;
2658 int need_dot
, committed
;
2659 int dirty
, num_illegal
, error
, nres
;
2661 ino
= XFS_AGINO_TO_INO(mp
, agno
, irec
->ino_startnum
+ ino_offset
);
2664 * open up directory inode, check all entries,
2665 * then call prune_dir_entries to remove all
2666 * remaining illegal directory entries.
2669 ASSERT(!is_inode_refchecked(irec
, ino_offset
) || dotdot_update
);
2671 error
= libxfs_iget(mp
, NULL
, ino
, 0, &ip
, 0);
2675 _("couldn't map inode %" PRIu64
", err = %d\n"),
2679 _("couldn't map inode %" PRIu64
", err = %d\n"),
2682 * see below for what we're doing if this
2683 * is root. Why do we need to do this here?
2684 * to ensure that the root doesn't show up
2685 * as being disconnected in the no_modify case.
2687 if (mp
->m_sb
.sb_rootino
== ino
) {
2688 add_inode_reached(irec
, 0);
2689 add_inode_ref(irec
, 0);
2693 add_inode_refchecked(irec
, 0);
2697 need_dot
= dirty
= num_illegal
= 0;
2699 if (mp
->m_sb
.sb_rootino
== ino
) {
2701 * mark root inode reached and bump up
2702 * link count for root inode to account
2703 * for '..' entry since the root inode is
2704 * never reached by a parent. we know
2705 * that root's '..' is always good --
2706 * guaranteed by phase 3 and/or below.
2708 add_inode_reached(irec
, ino_offset
);
2711 add_inode_refchecked(irec
, ino_offset
);
2713 hashtab
= dir_hash_init(ip
->i_d
.di_size
);
2716 * look for bogus entries
2718 switch (ip
->i_d
.di_format
) {
2719 case XFS_DINODE_FMT_EXTENTS
:
2720 case XFS_DINODE_FMT_BTREE
:
2722 * also check for missing '.' in longform dirs.
2723 * missing .. entries are added if required when
2724 * the directory is connected to lost+found. but
2725 * we need to create '.' entries here.
2727 longform_dir2_entry_check(mp
, ino
, ip
,
2728 &num_illegal
, &need_dot
,
2733 case XFS_DINODE_FMT_LOCAL
:
2734 tp
= libxfs_trans_alloc(mp
, 0);
2736 * using the remove reservation is overkill
2737 * since at most we'll only need to log the
2738 * inode but it's easier than wedging a
2739 * new define in ourselves.
2741 nres
= no_modify
? 0 : XFS_REMOVE_SPACE_RES(mp
);
2742 error
= libxfs_trans_reserve(tp
, &M_RES(mp
)->tr_remove
,
2747 libxfs_trans_ijoin(tp
, ip
, 0);
2748 libxfs_trans_ihold(tp
, ip
);
2750 shortform_dir2_entry_check(mp
, ino
, ip
, &dirty
,
2754 ASSERT(dirty
== 0 || (dirty
&& !no_modify
));
2756 libxfs_trans_log_inode(tp
, ip
,
2757 XFS_ILOG_CORE
| XFS_ILOG_DDATA
);
2758 libxfs_trans_commit(tp
,
2759 XFS_TRANS_RELEASE_LOG_RES
|
2762 libxfs_trans_cancel(tp
,
2763 XFS_TRANS_RELEASE_LOG_RES
);
2770 dir_hash_done(hashtab
);
2773 * if we have to create a .. for /, do it now *before*
2774 * we delete the bogus entries, otherwise the directory
2775 * could transform into a shortform dir which would
2776 * probably cause the simulation to choke. Even
2777 * if the illegal entries get shifted around, it's ok
2778 * because the entries are structurally intact and in
2779 * in hash-value order so the simulation won't get confused
2780 * if it has to move them around.
2782 if (!no_modify
&& need_root_dotdot
&& ino
== mp
->m_sb
.sb_rootino
) {
2783 ASSERT(ip
->i_d
.di_format
!= XFS_DINODE_FMT_LOCAL
);
2785 do_warn(_("recreating root directory .. entry\n"));
2787 tp
= libxfs_trans_alloc(mp
, 0);
2790 nres
= XFS_MKDIR_SPACE_RES(mp
, 2);
2791 error
= libxfs_trans_reserve(tp
, &M_RES(mp
)->tr_mkdir
, nres
, 0);
2795 libxfs_trans_ijoin(tp
, ip
, 0);
2796 libxfs_trans_ihold(tp
, ip
);
2798 xfs_bmap_init(&flist
, &first
);
2800 error
= libxfs_dir_createname(tp
, ip
, &xfs_name_dotdot
,
2801 ip
->i_ino
, &first
, &flist
, nres
);
2804 _("can't make \"..\" entry in root inode %" PRIu64
", createname error %d\n"), ino
, error
);
2806 libxfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
2808 error
= libxfs_bmap_finish(&tp
, &flist
, &committed
);
2810 libxfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
|
2813 need_root_dotdot
= 0;
2814 } else if (need_root_dotdot
&& ino
== mp
->m_sb
.sb_rootino
) {
2815 do_warn(_("would recreate root directory .. entry\n"));
2819 * if we need to create the '.' entry, do so only if
2820 * the directory is a longform dir. if it's been
2821 * turned into a shortform dir, then the inode is ok
2822 * since shortform dirs have no '.' entry and the inode
2823 * has already been committed by prune_lf_dir_entry().
2827 * bump up our link count but don't
2828 * bump up the inode link count. chances
2829 * are good that even though we lost '.'
2830 * the inode link counts reflect '.' so
2831 * leave the inode link count alone and if
2832 * it turns out to be wrong, we'll catch
2835 add_inode_ref(irec
, ino_offset
);
2839 _("would create missing \".\" entry in dir ino %" PRIu64
"\n"),
2841 } else if (ip
->i_d
.di_format
!= XFS_DINODE_FMT_LOCAL
) {
2843 * need to create . entry in longform dir.
2846 _("creating missing \".\" entry in dir ino %" PRIu64
"\n"), ino
);
2848 tp
= libxfs_trans_alloc(mp
, 0);
2851 nres
= XFS_MKDIR_SPACE_RES(mp
, 1);
2852 error
= libxfs_trans_reserve(tp
, &M_RES(mp
)->tr_mkdir
,
2857 libxfs_trans_ijoin(tp
, ip
, 0);
2858 libxfs_trans_ihold(tp
, ip
);
2860 xfs_bmap_init(&flist
, &first
);
2862 error
= libxfs_dir_createname(tp
, ip
, &xfs_name_dot
,
2863 ip
->i_ino
, &first
, &flist
, nres
);
2866 _("can't make \".\" entry in dir ino %" PRIu64
", createname error %d\n"),
2869 libxfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
2871 error
= libxfs_bmap_finish(&tp
, &flist
, &committed
);
2873 libxfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
2881 * mark realtime bitmap and summary inodes as reached.
2882 * quota inode will be marked here as well
2885 mark_standalone_inodes(xfs_mount_t
*mp
)
2887 ino_tree_node_t
*irec
;
2890 irec
= find_inode_rec(mp
, XFS_INO_TO_AGNO(mp
, mp
->m_sb
.sb_rbmino
),
2891 XFS_INO_TO_AGINO(mp
, mp
->m_sb
.sb_rbmino
));
2893 ASSERT(irec
!= NULL
);
2895 offset
= XFS_INO_TO_AGINO(mp
, mp
->m_sb
.sb_rbmino
) -
2898 add_inode_reached(irec
, offset
);
2900 irec
= find_inode_rec(mp
, XFS_INO_TO_AGNO(mp
, mp
->m_sb
.sb_rsumino
),
2901 XFS_INO_TO_AGINO(mp
, mp
->m_sb
.sb_rsumino
));
2903 offset
= XFS_INO_TO_AGINO(mp
, mp
->m_sb
.sb_rsumino
) -
2906 ASSERT(irec
!= NULL
);
2908 add_inode_reached(irec
, offset
);
2911 if (mp
->m_sb
.sb_uquotino
2912 && mp
->m_sb
.sb_uquotino
!= NULLFSINO
) {
2913 irec
= find_inode_rec(mp
, XFS_INO_TO_AGNO(mp
,
2914 mp
->m_sb
.sb_uquotino
),
2915 XFS_INO_TO_AGINO(mp
, mp
->m_sb
.sb_uquotino
));
2916 offset
= XFS_INO_TO_AGINO(mp
, mp
->m_sb
.sb_uquotino
)
2917 - irec
->ino_startnum
;
2918 add_inode_reached(irec
, offset
);
2920 if (mp
->m_sb
.sb_gquotino
2921 && mp
->m_sb
.sb_gquotino
!= NULLFSINO
) {
2922 irec
= find_inode_rec(mp
, XFS_INO_TO_AGNO(mp
,
2923 mp
->m_sb
.sb_gquotino
),
2924 XFS_INO_TO_AGINO(mp
, mp
->m_sb
.sb_gquotino
));
2925 offset
= XFS_INO_TO_AGINO(mp
, mp
->m_sb
.sb_gquotino
)
2926 - irec
->ino_startnum
;
2927 add_inode_reached(irec
, offset
);
2929 if (mp
->m_sb
.sb_pquotino
2930 && mp
->m_sb
.sb_pquotino
!= NULLFSINO
) {
2931 irec
= find_inode_rec(mp
, XFS_INO_TO_AGNO(mp
,
2932 mp
->m_sb
.sb_pquotino
),
2933 XFS_INO_TO_AGINO(mp
, mp
->m_sb
.sb_pquotino
));
2934 offset
= XFS_INO_TO_AGINO(mp
, mp
->m_sb
.sb_pquotino
)
2935 - irec
->ino_startnum
;
2936 add_inode_reached(irec
, offset
);
2942 check_for_orphaned_inodes(
2944 xfs_agnumber_t agno
,
2945 ino_tree_node_t
*irec
)
2950 for (i
= 0; i
< XFS_INODES_PER_CHUNK
; i
++) {
2951 ASSERT(is_inode_confirmed(irec
, i
));
2952 if (is_inode_free(irec
, i
))
2955 if (is_inode_reached(irec
, i
))
2958 ASSERT(inode_isadir(irec
, i
) ||
2959 num_inode_references(irec
, i
) == 0);
2961 ino
= XFS_AGINO_TO_INO(mp
, agno
, i
+ irec
->ino_startnum
);
2962 if (inode_isadir(irec
, i
))
2963 do_warn(_("disconnected dir inode %" PRIu64
", "), ino
);
2965 do_warn(_("disconnected inode %" PRIu64
", "), ino
);
2968 orphanage_ino
= mk_orphanage(mp
);
2969 do_warn(_("moving to %s\n"), ORPHANAGE
);
2970 mv_orphanage(mp
, ino
, inode_isadir(irec
, i
));
2972 do_warn(_("would move to %s\n"), ORPHANAGE
);
2975 * for read-only case, even though the inode isn't
2976 * really reachable, set the flag (and bump our link
2977 * count) anyway to fool phase 7
2979 add_inode_reached(irec
, i
);
2986 xfs_agnumber_t agno
,
2989 ino_tree_node_t
*irec
;
2991 prefetch_args_t
*pf_args
= arg
;
2993 wait_for_inode_prefetch(pf_args
);
2996 do_log(_(" - agno = %d\n"), agno
);
2998 for (irec
= findfirst_inode_rec(agno
); irec
; irec
= next_ino_rec(irec
)) {
2999 if (irec
->ino_isa_dir
== 0)
3003 sem_post(&pf_args
->ra_count
);
3005 for (i
= 0; i
< XFS_INODES_PER_CHUNK
; i
++) {
3006 if (inode_isadir(irec
, i
))
3007 process_dir_inode(wq
->mp
, agno
, irec
, i
);
3010 cleanup_inode_prefetch(pf_args
);
3014 update_missing_dotdot_entries(
3017 dotdot_update_t
*dir
;
3020 * these entries parents were updated, rebuild them again
3021 * set dotdot_update flag so processing routines do not count links
3024 while (!list_empty(&dotdot_update_list
)) {
3025 dir
= list_entry(dotdot_update_list
.prev
, struct dotdot_update
,
3027 list_del(&dir
->list
);
3028 process_dir_inode(mp
, dir
->agno
, dir
->irec
, dir
->ino_offset
);
3039 prefetch_args_t
*pf_args
[2];
3042 * we always do prefetch for phase 6 as it will fill in the gaps
3043 * not read during phase 3 prefetch.
3046 pf_args
[0] = start_inode_prefetch(0, 1, NULL
);
3047 for (i
= 0; i
< glob_agcount
; i
++) {
3048 pf_args
[(~i
) & 1] = start_inode_prefetch(i
+ 1, 1,
3050 traverse_function(&queue
, i
, pf_args
[i
& 1]);
3055 phase6(xfs_mount_t
*mp
)
3057 ino_tree_node_t
*irec
;
3060 memset(&zerocr
, 0, sizeof(struct cred
));
3061 memset(&zerofsx
, 0, sizeof(struct fsxattr
));
3064 do_log(_("Phase 6 - check inode connectivity...\n"));
3066 incore_ext_teardown(mp
);
3068 add_ino_ex_data(mp
);
3071 * verify existence of root directory - if we have to
3072 * make one, it's ok for the incore data structs not to
3073 * know about it since everything about it (and the other
3074 * inodes in its chunk if a new chunk was created) are ok
3076 if (need_root_inode
) {
3078 do_warn(_("reinitializing root directory\n"));
3080 need_root_inode
= 0;
3081 need_root_dotdot
= 0;
3083 do_warn(_("would reinitialize root directory\n"));
3089 do_warn(_("reinitializing realtime bitmap inode\n"));
3093 do_warn(_("would reinitialize realtime bitmap inode\n"));
3099 do_warn(_("reinitializing realtime summary inode\n"));
3103 do_warn(_("would reinitialize realtime summary inode\n"));
3109 _(" - resetting contents of realtime bitmap and summary inodes\n"));
3110 if (fill_rbmino(mp
)) {
3112 _("Warning: realtime bitmap may be inconsistent\n"));
3115 if (fill_rsumino(mp
)) {
3117 _("Warning: realtime bitmap may be inconsistent\n"));
3121 mark_standalone_inodes(mp
);
3123 do_log(_(" - traversing filesystem ...\n"));
3125 irec
= find_inode_rec(mp
, XFS_INO_TO_AGNO(mp
, mp
->m_sb
.sb_rootino
),
3126 XFS_INO_TO_AGINO(mp
, mp
->m_sb
.sb_rootino
));
3129 * we always have a root inode, even if it's free...
3130 * if the root is free, forget it, lost+found is already gone
3132 if (is_inode_free(irec
, 0) || !inode_isadir(irec
, 0)) {
3133 need_root_inode
= 1;
3137 * then process all inodes by walking incore inode tree
3142 * any directories that had updated ".." entries, rebuild them now
3144 update_missing_dotdot_entries(mp
);
3146 do_log(_(" - traversal finished ...\n"));
3147 do_log(_(" - moving disconnected inodes to %s ...\n"),
3151 * move all disconnected inodes to the orphanage
3153 for (i
= 0; i
< glob_agcount
; i
++) {
3154 irec
= findfirst_inode_rec(i
);
3155 while (irec
!= NULL
) {
3156 check_for_orphaned_inodes(mp
, i
, irec
);
3157 irec
= next_ino_rec(irec
);