1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26#include <linux/fs.h>
27#include <linux/types.h>
28#include <linux/highmem.h>
29
30#include <cluster/masklog.h>
31
32#include "ocfs2.h"
33
34#include "alloc.h"
35#include "inode.h"
36#include "journal.h"
37#include "uptodate.h"
38#include "buffer_head_io.h"
39#include "ocfs2_trace.h"
40
41
42
43
44
45
46enum ocfs2_state_bits {
47 BH_NeedsValidate = BH_JBDPrivateStart,
48};
49
50
51BUFFER_FNS(NeedsValidate, needs_validate);
52
53int ocfs2_write_block(struct ocfs2_super *osb, struct buffer_head *bh,
54 struct ocfs2_caching_info *ci)
55{
56 int ret = 0;
57
58 trace_ocfs2_write_block((unsigned long long)bh->b_blocknr, ci);
59
60 BUG_ON(bh->b_blocknr < OCFS2_SUPER_BLOCK_BLKNO);
61 BUG_ON(buffer_jbd(bh));
62
63
64
65
66 if (ocfs2_is_hard_readonly(osb)) {
67 ret = -EROFS;
68 mlog_errno(ret);
69 goto out;
70 }
71
72 ocfs2_metadata_cache_io_lock(ci);
73
74 lock_buffer(bh);
75 set_buffer_uptodate(bh);
76
77
78 clear_buffer_dirty(bh);
79
80 get_bh(bh);
81 bh->b_end_io = end_buffer_write_sync;
82 submit_bh(REQ_OP_WRITE, 0, bh);
83
84 wait_on_buffer(bh);
85
86 if (buffer_uptodate(bh)) {
87 ocfs2_set_buffer_uptodate(ci, bh);
88 } else {
89
90
91
92 ret = -EIO;
93 mlog_errno(ret);
94 }
95
96 ocfs2_metadata_cache_io_unlock(ci);
97out:
98 return ret;
99}
100
101int ocfs2_read_blocks_sync(struct ocfs2_super *osb, u64 block,
102 unsigned int nr, struct buffer_head *bhs[])
103{
104 int status = 0;
105 unsigned int i;
106 struct buffer_head *bh;
107
108 trace_ocfs2_read_blocks_sync((unsigned long long)block, nr);
109
110 if (!nr)
111 goto bail;
112
113 for (i = 0 ; i < nr ; i++) {
114 if (bhs[i] == NULL) {
115 bhs[i] = sb_getblk(osb->sb, block++);
116 if (bhs[i] == NULL) {
117 status = -ENOMEM;
118 mlog_errno(status);
119 goto bail;
120 }
121 }
122 bh = bhs[i];
123
124 if (buffer_jbd(bh)) {
125 trace_ocfs2_read_blocks_sync_jbd(
126 (unsigned long long)bh->b_blocknr);
127 continue;
128 }
129
130 if (buffer_dirty(bh)) {
131
132
133 mlog(ML_ERROR,
134 "trying to sync read a dirty "
135 "buffer! (blocknr = %llu), skipping\n",
136 (unsigned long long)bh->b_blocknr);
137 continue;
138 }
139
140 lock_buffer(bh);
141 if (buffer_jbd(bh)) {
142#ifdef CATCH_BH_JBD_RACES
143 mlog(ML_ERROR,
144 "block %llu had the JBD bit set "
145 "while I was in lock_buffer!",
146 (unsigned long long)bh->b_blocknr);
147 BUG();
148#else
149 unlock_buffer(bh);
150 continue;
151#endif
152 }
153
154 clear_buffer_uptodate(bh);
155 get_bh(bh);
156 bh->b_end_io = end_buffer_read_sync;
157 submit_bh(REQ_OP_READ, 0, bh);
158 }
159
160 for (i = nr; i > 0; i--) {
161 bh = bhs[i - 1];
162
163
164 if (!buffer_jbd(bh))
165 wait_on_buffer(bh);
166
167 if (!buffer_uptodate(bh)) {
168
169
170
171 status = -EIO;
172 put_bh(bh);
173 bhs[i - 1] = NULL;
174 }
175 }
176
177bail:
178 return status;
179}
180
181int ocfs2_read_blocks(struct ocfs2_caching_info *ci, u64 block, int nr,
182 struct buffer_head *bhs[], int flags,
183 int (*validate)(struct super_block *sb,
184 struct buffer_head *bh))
185{
186 int status = 0;
187 int i, ignore_cache = 0;
188 struct buffer_head *bh;
189 struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
190
191 trace_ocfs2_read_blocks_begin(ci, (unsigned long long)block, nr, flags);
192
193 BUG_ON(!ci);
194 BUG_ON((flags & OCFS2_BH_READAHEAD) &&
195 (flags & OCFS2_BH_IGNORE_CACHE));
196
197 if (bhs == NULL) {
198 status = -EINVAL;
199 mlog_errno(status);
200 goto bail;
201 }
202
203 if (nr < 0) {
204 mlog(ML_ERROR, "asked to read %d blocks!\n", nr);
205 status = -EINVAL;
206 mlog_errno(status);
207 goto bail;
208 }
209
210 if (nr == 0) {
211 status = 0;
212 goto bail;
213 }
214
215 ocfs2_metadata_cache_io_lock(ci);
216 for (i = 0 ; i < nr ; i++) {
217 if (bhs[i] == NULL) {
218 bhs[i] = sb_getblk(sb, block++);
219 if (bhs[i] == NULL) {
220 ocfs2_metadata_cache_io_unlock(ci);
221 status = -ENOMEM;
222 mlog_errno(status);
223 goto bail;
224 }
225 }
226 bh = bhs[i];
227 ignore_cache = (flags & OCFS2_BH_IGNORE_CACHE);
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253 if (!ignore_cache && !ocfs2_buffer_uptodate(ci, bh)) {
254 trace_ocfs2_read_blocks_from_disk(
255 (unsigned long long)bh->b_blocknr,
256 (unsigned long long)ocfs2_metadata_cache_owner(ci));
257
258
259 ignore_cache = 1;
260 }
261
262 trace_ocfs2_read_blocks_bh((unsigned long long)bh->b_blocknr,
263 ignore_cache, buffer_jbd(bh), buffer_dirty(bh));
264
265 if (buffer_jbd(bh)) {
266 continue;
267 }
268
269 if (ignore_cache) {
270 if (buffer_dirty(bh)) {
271
272
273 continue;
274 }
275
276
277
278
279
280 if ((flags & OCFS2_BH_READAHEAD)
281 && ocfs2_buffer_read_ahead(ci, bh))
282 continue;
283
284 lock_buffer(bh);
285 if (buffer_jbd(bh)) {
286#ifdef CATCH_BH_JBD_RACES
287 mlog(ML_ERROR, "block %llu had the JBD bit set "
288 "while I was in lock_buffer!",
289 (unsigned long long)bh->b_blocknr);
290 BUG();
291#else
292 unlock_buffer(bh);
293 continue;
294#endif
295 }
296
297
298
299
300
301 if (!(flags & OCFS2_BH_IGNORE_CACHE)
302 && !(flags & OCFS2_BH_READAHEAD)
303 && ocfs2_buffer_uptodate(ci, bh)) {
304 unlock_buffer(bh);
305 continue;
306 }
307
308 clear_buffer_uptodate(bh);
309 get_bh(bh);
310 if (validate)
311 set_buffer_needs_validate(bh);
312 bh->b_end_io = end_buffer_read_sync;
313 submit_bh(REQ_OP_READ, 0, bh);
314 continue;
315 }
316 }
317
318 status = 0;
319
320 for (i = (nr - 1); i >= 0; i--) {
321 bh = bhs[i];
322
323 if (!(flags & OCFS2_BH_READAHEAD)) {
324 if (status) {
325
326 put_bh(bh);
327 bhs[i] = NULL;
328 continue;
329 }
330
331
332
333 if (!buffer_jbd(bh))
334 wait_on_buffer(bh);
335
336 if (!buffer_uptodate(bh)) {
337
338
339
340
341
342
343 status = -EIO;
344 put_bh(bh);
345 bhs[i] = NULL;
346 continue;
347 }
348
349 if (buffer_needs_validate(bh)) {
350
351
352
353 BUG_ON(buffer_jbd(bh));
354 clear_buffer_needs_validate(bh);
355 status = validate(sb, bh);
356 if (status) {
357 put_bh(bh);
358 bhs[i] = NULL;
359 continue;
360 }
361 }
362 }
363
364
365
366
367 ocfs2_set_buffer_uptodate(ci, bh);
368 }
369 ocfs2_metadata_cache_io_unlock(ci);
370
371 trace_ocfs2_read_blocks_end((unsigned long long)block, nr,
372 flags, ignore_cache);
373
374bail:
375
376 return status;
377}
378
379
380static void ocfs2_check_super_or_backup(struct super_block *sb,
381 sector_t blkno)
382{
383 int i;
384 u64 backup_blkno;
385
386 if (blkno == OCFS2_SUPER_BLOCK_BLKNO)
387 return;
388
389 for (i = 0; i < OCFS2_MAX_BACKUP_SUPERBLOCKS; i++) {
390 backup_blkno = ocfs2_backup_super_blkno(sb, i);
391 if (backup_blkno == blkno)
392 return;
393 }
394
395 BUG();
396}
397
398
399
400
401
402
403int ocfs2_write_super_or_backup(struct ocfs2_super *osb,
404 struct buffer_head *bh)
405{
406 int ret = 0;
407 struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
408
409 BUG_ON(buffer_jbd(bh));
410 ocfs2_check_super_or_backup(osb->sb, bh->b_blocknr);
411
412 if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb)) {
413 ret = -EROFS;
414 mlog_errno(ret);
415 goto out;
416 }
417
418 lock_buffer(bh);
419 set_buffer_uptodate(bh);
420
421
422 clear_buffer_dirty(bh);
423
424 get_bh(bh);
425 bh->b_end_io = end_buffer_write_sync;
426 ocfs2_compute_meta_ecc(osb->sb, bh->b_data, &di->i_check);
427 submit_bh(REQ_OP_WRITE, 0, bh);
428
429 wait_on_buffer(bh);
430
431 if (!buffer_uptodate(bh)) {
432 ret = -EIO;
433 mlog_errno(ret);
434 }
435
436out:
437 return ret;
438}
439