blob: 662d176856d8333fd7ca904544d3904c4c2749b3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/hfsplus/catalog.c
3 *
4 * Copyright (C) 2001
5 * Brad Boyer (flar@allandria.com)
6 * (C) 2003 Ardis Technologies <roman@ardistech.com>
7 *
8 * Handling of catalog records
9 */
10
11#include <linux/sched.h>
12
13#include "hfsplus_fs.h"
14#include "hfsplus_raw.h"
15
David Elliott2179d372006-01-18 17:43:08 -080016int hfsplus_cat_case_cmp_key(const hfsplus_btree_key *k1,
17 const hfsplus_btree_key *k2)
Linus Torvalds1da177e2005-04-16 15:20:36 -070018{
19 __be32 k1p, k2p;
20
21 k1p = k1->cat.parent;
22 k2p = k2->cat.parent;
23 if (k1p != k2p)
24 return be32_to_cpu(k1p) < be32_to_cpu(k2p) ? -1 : 1;
25
David Elliott2179d372006-01-18 17:43:08 -080026 return hfsplus_strcasecmp(&k1->cat.name, &k2->cat.name);
27}
28
29int hfsplus_cat_bin_cmp_key(const hfsplus_btree_key *k1,
30 const hfsplus_btree_key *k2)
31{
32 __be32 k1p, k2p;
33
34 k1p = k1->cat.parent;
35 k2p = k2->cat.parent;
36 if (k1p != k2p)
37 return be32_to_cpu(k1p) < be32_to_cpu(k2p) ? -1 : 1;
38
39 return hfsplus_strcmp(&k1->cat.name, &k2->cat.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040}
41
42void hfsplus_cat_build_key(struct super_block *sb, hfsplus_btree_key *key,
43 u32 parent, struct qstr *str)
44{
45 int len;
46
47 key->cat.parent = cpu_to_be32(parent);
48 if (str) {
49 hfsplus_asc2uni(sb, &key->cat.name, str->name, str->len);
50 len = be16_to_cpu(key->cat.name.length);
51 } else {
52 key->cat.name.length = 0;
53 len = 0;
54 }
55 key->key_len = cpu_to_be16(6 + 2 * len);
56}
57
58static void hfsplus_cat_build_key_uni(hfsplus_btree_key *key, u32 parent,
59 struct hfsplus_unistr *name)
60{
61 int ustrlen;
62
63 ustrlen = be16_to_cpu(name->length);
64 key->cat.parent = cpu_to_be32(parent);
65 key->cat.name.length = cpu_to_be16(ustrlen);
66 ustrlen *= 2;
67 memcpy(key->cat.name.unicode, name->unicode, ustrlen);
68 key->key_len = cpu_to_be16(6 + ustrlen);
69}
70
71static void hfsplus_set_perms(struct inode *inode, struct hfsplus_perm *perms)
72{
73 if (inode->i_flags & S_IMMUTABLE)
74 perms->rootflags |= HFSPLUS_FLG_IMMUTABLE;
75 else
76 perms->rootflags &= ~HFSPLUS_FLG_IMMUTABLE;
77 if (inode->i_flags & S_APPEND)
78 perms->rootflags |= HFSPLUS_FLG_APPEND;
79 else
80 perms->rootflags &= ~HFSPLUS_FLG_APPEND;
81 HFSPLUS_I(inode).rootflags = perms->rootflags;
82 HFSPLUS_I(inode).userflags = perms->userflags;
83 perms->mode = cpu_to_be16(inode->i_mode);
84 perms->owner = cpu_to_be32(inode->i_uid);
85 perms->group = cpu_to_be32(inode->i_gid);
86}
87
88static int hfsplus_cat_build_record(hfsplus_cat_entry *entry, u32 cnid, struct inode *inode)
89{
90 if (S_ISDIR(inode->i_mode)) {
91 struct hfsplus_cat_folder *folder;
92
93 folder = &entry->folder;
94 memset(folder, 0, sizeof(*folder));
95 folder->type = cpu_to_be16(HFSPLUS_FOLDER);
96 folder->id = cpu_to_be32(inode->i_ino);
97 folder->create_date = folder->content_mod_date =
98 folder->attribute_mod_date = folder->access_date = hfsp_now2mt();
99 hfsplus_set_perms(inode, &folder->permissions);
100 if (inode == HFSPLUS_SB(inode->i_sb).hidden_dir)
101 /* invisible and namelocked */
102 folder->user_info.frFlags = cpu_to_be16(0x5000);
103 return sizeof(*folder);
104 } else {
105 struct hfsplus_cat_file *file;
106
107 file = &entry->file;
108 memset(file, 0, sizeof(*file));
109 file->type = cpu_to_be16(HFSPLUS_FILE);
110 file->flags = cpu_to_be16(HFSPLUS_FILE_THREAD_EXISTS);
111 file->id = cpu_to_be32(cnid);
112 file->create_date = file->content_mod_date =
113 file->attribute_mod_date = file->access_date = hfsp_now2mt();
114 if (cnid == inode->i_ino) {
115 hfsplus_set_perms(inode, &file->permissions);
116 file->user_info.fdType = cpu_to_be32(HFSPLUS_SB(inode->i_sb).type);
117 file->user_info.fdCreator = cpu_to_be32(HFSPLUS_SB(inode->i_sb).creator);
118 if ((file->permissions.rootflags | file->permissions.userflags) & HFSPLUS_FLG_IMMUTABLE)
119 file->flags |= cpu_to_be16(HFSPLUS_FILE_LOCKED);
120 } else {
121 file->user_info.fdType = cpu_to_be32(HFSP_HARDLINK_TYPE);
122 file->user_info.fdCreator = cpu_to_be32(HFSP_HFSPLUS_CREATOR);
123 file->user_info.fdFlags = cpu_to_be16(0x100);
124 file->permissions.dev = cpu_to_be32(HFSPLUS_I(inode).dev);
125 }
126 return sizeof(*file);
127 }
128}
129
130static int hfsplus_fill_cat_thread(struct super_block *sb,
131 hfsplus_cat_entry *entry, int type,
132 u32 parentid, struct qstr *str)
133{
134 entry->type = cpu_to_be16(type);
135 entry->thread.reserved = 0;
136 entry->thread.parentID = cpu_to_be32(parentid);
137 hfsplus_asc2uni(sb, &entry->thread.nodeName, str->name, str->len);
138 return 10 + be16_to_cpu(entry->thread.nodeName.length) * 2;
139}
140
141/* Try to get a catalog entry for given catalog id */
142int hfsplus_find_cat(struct super_block *sb, u32 cnid,
143 struct hfs_find_data *fd)
144{
145 hfsplus_cat_entry tmp;
146 int err;
147 u16 type;
148
149 hfsplus_cat_build_key(sb, fd->search_key, cnid, NULL);
150 err = hfs_brec_read(fd, &tmp, sizeof(hfsplus_cat_entry));
151 if (err)
152 return err;
153
154 type = be16_to_cpu(tmp.type);
155 if (type != HFSPLUS_FOLDER_THREAD && type != HFSPLUS_FILE_THREAD) {
Roman Zippel634725a2006-01-18 17:43:05 -0800156 printk(KERN_ERR "hfs: found bad thread record in catalog\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 return -EIO;
158 }
159
160 hfsplus_cat_build_key_uni(fd->search_key, be32_to_cpu(tmp.thread.parentID),
161 &tmp.thread.nodeName);
162 return hfs_brec_find(fd);
163}
164
165int hfsplus_create_cat(u32 cnid, struct inode *dir, struct qstr *str, struct inode *inode)
166{
167 struct hfs_find_data fd;
168 struct super_block *sb;
169 hfsplus_cat_entry entry;
170 int entry_size;
171 int err;
172
173 dprint(DBG_CAT_MOD, "create_cat: %s,%u(%d)\n", str->name, cnid, inode->i_nlink);
174 sb = dir->i_sb;
175 hfs_find_init(HFSPLUS_SB(sb).cat_tree, &fd);
176
177 hfsplus_cat_build_key(sb, fd.search_key, cnid, NULL);
178 entry_size = hfsplus_fill_cat_thread(sb, &entry, S_ISDIR(inode->i_mode) ?
179 HFSPLUS_FOLDER_THREAD : HFSPLUS_FILE_THREAD,
180 dir->i_ino, str);
181 err = hfs_brec_find(&fd);
182 if (err != -ENOENT) {
183 if (!err)
184 err = -EEXIST;
185 goto err2;
186 }
187 err = hfs_brec_insert(&fd, &entry, entry_size);
188 if (err)
189 goto err2;
190
191 hfsplus_cat_build_key(sb, fd.search_key, dir->i_ino, str);
192 entry_size = hfsplus_cat_build_record(&entry, cnid, inode);
193 err = hfs_brec_find(&fd);
194 if (err != -ENOENT) {
195 /* panic? */
196 if (!err)
197 err = -EEXIST;
198 goto err1;
199 }
200 err = hfs_brec_insert(&fd, &entry, entry_size);
201 if (err)
202 goto err1;
203
204 dir->i_size++;
205 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
206 mark_inode_dirty(dir);
207 hfs_find_exit(&fd);
208 return 0;
209
210err1:
211 hfsplus_cat_build_key(sb, fd.search_key, cnid, NULL);
212 if (!hfs_brec_find(&fd))
213 hfs_brec_remove(&fd);
214err2:
215 hfs_find_exit(&fd);
216 return err;
217}
218
219int hfsplus_delete_cat(u32 cnid, struct inode *dir, struct qstr *str)
220{
221 struct super_block *sb;
222 struct hfs_find_data fd;
223 struct hfsplus_fork_raw fork;
224 struct list_head *pos;
225 int err, off;
226 u16 type;
227
228 dprint(DBG_CAT_MOD, "delete_cat: %s,%u\n", str ? str->name : NULL, cnid);
229 sb = dir->i_sb;
230 hfs_find_init(HFSPLUS_SB(sb).cat_tree, &fd);
231
232 if (!str) {
233 int len;
234
235 hfsplus_cat_build_key(sb, fd.search_key, cnid, NULL);
236 err = hfs_brec_find(&fd);
237 if (err)
238 goto out;
239
240 off = fd.entryoffset + offsetof(struct hfsplus_cat_thread, nodeName);
241 fd.search_key->cat.parent = cpu_to_be32(dir->i_ino);
242 hfs_bnode_read(fd.bnode, &fd.search_key->cat.name.length, off, 2);
243 len = be16_to_cpu(fd.search_key->cat.name.length) * 2;
244 hfs_bnode_read(fd.bnode, &fd.search_key->cat.name.unicode, off + 2, len);
245 fd.search_key->key_len = cpu_to_be16(6 + len);
246 } else
247 hfsplus_cat_build_key(sb, fd.search_key, dir->i_ino, str);
248
249 err = hfs_brec_find(&fd);
250 if (err)
251 goto out;
252
253 type = hfs_bnode_read_u16(fd.bnode, fd.entryoffset);
254 if (type == HFSPLUS_FILE) {
255#if 0
256 off = fd.entryoffset + offsetof(hfsplus_cat_file, data_fork);
257 hfs_bnode_read(fd.bnode, &fork, off, sizeof(fork));
258 hfsplus_free_fork(sb, cnid, &fork, HFSPLUS_TYPE_DATA);
259#endif
260
261 off = fd.entryoffset + offsetof(struct hfsplus_cat_file, rsrc_fork);
262 hfs_bnode_read(fd.bnode, &fork, off, sizeof(fork));
263 hfsplus_free_fork(sb, cnid, &fork, HFSPLUS_TYPE_RSRC);
264 }
265
266 list_for_each(pos, &HFSPLUS_I(dir).open_dir_list) {
267 struct hfsplus_readdir_data *rd =
268 list_entry(pos, struct hfsplus_readdir_data, list);
269 if (fd.tree->keycmp(fd.search_key, (void *)&rd->key) < 0)
270 rd->file->f_pos--;
271 }
272
273 err = hfs_brec_remove(&fd);
274 if (err)
275 goto out;
276
277 hfsplus_cat_build_key(sb, fd.search_key, cnid, NULL);
278 err = hfs_brec_find(&fd);
279 if (err)
280 goto out;
281
282 err = hfs_brec_remove(&fd);
283 if (err)
284 goto out;
285
286 dir->i_size--;
287 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
288 mark_inode_dirty(dir);
289out:
290 hfs_find_exit(&fd);
291
292 return err;
293}
294
295int hfsplus_rename_cat(u32 cnid,
296 struct inode *src_dir, struct qstr *src_name,
297 struct inode *dst_dir, struct qstr *dst_name)
298{
299 struct super_block *sb;
300 struct hfs_find_data src_fd, dst_fd;
301 hfsplus_cat_entry entry;
302 int entry_size, type;
303 int err = 0;
304
305 dprint(DBG_CAT_MOD, "rename_cat: %u - %lu,%s - %lu,%s\n", cnid, src_dir->i_ino, src_name->name,
306 dst_dir->i_ino, dst_name->name);
307 sb = src_dir->i_sb;
308 hfs_find_init(HFSPLUS_SB(sb).cat_tree, &src_fd);
309 dst_fd = src_fd;
310
311 /* find the old dir entry and read the data */
312 hfsplus_cat_build_key(sb, src_fd.search_key, src_dir->i_ino, src_name);
313 err = hfs_brec_find(&src_fd);
314 if (err)
315 goto out;
316
317 hfs_bnode_read(src_fd.bnode, &entry, src_fd.entryoffset,
318 src_fd.entrylength);
319
320 /* create new dir entry with the data from the old entry */
321 hfsplus_cat_build_key(sb, dst_fd.search_key, dst_dir->i_ino, dst_name);
322 err = hfs_brec_find(&dst_fd);
323 if (err != -ENOENT) {
324 if (!err)
325 err = -EEXIST;
326 goto out;
327 }
328
329 err = hfs_brec_insert(&dst_fd, &entry, src_fd.entrylength);
330 if (err)
331 goto out;
332 dst_dir->i_size++;
333 dst_dir->i_mtime = dst_dir->i_ctime = CURRENT_TIME_SEC;
334 mark_inode_dirty(dst_dir);
335
336 /* finally remove the old entry */
337 hfsplus_cat_build_key(sb, src_fd.search_key, src_dir->i_ino, src_name);
338 err = hfs_brec_find(&src_fd);
339 if (err)
340 goto out;
341 err = hfs_brec_remove(&src_fd);
342 if (err)
343 goto out;
344 src_dir->i_size--;
345 src_dir->i_mtime = src_dir->i_ctime = CURRENT_TIME_SEC;
346 mark_inode_dirty(src_dir);
347
348 /* remove old thread entry */
349 hfsplus_cat_build_key(sb, src_fd.search_key, cnid, NULL);
350 err = hfs_brec_find(&src_fd);
351 if (err)
352 goto out;
353 type = hfs_bnode_read_u16(src_fd.bnode, src_fd.entryoffset);
354 err = hfs_brec_remove(&src_fd);
355 if (err)
356 goto out;
357
358 /* create new thread entry */
359 hfsplus_cat_build_key(sb, dst_fd.search_key, cnid, NULL);
360 entry_size = hfsplus_fill_cat_thread(sb, &entry, type, dst_dir->i_ino, dst_name);
361 err = hfs_brec_find(&dst_fd);
362 if (err != -ENOENT) {
363 if (!err)
364 err = -EEXIST;
365 goto out;
366 }
367 err = hfs_brec_insert(&dst_fd, &entry, entry_size);
368out:
369 hfs_bnode_put(dst_fd.bnode);
370 hfs_find_exit(&src_fd);
371 return err;
372}