projects/linux·blob master · refs

702 lines · 19.9 KiB

1/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * linux/include/linux/hfsplus_fs.h
4 *
5 * Copyright (C) 1999
6 * Brad Boyer ([email protected])
7 * (C) 2003 Ardis Technologies <[email protected]>
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
28typedef 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 */
34enum 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 */
41struct 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
68struct page;
69
70/* An HFS+ BTree node in memory */
71struct 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
109struct hfsplus_vh;
110struct hfs_btree;
111
112struct 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
174static inline struct hfsplus_sb_info *HFSPLUS_SB(struct super_block *sb)
175{
176 return sb->s_fs_info;
177}
178
179
180struct 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
235static 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 */
253static 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
260struct 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
273struct 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 */
281static 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
328typedef 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 */
337int __init hfsplus_create_attr_tree_cache(void);
338void hfsplus_destroy_attr_tree_cache(void);
339int hfsplus_attr_bin_cmp_key(const hfsplus_btree_key *k1,
340 const hfsplus_btree_key *k2);
341int hfsplus_attr_build_key(struct super_block *sb, hfsplus_btree_key *key,
342 u32 cnid, const char *name);
343hfsplus_attr_entry *hfsplus_alloc_attr_entry(void);
344void hfsplus_destroy_attr_entry(hfsplus_attr_entry *entry);
345int hfsplus_find_attr(struct super_block *sb, u32 cnid, const char *name,
346 struct hfs_find_data *fd);
347int hfsplus_attr_exists(struct inode *inode, const char *name);
348int hfsplus_create_attr(struct inode *inode, const char *name,
349 const void *value, size_t size);
350int hfsplus_delete_attr(struct inode *inode, const char *name);
351int hfsplus_delete_all_attrs(struct inode *dir, u32 cnid);
352int hfsplus_replace_attr(struct inode *inode,
353 const char *name,
354 const void *value, size_t size);
355
356/* bitmap.c */
357int hfsplus_block_allocate(struct super_block *sb, u32 size, u32 offset,
358 u32 *max);
359int hfsplus_block_free(struct super_block *sb, u32 offset, u32 count);
360
361/* btree.c */
362u32 hfsplus_calc_btree_clump_size(u32 block_size, u32 node_size, u64 sectors,
363 int file_id);
364struct hfs_btree *hfs_btree_open(struct super_block *sb, u32 id);
365void hfs_btree_close(struct hfs_btree *tree);
366int hfs_btree_write(struct hfs_btree *tree);
367int hfs_bmap_reserve(struct hfs_btree *tree, u32 rsvd_nodes);
368struct hfs_bnode *hfs_bmap_alloc(struct hfs_btree *tree);
369void hfs_bmap_free(struct hfs_bnode *node);
370
371/* bnode.c */
372void hfs_bnode_read(struct hfs_bnode *node, void *buf, u32 off, u32 len);
373u16 hfs_bnode_read_u16(struct hfs_bnode *node, u32 off);
374u8 hfs_bnode_read_u8(struct hfs_bnode *node, u32 off);
375void hfs_bnode_read_key(struct hfs_bnode *node, void *key, u32 off);
376void hfs_bnode_write(struct hfs_bnode *node, void *buf, u32 off, u32 len);
377void hfs_bnode_write_u16(struct hfs_bnode *node, u32 off, u16 data);
378void hfs_bnode_clear(struct hfs_bnode *node, u32 off, u32 len);
379void hfs_bnode_copy(struct hfs_bnode *dst_node, u32 dst,
380 struct hfs_bnode *src_node, u32 src, u32 len);
381void hfs_bnode_move(struct hfs_bnode *node, u32 dst, u32 src, u32 len);
382void hfs_bnode_dump(struct hfs_bnode *node);
383void hfs_bnode_unlink(struct hfs_bnode *node);
384struct hfs_bnode *hfs_bnode_findhash(struct hfs_btree *tree, u32 cnid);
385void hfs_bnode_unhash(struct hfs_bnode *node);
386struct hfs_bnode *hfs_bnode_find(struct hfs_btree *tree, u32 num);
387void hfs_bnode_free(struct hfs_bnode *node);
388struct hfs_bnode *hfs_bnode_create(struct hfs_btree *tree, u32 num);
389void hfs_bnode_get(struct hfs_bnode *node);
390void hfs_bnode_put(struct hfs_bnode *node);
391bool hfs_bnode_need_zeroout(struct hfs_btree *tree);
392
393/* brec.c */
394u16 hfs_brec_lenoff(struct hfs_bnode *node, u16 rec, u16 *off);
395u16 hfs_brec_keylen(struct hfs_bnode *node, u16 rec);
396int hfs_brec_insert(struct hfs_find_data *fd, void *entry, u32 entry_len);
397int hfs_brec_remove(struct hfs_find_data *fd);
398
399/* bfind.c */
400int hfs_find_init(struct hfs_btree *tree, struct hfs_find_data *fd);
401void hfs_find_exit(struct hfs_find_data *fd);
402int hfs_find_1st_rec_by_cnid(struct hfs_bnode *bnode, struct hfs_find_data *fd,
403 int *begin, int *end, int *cur_rec);
404int hfs_find_rec_by_key(struct hfs_bnode *bnode, struct hfs_find_data *fd,
405 int *begin, int *end, int *cur_rec);
406int __hfs_brec_find(struct hfs_bnode *bnode, struct hfs_find_data *fd,
407 search_strategy_t rec_found);
408int hfs_brec_find(struct hfs_find_data *fd, search_strategy_t do_key_compare);
409int hfs_brec_read(struct hfs_find_data *fd, void *rec, u32 rec_len);
410int hfs_brec_goto(struct hfs_find_data *fd, int cnt);
411
412/* catalog.c */
413int hfsplus_cat_case_cmp_key(const hfsplus_btree_key *k1,
414 const hfsplus_btree_key *k2);
415int hfsplus_cat_bin_cmp_key(const hfsplus_btree_key *k1,
416 const hfsplus_btree_key *k2);
417int hfsplus_cat_build_key(struct super_block *sb, hfsplus_btree_key *key,
418 u32 parent, const struct qstr *str);
419void hfsplus_cat_build_key_with_cnid(struct super_block *sb,
420 hfsplus_btree_key *key, u32 parent);
421void hfsplus_cat_set_perms(struct inode *inode, struct hfsplus_perm *perms);
422int hfsplus_find_cat(struct super_block *sb, u32 cnid,
423 struct hfs_find_data *fd);
424int hfsplus_create_cat(u32 cnid, struct inode *dir, const struct qstr *str,
425 struct inode *inode);
426int hfsplus_delete_cat(u32 cnid, struct inode *dir, const struct qstr *str);
427int 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 */
431extern const struct inode_operations hfsplus_dir_inode_operations;
432extern const struct file_operations hfsplus_dir_operations;
433
434/* extents.c */
435int hfsplus_ext_cmp_key(const hfsplus_btree_key *k1,
436 const hfsplus_btree_key *k2);
437int hfsplus_ext_write_extent(struct inode *inode);
438int hfsplus_get_block(struct inode *inode, sector_t iblock,
439 struct buffer_head *bh_result, int create);
440int hfsplus_free_fork(struct super_block *sb, u32 cnid,
441 struct hfsplus_fork_raw *fork, int type);
442int hfsplus_file_extend(struct inode *inode, bool zeroout);
443void hfsplus_file_truncate(struct inode *inode);
444
445/* inode.c */
446extern const struct address_space_operations hfsplus_aops;
447extern const struct address_space_operations hfsplus_btree_aops;
448extern const struct dentry_operations hfsplus_dentry_operations;
449
450int hfsplus_write_begin(const struct kiocb *iocb,
451 struct address_space *mapping,
452 loff_t pos, unsigned len, struct folio **foliop,
453 void **fsdata);
454struct inode *hfsplus_new_inode(struct super_block *sb, struct inode *dir,
455 umode_t mode);
456void hfsplus_delete_inode(struct inode *inode);
457void hfsplus_inode_read_fork(struct inode *inode,
458 struct hfsplus_fork_raw *fork);
459void hfsplus_inode_write_fork(struct inode *inode,
460 struct hfsplus_fork_raw *fork);
461int hfsplus_cat_read_inode(struct inode *inode, struct hfs_find_data *fd);
462int hfsplus_cat_write_inode(struct inode *inode);
463int hfsplus_getattr(const struct mnt_idmap *idmap, const struct path *path,
464 struct kstat *stat, u32 request_mask,
465 unsigned int query_flags);
466int hfsplus_file_fsync(struct file *file, loff_t start, loff_t end,
467 int datasync);
468int hfsplus_fileattr_get(struct dentry *dentry, struct file_kattr *fa);
469int hfsplus_fileattr_set(const struct mnt_idmap *idmap,
470 struct dentry *dentry, struct file_kattr *fa);
471
472/* ioctl.c */
473long hfsplus_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
474
475/* options.c */
476void hfsplus_fill_defaults(struct hfsplus_sb_info *opts);
477int hfsplus_parse_param(struct fs_context *fc, struct fs_parameter *param);
478int hfsplus_show_options(struct seq_file *seq, struct dentry *root);
479
480/* part_tbl.c */
481int hfs_part_find(struct super_block *sb, sector_t *part_start,
482 sector_t *part_size);
483
484/* super.c */
485struct inode *hfsplus_iget(struct super_block *sb, unsigned long ino);
486void hfsplus_mark_mdb_dirty(struct super_block *sb);
487void hfsplus_prepare_volume_header_for_commit(struct hfsplus_vh *vhdr);
488int hfsplus_commit_superblock(struct super_block *sb);
489
490/* unicode.c */
491int hfsplus_strcasecmp(const struct hfsplus_unistr *s1,
492 const struct hfsplus_unistr *s2);
493int hfsplus_strcmp(const struct hfsplus_unistr *s1,
494 const struct hfsplus_unistr *s2);
495int hfsplus_uni2asc_str(struct super_block *sb,
496 const struct hfsplus_unistr *ustr, char *astr,
497 int *len_p);
498int hfsplus_uni2asc_xattr_str(struct super_block *sb,
499 const struct hfsplus_attr_unistr *ustr,
500 char *astr, int *len_p);
501int hfsplus_asc2uni(struct super_block *sb, struct hfsplus_unistr *ustr,
502 int max_unistr_len, const char *astr, int len,
503 int name_type);
504int hfsplus_hash_dentry(const struct dentry *dentry, struct qstr *str);
505int hfsplus_compare_dentry(const struct dentry *dentry, unsigned int len,
506 const char *str, const struct qstr *name);
507
508/* wrapper.c */
509int hfsplus_submit_bio(struct super_block *sb, sector_t sector, void *buf,
510 void **data, blk_opf_t opf);
511int hfsplus_read_wrapper(struct super_block *sb);
512
513static 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
520static inline
521bool is_hfs_thread_record_type(u16 type)
522{
523 return type == HFSPLUS_FOLDER_THREAD || type == HFSPLUS_FILE_THREAD;
524}
525
526int 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
538static 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
545static inline __be32 __hfsp_ut2mt(time64_t ut)
546{
547 return cpu_to_be32(lower_32_bits(ut) + HFSPLUS_UTC_OFFSET);
548}
549
550static inline enum hfsplus_btree_mutex_classes
551hfsplus_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
571static inline
572bool 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
592static inline
593bool 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
614static inline
615bool 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
625static inline
626bool 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
652static inline
653bool 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
661static inline
662void 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
671static inline
672u32 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