Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
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 | ||
1da177e4 LT |
11 | |
12 | #include "hfsplus_fs.h" | |
13 | #include "hfsplus_raw.h" | |
14 | ||
2179d372 DE |
15 | int hfsplus_cat_case_cmp_key(const hfsplus_btree_key *k1, |
16 | const hfsplus_btree_key *k2) | |
1da177e4 LT |
17 | { |
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 | ||
2179d372 DE |
25 | return hfsplus_strcasecmp(&k1->cat.name, &k2->cat.name); |
26 | } | |
27 | ||
28 | int 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); | |
1da177e4 LT |
39 | } |
40 | ||
41 | void 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 | ||
57 | static 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 | ||
90e61690 | 70 | void hfsplus_cat_set_perms(struct inode *inode, struct hfsplus_perm *perms) |
1da177e4 LT |
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; | |
90e61690 CH |
80 | |
81 | perms->userflags = HFSPLUS_I(inode)->userflags; | |
1da177e4 LT |
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); | |
90e61690 CH |
85 | |
86 | if (S_ISREG(inode->i_mode)) | |
87 | perms->dev = cpu_to_be32(inode->i_nlink); | |
88 | else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) | |
89 | perms->dev = cpu_to_be32(inode->i_rdev); | |
90 | else | |
91 | perms->dev = 0; | |
1da177e4 LT |
92 | } |
93 | ||
2753cc28 AS |
94 | static int hfsplus_cat_build_record(hfsplus_cat_entry *entry, |
95 | u32 cnid, struct inode *inode) | |
1da177e4 | 96 | { |
dd73a01a CH |
97 | struct hfsplus_sb_info *sbi = HFSPLUS_SB(inode->i_sb); |
98 | ||
1da177e4 LT |
99 | if (S_ISDIR(inode->i_mode)) { |
100 | struct hfsplus_cat_folder *folder; | |
101 | ||
102 | folder = &entry->folder; | |
103 | memset(folder, 0, sizeof(*folder)); | |
104 | folder->type = cpu_to_be16(HFSPLUS_FOLDER); | |
105 | folder->id = cpu_to_be32(inode->i_ino); | |
6af502de | 106 | HFSPLUS_I(inode)->create_date = |
9a4cad95 RZ |
107 | folder->create_date = |
108 | folder->content_mod_date = | |
109 | folder->attribute_mod_date = | |
110 | folder->access_date = hfsp_now2mt(); | |
90e61690 | 111 | hfsplus_cat_set_perms(inode, &folder->permissions); |
dd73a01a | 112 | if (inode == sbi->hidden_dir) |
1da177e4 LT |
113 | /* invisible and namelocked */ |
114 | folder->user_info.frFlags = cpu_to_be16(0x5000); | |
115 | return sizeof(*folder); | |
116 | } else { | |
117 | struct hfsplus_cat_file *file; | |
118 | ||
119 | file = &entry->file; | |
120 | memset(file, 0, sizeof(*file)); | |
121 | file->type = cpu_to_be16(HFSPLUS_FILE); | |
122 | file->flags = cpu_to_be16(HFSPLUS_FILE_THREAD_EXISTS); | |
123 | file->id = cpu_to_be32(cnid); | |
6af502de | 124 | HFSPLUS_I(inode)->create_date = |
9a4cad95 RZ |
125 | file->create_date = |
126 | file->content_mod_date = | |
127 | file->attribute_mod_date = | |
128 | file->access_date = hfsp_now2mt(); | |
1da177e4 | 129 | if (cnid == inode->i_ino) { |
90e61690 | 130 | hfsplus_cat_set_perms(inode, &file->permissions); |
6b192832 | 131 | if (S_ISLNK(inode->i_mode)) { |
2753cc28 AS |
132 | file->user_info.fdType = |
133 | cpu_to_be32(HFSP_SYMLINK_TYPE); | |
134 | file->user_info.fdCreator = | |
135 | cpu_to_be32(HFSP_SYMLINK_CREATOR); | |
6b192832 | 136 | } else { |
2753cc28 AS |
137 | file->user_info.fdType = |
138 | cpu_to_be32(sbi->type); | |
139 | file->user_info.fdCreator = | |
140 | cpu_to_be32(sbi->creator); | |
6b192832 | 141 | } |
2753cc28 AS |
142 | if (HFSPLUS_FLG_IMMUTABLE & |
143 | (file->permissions.rootflags | | |
144 | file->permissions.userflags)) | |
145 | file->flags |= | |
146 | cpu_to_be16(HFSPLUS_FILE_LOCKED); | |
1da177e4 | 147 | } else { |
2753cc28 AS |
148 | file->user_info.fdType = |
149 | cpu_to_be32(HFSP_HARDLINK_TYPE); | |
150 | file->user_info.fdCreator = | |
151 | cpu_to_be32(HFSP_HFSPLUS_CREATOR); | |
152 | file->user_info.fdFlags = | |
153 | cpu_to_be16(0x100); | |
154 | file->create_date = | |
155 | HFSPLUS_I(sbi->hidden_dir)->create_date; | |
156 | file->permissions.dev = | |
157 | cpu_to_be32(HFSPLUS_I(inode)->linkid); | |
1da177e4 LT |
158 | } |
159 | return sizeof(*file); | |
160 | } | |
161 | } | |
162 | ||
163 | static int hfsplus_fill_cat_thread(struct super_block *sb, | |
164 | hfsplus_cat_entry *entry, int type, | |
165 | u32 parentid, struct qstr *str) | |
166 | { | |
167 | entry->type = cpu_to_be16(type); | |
168 | entry->thread.reserved = 0; | |
169 | entry->thread.parentID = cpu_to_be32(parentid); | |
170 | hfsplus_asc2uni(sb, &entry->thread.nodeName, str->name, str->len); | |
171 | return 10 + be16_to_cpu(entry->thread.nodeName.length) * 2; | |
172 | } | |
173 | ||
174 | /* Try to get a catalog entry for given catalog id */ | |
175 | int hfsplus_find_cat(struct super_block *sb, u32 cnid, | |
176 | struct hfs_find_data *fd) | |
177 | { | |
178 | hfsplus_cat_entry tmp; | |
179 | int err; | |
180 | u16 type; | |
181 | ||
182 | hfsplus_cat_build_key(sb, fd->search_key, cnid, NULL); | |
183 | err = hfs_brec_read(fd, &tmp, sizeof(hfsplus_cat_entry)); | |
184 | if (err) | |
185 | return err; | |
186 | ||
187 | type = be16_to_cpu(tmp.type); | |
188 | if (type != HFSPLUS_FOLDER_THREAD && type != HFSPLUS_FILE_THREAD) { | |
634725a9 | 189 | printk(KERN_ERR "hfs: found bad thread record in catalog\n"); |
1da177e4 LT |
190 | return -EIO; |
191 | } | |
192 | ||
efc7ffcb ES |
193 | if (be16_to_cpu(tmp.thread.nodeName.length) > 255) { |
194 | printk(KERN_ERR "hfs: catalog name length corrupted\n"); | |
195 | return -EIO; | |
196 | } | |
197 | ||
2753cc28 AS |
198 | hfsplus_cat_build_key_uni(fd->search_key, |
199 | be32_to_cpu(tmp.thread.parentID), | |
200 | &tmp.thread.nodeName); | |
1da177e4 LT |
201 | return hfs_brec_find(fd); |
202 | } | |
203 | ||
2753cc28 AS |
204 | int hfsplus_create_cat(u32 cnid, struct inode *dir, |
205 | struct qstr *str, struct inode *inode) | |
1da177e4 | 206 | { |
dd73a01a | 207 | struct super_block *sb = dir->i_sb; |
1da177e4 | 208 | struct hfs_find_data fd; |
1da177e4 LT |
209 | hfsplus_cat_entry entry; |
210 | int entry_size; | |
211 | int err; | |
212 | ||
2753cc28 AS |
213 | dprint(DBG_CAT_MOD, "create_cat: %s,%u(%d)\n", |
214 | str->name, cnid, inode->i_nlink); | |
dd73a01a | 215 | hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd); |
1da177e4 LT |
216 | |
217 | hfsplus_cat_build_key(sb, fd.search_key, cnid, NULL); | |
2753cc28 AS |
218 | entry_size = hfsplus_fill_cat_thread(sb, &entry, |
219 | S_ISDIR(inode->i_mode) ? | |
1da177e4 | 220 | HFSPLUS_FOLDER_THREAD : HFSPLUS_FILE_THREAD, |
2753cc28 | 221 | dir->i_ino, str); |
1da177e4 LT |
222 | err = hfs_brec_find(&fd); |
223 | if (err != -ENOENT) { | |
224 | if (!err) | |
225 | err = -EEXIST; | |
226 | goto err2; | |
227 | } | |
228 | err = hfs_brec_insert(&fd, &entry, entry_size); | |
229 | if (err) | |
230 | goto err2; | |
231 | ||
232 | hfsplus_cat_build_key(sb, fd.search_key, dir->i_ino, str); | |
233 | entry_size = hfsplus_cat_build_record(&entry, cnid, inode); | |
234 | err = hfs_brec_find(&fd); | |
235 | if (err != -ENOENT) { | |
236 | /* panic? */ | |
237 | if (!err) | |
238 | err = -EEXIST; | |
239 | goto err1; | |
240 | } | |
241 | err = hfs_brec_insert(&fd, &entry, entry_size); | |
242 | if (err) | |
243 | goto err1; | |
244 | ||
245 | dir->i_size++; | |
246 | dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC; | |
e3494705 CH |
247 | hfsplus_mark_inode_dirty(dir, HFSPLUS_I_CAT_DIRTY); |
248 | ||
1da177e4 LT |
249 | hfs_find_exit(&fd); |
250 | return 0; | |
251 | ||
252 | err1: | |
253 | hfsplus_cat_build_key(sb, fd.search_key, cnid, NULL); | |
254 | if (!hfs_brec_find(&fd)) | |
255 | hfs_brec_remove(&fd); | |
256 | err2: | |
257 | hfs_find_exit(&fd); | |
258 | return err; | |
259 | } | |
260 | ||
261 | int hfsplus_delete_cat(u32 cnid, struct inode *dir, struct qstr *str) | |
262 | { | |
dd73a01a | 263 | struct super_block *sb = dir->i_sb; |
1da177e4 LT |
264 | struct hfs_find_data fd; |
265 | struct hfsplus_fork_raw fork; | |
266 | struct list_head *pos; | |
267 | int err, off; | |
268 | u16 type; | |
269 | ||
2753cc28 AS |
270 | dprint(DBG_CAT_MOD, "delete_cat: %s,%u\n", |
271 | str ? str->name : NULL, cnid); | |
dd73a01a | 272 | hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd); |
1da177e4 LT |
273 | |
274 | if (!str) { | |
275 | int len; | |
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 | ||
2753cc28 AS |
282 | off = fd.entryoffset + |
283 | offsetof(struct hfsplus_cat_thread, nodeName); | |
1da177e4 | 284 | fd.search_key->cat.parent = cpu_to_be32(dir->i_ino); |
2753cc28 AS |
285 | hfs_bnode_read(fd.bnode, |
286 | &fd.search_key->cat.name.length, off, 2); | |
1da177e4 | 287 | len = be16_to_cpu(fd.search_key->cat.name.length) * 2; |
2753cc28 AS |
288 | hfs_bnode_read(fd.bnode, |
289 | &fd.search_key->cat.name.unicode, | |
290 | off + 2, len); | |
1da177e4 LT |
291 | fd.search_key->key_len = cpu_to_be16(6 + len); |
292 | } else | |
293 | hfsplus_cat_build_key(sb, fd.search_key, dir->i_ino, str); | |
294 | ||
295 | err = hfs_brec_find(&fd); | |
296 | if (err) | |
297 | goto out; | |
298 | ||
299 | type = hfs_bnode_read_u16(fd.bnode, fd.entryoffset); | |
300 | if (type == HFSPLUS_FILE) { | |
301 | #if 0 | |
302 | off = fd.entryoffset + offsetof(hfsplus_cat_file, data_fork); | |
303 | hfs_bnode_read(fd.bnode, &fork, off, sizeof(fork)); | |
304 | hfsplus_free_fork(sb, cnid, &fork, HFSPLUS_TYPE_DATA); | |
305 | #endif | |
306 | ||
2753cc28 AS |
307 | off = fd.entryoffset + |
308 | offsetof(struct hfsplus_cat_file, rsrc_fork); | |
1da177e4 LT |
309 | hfs_bnode_read(fd.bnode, &fork, off, sizeof(fork)); |
310 | hfsplus_free_fork(sb, cnid, &fork, HFSPLUS_TYPE_RSRC); | |
311 | } | |
312 | ||
6af502de | 313 | list_for_each(pos, &HFSPLUS_I(dir)->open_dir_list) { |
1da177e4 LT |
314 | struct hfsplus_readdir_data *rd = |
315 | list_entry(pos, struct hfsplus_readdir_data, list); | |
316 | if (fd.tree->keycmp(fd.search_key, (void *)&rd->key) < 0) | |
317 | rd->file->f_pos--; | |
318 | } | |
319 | ||
320 | err = hfs_brec_remove(&fd); | |
321 | if (err) | |
322 | goto out; | |
323 | ||
324 | hfsplus_cat_build_key(sb, fd.search_key, cnid, NULL); | |
325 | err = hfs_brec_find(&fd); | |
326 | if (err) | |
327 | goto out; | |
328 | ||
329 | err = hfs_brec_remove(&fd); | |
330 | if (err) | |
331 | goto out; | |
332 | ||
333 | dir->i_size--; | |
334 | dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC; | |
e3494705 | 335 | hfsplus_mark_inode_dirty(dir, HFSPLUS_I_CAT_DIRTY); |
1da177e4 LT |
336 | out: |
337 | hfs_find_exit(&fd); | |
338 | ||
339 | return err; | |
340 | } | |
341 | ||
342 | int hfsplus_rename_cat(u32 cnid, | |
343 | struct inode *src_dir, struct qstr *src_name, | |
344 | struct inode *dst_dir, struct qstr *dst_name) | |
345 | { | |
dd73a01a | 346 | struct super_block *sb = src_dir->i_sb; |
1da177e4 LT |
347 | struct hfs_find_data src_fd, dst_fd; |
348 | hfsplus_cat_entry entry; | |
349 | int entry_size, type; | |
350 | int err = 0; | |
351 | ||
2753cc28 AS |
352 | dprint(DBG_CAT_MOD, "rename_cat: %u - %lu,%s - %lu,%s\n", |
353 | cnid, src_dir->i_ino, src_name->name, | |
1da177e4 | 354 | dst_dir->i_ino, dst_name->name); |
dd73a01a | 355 | hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &src_fd); |
1da177e4 LT |
356 | dst_fd = src_fd; |
357 | ||
358 | /* find the old dir entry and read the data */ | |
359 | hfsplus_cat_build_key(sb, src_fd.search_key, src_dir->i_ino, src_name); | |
360 | err = hfs_brec_find(&src_fd); | |
361 | if (err) | |
362 | goto out; | |
363 | ||
364 | hfs_bnode_read(src_fd.bnode, &entry, src_fd.entryoffset, | |
365 | src_fd.entrylength); | |
366 | ||
367 | /* create new dir entry with the data from the old entry */ | |
368 | hfsplus_cat_build_key(sb, dst_fd.search_key, dst_dir->i_ino, dst_name); | |
369 | err = hfs_brec_find(&dst_fd); | |
370 | if (err != -ENOENT) { | |
371 | if (!err) | |
372 | err = -EEXIST; | |
373 | goto out; | |
374 | } | |
375 | ||
376 | err = hfs_brec_insert(&dst_fd, &entry, src_fd.entrylength); | |
377 | if (err) | |
378 | goto out; | |
379 | dst_dir->i_size++; | |
380 | dst_dir->i_mtime = dst_dir->i_ctime = CURRENT_TIME_SEC; | |
1da177e4 LT |
381 | |
382 | /* finally remove the old entry */ | |
383 | hfsplus_cat_build_key(sb, src_fd.search_key, src_dir->i_ino, src_name); | |
384 | err = hfs_brec_find(&src_fd); | |
385 | if (err) | |
386 | goto out; | |
387 | err = hfs_brec_remove(&src_fd); | |
388 | if (err) | |
389 | goto out; | |
390 | src_dir->i_size--; | |
391 | src_dir->i_mtime = src_dir->i_ctime = CURRENT_TIME_SEC; | |
1da177e4 LT |
392 | |
393 | /* remove old thread entry */ | |
394 | hfsplus_cat_build_key(sb, src_fd.search_key, cnid, NULL); | |
395 | err = hfs_brec_find(&src_fd); | |
396 | if (err) | |
397 | goto out; | |
398 | type = hfs_bnode_read_u16(src_fd.bnode, src_fd.entryoffset); | |
399 | err = hfs_brec_remove(&src_fd); | |
400 | if (err) | |
401 | goto out; | |
402 | ||
403 | /* create new thread entry */ | |
404 | hfsplus_cat_build_key(sb, dst_fd.search_key, cnid, NULL); | |
2753cc28 AS |
405 | entry_size = hfsplus_fill_cat_thread(sb, &entry, type, |
406 | dst_dir->i_ino, dst_name); | |
1da177e4 LT |
407 | err = hfs_brec_find(&dst_fd); |
408 | if (err != -ENOENT) { | |
409 | if (!err) | |
410 | err = -EEXIST; | |
411 | goto out; | |
412 | } | |
413 | err = hfs_brec_insert(&dst_fd, &entry, entry_size); | |
e3494705 CH |
414 | |
415 | hfsplus_mark_inode_dirty(dst_dir, HFSPLUS_I_CAT_DIRTY); | |
416 | hfsplus_mark_inode_dirty(src_dir, HFSPLUS_I_CAT_DIRTY); | |
1da177e4 LT |
417 | out: |
418 | hfs_bnode_put(dst_fd.bnode); | |
419 | hfs_find_exit(&src_fd); | |
420 | return err; | |
421 | } |