dir.c 7.7 KB

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