blob: 75ac1e466f1c2a4d5a1ca305ccd9f452164f09cc [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
12#include "hfsplus_fs.h"
13#include "hfsplus_raw.h"
14
David Elliott2179d372006-01-18 17:43:08 -080015int hfsplus_cat_case_cmp_key(const hfsplus_btree_key *k1,
16 const hfsplus_btree_key *k2)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017{
18 __be32 k1p, k2p;
19
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;
24
David Elliott2179d372006-01-18 17:43:08 -080025 return hfsplus_strcasecmp(&k1->cat.name, &k2->cat.name);
26}
27
28int hfsplus_cat_bin_cmp_key(const hfsplus_btree_key *k1,
29 const hfsplus_btree_key *k2)
30{
31 __be32 k1p, k2p;
32
33 k1p = k1->cat.parent;
34 k2p = k2->cat.parent;
35 if (k1p != k2p)
36 return be32_to_cpu(k1p) < be32_to_cpu(k2p) ? -1 : 1;
37
38 return hfsplus_strcmp(&k1->cat.name, &k2->cat.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039}
40
41void hfsplus_cat_build_key(struct super_block *sb, hfsplus_btree_key *key,
42 u32 parent, struct qstr *str)
43{
44 int len;
45
46 key->cat.parent = cpu_to_be32(parent);
47 if (str) {
48 hfsplus_asc2uni(sb, &key->cat.name, str->name, str->len);
49 len = be16_to_cpu(key->cat.name.length);
50 } else {
51 key->cat.name.length = 0;
52 len = 0;
53 }
54 key->key_len = cpu_to_be16(6 + 2 * len);
55}
56
57static void hfsplus_cat_build_key_uni(hfsplus_btree_key *key, u32 parent,
58 struct hfsplus_unistr *name)
59{
60 int ustrlen;
61
62 ustrlen = be16_to_cpu(name->length);
63 key->cat.parent = cpu_to_be32(parent);
64 key->cat.name.length = cpu_to_be16(ustrlen);
65 ustrlen *= 2;
66 memcpy(key->cat.name.unicode, name->unicode, ustrlen);
67 key->key_len = cpu_to_be16(6 + ustrlen);
68}
69
70static void hfsplus_set_perms(struct inode *inode, struct hfsplus_perm *perms)
71{
72 if (inode->i_flags & S_IMMUTABLE)
73 perms->rootflags |= HFSPLUS_FLG_IMMUTABLE;
74 else
75 perms->rootflags &= ~HFSPLUS_FLG_IMMUTABLE;
76 if (inode->i_flags & S_APPEND)
77 perms->rootflags |= HFSPLUS_FLG_APPEND;
78 else
79 perms->rootflags &= ~HFSPLUS_FLG_APPEND;
80 HFSPLUS_I(inode).rootflags = perms->rootflags;
81 HFSPLUS_I(inode).userflags = perms->userflags;
82 perms->mode = cpu_to_be16(inode->i_mode);
83 perms->owner = cpu_to_be32(inode->i_uid);
84 perms->group = cpu_to_be32(inode->i_gid);
85}
86
87static int hfsplus_cat_build_record(hfsplus_cat_entry *entry, u32 cnid, struct inode *inode)
88{
Christoph Hellwigdd73a012010-10-01 05:42:59 +020089 struct hfsplus_sb_info *sbi = HFSPLUS_SB(inode->i_sb);
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 if (S_ISDIR(inode->i_mode)) {
92 struct hfsplus_cat_folder *folder;
93
94 folder = &entry->folder;
95 memset(folder, 0, sizeof(*folder));
96 folder->type = cpu_to_be16(HFSPLUS_FOLDER);
97 folder->id = cpu_to_be32(inode->i_ino);
Roman Zippel9a4cad92006-01-18 17:43:09 -080098 HFSPLUS_I(inode).create_date =
99 folder->create_date =
100 folder->content_mod_date =
101 folder->attribute_mod_date =
102 folder->access_date = hfsp_now2mt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 hfsplus_set_perms(inode, &folder->permissions);
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200104 if (inode == sbi->hidden_dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 /* invisible and namelocked */
106 folder->user_info.frFlags = cpu_to_be16(0x5000);
107 return sizeof(*folder);
108 } else {
109 struct hfsplus_cat_file *file;
110
111 file = &entry->file;
112 memset(file, 0, sizeof(*file));
113 file->type = cpu_to_be16(HFSPLUS_FILE);
114 file->flags = cpu_to_be16(HFSPLUS_FILE_THREAD_EXISTS);
115 file->id = cpu_to_be32(cnid);
Roman Zippel9a4cad92006-01-18 17:43:09 -0800116 HFSPLUS_I(inode).create_date =
117 file->create_date =
118 file->content_mod_date =
119 file->attribute_mod_date =
120 file->access_date = hfsp_now2mt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 if (cnid == inode->i_ino) {
122 hfsplus_set_perms(inode, &file->permissions);
Roman Zippel6b1928322006-01-18 17:43:12 -0800123 if (S_ISLNK(inode->i_mode)) {
124 file->user_info.fdType = cpu_to_be32(HFSP_SYMLINK_TYPE);
125 file->user_info.fdCreator = cpu_to_be32(HFSP_SYMLINK_CREATOR);
126 } else {
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200127 file->user_info.fdType = cpu_to_be32(sbi->type);
128 file->user_info.fdCreator = cpu_to_be32(sbi->creator);
Roman Zippel6b1928322006-01-18 17:43:12 -0800129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 if ((file->permissions.rootflags | file->permissions.userflags) & HFSPLUS_FLG_IMMUTABLE)
131 file->flags |= cpu_to_be16(HFSPLUS_FILE_LOCKED);
132 } else {
133 file->user_info.fdType = cpu_to_be32(HFSP_HARDLINK_TYPE);
134 file->user_info.fdCreator = cpu_to_be32(HFSP_HFSPLUS_CREATOR);
135 file->user_info.fdFlags = cpu_to_be16(0x100);
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200136 file->create_date = HFSPLUS_I(sbi->hidden_dir).create_date;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 file->permissions.dev = cpu_to_be32(HFSPLUS_I(inode).dev);
138 }
139 return sizeof(*file);
140 }
141}
142
143static int hfsplus_fill_cat_thread(struct super_block *sb,
144 hfsplus_cat_entry *entry, int type,
145 u32 parentid, struct qstr *str)
146{
147 entry->type = cpu_to_be16(type);
148 entry->thread.reserved = 0;
149 entry->thread.parentID = cpu_to_be32(parentid);
150 hfsplus_asc2uni(sb, &entry->thread.nodeName, str->name, str->len);
151 return 10 + be16_to_cpu(entry->thread.nodeName.length) * 2;
152}
153
154/* Try to get a catalog entry for given catalog id */
155int hfsplus_find_cat(struct super_block *sb, u32 cnid,
156 struct hfs_find_data *fd)
157{
158 hfsplus_cat_entry tmp;
159 int err;
160 u16 type;
161
162 hfsplus_cat_build_key(sb, fd->search_key, cnid, NULL);
163 err = hfs_brec_read(fd, &tmp, sizeof(hfsplus_cat_entry));
164 if (err)
165 return err;
166
167 type = be16_to_cpu(tmp.type);
168 if (type != HFSPLUS_FOLDER_THREAD && type != HFSPLUS_FILE_THREAD) {
Roman Zippel634725a2006-01-18 17:43:05 -0800169 printk(KERN_ERR "hfs: found bad thread record in catalog\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 return -EIO;
171 }
172
Eric Sesterhennefc7ffc2008-10-15 22:04:08 -0700173 if (be16_to_cpu(tmp.thread.nodeName.length) > 255) {
174 printk(KERN_ERR "hfs: catalog name length corrupted\n");
175 return -EIO;
176 }
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 hfsplus_cat_build_key_uni(fd->search_key, be32_to_cpu(tmp.thread.parentID),
179 &tmp.thread.nodeName);
180 return hfs_brec_find(fd);
181}
182
183int hfsplus_create_cat(u32 cnid, struct inode *dir, struct qstr *str, struct inode *inode)
184{
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200185 struct super_block *sb = dir->i_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 struct hfs_find_data fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 hfsplus_cat_entry entry;
188 int entry_size;
189 int err;
190
191 dprint(DBG_CAT_MOD, "create_cat: %s,%u(%d)\n", str->name, cnid, inode->i_nlink);
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200192 hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 hfsplus_cat_build_key(sb, fd.search_key, cnid, NULL);
195 entry_size = hfsplus_fill_cat_thread(sb, &entry, S_ISDIR(inode->i_mode) ?
196 HFSPLUS_FOLDER_THREAD : HFSPLUS_FILE_THREAD,
197 dir->i_ino, str);
198 err = hfs_brec_find(&fd);
199 if (err != -ENOENT) {
200 if (!err)
201 err = -EEXIST;
202 goto err2;
203 }
204 err = hfs_brec_insert(&fd, &entry, entry_size);
205 if (err)
206 goto err2;
207
208 hfsplus_cat_build_key(sb, fd.search_key, dir->i_ino, str);
209 entry_size = hfsplus_cat_build_record(&entry, cnid, inode);
210 err = hfs_brec_find(&fd);
211 if (err != -ENOENT) {
212 /* panic? */
213 if (!err)
214 err = -EEXIST;
215 goto err1;
216 }
217 err = hfs_brec_insert(&fd, &entry, entry_size);
218 if (err)
219 goto err1;
220
221 dir->i_size++;
222 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
223 mark_inode_dirty(dir);
224 hfs_find_exit(&fd);
225 return 0;
226
227err1:
228 hfsplus_cat_build_key(sb, fd.search_key, cnid, NULL);
229 if (!hfs_brec_find(&fd))
230 hfs_brec_remove(&fd);
231err2:
232 hfs_find_exit(&fd);
233 return err;
234}
235
236int hfsplus_delete_cat(u32 cnid, struct inode *dir, struct qstr *str)
237{
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200238 struct super_block *sb = dir->i_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 struct hfs_find_data fd;
240 struct hfsplus_fork_raw fork;
241 struct list_head *pos;
242 int err, off;
243 u16 type;
244
245 dprint(DBG_CAT_MOD, "delete_cat: %s,%u\n", str ? str->name : NULL, cnid);
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200246 hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 if (!str) {
249 int len;
250
251 hfsplus_cat_build_key(sb, fd.search_key, cnid, NULL);
252 err = hfs_brec_find(&fd);
253 if (err)
254 goto out;
255
256 off = fd.entryoffset + offsetof(struct hfsplus_cat_thread, nodeName);
257 fd.search_key->cat.parent = cpu_to_be32(dir->i_ino);
258 hfs_bnode_read(fd.bnode, &fd.search_key->cat.name.length, off, 2);
259 len = be16_to_cpu(fd.search_key->cat.name.length) * 2;
260 hfs_bnode_read(fd.bnode, &fd.search_key->cat.name.unicode, off + 2, len);
261 fd.search_key->key_len = cpu_to_be16(6 + len);
262 } else
263 hfsplus_cat_build_key(sb, fd.search_key, dir->i_ino, str);
264
265 err = hfs_brec_find(&fd);
266 if (err)
267 goto out;
268
269 type = hfs_bnode_read_u16(fd.bnode, fd.entryoffset);
270 if (type == HFSPLUS_FILE) {
271#if 0
272 off = fd.entryoffset + offsetof(hfsplus_cat_file, data_fork);
273 hfs_bnode_read(fd.bnode, &fork, off, sizeof(fork));
274 hfsplus_free_fork(sb, cnid, &fork, HFSPLUS_TYPE_DATA);
275#endif
276
277 off = fd.entryoffset + offsetof(struct hfsplus_cat_file, rsrc_fork);
278 hfs_bnode_read(fd.bnode, &fork, off, sizeof(fork));
279 hfsplus_free_fork(sb, cnid, &fork, HFSPLUS_TYPE_RSRC);
280 }
281
282 list_for_each(pos, &HFSPLUS_I(dir).open_dir_list) {
283 struct hfsplus_readdir_data *rd =
284 list_entry(pos, struct hfsplus_readdir_data, list);
285 if (fd.tree->keycmp(fd.search_key, (void *)&rd->key) < 0)
286 rd->file->f_pos--;
287 }
288
289 err = hfs_brec_remove(&fd);
290 if (err)
291 goto out;
292
293 hfsplus_cat_build_key(sb, fd.search_key, cnid, NULL);
294 err = hfs_brec_find(&fd);
295 if (err)
296 goto out;
297
298 err = hfs_brec_remove(&fd);
299 if (err)
300 goto out;
301
302 dir->i_size--;
303 dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
304 mark_inode_dirty(dir);
305out:
306 hfs_find_exit(&fd);
307
308 return err;
309}
310
311int hfsplus_rename_cat(u32 cnid,
312 struct inode *src_dir, struct qstr *src_name,
313 struct inode *dst_dir, struct qstr *dst_name)
314{
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200315 struct super_block *sb = src_dir->i_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 struct hfs_find_data src_fd, dst_fd;
317 hfsplus_cat_entry entry;
318 int entry_size, type;
319 int err = 0;
320
321 dprint(DBG_CAT_MOD, "rename_cat: %u - %lu,%s - %lu,%s\n", cnid, src_dir->i_ino, src_name->name,
322 dst_dir->i_ino, dst_name->name);
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200323 hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &src_fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 dst_fd = src_fd;
325
326 /* find the old dir entry and read the data */
327 hfsplus_cat_build_key(sb, src_fd.search_key, src_dir->i_ino, src_name);
328 err = hfs_brec_find(&src_fd);
329 if (err)
330 goto out;
331
332 hfs_bnode_read(src_fd.bnode, &entry, src_fd.entryoffset,
333 src_fd.entrylength);
334
335 /* create new dir entry with the data from the old entry */
336 hfsplus_cat_build_key(sb, dst_fd.search_key, dst_dir->i_ino, dst_name);
337 err = hfs_brec_find(&dst_fd);
338 if (err != -ENOENT) {
339 if (!err)
340 err = -EEXIST;
341 goto out;
342 }
343
344 err = hfs_brec_insert(&dst_fd, &entry, src_fd.entrylength);
345 if (err)
346 goto out;
347 dst_dir->i_size++;
348 dst_dir->i_mtime = dst_dir->i_ctime = CURRENT_TIME_SEC;
349 mark_inode_dirty(dst_dir);
350
351 /* finally remove the old entry */
352 hfsplus_cat_build_key(sb, src_fd.search_key, src_dir->i_ino, src_name);
353 err = hfs_brec_find(&src_fd);
354 if (err)
355 goto out;
356 err = hfs_brec_remove(&src_fd);
357 if (err)
358 goto out;
359 src_dir->i_size--;
360 src_dir->i_mtime = src_dir->i_ctime = CURRENT_TIME_SEC;
361 mark_inode_dirty(src_dir);
362
363 /* remove old thread entry */
364 hfsplus_cat_build_key(sb, src_fd.search_key, cnid, NULL);
365 err = hfs_brec_find(&src_fd);
366 if (err)
367 goto out;
368 type = hfs_bnode_read_u16(src_fd.bnode, src_fd.entryoffset);
369 err = hfs_brec_remove(&src_fd);
370 if (err)
371 goto out;
372
373 /* create new thread entry */
374 hfsplus_cat_build_key(sb, dst_fd.search_key, cnid, NULL);
375 entry_size = hfsplus_fill_cat_thread(sb, &entry, type, dst_dir->i_ino, dst_name);
376 err = hfs_brec_find(&dst_fd);
377 if (err != -ENOENT) {
378 if (!err)
379 err = -EEXIST;
380 goto out;
381 }
382 err = hfs_brec_insert(&dst_fd, &entry, entry_size);
383out:
384 hfs_bnode_put(dst_fd.bnode);
385 hfs_find_exit(&src_fd);
386 return err;
387}