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_op
= &hfsplus_dentry_operations
;
41 dentry
->d_fsdata
= NULL
;
42 hfs_find_init(HFSPLUS_SB(sb
)->cat_tree
, &fd
);
43 hfsplus_cat_build_key(sb
, fd
.search_key
, dir
->i_ino
, &dentry
->d_name
);
45 err
= hfs_brec_read(&fd
, &entry
, sizeof(entry
));
55 type
= be16_to_cpu(entry
.type
);
56 if (type
== HFSPLUS_FOLDER
) {
57 if (fd
.entrylength
< sizeof(struct hfsplus_cat_folder
)) {
61 cnid
= be32_to_cpu(entry
.folder
.id
);
62 dentry
->d_fsdata
= (void *)(unsigned long)cnid
;
63 } else if (type
== HFSPLUS_FILE
) {
64 if (fd
.entrylength
< sizeof(struct hfsplus_cat_file
)) {
68 cnid
= be32_to_cpu(entry
.file
.id
);
69 if (entry
.file
.user_info
.fdType
== cpu_to_be32(HFSP_HARDLINK_TYPE
) &&
70 entry
.file
.user_info
.fdCreator
== cpu_to_be32(HFSP_HFSPLUS_CREATOR
) &&
71 (entry
.file
.create_date
== HFSPLUS_I(HFSPLUS_SB(sb
)->hidden_dir
)->create_date
||
72 entry
.file
.create_date
== HFSPLUS_I(sb
->s_root
->d_inode
)->create_date
) &&
73 HFSPLUS_SB(sb
)->hidden_dir
) {
77 if (dentry
->d_fsdata
) {
79 * We found a link pointing to another link,
80 * so ignore it and treat it as regular file.
82 cnid
= (unsigned long)dentry
->d_fsdata
;
85 dentry
->d_fsdata
= (void *)(unsigned long)cnid
;
86 linkid
= be32_to_cpu(entry
.file
.permissions
.dev
);
87 str
.len
= sprintf(name
, "iNode%d", linkid
);
89 hfsplus_cat_build_key(sb
, fd
.search_key
,
90 HFSPLUS_SB(sb
)->hidden_dir
->i_ino
, &str
);
93 } else if (!dentry
->d_fsdata
)
94 dentry
->d_fsdata
= (void *)(unsigned long)cnid
;
96 printk(KERN_ERR
"hfs: invalid catalog entry type in lookup\n");
101 inode
= hfsplus_iget(dir
->i_sb
, cnid
);
103 return ERR_CAST(inode
);
104 if (S_ISREG(inode
->i_mode
))
105 HFSPLUS_I(inode
)->linkid
= linkid
;
107 d_add(dentry
, inode
);
114 static int hfsplus_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
116 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
117 struct super_block
*sb
= inode
->i_sb
;
119 char strbuf
[HFSPLUS_MAX_STRLEN
+ 1];
120 hfsplus_cat_entry entry
;
121 struct hfs_find_data fd
;
122 struct hfsplus_readdir_data
*rd
;
125 if (filp
->f_pos
>= inode
->i_size
)
128 hfs_find_init(HFSPLUS_SB(sb
)->cat_tree
, &fd
);
129 hfsplus_cat_build_key(sb
, fd
.search_key
, inode
->i_ino
, NULL
);
130 err
= hfs_brec_find(&fd
);
134 switch ((u32
)filp
->f_pos
) {
136 /* This is completely artificial... */
137 if (filldir(dirent
, ".", 1, 0, inode
->i_ino
, DT_DIR
))
142 hfs_bnode_read(fd
.bnode
, &entry
, fd
.entryoffset
, fd
.entrylength
);
143 if (be16_to_cpu(entry
.type
) != HFSPLUS_FOLDER_THREAD
) {
144 printk(KERN_ERR
"hfs: bad catalog folder thread\n");
148 if (fd
.entrylength
< HFSPLUS_MIN_THREAD_SZ
) {
149 printk(KERN_ERR
"hfs: truncated catalog thread\n");
153 if (filldir(dirent
, "..", 2, 1,
154 be32_to_cpu(entry
.thread
.parentID
), DT_DIR
))
159 if (filp
->f_pos
>= inode
->i_size
)
161 err
= hfs_brec_goto(&fd
, filp
->f_pos
- 1);
167 if (be32_to_cpu(fd
.key
->cat
.parent
) != inode
->i_ino
) {
168 printk(KERN_ERR
"hfs: walked past end of dir\n");
172 hfs_bnode_read(fd
.bnode
, &entry
, fd
.entryoffset
, fd
.entrylength
);
173 type
= be16_to_cpu(entry
.type
);
174 len
= HFSPLUS_MAX_STRLEN
;
175 err
= hfsplus_uni2asc(sb
, &fd
.key
->cat
.name
, strbuf
, &len
);
178 if (type
== HFSPLUS_FOLDER
) {
179 if (fd
.entrylength
< sizeof(struct hfsplus_cat_folder
)) {
180 printk(KERN_ERR
"hfs: small dir entry\n");
184 if (HFSPLUS_SB(sb
)->hidden_dir
&&
185 HFSPLUS_SB(sb
)->hidden_dir
->i_ino
==
186 be32_to_cpu(entry
.folder
.id
))
188 if (filldir(dirent
, strbuf
, len
, filp
->f_pos
,
189 be32_to_cpu(entry
.folder
.id
), DT_DIR
))
191 } else if (type
== HFSPLUS_FILE
) {
192 if (fd
.entrylength
< sizeof(struct hfsplus_cat_file
)) {
193 printk(KERN_ERR
"hfs: small file entry\n");
197 if (filldir(dirent
, strbuf
, len
, filp
->f_pos
,
198 be32_to_cpu(entry
.file
.id
), DT_REG
))
201 printk(KERN_ERR
"hfs: bad catalog entry type\n");
207 if (filp
->f_pos
>= inode
->i_size
)
209 err
= hfs_brec_goto(&fd
, 1);
213 rd
= filp
->private_data
;
215 rd
= kmalloc(sizeof(struct hfsplus_readdir_data
), GFP_KERNEL
);
220 filp
->private_data
= rd
;
222 list_add(&rd
->list
, &HFSPLUS_I(inode
)->open_dir_list
);
224 memcpy(&rd
->key
, fd
.key
, sizeof(struct hfsplus_cat_key
));
230 static int hfsplus_dir_release(struct inode
*inode
, struct file
*file
)
232 struct hfsplus_readdir_data
*rd
= file
->private_data
;
234 mutex_lock(&inode
->i_mutex
);
236 mutex_unlock(&inode
->i_mutex
);
242 static int hfsplus_link(struct dentry
*src_dentry
, struct inode
*dst_dir
,
243 struct dentry
*dst_dentry
)
245 struct hfsplus_sb_info
*sbi
= HFSPLUS_SB(dst_dir
->i_sb
);
246 struct inode
*inode
= src_dentry
->d_inode
;
247 struct inode
*src_dir
= src_dentry
->d_parent
->d_inode
;
253 if (HFSPLUS_IS_RSRC(inode
))
255 if (!S_ISREG(inode
->i_mode
))
258 mutex_lock(&sbi
->vh_mutex
);
259 if (inode
->i_ino
== (u32
)(unsigned long)src_dentry
->d_fsdata
) {
261 get_random_bytes(&id
, sizeof(cnid
));
264 str
.len
= sprintf(name
, "iNode%d", id
);
265 res
= hfsplus_rename_cat(inode
->i_ino
,
266 src_dir
, &src_dentry
->d_name
,
267 sbi
->hidden_dir
, &str
);
273 HFSPLUS_I(inode
)->linkid
= id
;
274 cnid
= sbi
->next_cnid
++;
275 src_dentry
->d_fsdata
= (void *)(unsigned long)cnid
;
276 res
= hfsplus_create_cat(cnid
, src_dir
, &src_dentry
->d_name
, inode
);
282 cnid
= sbi
->next_cnid
++;
283 res
= hfsplus_create_cat(cnid
, dst_dir
, &dst_dentry
->d_name
, inode
);
288 hfsplus_instantiate(dst_dentry
, inode
, cnid
);
290 inode
->i_ctime
= CURRENT_TIME_SEC
;
291 mark_inode_dirty(inode
);
293 dst_dir
->i_sb
->s_dirt
= 1;
295 mutex_unlock(&sbi
->vh_mutex
);
299 static int hfsplus_unlink(struct inode
*dir
, struct dentry
*dentry
)
301 struct hfsplus_sb_info
*sbi
= HFSPLUS_SB(dir
->i_sb
);
302 struct inode
*inode
= dentry
->d_inode
;
308 if (HFSPLUS_IS_RSRC(inode
))
311 mutex_lock(&sbi
->vh_mutex
);
312 cnid
= (u32
)(unsigned long)dentry
->d_fsdata
;
313 if (inode
->i_ino
== cnid
&&
314 atomic_read(&HFSPLUS_I(inode
)->opencnt
)) {
316 str
.len
= sprintf(name
, "temp%lu", inode
->i_ino
);
317 res
= hfsplus_rename_cat(inode
->i_ino
,
318 dir
, &dentry
->d_name
,
319 sbi
->hidden_dir
, &str
);
321 inode
->i_flags
|= S_DEAD
;
326 res
= hfsplus_delete_cat(cnid
, dir
, &dentry
->d_name
);
330 if (inode
->i_nlink
> 0)
332 if (inode
->i_ino
== cnid
)
334 if (!inode
->i_nlink
) {
335 if (inode
->i_ino
!= cnid
) {
337 if (!atomic_read(&HFSPLUS_I(inode
)->opencnt
)) {
338 res
= hfsplus_delete_cat(inode
->i_ino
,
342 hfsplus_delete_inode(inode
);
344 inode
->i_flags
|= S_DEAD
;
346 hfsplus_delete_inode(inode
);
349 inode
->i_ctime
= CURRENT_TIME_SEC
;
350 mark_inode_dirty(inode
);
352 mutex_unlock(&sbi
->vh_mutex
);
356 static int hfsplus_rmdir(struct inode
*dir
, struct dentry
*dentry
)
358 struct hfsplus_sb_info
*sbi
= HFSPLUS_SB(dir
->i_sb
);
359 struct inode
*inode
= dentry
->d_inode
;
362 if (inode
->i_size
!= 2)
365 mutex_lock(&sbi
->vh_mutex
);
366 res
= hfsplus_delete_cat(inode
->i_ino
, dir
, &dentry
->d_name
);
370 inode
->i_ctime
= CURRENT_TIME_SEC
;
371 hfsplus_delete_inode(inode
);
372 mark_inode_dirty(inode
);
374 mutex_unlock(&sbi
->vh_mutex
);
378 static int hfsplus_symlink(struct inode
*dir
, struct dentry
*dentry
,
381 struct hfsplus_sb_info
*sbi
= HFSPLUS_SB(dir
->i_sb
);
385 mutex_lock(&sbi
->vh_mutex
);
386 inode
= hfsplus_new_inode(dir
->i_sb
, S_IFLNK
| S_IRWXUGO
);
390 res
= page_symlink(inode
, symname
, strlen(symname
) + 1);
394 res
= hfsplus_create_cat(inode
->i_ino
, dir
, &dentry
->d_name
, inode
);
398 hfsplus_instantiate(dentry
, inode
, inode
->i_ino
);
399 mark_inode_dirty(inode
);
404 hfsplus_delete_inode(inode
);
407 mutex_unlock(&sbi
->vh_mutex
);
411 static int hfsplus_mknod(struct inode
*dir
, struct dentry
*dentry
,
412 int mode
, dev_t rdev
)
414 struct hfsplus_sb_info
*sbi
= HFSPLUS_SB(dir
->i_sb
);
418 mutex_lock(&sbi
->vh_mutex
);
419 inode
= hfsplus_new_inode(dir
->i_sb
, mode
);
423 if (S_ISBLK(mode
) || S_ISCHR(mode
) || S_ISFIFO(mode
) || S_ISSOCK(mode
))
424 init_special_inode(inode
, mode
, rdev
);
426 res
= hfsplus_create_cat(inode
->i_ino
, dir
, &dentry
->d_name
, inode
);
429 hfsplus_delete_inode(inode
);
434 hfsplus_instantiate(dentry
, inode
, inode
->i_ino
);
435 mark_inode_dirty(inode
);
437 mutex_unlock(&sbi
->vh_mutex
);
441 static int hfsplus_create(struct inode
*dir
, struct dentry
*dentry
, int mode
,
442 struct nameidata
*nd
)
444 return hfsplus_mknod(dir
, dentry
, mode
, 0);
447 static int hfsplus_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
449 return hfsplus_mknod(dir
, dentry
, mode
| S_IFDIR
, 0);
452 static int hfsplus_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
453 struct inode
*new_dir
, struct dentry
*new_dentry
)
457 /* Unlink destination if it already exists */
458 if (new_dentry
->d_inode
) {
459 if (S_ISDIR(new_dentry
->d_inode
->i_mode
))
460 res
= hfsplus_rmdir(new_dir
, new_dentry
);
462 res
= hfsplus_unlink(new_dir
, new_dentry
);
467 res
= hfsplus_rename_cat((u32
)(unsigned long)old_dentry
->d_fsdata
,
468 old_dir
, &old_dentry
->d_name
,
469 new_dir
, &new_dentry
->d_name
);
471 new_dentry
->d_fsdata
= old_dentry
->d_fsdata
;
475 const struct inode_operations hfsplus_dir_inode_operations
= {
476 .lookup
= hfsplus_lookup
,
477 .create
= hfsplus_create
,
478 .link
= hfsplus_link
,
479 .unlink
= hfsplus_unlink
,
480 .mkdir
= hfsplus_mkdir
,
481 .rmdir
= hfsplus_rmdir
,
482 .symlink
= hfsplus_symlink
,
483 .mknod
= hfsplus_mknod
,
484 .rename
= hfsplus_rename
,
487 const struct file_operations hfsplus_dir_operations
= {
488 .read
= generic_read_dir
,
489 .readdir
= hfsplus_readdir
,
490 .unlocked_ioctl
= hfsplus_ioctl
,
491 .llseek
= generic_file_llseek
,
492 .release
= hfsplus_dir_release
,