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;
129 *bhp = bh = sb_bread(s, secno);
133 printk("HPFS: hpfs_map_sector: read error\n");
138 /* Like hpfs_map_sector but don't read anything */
140 void *hpfs_get_sector(struct super_block *s, unsigned secno, struct buffer_head **bhp)
142 struct buffer_head *bh;
143 /*return hpfs_map_sector(s, secno, bhp, 0);*/
147 if ((*bhp = bh = sb_getblk(s, secno)) != NULL) {
148 if (!buffer_uptodate(bh)) wait_on_buffer(bh);
149 mark_buffer_uptodate(bh, 1);
152 printk("HPFS: hpfs_get_sector: getblk failed\n");
157 /* Map 4 sectors into a 4buffer and return pointers to it and to the buffer. */
159 void *hpfs_map_4sectors(struct super_block *s, unsigned secno, struct quad_buffer_head *qbh,
162 struct buffer_head *bh;
168 printk("HPFS: hpfs_map_4sectors: unaligned read\n");
172 qbh->data = data = (char *)kmalloc(2048, GFP_KERNEL);
174 printk("HPFS: hpfs_map_4sectors: out of memory\n");
178 qbh->bh[0] = bh = sb_bread(s, secno);
181 memcpy(data, bh->b_data, 512);
183 qbh->bh[1] = bh = sb_bread(s, secno + 1);
186 memcpy(data + 512, bh->b_data, 512);
188 qbh->bh[2] = bh = sb_bread(s, secno + 2);
191 memcpy(data + 2 * 512, bh->b_data, 512);
193 qbh->bh[3] = bh = sb_bread(s, secno + 3);
196 memcpy(data + 3 * 512, bh->b_data, 512);
208 printk("HPFS: hpfs_map_4sectors: read error\n");
213 /* Don't read sectors */
215 void *hpfs_get_4sectors(struct super_block *s, unsigned secno,
216 struct quad_buffer_head *qbh)
221 printk("HPFS: hpfs_get_4sectors: unaligned read\n");
225 /*return hpfs_map_4sectors(s, secno, qbh, 0);*/
226 if (!(qbh->data = kmalloc(2048, GFP_KERNEL))) {
227 printk("HPFS: hpfs_get_4sectors: out of memory\n");
230 if (!(hpfs_get_sector(s, secno, &qbh->bh[0]))) goto bail0;
231 if (!(hpfs_get_sector(s, secno + 1, &qbh->bh[1]))) goto bail1;
232 if (!(hpfs_get_sector(s, secno + 2, &qbh->bh[2]))) goto bail2;
233 if (!(hpfs_get_sector(s, secno + 3, &qbh->bh[3]))) goto bail3;
234 memcpy(qbh->data, qbh->bh[0]->b_data, 512);
235 memcpy(qbh->data + 512, qbh->bh[1]->b_data, 512);
236 memcpy(qbh->data + 2*512, qbh->bh[2]->b_data, 512);
237 memcpy(qbh->data + 3*512, qbh->bh[3]->b_data, 512);
240 bail3: brelse(qbh->bh[2]);
241 bail2: brelse(qbh->bh[1]);
242 bail1: brelse(qbh->bh[0]);
248 void hpfs_brelse4(struct quad_buffer_head *qbh)
257 void hpfs_mark_4buffers_dirty(struct quad_buffer_head *qbh)
259 PRINTK(("hpfs_mark_4buffers_dirty\n"));
260 memcpy(qbh->bh[0]->b_data, qbh->data, 512);
261 memcpy(qbh->bh[1]->b_data, qbh->data + 512, 512);
262 memcpy(qbh->bh[2]->b_data, qbh->data + 2 * 512, 512);
263 memcpy(qbh->bh[3]->b_data, qbh->data + 3 * 512, 512);
264 mark_buffer_dirty(qbh->bh[0]);
265 mark_buffer_dirty(qbh->bh[1]);
266 mark_buffer_dirty(qbh->bh[2]);
267 mark_buffer_dirty(qbh->bh[3]);