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