xref: /linux/fs/hfs/hfs_fs.h (revision 00b04f4495c354cad82f96d41947aa70f71e2cc4)
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 
22 #include "hfs.h"
23 
24 /*
25  * struct hfs_inode_info
26  *
27  * The HFS-specific part of a Linux (struct inode)
28  */
29 struct hfs_inode_info {
30 	atomic_t opencnt;
31 
32 	unsigned int flags;
33 
34 	/* to deal with localtime ugliness */
35 	int tz_secondswest;
36 
37 	struct hfs_cat_key cat_key;
38 
39 	struct inode *rsrc_inode;
40 
41 	struct mutex extents_lock;
42 
43 	u16 alloc_blocks, clump_blocks;
44 	sector_t fs_blocks;
45 	/* Allocation extents from catlog record or volume header */
46 	hfs_extent_rec first_extents;
47 	u16 first_blocks;
48 	hfs_extent_rec cached_extents;
49 	u16 cached_start, cached_blocks;
50 
51 	loff_t phys_size;
52 	struct inode vfs_inode;
53 };
54 
55 #define HFS_FLG_RSRC		0x0001
56 #define HFS_FLG_EXT_DIRTY	0x0002
57 #define HFS_FLG_EXT_NEW		0x0004
58 
59 #define HFS_IS_RSRC(inode)	(HFS_I(inode)->flags & HFS_FLG_RSRC)
60 
61 /*
62  * struct hfs_sb_info
63  *
64  * The HFS-specific part of a Linux (struct super_block)
65  */
66 struct hfs_sb_info {
67 	struct mutex mdb_lock;			/* MDB operations lock */
68 	struct buffer_head *mdb_bh;		/* The hfs_buffer
69 						   holding the real
70 						   superblock (aka VIB
71 						   or MDB) */
72 	unsigned int mdb_offset;		/* byte offset of the MDB
73 						   sector within mdb_bh's
74 						   data */
75 	struct hfs_mdb *mdb;			/* in-memory copy of the MDB */
76 	struct buffer_head *alt_mdb_bh;		/* The hfs_buffer holding
77 						   the alternate superblock */
78 	unsigned int alt_mdb_offset;		/* byte offset of the alternate
79 						   MDB sector within
80 						   alt_mdb_bh's data */
81 	struct hfs_mdb *alt_mdb;		/* in-memory copy of the
82 						   alternate MDB */
83 	__be32 *bitmap;				/* The page holding the
84 						   allocation bitmap */
85 	struct hfs_btree *ext_tree;			/* Information about
86 						   the extents b-tree */
87 	struct hfs_btree *cat_tree;			/* Information about
88 						   the catalog b-tree */
89 	atomic64_t file_count;			/* The number of
90 						   regular files in
91 						   the filesystem */
92 	atomic64_t folder_count;		/* The number of
93 						   directories in the
94 						   filesystem */
95 	atomic64_t next_id;			/* The next available
96 						   file id number */
97 	u32 clumpablks;				/* The number of allocation
98 						   blocks to try to add when
99 						   extending a file */
100 	u32 fs_start;				/* The first 512-byte
101 						   block represented
102 						   in the bitmap */
103 	u32 part_start;
104 	u16 root_files;				/* The number of
105 						   regular
106 						   (non-directory)
107 						   files in the root
108 						   directory */
109 	u16 root_dirs;				/* The number of
110 						   directories in the
111 						   root directory */
112 	u16 fs_ablocks;				/* The number of
113 						   allocation blocks
114 						   in the filesystem */
115 	u16 free_ablocks;			/* the number of unused
116 						   allocation blocks
117 						   in the filesystem */
118 	u32 alloc_blksz;			/* The size of an
119 						   "allocation block" */
120 	int s_quiet;				/* Silent failure when
121 						   changing owner or mode? */
122 	__be32 s_type;				/* Type for new files */
123 	__be32 s_creator;			/* Creator for new files */
124 	umode_t s_file_umask;			/* The umask applied to the
125 						   permissions on all files */
126 	umode_t s_dir_umask;			/* The umask applied to the
127 						   permissions on all dirs */
128 	kuid_t s_uid;				/* The uid of all files */
129 	kgid_t s_gid;				/* The gid of all files */
130 
131 	int session, part;
132 	struct nls_table *nls_io, *nls_disk;
133 	struct mutex bitmap_lock;
134 	unsigned long flags;
135 	u16 blockoffset;
136 	int fs_div;
137 	struct super_block *sb;
138 	int work_queued;		/* non-zero delayed work is queued */
139 	struct delayed_work mdb_work;	/* MDB flush delayed work */
140 	spinlock_t work_lock;		/* protects mdb_work and work_queued */
141 };
142 
143 #define HFS_FLG_BITMAP_DIRTY	0
144 #define HFS_FLG_MDB_DIRTY	1
145 #define HFS_FLG_ALT_MDB_DIRTY	2
146 
147 /* bitmap.c */
148 extern u32 hfs_vbm_search_free(struct super_block *sb, u32 goal, u32 *num_bits);
149 extern int hfs_clear_vbm_bits(struct super_block *sb, u16 start, u16 count);
150 
151 /* catalog.c */
152 extern int hfs_cat_keycmp(const btree_key *key1, const btree_key *key2);
153 struct hfs_find_data;
154 extern int hfs_cat_find_brec(struct super_block *sb, u32 cnid,
155 			     struct hfs_find_data *fd);
156 extern int hfs_cat_create(u32 cnid, struct inode *dir,
157 			  const struct qstr *str, struct inode *inode);
158 extern int hfs_cat_delete(u32 cnid, struct inode *dir, const struct qstr *str);
159 extern int hfs_cat_move(u32 cnid, struct inode *src_dir,
160 			const struct qstr *src_name,
161 			struct inode *dst_dir,
162 			const struct qstr *dst_name);
163 extern void hfs_cat_build_key(struct super_block *sb, btree_key *key,
164 			      u32 parent, const struct qstr *name);
165 
166 /*
167  * Validate the CNID of a catalog record.
168  */
hfs_is_valid_cnid(u32 cnid,u8 type)169 static inline bool hfs_is_valid_cnid(u32 cnid, u8 type)
170 {
171 	if (likely(cnid >= HFS_FIRSTUSER_CNID))
172 		return true;
173 
174 	switch (cnid) {
175 	case HFS_ROOT_CNID:
176 		return type == HFS_CDR_DIR;
177 	case HFS_EXT_CNID:
178 	case HFS_CAT_CNID:
179 		return type == HFS_CDR_FIL;
180 	default:
181 		return false;
182 	}
183 }
184 
185 /* dir.c */
186 extern const struct file_operations hfs_dir_operations;
187 extern const struct inode_operations hfs_dir_inode_operations;
188 
189 /* extent.c */
190 extern int hfs_ext_keycmp(const btree_key *key1, const btree_key *key2);
191 extern u16 hfs_ext_find_block(struct hfs_extent *ext, u16 off);
192 extern int hfs_free_fork(struct super_block *sb,
193 			 struct hfs_cat_file *file, int type);
194 extern int hfs_ext_write_extent(struct inode *inode);
195 extern int hfs_extend_file(struct inode *inode);
196 extern void hfs_file_truncate(struct inode *inode);
197 
198 extern int hfs_get_block(struct inode *inode, sector_t block,
199 			 struct buffer_head *bh_result, int create);
200 
201 /* inode.c */
202 extern const struct address_space_operations hfs_aops;
203 extern const struct address_space_operations hfs_btree_aops;
204 
205 struct file_kattr;
206 int hfs_fileattr_get(struct dentry *dentry, struct file_kattr *fa);
207 int hfs_write_begin(const struct kiocb *iocb, struct address_space *mapping,
208 		    loff_t pos, unsigned int len, struct folio **foliop,
209 		    void **fsdata);
210 extern struct inode *hfs_new_inode(struct inode *dir, const struct qstr *name,
211 				   umode_t mode);
212 extern void hfs_inode_write_fork(struct inode *inode, struct hfs_extent *ext,
213 				 __be32 *log_size, __be32 *phys_size);
214 extern int hfs_write_inode(struct inode *inode, struct writeback_control *wbc);
215 extern int hfs_inode_setattr(const struct mnt_idmap *idmap, struct dentry *dentry,
216 			     struct iattr *attr);
217 extern void hfs_inode_read_fork(struct inode *inode, struct hfs_extent *ext,
218 				__be32 __log_size, __be32 phys_size,
219 				u32 clump_size);
220 extern struct inode *hfs_iget(struct super_block *sb, struct hfs_cat_key *key,
221 				hfs_cat_rec *rec);
222 extern void hfs_evict_inode(struct inode *inode);
223 extern void hfs_delete_inode(struct inode *inode);
224 
225 /* attr.c */
226 extern const struct xattr_handler * const hfs_xattr_handlers[];
227 
228 /* mdb.c */
229 extern bool is_hfs_cnid_counts_valid(struct super_block *sb);
230 extern int hfs_mdb_get(struct super_block *sb);
231 extern int hfs_mdb_commit(struct super_block *sb);
232 extern void hfs_mdb_close(struct super_block *sb);
233 extern void hfs_mdb_put(struct super_block *sb);
234 
235 /* part_tbl.c */
236 extern int hfs_part_find(struct super_block *sb,
237 			 sector_t *part_start, sector_t *part_size);
238 
239 /* string.c */
240 extern const struct dentry_operations hfs_dentry_operations;
241 
242 extern int hfs_hash_dentry(const struct dentry *dentry, struct qstr *this);
243 extern int hfs_strcmp(const unsigned char *s1, unsigned int len1,
244 		      const unsigned char *s2, unsigned int len2);
245 extern int hfs_compare_dentry(const struct dentry *dentry,
246 				unsigned int len, const char *str,
247 				const struct qstr *name);
248 
249 /* trans.c */
250 extern void hfs_asc2mac(struct super_block *sb,
251 			struct hfs_name *out, const struct qstr *in);
252 extern int hfs_mac2asc(struct super_block *sb,
253 			char *out, const struct hfs_name *in);
254 
255 /* super.c */
256 extern void hfs_mark_mdb_dirty(struct super_block *sb);
257 
258 /*
259  * There are two time systems.  Both are based on seconds since
260  * a particular time/date.
261  *	Unix:	signed little-endian since 00:00 GMT, Jan. 1, 1970
262  *	mac:	unsigned big-endian since 00:00 GMT, Jan. 1, 1904
263  *
264  * HFS implementations are highly inconsistent, this one matches the
265  * traditional behavior of 64-bit Linux, giving the most useful
266  * time range between 1970 and 2106, by treating any on-disk timestamp
267  * under HFS_UTC_OFFSET (Jan 1 1970) as a time between 2040 and 2106.
268  */
269 #define HFS_UTC_OFFSET 2082844800U
270 
__hfs_m_to_utime(__be32 mt)271 static inline time64_t __hfs_m_to_utime(__be32 mt)
272 {
273 	time64_t ut = (u32)(be32_to_cpu(mt) - HFS_UTC_OFFSET);
274 
275 	return ut + sys_tz.tz_minuteswest * 60;
276 }
277 
__hfs_u_to_mtime(time64_t ut)278 static inline __be32 __hfs_u_to_mtime(time64_t ut)
279 {
280 	ut -= sys_tz.tz_minuteswest * 60;
281 
282 	return cpu_to_be32(lower_32_bits(ut) + HFS_UTC_OFFSET);
283 }
284 #define HFS_I(inode)	(container_of(inode, struct hfs_inode_info, vfs_inode))
285 #define HFS_SB(sb)	((struct hfs_sb_info *)(sb)->s_fs_info)
286 
287 #define hfs_m_to_utime(time)   (struct timespec64){ .tv_sec = __hfs_m_to_utime(time) }
288 #define hfs_u_to_mtime(time)   __hfs_u_to_mtime((time).tv_sec)
289 #define hfs_mtime()		__hfs_u_to_mtime(ktime_get_real_seconds())
290 
hfs_mdb_name(struct super_block * sb)291 static inline const char *hfs_mdb_name(struct super_block *sb)
292 {
293 	return sb->s_id;
294 }
295 
hfs_bitmap_dirty(struct super_block * sb)296 static inline void hfs_bitmap_dirty(struct super_block *sb)
297 {
298 	set_bit(HFS_FLG_BITMAP_DIRTY, &HFS_SB(sb)->flags);
299 	hfs_mark_mdb_dirty(sb);
300 }
301 
302 #define sb_bread512(sb, sec, data) ({			\
303 	struct buffer_head *__bh;			\
304 	sector_t __block;				\
305 	loff_t __start;					\
306 	int __offset;					\
307 							\
308 	__start = (loff_t)(sec) << HFS_SECTOR_SIZE_BITS;\
309 	__block = __start >> (sb)->s_blocksize_bits;	\
310 	__offset = __start & ((sb)->s_blocksize - 1);	\
311 	__bh = sb_bread((sb), __block);			\
312 	if (likely(__bh != NULL))			\
313 		data = (void *)(__bh->b_data + __offset);\
314 	else						\
315 		data = NULL;				\
316 	__bh;						\
317 })
318 
319 #endif
320