cylinder.c 5.7 KB

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