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>
15 #include <linux/nls.h>
17 #include "hfsplus_fs.h"
18 #include "hfsplus_raw.h"
22 static inline void hfsplus_instantiate(struct dentry
*dentry
,
23 struct inode
*inode
, u32 cnid
)
25 dentry
->d_fsdata
= (void *)(unsigned long)cnid
;
26 d_instantiate(dentry
, inode
);
29 /* Find the entry inside dir named dentry->d_name */
30 static struct dentry
*hfsplus_lookup(struct inode
*dir
, struct dentry
*dentry
,
33 struct inode
*inode
= NULL
;
34 struct hfs_find_data fd
;
35 struct super_block
*sb
;
36 hfsplus_cat_entry entry
;
43 dentry
->d_fsdata
= NULL
;
44 err
= hfs_find_init(HFSPLUS_SB(sb
)->cat_tree
, &fd
);
47 err
= hfsplus_cat_build_key(sb
, fd
.search_key
, dir
->i_ino
,
49 if (unlikely(err
< 0))
52 err
= hfs_brec_read(&fd
, &entry
, sizeof(entry
));
62 type
= be16_to_cpu(entry
.type
);
63 if (type
== HFSPLUS_FOLDER
) {
64 if (fd
.entrylength
< sizeof(struct hfsplus_cat_folder
)) {
68 cnid
= be32_to_cpu(entry
.folder
.id
);
69 dentry
->d_fsdata
= (void *)(unsigned long)cnid
;
70 } else if (type
== HFSPLUS_FILE
) {
71 if (fd
.entrylength
< sizeof(struct hfsplus_cat_file
)) {
75 cnid
= be32_to_cpu(entry
.file
.id
);
76 if (entry
.file
.user_info
.fdType
==
77 cpu_to_be32(HFSP_HARDLINK_TYPE
) &&
78 entry
.file
.user_info
.fdCreator
==
79 cpu_to_be32(HFSP_HFSPLUS_CREATOR
) &&
80 (entry
.file
.create_date
==
81 HFSPLUS_I(HFSPLUS_SB(sb
)->hidden_dir
)->
83 entry
.file
.create_date
==
84 HFSPLUS_I(d_inode(sb
->s_root
))->
86 HFSPLUS_SB(sb
)->hidden_dir
) {
90 if (dentry
->d_fsdata
) {
92 * We found a link pointing to another link,
93 * so ignore it and treat it as regular file.
95 cnid
= (unsigned long)dentry
->d_fsdata
;
98 dentry
->d_fsdata
= (void *)(unsigned long)cnid
;
100 be32_to_cpu(entry
.file
.permissions
.dev
);
101 str
.len
= sprintf(name
, "iNode%d", linkid
);
103 err
= hfsplus_cat_build_key(sb
, fd
.search_key
,
104 HFSPLUS_SB(sb
)->hidden_dir
->i_ino
,
106 if (unlikely(err
< 0))
110 } else if (!dentry
->d_fsdata
)
111 dentry
->d_fsdata
= (void *)(unsigned long)cnid
;
113 pr_err("invalid catalog entry type in lookup\n");
118 inode
= hfsplus_iget(dir
->i_sb
, cnid
);
120 return ERR_CAST(inode
);
121 if (S_ISREG(inode
->i_mode
))
122 HFSPLUS_I(inode
)->linkid
= linkid
;
124 d_add(dentry
, inode
);
131 static int hfsplus_readdir(struct file
*file
, struct dir_context
*ctx
)
133 struct inode
*inode
= file_inode(file
);
134 struct super_block
*sb
= inode
->i_sb
;
137 hfsplus_cat_entry entry
;
138 struct hfs_find_data fd
;
139 struct hfsplus_readdir_data
*rd
;
142 if (file
->f_pos
>= inode
->i_size
)
145 err
= hfs_find_init(HFSPLUS_SB(sb
)->cat_tree
, &fd
);
148 strbuf
= kmalloc(NLS_MAX_CHARSET_SIZE
* HFSPLUS_MAX_STRLEN
+ 1, GFP_KERNEL
);
153 hfsplus_cat_build_key_with_cnid(sb
, fd
.search_key
, inode
->i_ino
);
154 err
= hfs_brec_find(&fd
, hfs_find_rec_by_key
);
159 /* This is completely artificial... */
160 if (!dir_emit_dot(file
, ctx
))
165 if (fd
.entrylength
> sizeof(entry
) || fd
.entrylength
< 0) {
170 hfs_bnode_read(fd
.bnode
, &entry
, fd
.entryoffset
,
172 if (be16_to_cpu(entry
.type
) != HFSPLUS_FOLDER_THREAD
) {
173 pr_err("bad catalog folder thread\n");
177 if (fd
.entrylength
< HFSPLUS_MIN_THREAD_SZ
) {
178 pr_err("truncated catalog thread\n");
182 if (!dir_emit(ctx
, "..", 2,
183 be32_to_cpu(entry
.thread
.parentID
), DT_DIR
))
187 if (ctx
->pos
>= inode
->i_size
)
189 err
= hfs_brec_goto(&fd
, ctx
->pos
- 1);
193 if (be32_to_cpu(fd
.key
->cat
.parent
) != inode
->i_ino
) {
194 pr_err("walked past end of dir\n");
199 if (fd
.entrylength
> sizeof(entry
) || fd
.entrylength
< 0) {
204 hfs_bnode_read(fd
.bnode
, &entry
, fd
.entryoffset
,
206 type
= be16_to_cpu(entry
.type
);
207 len
= NLS_MAX_CHARSET_SIZE
* HFSPLUS_MAX_STRLEN
;
208 err
= hfsplus_uni2asc(sb
, &fd
.key
->cat
.name
, strbuf
, &len
);
211 if (type
== HFSPLUS_FOLDER
) {
213 sizeof(struct hfsplus_cat_folder
)) {
214 pr_err("small dir entry\n");
218 if (HFSPLUS_SB(sb
)->hidden_dir
&&
219 HFSPLUS_SB(sb
)->hidden_dir
->i_ino
==
220 be32_to_cpu(entry
.folder
.id
))
222 if (!dir_emit(ctx
, strbuf
, len
,
223 be32_to_cpu(entry
.folder
.id
), DT_DIR
))
225 } else if (type
== HFSPLUS_FILE
) {
227 unsigned type
= DT_UNKNOWN
;
229 if (fd
.entrylength
< sizeof(struct hfsplus_cat_file
)) {
230 pr_err("small file entry\n");
235 mode
= be16_to_cpu(entry
.file
.permissions
.mode
);
238 else if (S_ISLNK(mode
))
240 else if (S_ISFIFO(mode
))
242 else if (S_ISCHR(mode
))
244 else if (S_ISBLK(mode
))
246 else if (S_ISSOCK(mode
))
249 if (!dir_emit(ctx
, strbuf
, len
,
250 be32_to_cpu(entry
.file
.id
), type
))
253 pr_err("bad catalog entry type\n");
259 if (ctx
->pos
>= inode
->i_size
)
261 err
= hfs_brec_goto(&fd
, 1);
265 rd
= file
->private_data
;
267 rd
= kmalloc(sizeof(struct hfsplus_readdir_data
), GFP_KERNEL
);
272 file
->private_data
= rd
;
274 spin_lock(&HFSPLUS_I(inode
)->open_dir_lock
);
275 list_add(&rd
->list
, &HFSPLUS_I(inode
)->open_dir_list
);
276 spin_unlock(&HFSPLUS_I(inode
)->open_dir_lock
);
279 * Can be done after the list insertion; exclusion with
280 * hfsplus_delete_cat() is provided by directory lock.
282 memcpy(&rd
->key
, fd
.key
, sizeof(struct hfsplus_cat_key
));
289 static int hfsplus_dir_release(struct inode
*inode
, struct file
*file
)
291 struct hfsplus_readdir_data
*rd
= file
->private_data
;
293 spin_lock(&HFSPLUS_I(inode
)->open_dir_lock
);
295 spin_unlock(&HFSPLUS_I(inode
)->open_dir_lock
);
301 static int hfsplus_link(struct dentry
*src_dentry
, struct inode
*dst_dir
,
302 struct dentry
*dst_dentry
)
304 struct hfsplus_sb_info
*sbi
= HFSPLUS_SB(dst_dir
->i_sb
);
305 struct inode
*inode
= d_inode(src_dentry
);
306 struct inode
*src_dir
= d_inode(src_dentry
->d_parent
);
312 if (HFSPLUS_IS_RSRC(inode
))
314 if (!S_ISREG(inode
->i_mode
))
317 mutex_lock(&sbi
->vh_mutex
);
318 if (inode
->i_ino
== (u32
)(unsigned long)src_dentry
->d_fsdata
) {
320 get_random_bytes(&id
, sizeof(cnid
));
323 str
.len
= sprintf(name
, "iNode%d", id
);
324 res
= hfsplus_rename_cat(inode
->i_ino
,
325 src_dir
, &src_dentry
->d_name
,
326 sbi
->hidden_dir
, &str
);
332 HFSPLUS_I(inode
)->linkid
= id
;
333 cnid
= sbi
->next_cnid
++;
334 src_dentry
->d_fsdata
= (void *)(unsigned long)cnid
;
335 res
= hfsplus_create_cat(cnid
, src_dir
,
336 &src_dentry
->d_name
, inode
);
342 cnid
= sbi
->next_cnid
++;
343 res
= hfsplus_create_cat(cnid
, dst_dir
, &dst_dentry
->d_name
, inode
);
348 hfsplus_instantiate(dst_dentry
, inode
, cnid
);
350 inode
->i_ctime
= current_time(inode
);
351 mark_inode_dirty(inode
);
353 hfsplus_mark_mdb_dirty(dst_dir
->i_sb
);
355 mutex_unlock(&sbi
->vh_mutex
);
359 static int hfsplus_unlink(struct inode
*dir
, struct dentry
*dentry
)
361 struct hfsplus_sb_info
*sbi
= HFSPLUS_SB(dir
->i_sb
);
362 struct inode
*inode
= d_inode(dentry
);
368 if (HFSPLUS_IS_RSRC(inode
))
371 mutex_lock(&sbi
->vh_mutex
);
372 cnid
= (u32
)(unsigned long)dentry
->d_fsdata
;
373 if (inode
->i_ino
== cnid
&&
374 atomic_read(&HFSPLUS_I(inode
)->opencnt
)) {
376 str
.len
= sprintf(name
, "temp%lu", inode
->i_ino
);
377 res
= hfsplus_rename_cat(inode
->i_ino
,
378 dir
, &dentry
->d_name
,
379 sbi
->hidden_dir
, &str
);
381 inode
->i_flags
|= S_DEAD
;
386 res
= hfsplus_delete_cat(cnid
, dir
, &dentry
->d_name
);
390 if (inode
->i_nlink
> 0)
392 if (inode
->i_ino
== cnid
)
394 if (!inode
->i_nlink
) {
395 if (inode
->i_ino
!= cnid
) {
397 if (!atomic_read(&HFSPLUS_I(inode
)->opencnt
)) {
398 res
= hfsplus_delete_cat(inode
->i_ino
,
402 hfsplus_delete_inode(inode
);
404 inode
->i_flags
|= S_DEAD
;
406 hfsplus_delete_inode(inode
);
409 inode
->i_ctime
= current_time(inode
);
410 mark_inode_dirty(inode
);
412 mutex_unlock(&sbi
->vh_mutex
);
416 static int hfsplus_rmdir(struct inode
*dir
, struct dentry
*dentry
)
418 struct hfsplus_sb_info
*sbi
= HFSPLUS_SB(dir
->i_sb
);
419 struct inode
*inode
= d_inode(dentry
);
422 if (inode
->i_size
!= 2)
425 mutex_lock(&sbi
->vh_mutex
);
426 res
= hfsplus_delete_cat(inode
->i_ino
, dir
, &dentry
->d_name
);
430 inode
->i_ctime
= current_time(inode
);
431 hfsplus_delete_inode(inode
);
432 mark_inode_dirty(inode
);
434 mutex_unlock(&sbi
->vh_mutex
);
438 static int hfsplus_symlink(struct inode
*dir
, struct dentry
*dentry
,
441 struct hfsplus_sb_info
*sbi
= HFSPLUS_SB(dir
->i_sb
);
445 mutex_lock(&sbi
->vh_mutex
);
446 inode
= hfsplus_new_inode(dir
->i_sb
, S_IFLNK
| S_IRWXUGO
);
450 res
= page_symlink(inode
, symname
, strlen(symname
) + 1);
454 res
= hfsplus_create_cat(inode
->i_ino
, dir
, &dentry
->d_name
, inode
);
458 res
= hfsplus_init_inode_security(inode
, dir
, &dentry
->d_name
);
459 if (res
== -EOPNOTSUPP
)
460 res
= 0; /* Operation is not supported. */
462 /* Try to delete anyway without error analysis. */
463 hfsplus_delete_cat(inode
->i_ino
, dir
, &dentry
->d_name
);
467 hfsplus_instantiate(dentry
, inode
, inode
->i_ino
);
468 mark_inode_dirty(inode
);
473 hfsplus_delete_inode(inode
);
476 mutex_unlock(&sbi
->vh_mutex
);
480 static int hfsplus_mknod(struct inode
*dir
, struct dentry
*dentry
,
481 umode_t mode
, dev_t rdev
)
483 struct hfsplus_sb_info
*sbi
= HFSPLUS_SB(dir
->i_sb
);
487 mutex_lock(&sbi
->vh_mutex
);
488 inode
= hfsplus_new_inode(dir
->i_sb
, mode
);
492 if (S_ISBLK(mode
) || S_ISCHR(mode
) || S_ISFIFO(mode
) || S_ISSOCK(mode
))
493 init_special_inode(inode
, mode
, rdev
);
495 res
= hfsplus_create_cat(inode
->i_ino
, dir
, &dentry
->d_name
, inode
);
499 res
= hfsplus_init_inode_security(inode
, dir
, &dentry
->d_name
);
500 if (res
== -EOPNOTSUPP
)
501 res
= 0; /* Operation is not supported. */
503 /* Try to delete anyway without error analysis. */
504 hfsplus_delete_cat(inode
->i_ino
, dir
, &dentry
->d_name
);
508 hfsplus_instantiate(dentry
, inode
, inode
->i_ino
);
509 mark_inode_dirty(inode
);
514 hfsplus_delete_inode(inode
);
517 mutex_unlock(&sbi
->vh_mutex
);
521 static int hfsplus_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
524 return hfsplus_mknod(dir
, dentry
, mode
, 0);
527 static int hfsplus_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
529 return hfsplus_mknod(dir
, dentry
, mode
| S_IFDIR
, 0);
532 static int hfsplus_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
533 struct inode
*new_dir
, struct dentry
*new_dentry
,
538 if (flags
& ~RENAME_NOREPLACE
)
541 /* Unlink destination if it already exists */
542 if (d_really_is_positive(new_dentry
)) {
543 if (d_is_dir(new_dentry
))
544 res
= hfsplus_rmdir(new_dir
, new_dentry
);
546 res
= hfsplus_unlink(new_dir
, new_dentry
);
551 res
= hfsplus_rename_cat((u32
)(unsigned long)old_dentry
->d_fsdata
,
552 old_dir
, &old_dentry
->d_name
,
553 new_dir
, &new_dentry
->d_name
);
555 new_dentry
->d_fsdata
= old_dentry
->d_fsdata
;
559 const struct inode_operations hfsplus_dir_inode_operations
= {
560 .lookup
= hfsplus_lookup
,
561 .create
= hfsplus_create
,
562 .link
= hfsplus_link
,
563 .unlink
= hfsplus_unlink
,
564 .mkdir
= hfsplus_mkdir
,
565 .rmdir
= hfsplus_rmdir
,
566 .symlink
= hfsplus_symlink
,
567 .mknod
= hfsplus_mknod
,
568 .rename
= hfsplus_rename
,
569 .listxattr
= hfsplus_listxattr
,
570 #ifdef CONFIG_HFSPLUS_FS_POSIX_ACL
571 .get_acl
= hfsplus_get_posix_acl
,
572 .set_acl
= hfsplus_set_posix_acl
,
576 const struct file_operations hfsplus_dir_operations
= {
577 .fsync
= hfsplus_file_fsync
,
578 .read
= generic_read_dir
,
579 .iterate_shared
= hfsplus_readdir
,
580 .unlocked_ioctl
= hfsplus_ioctl
,
581 .llseek
= generic_file_llseek
,
582 .release
= hfsplus_dir_release
,