2 * linux/fs/hfsplus/catalog.c
5 * Brad Boyer (flar@allandria.com)
6 * (C) 2003 Ardis Technologies <roman@ardistech.com>
8 * Handling of catalog records
11 #include <linux/sched.h>
13 #include "hfsplus_fs.h"
14 #include "hfsplus_raw.h"
16 int hfsplus_cmp_cat_key(hfsplus_btree_key *k1, hfsplus_btree_key *k2)
23 return be32_to_cpu(k1p) < be32_to_cpu(k2p) ? -1 : 1;
25 return hfsplus_unistrcmp(&k1->cat.name, &k2->cat.name);
28 void hfsplus_fill_cat_key(hfsplus_btree_key *key, u32 parent,
33 key->cat.parent = cpu_to_be32(parent);
35 hfsplus_asc2uni(&key->cat.name, str->name, str->len);
36 len = be16_to_cpu(key->cat.name.length);
38 len = key->cat.name.length = 0;
39 key->key_len = cpu_to_be16(6 + 2 * len);
42 static void hfsplus_fill_cat_key_uni(hfsplus_btree_key *key, u32 parent,
47 ustrlen = be16_to_cpu(name->length);
48 key->cat.parent = cpu_to_be32(parent);
49 key->cat.name.length = cpu_to_be16(ustrlen);
51 memcpy(key->cat.name.unicode, name->unicode, ustrlen);
52 key->key_len = cpu_to_be16(6 + ustrlen);
55 static void hfsplus_set_perms(struct inode *inode, hfsplus_perm *perms)
57 perms->mode = cpu_to_be32(inode->i_mode);
58 perms->owner = cpu_to_be32(inode->i_uid);
59 perms->group = cpu_to_be32(inode->i_gid);
62 static int hfsplus_fill_cat_entry(hfsplus_cat_entry *entry, u32 cnid, struct inode *inode)
64 if (S_ISDIR(inode->i_mode)) {
65 hfsplus_cat_folder *folder;
67 folder = &entry->folder;
68 memset(folder, 0, sizeof(*folder));
69 folder->type = cpu_to_be16(HFSPLUS_FOLDER);
70 folder->id = cpu_to_be32(inode->i_ino);
71 folder->create_date = folder->content_mod_date =
72 folder->attribute_mod_date = folder->access_date =
74 hfsplus_set_perms(inode, &folder->permissions);
75 if (inode == HFSPLUS_SB(inode->i_sb).hidden_dir)
76 /* invisible and namelocked */
77 folder->user_info.frFlags = cpu_to_be16(0x5000);
78 return sizeof(*folder);
80 hfsplus_cat_file *file;
83 memset(file, 0, sizeof(*file));
84 file->type = cpu_to_be16(HFSPLUS_FILE);
85 file->id = cpu_to_be32(cnid);
86 file->create_date = file->content_mod_date =
87 file->attribute_mod_date = file->access_date =
89 if (cnid == inode->i_ino) {
90 hfsplus_set_perms(inode, &file->permissions);
91 file->user_info.fdType = cpu_to_be32(HFSPLUS_SB(inode->i_sb).type);
92 file->user_info.fdCreator = cpu_to_be32(HFSPLUS_SB(inode->i_sb).creator);
94 file->user_info.fdType = cpu_to_be32(HFSP_HARDLINK_TYPE);
95 file->user_info.fdCreator = cpu_to_be32(HFSP_HFSPLUS_CREATOR);
96 file->user_info.fdFlags = cpu_to_be16(0x100);
97 file->permissions.dev = cpu_to_be32(HFSPLUS_I(inode).dev);
103 static int hfsplus_fill_cat_thread(hfsplus_cat_entry *entry, int type,
104 u32 parentid, struct qstr *str)
106 entry->type = cpu_to_be16(type);
107 entry->thread.reserved = 0;
108 entry->thread.parentID = cpu_to_be32(parentid);
109 hfsplus_asc2uni(&entry->thread.nodeName, str->name, str->len);
110 return 10 + be16_to_cpu(entry->thread.nodeName.length) * 2;
113 /* Try to get a catalog entry for given catalog id */
114 int hfsplus_find_cat(struct super_block *sb, unsigned long cnid,
115 struct hfsplus_find_data *fd)
117 hfsplus_cat_entry tmp;
121 hfsplus_fill_cat_key(fd->search_key, cnid, NULL);
122 err = hfsplus_btree_find_entry(fd, &tmp, sizeof(hfsplus_cat_entry));
126 type = be16_to_cpu(tmp.type);
127 if (type != HFSPLUS_FOLDER_THREAD && type != HFSPLUS_FILE_THREAD) {
128 printk("HFS+-fs: Found bad thread record in catalog\n");
132 if (be16_to_cpu(tmp.thread.nodeName.length) > 255) {
133 printk(KERN_ERR "hfs: catalog name length corrupted\n");
137 hfsplus_fill_cat_key_uni(fd->search_key, be32_to_cpu(tmp.thread.parentID),
138 &tmp.thread.nodeName);
139 return hfsplus_btree_find(fd);
142 int hfsplus_create_cat(u32 cnid, struct inode *dir, struct qstr *str, struct inode *inode)
144 struct hfsplus_find_data fd;
145 struct super_block *sb;
146 hfsplus_cat_entry entry;
150 dprint(DBG_CAT_MOD, "create_cat: %s,%u(%d)\n", str->name, cnid, inode->i_nlink);
152 hfsplus_find_init(HFSPLUS_SB(sb).cat_tree, &fd);
154 hfsplus_fill_cat_key(fd.search_key, cnid, NULL);
155 entry_size = hfsplus_fill_cat_thread(&entry, S_ISDIR(inode->i_mode) ?
156 HFSPLUS_FOLDER_THREAD : HFSPLUS_FILE_THREAD,
158 err = hfsplus_btree_find(&fd);
159 if (err != -ENOENT) {
164 err = hfsplus_bnode_insert_rec(&fd, &entry, entry_size);
168 hfsplus_fill_cat_key(fd.search_key, dir->i_ino, str);
169 entry_size = hfsplus_fill_cat_entry(&entry, cnid, inode);
170 err = hfsplus_btree_find(&fd);
171 if (err != -ENOENT) {
177 err = hfsplus_bnode_insert_rec(&fd, &entry, entry_size);
180 mark_inode_dirty(dir);
183 hfsplus_find_exit(&fd);
188 int hfsplus_delete_cat(u32 cnid, struct inode *dir, struct qstr *str)
190 struct super_block *sb;
191 struct hfsplus_find_data fd;
192 hfsplus_fork_raw fork;
193 struct list_head *pos;
197 dprint(DBG_CAT_MOD, "delete_cat: %s,%u\n", str ? str->name : NULL, cnid);
199 hfsplus_find_init(HFSPLUS_SB(sb).cat_tree, &fd);
204 hfsplus_fill_cat_key(fd.search_key, cnid, NULL);
205 err = hfsplus_btree_find(&fd);
209 off = fd.entryoffset + offsetof(hfsplus_cat_thread, nodeName);
210 fd.search_key->cat.parent = cpu_to_be32(dir->i_ino);
211 hfsplus_bnode_readbytes(fd.bnode, &fd.search_key->cat.name.length, off, 2);
212 len = be16_to_cpu(fd.search_key->cat.name.length) * 2;
213 hfsplus_bnode_readbytes(fd.bnode, &fd.search_key->cat.name.unicode, off + 2, len);
214 fd.search_key->key_len = cpu_to_be16(6 + len);
216 hfsplus_fill_cat_key(fd.search_key, dir->i_ino, str);
218 err = hfsplus_btree_find(&fd);
222 type = hfsplus_bnode_read_u16(fd.bnode, fd.entryoffset);
223 if (type == HFSPLUS_FILE) {
225 off = fd.entryoffset + offsetof(hfsplus_cat_file, data_fork);
226 hfsplus_bnode_readbytes(fd.bnode, &fork, off, sizeof(fork));
227 hfsplus_free_fork(sb, cnid, &fork, HFSPLUS_TYPE_DATA);
230 off = fd.entryoffset + offsetof(hfsplus_cat_file, rsrc_fork);
231 hfsplus_bnode_readbytes(fd.bnode, &fork, off, sizeof(fork));
232 hfsplus_free_fork(sb, cnid, &fork, HFSPLUS_TYPE_RSRC);
235 list_for_each(pos, &HFSPLUS_I(dir).open_dir_list) {
236 struct hfsplus_readdir_data *rd =
237 list_entry(pos, struct hfsplus_readdir_data, list);
238 if (fd.tree->keycmp(fd.search_key, (void *)&rd->key) < 0)
242 err = hfsplus_bnode_remove_rec(&fd);
246 hfsplus_fill_cat_key(fd.search_key, cnid, NULL);
247 err = hfsplus_btree_find(&fd);
251 err = hfsplus_bnode_remove_rec(&fd);
256 mark_inode_dirty(dir);
258 hfsplus_find_exit(&fd);
263 int hfsplus_rename_cat(u32 cnid,
264 struct inode *src_dir, struct qstr *src_name,
265 struct inode *dst_dir, struct qstr *dst_name)
267 struct super_block *sb;
268 struct hfsplus_find_data src_fd, dst_fd;
269 hfsplus_cat_entry entry;
270 int entry_size, type;
273 dprint(DBG_CAT_MOD, "rename_cat: %u - %lu,%s - %lu,%s\n", cnid, src_dir->i_ino, src_name->name,
274 dst_dir->i_ino, dst_name->name);
276 hfsplus_find_init(HFSPLUS_SB(sb).cat_tree, &src_fd);
279 /* find the old dir entry and read the data */
280 hfsplus_fill_cat_key(src_fd.search_key, src_dir->i_ino, src_name);
281 err = hfsplus_btree_find(&src_fd);
285 hfsplus_bnode_readbytes(src_fd.bnode, &entry, src_fd.entryoffset,
288 /* create new dir entry with the data from the old entry */
289 hfsplus_fill_cat_key(dst_fd.search_key, dst_dir->i_ino, dst_name);
290 err = hfsplus_btree_find(&dst_fd);
291 if (err != -ENOENT) {
297 err = hfsplus_bnode_insert_rec(&dst_fd, &entry, src_fd.entrylength);
301 mark_inode_dirty(dst_dir);
303 /* finally remove the old entry */
304 hfsplus_fill_cat_key(src_fd.search_key, src_dir->i_ino, src_name);
305 err = hfsplus_btree_find(&src_fd);
308 err = hfsplus_bnode_remove_rec(&src_fd);
312 mark_inode_dirty(src_dir);
314 /* remove old thread entry */
315 hfsplus_fill_cat_key(src_fd.search_key, cnid, NULL);
316 err = hfsplus_btree_find(&src_fd);
319 type = hfsplus_bnode_read_u16(src_fd.bnode, src_fd.entryoffset);
320 err = hfsplus_bnode_remove_rec(&src_fd);
324 /* create new thread entry */
325 hfsplus_fill_cat_key(dst_fd.search_key, cnid, NULL);
326 entry_size = hfsplus_fill_cat_thread(&entry, type, dst_dir->i_ino, dst_name);
327 err = hfsplus_btree_find(&dst_fd);
328 if (err != -ENOENT) {
333 err = hfsplus_bnode_insert_rec(&dst_fd, &entry, entry_size);
335 hfsplus_put_bnode(dst_fd.bnode);
336 hfsplus_find_exit(&src_fd);