catalog.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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).rootflags = perms->rootflags;
  68. HFSPLUS_I(inode).userflags = perms->userflags;
  69. perms->mode = cpu_to_be16(inode->i_mode);
  70. perms->owner = cpu_to_be32(inode->i_uid);
  71. perms->group = cpu_to_be32(inode->i_gid);
  72. }
  73. static int hfsplus_cat_build_record(hfsplus_cat_entry *entry, u32 cnid, struct inode *inode)
  74. {
  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 == HFSPLUS_SB(inode->i_sb).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(HFSPLUS_SB(inode->i_sb).type);
  110. file->user_info.fdCreator = cpu_to_be32(HFSPLUS_SB(inode->i_sb).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(HFSPLUS_SB(inode->i_sb).hidden_dir).create_date;
  119. file->permissions.dev = cpu_to_be32(HFSPLUS_I(inode).dev);
  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. hfsplus_cat_build_key_uni(fd->search_key, be32_to_cpu(tmp.thread.parentID),
  151. &tmp.thread.nodeName);
  152. return hfs_brec_find(fd);
  153. }
  154. int hfsplus_create_cat(u32 cnid, struct inode *dir, struct qstr *str, struct inode *inode)
  155. {
  156. struct hfs_find_data fd;
  157. struct super_block *sb;
  158. hfsplus_cat_entry entry;
  159. int entry_size;
  160. int err;
  161. dprint(DBG_CAT_MOD, "create_cat: %s,%u(%d)\n", str->name, cnid, inode->i_nlink);
  162. sb = dir->i_sb;
  163. hfs_find_init(HFSPLUS_SB(sb).cat_tree, &fd);
  164. hfsplus_cat_build_key(sb, fd.search_key, cnid, NULL);
  165. entry_size = hfsplus_fill_cat_thread(sb, &entry, S_ISDIR(inode->i_mode) ?
  166. HFSPLUS_FOLDER_THREAD : HFSPLUS_FILE_THREAD,
  167. dir->i_ino, str);
  168. err = hfs_brec_find(&fd);
  169. if (err != -ENOENT) {
  170. if (!err)
  171. err = -EEXIST;
  172. goto err2;
  173. }
  174. err = hfs_brec_insert(&fd, &entry, entry_size);
  175. if (err)
  176. goto err2;
  177. hfsplus_cat_build_key(sb, fd.search_key, dir->i_ino, str);
  178. entry_size = hfsplus_cat_build_record(&entry, cnid, inode);
  179. err = hfs_brec_find(&fd);
  180. if (err != -ENOENT) {
  181. /* panic? */
  182. if (!err)
  183. err = -EEXIST;
  184. goto err1;
  185. }
  186. err = hfs_brec_insert(&fd, &entry, entry_size);
  187. if (err)
  188. goto err1;
  189. dir->i_size++;
  190. dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
  191. mark_inode_dirty(dir);
  192. hfs_find_exit(&fd);
  193. return 0;
  194. err1:
  195. hfsplus_cat_build_key(sb, fd.search_key, cnid, NULL);
  196. if (!hfs_brec_find(&fd))
  197. hfs_brec_remove(&fd);
  198. err2:
  199. hfs_find_exit(&fd);
  200. return err;
  201. }
  202. int hfsplus_delete_cat(u32 cnid, struct inode *dir, struct qstr *str)
  203. {
  204. struct super_block *sb;
  205. struct hfs_find_data fd;
  206. struct hfsplus_fork_raw fork;
  207. struct list_head *pos;
  208. int err, off;
  209. u16 type;
  210. dprint(DBG_CAT_MOD, "delete_cat: %s,%u\n", str ? str->name : NULL, cnid);
  211. sb = dir->i_sb;
  212. hfs_find_init(HFSPLUS_SB(sb).cat_tree, &fd);
  213. if (!str) {
  214. int len;
  215. hfsplus_cat_build_key(sb, fd.search_key, cnid, NULL);
  216. err = hfs_brec_find(&fd);
  217. if (err)
  218. goto out;
  219. off = fd.entryoffset + offsetof(struct hfsplus_cat_thread, nodeName);
  220. fd.search_key->cat.parent = cpu_to_be32(dir->i_ino);
  221. hfs_bnode_read(fd.bnode, &fd.search_key->cat.name.length, off, 2);
  222. len = be16_to_cpu(fd.search_key->cat.name.length) * 2;
  223. hfs_bnode_read(fd.bnode, &fd.search_key->cat.name.unicode, off + 2, len);
  224. fd.search_key->key_len = cpu_to_be16(6 + len);
  225. } else
  226. hfsplus_cat_build_key(sb, fd.search_key, dir->i_ino, str);
  227. err = hfs_brec_find(&fd);
  228. if (err)
  229. goto out;
  230. type = hfs_bnode_read_u16(fd.bnode, fd.entryoffset);
  231. if (type == HFSPLUS_FILE) {
  232. #if 0
  233. off = fd.entryoffset + offsetof(hfsplus_cat_file, data_fork);
  234. hfs_bnode_read(fd.bnode, &fork, off, sizeof(fork));
  235. hfsplus_free_fork(sb, cnid, &fork, HFSPLUS_TYPE_DATA);
  236. #endif
  237. off = fd.entryoffset + offsetof(struct hfsplus_cat_file, rsrc_fork);
  238. hfs_bnode_read(fd.bnode, &fork, off, sizeof(fork));
  239. hfsplus_free_fork(sb, cnid, &fork, HFSPLUS_TYPE_RSRC);
  240. }
  241. list_for_each(pos, &HFSPLUS_I(dir).open_dir_list) {
  242. struct hfsplus_readdir_data *rd =
  243. list_entry(pos, struct hfsplus_readdir_data, list);
  244. if (fd.tree->keycmp(fd.search_key, (void *)&rd->key) < 0)
  245. rd->file->f_pos--;
  246. }
  247. err = hfs_brec_remove(&fd);
  248. if (err)
  249. goto out;
  250. hfsplus_cat_build_key(sb, fd.search_key, cnid, NULL);
  251. err = hfs_brec_find(&fd);
  252. if (err)
  253. goto out;
  254. err = hfs_brec_remove(&fd);
  255. if (err)
  256. goto out;
  257. dir->i_size--;
  258. dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC;
  259. mark_inode_dirty(dir);
  260. out:
  261. hfs_find_exit(&fd);
  262. return err;
  263. }
  264. int hfsplus_rename_cat(u32 cnid,
  265. struct inode *src_dir, struct qstr *src_name,
  266. struct inode *dst_dir, struct qstr *dst_name)
  267. {
  268. struct super_block *sb;
  269. struct hfs_find_data src_fd, dst_fd;
  270. hfsplus_cat_entry entry;
  271. int entry_size, type;
  272. int err = 0;
  273. dprint(DBG_CAT_MOD, "rename_cat: %u - %lu,%s - %lu,%s\n", cnid, src_dir->i_ino, src_name->name,
  274. dst_dir->i_ino, dst_name->name);
  275. sb = src_dir->i_sb;
  276. hfs_find_init(HFSPLUS_SB(sb).cat_tree, &src_fd);
  277. dst_fd = src_fd;
  278. /* find the old dir entry and read the data */
  279. hfsplus_cat_build_key(sb, src_fd.search_key, src_dir->i_ino, src_name);
  280. err = hfs_brec_find(&src_fd);
  281. if (err)
  282. goto out;
  283. hfs_bnode_read(src_fd.bnode, &entry, src_fd.entryoffset,
  284. src_fd.entrylength);
  285. /* create new dir entry with the data from the old entry */
  286. hfsplus_cat_build_key(sb, dst_fd.search_key, dst_dir->i_ino, dst_name);
  287. err = hfs_brec_find(&dst_fd);
  288. if (err != -ENOENT) {
  289. if (!err)
  290. err = -EEXIST;
  291. goto out;
  292. }
  293. err = hfs_brec_insert(&dst_fd, &entry, src_fd.entrylength);
  294. if (err)
  295. goto out;
  296. dst_dir->i_size++;
  297. dst_dir->i_mtime = dst_dir->i_ctime = CURRENT_TIME_SEC;
  298. mark_inode_dirty(dst_dir);
  299. /* finally remove the old entry */
  300. hfsplus_cat_build_key(sb, src_fd.search_key, src_dir->i_ino, src_name);
  301. err = hfs_brec_find(&src_fd);
  302. if (err)
  303. goto out;
  304. err = hfs_brec_remove(&src_fd);
  305. if (err)
  306. goto out;
  307. src_dir->i_size--;
  308. src_dir->i_mtime = src_dir->i_ctime = CURRENT_TIME_SEC;
  309. mark_inode_dirty(src_dir);
  310. /* remove old thread entry */
  311. hfsplus_cat_build_key(sb, src_fd.search_key, cnid, NULL);
  312. err = hfs_brec_find(&src_fd);
  313. if (err)
  314. goto out;
  315. type = hfs_bnode_read_u16(src_fd.bnode, src_fd.entryoffset);
  316. err = hfs_brec_remove(&src_fd);
  317. if (err)
  318. goto out;
  319. /* create new thread entry */
  320. hfsplus_cat_build_key(sb, dst_fd.search_key, cnid, NULL);
  321. entry_size = hfsplus_fill_cat_thread(sb, &entry, type, 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, entry_size);
  329. out:
  330. hfs_bnode_put(dst_fd.bnode);
  331. hfs_find_exit(&src_fd);
  332. return err;
  333. }