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
;
39 dentry
->d_fsdata
= NULL
;
40 hfs_find_init(HFSPLUS_SB(sb
).cat_tree
, &fd
);
41 hfsplus_cat_build_key(sb
, fd
.search_key
, dir
->i_ino
, &dentry
->d_name
);
43 err
= hfs_brec_read(&fd
, &entry
, sizeof(entry
));
53 type
= be16_to_cpu(entry
.type
);
54 if (type
== HFSPLUS_FOLDER
) {
55 if (fd
.entrylength
< sizeof(struct hfsplus_cat_folder
)) {
59 cnid
= be32_to_cpu(entry
.folder
.id
);
60 dentry
->d_fsdata
= (void *)(unsigned long)cnid
;
61 } else if (type
== HFSPLUS_FILE
) {
62 if (fd
.entrylength
< sizeof(struct hfsplus_cat_file
)) {
66 cnid
= be32_to_cpu(entry
.file
.id
);
67 if (entry
.file
.user_info
.fdType
== cpu_to_be32(HFSP_HARDLINK_TYPE
) &&
68 entry
.file
.user_info
.fdCreator
== cpu_to_be32(HFSP_HFSPLUS_CREATOR
) &&
69 (entry
.file
.create_date
== HFSPLUS_I(HFSPLUS_SB(sb
).hidden_dir
).create_date
||
70 entry
.file
.create_date
== HFSPLUS_I(sb
->s_root
->d_inode
).create_date
) &&
71 HFSPLUS_SB(sb
).hidden_dir
) {
75 if (dentry
->d_fsdata
) {
77 * We found a link pointing to another link,
78 * so ignore it and treat it as regular file.
80 cnid
= (unsigned long)dentry
->d_fsdata
;
83 dentry
->d_fsdata
= (void *)(unsigned long)cnid
;
84 linkid
= be32_to_cpu(entry
.file
.permissions
.dev
);
85 str
.len
= sprintf(name
, "iNode%d", linkid
);
87 hfsplus_cat_build_key(sb
, fd
.search_key
, HFSPLUS_SB(sb
).hidden_dir
->i_ino
, &str
);
90 } else if (!dentry
->d_fsdata
)
91 dentry
->d_fsdata
= (void *)(unsigned long)cnid
;
93 printk(KERN_ERR
"hfs: invalid catalog entry type in lookup\n");
98 inode
= iget(dir
->i_sb
, cnid
);
100 return ERR_PTR(-EACCES
);
101 if (S_ISREG(inode
->i_mode
))
102 HFSPLUS_I(inode
).dev
= linkid
;
104 d_add(dentry
, inode
);
111 static int hfsplus_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
113 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
114 struct super_block
*sb
= inode
->i_sb
;
116 char strbuf
[HFSPLUS_MAX_STRLEN
+ 1];
117 hfsplus_cat_entry entry
;
118 struct hfs_find_data fd
;
119 struct hfsplus_readdir_data
*rd
;
122 if (filp
->f_pos
>= inode
->i_size
)
125 hfs_find_init(HFSPLUS_SB(sb
).cat_tree
, &fd
);
126 hfsplus_cat_build_key(sb
, fd
.search_key
, inode
->i_ino
, NULL
);
127 err
= hfs_brec_find(&fd
);
131 switch ((u32
)filp
->f_pos
) {
133 /* This is completely artificial... */
134 if (filldir(dirent
, ".", 1, 0, inode
->i_ino
, DT_DIR
))
139 hfs_bnode_read(fd
.bnode
, &entry
, fd
.entryoffset
, fd
.entrylength
);
140 if (be16_to_cpu(entry
.type
) != HFSPLUS_FOLDER_THREAD
) {
141 printk(KERN_ERR
"hfs: bad catalog folder thread\n");
145 if (fd
.entrylength
< HFSPLUS_MIN_THREAD_SZ
) {
146 printk(KERN_ERR
"hfs: truncated catalog thread\n");
150 if (filldir(dirent
, "..", 2, 1,
151 be32_to_cpu(entry
.thread
.parentID
), DT_DIR
))
156 if (filp
->f_pos
>= inode
->i_size
)
158 err
= hfs_brec_goto(&fd
, filp
->f_pos
- 1);
164 if (be32_to_cpu(fd
.key
->cat
.parent
) != inode
->i_ino
) {
165 printk(KERN_ERR
"hfs: walked past end of dir\n");
169 hfs_bnode_read(fd
.bnode
, &entry
, fd
.entryoffset
, fd
.entrylength
);
170 type
= be16_to_cpu(entry
.type
);
171 len
= HFSPLUS_MAX_STRLEN
;
172 err
= hfsplus_uni2asc(sb
, &fd
.key
->cat
.name
, strbuf
, &len
);
175 if (type
== HFSPLUS_FOLDER
) {
176 if (fd
.entrylength
< sizeof(struct hfsplus_cat_folder
)) {
177 printk(KERN_ERR
"hfs: small dir entry\n");
181 if (HFSPLUS_SB(sb
).hidden_dir
&&
182 HFSPLUS_SB(sb
).hidden_dir
->i_ino
== be32_to_cpu(entry
.folder
.id
))
184 if (filldir(dirent
, strbuf
, len
, filp
->f_pos
,
185 be32_to_cpu(entry
.folder
.id
), DT_DIR
))
187 } else if (type
== HFSPLUS_FILE
) {
188 if (fd
.entrylength
< sizeof(struct hfsplus_cat_file
)) {
189 printk(KERN_ERR
"hfs: small file entry\n");
193 if (filldir(dirent
, strbuf
, len
, filp
->f_pos
,
194 be32_to_cpu(entry
.file
.id
), DT_REG
))
197 printk(KERN_ERR
"hfs: bad catalog entry type\n");
203 if (filp
->f_pos
>= inode
->i_size
)
205 err
= hfs_brec_goto(&fd
, 1);
209 rd
= filp
->private_data
;
211 rd
= kmalloc(sizeof(struct hfsplus_readdir_data
), GFP_KERNEL
);
216 filp
->private_data
= rd
;
218 list_add(&rd
->list
, &HFSPLUS_I(inode
).open_dir_list
);
220 memcpy(&rd
->key
, fd
.key
, sizeof(struct hfsplus_cat_key
));
226 static int hfsplus_dir_release(struct inode
*inode
, struct file
*file
)
228 struct hfsplus_readdir_data
*rd
= file
->private_data
;
236 static int hfsplus_create(struct inode
*dir
, struct dentry
*dentry
, int mode
,
237 struct nameidata
*nd
)
242 inode
= hfsplus_new_inode(dir
->i_sb
, mode
);
246 res
= hfsplus_create_cat(inode
->i_ino
, dir
, &dentry
->d_name
, inode
);
249 hfsplus_delete_inode(inode
);
253 hfsplus_instantiate(dentry
, inode
, inode
->i_ino
);
254 mark_inode_dirty(inode
);
258 static int hfsplus_link(struct dentry
*src_dentry
, struct inode
*dst_dir
,
259 struct dentry
*dst_dentry
)
261 struct super_block
*sb
= dst_dir
->i_sb
;
262 struct inode
*inode
= src_dentry
->d_inode
;
263 struct inode
*src_dir
= src_dentry
->d_parent
->d_inode
;
269 if (HFSPLUS_IS_RSRC(inode
))
272 if (inode
->i_ino
== (u32
)(unsigned long)src_dentry
->d_fsdata
) {
274 get_random_bytes(&id
, sizeof(cnid
));
277 str
.len
= sprintf(name
, "iNode%d", id
);
278 res
= hfsplus_rename_cat(inode
->i_ino
,
279 src_dir
, &src_dentry
->d_name
,
280 HFSPLUS_SB(sb
).hidden_dir
, &str
);
286 HFSPLUS_I(inode
).dev
= id
;
287 cnid
= HFSPLUS_SB(sb
).next_cnid
++;
288 src_dentry
->d_fsdata
= (void *)(unsigned long)cnid
;
289 res
= hfsplus_create_cat(cnid
, src_dir
, &src_dentry
->d_name
, inode
);
293 HFSPLUS_SB(sb
).file_count
++;
295 cnid
= HFSPLUS_SB(sb
).next_cnid
++;
296 res
= hfsplus_create_cat(cnid
, dst_dir
, &dst_dentry
->d_name
, inode
);
301 hfsplus_instantiate(dst_dentry
, inode
, cnid
);
302 atomic_inc(&inode
->i_count
);
303 inode
->i_ctime
= CURRENT_TIME_SEC
;
304 mark_inode_dirty(inode
);
305 HFSPLUS_SB(sb
).file_count
++;
311 static int hfsplus_unlink(struct inode
*dir
, struct dentry
*dentry
)
313 struct super_block
*sb
= dir
->i_sb
;
314 struct inode
*inode
= dentry
->d_inode
;
320 if (HFSPLUS_IS_RSRC(inode
))
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 HFSPLUS_SB(sb
).hidden_dir
, &str
);
332 inode
->i_flags
|= S_DEAD
;
335 res
= hfsplus_delete_cat(cnid
, dir
, &dentry
->d_name
);
339 if (inode
->i_nlink
> 0)
341 hfsplus_delete_inode(inode
);
342 if (inode
->i_ino
!= cnid
&& !inode
->i_nlink
) {
343 if (!atomic_read(&HFSPLUS_I(inode
).opencnt
)) {
344 res
= hfsplus_delete_cat(inode
->i_ino
, HFSPLUS_SB(sb
).hidden_dir
, NULL
);
346 hfsplus_delete_inode(inode
);
348 inode
->i_flags
|= S_DEAD
;
351 inode
->i_ctime
= CURRENT_TIME_SEC
;
352 mark_inode_dirty(inode
);
357 static int hfsplus_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
362 inode
= hfsplus_new_inode(dir
->i_sb
, S_IFDIR
| mode
);
366 res
= hfsplus_create_cat(inode
->i_ino
, dir
, &dentry
->d_name
, inode
);
369 hfsplus_delete_inode(inode
);
373 hfsplus_instantiate(dentry
, inode
, inode
->i_ino
);
374 mark_inode_dirty(inode
);
378 static int hfsplus_rmdir(struct inode
*dir
, struct dentry
*dentry
)
383 inode
= dentry
->d_inode
;
384 if (inode
->i_size
!= 2)
386 res
= hfsplus_delete_cat(inode
->i_ino
, dir
, &dentry
->d_name
);
390 inode
->i_ctime
= CURRENT_TIME_SEC
;
391 hfsplus_delete_inode(inode
);
392 mark_inode_dirty(inode
);
396 static int hfsplus_symlink(struct inode
*dir
, struct dentry
*dentry
,
399 struct super_block
*sb
;
404 inode
= hfsplus_new_inode(sb
, S_IFLNK
| S_IRWXUGO
);
408 res
= page_symlink(inode
, symname
, strlen(symname
) + 1);
411 hfsplus_delete_inode(inode
);
416 mark_inode_dirty(inode
);
417 res
= hfsplus_create_cat(inode
->i_ino
, dir
, &dentry
->d_name
, inode
);
420 hfsplus_instantiate(dentry
, inode
, inode
->i_ino
);
421 mark_inode_dirty(inode
);
427 static int hfsplus_mknod(struct inode
*dir
, struct dentry
*dentry
,
428 int mode
, dev_t rdev
)
430 struct super_block
*sb
;
435 inode
= hfsplus_new_inode(sb
, mode
);
439 res
= hfsplus_create_cat(inode
->i_ino
, dir
, &dentry
->d_name
, inode
);
442 hfsplus_delete_inode(inode
);
446 init_special_inode(inode
, mode
, rdev
);
447 hfsplus_instantiate(dentry
, inode
, inode
->i_ino
);
448 mark_inode_dirty(inode
);
453 static int hfsplus_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
454 struct inode
*new_dir
, struct dentry
*new_dentry
)
458 /* Unlink destination if it already exists */
459 if (new_dentry
->d_inode
) {
460 res
= hfsplus_unlink(new_dir
, new_dentry
);
465 res
= hfsplus_rename_cat((u32
)(unsigned long)old_dentry
->d_fsdata
,
466 old_dir
, &old_dentry
->d_name
,
467 new_dir
, &new_dentry
->d_name
);
469 new_dentry
->d_fsdata
= old_dentry
->d_fsdata
;
473 const struct inode_operations hfsplus_dir_inode_operations
= {
474 .lookup
= hfsplus_lookup
,
475 .create
= hfsplus_create
,
476 .link
= hfsplus_link
,
477 .unlink
= hfsplus_unlink
,
478 .mkdir
= hfsplus_mkdir
,
479 .rmdir
= hfsplus_rmdir
,
480 .symlink
= hfsplus_symlink
,
481 .mknod
= hfsplus_mknod
,
482 .rename
= hfsplus_rename
,
485 const struct file_operations hfsplus_dir_operations
= {
486 .read
= generic_read_dir
,
487 .readdir
= hfsplus_readdir
,
488 .ioctl
= hfsplus_ioctl
,
489 .llseek
= generic_file_llseek
,
490 .release
= hfsplus_dir_release
,