blob: f626114449e4a74c44568d07b26b81af4cc41ff8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 */
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +04008#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09009#include <linux/slab.h>
Mikulas Patocka275f4952013-07-04 19:04:01 +020010#include <linux/blkdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include "hpfs_fn.h"
12
Mikulas Patockaa64eefa2015-09-02 22:50:12 +020013secno hpfs_search_hotfix_map(struct super_block *s, secno sec)
14{
15 unsigned i;
16 struct hpfs_sb_info *sbi = hpfs_sb(s);
17 for (i = 0; unlikely(i < sbi->n_hotfixes); i++) {
18 if (sbi->hotfix_from[i] == sec) {
19 return sbi->hotfix_to[i];
20 }
21 }
22 return sec;
23}
24
25unsigned hpfs_search_hotfix_map_for_range(struct super_block *s, secno sec, unsigned n)
26{
27 unsigned i;
28 struct hpfs_sb_info *sbi = hpfs_sb(s);
29 for (i = 0; unlikely(i < sbi->n_hotfixes); i++) {
30 if (sbi->hotfix_from[i] >= sec && sbi->hotfix_from[i] < sec + n) {
31 n = sbi->hotfix_from[i] - sec;
32 }
33 }
34 return n;
35}
36
Mikulas Patocka275f4952013-07-04 19:04:01 +020037void hpfs_prefetch_sectors(struct super_block *s, unsigned secno, int n)
38{
39 struct buffer_head *bh;
40 struct blk_plug plug;
41
42 if (n <= 0 || unlikely(secno >= hpfs_sb(s)->sb_fs_size))
43 return;
44
Mikulas Patockaa64eefa2015-09-02 22:50:12 +020045 if (unlikely(hpfs_search_hotfix_map_for_range(s, secno, n) != n))
46 return;
47
Mikulas Patocka275f4952013-07-04 19:04:01 +020048 bh = sb_find_get_block(s, secno);
49 if (bh) {
50 if (buffer_uptodate(bh)) {
51 brelse(bh);
52 return;
53 }
54 brelse(bh);
55 };
56
57 blk_start_plug(&plug);
58 while (n > 0) {
59 if (unlikely(secno >= hpfs_sb(s)->sb_fs_size))
60 break;
61 sb_breadahead(s, secno);
62 secno++;
63 n--;
64 }
65 blk_finish_plug(&plug);
66}
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068/* Map a sector into a buffer and return pointers to it and to the buffer. */
69
70void *hpfs_map_sector(struct super_block *s, unsigned secno, struct buffer_head **bhp,
71 int ahead)
72{
73 struct buffer_head *bh;
74
Mikulas Patocka7dd29d82011-05-08 20:42:54 +020075 hpfs_lock_assert(s);
76
Mikulas Patocka275f4952013-07-04 19:04:01 +020077 hpfs_prefetch_sectors(s, secno, ahead);
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 cond_resched();
80
Mikulas Patockaa64eefa2015-09-02 22:50:12 +020081 *bhp = bh = sb_bread(s, hpfs_search_hotfix_map(s, secno));
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 if (bh != NULL)
83 return bh->b_data;
84 else {
Fabian Fredericka19189e2014-06-06 14:36:36 -070085 pr_err("%s(): read error\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 return NULL;
87 }
88}
89
90/* Like hpfs_map_sector but don't read anything */
91
92void *hpfs_get_sector(struct super_block *s, unsigned secno, struct buffer_head **bhp)
93{
94 struct buffer_head *bh;
95 /*return hpfs_map_sector(s, secno, bhp, 0);*/
96
Mikulas Patocka7dd29d82011-05-08 20:42:54 +020097 hpfs_lock_assert(s);
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 cond_resched();
100
Mikulas Patockaa64eefa2015-09-02 22:50:12 +0200101 if ((*bhp = bh = sb_getblk(s, hpfs_search_hotfix_map(s, secno))) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 if (!buffer_uptodate(bh)) wait_on_buffer(bh);
103 set_buffer_uptodate(bh);
104 return bh->b_data;
105 } else {
Fabian Fredericka19189e2014-06-06 14:36:36 -0700106 pr_err("%s(): getblk failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 return NULL;
108 }
109}
110
111/* Map 4 sectors into a 4buffer and return pointers to it and to the buffer. */
112
113void *hpfs_map_4sectors(struct super_block *s, unsigned secno, struct quad_buffer_head *qbh,
114 int ahead)
115{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 char *data;
117
Mikulas Patocka7dd29d82011-05-08 20:42:54 +0200118 hpfs_lock_assert(s);
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 cond_resched();
121
122 if (secno & 3) {
Fabian Fredericka19189e2014-06-06 14:36:36 -0700123 pr_err("%s(): unaligned read\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 return NULL;
125 }
126
Mikulas Patocka275f4952013-07-04 19:04:01 +0200127 hpfs_prefetch_sectors(s, secno, 4 + ahead);
128
Mikulas Patockaa64eefa2015-09-02 22:50:12 +0200129 if (!hpfs_map_sector(s, secno + 0, &qbh->bh[0], 0)) goto bail0;
130 if (!hpfs_map_sector(s, secno + 1, &qbh->bh[1], 0)) goto bail1;
131 if (!hpfs_map_sector(s, secno + 2, &qbh->bh[2], 0)) goto bail2;
132 if (!hpfs_map_sector(s, secno + 3, &qbh->bh[3], 0)) goto bail3;
Mikulas Patocka1c0b8a7a2014-01-29 00:11:33 +0100133
134 if (likely(qbh->bh[1]->b_data == qbh->bh[0]->b_data + 1 * 512) &&
135 likely(qbh->bh[2]->b_data == qbh->bh[0]->b_data + 2 * 512) &&
136 likely(qbh->bh[3]->b_data == qbh->bh[0]->b_data + 3 * 512)) {
137 return qbh->data = qbh->bh[0]->b_data;
138 }
139
Panagiotis Issarisf52720c2006-09-27 01:49:39 -0700140 qbh->data = data = kmalloc(2048, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 if (!data) {
Fabian Fredericka19189e2014-06-06 14:36:36 -0700142 pr_err("%s(): out of memory\n", __func__);
Mikulas Patocka1c0b8a7a2014-01-29 00:11:33 +0100143 goto bail4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 }
145
Mikulas Patocka1c0b8a7a2014-01-29 00:11:33 +0100146 memcpy(data + 0 * 512, qbh->bh[0]->b_data, 512);
147 memcpy(data + 1 * 512, qbh->bh[1]->b_data, 512);
148 memcpy(data + 2 * 512, qbh->bh[2]->b_data, 512);
149 memcpy(data + 3 * 512, qbh->bh[3]->b_data, 512);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151 return data;
152
Mikulas Patocka1c0b8a7a2014-01-29 00:11:33 +0100153 bail4:
154 brelse(qbh->bh[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 bail3:
156 brelse(qbh->bh[2]);
157 bail2:
158 brelse(qbh->bh[1]);
159 bail1:
160 brelse(qbh->bh[0]);
161 bail0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 return NULL;
163}
164
165/* Don't read sectors */
166
167void *hpfs_get_4sectors(struct super_block *s, unsigned secno,
168 struct quad_buffer_head *qbh)
169{
170 cond_resched();
171
Mikulas Patocka7dd29d82011-05-08 20:42:54 +0200172 hpfs_lock_assert(s);
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 if (secno & 3) {
Fabian Fredericka19189e2014-06-06 14:36:36 -0700175 pr_err("%s(): unaligned read\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 return NULL;
177 }
178
Mikulas Patocka1c0b8a7a2014-01-29 00:11:33 +0100179 if (!hpfs_get_sector(s, secno + 0, &qbh->bh[0])) goto bail0;
180 if (!hpfs_get_sector(s, secno + 1, &qbh->bh[1])) goto bail1;
181 if (!hpfs_get_sector(s, secno + 2, &qbh->bh[2])) goto bail2;
182 if (!hpfs_get_sector(s, secno + 3, &qbh->bh[3])) goto bail3;
183
184 if (likely(qbh->bh[1]->b_data == qbh->bh[0]->b_data + 1 * 512) &&
185 likely(qbh->bh[2]->b_data == qbh->bh[0]->b_data + 2 * 512) &&
186 likely(qbh->bh[3]->b_data == qbh->bh[0]->b_data + 3 * 512)) {
187 return qbh->data = qbh->bh[0]->b_data;
188 }
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 if (!(qbh->data = kmalloc(2048, GFP_NOFS))) {
Fabian Fredericka19189e2014-06-06 14:36:36 -0700191 pr_err("%s(): out of memory\n", __func__);
Mikulas Patocka1c0b8a7a2014-01-29 00:11:33 +0100192 goto bail4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 return qbh->data;
195
Mikulas Patocka1c0b8a7a2014-01-29 00:11:33 +0100196bail4:
197 brelse(qbh->bh[3]);
198bail3:
199 brelse(qbh->bh[2]);
200bail2:
201 brelse(qbh->bh[1]);
202bail1:
203 brelse(qbh->bh[0]);
204bail0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 return NULL;
206}
207
208
209void hpfs_brelse4(struct quad_buffer_head *qbh)
210{
Mikulas Patocka1c0b8a7a2014-01-29 00:11:33 +0100211 if (unlikely(qbh->data != qbh->bh[0]->b_data))
212 kfree(qbh->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 brelse(qbh->bh[0]);
Mikulas Patocka1c0b8a7a2014-01-29 00:11:33 +0100214 brelse(qbh->bh[1]);
215 brelse(qbh->bh[2]);
216 brelse(qbh->bh[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
218
219void hpfs_mark_4buffers_dirty(struct quad_buffer_head *qbh)
220{
Mikulas Patocka1c0b8a7a2014-01-29 00:11:33 +0100221 if (unlikely(qbh->data != qbh->bh[0]->b_data)) {
222 memcpy(qbh->bh[0]->b_data, qbh->data + 0 * 512, 512);
223 memcpy(qbh->bh[1]->b_data, qbh->data + 1 * 512, 512);
224 memcpy(qbh->bh[2]->b_data, qbh->data + 2 * 512, 512);
225 memcpy(qbh->bh[3]->b_data, qbh->data + 3 * 512, 512);
226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 mark_buffer_dirty(qbh->bh[0]);
228 mark_buffer_dirty(qbh->bh[1]);
229 mark_buffer_dirty(qbh->bh[2]);
230 mark_buffer_dirty(qbh->bh[3]);
231}