dir.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. /*
  2. * linux/fs/hfsplus/dir.c
  3. *
  4. * Copyright (C) 2001
  5. * Brad Boyer (flar@allandria.com)
  6. * (C) 2003 Ardis Technologies <roman@ardistech.com>
  7. *
  8. * Handling of directories
  9. */
  10. #include <linux/errno.h>
  11. #include <linux/fs.h>
  12. #include <linux/slab.h>
  13. #include <linux/random.h>
  14. #include "hfsplus_fs.h"
  15. #include "hfsplus_raw.h"
  16. static inline void hfsplus_instantiate(struct dentry *dentry,
  17. struct inode *inode, u32 cnid)
  18. {
  19. dentry->d_fsdata = (void *)(unsigned long)cnid;
  20. d_instantiate(dentry, inode);
  21. }
  22. /* Find the entry inside dir named dentry->d_name */
  23. static struct dentry *hfsplus_lookup(struct inode *dir, struct dentry *dentry,
  24. struct nameidata *nd)
  25. {
  26. struct inode *inode = NULL;
  27. struct hfs_find_data fd;
  28. struct super_block *sb;
  29. hfsplus_cat_entry entry;
  30. int err;
  31. u32 cnid, linkid = 0;
  32. u16 type;
  33. sb = dir->i_sb;
  34. dentry->d_fsdata = NULL;
  35. hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd);
  36. hfsplus_cat_build_key(sb, fd.search_key, dir->i_ino, &dentry->d_name);
  37. again:
  38. err = hfs_brec_read(&fd, &entry, sizeof(entry));
  39. if (err) {
  40. if (err == -ENOENT) {
  41. hfs_find_exit(&fd);
  42. /* No such entry */
  43. inode = NULL;
  44. goto out;
  45. }
  46. goto fail;
  47. }
  48. type = be16_to_cpu(entry.type);
  49. if (type == HFSPLUS_FOLDER) {
  50. if (fd.entrylength < sizeof(struct hfsplus_cat_folder)) {
  51. err = -EIO;
  52. goto fail;
  53. }
  54. cnid = be32_to_cpu(entry.folder.id);
  55. dentry->d_fsdata = (void *)(unsigned long)cnid;
  56. } else if (type == HFSPLUS_FILE) {
  57. if (fd.entrylength < sizeof(struct hfsplus_cat_file)) {
  58. err = -EIO;
  59. goto fail;
  60. }
  61. cnid = be32_to_cpu(entry.file.id);
  62. if (entry.file.user_info.fdType ==
  63. cpu_to_be32(HFSP_HARDLINK_TYPE) &&
  64. entry.file.user_info.fdCreator ==
  65. cpu_to_be32(HFSP_HFSPLUS_CREATOR) &&
  66. (entry.file.create_date ==
  67. HFSPLUS_I(HFSPLUS_SB(sb)->hidden_dir)->
  68. create_date ||
  69. entry.file.create_date ==
  70. HFSPLUS_I(sb->s_root->d_inode)->
  71. create_date) &&
  72. HFSPLUS_SB(sb)->hidden_dir) {
  73. struct qstr str;
  74. char name[32];
  75. if (dentry->d_fsdata) {
  76. /*
  77. * We found a link pointing to another link,
  78. * so ignore it and treat it as regular file.
  79. */
  80. cnid = (unsigned long)dentry->d_fsdata;
  81. linkid = 0;
  82. } else {
  83. dentry->d_fsdata = (void *)(unsigned long)cnid;
  84. linkid =
  85. be32_to_cpu(entry.file.permissions.dev);
  86. str.len = sprintf(name, "iNode%d", linkid);
  87. str.name = name;
  88. hfsplus_cat_build_key(sb, fd.search_key,
  89. HFSPLUS_SB(sb)->hidden_dir->i_ino,
  90. &str);
  91. goto again;
  92. }
  93. } else if (!dentry->d_fsdata)
  94. dentry->d_fsdata = (void *)(unsigned long)cnid;
  95. } else {
  96. printk(KERN_ERR "hfs: invalid catalog entry type in lookup\n");
  97. err = -EIO;
  98. goto fail;
  99. }
  100. hfs_find_exit(&fd);
  101. inode = hfsplus_iget(dir->i_sb, cnid);
  102. if (IS_ERR(inode))
  103. return ERR_CAST(inode);
  104. if (S_ISREG(inode->i_mode))
  105. HFSPLUS_I(inode)->linkid = linkid;
  106. out:
  107. d_add(dentry, inode);
  108. return NULL;
  109. fail:
  110. hfs_find_exit(&fd);
  111. return ERR_PTR(err);
  112. }
  113. static int hfsplus_readdir(struct file *filp, void *dirent, filldir_t filldir)
  114. {
  115. struct inode *inode = filp->f_path.dentry->d_inode;
  116. struct super_block *sb = inode->i_sb;
  117. int len, err;
  118. char strbuf[HFSPLUS_MAX_STRLEN + 1];
  119. hfsplus_cat_entry entry;
  120. struct hfs_find_data fd;
  121. struct hfsplus_readdir_data *rd;
  122. u16 type;
  123. if (filp->f_pos >= inode->i_size)
  124. return 0;
  125. hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd);
  126. hfsplus_cat_build_key(sb, fd.search_key, inode->i_ino, NULL);
  127. err = hfs_brec_find(&fd);
  128. if (err)
  129. goto out;
  130. switch ((u32)filp->f_pos) {
  131. case 0:
  132. /* This is completely artificial... */
  133. if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR))
  134. goto out;
  135. filp->f_pos++;
  136. /* fall through */
  137. case 1:
  138. if (fd.entrylength > sizeof(entry) || fd.entrylength < 0) {
  139. err = -EIO;
  140. goto out;
  141. }
  142. hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
  143. fd.entrylength);
  144. if (be16_to_cpu(entry.type) != HFSPLUS_FOLDER_THREAD) {
  145. printk(KERN_ERR "hfs: bad catalog folder thread\n");
  146. err = -EIO;
  147. goto out;
  148. }
  149. if (fd.entrylength < HFSPLUS_MIN_THREAD_SZ) {
  150. printk(KERN_ERR "hfs: truncated catalog thread\n");
  151. err = -EIO;
  152. goto out;
  153. }
  154. if (filldir(dirent, "..", 2, 1,
  155. be32_to_cpu(entry.thread.parentID), DT_DIR))
  156. goto out;
  157. filp->f_pos++;
  158. /* fall through */
  159. default:
  160. if (filp->f_pos >= inode->i_size)
  161. goto out;
  162. err = hfs_brec_goto(&fd, filp->f_pos - 1);
  163. if (err)
  164. goto out;
  165. }
  166. for (;;) {
  167. if (be32_to_cpu(fd.key->cat.parent) != inode->i_ino) {
  168. printk(KERN_ERR "hfs: walked past end of dir\n");
  169. err = -EIO;
  170. goto out;
  171. }
  172. if (fd.entrylength > sizeof(entry) || fd.entrylength < 0) {
  173. err = -EIO;
  174. goto out;
  175. }
  176. hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
  177. fd.entrylength);
  178. type = be16_to_cpu(entry.type);
  179. len = HFSPLUS_MAX_STRLEN;
  180. err = hfsplus_uni2asc(sb, &fd.key->cat.name, strbuf, &len);
  181. if (err)
  182. goto out;
  183. if (type == HFSPLUS_FOLDER) {
  184. if (fd.entrylength <
  185. sizeof(struct hfsplus_cat_folder)) {
  186. printk(KERN_ERR "hfs: small dir entry\n");
  187. err = -EIO;
  188. goto out;
  189. }
  190. if (HFSPLUS_SB(sb)->hidden_dir &&
  191. HFSPLUS_SB(sb)->hidden_dir->i_ino ==
  192. be32_to_cpu(entry.folder.id))
  193. goto next;
  194. if (filldir(dirent, strbuf, len, filp->f_pos,
  195. be32_to_cpu(entry.folder.id), DT_DIR))
  196. break;
  197. } else if (type == HFSPLUS_FILE) {
  198. if (fd.entrylength < sizeof(struct hfsplus_cat_file)) {
  199. printk(KERN_ERR "hfs: small file entry\n");
  200. err = -EIO;
  201. goto out;
  202. }
  203. if (filldir(dirent, strbuf, len, filp->f_pos,
  204. be32_to_cpu(entry.file.id), DT_REG))
  205. break;
  206. } else {
  207. printk(KERN_ERR "hfs: bad catalog entry type\n");
  208. err = -EIO;
  209. goto out;
  210. }
  211. next:
  212. filp->f_pos++;
  213. if (filp->f_pos >= inode->i_size)
  214. goto out;
  215. err = hfs_brec_goto(&fd, 1);
  216. if (err)
  217. goto out;
  218. }
  219. rd = filp->private_data;
  220. if (!rd) {
  221. rd = kmalloc(sizeof(struct hfsplus_readdir_data), GFP_KERNEL);
  222. if (!rd) {
  223. err = -ENOMEM;
  224. goto out;
  225. }
  226. filp->private_data = rd;
  227. rd->file = filp;
  228. list_add(&rd->list, &HFSPLUS_I(inode)->open_dir_list);
  229. }
  230. memcpy(&rd->key, fd.key, sizeof(struct hfsplus_cat_key));
  231. out:
  232. hfs_find_exit(&fd);
  233. return err;
  234. }
  235. static int hfsplus_dir_release(struct inode *inode, struct file *file)
  236. {
  237. struct hfsplus_readdir_data *rd = file->private_data;
  238. if (rd) {
  239. mutex_lock(&inode->i_mutex);
  240. list_del(&rd->list);
  241. mutex_unlock(&inode->i_mutex);
  242. kfree(rd);
  243. }
  244. return 0;
  245. }
  246. static int hfsplus_link(struct dentry *src_dentry, struct inode *dst_dir,
  247. struct dentry *dst_dentry)
  248. {
  249. struct hfsplus_sb_info *sbi = HFSPLUS_SB(dst_dir->i_sb);
  250. struct inode *inode = src_dentry->d_inode;
  251. struct inode *src_dir = src_dentry->d_parent->d_inode;
  252. struct qstr str;
  253. char name[32];
  254. u32 cnid, id;
  255. int res;
  256. if (HFSPLUS_IS_RSRC(inode))
  257. return -EPERM;
  258. if (!S_ISREG(inode->i_mode))
  259. return -EPERM;
  260. mutex_lock(&sbi->vh_mutex);
  261. if (inode->i_ino == (u32)(unsigned long)src_dentry->d_fsdata) {
  262. for (;;) {
  263. get_random_bytes(&id, sizeof(cnid));
  264. id &= 0x3fffffff;
  265. str.name = name;
  266. str.len = sprintf(name, "iNode%d", id);
  267. res = hfsplus_rename_cat(inode->i_ino,
  268. src_dir, &src_dentry->d_name,
  269. sbi->hidden_dir, &str);
  270. if (!res)
  271. break;
  272. if (res != -EEXIST)
  273. goto out;
  274. }
  275. HFSPLUS_I(inode)->linkid = id;
  276. cnid = sbi->next_cnid++;
  277. src_dentry->d_fsdata = (void *)(unsigned long)cnid;
  278. res = hfsplus_create_cat(cnid, src_dir,
  279. &src_dentry->d_name, inode);
  280. if (res)
  281. /* panic? */
  282. goto out;
  283. sbi->file_count++;
  284. }
  285. cnid = sbi->next_cnid++;
  286. res = hfsplus_create_cat(cnid, dst_dir, &dst_dentry->d_name, inode);
  287. if (res)
  288. goto out;
  289. inc_nlink(inode);
  290. hfsplus_instantiate(dst_dentry, inode, cnid);
  291. ihold(inode);
  292. inode->i_ctime = CURRENT_TIME_SEC;
  293. mark_inode_dirty(inode);
  294. sbi->file_count++;
  295. dst_dir->i_sb->s_dirt = 1;
  296. out:
  297. mutex_unlock(&sbi->vh_mutex);
  298. return res;
  299. }
  300. static int hfsplus_unlink(struct inode *dir, struct dentry *dentry)
  301. {
  302. struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
  303. struct inode *inode = dentry->d_inode;
  304. struct qstr str;
  305. char name[32];
  306. u32 cnid;
  307. int res;
  308. if (HFSPLUS_IS_RSRC(inode))
  309. return -EPERM;
  310. mutex_lock(&sbi->vh_mutex);
  311. cnid = (u32)(unsigned long)dentry->d_fsdata;
  312. if (inode->i_ino == cnid &&
  313. atomic_read(&HFSPLUS_I(inode)->opencnt)) {
  314. str.name = name;
  315. str.len = sprintf(name, "temp%lu", inode->i_ino);
  316. res = hfsplus_rename_cat(inode->i_ino,
  317. dir, &dentry->d_name,
  318. sbi->hidden_dir, &str);
  319. if (!res) {
  320. inode->i_flags |= S_DEAD;
  321. drop_nlink(inode);
  322. }
  323. goto out;
  324. }
  325. res = hfsplus_delete_cat(cnid, dir, &dentry->d_name);
  326. if (res)
  327. goto out;
  328. if (inode->i_nlink > 0)
  329. drop_nlink(inode);
  330. if (inode->i_ino == cnid)
  331. clear_nlink(inode);
  332. if (!inode->i_nlink) {
  333. if (inode->i_ino != cnid) {
  334. sbi->file_count--;
  335. if (!atomic_read(&HFSPLUS_I(inode)->opencnt)) {
  336. res = hfsplus_delete_cat(inode->i_ino,
  337. sbi->hidden_dir,
  338. NULL);
  339. if (!res)
  340. hfsplus_delete_inode(inode);
  341. } else
  342. inode->i_flags |= S_DEAD;
  343. } else
  344. hfsplus_delete_inode(inode);
  345. } else
  346. sbi->file_count--;
  347. inode->i_ctime = CURRENT_TIME_SEC;
  348. mark_inode_dirty(inode);
  349. out:
  350. mutex_unlock(&sbi->vh_mutex);
  351. return res;
  352. }
  353. static int hfsplus_rmdir(struct inode *dir, struct dentry *dentry)
  354. {
  355. struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
  356. struct inode *inode = dentry->d_inode;
  357. int res;
  358. if (inode->i_size != 2)
  359. return -ENOTEMPTY;
  360. mutex_lock(&sbi->vh_mutex);
  361. res = hfsplus_delete_cat(inode->i_ino, dir, &dentry->d_name);
  362. if (res)
  363. goto out;
  364. clear_nlink(inode);
  365. inode->i_ctime = CURRENT_TIME_SEC;
  366. hfsplus_delete_inode(inode);
  367. mark_inode_dirty(inode);
  368. out:
  369. mutex_unlock(&sbi->vh_mutex);
  370. return res;
  371. }
  372. static int hfsplus_symlink(struct inode *dir, struct dentry *dentry,
  373. const char *symname)
  374. {
  375. struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
  376. struct inode *inode;
  377. int res = -ENOSPC;
  378. mutex_lock(&sbi->vh_mutex);
  379. inode = hfsplus_new_inode(dir->i_sb, S_IFLNK | S_IRWXUGO);
  380. if (!inode)
  381. goto out;
  382. res = page_symlink(inode, symname, strlen(symname) + 1);
  383. if (res)
  384. goto out_err;
  385. res = hfsplus_create_cat(inode->i_ino, dir, &dentry->d_name, inode);
  386. if (res)
  387. goto out_err;
  388. hfsplus_instantiate(dentry, inode, inode->i_ino);
  389. mark_inode_dirty(inode);
  390. goto out;
  391. out_err:
  392. inode->i_nlink = 0;
  393. hfsplus_delete_inode(inode);
  394. iput(inode);
  395. out:
  396. mutex_unlock(&sbi->vh_mutex);
  397. return res;
  398. }
  399. static int hfsplus_mknod(struct inode *dir, struct dentry *dentry,
  400. int mode, dev_t rdev)
  401. {
  402. struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
  403. struct inode *inode;
  404. int res = -ENOSPC;
  405. mutex_lock(&sbi->vh_mutex);
  406. inode = hfsplus_new_inode(dir->i_sb, mode);
  407. if (!inode)
  408. goto out;
  409. if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISFIFO(mode) || S_ISSOCK(mode))
  410. init_special_inode(inode, mode, rdev);
  411. res = hfsplus_create_cat(inode->i_ino, dir, &dentry->d_name, inode);
  412. if (res) {
  413. inode->i_nlink = 0;
  414. hfsplus_delete_inode(inode);
  415. iput(inode);
  416. goto out;
  417. }
  418. hfsplus_instantiate(dentry, inode, inode->i_ino);
  419. mark_inode_dirty(inode);
  420. out:
  421. mutex_unlock(&sbi->vh_mutex);
  422. return res;
  423. }
  424. static int hfsplus_create(struct inode *dir, struct dentry *dentry, int mode,
  425. struct nameidata *nd)
  426. {
  427. return hfsplus_mknod(dir, dentry, mode, 0);
  428. }
  429. static int hfsplus_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  430. {
  431. return hfsplus_mknod(dir, dentry, mode | S_IFDIR, 0);
  432. }
  433. static int hfsplus_rename(struct inode *old_dir, struct dentry *old_dentry,
  434. struct inode *new_dir, struct dentry *new_dentry)
  435. {
  436. int res;
  437. /* Unlink destination if it already exists */
  438. if (new_dentry->d_inode) {
  439. if (S_ISDIR(new_dentry->d_inode->i_mode))
  440. res = hfsplus_rmdir(new_dir, new_dentry);
  441. else
  442. res = hfsplus_unlink(new_dir, new_dentry);
  443. if (res)
  444. return res;
  445. }
  446. res = hfsplus_rename_cat((u32)(unsigned long)old_dentry->d_fsdata,
  447. old_dir, &old_dentry->d_name,
  448. new_dir, &new_dentry->d_name);
  449. if (!res)
  450. new_dentry->d_fsdata = old_dentry->d_fsdata;
  451. return res;
  452. }
  453. const struct inode_operations hfsplus_dir_inode_operations = {
  454. .lookup = hfsplus_lookup,
  455. .create = hfsplus_create,
  456. .link = hfsplus_link,
  457. .unlink = hfsplus_unlink,
  458. .mkdir = hfsplus_mkdir,
  459. .rmdir = hfsplus_rmdir,
  460. .symlink = hfsplus_symlink,
  461. .mknod = hfsplus_mknod,
  462. .rename = hfsplus_rename,
  463. };
  464. const struct file_operations hfsplus_dir_operations = {
  465. .fsync = hfsplus_file_fsync,
  466. .read = generic_read_dir,
  467. .readdir = hfsplus_readdir,
  468. .unlocked_ioctl = hfsplus_ioctl,
  469. .llseek = generic_file_llseek,
  470. .release = hfsplus_dir_release,
  471. };