cylinder.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * linux/fs/ufs/cylinder.c
  3. *
  4. * Copyright (C) 1998
  5. * Daniel Pirkl <daniel.pirkl@email.cz>
  6. * Charles University, Faculty of Mathematics and Physics
  7. *
  8. * ext2 - inode (block) bitmap caching inspired
  9. */
  10. #include <linux/fs.h>
  11. #include <linux/time.h>
  12. #include <linux/stat.h>
  13. #include <linux/string.h>
  14. #include <linux/bitops.h>
  15. #include <asm/byteorder.h>
  16. #include "ufs_fs.h"
  17. #include "ufs.h"
  18. #include "swab.h"
  19. #include "util.h"
  20. /*
  21. * Read cylinder group into cache. The memory space for ufs_cg_private_info
  22. * structure is already allocated during ufs_read_super.
  23. */
  24. static void ufs_read_cylinder (struct super_block * sb,
  25. unsigned cgno, unsigned bitmap_nr)
  26. {
  27. struct ufs_sb_info * sbi = UFS_SB(sb);
  28. struct ufs_sb_private_info * uspi;
  29. struct ufs_cg_private_info * ucpi;
  30. struct ufs_cylinder_group * ucg;
  31. unsigned i, j;
  32. UFSD("ENTER, cgno %u, bitmap_nr %u\n", cgno, bitmap_nr);
  33. uspi = sbi->s_uspi;
  34. ucpi = sbi->s_ucpi[bitmap_nr];
  35. ucg = (struct ufs_cylinder_group *)sbi->s_ucg[cgno]->b_data;
  36. UCPI_UBH(ucpi)->fragment = ufs_cgcmin(cgno);
  37. UCPI_UBH(ucpi)->count = uspi->s_cgsize >> sb->s_blocksize_bits;
  38. /*
  39. * We have already the first fragment of cylinder group block in buffer
  40. */
  41. UCPI_UBH(ucpi)->bh[0] = sbi->s_ucg[cgno];
  42. for (i = 1; i < UCPI_UBH(ucpi)->count; i++)
  43. if (!(UCPI_UBH(ucpi)->bh[i] = sb_bread(sb, UCPI_UBH(ucpi)->fragment + i)))
  44. goto failed;
  45. sbi->s_cgno[bitmap_nr] = cgno;
  46. ucpi->c_cgx = fs32_to_cpu(sb, ucg->cg_cgx);
  47. ucpi->c_ncyl = fs16_to_cpu(sb, ucg->cg_ncyl);
  48. ucpi->c_niblk = fs16_to_cpu(sb, ucg->cg_niblk);
  49. ucpi->c_ndblk = fs32_to_cpu(sb, ucg->cg_ndblk);
  50. ucpi->c_rotor = fs32_to_cpu(sb, ucg->cg_rotor);
  51. ucpi->c_frotor = fs32_to_cpu(sb, ucg->cg_frotor);
  52. ucpi->c_irotor = fs32_to_cpu(sb, ucg->cg_irotor);
  53. ucpi->c_btotoff = fs32_to_cpu(sb, ucg->cg_btotoff);
  54. ucpi->c_boff = fs32_to_cpu(sb, ucg->cg_boff);
  55. ucpi->c_iusedoff = fs32_to_cpu(sb, ucg->cg_iusedoff);
  56. ucpi->c_freeoff = fs32_to_cpu(sb, ucg->cg_freeoff);
  57. ucpi->c_nextfreeoff = fs32_to_cpu(sb, ucg->cg_nextfreeoff);
  58. ucpi->c_clustersumoff = fs32_to_cpu(sb, ucg->cg_u.cg_44.cg_clustersumoff);
  59. ucpi->c_clusteroff = fs32_to_cpu(sb, ucg->cg_u.cg_44.cg_clusteroff);
  60. ucpi->c_nclusterblks = fs32_to_cpu(sb, ucg->cg_u.cg_44.cg_nclusterblks);
  61. UFSD("EXIT\n");
  62. return;
  63. failed:
  64. for (j = 1; j < i; j++)
  65. brelse (sbi->s_ucg[j]);
  66. sbi->s_cgno[bitmap_nr] = UFS_CGNO_EMPTY;
  67. ufs_error (sb, "ufs_read_cylinder", "can't read cylinder group block %u", cgno);
  68. }
  69. /*
  70. * Remove cylinder group from cache, doesn't release memory
  71. * allocated for cylinder group (this is done at ufs_put_super only).
  72. */
  73. void ufs_put_cylinder (struct super_block * sb, unsigned bitmap_nr)
  74. {
  75. struct ufs_sb_info * sbi = UFS_SB(sb);
  76. struct ufs_sb_private_info * uspi;
  77. struct ufs_cg_private_info * ucpi;
  78. struct ufs_cylinder_group * ucg;
  79. unsigned i;
  80. UFSD("ENTER, bitmap_nr %u\n", bitmap_nr);
  81. uspi = sbi->s_uspi;
  82. if (sbi->s_cgno[bitmap_nr] == UFS_CGNO_EMPTY) {
  83. UFSD("EXIT\n");
  84. return;
  85. }
  86. ucpi = sbi->s_ucpi[bitmap_nr];
  87. ucg = ubh_get_ucg(UCPI_UBH(ucpi));
  88. if (uspi->s_ncg > UFS_MAX_GROUP_LOADED && bitmap_nr >= sbi->s_cg_loaded) {
  89. ufs_panic (sb, "ufs_put_cylinder", "internal error");
  90. return;
  91. }
  92. /*
  93. * rotor is not so important data, so we put it to disk
  94. * at the end of working with cylinder
  95. */
  96. ucg->cg_rotor = cpu_to_fs32(sb, ucpi->c_rotor);
  97. ucg->cg_frotor = cpu_to_fs32(sb, ucpi->c_frotor);
  98. ucg->cg_irotor = cpu_to_fs32(sb, ucpi->c_irotor);
  99. ubh_mark_buffer_dirty (UCPI_UBH(ucpi));
  100. for (i = 1; i < UCPI_UBH(ucpi)->count; i++) {
  101. brelse (UCPI_UBH(ucpi)->bh[i]);
  102. }
  103. sbi->s_cgno[bitmap_nr] = UFS_CGNO_EMPTY;
  104. UFSD("EXIT\n");
  105. }
  106. /*
  107. * Find cylinder group in cache and return it as pointer.
  108. * If cylinder group is not in cache, we will load it from disk.
  109. *
  110. * The cache is managed by LRU algorithm.
  111. */
  112. struct ufs_cg_private_info * ufs_load_cylinder (
  113. struct super_block * sb, unsigned cgno)
  114. {
  115. struct ufs_sb_info * sbi = UFS_SB(sb);
  116. struct ufs_sb_private_info * uspi;
  117. struct ufs_cg_private_info * ucpi;
  118. unsigned cg, i, j;
  119. UFSD("ENTER, cgno %u\n", cgno);
  120. uspi = sbi->s_uspi;
  121. if (cgno >= uspi->s_ncg) {
  122. ufs_panic (sb, "ufs_load_cylinder", "internal error, high number of cg");
  123. return NULL;
  124. }
  125. /*
  126. * Cylinder group number cg it in cache and it was last used
  127. */
  128. if (sbi->s_cgno[0] == cgno) {
  129. UFSD("EXIT\n");
  130. return sbi->s_ucpi[0];
  131. }
  132. /*
  133. * Number of cylinder groups is not higher than UFS_MAX_GROUP_LOADED
  134. */
  135. if (uspi->s_ncg <= UFS_MAX_GROUP_LOADED) {
  136. if (sbi->s_cgno[cgno] != UFS_CGNO_EMPTY) {
  137. if (sbi->s_cgno[cgno] != cgno) {
  138. ufs_panic (sb, "ufs_load_cylinder", "internal error, wrong number of cg in cache");
  139. UFSD("EXIT (FAILED)\n");
  140. return NULL;
  141. }
  142. else {
  143. UFSD("EXIT\n");
  144. return sbi->s_ucpi[cgno];
  145. }
  146. } else {
  147. ufs_read_cylinder (sb, cgno, cgno);
  148. UFSD("EXIT\n");
  149. return sbi->s_ucpi[cgno];
  150. }
  151. }
  152. /*
  153. * Cylinder group number cg is in cache but it was not last used,
  154. * we will move to the first position
  155. */
  156. for (i = 0; i < sbi->s_cg_loaded && sbi->s_cgno[i] != cgno; i++);
  157. if (i < sbi->s_cg_loaded && sbi->s_cgno[i] == cgno) {
  158. cg = sbi->s_cgno[i];
  159. ucpi = sbi->s_ucpi[i];
  160. for (j = i; j > 0; j--) {
  161. sbi->s_cgno[j] = sbi->s_cgno[j-1];
  162. sbi->s_ucpi[j] = sbi->s_ucpi[j-1];
  163. }
  164. sbi->s_cgno[0] = cg;
  165. sbi->s_ucpi[0] = ucpi;
  166. /*
  167. * Cylinder group number cg is not in cache, we will read it from disk
  168. * and put it to the first position
  169. */
  170. } else {
  171. if (sbi->s_cg_loaded < UFS_MAX_GROUP_LOADED)
  172. sbi->s_cg_loaded++;
  173. else
  174. ufs_put_cylinder (sb, UFS_MAX_GROUP_LOADED-1);
  175. ucpi = sbi->s_ucpi[sbi->s_cg_loaded - 1];
  176. for (j = sbi->s_cg_loaded - 1; j > 0; j--) {
  177. sbi->s_cgno[j] = sbi->s_cgno[j-1];
  178. sbi->s_ucpi[j] = sbi->s_ucpi[j-1];
  179. }
  180. sbi->s_ucpi[0] = ucpi;
  181. ufs_read_cylinder (sb, cgno, 0);
  182. }
  183. UFSD("EXIT\n");
  184. return sbi->s_ucpi[0];
  185. }