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"
20 static inline void hfsplus_instantiate(struct dentry
*dentry
,
21 struct inode
*inode
, u32 cnid
)
23 dentry
->d_fsdata
= (void *)(unsigned long)cnid
;
24 d_instantiate(dentry
, inode
);
27 /* Find the entry inside dir named dentry->d_name */
28 static struct dentry
*hfsplus_lookup(struct inode
*dir
, struct dentry
*dentry
,
31 struct inode
*inode
= NULL
;
32 struct hfs_find_data fd
;
33 struct super_block
*sb
;
34 hfsplus_cat_entry entry
;
41 dentry
->d_fsdata
= NULL
;
42 err
= hfs_find_init(HFSPLUS_SB(sb
)->cat_tree
, &fd
);
45 hfsplus_cat_build_key(sb
, fd
.search_key
, dir
->i_ino
, &dentry
->d_name
);
47 err
= hfs_brec_read(&fd
, &entry
, sizeof(entry
));
57 type
= be16_to_cpu(entry
.type
);
58 if (type
== HFSPLUS_FOLDER
) {
59 if (fd
.entrylength
< sizeof(struct hfsplus_cat_folder
)) {
63 cnid
= be32_to_cpu(entry
.folder
.id
);
64 dentry
->d_fsdata
= (void *)(unsigned long)cnid
;
65 } else if (type
== HFSPLUS_FILE
) {
66 if (fd
.entrylength
< sizeof(struct hfsplus_cat_file
)) {
70 cnid
= be32_to_cpu(entry
.file
.id
);
71 if (entry
.file
.user_info
.fdType
==
72 cpu_to_be32(HFSP_HARDLINK_TYPE
) &&
73 entry
.file
.user_info
.fdCreator
==
74 cpu_to_be32(HFSP_HFSPLUS_CREATOR
) &&
75 (entry
.file
.create_date
==
76 HFSPLUS_I(HFSPLUS_SB(sb
)->hidden_dir
)->
78 entry
.file
.create_date
==
79 HFSPLUS_I(sb
->s_root
->d_inode
)->
81 HFSPLUS_SB(sb
)->hidden_dir
) {
85 if (dentry
->d_fsdata
) {
87 * We found a link pointing to another link,
88 * so ignore it and treat it as regular file.
90 cnid
= (unsigned long)dentry
->d_fsdata
;
93 dentry
->d_fsdata
= (void *)(unsigned long)cnid
;
95 be32_to_cpu(entry
.file
.permissions
.dev
);
96 str
.len
= sprintf(name
, "iNode%d", linkid
);
98 hfsplus_cat_build_key(sb
, fd
.search_key
,
99 HFSPLUS_SB(sb
)->hidden_dir
->i_ino
,
103 } else if (!dentry
->d_fsdata
)
104 dentry
->d_fsdata
= (void *)(unsigned long)cnid
;
106 printk(KERN_ERR
"hfs: invalid catalog entry type in lookup\n");
111 inode
= hfsplus_iget(dir
->i_sb
, cnid
);
113 return ERR_CAST(inode
);
114 if (S_ISREG(inode
->i_mode
))
115 HFSPLUS_I(inode
)->linkid
= linkid
;
117 d_add(dentry
, inode
);
124 static int hfsplus_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
126 struct inode
*inode
= file_inode(filp
);
127 struct super_block
*sb
= inode
->i_sb
;
129 char strbuf
[HFSPLUS_MAX_STRLEN
+ 1];
130 hfsplus_cat_entry entry
;
131 struct hfs_find_data fd
;
132 struct hfsplus_readdir_data
*rd
;
135 if (filp
->f_pos
>= inode
->i_size
)
138 err
= hfs_find_init(HFSPLUS_SB(sb
)->cat_tree
, &fd
);
141 hfsplus_cat_build_key(sb
, fd
.search_key
, inode
->i_ino
, NULL
);
142 err
= hfs_brec_find(&fd
, hfs_find_rec_by_key
);
146 switch ((u32
)filp
->f_pos
) {
148 /* This is completely artificial... */
149 if (filldir(dirent
, ".", 1, 0, inode
->i_ino
, DT_DIR
))
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 printk(KERN_ERR
"hfs: bad catalog folder thread\n");
166 if (fd
.entrylength
< HFSPLUS_MIN_THREAD_SZ
) {
167 printk(KERN_ERR
"hfs: truncated catalog thread\n");
171 if (filldir(dirent
, "..", 2, 1,
172 be32_to_cpu(entry
.thread
.parentID
), DT_DIR
))
177 if (filp
->f_pos
>= inode
->i_size
)
179 err
= hfs_brec_goto(&fd
, filp
->f_pos
- 1);
185 if (be32_to_cpu(fd
.key
->cat
.parent
) != inode
->i_ino
) {
186 printk(KERN_ERR
"hfs: walked past end of dir\n");
191 if (fd
.entrylength
> sizeof(entry
) || fd
.entrylength
< 0) {
196 hfs_bnode_read(fd
.bnode
, &entry
, fd
.entryoffset
,
198 type
= be16_to_cpu(entry
.type
);
199 len
= HFSPLUS_MAX_STRLEN
;
200 err
= hfsplus_uni2asc(sb
, &fd
.key
->cat
.name
, strbuf
, &len
);
203 if (type
== HFSPLUS_FOLDER
) {
205 sizeof(struct hfsplus_cat_folder
)) {
206 printk(KERN_ERR
"hfs: small dir entry\n");
210 if (HFSPLUS_SB(sb
)->hidden_dir
&&
211 HFSPLUS_SB(sb
)->hidden_dir
->i_ino
==
212 be32_to_cpu(entry
.folder
.id
))
214 if (filldir(dirent
, strbuf
, len
, filp
->f_pos
,
215 be32_to_cpu(entry
.folder
.id
), DT_DIR
))
217 } else if (type
== HFSPLUS_FILE
) {
218 if (fd
.entrylength
< sizeof(struct hfsplus_cat_file
)) {
219 printk(KERN_ERR
"hfs: small file entry\n");
223 if (filldir(dirent
, strbuf
, len
, filp
->f_pos
,
224 be32_to_cpu(entry
.file
.id
), DT_REG
))
227 printk(KERN_ERR
"hfs: bad catalog entry type\n");
233 if (filp
->f_pos
>= inode
->i_size
)
235 err
= hfs_brec_goto(&fd
, 1);
239 rd
= filp
->private_data
;
241 rd
= kmalloc(sizeof(struct hfsplus_readdir_data
), GFP_KERNEL
);
246 filp
->private_data
= rd
;
248 list_add(&rd
->list
, &HFSPLUS_I(inode
)->open_dir_list
);
250 memcpy(&rd
->key
, fd
.key
, sizeof(struct hfsplus_cat_key
));
256 static int hfsplus_dir_release(struct inode
*inode
, struct file
*file
)
258 struct hfsplus_readdir_data
*rd
= file
->private_data
;
260 mutex_lock(&inode
->i_mutex
);
262 mutex_unlock(&inode
->i_mutex
);
268 static int hfsplus_link(struct dentry
*src_dentry
, struct inode
*dst_dir
,
269 struct dentry
*dst_dentry
)
271 struct hfsplus_sb_info
*sbi
= HFSPLUS_SB(dst_dir
->i_sb
);
272 struct inode
*inode
= src_dentry
->d_inode
;
273 struct inode
*src_dir
= src_dentry
->d_parent
->d_inode
;
279 if (HFSPLUS_IS_RSRC(inode
))
281 if (!S_ISREG(inode
->i_mode
))
284 mutex_lock(&sbi
->vh_mutex
);
285 if (inode
->i_ino
== (u32
)(unsigned long)src_dentry
->d_fsdata
) {
287 get_random_bytes(&id
, sizeof(cnid
));
290 str
.len
= sprintf(name
, "iNode%d", id
);
291 res
= hfsplus_rename_cat(inode
->i_ino
,
292 src_dir
, &src_dentry
->d_name
,
293 sbi
->hidden_dir
, &str
);
299 HFSPLUS_I(inode
)->linkid
= id
;
300 cnid
= sbi
->next_cnid
++;
301 src_dentry
->d_fsdata
= (void *)(unsigned long)cnid
;
302 res
= hfsplus_create_cat(cnid
, src_dir
,
303 &src_dentry
->d_name
, inode
);
309 cnid
= sbi
->next_cnid
++;
310 res
= hfsplus_create_cat(cnid
, dst_dir
, &dst_dentry
->d_name
, inode
);
315 hfsplus_instantiate(dst_dentry
, inode
, cnid
);
317 inode
->i_ctime
= CURRENT_TIME_SEC
;
318 mark_inode_dirty(inode
);
320 hfsplus_mark_mdb_dirty(dst_dir
->i_sb
);
322 mutex_unlock(&sbi
->vh_mutex
);
326 static int hfsplus_unlink(struct inode
*dir
, struct dentry
*dentry
)
328 struct hfsplus_sb_info
*sbi
= HFSPLUS_SB(dir
->i_sb
);
329 struct inode
*inode
= dentry
->d_inode
;
335 if (HFSPLUS_IS_RSRC(inode
))
338 mutex_lock(&sbi
->vh_mutex
);
339 cnid
= (u32
)(unsigned long)dentry
->d_fsdata
;
340 if (inode
->i_ino
== cnid
&&
341 atomic_read(&HFSPLUS_I(inode
)->opencnt
)) {
343 str
.len
= sprintf(name
, "temp%lu", inode
->i_ino
);
344 res
= hfsplus_rename_cat(inode
->i_ino
,
345 dir
, &dentry
->d_name
,
346 sbi
->hidden_dir
, &str
);
348 inode
->i_flags
|= S_DEAD
;
353 res
= hfsplus_delete_cat(cnid
, dir
, &dentry
->d_name
);
357 if (inode
->i_nlink
> 0)
359 if (inode
->i_ino
== cnid
)
361 if (!inode
->i_nlink
) {
362 if (inode
->i_ino
!= cnid
) {
364 if (!atomic_read(&HFSPLUS_I(inode
)->opencnt
)) {
365 res
= hfsplus_delete_cat(inode
->i_ino
,
369 hfsplus_delete_inode(inode
);
371 inode
->i_flags
|= S_DEAD
;
373 hfsplus_delete_inode(inode
);
376 inode
->i_ctime
= CURRENT_TIME_SEC
;
377 mark_inode_dirty(inode
);
379 mutex_unlock(&sbi
->vh_mutex
);
383 static int hfsplus_rmdir(struct inode
*dir
, struct dentry
*dentry
)
385 struct hfsplus_sb_info
*sbi
= HFSPLUS_SB(dir
->i_sb
);
386 struct inode
*inode
= dentry
->d_inode
;
389 if (inode
->i_size
!= 2)
392 mutex_lock(&sbi
->vh_mutex
);
393 res
= hfsplus_delete_cat(inode
->i_ino
, dir
, &dentry
->d_name
);
397 inode
->i_ctime
= CURRENT_TIME_SEC
;
398 hfsplus_delete_inode(inode
);
399 mark_inode_dirty(inode
);
401 mutex_unlock(&sbi
->vh_mutex
);
405 static int hfsplus_symlink(struct inode
*dir
, struct dentry
*dentry
,
408 struct hfsplus_sb_info
*sbi
= HFSPLUS_SB(dir
->i_sb
);
412 mutex_lock(&sbi
->vh_mutex
);
413 inode
= hfsplus_new_inode(dir
->i_sb
, S_IFLNK
| S_IRWXUGO
);
417 res
= page_symlink(inode
, symname
, strlen(symname
) + 1);
421 res
= hfsplus_create_cat(inode
->i_ino
, dir
, &dentry
->d_name
, inode
);
425 res
= hfsplus_init_inode_security(inode
, dir
, &dentry
->d_name
);
426 if (res
== -EOPNOTSUPP
)
427 res
= 0; /* Operation is not supported. */
429 /* Try to delete anyway without error analysis. */
430 hfsplus_delete_cat(inode
->i_ino
, dir
, &dentry
->d_name
);
434 hfsplus_instantiate(dentry
, inode
, inode
->i_ino
);
435 mark_inode_dirty(inode
);
440 hfsplus_delete_inode(inode
);
443 mutex_unlock(&sbi
->vh_mutex
);
447 static int hfsplus_mknod(struct inode
*dir
, struct dentry
*dentry
,
448 umode_t mode
, dev_t rdev
)
450 struct hfsplus_sb_info
*sbi
= HFSPLUS_SB(dir
->i_sb
);
454 mutex_lock(&sbi
->vh_mutex
);
455 inode
= hfsplus_new_inode(dir
->i_sb
, mode
);
459 if (S_ISBLK(mode
) || S_ISCHR(mode
) || S_ISFIFO(mode
) || S_ISSOCK(mode
))
460 init_special_inode(inode
, mode
, rdev
);
462 res
= hfsplus_create_cat(inode
->i_ino
, dir
, &dentry
->d_name
, inode
);
466 res
= hfsplus_init_inode_security(inode
, dir
, &dentry
->d_name
);
467 if (res
== -EOPNOTSUPP
)
468 res
= 0; /* Operation is not supported. */
470 /* Try to delete anyway without error analysis. */
471 hfsplus_delete_cat(inode
->i_ino
, dir
, &dentry
->d_name
);
475 hfsplus_instantiate(dentry
, inode
, inode
->i_ino
);
476 mark_inode_dirty(inode
);
481 hfsplus_delete_inode(inode
);
484 mutex_unlock(&sbi
->vh_mutex
);
488 static int hfsplus_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
491 return hfsplus_mknod(dir
, dentry
, mode
, 0);
494 static int hfsplus_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
496 return hfsplus_mknod(dir
, dentry
, mode
| S_IFDIR
, 0);
499 static int hfsplus_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
500 struct inode
*new_dir
, struct dentry
*new_dentry
)
504 /* Unlink destination if it already exists */
505 if (new_dentry
->d_inode
) {
506 if (S_ISDIR(new_dentry
->d_inode
->i_mode
))
507 res
= hfsplus_rmdir(new_dir
, new_dentry
);
509 res
= hfsplus_unlink(new_dir
, new_dentry
);
514 res
= hfsplus_rename_cat((u32
)(unsigned long)old_dentry
->d_fsdata
,
515 old_dir
, &old_dentry
->d_name
,
516 new_dir
, &new_dentry
->d_name
);
518 new_dentry
->d_fsdata
= old_dentry
->d_fsdata
;
522 const struct inode_operations hfsplus_dir_inode_operations
= {
523 .lookup
= hfsplus_lookup
,
524 .create
= hfsplus_create
,
525 .link
= hfsplus_link
,
526 .unlink
= hfsplus_unlink
,
527 .mkdir
= hfsplus_mkdir
,
528 .rmdir
= hfsplus_rmdir
,
529 .symlink
= hfsplus_symlink
,
530 .mknod
= hfsplus_mknod
,
531 .rename
= hfsplus_rename
,
532 .setxattr
= generic_setxattr
,
533 .getxattr
= generic_getxattr
,
534 .listxattr
= hfsplus_listxattr
,
535 .removexattr
= hfsplus_removexattr
,
538 const struct file_operations hfsplus_dir_operations
= {
539 .fsync
= hfsplus_file_fsync
,
540 .read
= generic_read_dir
,
541 .readdir
= hfsplus_readdir
,
542 .unlocked_ioctl
= hfsplus_ioctl
,
543 .llseek
= generic_file_llseek
,
544 .release
= hfsplus_dir_release
,