catalog.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd);
  189. hfsplus_cat_build_key(sb, fd.search_key, cnid, NULL);
  190. entry_size = hfsplus_fill_cat_thread(sb, &entry,
  191. S_ISDIR(inode->i_mode) ?
  192. HFSPLUS_FOLDER_THREAD : HFSPLUS_FILE_THREAD,
  193. dir->i_ino, str);
  194. err = hfs_brec_find(&fd);
  195. if (err != -ENOENT) {
  196. if (!err)
  197. err = -EEXIST;
  198. goto err2;
  199. }
  200. err = hfs_brec_insert(&fd, &entry, entry_size);
  201. if (err)
  202. goto err2;
  203. hfsplus_cat_build_key(sb, fd.search_key, dir->i_ino, str);
  204. entry_size = hfsplus_cat_build_record(&entry, cnid, inode);
  205. err = hfs_brec_find(&fd);
  206. if (err != -ENOENT) {
  207. /* panic? */
  208. if (!err)
  209. err = -EEXIST;
  210. goto err1;
  211. }
  212. err = hfs_brec_insert(&fd, &entry, entry_size);
  213. if (err)
  214. goto err1;
  215. dir->i_size++;
  216. dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
  217. hfsplus_mark_inode_dirty(dir, HFSPLUS_I_CAT_DIRTY);
  218. hfs_find_exit(&fd);
  219. return 0;
  220. err1:
  221. hfsplus_cat_build_key(sb, fd.search_key, cnid, NULL);
  222. if (!hfs_brec_find(&fd))
  223. hfs_brec_remove(&fd);
  224. err2:
  225. hfs_find_exit(&fd);
  226. return err;
  227. }
  228. int hfsplus_delete_cat(u32 cnid, struct inode *dir, struct qstr *str)
  229. {
  230. struct super_block *sb = dir->i_sb;
  231. struct hfs_find_data fd;
  232. struct hfsplus_fork_raw fork;
  233. struct list_head *pos;
  234. int err, off;
  235. u16 type;
  236. dprint(DBG_CAT_MOD, "delete_cat: %s,%u\n",
  237. str ? str->name : NULL, cnid);
  238. hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd);
  239. if (!str) {
  240. int len;
  241. hfsplus_cat_build_key(sb, fd.search_key, cnid, NULL);
  242. err = hfs_brec_find(&fd);
  243. if (err)
  244. goto out;
  245. off = fd.entryoffset +
  246. offsetof(struct hfsplus_cat_thread, nodeName);
  247. fd.search_key->cat.parent = cpu_to_be32(dir->i_ino);
  248. hfs_bnode_read(fd.bnode,
  249. &fd.search_key->cat.name.length, off, 2);
  250. len = be16_to_cpu(fd.search_key->cat.name.length) * 2;
  251. hfs_bnode_read(fd.bnode,
  252. &fd.search_key->cat.name.unicode,
  253. off + 2, len);
  254. fd.search_key->key_len = cpu_to_be16(6 + len);
  255. } else
  256. hfsplus_cat_build_key(sb, fd.search_key, dir->i_ino, str);
  257. err = hfs_brec_find(&fd);
  258. if (err)
  259. goto out;
  260. type = hfs_bnode_read_u16(fd.bnode, fd.entryoffset);
  261. if (type == HFSPLUS_FILE) {
  262. #if 0
  263. off = fd.entryoffset + offsetof(hfsplus_cat_file, data_fork);
  264. hfs_bnode_read(fd.bnode, &fork, off, sizeof(fork));
  265. hfsplus_free_fork(sb, cnid, &fork, HFSPLUS_TYPE_DATA);
  266. #endif
  267. off = fd.entryoffset +
  268. offsetof(struct hfsplus_cat_file, rsrc_fork);
  269. hfs_bnode_read(fd.bnode, &fork, off, sizeof(fork));
  270. hfsplus_free_fork(sb, cnid, &fork, HFSPLUS_TYPE_RSRC);
  271. }
  272. list_for_each(pos, &HFSPLUS_I(dir)->open_dir_list) {
  273. struct hfsplus_readdir_data *rd =
  274. list_entry(pos, struct hfsplus_readdir_data, list);
  275. if (fd.tree->keycmp(fd.search_key, (void *)&rd->key) < 0)
  276. rd->file->f_pos--;
  277. }
  278. err = hfs_brec_remove(&fd);
  279. if (err)
  280. goto out;
  281. hfsplus_cat_build_key(sb, fd.search_key, cnid, NULL);
  282. err = hfs_brec_find(&fd);
  283. if (err)
  284. goto out;
  285. err = hfs_brec_remove(&fd);
  286. if (err)
  287. goto out;
  288. dir->i_size--;
  289. dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
  290. hfsplus_mark_inode_dirty(dir, HFSPLUS_I_CAT_DIRTY);
  291. out:
  292. hfs_find_exit(&fd);
  293. return err;
  294. }
  295. int hfsplus_rename_cat(u32 cnid,
  296. struct inode *src_dir, struct qstr *src_name,
  297. struct inode *dst_dir, struct qstr *dst_name)
  298. {
  299. struct super_block *sb = src_dir->i_sb;
  300. struct hfs_find_data src_fd, dst_fd;
  301. hfsplus_cat_entry entry;
  302. int entry_size, type;
  303. int err = 0;
  304. dprint(DBG_CAT_MOD, "rename_cat: %u - %lu,%s - %lu,%s\n",
  305. cnid, src_dir->i_ino, src_name->name,
  306. dst_dir->i_ino, dst_name->name);
  307. hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &src_fd);
  308. dst_fd = src_fd;
  309. /* find the old dir entry and read the data */
  310. hfsplus_cat_build_key(sb, src_fd.search_key, src_dir->i_ino, src_name);
  311. err = hfs_brec_find(&src_fd);
  312. if (err)
  313. goto out;
  314. if (src_fd.entrylength > sizeof(entry) || src_fd.entrylength < 0) {
  315. err = -EIO;
  316. goto out;
  317. }
  318. hfs_bnode_read(src_fd.bnode, &entry, src_fd.entryoffset,
  319. src_fd.entrylength);
  320. /* create new dir entry with the data from the old entry */
  321. hfsplus_cat_build_key(sb, dst_fd.search_key, dst_dir->i_ino, dst_name);
  322. err = hfs_brec_find(&dst_fd);
  323. if (err != -ENOENT) {
  324. if (!err)
  325. err = -EEXIST;
  326. goto out;
  327. }
  328. err = hfs_brec_insert(&dst_fd, &entry, src_fd.entrylength);
  329. if (err)
  330. goto out;
  331. dst_dir->i_size++;
  332. dst_dir->i_mtime = dst_dir->i_ctime = CURRENT_TIME_SEC;
  333. /* finally remove the old entry */
  334. hfsplus_cat_build_key(sb, src_fd.search_key, src_dir->i_ino, src_name);
  335. err = hfs_brec_find(&src_fd);
  336. if (err)
  337. goto out;
  338. err = hfs_brec_remove(&src_fd);
  339. if (err)
  340. goto out;
  341. src_dir->i_size--;
  342. src_dir->i_mtime = src_dir->i_ctime = CURRENT_TIME_SEC;
  343. /* remove old thread entry */
  344. hfsplus_cat_build_key(sb, src_fd.search_key, cnid, NULL);
  345. err = hfs_brec_find(&src_fd);
  346. if (err)
  347. goto out;
  348. type = hfs_bnode_read_u16(src_fd.bnode, src_fd.entryoffset);
  349. err = hfs_brec_remove(&src_fd);
  350. if (err)
  351. goto out;
  352. /* create new thread entry */
  353. hfsplus_cat_build_key(sb, dst_fd.search_key, cnid, NULL);
  354. entry_size = hfsplus_fill_cat_thread(sb, &entry, type,
  355. dst_dir->i_ino, dst_name);
  356. err = hfs_brec_find(&dst_fd);
  357. if (err != -ENOENT) {
  358. if (!err)
  359. err = -EEXIST;
  360. goto out;
  361. }
  362. err = hfs_brec_insert(&dst_fd, &entry, entry_size);
  363. hfsplus_mark_inode_dirty(dst_dir, HFSPLUS_I_CAT_DIRTY);
  364. hfsplus_mark_inode_dirty(src_dir, HFSPLUS_I_CAT_DIRTY);
  365. out:
  366. hfs_bnode_put(dst_fd.bnode);
  367. hfs_find_exit(&src_fd);
  368. return err;
  369. }