4 * Copyright (C) 1995-1997 Paul H. Hargrove
5 * (C) 2003 Ardis Technologies <roman@ardistech.com>
6 * This file may be distributed under the terms of the GNU General Public License.
8 * This file contains directory-related functions independent of which
9 * scheme is being used to represent forks.
11 * Based on the minix file system code, (C) 1991, 1992 by Linus Torvalds
20 static struct dentry
*hfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
24 struct hfs_find_data fd
;
25 struct inode
*inode
= NULL
;
28 dentry
->d_op
= &hfs_dentry_operations
;
30 hfs_find_init(HFS_SB(dir
->i_sb
)->cat_tree
, &fd
);
31 hfs_cat_build_key(fd
.search_key
, dir
->i_ino
, &dentry
->d_name
);
32 res
= hfs_brec_read(&fd
, &rec
, sizeof(rec
));
42 inode
= hfs_iget(dir
->i_sb
, &fd
.search_key
->cat
, &rec
);
45 return ERR_PTR(-EACCES
);
54 static int hfs_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
56 struct inode
*inode
= filp
->f_dentry
->d_inode
;
57 struct super_block
*sb
= inode
->i_sb
;
59 char strbuf
[HFS_NAMELEN
+ 1];
60 union hfs_cat_rec entry
;
61 struct hfs_find_data fd
;
62 struct hfs_readdir_data
*rd
;
65 if (filp
->f_pos
>= inode
->i_size
)
68 hfs_find_init(HFS_SB(sb
)->cat_tree
, &fd
);
69 hfs_cat_build_key(fd
.search_key
, inode
->i_ino
, NULL
);
70 err
= hfs_brec_find(&fd
);
74 switch ((u32
)filp
->f_pos
) {
76 /* This is completely artificial... */
77 if (filldir(dirent
, ".", 1, 0, inode
->i_ino
, DT_DIR
))
82 hfs_bnode_read(fd
.bnode
, &entry
, fd
.entryoffset
, fd
.entrylength
);
83 if (entry
.type
!= HFS_CDR_THD
) {
84 printk("HFS: bad catalog folder thread\n");
88 //if (fd.entrylength < HFS_MIN_THREAD_SZ) {
89 // printk("HFS: truncated catalog thread\n");
93 if (filldir(dirent
, "..", 2, 1,
94 be32_to_cpu(entry
.thread
.ParID
), DT_DIR
))
99 if (filp
->f_pos
>= inode
->i_size
)
101 err
= hfs_brec_goto(&fd
, filp
->f_pos
- 1);
107 if (be32_to_cpu(fd
.key
->cat
.ParID
) != inode
->i_ino
) {
108 printk("HFS: walked past end of dir\n");
112 hfs_bnode_read(fd
.bnode
, &entry
, fd
.entryoffset
, fd
.entrylength
);
114 len
= hfs_mac2triv(strbuf
, &fd
.key
->cat
.CName
);
115 if (type
== HFS_CDR_DIR
) {
116 if (fd
.entrylength
< sizeof(struct hfs_cat_dir
)) {
117 printk("HFS: small dir entry\n");
121 if (filldir(dirent
, strbuf
, len
, filp
->f_pos
,
122 be32_to_cpu(entry
.dir
.DirID
), DT_DIR
))
124 } else if (type
== HFS_CDR_FIL
) {
125 if (fd
.entrylength
< sizeof(struct hfs_cat_file
)) {
126 printk("HFS: small file entry\n");
130 if (filldir(dirent
, strbuf
, len
, filp
->f_pos
,
131 be32_to_cpu(entry
.file
.FlNum
), DT_REG
))
134 printk("HFS: bad catalog entry type %d\n", type
);
139 if (filp
->f_pos
>= inode
->i_size
)
141 err
= hfs_brec_goto(&fd
, 1);
145 rd
= filp
->private_data
;
147 rd
= kmalloc(sizeof(struct hfs_readdir_data
), GFP_KERNEL
);
152 filp
->private_data
= rd
;
154 list_add(&rd
->list
, &HFS_I(inode
)->open_dir_list
);
156 memcpy(&rd
->key
, &fd
.key
, sizeof(struct hfs_cat_key
));
162 static int hfs_dir_release(struct inode
*inode
, struct file
*file
)
164 struct hfs_readdir_data
*rd
= file
->private_data
;
175 * This is the create() entry in the inode_operations structure for
176 * regular HFS directories. The purpose is to create a new file in
177 * a directory and return a corresponding inode, given the inode for
178 * the directory and the name (and its length) of the new file.
180 static int hfs_create(struct inode
*dir
, struct dentry
*dentry
, int mode
,
181 struct nameidata
*nd
)
186 inode
= hfs_new_inode(dir
, &dentry
->d_name
, mode
);
190 res
= hfs_cat_create(inode
->i_ino
, dir
, &dentry
->d_name
, inode
);
193 hfs_delete_inode(inode
);
197 d_instantiate(dentry
, inode
);
198 mark_inode_dirty(inode
);
205 * This is the mkdir() entry in the inode_operations structure for
206 * regular HFS directories. The purpose is to create a new directory
207 * in a directory, given the inode for the parent directory and the
208 * name (and its length) of the new directory.
210 static int hfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
215 inode
= hfs_new_inode(dir
, &dentry
->d_name
, S_IFDIR
| mode
);
219 res
= hfs_cat_create(inode
->i_ino
, dir
, &dentry
->d_name
, inode
);
222 hfs_delete_inode(inode
);
226 d_instantiate(dentry
, inode
);
227 mark_inode_dirty(inode
);
234 * This is the unlink() entry in the inode_operations structure for
235 * regular HFS directories. The purpose is to delete an existing
236 * file, given the inode for the parent directory and the name
237 * (and its length) of the existing file.
239 static int hfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
244 inode
= dentry
->d_inode
;
245 res
= hfs_cat_delete(inode
->i_ino
, dir
, &dentry
->d_name
);
250 hfs_delete_inode(inode
);
251 inode
->i_ctime
= CURRENT_TIME_SEC
;
252 mark_inode_dirty(inode
);
260 * This is the rmdir() entry in the inode_operations structure for
261 * regular HFS directories. The purpose is to delete an existing
262 * directory, given the inode for the parent directory and the name
263 * (and its length) of the existing directory.
265 static int hfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
270 inode
= dentry
->d_inode
;
271 if (inode
->i_size
!= 2)
273 res
= hfs_cat_delete(inode
->i_ino
, dir
, &dentry
->d_name
);
277 inode
->i_ctime
= CURRENT_TIME_SEC
;
278 hfs_delete_inode(inode
);
279 mark_inode_dirty(inode
);
286 * This is the rename() entry in the inode_operations structure for
287 * regular HFS directories. The purpose is to rename an existing
288 * file or directory, given the inode for the current directory and
289 * the name (and its length) of the existing file/directory and the
290 * inode for the new directory and the name (and its length) of the
291 * new file/directory.
292 * XXX: how do you handle must_be dir?
294 static int hfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
295 struct inode
*new_dir
, struct dentry
*new_dentry
)
299 /* Unlink destination if it already exists */
300 if (new_dentry
->d_inode
) {
301 res
= hfs_unlink(new_dir
, new_dentry
);
306 res
= hfs_cat_move(old_dentry
->d_inode
->i_ino
,
307 old_dir
, &old_dentry
->d_name
,
308 new_dir
, &new_dentry
->d_name
);
310 hfs_cat_build_key((btree_key
*)&HFS_I(old_dentry
->d_inode
)->cat_key
,
311 new_dir
->i_ino
, &new_dentry
->d_name
);
315 struct file_operations hfs_dir_operations
= {
316 .read
= generic_read_dir
,
317 .readdir
= hfs_readdir
,
318 .llseek
= generic_file_llseek
,
319 .release
= hfs_dir_release
,
322 struct inode_operations hfs_dir_inode_operations
= {
323 .create
= hfs_create
,
324 .lookup
= hfs_lookup
,
325 .unlink
= hfs_unlink
,
328 .rename
= hfs_rename
,
329 .setattr
= hfs_inode_setattr
,