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