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