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