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