catalog.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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. /* Generates key for catalog file/folders record. */
  33. int hfsplus_cat_build_key(struct super_block *sb,
  34. hfsplus_btree_key *key, u32 parent, const struct qstr *str)
  35. {
  36. int len, err;
  37. key->cat.parent = cpu_to_be32(parent);
  38. err = hfsplus_asc2uni(sb, &key->cat.name, HFSPLUS_MAX_STRLEN,
  39. str->name, str->len);
  40. if (unlikely(err < 0))
  41. return err;
  42. len = be16_to_cpu(key->cat.name.length);
  43. key->key_len = cpu_to_be16(6 + 2 * len);
  44. return 0;
  45. }
  46. /* Generates key for catalog thread record. */
  47. void hfsplus_cat_build_key_with_cnid(struct super_block *sb,
  48. hfsplus_btree_key *key, u32 parent)
  49. {
  50. key->cat.parent = cpu_to_be32(parent);
  51. key->cat.name.length = 0;
  52. key->key_len = cpu_to_be16(6);
  53. }
  54. static void hfsplus_cat_build_key_uni(hfsplus_btree_key *key, u32 parent,
  55. struct hfsplus_unistr *name)
  56. {
  57. int ustrlen;
  58. ustrlen = be16_to_cpu(name->length);
  59. key->cat.parent = cpu_to_be32(parent);
  60. key->cat.name.length = cpu_to_be16(ustrlen);
  61. ustrlen *= 2;
  62. memcpy(key->cat.name.unicode, name->unicode, ustrlen);
  63. key->key_len = cpu_to_be16(6 + ustrlen);
  64. }
  65. void hfsplus_cat_set_perms(struct inode *inode, struct hfsplus_perm *perms)
  66. {
  67. if (inode->i_flags & S_IMMUTABLE)
  68. perms->rootflags |= HFSPLUS_FLG_IMMUTABLE;
  69. else
  70. perms->rootflags &= ~HFSPLUS_FLG_IMMUTABLE;
  71. if (inode->i_flags & S_APPEND)
  72. perms->rootflags |= HFSPLUS_FLG_APPEND;
  73. else
  74. perms->rootflags &= ~HFSPLUS_FLG_APPEND;
  75. perms->userflags = HFSPLUS_I(inode)->userflags;
  76. perms->mode = cpu_to_be16(inode->i_mode);
  77. perms->owner = cpu_to_be32(i_uid_read(inode));
  78. perms->group = cpu_to_be32(i_gid_read(inode));
  79. if (S_ISREG(inode->i_mode))
  80. perms->dev = cpu_to_be32(inode->i_nlink);
  81. else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode))
  82. perms->dev = cpu_to_be32(inode->i_rdev);
  83. else
  84. perms->dev = 0;
  85. }
  86. static int hfsplus_cat_build_record(hfsplus_cat_entry *entry,
  87. u32 cnid, struct inode *inode)
  88. {
  89. struct hfsplus_sb_info *sbi = HFSPLUS_SB(inode->i_sb);
  90. if (S_ISDIR(inode->i_mode)) {
  91. struct hfsplus_cat_folder *folder;
  92. folder = &entry->folder;
  93. memset(folder, 0, sizeof(*folder));
  94. folder->type = cpu_to_be16(HFSPLUS_FOLDER);
  95. if (test_bit(HFSPLUS_SB_HFSX, &sbi->flags))
  96. folder->flags |= cpu_to_be16(HFSPLUS_HAS_FOLDER_COUNT);
  97. folder->id = cpu_to_be32(inode->i_ino);
  98. HFSPLUS_I(inode)->create_date =
  99. folder->create_date =
  100. folder->content_mod_date =
  101. folder->attribute_mod_date =
  102. folder->access_date = hfsp_now2mt();
  103. hfsplus_cat_set_perms(inode, &folder->permissions);
  104. if (inode == sbi->hidden_dir)
  105. /* invisible and namelocked */
  106. folder->user_info.frFlags = cpu_to_be16(0x5000);
  107. return sizeof(*folder);
  108. } else {
  109. struct hfsplus_cat_file *file;
  110. file = &entry->file;
  111. memset(file, 0, sizeof(*file));
  112. file->type = cpu_to_be16(HFSPLUS_FILE);
  113. file->flags = cpu_to_be16(HFSPLUS_FILE_THREAD_EXISTS);
  114. file->id = cpu_to_be32(cnid);
  115. HFSPLUS_I(inode)->create_date =
  116. file->create_date =
  117. file->content_mod_date =
  118. file->attribute_mod_date =
  119. file->access_date = hfsp_now2mt();
  120. if (cnid == inode->i_ino) {
  121. hfsplus_cat_set_perms(inode, &file->permissions);
  122. if (S_ISLNK(inode->i_mode)) {
  123. file->user_info.fdType =
  124. cpu_to_be32(HFSP_SYMLINK_TYPE);
  125. file->user_info.fdCreator =
  126. cpu_to_be32(HFSP_SYMLINK_CREATOR);
  127. } else {
  128. file->user_info.fdType =
  129. cpu_to_be32(sbi->type);
  130. file->user_info.fdCreator =
  131. cpu_to_be32(sbi->creator);
  132. }
  133. if (HFSPLUS_FLG_IMMUTABLE &
  134. (file->permissions.rootflags |
  135. file->permissions.userflags))
  136. file->flags |=
  137. cpu_to_be16(HFSPLUS_FILE_LOCKED);
  138. } else {
  139. file->user_info.fdType =
  140. cpu_to_be32(HFSP_HARDLINK_TYPE);
  141. file->user_info.fdCreator =
  142. cpu_to_be32(HFSP_HFSPLUS_CREATOR);
  143. file->user_info.fdFlags =
  144. cpu_to_be16(0x100);
  145. file->create_date =
  146. HFSPLUS_I(sbi->hidden_dir)->create_date;
  147. file->permissions.dev =
  148. cpu_to_be32(HFSPLUS_I(inode)->linkid);
  149. }
  150. return sizeof(*file);
  151. }
  152. }
  153. static int hfsplus_fill_cat_thread(struct super_block *sb,
  154. hfsplus_cat_entry *entry, int type,
  155. u32 parentid, const struct qstr *str)
  156. {
  157. int err;
  158. entry->type = cpu_to_be16(type);
  159. entry->thread.reserved = 0;
  160. entry->thread.parentID = cpu_to_be32(parentid);
  161. err = hfsplus_asc2uni(sb, &entry->thread.nodeName, HFSPLUS_MAX_STRLEN,
  162. str->name, str->len);
  163. if (unlikely(err < 0))
  164. return err;
  165. return 10 + be16_to_cpu(entry->thread.nodeName.length) * 2;
  166. }
  167. /* Try to get a catalog entry for given catalog id */
  168. int hfsplus_find_cat(struct super_block *sb, u32 cnid,
  169. struct hfs_find_data *fd)
  170. {
  171. hfsplus_cat_entry tmp;
  172. int err;
  173. u16 type;
  174. hfsplus_cat_build_key_with_cnid(sb, fd->search_key, cnid);
  175. err = hfs_brec_read(fd, &tmp, sizeof(hfsplus_cat_entry));
  176. if (err)
  177. return err;
  178. type = be16_to_cpu(tmp.type);
  179. if (type != HFSPLUS_FOLDER_THREAD && type != HFSPLUS_FILE_THREAD) {
  180. pr_err("found bad thread record in catalog\n");
  181. return -EIO;
  182. }
  183. if (be16_to_cpu(tmp.thread.nodeName.length) > 255) {
  184. pr_err("catalog name length corrupted\n");
  185. return -EIO;
  186. }
  187. hfsplus_cat_build_key_uni(fd->search_key,
  188. be32_to_cpu(tmp.thread.parentID),
  189. &tmp.thread.nodeName);
  190. return hfs_brec_find(fd, hfs_find_rec_by_key);
  191. }
  192. static void hfsplus_subfolders_inc(struct inode *dir)
  193. {
  194. struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
  195. if (test_bit(HFSPLUS_SB_HFSX, &sbi->flags)) {
  196. /*
  197. * Increment subfolder count. Note, the value is only meaningful
  198. * for folders with HFSPLUS_HAS_FOLDER_COUNT flag set.
  199. */
  200. HFSPLUS_I(dir)->subfolders++;
  201. }
  202. }
  203. static void hfsplus_subfolders_dec(struct inode *dir)
  204. {
  205. struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
  206. if (test_bit(HFSPLUS_SB_HFSX, &sbi->flags)) {
  207. /*
  208. * Decrement subfolder count. Note, the value is only meaningful
  209. * for folders with HFSPLUS_HAS_FOLDER_COUNT flag set.
  210. *
  211. * Check for zero. Some subfolders may have been created
  212. * by an implementation ignorant of this counter.
  213. */
  214. if (HFSPLUS_I(dir)->subfolders)
  215. HFSPLUS_I(dir)->subfolders--;
  216. }
  217. }
  218. int hfsplus_create_cat(u32 cnid, struct inode *dir,
  219. const struct qstr *str, struct inode *inode)
  220. {
  221. struct super_block *sb = dir->i_sb;
  222. struct hfs_find_data fd;
  223. hfsplus_cat_entry entry;
  224. int entry_size;
  225. int err;
  226. hfs_dbg(CAT_MOD, "create_cat: %s,%u(%d)\n",
  227. str->name, cnid, inode->i_nlink);
  228. err = hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd);
  229. if (err)
  230. return err;
  231. hfsplus_cat_build_key_with_cnid(sb, fd.search_key, cnid);
  232. entry_size = hfsplus_fill_cat_thread(sb, &entry,
  233. S_ISDIR(inode->i_mode) ?
  234. HFSPLUS_FOLDER_THREAD : HFSPLUS_FILE_THREAD,
  235. dir->i_ino, str);
  236. if (unlikely(entry_size < 0)) {
  237. err = entry_size;
  238. goto err2;
  239. }
  240. err = hfs_brec_find(&fd, hfs_find_rec_by_key);
  241. if (err != -ENOENT) {
  242. if (!err)
  243. err = -EEXIST;
  244. goto err2;
  245. }
  246. err = hfs_brec_insert(&fd, &entry, entry_size);
  247. if (err)
  248. goto err2;
  249. err = hfsplus_cat_build_key(sb, fd.search_key, dir->i_ino, str);
  250. if (unlikely(err))
  251. goto err1;
  252. entry_size = hfsplus_cat_build_record(&entry, cnid, inode);
  253. err = hfs_brec_find(&fd, hfs_find_rec_by_key);
  254. if (err != -ENOENT) {
  255. /* panic? */
  256. if (!err)
  257. err = -EEXIST;
  258. goto err1;
  259. }
  260. err = hfs_brec_insert(&fd, &entry, entry_size);
  261. if (err)
  262. goto err1;
  263. dir->i_size++;
  264. if (S_ISDIR(inode->i_mode))
  265. hfsplus_subfolders_inc(dir);
  266. dir->i_mtime = dir->i_ctime = current_time(dir);
  267. hfsplus_mark_inode_dirty(dir, HFSPLUS_I_CAT_DIRTY);
  268. hfs_find_exit(&fd);
  269. return 0;
  270. err1:
  271. hfsplus_cat_build_key_with_cnid(sb, fd.search_key, cnid);
  272. if (!hfs_brec_find(&fd, hfs_find_rec_by_key))
  273. hfs_brec_remove(&fd);
  274. err2:
  275. hfs_find_exit(&fd);
  276. return err;
  277. }
  278. int hfsplus_delete_cat(u32 cnid, struct inode *dir, const struct qstr *str)
  279. {
  280. struct super_block *sb = dir->i_sb;
  281. struct hfs_find_data fd;
  282. struct hfsplus_fork_raw fork;
  283. struct list_head *pos;
  284. int err, off;
  285. u16 type;
  286. hfs_dbg(CAT_MOD, "delete_cat: %s,%u\n", str ? str->name : NULL, cnid);
  287. err = hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd);
  288. if (err)
  289. return err;
  290. if (!str) {
  291. int len;
  292. hfsplus_cat_build_key_with_cnid(sb, fd.search_key, cnid);
  293. err = hfs_brec_find(&fd, hfs_find_rec_by_key);
  294. if (err)
  295. goto out;
  296. off = fd.entryoffset +
  297. offsetof(struct hfsplus_cat_thread, nodeName);
  298. fd.search_key->cat.parent = cpu_to_be32(dir->i_ino);
  299. hfs_bnode_read(fd.bnode,
  300. &fd.search_key->cat.name.length, off, 2);
  301. len = be16_to_cpu(fd.search_key->cat.name.length) * 2;
  302. hfs_bnode_read(fd.bnode,
  303. &fd.search_key->cat.name.unicode,
  304. off + 2, len);
  305. fd.search_key->key_len = cpu_to_be16(6 + len);
  306. } else {
  307. err = hfsplus_cat_build_key(sb, fd.search_key, dir->i_ino, str);
  308. if (unlikely(err))
  309. goto out;
  310. }
  311. err = hfs_brec_find(&fd, hfs_find_rec_by_key);
  312. if (err)
  313. goto out;
  314. type = hfs_bnode_read_u16(fd.bnode, fd.entryoffset);
  315. if (type == HFSPLUS_FILE) {
  316. #if 0
  317. off = fd.entryoffset + offsetof(hfsplus_cat_file, data_fork);
  318. hfs_bnode_read(fd.bnode, &fork, off, sizeof(fork));
  319. hfsplus_free_fork(sb, cnid, &fork, HFSPLUS_TYPE_DATA);
  320. #endif
  321. off = fd.entryoffset +
  322. offsetof(struct hfsplus_cat_file, rsrc_fork);
  323. hfs_bnode_read(fd.bnode, &fork, off, sizeof(fork));
  324. hfsplus_free_fork(sb, cnid, &fork, HFSPLUS_TYPE_RSRC);
  325. }
  326. /* we only need to take spinlock for exclusion with ->release() */
  327. spin_lock(&HFSPLUS_I(dir)->open_dir_lock);
  328. list_for_each(pos, &HFSPLUS_I(dir)->open_dir_list) {
  329. struct hfsplus_readdir_data *rd =
  330. list_entry(pos, struct hfsplus_readdir_data, list);
  331. if (fd.tree->keycmp(fd.search_key, (void *)&rd->key) < 0)
  332. rd->file->f_pos--;
  333. }
  334. spin_unlock(&HFSPLUS_I(dir)->open_dir_lock);
  335. err = hfs_brec_remove(&fd);
  336. if (err)
  337. goto out;
  338. hfsplus_cat_build_key_with_cnid(sb, fd.search_key, cnid);
  339. err = hfs_brec_find(&fd, hfs_find_rec_by_key);
  340. if (err)
  341. goto out;
  342. err = hfs_brec_remove(&fd);
  343. if (err)
  344. goto out;
  345. dir->i_size--;
  346. if (type == HFSPLUS_FOLDER)
  347. hfsplus_subfolders_dec(dir);
  348. dir->i_mtime = dir->i_ctime = current_time(dir);
  349. hfsplus_mark_inode_dirty(dir, HFSPLUS_I_CAT_DIRTY);
  350. if (type == HFSPLUS_FILE || type == HFSPLUS_FOLDER) {
  351. if (HFSPLUS_SB(sb)->attr_tree)
  352. hfsplus_delete_all_attrs(dir, cnid);
  353. }
  354. out:
  355. hfs_find_exit(&fd);
  356. return err;
  357. }
  358. int hfsplus_rename_cat(u32 cnid,
  359. struct inode *src_dir, const struct qstr *src_name,
  360. struct inode *dst_dir, const struct qstr *dst_name)
  361. {
  362. struct super_block *sb = src_dir->i_sb;
  363. struct hfs_find_data src_fd, dst_fd;
  364. hfsplus_cat_entry entry;
  365. int entry_size, type;
  366. int err;
  367. hfs_dbg(CAT_MOD, "rename_cat: %u - %lu,%s - %lu,%s\n",
  368. cnid, src_dir->i_ino, src_name->name,
  369. dst_dir->i_ino, dst_name->name);
  370. err = hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &src_fd);
  371. if (err)
  372. return err;
  373. dst_fd = src_fd;
  374. /* find the old dir entry and read the data */
  375. err = hfsplus_cat_build_key(sb, src_fd.search_key,
  376. src_dir->i_ino, src_name);
  377. if (unlikely(err))
  378. goto out;
  379. err = hfs_brec_find(&src_fd, hfs_find_rec_by_key);
  380. if (err)
  381. goto out;
  382. if (src_fd.entrylength > sizeof(entry) || src_fd.entrylength < 0) {
  383. err = -EIO;
  384. goto out;
  385. }
  386. hfs_bnode_read(src_fd.bnode, &entry, src_fd.entryoffset,
  387. src_fd.entrylength);
  388. type = be16_to_cpu(entry.type);
  389. /* create new dir entry with the data from the old entry */
  390. err = hfsplus_cat_build_key(sb, dst_fd.search_key,
  391. dst_dir->i_ino, dst_name);
  392. if (unlikely(err))
  393. goto out;
  394. err = hfs_brec_find(&dst_fd, hfs_find_rec_by_key);
  395. if (err != -ENOENT) {
  396. if (!err)
  397. err = -EEXIST;
  398. goto out;
  399. }
  400. err = hfs_brec_insert(&dst_fd, &entry, src_fd.entrylength);
  401. if (err)
  402. goto out;
  403. dst_dir->i_size++;
  404. if (type == HFSPLUS_FOLDER)
  405. hfsplus_subfolders_inc(dst_dir);
  406. dst_dir->i_mtime = dst_dir->i_ctime = current_time(dst_dir);
  407. /* finally remove the old entry */
  408. err = hfsplus_cat_build_key(sb, src_fd.search_key,
  409. src_dir->i_ino, src_name);
  410. if (unlikely(err))
  411. goto out;
  412. err = hfs_brec_find(&src_fd, hfs_find_rec_by_key);
  413. if (err)
  414. goto out;
  415. err = hfs_brec_remove(&src_fd);
  416. if (err)
  417. goto out;
  418. src_dir->i_size--;
  419. if (type == HFSPLUS_FOLDER)
  420. hfsplus_subfolders_dec(src_dir);
  421. src_dir->i_mtime = src_dir->i_ctime = current_time(src_dir);
  422. /* remove old thread entry */
  423. hfsplus_cat_build_key_with_cnid(sb, src_fd.search_key, cnid);
  424. err = hfs_brec_find(&src_fd, hfs_find_rec_by_key);
  425. if (err)
  426. goto out;
  427. type = hfs_bnode_read_u16(src_fd.bnode, src_fd.entryoffset);
  428. err = hfs_brec_remove(&src_fd);
  429. if (err)
  430. goto out;
  431. /* create new thread entry */
  432. hfsplus_cat_build_key_with_cnid(sb, dst_fd.search_key, cnid);
  433. entry_size = hfsplus_fill_cat_thread(sb, &entry, type,
  434. dst_dir->i_ino, dst_name);
  435. if (unlikely(entry_size < 0)) {
  436. err = entry_size;
  437. goto out;
  438. }
  439. err = hfs_brec_find(&dst_fd, hfs_find_rec_by_key);
  440. if (err != -ENOENT) {
  441. if (!err)
  442. err = -EEXIST;
  443. goto out;
  444. }
  445. err = hfs_brec_insert(&dst_fd, &entry, entry_size);
  446. hfsplus_mark_inode_dirty(dst_dir, HFSPLUS_I_CAT_DIRTY);
  447. hfsplus_mark_inode_dirty(src_dir, HFSPLUS_I_CAT_DIRTY);
  448. out:
  449. hfs_bnode_put(dst_fd.bnode);
  450. hfs_find_exit(&src_fd);
  451. return err;
  452. }