dir.c 8.6 KB

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