Skip to content

Commit d45bce8

Browse files
duanegLinus Torvalds
authored andcommitted
HFS+: add custom dentry hash and comparison operations
Add custom dentry hash and comparison operations for HFS+ filesystems that are case-insensitive and/or do automatic unicode decomposition. The new operations reuse the existing HFS+ ASCII to unicode conversion, unicode decomposition and case folding functionality. Signed-off-by: Duane Griffin <duaneg@dghda.com> Signed-off-by: Roman Zippel <zippel@linux-m68k.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 1e96b7c commit d45bce8

6 files changed

Lines changed: 138 additions & 1 deletion

File tree

‎fs/hfsplus/btree.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,10 @@ struct hfs_btree *hfs_btree_open(struct super_block *sb, u32 id)
6262
if ((HFSPLUS_SB(sb).flags & HFSPLUS_SB_HFSX) &&
6363
(head->key_type == HFSPLUS_KEY_BINARY))
6464
tree->keycmp = hfsplus_cat_bin_cmp_key;
65-
else
65+
else {
6666
tree->keycmp = hfsplus_cat_case_cmp_key;
67+
HFSPLUS_SB(sb).flags |= HFSPLUS_SB_CASEFOLD;
68+
}
6769
} else {
6870
printk(KERN_ERR "hfs: unknown B*Tree requested\n");
6971
goto fail_page;

‎fs/hfsplus/dir.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ static struct dentry *hfsplus_lookup(struct inode *dir, struct dentry *dentry,
3636
u16 type;
3737

3838
sb = dir->i_sb;
39+
40+
dentry->d_op = &hfsplus_dentry_operations;
3941
dentry->d_fsdata = NULL;
4042
hfs_find_init(HFSPLUS_SB(sb).cat_tree, &fd);
4143
hfsplus_cat_build_key(sb, fd.search_key, dir->i_ino, &dentry->d_name);

‎fs/hfsplus/hfsplus_fs.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ struct hfsplus_sb_info {
150150
#define HFSPLUS_SB_NODECOMPOSE 0x0002
151151
#define HFSPLUS_SB_FORCE 0x0004
152152
#define HFSPLUS_SB_HFSX 0x0008
153+
#define HFSPLUS_SB_CASEFOLD 0x0010
153154

154155

155156
struct hfsplus_inode_info {
@@ -321,6 +322,7 @@ void hfsplus_file_truncate(struct inode *);
321322
/* inode.c */
322323
extern const struct address_space_operations hfsplus_aops;
323324
extern const struct address_space_operations hfsplus_btree_aops;
325+
extern struct dentry_operations hfsplus_dentry_operations;
324326

325327
void hfsplus_inode_read_fork(struct inode *, struct hfsplus_fork_raw *);
326328
void hfsplus_inode_write_fork(struct inode *, struct hfsplus_fork_raw *);
@@ -353,6 +355,8 @@ int hfsplus_strcasecmp(const struct hfsplus_unistr *, const struct hfsplus_unist
353355
int hfsplus_strcmp(const struct hfsplus_unistr *, const struct hfsplus_unistr *);
354356
int hfsplus_uni2asc(struct super_block *, const struct hfsplus_unistr *, char *, int *);
355357
int hfsplus_asc2uni(struct super_block *, struct hfsplus_unistr *, const char *, int);
358+
int hfsplus_hash_dentry(struct dentry *dentry, struct qstr *str);
359+
int hfsplus_compare_dentry(struct dentry *dentry, struct qstr *s1, struct qstr *s2);
356360

357361
/* wrapper.c */
358362
int hfsplus_read_wrapper(struct super_block *);

‎fs/hfsplus/inode.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,11 @@ const struct address_space_operations hfsplus_aops = {
131131
.writepages = hfsplus_writepages,
132132
};
133133

134+
struct dentry_operations hfsplus_dentry_operations = {
135+
.d_hash = hfsplus_hash_dentry,
136+
.d_compare = hfsplus_compare_dentry,
137+
};
138+
134139
static struct dentry *hfsplus_file_lookup(struct inode *dir, struct dentry *dentry,
135140
struct nameidata *nd)
136141
{

‎fs/hfsplus/super.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,7 @@ static int hfsplus_fill_super(struct super_block *sb, void *data, int silent)
380380
iput(root);
381381
goto cleanup;
382382
}
383+
sb->s_root->d_op = &hfsplus_dentry_operations;
383384

384385
str.len = sizeof(HFSP_HIDDENDIR_NAME) - 1;
385386
str.name = HFSP_HIDDENDIR_NAME;

‎fs/hfsplus/unicode.c‎

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,3 +314,126 @@ int hfsplus_asc2uni(struct super_block *sb, struct hfsplus_unistr *ustr,
314314
return -ENAMETOOLONG;
315315
return 0;
316316
}
317+
318+
/*
319+
* Hash a string to an integer as appropriate for the HFS+ filesystem.
320+
* Composed unicode characters are decomposed and case-folding is performed
321+
* if the appropriate bits are (un)set on the superblock.
322+
*/
323+
int hfsplus_hash_dentry(struct dentry *dentry, struct qstr *str)
324+
{
325+
struct super_block *sb = dentry->d_sb;
326+
const char *astr;
327+
const u16 *dstr;
328+
int casefold, decompose, size, dsize, len;
329+
unsigned long hash;
330+
wchar_t c;
331+
u16 c2;
332+
333+
casefold = (HFSPLUS_SB(sb).flags & HFSPLUS_SB_CASEFOLD);
334+
decompose = !(HFSPLUS_SB(sb).flags & HFSPLUS_SB_NODECOMPOSE);
335+
hash = init_name_hash();
336+
astr = str->name;
337+
len = str->len;
338+
while (len > 0) {
339+
size = asc2unichar(sb, astr, len, &c);
340+
astr += size;
341+
len -= size;
342+
343+
if (decompose && (dstr = decompose_unichar(c, &dsize))) {
344+
do {
345+
c2 = *dstr++;
346+
if (!casefold || (c2 = case_fold(c2)))
347+
hash = partial_name_hash(c2, hash);
348+
} while (--dsize > 0);
349+
} else {
350+
c2 = c;
351+
if (!casefold || (c2 = case_fold(c2)))
352+
hash = partial_name_hash(c2, hash);
353+
}
354+
}
355+
str->hash = end_name_hash(hash);
356+
357+
return 0;
358+
}
359+
360+
/*
361+
* Compare strings with HFS+ filename ordering.
362+
* Composed unicode characters are decomposed and case-folding is performed
363+
* if the appropriate bits are (un)set on the superblock.
364+
*/
365+
int hfsplus_compare_dentry(struct dentry *dentry, struct qstr *s1, struct qstr *s2)
366+
{
367+
struct super_block *sb = dentry->d_sb;
368+
int casefold, decompose, size;
369+
int dsize1, dsize2, len1, len2;
370+
const u16 *dstr1, *dstr2;
371+
const char *astr1, *astr2;
372+
u16 c1, c2;
373+
wchar_t c;
374+
375+
casefold = (HFSPLUS_SB(sb).flags & HFSPLUS_SB_CASEFOLD);
376+
decompose = !(HFSPLUS_SB(sb).flags & HFSPLUS_SB_NODECOMPOSE);
377+
astr1 = s1->name;
378+
len1 = s1->len;
379+
astr2 = s2->name;
380+
len2 = s2->len;
381+
dsize1 = dsize2 = 0;
382+
dstr1 = dstr2 = NULL;
383+
384+
while (len1 > 0 && len2 > 0) {
385+
if (!dsize1) {
386+
size = asc2unichar(sb, astr1, len1, &c);
387+
astr1 += size;
388+
len1 -= size;
389+
390+
if (!decompose || !(dstr1 = decompose_unichar(c, &dsize1))) {
391+
c1 = c;
392+
dstr1 = &c1;
393+
dsize1 = 1;
394+
}
395+
}
396+
397+
if (!dsize2) {
398+
size = asc2unichar(sb, astr2, len2, &c);
399+
astr2 += size;
400+
len2 -= size;
401+
402+
if (!decompose || !(dstr2 = decompose_unichar(c, &dsize2))) {
403+
c2 = c;
404+
dstr2 = &c2;
405+
dsize2 = 1;
406+
}
407+
}
408+
409+
c1 = *dstr1;
410+
c2 = *dstr2;
411+
if (casefold) {
412+
if (!(c1 = case_fold(c1))) {
413+
dstr1++;
414+
dsize1--;
415+
continue;
416+
}
417+
if (!(c2 = case_fold(c2))) {
418+
dstr2++;
419+
dsize2--;
420+
continue;
421+
}
422+
}
423+
if (c1 < c2)
424+
return -1;
425+
else if (c1 > c2)
426+
return 1;
427+
428+
dstr1++;
429+
dsize1--;
430+
dstr2++;
431+
dsize2--;
432+
}
433+
434+
if (len1 < len2)
435+
return -1;
436+
if (len1 > len2)
437+
return 1;
438+
return 0;
439+
}

0 commit comments

Comments
 (0)