hfsplus: read support for directory hardlinks
Subsystems (2)
PATCH 1/1 hfsplus: read support for directory hardlinks
With OS X 10.5 in general, and Time Machine backups in particular, Apple added support for directory hardlinks in HFS+. As for file hardlinks, directory hardlinks are represented by normal files with special creator and type attributes, storing an pseudo inode number in the link count, referencing a secret file in a special folder. This patch extends the existing lookup of file hardlinks to also support folder hardlinks.
--- fs/hfsplus/catalog.c | 2 +- fs/hfsplus/dir.c | 59 +++++++++++++++++++++++++++++++++++---------- fs/hfsplus/hfsplus_fs.h | 1 + fs/hfsplus/hfsplus_raw.h | 5 ++++ fs/hfsplus/super.c | 55 +++++++++++++++++++++++++++++------------- 5 files changed, 91 insertions(+), 31 deletions(-)
fs/hfsplus/catalog.c +1 -1
@@ -109,7 +109,7 @@ static int hfsplus_cat_build_record(hfsplus_cat_entry *entry, folder->attribute_mod_date = folder->access_date =hfsp_now2mt ();hfsplus_cat_set_perms (inode, &folder->permissions); -if (inode == sbi->hidden_dir) +if (inode == sbi->hidden_dir || inode == sbi->alias_dir)/* invisible and namelocked */ folder->user_info.frFlags =cpu_to_be16 (0x5000 );return sizeof (*folder);
fs/hfsplus/dir.c +46 -13
93 lines not on this page · raw message
fs/hfsplus/hfsplus_fs.h +1 -0
@@ -117,6 +117,7 @@ struct hfsplus_sb_info {struct hfs_btree *attr_tree;struct inode *alloc_file;struct inode *hidden_dir; +struct inode *alias_dir;struct nls_table *nls;/* Runtime variables */
fs/hfsplus/hfsplus_raw.h +5 -0
@@ -36,6 +36,11 @@#define HFSP_WRAPOFF_EMBEDSIG 0x7C#define HFSP_WRAPOFF_EMBEDEXT 0x7E +#define HFSP_ALIASDIR_NAME ".HFS+ Private Directory Data\r" + +#define HFSP_FOLDER_ALIAS_TYPE 0x66647270/* 'fdrp' */ +#define HFSP_MACS_CREATOR 0x4d414353/* 'MACS' */ +#define HFSP_HIDDENDIR_NAME \ "\xe2\x90\x80\xe2\x90\x80\xe2\x90\x80\xe2\x90\x80HFS+ Private Data"
fs/hfsplus/super.c +38 -17
89 lines not on this page · raw message
Signature · 1 line
1.7.5
Christoph Hellwig
AppliedI've applied it with some minor editing to make the indentation and variable names match the surrounding code a tad better.
Thanks a lot Gustav!
Al Viro
maintainerQuoted 4 lines · Christoph Hellwig
I've applied it with some minor editing to make the indentation and variable names match the surrounding code a tad better.
Thanks a lot Gustav!
What happens to rename() with that thing? Specifically, what happens to loop detection?
Quoted 2 lines · Al Viro
What happens to
rename()with that thing? Specifically, what happens to loop detection?
Good point.
Since my main interest was retrieving contents of a backup, I did not much consider the impact of write operations. Before the patch, the source files were exposed as normal files, and the target directories as normal directories, so one could easily have introduced loops with rename operations before.
In line with the read-only support, I would suggest to conservatively prevent any rename where the source is a directory hardlink. I have prepared a patch along those lines, but I cannot test it right now.
PATCH 1/1 hfsplus: disable rename of directory hardlinks
--- fs/hfsplus/dir.c | 9 ++++++++- 1 files changed, 8 insertions(+), 1 deletions(-)
fs/hfsplus/dir.c +8 -1
@@ -108,7 +108,7 @@ again: inode =hfsplus_iget (dir->i_sb, cnid);if (IS_ERR (inode))return ERR_CAST (inode); -if (S_ISREG (inode->i_mode)) +if (S_ISREG (inode->i_mode) ||S_ISDIR (inode->i_mode))HFSPLUS_I (inode)->linkid = linkid; out:d_add (dentry, inode);
@@ -465,6 +465,13 @@ static int hfsplus_rename(struct inode *old_dir, struct dentry *old_dentry, {int res; +/* Renaming directory hardlinks to could create loops. +* Conservatively prevent any hardlink renaming. +*/ +if (S_ISDIR (old_dentry->d_inode->i_mode) && +HFSPLUS_I (old_dentry->d_inode)->linkid) +return -EPERM ; +/* Unlink destination if it already exists */ if (new_dentry->d_inode) {if (S_ISDIR (new_dentry->d_inode->i_mode))
Signature · 1 line
1.7.5
Re: [PATCH] hfsplus: disable rename of directory hardlinks
Quoted 19 lines · Gustav Munkby
Signed-off-by Gustav Munkby <[email protected]>---
fs/hfsplus/dir.c| 9 ++++++++-1 files changed, 8 insertions(+), 1 deletions(-)fs/hfsplus/dir.c +1
-1@@ -108,7 +108,7 @@ again: inode =hfsplus_iget (dir->i_sb, cnid);if (IS_ERR (inode))return ERR_CAST (inode); -if (S_ISREG (inode->i_mode)) +if (S_ISREG (inode->i_mode) ||S_ISDIR (inode->i_mode))HFSPLUS_I (inode)->linkid = linkid;out:
d_add(dentry, inode);
fs/hfsplus/dir.c static int hfsplus_rename(struct inode *old_dir, struct dentry *old_dentry, · line 470
Quoted 6 lines
fs/hfsplus/dir.c +3
-0@@ -465,6 +465,13 @@ static int hfsplus_rename(struct inode *old_dir, struct dentry *old_dentry, {int res; +/* Renaming directory hardlinks to could create loops. +* Conservatively prevent any hardlink renaming. +*/
It should also be safe to rename a directory hard link if the source and target parent directories are the same.
This would allow the case of changing the directory name without moving it to another location.
Quoted 14 lines
fs/hfsplus/dir.c +4
-0+if (S_ISDIR (old_dentry->d_inode->i_mode) && +HFSPLUS_I (old_dentry->d_inode)->linkid) +return -EPERM ; +/* Unlink destination if it already exists */ if (new_dentry->d_inode) {if (S_ISDIR (new_dentry->d_inode->i_mode))Signature · 6 lines
1.7.5 -- To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [PATCH] hfsplus: disable rename of directory hardlinks
Quoted 7 lines
fs/hfsplus/dir.c static int hfsplus_rename(struct inode *old_dir, struct dentry *old_dentry, · line 470
Quoted 3 lines
fs/hfsplus/dir.c +3
-0+/* Renaming directory hardlinks to could create loops. +* Conservatively prevent any hardlink renaming. +*/ It should also be safe to rename a directory hard link if the source and target parent directories are the same.
This would allow the case of changing the directory name without moving it to another location.
Theoretically, yes. In fact, it seems the implementation on OS X is even more liberal by alos allowing moves into other directories as long as the target is not already beneath another hardlink.
However, for that to make sense, proper support for renaming hardlinks (files as well as directories) must first be implemented. The way I understand the hfsplus code both rename and unlink is currently broken for any type of hardlink, because it operates on the link target rather than the link source. Hardlinks in HFS+ are represented much like low-level soft links, with one target file/directory in a private system directory, and several link sources. Operating on the link target would invalidate all link sources on rename/unlink, which is clearly not desired.
Quoted 10 lines · Gustav Munkby
Quoted 2 lines · Al Viro
What happens to
rename()with that thing? Specifically, what happens to loop detection?Good point.
Since my main interest was retrieving contents of a backup, I did not much consider the impact of write operations. Before the patch, the source files were exposed as normal files, and the target directories as normal directories, so one could easily have introduced loops with rename operations before.
Given that you have the filesystem around can you clarify where the additional directory links live? I was under the impression they are in hidden backup directories not normally accessible, so there's always just one link in the normal namespace.
The basic idea would be that we allow any kind of renames in the normal namespace, but disallow any kind of operation involving the time machine backups.
Quoted 12 lines · Christoph Hellwig
Quoted 10 lines · Gustav Munkby
Quoted 2 lines · Al Viro
What happens to
rename()with that thing? Specifically, what happens to loop detection?Good point.
Since my main interest was retrieving contents of a backup, I did not much consider the impact of write operations. Before the patch, the source files were exposed as normal files, and the target directories as normal directories, so one could easily have introduced loops with rename operations before.
Quoted 9 lines
Given that you have the filesystem around can you clarify where the additional directory links live? I was under the impression they are in hidden backup directories not normally accessible, so there's always just one link in the normal namespace.
The basic idea would be that we allow any kind of renames in the normal namespace, but disallow any kind of operation involving the time machine backups.
You are correct, the hardlink targets are all inside a hidden private directory. And then there are hardlink sources both inside and outside this hidden directory linking to directories inside this directory. While the hidden directory is not directly accessible to clients, its subdirectories are through the hardlinks.
Perhaps it was obvious to everyone else, but I just realized that it is not enough to prevent moving the directory hardlinks themselves, but all directory renames must be limited (or checked), since the old directory might contain a nested directory hardlink.Disallowing renames of directories altogether is clearly not a viable option.
The following three (somewhat overlapping) classes of renames should always be possible without any risk for introducing loops.
1) Allow renames where target directory is an ancestor of source directory.
2) Allow renames that doesn't change the closest hardlink ancestor.
3) Allow renames into target directories without hardlinked parents.
The first check seems simple enough to implement, but the other two might require additional locking as far as I understand. Alternatively, they could both be implemented by adding some additional flags/data to dentry->d_fsdata.
Given that our view of how these links behave was a bit too simplistic I'll drop the patch from my tree for now. I'm actually not quite sure if we can implement the required locking correctly. It would be interesting to see what Apple does given that the hfsplus code is part of the xnu source tarball, but given their general implementaton quality I'd expect something utterly hacky.
Quoted 6 lines · Christoph Hellwig
Given that our view of how these links behave was a bit too simplistic I'll drop the patch from my tree for now. I'm actually not quite sure if we can implement the required locking correctly. It would be interesting to see what Apple does given that the hfsplus code is part of the xnu source tarball, but given their general implementaton quality I'd expect something utterly hacky.
I really don't see how the patch makes anything worse. In fact it makes things slightly better by preventing modifications of the files in the hidden directory. The raw files are currently exposed and all renames possible with the patch are equally possible without it.
Moreover, I've done some more tests the last few weeks, and it turns out that Apple only implements folder hardlinks for journaled HFS+. Since journaled HFS+ is only supported readonly by the hfsplus driver, the write operations are currently not an issue.
It seems the appropriate read-only patch would ensure that the destination folder is always hidden, and folder hardlinks are only ever resolved when the filesystem is mounted read only. This will:
- prevent creation of folder hardlinks independent of whether the filesystem is readonly or not;
- enable navigation of any folder hardlinks when mounted readonly;
- ensure all write operations work well for files not beneath a folder hardlink, including all files on filesystems without folder hardlinks.
Thoughts?
PATCH v2 1/1 hfsplus: readonly support for directory hardlinks
Add support for resolving directory hardlinks as introduced with Apple OS X 10.5 in general, and Time Machine backups in particular. Prevent tampering of directory hardlink metadata when mounted writable. Limit lookup to readonly filesystems to prevent rename from creating filesystem loops.
--- fs/hfsplus/catalog.c | 2 +- fs/hfsplus/dir.c | 70 ++++++++++++++++++++++++++++++++++++--------- fs/hfsplus/hfsplus_fs.h | 1 + fs/hfsplus/hfsplus_raw.h | 5 +++ fs/hfsplus/super.c | 54 ++++++++++++++++++++++++----------- 5 files changed, 100 insertions(+), 32 deletions(-)
fs/hfsplus/catalog.c +1 -1
@@ -109,7 +109,7 @@ static int hfsplus_cat_build_record(hfsplus_cat_entry *entry, folder->attribute_mod_date = folder->access_date =hfsp_now2mt ();hfsplus_cat_set_perms (inode, &folder->permissions); -if (inode == sbi->hidden_dir) +if (inode == sbi->hidden_dir || inode == sbi->alias_dir)/* invisible and namelocked */ folder->user_info.frFlags =cpu_to_be16 (0x5000 );return sizeof (*folder);
fs/hfsplus/dir.c +56 -14
110 lines not on this page · raw message
fs/hfsplus/hfsplus_fs.h +1 -0
@@ -117,6 +117,7 @@ struct hfsplus_sb_info {struct hfs_btree *attr_tree;struct inode *alloc_file;struct inode *hidden_dir; +struct inode *alias_dir;struct nls_table *nls;/* Runtime variables */
fs/hfsplus/hfsplus_raw.h +5 -0
@@ -36,6 +36,11 @@#define HFSP_WRAPOFF_EMBEDSIG 0x7C#define HFSP_WRAPOFF_EMBEDEXT 0x7E +#define HFSP_ALIASDIR_NAME ".HFS+ Private Directory Data\r" + +#define HFSP_FOLDER_ALIAS_TYPE 0x66647270/* 'fdrp' */ +#define HFSP_MACS_CREATOR 0x4d414353/* 'MACS' */ +#define HFSP_HIDDENDIR_NAME \ "\xe2\x90\x80\xe2\x90\x80\xe2\x90\x80\xe2\x90\x80HFS+ Private Data"
fs/hfsplus/super.c +37 -17
88 lines not on this page · raw message
Signature · 1 line
1.7.5.1