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"
19 static inline void hfsplus_instantiate(struct dentry
*dentry
,
20 struct inode
*inode
, u32 cnid
)
22 dentry
->d_fsdata
= (void *)(unsigned long)cnid
;
23 d_instantiate(dentry
, inode
);
26 /* Find the entry inside dir named dentry->d_name */
27 static struct dentry
*hfsplus_lookup(struct inode
*dir
, struct dentry
*dentry
,
30 struct inode
*inode
= NULL
;
31 struct hfs_find_data fd
;
32 struct super_block
*sb
;
33 hfsplus_cat_entry entry
;
40 dentry
->d_fsdata
= NULL
;
41 hfs_find_init(HFSPLUS_SB(sb
)->cat_tree
, &fd
);
42 hfsplus_cat_build_key(sb
, fd
.search_key
, dir
->i_ino
, &dentry
->d_name
);
44 err
= hfs_brec_read(&fd
, &entry
, sizeof(entry
));
54 type
= be16_to_cpu(entry
.type
);
55 if (type
== HFSPLUS_FOLDER
) {
56 if (fd
.entrylength
< sizeof(struct hfsplus_cat_folder
)) {
60 cnid
= be32_to_cpu(entry
.folder
.id
);
61 dentry
->d_fsdata
= (void *)(unsigned long)cnid
;
62 } else if (type
== HFSPLUS_FILE
) {
63 if (fd
.entrylength
< sizeof(struct hfsplus_cat_file
)) {
67 cnid
= be32_to_cpu(entry
.file
.id
);
68 if (entry
.file
.user_info
.fdType
==
69 cpu_to_be32(HFSP_HARDLINK_TYPE
) &&
70 entry
.file
.user_info
.fdCreator
==
71 cpu_to_be32(HFSP_HFSPLUS_CREATOR
) &&
72 (entry
.file
.create_date
==
73 HFSPLUS_I(HFSPLUS_SB(sb
)->hidden_dir
)->
75 entry
.file
.create_date
==
76 HFSPLUS_I(sb
->s_root
->d_inode
)->
78 HFSPLUS_SB(sb
)->hidden_dir
) {
82 if (dentry
->d_fsdata
) {
84 * We found a link pointing to another link,
85 * so ignore it and treat it as regular file.
87 cnid
= (unsigned long)dentry
->d_fsdata
;
90 dentry
->d_fsdata
= (void *)(unsigned long)cnid
;
92 be32_to_cpu(entry
.file
.permissions
.dev
);
93 str
.len
= sprintf(name
, "iNode%d", linkid
);
95 hfsplus_cat_build_key(sb
, fd
.search_key
,
96 HFSPLUS_SB(sb
)->hidden_dir
->i_ino
,
100 } else if (!dentry
->d_fsdata
)
101 dentry
->d_fsdata
= (void *)(unsigned long)cnid
;
103 printk(KERN_ERR
"hfs: invalid catalog entry type in lookup\n");
108 inode
= hfsplus_iget(dir
->i_sb
, cnid
);
110 return ERR_CAST(inode
);
111 if (S_ISREG(inode
->i_mode
))
112 HFSPLUS_I(inode
)->linkid
= linkid
;
114 d_add(dentry
, inode
);
121 static int hfsplus_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
123 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
124 struct super_block
*sb
= inode
->i_sb
;
126 char strbuf
[HFSPLUS_MAX_STRLEN
+ 1];
127 hfsplus_cat_entry entry
;
128 struct hfs_find_data fd
;
129 struct hfsplus_readdir_data
*rd
;
132 if (filp
->f_pos
>= inode
->i_size
)
135 hfs_find_init(HFSPLUS_SB(sb
)->cat_tree
, &fd
);
136 hfsplus_cat_build_key(sb
, fd
.search_key
, inode
->i_ino
, NULL
);
137 err
= hfs_brec_find(&fd
);
141 switch ((u32
)filp
->f_pos
) {
143 /* This is completely artificial... */
144 if (filldir(dirent
, ".", 1, 0, inode
->i_ino
, DT_DIR
))
149 hfs_bnode_read(fd
.bnode
, &entry
, fd
.entryoffset
,
151 if (be16_to_cpu(entry
.type
) != HFSPLUS_FOLDER_THREAD
) {
152 printk(KERN_ERR
"hfs: bad catalog folder thread\n");
156 if (fd
.entrylength
< HFSPLUS_MIN_THREAD_SZ
) {
157 printk(KERN_ERR
"hfs: truncated catalog thread\n");
161 if (filldir(dirent
, "..", 2, 1,
162 be32_to_cpu(entry
.thread
.parentID
), DT_DIR
))
167 if (filp
->f_pos
>= inode
->i_size
)
169 err
= hfs_brec_goto(&fd
, filp
->f_pos
- 1);
175 if (be32_to_cpu(fd
.key
->cat
.parent
) != inode
->i_ino
) {
176 printk(KERN_ERR
"hfs: walked past end of dir\n");
180 hfs_bnode_read(fd
.bnode
, &entry
, fd
.entryoffset
,
182 type
= be16_to_cpu(entry
.type
);
183 len
= HFSPLUS_MAX_STRLEN
;
184 err
= hfsplus_uni2asc(sb
, &fd
.key
->cat
.name
, strbuf
, &len
);
187 if (type
== HFSPLUS_FOLDER
) {
189 sizeof(struct hfsplus_cat_folder
)) {
190 printk(KERN_ERR
"hfs: small dir entry\n");
194 if (HFSPLUS_SB(sb
)->hidden_dir
&&
195 HFSPLUS_SB(sb
)->hidden_dir
->i_ino
==
196 be32_to_cpu(entry
.folder
.id
))
198 if (filldir(dirent
, strbuf
, len
, filp
->f_pos
,
199 be32_to_cpu(entry
.folder
.id
), DT_DIR
))
201 } else if (type
== HFSPLUS_FILE
) {
202 if (fd
.entrylength
< sizeof(struct hfsplus_cat_file
)) {
203 printk(KERN_ERR
"hfs: small file entry\n");
207 if (filldir(dirent
, strbuf
, len
, filp
->f_pos
,
208 be32_to_cpu(entry
.file
.id
), DT_REG
))
211 printk(KERN_ERR
"hfs: bad catalog entry type\n");
217 if (filp
->f_pos
>= inode
->i_size
)
219 err
= hfs_brec_goto(&fd
, 1);
223 rd
= filp
->private_data
;
225 rd
= kmalloc(sizeof(struct hfsplus_readdir_data
), GFP_KERNEL
);
230 filp
->private_data
= rd
;
232 list_add(&rd
->list
, &HFSPLUS_I(inode
)->open_dir_list
);
234 memcpy(&rd
->key
, fd
.key
, sizeof(struct hfsplus_cat_key
));
240 static int hfsplus_dir_release(struct inode
*inode
, struct file
*file
)
242 struct hfsplus_readdir_data
*rd
= file
->private_data
;
244 mutex_lock(&inode
->i_mutex
);
246 mutex_unlock(&inode
->i_mutex
);
252 static int hfsplus_link(struct dentry
*src_dentry
, struct inode
*dst_dir
,
253 struct dentry
*dst_dentry
)
255 struct hfsplus_sb_info
*sbi
= HFSPLUS_SB(dst_dir
->i_sb
);
256 struct inode
*inode
= src_dentry
->d_inode
;
257 struct inode
*src_dir
= src_dentry
->d_parent
->d_inode
;
263 if (HFSPLUS_IS_RSRC(inode
))
265 if (!S_ISREG(inode
->i_mode
))
268 mutex_lock(&sbi
->vh_mutex
);
269 if (inode
->i_ino
== (u32
)(unsigned long)src_dentry
->d_fsdata
) {
271 get_random_bytes(&id
, sizeof(cnid
));
274 str
.len
= sprintf(name
, "iNode%d", id
);
275 res
= hfsplus_rename_cat(inode
->i_ino
,
276 src_dir
, &src_dentry
->d_name
,
277 sbi
->hidden_dir
, &str
);
283 HFSPLUS_I(inode
)->linkid
= id
;
284 cnid
= sbi
->next_cnid
++;
285 src_dentry
->d_fsdata
= (void *)(unsigned long)cnid
;
286 res
= hfsplus_create_cat(cnid
, src_dir
,
287 &src_dentry
->d_name
, inode
);
293 cnid
= sbi
->next_cnid
++;
294 res
= hfsplus_create_cat(cnid
, dst_dir
, &dst_dentry
->d_name
, inode
);
299 hfsplus_instantiate(dst_dentry
, inode
, cnid
);
301 inode
->i_ctime
= CURRENT_TIME_SEC
;
302 mark_inode_dirty(inode
);
304 dst_dir
->i_sb
->s_dirt
= 1;
306 mutex_unlock(&sbi
->vh_mutex
);
310 static int hfsplus_unlink(struct inode
*dir
, struct dentry
*dentry
)
312 struct hfsplus_sb_info
*sbi
= HFSPLUS_SB(dir
->i_sb
);
313 struct inode
*inode
= dentry
->d_inode
;
319 if (HFSPLUS_IS_RSRC(inode
))
322 mutex_lock(&sbi
->vh_mutex
);
323 cnid
= (u32
)(unsigned long)dentry
->d_fsdata
;
324 if (inode
->i_ino
== cnid
&&
325 atomic_read(&HFSPLUS_I(inode
)->opencnt
)) {
327 str
.len
= sprintf(name
, "temp%lu", inode
->i_ino
);
328 res
= hfsplus_rename_cat(inode
->i_ino
,
329 dir
, &dentry
->d_name
,
330 sbi
->hidden_dir
, &str
);
332 inode
->i_flags
|= S_DEAD
;
337 res
= hfsplus_delete_cat(cnid
, dir
, &dentry
->d_name
);
341 if (inode
->i_nlink
> 0)
343 if (inode
->i_ino
== cnid
)
345 if (!inode
->i_nlink
) {
346 if (inode
->i_ino
!= cnid
) {
348 if (!atomic_read(&HFSPLUS_I(inode
)->opencnt
)) {
349 res
= hfsplus_delete_cat(inode
->i_ino
,
353 hfsplus_delete_inode(inode
);
355 inode
->i_flags
|= S_DEAD
;
357 hfsplus_delete_inode(inode
);
360 inode
->i_ctime
= CURRENT_TIME_SEC
;
361 mark_inode_dirty(inode
);
363 mutex_unlock(&sbi
->vh_mutex
);
367 static int hfsplus_rmdir(struct inode
*dir
, struct dentry
*dentry
)
369 struct hfsplus_sb_info
*sbi
= HFSPLUS_SB(dir
->i_sb
);
370 struct inode
*inode
= dentry
->d_inode
;
373 if (inode
->i_size
!= 2)
376 mutex_lock(&sbi
->vh_mutex
);
377 res
= hfsplus_delete_cat(inode
->i_ino
, dir
, &dentry
->d_name
);
381 inode
->i_ctime
= CURRENT_TIME_SEC
;
382 hfsplus_delete_inode(inode
);
383 mark_inode_dirty(inode
);
385 mutex_unlock(&sbi
->vh_mutex
);
389 static int hfsplus_symlink(struct inode
*dir
, struct dentry
*dentry
,
392 struct hfsplus_sb_info
*sbi
= HFSPLUS_SB(dir
->i_sb
);
396 mutex_lock(&sbi
->vh_mutex
);
397 inode
= hfsplus_new_inode(dir
->i_sb
, S_IFLNK
| S_IRWXUGO
);
401 res
= page_symlink(inode
, symname
, strlen(symname
) + 1);
405 res
= hfsplus_create_cat(inode
->i_ino
, dir
, &dentry
->d_name
, inode
);
409 hfsplus_instantiate(dentry
, inode
, inode
->i_ino
);
410 mark_inode_dirty(inode
);
415 hfsplus_delete_inode(inode
);
418 mutex_unlock(&sbi
->vh_mutex
);
422 static int hfsplus_mknod(struct inode
*dir
, struct dentry
*dentry
,
423 int mode
, dev_t rdev
)
425 struct hfsplus_sb_info
*sbi
= HFSPLUS_SB(dir
->i_sb
);
429 mutex_lock(&sbi
->vh_mutex
);
430 inode
= hfsplus_new_inode(dir
->i_sb
, mode
);
434 if (S_ISBLK(mode
) || S_ISCHR(mode
) || S_ISFIFO(mode
) || S_ISSOCK(mode
))
435 init_special_inode(inode
, mode
, rdev
);
437 res
= hfsplus_create_cat(inode
->i_ino
, dir
, &dentry
->d_name
, inode
);
440 hfsplus_delete_inode(inode
);
445 hfsplus_instantiate(dentry
, inode
, inode
->i_ino
);
446 mark_inode_dirty(inode
);
448 mutex_unlock(&sbi
->vh_mutex
);
452 static int hfsplus_create(struct inode
*dir
, struct dentry
*dentry
, int mode
,
453 struct nameidata
*nd
)
455 return hfsplus_mknod(dir
, dentry
, mode
, 0);
458 static int hfsplus_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
460 return hfsplus_mknod(dir
, dentry
, mode
| S_IFDIR
, 0);
463 static int hfsplus_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
464 struct inode
*new_dir
, struct dentry
*new_dentry
)
468 /* Unlink destination if it already exists */
469 if (new_dentry
->d_inode
) {
470 if (S_ISDIR(new_dentry
->d_inode
->i_mode
))
471 res
= hfsplus_rmdir(new_dir
, new_dentry
);
473 res
= hfsplus_unlink(new_dir
, new_dentry
);
478 res
= hfsplus_rename_cat((u32
)(unsigned long)old_dentry
->d_fsdata
,
479 old_dir
, &old_dentry
->d_name
,
480 new_dir
, &new_dentry
->d_name
);
482 new_dentry
->d_fsdata
= old_dentry
->d_fsdata
;
486 const struct inode_operations hfsplus_dir_inode_operations
= {
487 .lookup
= hfsplus_lookup
,
488 .create
= hfsplus_create
,
489 .link
= hfsplus_link
,
490 .unlink
= hfsplus_unlink
,
491 .mkdir
= hfsplus_mkdir
,
492 .rmdir
= hfsplus_rmdir
,
493 .symlink
= hfsplus_symlink
,
494 .mknod
= hfsplus_mknod
,
495 .rename
= hfsplus_rename
,
498 const struct file_operations hfsplus_dir_operations
= {
499 .fsync
= hfsplus_file_fsync
,
500 .read
= generic_read_dir
,
501 .readdir
= hfsplus_readdir
,
502 .unlocked_ioctl
= hfsplus_ioctl
,
503 .llseek
= generic_file_llseek
,
504 .release
= hfsplus_dir_release
,