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