PageRenderTime 31ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/fs/buffer.c

https://github.com/mstsirkin/linux
C | 1793 lines | 1042 code | 166 blank | 585 comment | 184 complexity | 2236fd0cc51a3c1130becbc8ed1ab81d MD5 | raw file
  1. /*
  2. * linux/fs/buffer.c
  3. *
  4. * Copyright (C) 1991, 1992, 2002 Linus Torvalds
  5. */
  6. /*
  7. * Start bdflush() with kernel_thread not syscall - Paul Gortmaker, 12/95
  8. *
  9. * Removed a lot of unnecessary code and simplified things now that
  10. * the buffer cache isn't our primary cache - Andrew Tridgell 12/96
  11. *
  12. * Speed up hash, lru, and free list operations. Use gfp() for allocating
  13. * hash table, use SLAB cache for buffer heads. SMP threading. -DaveM
  14. *
  15. * Added 32k buffer block sizes - these are required older ARM systems. - RMK
  16. *
  17. * async buffer flushing, 1999 Andrea Arcangeli <andrea@suse.de>
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/syscalls.h>
  21. #include <linux/fs.h>
  22. #include <linux/mm.h>
  23. #include <linux/percpu.h>
  24. #include <linux/slab.h>
  25. #include <linux/capability.h>
  26. #include <linux/blkdev.h>
  27. #include <linux/file.h>
  28. #include <linux/quotaops.h>
  29. #include <linux/highmem.h>
  30. #include <linux/module.h>
  31. #include <linux/writeback.h>
  32. #include <linux/hash.h>
  33. #include <linux/suspend.h>
  34. #include <linux/buffer_head.h>
  35. #include <linux/task_io_accounting_ops.h>
  36. #include <linux/bio.h>
  37. #include <linux/notifier.h>
  38. #include <linux/cpu.h>
  39. #include <linux/bitops.h>
  40. #include <linux/mpage.h>
  41. #include <linux/bit_spinlock.h>
  42. #include <linux/cleancache.h>
  43. static int fsync_buffers_list(spinlock_t *lock, struct list_head *list);
  44. #define BH_ENTRY(list) list_entry((list), struct buffer_head, b_assoc_buffers)
  45. inline void
  46. init_buffer(struct buffer_head *bh, bh_end_io_t *handler, void *private)
  47. {
  48. bh->b_end_io = handler;
  49. bh->b_private = private;
  50. }
  51. EXPORT_SYMBOL(init_buffer);
  52. static int sleep_on_buffer(void *word)
  53. {
  54. io_schedule();
  55. return 0;
  56. }
  57. void __lock_buffer(struct buffer_head *bh)
  58. {
  59. wait_on_bit_lock(&bh->b_state, BH_Lock, sleep_on_buffer,
  60. TASK_UNINTERRUPTIBLE);
  61. }
  62. EXPORT_SYMBOL(__lock_buffer);
  63. void unlock_buffer(struct buffer_head *bh)
  64. {
  65. clear_bit_unlock(BH_Lock, &bh->b_state);
  66. smp_mb__after_clear_bit();
  67. wake_up_bit(&bh->b_state, BH_Lock);
  68. }
  69. EXPORT_SYMBOL(unlock_buffer);
  70. /*
  71. * Block until a buffer comes unlocked. This doesn't stop it
  72. * from becoming locked again - you have to lock it yourself
  73. * if you want to preserve its state.
  74. */
  75. void __wait_on_buffer(struct buffer_head * bh)
  76. {
  77. wait_on_bit(&bh->b_state, BH_Lock, sleep_on_buffer, TASK_UNINTERRUPTIBLE);
  78. }
  79. EXPORT_SYMBOL(__wait_on_buffer);
  80. static void
  81. __clear_page_buffers(struct page *page)
  82. {
  83. ClearPagePrivate(page);
  84. set_page_private(page, 0);
  85. page_cache_release(page);
  86. }
  87. static int quiet_error(struct buffer_head *bh)
  88. {
  89. if (!test_bit(BH_Quiet, &bh->b_state) && printk_ratelimit())
  90. return 0;
  91. return 1;
  92. }
  93. static void buffer_io_error(struct buffer_head *bh)
  94. {
  95. char b[BDEVNAME_SIZE];
  96. printk(KERN_ERR "Buffer I/O error on device %s, logical block %Lu\n",
  97. bdevname(bh->b_bdev, b),
  98. (unsigned long long)bh->b_blocknr);
  99. }
  100. /*
  101. * End-of-IO handler helper function which does not touch the bh after
  102. * unlocking it.
  103. * Note: unlock_buffer() sort-of does touch the bh after unlocking it, but
  104. * a race there is benign: unlock_buffer() only use the bh's address for
  105. * hashing after unlocking the buffer, so it doesn't actually touch the bh
  106. * itself.
  107. */
  108. static void __end_buffer_read_notouch(struct buffer_head *bh, int uptodate)
  109. {
  110. if (uptodate) {
  111. set_buffer_uptodate(bh);
  112. } else {
  113. /* This happens, due to failed READA attempts. */
  114. clear_buffer_uptodate(bh);
  115. }
  116. unlock_buffer(bh);
  117. }
  118. /*
  119. * Default synchronous end-of-IO handler.. Just mark it up-to-date and
  120. * unlock the buffer. This is what ll_rw_block uses too.
  121. */
  122. void end_buffer_read_sync(struct buffer_head *bh, int uptodate)
  123. {
  124. __end_buffer_read_notouch(bh, uptodate);
  125. put_bh(bh);
  126. }
  127. EXPORT_SYMBOL(end_buffer_read_sync);
  128. void end_buffer_write_sync(struct buffer_head *bh, int uptodate)
  129. {
  130. char b[BDEVNAME_SIZE];
  131. if (uptodate) {
  132. set_buffer_uptodate(bh);
  133. } else {
  134. if (!quiet_error(bh)) {
  135. buffer_io_error(bh);
  136. printk(KERN_WARNING "lost page write due to "
  137. "I/O error on %s\n",
  138. bdevname(bh->b_bdev, b));
  139. }
  140. set_buffer_write_io_error(bh);
  141. clear_buffer_uptodate(bh);
  142. }
  143. unlock_buffer(bh);
  144. put_bh(bh);
  145. }
  146. EXPORT_SYMBOL(end_buffer_write_sync);
  147. /*
  148. * Various filesystems appear to want __find_get_block to be non-blocking.
  149. * But it's the page lock which protects the buffers. To get around this,
  150. * we get exclusion from try_to_free_buffers with the blockdev mapping's
  151. * private_lock.
  152. *
  153. * Hack idea: for the blockdev mapping, i_bufferlist_lock contention
  154. * may be quite high. This code could TryLock the page, and if that
  155. * succeeds, there is no need to take private_lock. (But if
  156. * private_lock is contended then so is mapping->tree_lock).
  157. */
  158. static struct buffer_head *
  159. __find_get_block_slow(struct block_device *bdev, sector_t block)
  160. {
  161. struct inode *bd_inode = bdev->bd_inode;
  162. struct address_space *bd_mapping = bd_inode->i_mapping;
  163. struct buffer_head *ret = NULL;
  164. pgoff_t index;
  165. struct buffer_head *bh;
  166. struct buffer_head *head;
  167. struct page *page;
  168. int all_mapped = 1;
  169. index = block >> (PAGE_CACHE_SHIFT - bd_inode->i_blkbits);
  170. page = find_get_page(bd_mapping, index);
  171. if (!page)
  172. goto out;
  173. spin_lock(&bd_mapping->private_lock);
  174. if (!page_has_buffers(page))
  175. goto out_unlock;
  176. head = page_buffers(page);
  177. bh = head;
  178. do {
  179. if (!buffer_mapped(bh))
  180. all_mapped = 0;
  181. else if (bh->b_blocknr == block) {
  182. ret = bh;
  183. get_bh(bh);
  184. goto out_unlock;
  185. }
  186. bh = bh->b_this_page;
  187. } while (bh != head);
  188. /* we might be here because some of the buffers on this page are
  189. * not mapped. This is due to various races between
  190. * file io on the block device and getblk. It gets dealt with
  191. * elsewhere, don't buffer_error if we had some unmapped buffers
  192. */
  193. if (all_mapped) {
  194. printk("__find_get_block_slow() failed. "
  195. "block=%llu, b_blocknr=%llu\n",
  196. (unsigned long long)block,
  197. (unsigned long long)bh->b_blocknr);
  198. printk("b_state=0x%08lx, b_size=%zu\n",
  199. bh->b_state, bh->b_size);
  200. printk("device blocksize: %d\n", 1 << bd_inode->i_blkbits);
  201. }
  202. out_unlock:
  203. spin_unlock(&bd_mapping->private_lock);
  204. page_cache_release(page);
  205. out:
  206. return ret;
  207. }
  208. /* If invalidate_buffers() will trash dirty buffers, it means some kind
  209. of fs corruption is going on. Trashing dirty data always imply losing
  210. information that was supposed to be just stored on the physical layer
  211. by the user.
  212. Thus invalidate_buffers in general usage is not allwowed to trash
  213. dirty buffers. For example ioctl(FLSBLKBUF) expects dirty data to
  214. be preserved. These buffers are simply skipped.
  215. We also skip buffers which are still in use. For example this can
  216. happen if a userspace program is reading the block device.
  217. NOTE: In the case where the user removed a removable-media-disk even if
  218. there's still dirty data not synced on disk (due a bug in the device driver
  219. or due an error of the user), by not destroying the dirty buffers we could
  220. generate corruption also on the next media inserted, thus a parameter is
  221. necessary to handle this case in the most safe way possible (trying
  222. to not corrupt also the new disk inserted with the data belonging to
  223. the old now corrupted disk). Also for the ramdisk the natural thing
  224. to do in order to release the ramdisk memory is to destroy dirty buffers.
  225. These are two special cases. Normal usage imply the device driver
  226. to issue a sync on the device (without waiting I/O completion) and
  227. then an invalidate_buffers call that doesn't trash dirty buffers.
  228. For handling cache coherency with the blkdev pagecache the 'update' case
  229. is been introduced. It is needed to re-read from disk any pinned
  230. buffer. NOTE: re-reading from disk is destructive so we can do it only
  231. when we assume nobody is changing the buffercache under our I/O and when
  232. we think the disk contains more recent information than the buffercache.
  233. The update == 1 pass marks the buffers we need to update, the update == 2
  234. pass does the actual I/O. */
  235. void invalidate_bdev(struct block_device *bdev)
  236. {
  237. struct address_space *mapping = bdev->bd_inode->i_mapping;
  238. if (mapping->nrpages == 0)
  239. return;
  240. invalidate_bh_lrus();
  241. lru_add_drain_all(); /* make sure all lru add caches are flushed */
  242. invalidate_mapping_pages(mapping, 0, -1);
  243. /* 99% of the time, we don't need to flush the cleancache on the bdev.
  244. * But, for the strange corners, lets be cautious
  245. */
  246. cleancache_flush_inode(mapping);
  247. }
  248. EXPORT_SYMBOL(invalidate_bdev);
  249. /*
  250. * Kick the writeback threads then try to free up some ZONE_NORMAL memory.
  251. */
  252. static void free_more_memory(void)
  253. {
  254. struct zone *zone;
  255. int nid;
  256. wakeup_flusher_threads(1024);
  257. yield();
  258. for_each_online_node(nid) {
  259. (void)first_zones_zonelist(node_zonelist(nid, GFP_NOFS),
  260. gfp_zone(GFP_NOFS), NULL,
  261. &zone);
  262. if (zone)
  263. try_to_free_pages(node_zonelist(nid, GFP_NOFS), 0,
  264. GFP_NOFS, NULL);
  265. }
  266. }
  267. /*
  268. * I/O completion handler for block_read_full_page() - pages
  269. * which come unlocked at the end of I/O.
  270. */
  271. static void end_buffer_async_read(struct buffer_head *bh, int uptodate)
  272. {
  273. unsigned long flags;
  274. struct buffer_head *first;
  275. struct buffer_head *tmp;
  276. struct page *page;
  277. int page_uptodate = 1;
  278. BUG_ON(!buffer_async_read(bh));
  279. page = bh->b_page;
  280. if (uptodate) {
  281. set_buffer_uptodate(bh);
  282. } else {
  283. clear_buffer_uptodate(bh);
  284. if (!quiet_error(bh))
  285. buffer_io_error(bh);
  286. SetPageError(page);
  287. }
  288. /*
  289. * Be _very_ careful from here on. Bad things can happen if
  290. * two buffer heads end IO at almost the same time and both
  291. * decide that the page is now completely done.
  292. */
  293. first = page_buffers(page);
  294. local_irq_save(flags);
  295. bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
  296. clear_buffer_async_read(bh);
  297. unlock_buffer(bh);
  298. tmp = bh;
  299. do {
  300. if (!buffer_uptodate(tmp))
  301. page_uptodate = 0;
  302. if (buffer_async_read(tmp)) {
  303. BUG_ON(!buffer_locked(tmp));
  304. goto still_busy;
  305. }
  306. tmp = tmp->b_this_page;
  307. } while (tmp != bh);
  308. bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  309. local_irq_restore(flags);
  310. /*
  311. * If none of the buffers had errors and they are all
  312. * uptodate then we can set the page uptodate.
  313. */
  314. if (page_uptodate && !PageError(page))
  315. SetPageUptodate(page);
  316. unlock_page(page);
  317. return;
  318. still_busy:
  319. bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  320. local_irq_restore(flags);
  321. return;
  322. }
  323. /*
  324. * Completion handler for block_write_full_page() - pages which are unlocked
  325. * during I/O, and which have PageWriteback cleared upon I/O completion.
  326. */
  327. void end_buffer_async_write(struct buffer_head *bh, int uptodate)
  328. {
  329. char b[BDEVNAME_SIZE];
  330. unsigned long flags;
  331. struct buffer_head *first;
  332. struct buffer_head *tmp;
  333. struct page *page;
  334. BUG_ON(!buffer_async_write(bh));
  335. page = bh->b_page;
  336. if (uptodate) {
  337. set_buffer_uptodate(bh);
  338. } else {
  339. if (!quiet_error(bh)) {
  340. buffer_io_error(bh);
  341. printk(KERN_WARNING "lost page write due to "
  342. "I/O error on %s\n",
  343. bdevname(bh->b_bdev, b));
  344. }
  345. set_bit(AS_EIO, &page->mapping->flags);
  346. set_buffer_write_io_error(bh);
  347. clear_buffer_uptodate(bh);
  348. SetPageError(page);
  349. }
  350. first = page_buffers(page);
  351. local_irq_save(flags);
  352. bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
  353. clear_buffer_async_write(bh);
  354. unlock_buffer(bh);
  355. tmp = bh->b_this_page;
  356. while (tmp != bh) {
  357. if (buffer_async_write(tmp)) {
  358. BUG_ON(!buffer_locked(tmp));
  359. goto still_busy;
  360. }
  361. tmp = tmp->b_this_page;
  362. }
  363. bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  364. local_irq_restore(flags);
  365. end_page_writeback(page);
  366. return;
  367. still_busy:
  368. bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
  369. local_irq_restore(flags);
  370. return;
  371. }
  372. EXPORT_SYMBOL(end_buffer_async_write);
  373. /*
  374. * If a page's buffers are under async readin (end_buffer_async_read
  375. * completion) then there is a possibility that another thread of
  376. * control could lock one of the buffers after it has completed
  377. * but while some of the other buffers have not completed. This
  378. * locked buffer would confuse end_buffer_async_read() into not unlocking
  379. * the page. So the absence of BH_Async_Read tells end_buffer_async_read()
  380. * that this buffer is not under async I/O.
  381. *
  382. * The page comes unlocked when it has no locked buffer_async buffers
  383. * left.
  384. *
  385. * PageLocked prevents anyone starting new async I/O reads any of
  386. * the buffers.
  387. *
  388. * PageWriteback is used to prevent simultaneous writeout of the same
  389. * page.
  390. *
  391. * PageLocked prevents anyone from starting writeback of a page which is
  392. * under read I/O (PageWriteback is only ever set against a locked page).
  393. */
  394. static void mark_buffer_async_read(struct buffer_head *bh)
  395. {
  396. bh->b_end_io = end_buffer_async_read;
  397. set_buffer_async_read(bh);
  398. }
  399. static void mark_buffer_async_write_endio(struct buffer_head *bh,
  400. bh_end_io_t *handler)
  401. {
  402. bh->b_end_io = handler;
  403. set_buffer_async_write(bh);
  404. }
  405. void mark_buffer_async_write(struct buffer_head *bh)
  406. {
  407. mark_buffer_async_write_endio(bh, end_buffer_async_write);
  408. }
  409. EXPORT_SYMBOL(mark_buffer_async_write);
  410. /*
  411. * fs/buffer.c contains helper functions for buffer-backed address space's
  412. * fsync functions. A common requirement for buffer-based filesystems is
  413. * that certain data from the backing blockdev needs to be written out for
  414. * a successful fsync(). For example, ext2 indirect blocks need to be
  415. * written back and waited upon before fsync() returns.
  416. *
  417. * The functions mark_buffer_inode_dirty(), fsync_inode_buffers(),
  418. * inode_has_buffers() and invalidate_inode_buffers() are provided for the
  419. * management of a list of dependent buffers at ->i_mapping->private_list.
  420. *
  421. * Locking is a little subtle: try_to_free_buffers() will remove buffers
  422. * from their controlling inode's queue when they are being freed. But
  423. * try_to_free_buffers() will be operating against the *blockdev* mapping
  424. * at the time, not against the S_ISREG file which depends on those buffers.
  425. * So the locking for private_list is via the private_lock in the address_space
  426. * which backs the buffers. Which is different from the address_space
  427. * against which the buffers are listed. So for a particular address_space,
  428. * mapping->private_lock does *not* protect mapping->private_list! In fact,
  429. * mapping->private_list will always be protected by the backing blockdev's
  430. * ->private_lock.
  431. *
  432. * Which introduces a requirement: all buffers on an address_space's
  433. * ->private_list must be from the same address_space: the blockdev's.
  434. *
  435. * address_spaces which do not place buffers at ->private_list via these
  436. * utility functions are free to use private_lock and private_list for
  437. * whatever they want. The only requirement is that list_empty(private_list)
  438. * be true at clear_inode() time.
  439. *
  440. * FIXME: clear_inode should not call invalidate_inode_buffers(). The
  441. * filesystems should do that. invalidate_inode_buffers() should just go
  442. * BUG_ON(!list_empty).
  443. *
  444. * FIXME: mark_buffer_dirty_inode() is a data-plane operation. It should
  445. * take an address_space, not an inode. And it should be called
  446. * mark_buffer_dirty_fsync() to clearly define why those buffers are being
  447. * queued up.
  448. *
  449. * FIXME: mark_buffer_dirty_inode() doesn't need to add the buffer to the
  450. * list if it is already on a list. Because if the buffer is on a list,
  451. * it *must* already be on the right one. If not, the filesystem is being
  452. * silly. This will save a ton of locking. But first we have to ensure
  453. * that buffers are taken *off* the old inode's list when they are freed
  454. * (presumably in truncate). That requires careful auditing of all
  455. * filesystems (do it inside bforget()). It could also be done by bringing
  456. * b_inode back.
  457. */
  458. /*
  459. * The buffer's backing address_space's private_lock must be held
  460. */
  461. static void __remove_assoc_queue(struct buffer_head *bh)
  462. {
  463. list_del_init(&bh->b_assoc_buffers);
  464. WARN_ON(!bh->b_assoc_map);
  465. if (buffer_write_io_error(bh))
  466. set_bit(AS_EIO, &bh->b_assoc_map->flags);
  467. bh->b_assoc_map = NULL;
  468. }
  469. int inode_has_buffers(struct inode *inode)
  470. {
  471. return !list_empty(&inode->i_data.private_list);
  472. }
  473. /*
  474. * osync is designed to support O_SYNC io. It waits synchronously for
  475. * all already-submitted IO to complete, but does not queue any new
  476. * writes to the disk.
  477. *
  478. * To do O_SYNC writes, just queue the buffer writes with ll_rw_block as
  479. * you dirty the buffers, and then use osync_inode_buffers to wait for
  480. * completion. Any other dirty buffers which are not yet queued for
  481. * write will not be flushed to disk by the osync.
  482. */
  483. static int osync_buffers_list(spinlock_t *lock, struct list_head *list)
  484. {
  485. struct buffer_head *bh;
  486. struct list_head *p;
  487. int err = 0;
  488. spin_lock(lock);
  489. repeat:
  490. list_for_each_prev(p, list) {
  491. bh = BH_ENTRY(p);
  492. if (buffer_locked(bh)) {
  493. get_bh(bh);
  494. spin_unlock(lock);
  495. wait_on_buffer(bh);
  496. if (!buffer_uptodate(bh))
  497. err = -EIO;
  498. brelse(bh);
  499. spin_lock(lock);
  500. goto repeat;
  501. }
  502. }
  503. spin_unlock(lock);
  504. return err;
  505. }
  506. static void do_thaw_one(struct super_block *sb, void *unused)
  507. {
  508. char b[BDEVNAME_SIZE];
  509. while (sb->s_bdev && !thaw_bdev(sb->s_bdev, sb))
  510. printk(KERN_WARNING "Emergency Thaw on %s\n",
  511. bdevname(sb->s_bdev, b));
  512. }
  513. static void do_thaw_all(struct work_struct *work)
  514. {
  515. iterate_supers(do_thaw_one, NULL);
  516. kfree(work);
  517. printk(KERN_WARNING "Emergency Thaw complete\n");
  518. }
  519. /**
  520. * emergency_thaw_all -- forcibly thaw every frozen filesystem
  521. *
  522. * Used for emergency unfreeze of all filesystems via SysRq
  523. */
  524. void emergency_thaw_all(void)
  525. {
  526. struct work_struct *work;
  527. work = kmalloc(sizeof(*work), GFP_ATOMIC);
  528. if (work) {
  529. INIT_WORK(work, do_thaw_all);
  530. schedule_work(work);
  531. }
  532. }
  533. /**
  534. * sync_mapping_buffers - write out & wait upon a mapping's "associated" buffers
  535. * @mapping: the mapping which wants those buffers written
  536. *
  537. * Starts I/O against the buffers at mapping->private_list, and waits upon
  538. * that I/O.
  539. *
  540. * Basically, this is a convenience function for fsync().
  541. * @mapping is a file or directory which needs those buffers to be written for
  542. * a successful fsync().
  543. */
  544. int sync_mapping_buffers(struct address_space *mapping)
  545. {
  546. struct address_space *buffer_mapping = mapping->assoc_mapping;
  547. if (buffer_mapping == NULL || list_empty(&mapping->private_list))
  548. return 0;
  549. return fsync_buffers_list(&buffer_mapping->private_lock,
  550. &mapping->private_list);
  551. }
  552. EXPORT_SYMBOL(sync_mapping_buffers);
  553. /*
  554. * Called when we've recently written block `bblock', and it is known that
  555. * `bblock' was for a buffer_boundary() buffer. This means that the block at
  556. * `bblock + 1' is probably a dirty indirect block. Hunt it down and, if it's
  557. * dirty, schedule it for IO. So that indirects merge nicely with their data.
  558. */
  559. void write_boundary_block(struct block_device *bdev,
  560. sector_t bblock, unsigned blocksize)
  561. {
  562. struct buffer_head *bh = __find_get_block(bdev, bblock + 1, blocksize);
  563. if (bh) {
  564. if (buffer_dirty(bh))
  565. ll_rw_block(WRITE, 1, &bh);
  566. put_bh(bh);
  567. }
  568. }
  569. void mark_buffer_dirty_inode(struct buffer_head *bh, struct inode *inode)
  570. {
  571. struct address_space *mapping = inode->i_mapping;
  572. struct address_space *buffer_mapping = bh->b_page->mapping;
  573. mark_buffer_dirty(bh);
  574. if (!mapping->assoc_mapping) {
  575. mapping->assoc_mapping = buffer_mapping;
  576. } else {
  577. BUG_ON(mapping->assoc_mapping != buffer_mapping);
  578. }
  579. if (!bh->b_assoc_map) {
  580. spin_lock(&buffer_mapping->private_lock);
  581. list_move_tail(&bh->b_assoc_buffers,
  582. &mapping->private_list);
  583. bh->b_assoc_map = mapping;
  584. spin_unlock(&buffer_mapping->private_lock);
  585. }
  586. }
  587. EXPORT_SYMBOL(mark_buffer_dirty_inode);
  588. /*
  589. * Mark the page dirty, and set it dirty in the radix tree, and mark the inode
  590. * dirty.
  591. *
  592. * If warn is true, then emit a warning if the page is not uptodate and has
  593. * not been truncated.
  594. */
  595. static void __set_page_dirty(struct page *page,
  596. struct address_space *mapping, int warn)
  597. {
  598. spin_lock_irq(&mapping->tree_lock);
  599. if (page->mapping) { /* Race with truncate? */
  600. WARN_ON_ONCE(warn && !PageUptodate(page));
  601. account_page_dirtied(page, mapping);
  602. radix_tree_tag_set(&mapping->page_tree,
  603. page_index(page), PAGECACHE_TAG_DIRTY);
  604. }
  605. spin_unlock_irq(&mapping->tree_lock);
  606. __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
  607. }
  608. /*
  609. * Add a page to the dirty page list.
  610. *
  611. * It is a sad fact of life that this function is called from several places
  612. * deeply under spinlocking. It may not sleep.
  613. *
  614. * If the page has buffers, the uptodate buffers are set dirty, to preserve
  615. * dirty-state coherency between the page and the buffers. It the page does
  616. * not have buffers then when they are later attached they will all be set
  617. * dirty.
  618. *
  619. * The buffers are dirtied before the page is dirtied. There's a small race
  620. * window in which a writepage caller may see the page cleanness but not the
  621. * buffer dirtiness. That's fine. If this code were to set the page dirty
  622. * before the buffers, a concurrent writepage caller could clear the page dirty
  623. * bit, see a bunch of clean buffers and we'd end up with dirty buffers/clean
  624. * page on the dirty page list.
  625. *
  626. * We use private_lock to lock against try_to_free_buffers while using the
  627. * page's buffer list. Also use this to protect against clean buffers being
  628. * added to the page after it was set dirty.
  629. *
  630. * FIXME: may need to call ->reservepage here as well. That's rather up to the
  631. * address_space though.
  632. */
  633. int __set_page_dirty_buffers(struct page *page)
  634. {
  635. int newly_dirty;
  636. struct address_space *mapping = page_mapping(page);
  637. if (unlikely(!mapping))
  638. return !TestSetPageDirty(page);
  639. spin_lock(&mapping->private_lock);
  640. if (page_has_buffers(page)) {
  641. struct buffer_head *head = page_buffers(page);
  642. struct buffer_head *bh = head;
  643. do {
  644. set_buffer_dirty(bh);
  645. bh = bh->b_this_page;
  646. } while (bh != head);
  647. }
  648. newly_dirty = !TestSetPageDirty(page);
  649. spin_unlock(&mapping->private_lock);
  650. if (newly_dirty)
  651. __set_page_dirty(page, mapping, 1);
  652. return newly_dirty;
  653. }
  654. EXPORT_SYMBOL(__set_page_dirty_buffers);
  655. /*
  656. * Write out and wait upon a list of buffers.
  657. *
  658. * We have conflicting pressures: we want to make sure that all
  659. * initially dirty buffers get waited on, but that any subsequently
  660. * dirtied buffers don't. After all, we don't want fsync to last
  661. * forever if somebody is actively writing to the file.
  662. *
  663. * Do this in two main stages: first we copy dirty buffers to a
  664. * temporary inode list, queueing the writes as we go. Then we clean
  665. * up, waiting for those writes to complete.
  666. *
  667. * During this second stage, any subsequent updates to the file may end
  668. * up refiling the buffer on the original inode's dirty list again, so
  669. * there is a chance we will end up with a buffer queued for write but
  670. * not yet completed on that list. So, as a final cleanup we go through
  671. * the osync code to catch these locked, dirty buffers without requeuing
  672. * any newly dirty buffers for write.
  673. */
  674. static int fsync_buffers_list(spinlock_t *lock, struct list_head *list)
  675. {
  676. struct buffer_head *bh;
  677. struct list_head tmp;
  678. struct address_space *mapping;
  679. int err = 0, err2;
  680. struct blk_plug plug;
  681. INIT_LIST_HEAD(&tmp);
  682. blk_start_plug(&plug);
  683. spin_lock(lock);
  684. while (!list_empty(list)) {
  685. bh = BH_ENTRY(list->next);
  686. mapping = bh->b_assoc_map;
  687. __remove_assoc_queue(bh);
  688. /* Avoid race with mark_buffer_dirty_inode() which does
  689. * a lockless check and we rely on seeing the dirty bit */
  690. smp_mb();
  691. if (buffer_dirty(bh) || buffer_locked(bh)) {
  692. list_add(&bh->b_assoc_buffers, &tmp);
  693. bh->b_assoc_map = mapping;
  694. if (buffer_dirty(bh)) {
  695. get_bh(bh);
  696. spin_unlock(lock);
  697. /*
  698. * Ensure any pending I/O completes so that
  699. * write_dirty_buffer() actually writes the
  700. * current contents - it is a noop if I/O is
  701. * still in flight on potentially older
  702. * contents.
  703. */
  704. write_dirty_buffer(bh, WRITE_SYNC);
  705. /*
  706. * Kick off IO for the previous mapping. Note
  707. * that we will not run the very last mapping,
  708. * wait_on_buffer() will do that for us
  709. * through sync_buffer().
  710. */
  711. brelse(bh);
  712. spin_lock(lock);
  713. }
  714. }
  715. }
  716. spin_unlock(lock);
  717. blk_finish_plug(&plug);
  718. spin_lock(lock);
  719. while (!list_empty(&tmp)) {
  720. bh = BH_ENTRY(tmp.prev);
  721. get_bh(bh);
  722. mapping = bh->b_assoc_map;
  723. __remove_assoc_queue(bh);
  724. /* Avoid race with mark_buffer_dirty_inode() which does
  725. * a lockless check and we rely on seeing the dirty bit */
  726. smp_mb();
  727. if (buffer_dirty(bh)) {
  728. list_add(&bh->b_assoc_buffers,
  729. &mapping->private_list);
  730. bh->b_assoc_map = mapping;
  731. }
  732. spin_unlock(lock);
  733. wait_on_buffer(bh);
  734. if (!buffer_uptodate(bh))
  735. err = -EIO;
  736. brelse(bh);
  737. spin_lock(lock);
  738. }
  739. spin_unlock(lock);
  740. err2 = osync_buffers_list(lock, list);
  741. if (err)
  742. return err;
  743. else
  744. return err2;
  745. }
  746. /*
  747. * Invalidate any and all dirty buffers on a given inode. We are
  748. * probably unmounting the fs, but that doesn't mean we have already
  749. * done a sync(). Just drop the buffers from the inode list.
  750. *
  751. * NOTE: we take the inode's blockdev's mapping's private_lock. Which
  752. * assumes that all the buffers are against the blockdev. Not true
  753. * for reiserfs.
  754. */
  755. void invalidate_inode_buffers(struct inode *inode)
  756. {
  757. if (inode_has_buffers(inode)) {
  758. struct address_space *mapping = &inode->i_data;
  759. struct list_head *list = &mapping->private_list;
  760. struct address_space *buffer_mapping = mapping->assoc_mapping;
  761. spin_lock(&buffer_mapping->private_lock);
  762. while (!list_empty(list))
  763. __remove_assoc_queue(BH_ENTRY(list->next));
  764. spin_unlock(&buffer_mapping->private_lock);
  765. }
  766. }
  767. EXPORT_SYMBOL(invalidate_inode_buffers);
  768. /*
  769. * Remove any clean buffers from the inode's buffer list. This is called
  770. * when we're trying to free the inode itself. Those buffers can pin it.
  771. *
  772. * Returns true if all buffers were removed.
  773. */
  774. int remove_inode_buffers(struct inode *inode)
  775. {
  776. int ret = 1;
  777. if (inode_has_buffers(inode)) {
  778. struct address_space *mapping = &inode->i_data;
  779. struct list_head *list = &mapping->private_list;
  780. struct address_space *buffer_mapping = mapping->assoc_mapping;
  781. spin_lock(&buffer_mapping->private_lock);
  782. while (!list_empty(list)) {
  783. struct buffer_head *bh = BH_ENTRY(list->next);
  784. if (buffer_dirty(bh)) {
  785. ret = 0;
  786. break;
  787. }
  788. __remove_assoc_queue(bh);
  789. }
  790. spin_unlock(&buffer_mapping->private_lock);
  791. }
  792. return ret;
  793. }
  794. /*
  795. * Create the appropriate buffers when given a page for data area and
  796. * the size of each buffer.. Use the bh->b_this_page linked list to
  797. * follow the buffers created. Return NULL if unable to create more
  798. * buffers.
  799. *
  800. * The retry flag is used to differentiate async IO (paging, swapping)
  801. * which may not fail from ordinary buffer allocations.
  802. */
  803. struct buffer_head *alloc_page_buffers(struct page *page, unsigned long size,
  804. int retry)
  805. {
  806. struct buffer_head *bh, *head;
  807. long offset;
  808. try_again:
  809. head = NULL;
  810. offset = PAGE_SIZE;
  811. while ((offset -= size) >= 0) {
  812. bh = alloc_buffer_head(GFP_NOFS);
  813. if (!bh)
  814. goto no_grow;
  815. bh->b_bdev = NULL;
  816. bh->b_this_page = head;
  817. bh->b_blocknr = -1;
  818. head = bh;
  819. bh->b_state = 0;
  820. atomic_set(&bh->b_count, 0);
  821. bh->b_size = size;
  822. /* Link the buffer to its page */
  823. set_bh_page(bh, page, offset);
  824. init_buffer(bh, NULL, NULL);
  825. }
  826. return head;
  827. /*
  828. * In case anything failed, we just free everything we got.
  829. */
  830. no_grow:
  831. if (head) {
  832. do {
  833. bh = head;
  834. head = head->b_this_page;
  835. free_buffer_head(bh);
  836. } while (head);
  837. }
  838. /*
  839. * Return failure for non-async IO requests. Async IO requests
  840. * are not allowed to fail, so we have to wait until buffer heads
  841. * become available. But we don't want tasks sleeping with
  842. * partially complete buffers, so all were released above.
  843. */
  844. if (!retry)
  845. return NULL;
  846. /* We're _really_ low on memory. Now we just
  847. * wait for old buffer heads to become free due to
  848. * finishing IO. Since this is an async request and
  849. * the reserve list is empty, we're sure there are
  850. * async buffer heads in use.
  851. */
  852. free_more_memory();
  853. goto try_again;
  854. }
  855. EXPORT_SYMBOL_GPL(alloc_page_buffers);
  856. static inline void
  857. link_dev_buffers(struct page *page, struct buffer_head *head)
  858. {
  859. struct buffer_head *bh, *tail;
  860. bh = head;
  861. do {
  862. tail = bh;
  863. bh = bh->b_this_page;
  864. } while (bh);
  865. tail->b_this_page = head;
  866. attach_page_buffers(page, head);
  867. }
  868. /*
  869. * Initialise the state of a blockdev page's buffers.
  870. */
  871. static void
  872. init_page_buffers(struct page *page, struct block_device *bdev,
  873. sector_t block, int size)
  874. {
  875. struct buffer_head *head = page_buffers(page);
  876. struct buffer_head *bh = head;
  877. int uptodate = PageUptodate(page);
  878. do {
  879. if (!buffer_mapped(bh)) {
  880. init_buffer(bh, NULL, NULL);
  881. bh->b_bdev = bdev;
  882. bh->b_blocknr = block;
  883. if (uptodate)
  884. set_buffer_uptodate(bh);
  885. set_buffer_mapped(bh);
  886. }
  887. block++;
  888. bh = bh->b_this_page;
  889. } while (bh != head);
  890. }
  891. /*
  892. * Create the page-cache page that contains the requested block.
  893. *
  894. * This is user purely for blockdev mappings.
  895. */
  896. static struct page *
  897. grow_dev_page(struct block_device *bdev, sector_t block,
  898. pgoff_t index, int size)
  899. {
  900. struct inode *inode = bdev->bd_inode;
  901. struct page *page;
  902. struct buffer_head *bh;
  903. page = find_or_create_page(inode->i_mapping, index,
  904. (mapping_gfp_mask(inode->i_mapping) & ~__GFP_FS)|__GFP_MOVABLE);
  905. if (!page)
  906. return NULL;
  907. BUG_ON(!PageLocked(page));
  908. if (page_has_buffers(page)) {
  909. bh = page_buffers(page);
  910. if (bh->b_size == size) {
  911. init_page_buffers(page, bdev, block, size);
  912. return page;
  913. }
  914. if (!try_to_free_buffers(page))
  915. goto failed;
  916. }
  917. /*
  918. * Allocate some buffers for this page
  919. */
  920. bh = alloc_page_buffers(page, size, 0);
  921. if (!bh)
  922. goto failed;
  923. /*
  924. * Link the page to the buffers and initialise them. Take the
  925. * lock to be atomic wrt __find_get_block(), which does not
  926. * run under the page lock.
  927. */
  928. spin_lock(&inode->i_mapping->private_lock);
  929. link_dev_buffers(page, bh);
  930. init_page_buffers(page, bdev, block, size);
  931. spin_unlock(&inode->i_mapping->private_lock);
  932. return page;
  933. failed:
  934. BUG();
  935. unlock_page(page);
  936. page_cache_release(page);
  937. return NULL;
  938. }
  939. /*
  940. * Create buffers for the specified block device block's page. If
  941. * that page was dirty, the buffers are set dirty also.
  942. */
  943. static int
  944. grow_buffers(struct block_device *bdev, sector_t block, int size)
  945. {
  946. struct page *page;
  947. pgoff_t index;
  948. int sizebits;
  949. sizebits = -1;
  950. do {
  951. sizebits++;
  952. } while ((size << sizebits) < PAGE_SIZE);
  953. index = block >> sizebits;
  954. /*
  955. * Check for a block which wants to lie outside our maximum possible
  956. * pagecache index. (this comparison is done using sector_t types).
  957. */
  958. if (unlikely(index != block >> sizebits)) {
  959. char b[BDEVNAME_SIZE];
  960. printk(KERN_ERR "%s: requested out-of-range block %llu for "
  961. "device %s\n",
  962. __func__, (unsigned long long)block,
  963. bdevname(bdev, b));
  964. return -EIO;
  965. }
  966. block = index << sizebits;
  967. /* Create a page with the proper size buffers.. */
  968. page = grow_dev_page(bdev, block, index, size);
  969. if (!page)
  970. return 0;
  971. unlock_page(page);
  972. page_cache_release(page);
  973. return 1;
  974. }
  975. static struct buffer_head *
  976. __getblk_slow(struct block_device *bdev, sector_t block, int size)
  977. {
  978. /* Size must be multiple of hard sectorsize */
  979. if (unlikely(size & (bdev_logical_block_size(bdev)-1) ||
  980. (size < 512 || size > PAGE_SIZE))) {
  981. printk(KERN_ERR "getblk(): invalid block size %d requested\n",
  982. size);
  983. printk(KERN_ERR "logical block size: %d\n",
  984. bdev_logical_block_size(bdev));
  985. dump_stack();
  986. return NULL;
  987. }
  988. for (;;) {
  989. struct buffer_head * bh;
  990. int ret;
  991. bh = __find_get_block(bdev, block, size);
  992. if (bh)
  993. return bh;
  994. ret = grow_buffers(bdev, block, size);
  995. if (ret < 0)
  996. return NULL;
  997. if (ret == 0)
  998. free_more_memory();
  999. }
  1000. }
  1001. /*
  1002. * The relationship between dirty buffers and dirty pages:
  1003. *
  1004. * Whenever a page has any dirty buffers, the page's dirty bit is set, and
  1005. * the page is tagged dirty in its radix tree.
  1006. *
  1007. * At all times, the dirtiness of the buffers represents the dirtiness of
  1008. * subsections of the page. If the page has buffers, the page dirty bit is
  1009. * merely a hint about the true dirty state.
  1010. *
  1011. * When a page is set dirty in its entirety, all its buffers are marked dirty
  1012. * (if the page has buffers).
  1013. *
  1014. * When a buffer is marked dirty, its page is dirtied, but the page's other
  1015. * buffers are not.
  1016. *
  1017. * Also. When blockdev buffers are explicitly read with bread(), they
  1018. * individually become uptodate. But their backing page remains not
  1019. * uptodate - even if all of its buffers are uptodate. A subsequent
  1020. * block_read_full_page() against that page will discover all the uptodate
  1021. * buffers, will set the page uptodate and will perform no I/O.
  1022. */
  1023. /**
  1024. * mark_buffer_dirty - mark a buffer_head as needing writeout
  1025. * @bh: the buffer_head to mark dirty
  1026. *
  1027. * mark_buffer_dirty() will set the dirty bit against the buffer, then set its
  1028. * backing page dirty, then tag the page as dirty in its address_space's radix
  1029. * tree and then attach the address_space's inode to its superblock's dirty
  1030. * inode list.
  1031. *
  1032. * mark_buffer_dirty() is atomic. It takes bh->b_page->mapping->private_lock,
  1033. * mapping->tree_lock and mapping->host->i_lock.
  1034. */
  1035. void mark_buffer_dirty(struct buffer_head *bh)
  1036. {
  1037. WARN_ON_ONCE(!buffer_uptodate(bh));
  1038. /*
  1039. * Very *carefully* optimize the it-is-already-dirty case.
  1040. *
  1041. * Don't let the final "is it dirty" escape to before we
  1042. * perhaps modified the buffer.
  1043. */
  1044. if (buffer_dirty(bh)) {
  1045. smp_mb();
  1046. if (buffer_dirty(bh))
  1047. return;
  1048. }
  1049. if (!test_set_buffer_dirty(bh)) {
  1050. struct page *page = bh->b_page;
  1051. if (!TestSetPageDirty(page)) {
  1052. struct address_space *mapping = page_mapping(page);
  1053. if (mapping)
  1054. __set_page_dirty(page, mapping, 0);
  1055. }
  1056. }
  1057. }
  1058. EXPORT_SYMBOL(mark_buffer_dirty);
  1059. /*
  1060. * Decrement a buffer_head's reference count. If all buffers against a page
  1061. * have zero reference count, are clean and unlocked, and if the page is clean
  1062. * and unlocked then try_to_free_buffers() may strip the buffers from the page
  1063. * in preparation for freeing it (sometimes, rarely, buffers are removed from
  1064. * a page but it ends up not being freed, and buffers may later be reattached).
  1065. */
  1066. void __brelse(struct buffer_head * buf)
  1067. {
  1068. if (atomic_read(&buf->b_count)) {
  1069. put_bh(buf);
  1070. return;
  1071. }
  1072. WARN(1, KERN_ERR "VFS: brelse: Trying to free free buffer\n");
  1073. }
  1074. EXPORT_SYMBOL(__brelse);
  1075. /*
  1076. * bforget() is like brelse(), except it discards any
  1077. * potentially dirty data.
  1078. */
  1079. void __bforget(struct buffer_head *bh)
  1080. {
  1081. clear_buffer_dirty(bh);
  1082. if (bh->b_assoc_map) {
  1083. struct address_space *buffer_mapping = bh->b_page->mapping;
  1084. spin_lock(&buffer_mapping->private_lock);
  1085. list_del_init(&bh->b_assoc_buffers);
  1086. bh->b_assoc_map = NULL;
  1087. spin_unlock(&buffer_mapping->private_lock);
  1088. }
  1089. __brelse(bh);
  1090. }
  1091. EXPORT_SYMBOL(__bforget);
  1092. static struct buffer_head *__bread_slow(struct buffer_head *bh)
  1093. {
  1094. lock_buffer(bh);
  1095. if (buffer_uptodate(bh)) {
  1096. unlock_buffer(bh);
  1097. return bh;
  1098. } else {
  1099. get_bh(bh);
  1100. bh->b_end_io = end_buffer_read_sync;
  1101. submit_bh(READ, bh);
  1102. wait_on_buffer(bh);
  1103. if (buffer_uptodate(bh))
  1104. return bh;
  1105. }
  1106. brelse(bh);
  1107. return NULL;
  1108. }
  1109. /*
  1110. * Per-cpu buffer LRU implementation. To reduce the cost of __find_get_block().
  1111. * The bhs[] array is sorted - newest buffer is at bhs[0]. Buffers have their
  1112. * refcount elevated by one when they're in an LRU. A buffer can only appear
  1113. * once in a particular CPU's LRU. A single buffer can be present in multiple
  1114. * CPU's LRUs at the same time.
  1115. *
  1116. * This is a transparent caching front-end to sb_bread(), sb_getblk() and
  1117. * sb_find_get_block().
  1118. *
  1119. * The LRUs themselves only need locking against invalidate_bh_lrus. We use
  1120. * a local interrupt disable for that.
  1121. */
  1122. #define BH_LRU_SIZE 8
  1123. struct bh_lru {
  1124. struct buffer_head *bhs[BH_LRU_SIZE];
  1125. };
  1126. static DEFINE_PER_CPU(struct bh_lru, bh_lrus) = {{ NULL }};
  1127. #ifdef CONFIG_SMP
  1128. #define bh_lru_lock() local_irq_disable()
  1129. #define bh_lru_unlock() local_irq_enable()
  1130. #else
  1131. #define bh_lru_lock() preempt_disable()
  1132. #define bh_lru_unlock() preempt_enable()
  1133. #endif
  1134. static inline void check_irqs_on(void)
  1135. {
  1136. #ifdef irqs_disabled
  1137. BUG_ON(irqs_disabled());
  1138. #endif
  1139. }
  1140. /*
  1141. * The LRU management algorithm is dopey-but-simple. Sorry.
  1142. */
  1143. static void bh_lru_install(struct buffer_head *bh)
  1144. {
  1145. struct buffer_head *evictee = NULL;
  1146. check_irqs_on();
  1147. bh_lru_lock();
  1148. if (__this_cpu_read(bh_lrus.bhs[0]) != bh) {
  1149. struct buffer_head *bhs[BH_LRU_SIZE];
  1150. int in;
  1151. int out = 0;
  1152. get_bh(bh);
  1153. bhs[out++] = bh;
  1154. for (in = 0; in < BH_LRU_SIZE; in++) {
  1155. struct buffer_head *bh2 =
  1156. __this_cpu_read(bh_lrus.bhs[in]);
  1157. if (bh2 == bh) {
  1158. __brelse(bh2);
  1159. } else {
  1160. if (out >= BH_LRU_SIZE) {
  1161. BUG_ON(evictee != NULL);
  1162. evictee = bh2;
  1163. } else {
  1164. bhs[out++] = bh2;
  1165. }
  1166. }
  1167. }
  1168. while (out < BH_LRU_SIZE)
  1169. bhs[out++] = NULL;
  1170. memcpy(__this_cpu_ptr(&bh_lrus.bhs), bhs, sizeof(bhs));
  1171. }
  1172. bh_lru_unlock();
  1173. if (evictee)
  1174. __brelse(evictee);
  1175. }
  1176. /*
  1177. * Look up the bh in this cpu's LRU. If it's there, move it to the head.
  1178. */
  1179. static struct buffer_head *
  1180. lookup_bh_lru(struct block_device *bdev, sector_t block, unsigned size)
  1181. {
  1182. struct buffer_head *ret = NULL;
  1183. unsigned int i;
  1184. check_irqs_on();
  1185. bh_lru_lock();
  1186. for (i = 0; i < BH_LRU_SIZE; i++) {
  1187. struct buffer_head *bh = __this_cpu_read(bh_lrus.bhs[i]);
  1188. if (bh && bh->b_bdev == bdev &&
  1189. bh->b_blocknr == block && bh->b_size == size) {
  1190. if (i) {
  1191. while (i) {
  1192. __this_cpu_write(bh_lrus.bhs[i],
  1193. __this_cpu_read(bh_lrus.bhs[i - 1]));
  1194. i--;
  1195. }
  1196. __this_cpu_write(bh_lrus.bhs[0], bh);
  1197. }
  1198. get_bh(bh);
  1199. ret = bh;
  1200. break;
  1201. }
  1202. }
  1203. bh_lru_unlock();
  1204. return ret;
  1205. }
  1206. /*
  1207. * Perform a pagecache lookup for the matching buffer. If it's there, refresh
  1208. * it in the LRU and mark it as accessed. If it is not present then return
  1209. * NULL
  1210. */
  1211. struct buffer_head *
  1212. __find_get_block(struct block_device *bdev, sector_t block, unsigned size)
  1213. {
  1214. struct buffer_head *bh = lookup_bh_lru(bdev, block, size);
  1215. if (bh == NULL) {
  1216. bh = __find_get_block_slow(bdev, block);
  1217. if (bh)
  1218. bh_lru_install(bh);
  1219. }
  1220. if (bh)
  1221. touch_buffer(bh);
  1222. return bh;
  1223. }
  1224. EXPORT_SYMBOL(__find_get_block);
  1225. /*
  1226. * __getblk will locate (and, if necessary, create) the buffer_head
  1227. * which corresponds to the passed block_device, block and size. The
  1228. * returned buffer has its reference count incremented.
  1229. *
  1230. * __getblk() cannot fail - it just keeps trying. If you pass it an
  1231. * illegal block number, __getblk() will happily return a buffer_head
  1232. * which represents the non-existent block. Very weird.
  1233. *
  1234. * __getblk() will lock up the machine if grow_dev_page's try_to_free_buffers()
  1235. * attempt is failing. FIXME, perhaps?
  1236. */
  1237. struct buffer_head *
  1238. __getblk(struct block_device *bdev, sector_t block, unsigned size)
  1239. {
  1240. struct buffer_head *bh = __find_get_block(bdev, block, size);
  1241. might_sleep();
  1242. if (bh == NULL)
  1243. bh = __getblk_slow(bdev, block, size);
  1244. return bh;
  1245. }
  1246. EXPORT_SYMBOL(__getblk);
  1247. /*
  1248. * Do async read-ahead on a buffer..
  1249. */
  1250. void __breadahead(struct block_device *bdev, sector_t block, unsigned size)
  1251. {
  1252. struct buffer_head *bh = __getblk(bdev, block, size);
  1253. if (likely(bh)) {
  1254. ll_rw_block(READA, 1, &bh);
  1255. brelse(bh);
  1256. }
  1257. }
  1258. EXPORT_SYMBOL(__breadahead);
  1259. /**
  1260. * __bread() - reads a specified block and returns the bh
  1261. * @bdev: the block_device to read from
  1262. * @block: number of block
  1263. * @size: size (in bytes) to read
  1264. *
  1265. * Reads a specified block, and returns buffer head that contains it.
  1266. * It returns NULL if the block was unreadable.
  1267. */
  1268. struct buffer_head *
  1269. __bread(struct block_device *bdev, sector_t block, unsigned size)
  1270. {
  1271. struct buffer_head *bh = __getblk(bdev, block, size);
  1272. if (likely(bh) && !buffer_uptodate(bh))
  1273. bh = __bread_slow(bh);
  1274. return bh;
  1275. }
  1276. EXPORT_SYMBOL(__bread);
  1277. /*
  1278. * invalidate_bh_lrus() is called rarely - but not only at unmount.
  1279. * This doesn't race because it runs in each cpu either in irq
  1280. * or with preempt disabled.
  1281. */
  1282. static void invalidate_bh_lru(void *arg)
  1283. {
  1284. struct bh_lru *b = &get_cpu_var(bh_lrus);
  1285. int i;
  1286. for (i = 0; i < BH_LRU_SIZE; i++) {
  1287. brelse(b->bhs[i]);
  1288. b->bhs[i] = NULL;
  1289. }
  1290. put_cpu_var(bh_lrus);
  1291. }
  1292. void invalidate_bh_lrus(void)
  1293. {
  1294. on_each_cpu(invalidate_bh_lru, NULL, 1);
  1295. }
  1296. EXPORT_SYMBOL_GPL(invalidate_bh_lrus);
  1297. void set_bh_page(struct buffer_head *bh,
  1298. struct page *page, unsigned long offset)
  1299. {
  1300. bh->b_page = page;
  1301. BUG_ON(offset >= PAGE_SIZE);
  1302. if (PageHighMem(page))
  1303. /*
  1304. * This catches illegal uses and preserves the offset:
  1305. */
  1306. bh->b_data = (char *)(0 + offset);
  1307. else
  1308. bh->b_data = page_address(page) + offset;
  1309. }
  1310. EXPORT_SYMBOL(set_bh_page);
  1311. /*
  1312. * Called when truncating a buffer on a page completely.
  1313. */
  1314. static void discard_buffer(struct buffer_head * bh)
  1315. {
  1316. lock_buffer(bh);
  1317. clear_buffer_dirty(bh);
  1318. bh->b_bdev = NULL;
  1319. clear_buffer_mapped(bh);
  1320. clear_buffer_req(bh);
  1321. clear_buffer_new(bh);
  1322. clear_buffer_delay(bh);
  1323. clear_buffer_unwritten(bh);
  1324. unlock_buffer(bh);
  1325. }
  1326. /**
  1327. * block_invalidatepage - invalidate part of all of a buffer-backed page
  1328. *
  1329. * @page: the page which is affected
  1330. * @offset: the index of the truncation point
  1331. *
  1332. * block_invalidatepage() is called when all or part of the page has become
  1333. * invalidatedby a truncate operation.
  1334. *
  1335. * block_invalidatepage() does not have to release all buffers, but it must
  1336. * ensure that no dirty buffer is left outside @offset and that no I/O
  1337. * is underway against any of the blocks which are outside the truncation
  1338. * point. Because the caller is about to free (and possibly reuse) those
  1339. * blocks on-disk.
  1340. */
  1341. void block_invalidatepage(struct page *page, unsigned long offset)
  1342. {
  1343. struct buffer_head *head, *bh, *next;
  1344. unsigned int curr_off = 0;
  1345. BUG_ON(!PageLocked(page));
  1346. if (!page_has_buffers(page))
  1347. goto out;
  1348. head = page_buffers(page);
  1349. bh = head;
  1350. do {
  1351. unsigned int next_off = curr_off + bh->b_size;
  1352. next = bh->b_this_page;
  1353. /*
  1354. * is this block fully invalidated?
  1355. */
  1356. if (offset <= curr_off)
  1357. discard_buffer(bh);
  1358. curr_off = next_off;
  1359. bh = next;
  1360. } while (bh != head);
  1361. /*
  1362. * We release buffers only if the entire page is being invalidated.
  1363. * The get_block cached value has been unconditionally invalidated,
  1364. * so real IO is not possible anymore.
  1365. */
  1366. if (offset == 0)
  1367. try_to_release_page(page, 0);
  1368. out:
  1369. return;
  1370. }
  1371. EXPORT_SYMBOL(block_invalidatepage);
  1372. /*
  1373. * We attach and possibly dirty the buffers atomically wrt
  1374. * __set_page_dirty_buffers() via private_lock. try_to_free_buffers
  1375. * is already excluded via the page lock.
  1376. */
  1377. void create_empty_buffers(struct page *page,
  1378. unsigned long blocksize, unsigned long b_state)
  1379. {
  1380. struct buffer_head *bh, *head, *tail;
  1381. head = alloc_page_buffers(page, blocksize, 1);
  1382. bh = head;
  1383. do {
  1384. bh->b_state |= b_state;
  1385. tail = bh;
  1386. bh = bh->b_this_page;
  1387. } while (bh);
  1388. tail->b_this_page = head;
  1389. spin_lock(&page->mapping->private_lock);
  1390. if (PageUptodate(page) || PageDirty(page)) {
  1391. bh = head;
  1392. do {
  1393. if (PageDirty(page))
  1394. set_buffer_dirty(bh);
  1395. if (PageUptodate(page))
  1396. set_buffer_uptodate(bh);
  1397. bh = bh->b_this_page;
  1398. } while (bh != head);
  1399. }
  1400. attach_page_buffers(page, head);
  1401. spin_unlock(&page->mapping->private_lock);
  1402. }
  1403. EXPORT_SYMBOL(create_empty_buffers);
  1404. /*
  1405. * We are taking a block for data and we don't want any output from any
  1406. * buffer-cache aliases starting from return from that function and
  1407. * until the moment when something will explicitly mark the buffer
  1408. * dirty (hopefully that will not happen until we will free that block ;-)
  1409. * We don't even need to mark it not-uptodate - nobody can expect
  1410. * anything from a newly allocated buffer anyway. We used to used
  1411. * unmap_buffer() for such invalidation, but that was wrong. We definitely
  1412. * don't want to mark the alias unmapped, for example - it would confuse
  1413. * anyone who might pick it with bread() afterwards...
  1414. *
  1415. * Also.. Note that bforget() doesn't lock the buffer. So there can
  1416. * be writeout I/O going on against recently-freed buffers. We don't
  1417. * wait on that I/O in bforget() - it's more efficient to wait on the I/O
  1418. * only if we really need to. That happens here.
  1419. */
  1420. void unmap_underlying_metadata(struct block_device *bdev, sector_t block)
  1421. {
  1422. struct buffer_head *old_bh;
  1423. might_sleep();
  1424. old_bh = __find_get_block_slow(bdev, block);
  1425. if (old_bh) {
  1426. clear_buffer_dirty(old_bh);
  1427. wait_on_buffer(old_bh);
  1428. clear_buffer_req(old_bh);
  1429. __brelse(old_bh);
  1430. }
  1431. }
  1432. EXPORT_SYMBOL(unmap_underlying_metadata);
  1433. /*
  1434. * NOTE! All mapped/uptodate combinations are valid:
  1435. *
  1436. * Mapped Uptodate Meaning
  1437. *
  1438. * No No "unknown" - must do get_block()
  1439. * No Yes "hole" - zero-filled
  1440. * Yes No "allocated" - allocated on disk, not read in
  1441. * Yes Yes "valid" - allocated and up-to-date in memory.
  1442. *
  1443. * "Dirty" is valid only with the last case (mapped+uptodate).
  1444. */
  1445. /*
  1446. * While block_write_full_page is writing back the dirty buffers under
  1447. * the page lock, whoever dirtied the buffers may decide to clean them
  1448. * again at any time. We handle that by only looking at the buffer
  1449. * state inside lock_buffer().
  1450. *
  1451. * If block_write_full_page() is called for regular writeback
  1452. * (wbc->sync_mode == WB_SYNC_NONE) then it will redirty a page which has a
  1453. * locked buffer. This only can happen if someone has written the buffer
  1454. * directly, with submit_bh(). At the address_space level PageWriteback
  1455. * prevents this contention from occurring.
  1456. *
  1457. * If block_write_full_page() is called with wbc->sync_mode ==
  1458. * WB_SYNC_ALL, the writes are posted using WRITE_SYNC; this
  1459. * causes the writes to be flagged as synchronous writes.
  1460. */
  1461. static int __block_write_full_page(struct inode *inode, struct page *page,
  1462. get_block_t *get_block, struct writeback_control *wbc,
  1463. bh_end_io_t *handler)
  1464. {
  1465. int err;
  1466. sector_t block;
  1467. sector_t last_block;
  1468. struct buffer_head *bh, *head;
  1469. const unsigned blocksize = 1 << inode->i_blkbits;
  1470. int nr_underway = 0;
  1471. int write_op = (wbc->sync_mode == WB_SYNC_ALL ?
  1472. WRITE_SYNC : WRITE);
  1473. BUG_ON(!PageLocked(page));
  1474. last_block = (i_size_read(inode) - 1) >> inode->i_blkbits;
  1475. if (!page_has_buffers(page)) {
  1476. create_empty_buffers(page, blocksize,
  1477. (1 << BH_Dirty)|(1 << BH_Uptodate));
  1478. }
  1479. /*
  1480. * Be very careful. We have no exclusion from __set_page_dirty_buffers
  1481. * here, and the (potentially unmapped) buffers may become dirty at
  1482. * any time. If a buffer becomes dirty here after we've inspected it
  1483. * then we just miss that fact, and the page stays dirty.
  1484. *
  1485. * Buffers outside i_size may be dirtied by __set_page_dirty_buffers;
  1486. * handle that here by just cleaning them.
  1487. */
  1488. block = (sector_t)page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
  1489. head = page_buffers(page);
  1490. bh = head;
  1491. /*
  1492. * Get all the dirty buffers mapped to disk addresses and
  1493. * handle any aliases from the underlying blockdev's mapping.
  1494. */
  1495. do {
  1496. if (block > last_block) {
  1497. /*
  1498. * mapped buffers outside i_size will occur, because
  1499. * this page can be outside i_size when there is a
  1500. * truncate in progress.
  1501. */
  1502. /*
  1503. * The buffer was zeroed by block_write_full_page()
  1504. */
  1505. clear_buffer_dirty(bh);
  1506. set_buffer_uptodate(bh);
  1507. } else if ((!buffer_mapped(bh) || buffer_delay(bh)) &&
  1508. buffer_dirty(bh)) {
  1509. WARN_ON(bh->b_size != blocksize);
  1510. err = get_block(inode, block, bh, 1);
  1511. if (err)
  1512. goto recover;
  1513. clear_buffer_delay(bh);
  1514. if (buffer_new(bh)) {
  1515. /* blockdev mappings never come here */
  1516. clear_buffer_new(bh);
  1517. unmap_underlying_metadata(bh->b_bdev,
  1518. bh->b_blocknr);
  1519. }
  1520. }
  1521. bh = bh->b_this_page;
  1522. block++;
  1523. } while (bh != head);
  1524. do {
  1525. if (!buffer_mapped(bh))
  1526. continue;
  1527. /*
  1528. * If it's a fully non-blocking write attempt and we cannot
  1529. * lock the buffer then redirty the page. Note that this can
  1530. * potentially cause a busy-wait loop from writeback threads
  1531. * and kswapd activity, but those code paths have their own
  1532. * higher-level throttling.
  1533. */
  1534. if (wbc->sync_mode != WB_SYNC_NONE) {
  1535. lock_buffer(bh);
  1536. } else if (!trylock_buffer(bh)) {
  1537. redirty_page_for_writepage(wbc, page);
  1538. continue;
  1539. }
  1540. if (test_clear_buffer_dirty(bh)) {
  1541. mark_buffer_async_write_endio(bh, handler);
  1542. } else {
  1543. unlock_buffer(bh);
  1544. }
  1545. } while ((bh = bh->b_this_page) != head);
  1546. /*
  1547. * The page and its buffers are protected by PageWriteback(), so we can
  1548. * drop the bh refcounts early.
  1549. */
  1550. BUG_ON(PageWriteback(page));
  1551. set_page_writeback(page);
  1552. do {
  1553. struct buffer_head *next = bh->b_this_page;
  1554. if (buffer_async_write(bh)) {
  1555. submit_bh(write_op, bh);
  1556. nr_underway++;
  1557. }
  1558. bh = next;
  1559. } while (bh != head);
  1560. unlock_page(page);
  1561. err = 0;
  1562. done:
  1563. if (nr_underway == 0) {
  1564. /*
  1565. * The page was marked dirty, but the buffers were
  1566. * clean. Someone wrote them back by hand with
  1567. * ll_rw_block/submit_bh. A rare case.
  1568. */
  1569. end_page_writeback(page);
  1570. /*
  1571. * The page and buffer_heads can be released at any time from
  1572. * here on.
  1573. */
  1574. }
  1575. return err;
  1576. recover:
  1577. /*
  1578. * ENOSPC, or some other error. We may already have added some
  1579. * blocks to the file, so we need to write these out to avoid
  1580. * exposing stale data.
  1581. * The page is currently locked and not marked for writeback
  1582. */
  1583. bh = head;
  1584. /* Recovery: lock and submit the mapped buffers */
  1585. do {
  1586. if (buffer_mapped(bh) && buffer_dirty(bh) &&
  1587. !buffer_delay(bh)) {
  1588. lock_buffer(bh);
  1589. mark_buffer_async_write_endio(bh, handler);
  1590. } else {
  1591. /*
  1592. * The buffer may have been set dirty during
  1593. * attachment to a dirty page.
  1594. */
  1595. clear_buffer_dirty(bh);
  1596. }
  1597. } while ((bh = bh->b_this_page) != head);
  1598. SetPageError(page);
  1599. BUG_ON(PageWriteback(page));
  1600. mapping_set_error(page->mapping, err);
  1601. set_page_writeback(page);
  1602. do {
  1603. struct buffer_head *next = bh->b_this_page;
  1604. if (buffer_async_write(bh)) {
  1605. clear_buffer_dirty(bh);
  1606. submit_bh(write_op, bh);
  1607. nr_underway++;
  1608. }
  1609. bh = next;
  1610. } while (bh != head);
  1611. unlock_page(page);
  1612. goto done;
  1613. }
  1614. /*
  1615. * If a page has any new buffers, zero them out here, and mark them uptodate
  1616. * and dirty so they'll be written out (in order to prevent uninitialised
  1617. * block data from leaking). And clear the new bit.
  1618. */
  1619. void page_zero_new_buffers(struct page *page, unsigned from, unsigned to)
  1620. {
  1621. unsigned int block_start, block_end;
  1622. struct buffer_head *head, *bh