dir.c 12 KB

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