catalog.c 10 KB

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