dir.c 7.8 KB

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