dir.c 8.6 KB

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