PageRenderTime 64ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 0ms

/fs/hpfs/dir.c

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