cylinder.c 5.7 KB

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