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