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