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/sched.h>
14 #include <linux/slab.h>
15 #include <linux/random.h>
16 #include <linux/version.h>
18 #include "hfsplus_fs.h"
19 #include "hfsplus_raw.h"
21 /* Find the entry inside dir named dentry->d_name */
22 static struct dentry *hfsplus_lookup(struct inode *dir, struct dentry *dentry,
25 struct inode *inode = NULL;
26 struct hfs_find_data fd;
27 struct super_block *sb;
28 hfsplus_cat_entry entry;
34 dentry->d_fsdata = NULL;
35 hfs_find_init(HFSPLUS_SB(sb).cat_tree, &fd);
36 hfsplus_cat_build_key(fd.search_key, dir->i_ino, &dentry->d_name);
38 err = hfs_brec_read(&fd, &entry, sizeof(entry));
48 type = be16_to_cpu(entry.type);
49 if (type == HFSPLUS_FOLDER) {
50 if (fd.entrylength < sizeof(struct hfsplus_cat_folder)) {
54 cnid = be32_to_cpu(entry.folder.id);
55 } else if (type == HFSPLUS_FILE) {
56 if (fd.entrylength < sizeof(struct hfsplus_cat_file)) {
60 cnid = be32_to_cpu(entry.file.id);
61 if (entry.file.user_info.fdType == cpu_to_be32(HFSP_HARDLINK_TYPE) &&
62 entry.file.user_info.fdCreator == cpu_to_be32(HFSP_HFSPLUS_CREATOR)) {
66 if (dentry->d_fsdata) {
71 dentry->d_fsdata = (void *)(unsigned long)cnid;
72 linkid = be32_to_cpu(entry.file.permissions.dev);
73 str.len = sprintf(name, "iNode%d", linkid);
75 hfsplus_cat_build_key(fd.search_key, HFSPLUS_SB(sb).hidden_dir->i_ino, &str);
77 } else if (!dentry->d_fsdata)
78 dentry->d_fsdata = (void *)(unsigned long)cnid;
80 printk("HFS+-fs: Illegal catalog entry type in lookup\n");
85 inode = iget(dir->i_sb, cnid);
87 return ERR_PTR(-EACCES);
88 if (S_ISREG(inode->i_mode))
89 HFSPLUS_I(inode).dev = linkid;
98 static int hfsplus_readdir(struct file *filp, void *dirent, filldir_t filldir)
100 struct inode *inode = filp->f_dentry->d_inode;
101 struct super_block *sb = inode->i_sb;
103 char strbuf[HFSPLUS_MAX_STRLEN + 1];
104 hfsplus_cat_entry entry;
105 struct hfs_find_data fd;
106 struct hfsplus_readdir_data *rd;
109 if (filp->f_pos >= inode->i_size)
112 hfs_find_init(HFSPLUS_SB(sb).cat_tree, &fd);
113 hfsplus_cat_build_key(fd.search_key, inode->i_ino, NULL);
114 err = hfs_brec_find(&fd);
118 switch ((u32)filp->f_pos) {
120 /* This is completely artificial... */
121 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR))
126 hfs_bnode_read(fd.bnode, &entry, fd.entryoffset, fd.entrylength);
127 if (be16_to_cpu(entry.type) != HFSPLUS_FOLDER_THREAD) {
128 printk("HFS+-fs: bad catalog folder thread\n");
132 if (fd.entrylength < HFSPLUS_MIN_THREAD_SZ) {
133 printk("HFS+-fs: truncated catalog thread\n");
137 if (filldir(dirent, "..", 2, 1,
138 be32_to_cpu(entry.thread.parentID), DT_DIR))
143 if (filp->f_pos >= inode->i_size)
145 err = hfs_brec_goto(&fd, filp->f_pos - 1);
151 if (be32_to_cpu(fd.key->cat.parent) != inode->i_ino) {
152 printk("HFS+-fs: walked past end of dir\n");
156 hfs_bnode_read(fd.bnode, &entry, fd.entryoffset, fd.entrylength);
157 type = be16_to_cpu(entry.type);
158 len = HFSPLUS_MAX_STRLEN;
159 err = hfsplus_uni2asc(&fd.key->cat.name, strbuf, &len);
162 if (type == HFSPLUS_FOLDER) {
163 if (fd.entrylength < sizeof(struct hfsplus_cat_folder)) {
164 printk("HFS+-fs: small dir entry\n");
168 if (HFSPLUS_SB(sb).hidden_dir &&
169 HFSPLUS_SB(sb).hidden_dir->i_ino == be32_to_cpu(entry.folder.id))
171 if (filldir(dirent, strbuf, len, filp->f_pos,
172 be32_to_cpu(entry.folder.id), DT_DIR))
174 } else if (type == HFSPLUS_FILE) {
175 if (fd.entrylength < sizeof(struct hfsplus_cat_file)) {
176 printk("HFS+-fs: small file entry\n");
180 if (filldir(dirent, strbuf, len, filp->f_pos,
181 be32_to_cpu(entry.file.id), DT_REG))
184 printk("HFS+-fs: bad catalog entry type\n");
190 if (filp->f_pos >= inode->i_size)
192 err = hfs_brec_goto(&fd, 1);
196 rd = filp->private_data;
198 rd = kmalloc(sizeof(struct hfsplus_readdir_data), GFP_KERNEL);
203 filp->private_data = rd;
205 list_add(&rd->list, &HFSPLUS_I(inode).open_dir_list);
207 memcpy(&rd->key, fd.key, sizeof(struct hfsplus_cat_key));
213 static int hfsplus_dir_release(struct inode *inode, struct file *file)
215 struct hfsplus_readdir_data *rd = file->private_data;
223 int hfsplus_create(struct inode *dir, struct dentry *dentry, int mode,
224 struct nameidata *nd)
229 inode = hfsplus_new_inode(dir->i_sb, mode);
233 res = hfsplus_create_cat(inode->i_ino, dir, &dentry->d_name, inode);
239 dentry->d_fsdata = (void *)inode->i_ino;
240 d_instantiate(dentry, inode);
241 mark_inode_dirty(inode);
245 int hfsplus_link(struct dentry *src_dentry, struct inode *dst_dir, struct dentry *dst_dentry)
247 struct super_block *sb = dst_dir->i_sb;
248 struct inode *inode = src_dentry->d_inode;
249 struct inode *src_dir = src_dentry->d_parent->d_inode;
255 if (HFSPLUS_IS_RSRC(inode))
258 if (inode->i_ino == (u32)(unsigned long)src_dentry->d_fsdata) {
260 get_random_bytes(&id, sizeof(cnid));
263 str.len = sprintf(name, "iNode%d", id);
264 res = hfsplus_rename_cat(inode->i_ino,
265 src_dir, &src_dentry->d_name,
266 HFSPLUS_SB(sb).hidden_dir, &str);
272 HFSPLUS_I(inode).dev = id;
273 cnid = HFSPLUS_SB(sb).next_cnid++;
274 src_dentry->d_fsdata = (void *)(unsigned long)cnid;
275 res = hfsplus_create_cat(cnid, src_dir, &src_dentry->d_name, inode);
279 HFSPLUS_SB(sb).file_count++;
281 cnid = HFSPLUS_SB(sb).next_cnid++;
282 res = hfsplus_create_cat(cnid, dst_dir, &dst_dentry->d_name, inode);
287 dst_dentry->d_fsdata = (void *)(unsigned long)cnid;
288 d_instantiate(dst_dentry, inode);
289 atomic_inc(&inode->i_count);
290 inode->i_ctime = CURRENT_TIME;
291 mark_inode_dirty(inode);
292 HFSPLUS_SB(sb).file_count++;
298 int hfsplus_unlink(struct inode *dir, struct dentry *dentry)
300 struct super_block *sb = dir->i_sb;
301 struct inode *inode = dentry->d_inode;
307 if (HFSPLUS_IS_RSRC(inode))
310 cnid = (u32)(unsigned long)dentry->d_fsdata;
311 if (inode->i_ino == cnid &&
312 atomic_read(&HFSPLUS_I(inode).opencnt)) {
314 str.len = sprintf(name, "temp%lu", inode->i_ino);
315 res = hfsplus_rename_cat(inode->i_ino,
316 dir, &dentry->d_name,
317 HFSPLUS_SB(sb).hidden_dir, &str);
319 inode->i_flags |= S_DEAD;
322 res = hfsplus_delete_cat(cnid, dir, &dentry->d_name);
327 hfsplus_delete_inode(inode);
328 if (inode->i_ino != cnid && !inode->i_nlink) {
329 if (!atomic_read(&HFSPLUS_I(inode).opencnt)) {
330 res = hfsplus_delete_cat(inode->i_ino, HFSPLUS_SB(sb).hidden_dir, NULL);
332 hfsplus_delete_inode(inode);
334 inode->i_flags |= S_DEAD;
336 inode->i_ctime = CURRENT_TIME;
337 mark_inode_dirty(inode);
342 int hfsplus_mkdir(struct inode *dir, struct dentry *dentry, int mode)
347 inode = hfsplus_new_inode(dir->i_sb, S_IFDIR | mode);
351 res = hfsplus_create_cat(inode->i_ino, dir, &dentry->d_name, inode);
357 d_instantiate(dentry, inode);
358 mark_inode_dirty(inode);
362 int hfsplus_rmdir(struct inode *dir, struct dentry *dentry)
367 inode = dentry->d_inode;
368 if (inode->i_size != 2)
370 res = hfsplus_delete_cat(inode->i_ino, dir, &dentry->d_name);
374 inode->i_ctime = CURRENT_TIME;
375 hfsplus_delete_inode(inode);
376 mark_inode_dirty(inode);
380 int hfsplus_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
382 struct super_block *sb;
387 inode = hfsplus_new_inode(sb, S_IFLNK | S_IRWXUGO);
391 res = page_symlink(inode, symname, strlen(symname) + 1);
398 mark_inode_dirty(inode);
399 res = hfsplus_create_cat(inode->i_ino, dir, &dentry->d_name, inode);
402 dentry->d_fsdata = (void *)inode->i_ino;
403 d_instantiate(dentry, inode);
404 mark_inode_dirty(inode);
410 int hfsplus_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
412 struct super_block *sb;
417 inode = hfsplus_new_inode(sb, mode);
421 res = hfsplus_create_cat(inode->i_ino, dir, &dentry->d_name, inode);
427 init_special_inode(inode, mode, rdev);
428 dentry->d_fsdata = (void *)inode->i_ino;
429 d_instantiate(dentry, inode);
430 mark_inode_dirty(inode);
435 int hfsplus_rename(struct inode *old_dir, struct dentry *old_dentry,
436 struct inode *new_dir, struct dentry *new_dentry)
440 /* Unlink destination if it already exists */
441 if (new_dentry->d_inode) {
442 res = hfsplus_unlink(new_dir, new_dentry);
447 res = hfsplus_rename_cat((u32)(unsigned long)old_dentry->d_fsdata,
448 old_dir, &old_dentry->d_name,
449 new_dir, &new_dentry->d_name);
451 new_dentry->d_fsdata = old_dentry->d_fsdata;
455 struct inode_operations hfsplus_dir_inode_operations = {
456 .lookup = hfsplus_lookup,
457 .create = hfsplus_create,
458 .link = hfsplus_link,
459 .unlink = hfsplus_unlink,
460 .mkdir = hfsplus_mkdir,
461 .rmdir = hfsplus_rmdir,
462 .symlink = hfsplus_symlink,
463 .mknod = hfsplus_mknod,
464 .rename = hfsplus_rename,
467 struct file_operations hfsplus_dir_operations = {
468 .read = generic_read_dir,
469 .readdir = hfsplus_readdir,
470 .ioctl = hfsplus_ioctl,
471 .llseek = generic_file_llseek,
472 .release = hfsplus_dir_release,