* added 0.99 linux version
[mascara-docs.git] / i386 / linux / linux-2.3.21 / fs / hpfs / buffer.c
blob8bcbf28fcabdefebf4804cd8d032fb0a2443200a
1 /*
2 * linux/fs/hpfs/buffer.c
4 * Mikulas Patocka (mikulas@artax.karlin.mff.cuni.cz), 1998-1999
6 * general buffer i/o
7 */
9 #include <linux/string.h>
10 #include "hpfs_fn.h"
12 void hpfs_lock_creation(struct super_block *s)
14 #ifdef DEBUG_LOCKS
15 printk("lock creation\n");
16 #endif
17 while (s->s_hpfs_creation_de_lock) sleep_on(&s->s_hpfs_creation_de);
18 s->s_hpfs_creation_de_lock = 1;
21 void hpfs_unlock_creation(struct super_block *s)
23 #ifdef DEBUG_LOCKS
24 printk("unlock creation\n");
25 #endif
26 s->s_hpfs_creation_de_lock = 0;
27 wake_up(&s->s_hpfs_creation_de);
30 void hpfs_lock_iget(struct super_block *s, int mode)
32 #ifdef DEBUG_LOCKS
33 printk("lock iget\n");
34 #endif
35 while (s->s_hpfs_rd_inode) sleep_on(&s->s_hpfs_iget_q);
36 s->s_hpfs_rd_inode = mode;
39 void hpfs_unlock_iget(struct super_block *s)
41 #ifdef DEBUG_LOCKS
42 printk("unlock iget\n");
43 #endif
44 s->s_hpfs_rd_inode = 0;
45 wake_up(&s->s_hpfs_iget_q);
48 void hpfs_lock_inode(struct inode *i)
50 if (i) down(&i->i_hpfs_sem);
53 void hpfs_unlock_inode(struct inode *i)
55 if (i) up(&i->i_hpfs_sem);
58 void hpfs_lock_2inodes(struct inode *i1, struct inode *i2)
60 if (!i1) { if (i2) down(&i2->i_hpfs_sem); return; }
61 if (!i2) { if (i1) down(&i1->i_hpfs_sem); return; }
62 if (i1->i_ino < i2->i_ino) {
63 down(&i1->i_hpfs_sem);
64 down(&i2->i_hpfs_sem);
65 } else if (i1->i_ino > i2->i_ino) {
66 down(&i2->i_hpfs_sem);
67 down(&i1->i_hpfs_sem);
68 } else down(&i1->i_hpfs_sem);
71 void hpfs_unlock_2inodes(struct inode *i1, struct inode *i2)
73 if (!i1) { if (i2) up(&i2->i_hpfs_sem); return; }
74 if (!i2) { if (i1) up(&i1->i_hpfs_sem); return; }
75 if (i1->i_ino < i2->i_ino) {
76 up(&i2->i_hpfs_sem);
77 up(&i1->i_hpfs_sem);
78 } else if (i1->i_ino > i2->i_ino) {
79 up(&i1->i_hpfs_sem);
80 up(&i2->i_hpfs_sem);
81 } else up(&i1->i_hpfs_sem);
84 void hpfs_lock_3inodes(struct inode *i1, struct inode *i2, struct inode *i3)
86 if (!i1) { hpfs_lock_2inodes(i2, i3); return; }
87 if (!i2) { hpfs_lock_2inodes(i1, i3); return; }
88 if (!i3) { hpfs_lock_2inodes(i1, i2); return; }
89 if (i1->i_ino < i2->i_ino && i1->i_ino < i3->i_ino) {
90 down(&i1->i_hpfs_sem);
91 hpfs_lock_2inodes(i2, i3);
92 } else if (i2->i_ino < i1->i_ino && i2->i_ino < i3->i_ino) {
93 down(&i2->i_hpfs_sem);
94 hpfs_lock_2inodes(i1, i3);
95 } else if (i3->i_ino < i1->i_ino && i3->i_ino < i2->i_ino) {
96 down(&i3->i_hpfs_sem);
97 hpfs_lock_2inodes(i1, i2);
98 } else if (i1->i_ino != i2->i_ino) hpfs_lock_2inodes(i1, i2);
99 else hpfs_lock_2inodes(i1, i3);
102 void hpfs_unlock_3inodes(struct inode *i1, struct inode *i2, struct inode *i3)
104 if (!i1) { hpfs_unlock_2inodes(i2, i3); return; }
105 if (!i2) { hpfs_unlock_2inodes(i1, i3); return; }
106 if (!i3) { hpfs_unlock_2inodes(i1, i2); return; }
107 if (i1->i_ino < i2->i_ino && i1->i_ino < i3->i_ino) {
108 hpfs_unlock_2inodes(i2, i3);
109 up(&i1->i_hpfs_sem);
110 } else if (i2->i_ino < i1->i_ino && i2->i_ino < i3->i_ino) {
111 hpfs_unlock_2inodes(i1, i3);
112 up(&i2->i_hpfs_sem);
113 } else if (i3->i_ino < i1->i_ino && i3->i_ino < i2->i_ino) {
114 hpfs_unlock_2inodes(i1, i2);
115 up(&i3->i_hpfs_sem);
116 } else if (i1->i_ino != i2->i_ino) hpfs_unlock_2inodes(i1, i2);
117 else hpfs_unlock_2inodes(i1, i3);
120 /* Map a sector into a buffer and return pointers to it and to the buffer. */
122 void *hpfs_map_sector(struct super_block *s, unsigned secno, struct buffer_head **bhp,
123 int ahead)
125 kdev_t dev = s->s_dev;
126 struct buffer_head *bh;
128 if (!ahead || secno + ahead >= s->s_hpfs_fs_size)
129 *bhp = bh = bread(dev, secno, 512);
130 else *bhp = bh = breada(dev, secno, 512, 0, (ahead + 1) << 9);
131 if (bh != NULL)
132 return bh->b_data;
133 else {
134 printk("HPFS: hpfs_map_sector: read error\n");
135 return NULL;
139 /* Like hpfs_map_sector but don't read anything */
141 void *hpfs_get_sector(struct super_block *s, unsigned secno, struct buffer_head **bhp)
143 struct buffer_head *bh;
144 /*return hpfs_map_sector(s, secno, bhp, 0);*/
146 if ((*bhp = bh = getblk(s->s_dev, secno, 512)) != NULL) {
147 mark_buffer_uptodate(bh, 1);
148 return bh->b_data;
149 } else {
150 printk("HPFS: hpfs_get_sector: getblk failed\n");
151 return NULL;
155 /* Map 4 sectors into a 4buffer and return pointers to it and to the buffer. */
157 void *hpfs_map_4sectors(struct super_block *s, unsigned secno, struct quad_buffer_head *qbh,
158 int ahead)
160 kdev_t dev = s->s_dev;
161 struct buffer_head *bh;
162 char *data;
164 if (secno & 3) {
165 printk("HPFS: hpfs_map_4sectors: unaligned read\n");
166 return 0;
169 qbh->data = data = (char *)kmalloc(2048, GFP_KERNEL);
170 if (!data) {
171 printk("HPFS: hpfs_map_4sectors: out of memory\n");
172 goto bail;
175 if (!ahead || secno + 4 + ahead > s->s_hpfs_fs_size)
176 qbh->bh[0] = bh = breada(dev, secno, 512, 0, 2048);
177 else qbh->bh[0] = bh = breada(dev, secno, 512, 0, (ahead + 4) << 9);
178 if (!bh)
179 goto bail0;
180 memcpy(data, bh->b_data, 512);
182 qbh->bh[1] = bh = bread(dev, secno + 1, 512);
183 if (!bh)
184 goto bail1;
185 memcpy(data + 512, bh->b_data, 512);
187 qbh->bh[2] = bh = bread(dev, secno + 2, 512);
188 if (!bh)
189 goto bail2;
190 memcpy(data + 2 * 512, bh->b_data, 512);
192 qbh->bh[3] = bh = bread(dev, secno + 3, 512);
193 if (!bh)
194 goto bail3;
195 memcpy(data + 3 * 512, bh->b_data, 512);
197 return data;
199 bail3:
200 brelse(qbh->bh[2]);
201 bail2:
202 brelse(qbh->bh[1]);
203 bail1:
204 brelse(qbh->bh[0]);
205 bail0:
206 kfree_s(data, 2048);
207 printk("HPFS: hpfs_map_4sectors: read error\n");
208 bail:
209 return NULL;
212 /* Don't read sectors */
214 void *hpfs_get_4sectors(struct super_block *s, unsigned secno,
215 struct quad_buffer_head *qbh)
217 if (secno & 3) {
218 printk("HPFS: hpfs_get_4sectors: unaligned read\n");
219 return 0;
222 /*return hpfs_map_4sectors(s, secno, qbh, 0);*/
223 if (!(qbh->data = kmalloc(2048, GFP_KERNEL))) {
224 printk("HPFS: hpfs_get_4sectors: out of memory\n");
225 return NULL;
227 if (!(hpfs_get_sector(s, secno, &qbh->bh[0]))) goto bail0;
228 if (!(hpfs_get_sector(s, secno + 1, &qbh->bh[1]))) goto bail1;
229 if (!(hpfs_get_sector(s, secno + 2, &qbh->bh[2]))) goto bail2;
230 if (!(hpfs_get_sector(s, secno + 3, &qbh->bh[3]))) goto bail3;
231 memcpy(qbh->data, qbh->bh[0]->b_data, 512);
232 memcpy(qbh->data + 512, qbh->bh[1]->b_data, 512);
233 memcpy(qbh->data + 2*512, qbh->bh[2]->b_data, 512);
234 memcpy(qbh->data + 3*512, qbh->bh[3]->b_data, 512);
235 return qbh->data;
237 bail3: brelse(qbh->bh[2]);
238 bail2: brelse(qbh->bh[1]);
239 bail1: brelse(qbh->bh[0]);
240 bail0:
241 return NULL;
245 void hpfs_brelse4(struct quad_buffer_head *qbh)
247 brelse(qbh->bh[3]);
248 brelse(qbh->bh[2]);
249 brelse(qbh->bh[1]);
250 brelse(qbh->bh[0]);
251 kfree_s(qbh->data, 2048);
254 void hpfs_mark_4buffers_dirty(struct quad_buffer_head *qbh)
256 PRINTK(("hpfs_mark_4buffers_dirty\n"));
257 memcpy(qbh->bh[0]->b_data, qbh->data, 512);
258 memcpy(qbh->bh[1]->b_data, qbh->data + 512, 512);
259 memcpy(qbh->bh[2]->b_data, qbh->data + 2 * 512, 512);
260 memcpy(qbh->bh[3]->b_data, qbh->data + 3 * 512, 512);
261 mark_buffer_dirty(qbh->bh[0],1);
262 mark_buffer_dirty(qbh->bh[1],1);
263 mark_buffer_dirty(qbh->bh[2],1);
264 mark_buffer_dirty(qbh->bh[3],1);