catalog.c 12 KB

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