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>
16 #include "hfsplus_fs.h"
17 #include "hfsplus_raw.h"
21 static inline void hfsplus_instantiate(struct dentry
*dentry
,
22 struct inode
*inode
, u32 cnid
)
24 dentry
->d_fsdata
= (void *)(unsigned long)cnid
;
25 d_instantiate(dentry
, inode
);
28 /* Find the entry inside dir named dentry->d_name */
29 static struct dentry
*hfsplus_lookup(struct inode
*dir
, struct dentry
*dentry
,
32 struct inode
*inode
= NULL
;
33 struct hfs_find_data fd
;
34 struct super_block
*sb
;
35 hfsplus_cat_entry entry
;
42 dentry
->d_fsdata
= NULL
;
43 err
= hfs_find_init(HFSPLUS_SB(sb
)->cat_tree
, &fd
);
46 hfsplus_cat_build_key(sb
, fd
.search_key
, dir
->i_ino
, &dentry
->d_name
);
48 err
= hfs_brec_read(&fd
, &entry
, sizeof(entry
));
58 type
= be16_to_cpu(entry
.type
);
59 if (type
== HFSPLUS_FOLDER
) {
60 if (fd
.entrylength
< sizeof(struct hfsplus_cat_folder
)) {
64 cnid
= be32_to_cpu(entry
.folder
.id
);
65 dentry
->d_fsdata
= (void *)(unsigned long)cnid
;
66 } else if (type
== HFSPLUS_FILE
) {
67 if (fd
.entrylength
< sizeof(struct hfsplus_cat_file
)) {
71 cnid
= be32_to_cpu(entry
.file
.id
);
72 if (entry
.file
.user_info
.fdType
==
73 cpu_to_be32(HFSP_HARDLINK_TYPE
) &&
74 entry
.file
.user_info
.fdCreator
==
75 cpu_to_be32(HFSP_HFSPLUS_CREATOR
) &&
76 (entry
.file
.create_date
==
77 HFSPLUS_I(HFSPLUS_SB(sb
)->hidden_dir
)->
79 entry
.file
.create_date
==
80 HFSPLUS_I(sb
->s_root
->d_inode
)->
82 HFSPLUS_SB(sb
)->hidden_dir
) {
86 if (dentry
->d_fsdata
) {
88 * We found a link pointing to another link,
89 * so ignore it and treat it as regular file.
91 cnid
= (unsigned long)dentry
->d_fsdata
;
94 dentry
->d_fsdata
= (void *)(unsigned long)cnid
;
96 be32_to_cpu(entry
.file
.permissions
.dev
);
97 str
.len
= sprintf(name
, "iNode%d", linkid
);
99 hfsplus_cat_build_key(sb
, fd
.search_key
,
100 HFSPLUS_SB(sb
)->hidden_dir
->i_ino
,
104 } else if (!dentry
->d_fsdata
)
105 dentry
->d_fsdata
= (void *)(unsigned long)cnid
;
107 pr_err("invalid catalog entry type in lookup\n");
112 inode
= hfsplus_iget(dir
->i_sb
, cnid
);
114 return ERR_CAST(inode
);
115 if (S_ISREG(inode
->i_mode
))
116 HFSPLUS_I(inode
)->linkid
= linkid
;
118 d_add(dentry
, inode
);
125 static int hfsplus_readdir(struct file
*file
, struct dir_context
*ctx
)
127 struct inode
*inode
= file_inode(file
);
128 struct super_block
*sb
= inode
->i_sb
;
130 char strbuf
[HFSPLUS_MAX_STRLEN
+ 1];
131 hfsplus_cat_entry entry
;
132 struct hfs_find_data fd
;
133 struct hfsplus_readdir_data
*rd
;
136 if (file
->f_pos
>= inode
->i_size
)
139 err
= hfs_find_init(HFSPLUS_SB(sb
)->cat_tree
, &fd
);
142 hfsplus_cat_build_key(sb
, fd
.search_key
, inode
->i_ino
, NULL
);
143 err
= hfs_brec_find(&fd
, hfs_find_rec_by_key
);
148 /* This is completely artificial... */
149 if (!dir_emit_dot(file
, ctx
))
154 if (fd
.entrylength
> sizeof(entry
) || fd
.entrylength
< 0) {
159 hfs_bnode_read(fd
.bnode
, &entry
, fd
.entryoffset
,
161 if (be16_to_cpu(entry
.type
) != HFSPLUS_FOLDER_THREAD
) {
162 pr_err("bad catalog folder thread\n");
166 if (fd
.entrylength
< HFSPLUS_MIN_THREAD_SZ
) {
167 pr_err("truncated catalog thread\n");
171 if (!dir_emit(ctx
, "..", 2,
172 be32_to_cpu(entry
.thread
.parentID
), DT_DIR
))
176 if (ctx
->pos
>= inode
->i_size
)
178 err
= hfs_brec_goto(&fd
, ctx
->pos
- 1);
182 if (be32_to_cpu(fd
.key
->cat
.parent
) != inode
->i_ino
) {
183 pr_err("walked past end of dir\n");
188 if (fd
.entrylength
> sizeof(entry
) || fd
.entrylength
< 0) {
193 hfs_bnode_read(fd
.bnode
, &entry
, fd
.entryoffset
,
195 type
= be16_to_cpu(entry
.type
);
196 len
= HFSPLUS_MAX_STRLEN
;
197 err
= hfsplus_uni2asc(sb
, &fd
.key
->cat
.name
, strbuf
, &len
);
200 if (type
== HFSPLUS_FOLDER
) {
202 sizeof(struct hfsplus_cat_folder
)) {
203 pr_err("small dir entry\n");
207 if (HFSPLUS_SB(sb
)->hidden_dir
&&
208 HFSPLUS_SB(sb
)->hidden_dir
->i_ino
==
209 be32_to_cpu(entry
.folder
.id
))
211 if (!dir_emit(ctx
, strbuf
, len
,
212 be32_to_cpu(entry
.folder
.id
), DT_DIR
))
214 } else if (type
== HFSPLUS_FILE
) {
215 if (fd
.entrylength
< sizeof(struct hfsplus_cat_file
)) {
216 pr_err("small file entry\n");
220 if (!dir_emit(ctx
, strbuf
, len
,
221 be32_to_cpu(entry
.file
.id
), DT_REG
))
224 pr_err("bad catalog entry type\n");
230 if (ctx
->pos
>= inode
->i_size
)
232 err
= hfs_brec_goto(&fd
, 1);
236 rd
= file
->private_data
;
238 rd
= kmalloc(sizeof(struct hfsplus_readdir_data
), GFP_KERNEL
);
243 file
->private_data
= rd
;
245 list_add(&rd
->list
, &HFSPLUS_I(inode
)->open_dir_list
);
247 memcpy(&rd
->key
, fd
.key
, sizeof(struct hfsplus_cat_key
));
253 static int hfsplus_dir_release(struct inode
*inode
, struct file
*file
)
255 struct hfsplus_readdir_data
*rd
= file
->private_data
;
257 mutex_lock(&inode
->i_mutex
);
259 mutex_unlock(&inode
->i_mutex
);
265 static int hfsplus_link(struct dentry
*src_dentry
, struct inode
*dst_dir
,
266 struct dentry
*dst_dentry
)
268 struct hfsplus_sb_info
*sbi
= HFSPLUS_SB(dst_dir
->i_sb
);
269 struct inode
*inode
= src_dentry
->d_inode
;
270 struct inode
*src_dir
= src_dentry
->d_parent
->d_inode
;
276 if (HFSPLUS_IS_RSRC(inode
))
278 if (!S_ISREG(inode
->i_mode
))
281 mutex_lock(&sbi
->vh_mutex
);
282 if (inode
->i_ino
== (u32
)(unsigned long)src_dentry
->d_fsdata
) {
284 get_random_bytes(&id
, sizeof(cnid
));
287 str
.len
= sprintf(name
, "iNode%d", id
);
288 res
= hfsplus_rename_cat(inode
->i_ino
,
289 src_dir
, &src_dentry
->d_name
,
290 sbi
->hidden_dir
, &str
);
296 HFSPLUS_I(inode
)->linkid
= id
;
297 cnid
= sbi
->next_cnid
++;
298 src_dentry
->d_fsdata
= (void *)(unsigned long)cnid
;
299 res
= hfsplus_create_cat(cnid
, src_dir
,
300 &src_dentry
->d_name
, inode
);
306 cnid
= sbi
->next_cnid
++;
307 res
= hfsplus_create_cat(cnid
, dst_dir
, &dst_dentry
->d_name
, inode
);
312 hfsplus_instantiate(dst_dentry
, inode
, cnid
);
314 inode
->i_ctime
= CURRENT_TIME_SEC
;
315 mark_inode_dirty(inode
);
317 hfsplus_mark_mdb_dirty(dst_dir
->i_sb
);
319 mutex_unlock(&sbi
->vh_mutex
);
323 static int hfsplus_unlink(struct inode
*dir
, struct dentry
*dentry
)
325 struct hfsplus_sb_info
*sbi
= HFSPLUS_SB(dir
->i_sb
);
326 struct inode
*inode
= dentry
->d_inode
;
332 if (HFSPLUS_IS_RSRC(inode
))
335 mutex_lock(&sbi
->vh_mutex
);
336 cnid
= (u32
)(unsigned long)dentry
->d_fsdata
;
337 if (inode
->i_ino
== cnid
&&
338 atomic_read(&HFSPLUS_I(inode
)->opencnt
)) {
340 str
.len
= sprintf(name
, "temp%lu", inode
->i_ino
);
341 res
= hfsplus_rename_cat(inode
->i_ino
,
342 dir
, &dentry
->d_name
,
343 sbi
->hidden_dir
, &str
);
345 inode
->i_flags
|= S_DEAD
;
350 res
= hfsplus_delete_cat(cnid
, dir
, &dentry
->d_name
);
354 if (inode
->i_nlink
> 0)
356 if (inode
->i_ino
== cnid
)
358 if (!inode
->i_nlink
) {
359 if (inode
->i_ino
!= cnid
) {
361 if (!atomic_read(&HFSPLUS_I(inode
)->opencnt
)) {
362 res
= hfsplus_delete_cat(inode
->i_ino
,
366 hfsplus_delete_inode(inode
);
368 inode
->i_flags
|= S_DEAD
;
370 hfsplus_delete_inode(inode
);
373 inode
->i_ctime
= CURRENT_TIME_SEC
;
374 mark_inode_dirty(inode
);
376 mutex_unlock(&sbi
->vh_mutex
);
380 static int hfsplus_rmdir(struct inode
*dir
, struct dentry
*dentry
)
382 struct hfsplus_sb_info
*sbi
= HFSPLUS_SB(dir
->i_sb
);
383 struct inode
*inode
= dentry
->d_inode
;
386 if (inode
->i_size
!= 2)
389 mutex_lock(&sbi
->vh_mutex
);
390 res
= hfsplus_delete_cat(inode
->i_ino
, dir
, &dentry
->d_name
);
394 inode
->i_ctime
= CURRENT_TIME_SEC
;
395 hfsplus_delete_inode(inode
);
396 mark_inode_dirty(inode
);
398 mutex_unlock(&sbi
->vh_mutex
);
402 static int hfsplus_symlink(struct inode
*dir
, struct dentry
*dentry
,
405 struct hfsplus_sb_info
*sbi
= HFSPLUS_SB(dir
->i_sb
);
409 mutex_lock(&sbi
->vh_mutex
);
410 inode
= hfsplus_new_inode(dir
->i_sb
, S_IFLNK
| S_IRWXUGO
);
414 res
= page_symlink(inode
, symname
, strlen(symname
) + 1);
418 res
= hfsplus_create_cat(inode
->i_ino
, dir
, &dentry
->d_name
, inode
);
422 res
= hfsplus_init_inode_security(inode
, dir
, &dentry
->d_name
);
423 if (res
== -EOPNOTSUPP
)
424 res
= 0; /* Operation is not supported. */
426 /* Try to delete anyway without error analysis. */
427 hfsplus_delete_cat(inode
->i_ino
, dir
, &dentry
->d_name
);
431 hfsplus_instantiate(dentry
, inode
, inode
->i_ino
);
432 mark_inode_dirty(inode
);
437 hfsplus_delete_inode(inode
);
440 mutex_unlock(&sbi
->vh_mutex
);
444 static int hfsplus_mknod(struct inode
*dir
, struct dentry
*dentry
,
445 umode_t mode
, dev_t rdev
)
447 struct hfsplus_sb_info
*sbi
= HFSPLUS_SB(dir
->i_sb
);
451 mutex_lock(&sbi
->vh_mutex
);
452 inode
= hfsplus_new_inode(dir
->i_sb
, mode
);
456 if (S_ISBLK(mode
) || S_ISCHR(mode
) || S_ISFIFO(mode
) || S_ISSOCK(mode
))
457 init_special_inode(inode
, mode
, rdev
);
459 res
= hfsplus_create_cat(inode
->i_ino
, dir
, &dentry
->d_name
, inode
);
463 res
= hfsplus_init_inode_security(inode
, dir
, &dentry
->d_name
);
464 if (res
== -EOPNOTSUPP
)
465 res
= 0; /* Operation is not supported. */
467 /* Try to delete anyway without error analysis. */
468 hfsplus_delete_cat(inode
->i_ino
, dir
, &dentry
->d_name
);
472 hfsplus_instantiate(dentry
, inode
, inode
->i_ino
);
473 mark_inode_dirty(inode
);
478 hfsplus_delete_inode(inode
);
481 mutex_unlock(&sbi
->vh_mutex
);
485 static int hfsplus_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
488 return hfsplus_mknod(dir
, dentry
, mode
, 0);
491 static int hfsplus_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
493 return hfsplus_mknod(dir
, dentry
, mode
| S_IFDIR
, 0);
496 static int hfsplus_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
497 struct inode
*new_dir
, struct dentry
*new_dentry
)
501 /* Unlink destination if it already exists */
502 if (new_dentry
->d_inode
) {
503 if (S_ISDIR(new_dentry
->d_inode
->i_mode
))
504 res
= hfsplus_rmdir(new_dir
, new_dentry
);
506 res
= hfsplus_unlink(new_dir
, new_dentry
);
511 res
= hfsplus_rename_cat((u32
)(unsigned long)old_dentry
->d_fsdata
,
512 old_dir
, &old_dentry
->d_name
,
513 new_dir
, &new_dentry
->d_name
);
515 new_dentry
->d_fsdata
= old_dentry
->d_fsdata
;
519 const struct inode_operations hfsplus_dir_inode_operations
= {
520 .lookup
= hfsplus_lookup
,
521 .create
= hfsplus_create
,
522 .link
= hfsplus_link
,
523 .unlink
= hfsplus_unlink
,
524 .mkdir
= hfsplus_mkdir
,
525 .rmdir
= hfsplus_rmdir
,
526 .symlink
= hfsplus_symlink
,
527 .mknod
= hfsplus_mknod
,
528 .rename
= hfsplus_rename
,
529 .setxattr
= generic_setxattr
,
530 .getxattr
= generic_getxattr
,
531 .listxattr
= hfsplus_listxattr
,
532 .removexattr
= hfsplus_removexattr
,
533 #ifdef CONFIG_HFSPLUS_FS_POSIX_ACL
534 .get_acl
= hfsplus_get_posix_acl
,
538 const struct file_operations hfsplus_dir_operations
= {
539 .fsync
= hfsplus_file_fsync
,
540 .read
= generic_read_dir
,
541 .iterate
= hfsplus_readdir
,
542 .unlocked_ioctl
= hfsplus_ioctl
,
543 .llseek
= generic_file_llseek
,
544 .release
= hfsplus_dir_release
,