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 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);
134 printk("HPFS: hpfs_map_sector: read error\n");
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);
150 printk("HPFS: hpfs_get_sector: getblk failed\n");
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
,
160 kdev_t dev
= s
->s_dev
;
161 struct buffer_head
*bh
;
165 printk("HPFS: hpfs_map_4sectors: unaligned read\n");
169 qbh
->data
= data
= (char *)kmalloc(2048, GFP_KERNEL
);
171 printk("HPFS: hpfs_map_4sectors: out of memory\n");
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);
180 memcpy(data
, bh
->b_data
, 512);
182 qbh
->bh
[1] = bh
= bread(dev
, secno
+ 1, 512);
185 memcpy(data
+ 512, bh
->b_data
, 512);
187 qbh
->bh
[2] = bh
= bread(dev
, secno
+ 2, 512);
190 memcpy(data
+ 2 * 512, bh
->b_data
, 512);
192 qbh
->bh
[3] = bh
= bread(dev
, secno
+ 3, 512);
195 memcpy(data
+ 3 * 512, bh
->b_data
, 512);
207 printk("HPFS: hpfs_map_4sectors: read error\n");
212 /* Don't read sectors */
214 void *hpfs_get_4sectors(struct super_block
*s
, unsigned secno
,
215 struct quad_buffer_head
*qbh
)
218 printk("HPFS: hpfs_get_4sectors: unaligned read\n");
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");
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);
237 bail3
: brelse(qbh
->bh
[2]);
238 bail2
: brelse(qbh
->bh
[1]);
239 bail1
: brelse(qbh
->bh
[0]);
245 void hpfs_brelse4(struct quad_buffer_head
*qbh
)
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);