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