00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <linux/kernel.h>
00022 #include <linux/syscalls.h>
00023 #include <linux/fs.h>
00024 #include <linux/mm.h>
00025 #include <linux/percpu.h>
00026 #include <linux/slab.h>
00027 #include <linux/capability.h>
00028 #include <linux/blkdev.h>
00029 #include <linux/file.h>
00030 #include <linux/quotaops.h>
00031 #include <linux/highmem.h>
00032 #include <linux/module.h>
00033 #include <linux/writeback.h>
00034 #include <linux/hash.h>
00035 #include <linux/suspend.h>
00036 #include <linux/buffer_head.h>
00037 #include <linux/task_io_accounting_ops.h>
00038 #include <linux/bio.h>
00039 #include <linux/notifier.h>
00040 #include <linux/cpu.h>
00041 #include <linux/bitops.h>
00042 #include <linux/mpage.h>
00043 #include <linux/bit_spinlock.h>
00044
00045 static int fsync_buffers_list(spinlock_t *lock, struct list_head *list);
00046
00047 #define BH_ENTRY(list) list_entry((list), struct buffer_head, b_assoc_buffers)
00048
00049 inline void
00050 init_buffer(struct buffer_head *bh, bh_end_io_t *handler, void *private)
00051 {
00052 bh->b_end_io = handler;
00053 bh->b_private = private;
00054 }
00055
00056 static int sync_buffer(void *word)
00057 {
00058 struct block_device *bd;
00059 struct buffer_head *bh
00060 = container_of(word, struct buffer_head, b_state);
00061
00062 smp_mb();
00063 bd = bh->b_bdev;
00064 if (bd)
00065 blk_run_address_space(bd->bd_inode->i_mapping);
00066 io_schedule();
00067 return 0;
00068 }
00069
00070 void __lock_buffer(struct buffer_head *bh)
00071 {
00072 wait_on_bit_lock(&bh->b_state, BH_Lock, sync_buffer,
00073 TASK_UNINTERRUPTIBLE);
00074 }
00075 EXPORT_SYMBOL(__lock_buffer);
00076
00077 void unlock_buffer(struct buffer_head *bh)
00078 {
00079 clear_bit_unlock(BH_Lock, &bh->b_state);
00080 smp_mb__after_clear_bit();
00081 wake_up_bit(&bh->b_state, BH_Lock);
00082 }
00083
00084
00085
00086
00087
00088
00089 void __wait_on_buffer(struct buffer_head * bh)
00090 {
00091 wait_on_bit(&bh->b_state, BH_Lock, sync_buffer, TASK_UNINTERRUPTIBLE);
00092 }
00093
00094 static void
00095 __clear_page_buffers(struct page *page)
00096 {
00097 ClearPagePrivate(page);
00098 set_page_private(page, 0);
00099 page_cache_release(page);
00100 }
00101
00102
00103 static int quiet_error(struct buffer_head *bh)
00104 {
00105 if (!test_bit(BH_Quiet, &bh->b_state) && printk_ratelimit())
00106 return 0;
00107 return 1;
00108 }
00109
00110
00111 static void buffer_io_error(struct buffer_head *bh)
00112 {
00113 char b[BDEVNAME_SIZE];
00114 printk(KERN_ERR "Buffer I/O error on device %s, logical block %Lu\n",
00115 bdevname(bh->b_bdev, b),
00116 (unsigned long long)bh->b_blocknr);
00117 }
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127 static void __end_buffer_read_notouch(struct buffer_head *bh, int uptodate)
00128 {
00129 if (uptodate) {
00130 set_buffer_uptodate(bh);
00131 } else {
00132
00133 clear_buffer_uptodate(bh);
00134 }
00135 unlock_buffer(bh);
00136 }
00137
00138
00139
00140
00141
00142 void end_buffer_read_sync(struct buffer_head *bh, int uptodate)
00143 {
00144 __end_buffer_read_notouch(bh, uptodate);
00145 put_bh(bh);
00146 }
00147
00148 void end_buffer_write_sync(struct buffer_head *bh, int uptodate)
00149 {
00150 char b[BDEVNAME_SIZE];
00151
00152 if (uptodate) {
00153 set_buffer_uptodate(bh);
00154 } else {
00155 if (!buffer_eopnotsupp(bh) && !quiet_error(bh)) {
00156 buffer_io_error(bh);
00157 printk(KERN_WARNING "lost page write due to "
00158 "I/O error on %s\n",
00159 bdevname(bh->b_bdev, b));
00160 }
00161 set_buffer_write_io_error(bh);
00162 clear_buffer_uptodate(bh);
00163 }
00164 unlock_buffer(bh);
00165 put_bh(bh);
00166 }
00167
00168
00169
00170
00171
00172 int sync_blockdev(struct block_device *bdev)
00173 {
00174 #ifndef DDE_LINUX
00175 int ret = 0;
00176
00177 if (bdev)
00178 ret = filemap_write_and_wait(bdev->bd_inode->i_mapping);
00179 return ret;
00180 #else
00181 WARN_UNIMPL;
00182 return 0;
00183 #endif
00184 }
00185 EXPORT_SYMBOL(sync_blockdev);
00186
00187
00188
00189
00190
00191
00192 int fsync_bdev(struct block_device *bdev)
00193 {
00194 #ifndef DDE_LINUX
00195 struct super_block *sb = get_super(bdev);
00196 if (sb) {
00197 int res = fsync_super(sb);
00198 drop_super(sb);
00199 return res;
00200 }
00201 return sync_blockdev(bdev);
00202 #else
00203 WARN_UNIMPL;
00204 return -1;
00205 #endif
00206 }
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222 struct super_block *freeze_bdev(struct block_device *bdev)
00223 {
00224 struct super_block *sb;
00225 int error = 0;
00226
00227 mutex_lock(&bdev->bd_fsfreeze_mutex);
00228 if (bdev->bd_fsfreeze_count > 0) {
00229 bdev->bd_fsfreeze_count++;
00230 sb = get_super(bdev);
00231 mutex_unlock(&bdev->bd_fsfreeze_mutex);
00232 return sb;
00233 }
00234 bdev->bd_fsfreeze_count++;
00235
00236 down(&bdev->bd_mount_sem);
00237 sb = get_super(bdev);
00238 if (sb && !(sb->s_flags & MS_RDONLY)) {
00239 sb->s_frozen = SB_FREEZE_WRITE;
00240 smp_wmb();
00241
00242 __fsync_super(sb);
00243
00244 sb->s_frozen = SB_FREEZE_TRANS;
00245 smp_wmb();
00246
00247 sync_blockdev(sb->s_bdev);
00248
00249 if (sb->s_op->freeze_fs) {
00250 error = sb->s_op->freeze_fs(sb);
00251 if (error) {
00252 printk(KERN_ERR
00253 "VFS:Filesystem freeze failed\n");
00254 sb->s_frozen = SB_UNFROZEN;
00255 drop_super(sb);
00256 up(&bdev->bd_mount_sem);
00257 bdev->bd_fsfreeze_count--;
00258 mutex_unlock(&bdev->bd_fsfreeze_mutex);
00259 return ERR_PTR(error);
00260 }
00261 }
00262 }
00263
00264 sync_blockdev(bdev);
00265 mutex_unlock(&bdev->bd_fsfreeze_mutex);
00266
00267 return sb;
00268 }
00269 EXPORT_SYMBOL(freeze_bdev);
00270
00271
00272
00273
00274
00275
00276
00277
00278 int thaw_bdev(struct block_device *bdev, struct super_block *sb)
00279 {
00280 int error = 0;
00281
00282 mutex_lock(&bdev->bd_fsfreeze_mutex);
00283 if (!bdev->bd_fsfreeze_count) {
00284 mutex_unlock(&bdev->bd_fsfreeze_mutex);
00285 return -EINVAL;
00286 }
00287
00288 bdev->bd_fsfreeze_count--;
00289 if (bdev->bd_fsfreeze_count > 0) {
00290 if (sb)
00291 drop_super(sb);
00292 mutex_unlock(&bdev->bd_fsfreeze_mutex);
00293 return 0;
00294 }
00295
00296 if (sb) {
00297 BUG_ON(sb->s_bdev != bdev);
00298 if (!(sb->s_flags & MS_RDONLY)) {
00299 if (sb->s_op->unfreeze_fs) {
00300 error = sb->s_op->unfreeze_fs(sb);
00301 if (error) {
00302 printk(KERN_ERR
00303 "VFS:Filesystem thaw failed\n");
00304 sb->s_frozen = SB_FREEZE_TRANS;
00305 bdev->bd_fsfreeze_count++;
00306 mutex_unlock(&bdev->bd_fsfreeze_mutex);
00307 return error;
00308 }
00309 }
00310 sb->s_frozen = SB_UNFROZEN;
00311 smp_wmb();
00312 wake_up(&sb->s_wait_unfrozen);
00313 }
00314 drop_super(sb);
00315 }
00316
00317 up(&bdev->bd_mount_sem);
00318 mutex_unlock(&bdev->bd_fsfreeze_mutex);
00319 return 0;
00320 }
00321 EXPORT_SYMBOL(thaw_bdev);
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334 static struct buffer_head *
00335 __find_get_block_slow(struct block_device *bdev, sector_t block)
00336 {
00337 struct inode *bd_inode = bdev->bd_inode;
00338 struct address_space *bd_mapping = bd_inode->i_mapping;
00339 struct buffer_head *ret = NULL;
00340 pgoff_t index;
00341 struct buffer_head *bh;
00342 struct buffer_head *head;
00343 struct page *page;
00344 int all_mapped = 1;
00345
00346 index = block >> (PAGE_CACHE_SHIFT - bd_inode->i_blkbits);
00347 page = find_get_page(bd_mapping, index);
00348 if (!page)
00349 goto out;
00350
00351 spin_lock(&bd_mapping->private_lock);
00352 if (!page_has_buffers(page))
00353 goto out_unlock;
00354 head = page_buffers(page);
00355 bh = head;
00356 do {
00357 if (bh->b_blocknr == block) {
00358 ret = bh;
00359 get_bh(bh);
00360 goto out_unlock;
00361 }
00362 if (!buffer_mapped(bh))
00363 all_mapped = 0;
00364 bh = bh->b_this_page;
00365 } while (bh != head);
00366
00367
00368
00369
00370
00371
00372 if (all_mapped) {
00373 printk("__find_get_block_slow() failed. "
00374 "block=%llu, b_blocknr=%llu\n",
00375 (unsigned long long)block,
00376 (unsigned long long)bh->b_blocknr);
00377 printk("b_state=0x%08lx, b_size=%zu\n",
00378 bh->b_state, bh->b_size);
00379 printk("device blocksize: %d\n", 1 << bd_inode->i_blkbits);
00380 }
00381 out_unlock:
00382 spin_unlock(&bd_mapping->private_lock);
00383 page_cache_release(page);
00384 out:
00385 return ret;
00386 }
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420 void invalidate_bdev(struct block_device *bdev)
00421 {
00422 struct address_space *mapping = bdev->bd_inode->i_mapping;
00423
00424 if (mapping->nrpages == 0)
00425 return;
00426
00427 #ifndef DDE_LINUX
00428 invalidate_bh_lrus();
00429 invalidate_mapping_pages(mapping, 0, -1);
00430 #endif
00431 }
00432
00433
00434
00435
00436 static void free_more_memory(void)
00437 {
00438 struct zone *zone;
00439 int nid;
00440
00441 #ifndef DDE_LINUX
00442 wakeup_pdflush(1024);
00443 yield();
00444
00445 for_each_online_node(nid) {
00446 (void)first_zones_zonelist(node_zonelist(nid, GFP_NOFS),
00447 gfp_zone(GFP_NOFS), NULL,
00448 &zone);
00449 if (zone)
00450 try_to_free_pages(node_zonelist(nid, GFP_NOFS), 0,
00451 GFP_NOFS);
00452 }
00453 #else
00454 WARN_UNIMPL;
00455 #endif
00456 }
00457
00458
00459
00460
00461
00462 static void end_buffer_async_read(struct buffer_head *bh, int uptodate)
00463 {
00464 unsigned long flags;
00465 struct buffer_head *first;
00466 struct buffer_head *tmp;
00467 struct page *page;
00468 int page_uptodate = 1;
00469
00470 BUG_ON(!buffer_async_read(bh));
00471
00472 page = bh->b_page;
00473 if (uptodate) {
00474 set_buffer_uptodate(bh);
00475 } else {
00476 clear_buffer_uptodate(bh);
00477 if (!quiet_error(bh))
00478 buffer_io_error(bh);
00479 SetPageError(page);
00480 }
00481
00482
00483
00484
00485
00486
00487 first = page_buffers(page);
00488 local_irq_save(flags);
00489 bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
00490 clear_buffer_async_read(bh);
00491 unlock_buffer(bh);
00492 tmp = bh;
00493 do {
00494 if (!buffer_uptodate(tmp))
00495 page_uptodate = 0;
00496 if (buffer_async_read(tmp)) {
00497 BUG_ON(!buffer_locked(tmp));
00498 goto still_busy;
00499 }
00500 tmp = tmp->b_this_page;
00501 } while (tmp != bh);
00502 bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
00503 local_irq_restore(flags);
00504
00505
00506
00507
00508
00509 if (page_uptodate && !PageError(page))
00510 SetPageUptodate(page);
00511 unlock_page(page);
00512 return;
00513
00514 still_busy:
00515 bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
00516 local_irq_restore(flags);
00517 return;
00518 }
00519
00520
00521
00522
00523
00524 static void end_buffer_async_write(struct buffer_head *bh, int uptodate)
00525 {
00526 char b[BDEVNAME_SIZE];
00527 unsigned long flags;
00528 struct buffer_head *first;
00529 struct buffer_head *tmp;
00530 struct page *page;
00531
00532 BUG_ON(!buffer_async_write(bh));
00533
00534 page = bh->b_page;
00535 if (uptodate) {
00536 set_buffer_uptodate(bh);
00537 } else {
00538 if (!quiet_error(bh)) {
00539 buffer_io_error(bh);
00540 printk(KERN_WARNING "lost page write due to "
00541 "I/O error on %s\n",
00542 bdevname(bh->b_bdev, b));
00543 }
00544 set_bit(AS_EIO, &page->mapping->flags);
00545 set_buffer_write_io_error(bh);
00546 clear_buffer_uptodate(bh);
00547 SetPageError(page);
00548 }
00549
00550 first = page_buffers(page);
00551 local_irq_save(flags);
00552 bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
00553
00554 clear_buffer_async_write(bh);
00555 unlock_buffer(bh);
00556 tmp = bh->b_this_page;
00557 while (tmp != bh) {
00558 if (buffer_async_write(tmp)) {
00559 BUG_ON(!buffer_locked(tmp));
00560 goto still_busy;
00561 }
00562 tmp = tmp->b_this_page;
00563 }
00564 bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
00565 local_irq_restore(flags);
00566 end_page_writeback(page);
00567 return;
00568
00569 still_busy:
00570 bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
00571 local_irq_restore(flags);
00572 return;
00573 }
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596 static void mark_buffer_async_read(struct buffer_head *bh)
00597 {
00598 bh->b_end_io = end_buffer_async_read;
00599 set_buffer_async_read(bh);
00600 }
00601
00602 void mark_buffer_async_write(struct buffer_head *bh)
00603 {
00604 bh->b_end_io = end_buffer_async_write;
00605 set_buffer_async_write(bh);
00606 }
00607 EXPORT_SYMBOL(mark_buffer_async_write);
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662 static void __remove_assoc_queue(struct buffer_head *bh)
00663 {
00664 list_del_init(&bh->b_assoc_buffers);
00665 WARN_ON(!bh->b_assoc_map);
00666 if (buffer_write_io_error(bh))
00667 set_bit(AS_EIO, &bh->b_assoc_map->flags);
00668 bh->b_assoc_map = NULL;
00669 }
00670
00671 int inode_has_buffers(struct inode *inode)
00672 {
00673 return !list_empty(&inode->i_data.private_list);
00674 }
00675
00676
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686 static int osync_buffers_list(spinlock_t *lock, struct list_head *list)
00687 {
00688 struct buffer_head *bh;
00689 struct list_head *p;
00690 int err = 0;
00691
00692 spin_lock(lock);
00693 repeat:
00694 list_for_each_prev(p, list) {
00695 bh = BH_ENTRY(p);
00696 if (buffer_locked(bh)) {
00697 get_bh(bh);
00698 spin_unlock(lock);
00699 wait_on_buffer(bh);
00700 if (!buffer_uptodate(bh))
00701 err = -EIO;
00702 brelse(bh);
00703 spin_lock(lock);
00704 goto repeat;
00705 }
00706 }
00707 spin_unlock(lock);
00708 return err;
00709 }
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722 int sync_mapping_buffers(struct address_space *mapping)
00723 {
00724 struct address_space *buffer_mapping = mapping->assoc_mapping;
00725
00726 if (buffer_mapping == NULL || list_empty(&mapping->private_list))
00727 return 0;
00728
00729 return fsync_buffers_list(&buffer_mapping->private_lock,
00730 &mapping->private_list);
00731 }
00732 EXPORT_SYMBOL(sync_mapping_buffers);
00733
00734
00735
00736
00737
00738
00739
00740 void write_boundary_block(struct block_device *bdev,
00741 sector_t bblock, unsigned blocksize)
00742 {
00743 struct buffer_head *bh = __find_get_block(bdev, bblock + 1, blocksize);
00744 if (bh) {
00745 if (buffer_dirty(bh))
00746 ll_rw_block(WRITE, 1, &bh);
00747 put_bh(bh);
00748 }
00749 }
00750
00751 void mark_buffer_dirty_inode(struct buffer_head *bh, struct inode *inode)
00752 {
00753 struct address_space *mapping = inode->i_mapping;
00754 struct address_space *buffer_mapping = bh->b_page->mapping;
00755
00756 mark_buffer_dirty(bh);
00757 if (!mapping->assoc_mapping) {
00758 mapping->assoc_mapping = buffer_mapping;
00759 } else {
00760 BUG_ON(mapping->assoc_mapping != buffer_mapping);
00761 }
00762 if (!bh->b_assoc_map) {
00763 spin_lock(&buffer_mapping->private_lock);
00764 list_move_tail(&bh->b_assoc_buffers,
00765 &mapping->private_list);
00766 bh->b_assoc_map = mapping;
00767 spin_unlock(&buffer_mapping->private_lock);
00768 }
00769 }
00770 EXPORT_SYMBOL(mark_buffer_dirty_inode);
00771
00772
00773
00774
00775
00776
00777
00778
00779 static void __set_page_dirty(struct page *page,
00780 struct address_space *mapping, int warn)
00781 {
00782 spin_lock_irq(&mapping->tree_lock);
00783 if (page->mapping) {
00784 WARN_ON_ONCE(warn && !PageUptodate(page));
00785
00786 if (mapping_cap_account_dirty(mapping)) {
00787 __inc_zone_page_state(page, NR_FILE_DIRTY);
00788 __inc_bdi_stat(mapping->backing_dev_info,
00789 BDI_RECLAIMABLE);
00790 task_dirty_inc(current);
00791 task_io_account_write(PAGE_CACHE_SIZE);
00792 }
00793 radix_tree_tag_set(&mapping->page_tree,
00794 page_index(page), PAGECACHE_TAG_DIRTY);
00795 }
00796 spin_unlock_irq(&mapping->tree_lock);
00797 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
00798 }
00799
00800
00801
00802
00803
00804
00805
00806
00807
00808
00809
00810
00811
00812
00813
00814
00815
00816
00817
00818
00819
00820
00821
00822
00823
00824
00825 int __set_page_dirty_buffers(struct page *page)
00826 {
00827 int newly_dirty;
00828 struct address_space *mapping = page_mapping(page);
00829
00830 if (unlikely(!mapping))
00831 return !TestSetPageDirty(page);
00832
00833 spin_lock(&mapping->private_lock);
00834 if (page_has_buffers(page)) {
00835 struct buffer_head *head = page_buffers(page);
00836 struct buffer_head *bh = head;
00837
00838 do {
00839 set_buffer_dirty(bh);
00840 bh = bh->b_this_page;
00841 } while (bh != head);
00842 }
00843 newly_dirty = !TestSetPageDirty(page);
00844 spin_unlock(&mapping->private_lock);
00845
00846 if (newly_dirty)
00847 __set_page_dirty(page, mapping, 1);
00848 return newly_dirty;
00849 }
00850 EXPORT_SYMBOL(__set_page_dirty_buffers);
00851
00852
00853
00854
00855
00856
00857
00858
00859
00860
00861
00862
00863
00864
00865
00866
00867
00868
00869
00870
00871 static int fsync_buffers_list(spinlock_t *lock, struct list_head *list)
00872 {
00873 struct buffer_head *bh;
00874 struct list_head tmp;
00875 struct address_space *mapping;
00876 int err = 0, err2;
00877
00878 INIT_LIST_HEAD(&tmp);
00879
00880 spin_lock(lock);
00881 while (!list_empty(list)) {
00882 bh = BH_ENTRY(list->next);
00883 mapping = bh->b_assoc_map;
00884 __remove_assoc_queue(bh);
00885
00886
00887 smp_mb();
00888 if (buffer_dirty(bh) || buffer_locked(bh)) {
00889 list_add(&bh->b_assoc_buffers, &tmp);
00890 bh->b_assoc_map = mapping;
00891 if (buffer_dirty(bh)) {
00892 get_bh(bh);
00893 spin_unlock(lock);
00894
00895
00896
00897
00898
00899
00900 ll_rw_block(SWRITE_SYNC, 1, &bh);
00901 brelse(bh);
00902 spin_lock(lock);
00903 }
00904 }
00905 }
00906
00907 while (!list_empty(&tmp)) {
00908 bh = BH_ENTRY(tmp.prev);
00909 get_bh(bh);
00910 mapping = bh->b_assoc_map;
00911 __remove_assoc_queue(bh);
00912
00913
00914 smp_mb();
00915 if (buffer_dirty(bh)) {
00916 list_add(&bh->b_assoc_buffers,
00917 &mapping->private_list);
00918 bh->b_assoc_map = mapping;
00919 }
00920 spin_unlock(lock);
00921 wait_on_buffer(bh);
00922 if (!buffer_uptodate(bh))
00923 err = -EIO;
00924 brelse(bh);
00925 spin_lock(lock);
00926 }
00927
00928 spin_unlock(lock);
00929 err2 = osync_buffers_list(lock, list);
00930 if (err)
00931 return err;
00932 else
00933 return err2;
00934 }
00935
00936
00937
00938
00939
00940
00941
00942
00943
00944
00945 void invalidate_inode_buffers(struct inode *inode)
00946 {
00947 if (inode_has_buffers(inode)) {
00948 struct address_space *mapping = &inode->i_data;
00949 struct list_head *list = &mapping->private_list;
00950 struct address_space *buffer_mapping = mapping->assoc_mapping;
00951
00952 spin_lock(&buffer_mapping->private_lock);
00953 while (!list_empty(list))
00954 __remove_assoc_queue(BH_ENTRY(list->next));
00955 spin_unlock(&buffer_mapping->private_lock);
00956 }
00957 }
00958 EXPORT_SYMBOL(invalidate_inode_buffers);
00959
00960
00961
00962
00963
00964
00965
00966 int remove_inode_buffers(struct inode *inode)
00967 {
00968 int ret = 1;
00969
00970 if (inode_has_buffers(inode)) {
00971 struct address_space *mapping = &inode->i_data;
00972 struct list_head *list = &mapping->private_list;
00973 struct address_space *buffer_mapping = mapping->assoc_mapping;
00974
00975 spin_lock(&buffer_mapping->private_lock);
00976 while (!list_empty(list)) {
00977 struct buffer_head *bh = BH_ENTRY(list->next);
00978 if (buffer_dirty(bh)) {
00979 ret = 0;
00980 break;
00981 }
00982 __remove_assoc_queue(bh);
00983 }
00984 spin_unlock(&buffer_mapping->private_lock);
00985 }
00986 return ret;
00987 }
00988
00989
00990
00991
00992
00993
00994
00995
00996
00997
00998 struct buffer_head *alloc_page_buffers(struct page *page, unsigned long size,
00999 int retry)
01000 {
01001 struct buffer_head *bh, *head;
01002 long offset;
01003
01004 try_again:
01005 head = NULL;
01006 offset = PAGE_SIZE;
01007 while ((offset -= size) >= 0) {
01008 bh = alloc_buffer_head(GFP_NOFS);
01009 if (!bh)
01010 goto no_grow;
01011
01012 bh->b_bdev = NULL;
01013 bh->b_this_page = head;
01014 bh->b_blocknr = -1;
01015 head = bh;
01016
01017 bh->b_state = 0;
01018 atomic_set(&bh->b_count, 0);
01019 bh->b_private = NULL;
01020 bh->b_size = size;
01021
01022
01023 set_bh_page(bh, page, offset);
01024
01025 init_buffer(bh, NULL, NULL);
01026 }
01027 return head;
01028
01029
01030
01031 no_grow:
01032 if (head) {
01033 do {
01034 bh = head;
01035 head = head->b_this_page;
01036 free_buffer_head(bh);
01037 } while (head);
01038 }
01039
01040
01041
01042
01043
01044
01045
01046 if (!retry)
01047 return NULL;
01048
01049
01050
01051
01052
01053
01054
01055 free_more_memory();
01056 goto try_again;
01057 }
01058 EXPORT_SYMBOL_GPL(alloc_page_buffers);
01059
01060 static inline void
01061 link_dev_buffers(struct page *page, struct buffer_head *head)
01062 {
01063 struct buffer_head *bh, *tail;
01064
01065 bh = head;
01066 do {
01067 tail = bh;
01068 bh = bh->b_this_page;
01069 } while (bh);
01070 tail->b_this_page = head;
01071 attach_page_buffers(page, head);
01072 }
01073
01074
01075
01076
01077 static void
01078 init_page_buffers(struct page *page, struct block_device *bdev,
01079 sector_t block, int size)
01080 {
01081 struct buffer_head *head = page_buffers(page);
01082 struct buffer_head *bh = head;
01083 int uptodate = PageUptodate(page);
01084
01085 do {
01086 if (!buffer_mapped(bh)) {
01087 init_buffer(bh, NULL, NULL);
01088 bh->b_bdev = bdev;
01089 bh->b_blocknr = block;
01090 if (uptodate)
01091 set_buffer_uptodate(bh);
01092 set_buffer_mapped(bh);
01093 }
01094 block++;
01095 bh = bh->b_this_page;
01096 } while (bh != head);
01097 }
01098
01099
01100
01101
01102
01103
01104 static struct page *
01105 grow_dev_page(struct block_device *bdev, sector_t block,
01106 pgoff_t index, int size)
01107 {
01108 struct inode *inode = bdev->bd_inode;
01109 struct page *page;
01110 struct buffer_head *bh;
01111
01112 #ifdef DDE_LINUX
01113 WARN_UNIMPL;
01114 return NULL;
01115 #endif
01116
01117 page = find_or_create_page(inode->i_mapping, index,
01118 (mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS)|__GFP_MOVABLE);
01119 if (!page)
01120 return NULL;
01121
01122 BUG_ON(!PageLocked(page));
01123
01124 if (page_has_buffers(page)) {
01125 bh = page_buffers(page);
01126 if (bh->b_size == size) {
01127 init_page_buffers(page, bdev, block, size);
01128 return page;
01129 }
01130 if (!try_to_free_buffers(page))
01131 goto failed;
01132 }
01133
01134
01135
01136
01137 bh = alloc_page_buffers(page, size, 0);
01138 if (!bh)
01139 goto failed;
01140
01141
01142
01143
01144
01145
01146 spin_lock(&inode->i_mapping->private_lock);
01147 link_dev_buffers(page, bh);
01148 init_page_buffers(page, bdev, block, size);
01149 spin_unlock(&inode->i_mapping->private_lock);
01150 return page;
01151
01152 failed:
01153 BUG();
01154 unlock_page(page);
01155 page_cache_release(page);
01156 return NULL;
01157 }
01158
01159
01160
01161
01162
01163 static int
01164 grow_buffers(struct block_device *bdev, sector_t block, int size)
01165 {
01166 struct page *page;
01167 pgoff_t index;
01168 int sizebits;
01169
01170 sizebits = -1;
01171 do {
01172 sizebits++;
01173 } while ((size << sizebits) < PAGE_SIZE);
01174
01175 index = block >> sizebits;
01176
01177
01178
01179
01180
01181 if (unlikely(index != block >> sizebits)) {
01182 char b[BDEVNAME_SIZE];
01183
01184 printk(KERN_ERR "%s: requested out-of-range block %llu for "
01185 "device %s\n",
01186 __func__, (unsigned long long)block,
01187 bdevname(bdev, b));
01188 return -EIO;
01189 }
01190 block = index << sizebits;
01191
01192 page = grow_dev_page(bdev, block, index, size);
01193 if (!page)
01194 return 0;
01195 unlock_page(page);
01196 page_cache_release(page);
01197 return 1;
01198 }
01199
01200 static struct buffer_head *
01201 __getblk_slow(struct block_device *bdev, sector_t block, int size)
01202 {
01203
01204 if (unlikely(size & (bdev_hardsect_size(bdev)-1) ||
01205 (size < 512 || size > PAGE_SIZE))) {
01206 printk(KERN_ERR "getblk(): invalid block size %d requested\n",
01207 size);
01208 printk(KERN_ERR "hardsect size: %d\n",
01209 bdev_hardsect_size(bdev));
01210
01211 dump_stack();
01212 return NULL;
01213 }
01214
01215 for (;;) {
01216 struct buffer_head * bh;
01217 int ret;
01218
01219 bh = __find_get_block(bdev, block, size);
01220 if (bh)
01221 return bh;
01222
01223 ret = grow_buffers(bdev, block, size);
01224 if (ret < 0)
01225 return NULL;
01226 if (ret == 0)
01227 free_more_memory();
01228 }
01229 }
01230
01231
01232
01233
01234
01235
01236
01237
01238
01239
01240
01241
01242
01243
01244
01245
01246
01247
01248
01249
01250
01251
01252
01253
01254
01255
01256
01257
01258
01259
01260
01261
01262
01263
01264
01265
01266 void mark_buffer_dirty(struct buffer_head *bh)
01267 {
01268 #ifndef DDE_LINUX
01269 WARN_ON_ONCE(!buffer_uptodate(bh));
01270
01271
01272
01273
01274
01275
01276
01277 if (buffer_dirty(bh)) {
01278 smp_mb();
01279 if (buffer_dirty(bh))
01280 return;
01281 }
01282
01283 if (!test_set_buffer_dirty(bh)) {
01284 struct page *page = bh->b_page;
01285 if (!TestSetPageDirty(page))
01286 __set_page_dirty(page, page_mapping(page), 0);
01287 }
01288 #else
01289 WARN_UNIMPL;
01290 #endif
01291 }
01292
01293
01294
01295
01296
01297
01298
01299
01300 void __brelse(struct buffer_head * buf)
01301 {
01302 if (atomic_read(&buf->b_count)) {
01303 put_bh(buf);
01304 return;
01305 }
01306 WARN(1, KERN_ERR "VFS: brelse: Trying to free free buffer\n");
01307 }
01308
01309
01310
01311
01312
01313 void __bforget(struct buffer_head *bh)
01314 {
01315 clear_buffer_dirty(bh);
01316 if (bh->b_assoc_map) {
01317 struct address_space *buffer_mapping = bh->b_page->mapping;
01318
01319 spin_lock(&buffer_mapping->private_lock);
01320 list_del_init(&bh->b_assoc_buffers);
01321 bh->b_assoc_map = NULL;
01322 spin_unlock(&buffer_mapping->private_lock);
01323 }
01324 __brelse(bh);
01325 }
01326
01327 static struct buffer_head *__bread_slow(struct buffer_head *bh)
01328 {
01329 lock_buffer(bh);
01330 if (buffer_uptodate(bh)) {
01331 unlock_buffer(bh);
01332 return bh;
01333 } else {
01334 get_bh(bh);
01335 bh->b_end_io = end_buffer_read_sync;
01336 submit_bh(READ, bh);
01337 wait_on_buffer(bh);
01338 if (buffer_uptodate(bh))
01339 return bh;
01340 }
01341 brelse(bh);
01342 return NULL;
01343 }
01344
01345
01346
01347
01348
01349
01350
01351
01352
01353
01354
01355
01356
01357
01358
01359 #define BH_LRU_SIZE 8
01360
01361 struct bh_lru {
01362 struct buffer_head *bhs[BH_LRU_SIZE];
01363 };
01364
01365 static DEFINE_PER_CPU(struct bh_lru, bh_lrus) = {{ NULL }};
01366
01367 #ifdef CONFIG_SMP
01368 #define bh_lru_lock() local_irq_disable()
01369 #define bh_lru_unlock() local_irq_enable()
01370 #else
01371 #define bh_lru_lock() preempt_disable()
01372 #define bh_lru_unlock() preempt_enable()
01373 #endif
01374
01375 static inline void check_irqs_on(void)
01376 {
01377 #ifdef irqs_disabled
01378 BUG_ON(irqs_disabled());
01379 #endif
01380 }
01381
01382
01383
01384
01385 static void bh_lru_install(struct buffer_head *bh)
01386 {
01387 struct buffer_head *evictee = NULL;
01388 struct bh_lru *lru;
01389
01390 check_irqs_on();
01391 bh_lru_lock();
01392 lru = &__get_cpu_var(bh_lrus);
01393 if (lru->bhs[0] != bh) {
01394 struct buffer_head *bhs[BH_LRU_SIZE];
01395 int in;
01396 int out = 0;
01397
01398 get_bh(bh);
01399 bhs[out++] = bh;
01400 for (in = 0; in < BH_LRU_SIZE; in++) {
01401 struct buffer_head *bh2 = lru->bhs[in];
01402
01403 if (bh2 == bh) {
01404 __brelse(bh2);
01405 } else {
01406 if (out >= BH_LRU_SIZE) {
01407 BUG_ON(evictee != NULL);
01408 evictee = bh2;
01409 } else {
01410 bhs[out++] = bh2;
01411 }
01412 }
01413 }
01414 while (out < BH_LRU_SIZE)
01415 bhs[out++] = NULL;
01416 memcpy(lru->bhs, bhs, sizeof(bhs));
01417 }
01418 bh_lru_unlock();
01419
01420 if (evictee)
01421 __brelse(evictee);
01422 }
01423
01424
01425
01426
01427 static struct buffer_head *
01428 lookup_bh_lru(struct block_device *bdev, sector_t block, unsigned size)
01429 {
01430 struct buffer_head *ret = NULL;
01431 struct bh_lru *lru;
01432 unsigned int i;
01433
01434 check_irqs_on();
01435 bh_lru_lock();
01436 lru = &__get_cpu_var(bh_lrus);
01437 for (i = 0; i < BH_LRU_SIZE; i++) {
01438 struct buffer_head *bh = lru->bhs[i];
01439
01440 if (bh && bh->b_bdev == bdev &&
01441 bh->b_blocknr == block && bh->b_size == size) {
01442 if (i) {
01443 while (i) {
01444 lru->bhs[i] = lru->bhs[i - 1];
01445 i--;
01446 }
01447 lru->bhs[0] = bh;
01448 }
01449 get_bh(bh);
01450 ret = bh;
01451 break;
01452 }
01453 }
01454 bh_lru_unlock();
01455 return ret;
01456 }
01457
01458
01459
01460
01461
01462
01463 struct buffer_head *
01464 __find_get_block(struct block_device *bdev, sector_t block, unsigned size)
01465 {
01466 struct buffer_head *bh = lookup_bh_lru(bdev, block, size);
01467
01468 if (bh == NULL) {
01469 bh = __find_get_block_slow(bdev, block);
01470 if (bh)
01471 bh_lru_install(bh);
01472 }
01473 if (bh)
01474 touch_buffer(bh);
01475 return bh;
01476 }
01477 EXPORT_SYMBOL(__find_get_block);
01478
01479
01480
01481
01482
01483
01484
01485
01486
01487
01488
01489
01490
01491 struct buffer_head *
01492 __getblk(struct block_device *bdev, sector_t block, unsigned size)
01493 {
01494 struct buffer_head *bh = __find_get_block(bdev, block, size);
01495
01496 might_sleep();
01497 if (bh == NULL)
01498 bh = __getblk_slow(bdev, block, size);
01499 return bh;
01500 }
01501 EXPORT_SYMBOL(__getblk);
01502
01503
01504
01505
01506 void __breadahead(struct block_device *bdev, sector_t block, unsigned size)
01507 {
01508 struct buffer_head *bh = __getblk(bdev, block, size);
01509 if (likely(bh)) {
01510 ll_rw_block(READA, 1, &bh);
01511 brelse(bh);
01512 }
01513 }
01514 EXPORT_SYMBOL(__breadahead);
01515
01516
01517
01518
01519
01520
01521
01522
01523
01524
01525 struct buffer_head *
01526 __bread(struct block_device *bdev, sector_t block, unsigned size)
01527 {
01528 struct buffer_head *bh = __getblk(bdev, block, size);
01529
01530 if (likely(bh) && !buffer_uptodate(bh))
01531 bh = __bread_slow(bh);
01532 return bh;
01533 }
01534 EXPORT_SYMBOL(__bread);
01535
01536
01537
01538
01539
01540
01541 static void invalidate_bh_lru(void *arg)
01542 {
01543 struct bh_lru *b = &get_cpu_var(bh_lrus);
01544 int i;
01545
01546 for (i = 0; i < BH_LRU_SIZE; i++) {
01547 brelse(b->bhs[i]);
01548 b->bhs[i] = NULL;
01549 }
01550 put_cpu_var(bh_lrus);
01551 }
01552
01553 void invalidate_bh_lrus(void)
01554 {
01555 #ifndef DDE_LINUX
01556 on_each_cpu(invalidate_bh_lru, NULL, 1);
01557 #endif
01558 }
01559 EXPORT_SYMBOL_GPL(invalidate_bh_lrus);
01560
01561 void set_bh_page(struct buffer_head *bh,
01562 struct page *page, unsigned long offset)
01563 {
01564 bh->b_page = page;
01565 BUG_ON(offset >= PAGE_SIZE);
01566 if (PageHighMem(page))
01567
01568
01569
01570 bh->b_data = (char *)(0 + offset);
01571 else
01572 bh->b_data = page_address(page) + offset;
01573 }
01574 EXPORT_SYMBOL(set_bh_page);
01575
01576
01577
01578
01579 static void discard_buffer(struct buffer_head * bh)
01580 {
01581 lock_buffer(bh);
01582 clear_buffer_dirty(bh);
01583 bh->b_bdev = NULL;
01584 clear_buffer_mapped(bh);
01585 clear_buffer_req(bh);
01586 clear_buffer_new(bh);
01587 clear_buffer_delay(bh);
01588 clear_buffer_unwritten(bh);
01589 unlock_buffer(bh);
01590 }
01591
01592
01593
01594
01595
01596
01597
01598
01599
01600
01601
01602
01603
01604
01605
01606
01607 void block_invalidatepage(struct page *page, unsigned long offset)
01608 {
01609 struct buffer_head *head, *bh, *next;
01610 unsigned int curr_off = 0;
01611
01612 BUG_ON(!PageLocked(page));
01613 if (!page_has_buffers(page))
01614 goto out;
01615
01616 head = page_buffers(page);
01617 bh = head;
01618 do {
01619 unsigned int next_off = curr_off + bh->b_size;
01620 next = bh->b_this_page;
01621
01622
01623
01624
01625 if (offset <= curr_off)
01626 discard_buffer(bh);
01627 curr_off = next_off;
01628 bh = next;
01629 } while (bh != head);
01630
01631
01632
01633
01634
01635
01636 if (offset == 0)
01637 try_to_release_page(page, 0);
01638 out:
01639 return;
01640 }
01641 EXPORT_SYMBOL(block_invalidatepage);
01642
01643
01644
01645
01646
01647
01648 void create_empty_buffers(struct page *page,
01649 unsigned long blocksize, unsigned long b_state)
01650 {
01651 struct buffer_head *bh, *head, *tail;
01652
01653 head = alloc_page_buffers(page, blocksize, 1);
01654 bh = head;
01655 do {
01656 bh->b_state |= b_state;
01657 tail = bh;
01658 bh = bh->b_this_page;
01659 } while (bh);
01660 tail->b_this_page = head;
01661
01662 spin_lock(&page->mapping->private_lock);
01663 if (PageUptodate(page) || PageDirty(page)) {
01664 bh = head;
01665 do {
01666 if (PageDirty(page))
01667 set_buffer_dirty(bh);
01668 if (PageUptodate(page))
01669 set_buffer_uptodate(bh);
01670 bh = bh->b_this_page;
01671 } while (bh != head);
01672 }
01673 attach_page_buffers(page, head);
01674 spin_unlock(&page->mapping->private_lock);
01675 }
01676 EXPORT_SYMBOL(create_empty_buffers);
01677
01678
01679
01680
01681
01682
01683
01684
01685
01686
01687
01688
01689
01690
01691
01692
01693
01694 void unmap_underlying_metadata(struct block_device *bdev, sector_t block)
01695 {
01696 struct buffer_head *old_bh;
01697
01698 might_sleep();
01699
01700 old_bh = __find_get_block_slow(bdev, block);
01701 if (old_bh) {
01702 clear_buffer_dirty(old_bh);
01703 wait_on_buffer(old_bh);
01704 clear_buffer_req(old_bh);
01705 __brelse(old_bh);
01706 }
01707 }
01708 EXPORT_SYMBOL(unmap_underlying_metadata);
01709
01710
01711
01712
01713
01714
01715
01716
01717
01718
01719
01720
01721
01722
01723
01724
01725
01726
01727
01728
01729
01730
01731
01732
01733
01734
01735 static int __block_write_full_page(struct inode *inode, struct page *page,
01736 get_block_t *get_block, struct writeback_control *wbc)
01737 {
01738 int err;
01739 sector_t block;
01740 sector_t last_block;
01741 struct buffer_head *bh, *head;
01742 const unsigned blocksize = 1 << inode->i_blkbits;
01743 int nr_underway = 0;
01744
01745 BUG_ON(!PageLocked(page));
01746
01747 last_block = (i_size_read(inode) - 1) >> inode->i_blkbits;
01748
01749 if (!page_has_buffers(page)) {
01750 create_empty_buffers(page, blocksize,
01751 (1 << BH_Dirty)|(1 << BH_Uptodate));
01752 }
01753
01754
01755
01756
01757
01758
01759
01760
01761
01762
01763
01764 block = (sector_t)page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
01765 head = page_buffers(page);
01766 bh = head;
01767
01768
01769
01770
01771
01772 do {
01773 if (block > last_block) {
01774
01775
01776
01777
01778
01779
01780
01781
01782 clear_buffer_dirty(bh);
01783 set_buffer_uptodate(bh);
01784 } else if ((!buffer_mapped(bh) || buffer_delay(bh)) &&
01785 buffer_dirty(bh)) {
01786 WARN_ON(bh->b_size != blocksize);
01787 err = get_block(inode, block, bh, 1);
01788 if (err)
01789 goto recover;
01790 clear_buffer_delay(bh);
01791 if (buffer_new(bh)) {
01792
01793 clear_buffer_new(bh);
01794 unmap_underlying_metadata(bh->b_bdev,
01795 bh->b_blocknr);
01796 }
01797 }
01798 bh = bh->b_this_page;
01799 block++;
01800 } while (bh != head);
01801
01802 do {
01803 if (!buffer_mapped(bh))
01804 continue;
01805
01806
01807
01808
01809
01810
01811
01812 if (wbc->sync_mode != WB_SYNC_NONE || !wbc->nonblocking) {
01813 lock_buffer(bh);
01814 } else if (!trylock_buffer(bh)) {
01815 redirty_page_for_writepage(wbc, page);
01816 continue;
01817 }
01818 if (test_clear_buffer_dirty(bh)) {
01819 mark_buffer_async_write(bh);
01820 } else {
01821 unlock_buffer(bh);
01822 }
01823 } while ((bh = bh->b_this_page) != head);
01824
01825
01826
01827
01828
01829 BUG_ON(PageWriteback(page));
01830 set_page_writeback(page);
01831
01832 do {
01833 struct buffer_head *next = bh->b_this_page;
01834 if (buffer_async_write(bh)) {
01835 submit_bh(WRITE, bh);
01836 nr_underway++;
01837 }
01838 bh = next;
01839 } while (bh != head);
01840 unlock_page(page);
01841
01842 err = 0;
01843 done:
01844 if (nr_underway == 0) {
01845
01846
01847
01848
01849
01850 end_page_writeback(page);
01851
01852
01853
01854
01855
01856 }
01857 return err;
01858
01859 recover:
01860
01861
01862
01863
01864
01865
01866 bh = head;
01867
01868 do {
01869 if (buffer_mapped(bh) && buffer_dirty(bh) &&
01870 !buffer_delay(bh)) {
01871 lock_buffer(bh);
01872 mark_buffer_async_write(bh);
01873 } else {
01874
01875
01876
01877
01878 clear_buffer_dirty(bh);
01879 }
01880 } while ((bh = bh->b_this_page) != head);
01881 SetPageError(page);
01882 BUG_ON(PageWriteback(page));
01883 mapping_set_error(page->mapping, err);
01884 set_page_writeback(page);
01885 do {
01886 struct buffer_head *next = bh->b_this_page;
01887 if (buffer_async_write(bh)) {
01888 clear_buffer_dirty(bh);
01889 submit_bh(WRITE, bh);
01890 nr_underway++;
01891 }
01892 bh = next;
01893 } while (bh != head);
01894 unlock_page(page);
01895 goto done;
01896 }
01897
01898
01899
01900
01901
01902
01903 void page_zero_new_buffers(struct page *page, unsigned from, unsigned to)
01904 {
01905 unsigned int block_start, block_end;
01906 struct buffer_head *head, *bh;
01907
01908 BUG_ON(!PageLocked(page));
01909 if (!page_has_buffers(page))
01910 return;
01911
01912 bh = head = page_buffers(page);
01913 block_start = 0;
01914 do {
01915 block_end = block_start + bh->b_size;
01916
01917 if (buffer_new(bh)) {
01918 if (block_end > from && block_start < to) {
01919 if (!PageUptodate(page)) {
01920 unsigned start, size;
01921
01922 start = max(from, block_start);
01923 size = min(to, block_end) - start;
01924
01925 zero_user(page, start, size);
01926 set_buffer_uptodate(bh);
01927 }
01928
01929 clear_buffer_new(bh);
01930 mark_buffer_dirty(bh);
01931 }
01932 }
01933
01934 block_start = block_end;
01935 bh = bh->b_this_page;
01936 } while (bh != head);
01937 }
01938 EXPORT_SYMBOL(page_zero_new_buffers);
01939
01940 static int __block_prepare_write(struct inode *inode, struct page *page,
01941 unsigned from, unsigned to, get_block_t *get_block)
01942 {
01943 unsigned block_start, block_end;
01944 sector_t block;
01945 int err = 0;
01946 unsigned blocksize, bbits;
01947 struct buffer_head *bh, *head, *wait[2], **wait_bh=wait;
01948
01949 BUG_ON(!PageLocked(page));
01950 BUG_ON(from > PAGE_CACHE_SIZE);
01951 BUG_ON(to > PAGE_CACHE_SIZE);
01952 BUG_ON(from > to);
01953
01954 blocksize = 1 << inode->i_blkbits;
01955 if (!page_has_buffers(page))
01956 create_empty_buffers(page, blocksize, 0);
01957 head = page_buffers(page);
01958
01959 bbits = inode->i_blkbits;
01960 block = (sector_t)page->index << (PAGE_CACHE_SHIFT - bbits);
01961
01962 for(bh = head, block_start = 0; bh != head || !block_start;
01963 block++, block_start=block_end, bh = bh->b_this_page) {
01964 block_end = block_start + blocksize;
01965 if (block_end <= from || block_start >= to) {
01966 if (PageUptodate(page)) {
01967 if (!buffer_uptodate(bh))
01968 set_buffer_uptodate(bh);
01969 }
01970 continue;
01971 }
01972 if (buffer_new(bh))
01973 clear_buffer_new(bh);
01974 if (!buffer_mapped(bh)) {
01975 WARN_ON(bh->b_size != blocksize);
01976 err = get_block(inode, block, bh, 1);
01977 if (err)
01978 break;
01979 if (buffer_new(bh)) {
01980 unmap_underlying_metadata(bh->b_bdev,
01981 bh->b_blocknr);
01982 if (PageUptodate(page)) {
01983 clear_buffer_new(bh);
01984 set_buffer_uptodate(bh);
01985 mark_buffer_dirty(bh);
01986 continue;
01987 }
01988 if (block_end > to || block_start < from)
01989 zero_user_segments(page,
01990 to, block_end,
01991 block_start, from);
01992 continue;
01993 }
01994 }
01995 if (PageUptodate(page)) {
01996 if (!buffer_uptodate(bh))
01997 set_buffer_uptodate(bh);
01998 continue;
01999 }
02000 if (!buffer_uptodate(bh) && !buffer_delay(bh) &&
02001 !buffer_unwritten(bh) &&
02002 (block_start < from || block_end > to)) {
02003 ll_rw_block(READ, 1, &bh);
02004 *wait_bh++=bh;
02005 }
02006 }
02007
02008
02009
02010 while(wait_bh > wait) {
02011 wait_on_buffer(*--wait_bh);
02012 if (!buffer_uptodate(*wait_bh))
02013 err = -EIO;
02014 }
02015 if (unlikely(err))
02016 page_zero_new_buffers(page, from, to);
02017 return err;
02018 }
02019
02020 static int __block_commit_write(struct inode *inode, struct page *page,
02021 unsigned from, unsigned to)
02022 {
02023 unsigned block_start, block_end;
02024 int partial = 0;
02025 unsigned blocksize;
02026 struct buffer_head *bh, *head;
02027
02028 blocksize = 1 << inode->i_blkbits;
02029
02030 for(bh = head = page_buffers(page), block_start = 0;
02031 bh != head || !block_start;
02032 block_start=block_end, bh = bh->b_this_page) {
02033 block_end = block_start + blocksize;
02034 if (block_end <= from || block_start >= to) {
02035 if (!buffer_uptodate(bh))
02036 partial = 1;
02037 } else {
02038 set_buffer_uptodate(bh);
02039 mark_buffer_dirty(bh);
02040 }
02041 clear_buffer_new(bh);
02042 }
02043
02044
02045
02046
02047
02048
02049
02050 if (!partial)
02051 SetPageUptodate(page);
02052 return 0;
02053 }
02054
02055
02056
02057
02058
02059
02060
02061
02062
02063 int block_write_begin(struct file *file, struct address_space *mapping,
02064 loff_t pos, unsigned len, unsigned flags,
02065 struct page **pagep, void **fsdata,
02066 get_block_t *get_block)
02067 {
02068 #ifndef DDE_LINUX
02069 struct inode *inode = mapping->host;
02070 int status = 0;
02071 struct page *page;
02072 pgoff_t index;
02073 unsigned start, end;
02074 int ownpage = 0;
02075
02076 index = pos >> PAGE_CACHE_SHIFT;
02077 start = pos & (PAGE_CACHE_SIZE - 1);
02078 end = start + len;
02079
02080 page = *pagep;
02081 if (page == NULL) {
02082 ownpage = 1;
02083 page = grab_cache_page_write_begin(mapping, index, flags);
02084 if (!page) {
02085 status = -ENOMEM;
02086 goto out;
02087 }
02088 *pagep = page;
02089 } else
02090 BUG_ON(!PageLocked(page));
02091
02092 status = __block_prepare_write(inode, page, start, end, get_block);
02093 if (unlikely(status)) {
02094 ClearPageUptodate(page);
02095
02096 if (ownpage) {
02097 unlock_page(page);
02098 page_cache_release(page);
02099 *pagep = NULL;
02100
02101 #ifndef DDE_LINUX
02102
02103
02104
02105
02106
02107 if (pos + len > inode->i_size)
02108 vmtruncate(inode, inode->i_size);
02109 #endif
02110 }
02111 }
02112
02113 out:
02114 return status;
02115 #else
02116 WARN_UNIMPL;
02117 return -1;
02118 #endif
02119 }
02120 EXPORT_SYMBOL(block_write_begin);
02121
02122 int block_write_end(struct file *file, struct address_space *mapping,
02123 loff_t pos, unsigned len, unsigned copied,
02124 struct page *page, void *fsdata)
02125 {
02126 struct inode *inode = mapping->host;
02127 unsigned start;
02128
02129 start = pos & (PAGE_CACHE_SIZE - 1);
02130
02131 if (unlikely(copied < len)) {
02132
02133
02134
02135
02136
02137
02138
02139
02140
02141
02142
02143
02144 if (!PageUptodate(page))
02145 copied = 0;
02146
02147 page_zero_new_buffers(page, start+copied, start+len);
02148 }
02149 flush_dcache_page(page);
02150
02151
02152 __block_commit_write(inode, page, start, start+copied);
02153
02154 return copied;
02155 }
02156 EXPORT_SYMBOL(block_write_end);
02157
02158 int generic_write_end(struct file *file, struct address_space *mapping,
02159 loff_t pos, unsigned len, unsigned copied,
02160 struct page *page, void *fsdata)
02161 {
02162 struct inode *inode = mapping->host;
02163 int i_size_changed = 0;
02164
02165 copied = block_write_end(file, mapping, pos, len, copied, page, fsdata);
02166
02167
02168
02169
02170
02171
02172
02173
02174 if (pos+copied > inode->i_size) {
02175 i_size_write(inode, pos+copied);
02176 i_size_changed = 1;
02177 }
02178
02179 unlock_page(page);
02180 page_cache_release(page);
02181
02182
02183
02184
02185
02186
02187
02188 if (i_size_changed)
02189 mark_inode_dirty(inode);
02190
02191 return copied;
02192 }
02193 EXPORT_SYMBOL(generic_write_end);
02194
02195
02196
02197
02198
02199
02200
02201
02202 int block_is_partially_uptodate(struct page *page, read_descriptor_t *desc,
02203 unsigned long from)
02204 {
02205 struct inode *inode = page->mapping->host;
02206 unsigned block_start, block_end, blocksize;
02207 unsigned to;
02208 struct buffer_head *bh, *head;
02209 int ret = 1;
02210
02211 if (!page_has_buffers(page))
02212 return 0;
02213
02214 blocksize = 1 << inode->i_blkbits;
02215 to = min_t(unsigned, PAGE_CACHE_SIZE - from, desc->count);
02216 to = from + to;
02217 if (from < blocksize && to > PAGE_CACHE_SIZE - blocksize)
02218 return 0;
02219
02220 head = page_buffers(page);
02221 bh = head;
02222 block_start = 0;
02223 do {
02224 block_end = block_start + blocksize;
02225 if (block_end > from && block_start < to) {
02226 if (!buffer_uptodate(bh)) {
02227 ret = 0;
02228 break;
02229 }
02230 if (block_end >= to)
02231 break;
02232 }
02233 block_start = block_end;
02234 bh = bh->b_this_page;
02235 } while (bh != head);
02236
02237 return ret;
02238 }
02239 EXPORT_SYMBOL(block_is_partially_uptodate);
02240
02241
02242
02243
02244
02245
02246
02247
02248 int block_read_full_page(struct page *page, get_block_t *get_block)
02249 {
02250 struct inode *inode = page->mapping->host;
02251 sector_t iblock, lblock;
02252 struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE];
02253 unsigned int blocksize;
02254 int nr, i;
02255 int fully_mapped = 1;
02256
02257 BUG_ON(!PageLocked(page));
02258 blocksize = 1 << inode->i_blkbits;
02259 if (!page_has_buffers(page))
02260 create_empty_buffers(page, blocksize, 0);
02261 head = page_buffers(page);
02262
02263 iblock = (sector_t)page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
02264 lblock = (i_size_read(inode)+blocksize-1) >> inode->i_blkbits;
02265 bh = head;
02266 nr = 0;
02267 i = 0;
02268
02269 do {
02270 if (buffer_uptodate(bh))
02271 continue;
02272
02273 if (!buffer_mapped(bh)) {
02274 int err = 0;
02275
02276 fully_mapped = 0;
02277 if (iblock < lblock) {
02278 WARN_ON(bh->b_size != blocksize);
02279 err = get_block(inode, iblock, bh, 0);
02280 if (err)
02281 SetPageError(page);
02282 }
02283 if (!buffer_mapped(bh)) {
02284 zero_user(page, i * blocksize, blocksize);
02285 if (!err)
02286 set_buffer_uptodate(bh);
02287 continue;
02288 }
02289
02290
02291
02292
02293 if (buffer_uptodate(bh))
02294 continue;
02295 }
02296 arr[nr++] = bh;
02297 } while (i++, iblock++, (bh = bh->b_this_page) != head);
02298
02299 if (fully_mapped)
02300 SetPageMappedToDisk(page);
02301
02302 if (!nr) {
02303
02304
02305
02306
02307 if (!PageError(page))
02308 SetPageUptodate(page);
02309 unlock_page(page);
02310 return 0;
02311 }
02312
02313
02314 for (i = 0; i < nr; i++) {
02315 bh = arr[i];
02316 lock_buffer(bh);
02317 mark_buffer_async_read(bh);
02318 }
02319
02320
02321
02322
02323
02324
02325 for (i = 0; i < nr; i++) {
02326 bh = arr[i];
02327 if (buffer_uptodate(bh))
02328 end_buffer_async_read(bh, 1);
02329 else
02330 submit_bh(READ, bh);
02331 }
02332 return 0;
02333 }
02334
02335
02336
02337
02338
02339 int generic_cont_expand_simple(struct inode *inode, loff_t size)
02340 {
02341 struct address_space *mapping = inode->i_mapping;
02342 struct page *page;
02343 void *fsdata;
02344 unsigned long limit;
02345 int err;
02346
02347 err = -EFBIG;
02348 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
02349 if (limit != RLIM_INFINITY && size > (loff_t)limit) {
02350 send_sig(SIGXFSZ, current, 0);
02351 goto out;
02352 }
02353 if (size > inode->i_sb->s_maxbytes)
02354 goto out;
02355
02356 err = pagecache_write_begin(NULL, mapping, size, 0,
02357 AOP_FLAG_UNINTERRUPTIBLE|AOP_FLAG_CONT_EXPAND,
02358 &page, &fsdata);
02359 if (err)
02360 goto out;
02361
02362 err = pagecache_write_end(NULL, mapping, size, 0, 0, page, fsdata);
02363 BUG_ON(err > 0);
02364
02365 out:
02366 return err;
02367 }
02368
02369 static int cont_expand_zero(struct file *file, struct address_space *mapping,
02370 loff_t pos, loff_t *bytes)
02371 {
02372 struct inode *inode = mapping->host;
02373 unsigned blocksize = 1 << inode->i_blkbits;
02374 struct page *page;
02375 void *fsdata;
02376 pgoff_t index, curidx;
02377 loff_t curpos;
02378 unsigned zerofrom, offset, len;
02379 int err = 0;
02380
02381 index = pos >> PAGE_CACHE_SHIFT;
02382 offset = pos & ~PAGE_CACHE_MASK;
02383
02384 while (index > (curidx = (curpos = *bytes)>>PAGE_CACHE_SHIFT)) {
02385 zerofrom = curpos & ~PAGE_CACHE_MASK;
02386 if (zerofrom & (blocksize-1)) {
02387 *bytes |= (blocksize-1);
02388 (*bytes)++;
02389 }
02390 len = PAGE_CACHE_SIZE - zerofrom;
02391
02392 err = pagecache_write_begin(file, mapping, curpos, len,
02393 AOP_FLAG_UNINTERRUPTIBLE,
02394 &page, &fsdata);
02395 if (err)
02396 goto out;
02397 zero_user(page, zerofrom, len);
02398 err = pagecache_write_end(file, mapping, curpos, len, len,
02399 page, fsdata);
02400 if (err < 0)
02401 goto out;
02402 BUG_ON(err != len);
02403 err = 0;
02404
02405 balance_dirty_pages_ratelimited(mapping);
02406 }
02407
02408
02409 if (index == curidx) {
02410 zerofrom = curpos & ~PAGE_CACHE_MASK;
02411
02412 if (offset <= zerofrom) {
02413 goto out;
02414 }
02415 if (zerofrom & (blocksize-1)) {
02416 *bytes |= (blocksize-1);
02417 (*bytes)++;
02418 }
02419 len = offset - zerofrom;
02420
02421 err = pagecache_write_begin(file, mapping, curpos, len,
02422 AOP_FLAG_UNINTERRUPTIBLE,
02423 &page, &fsdata);
02424 if (err)
02425 goto out;
02426 zero_user(page, zerofrom, len);
02427 err = pagecache_write_end(file, mapping, curpos, len, len,
02428 page, fsdata);
02429 if (err < 0)
02430 goto out;
02431 BUG_ON(err != len);
02432 err = 0;
02433 }
02434 out:
02435 return err;
02436 }
02437
02438
02439
02440
02441
02442 int cont_write_begin(struct file *file, struct address_space *mapping,
02443 loff_t pos, unsigned len, unsigned flags,
02444 struct page **pagep, void **fsdata,
02445 get_block_t *get_block, loff_t *bytes)
02446 {
02447 struct inode *inode = mapping->host;
02448 unsigned blocksize = 1 << inode->i_blkbits;
02449 unsigned zerofrom;
02450 int err;
02451
02452 err = cont_expand_zero(file, mapping, pos, bytes);
02453 if (err)
02454 goto out;
02455
02456 zerofrom = *bytes & ~PAGE_CACHE_MASK;
02457 if (pos+len > *bytes && zerofrom & (blocksize-1)) {
02458 *bytes |= (blocksize-1);
02459 (*bytes)++;
02460 }
02461
02462 *pagep = NULL;
02463 err = block_write_begin(file, mapping, pos, len,
02464 flags, pagep, fsdata, get_block);
02465 out:
02466 return err;
02467 }
02468
02469 int block_prepare_write(struct page *page, unsigned from, unsigned to,
02470 get_block_t *get_block)
02471 {
02472 struct inode *inode = page->mapping->host;
02473 int err = __block_prepare_write(inode, page, from, to, get_block);
02474 if (err)
02475 ClearPageUptodate(page);
02476 return err;
02477 }
02478
02479 int block_commit_write(struct page *page, unsigned from, unsigned to)
02480 {
02481 struct inode *inode = page->mapping->host;
02482 __block_commit_write(inode,page,from,to);
02483 return 0;
02484 }
02485
02486
02487
02488
02489
02490
02491
02492
02493
02494
02495
02496
02497
02498
02499
02500
02501 int
02502 block_page_mkwrite(struct vm_area_struct *vma, struct page *page,
02503 get_block_t get_block)
02504 {
02505 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
02506 unsigned long end;
02507 loff_t size;
02508 int ret = -EINVAL;
02509
02510 lock_page(page);
02511 size = i_size_read(inode);
02512 if ((page->mapping != inode->i_mapping) ||
02513 (page_offset(page) > size)) {
02514
02515 goto out_unlock;
02516 }
02517
02518
02519 if (((page->index + 1) << PAGE_CACHE_SHIFT) > size)
02520 end = size & ~PAGE_CACHE_MASK;
02521 else
02522 end = PAGE_CACHE_SIZE;
02523
02524 ret = block_prepare_write(page, 0, end, get_block);
02525 if (!ret)
02526 ret = block_commit_write(page, 0, end);
02527
02528 out_unlock:
02529 unlock_page(page);
02530 return ret;
02531 }
02532
02533
02534
02535
02536
02537
02538 static void end_buffer_read_nobh(struct buffer_head *bh, int uptodate)
02539 {
02540 __end_buffer_read_notouch(bh, uptodate);
02541 }
02542
02543
02544
02545
02546
02547
02548 static void attach_nobh_buffers(struct page *page, struct buffer_head *head)
02549 {
02550 struct buffer_head *bh;
02551
02552 BUG_ON(!PageLocked(page));
02553
02554 spin_lock(&page->mapping->private_lock);
02555 bh = head;
02556 do {
02557 if (PageDirty(page))
02558 set_buffer_dirty(bh);
02559 if (!bh->b_this_page)
02560 bh->b_this_page = head;
02561 bh = bh->b_this_page;
02562 } while (bh != head);
02563 attach_page_buffers(page, head);
02564 spin_unlock(&page->mapping->private_lock);
02565 }
02566
02567
02568
02569
02570
02571 int nobh_write_begin(struct file *file, struct address_space *mapping,
02572 loff_t pos, unsigned len, unsigned flags,
02573 struct page **pagep, void **fsdata,
02574 get_block_t *get_block)
02575 {
02576 struct inode *inode = mapping->host;
02577 const unsigned blkbits = inode->i_blkbits;
02578 const unsigned blocksize = 1 << blkbits;
02579 struct buffer_head *head, *bh;
02580 struct page *page;
02581 pgoff_t index;
02582 unsigned from, to;
02583 unsigned block_in_page;
02584 unsigned block_start, block_end;
02585 sector_t block_in_file;
02586 int nr_reads = 0;
02587 int ret = 0;
02588 int is_mapped_to_disk = 1;
02589
02590 index = pos >> PAGE_CACHE_SHIFT;
02591 from = pos & (PAGE_CACHE_SIZE - 1);
02592 to = from + len;
02593
02594 page = grab_cache_page_write_begin(mapping, index, flags);
02595 if (!page)
02596 return -ENOMEM;
02597 *pagep = page;
02598 *fsdata = NULL;
02599
02600 if (page_has_buffers(page)) {
02601 unlock_page(page);
02602 page_cache_release(page);
02603 *pagep = NULL;
02604 return block_write_begin(file, mapping, pos, len, flags, pagep,
02605 fsdata, get_block);
02606 }
02607
02608 if (PageMappedToDisk(page))
02609 return 0;
02610
02611
02612
02613
02614
02615
02616
02617
02618
02619
02620 head = alloc_page_buffers(page, blocksize, 0);
02621 if (!head) {
02622 ret = -ENOMEM;
02623 goto out_release;
02624 }
02625
02626 block_in_file = (sector_t)page->index << (PAGE_CACHE_SHIFT - blkbits);
02627
02628
02629
02630
02631
02632
02633 for (block_start = 0, block_in_page = 0, bh = head;
02634 block_start < PAGE_CACHE_SIZE;
02635 block_in_page++, block_start += blocksize, bh = bh->b_this_page) {
02636 int create;
02637
02638 block_end = block_start + blocksize;
02639 bh->b_state = 0;
02640 create = 1;
02641 if (block_start >= to)
02642 create = 0;
02643 ret = get_block(inode, block_in_file + block_in_page,
02644 bh, create);
02645 if (ret)
02646 goto failed;
02647 if (!buffer_mapped(bh))
02648 is_mapped_to_disk = 0;
02649 if (buffer_new(bh))
02650 unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);
02651 if (PageUptodate(page)) {
02652 set_buffer_uptodate(bh);
02653 continue;
02654 }
02655 if (buffer_new(bh) || !buffer_mapped(bh)) {
02656 zero_user_segments(page, block_start, from,
02657 to, block_end);
02658 continue;
02659 }
02660 if (buffer_uptodate(bh))
02661 continue;
02662 if (block_start < from || block_end > to) {
02663 lock_buffer(bh);
02664 bh->b_end_io = end_buffer_read_nobh;
02665 submit_bh(READ, bh);
02666 nr_reads++;
02667 }
02668 }
02669
02670 if (nr_reads) {
02671
02672
02673
02674
02675
02676 for (bh = head; bh; bh = bh->b_this_page) {
02677 wait_on_buffer(bh);
02678 if (!buffer_uptodate(bh))
02679 ret = -EIO;
02680 }
02681 if (ret)
02682 goto failed;
02683 }
02684
02685 if (is_mapped_to_disk)
02686 SetPageMappedToDisk(page);
02687
02688 *fsdata = head;
02689
02690 return 0;
02691
02692 failed:
02693 BUG_ON(!ret);
02694
02695
02696
02697
02698
02699
02700
02701 attach_nobh_buffers(page, head);
02702 page_zero_new_buffers(page, from, to);
02703
02704 out_release:
02705 unlock_page(page);
02706 page_cache_release(page);
02707 *pagep = NULL;
02708
02709 if (pos + len > inode->i_size)
02710 vmtruncate(inode, inode->i_size);
02711
02712 return ret;
02713 }
02714 EXPORT_SYMBOL(nobh_write_begin);
02715
02716 int nobh_write_end(struct file *file, struct address_space *mapping,
02717 loff_t pos, unsigned len, unsigned copied,
02718 struct page *page, void *fsdata)
02719 {
02720 struct inode *inode = page->mapping->host;
02721 struct buffer_head *head = fsdata;
02722 struct buffer_head *bh;
02723 BUG_ON(fsdata != NULL && page_has_buffers(page));
02724
02725 if (unlikely(copied < len) && head)
02726 attach_nobh_buffers(page, head);
02727 if (page_has_buffers(page))
02728 return generic_write_end(file, mapping, pos, len,
02729 copied, page, fsdata);
02730
02731 SetPageUptodate(page);
02732 set_page_dirty(page);
02733 if (pos+copied > inode->i_size) {
02734 i_size_write(inode, pos+copied);
02735 mark_inode_dirty(inode);
02736 }
02737
02738 unlock_page(page);
02739 page_cache_release(page);
02740
02741 while (head) {
02742 bh = head;
02743 head = head->b_this_page;
02744 free_buffer_head(bh);
02745 }
02746
02747 return copied;
02748 }
02749 EXPORT_SYMBOL(nobh_write_end);
02750
02751
02752
02753
02754
02755
02756 int nobh_writepage(struct page *page, get_block_t *get_block,
02757 struct writeback_control *wbc)
02758 {
02759 struct inode * const inode = page->mapping->host;
02760 loff_t i_size = i_size_read(inode);
02761 const pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
02762 unsigned offset;
02763 int ret;
02764
02765
02766 if (page->index < end_index)
02767 goto out;
02768
02769
02770 offset = i_size & (PAGE_CACHE_SIZE-1);
02771 if (page->index >= end_index+1 || !offset) {
02772
02773
02774
02775
02776
02777 #if 0
02778
02779 if (page->mapping->a_ops->invalidatepage)
02780 page->mapping->a_ops->invalidatepage(page, offset);
02781 #endif
02782 unlock_page(page);
02783 return 0;
02784 }
02785
02786
02787
02788
02789
02790
02791
02792
02793 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
02794 out:
02795 ret = mpage_writepage(page, get_block, wbc);
02796 if (ret == -EAGAIN)
02797 ret = __block_write_full_page(inode, page, get_block, wbc);
02798 return ret;
02799 }
02800 EXPORT_SYMBOL(nobh_writepage);
02801
02802 int nobh_truncate_page(struct address_space *mapping,
02803 loff_t from, get_block_t *get_block)
02804 {
02805 pgoff_t index = from >> PAGE_CACHE_SHIFT;
02806 unsigned offset = from & (PAGE_CACHE_SIZE-1);
02807 unsigned blocksize;
02808 sector_t iblock;
02809 unsigned length, pos;
02810 struct inode *inode = mapping->host;
02811 struct page *page;
02812 struct buffer_head map_bh;
02813 int err;
02814
02815 blocksize = 1 << inode->i_blkbits;
02816 length = offset & (blocksize - 1);
02817
02818
02819 if (!length)
02820 return 0;
02821
02822 length = blocksize - length;
02823 iblock = (sector_t)index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
02824
02825 page = grab_cache_page(mapping, index);
02826 err = -ENOMEM;
02827 if (!page)
02828 goto out;
02829
02830 if (page_has_buffers(page)) {
02831 has_buffers:
02832 unlock_page(page);
02833 page_cache_release(page);
02834 return block_truncate_page(mapping, from, get_block);
02835 }
02836
02837
02838 pos = blocksize;
02839 while (offset >= pos) {
02840 iblock++;
02841 pos += blocksize;
02842 }
02843
02844 err = get_block(inode, iblock, &map_bh, 0);
02845 if (err)
02846 goto unlock;
02847
02848 if (!buffer_mapped(&map_bh))
02849 goto unlock;
02850
02851
02852 if (!PageUptodate(page)) {
02853 err = mapping->a_ops->readpage(NULL, page);
02854 if (err) {
02855 page_cache_release(page);
02856 goto out;
02857 }
02858 lock_page(page);
02859 if (!PageUptodate(page)) {
02860 err = -EIO;
02861 goto unlock;
02862 }
02863 if (page_has_buffers(page))
02864 goto has_buffers;
02865 }
02866 zero_user(page, offset, length);
02867 set_page_dirty(page);
02868 err = 0;
02869
02870 unlock:
02871 unlock_page(page);
02872 page_cache_release(page);
02873 out:
02874 return err;
02875 }
02876 EXPORT_SYMBOL(nobh_truncate_page);
02877
02878 int block_truncate_page(struct address_space *mapping,
02879 loff_t from, get_block_t *get_block)
02880 {
02881 pgoff_t index = from >> PAGE_CACHE_SHIFT;
02882 unsigned offset = from & (PAGE_CACHE_SIZE-1);
02883 unsigned blocksize;
02884 sector_t iblock;
02885 unsigned length, pos;
02886 struct inode *inode = mapping->host;
02887 struct page *page;
02888 struct buffer_head *bh;
02889 int err;
02890
02891 blocksize = 1 << inode->i_blkbits;
02892 length = offset & (blocksize - 1);
02893
02894
02895 if (!length)
02896 return 0;
02897
02898 length = blocksize - length;
02899 iblock = (sector_t)index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
02900
02901 page = grab_cache_page(mapping, index);
02902 err = -ENOMEM;
02903 if (!page)
02904 goto out;
02905
02906 if (!page_has_buffers(page))
02907 create_empty_buffers(page, blocksize, 0);
02908
02909
02910 bh = page_buffers(page);
02911 pos = blocksize;
02912 while (offset >= pos) {
02913 bh = bh->b_this_page;
02914 iblock++;
02915 pos += blocksize;
02916 }
02917
02918 err = 0;
02919 if (!buffer_mapped(bh)) {
02920 WARN_ON(bh->b_size != blocksize);
02921 err = get_block(inode, iblock, bh, 0);
02922 if (err)
02923 goto unlock;
02924
02925 if (!buffer_mapped(bh))
02926 goto unlock;
02927 }
02928
02929
02930 if (PageUptodate(page))
02931 set_buffer_uptodate(bh);
02932
02933 if (!buffer_uptodate(bh) && !buffer_delay(bh) && !buffer_unwritten(bh)) {
02934 err = -EIO;
02935 ll_rw_block(READ, 1, &bh);
02936 wait_on_buffer(bh);
02937
02938 if (!buffer_uptodate(bh))
02939 goto unlock;
02940 }
02941
02942 zero_user(page, offset, length);
02943 mark_buffer_dirty(bh);
02944 err = 0;
02945
02946 unlock:
02947 unlock_page(page);
02948 page_cache_release(page);
02949 out:
02950 return err;
02951 }
02952
02953
02954
02955
02956 int block_write_full_page(struct page *page, get_block_t *get_block,
02957 struct writeback_control *wbc)
02958 {
02959 struct inode * const inode = page->mapping->host;
02960 loff_t i_size = i_size_read(inode);
02961 const pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
02962 unsigned offset;
02963
02964
02965 if (page->index < end_index)
02966 return __block_write_full_page(inode, page, get_block, wbc);
02967
02968
02969 offset = i_size & (PAGE_CACHE_SIZE-1);
02970 if (page->index >= end_index+1 || !offset) {
02971
02972
02973
02974
02975
02976 do_invalidatepage(page, 0);
02977 unlock_page(page);
02978 return 0;
02979 }
02980
02981
02982
02983
02984
02985
02986
02987
02988 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
02989 return __block_write_full_page(inode, page, get_block, wbc);
02990 }
02991
02992 sector_t generic_block_bmap(struct address_space *mapping, sector_t block,
02993 get_block_t *get_block)
02994 {
02995 struct buffer_head tmp;
02996 struct inode *inode = mapping->host;
02997 tmp.b_state = 0;
02998 tmp.b_blocknr = 0;
02999 tmp.b_size = 1 << inode->i_blkbits;
03000 get_block(inode, block, &tmp, 0);
03001 return tmp.b_blocknr;
03002 }
03003
03004 static void end_bio_bh_io_sync(struct bio *bio, int err)
03005 {
03006 struct buffer_head *bh = bio->bi_private;
03007
03008 if (err == -EOPNOTSUPP) {
03009 set_bit(BIO_EOPNOTSUPP, &bio->bi_flags);
03010 set_bit(BH_Eopnotsupp, &bh->b_state);
03011 }
03012
03013 if (unlikely (test_bit(BIO_QUIET,&bio->bi_flags)))
03014 set_bit(BH_Quiet, &bh->b_state);
03015
03016 bh->b_end_io(bh, test_bit(BIO_UPTODATE, &bio->bi_flags));
03017 bio_put(bio);
03018 }
03019
03020 int submit_bh(int rw, struct buffer_head * bh)
03021 {
03022 struct bio *bio;
03023 int ret = 0;
03024
03025 BUG_ON(!buffer_locked(bh));
03026 BUG_ON(!buffer_mapped(bh));
03027 BUG_ON(!bh->b_end_io);
03028
03029
03030
03031
03032
03033 if (buffer_ordered(bh) && (rw & WRITE))
03034 rw |= WRITE_BARRIER;
03035
03036
03037
03038
03039 if (test_set_buffer_req(bh) && (rw & WRITE))
03040 clear_buffer_write_io_error(bh);
03041
03042
03043
03044
03045
03046 bio = bio_alloc(GFP_NOIO, 1);
03047
03048 bio->bi_sector = bh->b_blocknr * (bh->b_size >> 9);
03049 bio->bi_bdev = bh->b_bdev;
03050 bio->bi_io_vec[0].bv_page = bh->b_page;
03051 bio->bi_io_vec[0].bv_len = bh->b_size;
03052 bio->bi_io_vec[0].bv_offset = bh_offset(bh);
03053
03054 bio->bi_vcnt = 1;
03055 bio->bi_idx = 0;
03056 bio->bi_size = bh->b_size;
03057
03058 bio->bi_end_io = end_bio_bh_io_sync;
03059 bio->bi_private = bh;
03060
03061 bio_get(bio);
03062 submit_bio(rw, bio);
03063
03064 if (bio_flagged(bio, BIO_EOPNOTSUPP))
03065 ret = -EOPNOTSUPP;
03066
03067 bio_put(bio);
03068 return ret;
03069 }
03070
03071
03072
03073
03074
03075
03076
03077
03078
03079
03080
03081
03082
03083
03084
03085
03086
03087
03088
03089
03090
03091
03092
03093
03094
03095
03096
03097 void ll_rw_block(int rw, int nr, struct buffer_head *bhs[])
03098 {
03099 int i;
03100
03101 for (i = 0; i < nr; i++) {
03102 struct buffer_head *bh = bhs[i];
03103
03104 if (rw == SWRITE || rw == SWRITE_SYNC)
03105 lock_buffer(bh);
03106 else if (!trylock_buffer(bh))
03107 continue;
03108
03109 if (rw == WRITE || rw == SWRITE || rw == SWRITE_SYNC) {
03110 if (test_clear_buffer_dirty(bh)) {
03111 bh->b_end_io = end_buffer_write_sync;
03112 get_bh(bh);
03113 if (rw == SWRITE_SYNC)
03114 submit_bh(WRITE_SYNC, bh);
03115 else
03116 submit_bh(WRITE, bh);
03117 continue;
03118 }
03119 } else {
03120 if (!buffer_uptodate(bh)) {
03121 bh->b_end_io = end_buffer_read_sync;
03122 get_bh(bh);
03123 submit_bh(rw, bh);
03124 continue;
03125 }
03126 }
03127 unlock_buffer(bh);
03128 }
03129 }
03130
03131
03132
03133
03134
03135
03136 int sync_dirty_buffer(struct buffer_head *bh)
03137 {
03138 int ret = 0;
03139
03140 WARN_ON(atomic_read(&bh->b_count) < 1);
03141 lock_buffer(bh);
03142 if (test_clear_buffer_dirty(bh)) {
03143 get_bh(bh);
03144 bh->b_end_io = end_buffer_write_sync;
03145 ret = submit_bh(WRITE, bh);
03146 wait_on_buffer(bh);
03147 if (buffer_eopnotsupp(bh)) {
03148 clear_buffer_eopnotsupp(bh);
03149 ret = -EOPNOTSUPP;
03150 }
03151 if (!ret && !buffer_uptodate(bh))
03152 ret = -EIO;
03153 } else {
03154 unlock_buffer(bh);
03155 }
03156 return ret;
03157 }
03158
03159
03160
03161
03162
03163
03164
03165
03166
03167
03168
03169
03170
03171
03172
03173
03174
03175
03176
03177
03178
03179 static inline int buffer_busy(struct buffer_head *bh)
03180 {
03181 return atomic_read(&bh->b_count) |
03182 (bh->b_state & ((1 << BH_Dirty) | (1 << BH_Lock)));
03183 }
03184
03185 static int
03186 drop_buffers(struct page *page, struct buffer_head **buffers_to_free)
03187 {
03188 struct buffer_head *head = page_buffers(page);
03189 struct buffer_head *bh;
03190
03191 bh = head;
03192 do {
03193 if (buffer_write_io_error(bh) && page->mapping)
03194 set_bit(AS_EIO, &page->mapping->flags);
03195 if (buffer_busy(bh))
03196 goto failed;
03197 bh = bh->b_this_page;
03198 } while (bh != head);
03199
03200 do {
03201 struct buffer_head *next = bh->b_this_page;
03202
03203 if (bh->b_assoc_map)
03204 __remove_assoc_queue(bh);
03205 bh = next;
03206 } while (bh != head);
03207 *buffers_to_free = head;
03208 __clear_page_buffers(page);
03209 return 1;
03210 failed:
03211 return 0;
03212 }
03213
03214 int try_to_free_buffers(struct page *page)
03215 {
03216 struct address_space * const mapping = page->mapping;
03217 struct buffer_head *buffers_to_free = NULL;
03218 int ret = 0;
03219
03220 BUG_ON(!PageLocked(page));
03221 if (PageWriteback(page))
03222 return 0;
03223
03224 if (mapping == NULL) {
03225 ret = drop_buffers(page, &buffers_to_free);
03226 goto out;
03227 }
03228
03229 spin_lock(&mapping->private_lock);
03230 ret = drop_buffers(page, &buffers_to_free);
03231
03232
03233
03234
03235
03236
03237
03238
03239
03240
03241
03242
03243
03244
03245
03246 #ifndef DDE_LINUX
03247 if (ret)
03248 cancel_dirty_page(page, PAGE_CACHE_SIZE);
03249 #endif
03250 spin_unlock(&mapping->private_lock);
03251 out:
03252 if (buffers_to_free) {
03253 struct buffer_head *bh = buffers_to_free;
03254
03255 do {
03256 struct buffer_head *next = bh->b_this_page;
03257 free_buffer_head(bh);
03258 bh = next;
03259 } while (bh != buffers_to_free);
03260 }
03261 return ret;
03262 }
03263 EXPORT_SYMBOL(try_to_free_buffers);
03264
03265 void block_sync_page(struct page *page)
03266 {
03267 struct address_space *mapping;
03268
03269 smp_mb();
03270 mapping = page_mapping(page);
03271 if (mapping)
03272 blk_run_backing_dev(mapping->backing_dev_info, page);
03273 }
03274
03275
03276
03277
03278
03279
03280
03281
03282 SYSCALL_DEFINE2(bdflush, int, func, long, data)
03283 {
03284 static int msg_count;
03285
03286 if (!capable(CAP_SYS_ADMIN))
03287 return -EPERM;
03288
03289 if (msg_count < 5) {
03290 msg_count++;
03291 printk(KERN_INFO
03292 "warning: process `%s' used the obsolete bdflush"
03293 " system call\n", current->comm);
03294 printk(KERN_INFO "Fix your initscripts?\n");
03295 }
03296
03297 if (func == 1)
03298 do_exit(0);
03299 return 0;
03300 }
03301
03302
03303
03304
03305 static struct kmem_cache *bh_cachep;
03306
03307
03308
03309
03310
03311 static int max_buffer_heads;
03312
03313 int buffer_heads_over_limit;
03314
03315 struct bh_accounting {
03316 int nr;
03317 int ratelimit;
03318 };
03319
03320 static DEFINE_PER_CPU(struct bh_accounting, bh_accounting) = {0, 0};
03321
03322 static void recalc_bh_state(void)
03323 {
03324 int i;
03325 int tot = 0;
03326
03327 if (__get_cpu_var(bh_accounting).ratelimit++ < 4096)
03328 return;
03329 __get_cpu_var(bh_accounting).ratelimit = 0;
03330 for_each_online_cpu(i)
03331 tot += per_cpu(bh_accounting, i).nr;
03332 buffer_heads_over_limit = (tot > max_buffer_heads);
03333 }
03334
03335 struct buffer_head *alloc_buffer_head(gfp_t gfp_flags)
03336 {
03337 struct buffer_head *ret = kmem_cache_alloc(bh_cachep, gfp_flags);
03338 if (ret) {
03339 INIT_LIST_HEAD(&ret->b_assoc_buffers);
03340 get_cpu_var(bh_accounting).nr++;
03341 recalc_bh_state();
03342 put_cpu_var(bh_accounting);
03343 }
03344 return ret;
03345 }
03346 EXPORT_SYMBOL(alloc_buffer_head);
03347
03348 void free_buffer_head(struct buffer_head *bh)
03349 {
03350 BUG_ON(!list_empty(&bh->b_assoc_buffers));
03351 kmem_cache_free(bh_cachep, bh);
03352 get_cpu_var(bh_accounting).nr--;
03353 recalc_bh_state();
03354 put_cpu_var(bh_accounting);
03355 }
03356 EXPORT_SYMBOL(free_buffer_head);
03357
03358 static void buffer_exit_cpu(int cpu)
03359 {
03360 int i;
03361 struct bh_lru *b = &per_cpu(bh_lrus, cpu);
03362
03363 for (i = 0; i < BH_LRU_SIZE; i++) {
03364 brelse(b->bhs[i]);
03365 b->bhs[i] = NULL;
03366 }
03367 get_cpu_var(bh_accounting).nr += per_cpu(bh_accounting, cpu).nr;
03368 per_cpu(bh_accounting, cpu).nr = 0;
03369 put_cpu_var(bh_accounting);
03370 }
03371
03372 static int buffer_cpu_notify(struct notifier_block *self,
03373 unsigned long action, void *hcpu)
03374 {
03375 if (action == CPU_DEAD || action == CPU_DEAD_FROZEN)
03376 buffer_exit_cpu((unsigned long)hcpu);
03377 return NOTIFY_OK;
03378 }
03379
03380
03381
03382
03383
03384
03385
03386
03387 int bh_uptodate_or_lock(struct buffer_head *bh)
03388 {
03389 if (!buffer_uptodate(bh)) {
03390 lock_buffer(bh);
03391 if (!buffer_uptodate(bh))
03392 return 0;
03393 unlock_buffer(bh);
03394 }
03395 return 1;
03396 }
03397 EXPORT_SYMBOL(bh_uptodate_or_lock);
03398
03399
03400
03401
03402
03403
03404
03405 int bh_submit_read(struct buffer_head *bh)
03406 {
03407 BUG_ON(!buffer_locked(bh));
03408
03409 if (buffer_uptodate(bh)) {
03410 unlock_buffer(bh);
03411 return 0;
03412 }
03413
03414 get_bh(bh);
03415 bh->b_end_io = end_buffer_read_sync;
03416 submit_bh(READ, bh);
03417 wait_on_buffer(bh);
03418 if (buffer_uptodate(bh))
03419 return 0;
03420 return -EIO;
03421 }
03422 EXPORT_SYMBOL(bh_submit_read);
03423
03424 static void
03425 init_buffer_head(void *data)
03426 {
03427 struct buffer_head *bh = data;
03428
03429 memset(bh, 0, sizeof(*bh));
03430 INIT_LIST_HEAD(&bh->b_assoc_buffers);
03431 }
03432
03433 void __init buffer_init(void)
03434 {
03435 int nrpages;
03436
03437 bh_cachep = kmem_cache_create("buffer_head",
03438 sizeof(struct buffer_head), 0,
03439 (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
03440 SLAB_MEM_SPREAD),
03441 init_buffer_head);
03442
03443
03444
03445
03446 nrpages = (nr_free_buffer_pages() * 10) / 100;
03447 max_buffer_heads = nrpages * (PAGE_SIZE / sizeof(struct buffer_head));
03448 hotcpu_notifier(buffer_cpu_notify, 0);
03449 }
03450
03451 EXPORT_SYMBOL(__bforget);
03452 EXPORT_SYMBOL(__brelse);
03453 EXPORT_SYMBOL(__wait_on_buffer);
03454 EXPORT_SYMBOL(block_commit_write);
03455 EXPORT_SYMBOL(block_prepare_write);
03456 EXPORT_SYMBOL(block_page_mkwrite);
03457 EXPORT_SYMBOL(block_read_full_page);
03458 EXPORT_SYMBOL(block_sync_page);
03459 EXPORT_SYMBOL(block_truncate_page);
03460 EXPORT_SYMBOL(block_write_full_page);
03461 EXPORT_SYMBOL(cont_write_begin);
03462 EXPORT_SYMBOL(end_buffer_read_sync);
03463 EXPORT_SYMBOL(end_buffer_write_sync);
03464 EXPORT_SYMBOL(file_fsync);
03465 EXPORT_SYMBOL(fsync_bdev);
03466 EXPORT_SYMBOL(generic_block_bmap);
03467 EXPORT_SYMBOL(generic_cont_expand_simple);
03468 EXPORT_SYMBOL(init_buffer);
03469 EXPORT_SYMBOL(invalidate_bdev);
03470 EXPORT_SYMBOL(ll_rw_block);
03471 EXPORT_SYMBOL(mark_buffer_dirty);
03472 EXPORT_SYMBOL(submit_bh);
03473 EXPORT_SYMBOL(sync_dirty_buffer);
03474 EXPORT_SYMBOL(unlock_buffer);