1 /*
2 * linux/fs/hfs/hfs_fs.h
3 *
4 * Copyright (C) 1995-1997 Paul H. Hargrove
5 * (C) 2003 Ardis Technologies <roman@ardistech.com>
6 * This file may be distributed under the terms of the GNU General Public License.
7 */
8
9 #ifndef _LINUX_HFS_FS_H
10 #define _LINUX_HFS_FS_H
11
12 #include <linux/slab.h>
13 #include <linux/types.h>
14 #include <linux/mutex.h>
15 #include <linux/buffer_head.h>
16 #include <linux/fs.h>
17 #include <linux/workqueue.h>
18
19 #include <asm/byteorder.h>
20 #include <linux/uaccess.h>
21 #include <linux/hfs_common.h>
22
23 #include "hfs.h"
24
25 /*
26 * struct hfs_inode_info
27 *
28 * The HFS-specific part of a Linux (struct inode)
29 */
30 struct hfs_inode_info {
31 atomic_t opencnt;
32
33 unsigned int flags;
34
35 /* to deal with localtime ugliness */
36 int tz_secondswest;
37
38 struct hfs_cat_key cat_key;
39
40 struct list_head open_dir_list;
41 spinlock_t open_dir_lock;
42 struct inode *rsrc_inode;
43
44 struct mutex extents_lock;
45
46 u16 alloc_blocks, clump_blocks;
47 sector_t fs_blocks;
48 /* Allocation extents from catlog record or volume header */
49 hfs_extent_rec first_extents;
50 u16 first_blocks;
51 hfs_extent_rec cached_extents;
52 u16 cached_start, cached_blocks;
53
54 loff_t phys_size;
55 struct inode vfs_inode;
56 };
57
58 #define HFS_FLG_RSRC 0x0001
59 #define HFS_FLG_EXT_DIRTY 0x0002
60 #define HFS_FLG_EXT_NEW 0x0004
61
62 #define HFS_IS_RSRC(inode) (HFS_I(inode)->flags & HFS_FLG_RSRC)
63
64 /*
65 * struct hfs_sb_info
66 *
67 * The HFS-specific part of a Linux (struct super_block)
68 */
69 struct hfs_sb_info {
70 struct buffer_head *mdb_bh; /* The hfs_buffer
71 holding the real
72 superblock (aka VIB
73 or MDB) */
74 struct hfs_mdb *mdb;
75 struct buffer_head *alt_mdb_bh; /* The hfs_buffer holding
76 the alternate superblock */
77 struct hfs_mdb *alt_mdb;
78 __be32 *bitmap; /* The page holding the
79 allocation bitmap */
80 struct hfs_btree *ext_tree; /* Information about
81 the extents b-tree */
82 struct hfs_btree *cat_tree; /* Information about
83 the catalog b-tree */
84 atomic64_t file_count; /* The number of
85 regular files in
86 the filesystem */
87 atomic64_t folder_count; /* The number of
88 directories in the
89 filesystem */
90 atomic64_t next_id; /* The next available
91 file id number */
92 u32 clumpablks; /* The number of allocation
93 blocks to try to add when
94 extending a file */
95 u32 fs_start; /* The first 512-byte
96 block represented
97 in the bitmap */
98 u32 part_start;
99 u16 root_files; /* The number of
100 regular
101 (non-directory)
102 files in the root
103 directory */
104 u16 root_dirs; /* The number of
105 directories in the
106 root directory */
107 u16 fs_ablocks; /* The number of
108 allocation blocks
109 in the filesystem */
110 u16 free_ablocks; /* the number of unused
111 allocation blocks
112 in the filesystem */
113 u32 alloc_blksz; /* The size of an
114 "allocation block" */
115 int s_quiet; /* Silent failure when
116 changing owner or mode? */
117 __be32 s_type; /* Type for new files */
118 __be32 s_creator; /* Creator for new files */
119 umode_t s_file_umask; /* The umask applied to the
120 permissions on all files */
121 umode_t s_dir_umask; /* The umask applied to the
122 permissions on all dirs */
123 kuid_t s_uid; /* The uid of all files */
124 kgid_t s_gid; /* The gid of all files */
125
126 int session, part;
127 struct nls_table *nls_io, *nls_disk;
128 struct mutex bitmap_lock;
129 unsigned long flags;
130 u16 blockoffset;
131 int fs_div;
132 struct super_block *sb;
133 int work_queued; /* non-zero delayed work is queued */
134 struct delayed_work mdb_work; /* MDB flush delayed work */
135 spinlock_t work_lock; /* protects mdb_work and work_queued */
136 };
137
138 #define HFS_FLG_BITMAP_DIRTY 0
139 #define HFS_FLG_MDB_DIRTY 1
140 #define HFS_FLG_ALT_MDB_DIRTY 2
141
142 /* bitmap.c */
143 extern u32 hfs_vbm_search_free(struct super_block *, u32, u32 *);
144 extern int hfs_clear_vbm_bits(struct super_block *, u16, u16);
145
146 /* catalog.c */
147 extern int hfs_cat_keycmp(const btree_key *, const btree_key *);
148 struct hfs_find_data;
149 extern int hfs_cat_find_brec(struct super_block *, u32, struct hfs_find_data *);
150 extern int hfs_cat_create(u32, struct inode *, const struct qstr *, struct inode *);
151 extern int hfs_cat_delete(u32, struct inode *, const struct qstr *);
152 extern int hfs_cat_move(u32, struct inode *, const struct qstr *,
153 struct inode *, const struct qstr *);
154 extern void hfs_cat_build_key(struct super_block *, btree_key *, u32, const struct qstr *);
155
156 /* dir.c */
157 extern const struct file_operations hfs_dir_operations;
158 extern const struct inode_operations hfs_dir_inode_operations;
159
160 /* extent.c */
161 extern int hfs_ext_keycmp(const btree_key *, const btree_key *);
162 extern u16 hfs_ext_find_block(struct hfs_extent *ext, u16 off);
163 extern int hfs_free_fork(struct super_block *, struct hfs_cat_file *, int);
164 extern int hfs_ext_write_extent(struct inode *);
165 extern int hfs_extend_file(struct inode *);
166 extern void hfs_file_truncate(struct inode *);
167
168 extern int hfs_get_block(struct inode *, sector_t, struct buffer_head *, int);
169
170 /* inode.c */
171 extern const struct address_space_operations hfs_aops;
172 extern const struct address_space_operations hfs_btree_aops;
173
174 int hfs_write_begin(const struct kiocb *iocb, struct address_space *mapping,
175 loff_t pos, unsigned len, struct folio **foliop, void **fsdata);
176 extern struct inode *hfs_new_inode(struct inode *, const struct qstr *, umode_t);
177 extern void hfs_inode_write_fork(struct inode *, struct hfs_extent *, __be32 *, __be32 *);
178 extern int hfs_write_inode(struct inode *, struct writeback_control *);
179 extern int hfs_inode_setattr(struct mnt_idmap *, struct dentry *,
180 struct iattr *);
181 extern void hfs_inode_read_fork(struct inode *inode, struct hfs_extent *ext,
182 __be32 log_size, __be32 phys_size, u32 clump_size);
183 extern struct inode *hfs_iget(struct super_block *, struct hfs_cat_key *, hfs_cat_rec *);
184 extern void hfs_evict_inode(struct inode *);
185 extern void hfs_delete_inode(struct inode *);
186
187 /* attr.c */
188 extern const struct xattr_handler * const hfs_xattr_handlers[];
189
190 /* mdb.c */
191 extern int hfs_mdb_get(struct super_block *);
192 extern void hfs_mdb_commit(struct super_block *);
193 extern void hfs_mdb_close(struct super_block *);
194 extern void hfs_mdb_put(struct super_block *);
195
196 /* part_tbl.c */
197 extern int hfs_part_find(struct super_block *, sector_t *, sector_t *);
198
199 /* string.c */
200 extern const struct dentry_operations hfs_dentry_operations;
201
202 extern int hfs_hash_dentry(const struct dentry *, struct qstr *);
203 extern int hfs_strcmp(const unsigned char *, unsigned int,
204 const unsigned char *, unsigned int);
205 extern int hfs_compare_dentry(const struct dentry *dentry,
206 unsigned int len, const char *str, const struct qstr *name);
207
208 /* trans.c */
209 extern void hfs_asc2mac(struct super_block *, struct hfs_name *, const struct qstr *);
210 extern int hfs_mac2asc(struct super_block *, char *, const struct hfs_name *);
211
212 /* super.c */
213 extern void hfs_mark_mdb_dirty(struct super_block *sb);
214
215 /*
216 * There are two time systems. Both are based on seconds since
217 * a particular time/date.
218 * Unix: signed little-endian since 00:00 GMT, Jan. 1, 1970
219 * mac: unsigned big-endian since 00:00 GMT, Jan. 1, 1904
220 *
221 * HFS implementations are highly inconsistent, this one matches the
222 * traditional behavior of 64-bit Linux, giving the most useful
223 * time range between 1970 and 2106, by treating any on-disk timestamp
224 * under HFS_UTC_OFFSET (Jan 1 1970) as a time between 2040 and 2106.
225 */
226 #define HFS_UTC_OFFSET 2082844800U
227
__hfs_m_to_utime(__be32 mt)228 static inline time64_t __hfs_m_to_utime(__be32 mt)
229 {
230 time64_t ut = (u32)(be32_to_cpu(mt) - HFS_UTC_OFFSET);
231
232 return ut + sys_tz.tz_minuteswest * 60;
233 }
234
__hfs_u_to_mtime(time64_t ut)235 static inline __be32 __hfs_u_to_mtime(time64_t ut)
236 {
237 ut -= sys_tz.tz_minuteswest * 60;
238
239 return cpu_to_be32(lower_32_bits(ut) + HFS_UTC_OFFSET);
240 }
241 #define HFS_I(inode) (container_of(inode, struct hfs_inode_info, vfs_inode))
242 #define HFS_SB(sb) ((struct hfs_sb_info *)(sb)->s_fs_info)
243
244 #define hfs_m_to_utime(time) (struct timespec64){ .tv_sec = __hfs_m_to_utime(time) }
245 #define hfs_u_to_mtime(time) __hfs_u_to_mtime((time).tv_sec)
246 #define hfs_mtime() __hfs_u_to_mtime(ktime_get_real_seconds())
247
hfs_mdb_name(struct super_block * sb)248 static inline const char *hfs_mdb_name(struct super_block *sb)
249 {
250 return sb->s_id;
251 }
252
hfs_bitmap_dirty(struct super_block * sb)253 static inline void hfs_bitmap_dirty(struct super_block *sb)
254 {
255 set_bit(HFS_FLG_BITMAP_DIRTY, &HFS_SB(sb)->flags);
256 hfs_mark_mdb_dirty(sb);
257 }
258
259 #define sb_bread512(sb, sec, data) ({ \
260 struct buffer_head *__bh; \
261 sector_t __block; \
262 loff_t __start; \
263 int __offset; \
264 \
265 __start = (loff_t)(sec) << HFS_SECTOR_SIZE_BITS;\
266 __block = __start >> (sb)->s_blocksize_bits; \
267 __offset = __start & ((sb)->s_blocksize - 1); \
268 __bh = sb_bread((sb), __block); \
269 if (likely(__bh != NULL)) \
270 data = (void *)(__bh->b_data + __offset);\
271 else \
272 data = NULL; \
273 __bh; \
274 })
275
276 #endif
277