catalog.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. static void hfsplus_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. HFSPLUS_I(inode)->userflags = perms->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. }
  72. static int hfsplus_cat_build_record(hfsplus_cat_entry *entry, u32 cnid, struct inode *inode)
  73. {
  74. struct hfsplus_sb_info *sbi = HFSPLUS_SB(inode->i_sb);
  75. if (S_ISDIR(inode->i_mode)) {
  76. struct hfsplus_cat_folder *folder;
  77. folder = &entry->folder;
  78. memset(folder, 0, sizeof(*folder));
  79. folder->type = cpu_to_be16(HFSPLUS_FOLDER);
  80. folder->id = cpu_to_be32(inode->i_ino);
  81. HFSPLUS_I(inode)->create_date =
  82. folder->create_date =
  83. folder->content_mod_date =
  84. folder->attribute_mod_date =
  85. folder->access_date = hfsp_now2mt();
  86. hfsplus_set_perms(inode, &folder->permissions);
  87. if (inode == sbi->hidden_dir)
  88. /* invisible and namelocked */
  89. folder->user_info.frFlags = cpu_to_be16(0x5000);
  90. return sizeof(*folder);
  91. } else {
  92. struct hfsplus_cat_file *file;
  93. file = &entry->file;
  94. memset(file, 0, sizeof(*file));
  95. file->type = cpu_to_be16(HFSPLUS_FILE);
  96. file->flags = cpu_to_be16(HFSPLUS_FILE_THREAD_EXISTS);
  97. file->id = cpu_to_be32(cnid);
  98. HFSPLUS_I(inode)->create_date =
  99. file->create_date =
  100. file->content_mod_date =
  101. file->attribute_mod_date =
  102. file->access_date = hfsp_now2mt();
  103. if (cnid == inode->i_ino) {
  104. hfsplus_set_perms(inode, &file->permissions);
  105. if (S_ISLNK(inode->i_mode)) {
  106. file->user_info.fdType = cpu_to_be32(HFSP_SYMLINK_TYPE);
  107. file->user_info.fdCreator = cpu_to_be32(HFSP_SYMLINK_CREATOR);
  108. } else {
  109. file->user_info.fdType = cpu_to_be32(sbi->type);
  110. file->user_info.fdCreator = cpu_to_be32(sbi->creator);
  111. }
  112. if ((file->permissions.rootflags | file->permissions.userflags) & HFSPLUS_FLG_IMMUTABLE)
  113. file->flags |= cpu_to_be16(HFSPLUS_FILE_LOCKED);
  114. } else {
  115. file->user_info.fdType = cpu_to_be32(HFSP_HARDLINK_TYPE);
  116. file->user_info.fdCreator = cpu_to_be32(HFSP_HFSPLUS_CREATOR);
  117. file->user_info.fdFlags = cpu_to_be16(0x100);
  118. file->create_date = HFSPLUS_I(sbi->hidden_dir)->create_date;
  119. file->permissions.dev = cpu_to_be32(HFSPLUS_I(inode)->linkid);
  120. }
  121. return sizeof(*file);
  122. }
  123. }
  124. static int hfsplus_fill_cat_thread(struct super_block *sb,
  125. hfsplus_cat_entry *entry, int type,
  126. u32 parentid, struct qstr *str)
  127. {
  128. entry->type = cpu_to_be16(type);
  129. entry->thread.reserved = 0;
  130. entry->thread.parentID = cpu_to_be32(parentid);
  131. hfsplus_asc2uni(sb, &entry->thread.nodeName, str->name, str->len);
  132. return 10 + be16_to_cpu(entry->thread.nodeName.length) * 2;
  133. }
  134. /* Try to get a catalog entry for given catalog id */
  135. int hfsplus_find_cat(struct super_block *sb, u32 cnid,
  136. struct hfs_find_data *fd)
  137. {
  138. hfsplus_cat_entry tmp;
  139. int err;
  140. u16 type;
  141. hfsplus_cat_build_key(sb, fd->search_key, cnid, NULL);
  142. err = hfs_brec_read(fd, &tmp, sizeof(hfsplus_cat_entry));
  143. if (err)
  144. return err;
  145. type = be16_to_cpu(tmp.type);
  146. if (type != HFSPLUS_FOLDER_THREAD && type != HFSPLUS_FILE_THREAD) {
  147. printk(KERN_ERR "hfs: found bad thread record in catalog\n");
  148. return -EIO;
  149. }
  150. if (be16_to_cpu(tmp.thread.nodeName.length) > 255) {
  151. printk(KERN_ERR "hfs: catalog name length corrupted\n");
  152. return -EIO;
  153. }
  154. hfsplus_cat_build_key_uni(fd->search_key, be32_to_cpu(tmp.thread.parentID),
  155. &tmp.thread.nodeName);
  156. return hfs_brec_find(fd);
  157. }
  158. int hfsplus_create_cat(u32 cnid, struct inode *dir, struct qstr *str, struct inode *inode)
  159. {
  160. struct super_block *sb = dir->i_sb;
  161. struct hfs_find_data fd;
  162. hfsplus_cat_entry entry;
  163. int entry_size;
  164. int err;
  165. dprint(DBG_CAT_MOD, "create_cat: %s,%u(%d)\n", str->name, cnid, inode->i_nlink);
  166. hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd);
  167. hfsplus_cat_build_key(sb, fd.search_key, cnid, NULL);
  168. entry_size = hfsplus_fill_cat_thread(sb, &entry, S_ISDIR(inode->i_mode) ?
  169. HFSPLUS_FOLDER_THREAD : HFSPLUS_FILE_THREAD,
  170. dir->i_ino, str);
  171. err = hfs_brec_find(&fd);
  172. if (err != -ENOENT) {
  173. if (!err)
  174. err = -EEXIST;
  175. goto err2;
  176. }
  177. err = hfs_brec_insert(&fd, &entry, entry_size);
  178. if (err)
  179. goto err2;
  180. hfsplus_cat_build_key(sb, fd.search_key, dir->i_ino, str);
  181. entry_size = hfsplus_cat_build_record(&entry, cnid, inode);
  182. err = hfs_brec_find(&fd);
  183. if (err != -ENOENT) {
  184. /* panic? */
  185. if (!err)
  186. err = -EEXIST;
  187. goto err1;
  188. }
  189. err = hfs_brec_insert(&fd, &entry, entry_size);
  190. if (err)
  191. goto err1;
  192. dir->i_size++;
  193. dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
  194. mark_inode_dirty(dir);
  195. hfs_find_exit(&fd);
  196. return 0;
  197. err1:
  198. hfsplus_cat_build_key(sb, fd.search_key, cnid, NULL);
  199. if (!hfs_brec_find(&fd))
  200. hfs_brec_remove(&fd);
  201. err2:
  202. hfs_find_exit(&fd);
  203. return err;
  204. }
  205. int hfsplus_delete_cat(u32 cnid, struct inode *dir, struct qstr *str)
  206. {
  207. struct super_block *sb = dir->i_sb;
  208. struct hfs_find_data fd;
  209. struct hfsplus_fork_raw fork;
  210. struct list_head *pos;
  211. int err, off;
  212. u16 type;
  213. dprint(DBG_CAT_MOD, "delete_cat: %s,%u\n", str ? str->name : NULL, cnid);
  214. hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd);
  215. if (!str) {
  216. int len;
  217. hfsplus_cat_build_key(sb, fd.search_key, cnid, NULL);
  218. err = hfs_brec_find(&fd);
  219. if (err)
  220. goto out;
  221. off = fd.entryoffset + offsetof(struct hfsplus_cat_thread, nodeName);
  222. fd.search_key->cat.parent = cpu_to_be32(dir->i_ino);
  223. hfs_bnode_read(fd.bnode, &fd.search_key->cat.name.length, off, 2);
  224. len = be16_to_cpu(fd.search_key->cat.name.length) * 2;
  225. hfs_bnode_read(fd.bnode, &fd.search_key->cat.name.unicode, off + 2, len);
  226. fd.search_key->key_len = cpu_to_be16(6 + len);
  227. } else
  228. hfsplus_cat_build_key(sb, fd.search_key, dir->i_ino, str);
  229. err = hfs_brec_find(&fd);
  230. if (err)
  231. goto out;
  232. type = hfs_bnode_read_u16(fd.bnode, fd.entryoffset);
  233. if (type == HFSPLUS_FILE) {
  234. #if 0
  235. off = fd.entryoffset + offsetof(hfsplus_cat_file, data_fork);
  236. hfs_bnode_read(fd.bnode, &fork, off, sizeof(fork));
  237. hfsplus_free_fork(sb, cnid, &fork, HFSPLUS_TYPE_DATA);
  238. #endif
  239. off = fd.entryoffset + offsetof(struct hfsplus_cat_file, rsrc_fork);
  240. hfs_bnode_read(fd.bnode, &fork, off, sizeof(fork));
  241. hfsplus_free_fork(sb, cnid, &fork, HFSPLUS_TYPE_RSRC);
  242. }
  243. list_for_each(pos, &HFSPLUS_I(dir)->open_dir_list) {
  244. struct hfsplus_readdir_data *rd =
  245. list_entry(pos, struct hfsplus_readdir_data, list);
  246. if (fd.tree->keycmp(fd.search_key, (void *)&rd->key) < 0)
  247. rd->file->f_pos--;
  248. }
  249. err = hfs_brec_remove(&fd);
  250. if (err)
  251. goto out;
  252. hfsplus_cat_build_key(sb, fd.search_key, cnid, NULL);
  253. err = hfs_brec_find(&fd);
  254. if (err)
  255. goto out;
  256. err = hfs_brec_remove(&fd);
  257. if (err)
  258. goto out;
  259. dir->i_size--;
  260. dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
  261. mark_inode_dirty(dir);
  262. out:
  263. hfs_find_exit(&fd);
  264. return err;
  265. }
  266. int hfsplus_rename_cat(u32 cnid,
  267. struct inode *src_dir, struct qstr *src_name,
  268. struct inode *dst_dir, struct qstr *dst_name)
  269. {
  270. struct super_block *sb = src_dir->i_sb;
  271. struct hfs_find_data src_fd, dst_fd;
  272. hfsplus_cat_entry entry;
  273. int entry_size, type;
  274. int err = 0;
  275. dprint(DBG_CAT_MOD, "rename_cat: %u - %lu,%s - %lu,%s\n", cnid, src_dir->i_ino, src_name->name,
  276. dst_dir->i_ino, dst_name->name);
  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. }