dir.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. if (res != -ENOENT)
  32. inode = ERR_PTR(res);
  33. } else {
  34. inode = hfs_iget(dir->i_sb, &fd.search_key->cat, &rec);
  35. if (!inode)
  36. inode = ERR_PTR(-EACCES);
  37. }
  38. hfs_find_exit(&fd);
  39. return d_splice_alias(inode, dentry);
  40. }
  41. /*
  42. * hfs_readdir
  43. */
  44. static int hfs_readdir(struct file *file, struct dir_context *ctx)
  45. {
  46. struct inode *inode = file_inode(file);
  47. struct super_block *sb = inode->i_sb;
  48. int len, err;
  49. char strbuf[HFS_MAX_NAMELEN];
  50. union hfs_cat_rec entry;
  51. struct hfs_find_data fd;
  52. struct hfs_readdir_data *rd;
  53. u16 type;
  54. if (ctx->pos >= inode->i_size)
  55. return 0;
  56. err = hfs_find_init(HFS_SB(sb)->cat_tree, &fd);
  57. if (err)
  58. return err;
  59. hfs_cat_build_key(sb, fd.search_key, inode->i_ino, NULL);
  60. err = hfs_brec_find(&fd);
  61. if (err)
  62. goto out;
  63. if (ctx->pos == 0) {
  64. /* This is completely artificial... */
  65. if (!dir_emit_dot(file, ctx))
  66. goto out;
  67. ctx->pos = 1;
  68. }
  69. if (ctx->pos == 1) {
  70. if (fd.entrylength > sizeof(entry) || fd.entrylength < 0) {
  71. err = -EIO;
  72. goto out;
  73. }
  74. hfs_bnode_read(fd.bnode, &entry, fd.entryoffset, fd.entrylength);
  75. if (entry.type != HFS_CDR_THD) {
  76. pr_err("bad catalog folder thread\n");
  77. err = -EIO;
  78. goto out;
  79. }
  80. //if (fd.entrylength < HFS_MIN_THREAD_SZ) {
  81. // pr_err("truncated catalog thread\n");
  82. // err = -EIO;
  83. // goto out;
  84. //}
  85. if (!dir_emit(ctx, "..", 2,
  86. be32_to_cpu(entry.thread.ParID), DT_DIR))
  87. goto out;
  88. ctx->pos = 2;
  89. }
  90. if (ctx->pos >= inode->i_size)
  91. goto out;
  92. err = hfs_brec_goto(&fd, ctx->pos - 1);
  93. if (err)
  94. goto out;
  95. for (;;) {
  96. if (be32_to_cpu(fd.key->cat.ParID) != inode->i_ino) {
  97. pr_err("walked past end of dir\n");
  98. err = -EIO;
  99. goto out;
  100. }
  101. if (fd.entrylength > sizeof(entry) || fd.entrylength < 0) {
  102. err = -EIO;
  103. goto out;
  104. }
  105. hfs_bnode_read(fd.bnode, &entry, fd.entryoffset, fd.entrylength);
  106. type = entry.type;
  107. len = hfs_mac2asc(sb, strbuf, &fd.key->cat.CName);
  108. if (type == HFS_CDR_DIR) {
  109. if (fd.entrylength < sizeof(struct hfs_cat_dir)) {
  110. pr_err("small dir entry\n");
  111. err = -EIO;
  112. goto out;
  113. }
  114. if (!dir_emit(ctx, strbuf, len,
  115. be32_to_cpu(entry.dir.DirID), DT_DIR))
  116. break;
  117. } else if (type == HFS_CDR_FIL) {
  118. if (fd.entrylength < sizeof(struct hfs_cat_file)) {
  119. pr_err("small file entry\n");
  120. err = -EIO;
  121. goto out;
  122. }
  123. if (!dir_emit(ctx, strbuf, len,
  124. be32_to_cpu(entry.file.FlNum), DT_REG))
  125. break;
  126. } else {
  127. pr_err("bad catalog entry type %d\n", type);
  128. err = -EIO;
  129. goto out;
  130. }
  131. ctx->pos++;
  132. if (ctx->pos >= inode->i_size)
  133. goto out;
  134. err = hfs_brec_goto(&fd, 1);
  135. if (err)
  136. goto out;
  137. }
  138. rd = file->private_data;
  139. if (!rd) {
  140. rd = kmalloc(sizeof(struct hfs_readdir_data), GFP_KERNEL);
  141. if (!rd) {
  142. err = -ENOMEM;
  143. goto out;
  144. }
  145. file->private_data = rd;
  146. rd->file = file;
  147. spin_lock(&HFS_I(inode)->open_dir_lock);
  148. list_add(&rd->list, &HFS_I(inode)->open_dir_list);
  149. spin_unlock(&HFS_I(inode)->open_dir_lock);
  150. }
  151. /*
  152. * Can be done after the list insertion; exclusion with
  153. * hfs_delete_cat() is provided by directory lock.
  154. */
  155. memcpy(&rd->key, &fd.key->cat, 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. spin_lock(&HFS_I(inode)->open_dir_lock);
  165. list_del(&rd->list);
  166. spin_unlock(&HFS_I(inode)->open_dir_lock);
  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(inode);
  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. unsigned int flags)
  264. {
  265. int res;
  266. if (flags & ~RENAME_NOREPLACE)
  267. return -EINVAL;
  268. /* Unlink destination if it already exists */
  269. if (d_really_is_positive(new_dentry)) {
  270. res = hfs_remove(new_dir, new_dentry);
  271. if (res)
  272. return res;
  273. }
  274. res = hfs_cat_move(d_inode(old_dentry)->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(d_inode(old_dentry))->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. .iterate_shared = 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. };