import of ftp.dlink.com/GPL/DSMG-600_reB/ppclinux.tar.gz
[linux-2.4.21-pre4.git] / fs / hpfs / buffer.c
1 /*
2  *  linux/fs/hpfs/buffer.c
3  *
4  *  Mikulas Patocka (mikulas@artax.karlin.mff.cuni.cz), 1998-1999
5  *
6  *  general buffer i/o
7  */
8
9 #include <linux/string.h>
10 #include "hpfs_fn.h"
11
12 void hpfs_lock_creation(struct super_block *s)
13 {
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;
19 }
20
21 void hpfs_unlock_creation(struct super_block *s)
22 {
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);
28 }
29
30 void hpfs_lock_iget(struct super_block *s, int mode)
31 {
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;
37 }
38
39 void hpfs_unlock_iget(struct super_block *s)
40 {
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);
46 }
47
48 void hpfs_lock_inode(struct inode *i)
49 {
50         if (i) down(&i->i_hpfs_sem);
51 }
52
53 void hpfs_unlock_inode(struct inode *i)
54 {
55         if (i) up(&i->i_hpfs_sem);
56 }
57
58 void hpfs_lock_2inodes(struct inode *i1, struct inode *i2)
59 {
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);
69 }
70
71 void hpfs_unlock_2inodes(struct inode *i1, struct inode *i2)
72 {
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);
82 }
83
84 void hpfs_lock_3inodes(struct inode *i1, struct inode *i2, struct inode *i3)
85 {
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);
100 }
101                 
102 void hpfs_unlock_3inodes(struct inode *i1, struct inode *i2, struct inode *i3)
103 {
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);
118 }
119
120 /* Map a sector into a buffer and return pointers to it and to the buffer. */
121
122 void *hpfs_map_sector(struct super_block *s, unsigned secno, struct buffer_head **bhp,
123                  int ahead)
124 {
125         struct buffer_head *bh;
126
127         *bhp = bh = sb_bread(s, secno);
128         if (bh != NULL)
129                 return bh->b_data;
130         else {
131                 printk("HPFS: hpfs_map_sector: read error\n");
132                 return NULL;
133         }
134 }
135
136 /* Like hpfs_map_sector but don't read anything */
137
138 void *hpfs_get_sector(struct super_block *s, unsigned secno, struct buffer_head **bhp)
139 {
140         struct buffer_head *bh;
141         /*return hpfs_map_sector(s, secno, bhp, 0);*/
142
143         if ((*bhp = bh = sb_getblk(s, secno)) != NULL) {
144                 if (!buffer_uptodate(bh)) wait_on_buffer(bh);
145                 mark_buffer_uptodate(bh, 1);
146                 return bh->b_data;
147         } else {
148                 printk("HPFS: hpfs_get_sector: getblk failed\n");
149                 return NULL;
150         }
151 }
152
153 /* Map 4 sectors into a 4buffer and return pointers to it and to the buffer. */
154
155 void *hpfs_map_4sectors(struct super_block *s, unsigned secno, struct quad_buffer_head *qbh,
156                    int ahead)
157 {
158         struct buffer_head *bh;
159         char *data;
160
161         if (secno & 3) {
162                 printk("HPFS: hpfs_map_4sectors: unaligned read\n");
163                 return 0;
164         }
165
166         qbh->data = data = (char *)kmalloc(2048, GFP_KERNEL);
167         if (!data) {
168                 printk("HPFS: hpfs_map_4sectors: out of memory\n");
169                 goto bail;
170         }
171
172         qbh->bh[0] = bh = sb_bread(s, secno);
173         if (!bh)
174                 goto bail0;
175         memcpy(data, bh->b_data, 512);
176
177         qbh->bh[1] = bh = sb_bread(s, secno + 1);
178         if (!bh)
179                 goto bail1;
180         memcpy(data + 512, bh->b_data, 512);
181
182         qbh->bh[2] = bh = sb_bread(s, secno + 2);
183         if (!bh)
184                 goto bail2;
185         memcpy(data + 2 * 512, bh->b_data, 512);
186
187         qbh->bh[3] = bh = sb_bread(s, secno + 3);
188         if (!bh)
189                 goto bail3;
190         memcpy(data + 3 * 512, bh->b_data, 512);
191
192         return data;
193
194  bail3:
195         brelse(qbh->bh[2]);
196  bail2:
197         brelse(qbh->bh[1]);
198  bail1:
199         brelse(qbh->bh[0]);
200  bail0:
201         kfree(data);
202         printk("HPFS: hpfs_map_4sectors: read error\n");
203  bail:
204         return NULL;
205 }
206
207 /* Don't read sectors */
208
209 void *hpfs_get_4sectors(struct super_block *s, unsigned secno,
210                           struct quad_buffer_head *qbh)
211 {
212         if (secno & 3) {
213                 printk("HPFS: hpfs_get_4sectors: unaligned read\n");
214                 return 0;
215         }
216
217         /*return hpfs_map_4sectors(s, secno, qbh, 0);*/
218         if (!(qbh->data = kmalloc(2048, GFP_KERNEL))) {
219                 printk("HPFS: hpfs_get_4sectors: out of memory\n");
220                 return NULL;
221         }
222         if (!(hpfs_get_sector(s, secno, &qbh->bh[0]))) goto bail0;
223         if (!(hpfs_get_sector(s, secno + 1, &qbh->bh[1]))) goto bail1;
224         if (!(hpfs_get_sector(s, secno + 2, &qbh->bh[2]))) goto bail2;
225         if (!(hpfs_get_sector(s, secno + 3, &qbh->bh[3]))) goto bail3;
226         memcpy(qbh->data, qbh->bh[0]->b_data, 512);
227         memcpy(qbh->data + 512, qbh->bh[1]->b_data, 512);
228         memcpy(qbh->data + 2*512, qbh->bh[2]->b_data, 512);
229         memcpy(qbh->data + 3*512, qbh->bh[3]->b_data, 512);
230         return qbh->data;
231
232         bail3:  brelse(qbh->bh[2]);
233         bail2:  brelse(qbh->bh[1]);
234         bail1:  brelse(qbh->bh[0]);
235         bail0:
236         return NULL;
237 }
238         
239
240 void hpfs_brelse4(struct quad_buffer_head *qbh)
241 {
242         brelse(qbh->bh[3]);
243         brelse(qbh->bh[2]);
244         brelse(qbh->bh[1]);
245         brelse(qbh->bh[0]);
246         kfree(qbh->data);
247 }       
248
249 void hpfs_mark_4buffers_dirty(struct quad_buffer_head *qbh)
250 {
251         PRINTK(("hpfs_mark_4buffers_dirty\n"));
252         memcpy(qbh->bh[0]->b_data, qbh->data, 512);
253         memcpy(qbh->bh[1]->b_data, qbh->data + 512, 512);
254         memcpy(qbh->bh[2]->b_data, qbh->data + 2 * 512, 512);
255         memcpy(qbh->bh[3]->b_data, qbh->data + 3 * 512, 512);
256         mark_buffer_dirty(qbh->bh[0]);
257         mark_buffer_dirty(qbh->bh[1]);
258         mark_buffer_dirty(qbh->bh[2]);
259         mark_buffer_dirty(qbh->bh[3]);
260 }