xref: /linux/fs/hfsplus/hfsplus_fs.h (revision 4d9981429aa61c31e67371ac09e7dbba6b59de14) !
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 
27 typedef 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 */
33 enum 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 */
40 struct 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 
67 struct page;
68 
69 /* An HFS+ BTree node in memory */
70 struct 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 
108 struct hfsplus_vh;
109 struct hfs_btree;
110 
111 struct 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 
HFSPLUS_SB(struct super_block * sb)173 static inline struct hfsplus_sb_info *HFSPLUS_SB(struct super_block *sb)
174 {
175 	return sb->s_fs_info;
176 }
177 
178 
179 struct 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 
HFSPLUS_I(struct inode * inode)236 static 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 #define HFSPLUS_CAT_TREE_I(sb) \
242 	HFSPLUS_SB(sb)->cat_tree->inode
243 #define HFSPLUS_EXT_TREE_I(sb) \
244 	HFSPLUS_SB(sb)->ext_tree->inode
245 #define HFSPLUS_ATTR_TREE_I(sb) \
246 	HFSPLUS_SB(sb)->attr_tree->inode
247 
248 /*
249  * Mark an inode dirty, and also mark the btree in which the
250  * specific type of metadata is stored.
251  * For data or metadata that gets written back by into the catalog btree
252  * by hfsplus_write_inode a plain mark_inode_dirty call is enough.
253  */
hfsplus_mark_inode_dirty(struct inode * inode,unsigned int flag)254 static inline void hfsplus_mark_inode_dirty(struct inode *inode,
255 		unsigned int flag)
256 {
257 	set_bit(flag, &HFSPLUS_I(inode)->flags);
258 	mark_inode_dirty(inode);
259 }
260 
261 struct hfs_find_data {
262 	/* filled by caller */
263 	hfsplus_btree_key *search_key;
264 	hfsplus_btree_key *key;
265 	/* filled by find */
266 	struct hfs_btree *tree;
267 	struct hfs_bnode *bnode;
268 	/* filled by findrec */
269 	int record;
270 	int keyoffset, keylength;
271 	int entryoffset, entrylength;
272 };
273 
274 struct hfsplus_readdir_data {
275 	struct list_head list;
276 	struct file *file;
277 	struct hfsplus_cat_key key;
278 };
279 
280 /*
281  * Find minimum acceptible I/O size for an hfsplus sb.
282  */
hfsplus_min_io_size(struct super_block * sb)283 static inline unsigned short hfsplus_min_io_size(struct super_block *sb)
284 {
285 	return max_t(unsigned short, HFSPLUS_SB(sb)->min_io_size,
286 		     HFSPLUS_SECTOR_SIZE);
287 }
288 
289 #define hfs_btree_open hfsplus_btree_open
290 #define hfs_btree_close hfsplus_btree_close
291 #define hfs_btree_write hfsplus_btree_write
292 #define hfs_bmap_reserve hfsplus_bmap_reserve
293 #define hfs_bmap_alloc hfsplus_bmap_alloc
294 #define hfs_bmap_free hfsplus_bmap_free
295 #define hfs_bnode_read hfsplus_bnode_read
296 #define hfs_bnode_read_u16 hfsplus_bnode_read_u16
297 #define hfs_bnode_read_u8 hfsplus_bnode_read_u8
298 #define hfs_bnode_read_key hfsplus_bnode_read_key
299 #define hfs_bnode_write hfsplus_bnode_write
300 #define hfs_bnode_write_u16 hfsplus_bnode_write_u16
301 #define hfs_bnode_clear hfsplus_bnode_clear
302 #define hfs_bnode_copy hfsplus_bnode_copy
303 #define hfs_bnode_move hfsplus_bnode_move
304 #define hfs_bnode_dump hfsplus_bnode_dump
305 #define hfs_bnode_unlink hfsplus_bnode_unlink
306 #define hfs_bnode_findhash hfsplus_bnode_findhash
307 #define hfs_bnode_find hfsplus_bnode_find
308 #define hfs_bnode_unhash hfsplus_bnode_unhash
309 #define hfs_bnode_free hfsplus_bnode_free
310 #define hfs_bnode_create hfsplus_bnode_create
311 #define hfs_bnode_get hfsplus_bnode_get
312 #define hfs_bnode_put hfsplus_bnode_put
313 #define hfs_brec_lenoff hfsplus_brec_lenoff
314 #define hfs_brec_keylen hfsplus_brec_keylen
315 #define hfs_brec_insert hfsplus_brec_insert
316 #define hfs_brec_remove hfsplus_brec_remove
317 #define hfs_find_init hfsplus_find_init
318 #define hfs_find_exit hfsplus_find_exit
319 #define __hfs_brec_find __hfsplus_brec_find
320 #define hfs_brec_find hfsplus_brec_find
321 #define hfs_brec_read hfsplus_brec_read
322 #define hfs_brec_goto hfsplus_brec_goto
323 #define hfs_part_find hfsplus_part_find
324 
325 /*
326  * hfs+-specific ioctl for making the filesystem bootable
327  */
328 #define HFSPLUS_IOC_BLESS _IO('h', 0x80)
329 
330 typedef int (*search_strategy_t)(struct hfs_bnode *,
331 				struct hfs_find_data *,
332 				int *, int *, int *);
333 
334 /*
335  * Functions in any *.c used in other files
336  */
337 
338 /* attributes.c */
339 int __init hfsplus_create_attr_tree_cache(void);
340 void hfsplus_destroy_attr_tree_cache(void);
341 int hfsplus_attr_bin_cmp_key(const hfsplus_btree_key *k1,
342 			     const hfsplus_btree_key *k2);
343 int hfsplus_attr_build_key(struct super_block *sb, hfsplus_btree_key *key,
344 			   u32 cnid, const char *name);
345 hfsplus_attr_entry *hfsplus_alloc_attr_entry(void);
346 void hfsplus_destroy_attr_entry(hfsplus_attr_entry *entry);
347 int hfsplus_find_attr(struct super_block *sb, u32 cnid, const char *name,
348 		      struct hfs_find_data *fd);
349 int hfsplus_attr_exists(struct inode *inode, const char *name);
350 int hfsplus_create_attr(struct inode *inode, const char *name,
351 			const void *value, size_t size);
352 int hfsplus_delete_attr(struct inode *inode, const char *name);
353 int hfsplus_delete_all_attrs(struct inode *dir, u32 cnid);
354 int hfsplus_replace_attr(struct inode *inode,
355 			 const char *name,
356 			 const void *value, size_t size);
357 
358 /* bitmap.c */
359 int hfsplus_block_allocate(struct super_block *sb, u32 size, u32 offset,
360 			   u32 *max);
361 int hfsplus_block_free(struct super_block *sb, u32 offset, u32 count);
362 
363 /* btree.c */
364 u32 hfsplus_calc_btree_clump_size(u32 block_size, u32 node_size, u64 sectors,
365 				  int file_id);
366 struct hfs_btree *hfs_btree_open(struct super_block *sb, u32 id);
367 void hfs_btree_close(struct hfs_btree *tree);
368 int hfs_btree_write(struct hfs_btree *tree);
369 int hfs_bmap_reserve(struct hfs_btree *tree, u32 rsvd_nodes);
370 struct hfs_bnode *hfs_bmap_alloc(struct hfs_btree *tree);
371 void hfs_bmap_free(struct hfs_bnode *node);
372 
373 /* bnode.c */
374 void hfs_bnode_read(struct hfs_bnode *node, void *buf, u32 off, u32 len);
375 u16 hfs_bnode_read_u16(struct hfs_bnode *node, u32 off);
376 u8 hfs_bnode_read_u8(struct hfs_bnode *node, u32 off);
377 void hfs_bnode_read_key(struct hfs_bnode *node, void *key, u32 off);
378 void hfs_bnode_write(struct hfs_bnode *node, void *buf, u32 off, u32 len);
379 void hfs_bnode_write_u16(struct hfs_bnode *node, u32 off, u16 data);
380 void hfs_bnode_clear(struct hfs_bnode *node, u32 off, u32 len);
381 void hfs_bnode_copy(struct hfs_bnode *dst_node, u32 dst,
382 		    struct hfs_bnode *src_node, u32 src, u32 len);
383 void hfs_bnode_move(struct hfs_bnode *node, u32 dst, u32 src, u32 len);
384 void hfs_bnode_dump(struct hfs_bnode *node);
385 void hfs_bnode_unlink(struct hfs_bnode *node);
386 struct hfs_bnode *hfs_bnode_findhash(struct hfs_btree *tree, u32 cnid);
387 void hfs_bnode_unhash(struct hfs_bnode *node);
388 struct hfs_bnode *hfs_bnode_find(struct hfs_btree *tree, u32 num);
389 void hfs_bnode_free(struct hfs_bnode *node);
390 struct hfs_bnode *hfs_bnode_create(struct hfs_btree *tree, u32 num);
391 void hfs_bnode_get(struct hfs_bnode *node);
392 void hfs_bnode_put(struct hfs_bnode *node);
393 bool hfs_bnode_need_zeroout(struct hfs_btree *tree);
394 
395 /* brec.c */
396 u16 hfs_brec_lenoff(struct hfs_bnode *node, u16 rec, u16 *off);
397 u16 hfs_brec_keylen(struct hfs_bnode *node, u16 rec);
398 int hfs_brec_insert(struct hfs_find_data *fd, void *entry, u32 entry_len);
399 int hfs_brec_remove(struct hfs_find_data *fd);
400 
401 /* bfind.c */
402 int hfs_find_init(struct hfs_btree *tree, struct hfs_find_data *fd);
403 void hfs_find_exit(struct hfs_find_data *fd);
404 int hfs_find_1st_rec_by_cnid(struct hfs_bnode *bnode, struct hfs_find_data *fd,
405 			     int *begin, int *end, int *cur_rec);
406 int hfs_find_rec_by_key(struct hfs_bnode *bnode, struct hfs_find_data *fd,
407 			int *begin, int *end, int *cur_rec);
408 int __hfs_brec_find(struct hfs_bnode *bnode, struct hfs_find_data *fd,
409 		    search_strategy_t rec_found);
410 int hfs_brec_find(struct hfs_find_data *fd, search_strategy_t do_key_compare);
411 int hfs_brec_read(struct hfs_find_data *fd, void *rec, u32 rec_len);
412 int hfs_brec_goto(struct hfs_find_data *fd, int cnt);
413 
414 /* catalog.c */
415 int hfsplus_cat_case_cmp_key(const hfsplus_btree_key *k1,
416 			     const hfsplus_btree_key *k2);
417 int hfsplus_cat_bin_cmp_key(const hfsplus_btree_key *k1,
418 			    const hfsplus_btree_key *k2);
419 int hfsplus_cat_build_key(struct super_block *sb, hfsplus_btree_key *key,
420 			   u32 parent, const struct qstr *str);
421 void hfsplus_cat_build_key_with_cnid(struct super_block *sb,
422 				     hfsplus_btree_key *key, u32 parent);
423 void hfsplus_cat_set_perms(struct inode *inode, struct hfsplus_perm *perms);
424 int hfsplus_find_cat(struct super_block *sb, u32 cnid,
425 		     struct hfs_find_data *fd);
426 int hfsplus_create_cat(u32 cnid, struct inode *dir, const struct qstr *str,
427 		       struct inode *inode);
428 int hfsplus_delete_cat(u32 cnid, struct inode *dir, const struct qstr *str);
429 int hfsplus_rename_cat(u32 cnid, struct inode *src_dir, const struct qstr *src_name,
430 		       struct inode *dst_dir, const struct qstr *dst_name);
431 
432 /* dir.c */
433 extern const struct inode_operations hfsplus_dir_inode_operations;
434 extern const struct file_operations hfsplus_dir_operations;
435 
436 /* extents.c */
437 int hfsplus_ext_cmp_key(const hfsplus_btree_key *k1,
438 			const hfsplus_btree_key *k2);
439 int hfsplus_ext_write_extent(struct inode *inode);
440 int hfsplus_get_block(struct inode *inode, sector_t iblock,
441 		      struct buffer_head *bh_result, int create);
442 int hfsplus_free_fork(struct super_block *sb, u32 cnid,
443 		      struct hfsplus_fork_raw *fork, int type);
444 int hfsplus_file_extend(struct inode *inode, bool zeroout);
445 void hfsplus_file_truncate(struct inode *inode);
446 
447 /* inode.c */
448 extern const struct address_space_operations hfsplus_aops;
449 extern const struct address_space_operations hfsplus_btree_aops;
450 extern const struct dentry_operations hfsplus_dentry_operations;
451 
452 int hfsplus_write_begin(const struct kiocb *iocb,
453 			struct address_space *mapping,
454 			loff_t pos, unsigned len, struct folio **foliop,
455 			void **fsdata);
456 struct inode *hfsplus_new_inode(struct super_block *sb, struct inode *dir,
457 				umode_t mode);
458 void hfsplus_delete_inode(struct inode *inode);
459 void hfsplus_inode_read_fork(struct inode *inode,
460 			     struct hfsplus_fork_raw *fork);
461 void hfsplus_inode_write_fork(struct inode *inode,
462 			      struct hfsplus_fork_raw *fork);
463 int hfsplus_cat_read_inode(struct inode *inode, struct hfs_find_data *fd);
464 int hfsplus_cat_write_inode(struct inode *inode);
465 int hfsplus_getattr(struct mnt_idmap *idmap, const struct path *path,
466 		    struct kstat *stat, u32 request_mask,
467 		    unsigned int query_flags);
468 int hfsplus_file_fsync(struct file *file, loff_t start, loff_t end,
469 		       int datasync);
470 int hfsplus_fileattr_get(struct dentry *dentry, struct file_kattr *fa);
471 int hfsplus_fileattr_set(struct mnt_idmap *idmap,
472 			 struct dentry *dentry, struct file_kattr *fa);
473 
474 /* ioctl.c */
475 long hfsplus_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
476 
477 /* options.c */
478 void hfsplus_fill_defaults(struct hfsplus_sb_info *opts);
479 int hfsplus_parse_param(struct fs_context *fc, struct fs_parameter *param);
480 int hfsplus_show_options(struct seq_file *seq, struct dentry *root);
481 
482 /* part_tbl.c */
483 int hfs_part_find(struct super_block *sb, sector_t *part_start,
484 		  sector_t *part_size);
485 
486 /* super.c */
487 struct inode *hfsplus_iget(struct super_block *sb, unsigned long ino);
488 void hfsplus_mark_mdb_dirty(struct super_block *sb);
489 void hfsplus_prepare_volume_header_for_commit(struct hfsplus_vh *vhdr);
490 int hfsplus_commit_superblock(struct super_block *sb);
491 
492 /* tables.c */
493 extern u16 hfsplus_case_fold_table[];
494 extern u16 hfsplus_decompose_table[];
495 extern u16 hfsplus_compose_table[];
496 
497 /* unicode.c */
498 int hfsplus_strcasecmp(const struct hfsplus_unistr *s1,
499 		       const struct hfsplus_unistr *s2);
500 int hfsplus_strcmp(const struct hfsplus_unistr *s1,
501 		   const struct hfsplus_unistr *s2);
502 int hfsplus_uni2asc_str(struct super_block *sb,
503 			const struct hfsplus_unistr *ustr, char *astr,
504 			int *len_p);
505 int hfsplus_uni2asc_xattr_str(struct super_block *sb,
506 			      const struct hfsplus_attr_unistr *ustr,
507 			      char *astr, int *len_p);
508 int hfsplus_asc2uni(struct super_block *sb, struct hfsplus_unistr *ustr,
509 		    int max_unistr_len, const char *astr, int len,
510 		    int name_type);
511 int hfsplus_hash_dentry(const struct dentry *dentry, struct qstr *str);
512 int hfsplus_compare_dentry(const struct dentry *dentry, unsigned int len,
513 			   const char *str, const struct qstr *name);
514 
515 /* wrapper.c */
516 int hfsplus_submit_bio(struct super_block *sb, sector_t sector, void *buf,
517 		       void **data, blk_opf_t opf);
518 int hfsplus_read_wrapper(struct super_block *sb);
519 
hfsplus_cat_thread_size(const struct hfsplus_cat_thread * thread)520 static inline u32 hfsplus_cat_thread_size(const struct hfsplus_cat_thread *thread)
521 {
522 	return offsetof(struct hfsplus_cat_thread, nodeName) +
523 	       offsetof(struct hfsplus_unistr, unicode) +
524 	       be16_to_cpu(thread->nodeName.length) * sizeof(hfsplus_unichr);
525 }
526 
527 int hfsplus_brec_read_cat(struct hfs_find_data *fd, hfsplus_cat_entry *entry);
528 
529 /*
530  * time helpers: convert between 1904-base and 1970-base timestamps
531  *
532  * HFS+ implementations are highly inconsistent, this one matches the
533  * traditional behavior of 64-bit Linux, giving the most useful
534  * time range between 1970 and 2106, by treating any on-disk timestamp
535  * under HFSPLUS_UTC_OFFSET (Jan 1 1970) as a time between 2040 and 2106.
536  */
537 #define HFSPLUS_UTC_OFFSET 2082844800U
538 
__hfsp_mt2ut(__be32 mt)539 static inline time64_t __hfsp_mt2ut(__be32 mt)
540 {
541 	time64_t ut = (u32)(be32_to_cpu(mt) - HFSPLUS_UTC_OFFSET);
542 
543 	return ut;
544 }
545 
__hfsp_ut2mt(time64_t ut)546 static inline __be32 __hfsp_ut2mt(time64_t ut)
547 {
548 	return cpu_to_be32(lower_32_bits(ut) + HFSPLUS_UTC_OFFSET);
549 }
550 
551 static inline enum hfsplus_btree_mutex_classes
hfsplus_btree_lock_class(struct hfs_btree * tree)552 hfsplus_btree_lock_class(struct hfs_btree *tree)
553 {
554 	enum hfsplus_btree_mutex_classes class;
555 
556 	switch (tree->cnid) {
557 	case HFSPLUS_CAT_CNID:
558 		class = CATALOG_BTREE_MUTEX;
559 		break;
560 	case HFSPLUS_EXT_CNID:
561 		class = EXTENTS_BTREE_MUTEX;
562 		break;
563 	case HFSPLUS_ATTR_CNID:
564 		class = ATTR_BTREE_MUTEX;
565 		break;
566 	default:
567 		BUG();
568 	}
569 	return class;
570 }
571 
572 static inline
is_bnode_offset_valid(struct hfs_bnode * node,u32 off)573 bool is_bnode_offset_valid(struct hfs_bnode *node, u32 off)
574 {
575 	bool is_valid;
576 
577 	if (!node || !node->tree)
578 		return false;
579 
580 	is_valid = off < node->tree->node_size;
581 
582 	if (!is_valid) {
583 		pr_err("requested invalid offset: "
584 		       "NODE: id %u, type %#x, height %u, "
585 		       "node_size %u, offset %u\n",
586 		       node->this, node->type, node->height,
587 		       node->tree->node_size, off);
588 	}
589 
590 	return is_valid;
591 }
592 
593 static inline
check_and_correct_requested_length(struct hfs_bnode * node,u32 off,u32 len)594 u32 check_and_correct_requested_length(struct hfs_bnode *node, u32 off, u32 len)
595 {
596 	unsigned int node_size;
597 
598 	if (!is_bnode_offset_valid(node, off))
599 		return 0;
600 
601 	node_size = node->tree->node_size;
602 
603 	if ((off + len) > node_size) {
604 		u32 new_len = node_size - off;
605 
606 		pr_err("requested length has been corrected: "
607 		       "NODE: id %u, type %#x, height %u, "
608 		       "node_size %u, offset %u, "
609 		       "requested_len %u, corrected_len %u\n",
610 		       node->this, node->type, node->height,
611 		       node->tree->node_size, off, len, new_len);
612 
613 		return new_len;
614 	}
615 
616 	return len;
617 }
618 
619 /* compatibility */
620 #define hfsp_mt2ut(t)		(struct timespec64){ .tv_sec = __hfsp_mt2ut(t) }
621 #define hfsp_ut2mt(t)		__hfsp_ut2mt((t).tv_sec)
622 #define hfsp_now2mt()		__hfsp_ut2mt(ktime_get_real_seconds())
623 
624 #endif
625