dir.c 11 KB

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