dir.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /*
  2. * linux/fs/hpfs/dir.c
  3. *
  4. * Mikulas Patocka (mikulas@artax.karlin.mff.cuni.cz), 1998-1999
  5. *
  6. * directory VFS functions
  7. */
  8. #include <linux/slab.h>
  9. #include "hpfs_fn.h"
  10. static int hpfs_dir_release(struct inode *inode, struct file *filp)
  11. {
  12. hpfs_lock(inode->i_sb);
  13. hpfs_del_pos(inode, &filp->f_pos);
  14. /*hpfs_write_if_changed(inode);*/
  15. hpfs_unlock(inode->i_sb);
  16. return 0;
  17. }
  18. /* This is slow, but it's not used often */
  19. static loff_t hpfs_dir_lseek(struct file *filp, loff_t off, int whence)
  20. {
  21. loff_t new_off = off + (whence == 1 ? filp->f_pos : 0);
  22. loff_t pos;
  23. struct quad_buffer_head qbh;
  24. struct inode *i = file_inode(filp);
  25. struct hpfs_inode_info *hpfs_inode = hpfs_i(i);
  26. struct super_block *s = i->i_sb;
  27. /* Somebody else will have to figure out what to do here */
  28. if (whence == SEEK_DATA || whence == SEEK_HOLE)
  29. return -EINVAL;
  30. inode_lock(i);
  31. hpfs_lock(s);
  32. /*pr_info("dir lseek\n");*/
  33. if (new_off == 0 || new_off == 1 || new_off == 11 || new_off == 12 || new_off == 13) goto ok;
  34. pos = ((loff_t) hpfs_de_as_down_as_possible(s, hpfs_inode->i_dno) << 4) + 1;
  35. while (pos != new_off) {
  36. if (map_pos_dirent(i, &pos, &qbh)) hpfs_brelse4(&qbh);
  37. else goto fail;
  38. if (pos == 12) goto fail;
  39. }
  40. if (unlikely(hpfs_add_pos(i, &filp->f_pos) < 0)) {
  41. hpfs_unlock(s);
  42. inode_unlock(i);
  43. return -ENOMEM;
  44. }
  45. ok:
  46. filp->f_pos = new_off;
  47. hpfs_unlock(s);
  48. inode_unlock(i);
  49. return new_off;
  50. fail:
  51. /*pr_warn("illegal lseek: %016llx\n", new_off);*/
  52. hpfs_unlock(s);
  53. inode_unlock(i);
  54. return -ESPIPE;
  55. }
  56. static int hpfs_readdir(struct file *file, struct dir_context *ctx)
  57. {
  58. struct inode *inode = file_inode(file);
  59. struct hpfs_inode_info *hpfs_inode = hpfs_i(inode);
  60. struct quad_buffer_head qbh;
  61. struct hpfs_dirent *de;
  62. int lc;
  63. loff_t next_pos;
  64. unsigned char *tempname;
  65. int c1, c2 = 0;
  66. int ret = 0;
  67. hpfs_lock(inode->i_sb);
  68. if (hpfs_sb(inode->i_sb)->sb_chk) {
  69. if (hpfs_chk_sectors(inode->i_sb, inode->i_ino, 1, "dir_fnode")) {
  70. ret = -EFSERROR;
  71. goto out;
  72. }
  73. if (hpfs_chk_sectors(inode->i_sb, hpfs_inode->i_dno, 4, "dir_dnode")) {
  74. ret = -EFSERROR;
  75. goto out;
  76. }
  77. }
  78. if (hpfs_sb(inode->i_sb)->sb_chk >= 2) {
  79. struct buffer_head *bh;
  80. struct fnode *fno;
  81. int e = 0;
  82. if (!(fno = hpfs_map_fnode(inode->i_sb, inode->i_ino, &bh))) {
  83. ret = -EIOERROR;
  84. goto out;
  85. }
  86. if (!fnode_is_dir(fno)) {
  87. e = 1;
  88. hpfs_error(inode->i_sb, "not a directory, fnode %08lx",
  89. (unsigned long)inode->i_ino);
  90. }
  91. if (hpfs_inode->i_dno != le32_to_cpu(fno->u.external[0].disk_secno)) {
  92. e = 1;
  93. 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));
  94. }
  95. brelse(bh);
  96. if (e) {
  97. ret = -EFSERROR;
  98. goto out;
  99. }
  100. }
  101. lc = hpfs_sb(inode->i_sb)->sb_lowercase;
  102. if (ctx->pos == 12) { /* diff -r requires this (note, that diff -r */
  103. ctx->pos = 13; /* also fails on msdos filesystem in 2.0) */
  104. goto out;
  105. }
  106. if (ctx->pos == 13) {
  107. ret = -ENOENT;
  108. goto out;
  109. }
  110. while (1) {
  111. again:
  112. /* This won't work when cycle is longer than number of dirents
  113. accepted by filldir, but what can I do?
  114. maybe killall -9 ls helps */
  115. if (hpfs_sb(inode->i_sb)->sb_chk)
  116. if (hpfs_stop_cycles(inode->i_sb, ctx->pos, &c1, &c2, "hpfs_readdir")) {
  117. ret = -EFSERROR;
  118. goto out;
  119. }
  120. if (ctx->pos == 12)
  121. goto out;
  122. if (ctx->pos == 3 || ctx->pos == 4 || ctx->pos == 5) {
  123. pr_err("pos==%d\n", (int)ctx->pos);
  124. goto out;
  125. }
  126. if (ctx->pos == 0) {
  127. if (!dir_emit_dot(file, ctx))
  128. goto out;
  129. ctx->pos = 11;
  130. }
  131. if (ctx->pos == 11) {
  132. if (!dir_emit(ctx, "..", 2, hpfs_inode->i_parent_dir, DT_DIR))
  133. goto out;
  134. ctx->pos = 1;
  135. }
  136. if (ctx->pos == 1) {
  137. ret = hpfs_add_pos(inode, &file->f_pos);
  138. if (unlikely(ret < 0))
  139. goto out;
  140. ctx->pos = ((loff_t) hpfs_de_as_down_as_possible(inode->i_sb, hpfs_inode->i_dno) << 4) + 1;
  141. file->f_version = inode->i_version;
  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. goto bail1;
  226. }
  227. if (result->i_state & I_NEW) {
  228. hpfs_init_inode(result);
  229. if (de->directory)
  230. hpfs_read_inode(result);
  231. else if (le32_to_cpu(de->ea_size) && hpfs_sb(dir->i_sb)->sb_eas)
  232. hpfs_read_inode(result);
  233. else {
  234. result->i_mode |= S_IFREG;
  235. result->i_mode &= ~0111;
  236. result->i_op = &hpfs_file_iops;
  237. result->i_fop = &hpfs_file_ops;
  238. set_nlink(result, 1);
  239. }
  240. unlock_new_inode(result);
  241. }
  242. hpfs_result = hpfs_i(result);
  243. if (!de->directory) hpfs_result->i_parent_dir = dir->i_ino;
  244. if (de->has_acl || de->has_xtd_perm) if (!(dir->i_sb->s_flags & MS_RDONLY)) {
  245. 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");
  246. goto bail1;
  247. }
  248. /*
  249. * Fill in the info from the directory if this is a newly created
  250. * inode.
  251. */
  252. if (!result->i_ctime.tv_sec) {
  253. if (!(result->i_ctime.tv_sec = local_to_gmt(dir->i_sb, le32_to_cpu(de->creation_date))))
  254. result->i_ctime.tv_sec = 1;
  255. result->i_ctime.tv_nsec = 0;
  256. result->i_mtime.tv_sec = local_to_gmt(dir->i_sb, le32_to_cpu(de->write_date));
  257. result->i_mtime.tv_nsec = 0;
  258. result->i_atime.tv_sec = local_to_gmt(dir->i_sb, le32_to_cpu(de->read_date));
  259. result->i_atime.tv_nsec = 0;
  260. hpfs_result->i_ea_size = le32_to_cpu(de->ea_size);
  261. if (!hpfs_result->i_ea_mode && de->read_only)
  262. result->i_mode &= ~0222;
  263. if (!de->directory) {
  264. if (result->i_size == -1) {
  265. result->i_size = le32_to_cpu(de->file_size);
  266. result->i_data.a_ops = &hpfs_aops;
  267. hpfs_i(result)->mmu_private = result->i_size;
  268. /*
  269. * i_blocks should count the fnode and any anodes.
  270. * We count 1 for the fnode and don't bother about
  271. * anodes -- the disk heads are on the directory band
  272. * and we want them to stay there.
  273. */
  274. result->i_blocks = 1 + ((result->i_size + 511) >> 9);
  275. }
  276. }
  277. }
  278. hpfs_brelse4(&qbh);
  279. /*
  280. * Made it.
  281. */
  282. end:
  283. end_add:
  284. hpfs_unlock(dir->i_sb);
  285. d_add(dentry, result);
  286. return NULL;
  287. /*
  288. * Didn't.
  289. */
  290. bail1:
  291. hpfs_brelse4(&qbh);
  292. /*bail:*/
  293. hpfs_unlock(dir->i_sb);
  294. return ERR_PTR(-ENOENT);
  295. }
  296. const struct file_operations hpfs_dir_ops =
  297. {
  298. .llseek = hpfs_dir_lseek,
  299. .read = generic_read_dir,
  300. .iterate_shared = hpfs_readdir,
  301. .release = hpfs_dir_release,
  302. .fsync = hpfs_file_fsync,
  303. .unlocked_ioctl = hpfs_ioctl,
  304. };