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
));
42 inode
= hfs_iget(dir
->i_sb
, &fd
.search_key
->cat
, &rec
);
45 return ERR_PTR(-EACCES
);
54 static int hfs_readdir(struct file
*file
, struct dir_context
*ctx
)
56 struct inode
*inode
= file_inode(file
);
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 (ctx
->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
);
77 /* This is completely artificial... */
78 if (!dir_emit_dot(file
, ctx
))
83 if (fd
.entrylength
> sizeof(entry
) || fd
.entrylength
< 0) {
88 hfs_bnode_read(fd
.bnode
, &entry
, fd
.entryoffset
, fd
.entrylength
);
89 if (entry
.type
!= HFS_CDR_THD
) {
90 pr_err("bad catalog folder thread\n");
94 //if (fd.entrylength < HFS_MIN_THREAD_SZ) {
95 // pr_err("truncated catalog thread\n");
99 if (!dir_emit(ctx
, "..", 2,
100 be32_to_cpu(entry
.thread
.ParID
), DT_DIR
))
104 if (ctx
->pos
>= inode
->i_size
)
106 err
= hfs_brec_goto(&fd
, ctx
->pos
- 1);
111 if (be32_to_cpu(fd
.key
->cat
.ParID
) != inode
->i_ino
) {
112 pr_err("walked past end of dir\n");
117 if (fd
.entrylength
> sizeof(entry
) || fd
.entrylength
< 0) {
122 hfs_bnode_read(fd
.bnode
, &entry
, fd
.entryoffset
, fd
.entrylength
);
124 len
= hfs_mac2asc(sb
, strbuf
, &fd
.key
->cat
.CName
);
125 if (type
== HFS_CDR_DIR
) {
126 if (fd
.entrylength
< sizeof(struct hfs_cat_dir
)) {
127 pr_err("small dir entry\n");
131 if (!dir_emit(ctx
, strbuf
, len
,
132 be32_to_cpu(entry
.dir
.DirID
), DT_DIR
))
134 } else if (type
== HFS_CDR_FIL
) {
135 if (fd
.entrylength
< sizeof(struct hfs_cat_file
)) {
136 pr_err("small file entry\n");
140 if (!dir_emit(ctx
, strbuf
, len
,
141 be32_to_cpu(entry
.file
.FlNum
), DT_REG
))
144 pr_err("bad catalog entry type %d\n", type
);
149 if (ctx
->pos
>= inode
->i_size
)
151 err
= hfs_brec_goto(&fd
, 1);
155 rd
= file
->private_data
;
157 rd
= kmalloc(sizeof(struct hfs_readdir_data
), GFP_KERNEL
);
162 file
->private_data
= rd
;
164 spin_lock(&HFS_I(inode
)->open_dir_lock
);
165 list_add(&rd
->list
, &HFS_I(inode
)->open_dir_list
);
166 spin_unlock(&HFS_I(inode
)->open_dir_lock
);
169 * Can be done after the list insertion; exclusion with
170 * hfs_delete_cat() is provided by directory lock.
172 memcpy(&rd
->key
, &fd
.key
, sizeof(struct hfs_cat_key
));
178 static int hfs_dir_release(struct inode
*inode
, struct file
*file
)
180 struct hfs_readdir_data
*rd
= file
->private_data
;
182 spin_lock(&HFS_I(inode
)->open_dir_lock
);
184 spin_unlock(&HFS_I(inode
)->open_dir_lock
);
193 * This is the create() entry in the inode_operations structure for
194 * regular HFS directories. The purpose is to create a new file in
195 * a directory and return a corresponding inode, given the inode for
196 * the directory and the name (and its length) of the new file.
198 static int hfs_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
204 inode
= hfs_new_inode(dir
, &dentry
->d_name
, mode
);
208 res
= hfs_cat_create(inode
->i_ino
, dir
, &dentry
->d_name
, inode
);
211 hfs_delete_inode(inode
);
215 d_instantiate(dentry
, inode
);
216 mark_inode_dirty(inode
);
223 * This is the mkdir() entry in the inode_operations structure for
224 * regular HFS directories. The purpose is to create a new directory
225 * in a directory, given the inode for the parent directory and the
226 * name (and its length) of the new directory.
228 static int hfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
233 inode
= hfs_new_inode(dir
, &dentry
->d_name
, S_IFDIR
| mode
);
237 res
= hfs_cat_create(inode
->i_ino
, dir
, &dentry
->d_name
, inode
);
240 hfs_delete_inode(inode
);
244 d_instantiate(dentry
, inode
);
245 mark_inode_dirty(inode
);
252 * This serves as both unlink() and rmdir() in the inode_operations
253 * structure for regular HFS directories. The purpose is to delete
254 * an existing child, given the inode for the parent directory and
255 * the name (and its length) of the existing directory.
257 * HFS does not have hardlinks, so both rmdir and unlink set the
258 * link count to 0. The only difference is the emptiness check.
260 static int hfs_remove(struct inode
*dir
, struct dentry
*dentry
)
262 struct inode
*inode
= d_inode(dentry
);
265 if (S_ISDIR(inode
->i_mode
) && inode
->i_size
!= 2)
267 res
= hfs_cat_delete(inode
->i_ino
, dir
, &dentry
->d_name
);
271 inode
->i_ctime
= current_time(inode
);
272 hfs_delete_inode(inode
);
273 mark_inode_dirty(inode
);
280 * This is the rename() entry in the inode_operations structure for
281 * regular HFS directories. The purpose is to rename an existing
282 * file or directory, given the inode for the current directory and
283 * the name (and its length) of the existing file/directory and the
284 * inode for the new directory and the name (and its length) of the
285 * new file/directory.
286 * XXX: how do you handle must_be dir?
288 static int hfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
289 struct inode
*new_dir
, struct dentry
*new_dentry
,
294 if (flags
& ~RENAME_NOREPLACE
)
297 /* Unlink destination if it already exists */
298 if (d_really_is_positive(new_dentry
)) {
299 res
= hfs_remove(new_dir
, new_dentry
);
304 res
= hfs_cat_move(d_inode(old_dentry
)->i_ino
,
305 old_dir
, &old_dentry
->d_name
,
306 new_dir
, &new_dentry
->d_name
);
308 hfs_cat_build_key(old_dir
->i_sb
,
309 (btree_key
*)&HFS_I(d_inode(old_dentry
))->cat_key
,
310 new_dir
->i_ino
, &new_dentry
->d_name
);
314 const struct file_operations hfs_dir_operations
= {
315 .read
= generic_read_dir
,
316 .iterate_shared
= hfs_readdir
,
317 .llseek
= generic_file_llseek
,
318 .release
= hfs_dir_release
,
321 const struct inode_operations hfs_dir_inode_operations
= {
322 .create
= hfs_create
,
323 .lookup
= hfs_lookup
,
324 .unlink
= hfs_remove
,
327 .rename
= hfs_rename
,
328 .setattr
= hfs_inode_setattr
,