blob: 947da72e72a303069cf0a396eff37af2650998f5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Torvalds1da177e2005-04-16 15:20:36 -070011
12#include "hfsplus_fs.h"
13#include "hfsplus_raw.h"
14
David Elliott2179d372006-01-18 17:43:08 -080015int hfsplus_cat_case_cmp_key(const hfsplus_btree_key *k1,
16 const hfsplus_btree_key *k2)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017{
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 Elliott2179d372006-01-18 17:43:08 -080025 return hfsplus_strcasecmp(&k1->cat.name, &k2->cat.name);
26}
27
28int 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 Torvalds1da177e2005-04-16 15:20:36 -070039}
40
Sougata Santra89ac9b42014-12-18 16:17:12 -080041/* Generates key for catalog file/folders record. */
42int hfsplus_cat_build_key(struct super_block *sb,
Al Virob5cce522016-07-20 16:17:26 -040043 hfsplus_btree_key *key, u32 parent, const struct qstr *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
Sougata Santra89ac9b42014-12-18 16:17:12 -080045 int len, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47 key->cat.parent = cpu_to_be32(parent);
Sougata Santra89ac9b42014-12-18 16:17:12 -080048 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 Torvalds1da177e2005-04-16 15:20:36 -070054 key->key_len = cpu_to_be16(6 + 2 * len);
Sougata Santra89ac9b42014-12-18 16:17:12 -080055 return 0;
56}
57
58/* Generates key for catalog thread record. */
59void 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 Torvalds1da177e2005-04-16 15:20:36 -070065}
66
67static 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 Hellwig90e61692010-10-14 09:54:39 -040080void hfsplus_cat_set_perms(struct inode *inode, struct hfsplus_perm *perms)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
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 Hellwig90e61692010-10-14 09:54:39 -040090
91 perms->userflags = HFSPLUS_I(inode)->userflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 perms->mode = cpu_to_be16(inode->i_mode);
Eric W. Biederman16525e32012-02-07 16:27:17 -080093 perms->owner = cpu_to_be32(i_uid_read(inode));
94 perms->group = cpu_to_be32(i_gid_read(inode));
Christoph Hellwig90e61692010-10-14 09:54:39 -040095
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 Torvalds1da177e2005-04-16 15:20:36 -0700102}
103
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200104static int hfsplus_cat_build_record(hfsplus_cat_entry *entry,
105 u32 cnid, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200107 struct hfsplus_sb_info *sbi = HFSPLUS_SB(inode->i_sb);
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 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 Antonovd7d673a2014-03-10 15:49:51 -0700115 if (test_bit(HFSPLUS_SB_HFSX, &sbi->flags))
116 folder->flags |= cpu_to_be16(HFSPLUS_HAS_FOLDER_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 folder->id = cpu_to_be32(inode->i_ino);
Christoph Hellwig6af502d2010-10-01 05:43:31 +0200118 HFSPLUS_I(inode)->create_date =
Roman Zippel9a4cad92006-01-18 17:43:09 -0800119 folder->create_date =
120 folder->content_mod_date =
121 folder->attribute_mod_date =
122 folder->access_date = hfsp_now2mt();
Christoph Hellwig90e61692010-10-14 09:54:39 -0400123 hfsplus_cat_set_perms(inode, &folder->permissions);
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200124 if (inode == sbi->hidden_dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 /* 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 Hellwig6af502d2010-10-01 05:43:31 +0200136 HFSPLUS_I(inode)->create_date =
Roman Zippel9a4cad92006-01-18 17:43:09 -0800137 file->create_date =
138 file->content_mod_date =
139 file->attribute_mod_date =
140 file->access_date = hfsp_now2mt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 if (cnid == inode->i_ino) {
Christoph Hellwig90e61692010-10-14 09:54:39 -0400142 hfsplus_cat_set_perms(inode, &file->permissions);
Roman Zippel6b1928322006-01-18 17:43:12 -0800143 if (S_ISLNK(inode->i_mode)) {
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200144 file->user_info.fdType =
145 cpu_to_be32(HFSP_SYMLINK_TYPE);
146 file->user_info.fdCreator =
147 cpu_to_be32(HFSP_SYMLINK_CREATOR);
Roman Zippel6b1928322006-01-18 17:43:12 -0800148 } else {
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200149 file->user_info.fdType =
150 cpu_to_be32(sbi->type);
151 file->user_info.fdCreator =
152 cpu_to_be32(sbi->creator);
Roman Zippel6b1928322006-01-18 17:43:12 -0800153 }
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200154 if (HFSPLUS_FLG_IMMUTABLE &
155 (file->permissions.rootflags |
156 file->permissions.userflags))
157 file->flags |=
158 cpu_to_be16(HFSPLUS_FILE_LOCKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 } else {
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200160 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 Torvalds1da177e2005-04-16 15:20:36 -0700170 }
171 return sizeof(*file);
172 }
173}
174
175static int hfsplus_fill_cat_thread(struct super_block *sb,
176 hfsplus_cat_entry *entry, int type,
Al Virob5cce522016-07-20 16:17:26 -0400177 u32 parentid, const struct qstr *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
Sougata Santra89ac9b42014-12-18 16:17:12 -0800179 int err;
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 entry->type = cpu_to_be16(type);
182 entry->thread.reserved = 0;
183 entry->thread.parentID = cpu_to_be32(parentid);
Sougata Santra89ac9b42014-12-18 16:17:12 -0800184 err = hfsplus_asc2uni(sb, &entry->thread.nodeName, HFSPLUS_MAX_STRLEN,
Vyacheslav Dubeyko324ef392013-02-27 17:03:04 -0800185 str->name, str->len);
Sougata Santra89ac9b42014-12-18 16:17:12 -0800186 if (unlikely(err < 0))
187 return err;
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 return 10 + be16_to_cpu(entry->thread.nodeName.length) * 2;
190}
191
192/* Try to get a catalog entry for given catalog id */
193int 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 Santra89ac9b42014-12-18 16:17:12 -0800200 hfsplus_cat_build_key_with_cnid(sb, fd->search_key, cnid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 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 Perchesd6142672013-04-30 15:27:55 -0700207 pr_err("found bad thread record in catalog\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 return -EIO;
209 }
210
Eric Sesterhennefc7ffc2008-10-15 22:04:08 -0700211 if (be16_to_cpu(tmp.thread.nodeName.length) > 255) {
Joe Perchesd6142672013-04-30 15:27:55 -0700212 pr_err("catalog name length corrupted\n");
Eric Sesterhennefc7ffc2008-10-15 22:04:08 -0700213 return -EIO;
214 }
215
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200216 hfsplus_cat_build_key_uni(fd->search_key,
217 be32_to_cpu(tmp.thread.parentID),
218 &tmp.thread.nodeName);
Vyacheslav Dubeyko324ef392013-02-27 17:03:04 -0800219 return hfs_brec_find(fd, hfs_find_rec_by_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
221
Sergei Antonovd7d673a2014-03-10 15:49:51 -0700222static 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
235static 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 Salikhmetov2753cc22010-12-16 18:08:38 +0200252int hfsplus_create_cat(u32 cnid, struct inode *dir,
Al Virob5cce522016-07-20 16:17:26 -0400253 const struct qstr *str, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200255 struct super_block *sb = dir->i_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 struct hfs_find_data fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 hfsplus_cat_entry entry;
258 int entry_size;
259 int err;
260
Joe Perchesc2b3e1f2013-04-30 15:27:54 -0700261 hfs_dbg(CAT_MOD, "create_cat: %s,%u(%d)\n",
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200262 str->name, cnid, inode->i_nlink);
Alexey Khoroshilov5bd9d992011-07-06 02:29:59 +0400263 err = hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd);
264 if (err)
265 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Ernesto A. Fernández376270e2018-10-30 15:06:14 -0700267 /*
268 * Fail early and avoid ENOSPC during the btree operations. We may
269 * have to split the root node at most once.
270 */
271 err = hfs_bmap_reserve(fd.tree, 2 * fd.tree->depth);
272 if (err)
273 goto err2;
274
Sougata Santra89ac9b42014-12-18 16:17:12 -0800275 hfsplus_cat_build_key_with_cnid(sb, fd.search_key, cnid);
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200276 entry_size = hfsplus_fill_cat_thread(sb, &entry,
277 S_ISDIR(inode->i_mode) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 HFSPLUS_FOLDER_THREAD : HFSPLUS_FILE_THREAD,
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200279 dir->i_ino, str);
Sougata Santra89ac9b42014-12-18 16:17:12 -0800280 if (unlikely(entry_size < 0)) {
281 err = entry_size;
282 goto err2;
283 }
284
Vyacheslav Dubeyko324ef392013-02-27 17:03:04 -0800285 err = hfs_brec_find(&fd, hfs_find_rec_by_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 if (err != -ENOENT) {
287 if (!err)
288 err = -EEXIST;
289 goto err2;
290 }
291 err = hfs_brec_insert(&fd, &entry, entry_size);
292 if (err)
293 goto err2;
294
Sougata Santra89ac9b42014-12-18 16:17:12 -0800295 err = hfsplus_cat_build_key(sb, fd.search_key, dir->i_ino, str);
296 if (unlikely(err))
297 goto err1;
298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 entry_size = hfsplus_cat_build_record(&entry, cnid, inode);
Vyacheslav Dubeyko324ef392013-02-27 17:03:04 -0800300 err = hfs_brec_find(&fd, hfs_find_rec_by_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 if (err != -ENOENT) {
302 /* panic? */
303 if (!err)
304 err = -EEXIST;
305 goto err1;
306 }
307 err = hfs_brec_insert(&fd, &entry, entry_size);
308 if (err)
309 goto err1;
310
311 dir->i_size++;
Sergei Antonovd7d673a2014-03-10 15:49:51 -0700312 if (S_ISDIR(inode->i_mode))
313 hfsplus_subfolders_inc(dir);
Deepa Dinamani02027d42016-09-14 07:48:05 -0700314 dir->i_mtime = dir->i_ctime = current_time(dir);
Christoph Hellwige3494702010-11-23 14:38:15 +0100315 hfsplus_mark_inode_dirty(dir, HFSPLUS_I_CAT_DIRTY);
316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 hfs_find_exit(&fd);
318 return 0;
319
320err1:
Sougata Santra89ac9b42014-12-18 16:17:12 -0800321 hfsplus_cat_build_key_with_cnid(sb, fd.search_key, cnid);
Vyacheslav Dubeyko324ef392013-02-27 17:03:04 -0800322 if (!hfs_brec_find(&fd, hfs_find_rec_by_key))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 hfs_brec_remove(&fd);
324err2:
325 hfs_find_exit(&fd);
326 return err;
327}
328
Al Virob5cce522016-07-20 16:17:26 -0400329int hfsplus_delete_cat(u32 cnid, struct inode *dir, const struct qstr *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200331 struct super_block *sb = dir->i_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 struct hfs_find_data fd;
333 struct hfsplus_fork_raw fork;
334 struct list_head *pos;
335 int err, off;
336 u16 type;
337
Joe Perchesc2b3e1f2013-04-30 15:27:54 -0700338 hfs_dbg(CAT_MOD, "delete_cat: %s,%u\n", str ? str->name : NULL, cnid);
Alexey Khoroshilov5bd9d992011-07-06 02:29:59 +0400339 err = hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd);
340 if (err)
341 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Ernesto A. Fernández376270e2018-10-30 15:06:14 -0700343 /*
344 * Fail early and avoid ENOSPC during the btree operations. We may
345 * have to split the root node at most once.
346 */
347 err = hfs_bmap_reserve(fd.tree, 2 * (int)fd.tree->depth - 2);
348 if (err)
349 goto out;
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 if (!str) {
352 int len;
353
Sougata Santra89ac9b42014-12-18 16:17:12 -0800354 hfsplus_cat_build_key_with_cnid(sb, fd.search_key, cnid);
Vyacheslav Dubeyko324ef392013-02-27 17:03:04 -0800355 err = hfs_brec_find(&fd, hfs_find_rec_by_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 if (err)
357 goto out;
358
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200359 off = fd.entryoffset +
360 offsetof(struct hfsplus_cat_thread, nodeName);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 fd.search_key->cat.parent = cpu_to_be32(dir->i_ino);
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200362 hfs_bnode_read(fd.bnode,
363 &fd.search_key->cat.name.length, off, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 len = be16_to_cpu(fd.search_key->cat.name.length) * 2;
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200365 hfs_bnode_read(fd.bnode,
366 &fd.search_key->cat.name.unicode,
367 off + 2, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 fd.search_key->key_len = cpu_to_be16(6 + len);
Dan Carpenterf01fa5f2015-04-16 12:46:56 -0700369 } else {
Sougata Santra89ac9b42014-12-18 16:17:12 -0800370 err = hfsplus_cat_build_key(sb, fd.search_key, dir->i_ino, str);
371 if (unlikely(err))
372 goto out;
Dan Carpenterf01fa5f2015-04-16 12:46:56 -0700373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Vyacheslav Dubeyko324ef392013-02-27 17:03:04 -0800375 err = hfs_brec_find(&fd, hfs_find_rec_by_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 if (err)
377 goto out;
378
379 type = hfs_bnode_read_u16(fd.bnode, fd.entryoffset);
380 if (type == HFSPLUS_FILE) {
381#if 0
382 off = fd.entryoffset + offsetof(hfsplus_cat_file, data_fork);
383 hfs_bnode_read(fd.bnode, &fork, off, sizeof(fork));
384 hfsplus_free_fork(sb, cnid, &fork, HFSPLUS_TYPE_DATA);
385#endif
386
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200387 off = fd.entryoffset +
388 offsetof(struct hfsplus_cat_file, rsrc_fork);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 hfs_bnode_read(fd.bnode, &fork, off, sizeof(fork));
390 hfsplus_free_fork(sb, cnid, &fork, HFSPLUS_TYPE_RSRC);
391 }
392
Al Viro323ee8f2016-05-12 20:02:09 -0400393 /* we only need to take spinlock for exclusion with ->release() */
394 spin_lock(&HFSPLUS_I(dir)->open_dir_lock);
Christoph Hellwig6af502d2010-10-01 05:43:31 +0200395 list_for_each(pos, &HFSPLUS_I(dir)->open_dir_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 struct hfsplus_readdir_data *rd =
397 list_entry(pos, struct hfsplus_readdir_data, list);
398 if (fd.tree->keycmp(fd.search_key, (void *)&rd->key) < 0)
399 rd->file->f_pos--;
400 }
Al Viro323ee8f2016-05-12 20:02:09 -0400401 spin_unlock(&HFSPLUS_I(dir)->open_dir_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 err = hfs_brec_remove(&fd);
404 if (err)
405 goto out;
406
Sougata Santra89ac9b42014-12-18 16:17:12 -0800407 hfsplus_cat_build_key_with_cnid(sb, fd.search_key, cnid);
Vyacheslav Dubeyko324ef392013-02-27 17:03:04 -0800408 err = hfs_brec_find(&fd, hfs_find_rec_by_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 if (err)
410 goto out;
411
412 err = hfs_brec_remove(&fd);
413 if (err)
414 goto out;
415
416 dir->i_size--;
Sergei Antonovd7d673a2014-03-10 15:49:51 -0700417 if (type == HFSPLUS_FOLDER)
418 hfsplus_subfolders_dec(dir);
Deepa Dinamani02027d42016-09-14 07:48:05 -0700419 dir->i_mtime = dir->i_ctime = current_time(dir);
Christoph Hellwige3494702010-11-23 14:38:15 +0100420 hfsplus_mark_inode_dirty(dir, HFSPLUS_I_CAT_DIRTY);
Vyacheslav Dubeyko324ef392013-02-27 17:03:04 -0800421
422 if (type == HFSPLUS_FILE || type == HFSPLUS_FOLDER) {
423 if (HFSPLUS_SB(sb)->attr_tree)
424 hfsplus_delete_all_attrs(dir, cnid);
425 }
426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427out:
428 hfs_find_exit(&fd);
429
430 return err;
431}
432
433int hfsplus_rename_cat(u32 cnid,
Al Virob5cce522016-07-20 16:17:26 -0400434 struct inode *src_dir, const struct qstr *src_name,
435 struct inode *dst_dir, const struct qstr *dst_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200437 struct super_block *sb = src_dir->i_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 struct hfs_find_data src_fd, dst_fd;
439 hfsplus_cat_entry entry;
440 int entry_size, type;
Alexey Khoroshilov5bd9d992011-07-06 02:29:59 +0400441 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Joe Perchesc2b3e1f2013-04-30 15:27:54 -0700443 hfs_dbg(CAT_MOD, "rename_cat: %u - %lu,%s - %lu,%s\n",
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200444 cnid, src_dir->i_ino, src_name->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 dst_dir->i_ino, dst_name->name);
Alexey Khoroshilov5bd9d992011-07-06 02:29:59 +0400446 err = hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &src_fd);
447 if (err)
448 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 dst_fd = src_fd;
450
Ernesto A. Fernández376270e2018-10-30 15:06:14 -0700451 /*
452 * Fail early and avoid ENOSPC during the btree operations. We may
453 * have to split the root node at most twice.
454 */
455 err = hfs_bmap_reserve(src_fd.tree, 4 * (int)src_fd.tree->depth - 1);
456 if (err)
457 goto out;
458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 /* find the old dir entry and read the data */
Sougata Santra89ac9b42014-12-18 16:17:12 -0800460 err = hfsplus_cat_build_key(sb, src_fd.search_key,
461 src_dir->i_ino, src_name);
462 if (unlikely(err))
463 goto out;
464
Vyacheslav Dubeyko324ef392013-02-27 17:03:04 -0800465 err = hfs_brec_find(&src_fd, hfs_find_rec_by_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 if (err)
467 goto out;
Greg Kroah-Hartman6f24f892012-05-04 12:09:39 -0700468 if (src_fd.entrylength > sizeof(entry) || src_fd.entrylength < 0) {
469 err = -EIO;
470 goto out;
471 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473 hfs_bnode_read(src_fd.bnode, &entry, src_fd.entryoffset,
474 src_fd.entrylength);
Sergei Antonovd7d673a2014-03-10 15:49:51 -0700475 type = be16_to_cpu(entry.type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
477 /* create new dir entry with the data from the old entry */
Sougata Santra89ac9b42014-12-18 16:17:12 -0800478 err = hfsplus_cat_build_key(sb, dst_fd.search_key,
479 dst_dir->i_ino, dst_name);
480 if (unlikely(err))
481 goto out;
482
Vyacheslav Dubeyko324ef392013-02-27 17:03:04 -0800483 err = hfs_brec_find(&dst_fd, hfs_find_rec_by_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 if (err != -ENOENT) {
485 if (!err)
486 err = -EEXIST;
487 goto out;
488 }
489
490 err = hfs_brec_insert(&dst_fd, &entry, src_fd.entrylength);
491 if (err)
492 goto out;
493 dst_dir->i_size++;
Sergei Antonovd7d673a2014-03-10 15:49:51 -0700494 if (type == HFSPLUS_FOLDER)
495 hfsplus_subfolders_inc(dst_dir);
Deepa Dinamani02027d42016-09-14 07:48:05 -0700496 dst_dir->i_mtime = dst_dir->i_ctime = current_time(dst_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 /* finally remove the old entry */
Sougata Santra89ac9b42014-12-18 16:17:12 -0800499 err = hfsplus_cat_build_key(sb, src_fd.search_key,
500 src_dir->i_ino, src_name);
501 if (unlikely(err))
502 goto out;
503
Vyacheslav Dubeyko324ef392013-02-27 17:03:04 -0800504 err = hfs_brec_find(&src_fd, hfs_find_rec_by_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 if (err)
506 goto out;
507 err = hfs_brec_remove(&src_fd);
508 if (err)
509 goto out;
510 src_dir->i_size--;
Sergei Antonovd7d673a2014-03-10 15:49:51 -0700511 if (type == HFSPLUS_FOLDER)
512 hfsplus_subfolders_dec(src_dir);
Deepa Dinamani02027d42016-09-14 07:48:05 -0700513 src_dir->i_mtime = src_dir->i_ctime = current_time(src_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515 /* remove old thread entry */
Sougata Santra89ac9b42014-12-18 16:17:12 -0800516 hfsplus_cat_build_key_with_cnid(sb, src_fd.search_key, cnid);
Vyacheslav Dubeyko324ef392013-02-27 17:03:04 -0800517 err = hfs_brec_find(&src_fd, hfs_find_rec_by_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 if (err)
519 goto out;
520 type = hfs_bnode_read_u16(src_fd.bnode, src_fd.entryoffset);
521 err = hfs_brec_remove(&src_fd);
522 if (err)
523 goto out;
524
525 /* create new thread entry */
Sougata Santra89ac9b42014-12-18 16:17:12 -0800526 hfsplus_cat_build_key_with_cnid(sb, dst_fd.search_key, cnid);
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200527 entry_size = hfsplus_fill_cat_thread(sb, &entry, type,
528 dst_dir->i_ino, dst_name);
Sougata Santra89ac9b42014-12-18 16:17:12 -0800529 if (unlikely(entry_size < 0)) {
530 err = entry_size;
531 goto out;
532 }
533
Vyacheslav Dubeyko324ef392013-02-27 17:03:04 -0800534 err = hfs_brec_find(&dst_fd, hfs_find_rec_by_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 if (err != -ENOENT) {
536 if (!err)
537 err = -EEXIST;
538 goto out;
539 }
540 err = hfs_brec_insert(&dst_fd, &entry, entry_size);
Christoph Hellwige3494702010-11-23 14:38:15 +0100541
542 hfsplus_mark_inode_dirty(dst_dir, HFSPLUS_I_CAT_DIRTY);
543 hfsplus_mark_inode_dirty(src_dir, HFSPLUS_I_CAT_DIRTY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544out:
545 hfs_bnode_put(dst_fd.bnode);
546 hfs_find_exit(&src_fd);
547 return err;
548}