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 key->cat.name.length = 0;
41 key->key_len = cpu_to_be16(6 + 2 * len);
44 static void hfsplus_cat_build_key_uni(hfsplus_btree_key *key, u32 parent,
45 struct hfsplus_unistr *name)
49 ustrlen = be16_to_cpu(name->length);
50 key->cat.parent = cpu_to_be32(parent);
51 key->cat.name.length = cpu_to_be16(ustrlen);
53 memcpy(key->cat.name.unicode, name->unicode, ustrlen);
54 key->key_len = cpu_to_be16(6 + ustrlen);
57 static void hfsplus_set_perms(struct inode *inode, struct hfsplus_perm *perms)
59 if (inode->i_flags & S_IMMUTABLE)
60 perms->rootflags |= HFSPLUS_FLG_IMMUTABLE;
62 perms->rootflags &= ~HFSPLUS_FLG_IMMUTABLE;
63 if (inode->i_flags & S_APPEND)
64 perms->rootflags |= HFSPLUS_FLG_APPEND;
66 perms->rootflags &= ~HFSPLUS_FLG_APPEND;
67 HFSPLUS_I(inode).rootflags = perms->rootflags;
68 HFSPLUS_I(inode).userflags = perms->userflags;
69 perms->mode = cpu_to_be16(inode->i_mode);
70 perms->owner = cpu_to_be32(inode->i_uid);
71 perms->group = cpu_to_be32(inode->i_gid);
74 static int hfsplus_cat_build_record(hfsplus_cat_entry *entry, u32 cnid, struct inode *inode)
76 if (S_ISDIR(inode->i_mode)) {
77 struct hfsplus_cat_folder *folder;
79 folder = &entry->folder;
80 memset(folder, 0, sizeof(*folder));
81 folder->type = cpu_to_be16(HFSPLUS_FOLDER);
82 folder->id = cpu_to_be32(inode->i_ino);
83 folder->create_date = folder->content_mod_date =
84 folder->attribute_mod_date = folder->access_date = hfsp_now2mt();
85 hfsplus_set_perms(inode, &folder->permissions);
86 if (inode == HFSPLUS_SB(inode->i_sb).hidden_dir)
87 /* invisible and namelocked */
88 folder->user_info.frFlags = cpu_to_be16(0x5000);
89 return sizeof(*folder);
91 struct hfsplus_cat_file *file;
94 memset(file, 0, sizeof(*file));
95 file->type = cpu_to_be16(HFSPLUS_FILE);
96 file->flags = cpu_to_be16(HFSPLUS_FILE_THREAD_EXISTS);
97 file->id = cpu_to_be32(cnid);
98 file->create_date = file->content_mod_date =
99 file->attribute_mod_date = file->access_date = hfsp_now2mt();
100 if (cnid == inode->i_ino) {
101 hfsplus_set_perms(inode, &file->permissions);
102 file->user_info.fdType = cpu_to_be32(HFSPLUS_SB(inode->i_sb).type);
103 file->user_info.fdCreator = cpu_to_be32(HFSPLUS_SB(inode->i_sb).creator);
104 if ((file->permissions.rootflags | file->permissions.userflags) & HFSPLUS_FLG_IMMUTABLE)
105 file->flags |= cpu_to_be16(HFSPLUS_FILE_LOCKED);
107 file->user_info.fdType = cpu_to_be32(HFSP_HARDLINK_TYPE);
108 file->user_info.fdCreator = cpu_to_be32(HFSP_HFSPLUS_CREATOR);
109 file->user_info.fdFlags = cpu_to_be16(0x100);
110 file->permissions.dev = cpu_to_be32(HFSPLUS_I(inode).dev);
112 return sizeof(*file);
116 static int hfsplus_fill_cat_thread(hfsplus_cat_entry *entry, int type,
117 u32 parentid, struct qstr *str)
119 entry->type = cpu_to_be16(type);
120 entry->thread.reserved = 0;
121 entry->thread.parentID = cpu_to_be32(parentid);
122 hfsplus_asc2uni(&entry->thread.nodeName, str->name, str->len);
123 return 10 + be16_to_cpu(entry->thread.nodeName.length) * 2;
126 /* Try to get a catalog entry for given catalog id */
127 int hfsplus_find_cat(struct super_block *sb, u32 cnid,
128 struct hfs_find_data *fd)
130 hfsplus_cat_entry tmp;
134 hfsplus_cat_build_key(fd->search_key, cnid, NULL);
135 err = hfs_brec_read(fd, &tmp, sizeof(hfsplus_cat_entry));
139 type = be16_to_cpu(tmp.type);
140 if (type != HFSPLUS_FOLDER_THREAD && type != HFSPLUS_FILE_THREAD) {
141 printk("HFS+-fs: Found bad thread record in catalog\n");
145 hfsplus_cat_build_key_uni(fd->search_key, be32_to_cpu(tmp.thread.parentID),
146 &tmp.thread.nodeName);
147 return hfs_brec_find(fd);
150 int hfsplus_create_cat(u32 cnid, struct inode *dir, struct qstr *str, struct inode *inode)
152 struct hfs_find_data fd;
153 struct super_block *sb;
154 hfsplus_cat_entry entry;
158 dprint(DBG_CAT_MOD, "create_cat: %s,%u(%d)\n", str->name, cnid, inode->i_nlink);
160 hfs_find_init(HFSPLUS_SB(sb).cat_tree, &fd);
162 hfsplus_cat_build_key(fd.search_key, cnid, NULL);
163 entry_size = hfsplus_fill_cat_thread(&entry, S_ISDIR(inode->i_mode) ?
164 HFSPLUS_FOLDER_THREAD : HFSPLUS_FILE_THREAD,
166 err = hfs_brec_find(&fd);
167 if (err != -ENOENT) {
172 err = hfs_brec_insert(&fd, &entry, entry_size);
176 hfsplus_cat_build_key(fd.search_key, dir->i_ino, str);
177 entry_size = hfsplus_cat_build_record(&entry, cnid, inode);
178 err = hfs_brec_find(&fd);
179 if (err != -ENOENT) {
185 err = hfs_brec_insert(&fd, &entry, entry_size);
190 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
191 mark_inode_dirty(dir);
196 hfsplus_cat_build_key(fd.search_key, cnid, NULL);
197 if (!hfs_brec_find(&fd))
198 hfs_brec_remove(&fd);
204 int hfsplus_delete_cat(u32 cnid, struct inode *dir, struct qstr *str)
206 struct super_block *sb;
207 struct hfs_find_data fd;
208 struct hfsplus_fork_raw fork;
209 struct list_head *pos;
213 dprint(DBG_CAT_MOD, "delete_cat: %s,%u\n", str ? str->name : NULL, cnid);
215 hfs_find_init(HFSPLUS_SB(sb).cat_tree, &fd);
220 hfsplus_cat_build_key(fd.search_key, cnid, NULL);
221 err = hfs_brec_find(&fd);
225 off = fd.entryoffset + offsetof(struct hfsplus_cat_thread, nodeName);
226 fd.search_key->cat.parent = cpu_to_be32(dir->i_ino);
227 hfs_bnode_read(fd.bnode, &fd.search_key->cat.name.length, off, 2);
228 len = be16_to_cpu(fd.search_key->cat.name.length) * 2;
229 hfs_bnode_read(fd.bnode, &fd.search_key->cat.name.unicode, off + 2, len);
230 fd.search_key->key_len = cpu_to_be16(6 + len);
232 hfsplus_cat_build_key(fd.search_key, dir->i_ino, str);
234 err = hfs_brec_find(&fd);
238 type = hfs_bnode_read_u16(fd.bnode, fd.entryoffset);
239 if (type == HFSPLUS_FILE) {
241 off = fd.entryoffset + offsetof(hfsplus_cat_file, data_fork);
242 hfs_bnode_read(fd.bnode, &fork, off, sizeof(fork));
243 hfsplus_free_fork(sb, cnid, &fork, HFSPLUS_TYPE_DATA);
246 off = fd.entryoffset + offsetof(struct hfsplus_cat_file, rsrc_fork);
247 hfs_bnode_read(fd.bnode, &fork, off, sizeof(fork));
248 hfsplus_free_fork(sb, cnid, &fork, HFSPLUS_TYPE_RSRC);
251 list_for_each(pos, &HFSPLUS_I(dir).open_dir_list) {
252 struct hfsplus_readdir_data *rd =
253 list_entry(pos, struct hfsplus_readdir_data, list);
254 if (fd.tree->keycmp(fd.search_key, (void *)&rd->key) < 0)
258 err = hfs_brec_remove(&fd);
262 hfsplus_cat_build_key(fd.search_key, cnid, NULL);
263 err = hfs_brec_find(&fd);
267 err = hfs_brec_remove(&fd);
272 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
273 mark_inode_dirty(dir);
280 int hfsplus_rename_cat(u32 cnid,
281 struct inode *src_dir, struct qstr *src_name,
282 struct inode *dst_dir, struct qstr *dst_name)
284 struct super_block *sb;
285 struct hfs_find_data src_fd, dst_fd;
286 hfsplus_cat_entry entry;
287 int entry_size, type;
290 dprint(DBG_CAT_MOD, "rename_cat: %u - %lu,%s - %lu,%s\n", cnid, src_dir->i_ino, src_name->name,
291 dst_dir->i_ino, dst_name->name);
293 hfs_find_init(HFSPLUS_SB(sb).cat_tree, &src_fd);
296 /* find the old dir entry and read the data */
297 hfsplus_cat_build_key(src_fd.search_key, src_dir->i_ino, src_name);
298 err = hfs_brec_find(&src_fd);
302 hfs_bnode_read(src_fd.bnode, &entry, src_fd.entryoffset,
305 /* create new dir entry with the data from the old entry */
306 hfsplus_cat_build_key(dst_fd.search_key, dst_dir->i_ino, dst_name);
307 err = hfs_brec_find(&dst_fd);
308 if (err != -ENOENT) {
314 err = hfs_brec_insert(&dst_fd, &entry, src_fd.entrylength);
318 dst_dir->i_mtime = dst_dir->i_ctime = CURRENT_TIME;
319 mark_inode_dirty(dst_dir);
321 /* finally remove the old entry */
322 hfsplus_cat_build_key(src_fd.search_key, src_dir->i_ino, src_name);
323 err = hfs_brec_find(&src_fd);
326 err = hfs_brec_remove(&src_fd);
330 src_dir->i_mtime = src_dir->i_ctime = CURRENT_TIME;
331 mark_inode_dirty(src_dir);
333 /* remove old thread entry */
334 hfsplus_cat_build_key(src_fd.search_key, cnid, NULL);
335 err = hfs_brec_find(&src_fd);
338 type = hfs_bnode_read_u16(src_fd.bnode, src_fd.entryoffset);
339 err = hfs_brec_remove(&src_fd);
343 /* create new thread entry */
344 hfsplus_cat_build_key(dst_fd.search_key, cnid, NULL);
345 entry_size = hfsplus_fill_cat_thread(&entry, type, dst_dir->i_ino, dst_name);
346 err = hfs_brec_find(&dst_fd);
347 if (err != -ENOENT) {
352 err = hfs_brec_insert(&dst_fd, &entry, entry_size);
354 hfs_bnode_put(dst_fd.bnode);
355 hfs_find_exit(&src_fd);