2 * linux/fs/hpfs/buffer.c
4 * Mikulas Patocka (mikulas@artax.karlin.mff.cuni.cz), 1998-1999
9 #include <linux/string.h>
12 void hpfs_lock_creation(struct super_block *s)
15 printk("lock creation\n");
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)
24 printk("unlock creation\n");
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)
33 printk("lock iget\n");
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)
42 printk("unlock iget\n");
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) {
78 } else if (i1->i_ino > i2->i_ino) {
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);
110 } else if (i2->i_ino < i1->i_ino && i2->i_ino < i3->i_ino) {
111 hpfs_unlock_2inodes(i1, i3);
113 } else if (i3->i_ino < i1->i_ino && i3->i_ino < i2->i_ino) {
114 hpfs_unlock_2inodes(i1, i2);
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,
125 struct buffer_head *bh;
127 *bhp = bh = sb_bread(s, secno);
131 printk("HPFS: hpfs_map_sector: read error\n");
136 /* Like hpfs_map_sector but don't read anything */
138 void *hpfs_get_sector(struct super_block *s, unsigned secno, struct buffer_head **bhp)
140 struct buffer_head *bh;
141 /*return hpfs_map_sector(s, secno, bhp, 0);*/
143 if ((*bhp = bh = sb_getblk(s, secno)) != NULL) {
144 if (!buffer_uptodate(bh)) wait_on_buffer(bh);
145 mark_buffer_uptodate(bh, 1);
148 printk("HPFS: hpfs_get_sector: getblk failed\n");
153 /* Map 4 sectors into a 4buffer and return pointers to it and to the buffer. */
155 void *hpfs_map_4sectors(struct super_block *s, unsigned secno, struct quad_buffer_head *qbh,
158 struct buffer_head *bh;
162 printk("HPFS: hpfs_map_4sectors: unaligned read\n");
166 qbh->data = data = (char *)kmalloc(2048, GFP_KERNEL);
168 printk("HPFS: hpfs_map_4sectors: out of memory\n");
172 qbh->bh[0] = bh = sb_bread(s, secno);
175 memcpy(data, bh->b_data, 512);
177 qbh->bh[1] = bh = sb_bread(s, secno + 1);
180 memcpy(data + 512, bh->b_data, 512);
182 qbh->bh[2] = bh = sb_bread(s, secno + 2);
185 memcpy(data + 2 * 512, bh->b_data, 512);
187 qbh->bh[3] = bh = sb_bread(s, secno + 3);
190 memcpy(data + 3 * 512, bh->b_data, 512);
202 printk("HPFS: hpfs_map_4sectors: read error\n");
207 /* Don't read sectors */
209 void *hpfs_get_4sectors(struct super_block *s, unsigned secno,
210 struct quad_buffer_head *qbh)
213 printk("HPFS: hpfs_get_4sectors: unaligned read\n");
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");
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);
232 bail3: brelse(qbh->bh[2]);
233 bail2: brelse(qbh->bh[1]);
234 bail1: brelse(qbh->bh[0]);
240 void hpfs_brelse4(struct quad_buffer_head *qbh)
249 void hpfs_mark_4buffers_dirty(struct quad_buffer_head *qbh)
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]);