File Index
Files and directories
Sign in
FILE

catalog.c

File preview with syntax highlighting.

linux-6.19.14/fs/hfs/catalog.c C
  1/*
  2 *  linux/fs/hfs/catalog.c
  3 *
  4 * Copyright (C) 1995-1997  Paul H. Hargrove
  5 * (C) 2003 Ardis Technologies <roman@ardistech.com>
  6 * This file may be distributed under the terms of the GNU General Public License.
  7 *
  8 * This file contains the functions related to the catalog B-tree.
  9 *
 10 * Cache code shamelessly stolen from
 11 *     linux/fs/inode.c Copyright (C) 1991, 1992  Linus Torvalds
 12 *     re-shamelessly stolen Copyright (C) 1997 Linus Torvalds
 13 */
 14
 15#include "hfs_fs.h"
 16#include "btree.h"
 17
 18/*
 19 * hfs_cat_build_key()
 20 *
 21 * Given the ID of the parent and the name build a search key.
 22 */
 23void hfs_cat_build_key(struct super_block *sb, btree_key *key, u32 parent, const struct qstr *name)
 24{
 25	key->cat.reserved = 0;
 26	key->cat.ParID = cpu_to_be32(parent);
 27	if (name) {
 28		hfs_asc2mac(sb, &key->cat.CName, name);
 29		key->key_len = 6 + key->cat.CName.len;
 30	} else {
 31		memset(&key->cat.CName, 0, sizeof(struct hfs_name));
 32		key->key_len = 6;
 33	}
 34}
 35
 36static int hfs_cat_build_record(hfs_cat_rec *rec, u32 cnid, struct inode *inode)
 37{
 38	__be32 mtime = hfs_mtime();
 39
 40	memset(rec, 0, sizeof(*rec));
 41	if (S_ISDIR(inode->i_mode)) {
 42		rec->type = HFS_CDR_DIR;
 43		rec->dir.DirID = cpu_to_be32(cnid);
 44		rec->dir.CrDat = mtime;
 45		rec->dir.MdDat = mtime;
 46		rec->dir.BkDat = 0;
 47		rec->dir.UsrInfo.frView = cpu_to_be16(0xff);
 48		return sizeof(struct hfs_cat_dir);
 49	} else {
 50		/* init some fields for the file record */
 51		rec->type = HFS_CDR_FIL;
 52		rec->file.Flags = HFS_FIL_USED | HFS_FIL_THD;
 53		if (!(inode->i_mode & S_IWUSR))
 54			rec->file.Flags |= HFS_FIL_LOCK;
 55		rec->file.FlNum = cpu_to_be32(cnid);
 56		rec->file.CrDat = mtime;
 57		rec->file.MdDat = mtime;
 58		rec->file.BkDat = 0;
 59		rec->file.UsrWds.fdType = HFS_SB(inode->i_sb)->s_type;
 60		rec->file.UsrWds.fdCreator = HFS_SB(inode->i_sb)->s_creator;
 61		return sizeof(struct hfs_cat_file);
 62	}
 63}
 64
 65static int hfs_cat_build_thread(struct super_block *sb,
 66				hfs_cat_rec *rec, int type,
 67				u32 parentid, const struct qstr *name)
 68{
 69	rec->type = type;
 70	memset(rec->thread.reserved, 0, sizeof(rec->thread.reserved));
 71	rec->thread.ParID = cpu_to_be32(parentid);
 72	hfs_asc2mac(sb, &rec->thread.CName, name);
 73	return sizeof(struct hfs_cat_thread);
 74}
 75
 76/*
 77 * create_entry()
 78 *
 79 * Add a new file or directory to the catalog B-tree and
 80 * return a (struct hfs_cat_entry) for it in '*result'.
 81 */
 82int hfs_cat_create(u32 cnid, struct inode *dir, const struct qstr *str, struct inode *inode)
 83{
 84	struct hfs_find_data fd;
 85	struct super_block *sb;
 86	union hfs_cat_rec entry;
 87	int entry_size;
 88	int err;
 89
 90	hfs_dbg("name %s, cnid %u, i_nlink %d\n",
 91		str->name, cnid, inode->i_nlink);
 92	if (dir->i_size >= HFS_MAX_VALENCE)
 93		return -ENOSPC;
 94
 95	sb = dir->i_sb;
 96	err = hfs_find_init(HFS_SB(sb)->cat_tree, &fd);
 97	if (err)
 98		return err;
 99
100	/*
101	 * Fail early and avoid ENOSPC during the btree operations. We may
102	 * have to split the root node at most once.
103	 */
104	err = hfs_bmap_reserve(fd.tree, 2 * fd.tree->depth);
105	if (err)
106		goto err2;
107
108	hfs_cat_build_key(sb, fd.search_key, cnid, NULL);
109	entry_size = hfs_cat_build_thread(sb, &entry, S_ISDIR(inode->i_mode) ?
110			HFS_CDR_THD : HFS_CDR_FTH,
111			dir->i_ino, str);
112	err = hfs_brec_find(&fd);
113	if (err != -ENOENT) {
114		if (!err)
115			err = -EEXIST;
116		goto err2;
117	}
118	err = hfs_brec_insert(&fd, &entry, entry_size);
119	if (err)
120		goto err2;
121
122	hfs_cat_build_key(sb, fd.search_key, dir->i_ino, str);
123	entry_size = hfs_cat_build_record(&entry, cnid, inode);
124	err = hfs_brec_find(&fd);
125	if (err != -ENOENT) {
126		/* panic? */
127		if (!err)
128			err = -EEXIST;
129		goto err1;
130	}
131	err = hfs_brec_insert(&fd, &entry, entry_size);
132	if (err)
133		goto err1;
134
135	dir->i_size++;
136	inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
137	mark_inode_dirty(dir);
138	hfs_find_exit(&fd);
139	return 0;
140
141err1:
142	hfs_cat_build_key(sb, fd.search_key, cnid, NULL);
143	if (!hfs_brec_find(&fd))
144		hfs_brec_remove(&fd);
145err2:
146	hfs_find_exit(&fd);
147	return err;
148}
149
150/*
151 * hfs_cat_compare()
152 *
153 * Description:
154 *   This is the comparison function used for the catalog B-tree.  In
155 *   comparing catalog B-tree entries, the parent id is the most
156 *   significant field (compared as unsigned ints).  The name field is
157 *   the least significant (compared in "Macintosh lexical order",
158 *   see hfs_strcmp() in string.c)
159 * Input Variable(s):
160 *   struct hfs_cat_key *key1: pointer to the first key to compare
161 *   struct hfs_cat_key *key2: pointer to the second key to compare
162 * Output Variable(s):
163 *   NONE
164 * Returns:
165 *   int: negative if key1<key2, positive if key1>key2, and 0 if key1==key2
166 * Preconditions:
167 *   key1 and key2 point to "valid" (struct hfs_cat_key)s.
168 * Postconditions:
169 *   This function has no side-effects
170 */
171int hfs_cat_keycmp(const btree_key *key1, const btree_key *key2)
172{
173	__be32 k1p, k2p;
174
175	k1p = key1->cat.ParID;
176	k2p = key2->cat.ParID;
177
178	if (k1p != k2p)
179		return be32_to_cpu(k1p) < be32_to_cpu(k2p) ? -1 : 1;
180
181	return hfs_strcmp(key1->cat.CName.name, key1->cat.CName.len,
182			  key2->cat.CName.name, key2->cat.CName.len);
183}
184
185/* Try to get a catalog entry for given catalog id */
186// move to read_super???
187int hfs_cat_find_brec(struct super_block *sb, u32 cnid,
188		      struct hfs_find_data *fd)
189{
190	hfs_cat_rec rec;
191	int res, len, type;
192
193	hfs_cat_build_key(sb, fd->search_key, cnid, NULL);
194	res = hfs_brec_read(fd, &rec, sizeof(rec));
195	if (res)
196		return res;
197
198	type = rec.type;
199	if (type != HFS_CDR_THD && type != HFS_CDR_FTH) {
200		pr_err("found bad thread record in catalog\n");
201		return -EIO;
202	}
203
204	fd->search_key->cat.ParID = rec.thread.ParID;
205	len = fd->search_key->cat.CName.len = rec.thread.CName.len;
206	if (len > HFS_NAMELEN) {
207		pr_err("bad catalog namelength\n");
208		return -EIO;
209	}
210	memcpy(fd->search_key->cat.CName.name, rec.thread.CName.name, len);
211	return hfs_brec_find(fd);
212}
213
214static inline
215void hfs_set_next_unused_CNID(struct super_block *sb,
216				u32 deleted_cnid, u32 found_cnid)
217{
218	if (found_cnid < HFS_FIRSTUSER_CNID) {
219		atomic64_cmpxchg(&HFS_SB(sb)->next_id,
220				 deleted_cnid + 1, HFS_FIRSTUSER_CNID);
221	} else {
222		atomic64_cmpxchg(&HFS_SB(sb)->next_id,
223				 deleted_cnid + 1, found_cnid + 1);
224	}
225}
226
227/*
228 * hfs_correct_next_unused_CNID()
229 *
230 * Correct the next unused CNID of Catalog Tree.
231 */
232static
233int hfs_correct_next_unused_CNID(struct super_block *sb, u32 cnid)
234{
235	struct hfs_btree *cat_tree;
236	struct hfs_bnode *node;
237	s64 leaf_head;
238	s64 leaf_tail;
239	s64 node_id;
240
241	hfs_dbg("cnid %u, next_id %lld\n",
242		cnid, atomic64_read(&HFS_SB(sb)->next_id));
243
244	if ((cnid + 1) < atomic64_read(&HFS_SB(sb)->next_id)) {
245		/* next ID should be unchanged */
246		return 0;
247	}
248
249	cat_tree = HFS_SB(sb)->cat_tree;
250	leaf_head = cat_tree->leaf_head;
251	leaf_tail = cat_tree->leaf_tail;
252
253	if (leaf_head > leaf_tail) {
254		pr_err("node is corrupted: leaf_head %lld, leaf_tail %lld\n",
255			leaf_head, leaf_tail);
256		return -ERANGE;
257	}
258
259	node = hfs_bnode_find(cat_tree, leaf_tail);
260	if (IS_ERR(node)) {
261		pr_err("fail to find leaf node: node ID %lld\n",
262			leaf_tail);
263		return -ENOENT;
264	}
265
266	node_id = leaf_tail;
267
268	do {
269		int i;
270
271		if (node_id != leaf_tail) {
272			node = hfs_bnode_find(cat_tree, node_id);
273			if (IS_ERR(node))
274				return -ENOENT;
275		}
276
277		hfs_dbg("node %lld, leaf_tail %lld, leaf_head %lld\n",
278			node_id, leaf_tail, leaf_head);
279
280		hfs_bnode_dump(node);
281
282		for (i = node->num_recs - 1; i >= 0; i--) {
283			hfs_cat_rec rec;
284			u16 off, len, keylen;
285			int entryoffset;
286			int entrylength;
287			u32 found_cnid;
288
289			len = hfs_brec_lenoff(node, i, &off);
290			keylen = hfs_brec_keylen(node, i);
291			if (keylen == 0) {
292				pr_err("fail to get the keylen: "
293					"node_id %lld, record index %d\n",
294					node_id, i);
295				return -EINVAL;
296			}
297
298			entryoffset = off + keylen;
299			entrylength = len - keylen;
300
301			if (entrylength > sizeof(rec)) {
302				pr_err("unexpected record length: "
303					"entrylength %d\n",
304					entrylength);
305				return -EINVAL;
306			}
307
308			hfs_bnode_read(node, &rec, entryoffset, entrylength);
309
310			if (rec.type == HFS_CDR_DIR) {
311				found_cnid = be32_to_cpu(rec.dir.DirID);
312				hfs_dbg("found_cnid %u\n", found_cnid);
313				hfs_set_next_unused_CNID(sb, cnid, found_cnid);
314				hfs_bnode_put(node);
315				return 0;
316			} else if (rec.type == HFS_CDR_FIL) {
317				found_cnid = be32_to_cpu(rec.file.FlNum);
318				hfs_dbg("found_cnid %u\n", found_cnid);
319				hfs_set_next_unused_CNID(sb, cnid, found_cnid);
320				hfs_bnode_put(node);
321				return 0;
322			}
323		}
324
325		node_id = node->prev;
326		hfs_bnode_put(node);
327
328	} while (node_id >= leaf_head);
329
330	return -ENOENT;
331}
332
333/*
334 * hfs_cat_delete()
335 *
336 * Delete the indicated file or directory.
337 * The associated thread is also removed unless ('with_thread'==0).
338 */
339int hfs_cat_delete(u32 cnid, struct inode *dir, const struct qstr *str)
340{
341	struct super_block *sb;
342	struct hfs_find_data fd;
343	struct hfs_readdir_data *rd;
344	int res, type;
345
346	hfs_dbg("name %s, cnid %u\n", str ? str->name : NULL, cnid);
347	sb = dir->i_sb;
348	res = hfs_find_init(HFS_SB(sb)->cat_tree, &fd);
349	if (res)
350		return res;
351
352	hfs_cat_build_key(sb, fd.search_key, dir->i_ino, str);
353	res = hfs_brec_find(&fd);
354	if (res)
355		goto out;
356
357	type = hfs_bnode_read_u8(fd.bnode, fd.entryoffset);
358	if (type == HFS_CDR_FIL) {
359		struct hfs_cat_file file;
360		hfs_bnode_read(fd.bnode, &file, fd.entryoffset, sizeof(file));
361		if (be32_to_cpu(file.FlNum) == cnid) {
362#if 0
363			hfs_free_fork(sb, &file, HFS_FK_DATA);
364#endif
365			hfs_free_fork(sb, &file, HFS_FK_RSRC);
366		}
367	}
368
369	/* we only need to take spinlock for exclusion with ->release() */
370	spin_lock(&HFS_I(dir)->open_dir_lock);
371	list_for_each_entry(rd, &HFS_I(dir)->open_dir_list, list) {
372		if (fd.tree->keycmp(fd.search_key, (void *)&rd->key) < 0)
373			rd->file->f_pos--;
374	}
375	spin_unlock(&HFS_I(dir)->open_dir_lock);
376
377	res = hfs_brec_remove(&fd);
378	if (res)
379		goto out;
380
381	hfs_cat_build_key(sb, fd.search_key, cnid, NULL);
382	res = hfs_brec_find(&fd);
383	if (!res) {
384		res = hfs_brec_remove(&fd);
385		if (res)
386			goto out;
387	}
388
389	dir->i_size--;
390	inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
391	mark_inode_dirty(dir);
392
393	res = hfs_correct_next_unused_CNID(sb, cnid);
394	if (res)
395		goto out;
396
397	res = 0;
398out:
399	hfs_find_exit(&fd);
400
401	return res;
402}
403
404/*
405 * hfs_cat_move()
406 *
407 * Rename a file or directory, possibly to a new directory.
408 * If the destination exists it is removed and a
409 * (struct hfs_cat_entry) for it is returned in '*result'.
410 */
411int hfs_cat_move(u32 cnid, struct inode *src_dir, const struct qstr *src_name,
412		 struct inode *dst_dir, const struct qstr *dst_name)
413{
414	struct super_block *sb;
415	struct hfs_find_data src_fd, dst_fd;
416	union hfs_cat_rec entry;
417	int entry_size, type;
418	int err;
419
420	hfs_dbg("cnid %u - (ino %lu, name %s) - (ino %lu, name %s)\n",
421		cnid, src_dir->i_ino, src_name->name,
422		dst_dir->i_ino, dst_name->name);
423	sb = src_dir->i_sb;
424	err = hfs_find_init(HFS_SB(sb)->cat_tree, &src_fd);
425	if (err)
426		return err;
427	dst_fd = src_fd;
428
429	/*
430	 * Fail early and avoid ENOSPC during the btree operations. We may
431	 * have to split the root node at most once.
432	 */
433	err = hfs_bmap_reserve(src_fd.tree, 2 * src_fd.tree->depth);
434	if (err)
435		goto out;
436
437	/* find the old dir entry and read the data */
438	hfs_cat_build_key(sb, src_fd.search_key, src_dir->i_ino, src_name);
439	err = hfs_brec_find(&src_fd);
440	if (err)
441		goto out;
442	if (src_fd.entrylength > sizeof(entry) || src_fd.entrylength < 0) {
443		err = -EIO;
444		goto out;
445	}
446
447	hfs_bnode_read(src_fd.bnode, &entry, src_fd.entryoffset,
448			    src_fd.entrylength);
449
450	/* create new dir entry with the data from the old entry */
451	hfs_cat_build_key(sb, dst_fd.search_key, dst_dir->i_ino, dst_name);
452	err = hfs_brec_find(&dst_fd);
453	if (err != -ENOENT) {
454		if (!err)
455			err = -EEXIST;
456		goto out;
457	}
458
459	err = hfs_brec_insert(&dst_fd, &entry, src_fd.entrylength);
460	if (err)
461		goto out;
462	dst_dir->i_size++;
463	inode_set_mtime_to_ts(dst_dir, inode_set_ctime_current(dst_dir));
464	mark_inode_dirty(dst_dir);
465
466	/* finally remove the old entry */
467	hfs_cat_build_key(sb, src_fd.search_key, src_dir->i_ino, src_name);
468	err = hfs_brec_find(&src_fd);
469	if (err)
470		goto out;
471	err = hfs_brec_remove(&src_fd);
472	if (err)
473		goto out;
474	src_dir->i_size--;
475	inode_set_mtime_to_ts(src_dir, inode_set_ctime_current(src_dir));
476	mark_inode_dirty(src_dir);
477
478	type = entry.type;
479	if (type == HFS_CDR_FIL && !(entry.file.Flags & HFS_FIL_THD))
480		goto out;
481
482	/* remove old thread entry */
483	hfs_cat_build_key(sb, src_fd.search_key, cnid, NULL);
484	err = hfs_brec_find(&src_fd);
485	if (err)
486		goto out;
487	err = hfs_brec_remove(&src_fd);
488	if (err)
489		goto out;
490
491	/* create new thread entry */
492	hfs_cat_build_key(sb, dst_fd.search_key, cnid, NULL);
493	entry_size = hfs_cat_build_thread(sb, &entry, type == HFS_CDR_FIL ? HFS_CDR_FTH : HFS_CDR_THD,
494					dst_dir->i_ino, dst_name);
495	err = hfs_brec_find(&dst_fd);
496	if (err != -ENOENT) {
497		if (!err)
498			err = -EEXIST;
499		goto out;
500	}
501	err = hfs_brec_insert(&dst_fd, &entry, entry_size);
502out:
503	hfs_bnode_put(dst_fd.bnode);
504	hfs_find_exit(&src_fd);
505	return err;
506}
~/ Read-only file preview.