dir.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. * linux/fs/hfs/dir.c
  3. *
  4. * Copyright (C) 1995-1997 Paul H. Hargrove
  5. * (C) 2003 Ardis Technologies <roman@ardistech.com>
  6. * This file may be distributed under the terms of the GNU General Public License.
  7. *
  8. * This file contains directory-related functions independent of which
  9. * scheme is being used to represent forks.
  10. *
  11. * Based on the minix file system code, (C) 1991, 1992 by Linus Torvalds
  12. */
  13. #include "hfs_fs.h"
  14. #include "btree.h"
  15. /*
  16. * hfs_lookup()
  17. */
  18. static struct dentry *hfs_lookup(struct inode *dir, struct dentry *dentry,
  19. struct nameidata *nd)
  20. {
  21. hfs_cat_rec rec;
  22. struct hfs_find_data fd;
  23. struct inode *inode = NULL;
  24. int res;
  25. dentry->d_op = &hfs_dentry_operations;
  26. hfs_find_init(HFS_SB(dir->i_sb)->cat_tree, &fd);
  27. hfs_cat_build_key(dir->i_sb, fd.search_key, dir->i_ino, &dentry->d_name);
  28. res = hfs_brec_read(&fd, &rec, sizeof(rec));
  29. if (res) {
  30. hfs_find_exit(&fd);
  31. if (res == -ENOENT) {
  32. /* No such entry */
  33. inode = NULL;
  34. goto done;
  35. }
  36. return ERR_PTR(res);
  37. }
  38. inode = hfs_iget(dir->i_sb, &fd.search_key->cat, &rec);
  39. hfs_find_exit(&fd);
  40. if (!inode)
  41. return ERR_PTR(-EACCES);
  42. done:
  43. d_add(dentry, inode);
  44. return NULL;
  45. }
  46. /*
  47. * hfs_readdir
  48. */
  49. static int hfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
  50. {
  51. struct inode *inode = filp->f_path.dentry->d_inode;
  52. struct super_block *sb = inode->i_sb;
  53. int len, err;
  54. char strbuf[HFS_MAX_NAMELEN];
  55. union hfs_cat_rec entry;
  56. struct hfs_find_data fd;
  57. struct hfs_readdir_data *rd;
  58. u16 type;
  59. if (filp->f_pos >= inode->i_size)
  60. return 0;
  61. hfs_find_init(HFS_SB(sb)->cat_tree, &fd);
  62. hfs_cat_build_key(sb, fd.search_key, inode->i_ino, NULL);
  63. err = hfs_brec_find(&fd);
  64. if (err)
  65. goto out;
  66. switch ((u32)filp->f_pos) {
  67. case 0:
  68. /* This is completely artificial... */
  69. if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR))
  70. goto out;
  71. filp->f_pos++;
  72. /* fall through */
  73. case 1:
  74. hfs_bnode_read(fd.bnode, &entry, fd.entryoffset, fd.entrylength);
  75. if (entry.type != HFS_CDR_THD) {
  76. printk(KERN_ERR "hfs: bad catalog folder thread\n");
  77. err = -EIO;
  78. goto out;
  79. }
  80. //if (fd.entrylength < HFS_MIN_THREAD_SZ) {
  81. // printk(KERN_ERR "hfs: truncated catalog thread\n");
  82. // err = -EIO;
  83. // goto out;
  84. //}
  85. if (filldir(dirent, "..", 2, 1,
  86. be32_to_cpu(entry.thread.ParID), DT_DIR))
  87. goto out;
  88. filp->f_pos++;
  89. /* fall through */
  90. default:
  91. if (filp->f_pos >= inode->i_size)
  92. goto out;
  93. err = hfs_brec_goto(&fd, filp->f_pos - 1);
  94. if (err)
  95. goto out;
  96. }
  97. for (;;) {
  98. if (be32_to_cpu(fd.key->cat.ParID) != inode->i_ino) {
  99. printk(KERN_ERR "hfs: walked past end of dir\n");
  100. err = -EIO;
  101. goto out;
  102. }
  103. hfs_bnode_read(fd.bnode, &entry, fd.entryoffset, fd.entrylength);
  104. type = entry.type;
  105. len = hfs_mac2asc(sb, strbuf, &fd.key->cat.CName);
  106. if (type == HFS_CDR_DIR) {
  107. if (fd.entrylength < sizeof(struct hfs_cat_dir)) {
  108. printk(KERN_ERR "hfs: small dir entry\n");
  109. err = -EIO;
  110. goto out;
  111. }
  112. if (filldir(dirent, strbuf, len, filp->f_pos,
  113. be32_to_cpu(entry.dir.DirID), DT_DIR))
  114. break;
  115. } else if (type == HFS_CDR_FIL) {
  116. if (fd.entrylength < sizeof(struct hfs_cat_file)) {
  117. printk(KERN_ERR "hfs: small file entry\n");
  118. err = -EIO;
  119. goto out;
  120. }
  121. if (filldir(dirent, strbuf, len, filp->f_pos,
  122. be32_to_cpu(entry.file.FlNum), DT_REG))
  123. break;
  124. } else {
  125. printk(KERN_ERR "hfs: bad catalog entry type %d\n", type);
  126. err = -EIO;
  127. goto out;
  128. }
  129. filp->f_pos++;
  130. if (filp->f_pos >= inode->i_size)
  131. goto out;
  132. err = hfs_brec_goto(&fd, 1);
  133. if (err)
  134. goto out;
  135. }
  136. rd = filp->private_data;
  137. if (!rd) {
  138. rd = kmalloc(sizeof(struct hfs_readdir_data), GFP_KERNEL);
  139. if (!rd) {
  140. err = -ENOMEM;
  141. goto out;
  142. }
  143. filp->private_data = rd;
  144. rd->file = filp;
  145. list_add(&rd->list, &HFS_I(inode)->open_dir_list);
  146. }
  147. memcpy(&rd->key, &fd.key, sizeof(struct hfs_cat_key));
  148. out:
  149. hfs_find_exit(&fd);
  150. return err;
  151. }
  152. static int hfs_dir_release(struct inode *inode, struct file *file)
  153. {
  154. struct hfs_readdir_data *rd = file->private_data;
  155. if (rd) {
  156. list_del(&rd->list);
  157. kfree(rd);
  158. }
  159. return 0;
  160. }
  161. /*
  162. * hfs_create()
  163. *
  164. * This is the create() entry in the inode_operations structure for
  165. * regular HFS directories. The purpose is to create a new file in
  166. * a directory and return a corresponding inode, given the inode for
  167. * the directory and the name (and its length) of the new file.
  168. */
  169. static int hfs_create(struct inode *dir, struct dentry *dentry, int mode,
  170. struct nameidata *nd)
  171. {
  172. struct inode *inode;
  173. int res;
  174. inode = hfs_new_inode(dir, &dentry->d_name, mode);
  175. if (!inode)
  176. return -ENOSPC;
  177. res = hfs_cat_create(inode->i_ino, dir, &dentry->d_name, inode);
  178. if (res) {
  179. inode->i_nlink = 0;
  180. hfs_delete_inode(inode);
  181. iput(inode);
  182. return res;
  183. }
  184. d_instantiate(dentry, inode);
  185. mark_inode_dirty(inode);
  186. return 0;
  187. }
  188. /*
  189. * hfs_mkdir()
  190. *
  191. * This is the mkdir() entry in the inode_operations structure for
  192. * regular HFS directories. The purpose is to create a new directory
  193. * in a directory, given the inode for the parent directory and the
  194. * name (and its length) of the new directory.
  195. */
  196. static int hfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  197. {
  198. struct inode *inode;
  199. int res;
  200. inode = hfs_new_inode(dir, &dentry->d_name, S_IFDIR | mode);
  201. if (!inode)
  202. return -ENOSPC;
  203. res = hfs_cat_create(inode->i_ino, dir, &dentry->d_name, inode);
  204. if (res) {
  205. inode->i_nlink = 0;
  206. hfs_delete_inode(inode);
  207. iput(inode);
  208. return res;
  209. }
  210. d_instantiate(dentry, inode);
  211. mark_inode_dirty(inode);
  212. return 0;
  213. }
  214. /*
  215. * hfs_unlink()
  216. *
  217. * This is the unlink() entry in the inode_operations structure for
  218. * regular HFS directories. The purpose is to delete an existing
  219. * file, given the inode for the parent directory and the name
  220. * (and its length) of the existing file.
  221. */
  222. static int hfs_unlink(struct inode *dir, struct dentry *dentry)
  223. {
  224. struct inode *inode;
  225. int res;
  226. inode = dentry->d_inode;
  227. res = hfs_cat_delete(inode->i_ino, dir, &dentry->d_name);
  228. if (res)
  229. return res;
  230. drop_nlink(inode);
  231. hfs_delete_inode(inode);
  232. inode->i_ctime = CURRENT_TIME_SEC;
  233. mark_inode_dirty(inode);
  234. return res;
  235. }
  236. /*
  237. * hfs_rmdir()
  238. *
  239. * This is the rmdir() entry in the inode_operations structure for
  240. * regular HFS directories. The purpose is to delete an existing
  241. * directory, given the inode for the parent directory and the name
  242. * (and its length) of the existing directory.
  243. */
  244. static int hfs_rmdir(struct inode *dir, struct dentry *dentry)
  245. {
  246. struct inode *inode;
  247. int res;
  248. inode = dentry->d_inode;
  249. if (inode->i_size != 2)
  250. return -ENOTEMPTY;
  251. res = hfs_cat_delete(inode->i_ino, dir, &dentry->d_name);
  252. if (res)
  253. return res;
  254. clear_nlink(inode);
  255. inode->i_ctime = CURRENT_TIME_SEC;
  256. hfs_delete_inode(inode);
  257. mark_inode_dirty(inode);
  258. return 0;
  259. }
  260. /*
  261. * hfs_rename()
  262. *
  263. * This is the rename() entry in the inode_operations structure for
  264. * regular HFS directories. The purpose is to rename an existing
  265. * file or directory, given the inode for the current directory and
  266. * the name (and its length) of the existing file/directory and the
  267. * inode for the new directory and the name (and its length) of the
  268. * new file/directory.
  269. * XXX: how do you handle must_be dir?
  270. */
  271. static int hfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  272. struct inode *new_dir, struct dentry *new_dentry)
  273. {
  274. int res;
  275. /* Unlink destination if it already exists */
  276. if (new_dentry->d_inode) {
  277. res = hfs_unlink(new_dir, new_dentry);
  278. if (res)
  279. return res;
  280. }
  281. res = hfs_cat_move(old_dentry->d_inode->i_ino,
  282. old_dir, &old_dentry->d_name,
  283. new_dir, &new_dentry->d_name);
  284. if (!res)
  285. hfs_cat_build_key(old_dir->i_sb,
  286. (btree_key *)&HFS_I(old_dentry->d_inode)->cat_key,
  287. new_dir->i_ino, &new_dentry->d_name);
  288. return res;
  289. }
  290. const struct file_operations hfs_dir_operations = {
  291. .read = generic_read_dir,
  292. .readdir = hfs_readdir,
  293. .llseek = generic_file_llseek,
  294. .release = hfs_dir_release,
  295. };
  296. const struct inode_operations hfs_dir_inode_operations = {
  297. .create = hfs_create,
  298. .lookup = hfs_lookup,
  299. .unlink = hfs_unlink,
  300. .mkdir = hfs_mkdir,
  301. .rmdir = hfs_rmdir,
  302. .rename = hfs_rename,
  303. .setattr = hfs_inode_setattr,
  304. };