1 // SPDX-License-Identifier: GPL-2.0
3 * linux/fs/hpfs/buffer.c
5 * Mikulas Patocka (mikulas@artax.karlin.mff.cuni.cz), 1998-1999
9 #include <linux/sched.h>
10 #include <linux/slab.h>
11 #include <linux/blkdev.h>
14 secno
hpfs_search_hotfix_map(struct super_block
*s
, secno sec
)
17 struct hpfs_sb_info
*sbi
= hpfs_sb(s
);
18 for (i
= 0; unlikely(i
< sbi
->n_hotfixes
); i
++) {
19 if (sbi
->hotfix_from
[i
] == sec
) {
20 return sbi
->hotfix_to
[i
];
26 unsigned hpfs_search_hotfix_map_for_range(struct super_block
*s
, secno sec
, unsigned n
)
29 struct hpfs_sb_info
*sbi
= hpfs_sb(s
);
30 for (i
= 0; unlikely(i
< sbi
->n_hotfixes
); i
++) {
31 if (sbi
->hotfix_from
[i
] >= sec
&& sbi
->hotfix_from
[i
] < sec
+ n
) {
32 n
= sbi
->hotfix_from
[i
] - sec
;
38 void hpfs_prefetch_sectors(struct super_block
*s
, unsigned secno
, int n
)
40 struct buffer_head
*bh
;
43 if (n
<= 0 || unlikely(secno
>= hpfs_sb(s
)->sb_fs_size
))
46 if (unlikely(hpfs_search_hotfix_map_for_range(s
, secno
, n
) != n
))
49 bh
= sb_find_get_block(s
, secno
);
51 if (buffer_uptodate(bh
)) {
58 blk_start_plug(&plug
);
60 if (unlikely(secno
>= hpfs_sb(s
)->sb_fs_size
))
62 sb_breadahead(s
, secno
);
66 blk_finish_plug(&plug
);
69 /* Map a sector into a buffer and return pointers to it and to the buffer. */
71 void *hpfs_map_sector(struct super_block
*s
, unsigned secno
, struct buffer_head
**bhp
,
74 struct buffer_head
*bh
;
78 hpfs_prefetch_sectors(s
, secno
, ahead
);
82 *bhp
= bh
= sb_bread(s
, hpfs_search_hotfix_map(s
, secno
));
86 pr_err("%s(): read error\n", __func__
);
91 /* Like hpfs_map_sector but don't read anything */
93 void *hpfs_get_sector(struct super_block
*s
, unsigned secno
, struct buffer_head
**bhp
)
95 struct buffer_head
*bh
;
96 /*return hpfs_map_sector(s, secno, bhp, 0);*/
102 if ((*bhp
= bh
= sb_getblk(s
, hpfs_search_hotfix_map(s
, secno
))) != NULL
) {
103 if (!buffer_uptodate(bh
)) wait_on_buffer(bh
);
104 set_buffer_uptodate(bh
);
107 pr_err("%s(): getblk failed\n", __func__
);
112 /* Map 4 sectors into a 4buffer and return pointers to it and to the buffer. */
114 void *hpfs_map_4sectors(struct super_block
*s
, unsigned secno
, struct quad_buffer_head
*qbh
,
124 pr_err("%s(): unaligned read\n", __func__
);
128 hpfs_prefetch_sectors(s
, secno
, 4 + ahead
);
130 if (!hpfs_map_sector(s
, secno
+ 0, &qbh
->bh
[0], 0)) goto bail0
;
131 if (!hpfs_map_sector(s
, secno
+ 1, &qbh
->bh
[1], 0)) goto bail1
;
132 if (!hpfs_map_sector(s
, secno
+ 2, &qbh
->bh
[2], 0)) goto bail2
;
133 if (!hpfs_map_sector(s
, secno
+ 3, &qbh
->bh
[3], 0)) goto bail3
;
135 if (likely(qbh
->bh
[1]->b_data
== qbh
->bh
[0]->b_data
+ 1 * 512) &&
136 likely(qbh
->bh
[2]->b_data
== qbh
->bh
[0]->b_data
+ 2 * 512) &&
137 likely(qbh
->bh
[3]->b_data
== qbh
->bh
[0]->b_data
+ 3 * 512)) {
138 return qbh
->data
= qbh
->bh
[0]->b_data
;
141 qbh
->data
= data
= kmalloc(2048, GFP_NOFS
);
143 pr_err("%s(): out of memory\n", __func__
);
147 memcpy(data
+ 0 * 512, qbh
->bh
[0]->b_data
, 512);
148 memcpy(data
+ 1 * 512, qbh
->bh
[1]->b_data
, 512);
149 memcpy(data
+ 2 * 512, qbh
->bh
[2]->b_data
, 512);
150 memcpy(data
+ 3 * 512, qbh
->bh
[3]->b_data
, 512);
166 /* Don't read sectors */
168 void *hpfs_get_4sectors(struct super_block
*s
, unsigned secno
,
169 struct quad_buffer_head
*qbh
)
176 pr_err("%s(): unaligned read\n", __func__
);
180 if (!hpfs_get_sector(s
, secno
+ 0, &qbh
->bh
[0])) goto bail0
;
181 if (!hpfs_get_sector(s
, secno
+ 1, &qbh
->bh
[1])) goto bail1
;
182 if (!hpfs_get_sector(s
, secno
+ 2, &qbh
->bh
[2])) goto bail2
;
183 if (!hpfs_get_sector(s
, secno
+ 3, &qbh
->bh
[3])) goto bail3
;
185 if (likely(qbh
->bh
[1]->b_data
== qbh
->bh
[0]->b_data
+ 1 * 512) &&
186 likely(qbh
->bh
[2]->b_data
== qbh
->bh
[0]->b_data
+ 2 * 512) &&
187 likely(qbh
->bh
[3]->b_data
== qbh
->bh
[0]->b_data
+ 3 * 512)) {
188 return qbh
->data
= qbh
->bh
[0]->b_data
;
191 if (!(qbh
->data
= kmalloc(2048, GFP_NOFS
))) {
192 pr_err("%s(): out of memory\n", __func__
);
210 void hpfs_brelse4(struct quad_buffer_head
*qbh
)
212 if (unlikely(qbh
->data
!= qbh
->bh
[0]->b_data
))
220 void hpfs_mark_4buffers_dirty(struct quad_buffer_head
*qbh
)
222 if (unlikely(qbh
->data
!= qbh
->bh
[0]->b_data
)) {
223 memcpy(qbh
->bh
[0]->b_data
, qbh
->data
+ 0 * 512, 512);
224 memcpy(qbh
->bh
[1]->b_data
, qbh
->data
+ 1 * 512, 512);
225 memcpy(qbh
->bh
[2]->b_data
, qbh
->data
+ 2 * 512, 512);
226 memcpy(qbh
->bh
[3]->b_data
, qbh
->data
+ 3 * 512, 512);
228 mark_buffer_dirty(qbh
->bh
[0]);
229 mark_buffer_dirty(qbh
->bh
[1]);
230 mark_buffer_dirty(qbh
->bh
[2]);
231 mark_buffer_dirty(qbh
->bh
[3]);