catalog.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. #include "hfsplus_fs.h"
  11. #include "hfsplus_raw.h"
  12. int hfsplus_cat_case_cmp_key(const hfsplus_btree_key *k1,
  13. const hfsplus_btree_key *k2)
  14. {
  15. __be32 k1p, k2p;
  16. k1p = k1->cat.parent;
  17. k2p = k2->cat.parent;
  18. if (k1p != k2p)
  19. return be32_to_cpu(k1p) < be32_to_cpu(k2p) ? -1 : 1;
  20. return hfsplus_strcasecmp(&k1->cat.name, &k2->cat.name);
  21. }
  22. int hfsplus_cat_bin_cmp_key(const hfsplus_btree_key *k1,
  23. const hfsplus_btree_key *k2)
  24. {
  25. __be32 k1p, k2p;
  26. k1p = k1->cat.parent;
  27. k2p = k2->cat.parent;
  28. if (k1p != k2p)
  29. return be32_to_cpu(k1p) < be32_to_cpu(k2p) ? -1 : 1;
  30. return hfsplus_strcmp(&k1->cat.name, &k2->cat.name);
  31. }
  32. void hfsplus_cat_build_key(struct super_block *sb, hfsplus_btree_key *key,
  33. u32 parent, struct qstr *str)
  34. {
  35. int len;
  36. key->cat.parent = cpu_to_be32(parent);
  37. if (str) {
  38. hfsplus_asc2uni(sb, &key->cat.name, str->name, str->len);
  39. len = be16_to_cpu(key->cat.name.length);
  40. } else {
  41. key->cat.name.length = 0;
  42. len = 0;
  43. }
  44. key->key_len = cpu_to_be16(6 + 2 * len);
  45. }
  46. static void hfsplus_cat_build_key_uni(hfsplus_btree_key *key, u32 parent,
  47. struct hfsplus_unistr *name)
  48. {
  49. int ustrlen;
  50. ustrlen = be16_to_cpu(name->length);
  51. key->cat.parent = cpu_to_be32(parent);
  52. key->cat.name.length = cpu_to_be16(ustrlen);
  53. ustrlen *= 2;
  54. memcpy(key->cat.name.unicode, name->unicode, ustrlen);
  55. key->key_len = cpu_to_be16(6 + ustrlen);
  56. }
  57. void hfsplus_cat_set_perms(struct inode *inode, struct hfsplus_perm *perms)
  58. {
  59. if (inode->i_flags & S_IMMUTABLE)
  60. perms->rootflags |= HFSPLUS_FLG_IMMUTABLE;
  61. else
  62. perms->rootflags &= ~HFSPLUS_FLG_IMMUTABLE;
  63. if (inode->i_flags & S_APPEND)
  64. perms->rootflags |= HFSPLUS_FLG_APPEND;
  65. else
  66. perms->rootflags &= ~HFSPLUS_FLG_APPEND;
  67. perms->userflags = HFSPLUS_I(inode)->userflags;
  68. perms->mode = cpu_to_be16(inode->i_mode);
  69. perms->owner = cpu_to_be32(inode->i_uid);
  70. perms->group = cpu_to_be32(inode->i_gid);
  71. if (S_ISREG(inode->i_mode))
  72. perms->dev = cpu_to_be32(inode->i_nlink);
  73. else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode))
  74. perms->dev = cpu_to_be32(inode->i_rdev);
  75. else
  76. perms->dev = 0;
  77. }
  78. static int hfsplus_cat_build_record(hfsplus_cat_entry *entry,
  79. u32 cnid, struct inode *inode)
  80. {
  81. struct hfsplus_sb_info *sbi = HFSPLUS_SB(inode->i_sb);
  82. if (S_ISDIR(inode->i_mode)) {
  83. struct hfsplus_cat_folder *folder;
  84. folder = &entry->folder;
  85. memset(folder, 0, sizeof(*folder));
  86. folder->type = cpu_to_be16(HFSPLUS_FOLDER);
  87. folder->id = cpu_to_be32(inode->i_ino);
  88. HFSPLUS_I(inode)->create_date =
  89. folder->create_date =
  90. folder->content_mod_date =
  91. folder->attribute_mod_date =
  92. folder->access_date = hfsp_now2mt();
  93. hfsplus_cat_set_perms(inode, &folder->permissions);
  94. if (inode == sbi->hidden_dir)
  95. /* invisible and namelocked */
  96. folder->user_info.frFlags = cpu_to_be16(0x5000);
  97. return sizeof(*folder);
  98. } else {
  99. struct hfsplus_cat_file *file;
  100. file = &entry->file;
  101. memset(file, 0, sizeof(*file));
  102. file->type = cpu_to_be16(HFSPLUS_FILE);
  103. file->flags = cpu_to_be16(HFSPLUS_FILE_THREAD_EXISTS);
  104. file->id = cpu_to_be32(cnid);
  105. HFSPLUS_I(inode)->create_date =
  106. file->create_date =
  107. file->content_mod_date =
  108. file->attribute_mod_date =
  109. file->access_date = hfsp_now2mt();
  110. if (cnid == inode->i_ino) {
  111. hfsplus_cat_set_perms(inode, &file->permissions);
  112. if (S_ISLNK(inode->i_mode)) {
  113. file->user_info.fdType =
  114. cpu_to_be32(HFSP_SYMLINK_TYPE);
  115. file->user_info.fdCreator =
  116. cpu_to_be32(HFSP_SYMLINK_CREATOR);
  117. } else {
  118. file->user_info.fdType =
  119. cpu_to_be32(sbi->type);
  120. file->user_info.fdCreator =
  121. cpu_to_be32(sbi->creator);
  122. }
  123. if (HFSPLUS_FLG_IMMUTABLE &
  124. (file->permissions.rootflags |
  125. file->permissions.userflags))
  126. file->flags |=
  127. cpu_to_be16(HFSPLUS_FILE_LOCKED);
  128. } else {
  129. file->user_info.fdType =
  130. cpu_to_be32(HFSP_HARDLINK_TYPE);
  131. file->user_info.fdCreator =
  132. cpu_to_be32(HFSP_HFSPLUS_CREATOR);
  133. file->user_info.fdFlags =
  134. cpu_to_be16(0x100);
  135. file->create_date =
  136. HFSPLUS_I(sbi->hidden_dir)->create_date;
  137. file->permissions.dev =
  138. cpu_to_be32(HFSPLUS_I(inode)->linkid);
  139. }
  140. return sizeof(*file);
  141. }
  142. }
  143. static int hfsplus_fill_cat_thread(struct super_block *sb,
  144. hfsplus_cat_entry *entry, int type,
  145. u32 parentid, struct qstr *str)
  146. {
  147. entry->type = cpu_to_be16(type);
  148. entry->thread.reserved = 0;
  149. entry->thread.parentID = cpu_to_be32(parentid);
  150. hfsplus_asc2uni(sb, &entry->thread.nodeName, str->name, str->len);
  151. return 10 + be16_to_cpu(entry->thread.nodeName.length) * 2;
  152. }
  153. /* Try to get a catalog entry for given catalog id */
  154. int hfsplus_find_cat(struct super_block *sb, u32 cnid,
  155. struct hfs_find_data *fd)
  156. {
  157. hfsplus_cat_entry tmp;
  158. int err;
  159. u16 type;
  160. hfsplus_cat_build_key(sb, fd->search_key, cnid, NULL);
  161. err = hfs_brec_read(fd, &tmp, sizeof(hfsplus_cat_entry));
  162. if (err)
  163. return err;
  164. type = be16_to_cpu(tmp.type);
  165. if (type != HFSPLUS_FOLDER_THREAD && type != HFSPLUS_FILE_THREAD) {
  166. printk(KERN_ERR "hfs: found bad thread record in catalog\n");
  167. return -EIO;
  168. }
  169. if (be16_to_cpu(tmp.thread.nodeName.length) > 255) {
  170. printk(KERN_ERR "hfs: catalog name length corrupted\n");
  171. return -EIO;
  172. }
  173. hfsplus_cat_build_key_uni(fd->search_key,
  174. be32_to_cpu(tmp.thread.parentID),
  175. &tmp.thread.nodeName);
  176. return hfs_brec_find(fd);
  177. }
  178. int hfsplus_create_cat(u32 cnid, struct inode *dir,
  179. struct qstr *str, struct inode *inode)
  180. {
  181. struct super_block *sb = dir->i_sb;
  182. struct hfs_find_data fd;
  183. hfsplus_cat_entry entry;
  184. int entry_size;
  185. int err;
  186. dprint(DBG_CAT_MOD, "create_cat: %s,%u(%d)\n",
  187. str->name, cnid, inode->i_nlink);
  188. err = hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd);
  189. if (err)
  190. return err;
  191. hfsplus_cat_build_key(sb, fd.search_key, cnid, NULL);
  192. entry_size = hfsplus_fill_cat_thread(sb, &entry,
  193. S_ISDIR(inode->i_mode) ?
  194. HFSPLUS_FOLDER_THREAD : HFSPLUS_FILE_THREAD,
  195. dir->i_ino, str);
  196. err = hfs_brec_find(&fd);
  197. if (err != -ENOENT) {
  198. if (!err)
  199. err = -EEXIST;
  200. goto err2;
  201. }
  202. err = hfs_brec_insert(&fd, &entry, entry_size);
  203. if (err)
  204. goto err2;
  205. hfsplus_cat_build_key(sb, fd.search_key, dir->i_ino, str);
  206. entry_size = hfsplus_cat_build_record(&entry, cnid, inode);
  207. err = hfs_brec_find(&fd);
  208. if (err != -ENOENT) {
  209. /* panic? */
  210. if (!err)
  211. err = -EEXIST;
  212. goto err1;
  213. }
  214. err = hfs_brec_insert(&fd, &entry, entry_size);
  215. if (err)
  216. goto err1;
  217. dir->i_size++;
  218. dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
  219. hfsplus_mark_inode_dirty(dir, HFSPLUS_I_CAT_DIRTY);
  220. hfs_find_exit(&fd);
  221. return 0;
  222. err1:
  223. hfsplus_cat_build_key(sb, fd.search_key, cnid, NULL);
  224. if (!hfs_brec_find(&fd))
  225. hfs_brec_remove(&fd);
  226. err2:
  227. hfs_find_exit(&fd);
  228. return err;
  229. }
  230. int hfsplus_delete_cat(u32 cnid, struct inode *dir, struct qstr *str)
  231. {
  232. struct super_block *sb = dir->i_sb;
  233. struct hfs_find_data fd;
  234. struct hfsplus_fork_raw fork;
  235. struct list_head *pos;
  236. int err, off;
  237. u16 type;
  238. dprint(DBG_CAT_MOD, "delete_cat: %s,%u\n",
  239. str ? str->name : NULL, cnid);
  240. err = hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd);
  241. if (err)
  242. return err;
  243. if (!str) {
  244. int len;
  245. hfsplus_cat_build_key(sb, fd.search_key, cnid, NULL);
  246. err = hfs_brec_find(&fd);
  247. if (err)
  248. goto out;
  249. off = fd.entryoffset +
  250. offsetof(struct hfsplus_cat_thread, nodeName);
  251. fd.search_key->cat.parent = cpu_to_be32(dir->i_ino);
  252. hfs_bnode_read(fd.bnode,
  253. &fd.search_key->cat.name.length, off, 2);
  254. len = be16_to_cpu(fd.search_key->cat.name.length) * 2;
  255. hfs_bnode_read(fd.bnode,
  256. &fd.search_key->cat.name.unicode,
  257. off + 2, len);
  258. fd.search_key->key_len = cpu_to_be16(6 + len);
  259. } else
  260. hfsplus_cat_build_key(sb, fd.search_key, dir->i_ino, str);
  261. err = hfs_brec_find(&fd);
  262. if (err)
  263. goto out;
  264. type = hfs_bnode_read_u16(fd.bnode, fd.entryoffset);
  265. if (type == HFSPLUS_FILE) {
  266. #if 0
  267. off = fd.entryoffset + offsetof(hfsplus_cat_file, data_fork);
  268. hfs_bnode_read(fd.bnode, &fork, off, sizeof(fork));
  269. hfsplus_free_fork(sb, cnid, &fork, HFSPLUS_TYPE_DATA);
  270. #endif
  271. off = fd.entryoffset +
  272. offsetof(struct hfsplus_cat_file, rsrc_fork);
  273. hfs_bnode_read(fd.bnode, &fork, off, sizeof(fork));
  274. hfsplus_free_fork(sb, cnid, &fork, HFSPLUS_TYPE_RSRC);
  275. }
  276. list_for_each(pos, &HFSPLUS_I(dir)->open_dir_list) {
  277. struct hfsplus_readdir_data *rd =
  278. list_entry(pos, struct hfsplus_readdir_data, list);
  279. if (fd.tree->keycmp(fd.search_key, (void *)&rd->key) < 0)
  280. rd->file->f_pos--;
  281. }
  282. err = hfs_brec_remove(&fd);
  283. if (err)
  284. goto out;
  285. hfsplus_cat_build_key(sb, fd.search_key, cnid, NULL);
  286. err = hfs_brec_find(&fd);
  287. if (err)
  288. goto out;
  289. err = hfs_brec_remove(&fd);
  290. if (err)
  291. goto out;
  292. dir->i_size--;
  293. dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
  294. hfsplus_mark_inode_dirty(dir, HFSPLUS_I_CAT_DIRTY);
  295. out:
  296. hfs_find_exit(&fd);
  297. return err;
  298. }
  299. int hfsplus_rename_cat(u32 cnid,
  300. struct inode *src_dir, struct qstr *src_name,
  301. struct inode *dst_dir, struct qstr *dst_name)
  302. {
  303. struct super_block *sb = src_dir->i_sb;
  304. struct hfs_find_data src_fd, dst_fd;
  305. hfsplus_cat_entry entry;
  306. int entry_size, type;
  307. int err;
  308. dprint(DBG_CAT_MOD, "rename_cat: %u - %lu,%s - %lu,%s\n",
  309. cnid, src_dir->i_ino, src_name->name,
  310. dst_dir->i_ino, dst_name->name);
  311. err = hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &src_fd);
  312. if (err)
  313. return err;
  314. dst_fd = src_fd;
  315. /* find the old dir entry and read the data */
  316. hfsplus_cat_build_key(sb, src_fd.search_key, src_dir->i_ino, src_name);
  317. err = hfs_brec_find(&src_fd);
  318. if (err)
  319. goto out;
  320. if (src_fd.entrylength > sizeof(entry) || src_fd.entrylength < 0) {
  321. err = -EIO;
  322. goto out;
  323. }
  324. hfs_bnode_read(src_fd.bnode, &entry, src_fd.entryoffset,
  325. src_fd.entrylength);
  326. /* create new dir entry with the data from the old entry */
  327. hfsplus_cat_build_key(sb, dst_fd.search_key, dst_dir->i_ino, dst_name);
  328. err = hfs_brec_find(&dst_fd);
  329. if (err != -ENOENT) {
  330. if (!err)
  331. err = -EEXIST;
  332. goto out;
  333. }
  334. err = hfs_brec_insert(&dst_fd, &entry, src_fd.entrylength);
  335. if (err)
  336. goto out;
  337. dst_dir->i_size++;
  338. dst_dir->i_mtime = dst_dir->i_ctime = CURRENT_TIME_SEC;
  339. /* finally remove the old entry */
  340. hfsplus_cat_build_key(sb, src_fd.search_key, src_dir->i_ino, src_name);
  341. err = hfs_brec_find(&src_fd);
  342. if (err)
  343. goto out;
  344. err = hfs_brec_remove(&src_fd);
  345. if (err)
  346. goto out;
  347. src_dir->i_size--;
  348. src_dir->i_mtime = src_dir->i_ctime = CURRENT_TIME_SEC;
  349. /* remove old thread entry */
  350. hfsplus_cat_build_key(sb, src_fd.search_key, cnid, NULL);
  351. err = hfs_brec_find(&src_fd);
  352. if (err)
  353. goto out;
  354. type = hfs_bnode_read_u16(src_fd.bnode, src_fd.entryoffset);
  355. err = hfs_brec_remove(&src_fd);
  356. if (err)
  357. goto out;
  358. /* create new thread entry */
  359. hfsplus_cat_build_key(sb, dst_fd.search_key, cnid, NULL);
  360. entry_size = hfsplus_fill_cat_thread(sb, &entry, type,
  361. dst_dir->i_ino, dst_name);
  362. err = hfs_brec_find(&dst_fd);
  363. if (err != -ENOENT) {
  364. if (!err)
  365. err = -EEXIST;
  366. goto out;
  367. }
  368. err = hfs_brec_insert(&dst_fd, &entry, entry_size);
  369. hfsplus_mark_inode_dirty(dst_dir, HFSPLUS_I_CAT_DIRTY);
  370. hfsplus_mark_inode_dirty(src_dir, HFSPLUS_I_CAT_DIRTY);
  371. out:
  372. hfs_bnode_put(dst_fd.bnode);
  373. hfs_find_exit(&src_fd);
  374. return err;
  375. }