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) { |
Vyacheslav Dubeyko | 324ef39 | 2013-02-27 17:03:04 -0800 | [diff] [blame] | 48 | hfsplus_asc2uni(sb, &key->cat.name, HFSPLUS_MAX_STRLEN, |
| 49 | str->name, str->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
| 58 | static 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 | |
Christoph Hellwig | 90e6169 | 2010-10-14 09:54:39 -0400 | [diff] [blame] | 71 | void hfsplus_cat_set_perms(struct inode *inode, struct hfsplus_perm *perms) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Christoph Hellwig | 90e6169 | 2010-10-14 09:54:39 -0400 | [diff] [blame] | 81 | |
| 82 | perms->userflags = HFSPLUS_I(inode)->userflags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | perms->mode = cpu_to_be16(inode->i_mode); |
Eric W. Biederman | 16525e3 | 2012-02-07 16:27:17 -0800 | [diff] [blame] | 84 | perms->owner = cpu_to_be32(i_uid_read(inode)); |
| 85 | perms->group = cpu_to_be32(i_gid_read(inode)); |
Christoph Hellwig | 90e6169 | 2010-10-14 09:54:39 -0400 | [diff] [blame] | 86 | |
| 87 | if (S_ISREG(inode->i_mode)) |
| 88 | perms->dev = cpu_to_be32(inode->i_nlink); |
| 89 | else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) |
| 90 | perms->dev = cpu_to_be32(inode->i_rdev); |
| 91 | else |
| 92 | perms->dev = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Anton Salikhmetov | 2753cc2 | 2010-12-16 18:08:38 +0200 | [diff] [blame] | 95 | static int hfsplus_cat_build_record(hfsplus_cat_entry *entry, |
| 96 | u32 cnid, struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | { |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame] | 98 | struct hfsplus_sb_info *sbi = HFSPLUS_SB(inode->i_sb); |
| 99 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | if (S_ISDIR(inode->i_mode)) { |
| 101 | struct hfsplus_cat_folder *folder; |
| 102 | |
| 103 | folder = &entry->folder; |
| 104 | memset(folder, 0, sizeof(*folder)); |
| 105 | folder->type = cpu_to_be16(HFSPLUS_FOLDER); |
Sergei Antonov | d7d673a | 2014-03-10 15:49:51 -0700 | [diff] [blame] | 106 | if (test_bit(HFSPLUS_SB_HFSX, &sbi->flags)) |
| 107 | folder->flags |= cpu_to_be16(HFSPLUS_HAS_FOLDER_COUNT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | folder->id = cpu_to_be32(inode->i_ino); |
Christoph Hellwig | 6af502d | 2010-10-01 05:43:31 +0200 | [diff] [blame] | 109 | HFSPLUS_I(inode)->create_date = |
Roman Zippel | 9a4cad9 | 2006-01-18 17:43:09 -0800 | [diff] [blame] | 110 | folder->create_date = |
| 111 | folder->content_mod_date = |
| 112 | folder->attribute_mod_date = |
| 113 | folder->access_date = hfsp_now2mt(); |
Christoph Hellwig | 90e6169 | 2010-10-14 09:54:39 -0400 | [diff] [blame] | 114 | hfsplus_cat_set_perms(inode, &folder->permissions); |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame] | 115 | if (inode == sbi->hidden_dir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | /* invisible and namelocked */ |
| 117 | folder->user_info.frFlags = cpu_to_be16(0x5000); |
| 118 | return sizeof(*folder); |
| 119 | } else { |
| 120 | struct hfsplus_cat_file *file; |
| 121 | |
| 122 | file = &entry->file; |
| 123 | memset(file, 0, sizeof(*file)); |
| 124 | file->type = cpu_to_be16(HFSPLUS_FILE); |
| 125 | file->flags = cpu_to_be16(HFSPLUS_FILE_THREAD_EXISTS); |
| 126 | file->id = cpu_to_be32(cnid); |
Christoph Hellwig | 6af502d | 2010-10-01 05:43:31 +0200 | [diff] [blame] | 127 | HFSPLUS_I(inode)->create_date = |
Roman Zippel | 9a4cad9 | 2006-01-18 17:43:09 -0800 | [diff] [blame] | 128 | file->create_date = |
| 129 | file->content_mod_date = |
| 130 | file->attribute_mod_date = |
| 131 | file->access_date = hfsp_now2mt(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | if (cnid == inode->i_ino) { |
Christoph Hellwig | 90e6169 | 2010-10-14 09:54:39 -0400 | [diff] [blame] | 133 | hfsplus_cat_set_perms(inode, &file->permissions); |
Roman Zippel | 6b192832 | 2006-01-18 17:43:12 -0800 | [diff] [blame] | 134 | if (S_ISLNK(inode->i_mode)) { |
Anton Salikhmetov | 2753cc2 | 2010-12-16 18:08:38 +0200 | [diff] [blame] | 135 | file->user_info.fdType = |
| 136 | cpu_to_be32(HFSP_SYMLINK_TYPE); |
| 137 | file->user_info.fdCreator = |
| 138 | cpu_to_be32(HFSP_SYMLINK_CREATOR); |
Roman Zippel | 6b192832 | 2006-01-18 17:43:12 -0800 | [diff] [blame] | 139 | } else { |
Anton Salikhmetov | 2753cc2 | 2010-12-16 18:08:38 +0200 | [diff] [blame] | 140 | file->user_info.fdType = |
| 141 | cpu_to_be32(sbi->type); |
| 142 | file->user_info.fdCreator = |
| 143 | cpu_to_be32(sbi->creator); |
Roman Zippel | 6b192832 | 2006-01-18 17:43:12 -0800 | [diff] [blame] | 144 | } |
Anton Salikhmetov | 2753cc2 | 2010-12-16 18:08:38 +0200 | [diff] [blame] | 145 | if (HFSPLUS_FLG_IMMUTABLE & |
| 146 | (file->permissions.rootflags | |
| 147 | file->permissions.userflags)) |
| 148 | file->flags |= |
| 149 | cpu_to_be16(HFSPLUS_FILE_LOCKED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | } else { |
Anton Salikhmetov | 2753cc2 | 2010-12-16 18:08:38 +0200 | [diff] [blame] | 151 | file->user_info.fdType = |
| 152 | cpu_to_be32(HFSP_HARDLINK_TYPE); |
| 153 | file->user_info.fdCreator = |
| 154 | cpu_to_be32(HFSP_HFSPLUS_CREATOR); |
| 155 | file->user_info.fdFlags = |
| 156 | cpu_to_be16(0x100); |
| 157 | file->create_date = |
| 158 | HFSPLUS_I(sbi->hidden_dir)->create_date; |
| 159 | file->permissions.dev = |
| 160 | cpu_to_be32(HFSPLUS_I(inode)->linkid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | } |
| 162 | return sizeof(*file); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | static int hfsplus_fill_cat_thread(struct super_block *sb, |
| 167 | hfsplus_cat_entry *entry, int type, |
| 168 | u32 parentid, struct qstr *str) |
| 169 | { |
| 170 | entry->type = cpu_to_be16(type); |
| 171 | entry->thread.reserved = 0; |
| 172 | entry->thread.parentID = cpu_to_be32(parentid); |
Vyacheslav Dubeyko | 324ef39 | 2013-02-27 17:03:04 -0800 | [diff] [blame] | 173 | hfsplus_asc2uni(sb, &entry->thread.nodeName, HFSPLUS_MAX_STRLEN, |
| 174 | str->name, str->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | return 10 + be16_to_cpu(entry->thread.nodeName.length) * 2; |
| 176 | } |
| 177 | |
| 178 | /* Try to get a catalog entry for given catalog id */ |
| 179 | int hfsplus_find_cat(struct super_block *sb, u32 cnid, |
| 180 | struct hfs_find_data *fd) |
| 181 | { |
| 182 | hfsplus_cat_entry tmp; |
| 183 | int err; |
| 184 | u16 type; |
| 185 | |
| 186 | hfsplus_cat_build_key(sb, fd->search_key, cnid, NULL); |
| 187 | err = hfs_brec_read(fd, &tmp, sizeof(hfsplus_cat_entry)); |
| 188 | if (err) |
| 189 | return err; |
| 190 | |
| 191 | type = be16_to_cpu(tmp.type); |
| 192 | if (type != HFSPLUS_FOLDER_THREAD && type != HFSPLUS_FILE_THREAD) { |
Joe Perches | d614267 | 2013-04-30 15:27:55 -0700 | [diff] [blame] | 193 | pr_err("found bad thread record in catalog\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | return -EIO; |
| 195 | } |
| 196 | |
Eric Sesterhenn | efc7ffc | 2008-10-15 22:04:08 -0700 | [diff] [blame] | 197 | if (be16_to_cpu(tmp.thread.nodeName.length) > 255) { |
Joe Perches | d614267 | 2013-04-30 15:27:55 -0700 | [diff] [blame] | 198 | pr_err("catalog name length corrupted\n"); |
Eric Sesterhenn | efc7ffc | 2008-10-15 22:04:08 -0700 | [diff] [blame] | 199 | return -EIO; |
| 200 | } |
| 201 | |
Anton Salikhmetov | 2753cc2 | 2010-12-16 18:08:38 +0200 | [diff] [blame] | 202 | hfsplus_cat_build_key_uni(fd->search_key, |
| 203 | be32_to_cpu(tmp.thread.parentID), |
| 204 | &tmp.thread.nodeName); |
Vyacheslav Dubeyko | 324ef39 | 2013-02-27 17:03:04 -0800 | [diff] [blame] | 205 | return hfs_brec_find(fd, hfs_find_rec_by_key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Sergei Antonov | d7d673a | 2014-03-10 15:49:51 -0700 | [diff] [blame] | 208 | static void hfsplus_subfolders_inc(struct inode *dir) |
| 209 | { |
| 210 | struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb); |
| 211 | |
| 212 | if (test_bit(HFSPLUS_SB_HFSX, &sbi->flags)) { |
| 213 | /* |
| 214 | * Increment subfolder count. Note, the value is only meaningful |
| 215 | * for folders with HFSPLUS_HAS_FOLDER_COUNT flag set. |
| 216 | */ |
| 217 | HFSPLUS_I(dir)->subfolders++; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | static void hfsplus_subfolders_dec(struct inode *dir) |
| 222 | { |
| 223 | struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb); |
| 224 | |
| 225 | if (test_bit(HFSPLUS_SB_HFSX, &sbi->flags)) { |
| 226 | /* |
| 227 | * Decrement subfolder count. Note, the value is only meaningful |
| 228 | * for folders with HFSPLUS_HAS_FOLDER_COUNT flag set. |
| 229 | * |
| 230 | * Check for zero. Some subfolders may have been created |
| 231 | * by an implementation ignorant of this counter. |
| 232 | */ |
| 233 | if (HFSPLUS_I(dir)->subfolders) |
| 234 | HFSPLUS_I(dir)->subfolders--; |
| 235 | } |
| 236 | } |
| 237 | |
Anton Salikhmetov | 2753cc2 | 2010-12-16 18:08:38 +0200 | [diff] [blame] | 238 | int hfsplus_create_cat(u32 cnid, struct inode *dir, |
| 239 | struct qstr *str, struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | { |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame] | 241 | struct super_block *sb = dir->i_sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | struct hfs_find_data fd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | hfsplus_cat_entry entry; |
| 244 | int entry_size; |
| 245 | int err; |
| 246 | |
Joe Perches | c2b3e1f | 2013-04-30 15:27:54 -0700 | [diff] [blame] | 247 | hfs_dbg(CAT_MOD, "create_cat: %s,%u(%d)\n", |
Anton Salikhmetov | 2753cc2 | 2010-12-16 18:08:38 +0200 | [diff] [blame] | 248 | str->name, cnid, inode->i_nlink); |
Alexey Khoroshilov | 5bd9d99 | 2011-07-06 02:29:59 +0400 | [diff] [blame] | 249 | err = hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd); |
| 250 | if (err) |
| 251 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | |
| 253 | hfsplus_cat_build_key(sb, fd.search_key, cnid, NULL); |
Anton Salikhmetov | 2753cc2 | 2010-12-16 18:08:38 +0200 | [diff] [blame] | 254 | entry_size = hfsplus_fill_cat_thread(sb, &entry, |
| 255 | S_ISDIR(inode->i_mode) ? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | HFSPLUS_FOLDER_THREAD : HFSPLUS_FILE_THREAD, |
Anton Salikhmetov | 2753cc2 | 2010-12-16 18:08:38 +0200 | [diff] [blame] | 257 | dir->i_ino, str); |
Vyacheslav Dubeyko | 324ef39 | 2013-02-27 17:03:04 -0800 | [diff] [blame] | 258 | err = hfs_brec_find(&fd, hfs_find_rec_by_key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | if (err != -ENOENT) { |
| 260 | if (!err) |
| 261 | err = -EEXIST; |
| 262 | goto err2; |
| 263 | } |
| 264 | err = hfs_brec_insert(&fd, &entry, entry_size); |
| 265 | if (err) |
| 266 | goto err2; |
| 267 | |
| 268 | hfsplus_cat_build_key(sb, fd.search_key, dir->i_ino, str); |
| 269 | entry_size = hfsplus_cat_build_record(&entry, cnid, inode); |
Vyacheslav Dubeyko | 324ef39 | 2013-02-27 17:03:04 -0800 | [diff] [blame] | 270 | err = hfs_brec_find(&fd, hfs_find_rec_by_key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | if (err != -ENOENT) { |
| 272 | /* panic? */ |
| 273 | if (!err) |
| 274 | err = -EEXIST; |
| 275 | goto err1; |
| 276 | } |
| 277 | err = hfs_brec_insert(&fd, &entry, entry_size); |
| 278 | if (err) |
| 279 | goto err1; |
| 280 | |
| 281 | dir->i_size++; |
Sergei Antonov | d7d673a | 2014-03-10 15:49:51 -0700 | [diff] [blame] | 282 | if (S_ISDIR(inode->i_mode)) |
| 283 | hfsplus_subfolders_inc(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC; |
Christoph Hellwig | e349470 | 2010-11-23 14:38:15 +0100 | [diff] [blame] | 285 | hfsplus_mark_inode_dirty(dir, HFSPLUS_I_CAT_DIRTY); |
| 286 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | hfs_find_exit(&fd); |
| 288 | return 0; |
| 289 | |
| 290 | err1: |
| 291 | hfsplus_cat_build_key(sb, fd.search_key, cnid, NULL); |
Vyacheslav Dubeyko | 324ef39 | 2013-02-27 17:03:04 -0800 | [diff] [blame] | 292 | if (!hfs_brec_find(&fd, hfs_find_rec_by_key)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | hfs_brec_remove(&fd); |
| 294 | err2: |
| 295 | hfs_find_exit(&fd); |
| 296 | return err; |
| 297 | } |
| 298 | |
| 299 | int hfsplus_delete_cat(u32 cnid, struct inode *dir, struct qstr *str) |
| 300 | { |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame] | 301 | struct super_block *sb = dir->i_sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | struct hfs_find_data fd; |
| 303 | struct hfsplus_fork_raw fork; |
| 304 | struct list_head *pos; |
| 305 | int err, off; |
| 306 | u16 type; |
| 307 | |
Joe Perches | c2b3e1f | 2013-04-30 15:27:54 -0700 | [diff] [blame] | 308 | hfs_dbg(CAT_MOD, "delete_cat: %s,%u\n", str ? str->name : NULL, cnid); |
Alexey Khoroshilov | 5bd9d99 | 2011-07-06 02:29:59 +0400 | [diff] [blame] | 309 | err = hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd); |
| 310 | if (err) |
| 311 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | |
| 313 | if (!str) { |
| 314 | int len; |
| 315 | |
| 316 | hfsplus_cat_build_key(sb, fd.search_key, cnid, NULL); |
Vyacheslav Dubeyko | 324ef39 | 2013-02-27 17:03:04 -0800 | [diff] [blame] | 317 | err = hfs_brec_find(&fd, hfs_find_rec_by_key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | if (err) |
| 319 | goto out; |
| 320 | |
Anton Salikhmetov | 2753cc2 | 2010-12-16 18:08:38 +0200 | [diff] [blame] | 321 | off = fd.entryoffset + |
| 322 | offsetof(struct hfsplus_cat_thread, nodeName); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | fd.search_key->cat.parent = cpu_to_be32(dir->i_ino); |
Anton Salikhmetov | 2753cc2 | 2010-12-16 18:08:38 +0200 | [diff] [blame] | 324 | hfs_bnode_read(fd.bnode, |
| 325 | &fd.search_key->cat.name.length, off, 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | len = be16_to_cpu(fd.search_key->cat.name.length) * 2; |
Anton Salikhmetov | 2753cc2 | 2010-12-16 18:08:38 +0200 | [diff] [blame] | 327 | hfs_bnode_read(fd.bnode, |
| 328 | &fd.search_key->cat.name.unicode, |
| 329 | off + 2, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | fd.search_key->key_len = cpu_to_be16(6 + len); |
| 331 | } else |
| 332 | hfsplus_cat_build_key(sb, fd.search_key, dir->i_ino, str); |
| 333 | |
Vyacheslav Dubeyko | 324ef39 | 2013-02-27 17:03:04 -0800 | [diff] [blame] | 334 | err = hfs_brec_find(&fd, hfs_find_rec_by_key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | if (err) |
| 336 | goto out; |
| 337 | |
| 338 | type = hfs_bnode_read_u16(fd.bnode, fd.entryoffset); |
| 339 | if (type == HFSPLUS_FILE) { |
| 340 | #if 0 |
| 341 | off = fd.entryoffset + offsetof(hfsplus_cat_file, data_fork); |
| 342 | hfs_bnode_read(fd.bnode, &fork, off, sizeof(fork)); |
| 343 | hfsplus_free_fork(sb, cnid, &fork, HFSPLUS_TYPE_DATA); |
| 344 | #endif |
| 345 | |
Anton Salikhmetov | 2753cc2 | 2010-12-16 18:08:38 +0200 | [diff] [blame] | 346 | off = fd.entryoffset + |
| 347 | offsetof(struct hfsplus_cat_file, rsrc_fork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | hfs_bnode_read(fd.bnode, &fork, off, sizeof(fork)); |
| 349 | hfsplus_free_fork(sb, cnid, &fork, HFSPLUS_TYPE_RSRC); |
| 350 | } |
| 351 | |
Christoph Hellwig | 6af502d | 2010-10-01 05:43:31 +0200 | [diff] [blame] | 352 | list_for_each(pos, &HFSPLUS_I(dir)->open_dir_list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | struct hfsplus_readdir_data *rd = |
| 354 | list_entry(pos, struct hfsplus_readdir_data, list); |
| 355 | if (fd.tree->keycmp(fd.search_key, (void *)&rd->key) < 0) |
| 356 | rd->file->f_pos--; |
| 357 | } |
| 358 | |
| 359 | err = hfs_brec_remove(&fd); |
| 360 | if (err) |
| 361 | goto out; |
| 362 | |
| 363 | hfsplus_cat_build_key(sb, fd.search_key, cnid, NULL); |
Vyacheslav Dubeyko | 324ef39 | 2013-02-27 17:03:04 -0800 | [diff] [blame] | 364 | err = hfs_brec_find(&fd, hfs_find_rec_by_key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | if (err) |
| 366 | goto out; |
| 367 | |
| 368 | err = hfs_brec_remove(&fd); |
| 369 | if (err) |
| 370 | goto out; |
| 371 | |
| 372 | dir->i_size--; |
Sergei Antonov | d7d673a | 2014-03-10 15:49:51 -0700 | [diff] [blame] | 373 | if (type == HFSPLUS_FOLDER) |
| 374 | hfsplus_subfolders_dec(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC; |
Christoph Hellwig | e349470 | 2010-11-23 14:38:15 +0100 | [diff] [blame] | 376 | hfsplus_mark_inode_dirty(dir, HFSPLUS_I_CAT_DIRTY); |
Vyacheslav Dubeyko | 324ef39 | 2013-02-27 17:03:04 -0800 | [diff] [blame] | 377 | |
| 378 | if (type == HFSPLUS_FILE || type == HFSPLUS_FOLDER) { |
| 379 | if (HFSPLUS_SB(sb)->attr_tree) |
| 380 | hfsplus_delete_all_attrs(dir, cnid); |
| 381 | } |
| 382 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | out: |
| 384 | hfs_find_exit(&fd); |
| 385 | |
| 386 | return err; |
| 387 | } |
| 388 | |
| 389 | int hfsplus_rename_cat(u32 cnid, |
| 390 | struct inode *src_dir, struct qstr *src_name, |
| 391 | struct inode *dst_dir, struct qstr *dst_name) |
| 392 | { |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame] | 393 | struct super_block *sb = src_dir->i_sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | struct hfs_find_data src_fd, dst_fd; |
| 395 | hfsplus_cat_entry entry; |
| 396 | int entry_size, type; |
Alexey Khoroshilov | 5bd9d99 | 2011-07-06 02:29:59 +0400 | [diff] [blame] | 397 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | |
Joe Perches | c2b3e1f | 2013-04-30 15:27:54 -0700 | [diff] [blame] | 399 | hfs_dbg(CAT_MOD, "rename_cat: %u - %lu,%s - %lu,%s\n", |
Anton Salikhmetov | 2753cc2 | 2010-12-16 18:08:38 +0200 | [diff] [blame] | 400 | cnid, src_dir->i_ino, src_name->name, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | dst_dir->i_ino, dst_name->name); |
Alexey Khoroshilov | 5bd9d99 | 2011-07-06 02:29:59 +0400 | [diff] [blame] | 402 | err = hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &src_fd); |
| 403 | if (err) |
| 404 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | dst_fd = src_fd; |
| 406 | |
| 407 | /* find the old dir entry and read the data */ |
| 408 | hfsplus_cat_build_key(sb, src_fd.search_key, src_dir->i_ino, src_name); |
Vyacheslav Dubeyko | 324ef39 | 2013-02-27 17:03:04 -0800 | [diff] [blame] | 409 | err = hfs_brec_find(&src_fd, hfs_find_rec_by_key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | if (err) |
| 411 | goto out; |
Greg Kroah-Hartman | 6f24f89 | 2012-05-04 12:09:39 -0700 | [diff] [blame] | 412 | if (src_fd.entrylength > sizeof(entry) || src_fd.entrylength < 0) { |
| 413 | err = -EIO; |
| 414 | goto out; |
| 415 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | |
| 417 | hfs_bnode_read(src_fd.bnode, &entry, src_fd.entryoffset, |
| 418 | src_fd.entrylength); |
Sergei Antonov | d7d673a | 2014-03-10 15:49:51 -0700 | [diff] [blame] | 419 | type = be16_to_cpu(entry.type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | |
| 421 | /* create new dir entry with the data from the old entry */ |
| 422 | hfsplus_cat_build_key(sb, dst_fd.search_key, dst_dir->i_ino, dst_name); |
Vyacheslav Dubeyko | 324ef39 | 2013-02-27 17:03:04 -0800 | [diff] [blame] | 423 | err = hfs_brec_find(&dst_fd, hfs_find_rec_by_key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | if (err != -ENOENT) { |
| 425 | if (!err) |
| 426 | err = -EEXIST; |
| 427 | goto out; |
| 428 | } |
| 429 | |
| 430 | err = hfs_brec_insert(&dst_fd, &entry, src_fd.entrylength); |
| 431 | if (err) |
| 432 | goto out; |
| 433 | dst_dir->i_size++; |
Sergei Antonov | d7d673a | 2014-03-10 15:49:51 -0700 | [diff] [blame] | 434 | if (type == HFSPLUS_FOLDER) |
| 435 | hfsplus_subfolders_inc(dst_dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | dst_dir->i_mtime = dst_dir->i_ctime = CURRENT_TIME_SEC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | |
| 438 | /* finally remove the old entry */ |
| 439 | hfsplus_cat_build_key(sb, src_fd.search_key, src_dir->i_ino, src_name); |
Vyacheslav Dubeyko | 324ef39 | 2013-02-27 17:03:04 -0800 | [diff] [blame] | 440 | err = hfs_brec_find(&src_fd, hfs_find_rec_by_key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | if (err) |
| 442 | goto out; |
| 443 | err = hfs_brec_remove(&src_fd); |
| 444 | if (err) |
| 445 | goto out; |
| 446 | src_dir->i_size--; |
Sergei Antonov | d7d673a | 2014-03-10 15:49:51 -0700 | [diff] [blame] | 447 | if (type == HFSPLUS_FOLDER) |
| 448 | hfsplus_subfolders_dec(src_dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | src_dir->i_mtime = src_dir->i_ctime = CURRENT_TIME_SEC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | |
| 451 | /* remove old thread entry */ |
| 452 | hfsplus_cat_build_key(sb, src_fd.search_key, cnid, NULL); |
Vyacheslav Dubeyko | 324ef39 | 2013-02-27 17:03:04 -0800 | [diff] [blame] | 453 | err = hfs_brec_find(&src_fd, hfs_find_rec_by_key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | if (err) |
| 455 | goto out; |
| 456 | type = hfs_bnode_read_u16(src_fd.bnode, src_fd.entryoffset); |
| 457 | err = hfs_brec_remove(&src_fd); |
| 458 | if (err) |
| 459 | goto out; |
| 460 | |
| 461 | /* create new thread entry */ |
| 462 | hfsplus_cat_build_key(sb, dst_fd.search_key, cnid, NULL); |
Anton Salikhmetov | 2753cc2 | 2010-12-16 18:08:38 +0200 | [diff] [blame] | 463 | entry_size = hfsplus_fill_cat_thread(sb, &entry, type, |
| 464 | dst_dir->i_ino, dst_name); |
Vyacheslav Dubeyko | 324ef39 | 2013-02-27 17:03:04 -0800 | [diff] [blame] | 465 | err = hfs_brec_find(&dst_fd, hfs_find_rec_by_key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | if (err != -ENOENT) { |
| 467 | if (!err) |
| 468 | err = -EEXIST; |
| 469 | goto out; |
| 470 | } |
| 471 | err = hfs_brec_insert(&dst_fd, &entry, entry_size); |
Christoph Hellwig | e349470 | 2010-11-23 14:38:15 +0100 | [diff] [blame] | 472 | |
| 473 | hfsplus_mark_inode_dirty(dst_dir, HFSPLUS_I_CAT_DIRTY); |
| 474 | hfsplus_mark_inode_dirty(src_dir, HFSPLUS_I_CAT_DIRTY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | out: |
| 476 | hfs_bnode_put(dst_fd.bnode); |
| 477 | hfs_find_exit(&src_fd); |
| 478 | return err; |
| 479 | } |