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 err
= hfs_find_init(HFSPLUS_SB(sb
)->cat_tree
, &fd
);
44 hfsplus_cat_build_key(sb
, fd
.search_key
, dir
->i_ino
, &dentry
->d_name
);
46 err
= hfs_brec_read(&fd
, &entry
, sizeof(entry
));
56 type
= be16_to_cpu(entry
.type
);
57 if (type
== HFSPLUS_FOLDER
) {
58 if (fd
.entrylength
< sizeof(struct hfsplus_cat_folder
)) {
62 cnid
= be32_to_cpu(entry
.folder
.id
);
63 dentry
->d_fsdata
= (void *)(unsigned long)cnid
;
64 } else if (type
== HFSPLUS_FILE
) {
65 if (fd
.entrylength
< sizeof(struct hfsplus_cat_file
)) {
69 cnid
= be32_to_cpu(entry
.file
.id
);
70 if (entry
.file
.user_info
.fdType
==
71 cpu_to_be32(HFSP_HARDLINK_TYPE
) &&
72 entry
.file
.user_info
.fdCreator
==
73 cpu_to_be32(HFSP_HFSPLUS_CREATOR
) &&
74 (entry
.file
.create_date
==
75 HFSPLUS_I(HFSPLUS_SB(sb
)->hidden_dir
)->
77 entry
.file
.create_date
==
78 HFSPLUS_I(sb
->s_root
->d_inode
)->
80 HFSPLUS_SB(sb
)->hidden_dir
) {
84 if (dentry
->d_fsdata
) {
86 * We found a link pointing to another link,
87 * so ignore it and treat it as regular file.
89 cnid
= (unsigned long)dentry
->d_fsdata
;
92 dentry
->d_fsdata
= (void *)(unsigned long)cnid
;
94 be32_to_cpu(entry
.file
.permissions
.dev
);
95 str
.len
= sprintf(name
, "iNode%d", linkid
);
97 hfsplus_cat_build_key(sb
, fd
.search_key
,
98 HFSPLUS_SB(sb
)->hidden_dir
->i_ino
,
102 } else if (!dentry
->d_fsdata
)
103 dentry
->d_fsdata
= (void *)(unsigned long)cnid
;
105 printk(KERN_ERR
"hfs: invalid catalog entry type in lookup\n");
110 inode
= hfsplus_iget(dir
->i_sb
, cnid
);
112 return ERR_CAST(inode
);
113 if (S_ISREG(inode
->i_mode
))
114 HFSPLUS_I(inode
)->linkid
= linkid
;
116 d_add(dentry
, inode
);
123 static int hfsplus_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
125 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
126 struct super_block
*sb
= inode
->i_sb
;
128 char strbuf
[HFSPLUS_MAX_STRLEN
+ 1];
129 hfsplus_cat_entry entry
;
130 struct hfs_find_data fd
;
131 struct hfsplus_readdir_data
*rd
;
134 if (filp
->f_pos
>= inode
->i_size
)
137 err
= hfs_find_init(HFSPLUS_SB(sb
)->cat_tree
, &fd
);
140 hfsplus_cat_build_key(sb
, fd
.search_key
, inode
->i_ino
, NULL
);
141 err
= hfs_brec_find(&fd
);
145 switch ((u32
)filp
->f_pos
) {
147 /* This is completely artificial... */
148 if (filldir(dirent
, ".", 1, 0, inode
->i_ino
, DT_DIR
))
153 hfs_bnode_read(fd
.bnode
, &entry
, fd
.entryoffset
,
155 if (be16_to_cpu(entry
.type
) != HFSPLUS_FOLDER_THREAD
) {
156 printk(KERN_ERR
"hfs: bad catalog folder thread\n");
160 if (fd
.entrylength
< HFSPLUS_MIN_THREAD_SZ
) {
161 printk(KERN_ERR
"hfs: truncated catalog thread\n");
165 if (filldir(dirent
, "..", 2, 1,
166 be32_to_cpu(entry
.thread
.parentID
), DT_DIR
))
171 if (filp
->f_pos
>= inode
->i_size
)
173 err
= hfs_brec_goto(&fd
, filp
->f_pos
- 1);
179 if (be32_to_cpu(fd
.key
->cat
.parent
) != inode
->i_ino
) {
180 printk(KERN_ERR
"hfs: walked past end of dir\n");
184 hfs_bnode_read(fd
.bnode
, &entry
, fd
.entryoffset
,
186 type
= be16_to_cpu(entry
.type
);
187 len
= HFSPLUS_MAX_STRLEN
;
188 err
= hfsplus_uni2asc(sb
, &fd
.key
->cat
.name
, strbuf
, &len
);
191 if (type
== HFSPLUS_FOLDER
) {
193 sizeof(struct hfsplus_cat_folder
)) {
194 printk(KERN_ERR
"hfs: small dir entry\n");
198 if (HFSPLUS_SB(sb
)->hidden_dir
&&
199 HFSPLUS_SB(sb
)->hidden_dir
->i_ino
==
200 be32_to_cpu(entry
.folder
.id
))
202 if (filldir(dirent
, strbuf
, len
, filp
->f_pos
,
203 be32_to_cpu(entry
.folder
.id
), DT_DIR
))
205 } else if (type
== HFSPLUS_FILE
) {
206 if (fd
.entrylength
< sizeof(struct hfsplus_cat_file
)) {
207 printk(KERN_ERR
"hfs: small file entry\n");
211 if (filldir(dirent
, strbuf
, len
, filp
->f_pos
,
212 be32_to_cpu(entry
.file
.id
), DT_REG
))
215 printk(KERN_ERR
"hfs: bad catalog entry type\n");
221 if (filp
->f_pos
>= inode
->i_size
)
223 err
= hfs_brec_goto(&fd
, 1);
227 rd
= filp
->private_data
;
229 rd
= kmalloc(sizeof(struct hfsplus_readdir_data
), GFP_KERNEL
);
234 filp
->private_data
= rd
;
236 list_add(&rd
->list
, &HFSPLUS_I(inode
)->open_dir_list
);
238 memcpy(&rd
->key
, fd
.key
, sizeof(struct hfsplus_cat_key
));
244 static int hfsplus_dir_release(struct inode
*inode
, struct file
*file
)
246 struct hfsplus_readdir_data
*rd
= file
->private_data
;
248 mutex_lock(&inode
->i_mutex
);
250 mutex_unlock(&inode
->i_mutex
);
256 static int hfsplus_link(struct dentry
*src_dentry
, struct inode
*dst_dir
,
257 struct dentry
*dst_dentry
)
259 struct hfsplus_sb_info
*sbi
= HFSPLUS_SB(dst_dir
->i_sb
);
260 struct inode
*inode
= src_dentry
->d_inode
;
261 struct inode
*src_dir
= src_dentry
->d_parent
->d_inode
;
267 if (HFSPLUS_IS_RSRC(inode
))
269 if (!S_ISREG(inode
->i_mode
))
272 mutex_lock(&sbi
->vh_mutex
);
273 if (inode
->i_ino
== (u32
)(unsigned long)src_dentry
->d_fsdata
) {
275 get_random_bytes(&id
, sizeof(cnid
));
278 str
.len
= sprintf(name
, "iNode%d", id
);
279 res
= hfsplus_rename_cat(inode
->i_ino
,
280 src_dir
, &src_dentry
->d_name
,
281 sbi
->hidden_dir
, &str
);
287 HFSPLUS_I(inode
)->linkid
= id
;
288 cnid
= sbi
->next_cnid
++;
289 src_dentry
->d_fsdata
= (void *)(unsigned long)cnid
;
290 res
= hfsplus_create_cat(cnid
, src_dir
,
291 &src_dentry
->d_name
, inode
);
297 cnid
= sbi
->next_cnid
++;
298 res
= hfsplus_create_cat(cnid
, dst_dir
, &dst_dentry
->d_name
, inode
);
303 hfsplus_instantiate(dst_dentry
, inode
, cnid
);
305 inode
->i_ctime
= CURRENT_TIME_SEC
;
306 mark_inode_dirty(inode
);
308 dst_dir
->i_sb
->s_dirt
= 1;
310 mutex_unlock(&sbi
->vh_mutex
);
314 static int hfsplus_unlink(struct inode
*dir
, struct dentry
*dentry
)
316 struct hfsplus_sb_info
*sbi
= HFSPLUS_SB(dir
->i_sb
);
317 struct inode
*inode
= dentry
->d_inode
;
323 if (HFSPLUS_IS_RSRC(inode
))
326 mutex_lock(&sbi
->vh_mutex
);
327 cnid
= (u32
)(unsigned long)dentry
->d_fsdata
;
328 if (inode
->i_ino
== cnid
&&
329 atomic_read(&HFSPLUS_I(inode
)->opencnt
)) {
331 str
.len
= sprintf(name
, "temp%lu", inode
->i_ino
);
332 res
= hfsplus_rename_cat(inode
->i_ino
,
333 dir
, &dentry
->d_name
,
334 sbi
->hidden_dir
, &str
);
336 inode
->i_flags
|= S_DEAD
;
341 res
= hfsplus_delete_cat(cnid
, dir
, &dentry
->d_name
);
345 if (inode
->i_nlink
> 0)
347 if (inode
->i_ino
== cnid
)
349 if (!inode
->i_nlink
) {
350 if (inode
->i_ino
!= cnid
) {
352 if (!atomic_read(&HFSPLUS_I(inode
)->opencnt
)) {
353 res
= hfsplus_delete_cat(inode
->i_ino
,
357 hfsplus_delete_inode(inode
);
359 inode
->i_flags
|= S_DEAD
;
361 hfsplus_delete_inode(inode
);
364 inode
->i_ctime
= CURRENT_TIME_SEC
;
365 mark_inode_dirty(inode
);
367 mutex_unlock(&sbi
->vh_mutex
);
371 static int hfsplus_rmdir(struct inode
*dir
, struct dentry
*dentry
)
373 struct hfsplus_sb_info
*sbi
= HFSPLUS_SB(dir
->i_sb
);
374 struct inode
*inode
= dentry
->d_inode
;
377 if (inode
->i_size
!= 2)
380 mutex_lock(&sbi
->vh_mutex
);
381 res
= hfsplus_delete_cat(inode
->i_ino
, dir
, &dentry
->d_name
);
385 inode
->i_ctime
= CURRENT_TIME_SEC
;
386 hfsplus_delete_inode(inode
);
387 mark_inode_dirty(inode
);
389 mutex_unlock(&sbi
->vh_mutex
);
393 static int hfsplus_symlink(struct inode
*dir
, struct dentry
*dentry
,
396 struct hfsplus_sb_info
*sbi
= HFSPLUS_SB(dir
->i_sb
);
400 mutex_lock(&sbi
->vh_mutex
);
401 inode
= hfsplus_new_inode(dir
->i_sb
, S_IFLNK
| S_IRWXUGO
);
405 res
= page_symlink(inode
, symname
, strlen(symname
) + 1);
409 res
= hfsplus_create_cat(inode
->i_ino
, dir
, &dentry
->d_name
, inode
);
413 hfsplus_instantiate(dentry
, inode
, inode
->i_ino
);
414 mark_inode_dirty(inode
);
419 hfsplus_delete_inode(inode
);
422 mutex_unlock(&sbi
->vh_mutex
);
426 static int hfsplus_mknod(struct inode
*dir
, struct dentry
*dentry
,
427 int mode
, dev_t rdev
)
429 struct hfsplus_sb_info
*sbi
= HFSPLUS_SB(dir
->i_sb
);
433 mutex_lock(&sbi
->vh_mutex
);
434 inode
= hfsplus_new_inode(dir
->i_sb
, mode
);
438 if (S_ISBLK(mode
) || S_ISCHR(mode
) || S_ISFIFO(mode
) || S_ISSOCK(mode
))
439 init_special_inode(inode
, mode
, rdev
);
441 res
= hfsplus_create_cat(inode
->i_ino
, dir
, &dentry
->d_name
, inode
);
444 hfsplus_delete_inode(inode
);
449 hfsplus_instantiate(dentry
, inode
, inode
->i_ino
);
450 mark_inode_dirty(inode
);
452 mutex_unlock(&sbi
->vh_mutex
);
456 static int hfsplus_create(struct inode
*dir
, struct dentry
*dentry
, int mode
,
457 struct nameidata
*nd
)
459 return hfsplus_mknod(dir
, dentry
, mode
, 0);
462 static int hfsplus_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
464 return hfsplus_mknod(dir
, dentry
, mode
| S_IFDIR
, 0);
467 static int hfsplus_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
468 struct inode
*new_dir
, struct dentry
*new_dentry
)
472 /* Unlink destination if it already exists */
473 if (new_dentry
->d_inode
) {
474 if (S_ISDIR(new_dentry
->d_inode
->i_mode
))
475 res
= hfsplus_rmdir(new_dir
, new_dentry
);
477 res
= hfsplus_unlink(new_dir
, new_dentry
);
482 res
= hfsplus_rename_cat((u32
)(unsigned long)old_dentry
->d_fsdata
,
483 old_dir
, &old_dentry
->d_name
,
484 new_dir
, &new_dentry
->d_name
);
486 new_dentry
->d_fsdata
= old_dentry
->d_fsdata
;
490 const struct inode_operations hfsplus_dir_inode_operations
= {
491 .lookup
= hfsplus_lookup
,
492 .create
= hfsplus_create
,
493 .link
= hfsplus_link
,
494 .unlink
= hfsplus_unlink
,
495 .mkdir
= hfsplus_mkdir
,
496 .rmdir
= hfsplus_rmdir
,
497 .symlink
= hfsplus_symlink
,
498 .mknod
= hfsplus_mknod
,
499 .rename
= hfsplus_rename
,
502 const struct file_operations hfsplus_dir_operations
= {
503 .fsync
= hfsplus_file_fsync
,
504 .read
= generic_read_dir
,
505 .readdir
= hfsplus_readdir
,
506 .unlocked_ioctl
= hfsplus_ioctl
,
507 .llseek
= generic_file_llseek
,
508 .release
= hfsplus_dir_release
,