dir.c 8.3 KB

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