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 hfs_find_init(HFS_SB(dir
->i_sb
)->cat_tree
, &fd
);
29 hfs_cat_build_key(dir
->i_sb
, fd
.search_key
, dir
->i_ino
, &dentry
->d_name
);
30 res
= hfs_brec_read(&fd
, &rec
, sizeof(rec
));
40 inode
= hfs_iget(dir
->i_sb
, &fd
.search_key
->cat
, &rec
);
43 return ERR_PTR(-EACCES
);
52 static int hfs_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
54 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
55 struct super_block
*sb
= inode
->i_sb
;
57 char strbuf
[HFS_MAX_NAMELEN
];
58 union hfs_cat_rec entry
;
59 struct hfs_find_data fd
;
60 struct hfs_readdir_data
*rd
;
63 if (filp
->f_pos
>= inode
->i_size
)
66 hfs_find_init(HFS_SB(sb
)->cat_tree
, &fd
);
67 hfs_cat_build_key(sb
, fd
.search_key
, inode
->i_ino
, NULL
);
68 err
= hfs_brec_find(&fd
);
72 switch ((u32
)filp
->f_pos
) {
74 /* This is completely artificial... */
75 if (filldir(dirent
, ".", 1, 0, inode
->i_ino
, DT_DIR
))
80 if (fd
.entrylength
> sizeof(entry
) || fd
.entrylength
< 0) {
85 hfs_bnode_read(fd
.bnode
, &entry
, fd
.entryoffset
, fd
.entrylength
);
86 if (entry
.type
!= HFS_CDR_THD
) {
87 printk(KERN_ERR
"hfs: bad catalog folder thread\n");
91 //if (fd.entrylength < HFS_MIN_THREAD_SZ) {
92 // printk(KERN_ERR "hfs: truncated catalog thread\n");
96 if (filldir(dirent
, "..", 2, 1,
97 be32_to_cpu(entry
.thread
.ParID
), DT_DIR
))
102 if (filp
->f_pos
>= inode
->i_size
)
104 err
= hfs_brec_goto(&fd
, filp
->f_pos
- 1);
110 if (be32_to_cpu(fd
.key
->cat
.ParID
) != inode
->i_ino
) {
111 printk(KERN_ERR
"hfs: walked past end of dir\n");
116 if (fd
.entrylength
> sizeof(entry
) || fd
.entrylength
< 0) {
121 hfs_bnode_read(fd
.bnode
, &entry
, fd
.entryoffset
, fd
.entrylength
);
123 len
= hfs_mac2asc(sb
, strbuf
, &fd
.key
->cat
.CName
);
124 if (type
== HFS_CDR_DIR
) {
125 if (fd
.entrylength
< sizeof(struct hfs_cat_dir
)) {
126 printk(KERN_ERR
"hfs: small dir entry\n");
130 if (filldir(dirent
, strbuf
, len
, filp
->f_pos
,
131 be32_to_cpu(entry
.dir
.DirID
), DT_DIR
))
133 } else if (type
== HFS_CDR_FIL
) {
134 if (fd
.entrylength
< sizeof(struct hfs_cat_file
)) {
135 printk(KERN_ERR
"hfs: small file entry\n");
139 if (filldir(dirent
, strbuf
, len
, filp
->f_pos
,
140 be32_to_cpu(entry
.file
.FlNum
), DT_REG
))
143 printk(KERN_ERR
"hfs: bad catalog entry type %d\n", type
);
148 if (filp
->f_pos
>= inode
->i_size
)
150 err
= hfs_brec_goto(&fd
, 1);
154 rd
= filp
->private_data
;
156 rd
= kmalloc(sizeof(struct hfs_readdir_data
), GFP_KERNEL
);
161 filp
->private_data
= rd
;
163 list_add(&rd
->list
, &HFS_I(inode
)->open_dir_list
);
165 memcpy(&rd
->key
, &fd
.key
, sizeof(struct hfs_cat_key
));
171 static int hfs_dir_release(struct inode
*inode
, struct file
*file
)
173 struct hfs_readdir_data
*rd
= file
->private_data
;
184 * This is the create() entry in the inode_operations structure for
185 * regular HFS directories. The purpose is to create a new file in
186 * a directory and return a corresponding inode, given the inode for
187 * the directory and the name (and its length) of the new file.
189 static int hfs_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
190 struct nameidata
*nd
)
195 inode
= hfs_new_inode(dir
, &dentry
->d_name
, mode
);
199 res
= hfs_cat_create(inode
->i_ino
, dir
, &dentry
->d_name
, inode
);
202 hfs_delete_inode(inode
);
206 d_instantiate(dentry
, inode
);
207 mark_inode_dirty(inode
);
214 * This is the mkdir() entry in the inode_operations structure for
215 * regular HFS directories. The purpose is to create a new directory
216 * in a directory, given the inode for the parent directory and the
217 * name (and its length) of the new directory.
219 static int hfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
224 inode
= hfs_new_inode(dir
, &dentry
->d_name
, S_IFDIR
| mode
);
228 res
= hfs_cat_create(inode
->i_ino
, dir
, &dentry
->d_name
, inode
);
231 hfs_delete_inode(inode
);
235 d_instantiate(dentry
, inode
);
236 mark_inode_dirty(inode
);
243 * This serves as both unlink() and rmdir() in the inode_operations
244 * structure for regular HFS directories. The purpose is to delete
245 * an existing child, given the inode for the parent directory and
246 * the name (and its length) of the existing directory.
248 * HFS does not have hardlinks, so both rmdir and unlink set the
249 * link count to 0. The only difference is the emptiness check.
251 static int hfs_remove(struct inode
*dir
, struct dentry
*dentry
)
253 struct inode
*inode
= dentry
->d_inode
;
256 if (S_ISDIR(inode
->i_mode
) && inode
->i_size
!= 2)
258 res
= hfs_cat_delete(inode
->i_ino
, dir
, &dentry
->d_name
);
262 inode
->i_ctime
= CURRENT_TIME_SEC
;
263 hfs_delete_inode(inode
);
264 mark_inode_dirty(inode
);
271 * This is the rename() entry in the inode_operations structure for
272 * regular HFS directories. The purpose is to rename an existing
273 * file or directory, given the inode for the current directory and
274 * the name (and its length) of the existing file/directory and the
275 * inode for the new directory and the name (and its length) of the
276 * new file/directory.
277 * XXX: how do you handle must_be dir?
279 static int hfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
280 struct inode
*new_dir
, struct dentry
*new_dentry
)
284 /* Unlink destination if it already exists */
285 if (new_dentry
->d_inode
) {
286 res
= hfs_remove(new_dir
, new_dentry
);
291 res
= hfs_cat_move(old_dentry
->d_inode
->i_ino
,
292 old_dir
, &old_dentry
->d_name
,
293 new_dir
, &new_dentry
->d_name
);
295 hfs_cat_build_key(old_dir
->i_sb
,
296 (btree_key
*)&HFS_I(old_dentry
->d_inode
)->cat_key
,
297 new_dir
->i_ino
, &new_dentry
->d_name
);
301 const struct file_operations hfs_dir_operations
= {
302 .read
= generic_read_dir
,
303 .readdir
= hfs_readdir
,
304 .llseek
= generic_file_llseek
,
305 .release
= hfs_dir_release
,
308 const struct inode_operations hfs_dir_inode_operations
= {
309 .create
= hfs_create
,
310 .lookup
= hfs_lookup
,
311 .unlink
= hfs_remove
,
314 .rename
= hfs_rename
,
315 .setattr
= hfs_inode_setattr
,