Skip to content

Commit d7d673a

Browse files
saprojtorvalds
authored andcommitted
hfsplus: add HFSX subfolder count support
Adds support for HFSX 'HasFolderCount' flag and a corresponding 'folderCount' field in folder records. (For reference see HFS_FOLDERCOUNT and kHFSHasFolderCountBit/kHFSHasFolderCountMask in Apple's source code.) Ignoring subfolder count leads to fs errors found by Mac: ... Checking catalog hierarchy. HasFolderCount flag needs to be set (id = 105) (It should be 0x10 instead of 0) Incorrect folder count in a directory (id = 2) (It should be 7 instead of 6) ... Steps to reproduce: Format with "newfs_hfs -s /dev/diskXXX". Mount in Linux. Create a new directory in root. Unmount. Run "fsck_hfs /dev/diskXXX". The patch handles directory creation, deletion, and rename. Signed-off-by: Sergei Antonov <saproj@gmail.com> Reviewed-by: Vyacheslav Dubeyko <slava@dubeyko.com> Cc: Al Viro <viro@zeniv.linux.org.uk> Cc: Christoph Hellwig <hch@infradead.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 5394223 commit d7d673a

4 files changed

Lines changed: 55 additions & 2 deletions

File tree

‎fs/hfsplus/catalog.c‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ static int hfsplus_cat_build_record(hfsplus_cat_entry *entry,
103103
folder = &entry->folder;
104104
memset(folder, 0, sizeof(*folder));
105105
folder->type = cpu_to_be16(HFSPLUS_FOLDER);
106+
if (test_bit(HFSPLUS_SB_HFSX, &sbi->flags))
107+
folder->flags |= cpu_to_be16(HFSPLUS_HAS_FOLDER_COUNT);
106108
folder->id = cpu_to_be32(inode->i_ino);
107109
HFSPLUS_I(inode)->create_date =
108110
folder->create_date =
@@ -203,6 +205,36 @@ int hfsplus_find_cat(struct super_block *sb, u32 cnid,
203205
return hfs_brec_find(fd, hfs_find_rec_by_key);
204206
}
205207

208+
static void hfsplus_subfolders_inc(struct inode *dir)
209+
{
210+
struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
211+
212+
if (test_bit(HFSPLUS_SB_HFSX, &sbi->flags)) {
213+
/*
214+
* Increment subfolder count. Note, the value is only meaningful
215+
* for folders with HFSPLUS_HAS_FOLDER_COUNT flag set.
216+
*/
217+
HFSPLUS_I(dir)->subfolders++;
218+
}
219+
}
220+
221+
static void hfsplus_subfolders_dec(struct inode *dir)
222+
{
223+
struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
224+
225+
if (test_bit(HFSPLUS_SB_HFSX, &sbi->flags)) {
226+
/*
227+
* Decrement subfolder count. Note, the value is only meaningful
228+
* for folders with HFSPLUS_HAS_FOLDER_COUNT flag set.
229+
*
230+
* Check for zero. Some subfolders may have been created
231+
* by an implementation ignorant of this counter.
232+
*/
233+
if (HFSPLUS_I(dir)->subfolders)
234+
HFSPLUS_I(dir)->subfolders--;
235+
}
236+
}
237+
206238
int hfsplus_create_cat(u32 cnid, struct inode *dir,
207239
struct qstr *str, struct inode *inode)
208240
{
@@ -247,6 +279,8 @@ int hfsplus_create_cat(u32 cnid, struct inode *dir,
247279
goto err1;
248280

249281
dir->i_size++;
282+
if (S_ISDIR(inode->i_mode))
283+
hfsplus_subfolders_inc(dir);
250284
dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
251285
hfsplus_mark_inode_dirty(dir, HFSPLUS_I_CAT_DIRTY);
252286

@@ -336,6 +370,8 @@ int hfsplus_delete_cat(u32 cnid, struct inode *dir, struct qstr *str)
336370
goto out;
337371

338372
dir->i_size--;
373+
if (type == HFSPLUS_FOLDER)
374+
hfsplus_subfolders_dec(dir);
339375
dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
340376
hfsplus_mark_inode_dirty(dir, HFSPLUS_I_CAT_DIRTY);
341377

@@ -380,6 +416,7 @@ int hfsplus_rename_cat(u32 cnid,
380416

381417
hfs_bnode_read(src_fd.bnode, &entry, src_fd.entryoffset,
382418
src_fd.entrylength);
419+
type = be16_to_cpu(entry.type);
383420

384421
/* create new dir entry with the data from the old entry */
385422
hfsplus_cat_build_key(sb, dst_fd.search_key, dst_dir->i_ino, dst_name);
@@ -394,6 +431,8 @@ int hfsplus_rename_cat(u32 cnid,
394431
if (err)
395432
goto out;
396433
dst_dir->i_size++;
434+
if (type == HFSPLUS_FOLDER)
435+
hfsplus_subfolders_inc(dst_dir);
397436
dst_dir->i_mtime = dst_dir->i_ctime = CURRENT_TIME_SEC;
398437

399438
/* finally remove the old entry */
@@ -405,6 +444,8 @@ int hfsplus_rename_cat(u32 cnid,
405444
if (err)
406445
goto out;
407446
src_dir->i_size--;
447+
if (type == HFSPLUS_FOLDER)
448+
hfsplus_subfolders_dec(src_dir);
408449
src_dir->i_mtime = src_dir->i_ctime = CURRENT_TIME_SEC;
409450

410451
/* remove old thread entry */

‎fs/hfsplus/hfsplus_fs.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ struct hfsplus_inode_info {
242242
*/
243243
sector_t fs_blocks;
244244
u8 userflags; /* BSD user file flags */
245+
u32 subfolders; /* Subfolder count (HFSX only) */
245246
struct list_head open_dir_list;
246247
loff_t phys_size;
247248

‎fs/hfsplus/hfsplus_raw.h‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ struct hfsplus_cat_folder {
261261
struct DInfo user_info;
262262
struct DXInfo finder_info;
263263
__be32 text_encoding;
264-
u32 reserved;
264+
__be32 subfolders; /* Subfolder count in HFSX. Reserved in HFS+. */
265265
} __packed;
266266

267267
/* HFS file info (stolen from hfs.h) */
@@ -301,11 +301,13 @@ struct hfsplus_cat_file {
301301
struct hfsplus_fork_raw rsrc_fork;
302302
} __packed;
303303

304-
/* File attribute bits */
304+
/* File and folder flag bits */
305305
#define HFSPLUS_FILE_LOCKED 0x0001
306306
#define HFSPLUS_FILE_THREAD_EXISTS 0x0002
307307
#define HFSPLUS_XATTR_EXISTS 0x0004
308308
#define HFSPLUS_ACL_EXISTS 0x0008
309+
#define HFSPLUS_HAS_FOLDER_COUNT 0x0010 /* Folder has subfolder count
310+
* (HFSX only) */
309311

310312
/* HFS+ catalog thread (part of a cat_entry) */
311313
struct hfsplus_cat_thread {

‎fs/hfsplus/inode.c‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,7 @@ struct inode *hfsplus_new_inode(struct super_block *sb, umode_t mode)
375375
hip->extent_state = 0;
376376
hip->flags = 0;
377377
hip->userflags = 0;
378+
hip->subfolders = 0;
378379
memset(hip->first_extents, 0, sizeof(hfsplus_extent_rec));
379380
memset(hip->cached_extents, 0, sizeof(hfsplus_extent_rec));
380381
hip->alloc_blocks = 0;
@@ -494,6 +495,10 @@ int hfsplus_cat_read_inode(struct inode *inode, struct hfs_find_data *fd)
494495
inode->i_ctime = hfsp_mt2ut(folder->attribute_mod_date);
495496
HFSPLUS_I(inode)->create_date = folder->create_date;
496497
HFSPLUS_I(inode)->fs_blocks = 0;
498+
if (folder->flags & cpu_to_be16(HFSPLUS_HAS_FOLDER_COUNT)) {
499+
HFSPLUS_I(inode)->subfolders =
500+
be32_to_cpu(folder->subfolders);
501+
}
497502
inode->i_op = &hfsplus_dir_inode_operations;
498503
inode->i_fop = &hfsplus_dir_operations;
499504
} else if (type == HFSPLUS_FILE) {
@@ -566,6 +571,10 @@ int hfsplus_cat_write_inode(struct inode *inode)
566571
folder->content_mod_date = hfsp_ut2mt(inode->i_mtime);
567572
folder->attribute_mod_date = hfsp_ut2mt(inode->i_ctime);
568573
folder->valence = cpu_to_be32(inode->i_size - 2);
574+
if (folder->flags & cpu_to_be16(HFSPLUS_HAS_FOLDER_COUNT)) {
575+
folder->subfolders =
576+
cpu_to_be32(HFSPLUS_I(inode)->subfolders);
577+
}
569578
hfs_bnode_write(fd.bnode, &entry, fd.entryoffset,
570579
sizeof(struct hfsplus_cat_folder));
571580
} else if (HFSPLUS_IS_RSRC(inode)) {

0 commit comments

Comments
 (0)