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 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
));
37 inode
= hfs_iget(dir
->i_sb
, &fd
.search_key
->cat
, &rec
);
39 inode
= ERR_PTR(-EACCES
);
42 return d_splice_alias(inode
, dentry
);
48 static int hfs_readdir(struct file
*file
, struct dir_context
*ctx
)
50 struct inode
*inode
= file_inode(file
);
51 struct super_block
*sb
= inode
->i_sb
;
53 char strbuf
[HFS_MAX_NAMELEN
];
54 union hfs_cat_rec entry
;
55 struct hfs_find_data fd
;
56 struct hfs_readdir_data
*rd
;
59 if (ctx
->pos
>= inode
->i_size
)
62 err
= hfs_find_init(HFS_SB(sb
)->cat_tree
, &fd
);
65 hfs_cat_build_key(sb
, fd
.search_key
, inode
->i_ino
, NULL
);
66 err
= hfs_brec_find(&fd
);
71 /* This is completely artificial... */
72 if (!dir_emit_dot(file
, ctx
))
77 if (fd
.entrylength
> sizeof(entry
) || fd
.entrylength
< 0) {
82 hfs_bnode_read(fd
.bnode
, &entry
, fd
.entryoffset
, fd
.entrylength
);
83 if (entry
.type
!= HFS_CDR_THD
) {
84 pr_err("bad catalog folder thread\n");
88 //if (fd.entrylength < HFS_MIN_THREAD_SZ) {
89 // pr_err("truncated catalog thread\n");
93 if (!dir_emit(ctx
, "..", 2,
94 be32_to_cpu(entry
.thread
.ParID
), DT_DIR
))
98 if (ctx
->pos
>= inode
->i_size
)
100 err
= hfs_brec_goto(&fd
, ctx
->pos
- 1);
105 if (be32_to_cpu(fd
.key
->cat
.ParID
) != inode
->i_ino
) {
106 pr_err("walked past end of dir\n");
111 if (fd
.entrylength
> sizeof(entry
) || fd
.entrylength
< 0) {
116 hfs_bnode_read(fd
.bnode
, &entry
, fd
.entryoffset
, fd
.entrylength
);
118 len
= hfs_mac2asc(sb
, strbuf
, &fd
.key
->cat
.CName
);
119 if (type
== HFS_CDR_DIR
) {
120 if (fd
.entrylength
< sizeof(struct hfs_cat_dir
)) {
121 pr_err("small dir entry\n");
125 if (!dir_emit(ctx
, strbuf
, len
,
126 be32_to_cpu(entry
.dir
.DirID
), DT_DIR
))
128 } else if (type
== HFS_CDR_FIL
) {
129 if (fd
.entrylength
< sizeof(struct hfs_cat_file
)) {
130 pr_err("small file entry\n");
134 if (!dir_emit(ctx
, strbuf
, len
,
135 be32_to_cpu(entry
.file
.FlNum
), DT_REG
))
138 pr_err("bad catalog entry type %d\n", type
);
143 if (ctx
->pos
>= inode
->i_size
)
145 err
= hfs_brec_goto(&fd
, 1);
149 rd
= file
->private_data
;
151 rd
= kmalloc(sizeof(struct hfs_readdir_data
), GFP_KERNEL
);
156 file
->private_data
= rd
;
158 spin_lock(&HFS_I(inode
)->open_dir_lock
);
159 list_add(&rd
->list
, &HFS_I(inode
)->open_dir_list
);
160 spin_unlock(&HFS_I(inode
)->open_dir_lock
);
163 * Can be done after the list insertion; exclusion with
164 * hfs_delete_cat() is provided by directory lock.
166 memcpy(&rd
->key
, &fd
.key
->cat
, sizeof(struct hfs_cat_key
));
172 static int hfs_dir_release(struct inode
*inode
, struct file
*file
)
174 struct hfs_readdir_data
*rd
= file
->private_data
;
176 spin_lock(&HFS_I(inode
)->open_dir_lock
);
178 spin_unlock(&HFS_I(inode
)->open_dir_lock
);
187 * This is the create() entry in the inode_operations structure for
188 * regular HFS directories. The purpose is to create a new file in
189 * a directory and return a corresponding inode, given the inode for
190 * the directory and the name (and its length) of the new file.
192 static int hfs_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
198 inode
= hfs_new_inode(dir
, &dentry
->d_name
, mode
);
202 res
= hfs_cat_create(inode
->i_ino
, dir
, &dentry
->d_name
, inode
);
205 hfs_delete_inode(inode
);
209 d_instantiate(dentry
, inode
);
210 mark_inode_dirty(inode
);
217 * This is the mkdir() entry in the inode_operations structure for
218 * regular HFS directories. The purpose is to create a new directory
219 * in a directory, given the inode for the parent directory and the
220 * name (and its length) of the new directory.
222 static int hfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
227 inode
= hfs_new_inode(dir
, &dentry
->d_name
, S_IFDIR
| mode
);
231 res
= hfs_cat_create(inode
->i_ino
, dir
, &dentry
->d_name
, inode
);
234 hfs_delete_inode(inode
);
238 d_instantiate(dentry
, inode
);
239 mark_inode_dirty(inode
);
246 * This serves as both unlink() and rmdir() in the inode_operations
247 * structure for regular HFS directories. The purpose is to delete
248 * an existing child, given the inode for the parent directory and
249 * the name (and its length) of the existing directory.
251 * HFS does not have hardlinks, so both rmdir and unlink set the
252 * link count to 0. The only difference is the emptiness check.
254 static int hfs_remove(struct inode
*dir
, struct dentry
*dentry
)
256 struct inode
*inode
= d_inode(dentry
);
259 if (S_ISDIR(inode
->i_mode
) && inode
->i_size
!= 2)
261 res
= hfs_cat_delete(inode
->i_ino
, dir
, &dentry
->d_name
);
265 inode
->i_ctime
= current_time(inode
);
266 hfs_delete_inode(inode
);
267 mark_inode_dirty(inode
);
274 * This is the rename() entry in the inode_operations structure for
275 * regular HFS directories. The purpose is to rename an existing
276 * file or directory, given the inode for the current directory and
277 * the name (and its length) of the existing file/directory and the
278 * inode for the new directory and the name (and its length) of the
279 * new file/directory.
280 * XXX: how do you handle must_be dir?
282 static int hfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
283 struct inode
*new_dir
, struct dentry
*new_dentry
,
288 if (flags
& ~RENAME_NOREPLACE
)
291 /* Unlink destination if it already exists */
292 if (d_really_is_positive(new_dentry
)) {
293 res
= hfs_remove(new_dir
, new_dentry
);
298 res
= hfs_cat_move(d_inode(old_dentry
)->i_ino
,
299 old_dir
, &old_dentry
->d_name
,
300 new_dir
, &new_dentry
->d_name
);
302 hfs_cat_build_key(old_dir
->i_sb
,
303 (btree_key
*)&HFS_I(d_inode(old_dentry
))->cat_key
,
304 new_dir
->i_ino
, &new_dentry
->d_name
);
308 const struct file_operations hfs_dir_operations
= {
309 .read
= generic_read_dir
,
310 .iterate_shared
= hfs_readdir
,
311 .llseek
= generic_file_llseek
,
312 .release
= hfs_dir_release
,
315 const struct inode_operations hfs_dir_inode_operations
= {
316 .create
= hfs_create
,
317 .lookup
= hfs_lookup
,
318 .unlink
= hfs_remove
,
321 .rename
= hfs_rename
,
322 .setattr
= hfs_inode_setattr
,