1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/bio.h>
18#include <linux/fs.h>
19#include <linux/buffer_head.h>
20#include <linux/blkdev.h>
21#include <linux/highmem.h>
22#include <linux/prefetch.h>
23#include <linux/mpage.h>
24#include <linux/writeback.h>
25#include <linux/pagevec.h>
26
27
28
29
30
31
32
33
34
35
36
37
38
39static int mpage_end_io_read(struct bio *bio, unsigned int bytes_done, int err)
40{
41 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
42 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
43
44 if (bio->bi_size)
45 return 1;
46
47 do {
48 struct page *page = bvec->bv_page;
49
50 if (--bvec >= bio->bi_io_vec)
51 prefetchw(&bvec->bv_page->flags);
52
53 if (uptodate) {
54 SetPageUptodate(page);
55 } else {
56 ClearPageUptodate(page);
57 SetPageError(page);
58 }
59 unlock_page(page);
60 } while (bvec >= bio->bi_io_vec);
61 bio_put(bio);
62 return 0;
63}
64
65static int mpage_end_io_write(struct bio *bio, unsigned int bytes_done, int err)
66{
67 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
68 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
69
70 if (bio->bi_size)
71 return 1;
72
73 do {
74 struct page *page = bvec->bv_page;
75
76 if (--bvec >= bio->bi_io_vec)
77 prefetchw(&bvec->bv_page->flags);
78
79 if (!uptodate)
80 SetPageError(page);
81 end_page_writeback(page);
82 } while (bvec >= bio->bi_io_vec);
83 bio_put(bio);
84 return 0;
85}
86
87struct bio *mpage_bio_submit(int rw, struct bio *bio)
88{
89 bio->bi_end_io = mpage_end_io_read;
90 if (rw == WRITE)
91 bio->bi_end_io = mpage_end_io_write;
92 submit_bio(rw, bio);
93 return NULL;
94}
95
96static struct bio *
97mpage_alloc(struct block_device *bdev,
98 sector_t first_sector, int nr_vecs, int gfp_flags)
99{
100 struct bio *bio;
101
102 bio = bio_alloc(gfp_flags, nr_vecs);
103
104 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
105 while (!bio && (nr_vecs /= 2))
106 bio = bio_alloc(gfp_flags, nr_vecs);
107 }
108
109 if (bio) {
110 bio->bi_bdev = bdev;
111 bio->bi_sector = first_sector;
112 }
113 return bio;
114}
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162static struct bio *
163do_mpage_readpage(struct bio *bio, struct page *page, unsigned nr_pages,
164 sector_t *last_block_in_bio, get_block_t get_block)
165{
166 struct inode *inode = page->mapping->host;
167 const unsigned blkbits = inode->i_blkbits;
168 const unsigned blocks_per_page = PAGE_CACHE_SIZE >> blkbits;
169 const unsigned blocksize = 1 << blkbits;
170 sector_t block_in_file;
171 sector_t last_block;
172 sector_t blocks[MAX_BUF_PER_PAGE];
173 unsigned page_block;
174 unsigned first_hole = blocks_per_page;
175 struct block_device *bdev = NULL;
176 struct buffer_head bh;
177
178 if (page_has_buffers(page))
179 goto confused;
180
181 block_in_file = page->index << (PAGE_CACHE_SHIFT - blkbits);
182 last_block = (inode->i_size + blocksize - 1) >> blkbits;
183
184 for (page_block = 0; page_block < blocks_per_page;
185 page_block++, block_in_file++) {
186 bh.b_state = 0;
187 if (block_in_file < last_block) {
188 if (get_block(inode, block_in_file, &bh, 0))
189 goto confused;
190 }
191
192 if (!buffer_mapped(&bh)) {
193 if (first_hole == blocks_per_page)
194 first_hole = page_block;
195 continue;
196 }
197
198 if (first_hole != blocks_per_page)
199 goto confused;
200
201
202 if (page_block && blocks[page_block-1] != bh.b_blocknr-1)
203 goto confused;
204 blocks[page_block] = bh.b_blocknr;
205 bdev = bh.b_bdev;
206 }
207
208 if (first_hole != blocks_per_page) {
209 memset(kmap(page) + (first_hole << blkbits), 0,
210 PAGE_CACHE_SIZE - (first_hole << blkbits));
211 flush_dcache_page(page);
212 kunmap(page);
213 if (first_hole == 0) {
214 SetPageUptodate(page);
215 unlock_page(page);
216 goto out;
217 }
218 }
219
220
221
222
223 if (bio && (*last_block_in_bio != blocks[0] - 1))
224 bio = mpage_bio_submit(READ, bio);
225
226alloc_new:
227 if (bio == NULL) {
228 bio = mpage_alloc(bdev, blocks[0] << (blkbits - 9),
229 nr_pages, GFP_KERNEL);
230 if (bio == NULL)
231 goto confused;
232 }
233
234 if (bio_add_page(bio, page, first_hole << blkbits, 0)) {
235 bio = mpage_bio_submit(READ, bio);
236 goto alloc_new;
237 }
238
239 if (buffer_boundary(&bh) || (first_hole != blocks_per_page))
240 bio = mpage_bio_submit(READ, bio);
241 else
242 *last_block_in_bio = blocks[blocks_per_page - 1];
243out:
244 return bio;
245
246confused:
247 if (bio)
248 bio = mpage_bio_submit(READ, bio);
249 block_read_full_page(page, get_block);
250 goto out;
251}
252
253int
254mpage_readpages(struct address_space *mapping, struct list_head *pages,
255 unsigned nr_pages, get_block_t get_block)
256{
257 struct bio *bio = NULL;
258 unsigned page_idx;
259 sector_t last_block_in_bio = 0;
260 struct pagevec lru_pvec;
261
262 pagevec_init(&lru_pvec);
263 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
264 struct page *page = list_entry(pages->prev, struct page, list);
265
266 prefetchw(&page->flags);
267 list_del(&page->list);
268 if (!add_to_page_cache(page, mapping, page->index)) {
269 bio = do_mpage_readpage(bio, page,
270 nr_pages - page_idx,
271 &last_block_in_bio, get_block);
272 if (!pagevec_add(&lru_pvec, page))
273 __pagevec_lru_add(&lru_pvec);
274 } else {
275 page_cache_release(page);
276 }
277 }
278 pagevec_lru_add(&lru_pvec);
279 BUG_ON(!list_empty(pages));
280 if (bio)
281 mpage_bio_submit(READ, bio);
282 return 0;
283}
284EXPORT_SYMBOL(mpage_readpages);
285
286
287
288
289int mpage_readpage(struct page *page, get_block_t get_block)
290{
291 struct bio *bio = NULL;
292 sector_t last_block_in_bio = 0;
293
294 bio = do_mpage_readpage(bio, page, 1,
295 &last_block_in_bio, get_block);
296 if (bio)
297 mpage_bio_submit(READ, bio);
298 return 0;
299}
300EXPORT_SYMBOL(mpage_readpage);
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318static inline struct bio *
319mpage_writepage(struct bio *bio, struct page *page, get_block_t get_block,
320 sector_t *last_block_in_bio, int *ret)
321{
322 struct inode *inode = page->mapping->host;
323 const unsigned blkbits = inode->i_blkbits;
324 unsigned long end_index;
325 const unsigned blocks_per_page = PAGE_CACHE_SIZE >> blkbits;
326 sector_t last_block;
327 sector_t block_in_file;
328 sector_t blocks[MAX_BUF_PER_PAGE];
329 unsigned page_block;
330 unsigned first_unmapped = blocks_per_page;
331 struct block_device *bdev = NULL;
332 int boundary = 0;
333
334 if (page_has_buffers(page)) {
335 struct buffer_head *head = page_buffers(page);
336 struct buffer_head *bh = head;
337
338
339 page_block = 0;
340 do {
341 BUG_ON(buffer_locked(bh));
342 if (!buffer_mapped(bh)) {
343
344
345
346
347 if (buffer_dirty(bh))
348 goto confused;
349 if (first_unmapped == blocks_per_page)
350 first_unmapped = page_block;
351 continue;
352 }
353
354 if (first_unmapped != blocks_per_page)
355 goto confused;
356
357 if (!buffer_dirty(bh) || !buffer_uptodate(bh))
358 goto confused;
359 if (page_block) {
360 if (bh->b_blocknr != blocks[page_block-1] + 1)
361 goto confused;
362 }
363 blocks[page_block++] = bh->b_blocknr;
364 boundary = buffer_boundary(bh);
365 bdev = bh->b_bdev;
366 } while ((bh = bh->b_this_page) != head);
367
368 if (first_unmapped)
369 goto page_is_mapped;
370
371
372
373
374
375
376
377 goto confused;
378 }
379
380
381
382
383 BUG_ON(!PageUptodate(page));
384 block_in_file = page->index << (PAGE_CACHE_SHIFT - blkbits);
385 last_block = (inode->i_size - 1) >> blkbits;
386 for (page_block = 0; page_block < blocks_per_page; ) {
387 struct buffer_head map_bh;
388
389 map_bh.b_state = 0;
390 if (get_block(inode, block_in_file, &map_bh, 1))
391 goto confused;
392 if (buffer_new(&map_bh))
393 unmap_underlying_metadata(map_bh.b_bdev,
394 map_bh.b_blocknr);
395 if (page_block) {
396 if (map_bh.b_blocknr != blocks[page_block-1] + 1)
397 goto confused;
398 }
399 blocks[page_block++] = map_bh.b_blocknr;
400 boundary = buffer_boundary(&map_bh);
401 bdev = map_bh.b_bdev;
402 if (block_in_file == last_block)
403 break;
404 block_in_file++;
405 }
406 if (page_block == 0)
407 buffer_error();
408
409 first_unmapped = page_block;
410
411 end_index = inode->i_size >> PAGE_CACHE_SHIFT;
412 if (page->index >= end_index) {
413 unsigned offset = inode->i_size & (PAGE_CACHE_SIZE - 1);
414
415 if (page->index > end_index || !offset)
416 goto confused;
417 memset(kmap(page) + offset, 0, PAGE_CACHE_SIZE - offset);
418 flush_dcache_page(page);
419 kunmap(page);
420 }
421
422page_is_mapped:
423
424
425
426
427 if (bio && *last_block_in_bio != blocks[0] - 1)
428 bio = mpage_bio_submit(WRITE, bio);
429
430alloc_new:
431 if (bio == NULL) {
432 const unsigned __nr_pages = 64;
433
434 bio = mpage_alloc(bdev, blocks[0] << (blkbits - 9),
435 __nr_pages, GFP_NOFS|__GFP_HIGH);
436 if (bio == NULL)
437 goto confused;
438 }
439
440
441
442
443
444 if (page_has_buffers(page)) {
445 struct buffer_head *head = page_buffers(page);
446 struct buffer_head *bh = head;
447 unsigned buffer_counter = 0;
448
449 do {
450 if (buffer_counter++ == first_unmapped)
451 break;
452 clear_buffer_dirty(bh);
453 bh = bh->b_this_page;
454 } while (bh != head);
455
456 if (buffer_heads_over_limit)
457 try_to_free_buffers(page);
458 }
459
460 if (bio_add_page(bio, page, first_unmapped << blkbits, 0)) {
461 bio = mpage_bio_submit(WRITE, bio);
462 goto alloc_new;
463 }
464
465 BUG_ON(PageWriteback(page));
466 SetPageWriteback(page);
467 unlock_page(page);
468 if (boundary || (first_unmapped != blocks_per_page))
469 bio = mpage_bio_submit(WRITE, bio);
470 else
471 *last_block_in_bio = blocks[blocks_per_page - 1];
472 goto out;
473
474confused:
475 if (bio)
476 bio = mpage_bio_submit(WRITE, bio);
477 *ret = page->mapping->a_ops->writepage(page);
478out:
479 return bio;
480}
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521int
522mpage_writepages(struct address_space *mapping,
523 struct writeback_control *wbc, get_block_t get_block)
524{
525 struct bio *bio = NULL;
526 sector_t last_block_in_bio = 0;
527 int ret = 0;
528 int done = 0;
529 int sync = called_for_sync();
530 struct pagevec pvec;
531 int (*writepage)(struct page *);
532
533 writepage = NULL;
534 if (get_block == NULL)
535 writepage = mapping->a_ops->writepage;
536
537 pagevec_init(&pvec);
538 write_lock(&mapping->page_lock);
539
540 list_splice_init(&mapping->dirty_pages, &mapping->io_pages);
541
542 while (!list_empty(&mapping->io_pages) && !done) {
543 struct page *page = list_entry(mapping->io_pages.prev,
544 struct page, list);
545 list_del(&page->list);
546 if (PageWriteback(page) && !sync) {
547 if (PageDirty(page)) {
548 list_add(&page->list, &mapping->dirty_pages);
549 continue;
550 }
551 list_add(&page->list, &mapping->locked_pages);
552 continue;
553 }
554 if (!PageDirty(page)) {
555 list_add(&page->list, &mapping->clean_pages);
556 continue;
557 }
558 list_add(&page->list, &mapping->locked_pages);
559
560 page_cache_get(page);
561 write_unlock(&mapping->page_lock);
562
563 lock_page(page);
564
565 if (sync)
566 wait_on_page_writeback(page);
567
568 if (page->mapping && !PageWriteback(page) &&
569 test_clear_page_dirty(page)) {
570 if (writepage) {
571 ret = (*writepage)(page);
572 } else {
573 bio = mpage_writepage(bio, page, get_block,
574 &last_block_in_bio, &ret);
575 }
576 if ((current->flags & PF_MEMALLOC) &&
577 !PageActive(page) && PageLRU(page)) {
578 if (!pagevec_add(&pvec, page))
579 pagevec_deactivate_inactive(&pvec);
580 page = NULL;
581 }
582 if (ret == -EAGAIN && page) {
583 __set_page_dirty_nobuffers(page);
584 ret = 0;
585 }
586 if (ret || (--(wbc->nr_to_write) <= 0))
587 done = 1;
588 } else {
589 unlock_page(page);
590 }
591
592 if (page)
593 page_cache_release(page);
594 write_lock(&mapping->page_lock);
595 }
596
597
598
599 write_unlock(&mapping->page_lock);
600 pagevec_deactivate_inactive(&pvec);
601 if (bio)
602 mpage_bio_submit(WRITE, bio);
603 return ret;
604}
605EXPORT_SYMBOL(mpage_writepages);
606