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