catalog.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. file->user_info.fdType = cpu_to_be32(HFSPLUS_SB(inode->i_sb).type);
  107. file->user_info.fdCreator = cpu_to_be32(HFSPLUS_SB(inode->i_sb).creator);
  108. if ((file->permissions.rootflags | file->permissions.userflags) & HFSPLUS_FLG_IMMUTABLE)
  109. file->flags |= cpu_to_be16(HFSPLUS_FILE_LOCKED);
  110. } else {
  111. file->user_info.fdType = cpu_to_be32(HFSP_HARDLINK_TYPE);
  112. file->user_info.fdCreator = cpu_to_be32(HFSP_HFSPLUS_CREATOR);
  113. file->user_info.fdFlags = cpu_to_be16(0x100);
  114. file->create_date = HFSPLUS_I(HFSPLUS_SB(inode->i_sb).hidden_dir).create_date;
  115. file->permissions.dev = cpu_to_be32(HFSPLUS_I(inode).dev);
  116. }
  117. return sizeof(*file);
  118. }
  119. }
  120. static int hfsplus_fill_cat_thread(struct super_block *sb,
  121. hfsplus_cat_entry *entry, int type,
  122. u32 parentid, struct qstr *str)
  123. {
  124. entry->type = cpu_to_be16(type);
  125. entry->thread.reserved = 0;
  126. entry->thread.parentID = cpu_to_be32(parentid);
  127. hfsplus_asc2uni(sb, &entry->thread.nodeName, str->name, str->len);
  128. return 10 + be16_to_cpu(entry->thread.nodeName.length) * 2;
  129. }
  130. /* Try to get a catalog entry for given catalog id */
  131. int hfsplus_find_cat(struct super_block *sb, u32 cnid,
  132. struct hfs_find_data *fd)
  133. {
  134. hfsplus_cat_entry tmp;
  135. int err;
  136. u16 type;
  137. hfsplus_cat_build_key(sb, fd->search_key, cnid, NULL);
  138. err = hfs_brec_read(fd, &tmp, sizeof(hfsplus_cat_entry));
  139. if (err)
  140. return err;
  141. type = be16_to_cpu(tmp.type);
  142. if (type != HFSPLUS_FOLDER_THREAD && type != HFSPLUS_FILE_THREAD) {
  143. printk(KERN_ERR "hfs: found bad thread record in catalog\n");
  144. return -EIO;
  145. }
  146. hfsplus_cat_build_key_uni(fd->search_key, be32_to_cpu(tmp.thread.parentID),
  147. &tmp.thread.nodeName);
  148. return hfs_brec_find(fd);
  149. }
  150. int hfsplus_create_cat(u32 cnid, struct inode *dir, struct qstr *str, struct inode *inode)
  151. {
  152. struct hfs_find_data fd;
  153. struct super_block *sb;
  154. hfsplus_cat_entry entry;
  155. int entry_size;
  156. int err;
  157. dprint(DBG_CAT_MOD, "create_cat: %s,%u(%d)\n", str->name, cnid, inode->i_nlink);
  158. sb = dir->i_sb;
  159. hfs_find_init(HFSPLUS_SB(sb).cat_tree, &fd);
  160. hfsplus_cat_build_key(sb, fd.search_key, cnid, NULL);
  161. entry_size = hfsplus_fill_cat_thread(sb, &entry, S_ISDIR(inode->i_mode) ?
  162. HFSPLUS_FOLDER_THREAD : HFSPLUS_FILE_THREAD,
  163. dir->i_ino, str);
  164. err = hfs_brec_find(&fd);
  165. if (err != -ENOENT) {
  166. if (!err)
  167. err = -EEXIST;
  168. goto err2;
  169. }
  170. err = hfs_brec_insert(&fd, &entry, entry_size);
  171. if (err)
  172. goto err2;
  173. hfsplus_cat_build_key(sb, fd.search_key, dir->i_ino, str);
  174. entry_size = hfsplus_cat_build_record(&entry, cnid, inode);
  175. err = hfs_brec_find(&fd);
  176. if (err != -ENOENT) {
  177. /* panic? */
  178. if (!err)
  179. err = -EEXIST;
  180. goto err1;
  181. }
  182. err = hfs_brec_insert(&fd, &entry, entry_size);
  183. if (err)
  184. goto err1;
  185. dir->i_size++;
  186. dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
  187. mark_inode_dirty(dir);
  188. hfs_find_exit(&fd);
  189. return 0;
  190. err1:
  191. hfsplus_cat_build_key(sb, fd.search_key, cnid, NULL);
  192. if (!hfs_brec_find(&fd))
  193. hfs_brec_remove(&fd);
  194. err2:
  195. hfs_find_exit(&fd);
  196. return err;
  197. }
  198. int hfsplus_delete_cat(u32 cnid, struct inode *dir, struct qstr *str)
  199. {
  200. struct super_block *sb;
  201. struct hfs_find_data fd;
  202. struct hfsplus_fork_raw fork;
  203. struct list_head *pos;
  204. int err, off;
  205. u16 type;
  206. dprint(DBG_CAT_MOD, "delete_cat: %s,%u\n", str ? str->name : NULL, cnid);
  207. sb = dir->i_sb;
  208. hfs_find_init(HFSPLUS_SB(sb).cat_tree, &fd);
  209. if (!str) {
  210. int len;
  211. hfsplus_cat_build_key(sb, fd.search_key, cnid, NULL);
  212. err = hfs_brec_find(&fd);
  213. if (err)
  214. goto out;
  215. off = fd.entryoffset + offsetof(struct hfsplus_cat_thread, nodeName);
  216. fd.search_key->cat.parent = cpu_to_be32(dir->i_ino);
  217. hfs_bnode_read(fd.bnode, &fd.search_key->cat.name.length, off, 2);
  218. len = be16_to_cpu(fd.search_key->cat.name.length) * 2;
  219. hfs_bnode_read(fd.bnode, &fd.search_key->cat.name.unicode, off + 2, len);
  220. fd.search_key->key_len = cpu_to_be16(6 + len);
  221. } else
  222. hfsplus_cat_build_key(sb, fd.search_key, dir->i_ino, str);
  223. err = hfs_brec_find(&fd);
  224. if (err)
  225. goto out;
  226. type = hfs_bnode_read_u16(fd.bnode, fd.entryoffset);
  227. if (type == HFSPLUS_FILE) {
  228. #if 0
  229. off = fd.entryoffset + offsetof(hfsplus_cat_file, data_fork);
  230. hfs_bnode_read(fd.bnode, &fork, off, sizeof(fork));
  231. hfsplus_free_fork(sb, cnid, &fork, HFSPLUS_TYPE_DATA);
  232. #endif
  233. off = fd.entryoffset + offsetof(struct hfsplus_cat_file, rsrc_fork);
  234. hfs_bnode_read(fd.bnode, &fork, off, sizeof(fork));
  235. hfsplus_free_fork(sb, cnid, &fork, HFSPLUS_TYPE_RSRC);
  236. }
  237. list_for_each(pos, &HFSPLUS_I(dir).open_dir_list) {
  238. struct hfsplus_readdir_data *rd =
  239. list_entry(pos, struct hfsplus_readdir_data, list);
  240. if (fd.tree->keycmp(fd.search_key, (void *)&rd->key) < 0)
  241. rd->file->f_pos--;
  242. }
  243. err = hfs_brec_remove(&fd);
  244. if (err)
  245. goto out;
  246. hfsplus_cat_build_key(sb, fd.search_key, cnid, NULL);
  247. err = hfs_brec_find(&fd);
  248. if (err)
  249. goto out;
  250. err = hfs_brec_remove(&fd);
  251. if (err)
  252. goto out;
  253. dir->i_size--;
  254. dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
  255. mark_inode_dirty(dir);
  256. out:
  257. hfs_find_exit(&fd);
  258. return err;
  259. }
  260. int hfsplus_rename_cat(u32 cnid,
  261. struct inode *src_dir, struct qstr *src_name,
  262. struct inode *dst_dir, struct qstr *dst_name)
  263. {
  264. struct super_block *sb;
  265. struct hfs_find_data src_fd, dst_fd;
  266. hfsplus_cat_entry entry;
  267. int entry_size, type;
  268. int err = 0;
  269. dprint(DBG_CAT_MOD, "rename_cat: %u - %lu,%s - %lu,%s\n", cnid, src_dir->i_ino, src_name->name,
  270. dst_dir->i_ino, dst_name->name);
  271. sb = src_dir->i_sb;
  272. hfs_find_init(HFSPLUS_SB(sb).cat_tree, &src_fd);
  273. dst_fd = src_fd;
  274. /* find the old dir entry and read the data */
  275. hfsplus_cat_build_key(sb, src_fd.search_key, src_dir->i_ino, src_name);
  276. err = hfs_brec_find(&src_fd);
  277. if (err)
  278. goto out;
  279. hfs_bnode_read(src_fd.bnode, &entry, src_fd.entryoffset,
  280. src_fd.entrylength);
  281. /* create new dir entry with the data from the old entry */
  282. hfsplus_cat_build_key(sb, dst_fd.search_key, dst_dir->i_ino, dst_name);
  283. err = hfs_brec_find(&dst_fd);
  284. if (err != -ENOENT) {
  285. if (!err)
  286. err = -EEXIST;
  287. goto out;
  288. }
  289. err = hfs_brec_insert(&dst_fd, &entry, src_fd.entrylength);
  290. if (err)
  291. goto out;
  292. dst_dir->i_size++;
  293. dst_dir->i_mtime = dst_dir->i_ctime = CURRENT_TIME_SEC;
  294. mark_inode_dirty(dst_dir);
  295. /* finally remove the old entry */
  296. hfsplus_cat_build_key(sb, src_fd.search_key, src_dir->i_ino, src_name);
  297. err = hfs_brec_find(&src_fd);
  298. if (err)
  299. goto out;
  300. err = hfs_brec_remove(&src_fd);
  301. if (err)
  302. goto out;
  303. src_dir->i_size--;
  304. src_dir->i_mtime = src_dir->i_ctime = CURRENT_TIME_SEC;
  305. mark_inode_dirty(src_dir);
  306. /* remove old thread entry */
  307. hfsplus_cat_build_key(sb, src_fd.search_key, cnid, NULL);
  308. err = hfs_brec_find(&src_fd);
  309. if (err)
  310. goto out;
  311. type = hfs_bnode_read_u16(src_fd.bnode, src_fd.entryoffset);
  312. err = hfs_brec_remove(&src_fd);
  313. if (err)
  314. goto out;
  315. /* create new thread entry */
  316. hfsplus_cat_build_key(sb, dst_fd.search_key, cnid, NULL);
  317. entry_size = hfsplus_fill_cat_thread(sb, &entry, type, dst_dir->i_ino, dst_name);
  318. err = hfs_brec_find(&dst_fd);
  319. if (err != -ENOENT) {
  320. if (!err)
  321. err = -EEXIST;
  322. goto out;
  323. }
  324. err = hfs_brec_insert(&dst_fd, &entry, entry_size);
  325. out:
  326. hfs_bnode_put(dst_fd.bnode);
  327. hfs_find_exit(&src_fd);
  328. return err;
  329. }