18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */ 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc. 48c2ecf20Sopenharmony_ci * All Rights Reserved. 58c2ecf20Sopenharmony_ci */ 68c2ecf20Sopenharmony_ci#ifndef __XFS_DIR2_H__ 78c2ecf20Sopenharmony_ci#define __XFS_DIR2_H__ 88c2ecf20Sopenharmony_ci 98c2ecf20Sopenharmony_ci#include "xfs_da_format.h" 108c2ecf20Sopenharmony_ci#include "xfs_da_btree.h" 118c2ecf20Sopenharmony_ci 128c2ecf20Sopenharmony_cistruct xfs_da_args; 138c2ecf20Sopenharmony_cistruct xfs_inode; 148c2ecf20Sopenharmony_cistruct xfs_mount; 158c2ecf20Sopenharmony_cistruct xfs_trans; 168c2ecf20Sopenharmony_cistruct xfs_dir2_sf_hdr; 178c2ecf20Sopenharmony_cistruct xfs_dir2_sf_entry; 188c2ecf20Sopenharmony_cistruct xfs_dir2_data_hdr; 198c2ecf20Sopenharmony_cistruct xfs_dir2_data_entry; 208c2ecf20Sopenharmony_cistruct xfs_dir2_data_unused; 218c2ecf20Sopenharmony_cistruct xfs_dir3_icfree_hdr; 228c2ecf20Sopenharmony_cistruct xfs_dir3_icleaf_hdr; 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_ciextern struct xfs_name xfs_name_dotdot; 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_ci/* 278c2ecf20Sopenharmony_ci * Convert inode mode to directory entry filetype 288c2ecf20Sopenharmony_ci */ 298c2ecf20Sopenharmony_ciextern unsigned char xfs_mode_to_ftype(int mode); 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_ci/* 328c2ecf20Sopenharmony_ci * Generic directory interface routines 338c2ecf20Sopenharmony_ci */ 348c2ecf20Sopenharmony_ciextern void xfs_dir_startup(void); 358c2ecf20Sopenharmony_ciextern int xfs_da_mount(struct xfs_mount *mp); 368c2ecf20Sopenharmony_ciextern void xfs_da_unmount(struct xfs_mount *mp); 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_ciextern int xfs_dir_isempty(struct xfs_inode *dp); 398c2ecf20Sopenharmony_ciextern int xfs_dir_init(struct xfs_trans *tp, struct xfs_inode *dp, 408c2ecf20Sopenharmony_ci struct xfs_inode *pdp); 418c2ecf20Sopenharmony_ciextern int xfs_dir_createname(struct xfs_trans *tp, struct xfs_inode *dp, 428c2ecf20Sopenharmony_ci struct xfs_name *name, xfs_ino_t inum, 438c2ecf20Sopenharmony_ci xfs_extlen_t tot); 448c2ecf20Sopenharmony_ciextern int xfs_dir_lookup(struct xfs_trans *tp, struct xfs_inode *dp, 458c2ecf20Sopenharmony_ci struct xfs_name *name, xfs_ino_t *inum, 468c2ecf20Sopenharmony_ci struct xfs_name *ci_name); 478c2ecf20Sopenharmony_ciextern int xfs_dir_removename(struct xfs_trans *tp, struct xfs_inode *dp, 488c2ecf20Sopenharmony_ci struct xfs_name *name, xfs_ino_t ino, 498c2ecf20Sopenharmony_ci xfs_extlen_t tot); 508c2ecf20Sopenharmony_ciextern int xfs_dir_replace(struct xfs_trans *tp, struct xfs_inode *dp, 518c2ecf20Sopenharmony_ci struct xfs_name *name, xfs_ino_t inum, 528c2ecf20Sopenharmony_ci xfs_extlen_t tot); 538c2ecf20Sopenharmony_ciextern int xfs_dir_canenter(struct xfs_trans *tp, struct xfs_inode *dp, 548c2ecf20Sopenharmony_ci struct xfs_name *name); 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_ci/* 578c2ecf20Sopenharmony_ci * Direct call from the bmap code, bypassing the generic directory layer. 588c2ecf20Sopenharmony_ci */ 598c2ecf20Sopenharmony_ciextern int xfs_dir2_sf_to_block(struct xfs_da_args *args); 608c2ecf20Sopenharmony_ci 618c2ecf20Sopenharmony_ci/* 628c2ecf20Sopenharmony_ci * Interface routines used by userspace utilities 638c2ecf20Sopenharmony_ci */ 648c2ecf20Sopenharmony_ciextern int xfs_dir2_isblock(struct xfs_da_args *args, int *r); 658c2ecf20Sopenharmony_ciextern int xfs_dir2_isleaf(struct xfs_da_args *args, int *r); 668c2ecf20Sopenharmony_ciextern int xfs_dir2_shrink_inode(struct xfs_da_args *args, xfs_dir2_db_t db, 678c2ecf20Sopenharmony_ci struct xfs_buf *bp); 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_ciextern void xfs_dir2_data_freescan(struct xfs_mount *mp, 708c2ecf20Sopenharmony_ci struct xfs_dir2_data_hdr *hdr, int *loghead); 718c2ecf20Sopenharmony_ciextern void xfs_dir2_data_log_entry(struct xfs_da_args *args, 728c2ecf20Sopenharmony_ci struct xfs_buf *bp, struct xfs_dir2_data_entry *dep); 738c2ecf20Sopenharmony_ciextern void xfs_dir2_data_log_header(struct xfs_da_args *args, 748c2ecf20Sopenharmony_ci struct xfs_buf *bp); 758c2ecf20Sopenharmony_ciextern void xfs_dir2_data_log_unused(struct xfs_da_args *args, 768c2ecf20Sopenharmony_ci struct xfs_buf *bp, struct xfs_dir2_data_unused *dup); 778c2ecf20Sopenharmony_ciextern void xfs_dir2_data_make_free(struct xfs_da_args *args, 788c2ecf20Sopenharmony_ci struct xfs_buf *bp, xfs_dir2_data_aoff_t offset, 798c2ecf20Sopenharmony_ci xfs_dir2_data_aoff_t len, int *needlogp, int *needscanp); 808c2ecf20Sopenharmony_ciextern int xfs_dir2_data_use_free(struct xfs_da_args *args, 818c2ecf20Sopenharmony_ci struct xfs_buf *bp, struct xfs_dir2_data_unused *dup, 828c2ecf20Sopenharmony_ci xfs_dir2_data_aoff_t offset, xfs_dir2_data_aoff_t len, 838c2ecf20Sopenharmony_ci int *needlogp, int *needscanp); 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_ciextern struct xfs_dir2_data_free *xfs_dir2_data_freefind( 868c2ecf20Sopenharmony_ci struct xfs_dir2_data_hdr *hdr, struct xfs_dir2_data_free *bf, 878c2ecf20Sopenharmony_ci struct xfs_dir2_data_unused *dup); 888c2ecf20Sopenharmony_ci 898c2ecf20Sopenharmony_ciextern int xfs_dir_ino_validate(struct xfs_mount *mp, xfs_ino_t ino); 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ciextern const struct xfs_buf_ops xfs_dir3_block_buf_ops; 928c2ecf20Sopenharmony_ciextern const struct xfs_buf_ops xfs_dir3_leafn_buf_ops; 938c2ecf20Sopenharmony_ciextern const struct xfs_buf_ops xfs_dir3_leaf1_buf_ops; 948c2ecf20Sopenharmony_ciextern const struct xfs_buf_ops xfs_dir3_free_buf_ops; 958c2ecf20Sopenharmony_ciextern const struct xfs_buf_ops xfs_dir3_data_buf_ops; 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_ci/* 988c2ecf20Sopenharmony_ci * Directory offset/block conversion functions. 998c2ecf20Sopenharmony_ci * 1008c2ecf20Sopenharmony_ci * DB blocks here are logical directory block numbers, not filesystem blocks. 1018c2ecf20Sopenharmony_ci */ 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_ci/* 1048c2ecf20Sopenharmony_ci * Convert dataptr to byte in file space 1058c2ecf20Sopenharmony_ci */ 1068c2ecf20Sopenharmony_cistatic inline xfs_dir2_off_t 1078c2ecf20Sopenharmony_cixfs_dir2_dataptr_to_byte(xfs_dir2_dataptr_t dp) 1088c2ecf20Sopenharmony_ci{ 1098c2ecf20Sopenharmony_ci return (xfs_dir2_off_t)dp << XFS_DIR2_DATA_ALIGN_LOG; 1108c2ecf20Sopenharmony_ci} 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ci/* 1138c2ecf20Sopenharmony_ci * Convert byte in file space to dataptr. It had better be aligned. 1148c2ecf20Sopenharmony_ci */ 1158c2ecf20Sopenharmony_cistatic inline xfs_dir2_dataptr_t 1168c2ecf20Sopenharmony_cixfs_dir2_byte_to_dataptr(xfs_dir2_off_t by) 1178c2ecf20Sopenharmony_ci{ 1188c2ecf20Sopenharmony_ci return (xfs_dir2_dataptr_t)(by >> XFS_DIR2_DATA_ALIGN_LOG); 1198c2ecf20Sopenharmony_ci} 1208c2ecf20Sopenharmony_ci 1218c2ecf20Sopenharmony_ci/* 1228c2ecf20Sopenharmony_ci * Convert byte in space to (DB) block 1238c2ecf20Sopenharmony_ci */ 1248c2ecf20Sopenharmony_cistatic inline xfs_dir2_db_t 1258c2ecf20Sopenharmony_cixfs_dir2_byte_to_db(struct xfs_da_geometry *geo, xfs_dir2_off_t by) 1268c2ecf20Sopenharmony_ci{ 1278c2ecf20Sopenharmony_ci return (xfs_dir2_db_t)(by >> geo->blklog); 1288c2ecf20Sopenharmony_ci} 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_ci/* 1318c2ecf20Sopenharmony_ci * Convert dataptr to a block number 1328c2ecf20Sopenharmony_ci */ 1338c2ecf20Sopenharmony_cistatic inline xfs_dir2_db_t 1348c2ecf20Sopenharmony_cixfs_dir2_dataptr_to_db(struct xfs_da_geometry *geo, xfs_dir2_dataptr_t dp) 1358c2ecf20Sopenharmony_ci{ 1368c2ecf20Sopenharmony_ci return xfs_dir2_byte_to_db(geo, xfs_dir2_dataptr_to_byte(dp)); 1378c2ecf20Sopenharmony_ci} 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ci/* 1408c2ecf20Sopenharmony_ci * Convert byte in space to offset in a block 1418c2ecf20Sopenharmony_ci */ 1428c2ecf20Sopenharmony_cistatic inline xfs_dir2_data_aoff_t 1438c2ecf20Sopenharmony_cixfs_dir2_byte_to_off(struct xfs_da_geometry *geo, xfs_dir2_off_t by) 1448c2ecf20Sopenharmony_ci{ 1458c2ecf20Sopenharmony_ci return (xfs_dir2_data_aoff_t)(by & (geo->blksize - 1)); 1468c2ecf20Sopenharmony_ci} 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_ci/* 1498c2ecf20Sopenharmony_ci * Convert dataptr to a byte offset in a block 1508c2ecf20Sopenharmony_ci */ 1518c2ecf20Sopenharmony_cistatic inline xfs_dir2_data_aoff_t 1528c2ecf20Sopenharmony_cixfs_dir2_dataptr_to_off(struct xfs_da_geometry *geo, xfs_dir2_dataptr_t dp) 1538c2ecf20Sopenharmony_ci{ 1548c2ecf20Sopenharmony_ci return xfs_dir2_byte_to_off(geo, xfs_dir2_dataptr_to_byte(dp)); 1558c2ecf20Sopenharmony_ci} 1568c2ecf20Sopenharmony_ci 1578c2ecf20Sopenharmony_ci/* 1588c2ecf20Sopenharmony_ci * Convert block and offset to byte in space 1598c2ecf20Sopenharmony_ci */ 1608c2ecf20Sopenharmony_cistatic inline xfs_dir2_off_t 1618c2ecf20Sopenharmony_cixfs_dir2_db_off_to_byte(struct xfs_da_geometry *geo, xfs_dir2_db_t db, 1628c2ecf20Sopenharmony_ci xfs_dir2_data_aoff_t o) 1638c2ecf20Sopenharmony_ci{ 1648c2ecf20Sopenharmony_ci return ((xfs_dir2_off_t)db << geo->blklog) + o; 1658c2ecf20Sopenharmony_ci} 1668c2ecf20Sopenharmony_ci 1678c2ecf20Sopenharmony_ci/* 1688c2ecf20Sopenharmony_ci * Convert block (DB) to block (dablk) 1698c2ecf20Sopenharmony_ci */ 1708c2ecf20Sopenharmony_cistatic inline xfs_dablk_t 1718c2ecf20Sopenharmony_cixfs_dir2_db_to_da(struct xfs_da_geometry *geo, xfs_dir2_db_t db) 1728c2ecf20Sopenharmony_ci{ 1738c2ecf20Sopenharmony_ci return (xfs_dablk_t)(db << (geo->blklog - geo->fsblog)); 1748c2ecf20Sopenharmony_ci} 1758c2ecf20Sopenharmony_ci 1768c2ecf20Sopenharmony_ci/* 1778c2ecf20Sopenharmony_ci * Convert byte in space to (DA) block 1788c2ecf20Sopenharmony_ci */ 1798c2ecf20Sopenharmony_cistatic inline xfs_dablk_t 1808c2ecf20Sopenharmony_cixfs_dir2_byte_to_da(struct xfs_da_geometry *geo, xfs_dir2_off_t by) 1818c2ecf20Sopenharmony_ci{ 1828c2ecf20Sopenharmony_ci return xfs_dir2_db_to_da(geo, xfs_dir2_byte_to_db(geo, by)); 1838c2ecf20Sopenharmony_ci} 1848c2ecf20Sopenharmony_ci 1858c2ecf20Sopenharmony_ci/* 1868c2ecf20Sopenharmony_ci * Convert block and offset to dataptr 1878c2ecf20Sopenharmony_ci */ 1888c2ecf20Sopenharmony_cistatic inline xfs_dir2_dataptr_t 1898c2ecf20Sopenharmony_cixfs_dir2_db_off_to_dataptr(struct xfs_da_geometry *geo, xfs_dir2_db_t db, 1908c2ecf20Sopenharmony_ci xfs_dir2_data_aoff_t o) 1918c2ecf20Sopenharmony_ci{ 1928c2ecf20Sopenharmony_ci return xfs_dir2_byte_to_dataptr(xfs_dir2_db_off_to_byte(geo, db, o)); 1938c2ecf20Sopenharmony_ci} 1948c2ecf20Sopenharmony_ci 1958c2ecf20Sopenharmony_ci/* 1968c2ecf20Sopenharmony_ci * Convert block (dablk) to block (DB) 1978c2ecf20Sopenharmony_ci */ 1988c2ecf20Sopenharmony_cistatic inline xfs_dir2_db_t 1998c2ecf20Sopenharmony_cixfs_dir2_da_to_db(struct xfs_da_geometry *geo, xfs_dablk_t da) 2008c2ecf20Sopenharmony_ci{ 2018c2ecf20Sopenharmony_ci return (xfs_dir2_db_t)(da >> (geo->blklog - geo->fsblog)); 2028c2ecf20Sopenharmony_ci} 2038c2ecf20Sopenharmony_ci 2048c2ecf20Sopenharmony_ci/* 2058c2ecf20Sopenharmony_ci * Convert block (dablk) to byte offset in space 2068c2ecf20Sopenharmony_ci */ 2078c2ecf20Sopenharmony_cistatic inline xfs_dir2_off_t 2088c2ecf20Sopenharmony_cixfs_dir2_da_to_byte(struct xfs_da_geometry *geo, xfs_dablk_t da) 2098c2ecf20Sopenharmony_ci{ 2108c2ecf20Sopenharmony_ci return xfs_dir2_db_off_to_byte(geo, xfs_dir2_da_to_db(geo, da), 0); 2118c2ecf20Sopenharmony_ci} 2128c2ecf20Sopenharmony_ci 2138c2ecf20Sopenharmony_ci/* 2148c2ecf20Sopenharmony_ci * Directory tail pointer accessor functions. Based on block geometry. 2158c2ecf20Sopenharmony_ci */ 2168c2ecf20Sopenharmony_cistatic inline struct xfs_dir2_block_tail * 2178c2ecf20Sopenharmony_cixfs_dir2_block_tail_p(struct xfs_da_geometry *geo, struct xfs_dir2_data_hdr *hdr) 2188c2ecf20Sopenharmony_ci{ 2198c2ecf20Sopenharmony_ci return ((struct xfs_dir2_block_tail *) 2208c2ecf20Sopenharmony_ci ((char *)hdr + geo->blksize)) - 1; 2218c2ecf20Sopenharmony_ci} 2228c2ecf20Sopenharmony_ci 2238c2ecf20Sopenharmony_cistatic inline struct xfs_dir2_leaf_tail * 2248c2ecf20Sopenharmony_cixfs_dir2_leaf_tail_p(struct xfs_da_geometry *geo, struct xfs_dir2_leaf *lp) 2258c2ecf20Sopenharmony_ci{ 2268c2ecf20Sopenharmony_ci return (struct xfs_dir2_leaf_tail *) 2278c2ecf20Sopenharmony_ci ((char *)lp + geo->blksize - 2288c2ecf20Sopenharmony_ci sizeof(struct xfs_dir2_leaf_tail)); 2298c2ecf20Sopenharmony_ci} 2308c2ecf20Sopenharmony_ci 2318c2ecf20Sopenharmony_ci/* 2328c2ecf20Sopenharmony_ci * The Linux API doesn't pass down the total size of the buffer 2338c2ecf20Sopenharmony_ci * we read into down to the filesystem. With the filldir concept 2348c2ecf20Sopenharmony_ci * it's not needed for correct information, but the XFS dir2 leaf 2358c2ecf20Sopenharmony_ci * code wants an estimate of the buffer size to calculate it's 2368c2ecf20Sopenharmony_ci * readahead window and size the buffers used for mapping to 2378c2ecf20Sopenharmony_ci * physical blocks. 2388c2ecf20Sopenharmony_ci * 2398c2ecf20Sopenharmony_ci * Try to give it an estimate that's good enough, maybe at some 2408c2ecf20Sopenharmony_ci * point we can change the ->readdir prototype to include the 2418c2ecf20Sopenharmony_ci * buffer size. For now we use the current glibc buffer size. 2428c2ecf20Sopenharmony_ci * musl libc hardcodes 2k and dietlibc uses PAGE_SIZE. 2438c2ecf20Sopenharmony_ci */ 2448c2ecf20Sopenharmony_ci#define XFS_READDIR_BUFSIZE (32768) 2458c2ecf20Sopenharmony_ci 2468c2ecf20Sopenharmony_ciunsigned char xfs_dir3_get_dtype(struct xfs_mount *mp, uint8_t filetype); 2478c2ecf20Sopenharmony_ciunsigned int xfs_dir3_data_end_offset(struct xfs_da_geometry *geo, 2488c2ecf20Sopenharmony_ci struct xfs_dir2_data_hdr *hdr); 2498c2ecf20Sopenharmony_cibool xfs_dir2_namecheck(const void *name, size_t length); 2508c2ecf20Sopenharmony_ci 2518c2ecf20Sopenharmony_ci#endif /* __XFS_DIR2_H__ */ 252