dir.c 11 KB

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