dir.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/hpfs/dir.c
  4. *
  5. * Mikulas Patocka ([email protected]), 1998-1999
  6. *
  7. * directory VFS functions
  8. */
  9. #include <linux/slab.h>
  10. #include "hpfs_fn.h"
  11. static int hpfs_dir_release(struct inode *inode, struct file *filp)
  12. {
  13. hpfs_lock(inode->i_sb);
  14. hpfs_del_pos(inode, &filp->f_pos);
  15. /*hpfs_write_if_changed(inode);*/
  16. hpfs_unlock(inode->i_sb);
  17. return 0;
  18. }
  19. /* This is slow, but it's not used often */
  20. static loff_t hpfs_dir_lseek(struct file *filp, loff_t off, int whence)
  21. {
  22. loff_t new_off = off + (whence == 1 ? filp->f_pos : 0);
  23. loff_t pos;
  24. struct quad_buffer_head qbh;
  25. struct inode *i = file_inode(filp);
  26. struct hpfs_inode_info *hpfs_inode = hpfs_i(i);
  27. struct super_block *s = i->i_sb;
  28. /* Somebody else will have to figure out what to do here */
  29. if (whence == SEEK_DATA || whence == SEEK_HOLE)
  30. return -EINVAL;
  31. inode_lock(i);
  32. hpfs_lock(s);
  33. /*pr_info("dir lseek\n");*/
  34. if (new_off == 0 || new_off == 1 || new_off == 11 || new_off == 12 || new_off == 13) goto ok;
  35. pos = ((loff_t) hpfs_de_as_down_as_possible(s, hpfs_inode->i_dno) << 4) + 1;
  36. while (pos != new_off) {
  37. if (map_pos_dirent(i, &pos, &qbh)) hpfs_brelse4(&qbh);
  38. else goto fail;
  39. if (pos == 12) goto fail;
  40. }
  41. if (unlikely(hpfs_add_pos(i, &filp->f_pos) < 0)) {
  42. hpfs_unlock(s);
  43. inode_unlock(i);
  44. return -ENOMEM;
  45. }
  46. ok:
  47. filp->f_pos = new_off;
  48. hpfs_unlock(s);
  49. inode_unlock(i);
  50. return new_off;
  51. fail:
  52. /*pr_warn("illegal lseek: %016llx\n", new_off);*/
  53. hpfs_unlock(s);
  54. inode_unlock(i);
  55. return -ESPIPE;
  56. }
  57. static int hpfs_readdir(struct file *file, struct dir_context *ctx)
  58. {
  59. struct inode *inode = file_inode(file);
  60. struct hpfs_inode_info *hpfs_inode = hpfs_i(inode);
  61. struct quad_buffer_head qbh;
  62. struct hpfs_dirent *de;
  63. int lc;
  64. loff_t next_pos;
  65. unsigned char *tempname;
  66. int c1, c2 = 0;
  67. int ret = 0;
  68. hpfs_lock(inode->i_sb);
  69. if (hpfs_sb(inode->i_sb)->sb_chk) {
  70. if (hpfs_chk_sectors(inode->i_sb, inode->i_ino, 1, "dir_fnode")) {
  71. ret = -EFSERROR;
  72. goto out;
  73. }
  74. if (hpfs_chk_sectors(inode->i_sb, hpfs_inode->i_dno, 4, "dir_dnode")) {
  75. ret = -EFSERROR;
  76. goto out;
  77. }
  78. }
  79. if (hpfs_sb(inode->i_sb)->sb_chk >= 2) {
  80. struct buffer_head *bh;
  81. struct fnode *fno;
  82. int e = 0;
  83. if (!(fno = hpfs_map_fnode(inode->i_sb, inode->i_ino, &bh))) {
  84. ret = -EIOERROR;
  85. goto out;
  86. }
  87. if (!fnode_is_dir(fno)) {
  88. e = 1;
  89. hpfs_error(inode->i_sb, "not a directory, fnode %08lx",
  90. (unsigned long)inode->i_ino);
  91. }
  92. if (hpfs_inode->i_dno != le32_to_cpu(fno->u.external[0].disk_secno)) {
  93. e = 1;
  94. hpfs_error(inode->i_sb, "corrupted inode: i_dno == %08x, fnode -> dnode == %08x", hpfs_inode->i_dno, le32_to_cpu(fno->u.external[0].disk_secno));
  95. }
  96. brelse(bh);
  97. if (e) {
  98. ret = -EFSERROR;
  99. goto out;
  100. }
  101. }
  102. lc = hpfs_sb(inode->i_sb)->sb_lowercase;
  103. if (ctx->pos == 12) { /* diff -r requires this (note, that diff -r */
  104. ctx->pos = 13; /* also fails on msdos filesystem in 2.0) */
  105. goto out;
  106. }
  107. if (ctx->pos == 13) {
  108. ret = -ENOENT;
  109. goto out;
  110. }
  111. while (1) {
  112. again:
  113. /* This won't work when cycle is longer than number of dirents
  114. accepted by filldir, but what can I do?
  115. maybe killall -9 ls helps */
  116. if (hpfs_sb(inode->i_sb)->sb_chk)
  117. if (hpfs_stop_cycles(inode->i_sb, ctx->pos, &c1, &c2, "hpfs_readdir")) {
  118. ret = -EFSERROR;
  119. goto out;
  120. }
  121. if (ctx->pos == 12)
  122. goto out;
  123. if (ctx->pos == 3 || ctx->pos == 4 || ctx->pos == 5) {
  124. pr_err("pos==%d\n", (int)ctx->pos);
  125. goto out;
  126. }
  127. if (ctx->pos == 0) {
  128. if (!dir_emit_dot(file, ctx))
  129. goto out;
  130. ctx->pos = 11;
  131. }
  132. if (ctx->pos == 11) {
  133. if (!dir_emit(ctx, "..", 2, hpfs_inode->i_parent_dir, DT_DIR))
  134. goto out;
  135. ctx->pos = 1;
  136. }
  137. if (ctx->pos == 1) {
  138. ret = hpfs_add_pos(inode, &file->f_pos);
  139. if (unlikely(ret < 0))
  140. goto out;
  141. ctx->pos = ((loff_t) hpfs_de_as_down_as_possible(inode->i_sb, hpfs_inode->i_dno) << 4) + 1;
  142. }
  143. next_pos = ctx->pos;
  144. if (!(de = map_pos_dirent(inode, &next_pos, &qbh))) {
  145. ctx->pos = next_pos;
  146. ret = -EIOERROR;
  147. goto out;
  148. }
  149. if (de->first || de->last) {
  150. if (hpfs_sb(inode->i_sb)->sb_chk) {
  151. if (de->first && !de->last && (de->namelen != 2
  152. || de ->name[0] != 1 || de->name[1] != 1))
  153. hpfs_error(inode->i_sb, "hpfs_readdir: bad ^A^A entry; pos = %08lx", (unsigned long)ctx->pos);
  154. if (de->last && (de->namelen != 1 || de ->name[0] != 255))
  155. hpfs_error(inode->i_sb, "hpfs_readdir: bad \\377 entry; pos = %08lx", (unsigned long)ctx->pos);
  156. }
  157. hpfs_brelse4(&qbh);
  158. ctx->pos = next_pos;
  159. goto again;
  160. }
  161. tempname = hpfs_translate_name(inode->i_sb, de->name, de->namelen, lc, de->not_8x3);
  162. if (!dir_emit(ctx, tempname, de->namelen, le32_to_cpu(de->fnode), DT_UNKNOWN)) {
  163. if (tempname != de->name) kfree(tempname);
  164. hpfs_brelse4(&qbh);
  165. goto out;
  166. }
  167. ctx->pos = next_pos;
  168. if (tempname != de->name) kfree(tempname);
  169. hpfs_brelse4(&qbh);
  170. }
  171. out:
  172. hpfs_unlock(inode->i_sb);
  173. return ret;
  174. }
  175. /*
  176. * lookup. Search the specified directory for the specified name, set
  177. * *result to the corresponding inode.
  178. *
  179. * lookup uses the inode number to tell read_inode whether it is reading
  180. * the inode of a directory or a file -- file ino's are odd, directory
  181. * ino's are even. read_inode avoids i/o for file inodes; everything
  182. * needed is up here in the directory. (And file fnodes are out in
  183. * the boondocks.)
  184. *
  185. * - M.P.: this is over, sometimes we've got to read file's fnode for eas
  186. * inode numbers are just fnode sector numbers; iget lock is used
  187. * to tell read_inode to read fnode or not.
  188. */
  189. struct dentry *hpfs_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
  190. {
  191. const unsigned char *name = dentry->d_name.name;
  192. unsigned len = dentry->d_name.len;
  193. struct quad_buffer_head qbh;
  194. struct hpfs_dirent *de;
  195. ino_t ino;
  196. int err;
  197. struct inode *result = NULL;
  198. struct hpfs_inode_info *hpfs_result;
  199. hpfs_lock(dir->i_sb);
  200. if ((err = hpfs_chk_name(name, &len))) {
  201. if (err == -ENAMETOOLONG) {
  202. hpfs_unlock(dir->i_sb);
  203. return ERR_PTR(-ENAMETOOLONG);
  204. }
  205. goto end_add;
  206. }
  207. /*
  208. * '.' and '..' will never be passed here.
  209. */
  210. de = map_dirent(dir, hpfs_i(dir)->i_dno, name, len, NULL, &qbh);
  211. /*
  212. * This is not really a bailout, just means file not found.
  213. */
  214. if (!de) goto end;
  215. /*
  216. * Get inode number, what we're after.
  217. */
  218. ino = le32_to_cpu(de->fnode);
  219. /*
  220. * Go find or make an inode.
  221. */
  222. result = iget_locked(dir->i_sb, ino);
  223. if (!result) {
  224. hpfs_error(dir->i_sb, "hpfs_lookup: can't get inode");
  225. result = ERR_PTR(-ENOMEM);
  226. goto bail1;
  227. }
  228. if (result->i_state & I_NEW) {
  229. hpfs_init_inode(result);
  230. if (de->directory)
  231. hpfs_read_inode(result);
  232. else if (le32_to_cpu(de->ea_size) && hpfs_sb(dir->i_sb)->sb_eas)
  233. hpfs_read_inode(result);
  234. else {
  235. result->i_mode |= S_IFREG;
  236. result->i_mode &= ~0111;
  237. result->i_op = &hpfs_file_iops;
  238. result->i_fop = &hpfs_file_ops;
  239. set_nlink(result, 1);
  240. }
  241. unlock_new_inode(result);
  242. }
  243. hpfs_result = hpfs_i(result);
  244. if (!de->directory) hpfs_result->i_parent_dir = dir->i_ino;
  245. if (de->has_acl || de->has_xtd_perm) if (!sb_rdonly(dir->i_sb)) {
  246. hpfs_error(result->i_sb, "ACLs or XPERM found. This is probably HPFS386. This driver doesn't support it now. Send me some info on these structures");
  247. iput(result);
  248. result = ERR_PTR(-EINVAL);
  249. goto bail1;
  250. }
  251. /*
  252. * Fill in the info from the directory if this is a newly created
  253. * inode.
  254. */
  255. if (!result->i_ctime.tv_sec) {
  256. if (!(result->i_ctime.tv_sec = local_to_gmt(dir->i_sb, le32_to_cpu(de->creation_date))))
  257. result->i_ctime.tv_sec = 1;
  258. result->i_ctime.tv_nsec = 0;
  259. result->i_mtime.tv_sec = local_to_gmt(dir->i_sb, le32_to_cpu(de->write_date));
  260. result->i_mtime.tv_nsec = 0;
  261. result->i_atime.tv_sec = local_to_gmt(dir->i_sb, le32_to_cpu(de->read_date));
  262. result->i_atime.tv_nsec = 0;
  263. hpfs_result->i_ea_size = le32_to_cpu(de->ea_size);
  264. if (!hpfs_result->i_ea_mode && de->read_only)
  265. result->i_mode &= ~0222;
  266. if (!de->directory) {
  267. if (result->i_size == -1) {
  268. result->i_size = le32_to_cpu(de->file_size);
  269. result->i_data.a_ops = &hpfs_aops;
  270. hpfs_i(result)->mmu_private = result->i_size;
  271. /*
  272. * i_blocks should count the fnode and any anodes.
  273. * We count 1 for the fnode and don't bother about
  274. * anodes -- the disk heads are on the directory band
  275. * and we want them to stay there.
  276. */
  277. result->i_blocks = 1 + ((result->i_size + 511) >> 9);
  278. }
  279. }
  280. }
  281. bail1:
  282. hpfs_brelse4(&qbh);
  283. /*
  284. * Made it.
  285. */
  286. end:
  287. end_add:
  288. hpfs_unlock(dir->i_sb);
  289. return d_splice_alias(result, dentry);
  290. }
  291. const struct file_operations hpfs_dir_ops =
  292. {
  293. .llseek = hpfs_dir_lseek,
  294. .read = generic_read_dir,
  295. .iterate_shared = hpfs_readdir,
  296. .release = hpfs_dir_release,
  297. .fsync = hpfs_file_fsync,
  298. .unlocked_ioctl = hpfs_ioctl,
  299. .compat_ioctl = compat_ptr_ioctl,
  300. };