dir.c 12 KB

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