1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * linux/fs/ufs/cylinder.c
4 *
5 * Copyright (C) 1998
6 * Daniel Pirkl <daniel.pirkl@email.cz>
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 */
ufs_read_cylinder(struct super_block * sb,unsigned cgno,unsigned bitmap_nr)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
72 /* these on-disk values become array and bitmap indices */
73 if (ucpi->c_cgx != cgno ||
74 ucpi->c_rotor >= uspi->s_fpg ||
75 ucpi->c_frotor >= uspi->s_fpg ||
76 ucpi->c_irotor >= uspi->s_ipg) {
77 ufs_error(sb, __func__,
78 "inconsistent metadata in cylinder group %u\n", cgno);
79 goto failed;
80 }
81 UFSD("EXIT\n");
82 return true;
83
84 failed:
85 for (j = 1; j < i; j++)
86 brelse(UCPI_UBH(ucpi)->bh[j]);
87 sbi->s_cgno[bitmap_nr] = UFS_CGNO_EMPTY;
88 ufs_error (sb, "ufs_read_cylinder", "can't read cylinder group block %u", cgno);
89 return false;
90 }
91
92 /*
93 * Remove cylinder group from cache, doesn't release memory
94 * allocated for cylinder group (this is done at ufs_put_super only).
95 */
ufs_put_cylinder(struct super_block * sb,unsigned bitmap_nr)96 void ufs_put_cylinder (struct super_block * sb, unsigned bitmap_nr)
97 {
98 struct ufs_sb_info * sbi = UFS_SB(sb);
99 struct ufs_sb_private_info * uspi;
100 struct ufs_cg_private_info * ucpi;
101 struct ufs_cylinder_group * ucg;
102 unsigned i;
103
104 UFSD("ENTER, bitmap_nr %u\n", bitmap_nr);
105
106 uspi = sbi->s_uspi;
107 if (sbi->s_cgno[bitmap_nr] == UFS_CGNO_EMPTY) {
108 UFSD("EXIT\n");
109 return;
110 }
111 ucpi = sbi->s_ucpi[bitmap_nr];
112 ucg = ubh_get_ucg(UCPI_UBH(ucpi));
113
114 if (uspi->s_ncg > UFS_MAX_GROUP_LOADED && bitmap_nr >= sbi->s_cg_loaded) {
115 ufs_panic (sb, "ufs_put_cylinder", "internal error");
116 return;
117 }
118 /*
119 * rotor is not so important data, so we put it to disk
120 * at the end of working with cylinder
121 */
122 ucg->cg_rotor = cpu_to_fs32(sb, ucpi->c_rotor);
123 ucg->cg_frotor = cpu_to_fs32(sb, ucpi->c_frotor);
124 ucg->cg_irotor = cpu_to_fs32(sb, ucpi->c_irotor);
125 ubh_mark_buffer_dirty (UCPI_UBH(ucpi));
126 for (i = 1; i < UCPI_UBH(ucpi)->count; i++) {
127 brelse (UCPI_UBH(ucpi)->bh[i]);
128 }
129
130 sbi->s_cgno[bitmap_nr] = UFS_CGNO_EMPTY;
131 UFSD("EXIT\n");
132 }
133
134 /*
135 * Find cylinder group in cache and return it as pointer.
136 * If cylinder group is not in cache, we will load it from disk.
137 *
138 * The cache is managed by LRU algorithm.
139 */
ufs_load_cylinder(struct super_block * sb,unsigned cgno)140 struct ufs_cg_private_info * ufs_load_cylinder (
141 struct super_block * sb, unsigned cgno)
142 {
143 struct ufs_sb_info * sbi = UFS_SB(sb);
144 struct ufs_sb_private_info * uspi;
145 struct ufs_cg_private_info * ucpi;
146 unsigned cg, i, j;
147
148 UFSD("ENTER, cgno %u\n", cgno);
149
150 uspi = sbi->s_uspi;
151 if (cgno >= uspi->s_ncg) {
152 ufs_panic (sb, "ufs_load_cylinder", "internal error, high number of cg");
153 return NULL;
154 }
155 /*
156 * Cylinder group number cg it in cache and it was last used
157 */
158 if (sbi->s_cgno[0] == cgno) {
159 UFSD("EXIT\n");
160 return sbi->s_ucpi[0];
161 }
162 /*
163 * Number of cylinder groups is not higher than UFS_MAX_GROUP_LOADED
164 */
165 if (uspi->s_ncg <= UFS_MAX_GROUP_LOADED) {
166 if (sbi->s_cgno[cgno] != UFS_CGNO_EMPTY) {
167 if (sbi->s_cgno[cgno] != cgno) {
168 ufs_panic (sb, "ufs_load_cylinder", "internal error, wrong number of cg in cache");
169 UFSD("EXIT (FAILED)\n");
170 return NULL;
171 }
172 } else {
173 if (unlikely(!ufs_read_cylinder (sb, cgno, cgno))) {
174 UFSD("EXIT (FAILED)\n");
175 return NULL;
176 }
177 }
178 UFSD("EXIT\n");
179 return sbi->s_ucpi[cgno];
180 }
181 /*
182 * Cylinder group number cg is in cache but it was not last used,
183 * we will move to the first position
184 */
185 for (i = 0; i < sbi->s_cg_loaded && sbi->s_cgno[i] != cgno; i++);
186 if (i < sbi->s_cg_loaded && sbi->s_cgno[i] == cgno) {
187 cg = sbi->s_cgno[i];
188 ucpi = sbi->s_ucpi[i];
189 for (j = i; j > 0; j--) {
190 sbi->s_cgno[j] = sbi->s_cgno[j-1];
191 sbi->s_ucpi[j] = sbi->s_ucpi[j-1];
192 }
193 sbi->s_cgno[0] = cg;
194 sbi->s_ucpi[0] = ucpi;
195 /*
196 * Cylinder group number cg is not in cache, we will read it from disk
197 * and put it to the first position
198 */
199 } else {
200 if (sbi->s_cg_loaded < UFS_MAX_GROUP_LOADED)
201 sbi->s_cg_loaded++;
202 else
203 ufs_put_cylinder (sb, UFS_MAX_GROUP_LOADED-1);
204 ucpi = sbi->s_ucpi[sbi->s_cg_loaded - 1];
205 for (j = sbi->s_cg_loaded - 1; j > 0; j--) {
206 sbi->s_cgno[j] = sbi->s_cgno[j-1];
207 sbi->s_ucpi[j] = sbi->s_ucpi[j-1];
208 }
209 sbi->s_ucpi[0] = ucpi;
210 if (unlikely(!ufs_read_cylinder (sb, cgno, 0))) {
211 UFSD("EXIT (FAILED)\n");
212 return NULL;
213 }
214 }
215 UFSD("EXIT\n");
216 return sbi->s_ucpi[0];
217 }
218