dir.c 8.6 KB

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