at master 18 kB view raw
1/* SPDX-License-Identifier: GPL-2.0 */ 2/* 3 * linux/include/linux/hfsplus_fs.h 4 * 5 * Copyright (C) 1999 6 * Brad Boyer (flar@pants.nu) 7 * (C) 2003 Ardis Technologies <roman@ardistech.com> 8 * 9 */ 10 11#ifndef _LINUX_HFSPLUS_FS_H 12#define _LINUX_HFSPLUS_FS_H 13 14#include <linux/fs.h> 15#include <linux/mutex.h> 16#include <linux/buffer_head.h> 17#include <linux/blkdev.h> 18#include <linux/fs_context.h> 19#include "hfsplus_raw.h" 20 21/* Runtime config options */ 22#define HFSPLUS_DEF_CR_TYPE 0x3F3F3F3F /* '????' */ 23 24#define HFSPLUS_TYPE_DATA 0x00 25#define HFSPLUS_TYPE_RSRC 0xFF 26 27typedef int (*btree_keycmp)(const hfsplus_btree_key *, 28 const hfsplus_btree_key *); 29 30#define NODE_HASH_SIZE 256 31 32/* B-tree mutex nested subclasses */ 33enum hfsplus_btree_mutex_classes { 34 CATALOG_BTREE_MUTEX, 35 EXTENTS_BTREE_MUTEX, 36 ATTR_BTREE_MUTEX, 37}; 38 39/* An HFS+ BTree held in memory */ 40struct hfs_btree { 41 struct super_block *sb; 42 struct inode *inode; 43 btree_keycmp keycmp; 44 45 u32 cnid; 46 u32 root; 47 u32 leaf_count; 48 u32 leaf_head; 49 u32 leaf_tail; 50 u32 node_count; 51 u32 free_nodes; 52 u32 attributes; 53 54 unsigned int node_size; 55 unsigned int node_size_shift; 56 unsigned int max_key_len; 57 unsigned int depth; 58 59 struct mutex tree_lock; 60 61 unsigned int pages_per_bnode; 62 spinlock_t hash_lock; 63 struct hfs_bnode *node_hash[NODE_HASH_SIZE]; 64 int node_hash_cnt; 65}; 66 67struct page; 68 69/* An HFS+ BTree node in memory */ 70struct hfs_bnode { 71 struct hfs_btree *tree; 72 73 u32 prev; 74 u32 this; 75 u32 next; 76 u32 parent; 77 78 u16 num_recs; 79 u8 type; 80 u8 height; 81 82 struct hfs_bnode *next_hash; 83 unsigned long flags; 84 wait_queue_head_t lock_wq; 85 atomic_t refcnt; 86 unsigned int page_offset; 87 struct page *page[]; 88}; 89 90#define HFS_BNODE_LOCK 0 91#define HFS_BNODE_ERROR 1 92#define HFS_BNODE_NEW 2 93#define HFS_BNODE_DIRTY 3 94#define HFS_BNODE_DELETED 4 95 96/* 97 * Attributes file states 98 */ 99#define HFSPLUS_EMPTY_ATTR_TREE 0 100#define HFSPLUS_CREATING_ATTR_TREE 1 101#define HFSPLUS_VALID_ATTR_TREE 2 102#define HFSPLUS_FAILED_ATTR_TREE 3 103 104/* 105 * HFS+ superblock info (built from Volume Header on disk) 106 */ 107 108struct hfsplus_vh; 109struct hfs_btree; 110 111struct hfsplus_sb_info { 112 void *s_vhdr_buf; 113 struct hfsplus_vh *s_vhdr; 114 void *s_backup_vhdr_buf; 115 struct hfsplus_vh *s_backup_vhdr; 116 struct hfs_btree *ext_tree; 117 struct hfs_btree *cat_tree; 118 struct hfs_btree *attr_tree; 119 atomic_t attr_tree_state; 120 struct inode *alloc_file; 121 struct inode *hidden_dir; 122 struct nls_table *nls; 123 124 /* Runtime variables */ 125 u32 blockoffset; 126 u32 min_io_size; 127 sector_t part_start; 128 sector_t sect_count; 129 int fs_shift; 130 131 /* immutable data from the volume header */ 132 u32 alloc_blksz; 133 int alloc_blksz_shift; 134 u32 total_blocks; 135 u32 data_clump_blocks, rsrc_clump_blocks; 136 137 /* mutable data from the volume header, protected by alloc_mutex */ 138 u32 free_blocks; 139 struct mutex alloc_mutex; 140 141 /* mutable data from the volume header, protected by vh_mutex */ 142 u32 next_cnid; 143 u32 file_count; 144 u32 folder_count; 145 struct mutex vh_mutex; 146 147 /* Config options */ 148 u32 creator; 149 u32 type; 150 151 umode_t umask; 152 kuid_t uid; 153 kgid_t gid; 154 155 int part, session; 156 unsigned long flags; 157 158 int work_queued; /* non-zero delayed work is queued */ 159 struct delayed_work sync_work; /* FS sync delayed work */ 160 spinlock_t work_lock; /* protects sync_work and work_queued */ 161 struct rcu_head rcu; 162}; 163 164#define HFSPLUS_SB_WRITEBACKUP 0 165#define HFSPLUS_SB_NODECOMPOSE 1 166#define HFSPLUS_SB_FORCE 2 167#define HFSPLUS_SB_HFSX 3 168#define HFSPLUS_SB_CASEFOLD 4 169#define HFSPLUS_SB_NOBARRIER 5 170#define HFSPLUS_SB_UID 6 171#define HFSPLUS_SB_GID 7 172 173static inline struct hfsplus_sb_info *HFSPLUS_SB(struct super_block *sb) 174{ 175 return sb->s_fs_info; 176} 177 178 179struct hfsplus_inode_info { 180 atomic_t opencnt; 181 182 /* 183 * Extent allocation information, protected by extents_lock. 184 */ 185 u32 first_blocks; 186 u32 clump_blocks; 187 u32 alloc_blocks; 188 u32 cached_start; 189 u32 cached_blocks; 190 hfsplus_extent_rec first_extents; 191 hfsplus_extent_rec cached_extents; 192 unsigned int extent_state; 193 struct mutex extents_lock; 194 195 /* 196 * Immutable data. 197 */ 198 struct inode *rsrc_inode; 199 __be32 create_date; 200 201 /* 202 * Protected by sbi->vh_mutex. 203 */ 204 u32 linkid; 205 206 /* 207 * Accessed using atomic bitops. 208 */ 209 unsigned long flags; 210 211 /* 212 * Protected by i_mutex. 213 */ 214 sector_t fs_blocks; 215 u8 userflags; /* BSD user file flags */ 216 u32 subfolders; /* Subfolder count (HFSX only) */ 217 struct list_head open_dir_list; 218 spinlock_t open_dir_lock; 219 loff_t phys_size; 220 221 struct inode vfs_inode; 222}; 223 224#define HFSPLUS_EXT_DIRTY 0x0001 225#define HFSPLUS_EXT_NEW 0x0002 226 227#define HFSPLUS_I_RSRC 0 /* represents a resource fork */ 228#define HFSPLUS_I_CAT_DIRTY 1 /* has changes in the catalog tree */ 229#define HFSPLUS_I_EXT_DIRTY 2 /* has changes in the extent tree */ 230#define HFSPLUS_I_ALLOC_DIRTY 3 /* has changes in the allocation file */ 231#define HFSPLUS_I_ATTR_DIRTY 4 /* has changes in the attributes tree */ 232 233#define HFSPLUS_IS_RSRC(inode) \ 234 test_bit(HFSPLUS_I_RSRC, &HFSPLUS_I(inode)->flags) 235 236static inline struct hfsplus_inode_info *HFSPLUS_I(struct inode *inode) 237{ 238 return container_of(inode, struct hfsplus_inode_info, vfs_inode); 239} 240 241/* 242 * Mark an inode dirty, and also mark the btree in which the 243 * specific type of metadata is stored. 244 * For data or metadata that gets written back by into the catalog btree 245 * by hfsplus_write_inode a plain mark_inode_dirty call is enough. 246 */ 247static inline void hfsplus_mark_inode_dirty(struct inode *inode, 248 unsigned int flag) 249{ 250 set_bit(flag, &HFSPLUS_I(inode)->flags); 251 mark_inode_dirty(inode); 252} 253 254struct hfs_find_data { 255 /* filled by caller */ 256 hfsplus_btree_key *search_key; 257 hfsplus_btree_key *key; 258 /* filled by find */ 259 struct hfs_btree *tree; 260 struct hfs_bnode *bnode; 261 /* filled by findrec */ 262 int record; 263 int keyoffset, keylength; 264 int entryoffset, entrylength; 265}; 266 267struct hfsplus_readdir_data { 268 struct list_head list; 269 struct file *file; 270 struct hfsplus_cat_key key; 271}; 272 273/* 274 * Find minimum acceptible I/O size for an hfsplus sb. 275 */ 276static inline unsigned short hfsplus_min_io_size(struct super_block *sb) 277{ 278 return max_t(unsigned short, HFSPLUS_SB(sb)->min_io_size, 279 HFSPLUS_SECTOR_SIZE); 280} 281 282#define hfs_btree_open hfsplus_btree_open 283#define hfs_btree_close hfsplus_btree_close 284#define hfs_btree_write hfsplus_btree_write 285#define hfs_bmap_reserve hfsplus_bmap_reserve 286#define hfs_bmap_alloc hfsplus_bmap_alloc 287#define hfs_bmap_free hfsplus_bmap_free 288#define hfs_bnode_read hfsplus_bnode_read 289#define hfs_bnode_read_u16 hfsplus_bnode_read_u16 290#define hfs_bnode_read_u8 hfsplus_bnode_read_u8 291#define hfs_bnode_read_key hfsplus_bnode_read_key 292#define hfs_bnode_write hfsplus_bnode_write 293#define hfs_bnode_write_u16 hfsplus_bnode_write_u16 294#define hfs_bnode_clear hfsplus_bnode_clear 295#define hfs_bnode_copy hfsplus_bnode_copy 296#define hfs_bnode_move hfsplus_bnode_move 297#define hfs_bnode_dump hfsplus_bnode_dump 298#define hfs_bnode_unlink hfsplus_bnode_unlink 299#define hfs_bnode_findhash hfsplus_bnode_findhash 300#define hfs_bnode_find hfsplus_bnode_find 301#define hfs_bnode_unhash hfsplus_bnode_unhash 302#define hfs_bnode_free hfsplus_bnode_free 303#define hfs_bnode_create hfsplus_bnode_create 304#define hfs_bnode_get hfsplus_bnode_get 305#define hfs_bnode_put hfsplus_bnode_put 306#define hfs_brec_lenoff hfsplus_brec_lenoff 307#define hfs_brec_keylen hfsplus_brec_keylen 308#define hfs_brec_insert hfsplus_brec_insert 309#define hfs_brec_remove hfsplus_brec_remove 310#define hfs_find_init hfsplus_find_init 311#define hfs_find_exit hfsplus_find_exit 312#define __hfs_brec_find __hfsplus_brec_find 313#define hfs_brec_find hfsplus_brec_find 314#define hfs_brec_read hfsplus_brec_read 315#define hfs_brec_goto hfsplus_brec_goto 316#define hfs_part_find hfsplus_part_find 317 318/* 319 * hfs+-specific ioctl for making the filesystem bootable 320 */ 321#define HFSPLUS_IOC_BLESS _IO('h', 0x80) 322 323typedef int (*search_strategy_t)(struct hfs_bnode *, 324 struct hfs_find_data *, 325 int *, int *, int *); 326 327/* 328 * Functions in any *.c used in other files 329 */ 330 331/* attributes.c */ 332int __init hfsplus_create_attr_tree_cache(void); 333void hfsplus_destroy_attr_tree_cache(void); 334int hfsplus_attr_bin_cmp_key(const hfsplus_btree_key *k1, 335 const hfsplus_btree_key *k2); 336int hfsplus_attr_build_key(struct super_block *sb, hfsplus_btree_key *key, 337 u32 cnid, const char *name); 338hfsplus_attr_entry *hfsplus_alloc_attr_entry(void); 339void hfsplus_destroy_attr_entry(hfsplus_attr_entry *entry); 340int hfsplus_find_attr(struct super_block *sb, u32 cnid, const char *name, 341 struct hfs_find_data *fd); 342int hfsplus_attr_exists(struct inode *inode, const char *name); 343int hfsplus_create_attr(struct inode *inode, const char *name, 344 const void *value, size_t size); 345int hfsplus_delete_attr(struct inode *inode, const char *name); 346int hfsplus_delete_all_attrs(struct inode *dir, u32 cnid); 347 348/* bitmap.c */ 349int hfsplus_block_allocate(struct super_block *sb, u32 size, u32 offset, 350 u32 *max); 351int hfsplus_block_free(struct super_block *sb, u32 offset, u32 count); 352 353/* btree.c */ 354u32 hfsplus_calc_btree_clump_size(u32 block_size, u32 node_size, u64 sectors, 355 int file_id); 356struct hfs_btree *hfs_btree_open(struct super_block *sb, u32 id); 357void hfs_btree_close(struct hfs_btree *tree); 358int hfs_btree_write(struct hfs_btree *tree); 359int hfs_bmap_reserve(struct hfs_btree *tree, u32 rsvd_nodes); 360struct hfs_bnode *hfs_bmap_alloc(struct hfs_btree *tree); 361void hfs_bmap_free(struct hfs_bnode *node); 362 363/* bnode.c */ 364void hfs_bnode_read(struct hfs_bnode *node, void *buf, u32 off, u32 len); 365u16 hfs_bnode_read_u16(struct hfs_bnode *node, u32 off); 366u8 hfs_bnode_read_u8(struct hfs_bnode *node, u32 off); 367void hfs_bnode_read_key(struct hfs_bnode *node, void *key, u32 off); 368void hfs_bnode_write(struct hfs_bnode *node, void *buf, u32 off, u32 len); 369void hfs_bnode_write_u16(struct hfs_bnode *node, u32 off, u16 data); 370void hfs_bnode_clear(struct hfs_bnode *node, u32 off, u32 len); 371void hfs_bnode_copy(struct hfs_bnode *dst_node, u32 dst, 372 struct hfs_bnode *src_node, u32 src, u32 len); 373void hfs_bnode_move(struct hfs_bnode *node, u32 dst, u32 src, u32 len); 374void hfs_bnode_dump(struct hfs_bnode *node); 375void hfs_bnode_unlink(struct hfs_bnode *node); 376struct hfs_bnode *hfs_bnode_findhash(struct hfs_btree *tree, u32 cnid); 377void hfs_bnode_unhash(struct hfs_bnode *node); 378struct hfs_bnode *hfs_bnode_find(struct hfs_btree *tree, u32 num); 379void hfs_bnode_free(struct hfs_bnode *node); 380struct hfs_bnode *hfs_bnode_create(struct hfs_btree *tree, u32 num); 381void hfs_bnode_get(struct hfs_bnode *node); 382void hfs_bnode_put(struct hfs_bnode *node); 383bool hfs_bnode_need_zeroout(struct hfs_btree *tree); 384 385/* brec.c */ 386u16 hfs_brec_lenoff(struct hfs_bnode *node, u16 rec, u16 *off); 387u16 hfs_brec_keylen(struct hfs_bnode *node, u16 rec); 388int hfs_brec_insert(struct hfs_find_data *fd, void *entry, u32 entry_len); 389int hfs_brec_remove(struct hfs_find_data *fd); 390 391/* bfind.c */ 392int hfs_find_init(struct hfs_btree *tree, struct hfs_find_data *fd); 393void hfs_find_exit(struct hfs_find_data *fd); 394int hfs_find_1st_rec_by_cnid(struct hfs_bnode *bnode, struct hfs_find_data *fd, 395 int *begin, int *end, int *cur_rec); 396int hfs_find_rec_by_key(struct hfs_bnode *bnode, struct hfs_find_data *fd, 397 int *begin, int *end, int *cur_rec); 398int __hfs_brec_find(struct hfs_bnode *bnode, struct hfs_find_data *fd, 399 search_strategy_t rec_found); 400int hfs_brec_find(struct hfs_find_data *fd, search_strategy_t do_key_compare); 401int hfs_brec_read(struct hfs_find_data *fd, void *rec, u32 rec_len); 402int hfs_brec_goto(struct hfs_find_data *fd, int cnt); 403 404/* catalog.c */ 405int hfsplus_cat_case_cmp_key(const hfsplus_btree_key *k1, 406 const hfsplus_btree_key *k2); 407int hfsplus_cat_bin_cmp_key(const hfsplus_btree_key *k1, 408 const hfsplus_btree_key *k2); 409int hfsplus_cat_build_key(struct super_block *sb, hfsplus_btree_key *key, 410 u32 parent, const struct qstr *str); 411void hfsplus_cat_build_key_with_cnid(struct super_block *sb, 412 hfsplus_btree_key *key, u32 parent); 413void hfsplus_cat_set_perms(struct inode *inode, struct hfsplus_perm *perms); 414int hfsplus_find_cat(struct super_block *sb, u32 cnid, 415 struct hfs_find_data *fd); 416int hfsplus_create_cat(u32 cnid, struct inode *dir, const struct qstr *str, 417 struct inode *inode); 418int hfsplus_delete_cat(u32 cnid, struct inode *dir, const struct qstr *str); 419int hfsplus_rename_cat(u32 cnid, struct inode *src_dir, const struct qstr *src_name, 420 struct inode *dst_dir, const struct qstr *dst_name); 421 422/* dir.c */ 423extern const struct inode_operations hfsplus_dir_inode_operations; 424extern const struct file_operations hfsplus_dir_operations; 425 426/* extents.c */ 427int hfsplus_ext_cmp_key(const hfsplus_btree_key *k1, 428 const hfsplus_btree_key *k2); 429int hfsplus_ext_write_extent(struct inode *inode); 430int hfsplus_get_block(struct inode *inode, sector_t iblock, 431 struct buffer_head *bh_result, int create); 432int hfsplus_free_fork(struct super_block *sb, u32 cnid, 433 struct hfsplus_fork_raw *fork, int type); 434int hfsplus_file_extend(struct inode *inode, bool zeroout); 435void hfsplus_file_truncate(struct inode *inode); 436 437/* inode.c */ 438extern const struct address_space_operations hfsplus_aops; 439extern const struct address_space_operations hfsplus_btree_aops; 440extern const struct dentry_operations hfsplus_dentry_operations; 441 442int hfsplus_write_begin(const struct kiocb *iocb, 443 struct address_space *mapping, 444 loff_t pos, unsigned len, struct folio **foliop, 445 void **fsdata); 446struct inode *hfsplus_new_inode(struct super_block *sb, struct inode *dir, 447 umode_t mode); 448void hfsplus_delete_inode(struct inode *inode); 449void hfsplus_inode_read_fork(struct inode *inode, 450 struct hfsplus_fork_raw *fork); 451void hfsplus_inode_write_fork(struct inode *inode, 452 struct hfsplus_fork_raw *fork); 453int hfsplus_cat_read_inode(struct inode *inode, struct hfs_find_data *fd); 454int hfsplus_cat_write_inode(struct inode *inode); 455int hfsplus_getattr(struct mnt_idmap *idmap, const struct path *path, 456 struct kstat *stat, u32 request_mask, 457 unsigned int query_flags); 458int hfsplus_file_fsync(struct file *file, loff_t start, loff_t end, 459 int datasync); 460int hfsplus_fileattr_get(struct dentry *dentry, struct file_kattr *fa); 461int hfsplus_fileattr_set(struct mnt_idmap *idmap, 462 struct dentry *dentry, struct file_kattr *fa); 463 464/* ioctl.c */ 465long hfsplus_ioctl(struct file *filp, unsigned int cmd, unsigned long arg); 466 467/* options.c */ 468void hfsplus_fill_defaults(struct hfsplus_sb_info *opts); 469int hfsplus_parse_param(struct fs_context *fc, struct fs_parameter *param); 470int hfsplus_show_options(struct seq_file *seq, struct dentry *root); 471 472/* part_tbl.c */ 473int hfs_part_find(struct super_block *sb, sector_t *part_start, 474 sector_t *part_size); 475 476/* super.c */ 477struct inode *hfsplus_iget(struct super_block *sb, unsigned long ino); 478void hfsplus_mark_mdb_dirty(struct super_block *sb); 479void hfsplus_prepare_volume_header_for_commit(struct hfsplus_vh *vhdr); 480int hfsplus_commit_superblock(struct super_block *sb); 481 482/* tables.c */ 483extern u16 hfsplus_case_fold_table[]; 484extern u16 hfsplus_decompose_table[]; 485extern u16 hfsplus_compose_table[]; 486 487/* unicode.c */ 488int hfsplus_strcasecmp(const struct hfsplus_unistr *s1, 489 const struct hfsplus_unistr *s2); 490int hfsplus_strcmp(const struct hfsplus_unistr *s1, 491 const struct hfsplus_unistr *s2); 492int hfsplus_uni2asc_str(struct super_block *sb, 493 const struct hfsplus_unistr *ustr, char *astr, 494 int *len_p); 495int hfsplus_uni2asc_xattr_str(struct super_block *sb, 496 const struct hfsplus_attr_unistr *ustr, 497 char *astr, int *len_p); 498int hfsplus_asc2uni(struct super_block *sb, struct hfsplus_unistr *ustr, 499 int max_unistr_len, const char *astr, int len); 500int hfsplus_hash_dentry(const struct dentry *dentry, struct qstr *str); 501int hfsplus_compare_dentry(const struct dentry *dentry, unsigned int len, 502 const char *str, const struct qstr *name); 503 504/* wrapper.c */ 505int hfsplus_submit_bio(struct super_block *sb, sector_t sector, void *buf, 506 void **data, blk_opf_t opf); 507int hfsplus_read_wrapper(struct super_block *sb); 508 509/* 510 * time helpers: convert between 1904-base and 1970-base timestamps 511 * 512 * HFS+ implementations are highly inconsistent, this one matches the 513 * traditional behavior of 64-bit Linux, giving the most useful 514 * time range between 1970 and 2106, by treating any on-disk timestamp 515 * under HFSPLUS_UTC_OFFSET (Jan 1 1970) as a time between 2040 and 2106. 516 */ 517#define HFSPLUS_UTC_OFFSET 2082844800U 518 519static inline time64_t __hfsp_mt2ut(__be32 mt) 520{ 521 time64_t ut = (u32)(be32_to_cpu(mt) - HFSPLUS_UTC_OFFSET); 522 523 return ut; 524} 525 526static inline __be32 __hfsp_ut2mt(time64_t ut) 527{ 528 return cpu_to_be32(lower_32_bits(ut) + HFSPLUS_UTC_OFFSET); 529} 530 531static inline enum hfsplus_btree_mutex_classes 532hfsplus_btree_lock_class(struct hfs_btree *tree) 533{ 534 enum hfsplus_btree_mutex_classes class; 535 536 switch (tree->cnid) { 537 case HFSPLUS_CAT_CNID: 538 class = CATALOG_BTREE_MUTEX; 539 break; 540 case HFSPLUS_EXT_CNID: 541 class = EXTENTS_BTREE_MUTEX; 542 break; 543 case HFSPLUS_ATTR_CNID: 544 class = ATTR_BTREE_MUTEX; 545 break; 546 default: 547 BUG(); 548 } 549 return class; 550} 551 552static inline 553bool is_bnode_offset_valid(struct hfs_bnode *node, u32 off) 554{ 555 bool is_valid = off < node->tree->node_size; 556 557 if (!is_valid) { 558 pr_err("requested invalid offset: " 559 "NODE: id %u, type %#x, height %u, " 560 "node_size %u, offset %u\n", 561 node->this, node->type, node->height, 562 node->tree->node_size, off); 563 } 564 565 return is_valid; 566} 567 568static inline 569u32 check_and_correct_requested_length(struct hfs_bnode *node, u32 off, u32 len) 570{ 571 unsigned int node_size; 572 573 if (!is_bnode_offset_valid(node, off)) 574 return 0; 575 576 node_size = node->tree->node_size; 577 578 if ((off + len) > node_size) { 579 u32 new_len = node_size - off; 580 581 pr_err("requested length has been corrected: " 582 "NODE: id %u, type %#x, height %u, " 583 "node_size %u, offset %u, " 584 "requested_len %u, corrected_len %u\n", 585 node->this, node->type, node->height, 586 node->tree->node_size, off, len, new_len); 587 588 return new_len; 589 } 590 591 return len; 592} 593 594/* compatibility */ 595#define hfsp_mt2ut(t) (struct timespec64){ .tv_sec = __hfsp_mt2ut(t) } 596#define hfsp_ut2mt(t) __hfsp_ut2mt((t).tv_sec) 597#define hfsp_now2mt() __hfsp_ut2mt(ktime_get_real_seconds()) 598 599#endif