dir.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 *file, struct dir_context *ctx)
  51. {
  52. struct inode *inode = file_inode(file);
  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 (ctx->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. if (ctx->pos == 0) {
  70. /* This is completely artificial... */
  71. if (!dir_emit_dot(file, ctx))
  72. goto out;
  73. ctx->pos = 1;
  74. }
  75. if (ctx->pos == 1) {
  76. if (fd.entrylength > sizeof(entry) || fd.entrylength < 0) {
  77. err = -EIO;
  78. goto out;
  79. }
  80. hfs_bnode_read(fd.bnode, &entry, fd.entryoffset, fd.entrylength);
  81. if (entry.type != HFS_CDR_THD) {
  82. pr_err("bad catalog folder thread\n");
  83. err = -EIO;
  84. goto out;
  85. }
  86. //if (fd.entrylength < HFS_MIN_THREAD_SZ) {
  87. // pr_err("truncated catalog thread\n");
  88. // err = -EIO;
  89. // goto out;
  90. //}
  91. if (!dir_emit(ctx, "..", 2,
  92. be32_to_cpu(entry.thread.ParID), DT_DIR))
  93. goto out;
  94. ctx->pos = 2;
  95. }
  96. if (ctx->pos >= inode->i_size)
  97. goto out;
  98. err = hfs_brec_goto(&fd, ctx->pos - 1);
  99. if (err)
  100. goto out;
  101. for (;;) {
  102. if (be32_to_cpu(fd.key->cat.ParID) != inode->i_ino) {
  103. pr_err("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. pr_err("small dir entry\n");
  117. err = -EIO;
  118. goto out;
  119. }
  120. if (!dir_emit(ctx, strbuf, len,
  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. pr_err("small file entry\n");
  126. err = -EIO;
  127. goto out;
  128. }
  129. if (!dir_emit(ctx, strbuf, len,
  130. be32_to_cpu(entry.file.FlNum), DT_REG))
  131. break;
  132. } else {
  133. pr_err("bad catalog entry type %d\n", type);
  134. err = -EIO;
  135. goto out;
  136. }
  137. ctx->pos++;
  138. if (ctx->pos >= inode->i_size)
  139. goto out;
  140. err = hfs_brec_goto(&fd, 1);
  141. if (err)
  142. goto out;
  143. }
  144. rd = file->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. file->private_data = rd;
  152. rd->file = file;
  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. mutex_lock(&inode->i_mutex);
  165. list_del(&rd->list);
  166. mutex_unlock(&inode->i_mutex);
  167. kfree(rd);
  168. }
  169. return 0;
  170. }
  171. /*
  172. * hfs_create()
  173. *
  174. * This is the create() entry in the inode_operations structure for
  175. * regular HFS directories. The purpose is to create a new file in
  176. * a directory and return a corresponding inode, given the inode for
  177. * the directory and the name (and its length) of the new file.
  178. */
  179. static int hfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  180. bool excl)
  181. {
  182. struct inode *inode;
  183. int res;
  184. inode = hfs_new_inode(dir, &dentry->d_name, mode);
  185. if (!inode)
  186. return -ENOMEM;
  187. res = hfs_cat_create(inode->i_ino, dir, &dentry->d_name, inode);
  188. if (res) {
  189. clear_nlink(inode);
  190. hfs_delete_inode(inode);
  191. iput(inode);
  192. return res;
  193. }
  194. d_instantiate(dentry, inode);
  195. mark_inode_dirty(inode);
  196. return 0;
  197. }
  198. /*
  199. * hfs_mkdir()
  200. *
  201. * This is the mkdir() entry in the inode_operations structure for
  202. * regular HFS directories. The purpose is to create a new directory
  203. * in a directory, given the inode for the parent directory and the
  204. * name (and its length) of the new directory.
  205. */
  206. static int hfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  207. {
  208. struct inode *inode;
  209. int res;
  210. inode = hfs_new_inode(dir, &dentry->d_name, S_IFDIR | mode);
  211. if (!inode)
  212. return -ENOMEM;
  213. res = hfs_cat_create(inode->i_ino, dir, &dentry->d_name, inode);
  214. if (res) {
  215. clear_nlink(inode);
  216. hfs_delete_inode(inode);
  217. iput(inode);
  218. return res;
  219. }
  220. d_instantiate(dentry, inode);
  221. mark_inode_dirty(inode);
  222. return 0;
  223. }
  224. /*
  225. * hfs_remove()
  226. *
  227. * This serves as both unlink() and rmdir() in the inode_operations
  228. * structure for regular HFS directories. The purpose is to delete
  229. * an existing child, given the inode for the parent directory and
  230. * the name (and its length) of the existing directory.
  231. *
  232. * HFS does not have hardlinks, so both rmdir and unlink set the
  233. * link count to 0. The only difference is the emptiness check.
  234. */
  235. static int hfs_remove(struct inode *dir, struct dentry *dentry)
  236. {
  237. struct inode *inode = d_inode(dentry);
  238. int res;
  239. if (S_ISDIR(inode->i_mode) && inode->i_size != 2)
  240. return -ENOTEMPTY;
  241. res = hfs_cat_delete(inode->i_ino, dir, &dentry->d_name);
  242. if (res)
  243. return res;
  244. clear_nlink(inode);
  245. inode->i_ctime = CURRENT_TIME_SEC;
  246. hfs_delete_inode(inode);
  247. mark_inode_dirty(inode);
  248. return 0;
  249. }
  250. /*
  251. * hfs_rename()
  252. *
  253. * This is the rename() entry in the inode_operations structure for
  254. * regular HFS directories. The purpose is to rename an existing
  255. * file or directory, given the inode for the current directory and
  256. * the name (and its length) of the existing file/directory and the
  257. * inode for the new directory and the name (and its length) of the
  258. * new file/directory.
  259. * XXX: how do you handle must_be dir?
  260. */
  261. static int hfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  262. struct inode *new_dir, struct dentry *new_dentry)
  263. {
  264. int res;
  265. /* Unlink destination if it already exists */
  266. if (d_really_is_positive(new_dentry)) {
  267. res = hfs_remove(new_dir, new_dentry);
  268. if (res)
  269. return res;
  270. }
  271. res = hfs_cat_move(d_inode(old_dentry)->i_ino,
  272. old_dir, &old_dentry->d_name,
  273. new_dir, &new_dentry->d_name);
  274. if (!res)
  275. hfs_cat_build_key(old_dir->i_sb,
  276. (btree_key *)&HFS_I(d_inode(old_dentry))->cat_key,
  277. new_dir->i_ino, &new_dentry->d_name);
  278. return res;
  279. }
  280. const struct file_operations hfs_dir_operations = {
  281. .read = generic_read_dir,
  282. .iterate = hfs_readdir,
  283. .llseek = generic_file_llseek,
  284. .release = hfs_dir_release,
  285. };
  286. const struct inode_operations hfs_dir_inode_operations = {
  287. .create = hfs_create,
  288. .lookup = hfs_lookup,
  289. .unlink = hfs_remove,
  290. .mkdir = hfs_mkdir,
  291. .rmdir = hfs_remove,
  292. .rename = hfs_rename,
  293. .setattr = hfs_inode_setattr,
  294. };