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