blob: b8eb0322a3e5947dee7e6585a85dcab1f2f9de9e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Joe Perchesd6142672013-04-30 15:27:55 -070012#ifdef pr_fmt
13#undef pr_fmt
14#endif
15
16#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/slab.h>
19#include <linux/types.h>
Matthias Kaehlcke3084b722008-07-25 01:46:34 -070020#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/buffer_head.h>
22#include <linux/fs.h>
Artem Bityutskiy5687b572012-07-12 17:28:49 +030023#include <linux/workqueue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#include <asm/byteorder.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080026#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28#include "hfs.h"
29
30#define DBG_BNODE_REFS 0x00000001
31#define DBG_BNODE_MOD 0x00000002
32#define DBG_CAT_MOD 0x00000004
33#define DBG_INODE 0x00000008
34#define DBG_SUPER 0x00000010
35#define DBG_EXTENT 0x00000020
36#define DBG_BITMAP 0x00000040
37
38//#define DBG_MASK (DBG_EXTENT|DBG_INODE|DBG_BNODE_MOD|DBG_CAT_MOD|DBG_BITMAP)
39//#define DBG_MASK (DBG_BNODE_MOD|DBG_CAT_MOD|DBG_INODE)
40//#define DBG_MASK (DBG_CAT_MOD|DBG_BNODE_REFS|DBG_INODE|DBG_EXTENT)
41#define DBG_MASK (0)
42
Joe Perchesd6142672013-04-30 15:27:55 -070043#define hfs_dbg(flg, fmt, ...) \
44do { \
45 if (DBG_##flg & DBG_MASK) \
46 printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); \
Joe Perchesc2b3e1f2013-04-30 15:27:54 -070047} while (0)
48
Joe Perchesd6142672013-04-30 15:27:55 -070049#define hfs_dbg_cont(flg, fmt, ...) \
50do { \
51 if (DBG_##flg & DBG_MASK) \
52 pr_cont(fmt, ##__VA_ARGS__); \
Joe Perchesc2b3e1f2013-04-30 15:27:54 -070053} while (0)
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Linus Torvalds1da177e2005-04-16 15:20:36 -070056/*
57 * struct hfs_inode_info
58 *
59 * The HFS-specific part of a Linux (struct inode)
60 */
61struct hfs_inode_info {
62 atomic_t opencnt;
63
64 unsigned int flags;
65
66 /* to deal with localtime ugliness */
67 int tz_secondswest;
68
69 struct hfs_cat_key cat_key;
70
71 struct list_head open_dir_list;
Al Viro9717a912016-05-12 20:13:50 -040072 spinlock_t open_dir_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 struct inode *rsrc_inode;
74
Matthias Kaehlcke39f8d472008-07-25 01:46:35 -070075 struct mutex extents_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77 u16 alloc_blocks, clump_blocks;
78 sector_t fs_blocks;
79 /* Allocation extents from catlog record or volume header */
80 hfs_extent_rec first_extents;
81 u16 first_blocks;
82 hfs_extent_rec cached_extents;
83 u16 cached_start, cached_blocks;
84
85 loff_t phys_size;
86 struct inode vfs_inode;
87};
88
89#define HFS_FLG_RSRC 0x0001
90#define HFS_FLG_EXT_DIRTY 0x0002
91#define HFS_FLG_EXT_NEW 0x0004
92
93#define HFS_IS_RSRC(inode) (HFS_I(inode)->flags & HFS_FLG_RSRC)
94
95/*
96 * struct hfs_sb_info
97 *
98 * The HFS-specific part of a Linux (struct super_block)
99 */
100struct hfs_sb_info {
101 struct buffer_head *mdb_bh; /* The hfs_buffer
102 holding the real
103 superblock (aka VIB
104 or MDB) */
105 struct hfs_mdb *mdb;
106 struct buffer_head *alt_mdb_bh; /* The hfs_buffer holding
107 the alternate superblock */
108 struct hfs_mdb *alt_mdb;
109 __be32 *bitmap; /* The page holding the
110 allocation bitmap */
111 struct hfs_btree *ext_tree; /* Information about
112 the extents b-tree */
113 struct hfs_btree *cat_tree; /* Information about
114 the catalog b-tree */
115 u32 file_count; /* The number of
116 regular files in
117 the filesystem */
118 u32 folder_count; /* The number of
119 directories in the
120 filesystem */
121 u32 next_id; /* The next available
122 file id number */
123 u32 clumpablks; /* The number of allocation
124 blocks to try to add when
125 extending a file */
126 u32 fs_start; /* The first 512-byte
127 block represented
128 in the bitmap */
129 u32 part_start;
130 u16 root_files; /* The number of
131 regular
132 (non-directory)
133 files in the root
134 directory */
135 u16 root_dirs; /* The number of
136 directories in the
137 root directory */
138 u16 fs_ablocks; /* The number of
139 allocation blocks
140 in the filesystem */
141 u16 free_ablocks; /* the number of unused
142 allocation blocks
143 in the filesystem */
144 u32 alloc_blksz; /* The size of an
145 "allocation block" */
146 int s_quiet; /* Silent failure when
147 changing owner or mode? */
148 __be32 s_type; /* Type for new files */
149 __be32 s_creator; /* Creator for new files */
150 umode_t s_file_umask; /* The umask applied to the
151 permissions on all files */
152 umode_t s_dir_umask; /* The umask applied to the
153 permissions on all dirs */
Eric W. Biederman43b5e4c2012-02-07 16:26:59 -0800154 kuid_t s_uid; /* The uid of all files */
155 kgid_t s_gid; /* The gid of all files */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 int session, part;
Roman Zippel328b9222005-09-06 15:18:49 -0700158 struct nls_table *nls_io, *nls_disk;
Matthias Kaehlcke3084b722008-07-25 01:46:34 -0700159 struct mutex bitmap_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 u16 blockoffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 int fs_div;
Artem Bityutskiyb16ca622012-07-12 17:28:48 +0300163 struct super_block *sb;
Artem Bityutskiy5687b572012-07-12 17:28:49 +0300164 int work_queued; /* non-zero delayed work is queued */
165 struct delayed_work mdb_work; /* MDB flush delayed work */
166 spinlock_t work_lock; /* protects mdb_work and work_queued */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167};
168
169#define HFS_FLG_BITMAP_DIRTY 0
170#define HFS_FLG_MDB_DIRTY 1
171#define HFS_FLG_ALT_MDB_DIRTY 2
172
173/* bitmap.c */
174extern u32 hfs_vbm_search_free(struct super_block *, u32, u32 *);
175extern int hfs_clear_vbm_bits(struct super_block *, u16, u16);
176
177/* catalog.c */
178extern int hfs_cat_keycmp(const btree_key *, const btree_key *);
179struct hfs_find_data;
180extern int hfs_cat_find_brec(struct super_block *, u32, struct hfs_find_data *);
Al Viro71e93962016-07-20 16:22:29 -0400181extern int hfs_cat_create(u32, struct inode *, const struct qstr *, struct inode *);
182extern int hfs_cat_delete(u32, struct inode *, const struct qstr *);
183extern int hfs_cat_move(u32, struct inode *, const struct qstr *,
184 struct inode *, const struct qstr *);
185extern void hfs_cat_build_key(struct super_block *, btree_key *, u32, const struct qstr *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187/* dir.c */
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800188extern const struct file_operations hfs_dir_operations;
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800189extern const struct inode_operations hfs_dir_inode_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191/* extent.c */
192extern int hfs_ext_keycmp(const btree_key *, const btree_key *);
193extern int hfs_free_fork(struct super_block *, struct hfs_cat_file *, int);
Alexey Khoroshilov9509f172013-04-30 15:27:52 -0700194extern int hfs_ext_write_extent(struct inode *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195extern int hfs_extend_file(struct inode *);
196extern void hfs_file_truncate(struct inode *);
197
198extern int hfs_get_block(struct inode *, sector_t, struct buffer_head *, int);
199
200/* inode.c */
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700201extern const struct address_space_operations hfs_aops;
202extern const struct address_space_operations hfs_btree_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Al Viro71e93962016-07-20 16:22:29 -0400204extern struct inode *hfs_new_inode(struct inode *, const struct qstr *, umode_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205extern void hfs_inode_write_fork(struct inode *, struct hfs_extent *, __be32 *, __be32 *);
Christoph Hellwiga9185b42010-03-05 09:21:37 +0100206extern int hfs_write_inode(struct inode *, struct writeback_control *);
Christian Brauner549c7292021-01-21 14:19:43 +0100207extern int hfs_inode_setattr(struct user_namespace *, struct dentry *,
208 struct iattr *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209extern void hfs_inode_read_fork(struct inode *inode, struct hfs_extent *ext,
210 __be32 log_size, __be32 phys_size, u32 clump_size);
211extern struct inode *hfs_iget(struct super_block *, struct hfs_cat_key *, hfs_cat_rec *);
Al Virob57922d2010-06-07 14:34:48 -0400212extern void hfs_evict_inode(struct inode *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213extern void hfs_delete_inode(struct inode *);
214
215/* attr.c */
Andreas Gruenbacherb8020ef2016-09-29 17:48:32 +0200216extern const struct xattr_handler *hfs_xattr_handlers[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
218/* mdb.c */
219extern int hfs_mdb_get(struct super_block *);
220extern void hfs_mdb_commit(struct super_block *);
221extern void hfs_mdb_close(struct super_block *);
222extern void hfs_mdb_put(struct super_block *);
223
224/* part_tbl.c */
225extern int hfs_part_find(struct super_block *, sector_t *, sector_t *);
226
227/* string.c */
Al Viroe16404e2009-02-20 05:55:13 +0000228extern const struct dentry_operations hfs_dentry_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Linus Torvaldsda53be12013-05-21 15:22:44 -0700230extern int hfs_hash_dentry(const struct dentry *, struct qstr *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231extern int hfs_strcmp(const unsigned char *, unsigned int,
232 const unsigned char *, unsigned int);
Al Viro6fa67e72016-07-31 16:37:25 -0400233extern int hfs_compare_dentry(const struct dentry *dentry,
Nick Piggin621e1552011-01-07 17:49:27 +1100234 unsigned int len, const char *str, const struct qstr *name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236/* trans.c */
Al Viro71e93962016-07-20 16:22:29 -0400237extern void hfs_asc2mac(struct super_block *, struct hfs_name *, const struct qstr *);
Roman Zippel328b9222005-09-06 15:18:49 -0700238extern int hfs_mac2asc(struct super_block *, char *, const struct hfs_name *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Artem Bityutskiy5687b572012-07-12 17:28:49 +0300240/* super.c */
241extern void hfs_mark_mdb_dirty(struct super_block *sb);
242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243/*
244 * There are two time systems. Both are based on seconds since
245 * a particular time/date.
Arnd Bergmann4ddfc3dc2018-06-20 09:47:26 +0200246 * Unix: signed little-endian since 00:00 GMT, Jan. 1, 1970
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 * mac: unsigned big-endian since 00:00 GMT, Jan. 1, 1904
248 *
Arnd Bergmann4ddfc3dc2018-06-20 09:47:26 +0200249 * HFS implementations are highly inconsistent, this one matches the
250 * traditional behavior of 64-bit Linux, giving the most useful
251 * time range between 1970 and 2106, by treating any on-disk timestamp
252 * under HFS_UTC_OFFSET (Jan 1 1970) as a time between 2040 and 2106.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 */
Arnd Bergmann4ddfc3dc2018-06-20 09:47:26 +0200254#define HFS_UTC_OFFSET 2082844800U
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Arnd Bergmann4ddfc3dc2018-06-20 09:47:26 +0200256static inline time64_t __hfs_m_to_utime(__be32 mt)
257{
258 time64_t ut = (u32)(be32_to_cpu(mt) - HFS_UTC_OFFSET);
259
260 return ut + sys_tz.tz_minuteswest * 60;
261}
262
263static inline __be32 __hfs_u_to_mtime(time64_t ut)
264{
265 ut -= sys_tz.tz_minuteswest * 60;
266
267 return cpu_to_be32(lower_32_bits(ut) + HFS_UTC_OFFSET);
268}
Rasmus Villemoesdb6172c2015-03-19 12:28:04 +0100269#define HFS_I(inode) (container_of(inode, struct hfs_inode_info, vfs_inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270#define HFS_SB(sb) ((struct hfs_sb_info *)(sb)->s_fs_info)
271
Arnd Bergmann4ddfc3dc2018-06-20 09:47:26 +0200272#define hfs_m_to_utime(time) (struct timespec64){ .tv_sec = __hfs_m_to_utime(time) }
273#define hfs_u_to_mtime(time) __hfs_u_to_mtime((time).tv_sec)
274#define hfs_mtime() __hfs_u_to_mtime(ktime_get_real_seconds())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276static inline const char *hfs_mdb_name(struct super_block *sb)
277{
278 return sb->s_id;
279}
280
281static inline void hfs_bitmap_dirty(struct super_block *sb)
282{
283 set_bit(HFS_FLG_BITMAP_DIRTY, &HFS_SB(sb)->flags);
Artem Bityutskiy5687b572012-07-12 17:28:49 +0300284 hfs_mark_mdb_dirty(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287#define sb_bread512(sb, sec, data) ({ \
288 struct buffer_head *__bh; \
289 sector_t __block; \
290 loff_t __start; \
291 int __offset; \
292 \
293 __start = (loff_t)(sec) << HFS_SECTOR_SIZE_BITS;\
294 __block = __start >> (sb)->s_blocksize_bits; \
295 __offset = __start & ((sb)->s_blocksize - 1); \
296 __bh = sb_bread((sb), __block); \
297 if (likely(__bh != NULL)) \
298 data = (void *)(__bh->b_data + __offset);\
299 else \
300 data = NULL; \
301 __bh; \
302})
303
304#endif