4 * Copyright (C) 1995-1997 Paul H. Hargrove
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 res = hfs_find_init(HFS_SB(dir->i_sb)->cat_tree, &fd);
31 hfs_cat_build_key(dir->i_sb, 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 = file_inode(filp);
57 struct super_block *sb = inode->i_sb;
59 char strbuf[HFS_MAX_NAMELEN];
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 err = hfs_find_init(HFS_SB(sb)->cat_tree, &fd);
71 hfs_cat_build_key(sb, fd.search_key, inode->i_ino, NULL);
72 err = hfs_brec_find(&fd);
76 switch ((u32)filp->f_pos) {
78 /* This is completely artificial... */
79 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR))
84 if (fd.entrylength > sizeof(entry) || fd.entrylength < 0) {
89 hfs_bnode_read(fd.bnode, &entry, fd.entryoffset, fd.entrylength);
90 if (entry.type != HFS_CDR_THD) {
91 pr_err("bad catalog folder thread\n");
95 //if (fd.entrylength < HFS_MIN_THREAD_SZ) {
96 // pr_err("truncated catalog thread\n");
100 if (filldir(dirent, "..", 2, 1,
101 be32_to_cpu(entry.thread.ParID), DT_DIR))
106 if (filp->f_pos >= inode->i_size)
108 err = hfs_brec_goto(&fd, filp->f_pos - 1);
114 if (be32_to_cpu(fd.key->cat.ParID) != inode->i_ino) {
115 pr_err("walked past end of dir\n");
120 if (fd.entrylength > sizeof(entry) || fd.entrylength < 0) {
125 hfs_bnode_read(fd.bnode, &entry, fd.entryoffset, fd.entrylength);
127 len = hfs_mac2asc(sb, strbuf, &fd.key->cat.CName);
128 if (type == HFS_CDR_DIR) {
129 if (fd.entrylength < sizeof(struct hfs_cat_dir)) {
130 pr_err("small dir entry\n");
134 if (filldir(dirent, strbuf, len, filp->f_pos,
135 be32_to_cpu(entry.dir.DirID), DT_DIR))
137 } else if (type == HFS_CDR_FIL) {
138 if (fd.entrylength < sizeof(struct hfs_cat_file)) {
139 pr_err("small file entry\n");
143 if (filldir(dirent, strbuf, len, filp->f_pos,
144 be32_to_cpu(entry.file.FlNum), DT_REG))
147 pr_err("bad catalog entry type %d\n", type);
152 if (filp->f_pos >= inode->i_size)
154 err = hfs_brec_goto(&fd, 1);
158 rd = filp->private_data;
160 rd = kmalloc(sizeof(struct hfs_readdir_data), GFP_KERNEL);
165 filp->private_data = rd;
167 list_add(&rd->list, &HFS_I(inode)->open_dir_list);
169 memcpy(&rd->key, &fd.key, sizeof(struct hfs_cat_key));
175 static int hfs_dir_release(struct inode *inode, struct file *file)
177 struct hfs_readdir_data *rd = file->private_data;
179 mutex_lock(&inode->i_mutex);
181 mutex_unlock(&inode->i_mutex);
190 * This is the create() entry in the inode_operations structure for
191 * regular HFS directories. The purpose is to create a new file in
192 * a directory and return a corresponding inode, given the inode for
193 * the directory and the name (and its length) of the new file.
195 static int hfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
201 inode = hfs_new_inode(dir, &dentry->d_name, mode);
205 res = hfs_cat_create(inode->i_ino, dir, &dentry->d_name, inode);
208 hfs_delete_inode(inode);
212 d_instantiate(dentry, inode);
213 mark_inode_dirty(inode);
220 * This is the mkdir() entry in the inode_operations structure for
221 * regular HFS directories. The purpose is to create a new directory
222 * in a directory, given the inode for the parent directory and the
223 * name (and its length) of the new directory.
225 static int hfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
230 inode = hfs_new_inode(dir, &dentry->d_name, S_IFDIR | mode);
234 res = hfs_cat_create(inode->i_ino, dir, &dentry->d_name, inode);
237 hfs_delete_inode(inode);
241 d_instantiate(dentry, inode);
242 mark_inode_dirty(inode);
249 * This serves as both unlink() and rmdir() in the inode_operations
250 * structure for regular HFS directories. The purpose is to delete
251 * an existing child, given the inode for the parent directory and
252 * the name (and its length) of the existing directory.
254 * HFS does not have hardlinks, so both rmdir and unlink set the
255 * link count to 0. The only difference is the emptiness check.
257 static int hfs_remove(struct inode *dir, struct dentry *dentry)
259 struct inode *inode = dentry->d_inode;
262 if (S_ISDIR(inode->i_mode) && inode->i_size != 2)
264 res = hfs_cat_delete(inode->i_ino, dir, &dentry->d_name);
268 inode->i_ctime = CURRENT_TIME_SEC;
269 hfs_delete_inode(inode);
270 mark_inode_dirty(inode);
277 * This is the rename() entry in the inode_operations structure for
278 * regular HFS directories. The purpose is to rename an existing
279 * file or directory, given the inode for the current directory and
280 * the name (and its length) of the existing file/directory and the
281 * inode for the new directory and the name (and its length) of the
282 * new file/directory.
283 * XXX: how do you handle must_be dir?
285 static int hfs_rename(struct inode *old_dir, struct dentry *old_dentry,
286 struct inode *new_dir, struct dentry *new_dentry)
290 /* Unlink destination if it already exists */
291 if (new_dentry->d_inode) {
292 res = hfs_remove(new_dir, new_dentry);
297 res = hfs_cat_move(old_dentry->d_inode->i_ino,
298 old_dir, &old_dentry->d_name,
299 new_dir, &new_dentry->d_name);
301 hfs_cat_build_key(old_dir->i_sb,
302 (btree_key *)&HFS_I(old_dentry->d_inode)->cat_key,
303 new_dir->i_ino, &new_dentry->d_name);
307 const struct file_operations hfs_dir_operations = {
308 .read = generic_read_dir,
309 .readdir = hfs_readdir,
310 .llseek = generic_file_llseek,
311 .release = hfs_dir_release,
314 const struct inode_operations hfs_dir_inode_operations = {
315 .create = hfs_create,
316 .lookup = hfs_lookup,
317 .unlink = hfs_remove,
320 .rename = hfs_rename,
321 .setattr = hfs_inode_setattr,