1 // SPDX-License-Identifier: GPL-2.0
3 * linux/fs/hfsplus/dir.c
6 * Brad Boyer (flar@allandria.com)
7 * (C) 2003 Ardis Technologies <roman@ardistech.com>
9 * Handling of directories
12 #include <linux/errno.h>
14 #include <linux/slab.h>
15 #include <linux/random.h>
16 #include <linux/nls.h>
18 #include "hfsplus_fs.h"
19 #include "hfsplus_raw.h"
23 static inline void hfsplus_instantiate(struct dentry
*dentry
,
24 struct inode
*inode
, u32 cnid
)
26 dentry
->d_fsdata
= (void *)(unsigned long)cnid
;
27 d_instantiate(dentry
, inode
);
30 /* Find the entry inside dir named dentry->d_name */
31 static struct dentry
*hfsplus_lookup(struct inode
*dir
, struct dentry
*dentry
,
34 struct inode
*inode
= NULL
;
35 struct hfs_find_data fd
;
36 struct super_block
*sb
;
37 hfsplus_cat_entry entry
;
44 dentry
->d_fsdata
= NULL
;
45 err
= hfs_find_init(HFSPLUS_SB(sb
)->cat_tree
, &fd
);
48 err
= hfsplus_cat_build_key(sb
, fd
.search_key
, dir
->i_ino
,
50 if (unlikely(err
< 0))
53 err
= hfs_brec_read(&fd
, &entry
, sizeof(entry
));
63 type
= be16_to_cpu(entry
.type
);
64 if (type
== HFSPLUS_FOLDER
) {
65 if (fd
.entrylength
< sizeof(struct hfsplus_cat_folder
)) {
69 cnid
= be32_to_cpu(entry
.folder
.id
);
70 dentry
->d_fsdata
= (void *)(unsigned long)cnid
;
71 } else if (type
== HFSPLUS_FILE
) {
72 if (fd
.entrylength
< sizeof(struct hfsplus_cat_file
)) {
76 cnid
= be32_to_cpu(entry
.file
.id
);
77 if (entry
.file
.user_info
.fdType
==
78 cpu_to_be32(HFSP_HARDLINK_TYPE
) &&
79 entry
.file
.user_info
.fdCreator
==
80 cpu_to_be32(HFSP_HFSPLUS_CREATOR
) &&
81 (entry
.file
.create_date
==
82 HFSPLUS_I(HFSPLUS_SB(sb
)->hidden_dir
)->
84 entry
.file
.create_date
==
85 HFSPLUS_I(d_inode(sb
->s_root
))->
87 HFSPLUS_SB(sb
)->hidden_dir
) {
91 if (dentry
->d_fsdata
) {
93 * We found a link pointing to another link,
94 * so ignore it and treat it as regular file.
96 cnid
= (unsigned long)dentry
->d_fsdata
;
99 dentry
->d_fsdata
= (void *)(unsigned long)cnid
;
101 be32_to_cpu(entry
.file
.permissions
.dev
);
102 str
.len
= sprintf(name
, "iNode%d", linkid
);
104 err
= hfsplus_cat_build_key(sb
, fd
.search_key
,
105 HFSPLUS_SB(sb
)->hidden_dir
->i_ino
,
107 if (unlikely(err
< 0))
111 } else if (!dentry
->d_fsdata
)
112 dentry
->d_fsdata
= (void *)(unsigned long)cnid
;
114 pr_err("invalid catalog entry type in lookup\n");
119 inode
= hfsplus_iget(dir
->i_sb
, cnid
);
121 return ERR_CAST(inode
);
122 if (S_ISREG(inode
->i_mode
))
123 HFSPLUS_I(inode
)->linkid
= linkid
;
125 d_add(dentry
, inode
);
132 static int hfsplus_readdir(struct file
*file
, struct dir_context
*ctx
)
134 struct inode
*inode
= file_inode(file
);
135 struct super_block
*sb
= inode
->i_sb
;
138 hfsplus_cat_entry entry
;
139 struct hfs_find_data fd
;
140 struct hfsplus_readdir_data
*rd
;
143 if (file
->f_pos
>= inode
->i_size
)
146 err
= hfs_find_init(HFSPLUS_SB(sb
)->cat_tree
, &fd
);
149 strbuf
= kmalloc(NLS_MAX_CHARSET_SIZE
* HFSPLUS_MAX_STRLEN
+ 1, GFP_KERNEL
);
154 hfsplus_cat_build_key_with_cnid(sb
, fd
.search_key
, inode
->i_ino
);
155 err
= hfs_brec_find(&fd
, hfs_find_rec_by_key
);
160 /* This is completely artificial... */
161 if (!dir_emit_dot(file
, ctx
))
166 if (fd
.entrylength
> sizeof(entry
) || fd
.entrylength
< 0) {
171 hfs_bnode_read(fd
.bnode
, &entry
, fd
.entryoffset
,
173 if (be16_to_cpu(entry
.type
) != HFSPLUS_FOLDER_THREAD
) {
174 pr_err("bad catalog folder thread\n");
178 if (fd
.entrylength
< HFSPLUS_MIN_THREAD_SZ
) {
179 pr_err("truncated catalog thread\n");
183 if (!dir_emit(ctx
, "..", 2,
184 be32_to_cpu(entry
.thread
.parentID
), DT_DIR
))
188 if (ctx
->pos
>= inode
->i_size
)
190 err
= hfs_brec_goto(&fd
, ctx
->pos
- 1);
194 if (be32_to_cpu(fd
.key
->cat
.parent
) != inode
->i_ino
) {
195 pr_err("walked past end of dir\n");
200 if (fd
.entrylength
> sizeof(entry
) || fd
.entrylength
< 0) {
205 hfs_bnode_read(fd
.bnode
, &entry
, fd
.entryoffset
,
207 type
= be16_to_cpu(entry
.type
);
208 len
= NLS_MAX_CHARSET_SIZE
* HFSPLUS_MAX_STRLEN
;
209 err
= hfsplus_uni2asc(sb
, &fd
.key
->cat
.name
, strbuf
, &len
);
212 if (type
== HFSPLUS_FOLDER
) {
214 sizeof(struct hfsplus_cat_folder
)) {
215 pr_err("small dir entry\n");
219 if (HFSPLUS_SB(sb
)->hidden_dir
&&
220 HFSPLUS_SB(sb
)->hidden_dir
->i_ino
==
221 be32_to_cpu(entry
.folder
.id
))
223 if (!dir_emit(ctx
, strbuf
, len
,
224 be32_to_cpu(entry
.folder
.id
), DT_DIR
))
226 } else if (type
== HFSPLUS_FILE
) {
228 unsigned type
= DT_UNKNOWN
;
230 if (fd
.entrylength
< sizeof(struct hfsplus_cat_file
)) {
231 pr_err("small file entry\n");
236 mode
= be16_to_cpu(entry
.file
.permissions
.mode
);
239 else if (S_ISLNK(mode
))
241 else if (S_ISFIFO(mode
))
243 else if (S_ISCHR(mode
))
245 else if (S_ISBLK(mode
))
247 else if (S_ISSOCK(mode
))
250 if (!dir_emit(ctx
, strbuf
, len
,
251 be32_to_cpu(entry
.file
.id
), type
))
254 pr_err("bad catalog entry type\n");
260 if (ctx
->pos
>= inode
->i_size
)
262 err
= hfs_brec_goto(&fd
, 1);
266 rd
= file
->private_data
;
268 rd
= kmalloc(sizeof(struct hfsplus_readdir_data
), GFP_KERNEL
);
273 file
->private_data
= rd
;
275 spin_lock(&HFSPLUS_I(inode
)->open_dir_lock
);
276 list_add(&rd
->list
, &HFSPLUS_I(inode
)->open_dir_list
);
277 spin_unlock(&HFSPLUS_I(inode
)->open_dir_lock
);
280 * Can be done after the list insertion; exclusion with
281 * hfsplus_delete_cat() is provided by directory lock.
283 memcpy(&rd
->key
, fd
.key
, sizeof(struct hfsplus_cat_key
));
290 static int hfsplus_dir_release(struct inode
*inode
, struct file
*file
)
292 struct hfsplus_readdir_data
*rd
= file
->private_data
;
294 spin_lock(&HFSPLUS_I(inode
)->open_dir_lock
);
296 spin_unlock(&HFSPLUS_I(inode
)->open_dir_lock
);
302 static int hfsplus_link(struct dentry
*src_dentry
, struct inode
*dst_dir
,
303 struct dentry
*dst_dentry
)
305 struct hfsplus_sb_info
*sbi
= HFSPLUS_SB(dst_dir
->i_sb
);
306 struct inode
*inode
= d_inode(src_dentry
);
307 struct inode
*src_dir
= d_inode(src_dentry
->d_parent
);
313 if (HFSPLUS_IS_RSRC(inode
))
315 if (!S_ISREG(inode
->i_mode
))
318 mutex_lock(&sbi
->vh_mutex
);
319 if (inode
->i_ino
== (u32
)(unsigned long)src_dentry
->d_fsdata
) {
321 get_random_bytes(&id
, sizeof(cnid
));
324 str
.len
= sprintf(name
, "iNode%d", id
);
325 res
= hfsplus_rename_cat(inode
->i_ino
,
326 src_dir
, &src_dentry
->d_name
,
327 sbi
->hidden_dir
, &str
);
333 HFSPLUS_I(inode
)->linkid
= id
;
334 cnid
= sbi
->next_cnid
++;
335 src_dentry
->d_fsdata
= (void *)(unsigned long)cnid
;
336 res
= hfsplus_create_cat(cnid
, src_dir
,
337 &src_dentry
->d_name
, inode
);
343 cnid
= sbi
->next_cnid
++;
344 res
= hfsplus_create_cat(cnid
, dst_dir
, &dst_dentry
->d_name
, inode
);
349 hfsplus_instantiate(dst_dentry
, inode
, cnid
);
351 inode
->i_ctime
= current_time(inode
);
352 mark_inode_dirty(inode
);
354 hfsplus_mark_mdb_dirty(dst_dir
->i_sb
);
356 mutex_unlock(&sbi
->vh_mutex
);
360 static int hfsplus_unlink(struct inode
*dir
, struct dentry
*dentry
)
362 struct hfsplus_sb_info
*sbi
= HFSPLUS_SB(dir
->i_sb
);
363 struct inode
*inode
= d_inode(dentry
);
369 if (HFSPLUS_IS_RSRC(inode
))
372 mutex_lock(&sbi
->vh_mutex
);
373 cnid
= (u32
)(unsigned long)dentry
->d_fsdata
;
374 if (inode
->i_ino
== cnid
&&
375 atomic_read(&HFSPLUS_I(inode
)->opencnt
)) {
377 str
.len
= sprintf(name
, "temp%lu", inode
->i_ino
);
378 res
= hfsplus_rename_cat(inode
->i_ino
,
379 dir
, &dentry
->d_name
,
380 sbi
->hidden_dir
, &str
);
382 inode
->i_flags
|= S_DEAD
;
387 res
= hfsplus_delete_cat(cnid
, dir
, &dentry
->d_name
);
391 if (inode
->i_nlink
> 0)
393 if (inode
->i_ino
== cnid
)
395 if (!inode
->i_nlink
) {
396 if (inode
->i_ino
!= cnid
) {
398 if (!atomic_read(&HFSPLUS_I(inode
)->opencnt
)) {
399 res
= hfsplus_delete_cat(inode
->i_ino
,
403 hfsplus_delete_inode(inode
);
405 inode
->i_flags
|= S_DEAD
;
407 hfsplus_delete_inode(inode
);
410 inode
->i_ctime
= current_time(inode
);
411 mark_inode_dirty(inode
);
413 mutex_unlock(&sbi
->vh_mutex
);
417 static int hfsplus_rmdir(struct inode
*dir
, struct dentry
*dentry
)
419 struct hfsplus_sb_info
*sbi
= HFSPLUS_SB(dir
->i_sb
);
420 struct inode
*inode
= d_inode(dentry
);
423 if (inode
->i_size
!= 2)
426 mutex_lock(&sbi
->vh_mutex
);
427 res
= hfsplus_delete_cat(inode
->i_ino
, dir
, &dentry
->d_name
);
431 inode
->i_ctime
= current_time(inode
);
432 hfsplus_delete_inode(inode
);
433 mark_inode_dirty(inode
);
435 mutex_unlock(&sbi
->vh_mutex
);
439 static int hfsplus_symlink(struct inode
*dir
, struct dentry
*dentry
,
442 struct hfsplus_sb_info
*sbi
= HFSPLUS_SB(dir
->i_sb
);
446 mutex_lock(&sbi
->vh_mutex
);
447 inode
= hfsplus_new_inode(dir
->i_sb
, dir
, S_IFLNK
| S_IRWXUGO
);
451 res
= page_symlink(inode
, symname
, strlen(symname
) + 1);
455 res
= hfsplus_create_cat(inode
->i_ino
, dir
, &dentry
->d_name
, inode
);
459 res
= hfsplus_init_inode_security(inode
, dir
, &dentry
->d_name
);
460 if (res
== -EOPNOTSUPP
)
461 res
= 0; /* Operation is not supported. */
463 /* Try to delete anyway without error analysis. */
464 hfsplus_delete_cat(inode
->i_ino
, dir
, &dentry
->d_name
);
468 hfsplus_instantiate(dentry
, inode
, inode
->i_ino
);
469 mark_inode_dirty(inode
);
474 hfsplus_delete_inode(inode
);
477 mutex_unlock(&sbi
->vh_mutex
);
481 static int hfsplus_mknod(struct inode
*dir
, struct dentry
*dentry
,
482 umode_t mode
, dev_t rdev
)
484 struct hfsplus_sb_info
*sbi
= HFSPLUS_SB(dir
->i_sb
);
488 mutex_lock(&sbi
->vh_mutex
);
489 inode
= hfsplus_new_inode(dir
->i_sb
, dir
, mode
);
493 if (S_ISBLK(mode
) || S_ISCHR(mode
) || S_ISFIFO(mode
) || S_ISSOCK(mode
))
494 init_special_inode(inode
, mode
, rdev
);
496 res
= hfsplus_create_cat(inode
->i_ino
, dir
, &dentry
->d_name
, inode
);
500 res
= hfsplus_init_inode_security(inode
, dir
, &dentry
->d_name
);
501 if (res
== -EOPNOTSUPP
)
502 res
= 0; /* Operation is not supported. */
504 /* Try to delete anyway without error analysis. */
505 hfsplus_delete_cat(inode
->i_ino
, dir
, &dentry
->d_name
);
509 hfsplus_instantiate(dentry
, inode
, inode
->i_ino
);
510 mark_inode_dirty(inode
);
515 hfsplus_delete_inode(inode
);
518 mutex_unlock(&sbi
->vh_mutex
);
522 static int hfsplus_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
525 return hfsplus_mknod(dir
, dentry
, mode
, 0);
528 static int hfsplus_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
530 return hfsplus_mknod(dir
, dentry
, mode
| S_IFDIR
, 0);
533 static int hfsplus_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
534 struct inode
*new_dir
, struct dentry
*new_dentry
,
539 if (flags
& ~RENAME_NOREPLACE
)
542 /* Unlink destination if it already exists */
543 if (d_really_is_positive(new_dentry
)) {
544 if (d_is_dir(new_dentry
))
545 res
= hfsplus_rmdir(new_dir
, new_dentry
);
547 res
= hfsplus_unlink(new_dir
, new_dentry
);
552 res
= hfsplus_rename_cat((u32
)(unsigned long)old_dentry
->d_fsdata
,
553 old_dir
, &old_dentry
->d_name
,
554 new_dir
, &new_dentry
->d_name
);
556 new_dentry
->d_fsdata
= old_dentry
->d_fsdata
;
560 const struct inode_operations hfsplus_dir_inode_operations
= {
561 .lookup
= hfsplus_lookup
,
562 .create
= hfsplus_create
,
563 .link
= hfsplus_link
,
564 .unlink
= hfsplus_unlink
,
565 .mkdir
= hfsplus_mkdir
,
566 .rmdir
= hfsplus_rmdir
,
567 .symlink
= hfsplus_symlink
,
568 .mknod
= hfsplus_mknod
,
569 .rename
= hfsplus_rename
,
570 .listxattr
= hfsplus_listxattr
,
571 #ifdef CONFIG_HFSPLUS_FS_POSIX_ACL
572 .get_acl
= hfsplus_get_posix_acl
,
573 .set_acl
= hfsplus_set_posix_acl
,
577 const struct file_operations hfsplus_dir_operations
= {
578 .fsync
= hfsplus_file_fsync
,
579 .read
= generic_read_dir
,
580 .iterate_shared
= hfsplus_readdir
,
581 .unlocked_ioctl
= hfsplus_ioctl
,
582 .llseek
= generic_file_llseek
,
583 .release
= hfsplus_dir_release
,