dir.c 12 KB

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