[CPUFREQ] ondemand governor automatic downscaling
[linux-2.6/mini2440.git] / fs / hfsplus / catalog.c
blob94712790c8b3f2414e4516fa4f291c19f55a2ff9
1 /*
2 * linux/fs/hfsplus/catalog.c
4 * Copyright (C) 2001
5 * Brad Boyer (flar@allandria.com)
6 * (C) 2003 Ardis Technologies <roman@ardistech.com>
8 * Handling of catalog records
9 */
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)
18 __be32 k1p, k2p;
20 k1p = k1->cat.parent;
21 k2p = k2->cat.parent;
22 if (k1p != k2p)
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(struct super_block *sb, hfsplus_btree_key *key,
29 u32 parent, struct qstr *str)
31 int len;
33 key->cat.parent = cpu_to_be32(parent);
34 if (str) {
35 hfsplus_asc2uni(sb, &key->cat.name, str->name, str->len);
36 len = be16_to_cpu(key->cat.name.length);
37 } else {
38 key->cat.name.length = 0;
39 len = 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)
47 int ustrlen;
49 ustrlen = be16_to_cpu(name->length);
50 key->cat.parent = cpu_to_be32(parent);
51 key->cat.name.length = cpu_to_be16(ustrlen);
52 ustrlen *= 2;
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;
61 else
62 perms->rootflags &= ~HFSPLUS_FLG_IMMUTABLE;
63 if (inode->i_flags & S_APPEND)
64 perms->rootflags |= HFSPLUS_FLG_APPEND;
65 else
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);
90 } else {
91 struct hfsplus_cat_file *file;
93 file = &entry->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);
106 } else {
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(struct super_block *sb,
117 hfsplus_cat_entry *entry, int type,
118 u32 parentid, struct qstr *str)
120 entry->type = cpu_to_be16(type);
121 entry->thread.reserved = 0;
122 entry->thread.parentID = cpu_to_be32(parentid);
123 hfsplus_asc2uni(sb, &entry->thread.nodeName, str->name, str->len);
124 return 10 + be16_to_cpu(entry->thread.nodeName.length) * 2;
127 /* Try to get a catalog entry for given catalog id */
128 int hfsplus_find_cat(struct super_block *sb, u32 cnid,
129 struct hfs_find_data *fd)
131 hfsplus_cat_entry tmp;
132 int err;
133 u16 type;
135 hfsplus_cat_build_key(sb, fd->search_key, cnid, NULL);
136 err = hfs_brec_read(fd, &tmp, sizeof(hfsplus_cat_entry));
137 if (err)
138 return err;
140 type = be16_to_cpu(tmp.type);
141 if (type != HFSPLUS_FOLDER_THREAD && type != HFSPLUS_FILE_THREAD) {
142 printk("HFS+-fs: Found bad thread record in catalog\n");
143 return -EIO;
146 hfsplus_cat_build_key_uni(fd->search_key, be32_to_cpu(tmp.thread.parentID),
147 &tmp.thread.nodeName);
148 return hfs_brec_find(fd);
151 int hfsplus_create_cat(u32 cnid, struct inode *dir, struct qstr *str, struct inode *inode)
153 struct hfs_find_data fd;
154 struct super_block *sb;
155 hfsplus_cat_entry entry;
156 int entry_size;
157 int err;
159 dprint(DBG_CAT_MOD, "create_cat: %s,%u(%d)\n", str->name, cnid, inode->i_nlink);
160 sb = dir->i_sb;
161 hfs_find_init(HFSPLUS_SB(sb).cat_tree, &fd);
163 hfsplus_cat_build_key(sb, fd.search_key, cnid, NULL);
164 entry_size = hfsplus_fill_cat_thread(sb, &entry, S_ISDIR(inode->i_mode) ?
165 HFSPLUS_FOLDER_THREAD : HFSPLUS_FILE_THREAD,
166 dir->i_ino, str);
167 err = hfs_brec_find(&fd);
168 if (err != -ENOENT) {
169 if (!err)
170 err = -EEXIST;
171 goto err2;
173 err = hfs_brec_insert(&fd, &entry, entry_size);
174 if (err)
175 goto err2;
177 hfsplus_cat_build_key(sb, fd.search_key, dir->i_ino, str);
178 entry_size = hfsplus_cat_build_record(&entry, cnid, inode);
179 err = hfs_brec_find(&fd);
180 if (err != -ENOENT) {
181 /* panic? */
182 if (!err)
183 err = -EEXIST;
184 goto err1;
186 err = hfs_brec_insert(&fd, &entry, entry_size);
187 if (err)
188 goto err1;
190 dir->i_size++;
191 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
192 mark_inode_dirty(dir);
193 hfs_find_exit(&fd);
194 return 0;
196 err1:
197 hfsplus_cat_build_key(sb, fd.search_key, cnid, NULL);
198 if (!hfs_brec_find(&fd))
199 hfs_brec_remove(&fd);
200 err2:
201 hfs_find_exit(&fd);
202 return err;
205 int hfsplus_delete_cat(u32 cnid, struct inode *dir, struct qstr *str)
207 struct super_block *sb;
208 struct hfs_find_data fd;
209 struct hfsplus_fork_raw fork;
210 struct list_head *pos;
211 int err, off;
212 u16 type;
214 dprint(DBG_CAT_MOD, "delete_cat: %s,%u\n", str ? str->name : NULL, cnid);
215 sb = dir->i_sb;
216 hfs_find_init(HFSPLUS_SB(sb).cat_tree, &fd);
218 if (!str) {
219 int len;
221 hfsplus_cat_build_key(sb, fd.search_key, cnid, NULL);
222 err = hfs_brec_find(&fd);
223 if (err)
224 goto out;
226 off = fd.entryoffset + offsetof(struct hfsplus_cat_thread, nodeName);
227 fd.search_key->cat.parent = cpu_to_be32(dir->i_ino);
228 hfs_bnode_read(fd.bnode, &fd.search_key->cat.name.length, off, 2);
229 len = be16_to_cpu(fd.search_key->cat.name.length) * 2;
230 hfs_bnode_read(fd.bnode, &fd.search_key->cat.name.unicode, off + 2, len);
231 fd.search_key->key_len = cpu_to_be16(6 + len);
232 } else
233 hfsplus_cat_build_key(sb, fd.search_key, dir->i_ino, str);
235 err = hfs_brec_find(&fd);
236 if (err)
237 goto out;
239 type = hfs_bnode_read_u16(fd.bnode, fd.entryoffset);
240 if (type == HFSPLUS_FILE) {
241 #if 0
242 off = fd.entryoffset + offsetof(hfsplus_cat_file, data_fork);
243 hfs_bnode_read(fd.bnode, &fork, off, sizeof(fork));
244 hfsplus_free_fork(sb, cnid, &fork, HFSPLUS_TYPE_DATA);
245 #endif
247 off = fd.entryoffset + offsetof(struct hfsplus_cat_file, rsrc_fork);
248 hfs_bnode_read(fd.bnode, &fork, off, sizeof(fork));
249 hfsplus_free_fork(sb, cnid, &fork, HFSPLUS_TYPE_RSRC);
252 list_for_each(pos, &HFSPLUS_I(dir).open_dir_list) {
253 struct hfsplus_readdir_data *rd =
254 list_entry(pos, struct hfsplus_readdir_data, list);
255 if (fd.tree->keycmp(fd.search_key, (void *)&rd->key) < 0)
256 rd->file->f_pos--;
259 err = hfs_brec_remove(&fd);
260 if (err)
261 goto out;
263 hfsplus_cat_build_key(sb, fd.search_key, cnid, NULL);
264 err = hfs_brec_find(&fd);
265 if (err)
266 goto out;
268 err = hfs_brec_remove(&fd);
269 if (err)
270 goto out;
272 dir->i_size--;
273 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
274 mark_inode_dirty(dir);
275 out:
276 hfs_find_exit(&fd);
278 return err;
281 int hfsplus_rename_cat(u32 cnid,
282 struct inode *src_dir, struct qstr *src_name,
283 struct inode *dst_dir, struct qstr *dst_name)
285 struct super_block *sb;
286 struct hfs_find_data src_fd, dst_fd;
287 hfsplus_cat_entry entry;
288 int entry_size, type;
289 int err = 0;
291 dprint(DBG_CAT_MOD, "rename_cat: %u - %lu,%s - %lu,%s\n", cnid, src_dir->i_ino, src_name->name,
292 dst_dir->i_ino, dst_name->name);
293 sb = src_dir->i_sb;
294 hfs_find_init(HFSPLUS_SB(sb).cat_tree, &src_fd);
295 dst_fd = src_fd;
297 /* find the old dir entry and read the data */
298 hfsplus_cat_build_key(sb, src_fd.search_key, src_dir->i_ino, src_name);
299 err = hfs_brec_find(&src_fd);
300 if (err)
301 goto out;
303 hfs_bnode_read(src_fd.bnode, &entry, src_fd.entryoffset,
304 src_fd.entrylength);
306 /* create new dir entry with the data from the old entry */
307 hfsplus_cat_build_key(sb, dst_fd.search_key, dst_dir->i_ino, dst_name);
308 err = hfs_brec_find(&dst_fd);
309 if (err != -ENOENT) {
310 if (!err)
311 err = -EEXIST;
312 goto out;
315 err = hfs_brec_insert(&dst_fd, &entry, src_fd.entrylength);
316 if (err)
317 goto out;
318 dst_dir->i_size++;
319 dst_dir->i_mtime = dst_dir->i_ctime = CURRENT_TIME_SEC;
320 mark_inode_dirty(dst_dir);
322 /* finally remove the old entry */
323 hfsplus_cat_build_key(sb, src_fd.search_key, src_dir->i_ino, src_name);
324 err = hfs_brec_find(&src_fd);
325 if (err)
326 goto out;
327 err = hfs_brec_remove(&src_fd);
328 if (err)
329 goto out;
330 src_dir->i_size--;
331 src_dir->i_mtime = src_dir->i_ctime = CURRENT_TIME_SEC;
332 mark_inode_dirty(src_dir);
334 /* remove old thread entry */
335 hfsplus_cat_build_key(sb, src_fd.search_key, cnid, NULL);
336 err = hfs_brec_find(&src_fd);
337 if (err)
338 goto out;
339 type = hfs_bnode_read_u16(src_fd.bnode, src_fd.entryoffset);
340 err = hfs_brec_remove(&src_fd);
341 if (err)
342 goto out;
344 /* create new thread entry */
345 hfsplus_cat_build_key(sb, dst_fd.search_key, cnid, NULL);
346 entry_size = hfsplus_fill_cat_thread(sb, &entry, type, dst_dir->i_ino, dst_name);
347 err = hfs_brec_find(&dst_fd);
348 if (err != -ENOENT) {
349 if (!err)
350 err = -EEXIST;
351 goto out;
353 err = hfs_brec_insert(&dst_fd, &entry, entry_size);
354 out:
355 hfs_bnode_put(dst_fd.bnode);
356 hfs_find_exit(&src_fd);
357 return err;