1/*
2 *  linux/include/linux/hfsplus_fs.h
3 *
4 * Copyright (C) 1999
5 * Brad Boyer (flar@pants.nu)
6 * (C) 2003 Ardis Technologies <roman@ardistech.com>
7 *
8 */
9
10#ifndef _LINUX_HFSPLUS_FS_H
11#define _LINUX_HFSPLUS_FS_H
12
13#include <linux/fs.h>
14#include <linux/buffer_head.h>
15#include "hfsplus_jbd.h"
16#include "hfsplus_raw.h"
17
18#define DBG_BNODE_REFS	0x00000001
19#define DBG_BNODE_MOD	0x00000002
20#define DBG_CAT_MOD	0x00000004
21#define DBG_INODE	0x00000008
22#define DBG_SUPER	0x00000010
23#define DBG_EXTENT	0x00000020
24#define DBG_BITMAP	0x00000040
25#define DBG_JOURNAL	0x00000080
26#define DBG_JREPLAY	0x00000100
27#define DBG_JTRANS	0x00000200
28#define DBG_JCOMMIT	0x00000400
29#define DBG_JCHKPT	0x00000800
30
31//#define DBG_MASK	(DBG_EXTENT|DBG_INODE|DBG_BNODE_MOD)
32//#define DBG_MASK	(DBG_BNODE_MOD|DBG_CAT_MOD|DBG_INODE)
33//#define DBG_MASK	(DBG_CAT_MOD|DBG_BNODE_REFS|DBG_INODE|DBG_EXTENT)
34//#define DBG_MASK	(0)
35#define DBG_MASK		(DBG_JOURNAL|DBG_JREPLAY)
36
37#define HFSPLUS_JOURNAL_PRESENT			1
38#define HFSPLUS_JOURNAL_CONSISTENT		0
39#define HFSPLUS_JOURNAL_INCONSISTENT	1
40#define HFSPLUS_JOURNAL_UIBYTE			0x5A /* Unimportant byte value */
41#define HFSPLUS_JOURNAL_SUCCESS			0
42#define HFSPLUS_JOURNAL_FAIL				1
43#define HFSPLUS_JOURNAL_SWAP				1
44
45#define dprint(flg, fmt, args...) \
46	if (flg & DBG_MASK) printk(fmt , ## args)
47
48/* Runtime config options */
49#define HFSPLUS_DEF_CR_TYPE    0x3F3F3F3F  /* '????' */
50
51#define HFSPLUS_TYPE_DATA 0x00
52#define HFSPLUS_TYPE_RSRC 0xFF
53
54typedef int (*btree_keycmp)(const hfsplus_btree_key *, const hfsplus_btree_key *);
55
56#define NODE_HASH_SIZE	256
57
58/* An HFS+ BTree held in memory */
59struct hfs_btree {
60	struct super_block *sb;
61	struct inode *inode;
62	btree_keycmp keycmp;
63
64	u32 cnid;
65	u32 root;
66	u32 leaf_count;
67	u32 leaf_head;
68	u32 leaf_tail;
69	u32 node_count;
70	u32 free_nodes;
71	u32 attributes;
72
73	unsigned int node_size;
74	unsigned int node_size_shift;
75	unsigned int max_key_len;
76	unsigned int depth;
77
78	//unsigned int map1_size, map_size;
79	struct semaphore tree_lock;
80
81	unsigned int pages_per_bnode;
82	spinlock_t hash_lock;
83	struct hfs_bnode *node_hash[NODE_HASH_SIZE];
84	int node_hash_cnt;
85};
86
87struct page;
88
89/* An HFS+ BTree node in memory */
90struct hfs_bnode {
91	struct hfs_btree *tree;
92
93	u32 prev;
94	u32 this;
95	u32 next;
96	u32 parent;
97
98	u16 num_recs;
99	u8 type;
100	u8 height;
101
102	struct hfs_bnode *next_hash;
103	unsigned long flags;
104	wait_queue_head_t lock_wq;
105	atomic_t refcnt;
106	unsigned int page_offset;
107	struct page *page[0];
108};
109
110#define HFS_BNODE_LOCK		0
111#define HFS_BNODE_ERROR		1
112#define HFS_BNODE_NEW		2
113#define HFS_BNODE_DIRTY		3
114#define HFS_BNODE_DELETED	4
115
116/* An HFS+ Journal held in memory */
117struct hfsplus_journal {
118	struct semaphore jnl_lock;
119	u32 journaled;
120	u32 flags;
121
122	/* Journal info block specific */
123	struct buffer_head *jib_bh;
124	struct hfsplus_journal_info_block *jibhdr;
125	u64 jib_offset;
126
127	/* Journal header specific */
128	struct buffer_head *jh_bh;
129	u32 jh_bh_size;
130	u64 jh_offset;
131	struct hfsplus_journal_header *jhdr;
132
133	/* block number of meta-data */
134	u32 ext_block;
135	u32 alloc_block;
136	u32 catalog_block;
137	u32 attr_block;
138
139	/* Pointer of JBD */
140	struct hfsplus_jbd_s *s_journal;
141
142	/* Maximum number of blocks allowed in a single transaction */
143	u16 journal_maxblock;
144
145	struct super_block *sbp;
146};
147
148/*
149 * HFS+ superblock info (built from Volume Header on disk)
150 */
151
152struct hfsplus_vh;
153struct hfs_btree;
154
155struct hfsplus_sb_info {
156	struct buffer_head *s_vhbh;
157	struct hfsplus_vh *s_vhdr;
158	struct hfs_btree *ext_tree;
159	struct hfs_btree *cat_tree;
160	struct hfs_btree *attr_tree;
161	struct inode *alloc_file;
162	struct inode *hidden_dir;
163	struct nls_table *nls;
164
165	/* Runtime variables */
166	u32 blockoffset;
167	u32 sect_count;
168	int fs_shift;
169
170	/* Stuff in host order from Vol Header */
171	struct hfsplus_journal jnl;
172	u32 alloc_blksz;
173	int alloc_blksz_shift;
174	u32 total_blocks;
175	u32 free_blocks;
176	u32 next_alloc;
177	u32 next_cnid;
178	u32 file_count;
179	u32 folder_count;
180	u32 data_clump_blocks, rsrc_clump_blocks;
181
182	/* Config options */
183	u32 creator;
184	u32 type;
185
186	umode_t umask;
187	uid_t uid;
188	gid_t gid;
189
190	int part, session;
191
192	unsigned long flags;
193
194	struct hlist_head rsrc_inodes;
195};
196
197#define HFSPLUS_SB_WRITEBACKUP	0x0001
198#define HFSPLUS_SB_NODECOMPOSE	0x0002
199#define HFSPLUS_SB_FORCE	0x0004
200#define HFSPLUS_SB_HFSX		0x0008
201
202
203struct hfsplus_inode_info {
204	struct semaphore extents_lock;
205	u32 clump_blocks, alloc_blocks;
206	sector_t fs_blocks;
207	/* Allocation extents from catalog record or volume header */
208	hfsplus_extent_rec first_extents;
209	u32 first_blocks;
210	hfsplus_extent_rec cached_extents;
211	u32 cached_start, cached_blocks;
212	atomic_t opencnt;
213
214	struct inode *rsrc_inode;
215	unsigned long flags;
216
217	__be32 create_date;
218	/* Device number in hfsplus_permissions in catalog */
219	u32 dev;
220	/* BSD system and user file flags */
221	u8 rootflags;
222	u8 userflags;
223
224	struct list_head open_dir_list;
225	loff_t phys_size;
226	struct inode vfs_inode;
227};
228
229#define HFSPLUS_FLG_RSRC	0x0001
230#define HFSPLUS_FLG_EXT_DIRTY	0x0002
231#define HFSPLUS_FLG_EXT_NEW	0x0004
232
233#define HFSPLUS_IS_DATA(inode)   (!(HFSPLUS_I(inode).flags & HFSPLUS_FLG_RSRC))
234#define HFSPLUS_IS_RSRC(inode)   (HFSPLUS_I(inode).flags & HFSPLUS_FLG_RSRC)
235
236struct hfs_find_data {
237	/* filled by caller */
238	hfsplus_btree_key *search_key;
239	hfsplus_btree_key *key;
240	/* filled by find */
241	struct hfs_btree *tree;
242	struct hfs_bnode *bnode;
243	/* filled by findrec */
244	int record;
245	int keyoffset, keylength;
246	int entryoffset, entrylength;
247};
248
249struct hfsplus_readdir_data {
250	struct list_head list;
251	struct file *file;
252	struct hfsplus_cat_key key;
253};
254
255#define hfs_btree_open hfsplus_btree_open
256#define hfs_btree_close hfsplus_btree_close
257#define hfs_btree_write hfsplus_btree_write
258#define hfs_bmap_alloc hfsplus_bmap_alloc
259#define hfs_bmap_free hfsplus_bmap_free
260#define hfs_bnode_read hfsplus_bnode_read
261#define hfs_bnode_read_u16 hfsplus_bnode_read_u16
262#define hfs_bnode_read_u8 hfsplus_bnode_read_u8
263#define hfs_bnode_read_key hfsplus_bnode_read_key
264#define hfs_bnode_write hfsplus_bnode_write
265#define hfs_bnode_write_u16 hfsplus_bnode_write_u16
266#define hfs_bnode_clear hfsplus_bnode_clear
267#define hfs_bnode_copy hfsplus_bnode_copy
268#define hfs_bnode_move hfsplus_bnode_move
269#define hfs_bnode_dump hfsplus_bnode_dump
270#define hfs_bnode_unlink hfsplus_bnode_unlink
271#define hfs_bnode_findhash hfsplus_bnode_findhash
272#define hfs_bnode_find hfsplus_bnode_find
273#define hfs_bnode_unhash hfsplus_bnode_unhash
274#define hfs_bnode_free hfsplus_bnode_free
275#define hfs_bnode_create hfsplus_bnode_create
276#define hfs_bnode_get hfsplus_bnode_get
277#define hfs_bnode_put hfsplus_bnode_put
278#define hfs_brec_lenoff hfsplus_brec_lenoff
279#define hfs_brec_keylen hfsplus_brec_keylen
280#define hfs_brec_insert hfsplus_brec_insert
281#define hfs_brec_remove hfsplus_brec_remove
282#define hfs_find_init hfsplus_find_init
283#define hfs_find_exit hfsplus_find_exit
284#define __hfs_brec_find __hplusfs_brec_find
285#define hfs_brec_find hfsplus_brec_find
286#define hfs_brec_read hfsplus_brec_read
287#define hfs_brec_goto hfsplus_brec_goto
288#define hfs_part_find hfsplus_part_find
289
290/*
291 * definitions for ext2 flag ioctls (linux really needs a generic
292 * interface for this).
293 */
294
295/* ext2 ioctls (EXT2_IOC_GETFLAGS and EXT2_IOC_SETFLAGS) to support
296 * chattr/lsattr */
297#define HFSPLUS_IOC_EXT2_GETFLAGS	FS_IOC_GETFLAGS
298#define HFSPLUS_IOC_EXT2_SETFLAGS	FS_IOC_SETFLAGS
299
300
301/*
302 * Functions in any *.c used in other files
303 */
304
305/* bitmap.c */
306int hfsplus_block_allocate(hfsplus_handle_t *, struct super_block *, u32, u32, u32 *);
307int hfsplus_block_free(hfsplus_handle_t *, struct super_block *, u32, u32);
308
309/* btree.c */
310struct hfs_btree *hfs_btree_open(struct super_block *, u32);
311void hfs_btree_close(struct hfs_btree *);
312void hfs_btree_write(hfsplus_handle_t *, struct hfs_btree *);
313struct hfs_bnode *hfs_bmap_alloc(hfsplus_handle_t *, struct hfs_btree *);
314void hfs_bmap_free(hfsplus_handle_t *, struct hfs_bnode *);
315
316/* bnode.c */
317void hfs_bnode_read(struct hfs_bnode *, void *, int, int);
318u16 hfs_bnode_read_u16(struct hfs_bnode *, int);
319u8 hfs_bnode_read_u8(struct hfs_bnode *, int);
320void hfs_bnode_read_key(struct hfs_bnode *, void *, int);
321void hfs_bnode_write(hfsplus_handle_t *, struct hfs_bnode *, void *, int, int);
322void hfs_bnode_write_u16(hfsplus_handle_t *, struct hfs_bnode *, int, u16);
323void hfs_bnode_clear(hfsplus_handle_t *, struct hfs_bnode *, int, int);
324void hfs_bnode_copy(hfsplus_handle_t *, struct hfs_bnode *, int,
325		    struct hfs_bnode *, int, int);
326void hfs_bnode_move(hfsplus_handle_t *, struct hfs_bnode *, int, int, int);
327void hfs_bnode_dump(struct hfs_bnode *);
328void hfs_bnode_unlink(hfsplus_handle_t *, struct hfs_bnode *);
329struct hfs_bnode *hfs_bnode_findhash(struct hfs_btree *, u32);
330struct hfs_bnode *hfs_bnode_find(hfsplus_handle_t *, struct hfs_btree *, u32);
331void hfs_bnode_unhash(struct hfs_bnode *);
332void hfs_bnode_free(struct hfs_bnode *);
333struct hfs_bnode *hfs_bnode_create(hfsplus_handle_t *, struct hfs_btree *, u32);
334void hfs_bnode_get(struct hfs_bnode *);
335void hfs_bnode_put(hfsplus_handle_t *, struct hfs_bnode *);
336
337/* brec.c */
338u16 hfs_brec_lenoff(struct hfs_bnode *, u16, u16 *);
339u16 hfs_brec_keylen(struct hfs_bnode *, u16);
340int hfs_brec_insert(hfsplus_handle_t *, struct hfs_find_data *, void *, int);
341int hfs_brec_remove(hfsplus_handle_t *, struct hfs_find_data *);
342
343/* bfind.c */
344int hfs_find_init(struct hfs_btree *, struct hfs_find_data *);
345void hfs_find_exit(hfsplus_handle_t *, struct hfs_find_data *);
346int hfsplus_journalled_find_init(struct hfs_btree *, struct hfs_find_data *);
347void hfsplus_journalled_find_exit(hfsplus_handle_t *, struct hfs_find_data *);
348int __hfs_brec_find(struct hfs_bnode *, struct hfs_find_data *);
349int hfs_brec_find(hfsplus_handle_t *, struct hfs_find_data *);
350int hfs_brec_read(hfsplus_handle_t *, struct hfs_find_data *, void *, int);
351int hfs_brec_goto(hfsplus_handle_t *, struct hfs_find_data *, int);
352
353/* catalog.c */
354int hfsplus_cat_case_cmp_key(const hfsplus_btree_key *, const hfsplus_btree_key *);
355int hfsplus_cat_bin_cmp_key(const hfsplus_btree_key *, const hfsplus_btree_key *);
356void hfsplus_cat_build_key(struct super_block *sb, hfsplus_btree_key *, u32, struct qstr *);
357int hfsplus_find_cat(hfsplus_handle_t *, struct super_block *, u32, struct hfs_find_data *);
358int hfsplus_create_cat(hfsplus_handle_t *, u32, struct inode *, struct qstr *, struct inode *);
359int hfsplus_delete_cat(hfsplus_handle_t *, u32, struct inode *, struct qstr *);
360int hfsplus_rename_cat(hfsplus_handle_t *, u32, struct inode *, struct qstr *,
361		       struct inode *, struct qstr *);
362
363/* extents.c */
364int hfsplus_ext_cmp_key(const hfsplus_btree_key *, const hfsplus_btree_key *);
365void hfsplus_ext_write_extent(hfsplus_handle_t *, struct inode *);
366int hfsplus_get_block(struct inode *, sector_t, struct buffer_head *, int);
367int hfsplus_free_fork(hfsplus_handle_t *handle, struct super_block *, u32, struct hfsplus_fork_raw *, int);
368int hfsplus_file_extend(hfsplus_handle_t *, struct inode *);
369void hfsplus_file_truncate(struct inode *);
370
371/* inode.c */
372extern const struct address_space_operations hfsplus_aops;
373extern const struct address_space_operations hfsplus_btree_aops;
374extern struct address_space_operations hfsplus_journalled_aops;
375extern struct address_space_operations hfsplus_journalled_btree_aops;
376
377void hfsplus_inode_read_fork(struct inode *, struct hfsplus_fork_raw *);
378void hfsplus_inode_write_fork(struct inode *, struct hfsplus_fork_raw *);
379int hfsplus_cat_read_inode(struct inode *, struct hfs_find_data *);
380int hfsplus_cat_write_inode(hfsplus_handle_t *, struct inode *);
381struct inode *hfsplus_new_inode(hfsplus_handle_t *, struct super_block *, int);
382void hfsplus_delete_inode(hfsplus_handle_t *, struct inode *);
383int hfsplus_do_journal_get_write_access(hfsplus_handle_t *, struct buffer_head *);
384int hfsplus_commit_write_fn(hfsplus_handle_t *, struct buffer_head *);
385int hfsplus_walk_page_buffers(hfsplus_handle_t *, struct buffer_head *, unsigned ,
386				unsigned , int *, int (*fn)(hfsplus_handle_t *, struct buffer_head *));
387
388/* ioctl.c */
389int hfsplus_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
390		  unsigned long arg);
391int hfsplus_setxattr(struct dentry *dentry, const char *name,
392		     const void *value, size_t size, int flags);
393ssize_t hfsplus_getxattr(struct dentry *dentry, const char *name,
394			 void *value, size_t size);
395ssize_t hfsplus_listxattr(struct dentry *dentry, char *buffer, size_t size);
396
397/* options.c */
398int hfsplus_parse_options(char *, struct hfsplus_sb_info *);
399void hfsplus_fill_defaults(struct hfsplus_sb_info *);
400int hfsplus_show_options(struct seq_file *, struct vfsmount *);
401
402/* tables.c */
403extern u16 hfsplus_case_fold_table[];
404extern u16 hfsplus_decompose_table[];
405extern u16 hfsplus_compose_table[];
406
407/* unicode.c */
408int hfsplus_strcasecmp(const struct hfsplus_unistr *, const struct hfsplus_unistr *);
409int hfsplus_strcmp(const struct hfsplus_unistr *, const struct hfsplus_unistr *);
410int hfsplus_uni2asc(struct super_block *, const struct hfsplus_unistr *, char *, int *);
411int hfsplus_asc2uni(struct super_block *, struct hfsplus_unistr *, const char *, int);
412
413/* wrapper.c */
414int hfsplus_read_wrapper(struct super_block *);
415
416int hfs_part_find(struct super_block *, sector_t *, sector_t *);
417
418/* journal.c */
419void hfsplus_journalled_init(struct super_block *, struct hfsplus_vh *);
420void hfsplus_journalled_deinit(struct super_block *);
421int hfsplus_journalled_create(struct super_block *);
422int hfsplus_journalled_check(struct super_block *);
423void print_volume_header(struct super_block *);
424void swap_block_list_header(struct hfsplus_blhdr *);
425hfsplus_jbd_t * hfsplus_get_dev_journal(struct super_block *);
426int hfsplus_journalled_mark_inode_dirty(const char *, hfsplus_handle_t *, struct inode *);
427int hfsplus_journalled_set_page_dirty(hfsplus_handle_t *, struct page *);
428int hfsplus_journal_start(const char *, struct super_block *sb, hfsplus_handle_t *hfsplus_handle);
429int hfsplus_journal_stop(hfsplus_handle_t *);
430int hfsplus_journal_get_write_access(const char *, hfsplus_handle_t *, struct buffer_head *);
431int hfsplus_journal_dirty_metadata(const char *, struct buffer_head *, hfsplus_handle_t *);
432int hfsplus_force_commit(struct super_block *);
433void hfsplus_jhdr_checksum_calculate(hfsplus_jbd_t *);
434void hfsplus_journal_header_start_update(hfsplus_jbd_t *, unsigned long);
435void hfsplus_journal_header_end_update(hfsplus_jbd_t *, struct hfsplus_journal_header *);
436void hfsplus_journal_mark_journal_empty(hfsplus_jbd_t *);
437void hfsplus_test_block_list_header(const char *, struct hfsplus_journal_header *, struct hfsplus_journal *);
438
439/* access macros */
440/*
441static inline struct hfsplus_sb_info *HFSPLUS_SB(struct super_block *sb)
442{
443	return sb->s_fs_info;
444}
445static inline struct hfsplus_inode_info *HFSPLUS_I(struct inode *inode)
446{
447	return list_entry(inode, struct hfsplus_inode_info, vfs_inode);
448}
449*/
450#define HFSPLUS_SB(super)	(*(struct hfsplus_sb_info *)(super)->s_fs_info)
451#define HFSPLUS_I(inode)	(*list_entry(inode, struct hfsplus_inode_info, vfs_inode))
452
453#define hfsplus_kmap(p)		({ struct page *__p = (p); kmap(__p); })
454#define hfsplus_kunmap(p)	({ struct page *__p = (p); kunmap(__p); __p; })
455
456#define sb_bread512(sb, sec, data) ({			\
457	struct buffer_head *__bh;			\
458	sector_t __block;				\
459	loff_t __start;					\
460	int __offset;					\
461							\
462	__start = (loff_t)(sec) << HFSPLUS_SECTOR_SHIFT;\
463	__block = __start >> (sb)->s_blocksize_bits;	\
464	__offset = __start & ((sb)->s_blocksize - 1);	\
465	__bh = sb_bread((sb), __block);			\
466	if (likely(__bh != NULL))			\
467		data = (void *)(__bh->b_data + __offset);\
468	else						\
469		data = NULL;				\
470	__bh;						\
471})
472
473/* time macros */
474#define __hfsp_mt2ut(t)		(be32_to_cpu(t) - 2082844800U)
475#define __hfsp_ut2mt(t)		(cpu_to_be32(t + 2082844800U))
476
477/* compatibility */
478#define hfsp_mt2ut(t)		(struct timespec){ .tv_sec = __hfsp_mt2ut(t) }
479#define hfsp_ut2mt(t)		__hfsp_ut2mt((t).tv_sec)
480#define hfsp_now2mt()		__hfsp_ut2mt(get_seconds())
481
482#define kdev_t_to_nr(x)		(x)
483
484#endif
485