dir.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. spin_lock(&HFS_I(inode)->open_dir_lock);
  154. list_add(&rd->list, &HFS_I(inode)->open_dir_list);
  155. spin_unlock(&HFS_I(inode)->open_dir_lock);
  156. }
  157. /*
  158. * Can be done after the list insertion; exclusion with
  159. * hfs_delete_cat() is provided by directory lock.
  160. */
  161. memcpy(&rd->key, &fd.key, sizeof(struct hfs_cat_key));
  162. out:
  163. hfs_find_exit(&fd);
  164. return err;
  165. }
  166. static int hfs_dir_release(struct inode *inode, struct file *file)
  167. {
  168. struct hfs_readdir_data *rd = file->private_data;
  169. if (rd) {
  170. spin_lock(&HFS_I(inode)->open_dir_lock);
  171. list_del(&rd->list);
  172. spin_unlock(&HFS_I(inode)->open_dir_lock);
  173. kfree(rd);
  174. }
  175. return 0;
  176. }
  177. /*
  178. * hfs_create()
  179. *
  180. * This is the create() entry in the inode_operations structure for
  181. * regular HFS directories. The purpose is to create a new file in
  182. * a directory and return a corresponding inode, given the inode for
  183. * the directory and the name (and its length) of the new file.
  184. */
  185. static int hfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  186. bool excl)
  187. {
  188. struct inode *inode;
  189. int res;
  190. inode = hfs_new_inode(dir, &dentry->d_name, mode);
  191. if (!inode)
  192. return -ENOMEM;
  193. res = hfs_cat_create(inode->i_ino, dir, &dentry->d_name, inode);
  194. if (res) {
  195. clear_nlink(inode);
  196. hfs_delete_inode(inode);
  197. iput(inode);
  198. return res;
  199. }
  200. d_instantiate(dentry, inode);
  201. mark_inode_dirty(inode);
  202. return 0;
  203. }
  204. /*
  205. * hfs_mkdir()
  206. *
  207. * This is the mkdir() entry in the inode_operations structure for
  208. * regular HFS directories. The purpose is to create a new directory
  209. * in a directory, given the inode for the parent directory and the
  210. * name (and its length) of the new directory.
  211. */
  212. static int hfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  213. {
  214. struct inode *inode;
  215. int res;
  216. inode = hfs_new_inode(dir, &dentry->d_name, S_IFDIR | mode);
  217. if (!inode)
  218. return -ENOMEM;
  219. res = hfs_cat_create(inode->i_ino, dir, &dentry->d_name, inode);
  220. if (res) {
  221. clear_nlink(inode);
  222. hfs_delete_inode(inode);
  223. iput(inode);
  224. return res;
  225. }
  226. d_instantiate(dentry, inode);
  227. mark_inode_dirty(inode);
  228. return 0;
  229. }
  230. /*
  231. * hfs_remove()
  232. *
  233. * This serves as both unlink() and rmdir() in the inode_operations
  234. * structure for regular HFS directories. The purpose is to delete
  235. * an existing child, given the inode for the parent directory and
  236. * the name (and its length) of the existing directory.
  237. *
  238. * HFS does not have hardlinks, so both rmdir and unlink set the
  239. * link count to 0. The only difference is the emptiness check.
  240. */
  241. static int hfs_remove(struct inode *dir, struct dentry *dentry)
  242. {
  243. struct inode *inode = d_inode(dentry);
  244. int res;
  245. if (S_ISDIR(inode->i_mode) && inode->i_size != 2)
  246. return -ENOTEMPTY;
  247. res = hfs_cat_delete(inode->i_ino, dir, &dentry->d_name);
  248. if (res)
  249. return res;
  250. clear_nlink(inode);
  251. inode->i_ctime = current_time(inode);
  252. hfs_delete_inode(inode);
  253. mark_inode_dirty(inode);
  254. return 0;
  255. }
  256. /*
  257. * hfs_rename()
  258. *
  259. * This is the rename() entry in the inode_operations structure for
  260. * regular HFS directories. The purpose is to rename an existing
  261. * file or directory, given the inode for the current directory and
  262. * the name (and its length) of the existing file/directory and the
  263. * inode for the new directory and the name (and its length) of the
  264. * new file/directory.
  265. * XXX: how do you handle must_be dir?
  266. */
  267. static int hfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  268. struct inode *new_dir, struct dentry *new_dentry,
  269. unsigned int flags)
  270. {
  271. int res;
  272. if (flags & ~RENAME_NOREPLACE)
  273. return -EINVAL;
  274. /* Unlink destination if it already exists */
  275. if (d_really_is_positive(new_dentry)) {
  276. res = hfs_remove(new_dir, new_dentry);
  277. if (res)
  278. return res;
  279. }
  280. res = hfs_cat_move(d_inode(old_dentry)->i_ino,
  281. old_dir, &old_dentry->d_name,
  282. new_dir, &new_dentry->d_name);
  283. if (!res)
  284. hfs_cat_build_key(old_dir->i_sb,
  285. (btree_key *)&HFS_I(d_inode(old_dentry))->cat_key,
  286. new_dir->i_ino, &new_dentry->d_name);
  287. return res;
  288. }
  289. const struct file_operations hfs_dir_operations = {
  290. .read = generic_read_dir,
  291. .iterate_shared = hfs_readdir,
  292. .llseek = generic_file_llseek,
  293. .release = hfs_dir_release,
  294. };
  295. const struct inode_operations hfs_dir_inode_operations = {
  296. .create = hfs_create,
  297. .lookup = hfs_lookup,
  298. .unlink = hfs_remove,
  299. .mkdir = hfs_mkdir,
  300. .rmdir = hfs_remove,
  301. .rename = hfs_rename,
  302. .setattr = hfs_inode_setattr,
  303. };