0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #include "hfsplus_fs.h"
0014 #include "hfsplus_raw.h"
0015
0016 int hfsplus_cat_case_cmp_key(const hfsplus_btree_key *k1,
0017 const hfsplus_btree_key *k2)
0018 {
0019 __be32 k1p, k2p;
0020
0021 k1p = k1->cat.parent;
0022 k2p = k2->cat.parent;
0023 if (k1p != k2p)
0024 return be32_to_cpu(k1p) < be32_to_cpu(k2p) ? -1 : 1;
0025
0026 return hfsplus_strcasecmp(&k1->cat.name, &k2->cat.name);
0027 }
0028
0029 int hfsplus_cat_bin_cmp_key(const hfsplus_btree_key *k1,
0030 const hfsplus_btree_key *k2)
0031 {
0032 __be32 k1p, k2p;
0033
0034 k1p = k1->cat.parent;
0035 k2p = k2->cat.parent;
0036 if (k1p != k2p)
0037 return be32_to_cpu(k1p) < be32_to_cpu(k2p) ? -1 : 1;
0038
0039 return hfsplus_strcmp(&k1->cat.name, &k2->cat.name);
0040 }
0041
0042
0043 int hfsplus_cat_build_key(struct super_block *sb,
0044 hfsplus_btree_key *key, u32 parent, const struct qstr *str)
0045 {
0046 int len, err;
0047
0048 key->cat.parent = cpu_to_be32(parent);
0049 err = hfsplus_asc2uni(sb, &key->cat.name, HFSPLUS_MAX_STRLEN,
0050 str->name, str->len);
0051 if (unlikely(err < 0))
0052 return err;
0053
0054 len = be16_to_cpu(key->cat.name.length);
0055 key->key_len = cpu_to_be16(6 + 2 * len);
0056 return 0;
0057 }
0058
0059
0060 void hfsplus_cat_build_key_with_cnid(struct super_block *sb,
0061 hfsplus_btree_key *key, u32 parent)
0062 {
0063 key->cat.parent = cpu_to_be32(parent);
0064 key->cat.name.length = 0;
0065 key->key_len = cpu_to_be16(6);
0066 }
0067
0068 static void hfsplus_cat_build_key_uni(hfsplus_btree_key *key, u32 parent,
0069 struct hfsplus_unistr *name)
0070 {
0071 int ustrlen;
0072
0073 ustrlen = be16_to_cpu(name->length);
0074 key->cat.parent = cpu_to_be32(parent);
0075 key->cat.name.length = cpu_to_be16(ustrlen);
0076 ustrlen *= 2;
0077 memcpy(key->cat.name.unicode, name->unicode, ustrlen);
0078 key->key_len = cpu_to_be16(6 + ustrlen);
0079 }
0080
0081 void hfsplus_cat_set_perms(struct inode *inode, struct hfsplus_perm *perms)
0082 {
0083 if (inode->i_flags & S_IMMUTABLE)
0084 perms->rootflags |= HFSPLUS_FLG_IMMUTABLE;
0085 else
0086 perms->rootflags &= ~HFSPLUS_FLG_IMMUTABLE;
0087 if (inode->i_flags & S_APPEND)
0088 perms->rootflags |= HFSPLUS_FLG_APPEND;
0089 else
0090 perms->rootflags &= ~HFSPLUS_FLG_APPEND;
0091
0092 perms->userflags = HFSPLUS_I(inode)->userflags;
0093 perms->mode = cpu_to_be16(inode->i_mode);
0094 perms->owner = cpu_to_be32(i_uid_read(inode));
0095 perms->group = cpu_to_be32(i_gid_read(inode));
0096
0097 if (S_ISREG(inode->i_mode))
0098 perms->dev = cpu_to_be32(inode->i_nlink);
0099 else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode))
0100 perms->dev = cpu_to_be32(inode->i_rdev);
0101 else
0102 perms->dev = 0;
0103 }
0104
0105 static int hfsplus_cat_build_record(hfsplus_cat_entry *entry,
0106 u32 cnid, struct inode *inode)
0107 {
0108 struct hfsplus_sb_info *sbi = HFSPLUS_SB(inode->i_sb);
0109
0110 if (S_ISDIR(inode->i_mode)) {
0111 struct hfsplus_cat_folder *folder;
0112
0113 folder = &entry->folder;
0114 memset(folder, 0, sizeof(*folder));
0115 folder->type = cpu_to_be16(HFSPLUS_FOLDER);
0116 if (test_bit(HFSPLUS_SB_HFSX, &sbi->flags))
0117 folder->flags |= cpu_to_be16(HFSPLUS_HAS_FOLDER_COUNT);
0118 folder->id = cpu_to_be32(inode->i_ino);
0119 HFSPLUS_I(inode)->create_date =
0120 folder->create_date =
0121 folder->content_mod_date =
0122 folder->attribute_mod_date =
0123 folder->access_date = hfsp_now2mt();
0124 hfsplus_cat_set_perms(inode, &folder->permissions);
0125 if (inode == sbi->hidden_dir)
0126
0127 folder->user_info.frFlags = cpu_to_be16(0x5000);
0128 return sizeof(*folder);
0129 } else {
0130 struct hfsplus_cat_file *file;
0131
0132 file = &entry->file;
0133 memset(file, 0, sizeof(*file));
0134 file->type = cpu_to_be16(HFSPLUS_FILE);
0135 file->flags = cpu_to_be16(HFSPLUS_FILE_THREAD_EXISTS);
0136 file->id = cpu_to_be32(cnid);
0137 HFSPLUS_I(inode)->create_date =
0138 file->create_date =
0139 file->content_mod_date =
0140 file->attribute_mod_date =
0141 file->access_date = hfsp_now2mt();
0142 if (cnid == inode->i_ino) {
0143 hfsplus_cat_set_perms(inode, &file->permissions);
0144 if (S_ISLNK(inode->i_mode)) {
0145 file->user_info.fdType =
0146 cpu_to_be32(HFSP_SYMLINK_TYPE);
0147 file->user_info.fdCreator =
0148 cpu_to_be32(HFSP_SYMLINK_CREATOR);
0149 } else {
0150 file->user_info.fdType =
0151 cpu_to_be32(sbi->type);
0152 file->user_info.fdCreator =
0153 cpu_to_be32(sbi->creator);
0154 }
0155 if (HFSPLUS_FLG_IMMUTABLE &
0156 (file->permissions.rootflags |
0157 file->permissions.userflags))
0158 file->flags |=
0159 cpu_to_be16(HFSPLUS_FILE_LOCKED);
0160 } else {
0161 file->user_info.fdType =
0162 cpu_to_be32(HFSP_HARDLINK_TYPE);
0163 file->user_info.fdCreator =
0164 cpu_to_be32(HFSP_HFSPLUS_CREATOR);
0165 file->user_info.fdFlags =
0166 cpu_to_be16(0x100);
0167 file->create_date =
0168 HFSPLUS_I(sbi->hidden_dir)->create_date;
0169 file->permissions.dev =
0170 cpu_to_be32(HFSPLUS_I(inode)->linkid);
0171 }
0172 return sizeof(*file);
0173 }
0174 }
0175
0176 static int hfsplus_fill_cat_thread(struct super_block *sb,
0177 hfsplus_cat_entry *entry, int type,
0178 u32 parentid, const struct qstr *str)
0179 {
0180 int err;
0181
0182 entry->type = cpu_to_be16(type);
0183 entry->thread.reserved = 0;
0184 entry->thread.parentID = cpu_to_be32(parentid);
0185 err = hfsplus_asc2uni(sb, &entry->thread.nodeName, HFSPLUS_MAX_STRLEN,
0186 str->name, str->len);
0187 if (unlikely(err < 0))
0188 return err;
0189
0190 return 10 + be16_to_cpu(entry->thread.nodeName.length) * 2;
0191 }
0192
0193
0194 int hfsplus_find_cat(struct super_block *sb, u32 cnid,
0195 struct hfs_find_data *fd)
0196 {
0197 hfsplus_cat_entry tmp;
0198 int err;
0199 u16 type;
0200
0201 hfsplus_cat_build_key_with_cnid(sb, fd->search_key, cnid);
0202 err = hfs_brec_read(fd, &tmp, sizeof(hfsplus_cat_entry));
0203 if (err)
0204 return err;
0205
0206 type = be16_to_cpu(tmp.type);
0207 if (type != HFSPLUS_FOLDER_THREAD && type != HFSPLUS_FILE_THREAD) {
0208 pr_err("found bad thread record in catalog\n");
0209 return -EIO;
0210 }
0211
0212 if (be16_to_cpu(tmp.thread.nodeName.length) > 255) {
0213 pr_err("catalog name length corrupted\n");
0214 return -EIO;
0215 }
0216
0217 hfsplus_cat_build_key_uni(fd->search_key,
0218 be32_to_cpu(tmp.thread.parentID),
0219 &tmp.thread.nodeName);
0220 return hfs_brec_find(fd, hfs_find_rec_by_key);
0221 }
0222
0223 static void hfsplus_subfolders_inc(struct inode *dir)
0224 {
0225 struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
0226
0227 if (test_bit(HFSPLUS_SB_HFSX, &sbi->flags)) {
0228
0229
0230
0231
0232 HFSPLUS_I(dir)->subfolders++;
0233 }
0234 }
0235
0236 static void hfsplus_subfolders_dec(struct inode *dir)
0237 {
0238 struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
0239
0240 if (test_bit(HFSPLUS_SB_HFSX, &sbi->flags)) {
0241
0242
0243
0244
0245
0246
0247
0248 if (HFSPLUS_I(dir)->subfolders)
0249 HFSPLUS_I(dir)->subfolders--;
0250 }
0251 }
0252
0253 int hfsplus_create_cat(u32 cnid, struct inode *dir,
0254 const struct qstr *str, struct inode *inode)
0255 {
0256 struct super_block *sb = dir->i_sb;
0257 struct hfs_find_data fd;
0258 hfsplus_cat_entry entry;
0259 int entry_size;
0260 int err;
0261
0262 hfs_dbg(CAT_MOD, "create_cat: %s,%u(%d)\n",
0263 str->name, cnid, inode->i_nlink);
0264 err = hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd);
0265 if (err)
0266 return err;
0267
0268
0269
0270
0271
0272 err = hfs_bmap_reserve(fd.tree, 2 * fd.tree->depth);
0273 if (err)
0274 goto err2;
0275
0276 hfsplus_cat_build_key_with_cnid(sb, fd.search_key, cnid);
0277 entry_size = hfsplus_fill_cat_thread(sb, &entry,
0278 S_ISDIR(inode->i_mode) ?
0279 HFSPLUS_FOLDER_THREAD : HFSPLUS_FILE_THREAD,
0280 dir->i_ino, str);
0281 if (unlikely(entry_size < 0)) {
0282 err = entry_size;
0283 goto err2;
0284 }
0285
0286 err = hfs_brec_find(&fd, hfs_find_rec_by_key);
0287 if (err != -ENOENT) {
0288 if (!err)
0289 err = -EEXIST;
0290 goto err2;
0291 }
0292 err = hfs_brec_insert(&fd, &entry, entry_size);
0293 if (err)
0294 goto err2;
0295
0296 err = hfsplus_cat_build_key(sb, fd.search_key, dir->i_ino, str);
0297 if (unlikely(err))
0298 goto err1;
0299
0300 entry_size = hfsplus_cat_build_record(&entry, cnid, inode);
0301 err = hfs_brec_find(&fd, hfs_find_rec_by_key);
0302 if (err != -ENOENT) {
0303
0304 if (!err)
0305 err = -EEXIST;
0306 goto err1;
0307 }
0308 err = hfs_brec_insert(&fd, &entry, entry_size);
0309 if (err)
0310 goto err1;
0311
0312 dir->i_size++;
0313 if (S_ISDIR(inode->i_mode))
0314 hfsplus_subfolders_inc(dir);
0315 dir->i_mtime = dir->i_ctime = current_time(dir);
0316 hfsplus_mark_inode_dirty(dir, HFSPLUS_I_CAT_DIRTY);
0317
0318 hfs_find_exit(&fd);
0319 return 0;
0320
0321 err1:
0322 hfsplus_cat_build_key_with_cnid(sb, fd.search_key, cnid);
0323 if (!hfs_brec_find(&fd, hfs_find_rec_by_key))
0324 hfs_brec_remove(&fd);
0325 err2:
0326 hfs_find_exit(&fd);
0327 return err;
0328 }
0329
0330 int hfsplus_delete_cat(u32 cnid, struct inode *dir, const struct qstr *str)
0331 {
0332 struct super_block *sb = dir->i_sb;
0333 struct hfs_find_data fd;
0334 struct hfsplus_fork_raw fork;
0335 struct list_head *pos;
0336 int err, off;
0337 u16 type;
0338
0339 hfs_dbg(CAT_MOD, "delete_cat: %s,%u\n", str ? str->name : NULL, cnid);
0340 err = hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd);
0341 if (err)
0342 return err;
0343
0344
0345
0346
0347
0348 err = hfs_bmap_reserve(fd.tree, 2 * (int)fd.tree->depth - 2);
0349 if (err)
0350 goto out;
0351
0352 if (!str) {
0353 int len;
0354
0355 hfsplus_cat_build_key_with_cnid(sb, fd.search_key, cnid);
0356 err = hfs_brec_find(&fd, hfs_find_rec_by_key);
0357 if (err)
0358 goto out;
0359
0360 off = fd.entryoffset +
0361 offsetof(struct hfsplus_cat_thread, nodeName);
0362 fd.search_key->cat.parent = cpu_to_be32(dir->i_ino);
0363 hfs_bnode_read(fd.bnode,
0364 &fd.search_key->cat.name.length, off, 2);
0365 len = be16_to_cpu(fd.search_key->cat.name.length) * 2;
0366 hfs_bnode_read(fd.bnode,
0367 &fd.search_key->cat.name.unicode,
0368 off + 2, len);
0369 fd.search_key->key_len = cpu_to_be16(6 + len);
0370 } else {
0371 err = hfsplus_cat_build_key(sb, fd.search_key, dir->i_ino, str);
0372 if (unlikely(err))
0373 goto out;
0374 }
0375
0376 err = hfs_brec_find(&fd, hfs_find_rec_by_key);
0377 if (err)
0378 goto out;
0379
0380 type = hfs_bnode_read_u16(fd.bnode, fd.entryoffset);
0381 if (type == HFSPLUS_FILE) {
0382 #if 0
0383 off = fd.entryoffset + offsetof(hfsplus_cat_file, data_fork);
0384 hfs_bnode_read(fd.bnode, &fork, off, sizeof(fork));
0385 hfsplus_free_fork(sb, cnid, &fork, HFSPLUS_TYPE_DATA);
0386 #endif
0387
0388 off = fd.entryoffset +
0389 offsetof(struct hfsplus_cat_file, rsrc_fork);
0390 hfs_bnode_read(fd.bnode, &fork, off, sizeof(fork));
0391 hfsplus_free_fork(sb, cnid, &fork, HFSPLUS_TYPE_RSRC);
0392 }
0393
0394
0395 spin_lock(&HFSPLUS_I(dir)->open_dir_lock);
0396 list_for_each(pos, &HFSPLUS_I(dir)->open_dir_list) {
0397 struct hfsplus_readdir_data *rd =
0398 list_entry(pos, struct hfsplus_readdir_data, list);
0399 if (fd.tree->keycmp(fd.search_key, (void *)&rd->key) < 0)
0400 rd->file->f_pos--;
0401 }
0402 spin_unlock(&HFSPLUS_I(dir)->open_dir_lock);
0403
0404 err = hfs_brec_remove(&fd);
0405 if (err)
0406 goto out;
0407
0408 hfsplus_cat_build_key_with_cnid(sb, fd.search_key, cnid);
0409 err = hfs_brec_find(&fd, hfs_find_rec_by_key);
0410 if (err)
0411 goto out;
0412
0413 err = hfs_brec_remove(&fd);
0414 if (err)
0415 goto out;
0416
0417 dir->i_size--;
0418 if (type == HFSPLUS_FOLDER)
0419 hfsplus_subfolders_dec(dir);
0420 dir->i_mtime = dir->i_ctime = current_time(dir);
0421 hfsplus_mark_inode_dirty(dir, HFSPLUS_I_CAT_DIRTY);
0422
0423 if (type == HFSPLUS_FILE || type == HFSPLUS_FOLDER) {
0424 if (HFSPLUS_SB(sb)->attr_tree)
0425 hfsplus_delete_all_attrs(dir, cnid);
0426 }
0427
0428 out:
0429 hfs_find_exit(&fd);
0430
0431 return err;
0432 }
0433
0434 int hfsplus_rename_cat(u32 cnid,
0435 struct inode *src_dir, const struct qstr *src_name,
0436 struct inode *dst_dir, const struct qstr *dst_name)
0437 {
0438 struct super_block *sb = src_dir->i_sb;
0439 struct hfs_find_data src_fd, dst_fd;
0440 hfsplus_cat_entry entry;
0441 int entry_size, type;
0442 int err;
0443
0444 hfs_dbg(CAT_MOD, "rename_cat: %u - %lu,%s - %lu,%s\n",
0445 cnid, src_dir->i_ino, src_name->name,
0446 dst_dir->i_ino, dst_name->name);
0447 err = hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &src_fd);
0448 if (err)
0449 return err;
0450 dst_fd = src_fd;
0451
0452
0453
0454
0455
0456 err = hfs_bmap_reserve(src_fd.tree, 4 * (int)src_fd.tree->depth - 1);
0457 if (err)
0458 goto out;
0459
0460
0461 err = hfsplus_cat_build_key(sb, src_fd.search_key,
0462 src_dir->i_ino, src_name);
0463 if (unlikely(err))
0464 goto out;
0465
0466 err = hfs_brec_find(&src_fd, hfs_find_rec_by_key);
0467 if (err)
0468 goto out;
0469 if (src_fd.entrylength > sizeof(entry) || src_fd.entrylength < 0) {
0470 err = -EIO;
0471 goto out;
0472 }
0473
0474 hfs_bnode_read(src_fd.bnode, &entry, src_fd.entryoffset,
0475 src_fd.entrylength);
0476 type = be16_to_cpu(entry.type);
0477
0478
0479 err = hfsplus_cat_build_key(sb, dst_fd.search_key,
0480 dst_dir->i_ino, dst_name);
0481 if (unlikely(err))
0482 goto out;
0483
0484 err = hfs_brec_find(&dst_fd, hfs_find_rec_by_key);
0485 if (err != -ENOENT) {
0486 if (!err)
0487 err = -EEXIST;
0488 goto out;
0489 }
0490
0491 err = hfs_brec_insert(&dst_fd, &entry, src_fd.entrylength);
0492 if (err)
0493 goto out;
0494 dst_dir->i_size++;
0495 if (type == HFSPLUS_FOLDER)
0496 hfsplus_subfolders_inc(dst_dir);
0497 dst_dir->i_mtime = dst_dir->i_ctime = current_time(dst_dir);
0498
0499
0500 err = hfsplus_cat_build_key(sb, src_fd.search_key,
0501 src_dir->i_ino, src_name);
0502 if (unlikely(err))
0503 goto out;
0504
0505 err = hfs_brec_find(&src_fd, hfs_find_rec_by_key);
0506 if (err)
0507 goto out;
0508 err = hfs_brec_remove(&src_fd);
0509 if (err)
0510 goto out;
0511 src_dir->i_size--;
0512 if (type == HFSPLUS_FOLDER)
0513 hfsplus_subfolders_dec(src_dir);
0514 src_dir->i_mtime = src_dir->i_ctime = current_time(src_dir);
0515
0516
0517 hfsplus_cat_build_key_with_cnid(sb, src_fd.search_key, cnid);
0518 err = hfs_brec_find(&src_fd, hfs_find_rec_by_key);
0519 if (err)
0520 goto out;
0521 type = hfs_bnode_read_u16(src_fd.bnode, src_fd.entryoffset);
0522 err = hfs_brec_remove(&src_fd);
0523 if (err)
0524 goto out;
0525
0526
0527 hfsplus_cat_build_key_with_cnid(sb, dst_fd.search_key, cnid);
0528 entry_size = hfsplus_fill_cat_thread(sb, &entry, type,
0529 dst_dir->i_ino, dst_name);
0530 if (unlikely(entry_size < 0)) {
0531 err = entry_size;
0532 goto out;
0533 }
0534
0535 err = hfs_brec_find(&dst_fd, hfs_find_rec_by_key);
0536 if (err != -ENOENT) {
0537 if (!err)
0538 err = -EEXIST;
0539 goto out;
0540 }
0541 err = hfs_brec_insert(&dst_fd, &entry, entry_size);
0542
0543 hfsplus_mark_inode_dirty(dst_dir, HFSPLUS_I_CAT_DIRTY);
0544 hfsplus_mark_inode_dirty(src_dir, HFSPLUS_I_CAT_DIRTY);
0545 out:
0546 hfs_bnode_put(dst_fd.bnode);
0547 hfs_find_exit(&src_fd);
0548 return err;
0549 }