blob: 09c39e5e638631a784faed8106835dbe7e49ff47 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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/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 "swab.h"
21#include "util.h"
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023/*
24 * Read cylinder group into cache. The memory space for ufs_cg_private_info
25 * structure is already allocated during ufs_read_super.
26 */
27static void ufs_read_cylinder (struct super_block * sb,
28 unsigned cgno, unsigned bitmap_nr)
29{
30 struct ufs_sb_info * sbi = UFS_SB(sb);
31 struct ufs_sb_private_info * uspi;
32 struct ufs_cg_private_info * ucpi;
33 struct ufs_cylinder_group * ucg;
34 unsigned i, j;
35
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -070036 UFSD("ENTER, cgno %u, bitmap_nr %u\n", cgno, bitmap_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 uspi = sbi->s_uspi;
38 ucpi = sbi->s_ucpi[bitmap_nr];
39 ucg = (struct ufs_cylinder_group *)sbi->s_ucg[cgno]->b_data;
40
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -070041 UCPI_UBH(ucpi)->fragment = ufs_cgcmin(cgno);
42 UCPI_UBH(ucpi)->count = uspi->s_cgsize >> sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 /*
44 * We have already the first fragment of cylinder group block in buffer
45 */
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -070046 UCPI_UBH(ucpi)->bh[0] = sbi->s_ucg[cgno];
47 for (i = 1; i < UCPI_UBH(ucpi)->count; i++)
48 if (!(UCPI_UBH(ucpi)->bh[i] = sb_bread(sb, UCPI_UBH(ucpi)->fragment + i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 goto failed;
50 sbi->s_cgno[bitmap_nr] = cgno;
51
52 ucpi->c_cgx = fs32_to_cpu(sb, ucg->cg_cgx);
53 ucpi->c_ncyl = fs16_to_cpu(sb, ucg->cg_ncyl);
54 ucpi->c_niblk = fs16_to_cpu(sb, ucg->cg_niblk);
55 ucpi->c_ndblk = fs32_to_cpu(sb, ucg->cg_ndblk);
56 ucpi->c_rotor = fs32_to_cpu(sb, ucg->cg_rotor);
57 ucpi->c_frotor = fs32_to_cpu(sb, ucg->cg_frotor);
58 ucpi->c_irotor = fs32_to_cpu(sb, ucg->cg_irotor);
59 ucpi->c_btotoff = fs32_to_cpu(sb, ucg->cg_btotoff);
60 ucpi->c_boff = fs32_to_cpu(sb, ucg->cg_boff);
61 ucpi->c_iusedoff = fs32_to_cpu(sb, ucg->cg_iusedoff);
62 ucpi->c_freeoff = fs32_to_cpu(sb, ucg->cg_freeoff);
63 ucpi->c_nextfreeoff = fs32_to_cpu(sb, ucg->cg_nextfreeoff);
64 ucpi->c_clustersumoff = fs32_to_cpu(sb, ucg->cg_u.cg_44.cg_clustersumoff);
65 ucpi->c_clusteroff = fs32_to_cpu(sb, ucg->cg_u.cg_44.cg_clusteroff);
66 ucpi->c_nclusterblks = fs32_to_cpu(sb, ucg->cg_u.cg_44.cg_nclusterblks);
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -070067 UFSD("EXIT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 return;
69
70failed:
71 for (j = 1; j < i; j++)
72 brelse (sbi->s_ucg[j]);
73 sbi->s_cgno[bitmap_nr] = UFS_CGNO_EMPTY;
74 ufs_error (sb, "ufs_read_cylinder", "can't read cylinder group block %u", cgno);
75}
76
77/*
78 * Remove cylinder group from cache, doesn't release memory
79 * allocated for cylinder group (this is done at ufs_put_super only).
80 */
81void ufs_put_cylinder (struct super_block * sb, unsigned bitmap_nr)
82{
83 struct ufs_sb_info * sbi = UFS_SB(sb);
84 struct ufs_sb_private_info * uspi;
85 struct ufs_cg_private_info * ucpi;
86 struct ufs_cylinder_group * ucg;
87 unsigned i;
88
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -070089 UFSD("ENTER, bitmap_nr %u\n", bitmap_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91 uspi = sbi->s_uspi;
92 if (sbi->s_cgno[bitmap_nr] == UFS_CGNO_EMPTY) {
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -070093 UFSD("EXIT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 return;
95 }
96 ucpi = sbi->s_ucpi[bitmap_nr];
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -070097 ucg = ubh_get_ucg(UCPI_UBH(ucpi));
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99 if (uspi->s_ncg > UFS_MAX_GROUP_LOADED && bitmap_nr >= sbi->s_cg_loaded) {
100 ufs_panic (sb, "ufs_put_cylinder", "internal error");
101 return;
102 }
103 /*
104 * rotor is not so important data, so we put it to disk
105 * at the end of working with cylinder
106 */
107 ucg->cg_rotor = cpu_to_fs32(sb, ucpi->c_rotor);
108 ucg->cg_frotor = cpu_to_fs32(sb, ucpi->c_frotor);
109 ucg->cg_irotor = cpu_to_fs32(sb, ucpi->c_irotor);
Evgeniy Dushistov9695ef12006-06-25 05:47:22 -0700110 ubh_mark_buffer_dirty (UCPI_UBH(ucpi));
111 for (i = 1; i < UCPI_UBH(ucpi)->count; i++) {
112 brelse (UCPI_UBH(ucpi)->bh[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 }
114
115 sbi->s_cgno[bitmap_nr] = UFS_CGNO_EMPTY;
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700116 UFSD("EXIT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117}
118
119/*
120 * Find cylinder group in cache and return it as pointer.
121 * If cylinder group is not in cache, we will load it from disk.
122 *
123 * The cache is managed by LRU algorithm.
124 */
125struct ufs_cg_private_info * ufs_load_cylinder (
126 struct super_block * sb, unsigned cgno)
127{
128 struct ufs_sb_info * sbi = UFS_SB(sb);
129 struct ufs_sb_private_info * uspi;
130 struct ufs_cg_private_info * ucpi;
131 unsigned cg, i, j;
132
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700133 UFSD("ENTER, cgno %u\n", cgno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135 uspi = sbi->s_uspi;
136 if (cgno >= uspi->s_ncg) {
137 ufs_panic (sb, "ufs_load_cylinder", "internal error, high number of cg");
138 return NULL;
139 }
140 /*
141 * Cylinder group number cg it in cache and it was last used
142 */
143 if (sbi->s_cgno[0] == cgno) {
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700144 UFSD("EXIT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 return sbi->s_ucpi[0];
146 }
147 /*
148 * Number of cylinder groups is not higher than UFS_MAX_GROUP_LOADED
149 */
150 if (uspi->s_ncg <= UFS_MAX_GROUP_LOADED) {
151 if (sbi->s_cgno[cgno] != UFS_CGNO_EMPTY) {
152 if (sbi->s_cgno[cgno] != cgno) {
153 ufs_panic (sb, "ufs_load_cylinder", "internal error, wrong number of cg in cache");
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700154 UFSD("EXIT (FAILED)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 return NULL;
156 }
157 else {
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700158 UFSD("EXIT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 return sbi->s_ucpi[cgno];
160 }
161 } else {
162 ufs_read_cylinder (sb, cgno, cgno);
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700163 UFSD("EXIT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 return sbi->s_ucpi[cgno];
165 }
166 }
167 /*
168 * Cylinder group number cg is in cache but it was not last used,
169 * we will move to the first position
170 */
171 for (i = 0; i < sbi->s_cg_loaded && sbi->s_cgno[i] != cgno; i++);
172 if (i < sbi->s_cg_loaded && sbi->s_cgno[i] == cgno) {
173 cg = sbi->s_cgno[i];
174 ucpi = sbi->s_ucpi[i];
175 for (j = i; j > 0; j--) {
176 sbi->s_cgno[j] = sbi->s_cgno[j-1];
177 sbi->s_ucpi[j] = sbi->s_ucpi[j-1];
178 }
179 sbi->s_cgno[0] = cg;
180 sbi->s_ucpi[0] = ucpi;
181 /*
182 * Cylinder group number cg is not in cache, we will read it from disk
183 * and put it to the first position
184 */
185 } else {
186 if (sbi->s_cg_loaded < UFS_MAX_GROUP_LOADED)
187 sbi->s_cg_loaded++;
188 else
189 ufs_put_cylinder (sb, UFS_MAX_GROUP_LOADED-1);
190 ucpi = sbi->s_ucpi[sbi->s_cg_loaded - 1];
191 for (j = sbi->s_cg_loaded - 1; j > 0; j--) {
192 sbi->s_cgno[j] = sbi->s_cgno[j-1];
193 sbi->s_ucpi[j] = sbi->s_ucpi[j-1];
194 }
195 sbi->s_ucpi[0] = ucpi;
196 ufs_read_cylinder (sb, cgno, 0);
197 }
Evgeniy Dushistovabf5d152006-06-25 05:47:24 -0700198 UFSD("EXIT\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 return sbi->s_ucpi[0];
200}