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