dir.c 8.3 KB

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