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_cat_cmp_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_cat_build_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_cat_build_key_uni(hfsplus_btree_key *key, u32 parent,
43 struct hfsplus_unistr *name)
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, struct hfsplus_perm *perms)
57 if (inode->i_flags & S_IMMUTABLE)
58 perms->rootflags |= HFSPLUS_FLG_IMMUTABLE;
60 perms->rootflags &= ~HFSPLUS_FLG_IMMUTABLE;
61 if (inode->i_flags & S_APPEND)
62 perms->rootflags |= HFSPLUS_FLG_APPEND;
64 perms->rootflags &= ~HFSPLUS_FLG_APPEND;
65 HFSPLUS_I(inode).rootflags = perms->rootflags;
66 HFSPLUS_I(inode).userflags = perms->userflags;
67 perms->mode = cpu_to_be16(inode->i_mode);
68 perms->owner = cpu_to_be32(inode->i_uid);
69 perms->group = cpu_to_be32(inode->i_gid);
72 static int hfsplus_cat_build_record(hfsplus_cat_entry *entry, u32 cnid, struct inode *inode)
74 if (S_ISDIR(inode->i_mode)) {
75 struct hfsplus_cat_folder *folder;
77 folder = &entry->folder;
78 memset(folder, 0, sizeof(*folder));
79 folder->type = cpu_to_be16(HFSPLUS_FOLDER);
80 folder->id = cpu_to_be32(inode->i_ino);
81 folder->create_date = folder->content_mod_date =
82 folder->attribute_mod_date = folder->access_date = hfsp_now2mt();
83 hfsplus_set_perms(inode, &folder->permissions);
84 if (inode == HFSPLUS_SB(inode->i_sb).hidden_dir)
85 /* invisible and namelocked */
86 folder->user_info.frFlags = cpu_to_be16(0x5000);
87 return sizeof(*folder);
89 struct hfsplus_cat_file *file;
92 memset(file, 0, sizeof(*file));
93 file->type = cpu_to_be16(HFSPLUS_FILE);
94 file->flags = cpu_to_be16(HFSPLUS_FILE_THREAD_EXISTS);
95 file->id = cpu_to_be32(cnid);
96 file->create_date = file->content_mod_date =
97 file->attribute_mod_date = file->access_date = hfsp_now2mt();
98 if (cnid == inode->i_ino) {
99 hfsplus_set_perms(inode, &file->permissions);
100 file->user_info.fdType = cpu_to_be32(HFSPLUS_SB(inode->i_sb).type);
101 file->user_info.fdCreator = cpu_to_be32(HFSPLUS_SB(inode->i_sb).creator);
102 if ((file->permissions.rootflags | file->permissions.userflags) & HFSPLUS_FLG_IMMUTABLE)
103 file->flags |= cpu_to_be16(HFSPLUS_FILE_LOCKED);
105 file->user_info.fdType = cpu_to_be32(HFSP_HARDLINK_TYPE);
106 file->user_info.fdCreator = cpu_to_be32(HFSP_HFSPLUS_CREATOR);
107 file->user_info.fdFlags = cpu_to_be16(0x100);
108 file->permissions.dev = cpu_to_be32(HFSPLUS_I(inode).dev);
110 return sizeof(*file);
114 static int hfsplus_fill_cat_thread(hfsplus_cat_entry *entry, int type,
115 u32 parentid, struct qstr *str)
117 entry->type = cpu_to_be16(type);
118 entry->thread.reserved = 0;
119 entry->thread.parentID = cpu_to_be32(parentid);
120 hfsplus_asc2uni(&entry->thread.nodeName, str->name, str->len);
121 return 10 + be16_to_cpu(entry->thread.nodeName.length) * 2;
124 /* Try to get a catalog entry for given catalog id */
125 int hfsplus_find_cat(struct super_block *sb, u32 cnid,
126 struct hfs_find_data *fd)
128 hfsplus_cat_entry tmp;
132 hfsplus_cat_build_key(fd->search_key, cnid, NULL);
133 err = hfs_brec_read(fd, &tmp, sizeof(hfsplus_cat_entry));
137 type = be16_to_cpu(tmp.type);
138 if (type != HFSPLUS_FOLDER_THREAD && type != HFSPLUS_FILE_THREAD) {
139 printk("HFS+-fs: Found bad thread record in catalog\n");
143 hfsplus_cat_build_key_uni(fd->search_key, be32_to_cpu(tmp.thread.parentID),
144 &tmp.thread.nodeName);
145 return hfs_brec_find(fd);
148 int hfsplus_create_cat(u32 cnid, struct inode *dir, struct qstr *str, struct inode *inode)
150 struct hfs_find_data fd;
151 struct super_block *sb;
152 hfsplus_cat_entry entry;
156 dprint(DBG_CAT_MOD, "create_cat: %s,%u(%d)\n", str->name, cnid, inode->i_nlink);
158 hfs_find_init(HFSPLUS_SB(sb).cat_tree, &fd);
160 hfsplus_cat_build_key(fd.search_key, cnid, NULL);
161 entry_size = hfsplus_fill_cat_thread(&entry, S_ISDIR(inode->i_mode) ?
162 HFSPLUS_FOLDER_THREAD : HFSPLUS_FILE_THREAD,
164 err = hfs_brec_find(&fd);
165 if (err != -ENOENT) {
170 err = hfs_brec_insert(&fd, &entry, entry_size);
174 hfsplus_cat_build_key(fd.search_key, dir->i_ino, str);
175 entry_size = hfsplus_cat_build_record(&entry, cnid, inode);
176 err = hfs_brec_find(&fd);
177 if (err != -ENOENT) {
183 err = hfs_brec_insert(&fd, &entry, entry_size);
186 mark_inode_dirty(dir);
194 int hfsplus_delete_cat(u32 cnid, struct inode *dir, struct qstr *str)
196 struct super_block *sb;
197 struct hfs_find_data fd;
198 struct hfsplus_fork_raw fork;
199 struct list_head *pos;
203 dprint(DBG_CAT_MOD, "delete_cat: %s,%u\n", str ? str->name : NULL, cnid);
205 hfs_find_init(HFSPLUS_SB(sb).cat_tree, &fd);
210 hfsplus_cat_build_key(fd.search_key, cnid, NULL);
211 err = hfs_brec_find(&fd);
215 off = fd.entryoffset + offsetof(struct hfsplus_cat_thread, nodeName);
216 fd.search_key->cat.parent = cpu_to_be32(dir->i_ino);
217 hfs_bnode_read(fd.bnode, &fd.search_key->cat.name.length, off, 2);
218 len = be16_to_cpu(fd.search_key->cat.name.length) * 2;
219 hfs_bnode_read(fd.bnode, &fd.search_key->cat.name.unicode, off + 2, len);
220 fd.search_key->key_len = cpu_to_be16(6 + len);
222 hfsplus_cat_build_key(fd.search_key, dir->i_ino, str);
224 err = hfs_brec_find(&fd);
228 type = hfs_bnode_read_u16(fd.bnode, fd.entryoffset);
229 if (type == HFSPLUS_FILE) {
231 off = fd.entryoffset + offsetof(hfsplus_cat_file, data_fork);
232 hfs_bnode_read(fd.bnode, &fork, off, sizeof(fork));
233 hfsplus_free_fork(sb, cnid, &fork, HFSPLUS_TYPE_DATA);
236 off = fd.entryoffset + offsetof(struct hfsplus_cat_file, rsrc_fork);
237 hfs_bnode_read(fd.bnode, &fork, off, sizeof(fork));
238 hfsplus_free_fork(sb, cnid, &fork, HFSPLUS_TYPE_RSRC);
241 list_for_each(pos, &HFSPLUS_I(dir).open_dir_list) {
242 struct hfsplus_readdir_data *rd =
243 list_entry(pos, struct hfsplus_readdir_data, list);
244 if (fd.tree->keycmp(fd.search_key, (void *)&rd->key) < 0)
248 err = hfs_brec_remove(&fd);
252 hfsplus_cat_build_key(fd.search_key, cnid, NULL);
253 err = hfs_brec_find(&fd);
257 err = hfs_brec_remove(&fd);
262 mark_inode_dirty(dir);
269 int hfsplus_rename_cat(u32 cnid,
270 struct inode *src_dir, struct qstr *src_name,
271 struct inode *dst_dir, struct qstr *dst_name)
273 struct super_block *sb;
274 struct hfs_find_data src_fd, dst_fd;
275 hfsplus_cat_entry entry;
276 int entry_size, type;
279 dprint(DBG_CAT_MOD, "rename_cat: %u - %lu,%s - %lu,%s\n", cnid, src_dir->i_ino, src_name->name,
280 dst_dir->i_ino, dst_name->name);
282 hfs_find_init(HFSPLUS_SB(sb).cat_tree, &src_fd);
285 /* find the old dir entry and read the data */
286 hfsplus_cat_build_key(src_fd.search_key, src_dir->i_ino, src_name);
287 err = hfs_brec_find(&src_fd);
291 hfs_bnode_read(src_fd.bnode, &entry, src_fd.entryoffset,
294 /* create new dir entry with the data from the old entry */
295 hfsplus_cat_build_key(dst_fd.search_key, dst_dir->i_ino, dst_name);
296 err = hfs_brec_find(&dst_fd);
297 if (err != -ENOENT) {
303 err = hfs_brec_insert(&dst_fd, &entry, src_fd.entrylength);
307 mark_inode_dirty(dst_dir);
309 /* finally remove the old entry */
310 hfsplus_cat_build_key(src_fd.search_key, src_dir->i_ino, src_name);
311 err = hfs_brec_find(&src_fd);
314 err = hfs_brec_remove(&src_fd);
318 mark_inode_dirty(src_dir);
320 /* remove old thread entry */
321 hfsplus_cat_build_key(src_fd.search_key, cnid, NULL);
322 err = hfs_brec_find(&src_fd);
325 type = hfs_bnode_read_u16(src_fd.bnode, src_fd.entryoffset);
326 err = hfs_brec_remove(&src_fd);
330 /* create new thread entry */
331 hfsplus_cat_build_key(dst_fd.search_key, cnid, NULL);
332 entry_size = hfsplus_fill_cat_thread(&entry, type, dst_dir->i_ino, dst_name);
333 err = hfs_brec_find(&dst_fd);
334 if (err != -ENOENT) {
339 err = hfs_brec_insert(&dst_fd, &entry, entry_size);
341 hfs_bnode_put(dst_fd.bnode);
342 hfs_find_exit(&src_fd);