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 hfsplus_cat_build_key(sb, fd.search_key, dir->i_ino, &dentry->d_name);
49 err = hfs_brec_read(&fd, &entry, sizeof(entry));
59 type = be16_to_cpu(entry.type);
60 if (type == HFSPLUS_FOLDER) {
61 if (fd.entrylength < sizeof(struct hfsplus_cat_folder)) {
65 cnid = be32_to_cpu(entry.folder.id);
66 dentry->d_fsdata = (void *)(unsigned long)cnid;
67 } else if (type == HFSPLUS_FILE) {
68 if (fd.entrylength < sizeof(struct hfsplus_cat_file)) {
72 cnid = be32_to_cpu(entry.file.id);
73 if (entry.file.user_info.fdType ==
74 cpu_to_be32(HFSP_HARDLINK_TYPE) &&
75 entry.file.user_info.fdCreator ==
76 cpu_to_be32(HFSP_HFSPLUS_CREATOR) &&
77 (entry.file.create_date ==
78 HFSPLUS_I(HFSPLUS_SB(sb)->hidden_dir)->
80 entry.file.create_date ==
81 HFSPLUS_I(sb->s_root->d_inode)->
83 HFSPLUS_SB(sb)->hidden_dir) {
87 if (dentry->d_fsdata) {
89 * We found a link pointing to another link,
90 * so ignore it and treat it as regular file.
92 cnid = (unsigned long)dentry->d_fsdata;
95 dentry->d_fsdata = (void *)(unsigned long)cnid;
97 be32_to_cpu(entry.file.permissions.dev);
98 str.len = sprintf(name, "iNode%d", linkid);
100 hfsplus_cat_build_key(sb, fd.search_key,
101 HFSPLUS_SB(sb)->hidden_dir->i_ino,
105 } else if (!dentry->d_fsdata)
106 dentry->d_fsdata = (void *)(unsigned long)cnid;
108 pr_err("invalid catalog entry type in lookup\n");
113 inode = hfsplus_iget(dir->i_sb, cnid);
115 return ERR_CAST(inode);
116 if (S_ISREG(inode->i_mode))
117 HFSPLUS_I(inode)->linkid = linkid;
119 d_add(dentry, inode);
126 static int hfsplus_readdir(struct file *file, struct dir_context *ctx)
128 struct inode *inode = file_inode(file);
129 struct super_block *sb = inode->i_sb;
132 hfsplus_cat_entry entry;
133 struct hfs_find_data fd;
134 struct hfsplus_readdir_data *rd;
137 if (file->f_pos >= inode->i_size)
140 err = hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd);
143 strbuf = kmalloc(NLS_MAX_CHARSET_SIZE * HFSPLUS_MAX_STRLEN + 1, GFP_KERNEL);
148 hfsplus_cat_build_key(sb, fd.search_key, inode->i_ino, NULL);
149 err = hfs_brec_find(&fd, hfs_find_rec_by_key);
154 /* This is completely artificial... */
155 if (!dir_emit_dot(file, ctx))
160 if (fd.entrylength > sizeof(entry) || fd.entrylength < 0) {
165 hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
167 if (be16_to_cpu(entry.type) != HFSPLUS_FOLDER_THREAD) {
168 pr_err("bad catalog folder thread\n");
172 if (fd.entrylength < HFSPLUS_MIN_THREAD_SZ) {
173 pr_err("truncated catalog thread\n");
177 if (!dir_emit(ctx, "..", 2,
178 be32_to_cpu(entry.thread.parentID), DT_DIR))
182 if (ctx->pos >= inode->i_size)
184 err = hfs_brec_goto(&fd, ctx->pos - 1);
188 if (be32_to_cpu(fd.key->cat.parent) != inode->i_ino) {
189 pr_err("walked past end of dir\n");
194 if (fd.entrylength > sizeof(entry) || fd.entrylength < 0) {
199 hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
201 type = be16_to_cpu(entry.type);
202 len = NLS_MAX_CHARSET_SIZE * HFSPLUS_MAX_STRLEN;
203 err = hfsplus_uni2asc(sb, &fd.key->cat.name, strbuf, &len);
206 if (type == HFSPLUS_FOLDER) {
208 sizeof(struct hfsplus_cat_folder)) {
209 pr_err("small dir entry\n");
213 if (HFSPLUS_SB(sb)->hidden_dir &&
214 HFSPLUS_SB(sb)->hidden_dir->i_ino ==
215 be32_to_cpu(entry.folder.id))
217 if (!dir_emit(ctx, strbuf, len,
218 be32_to_cpu(entry.folder.id), DT_DIR))
220 } else if (type == HFSPLUS_FILE) {
222 unsigned type = DT_UNKNOWN;
224 if (fd.entrylength < sizeof(struct hfsplus_cat_file)) {
225 pr_err("small file entry\n");
230 mode = be16_to_cpu(entry.file.permissions.mode);
233 else if (S_ISLNK(mode))
235 else if (S_ISFIFO(mode))
237 else if (S_ISCHR(mode))
239 else if (S_ISBLK(mode))
241 else if (S_ISSOCK(mode))
244 if (!dir_emit(ctx, strbuf, len,
245 be32_to_cpu(entry.file.id), type))
248 pr_err("bad catalog entry type\n");
254 if (ctx->pos >= inode->i_size)
256 err = hfs_brec_goto(&fd, 1);
260 rd = file->private_data;
262 rd = kmalloc(sizeof(struct hfsplus_readdir_data), GFP_KERNEL);
267 file->private_data = rd;
269 list_add(&rd->list, &HFSPLUS_I(inode)->open_dir_list);
271 memcpy(&rd->key, fd.key, sizeof(struct hfsplus_cat_key));
278 static int hfsplus_dir_release(struct inode *inode, struct file *file)
280 struct hfsplus_readdir_data *rd = file->private_data;
282 mutex_lock(&inode->i_mutex);
284 mutex_unlock(&inode->i_mutex);
290 static int hfsplus_link(struct dentry *src_dentry, struct inode *dst_dir,
291 struct dentry *dst_dentry)
293 struct hfsplus_sb_info *sbi = HFSPLUS_SB(dst_dir->i_sb);
294 struct inode *inode = src_dentry->d_inode;
295 struct inode *src_dir = src_dentry->d_parent->d_inode;
301 if (HFSPLUS_IS_RSRC(inode))
303 if (!S_ISREG(inode->i_mode))
306 mutex_lock(&sbi->vh_mutex);
307 if (inode->i_ino == (u32)(unsigned long)src_dentry->d_fsdata) {
309 get_random_bytes(&id, sizeof(cnid));
312 str.len = sprintf(name, "iNode%d", id);
313 res = hfsplus_rename_cat(inode->i_ino,
314 src_dir, &src_dentry->d_name,
315 sbi->hidden_dir, &str);
321 HFSPLUS_I(inode)->linkid = id;
322 cnid = sbi->next_cnid++;
323 src_dentry->d_fsdata = (void *)(unsigned long)cnid;
324 res = hfsplus_create_cat(cnid, src_dir,
325 &src_dentry->d_name, inode);
331 cnid = sbi->next_cnid++;
332 res = hfsplus_create_cat(cnid, dst_dir, &dst_dentry->d_name, inode);
337 hfsplus_instantiate(dst_dentry, inode, cnid);
339 inode->i_ctime = CURRENT_TIME_SEC;
340 mark_inode_dirty(inode);
342 hfsplus_mark_mdb_dirty(dst_dir->i_sb);
344 mutex_unlock(&sbi->vh_mutex);
348 static int hfsplus_unlink(struct inode *dir, struct dentry *dentry)
350 struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
351 struct inode *inode = dentry->d_inode;
357 if (HFSPLUS_IS_RSRC(inode))
360 mutex_lock(&sbi->vh_mutex);
361 cnid = (u32)(unsigned long)dentry->d_fsdata;
362 if (inode->i_ino == cnid &&
363 atomic_read(&HFSPLUS_I(inode)->opencnt)) {
365 str.len = sprintf(name, "temp%lu", inode->i_ino);
366 res = hfsplus_rename_cat(inode->i_ino,
367 dir, &dentry->d_name,
368 sbi->hidden_dir, &str);
370 inode->i_flags |= S_DEAD;
375 res = hfsplus_delete_cat(cnid, dir, &dentry->d_name);
379 if (inode->i_nlink > 0)
381 if (inode->i_ino == cnid)
383 if (!inode->i_nlink) {
384 if (inode->i_ino != cnid) {
386 if (!atomic_read(&HFSPLUS_I(inode)->opencnt)) {
387 res = hfsplus_delete_cat(inode->i_ino,
391 hfsplus_delete_inode(inode);
393 inode->i_flags |= S_DEAD;
395 hfsplus_delete_inode(inode);
398 inode->i_ctime = CURRENT_TIME_SEC;
399 mark_inode_dirty(inode);
401 mutex_unlock(&sbi->vh_mutex);
405 static int hfsplus_rmdir(struct inode *dir, struct dentry *dentry)
407 struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
408 struct inode *inode = dentry->d_inode;
411 if (inode->i_size != 2)
414 mutex_lock(&sbi->vh_mutex);
415 res = hfsplus_delete_cat(inode->i_ino, dir, &dentry->d_name);
419 inode->i_ctime = CURRENT_TIME_SEC;
420 hfsplus_delete_inode(inode);
421 mark_inode_dirty(inode);
423 mutex_unlock(&sbi->vh_mutex);
427 static int hfsplus_symlink(struct inode *dir, struct dentry *dentry,
430 struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
434 mutex_lock(&sbi->vh_mutex);
435 inode = hfsplus_new_inode(dir->i_sb, S_IFLNK | S_IRWXUGO);
439 res = page_symlink(inode, symname, strlen(symname) + 1);
443 res = hfsplus_create_cat(inode->i_ino, dir, &dentry->d_name, inode);
447 res = hfsplus_init_inode_security(inode, dir, &dentry->d_name);
448 if (res == -EOPNOTSUPP)
449 res = 0; /* Operation is not supported. */
451 /* Try to delete anyway without error analysis. */
452 hfsplus_delete_cat(inode->i_ino, dir, &dentry->d_name);
456 hfsplus_instantiate(dentry, inode, inode->i_ino);
457 mark_inode_dirty(inode);
462 hfsplus_delete_inode(inode);
465 mutex_unlock(&sbi->vh_mutex);
469 static int hfsplus_mknod(struct inode *dir, struct dentry *dentry,
470 umode_t mode, dev_t rdev)
472 struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
476 mutex_lock(&sbi->vh_mutex);
477 inode = hfsplus_new_inode(dir->i_sb, mode);
481 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISFIFO(mode) || S_ISSOCK(mode))
482 init_special_inode(inode, mode, rdev);
484 res = hfsplus_create_cat(inode->i_ino, dir, &dentry->d_name, inode);
488 res = hfsplus_init_inode_security(inode, dir, &dentry->d_name);
489 if (res == -EOPNOTSUPP)
490 res = 0; /* Operation is not supported. */
492 /* Try to delete anyway without error analysis. */
493 hfsplus_delete_cat(inode->i_ino, dir, &dentry->d_name);
497 hfsplus_instantiate(dentry, inode, inode->i_ino);
498 mark_inode_dirty(inode);
503 hfsplus_delete_inode(inode);
506 mutex_unlock(&sbi->vh_mutex);
510 static int hfsplus_create(struct inode *dir, struct dentry *dentry, umode_t mode,
513 return hfsplus_mknod(dir, dentry, mode, 0);
516 static int hfsplus_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
518 return hfsplus_mknod(dir, dentry, mode | S_IFDIR, 0);
521 static int hfsplus_rename(struct inode *old_dir, struct dentry *old_dentry,
522 struct inode *new_dir, struct dentry *new_dentry)
526 /* Unlink destination if it already exists */
527 if (new_dentry->d_inode) {
528 if (S_ISDIR(new_dentry->d_inode->i_mode))
529 res = hfsplus_rmdir(new_dir, new_dentry);
531 res = hfsplus_unlink(new_dir, new_dentry);
536 res = hfsplus_rename_cat((u32)(unsigned long)old_dentry->d_fsdata,
537 old_dir, &old_dentry->d_name,
538 new_dir, &new_dentry->d_name);
540 new_dentry->d_fsdata = old_dentry->d_fsdata;
544 const struct inode_operations hfsplus_dir_inode_operations = {
545 .lookup = hfsplus_lookup,
546 .create = hfsplus_create,
547 .link = hfsplus_link,
548 .unlink = hfsplus_unlink,
549 .mkdir = hfsplus_mkdir,
550 .rmdir = hfsplus_rmdir,
551 .symlink = hfsplus_symlink,
552 .mknod = hfsplus_mknod,
553 .rename = hfsplus_rename,
554 .setxattr = generic_setxattr,
555 .getxattr = generic_getxattr,
556 .listxattr = hfsplus_listxattr,
557 .removexattr = generic_removexattr,
558 #ifdef CONFIG_HFSPLUS_FS_POSIX_ACL
559 .get_acl = hfsplus_get_posix_acl,
560 .set_acl = hfsplus_set_posix_acl,
564 const struct file_operations hfsplus_dir_operations = {
565 .fsync = hfsplus_file_fsync,
566 .read = generic_read_dir,
567 .iterate = hfsplus_readdir,
568 .unlocked_ioctl = hfsplus_ioctl,
569 .llseek = generic_file_llseek,
570 .release = hfsplus_dir_release,