^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* SPDX-License-Identifier: GPL-2.0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * linux/fs/hfs/btree.h
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2001
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Brad Boyer (flar@allandria.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * (C) 2003 Ardis Technologies <roman@ardistech.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include "hfs_fs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) typedef int (*btree_keycmp)(const btree_key *, const btree_key *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #define NODE_HASH_SIZE 256
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) /* B-tree mutex nested subclasses */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) enum hfs_btree_mutex_classes {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) CATALOG_BTREE_MUTEX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) EXTENTS_BTREE_MUTEX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) ATTR_BTREE_MUTEX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) /* A HFS BTree held in memory */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) struct hfs_btree {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) struct super_block *sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) btree_keycmp keycmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) u32 cnid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) u32 root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) u32 leaf_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) u32 leaf_head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) u32 leaf_tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) u32 node_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) u32 free_nodes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) u32 attributes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) unsigned int node_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) unsigned int node_size_shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) unsigned int max_key_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) unsigned int depth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) //unsigned int map1_size, map_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) struct mutex tree_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) unsigned int pages_per_bnode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) spinlock_t hash_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct hfs_bnode *node_hash[NODE_HASH_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) int node_hash_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) /* A HFS BTree node in memory */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) struct hfs_bnode {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) struct hfs_btree *tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) u32 prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) u32 this;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) u32 next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) u32 parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) u16 num_recs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) u8 type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) u8 height;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) struct hfs_bnode *next_hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) wait_queue_head_t lock_wq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) atomic_t refcnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) unsigned int page_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) struct page *page[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) #define HFS_BNODE_ERROR 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) #define HFS_BNODE_NEW 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) #define HFS_BNODE_DELETED 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) struct hfs_find_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) btree_key *key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) btree_key *search_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) struct hfs_btree *tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) struct hfs_bnode *bnode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) int record;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) int keyoffset, keylength;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) int entryoffset, entrylength;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) /* btree.c */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) extern struct hfs_btree *hfs_btree_open(struct super_block *, u32, btree_keycmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) extern void hfs_btree_close(struct hfs_btree *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) extern void hfs_btree_write(struct hfs_btree *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) extern int hfs_bmap_reserve(struct hfs_btree *, int);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) extern struct hfs_bnode * hfs_bmap_alloc(struct hfs_btree *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) extern void hfs_bmap_free(struct hfs_bnode *node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) /* bnode.c */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) extern void hfs_bnode_read(struct hfs_bnode *, void *, int, int);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) extern u16 hfs_bnode_read_u16(struct hfs_bnode *, int);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) extern u8 hfs_bnode_read_u8(struct hfs_bnode *, int);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) extern void hfs_bnode_read_key(struct hfs_bnode *, void *, int);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) extern void hfs_bnode_write(struct hfs_bnode *, void *, int, int);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) extern void hfs_bnode_write_u16(struct hfs_bnode *, int, u16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) extern void hfs_bnode_write_u8(struct hfs_bnode *, int, u8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) extern void hfs_bnode_clear(struct hfs_bnode *, int, int);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) extern void hfs_bnode_copy(struct hfs_bnode *, int,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) struct hfs_bnode *, int, int);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) extern void hfs_bnode_move(struct hfs_bnode *, int, int, int);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) extern void hfs_bnode_dump(struct hfs_bnode *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) extern void hfs_bnode_unlink(struct hfs_bnode *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) extern struct hfs_bnode *hfs_bnode_findhash(struct hfs_btree *, u32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) extern struct hfs_bnode *hfs_bnode_find(struct hfs_btree *, u32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) extern void hfs_bnode_unhash(struct hfs_bnode *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) extern void hfs_bnode_free(struct hfs_bnode *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) extern struct hfs_bnode *hfs_bnode_create(struct hfs_btree *, u32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) extern void hfs_bnode_get(struct hfs_bnode *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) extern void hfs_bnode_put(struct hfs_bnode *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) /* brec.c */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) extern u16 hfs_brec_lenoff(struct hfs_bnode *, u16, u16 *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) extern u16 hfs_brec_keylen(struct hfs_bnode *, u16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) extern int hfs_brec_insert(struct hfs_find_data *, void *, int);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) extern int hfs_brec_remove(struct hfs_find_data *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) /* bfind.c */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) extern int hfs_find_init(struct hfs_btree *, struct hfs_find_data *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) extern void hfs_find_exit(struct hfs_find_data *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) extern int __hfs_brec_find(struct hfs_bnode *, struct hfs_find_data *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) extern int hfs_brec_find(struct hfs_find_data *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) extern int hfs_brec_read(struct hfs_find_data *, void *, int);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) extern int hfs_brec_goto(struct hfs_find_data *, int);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) struct hfs_bnode_desc {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) __be32 next; /* (V) Number of the next node at this level */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) __be32 prev; /* (V) Number of the prev node at this level */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) u8 type; /* (F) The type of node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) u8 height; /* (F) The level of this node (leaves=1) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) __be16 num_recs; /* (V) The number of records in this node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) u16 reserved;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) } __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) #define HFS_NODE_INDEX 0x00 /* An internal (index) node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) #define HFS_NODE_HEADER 0x01 /* The tree header node (node 0) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) #define HFS_NODE_MAP 0x02 /* Holds part of the bitmap of used nodes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) #define HFS_NODE_LEAF 0xFF /* A leaf (ndNHeight==1) node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) struct hfs_btree_header_rec {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) __be16 depth; /* (V) The number of levels in this B-tree */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) __be32 root; /* (V) The node number of the root node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) __be32 leaf_count; /* (V) The number of leaf records */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) __be32 leaf_head; /* (V) The number of the first leaf node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) __be32 leaf_tail; /* (V) The number of the last leaf node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) __be16 node_size; /* (F) The number of bytes in a node (=512) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) __be16 max_key_len; /* (F) The length of a key in an index node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) __be32 node_count; /* (V) The total number of nodes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) __be32 free_nodes; /* (V) The number of unused nodes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) u16 reserved1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) __be32 clump_size; /* (F) clump size. not usually used. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) u8 btree_type; /* (F) BTree type */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) u8 reserved2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) __be32 attributes; /* (F) attributes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) u32 reserved3[16];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) } __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) #define BTREE_ATTR_BADCLOSE 0x00000001 /* b-tree not closed properly. not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) used by hfsplus. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) #define HFS_TREE_BIGKEYS 0x00000002 /* key length is u16 instead of u8.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) used by hfsplus. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) #define HFS_TREE_VARIDXKEYS 0x00000004 /* variable key length instead of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) max key length. use din catalog
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) b-tree but not in extents
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) b-tree (hfsplus). */