2 * linux/fs/hfsplus/dir.c
5 * Brad Boyer (flar@allandria.com)
6 * (C) 2003 Ardis Technologies <roman@ardistech.com>
8 * Handling of directories
11 #include <linux/errno.h>
13 #include <linux/slab.h>
14 #include <linux/random.h>
15 #include <linux/nls.h>
17 #include "hfsplus_fs.h"
18 #include "hfsplus_raw.h"
22 static inline void hfsplus_instantiate(struct dentry
*dentry
,
23 struct inode
*inode
, u32 cnid
)
25 dentry
->d_fsdata
= (void *)(unsigned long)cnid
;
26 d_instantiate(dentry
, inode
);
29 /* Find the entry inside dir named dentry->d_name */
30 static struct dentry
*hfsplus_lookup(struct inode
*dir
, struct dentry
*dentry
,
33 struct inode
*inode
= NULL
;
34 struct hfs_find_data fd
;
35 struct super_block
*sb
;
36 hfsplus_cat_entry entry
;
43 dentry
->d_fsdata
= NULL
;
44 err
= hfs_find_init(HFSPLUS_SB(sb
)->cat_tree
, &fd
);
47 err
= hfsplus_cat_build_key(sb
, fd
.search_key
, dir
->i_ino
,
49 if (unlikely(err
< 0))
52 err
= hfs_brec_read(&fd
, &entry
, sizeof(entry
));
62 type
= be16_to_cpu(entry
.type
);
63 if (type
== HFSPLUS_FOLDER
) {
64 if (fd
.entrylength
< sizeof(struct hfsplus_cat_folder
)) {
68 cnid
= be32_to_cpu(entry
.folder
.id
);
69 dentry
->d_fsdata
= (void *)(unsigned long)cnid
;
70 } else if (type
== HFSPLUS_FILE
) {
71 if (fd
.entrylength
< sizeof(struct hfsplus_cat_file
)) {
75 cnid
= be32_to_cpu(entry
.file
.id
);
76 if (entry
.file
.user_info
.fdType
==
77 cpu_to_be32(HFSP_HARDLINK_TYPE
) &&
78 entry
.file
.user_info
.fdCreator
==
79 cpu_to_be32(HFSP_HFSPLUS_CREATOR
) &&
80 (entry
.file
.create_date
==
81 HFSPLUS_I(HFSPLUS_SB(sb
)->hidden_dir
)->
83 entry
.file
.create_date
==
84 HFSPLUS_I(d_inode(sb
->s_root
))->
86 HFSPLUS_SB(sb
)->hidden_dir
) {
90 if (dentry
->d_fsdata
) {
92 * We found a link pointing to another link,
93 * so ignore it and treat it as regular file.
95 cnid
= (unsigned long)dentry
->d_fsdata
;
98 dentry
->d_fsdata
= (void *)(unsigned long)cnid
;
100 be32_to_cpu(entry
.file
.permissions
.dev
);
101 str
.len
= sprintf(name
, "iNode%d", linkid
);
103 err
= hfsplus_cat_build_key(sb
, fd
.search_key
,
104 HFSPLUS_SB(sb
)->hidden_dir
->i_ino
,
106 if (unlikely(err
< 0))
110 } else if (!dentry
->d_fsdata
)
111 dentry
->d_fsdata
= (void *)(unsigned long)cnid
;
113 pr_err("invalid catalog entry type in lookup\n");
118 inode
= hfsplus_iget(dir
->i_sb
, cnid
);
120 return ERR_CAST(inode
);
121 if (S_ISREG(inode
->i_mode
))
122 HFSPLUS_I(inode
)->linkid
= linkid
;
124 d_add(dentry
, inode
);
131 static int hfsplus_readdir(struct file
*file
, struct dir_context
*ctx
)
133 struct inode
*inode
= file_inode(file
);
134 struct super_block
*sb
= inode
->i_sb
;
137 hfsplus_cat_entry entry
;
138 struct hfs_find_data fd
;
139 struct hfsplus_readdir_data
*rd
;
142 if (file
->f_pos
>= inode
->i_size
)
145 err
= hfs_find_init(HFSPLUS_SB(sb
)->cat_tree
, &fd
);
148 strbuf
= kmalloc(NLS_MAX_CHARSET_SIZE
* HFSPLUS_MAX_STRLEN
+ 1, GFP_KERNEL
);
153 hfsplus_cat_build_key_with_cnid(sb
, fd
.search_key
, inode
->i_ino
);
154 err
= hfs_brec_find(&fd
, hfs_find_rec_by_key
);
159 /* This is completely artificial... */
160 if (!dir_emit_dot(file
, ctx
))
165 if (fd
.entrylength
> sizeof(entry
) || fd
.entrylength
< 0) {
170 hfs_bnode_read(fd
.bnode
, &entry
, fd
.entryoffset
,
172 if (be16_to_cpu(entry
.type
) != HFSPLUS_FOLDER_THREAD
) {
173 pr_err("bad catalog folder thread\n");
177 if (fd
.entrylength
< HFSPLUS_MIN_THREAD_SZ
) {
178 pr_err("truncated catalog thread\n");
182 if (!dir_emit(ctx
, "..", 2,
183 be32_to_cpu(entry
.thread
.parentID
), DT_DIR
))
187 if (ctx
->pos
>= inode
->i_size
)
189 err
= hfs_brec_goto(&fd
, ctx
->pos
- 1);
193 if (be32_to_cpu(fd
.key
->cat
.parent
) != inode
->i_ino
) {
194 pr_err("walked past end of dir\n");
199 if (fd
.entrylength
> sizeof(entry
) || fd
.entrylength
< 0) {
204 hfs_bnode_read(fd
.bnode
, &entry
, fd
.entryoffset
,
206 type
= be16_to_cpu(entry
.type
);
207 len
= NLS_MAX_CHARSET_SIZE
* HFSPLUS_MAX_STRLEN
;
208 err
= hfsplus_uni2asc(sb
, &fd
.key
->cat
.name
, strbuf
, &len
);
211 if (type
== HFSPLUS_FOLDER
) {
213 sizeof(struct hfsplus_cat_folder
)) {
214 pr_err("small dir entry\n");
218 if (HFSPLUS_SB(sb
)->hidden_dir
&&
219 HFSPLUS_SB(sb
)->hidden_dir
->i_ino
==
220 be32_to_cpu(entry
.folder
.id
))
222 if (!dir_emit(ctx
, strbuf
, len
,
223 be32_to_cpu(entry
.folder
.id
), DT_DIR
))
225 } else if (type
== HFSPLUS_FILE
) {
227 unsigned type
= DT_UNKNOWN
;
229 if (fd
.entrylength
< sizeof(struct hfsplus_cat_file
)) {
230 pr_err("small file entry\n");
235 mode
= be16_to_cpu(entry
.file
.permissions
.mode
);
238 else if (S_ISLNK(mode
))
240 else if (S_ISFIFO(mode
))
242 else if (S_ISCHR(mode
))
244 else if (S_ISBLK(mode
))
246 else if (S_ISSOCK(mode
))
249 if (!dir_emit(ctx
, strbuf
, len
,
250 be32_to_cpu(entry
.file
.id
), type
))
253 pr_err("bad catalog entry type\n");
259 if (ctx
->pos
>= inode
->i_size
)
261 err
= hfs_brec_goto(&fd
, 1);
265 rd
= file
->private_data
;
267 rd
= kmalloc(sizeof(struct hfsplus_readdir_data
), GFP_KERNEL
);
272 file
->private_data
= rd
;
274 list_add(&rd
->list
, &HFSPLUS_I(inode
)->open_dir_list
);
276 memcpy(&rd
->key
, fd
.key
, sizeof(struct hfsplus_cat_key
));
283 static int hfsplus_dir_release(struct inode
*inode
, struct file
*file
)
285 struct hfsplus_readdir_data
*rd
= file
->private_data
;
295 static int hfsplus_link(struct dentry
*src_dentry
, struct inode
*dst_dir
,
296 struct dentry
*dst_dentry
)
298 struct hfsplus_sb_info
*sbi
= HFSPLUS_SB(dst_dir
->i_sb
);
299 struct inode
*inode
= d_inode(src_dentry
);
300 struct inode
*src_dir
= d_inode(src_dentry
->d_parent
);
306 if (HFSPLUS_IS_RSRC(inode
))
308 if (!S_ISREG(inode
->i_mode
))
311 mutex_lock(&sbi
->vh_mutex
);
312 if (inode
->i_ino
== (u32
)(unsigned long)src_dentry
->d_fsdata
) {
314 get_random_bytes(&id
, sizeof(cnid
));
317 str
.len
= sprintf(name
, "iNode%d", id
);
318 res
= hfsplus_rename_cat(inode
->i_ino
,
319 src_dir
, &src_dentry
->d_name
,
320 sbi
->hidden_dir
, &str
);
326 HFSPLUS_I(inode
)->linkid
= id
;
327 cnid
= sbi
->next_cnid
++;
328 src_dentry
->d_fsdata
= (void *)(unsigned long)cnid
;
329 res
= hfsplus_create_cat(cnid
, src_dir
,
330 &src_dentry
->d_name
, inode
);
336 cnid
= sbi
->next_cnid
++;
337 res
= hfsplus_create_cat(cnid
, dst_dir
, &dst_dentry
->d_name
, inode
);
342 hfsplus_instantiate(dst_dentry
, inode
, cnid
);
344 inode
->i_ctime
= CURRENT_TIME_SEC
;
345 mark_inode_dirty(inode
);
347 hfsplus_mark_mdb_dirty(dst_dir
->i_sb
);
349 mutex_unlock(&sbi
->vh_mutex
);
353 static int hfsplus_unlink(struct inode
*dir
, struct dentry
*dentry
)
355 struct hfsplus_sb_info
*sbi
= HFSPLUS_SB(dir
->i_sb
);
356 struct inode
*inode
= d_inode(dentry
);
362 if (HFSPLUS_IS_RSRC(inode
))
365 mutex_lock(&sbi
->vh_mutex
);
366 cnid
= (u32
)(unsigned long)dentry
->d_fsdata
;
367 if (inode
->i_ino
== cnid
&&
368 atomic_read(&HFSPLUS_I(inode
)->opencnt
)) {
370 str
.len
= sprintf(name
, "temp%lu", inode
->i_ino
);
371 res
= hfsplus_rename_cat(inode
->i_ino
,
372 dir
, &dentry
->d_name
,
373 sbi
->hidden_dir
, &str
);
375 inode
->i_flags
|= S_DEAD
;
380 res
= hfsplus_delete_cat(cnid
, dir
, &dentry
->d_name
);
384 if (inode
->i_nlink
> 0)
386 if (inode
->i_ino
== cnid
)
388 if (!inode
->i_nlink
) {
389 if (inode
->i_ino
!= cnid
) {
391 if (!atomic_read(&HFSPLUS_I(inode
)->opencnt
)) {
392 res
= hfsplus_delete_cat(inode
->i_ino
,
396 hfsplus_delete_inode(inode
);
398 inode
->i_flags
|= S_DEAD
;
400 hfsplus_delete_inode(inode
);
403 inode
->i_ctime
= CURRENT_TIME_SEC
;
404 mark_inode_dirty(inode
);
406 mutex_unlock(&sbi
->vh_mutex
);
410 static int hfsplus_rmdir(struct inode
*dir
, struct dentry
*dentry
)
412 struct hfsplus_sb_info
*sbi
= HFSPLUS_SB(dir
->i_sb
);
413 struct inode
*inode
= d_inode(dentry
);
416 if (inode
->i_size
!= 2)
419 mutex_lock(&sbi
->vh_mutex
);
420 res
= hfsplus_delete_cat(inode
->i_ino
, dir
, &dentry
->d_name
);
424 inode
->i_ctime
= CURRENT_TIME_SEC
;
425 hfsplus_delete_inode(inode
);
426 mark_inode_dirty(inode
);
428 mutex_unlock(&sbi
->vh_mutex
);
432 static int hfsplus_symlink(struct inode
*dir
, struct dentry
*dentry
,
435 struct hfsplus_sb_info
*sbi
= HFSPLUS_SB(dir
->i_sb
);
439 mutex_lock(&sbi
->vh_mutex
);
440 inode
= hfsplus_new_inode(dir
->i_sb
, S_IFLNK
| S_IRWXUGO
);
444 res
= page_symlink(inode
, symname
, strlen(symname
) + 1);
448 res
= hfsplus_create_cat(inode
->i_ino
, dir
, &dentry
->d_name
, inode
);
452 res
= hfsplus_init_inode_security(inode
, dir
, &dentry
->d_name
);
453 if (res
== -EOPNOTSUPP
)
454 res
= 0; /* Operation is not supported. */
456 /* Try to delete anyway without error analysis. */
457 hfsplus_delete_cat(inode
->i_ino
, dir
, &dentry
->d_name
);
461 hfsplus_instantiate(dentry
, inode
, inode
->i_ino
);
462 mark_inode_dirty(inode
);
467 hfsplus_delete_inode(inode
);
470 mutex_unlock(&sbi
->vh_mutex
);
474 static int hfsplus_mknod(struct inode
*dir
, struct dentry
*dentry
,
475 umode_t mode
, dev_t rdev
)
477 struct hfsplus_sb_info
*sbi
= HFSPLUS_SB(dir
->i_sb
);
481 mutex_lock(&sbi
->vh_mutex
);
482 inode
= hfsplus_new_inode(dir
->i_sb
, mode
);
486 if (S_ISBLK(mode
) || S_ISCHR(mode
) || S_ISFIFO(mode
) || S_ISSOCK(mode
))
487 init_special_inode(inode
, mode
, rdev
);
489 res
= hfsplus_create_cat(inode
->i_ino
, dir
, &dentry
->d_name
, inode
);
493 res
= hfsplus_init_inode_security(inode
, dir
, &dentry
->d_name
);
494 if (res
== -EOPNOTSUPP
)
495 res
= 0; /* Operation is not supported. */
497 /* Try to delete anyway without error analysis. */
498 hfsplus_delete_cat(inode
->i_ino
, dir
, &dentry
->d_name
);
502 hfsplus_instantiate(dentry
, inode
, inode
->i_ino
);
503 mark_inode_dirty(inode
);
508 hfsplus_delete_inode(inode
);
511 mutex_unlock(&sbi
->vh_mutex
);
515 static int hfsplus_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
518 return hfsplus_mknod(dir
, dentry
, mode
, 0);
521 static int hfsplus_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
523 return hfsplus_mknod(dir
, dentry
, mode
| S_IFDIR
, 0);
526 static int hfsplus_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
527 struct inode
*new_dir
, struct dentry
*new_dentry
)
531 /* Unlink destination if it already exists */
532 if (d_really_is_positive(new_dentry
)) {
533 if (d_is_dir(new_dentry
))
534 res
= hfsplus_rmdir(new_dir
, new_dentry
);
536 res
= hfsplus_unlink(new_dir
, new_dentry
);
541 res
= hfsplus_rename_cat((u32
)(unsigned long)old_dentry
->d_fsdata
,
542 old_dir
, &old_dentry
->d_name
,
543 new_dir
, &new_dentry
->d_name
);
545 new_dentry
->d_fsdata
= old_dentry
->d_fsdata
;
549 const struct inode_operations hfsplus_dir_inode_operations
= {
550 .lookup
= hfsplus_lookup
,
551 .create
= hfsplus_create
,
552 .link
= hfsplus_link
,
553 .unlink
= hfsplus_unlink
,
554 .mkdir
= hfsplus_mkdir
,
555 .rmdir
= hfsplus_rmdir
,
556 .symlink
= hfsplus_symlink
,
557 .mknod
= hfsplus_mknod
,
558 .rename
= hfsplus_rename
,
559 .setxattr
= generic_setxattr
,
560 .getxattr
= generic_getxattr
,
561 .listxattr
= hfsplus_listxattr
,
562 .removexattr
= generic_removexattr
,
563 #ifdef CONFIG_HFSPLUS_FS_POSIX_ACL
564 .get_acl
= hfsplus_get_posix_acl
,
565 .set_acl
= hfsplus_set_posix_acl
,
569 const struct file_operations hfsplus_dir_operations
= {
570 .fsync
= hfsplus_file_fsync
,
571 .read
= generic_read_dir
,
572 .iterate
= hfsplus_readdir
,
573 .unlocked_ioctl
= hfsplus_ioctl
,
574 .llseek
= generic_file_llseek
,
575 .release
= hfsplus_dir_release
,