catalog.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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(CAT_MOD, "create_cat: %s,%u(%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. hfs_cat_build_key(sb, fd.search_key, cnid, NULL);
  92. entry_size = hfs_cat_build_thread(sb, &entry, S_ISDIR(inode->i_mode) ?
  93. HFS_CDR_THD : HFS_CDR_FTH,
  94. dir->i_ino, str);
  95. err = hfs_brec_find(&fd);
  96. if (err != -ENOENT) {
  97. if (!err)
  98. err = -EEXIST;
  99. goto err2;
  100. }
  101. err = hfs_brec_insert(&fd, &entry, entry_size);
  102. if (err)
  103. goto err2;
  104. hfs_cat_build_key(sb, fd.search_key, dir->i_ino, str);
  105. entry_size = hfs_cat_build_record(&entry, cnid, inode);
  106. err = hfs_brec_find(&fd);
  107. if (err != -ENOENT) {
  108. /* panic? */
  109. if (!err)
  110. err = -EEXIST;
  111. goto err1;
  112. }
  113. err = hfs_brec_insert(&fd, &entry, entry_size);
  114. if (err)
  115. goto err1;
  116. dir->i_size++;
  117. dir->i_mtime = dir->i_ctime = current_time(dir);
  118. mark_inode_dirty(dir);
  119. hfs_find_exit(&fd);
  120. return 0;
  121. err1:
  122. hfs_cat_build_key(sb, fd.search_key, cnid, NULL);
  123. if (!hfs_brec_find(&fd))
  124. hfs_brec_remove(&fd);
  125. err2:
  126. hfs_find_exit(&fd);
  127. return err;
  128. }
  129. /*
  130. * hfs_cat_compare()
  131. *
  132. * Description:
  133. * This is the comparison function used for the catalog B-tree. In
  134. * comparing catalog B-tree entries, the parent id is the most
  135. * significant field (compared as unsigned ints). The name field is
  136. * the least significant (compared in "Macintosh lexical order",
  137. * see hfs_strcmp() in string.c)
  138. * Input Variable(s):
  139. * struct hfs_cat_key *key1: pointer to the first key to compare
  140. * struct hfs_cat_key *key2: pointer to the second key to compare
  141. * Output Variable(s):
  142. * NONE
  143. * Returns:
  144. * int: negative if key1<key2, positive if key1>key2, and 0 if key1==key2
  145. * Preconditions:
  146. * key1 and key2 point to "valid" (struct hfs_cat_key)s.
  147. * Postconditions:
  148. * This function has no side-effects
  149. */
  150. int hfs_cat_keycmp(const btree_key *key1, const btree_key *key2)
  151. {
  152. __be32 k1p, k2p;
  153. k1p = key1->cat.ParID;
  154. k2p = key2->cat.ParID;
  155. if (k1p != k2p)
  156. return be32_to_cpu(k1p) < be32_to_cpu(k2p) ? -1 : 1;
  157. return hfs_strcmp(key1->cat.CName.name, key1->cat.CName.len,
  158. key2->cat.CName.name, key2->cat.CName.len);
  159. }
  160. /* Try to get a catalog entry for given catalog id */
  161. // move to read_super???
  162. int hfs_cat_find_brec(struct super_block *sb, u32 cnid,
  163. struct hfs_find_data *fd)
  164. {
  165. hfs_cat_rec rec;
  166. int res, len, type;
  167. hfs_cat_build_key(sb, fd->search_key, cnid, NULL);
  168. res = hfs_brec_read(fd, &rec, sizeof(rec));
  169. if (res)
  170. return res;
  171. type = rec.type;
  172. if (type != HFS_CDR_THD && type != HFS_CDR_FTH) {
  173. pr_err("found bad thread record in catalog\n");
  174. return -EIO;
  175. }
  176. fd->search_key->cat.ParID = rec.thread.ParID;
  177. len = fd->search_key->cat.CName.len = rec.thread.CName.len;
  178. if (len > HFS_NAMELEN) {
  179. pr_err("bad catalog namelength\n");
  180. return -EIO;
  181. }
  182. memcpy(fd->search_key->cat.CName.name, rec.thread.CName.name, len);
  183. return hfs_brec_find(fd);
  184. }
  185. /*
  186. * hfs_cat_delete()
  187. *
  188. * Delete the indicated file or directory.
  189. * The associated thread is also removed unless ('with_thread'==0).
  190. */
  191. int hfs_cat_delete(u32 cnid, struct inode *dir, const struct qstr *str)
  192. {
  193. struct super_block *sb;
  194. struct hfs_find_data fd;
  195. struct hfs_readdir_data *rd;
  196. int res, type;
  197. hfs_dbg(CAT_MOD, "delete_cat: %s,%u\n", str ? str->name : NULL, cnid);
  198. sb = dir->i_sb;
  199. res = hfs_find_init(HFS_SB(sb)->cat_tree, &fd);
  200. if (res)
  201. return res;
  202. hfs_cat_build_key(sb, fd.search_key, dir->i_ino, str);
  203. res = hfs_brec_find(&fd);
  204. if (res)
  205. goto out;
  206. type = hfs_bnode_read_u8(fd.bnode, fd.entryoffset);
  207. if (type == HFS_CDR_FIL) {
  208. struct hfs_cat_file file;
  209. hfs_bnode_read(fd.bnode, &file, fd.entryoffset, sizeof(file));
  210. if (be32_to_cpu(file.FlNum) == cnid) {
  211. #if 0
  212. hfs_free_fork(sb, &file, HFS_FK_DATA);
  213. #endif
  214. hfs_free_fork(sb, &file, HFS_FK_RSRC);
  215. }
  216. }
  217. /* we only need to take spinlock for exclusion with ->release() */
  218. spin_lock(&HFS_I(dir)->open_dir_lock);
  219. list_for_each_entry(rd, &HFS_I(dir)->open_dir_list, list) {
  220. if (fd.tree->keycmp(fd.search_key, (void *)&rd->key) < 0)
  221. rd->file->f_pos--;
  222. }
  223. spin_unlock(&HFS_I(dir)->open_dir_lock);
  224. res = hfs_brec_remove(&fd);
  225. if (res)
  226. goto out;
  227. hfs_cat_build_key(sb, fd.search_key, cnid, NULL);
  228. res = hfs_brec_find(&fd);
  229. if (!res) {
  230. res = hfs_brec_remove(&fd);
  231. if (res)
  232. goto out;
  233. }
  234. dir->i_size--;
  235. dir->i_mtime = dir->i_ctime = current_time(dir);
  236. mark_inode_dirty(dir);
  237. res = 0;
  238. out:
  239. hfs_find_exit(&fd);
  240. return res;
  241. }
  242. /*
  243. * hfs_cat_move()
  244. *
  245. * Rename a file or directory, possibly to a new directory.
  246. * If the destination exists it is removed and a
  247. * (struct hfs_cat_entry) for it is returned in '*result'.
  248. */
  249. int hfs_cat_move(u32 cnid, struct inode *src_dir, const struct qstr *src_name,
  250. struct inode *dst_dir, const struct qstr *dst_name)
  251. {
  252. struct super_block *sb;
  253. struct hfs_find_data src_fd, dst_fd;
  254. union hfs_cat_rec entry;
  255. int entry_size, type;
  256. int err;
  257. hfs_dbg(CAT_MOD, "rename_cat: %u - %lu,%s - %lu,%s\n",
  258. cnid, src_dir->i_ino, src_name->name,
  259. dst_dir->i_ino, dst_name->name);
  260. sb = src_dir->i_sb;
  261. err = hfs_find_init(HFS_SB(sb)->cat_tree, &src_fd);
  262. if (err)
  263. return err;
  264. dst_fd = src_fd;
  265. /* find the old dir entry and read the data */
  266. hfs_cat_build_key(sb, src_fd.search_key, src_dir->i_ino, src_name);
  267. err = hfs_brec_find(&src_fd);
  268. if (err)
  269. goto out;
  270. if (src_fd.entrylength > sizeof(entry) || src_fd.entrylength < 0) {
  271. err = -EIO;
  272. goto out;
  273. }
  274. hfs_bnode_read(src_fd.bnode, &entry, src_fd.entryoffset,
  275. src_fd.entrylength);
  276. /* create new dir entry with the data from the old entry */
  277. hfs_cat_build_key(sb, dst_fd.search_key, dst_dir->i_ino, dst_name);
  278. err = hfs_brec_find(&dst_fd);
  279. if (err != -ENOENT) {
  280. if (!err)
  281. err = -EEXIST;
  282. goto out;
  283. }
  284. err = hfs_brec_insert(&dst_fd, &entry, src_fd.entrylength);
  285. if (err)
  286. goto out;
  287. dst_dir->i_size++;
  288. dst_dir->i_mtime = dst_dir->i_ctime = current_time(dst_dir);
  289. mark_inode_dirty(dst_dir);
  290. /* finally remove the old entry */
  291. hfs_cat_build_key(sb, src_fd.search_key, src_dir->i_ino, src_name);
  292. err = hfs_brec_find(&src_fd);
  293. if (err)
  294. goto out;
  295. err = hfs_brec_remove(&src_fd);
  296. if (err)
  297. goto out;
  298. src_dir->i_size--;
  299. src_dir->i_mtime = src_dir->i_ctime = current_time(src_dir);
  300. mark_inode_dirty(src_dir);
  301. type = entry.type;
  302. if (type == HFS_CDR_FIL && !(entry.file.Flags & HFS_FIL_THD))
  303. goto out;
  304. /* remove old thread entry */
  305. hfs_cat_build_key(sb, src_fd.search_key, cnid, NULL);
  306. err = hfs_brec_find(&src_fd);
  307. if (err)
  308. goto out;
  309. err = hfs_brec_remove(&src_fd);
  310. if (err)
  311. goto out;
  312. /* create new thread entry */
  313. hfs_cat_build_key(sb, dst_fd.search_key, cnid, NULL);
  314. entry_size = hfs_cat_build_thread(sb, &entry, type == HFS_CDR_FIL ? HFS_CDR_FTH : HFS_CDR_THD,
  315. dst_dir->i_ino, dst_name);
  316. err = hfs_brec_find(&dst_fd);
  317. if (err != -ENOENT) {
  318. if (!err)
  319. err = -EEXIST;
  320. goto out;
  321. }
  322. err = hfs_brec_insert(&dst_fd, &entry, entry_size);
  323. out:
  324. hfs_bnode_put(dst_fd.bnode);
  325. hfs_find_exit(&src_fd);
  326. return err;
  327. }