1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#include <linux/kernel.h>
22#include <linux/syscalls.h>
23#include <linux/fs.h>
24#include <linux/mm.h>
25#include <linux/percpu.h>
26#include <linux/slab.h>
27#include <linux/capability.h>
28#include <linux/blkdev.h>
29#include <linux/file.h>
30#include <linux/quotaops.h>
31#include <linux/highmem.h>
32#include <linux/module.h>
33#include <linux/writeback.h>
34#include <linux/hash.h>
35#include <linux/suspend.h>
36#include <linux/buffer_head.h>
37#include <linux/task_io_accounting_ops.h>
38#include <linux/bio.h>
39#include <linux/notifier.h>
40#include <linux/cpu.h>
41#include <linux/bitops.h>
42#include <linux/mpage.h>
43#include <linux/bit_spinlock.h>
44
45static int fsync_buffers_list(spinlock_t *lock, struct list_head *list);
46
47#define BH_ENTRY(list) list_entry((list), struct buffer_head, b_assoc_buffers)
48
49inline void
50init_buffer(struct buffer_head *bh, bh_end_io_t *handler, void *private)
51{
52 bh->b_end_io = handler;
53 bh->b_private = private;
54}
55
56static int sync_buffer(void *word)
57{
58 struct block_device *bd;
59 struct buffer_head *bh
60 = container_of(word, struct buffer_head, b_state);
61
62 smp_mb();
63 bd = bh->b_bdev;
64 if (bd)
65 blk_run_address_space(bd->bd_inode->i_mapping);
66 io_schedule();
67 return 0;
68}
69
70void __lock_buffer(struct buffer_head *bh)
71{
72 wait_on_bit_lock(&bh->b_state, BH_Lock, sync_buffer,
73 TASK_UNINTERRUPTIBLE);
74}
75EXPORT_SYMBOL(__lock_buffer);
76
77void unlock_buffer(struct buffer_head *bh)
78{
79 clear_bit_unlock(BH_Lock, &bh->b_state);
80 smp_mb__after_clear_bit();
81 wake_up_bit(&bh->b_state, BH_Lock);
82}
83
84
85
86
87
88
89void __wait_on_buffer(struct buffer_head * bh)
90{
91 wait_on_bit(&bh->b_state, BH_Lock, sync_buffer, TASK_UNINTERRUPTIBLE);
92}
93
94static void
95__clear_page_buffers(struct page *page)
96{
97 ClearPagePrivate(page);
98 set_page_private(page, 0);
99 page_cache_release(page);
100}
101
102
103static int quiet_error(struct buffer_head *bh)
104{
105 if (!test_bit(BH_Quiet, &bh->b_state) && printk_ratelimit())
106 return 0;
107 return 1;
108}
109
110
111static void buffer_io_error(struct buffer_head *bh)
112{
113 char b[BDEVNAME_SIZE];
114 printk(KERN_ERR "Buffer I/O error on device %s, logical block %Lu\n",
115 bdevname(bh->b_bdev, b),
116 (unsigned long long)bh->b_blocknr);
117}
118
119
120
121
122
123
124
125
126
127static void __end_buffer_read_notouch(struct buffer_head *bh, int uptodate)
128{
129 if (uptodate) {
130 set_buffer_uptodate(bh);
131 } else {
132
133 clear_buffer_uptodate(bh);
134 }
135 unlock_buffer(bh);
136}
137
138
139
140
141
142void end_buffer_read_sync(struct buffer_head *bh, int uptodate)
143{
144 __end_buffer_read_notouch(bh, uptodate);
145 put_bh(bh);
146}
147
148void end_buffer_write_sync(struct buffer_head *bh, int uptodate)
149{
150 char b[BDEVNAME_SIZE];
151
152 if (uptodate) {
153 set_buffer_uptodate(bh);
154 } else {
155 if (!buffer_eopnotsupp(bh) && !quiet_error(bh)) {
156 buffer_io_error(bh);
157 printk(KERN_WARNING "lost page write due to "
158 "I/O error on %s\n",
159 bdevname(bh->b_bdev, b));
160 }
161 set_buffer_write_io_error(bh);
162 clear_buffer_uptodate(bh);
163 }
164 unlock_buffer(bh);
165 put_bh(bh);
166}
167
168
169
170
171
172
173
174
175
176
177
178
179static struct buffer_head *
180__find_get_block_slow(struct block_device *bdev, sector_t block)
181{
182 struct inode *bd_inode = bdev->bd_inode;
183 struct address_space *bd_mapping = bd_inode->i_mapping;
184 struct buffer_head *ret = NULL;
185 pgoff_t index;
186 struct buffer_head *bh;
187 struct buffer_head *head;
188 struct page *page;
189 int all_mapped = 1;
190
191 index = block >> (PAGE_CACHE_SHIFT - bd_inode->i_blkbits);
192 page = find_get_page(bd_mapping, index);
193 if (!page)
194 goto out;
195
196 spin_lock(&bd_mapping->private_lock);
197 if (!page_has_buffers(page))
198 goto out_unlock;
199 head = page_buffers(page);
200 bh = head;
201 do {
202 if (!buffer_mapped(bh))
203 all_mapped = 0;
204 else if (bh->b_blocknr == block) {
205 ret = bh;
206 get_bh(bh);
207 goto out_unlock;
208 }
209 bh = bh->b_this_page;
210 } while (bh != head);
211
212
213
214
215
216
217 if (all_mapped) {
218 printk("__find_get_block_slow() failed. "
219 "block=%llu, b_blocknr=%llu\n",
220 (unsigned long long)block,
221 (unsigned long long)bh->b_blocknr);
222 printk("b_state=0x%08lx, b_size=%zu\n",
223 bh->b_state, bh->b_size);
224 printk("device blocksize: %d\n", 1 << bd_inode->i_blkbits);
225 }
226out_unlock:
227 spin_unlock(&bd_mapping->private_lock);
228 page_cache_release(page);
229out:
230 return ret;
231}
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265void invalidate_bdev(struct block_device *bdev)
266{
267 struct address_space *mapping = bdev->bd_inode->i_mapping;
268
269 if (mapping->nrpages == 0)
270 return;
271
272 invalidate_bh_lrus();
273 invalidate_mapping_pages(mapping, 0, -1);
274}
275
276
277
278
279static void free_more_memory(void)
280{
281 struct zone *zone;
282 int nid;
283
284 wakeup_pdflush(1024);
285 yield();
286
287 for_each_online_node(nid) {
288 (void)first_zones_zonelist(node_zonelist(nid, GFP_NOFS),
289 gfp_zone(GFP_NOFS), NULL,
290 &zone);
291 if (zone)
292 try_to_free_pages(node_zonelist(nid, GFP_NOFS), 0,
293 GFP_NOFS, NULL);
294 }
295}
296
297
298
299
300
301static void end_buffer_async_read(struct buffer_head *bh, int uptodate)
302{
303 unsigned long flags;
304 struct buffer_head *first;
305 struct buffer_head *tmp;
306 struct page *page;
307 int page_uptodate = 1;
308
309 BUG_ON(!buffer_async_read(bh));
310
311 page = bh->b_page;
312 if (uptodate) {
313 set_buffer_uptodate(bh);
314 } else {
315 clear_buffer_uptodate(bh);
316 if (!quiet_error(bh))
317 buffer_io_error(bh);
318 SetPageError(page);
319 }
320
321
322
323
324
325
326 first = page_buffers(page);
327 local_irq_save(flags);
328 bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
329 clear_buffer_async_read(bh);
330 unlock_buffer(bh);
331 tmp = bh;
332 do {
333 if (!buffer_uptodate(tmp))
334 page_uptodate = 0;
335 if (buffer_async_read(tmp)) {
336 BUG_ON(!buffer_locked(tmp));
337 goto still_busy;
338 }
339 tmp = tmp->b_this_page;
340 } while (tmp != bh);
341 bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
342 local_irq_restore(flags);
343
344
345
346
347
348 if (page_uptodate && !PageError(page))
349 SetPageUptodate(page);
350 unlock_page(page);
351 return;
352
353still_busy:
354 bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
355 local_irq_restore(flags);
356 return;
357}
358
359
360
361
362
363void end_buffer_async_write(struct buffer_head *bh, int uptodate)
364{
365 char b[BDEVNAME_SIZE];
366 unsigned long flags;
367 struct buffer_head *first;
368 struct buffer_head *tmp;
369 struct page *page;
370
371 BUG_ON(!buffer_async_write(bh));
372
373 page = bh->b_page;
374 if (uptodate) {
375 set_buffer_uptodate(bh);
376 } else {
377 if (!quiet_error(bh)) {
378 buffer_io_error(bh);
379 printk(KERN_WARNING "lost page write due to "
380 "I/O error on %s\n",
381 bdevname(bh->b_bdev, b));
382 }
383 set_bit(AS_EIO, &page->mapping->flags);
384 set_buffer_write_io_error(bh);
385 clear_buffer_uptodate(bh);
386 SetPageError(page);
387 }
388
389 first = page_buffers(page);
390 local_irq_save(flags);
391 bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
392
393 clear_buffer_async_write(bh);
394 unlock_buffer(bh);
395 tmp = bh->b_this_page;
396 while (tmp != bh) {
397 if (buffer_async_write(tmp)) {
398 BUG_ON(!buffer_locked(tmp));
399 goto still_busy;
400 }
401 tmp = tmp->b_this_page;
402 }
403 bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
404 local_irq_restore(flags);
405 end_page_writeback(page);
406 return;
407
408still_busy:
409 bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
410 local_irq_restore(flags);
411 return;
412}
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435static void mark_buffer_async_read(struct buffer_head *bh)
436{
437 bh->b_end_io = end_buffer_async_read;
438 set_buffer_async_read(bh);
439}
440
441void mark_buffer_async_write_endio(struct buffer_head *bh,
442 bh_end_io_t *handler)
443{
444 bh->b_end_io = handler;
445 set_buffer_async_write(bh);
446}
447
448void mark_buffer_async_write(struct buffer_head *bh)
449{
450 mark_buffer_async_write_endio(bh, end_buffer_async_write);
451}
452EXPORT_SYMBOL(mark_buffer_async_write);
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
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
507static void __remove_assoc_queue(struct buffer_head *bh)
508{
509 list_del_init(&bh->b_assoc_buffers);
510 WARN_ON(!bh->b_assoc_map);
511 if (buffer_write_io_error(bh))
512 set_bit(AS_EIO, &bh->b_assoc_map->flags);
513 bh->b_assoc_map = NULL;
514}
515
516int inode_has_buffers(struct inode *inode)
517{
518 return !list_empty(&inode->i_data.private_list);
519}
520
521
522
523
524
525
526
527
528
529
530
531static int osync_buffers_list(spinlock_t *lock, struct list_head *list)
532{
533 struct buffer_head *bh;
534 struct list_head *p;
535 int err = 0;
536
537 spin_lock(lock);
538repeat:
539 list_for_each_prev(p, list) {
540 bh = BH_ENTRY(p);
541 if (buffer_locked(bh)) {
542 get_bh(bh);
543 spin_unlock(lock);
544 wait_on_buffer(bh);
545 if (!buffer_uptodate(bh))
546 err = -EIO;
547 brelse(bh);
548 spin_lock(lock);
549 goto repeat;
550 }
551 }
552 spin_unlock(lock);
553 return err;
554}
555
556void do_thaw_all(struct work_struct *work)
557{
558 struct super_block *sb;
559 char b[BDEVNAME_SIZE];
560
561 spin_lock(&sb_lock);
562restart:
563 list_for_each_entry(sb, &super_blocks, s_list) {
564 sb->s_count++;
565 spin_unlock(&sb_lock);
566 down_read(&sb->s_umount);
567 while (sb->s_bdev && !thaw_bdev(sb->s_bdev, sb))
568 printk(KERN_WARNING "Emergency Thaw on %s\n",
569 bdevname(sb->s_bdev, b));
570 up_read(&sb->s_umount);
571 spin_lock(&sb_lock);
572 if (__put_super_and_need_restart(sb))
573 goto restart;
574 }
575 spin_unlock(&sb_lock);
576 kfree(work);
577 printk(KERN_WARNING "Emergency Thaw complete\n");
578}
579
580
581
582
583
584
585void emergency_thaw_all(void)
586{
587 struct work_struct *work;
588
589 work = kmalloc(sizeof(*work), GFP_ATOMIC);
590 if (work) {
591 INIT_WORK(work, do_thaw_all);
592 schedule_work(work);
593 }
594}
595
596
597
598
599
600
601
602
603
604
605
606
607int sync_mapping_buffers(struct address_space *mapping)
608{
609 struct address_space *buffer_mapping = mapping->assoc_mapping;
610
611 if (buffer_mapping == NULL || list_empty(&mapping->private_list))
612 return 0;
613
614 return fsync_buffers_list(&buffer_mapping->private_lock,
615 &mapping->private_list);
616}
617EXPORT_SYMBOL(sync_mapping_buffers);
618
619
620
621
622
623
624
625void write_boundary_block(struct block_device *bdev,
626 sector_t bblock, unsigned blocksize)
627{
628 struct buffer_head *bh = __find_get_block(bdev, bblock + 1, blocksize);
629 if (bh) {
630 if (buffer_dirty(bh))
631 ll_rw_block(WRITE, 1, &bh);
632 put_bh(bh);
633 }
634}
635
636void mark_buffer_dirty_inode(struct buffer_head *bh, struct inode *inode)
637{
638 struct address_space *mapping = inode->i_mapping;
639 struct address_space *buffer_mapping = bh->b_page->mapping;
640
641 mark_buffer_dirty(bh);
642 if (!mapping->assoc_mapping) {
643 mapping->assoc_mapping = buffer_mapping;
644 } else {
645 BUG_ON(mapping->assoc_mapping != buffer_mapping);
646 }
647 if (!bh->b_assoc_map) {
648 spin_lock(&buffer_mapping->private_lock);
649 list_move_tail(&bh->b_assoc_buffers,
650 &mapping->private_list);
651 bh->b_assoc_map = mapping;
652 spin_unlock(&buffer_mapping->private_lock);
653 }
654}
655EXPORT_SYMBOL(mark_buffer_dirty_inode);
656
657
658
659
660
661
662
663
664static void __set_page_dirty(struct page *page,
665 struct address_space *mapping, int warn)
666{
667 spin_lock_irq(&mapping->tree_lock);
668 if (page->mapping) {
669 WARN_ON_ONCE(warn && !PageUptodate(page));
670 account_page_dirtied(page, mapping);
671 radix_tree_tag_set(&mapping->page_tree,
672 page_index(page), PAGECACHE_TAG_DIRTY);
673 }
674 spin_unlock_irq(&mapping->tree_lock);
675 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
676}
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703int __set_page_dirty_buffers(struct page *page)
704{
705 int newly_dirty;
706 struct address_space *mapping = page_mapping(page);
707
708 if (unlikely(!mapping))
709 return !TestSetPageDirty(page);
710
711 spin_lock(&mapping->private_lock);
712 if (page_has_buffers(page)) {
713 struct buffer_head *head = page_buffers(page);
714 struct buffer_head *bh = head;
715
716 do {
717 set_buffer_dirty(bh);
718 bh = bh->b_this_page;
719 } while (bh != head);
720 }
721 newly_dirty = !TestSetPageDirty(page);
722 spin_unlock(&mapping->private_lock);
723
724 if (newly_dirty)
725 __set_page_dirty(page, mapping, 1);
726 return newly_dirty;
727}
728EXPORT_SYMBOL(__set_page_dirty_buffers);
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749static int fsync_buffers_list(spinlock_t *lock, struct list_head *list)
750{
751 struct buffer_head *bh;
752 struct list_head tmp;
753 struct address_space *mapping, *prev_mapping = NULL;
754 int err = 0, err2;
755
756 INIT_LIST_HEAD(&tmp);
757
758 spin_lock(lock);
759 while (!list_empty(list)) {
760 bh = BH_ENTRY(list->next);
761 mapping = bh->b_assoc_map;
762 __remove_assoc_queue(bh);
763
764
765 smp_mb();
766 if (buffer_dirty(bh) || buffer_locked(bh)) {
767 list_add(&bh->b_assoc_buffers, &tmp);
768 bh->b_assoc_map = mapping;
769 if (buffer_dirty(bh)) {
770 get_bh(bh);
771 spin_unlock(lock);
772
773
774
775
776
777
778 ll_rw_block(SWRITE_SYNC_PLUG, 1, &bh);
779
780
781
782
783
784
785
786 if (prev_mapping && prev_mapping != mapping)
787 blk_run_address_space(prev_mapping);
788 prev_mapping = mapping;
789
790 brelse(bh);
791 spin_lock(lock);
792 }
793 }
794 }
795
796 while (!list_empty(&tmp)) {
797 bh = BH_ENTRY(tmp.prev);
798 get_bh(bh);
799 mapping = bh->b_assoc_map;
800 __remove_assoc_queue(bh);
801
802
803 smp_mb();
804 if (buffer_dirty(bh)) {
805 list_add(&bh->b_assoc_buffers,
806 &mapping->private_list);
807 bh->b_assoc_map = mapping;
808 }
809 spin_unlock(lock);
810 wait_on_buffer(bh);
811 if (!buffer_uptodate(bh))
812 err = -EIO;
813 brelse(bh);
814 spin_lock(lock);
815 }
816
817 spin_unlock(lock);
818 err2 = osync_buffers_list(lock, list);
819 if (err)
820 return err;
821 else
822 return err2;
823}
824
825
826
827
828
829
830
831
832
833
834void invalidate_inode_buffers(struct inode *inode)
835{
836 if (inode_has_buffers(inode)) {
837 struct address_space *mapping = &inode->i_data;
838 struct list_head *list = &mapping->private_list;
839 struct address_space *buffer_mapping = mapping->assoc_mapping;
840
841 spin_lock(&buffer_mapping->private_lock);
842 while (!list_empty(list))
843 __remove_assoc_queue(BH_ENTRY(list->next));
844 spin_unlock(&buffer_mapping->private_lock);
845 }
846}
847EXPORT_SYMBOL(invalidate_inode_buffers);
848
849
850
851
852
853
854
855int remove_inode_buffers(struct inode *inode)
856{
857 int ret = 1;
858
859 if (inode_has_buffers(inode)) {
860 struct address_space *mapping = &inode->i_data;
861 struct list_head *list = &mapping->private_list;
862 struct address_space *buffer_mapping = mapping->assoc_mapping;
863
864 spin_lock(&buffer_mapping->private_lock);
865 while (!list_empty(list)) {
866 struct buffer_head *bh = BH_ENTRY(list->next);
867 if (buffer_dirty(bh)) {
868 ret = 0;
869 break;
870 }
871 __remove_assoc_queue(bh);
872 }
873 spin_unlock(&buffer_mapping->private_lock);
874 }
875 return ret;
876}
877
878
879
880
881
882
883
884
885
886
887struct buffer_head *alloc_page_buffers(struct page *page, unsigned long size,
888 int retry)
889{
890 struct buffer_head *bh, *head;
891 long offset;
892
893try_again:
894 head = NULL;
895 offset = PAGE_SIZE;
896 while ((offset -= size) >= 0) {
897 bh = alloc_buffer_head(GFP_NOFS);
898 if (!bh)
899 goto no_grow;
900
901 bh->b_bdev = NULL;
902 bh->b_this_page = head;
903 bh->b_blocknr = -1;
904 head = bh;
905
906 bh->b_state = 0;
907 atomic_set(&bh->b_count, 0);
908 bh->b_private = NULL;
909 bh->b_size = size;
910
911
912 set_bh_page(bh, page, offset);
913
914 init_buffer(bh, NULL, NULL);
915 }
916 return head;
917
918
919
920no_grow:
921 if (head) {
922 do {
923 bh = head;
924 head = head->b_this_page;
925 free_buffer_head(bh);
926 } while (head);
927 }
928
929
930
931
932
933
934
935 if (!retry)
936 return NULL;
937
938
939
940
941
942
943
944 free_more_memory();
945 goto try_again;
946}
947EXPORT_SYMBOL_GPL(alloc_page_buffers);
948
949static inline void
950link_dev_buffers(struct page *page, struct buffer_head *head)
951{
952 struct buffer_head *bh, *tail;
953
954 bh = head;
955 do {
956 tail = bh;
957 bh = bh->b_this_page;
958 } while (bh);
959 tail->b_this_page = head;
960 attach_page_buffers(page, head);
961}
962
963
964
965
966static void
967init_page_buffers(struct page *page, struct block_device *bdev,
968 sector_t block, int size)
969{
970 struct buffer_head *head = page_buffers(page);
971 struct buffer_head *bh = head;
972 int uptodate = PageUptodate(page);
973
974 do {
975 if (!buffer_mapped(bh)) {
976 init_buffer(bh, NULL, NULL);
977 bh->b_bdev = bdev;
978 bh->b_blocknr = block;
979 if (uptodate)
980 set_buffer_uptodate(bh);
981 set_buffer_mapped(bh);
982 }
983 block++;
984 bh = bh->b_this_page;
985 } while (bh != head);
986}
987
988
989
990
991
992
993static struct page *
994grow_dev_page(struct block_device *bdev, sector_t block,
995 pgoff_t index, int size)
996{
997 struct inode *inode = bdev->bd_inode;
998 struct page *page;
999 struct buffer_head *bh;
1000
1001 page = find_or_create_page(inode->i_mapping, index,
1002 (mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS)|__GFP_MOVABLE);
1003 if (!page)
1004 return NULL;
1005
1006 BUG_ON(!PageLocked(page));
1007
1008 if (page_has_buffers(page)) {
1009 bh = page_buffers(page);
1010 if (bh->b_size == size) {
1011 init_page_buffers(page, bdev, block, size);
1012 return page;
1013 }
1014 if (!try_to_free_buffers(page))
1015 goto failed;
1016 }
1017
1018
1019
1020
1021 bh = alloc_page_buffers(page, size, 0);
1022 if (!bh)
1023 goto failed;
1024
1025
1026
1027
1028
1029
1030 spin_lock(&inode->i_mapping->private_lock);
1031 link_dev_buffers(page, bh);
1032 init_page_buffers(page, bdev, block, size);
1033 spin_unlock(&inode->i_mapping->private_lock);
1034 return page;
1035
1036failed:
1037 BUG();
1038 unlock_page(page);
1039 page_cache_release(page);
1040 return NULL;
1041}
1042
1043
1044
1045
1046
1047static int
1048grow_buffers(struct block_device *bdev, sector_t block, int size)
1049{
1050 struct page *page;
1051 pgoff_t index;
1052 int sizebits;
1053
1054 sizebits = -1;
1055 do {
1056 sizebits++;
1057 } while ((size << sizebits) < PAGE_SIZE);
1058
1059 index = block >> sizebits;
1060
1061
1062
1063
1064
1065 if (unlikely(index != block >> sizebits)) {
1066 char b[BDEVNAME_SIZE];
1067
1068 printk(KERN_ERR "%s: requested out-of-range block %llu for "
1069 "device %s\n",
1070 __func__, (unsigned long long)block,
1071 bdevname(bdev, b));
1072 return -EIO;
1073 }
1074 block = index << sizebits;
1075
1076 page = grow_dev_page(bdev, block, index, size);
1077 if (!page)
1078 return 0;
1079 unlock_page(page);
1080 page_cache_release(page);
1081 return 1;
1082}
1083
1084static struct buffer_head *
1085__getblk_slow(struct block_device *bdev, sector_t block, int size)
1086{
1087
1088 if (unlikely(size & (bdev_logical_block_size(bdev)-1) ||
1089 (size < 512 || size > PAGE_SIZE))) {
1090 printk(KERN_ERR "getblk(): invalid block size %d requested\n",
1091 size);
1092 printk(KERN_ERR "logical block size: %d\n",
1093 bdev_logical_block_size(bdev));
1094
1095 dump_stack();
1096 return NULL;
1097 }
1098
1099 for (;;) {
1100 struct buffer_head * bh;
1101 int ret;
1102
1103 bh = __find_get_block(bdev, block, size);
1104 if (bh)
1105 return bh;
1106
1107 ret = grow_buffers(bdev, block, size);
1108 if (ret < 0)
1109 return NULL;
1110 if (ret == 0)
1111 free_more_memory();
1112 }
1113}
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150void mark_buffer_dirty(struct buffer_head *bh)
1151{
1152 WARN_ON_ONCE(!buffer_uptodate(bh));
1153
1154
1155
1156
1157
1158
1159
1160 if (buffer_dirty(bh)) {
1161 smp_mb();
1162 if (buffer_dirty(bh))
1163 return;
1164 }
1165
1166 if (!test_set_buffer_dirty(bh)) {
1167 struct page *page = bh->b_page;
1168 if (!TestSetPageDirty(page)) {
1169 struct address_space *mapping = page_mapping(page);
1170 if (mapping)
1171 __set_page_dirty(page, mapping, 0);
1172 }
1173 }
1174}
1175
1176
1177
1178
1179
1180
1181
1182
1183void __brelse(struct buffer_head * buf)
1184{
1185 if (atomic_read(&buf->b_count)) {
1186 put_bh(buf);
1187 return;
1188 }
1189 WARN(1, KERN_ERR "VFS: brelse: Trying to free free buffer\n");
1190}
1191
1192
1193
1194
1195
1196void __bforget(struct buffer_head *bh)
1197{
1198 clear_buffer_dirty(bh);
1199 if (bh->b_assoc_map) {
1200 struct address_space *buffer_mapping = bh->b_page->mapping;
1201
1202 spin_lock(&buffer_mapping->private_lock);
1203 list_del_init(&bh->b_assoc_buffers);
1204 bh->b_assoc_map = NULL;
1205 spin_unlock(&buffer_mapping->private_lock);
1206 }
1207 __brelse(bh);
1208}
1209
1210static struct buffer_head *__bread_slow(struct buffer_head *bh)
1211{
1212 lock_buffer(bh);
1213 if (buffer_uptodate(bh)) {
1214 unlock_buffer(bh);
1215 return bh;
1216 } else {
1217 get_bh(bh);
1218 bh->b_end_io = end_buffer_read_sync;
1219 submit_bh(READ, bh);
1220 wait_on_buffer(bh);
1221 if (buffer_uptodate(bh))
1222 return bh;
1223 }
1224 brelse(bh);
1225 return NULL;
1226}
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242#define BH_LRU_SIZE 8
1243
1244struct bh_lru {
1245 struct buffer_head *bhs[BH_LRU_SIZE];
1246};
1247
1248static DEFINE_PER_CPU(struct bh_lru, bh_lrus) = {{ NULL }};
1249
1250#ifdef CONFIG_SMP
1251#define bh_lru_lock() local_irq_disable()
1252#define bh_lru_unlock() local_irq_enable()
1253#else
1254#define bh_lru_lock() preempt_disable()
1255#define bh_lru_unlock() preempt_enable()
1256#endif
1257
1258static inline void check_irqs_on(void)
1259{
1260#ifdef irqs_disabled
1261 BUG_ON(irqs_disabled());
1262#endif
1263}
1264
1265
1266
1267
1268static void bh_lru_install(struct buffer_head *bh)
1269{
1270 struct buffer_head *evictee = NULL;
1271 struct bh_lru *lru;
1272
1273 check_irqs_on();
1274 bh_lru_lock();
1275 lru = &__get_cpu_var(bh_lrus);
1276 if (lru->bhs[0] != bh) {
1277 struct buffer_head *bhs[BH_LRU_SIZE];
1278 int in;
1279 int out = 0;
1280
1281 get_bh(bh);
1282 bhs[out++] = bh;
1283 for (in = 0; in < BH_LRU_SIZE; in++) {
1284 struct buffer_head *bh2 = lru->bhs[in];
1285
1286 if (bh2 == bh) {
1287 __brelse(bh2);
1288 } else {
1289 if (out >= BH_LRU_SIZE) {
1290 BUG_ON(evictee != NULL);
1291 evictee = bh2;
1292 } else {
1293 bhs[out++] = bh2;
1294 }
1295 }
1296 }
1297 while (out < BH_LRU_SIZE)
1298 bhs[out++] = NULL;
1299 memcpy(lru->bhs, bhs, sizeof(bhs));
1300 }
1301 bh_lru_unlock();
1302
1303 if (evictee)
1304 __brelse(evictee);
1305}
1306
1307
1308
1309
1310static struct buffer_head *
1311lookup_bh_lru(struct block_device *bdev, sector_t block, unsigned size)
1312{
1313 struct buffer_head *ret = NULL;
1314 struct bh_lru *lru;
1315 unsigned int i;
1316
1317 check_irqs_on();
1318 bh_lru_lock();
1319 lru = &__get_cpu_var(bh_lrus);
1320 for (i = 0; i < BH_LRU_SIZE; i++) {
1321 struct buffer_head *bh = lru->bhs[i];
1322
1323 if (bh && bh->b_bdev == bdev &&
1324 bh->b_blocknr == block && bh->b_size == size) {
1325 if (i) {
1326 while (i) {
1327 lru->bhs[i] = lru->bhs[i - 1];
1328 i--;
1329 }
1330 lru->bhs[0] = bh;
1331 }
1332 get_bh(bh);
1333 ret = bh;
1334 break;
1335 }
1336 }
1337 bh_lru_unlock();
1338 return ret;
1339}
1340
1341
1342
1343
1344
1345
1346struct buffer_head *
1347__find_get_block(struct block_device *bdev, sector_t block, unsigned size)
1348{
1349 struct buffer_head *bh = lookup_bh_lru(bdev, block, size);
1350
1351 if (bh == NULL) {
1352 bh = __find_get_block_slow(bdev, block);
1353 if (bh)
1354 bh_lru_install(bh);
1355 }
1356 if (bh)
1357 touch_buffer(bh);
1358 return bh;
1359}
1360EXPORT_SYMBOL(__find_get_block);
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374struct buffer_head *
1375__getblk(struct block_device *bdev, sector_t block, unsigned size)
1376{
1377 struct buffer_head *bh = __find_get_block(bdev, block, size);
1378
1379 might_sleep();
1380 if (bh == NULL)
1381 bh = __getblk_slow(bdev, block, size);
1382 return bh;
1383}
1384EXPORT_SYMBOL(__getblk);
1385
1386
1387
1388
1389void __breadahead(struct block_device *bdev, sector_t block, unsigned size)
1390{
1391 struct buffer_head *bh = __getblk(bdev, block, size);
1392 if (likely(bh)) {
1393 ll_rw_block(READA, 1, &bh);
1394 brelse(bh);
1395 }
1396}
1397EXPORT_SYMBOL(__breadahead);
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408struct buffer_head *
1409__bread(struct block_device *bdev, sector_t block, unsigned size)
1410{
1411 struct buffer_head *bh = __getblk(bdev, block, size);
1412
1413 if (likely(bh) && !buffer_uptodate(bh))
1414 bh = __bread_slow(bh);
1415 return bh;
1416}
1417EXPORT_SYMBOL(__bread);
1418
1419
1420
1421
1422
1423
1424static void invalidate_bh_lru(void *arg)
1425{
1426 struct bh_lru *b = &get_cpu_var(bh_lrus);
1427 int i;
1428
1429 for (i = 0; i < BH_LRU_SIZE; i++) {
1430 brelse(b->bhs[i]);
1431 b->bhs[i] = NULL;
1432 }
1433 put_cpu_var(bh_lrus);
1434}
1435
1436void invalidate_bh_lrus(void)
1437{
1438 on_each_cpu(invalidate_bh_lru, NULL, 1);
1439}
1440EXPORT_SYMBOL_GPL(invalidate_bh_lrus);
1441
1442void set_bh_page(struct buffer_head *bh,
1443 struct page *page, unsigned long offset)
1444{
1445 bh->b_page = page;
1446 BUG_ON(offset >= PAGE_SIZE);
1447 if (PageHighMem(page))
1448
1449
1450
1451 bh->b_data = (char *)(0 + offset);
1452 else
1453 bh->b_data = page_address(page) + offset;
1454}
1455EXPORT_SYMBOL(set_bh_page);
1456
1457
1458
1459
1460static void discard_buffer(struct buffer_head * bh)
1461{
1462 lock_buffer(bh);
1463 clear_buffer_dirty(bh);
1464 bh->b_bdev = NULL;
1465 clear_buffer_mapped(bh);
1466 clear_buffer_req(bh);
1467 clear_buffer_new(bh);
1468 clear_buffer_delay(bh);
1469 clear_buffer_unwritten(bh);
1470 unlock_buffer(bh);
1471}
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488void block_invalidatepage(struct page *page, unsigned long offset)
1489{
1490 struct buffer_head *head, *bh, *next;
1491 unsigned int curr_off = 0;
1492
1493 BUG_ON(!PageLocked(page));
1494 if (!page_has_buffers(page))
1495 goto out;
1496
1497 head = page_buffers(page);
1498 bh = head;
1499 do {
1500 unsigned int next_off = curr_off + bh->b_size;
1501 next = bh->b_this_page;
1502
1503
1504
1505
1506 if (offset <= curr_off)
1507 discard_buffer(bh);
1508 curr_off = next_off;
1509 bh = next;
1510 } while (bh != head);
1511
1512
1513
1514
1515
1516
1517 if (offset == 0)
1518 try_to_release_page(page, 0);
1519out:
1520 return;
1521}
1522EXPORT_SYMBOL(block_invalidatepage);
1523
1524
1525
1526
1527
1528
1529void create_empty_buffers(struct page *page,
1530 unsigned long blocksize, unsigned long b_state)
1531{
1532 struct buffer_head *bh, *head, *tail;
1533
1534 head = alloc_page_buffers(page, blocksize, 1);
1535 bh = head;
1536 do {
1537 bh->b_state |= b_state;
1538 tail = bh;
1539 bh = bh->b_this_page;
1540 } while (bh);
1541 tail->b_this_page = head;
1542
1543 spin_lock(&page->mapping->private_lock);
1544 if (PageUptodate(page) || PageDirty(page)) {
1545 bh = head;
1546 do {
1547 if (PageDirty(page))
1548 set_buffer_dirty(bh);
1549 if (PageUptodate(page))
1550 set_buffer_uptodate(bh);
1551 bh = bh->b_this_page;
1552 } while (bh != head);
1553 }
1554 attach_page_buffers(page, head);
1555 spin_unlock(&page->mapping->private_lock);
1556}
1557EXPORT_SYMBOL(create_empty_buffers);
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575void unmap_underlying_metadata(struct block_device *bdev, sector_t block)
1576{
1577 struct buffer_head *old_bh;
1578
1579 might_sleep();
1580
1581 old_bh = __find_get_block_slow(bdev, block);
1582 if (old_bh) {
1583 clear_buffer_dirty(old_bh);
1584 wait_on_buffer(old_bh);
1585 clear_buffer_req(old_bh);
1586 __brelse(old_bh);
1587 }
1588}
1589EXPORT_SYMBOL(unmap_underlying_metadata);
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626static int __block_write_full_page(struct inode *inode, struct page *page,
1627 get_block_t *get_block, struct writeback_control *wbc,
1628 bh_end_io_t *handler)
1629{
1630 int err;
1631 sector_t block;
1632 sector_t last_block;
1633 struct buffer_head *bh, *head;
1634 const unsigned blocksize = 1 << inode->i_blkbits;
1635 int nr_underway = 0;
1636 int write_op = (wbc->sync_mode == WB_SYNC_ALL ?
1637 WRITE_SYNC_PLUG : WRITE);
1638
1639 BUG_ON(!PageLocked(page));
1640
1641 last_block = (i_size_read(inode) - 1) >> inode->i_blkbits;
1642
1643 if (!page_has_buffers(page)) {
1644 create_empty_buffers(page, blocksize,
1645 (1 << BH_Dirty)|(1 << BH_Uptodate));
1646 }
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658 block = (sector_t)page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
1659 head = page_buffers(page);
1660 bh = head;
1661
1662
1663
1664
1665
1666 do {
1667 if (block > last_block) {
1668
1669
1670
1671
1672
1673
1674
1675
1676 clear_buffer_dirty(bh);
1677 set_buffer_uptodate(bh);
1678 } else if ((!buffer_mapped(bh) || buffer_delay(bh)) &&
1679 buffer_dirty(bh)) {
1680 WARN_ON(bh->b_size != blocksize);
1681 err = get_block(inode, block, bh, 1);
1682 if (err)
1683 goto recover;
1684 clear_buffer_delay(bh);
1685 if (buffer_new(bh)) {
1686
1687 clear_buffer_new(bh);
1688 unmap_underlying_metadata(bh->b_bdev,
1689 bh->b_blocknr);
1690 }
1691 }
1692 bh = bh->b_this_page;
1693 block++;
1694 } while (bh != head);
1695
1696 do {
1697 if (!buffer_mapped(bh))
1698 continue;
1699
1700
1701
1702
1703
1704
1705
1706 if (wbc->sync_mode != WB_SYNC_NONE || !wbc->nonblocking) {
1707 lock_buffer(bh);
1708 } else if (!trylock_buffer(bh)) {
1709 redirty_page_for_writepage(wbc, page);
1710 continue;
1711 }
1712 if (test_clear_buffer_dirty(bh)) {
1713 mark_buffer_async_write_endio(bh, handler);
1714 } else {
1715 unlock_buffer(bh);
1716 }
1717 } while ((bh = bh->b_this_page) != head);
1718
1719
1720
1721
1722
1723 BUG_ON(PageWriteback(page));
1724 set_page_writeback(page);
1725
1726 do {
1727 struct buffer_head *next = bh->b_this_page;
1728 if (buffer_async_write(bh)) {
1729 submit_bh(write_op, bh);
1730 nr_underway++;
1731 }
1732 bh = next;
1733 } while (bh != head);
1734 unlock_page(page);
1735
1736 err = 0;
1737done:
1738 if (nr_underway == 0) {
1739
1740
1741
1742
1743
1744 end_page_writeback(page);
1745
1746
1747
1748
1749
1750 }
1751 return err;
1752
1753recover:
1754
1755
1756
1757
1758
1759
1760 bh = head;
1761
1762 do {
1763 if (buffer_mapped(bh) && buffer_dirty(bh) &&
1764 !buffer_delay(bh)) {
1765 lock_buffer(bh);
1766 mark_buffer_async_write_endio(bh, handler);
1767 } else {
1768
1769
1770
1771
1772 clear_buffer_dirty(bh);
1773 }
1774 } while ((bh = bh->b_this_page) != head);
1775 SetPageError(page);
1776 BUG_ON(PageWriteback(page));
1777 mapping_set_error(page->mapping, err);
1778 set_page_writeback(page);
1779 do {
1780 struct buffer_head *next = bh->b_this_page;
1781 if (buffer_async_write(bh)) {
1782 clear_buffer_dirty(bh);
1783 submit_bh(write_op, bh);
1784 nr_underway++;
1785 }
1786 bh = next;
1787 } while (bh != head);
1788 unlock_page(page);
1789 goto done;
1790}
1791
1792
1793
1794
1795
1796
1797void page_zero_new_buffers(struct page *page, unsigned from, unsigned to)
1798{
1799 unsigned int block_start, block_end;
1800 struct buffer_head *head, *bh;
1801
1802 BUG_ON(!PageLocked(page));
1803 if (!page_has_buffers(page))
1804 return;
1805
1806 bh = head = page_buffers(page);
1807 block_start = 0;
1808 do {
1809 block_end = block_start + bh->b_size;
1810
1811 if (buffer_new(bh)) {
1812 if (block_end > from && block_start < to) {
1813 if (!PageUptodate(page)) {
1814 unsigned start, size;
1815
1816 start = max(from, block_start);
1817 size = min(to, block_end) - start;
1818
1819 zero_user(page, start, size);
1820 set_buffer_uptodate(bh);
1821 }
1822
1823 clear_buffer_new(bh);
1824 mark_buffer_dirty(bh);
1825 }
1826 }
1827
1828 block_start = block_end;
1829 bh = bh->b_this_page;
1830 } while (bh != head);
1831}
1832EXPORT_SYMBOL(page_zero_new_buffers);
1833
1834static int __block_prepare_write(struct inode *inode, struct page *page,
1835 unsigned from, unsigned to, get_block_t *get_block)
1836{
1837 unsigned block_start, block_end;
1838 sector_t block;
1839 int err = 0;
1840 unsigned blocksize, bbits;
1841 struct buffer_head *bh, *head, *wait[2], **wait_bh=wait;
1842
1843 BUG_ON(!PageLocked(page));
1844 BUG_ON(from > PAGE_CACHE_SIZE);
1845 BUG_ON(to > PAGE_CACHE_SIZE);
1846 BUG_ON(from > to);
1847
1848 blocksize = 1 << inode->i_blkbits;
1849 if (!page_has_buffers(page))
1850 create_empty_buffers(page, blocksize, 0);
1851 head = page_buffers(page);
1852
1853 bbits = inode->i_blkbits;
1854 block = (sector_t)page->index << (PAGE_CACHE_SHIFT - bbits);
1855
1856 for(bh = head, block_start = 0; bh != head || !block_start;
1857 block++, block_start=block_end, bh = bh->b_this_page) {
1858 block_end = block_start + blocksize;
1859 if (block_end <= from || block_start >= to) {
1860 if (PageUptodate(page)) {
1861 if (!buffer_uptodate(bh))
1862 set_buffer_uptodate(bh);
1863 }
1864 continue;
1865 }
1866 if (buffer_new(bh))
1867 clear_buffer_new(bh);
1868 if (!buffer_mapped(bh)) {
1869 WARN_ON(bh->b_size != blocksize);
1870 err = get_block(inode, block, bh, 1);
1871 if (err)
1872 break;
1873 if (buffer_new(bh)) {
1874 unmap_underlying_metadata(bh->b_bdev,
1875 bh->b_blocknr);
1876 if (PageUptodate(page)) {
1877 clear_buffer_new(bh);
1878 set_buffer_uptodate(bh);
1879 mark_buffer_dirty(bh);
1880 continue;
1881 }
1882 if (block_end > to || block_start < from)
1883 zero_user_segments(page,
1884 to, block_end,
1885 block_start, from);
1886 continue;
1887 }
1888 }
1889 if (PageUptodate(page)) {
1890 if (!buffer_uptodate(bh))
1891 set_buffer_uptodate(bh);
1892 continue;
1893 }
1894 if (!buffer_uptodate(bh) && !buffer_delay(bh) &&
1895 !buffer_unwritten(bh) &&
1896 (block_start < from || block_end > to)) {
1897 ll_rw_block(READ, 1, &bh);
1898 *wait_bh++=bh;
1899 }
1900 }
1901
1902
1903
1904 while(wait_bh > wait) {
1905 wait_on_buffer(*--wait_bh);
1906 if (!buffer_uptodate(*wait_bh))
1907 err = -EIO;
1908 }
1909 if (unlikely(err))
1910 page_zero_new_buffers(page, from, to);
1911 return err;
1912}
1913
1914static int __block_commit_write(struct inode *inode, struct page *page,
1915 unsigned from, unsigned to)
1916{
1917 unsigned block_start, block_end;
1918 int partial = 0;
1919 unsigned blocksize;
1920 struct buffer_head *bh, *head;
1921
1922 blocksize = 1 << inode->i_blkbits;
1923
1924 for(bh = head = page_buffers(page), block_start = 0;
1925 bh != head || !block_start;
1926 block_start=block_end, bh = bh->b_this_page) {
1927 block_end = block_start + blocksize;
1928 if (block_end <= from || block_start >= to) {
1929 if (!buffer_uptodate(bh))
1930 partial = 1;
1931 } else {
1932 set_buffer_uptodate(bh);
1933 mark_buffer_dirty(bh);
1934 }
1935 clear_buffer_new(bh);
1936 }
1937
1938
1939
1940
1941
1942
1943
1944 if (!partial)
1945 SetPageUptodate(page);
1946 return 0;
1947}
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957int block_write_begin(struct file *file, struct address_space *mapping,
1958 loff_t pos, unsigned len, unsigned flags,
1959 struct page **pagep, void **fsdata,
1960 get_block_t *get_block)
1961{
1962 struct inode *inode = mapping->host;
1963 int status = 0;
1964 struct page *page;
1965 pgoff_t index;
1966 unsigned start, end;
1967 int ownpage = 0;
1968
1969 index = pos >> PAGE_CACHE_SHIFT;
1970 start = pos & (PAGE_CACHE_SIZE - 1);
1971 end = start + len;
1972
1973 page = *pagep;
1974 if (page == NULL) {
1975 ownpage = 1;
1976 page = grab_cache_page_write_begin(mapping, index, flags);
1977 if (!page) {
1978 status = -ENOMEM;
1979 goto out;
1980 }
1981 *pagep = page;
1982 } else
1983 BUG_ON(!PageLocked(page));
1984
1985 status = __block_prepare_write(inode, page, start, end, get_block);
1986 if (unlikely(status)) {
1987 ClearPageUptodate(page);
1988
1989 if (ownpage) {
1990 unlock_page(page);
1991 page_cache_release(page);
1992 *pagep = NULL;
1993
1994
1995
1996
1997
1998
1999 if (pos + len > inode->i_size)
2000 vmtruncate(inode, inode->i_size);
2001 }
2002 }
2003
2004out:
2005 return status;
2006}
2007EXPORT_SYMBOL(block_write_begin);
2008
2009int block_write_end(struct file *file, struct address_space *mapping,
2010 loff_t pos, unsigned len, unsigned copied,
2011 struct page *page, void *fsdata)
2012{
2013 struct inode *inode = mapping->host;
2014 unsigned start;
2015
2016 start = pos & (PAGE_CACHE_SIZE - 1);
2017
2018 if (unlikely(copied < len)) {
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031 if (!PageUptodate(page))
2032 copied = 0;
2033
2034 page_zero_new_buffers(page, start+copied, start+len);
2035 }
2036 flush_dcache_page(page);
2037
2038
2039 __block_commit_write(inode, page, start, start+copied);
2040
2041 return copied;
2042}
2043EXPORT_SYMBOL(block_write_end);
2044
2045int generic_write_end(struct file *file, struct address_space *mapping,
2046 loff_t pos, unsigned len, unsigned copied,
2047 struct page *page, void *fsdata)
2048{
2049 struct inode *inode = mapping->host;
2050 int i_size_changed = 0;
2051
2052 copied = block_write_end(file, mapping, pos, len, copied, page, fsdata);
2053
2054
2055
2056
2057
2058
2059
2060
2061 if (pos+copied > inode->i_size) {
2062 i_size_write(inode, pos+copied);
2063 i_size_changed = 1;
2064 }
2065
2066 unlock_page(page);
2067 page_cache_release(page);
2068
2069
2070
2071
2072
2073
2074
2075 if (i_size_changed)
2076 mark_inode_dirty(inode);
2077
2078 return copied;
2079}
2080EXPORT_SYMBOL(generic_write_end);
2081
2082
2083
2084
2085
2086
2087
2088
2089int block_is_partially_uptodate(struct page *page, read_descriptor_t *desc,
2090 unsigned long from)
2091{
2092 struct inode *inode = page->mapping->host;
2093 unsigned block_start, block_end, blocksize;
2094 unsigned to;
2095 struct buffer_head *bh, *head;
2096 int ret = 1;
2097
2098 if (!page_has_buffers(page))
2099 return 0;
2100
2101 blocksize = 1 << inode->i_blkbits;
2102 to = min_t(unsigned, PAGE_CACHE_SIZE - from, desc->count);
2103 to = from + to;
2104 if (from < blocksize && to > PAGE_CACHE_SIZE - blocksize)
2105 return 0;
2106
2107 head = page_buffers(page);
2108 bh = head;
2109 block_start = 0;
2110 do {
2111 block_end = block_start + blocksize;
2112 if (block_end > from && block_start < to) {
2113 if (!buffer_uptodate(bh)) {
2114 ret = 0;
2115 break;
2116 }
2117 if (block_end >= to)
2118 break;
2119 }
2120 block_start = block_end;
2121 bh = bh->b_this_page;
2122 } while (bh != head);
2123
2124 return ret;
2125}
2126EXPORT_SYMBOL(block_is_partially_uptodate);
2127
2128
2129
2130
2131
2132
2133
2134
2135int block_read_full_page(struct page *page, get_block_t *get_block)
2136{
2137 struct inode *inode = page->mapping->host;
2138 sector_t iblock, lblock;
2139 struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE];
2140 unsigned int blocksize;
2141 int nr, i;
2142 int fully_mapped = 1;
2143
2144 BUG_ON(!PageLocked(page));
2145 blocksize = 1 << inode->i_blkbits;
2146 if (!page_has_buffers(page))
2147 create_empty_buffers(page, blocksize, 0);
2148 head = page_buffers(page);
2149
2150 iblock = (sector_t)page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
2151 lblock = (i_size_read(inode)+blocksize-1) >> inode->i_blkbits;
2152 bh = head;
2153 nr = 0;
2154 i = 0;
2155
2156 do {
2157 if (buffer_uptodate(bh))
2158 continue;
2159
2160 if (!buffer_mapped(bh)) {
2161 int err = 0;
2162
2163 fully_mapped = 0;
2164 if (iblock < lblock) {
2165 WARN_ON(bh->b_size != blocksize);
2166 err = get_block(inode, iblock, bh, 0);
2167 if (err)
2168 SetPageError(page);
2169 }
2170 if (!buffer_mapped(bh)) {
2171 zero_user(page, i * blocksize, blocksize);
2172 if (!err)
2173 set_buffer_uptodate(bh);
2174 continue;
2175 }
2176
2177
2178
2179
2180 if (buffer_uptodate(bh))
2181 continue;
2182 }
2183 arr[nr++] = bh;
2184 } while (i++, iblock++, (bh = bh->b_this_page) != head);
2185
2186 if (fully_mapped)
2187 SetPageMappedToDisk(page);
2188
2189 if (!nr) {
2190
2191
2192
2193
2194 if (!PageError(page))
2195 SetPageUptodate(page);
2196 unlock_page(page);
2197 return 0;
2198 }
2199
2200
2201 for (i = 0; i < nr; i++) {
2202 bh = arr[i];
2203 lock_buffer(bh);
2204 mark_buffer_async_read(bh);
2205 }
2206
2207
2208
2209
2210
2211
2212 for (i = 0; i < nr; i++) {
2213 bh = arr[i];
2214 if (buffer_uptodate(bh))
2215 end_buffer_async_read(bh, 1);
2216 else
2217 submit_bh(READ, bh);
2218 }
2219 return 0;
2220}
2221
2222
2223
2224
2225
2226int generic_cont_expand_simple(struct inode *inode, loff_t size)
2227{
2228 struct address_space *mapping = inode->i_mapping;
2229 struct page *page;
2230 void *fsdata;
2231 unsigned long limit;
2232 int err;
2233
2234 err = -EFBIG;
2235 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
2236 if (limit != RLIM_INFINITY && size > (loff_t)limit) {
2237 send_sig(SIGXFSZ, current, 0);
2238 goto out;
2239 }
2240 if (size > inode->i_sb->s_maxbytes)
2241 goto out;
2242
2243 err = pagecache_write_begin(NULL, mapping, size, 0,
2244 AOP_FLAG_UNINTERRUPTIBLE|AOP_FLAG_CONT_EXPAND,
2245 &page, &fsdata);
2246 if (err)
2247 goto out;
2248
2249 err = pagecache_write_end(NULL, mapping, size, 0, 0, page, fsdata);
2250 BUG_ON(err > 0);
2251
2252out:
2253 return err;
2254}
2255
2256static int cont_expand_zero(struct file *file, struct address_space *mapping,
2257 loff_t pos, loff_t *bytes)
2258{
2259 struct inode *inode = mapping->host;
2260 unsigned blocksize = 1 << inode->i_blkbits;
2261 struct page *page;
2262 void *fsdata;
2263 pgoff_t index, curidx;
2264 loff_t curpos;
2265 unsigned zerofrom, offset, len;
2266 int err = 0;
2267
2268 index = pos >> PAGE_CACHE_SHIFT;
2269 offset = pos & ~PAGE_CACHE_MASK;
2270
2271 while (index > (curidx = (curpos = *bytes)>>PAGE_CACHE_SHIFT)) {
2272 zerofrom = curpos & ~PAGE_CACHE_MASK;
2273 if (zerofrom & (blocksize-1)) {
2274 *bytes |= (blocksize-1);
2275 (*bytes)++;
2276 }
2277 len = PAGE_CACHE_SIZE - zerofrom;
2278
2279 err = pagecache_write_begin(file, mapping, curpos, len,
2280 AOP_FLAG_UNINTERRUPTIBLE,
2281 &page, &fsdata);
2282 if (err)
2283 goto out;
2284 zero_user(page, zerofrom, len);
2285 err = pagecache_write_end(file, mapping, curpos, len, len,
2286 page, fsdata);
2287 if (err < 0)
2288 goto out;
2289 BUG_ON(err != len);
2290 err = 0;
2291
2292 balance_dirty_pages_ratelimited(mapping);
2293 }
2294
2295
2296 if (index == curidx) {
2297 zerofrom = curpos & ~PAGE_CACHE_MASK;
2298
2299 if (offset <= zerofrom) {
2300 goto out;
2301 }
2302 if (zerofrom & (blocksize-1)) {
2303 *bytes |= (blocksize-1);
2304 (*bytes)++;
2305 }
2306 len = offset - zerofrom;
2307
2308 err = pagecache_write_begin(file, mapping, curpos, len,
2309 AOP_FLAG_UNINTERRUPTIBLE,
2310 &page, &fsdata);
2311 if (err)
2312 goto out;
2313 zero_user(page, zerofrom, len);
2314 err = pagecache_write_end(file, mapping, curpos, len, len,
2315 page, fsdata);
2316 if (err < 0)
2317 goto out;
2318 BUG_ON(err != len);
2319 err = 0;
2320 }
2321out:
2322 return err;
2323}
2324
2325
2326
2327
2328
2329int cont_write_begin(struct file *file, struct address_space *mapping,
2330 loff_t pos, unsigned len, unsigned flags,
2331 struct page **pagep, void **fsdata,
2332 get_block_t *get_block, loff_t *bytes)
2333{
2334 struct inode *inode = mapping->host;
2335 unsigned blocksize = 1 << inode->i_blkbits;
2336 unsigned zerofrom;
2337 int err;
2338
2339 err = cont_expand_zero(file, mapping, pos, bytes);
2340 if (err)
2341 goto out;
2342
2343 zerofrom = *bytes & ~PAGE_CACHE_MASK;
2344 if (pos+len > *bytes && zerofrom & (blocksize-1)) {
2345 *bytes |= (blocksize-1);
2346 (*bytes)++;
2347 }
2348
2349 *pagep = NULL;
2350 err = block_write_begin(file, mapping, pos, len,
2351 flags, pagep, fsdata, get_block);
2352out:
2353 return err;
2354}
2355
2356int block_prepare_write(struct page *page, unsigned from, unsigned to,
2357 get_block_t *get_block)
2358{
2359 struct inode *inode = page->mapping->host;
2360 int err = __block_prepare_write(inode, page, from, to, get_block);
2361 if (err)
2362 ClearPageUptodate(page);
2363 return err;
2364}
2365
2366int block_commit_write(struct page *page, unsigned from, unsigned to)
2367{
2368 struct inode *inode = page->mapping->host;
2369 __block_commit_write(inode,page,from,to);
2370 return 0;
2371}
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388int
2389block_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf,
2390 get_block_t get_block)
2391{
2392 struct page *page = vmf->page;
2393 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
2394 unsigned long end;
2395 loff_t size;
2396 int ret = VM_FAULT_NOPAGE;
2397
2398 lock_page(page);
2399 size = i_size_read(inode);
2400 if ((page->mapping != inode->i_mapping) ||
2401 (page_offset(page) > size)) {
2402
2403 unlock_page(page);
2404 goto out;
2405 }
2406
2407
2408 if (((page->index + 1) << PAGE_CACHE_SHIFT) > size)
2409 end = size & ~PAGE_CACHE_MASK;
2410 else
2411 end = PAGE_CACHE_SIZE;
2412
2413 ret = block_prepare_write(page, 0, end, get_block);
2414 if (!ret)
2415 ret = block_commit_write(page, 0, end);
2416
2417 if (unlikely(ret)) {
2418 unlock_page(page);
2419 if (ret == -ENOMEM)
2420 ret = VM_FAULT_OOM;
2421 else
2422 ret = VM_FAULT_SIGBUS;
2423 } else
2424 ret = VM_FAULT_LOCKED;
2425
2426out:
2427 return ret;
2428}
2429
2430
2431
2432
2433
2434
2435static void end_buffer_read_nobh(struct buffer_head *bh, int uptodate)
2436{
2437 __end_buffer_read_notouch(bh, uptodate);
2438}
2439
2440
2441
2442
2443
2444
2445static void attach_nobh_buffers(struct page *page, struct buffer_head *head)
2446{
2447 struct buffer_head *bh;
2448
2449 BUG_ON(!PageLocked(page));
2450
2451 spin_lock(&page->mapping->private_lock);
2452 bh = head;
2453 do {
2454 if (PageDirty(page))
2455 set_buffer_dirty(bh);
2456 if (!bh->b_this_page)
2457 bh->b_this_page = head;
2458 bh = bh->b_this_page;
2459 } while (bh != head);
2460 attach_page_buffers(page, head);
2461 spin_unlock(&page->mapping->private_lock);
2462}
2463
2464
2465
2466
2467
2468int nobh_write_begin(struct file *file, struct address_space *mapping,
2469 loff_t pos, unsigned len, unsigned flags,
2470 struct page **pagep, void **fsdata,
2471 get_block_t *get_block)
2472{
2473 struct inode *inode = mapping->host;
2474 const unsigned blkbits = inode->i_blkbits;
2475 const unsigned blocksize = 1 << blkbits;
2476 struct buffer_head *head, *bh;
2477 struct page *page;
2478 pgoff_t index;
2479 unsigned from, to;
2480 unsigned block_in_page;
2481 unsigned block_start, block_end;
2482 sector_t block_in_file;
2483 int nr_reads = 0;
2484 int ret = 0;
2485 int is_mapped_to_disk = 1;
2486
2487 index = pos >> PAGE_CACHE_SHIFT;
2488 from = pos & (PAGE_CACHE_SIZE - 1);
2489 to = from + len;
2490
2491 page = grab_cache_page_write_begin(mapping, index, flags);
2492 if (!page)
2493 return -ENOMEM;
2494 *pagep = page;
2495 *fsdata = NULL;
2496
2497 if (page_has_buffers(page)) {
2498 unlock_page(page);
2499 page_cache_release(page);
2500 *pagep = NULL;
2501 return block_write_begin(file, mapping, pos, len, flags, pagep,
2502 fsdata, get_block);
2503 }
2504
2505 if (PageMappedToDisk(page))
2506 return 0;
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517 head = alloc_page_buffers(page, blocksize, 0);
2518 if (!head) {
2519 ret = -ENOMEM;
2520 goto out_release;
2521 }
2522
2523 block_in_file = (sector_t)page->index << (PAGE_CACHE_SHIFT - blkbits);
2524
2525
2526
2527
2528
2529
2530 for (block_start = 0, block_in_page = 0, bh = head;
2531 block_start < PAGE_CACHE_SIZE;
2532 block_in_page++, block_start += blocksize, bh = bh->b_this_page) {
2533 int create;
2534
2535 block_end = block_start + blocksize;
2536 bh->b_state = 0;
2537 create = 1;
2538 if (block_start >= to)
2539 create = 0;
2540 ret = get_block(inode, block_in_file + block_in_page,
2541 bh, create);
2542 if (ret)
2543 goto failed;
2544 if (!buffer_mapped(bh))
2545 is_mapped_to_disk = 0;
2546 if (buffer_new(bh))
2547 unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);
2548 if (PageUptodate(page)) {
2549 set_buffer_uptodate(bh);
2550 continue;
2551 }
2552 if (buffer_new(bh) || !buffer_mapped(bh)) {
2553 zero_user_segments(page, block_start, from,
2554 to, block_end);
2555 continue;
2556 }
2557 if (buffer_uptodate(bh))
2558 continue;
2559 if (block_start < from || block_end > to) {
2560 lock_buffer(bh);
2561 bh->b_end_io = end_buffer_read_nobh;
2562 submit_bh(READ, bh);
2563 nr_reads++;
2564 }
2565 }
2566
2567 if (nr_reads) {
2568
2569
2570
2571
2572
2573 for (bh = head; bh; bh = bh->b_this_page) {
2574 wait_on_buffer(bh);
2575 if (!buffer_uptodate(bh))
2576 ret = -EIO;
2577 }
2578 if (ret)
2579 goto failed;
2580 }
2581
2582 if (is_mapped_to_disk)
2583 SetPageMappedToDisk(page);
2584
2585 *fsdata = head;
2586
2587 return 0;
2588
2589failed:
2590 BUG_ON(!ret);
2591
2592
2593
2594
2595
2596
2597
2598 attach_nobh_buffers(page, head);
2599 page_zero_new_buffers(page, from, to);
2600
2601out_release:
2602 unlock_page(page);
2603 page_cache_release(page);
2604 *pagep = NULL;
2605
2606 if (pos + len > inode->i_size)
2607 vmtruncate(inode, inode->i_size);
2608
2609 return ret;
2610}
2611EXPORT_SYMBOL(nobh_write_begin);
2612
2613int nobh_write_end(struct file *file, struct address_space *mapping,
2614 loff_t pos, unsigned len, unsigned copied,
2615 struct page *page, void *fsdata)
2616{
2617 struct inode *inode = page->mapping->host;
2618 struct buffer_head *head = fsdata;
2619 struct buffer_head *bh;
2620 BUG_ON(fsdata != NULL && page_has_buffers(page));
2621
2622 if (unlikely(copied < len) && head)
2623 attach_nobh_buffers(page, head);
2624 if (page_has_buffers(page))
2625 return generic_write_end(file, mapping, pos, len,
2626 copied, page, fsdata);
2627
2628 SetPageUptodate(page);
2629 set_page_dirty(page);
2630 if (pos+copied > inode->i_size) {
2631 i_size_write(inode, pos+copied);
2632 mark_inode_dirty(inode);
2633 }
2634
2635 unlock_page(page);
2636 page_cache_release(page);
2637
2638 while (head) {
2639 bh = head;
2640 head = head->b_this_page;
2641 free_buffer_head(bh);
2642 }
2643
2644 return copied;
2645}
2646EXPORT_SYMBOL(nobh_write_end);
2647
2648
2649
2650
2651
2652
2653int nobh_writepage(struct page *page, get_block_t *get_block,
2654 struct writeback_control *wbc)
2655{
2656 struct inode * const inode = page->mapping->host;
2657 loff_t i_size = i_size_read(inode);
2658 const pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
2659 unsigned offset;
2660 int ret;
2661
2662
2663 if (page->index < end_index)
2664 goto out;
2665
2666
2667 offset = i_size & (PAGE_CACHE_SIZE-1);
2668 if (page->index >= end_index+1 || !offset) {
2669
2670
2671
2672
2673
2674#if 0
2675
2676 if (page->mapping->a_ops->invalidatepage)
2677 page->mapping->a_ops->invalidatepage(page, offset);
2678#endif
2679 unlock_page(page);
2680 return 0;
2681 }
2682
2683
2684
2685
2686
2687
2688
2689
2690 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
2691out:
2692 ret = mpage_writepage(page, get_block, wbc);
2693 if (ret == -EAGAIN)
2694 ret = __block_write_full_page(inode, page, get_block, wbc,
2695 end_buffer_async_write);
2696 return ret;
2697}
2698EXPORT_SYMBOL(nobh_writepage);
2699
2700int nobh_truncate_page(struct address_space *mapping,
2701 loff_t from, get_block_t *get_block)
2702{
2703 pgoff_t index = from >> PAGE_CACHE_SHIFT;
2704 unsigned offset = from & (PAGE_CACHE_SIZE-1);
2705 unsigned blocksize;
2706 sector_t iblock;
2707 unsigned length, pos;
2708 struct inode *inode = mapping->host;
2709 struct page *page;
2710 struct buffer_head map_bh;
2711 int err;
2712
2713 blocksize = 1 << inode->i_blkbits;
2714 length = offset & (blocksize - 1);
2715
2716
2717 if (!length)
2718 return 0;
2719
2720 length = blocksize - length;
2721 iblock = (sector_t)index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
2722
2723 page = grab_cache_page(mapping, index);
2724 err = -ENOMEM;
2725 if (!page)
2726 goto out;
2727
2728 if (page_has_buffers(page)) {
2729has_buffers:
2730 unlock_page(page);
2731 page_cache_release(page);
2732 return block_truncate_page(mapping, from, get_block);
2733 }
2734
2735
2736 pos = blocksize;
2737 while (offset >= pos) {
2738 iblock++;
2739 pos += blocksize;
2740 }
2741
2742 map_bh.b_size = blocksize;
2743 map_bh.b_state = 0;
2744 err = get_block(inode, iblock, &map_bh, 0);
2745 if (err)
2746 goto unlock;
2747
2748 if (!buffer_mapped(&map_bh))
2749 goto unlock;
2750
2751
2752 if (!PageUptodate(page)) {
2753 err = mapping->a_ops->readpage(NULL, page);
2754 if (err) {
2755 page_cache_release(page);
2756 goto out;
2757 }
2758 lock_page(page);
2759 if (!PageUptodate(page)) {
2760 err = -EIO;
2761 goto unlock;
2762 }
2763 if (page_has_buffers(page))
2764 goto has_buffers;
2765 }
2766 zero_user(page, offset, length);
2767 set_page_dirty(page);
2768 err = 0;
2769
2770unlock:
2771 unlock_page(page);
2772 page_cache_release(page);
2773out:
2774 return err;
2775}
2776EXPORT_SYMBOL(nobh_truncate_page);
2777
2778int block_truncate_page(struct address_space *mapping,
2779 loff_t from, get_block_t *get_block)
2780{
2781 pgoff_t index = from >> PAGE_CACHE_SHIFT;
2782 unsigned offset = from & (PAGE_CACHE_SIZE-1);
2783 unsigned blocksize;
2784 sector_t iblock;
2785 unsigned length, pos;
2786 struct inode *inode = mapping->host;
2787 struct page *page;
2788 struct buffer_head *bh;
2789 int err;
2790
2791 blocksize = 1 << inode->i_blkbits;
2792 length = offset & (blocksize - 1);
2793
2794
2795 if (!length)
2796 return 0;
2797
2798 length = blocksize - length;
2799 iblock = (sector_t)index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
2800
2801 page = grab_cache_page(mapping, index);
2802 err = -ENOMEM;
2803 if (!page)
2804 goto out;
2805
2806 if (!page_has_buffers(page))
2807 create_empty_buffers(page, blocksize, 0);
2808
2809
2810 bh = page_buffers(page);
2811 pos = blocksize;
2812 while (offset >= pos) {
2813 bh = bh->b_this_page;
2814 iblock++;
2815 pos += blocksize;
2816 }
2817
2818 err = 0;
2819 if (!buffer_mapped(bh)) {
2820 WARN_ON(bh->b_size != blocksize);
2821 err = get_block(inode, iblock, bh, 0);
2822 if (err)
2823 goto unlock;
2824
2825 if (!buffer_mapped(bh))
2826 goto unlock;
2827 }
2828
2829
2830 if (PageUptodate(page))
2831 set_buffer_uptodate(bh);
2832
2833 if (!buffer_uptodate(bh) && !buffer_delay(bh) && !buffer_unwritten(bh)) {
2834 err = -EIO;
2835 ll_rw_block(READ, 1, &bh);
2836 wait_on_buffer(bh);
2837
2838 if (!buffer_uptodate(bh))
2839 goto unlock;
2840 }
2841
2842 zero_user(page, offset, length);
2843 mark_buffer_dirty(bh);
2844 err = 0;
2845
2846unlock:
2847 unlock_page(page);
2848 page_cache_release(page);
2849out:
2850 return err;
2851}
2852
2853
2854
2855
2856
2857int block_write_full_page_endio(struct page *page, get_block_t *get_block,
2858 struct writeback_control *wbc, bh_end_io_t *handler)
2859{
2860 struct inode * const inode = page->mapping->host;
2861 loff_t i_size = i_size_read(inode);
2862 const pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
2863 unsigned offset;
2864
2865
2866 if (page->index < end_index)
2867 return __block_write_full_page(inode, page, get_block, wbc,
2868 handler);
2869
2870
2871 offset = i_size & (PAGE_CACHE_SIZE-1);
2872 if (page->index >= end_index+1 || !offset) {
2873
2874
2875
2876
2877
2878 do_invalidatepage(page, 0);
2879 unlock_page(page);
2880 return 0;
2881 }
2882
2883
2884
2885
2886
2887
2888
2889
2890 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
2891 return __block_write_full_page(inode, page, get_block, wbc, handler);
2892}
2893
2894
2895
2896
2897int block_write_full_page(struct page *page, get_block_t *get_block,
2898 struct writeback_control *wbc)
2899{
2900 return block_write_full_page_endio(page, get_block, wbc,
2901 end_buffer_async_write);
2902}
2903
2904
2905sector_t generic_block_bmap(struct address_space *mapping, sector_t block,
2906 get_block_t *get_block)
2907{
2908 struct buffer_head tmp;
2909 struct inode *inode = mapping->host;
2910 tmp.b_state = 0;
2911 tmp.b_blocknr = 0;
2912 tmp.b_size = 1 << inode->i_blkbits;
2913 get_block(inode, block, &tmp, 0);
2914 return tmp.b_blocknr;
2915}
2916
2917static void end_bio_bh_io_sync(struct bio *bio, int err)
2918{
2919 struct buffer_head *bh = bio->bi_private;
2920
2921 if (err == -EOPNOTSUPP) {
2922 set_bit(BIO_EOPNOTSUPP, &bio->bi_flags);
2923 set_bit(BH_Eopnotsupp, &bh->b_state);
2924 }
2925
2926 if (unlikely (test_bit(BIO_QUIET,&bio->bi_flags)))
2927 set_bit(BH_Quiet, &bh->b_state);
2928
2929 bh->b_end_io(bh, test_bit(BIO_UPTODATE, &bio->bi_flags));
2930 bio_put(bio);
2931}
2932
2933int submit_bh(int rw, struct buffer_head * bh)
2934{
2935 struct bio *bio;
2936 int ret = 0;
2937
2938 BUG_ON(!buffer_locked(bh));
2939 BUG_ON(!buffer_mapped(bh));
2940 BUG_ON(!bh->b_end_io);
2941 BUG_ON(buffer_delay(bh));
2942 BUG_ON(buffer_unwritten(bh));
2943
2944
2945
2946
2947
2948 if (buffer_ordered(bh) && (rw & WRITE))
2949 rw |= WRITE_BARRIER;
2950
2951
2952
2953
2954 if (test_set_buffer_req(bh) && (rw & WRITE))
2955 clear_buffer_write_io_error(bh);
2956
2957
2958
2959
2960
2961 bio = bio_alloc(GFP_NOIO, 1);
2962
2963 bio->bi_sector = bh->b_blocknr * (bh->b_size >> 9);
2964 bio->bi_bdev = bh->b_bdev;
2965 bio->bi_io_vec[0].bv_page = bh->b_page;
2966 bio->bi_io_vec[0].bv_len = bh->b_size;
2967 bio->bi_io_vec[0].bv_offset = bh_offset(bh);
2968
2969 bio->bi_vcnt = 1;
2970 bio->bi_idx = 0;
2971 bio->bi_size = bh->b_size;
2972
2973 bio->bi_end_io = end_bio_bh_io_sync;
2974 bio->bi_private = bh;
2975
2976 bio_get(bio);
2977 submit_bio(rw, bio);
2978
2979 if (bio_flagged(bio, BIO_EOPNOTSUPP))
2980 ret = -EOPNOTSUPP;
2981
2982 bio_put(bio);
2983 return ret;
2984}
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012void ll_rw_block(int rw, int nr, struct buffer_head *bhs[])
3013{
3014 int i;
3015
3016 for (i = 0; i < nr; i++) {
3017 struct buffer_head *bh = bhs[i];
3018
3019 if (rw == SWRITE || rw == SWRITE_SYNC || rw == SWRITE_SYNC_PLUG)
3020 lock_buffer(bh);
3021 else if (!trylock_buffer(bh))
3022 continue;
3023
3024 if (rw == WRITE || rw == SWRITE || rw == SWRITE_SYNC ||
3025 rw == SWRITE_SYNC_PLUG) {
3026 if (test_clear_buffer_dirty(bh)) {
3027 bh->b_end_io = end_buffer_write_sync;
3028 get_bh(bh);
3029 if (rw == SWRITE_SYNC)
3030 submit_bh(WRITE_SYNC, bh);
3031 else
3032 submit_bh(WRITE, bh);
3033 continue;
3034 }
3035 } else {
3036 if (!buffer_uptodate(bh)) {
3037 bh->b_end_io = end_buffer_read_sync;
3038 get_bh(bh);
3039 submit_bh(rw, bh);
3040 continue;
3041 }
3042 }
3043 unlock_buffer(bh);
3044 }
3045}
3046
3047
3048
3049
3050
3051
3052int sync_dirty_buffer(struct buffer_head *bh)
3053{
3054 int ret = 0;
3055
3056 WARN_ON(atomic_read(&bh->b_count) < 1);
3057 lock_buffer(bh);
3058 if (test_clear_buffer_dirty(bh)) {
3059 get_bh(bh);
3060 bh->b_end_io = end_buffer_write_sync;
3061 ret = submit_bh(WRITE_SYNC, bh);
3062 wait_on_buffer(bh);
3063 if (buffer_eopnotsupp(bh)) {
3064 clear_buffer_eopnotsupp(bh);
3065 ret = -EOPNOTSUPP;
3066 }
3067 if (!ret && !buffer_uptodate(bh))
3068 ret = -EIO;
3069 } else {
3070 unlock_buffer(bh);
3071 }
3072 return ret;
3073}
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095static inline int buffer_busy(struct buffer_head *bh)
3096{
3097 return atomic_read(&bh->b_count) |
3098 (bh->b_state & ((1 << BH_Dirty) | (1 << BH_Lock)));
3099}
3100
3101static int
3102drop_buffers(struct page *page, struct buffer_head **buffers_to_free)
3103{
3104 struct buffer_head *head = page_buffers(page);
3105 struct buffer_head *bh;
3106
3107 bh = head;
3108 do {
3109 if (buffer_write_io_error(bh) && page->mapping)
3110 set_bit(AS_EIO, &page->mapping->flags);
3111 if (buffer_busy(bh))
3112 goto failed;
3113 bh = bh->b_this_page;
3114 } while (bh != head);
3115
3116 do {
3117 struct buffer_head *next = bh->b_this_page;
3118
3119 if (bh->b_assoc_map)
3120 __remove_assoc_queue(bh);
3121 bh = next;
3122 } while (bh != head);
3123 *buffers_to_free = head;
3124 __clear_page_buffers(page);
3125 return 1;
3126failed:
3127 return 0;
3128}
3129
3130int try_to_free_buffers(struct page *page)
3131{
3132 struct address_space * const mapping = page->mapping;
3133 struct buffer_head *buffers_to_free = NULL;
3134 int ret = 0;
3135
3136 BUG_ON(!PageLocked(page));
3137 if (PageWriteback(page))
3138 return 0;
3139
3140 if (mapping == NULL) {
3141 ret = drop_buffers(page, &buffers_to_free);
3142 goto out;
3143 }
3144
3145 spin_lock(&mapping->private_lock);
3146 ret = drop_buffers(page, &buffers_to_free);
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162 if (ret)
3163 cancel_dirty_page(page, PAGE_CACHE_SIZE);
3164 spin_unlock(&mapping->private_lock);
3165out:
3166 if (buffers_to_free) {
3167 struct buffer_head *bh = buffers_to_free;
3168
3169 do {
3170 struct buffer_head *next = bh->b_this_page;
3171 free_buffer_head(bh);
3172 bh = next;
3173 } while (bh != buffers_to_free);
3174 }
3175 return ret;
3176}
3177EXPORT_SYMBOL(try_to_free_buffers);
3178
3179void block_sync_page(struct page *page)
3180{
3181 struct address_space *mapping;
3182
3183 smp_mb();
3184 mapping = page_mapping(page);
3185 if (mapping)
3186 blk_run_backing_dev(mapping->backing_dev_info, page);
3187}
3188
3189
3190
3191
3192
3193
3194
3195
3196SYSCALL_DEFINE2(bdflush, int, func, long, data)
3197{
3198 static int msg_count;
3199
3200 if (!capable(CAP_SYS_ADMIN))
3201 return -EPERM;
3202
3203 if (msg_count < 5) {
3204 msg_count++;
3205 printk(KERN_INFO
3206 "warning: process `%s' used the obsolete bdflush"
3207 " system call\n", current->comm);
3208 printk(KERN_INFO "Fix your initscripts?\n");
3209 }
3210
3211 if (func == 1)
3212 do_exit(0);
3213 return 0;
3214}
3215
3216
3217
3218
3219static struct kmem_cache *bh_cachep;
3220
3221
3222
3223
3224
3225static int max_buffer_heads;
3226
3227int buffer_heads_over_limit;
3228
3229struct bh_accounting {
3230 int nr;
3231 int ratelimit;
3232};
3233
3234static DEFINE_PER_CPU(struct bh_accounting, bh_accounting) = {0, 0};
3235
3236static void recalc_bh_state(void)
3237{
3238 int i;
3239 int tot = 0;
3240
3241 if (__get_cpu_var(bh_accounting).ratelimit++ < 4096)
3242 return;
3243 __get_cpu_var(bh_accounting).ratelimit = 0;
3244 for_each_online_cpu(i)
3245 tot += per_cpu(bh_accounting, i).nr;
3246 buffer_heads_over_limit = (tot > max_buffer_heads);
3247}
3248
3249struct buffer_head *alloc_buffer_head(gfp_t gfp_flags)
3250{
3251 struct buffer_head *ret = kmem_cache_alloc(bh_cachep, gfp_flags);
3252 if (ret) {
3253 INIT_LIST_HEAD(&ret->b_assoc_buffers);
3254 get_cpu_var(bh_accounting).nr++;
3255 recalc_bh_state();
3256 put_cpu_var(bh_accounting);
3257 }
3258 return ret;
3259}
3260EXPORT_SYMBOL(alloc_buffer_head);
3261
3262void free_buffer_head(struct buffer_head *bh)
3263{
3264 BUG_ON(!list_empty(&bh->b_assoc_buffers));
3265 kmem_cache_free(bh_cachep, bh);
3266 get_cpu_var(bh_accounting).nr--;
3267 recalc_bh_state();
3268 put_cpu_var(bh_accounting);
3269}
3270EXPORT_SYMBOL(free_buffer_head);
3271
3272static void buffer_exit_cpu(int cpu)
3273{
3274 int i;
3275 struct bh_lru *b = &per_cpu(bh_lrus, cpu);
3276
3277 for (i = 0; i < BH_LRU_SIZE; i++) {
3278 brelse(b->bhs[i]);
3279 b->bhs[i] = NULL;
3280 }
3281 get_cpu_var(bh_accounting).nr += per_cpu(bh_accounting, cpu).nr;
3282 per_cpu(bh_accounting, cpu).nr = 0;
3283 put_cpu_var(bh_accounting);
3284}
3285
3286static int buffer_cpu_notify(struct notifier_block *self,
3287 unsigned long action, void *hcpu)
3288{
3289 if (action == CPU_DEAD || action == CPU_DEAD_FROZEN)
3290 buffer_exit_cpu((unsigned long)hcpu);
3291 return NOTIFY_OK;
3292}
3293
3294
3295
3296
3297
3298
3299
3300
3301int bh_uptodate_or_lock(struct buffer_head *bh)
3302{
3303 if (!buffer_uptodate(bh)) {
3304 lock_buffer(bh);
3305 if (!buffer_uptodate(bh))
3306 return 0;
3307 unlock_buffer(bh);
3308 }
3309 return 1;
3310}
3311EXPORT_SYMBOL(bh_uptodate_or_lock);
3312
3313
3314
3315
3316
3317
3318
3319int bh_submit_read(struct buffer_head *bh)
3320{
3321 BUG_ON(!buffer_locked(bh));
3322
3323 if (buffer_uptodate(bh)) {
3324 unlock_buffer(bh);
3325 return 0;
3326 }
3327
3328 get_bh(bh);
3329 bh->b_end_io = end_buffer_read_sync;
3330 submit_bh(READ, bh);
3331 wait_on_buffer(bh);
3332 if (buffer_uptodate(bh))
3333 return 0;
3334 return -EIO;
3335}
3336EXPORT_SYMBOL(bh_submit_read);
3337
3338static void
3339init_buffer_head(void *data)
3340{
3341 struct buffer_head *bh = data;
3342
3343 memset(bh, 0, sizeof(*bh));
3344 INIT_LIST_HEAD(&bh->b_assoc_buffers);
3345}
3346
3347void __init buffer_init(void)
3348{
3349 int nrpages;
3350
3351 bh_cachep = kmem_cache_create("buffer_head",
3352 sizeof(struct buffer_head), 0,
3353 (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
3354 SLAB_MEM_SPREAD),
3355 init_buffer_head);
3356
3357
3358
3359
3360 nrpages = (nr_free_buffer_pages() * 10) / 100;
3361 max_buffer_heads = nrpages * (PAGE_SIZE / sizeof(struct buffer_head));
3362 hotcpu_notifier(buffer_cpu_notify, 0);
3363}
3364
3365EXPORT_SYMBOL(__bforget);
3366EXPORT_SYMBOL(__brelse);
3367EXPORT_SYMBOL(__wait_on_buffer);
3368EXPORT_SYMBOL(block_commit_write);
3369EXPORT_SYMBOL(block_prepare_write);
3370EXPORT_SYMBOL(block_page_mkwrite);
3371EXPORT_SYMBOL(block_read_full_page);
3372EXPORT_SYMBOL(block_sync_page);
3373EXPORT_SYMBOL(block_truncate_page);
3374EXPORT_SYMBOL(block_write_full_page);
3375EXPORT_SYMBOL(block_write_full_page_endio);
3376EXPORT_SYMBOL(cont_write_begin);
3377EXPORT_SYMBOL(end_buffer_read_sync);
3378EXPORT_SYMBOL(end_buffer_write_sync);
3379EXPORT_SYMBOL(end_buffer_async_write);
3380EXPORT_SYMBOL(file_fsync);
3381EXPORT_SYMBOL(generic_block_bmap);
3382EXPORT_SYMBOL(generic_cont_expand_simple);
3383EXPORT_SYMBOL(init_buffer);
3384EXPORT_SYMBOL(invalidate_bdev);
3385EXPORT_SYMBOL(ll_rw_block);
3386EXPORT_SYMBOL(mark_buffer_dirty);
3387EXPORT_SYMBOL(submit_bh);
3388EXPORT_SYMBOL(sync_dirty_buffer);
3389EXPORT_SYMBOL(unlock_buffer);
3390