dir.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. if (fd.entrylength > sizeof(entry) || fd.entrylength < 0) {
  75. err = -EIO;
  76. goto out;
  77. }
  78. hfs_bnode_read(fd.bnode, &entry, fd.entryoffset, fd.entrylength);
  79. if (entry.type != HFS_CDR_THD) {
  80. printk(KERN_ERR "hfs: bad catalog folder thread\n");
  81. err = -EIO;
  82. goto out;
  83. }
  84. //if (fd.entrylength < HFS_MIN_THREAD_SZ) {
  85. // printk(KERN_ERR "hfs: truncated catalog thread\n");
  86. // err = -EIO;
  87. // goto out;
  88. //}
  89. if (filldir(dirent, "..", 2, 1,
  90. be32_to_cpu(entry.thread.ParID), DT_DIR))
  91. goto out;
  92. filp->f_pos++;
  93. /* fall through */
  94. default:
  95. if (filp->f_pos >= inode->i_size)
  96. goto out;
  97. err = hfs_brec_goto(&fd, filp->f_pos - 1);
  98. if (err)
  99. goto out;
  100. }
  101. for (;;) {
  102. if (be32_to_cpu(fd.key->cat.ParID) != inode->i_ino) {
  103. printk(KERN_ERR "hfs: walked past end of dir\n");
  104. err = -EIO;
  105. goto out;
  106. }
  107. if (fd.entrylength > sizeof(entry) || fd.entrylength < 0) {
  108. err = -EIO;
  109. goto out;
  110. }
  111. hfs_bnode_read(fd.bnode, &entry, fd.entryoffset, fd.entrylength);
  112. type = entry.type;
  113. len = hfs_mac2asc(sb, strbuf, &fd.key->cat.CName);
  114. if (type == HFS_CDR_DIR) {
  115. if (fd.entrylength < sizeof(struct hfs_cat_dir)) {
  116. printk(KERN_ERR "hfs: small dir entry\n");
  117. err = -EIO;
  118. goto out;
  119. }
  120. if (filldir(dirent, strbuf, len, filp->f_pos,
  121. be32_to_cpu(entry.dir.DirID), DT_DIR))
  122. break;
  123. } else if (type == HFS_CDR_FIL) {
  124. if (fd.entrylength < sizeof(struct hfs_cat_file)) {
  125. printk(KERN_ERR "hfs: small file entry\n");
  126. err = -EIO;
  127. goto out;
  128. }
  129. if (filldir(dirent, strbuf, len, filp->f_pos,
  130. be32_to_cpu(entry.file.FlNum), DT_REG))
  131. break;
  132. } else {
  133. printk(KERN_ERR "hfs: bad catalog entry type %d\n", type);
  134. err = -EIO;
  135. goto out;
  136. }
  137. filp->f_pos++;
  138. if (filp->f_pos >= inode->i_size)
  139. goto out;
  140. err = hfs_brec_goto(&fd, 1);
  141. if (err)
  142. goto out;
  143. }
  144. rd = filp->private_data;
  145. if (!rd) {
  146. rd = kmalloc(sizeof(struct hfs_readdir_data), GFP_KERNEL);
  147. if (!rd) {
  148. err = -ENOMEM;
  149. goto out;
  150. }
  151. filp->private_data = rd;
  152. rd->file = filp;
  153. list_add(&rd->list, &HFS_I(inode)->open_dir_list);
  154. }
  155. memcpy(&rd->key, &fd.key, sizeof(struct hfs_cat_key));
  156. out:
  157. hfs_find_exit(&fd);
  158. return err;
  159. }
  160. static int hfs_dir_release(struct inode *inode, struct file *file)
  161. {
  162. struct hfs_readdir_data *rd = file->private_data;
  163. if (rd) {
  164. list_del(&rd->list);
  165. kfree(rd);
  166. }
  167. return 0;
  168. }
  169. /*
  170. * hfs_create()
  171. *
  172. * This is the create() entry in the inode_operations structure for
  173. * regular HFS directories. The purpose is to create a new file in
  174. * a directory and return a corresponding inode, given the inode for
  175. * the directory and the name (and its length) of the new file.
  176. */
  177. static int hfs_create(struct inode *dir, struct dentry *dentry, int mode,
  178. struct nameidata *nd)
  179. {
  180. struct inode *inode;
  181. int res;
  182. inode = hfs_new_inode(dir, &dentry->d_name, mode);
  183. if (!inode)
  184. return -ENOSPC;
  185. res = hfs_cat_create(inode->i_ino, dir, &dentry->d_name, inode);
  186. if (res) {
  187. inode->i_nlink = 0;
  188. hfs_delete_inode(inode);
  189. iput(inode);
  190. return res;
  191. }
  192. d_instantiate(dentry, inode);
  193. mark_inode_dirty(inode);
  194. return 0;
  195. }
  196. /*
  197. * hfs_mkdir()
  198. *
  199. * This is the mkdir() entry in the inode_operations structure for
  200. * regular HFS directories. The purpose is to create a new directory
  201. * in a directory, given the inode for the parent directory and the
  202. * name (and its length) of the new directory.
  203. */
  204. static int hfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  205. {
  206. struct inode *inode;
  207. int res;
  208. inode = hfs_new_inode(dir, &dentry->d_name, S_IFDIR | mode);
  209. if (!inode)
  210. return -ENOSPC;
  211. res = hfs_cat_create(inode->i_ino, dir, &dentry->d_name, inode);
  212. if (res) {
  213. inode->i_nlink = 0;
  214. hfs_delete_inode(inode);
  215. iput(inode);
  216. return res;
  217. }
  218. d_instantiate(dentry, inode);
  219. mark_inode_dirty(inode);
  220. return 0;
  221. }
  222. /*
  223. * hfs_unlink()
  224. *
  225. * This is the unlink() entry in the inode_operations structure for
  226. * regular HFS directories. The purpose is to delete an existing
  227. * file, given the inode for the parent directory and the name
  228. * (and its length) of the existing file.
  229. */
  230. static int hfs_unlink(struct inode *dir, struct dentry *dentry)
  231. {
  232. struct inode *inode;
  233. int res;
  234. inode = dentry->d_inode;
  235. res = hfs_cat_delete(inode->i_ino, dir, &dentry->d_name);
  236. if (res)
  237. return res;
  238. drop_nlink(inode);
  239. hfs_delete_inode(inode);
  240. inode->i_ctime = CURRENT_TIME_SEC;
  241. mark_inode_dirty(inode);
  242. return res;
  243. }
  244. /*
  245. * hfs_rmdir()
  246. *
  247. * This is the rmdir() entry in the inode_operations structure for
  248. * regular HFS directories. The purpose is to delete an existing
  249. * directory, given the inode for the parent directory and the name
  250. * (and its length) of the existing directory.
  251. */
  252. static int hfs_rmdir(struct inode *dir, struct dentry *dentry)
  253. {
  254. struct inode *inode;
  255. int res;
  256. inode = dentry->d_inode;
  257. if (inode->i_size != 2)
  258. return -ENOTEMPTY;
  259. res = hfs_cat_delete(inode->i_ino, dir, &dentry->d_name);
  260. if (res)
  261. return res;
  262. clear_nlink(inode);
  263. inode->i_ctime = CURRENT_TIME_SEC;
  264. hfs_delete_inode(inode);
  265. mark_inode_dirty(inode);
  266. return 0;
  267. }
  268. /*
  269. * hfs_rename()
  270. *
  271. * This is the rename() entry in the inode_operations structure for
  272. * regular HFS directories. The purpose is to rename an existing
  273. * file or directory, given the inode for the current directory and
  274. * the name (and its length) of the existing file/directory and the
  275. * inode for the new directory and the name (and its length) of the
  276. * new file/directory.
  277. * XXX: how do you handle must_be dir?
  278. */
  279. static int hfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  280. struct inode *new_dir, struct dentry *new_dentry)
  281. {
  282. int res;
  283. /* Unlink destination if it already exists */
  284. if (new_dentry->d_inode) {
  285. res = hfs_unlink(new_dir, new_dentry);
  286. if (res)
  287. return res;
  288. }
  289. res = hfs_cat_move(old_dentry->d_inode->i_ino,
  290. old_dir, &old_dentry->d_name,
  291. new_dir, &new_dentry->d_name);
  292. if (!res)
  293. hfs_cat_build_key(old_dir->i_sb,
  294. (btree_key *)&HFS_I(old_dentry->d_inode)->cat_key,
  295. new_dir->i_ino, &new_dentry->d_name);
  296. return res;
  297. }
  298. const struct file_operations hfs_dir_operations = {
  299. .read = generic_read_dir,
  300. .readdir = hfs_readdir,
  301. .llseek = generic_file_llseek,
  302. .release = hfs_dir_release,
  303. };
  304. const struct inode_operations hfs_dir_inode_operations = {
  305. .create = hfs_create,
  306. .lookup = hfs_lookup,
  307. .unlink = hfs_unlink,
  308. .mkdir = hfs_mkdir,
  309. .rmdir = hfs_rmdir,
  310. .rename = hfs_rename,
  311. .setattr = hfs_inode_setattr,
  312. };