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>
17 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
18 #include <linux/buffer_head.h>
21 #include "hfsplus_fs.h"
22 #include "hfsplus_raw.h"
24 /* Find the entry inside dir named dentry->d_name */
25 static struct dentry *hfsplus_lookup(struct inode *dir, struct dentry *dentry)
27 struct inode *inode = NULL;
28 struct hfsplus_find_data fd;
29 struct super_block *sb;
30 hfsplus_cat_entry entry;
36 dentry->d_fsdata = NULL;
37 hfsplus_find_init(HFSPLUS_SB(sb).cat_tree, &fd);
38 hfsplus_fill_cat_key(fd.search_key, dir->i_ino, &dentry->d_name);
40 err = hfsplus_btree_find_entry(&fd, &entry, sizeof(entry));
43 hfsplus_find_exit(&fd);
50 type = be16_to_cpu(entry.type);
51 if (type == HFSPLUS_FOLDER) {
52 if (fd.entrylength < sizeof(hfsplus_cat_folder)) {
56 cnid = be32_to_cpu(entry.folder.id);
57 } else if (type == HFSPLUS_FILE) {
58 if (fd.entrylength < sizeof(hfsplus_cat_file)) {
62 cnid = be32_to_cpu(entry.file.id);
63 if (entry.file.user_info.fdType == cpu_to_be32(HFSP_HARDLINK_TYPE) &&
64 entry.file.user_info.fdCreator == cpu_to_be32(HFSP_HFSPLUS_CREATOR)) {
68 if (dentry->d_fsdata) {
73 dentry->d_fsdata = (void *)(unsigned long)cnid;
74 linkid = be32_to_cpu(entry.file.permissions.dev);
75 str.len = sprintf(name, "iNode%d", linkid);
77 hfsplus_fill_cat_key(fd.search_key, HFSPLUS_SB(sb).hidden_dir->i_ino, &str);
79 } else if (!dentry->d_fsdata)
80 dentry->d_fsdata = (void *)(unsigned long)cnid;
82 printk("HFS+-fs: Illegal catalog entry type in lookup\n");
86 hfsplus_find_exit(&fd);
87 inode = iget(dir->i_sb, cnid);
89 return ERR_PTR(-EACCES);
90 if (S_ISREG(inode->i_mode))
91 HFSPLUS_I(inode).dev = linkid;
96 hfsplus_find_exit(&fd);
100 static int hfsplus_readdir(struct file *filp, void *dirent, filldir_t filldir)
102 struct inode *inode = filp->f_dentry->d_inode;
103 struct super_block *sb = inode->i_sb;
105 char strbuf[HFSPLUS_MAX_STRLEN + 1];
106 hfsplus_cat_entry entry;
107 struct hfsplus_find_data fd;
108 struct hfsplus_readdir_data *rd;
111 if (filp->f_pos >= inode->i_size)
114 hfsplus_find_init(HFSPLUS_SB(sb).cat_tree, &fd);
115 hfsplus_fill_cat_key(fd.search_key, inode->i_ino, NULL);
116 err = hfsplus_btree_find(&fd);
120 switch ((u32)filp->f_pos) {
122 /* This is completely artificial... */
123 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR))
128 hfsplus_bnode_readbytes(fd.bnode, &entry, fd.entryoffset, fd.entrylength);
129 if (be16_to_cpu(entry.type) != HFSPLUS_FOLDER_THREAD) {
130 printk("HFS+-fs: bad catalog folder thread\n");
134 if (fd.entrylength < HFSPLUS_MIN_THREAD_SZ) {
135 printk("HFS+-fs: truncated catalog thread\n");
139 if (filldir(dirent, "..", 2, 1,
140 be32_to_cpu(entry.thread.parentID), DT_DIR))
145 if (filp->f_pos >= inode->i_size)
147 err = hfsplus_btree_move(&fd, filp->f_pos - 1);
153 if (be32_to_cpu(fd.key->cat.parent) != inode->i_ino) {
154 printk("HFS+-fs: walked past end of dir\n");
158 hfsplus_bnode_readbytes(fd.bnode, &entry, fd.entryoffset, fd.entrylength);
159 type = be16_to_cpu(entry.type);
160 len = HFSPLUS_MAX_STRLEN;
161 err = hfsplus_uni2asc(&fd.key->cat.name, strbuf, &len);
164 if (type == HFSPLUS_FOLDER) {
165 if (fd.entrylength < sizeof(hfsplus_cat_folder)) {
166 printk("HFS+-fs: small dir entry\n");
170 if (HFSPLUS_SB(sb).hidden_dir &&
171 HFSPLUS_SB(sb).hidden_dir->i_ino == be32_to_cpu(entry.folder.id))
173 if (filldir(dirent, strbuf, len, filp->f_pos,
174 be32_to_cpu(entry.folder.id), DT_DIR))
176 } else if (type == HFSPLUS_FILE) {
177 if (fd.entrylength < sizeof(hfsplus_cat_file)) {
178 printk("HFS+-fs: small file entry\n");
182 if (filldir(dirent, strbuf, len, filp->f_pos,
183 be32_to_cpu(entry.file.id), DT_REG))
186 printk("HFS+-fs: bad catalog entry type\n");
192 if (filp->f_pos >= inode->i_size)
194 err = hfsplus_btree_move(&fd, 1);
198 rd = filp->private_data;
199 if (!filp->private_data) {
200 rd = kmalloc(sizeof(struct hfsplus_readdir_data), GFP_KERNEL);
205 filp->private_data = rd;
207 list_add(&rd->list, &HFSPLUS_I(inode).open_dir_list);
209 memcpy(&rd->key, fd.key, sizeof(hfsplus_cat_key));
211 hfsplus_find_exit(&fd);
215 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
216 static loff_t hfsplus_seek_dir(struct file *file, loff_t offset, int origin)
220 down(&file->f_dentry->d_inode->i_sem);
221 res = default_llseek(file, offset, origin);
222 up(&file->f_dentry->d_inode->i_sem);
228 static int hfsplus_dir_release(struct inode *inode, struct file *file)
230 struct hfsplus_readdir_data *rd = file->private_data;
238 int hfsplus_create(struct inode *dir, struct dentry *dentry, int mode)
243 inode = hfsplus_new_inode(dir->i_sb, mode);
247 res = hfsplus_create_cat(inode->i_ino, dir, &dentry->d_name, inode);
253 dentry->d_fsdata = (void *)inode->i_ino;
254 d_instantiate(dentry, inode);
255 mark_inode_dirty(inode);
259 int hfsplus_link(struct dentry *src_dentry, struct inode *dst_dir, struct dentry *dst_dentry)
261 struct super_block *sb = dst_dir->i_sb;
262 struct inode *inode = src_dentry->d_inode;
263 struct inode *src_dir = src_dentry->d_parent->d_inode;
269 if (HFSPLUS_IS_RSRC(inode))
272 if (inode->i_ino == (u32)(unsigned long)src_dentry->d_fsdata) {
274 get_random_bytes(&id, sizeof(cnid));
277 str.len = sprintf(name, "iNode%d", id);
278 res = hfsplus_rename_cat(inode->i_ino,
279 src_dir, &src_dentry->d_name,
280 HFSPLUS_SB(sb).hidden_dir, &str);
286 HFSPLUS_I(inode).dev = id;
287 cnid = HFSPLUS_SB(sb).next_cnid++;
288 src_dentry->d_fsdata = (void *)(unsigned long)cnid;
289 res = hfsplus_create_cat(cnid, src_dir, &src_dentry->d_name, inode);
293 HFSPLUS_SB(sb).file_count++;
295 cnid = HFSPLUS_SB(sb).next_cnid++;
296 res = hfsplus_create_cat(cnid, dst_dir, &dst_dentry->d_name, inode);
301 dst_dentry->d_fsdata = (void *)(unsigned long)cnid;
302 d_instantiate(dst_dentry, inode);
303 atomic_inc(&inode->i_count);
304 inode->i_ctime = CURRENT_TIME;
305 mark_inode_dirty(inode);
306 HFSPLUS_SB(sb).file_count++;
312 int hfsplus_unlink(struct inode *dir, struct dentry *dentry)
314 struct super_block *sb = dir->i_sb;
315 struct inode *inode = dentry->d_inode;
321 if (HFSPLUS_IS_RSRC(inode))
324 cnid = (u32)(unsigned long)dentry->d_fsdata;
325 if (inode->i_ino == cnid &&
326 atomic_read(&HFSPLUS_I(inode).opencnt)) {
328 str.len = sprintf(name, "temp%lu", inode->i_ino);
329 res = hfsplus_rename_cat(inode->i_ino,
330 dir, &dentry->d_name,
331 HFSPLUS_SB(sb).hidden_dir, &str);
333 inode->i_flags |= S_DEAD;
336 res = hfsplus_delete_cat(cnid, dir, &dentry->d_name);
341 hfsplus_delete_inode(inode);
342 if (inode->i_ino != cnid && !inode->i_nlink) {
343 if (!atomic_read(&HFSPLUS_I(inode).opencnt)) {
344 res = hfsplus_delete_cat(inode->i_ino, HFSPLUS_SB(sb).hidden_dir, NULL);
346 hfsplus_delete_inode(inode);
348 inode->i_flags |= S_DEAD;
350 inode->i_ctime = CURRENT_TIME;
351 mark_inode_dirty(inode);
356 int hfsplus_mkdir(struct inode *dir, struct dentry *dentry, int mode)
361 inode = hfsplus_new_inode(dir->i_sb, S_IFDIR | mode);
365 res = hfsplus_create_cat(inode->i_ino, dir, &dentry->d_name, inode);
371 d_instantiate(dentry, inode);
372 mark_inode_dirty(inode);
376 int hfsplus_rmdir(struct inode *dir, struct dentry *dentry)
381 inode = dentry->d_inode;
382 if (inode->i_size != 2)
384 res = hfsplus_delete_cat(inode->i_ino, dir, &dentry->d_name);
388 inode->i_ctime = CURRENT_TIME;
389 hfsplus_delete_inode(inode);
390 mark_inode_dirty(inode);
394 int hfsplus_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
396 struct super_block *sb;
401 inode = hfsplus_new_inode(sb, S_IFLNK | S_IRWXUGO);
405 res = page_symlink(inode, symname, strlen(symname) + 1);
412 mark_inode_dirty(inode);
413 res = hfsplus_create_cat(inode->i_ino, dir, &dentry->d_name, inode);
416 d_instantiate(dentry, inode);
417 mark_inode_dirty(inode);
423 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
424 int hfsplus_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
426 int hfsplus_mknod(struct inode *dir, struct dentry *dentry, int mode, int rdev)
429 struct super_block *sb;
434 inode = hfsplus_new_inode(sb, mode);
438 res = hfsplus_create_cat(inode->i_ino, dir, &dentry->d_name, inode);
444 init_special_inode(inode, mode, rdev);
445 d_instantiate(dentry, inode);
446 mark_inode_dirty(inode);
451 int hfsplus_rename(struct inode *old_dir, struct dentry *old_dentry,
452 struct inode *new_dir, struct dentry *new_dentry)
456 /* Unlink destination if it already exists */
457 if (new_dentry->d_inode) {
458 res = hfsplus_unlink(new_dir, new_dentry);
463 res = hfsplus_rename_cat((u32)(unsigned long)old_dentry->d_fsdata,
464 old_dir, &old_dentry->d_name,
465 new_dir, &new_dentry->d_name);
467 new_dentry->d_fsdata = old_dentry->d_fsdata;
471 struct inode_operations hfsplus_dir_inode_operations = {
472 .lookup = hfsplus_lookup,
473 .create = hfsplus_create,
474 .link = hfsplus_link,
475 .unlink = hfsplus_unlink,
476 .mkdir = hfsplus_mkdir,
477 .rmdir = hfsplus_rmdir,
478 .symlink = hfsplus_symlink,
479 .mknod = hfsplus_mknod,
480 .rename = hfsplus_rename,
483 struct file_operations hfsplus_dir_operations = {
484 .read = generic_read_dir,
485 .readdir = hfsplus_readdir,
486 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
487 .llseek = hfsplus_seek_dir,
489 .llseek = generic_file_llseek,
491 .release = hfsplus_dir_release,