1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * linux/fs/buffer.c
4 *
5 * Copyright (C) 1991, 1992, 2002 Linus Torvalds
6 */
7
8 /*
9 * Start bdflush() with kernel_thread not syscall - Paul Gortmaker, 12/95
10 *
11 * Removed a lot of unnecessary code and simplified things now that
12 * the buffer cache isn't our primary cache - Andrew Tridgell 12/96
13 *
14 * Speed up hash, lru, and free list operations. Use gfp() for allocating
15 * hash table, use SLAB cache for buffer heads. SMP threading. -DaveM
16 *
17 * Added 32k buffer block sizes - these are required older ARM systems. - RMK
18 *
19 * async buffer flushing, 1999 Andrea Arcangeli <andrea@suse.de>
20 */
21
22 #include <linux/kernel.h>
23 #include <linux/sched/signal.h>
24 #include <linux/syscalls.h>
25 #include <linux/fs.h>
26 #include <linux/iomap.h>
27 #include <linux/mm.h>
28 #include <linux/percpu.h>
29 #include <linux/slab.h>
30 #include <linux/capability.h>
31 #include <linux/blkdev.h>
32 #include <linux/file.h>
33 #include <linux/quotaops.h>
34 #include <linux/highmem.h>
35 #include <linux/export.h>
36 #include <linux/backing-dev.h>
37 #include <linux/writeback.h>
38 #include <linux/hash.h>
39 #include <linux/suspend.h>
40 #include <linux/buffer_head.h>
41 #include <linux/task_io_accounting_ops.h>
42 #include <linux/bio.h>
43 #include <linux/cpu.h>
44 #include <linux/bitops.h>
45 #include <linux/mpage.h>
46 #include <linux/bit_spinlock.h>
47 #include <linux/pagevec.h>
48 #include <linux/sched/mm.h>
49 #include <trace/events/block.h>
50 #include <linux/fscrypt.h>
51
52 static int fsync_buffers_list(spinlock_t *lock, struct list_head *list);
53 static int submit_bh_wbc(int op, int op_flags, struct buffer_head *bh,
54 enum rw_hint hint, struct writeback_control *wbc);
55
56 #define BH_ENTRY(list) list_entry((list), struct buffer_head, b_assoc_buffers)
57
touch_buffer(struct buffer_head * bh)58 inline void touch_buffer(struct buffer_head *bh)
59 {
60 trace_block_touch_buffer(bh);
61 mark_page_accessed(bh->b_page);
62 }
63 EXPORT_SYMBOL(touch_buffer);
64
__lock_buffer(struct buffer_head * bh)65 void __lock_buffer(struct buffer_head *bh)
66 {
67 wait_on_bit_lock_io(&bh->b_state, BH_Lock, TASK_UNINTERRUPTIBLE);
68 }
69 EXPORT_SYMBOL(__lock_buffer);
70
unlock_buffer(struct buffer_head * bh)71 void unlock_buffer(struct buffer_head *bh)
72 {
73 clear_bit_unlock(BH_Lock, &bh->b_state);
74 smp_mb__after_atomic();
75 wake_up_bit(&bh->b_state, BH_Lock);
76 }
77 EXPORT_SYMBOL(unlock_buffer);
78
79 /*
80 * Returns if the page has dirty or writeback buffers. If all the buffers
81 * are unlocked and clean then the PageDirty information is stale. If
82 * any of the pages are locked, it is assumed they are locked for IO.
83 */
buffer_check_dirty_writeback(struct page * page,bool * dirty,bool * writeback)84 void buffer_check_dirty_writeback(struct page *page,
85 bool *dirty, bool *writeback)
86 {
87 struct buffer_head *head, *bh;
88 *dirty = false;
89 *writeback = false;
90
91 BUG_ON(!PageLocked(page));
92
93 if (!page_has_buffers(page))
94 return;
95
96 if (PageWriteback(page))
97 *writeback = true;
98
99 head = page_buffers(page);
100 bh = head;
101 do {
102 if (buffer_locked(bh))
103 *writeback = true;
104
105 if (buffer_dirty(bh))
106 *dirty = true;
107
108 bh = bh->b_this_page;
109 } while (bh != head);
110 }
111 EXPORT_SYMBOL(buffer_check_dirty_writeback);
112
113 /*
114 * Block until a buffer comes unlocked. This doesn't stop it
115 * from becoming locked again - you have to lock it yourself
116 * if you want to preserve its state.
117 */
__wait_on_buffer(struct buffer_head * bh)118 void __wait_on_buffer(struct buffer_head * bh)
119 {
120 wait_on_bit_io(&bh->b_state, BH_Lock, TASK_UNINTERRUPTIBLE);
121 }
122 EXPORT_SYMBOL(__wait_on_buffer);
123
124 static void
__clear_page_buffers(struct page * page)125 __clear_page_buffers(struct page *page)
126 {
127 ClearPagePrivate(page);
128 set_page_private(page, 0);
129 put_page(page);
130 }
131
buffer_io_error(struct buffer_head * bh,char * msg)132 static void buffer_io_error(struct buffer_head *bh, char *msg)
133 {
134 if (!test_bit(BH_Quiet, &bh->b_state))
135 printk_ratelimited(KERN_ERR
136 "Buffer I/O error on dev %pg, logical block %llu%s\n",
137 bh->b_bdev, (unsigned long long)bh->b_blocknr, msg);
138 }
139
140 /*
141 * End-of-IO handler helper function which does not touch the bh after
142 * unlocking it.
143 * Note: unlock_buffer() sort-of does touch the bh after unlocking it, but
144 * a race there is benign: unlock_buffer() only use the bh's address for
145 * hashing after unlocking the buffer, so it doesn't actually touch the bh
146 * itself.
147 */
__end_buffer_read_notouch(struct buffer_head * bh,int uptodate)148 static void __end_buffer_read_notouch(struct buffer_head *bh, int uptodate)
149 {
150 if (uptodate) {
151 set_buffer_uptodate(bh);
152 } else {
153 /* This happens, due to failed read-ahead attempts. */
154 clear_buffer_uptodate(bh);
155 }
156 unlock_buffer(bh);
157 }
158
159 /*
160 * Default synchronous end-of-IO handler.. Just mark it up-to-date and
161 * unlock the buffer. This is what ll_rw_block uses too.
162 */
end_buffer_read_sync(struct buffer_head * bh,int uptodate)163 void end_buffer_read_sync(struct buffer_head *bh, int uptodate)
164 {
165 __end_buffer_read_notouch(bh, uptodate);
166 put_bh(bh);
167 }
168 EXPORT_SYMBOL(end_buffer_read_sync);
169
end_buffer_write_sync(struct buffer_head * bh,int uptodate)170 void end_buffer_write_sync(struct buffer_head *bh, int uptodate)
171 {
172 if (uptodate) {
173 set_buffer_uptodate(bh);
174 } else {
175 buffer_io_error(bh, ", lost sync page write");
176 mark_buffer_write_io_error(bh);
177 clear_buffer_uptodate(bh);
178 }
179 unlock_buffer(bh);
180 put_bh(bh);
181 }
182 EXPORT_SYMBOL(end_buffer_write_sync);
183
184 /*
185 * Various filesystems appear to want __find_get_block to be non-blocking.
186 * But it's the page lock which protects the buffers. To get around this,
187 * we get exclusion from try_to_free_buffers with the blockdev mapping's
188 * private_lock.
189 *
190 * Hack idea: for the blockdev mapping, private_lock contention
191 * may be quite high. This code could TryLock the page, and if that
192 * succeeds, there is no need to take private_lock.
193 */
194 static struct buffer_head *
__find_get_block_slow(struct block_device * bdev,sector_t block)195 __find_get_block_slow(struct block_device *bdev, sector_t block)
196 {
197 struct inode *bd_inode = bdev->bd_inode;
198 struct address_space *bd_mapping = bd_inode->i_mapping;
199 struct buffer_head *ret = NULL;
200 pgoff_t index;
201 struct buffer_head *bh;
202 struct buffer_head *head;
203 struct page *page;
204 int all_mapped = 1;
205 static DEFINE_RATELIMIT_STATE(last_warned, HZ, 1);
206
207 index = block >> (PAGE_SHIFT - bd_inode->i_blkbits);
208 page = find_get_page_flags(bd_mapping, index, FGP_ACCESSED);
209 if (!page)
210 goto out;
211
212 spin_lock(&bd_mapping->private_lock);
213 if (!page_has_buffers(page))
214 goto out_unlock;
215 head = page_buffers(page);
216 bh = head;
217 do {
218 if (!buffer_mapped(bh))
219 all_mapped = 0;
220 else if (bh->b_blocknr == block) {
221 ret = bh;
222 get_bh(bh);
223 goto out_unlock;
224 }
225 bh = bh->b_this_page;
226 } while (bh != head);
227
228 /* we might be here because some of the buffers on this page are
229 * not mapped. This is due to various races between
230 * file io on the block device and getblk. It gets dealt with
231 * elsewhere, don't buffer_error if we had some unmapped buffers
232 */
233 ratelimit_set_flags(&last_warned, RATELIMIT_MSG_ON_RELEASE);
234 if (all_mapped && __ratelimit(&last_warned)) {
235 printk("__find_get_block_slow() failed. block=%llu, "
236 "b_blocknr=%llu, b_state=0x%08lx, b_size=%zu, "
237 "device %pg blocksize: %d\n",
238 (unsigned long long)block,
239 (unsigned long long)bh->b_blocknr,
240 bh->b_state, bh->b_size, bdev,
241 1 << bd_inode->i_blkbits);
242 }
243 out_unlock:
244 spin_unlock(&bd_mapping->private_lock);
245 put_page(page);
246 out:
247 return ret;
248 }
249
250 /*
251 * I/O completion handler for block_read_full_page() - pages
252 * which come unlocked at the end of I/O.
253 */
end_buffer_async_read(struct buffer_head * bh,int uptodate)254 static void end_buffer_async_read(struct buffer_head *bh, int uptodate)
255 {
256 unsigned long flags;
257 struct buffer_head *first;
258 struct buffer_head *tmp;
259 struct page *page;
260 int page_uptodate = 1;
261
262 BUG_ON(!buffer_async_read(bh));
263
264 page = bh->b_page;
265 if (uptodate) {
266 set_buffer_uptodate(bh);
267 } else {
268 clear_buffer_uptodate(bh);
269 buffer_io_error(bh, ", async page read");
270 SetPageError(page);
271 }
272
273 /*
274 * Be _very_ careful from here on. Bad things can happen if
275 * two buffer heads end IO at almost the same time and both
276 * decide that the page is now completely done.
277 */
278 first = page_buffers(page);
279 local_irq_save(flags);
280 bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
281 clear_buffer_async_read(bh);
282 unlock_buffer(bh);
283 tmp = bh;
284 do {
285 if (!buffer_uptodate(tmp))
286 page_uptodate = 0;
287 if (buffer_async_read(tmp)) {
288 BUG_ON(!buffer_locked(tmp));
289 goto still_busy;
290 }
291 tmp = tmp->b_this_page;
292 } while (tmp != bh);
293 bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
294 local_irq_restore(flags);
295
296 /*
297 * If none of the buffers had errors and they are all
298 * uptodate then we can set the page uptodate.
299 */
300 if (page_uptodate && !PageError(page))
301 SetPageUptodate(page);
302 unlock_page(page);
303 return;
304
305 still_busy:
306 bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
307 local_irq_restore(flags);
308 return;
309 }
310
311 /*
312 * Completion handler for block_write_full_page() - pages which are unlocked
313 * during I/O, and which have PageWriteback cleared upon I/O completion.
314 */
end_buffer_async_write(struct buffer_head * bh,int uptodate)315 void end_buffer_async_write(struct buffer_head *bh, int uptodate)
316 {
317 unsigned long flags;
318 struct buffer_head *first;
319 struct buffer_head *tmp;
320 struct page *page;
321
322 BUG_ON(!buffer_async_write(bh));
323
324 page = bh->b_page;
325 if (uptodate) {
326 set_buffer_uptodate(bh);
327 } else {
328 buffer_io_error(bh, ", lost async page write");
329 mark_buffer_write_io_error(bh);
330 clear_buffer_uptodate(bh);
331 SetPageError(page);
332 }
333
334 first = page_buffers(page);
335 local_irq_save(flags);
336 bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
337
338 clear_buffer_async_write(bh);
339 unlock_buffer(bh);
340 tmp = bh->b_this_page;
341 while (tmp != bh) {
342 if (buffer_async_write(tmp)) {
343 BUG_ON(!buffer_locked(tmp));
344 goto still_busy;
345 }
346 tmp = tmp->b_this_page;
347 }
348 bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
349 local_irq_restore(flags);
350 end_page_writeback(page);
351 return;
352
353 still_busy:
354 bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
355 local_irq_restore(flags);
356 return;
357 }
358 EXPORT_SYMBOL(end_buffer_async_write);
359
360 /*
361 * If a page's buffers are under async readin (end_buffer_async_read
362 * completion) then there is a possibility that another thread of
363 * control could lock one of the buffers after it has completed
364 * but while some of the other buffers have not completed. This
365 * locked buffer would confuse end_buffer_async_read() into not unlocking
366 * the page. So the absence of BH_Async_Read tells end_buffer_async_read()
367 * that this buffer is not under async I/O.
368 *
369 * The page comes unlocked when it has no locked buffer_async buffers
370 * left.
371 *
372 * PageLocked prevents anyone starting new async I/O reads any of
373 * the buffers.
374 *
375 * PageWriteback is used to prevent simultaneous writeout of the same
376 * page.
377 *
378 * PageLocked prevents anyone from starting writeback of a page which is
379 * under read I/O (PageWriteback is only ever set against a locked page).
380 */
mark_buffer_async_read(struct buffer_head * bh)381 static void mark_buffer_async_read(struct buffer_head *bh)
382 {
383 bh->b_end_io = end_buffer_async_read;
384 set_buffer_async_read(bh);
385 }
386
mark_buffer_async_write_endio(struct buffer_head * bh,bh_end_io_t * handler)387 static void mark_buffer_async_write_endio(struct buffer_head *bh,
388 bh_end_io_t *handler)
389 {
390 bh->b_end_io = handler;
391 set_buffer_async_write(bh);
392 }
393
mark_buffer_async_write(struct buffer_head * bh)394 void mark_buffer_async_write(struct buffer_head *bh)
395 {
396 mark_buffer_async_write_endio(bh, end_buffer_async_write);
397 }
398 EXPORT_SYMBOL(mark_buffer_async_write);
399
400
401 /*
402 * fs/buffer.c contains helper functions for buffer-backed address space's
403 * fsync functions. A common requirement for buffer-based filesystems is
404 * that certain data from the backing blockdev needs to be written out for
405 * a successful fsync(). For example, ext2 indirect blocks need to be
406 * written back and waited upon before fsync() returns.
407 *
408 * The functions mark_buffer_inode_dirty(), fsync_inode_buffers(),
409 * inode_has_buffers() and invalidate_inode_buffers() are provided for the
410 * management of a list of dependent buffers at ->i_mapping->private_list.
411 *
412 * Locking is a little subtle: try_to_free_buffers() will remove buffers
413 * from their controlling inode's queue when they are being freed. But
414 * try_to_free_buffers() will be operating against the *blockdev* mapping
415 * at the time, not against the S_ISREG file which depends on those buffers.
416 * So the locking for private_list is via the private_lock in the address_space
417 * which backs the buffers. Which is different from the address_space
418 * against which the buffers are listed. So for a particular address_space,
419 * mapping->private_lock does *not* protect mapping->private_list! In fact,
420 * mapping->private_list will always be protected by the backing blockdev's
421 * ->private_lock.
422 *
423 * Which introduces a requirement: all buffers on an address_space's
424 * ->private_list must be from the same address_space: the blockdev's.
425 *
426 * address_spaces which do not place buffers at ->private_list via these
427 * utility functions are free to use private_lock and private_list for
428 * whatever they want. The only requirement is that list_empty(private_list)
429 * be true at clear_inode() time.
430 *
431 * FIXME: clear_inode should not call invalidate_inode_buffers(). The
432 * filesystems should do that. invalidate_inode_buffers() should just go
433 * BUG_ON(!list_empty).
434 *
435 * FIXME: mark_buffer_dirty_inode() is a data-plane operation. It should
436 * take an address_space, not an inode. And it should be called
437 * mark_buffer_dirty_fsync() to clearly define why those buffers are being
438 * queued up.
439 *
440 * FIXME: mark_buffer_dirty_inode() doesn't need to add the buffer to the
441 * list if it is already on a list. Because if the buffer is on a list,
442 * it *must* already be on the right one. If not, the filesystem is being
443 * silly. This will save a ton of locking. But first we have to ensure
444 * that buffers are taken *off* the old inode's list when they are freed
445 * (presumably in truncate). That requires careful auditing of all
446 * filesystems (do it inside bforget()). It could also be done by bringing
447 * b_inode back.
448 */
449
450 /*
451 * The buffer's backing address_space's private_lock must be held
452 */
__remove_assoc_queue(struct buffer_head * bh)453 static void __remove_assoc_queue(struct buffer_head *bh)
454 {
455 list_del_init(&bh->b_assoc_buffers);
456 WARN_ON(!bh->b_assoc_map);
457 bh->b_assoc_map = NULL;
458 }
459
inode_has_buffers(struct inode * inode)460 int inode_has_buffers(struct inode *inode)
461 {
462 return !list_empty(&inode->i_data.private_list);
463 }
464
465 /*
466 * osync is designed to support O_SYNC io. It waits synchronously for
467 * all already-submitted IO to complete, but does not queue any new
468 * writes to the disk.
469 *
470 * To do O_SYNC writes, just queue the buffer writes with ll_rw_block as
471 * you dirty the buffers, and then use osync_inode_buffers to wait for
472 * completion. Any other dirty buffers which are not yet queued for
473 * write will not be flushed to disk by the osync.
474 */
osync_buffers_list(spinlock_t * lock,struct list_head * list)475 static int osync_buffers_list(spinlock_t *lock, struct list_head *list)
476 {
477 struct buffer_head *bh;
478 struct list_head *p;
479 int err = 0;
480
481 spin_lock(lock);
482 repeat:
483 list_for_each_prev(p, list) {
484 bh = BH_ENTRY(p);
485 if (buffer_locked(bh)) {
486 get_bh(bh);
487 spin_unlock(lock);
488 wait_on_buffer(bh);
489 if (!buffer_uptodate(bh))
490 err = -EIO;
491 brelse(bh);
492 spin_lock(lock);
493 goto repeat;
494 }
495 }
496 spin_unlock(lock);
497 return err;
498 }
499
emergency_thaw_bdev(struct super_block * sb)500 void emergency_thaw_bdev(struct super_block *sb)
501 {
502 while (sb->s_bdev && !thaw_bdev(sb->s_bdev, sb))
503 printk(KERN_WARNING "Emergency Thaw on %pg\n", sb->s_bdev);
504 }
505
506 /**
507 * sync_mapping_buffers - write out & wait upon a mapping's "associated" buffers
508 * @mapping: the mapping which wants those buffers written
509 *
510 * Starts I/O against the buffers at mapping->private_list, and waits upon
511 * that I/O.
512 *
513 * Basically, this is a convenience function for fsync().
514 * @mapping is a file or directory which needs those buffers to be written for
515 * a successful fsync().
516 */
sync_mapping_buffers(struct address_space * mapping)517 int sync_mapping_buffers(struct address_space *mapping)
518 {
519 struct address_space *buffer_mapping = mapping->private_data;
520
521 if (buffer_mapping == NULL || list_empty(&mapping->private_list))
522 return 0;
523
524 return fsync_buffers_list(&buffer_mapping->private_lock,
525 &mapping->private_list);
526 }
527 EXPORT_SYMBOL(sync_mapping_buffers);
528
529 /*
530 * Called when we've recently written block `bblock', and it is known that
531 * `bblock' was for a buffer_boundary() buffer. This means that the block at
532 * `bblock + 1' is probably a dirty indirect block. Hunt it down and, if it's
533 * dirty, schedule it for IO. So that indirects merge nicely with their data.
534 */
write_boundary_block(struct block_device * bdev,sector_t bblock,unsigned blocksize)535 void write_boundary_block(struct block_device *bdev,
536 sector_t bblock, unsigned blocksize)
537 {
538 struct buffer_head *bh = __find_get_block(bdev, bblock + 1, blocksize);
539 if (bh) {
540 if (buffer_dirty(bh))
541 ll_rw_block(REQ_OP_WRITE, 0, 1, &bh);
542 put_bh(bh);
543 }
544 }
545
mark_buffer_dirty_inode(struct buffer_head * bh,struct inode * inode)546 void mark_buffer_dirty_inode(struct buffer_head *bh, struct inode *inode)
547 {
548 struct address_space *mapping = inode->i_mapping;
549 struct address_space *buffer_mapping = bh->b_page->mapping;
550
551 mark_buffer_dirty(bh);
552 if (!mapping->private_data) {
553 mapping->private_data = buffer_mapping;
554 } else {
555 BUG_ON(mapping->private_data != buffer_mapping);
556 }
557 if (!bh->b_assoc_map) {
558 spin_lock(&buffer_mapping->private_lock);
559 list_move_tail(&bh->b_assoc_buffers,
560 &mapping->private_list);
561 bh->b_assoc_map = mapping;
562 spin_unlock(&buffer_mapping->private_lock);
563 }
564 }
565 EXPORT_SYMBOL(mark_buffer_dirty_inode);
566
567 /*
568 * Mark the page dirty, and set it dirty in the page cache, and mark the inode
569 * dirty.
570 *
571 * If warn is true, then emit a warning if the page is not uptodate and has
572 * not been truncated.
573 *
574 * The caller must hold lock_page_memcg().
575 */
__set_page_dirty(struct page * page,struct address_space * mapping,int warn)576 void __set_page_dirty(struct page *page, struct address_space *mapping,
577 int warn)
578 {
579 unsigned long flags;
580
581 xa_lock_irqsave(&mapping->i_pages, flags);
582 if (page->mapping) { /* Race with truncate? */
583 WARN_ON_ONCE(warn && !PageUptodate(page));
584 account_page_dirtied(page, mapping);
585 __xa_set_mark(&mapping->i_pages, page_index(page),
586 PAGECACHE_TAG_DIRTY);
587 }
588 xa_unlock_irqrestore(&mapping->i_pages, flags);
589 }
590 EXPORT_SYMBOL_GPL(__set_page_dirty);
591
592 /*
593 * Add a page to the dirty page list.
594 *
595 * It is a sad fact of life that this function is called from several places
596 * deeply under spinlocking. It may not sleep.
597 *
598 * If the page has buffers, the uptodate buffers are set dirty, to preserve
599 * dirty-state coherency between the page and the buffers. It the page does
600 * not have buffers then when they are later attached they will all be set
601 * dirty.
602 *
603 * The buffers are dirtied before the page is dirtied. There's a small race
604 * window in which a writepage caller may see the page cleanness but not the
605 * buffer dirtiness. That's fine. If this code were to set the page dirty
606 * before the buffers, a concurrent writepage caller could clear the page dirty
607 * bit, see a bunch of clean buffers and we'd end up with dirty buffers/clean
608 * page on the dirty page list.
609 *
610 * We use private_lock to lock against try_to_free_buffers while using the
611 * page's buffer list. Also use this to protect against clean buffers being
612 * added to the page after it was set dirty.
613 *
614 * FIXME: may need to call ->reservepage here as well. That's rather up to the
615 * address_space though.
616 */
__set_page_dirty_buffers(struct page * page)617 int __set_page_dirty_buffers(struct page *page)
618 {
619 int newly_dirty;
620 struct address_space *mapping = page_mapping(page);
621
622 if (unlikely(!mapping))
623 return !TestSetPageDirty(page);
624
625 spin_lock(&mapping->private_lock);
626 if (page_has_buffers(page)) {
627 struct buffer_head *head = page_buffers(page);
628 struct buffer_head *bh = head;
629
630 do {
631 set_buffer_dirty(bh);
632 bh = bh->b_this_page;
633 } while (bh != head);
634 }
635 /*
636 * Lock out page->mem_cgroup migration to keep PageDirty
637 * synchronized with per-memcg dirty page counters.
638 */
639 lock_page_memcg(page);
640 newly_dirty = !TestSetPageDirty(page);
641 spin_unlock(&mapping->private_lock);
642
643 if (newly_dirty)
644 __set_page_dirty(page, mapping, 1);
645
646 unlock_page_memcg(page);
647
648 if (newly_dirty)
649 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
650
651 return newly_dirty;
652 }
653 EXPORT_SYMBOL(__set_page_dirty_buffers);
654
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 */
fsync_buffers_list(spinlock_t * lock,struct list_head * list)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
682 INIT_LIST_HEAD(&tmp);
683 blk_start_plug(&plug);
684
685 spin_lock(lock);
686 while (!list_empty(list)) {
687 bh = BH_ENTRY(list->next);
688 mapping = bh->b_assoc_map;
689 __remove_assoc_queue(bh);
690 /* Avoid race with mark_buffer_dirty_inode() which does
691 * a lockless check and we rely on seeing the dirty bit */
692 smp_mb();
693 if (buffer_dirty(bh) || buffer_locked(bh)) {
694 list_add(&bh->b_assoc_buffers, &tmp);
695 bh->b_assoc_map = mapping;
696 if (buffer_dirty(bh)) {
697 get_bh(bh);
698 spin_unlock(lock);
699 /*
700 * Ensure any pending I/O completes so that
701 * write_dirty_buffer() actually writes the
702 * current contents - it is a noop if I/O is
703 * still in flight on potentially older
704 * contents.
705 */
706 write_dirty_buffer(bh, REQ_SYNC);
707
708 /*
709 * Kick off IO for the previous mapping. Note
710 * that we will not run the very last mapping,
711 * wait_on_buffer() will do that for us
712 * through sync_buffer().
713 */
714 brelse(bh);
715 spin_lock(lock);
716 }
717 }
718 }
719
720 spin_unlock(lock);
721 blk_finish_plug(&plug);
722 spin_lock(lock);
723
724 while (!list_empty(&tmp)) {
725 bh = BH_ENTRY(tmp.prev);
726 get_bh(bh);
727 mapping = bh->b_assoc_map;
728 __remove_assoc_queue(bh);
729 /* Avoid race with mark_buffer_dirty_inode() which does
730 * a lockless check and we rely on seeing the dirty bit */
731 smp_mb();
732 if (buffer_dirty(bh)) {
733 list_add(&bh->b_assoc_buffers,
734 &mapping->private_list);
735 bh->b_assoc_map = mapping;
736 }
737 spin_unlock(lock);
738 wait_on_buffer(bh);
739 if (!buffer_uptodate(bh))
740 err = -EIO;
741 brelse(bh);
742 spin_lock(lock);
743 }
744
745 spin_unlock(lock);
746 err2 = osync_buffers_list(lock, list);
747 if (err)
748 return err;
749 else
750 return err2;
751 }
752
753 /*
754 * Invalidate any and all dirty buffers on a given inode. We are
755 * probably unmounting the fs, but that doesn't mean we have already
756 * done a sync(). Just drop the buffers from the inode list.
757 *
758 * NOTE: we take the inode's blockdev's mapping's private_lock. Which
759 * assumes that all the buffers are against the blockdev. Not true
760 * for reiserfs.
761 */
invalidate_inode_buffers(struct inode * inode)762 void invalidate_inode_buffers(struct inode *inode)
763 {
764 if (inode_has_buffers(inode)) {
765 struct address_space *mapping = &inode->i_data;
766 struct list_head *list = &mapping->private_list;
767 struct address_space *buffer_mapping = mapping->private_data;
768
769 spin_lock(&buffer_mapping->private_lock);
770 while (!list_empty(list))
771 __remove_assoc_queue(BH_ENTRY(list->next));
772 spin_unlock(&buffer_mapping->private_lock);
773 }
774 }
775 EXPORT_SYMBOL(invalidate_inode_buffers);
776
777 /*
778 * Remove any clean buffers from the inode's buffer list. This is called
779 * when we're trying to free the inode itself. Those buffers can pin it.
780 *
781 * Returns true if all buffers were removed.
782 */
remove_inode_buffers(struct inode * inode)783 int remove_inode_buffers(struct inode *inode)
784 {
785 int ret = 1;
786
787 if (inode_has_buffers(inode)) {
788 struct address_space *mapping = &inode->i_data;
789 struct list_head *list = &mapping->private_list;
790 struct address_space *buffer_mapping = mapping->private_data;
791
792 spin_lock(&buffer_mapping->private_lock);
793 while (!list_empty(list)) {
794 struct buffer_head *bh = BH_ENTRY(list->next);
795 if (buffer_dirty(bh)) {
796 ret = 0;
797 break;
798 }
799 __remove_assoc_queue(bh);
800 }
801 spin_unlock(&buffer_mapping->private_lock);
802 }
803 return ret;
804 }
805
806 /*
807 * Create the appropriate buffers when given a page for data area and
808 * the size of each buffer.. Use the bh->b_this_page linked list to
809 * follow the buffers created. Return NULL if unable to create more
810 * buffers.
811 *
812 * The retry flag is used to differentiate async IO (paging, swapping)
813 * which may not fail from ordinary buffer allocations.
814 */
alloc_page_buffers(struct page * page,unsigned long size,bool retry)815 struct buffer_head *alloc_page_buffers(struct page *page, unsigned long size,
816 bool retry)
817 {
818 struct buffer_head *bh, *head;
819 gfp_t gfp = GFP_NOFS | __GFP_ACCOUNT;
820 long offset;
821 struct mem_cgroup *memcg;
822
823 if (retry)
824 gfp |= __GFP_NOFAIL;
825
826 memcg = get_mem_cgroup_from_page(page);
827 memalloc_use_memcg(memcg);
828
829 head = NULL;
830 offset = PAGE_SIZE;
831 while ((offset -= size) >= 0) {
832 bh = alloc_buffer_head(gfp);
833 if (!bh)
834 goto no_grow;
835
836 bh->b_this_page = head;
837 bh->b_blocknr = -1;
838 head = bh;
839
840 bh->b_size = size;
841
842 /* Link the buffer to its page */
843 set_bh_page(bh, page, offset);
844 }
845 out:
846 memalloc_unuse_memcg();
847 mem_cgroup_put(memcg);
848 return head;
849 /*
850 * In case anything failed, we just free everything we got.
851 */
852 no_grow:
853 if (head) {
854 do {
855 bh = head;
856 head = head->b_this_page;
857 free_buffer_head(bh);
858 } while (head);
859 }
860
861 goto out;
862 }
863 EXPORT_SYMBOL_GPL(alloc_page_buffers);
864
865 static inline void
link_dev_buffers(struct page * page,struct buffer_head * head)866 link_dev_buffers(struct page *page, struct buffer_head *head)
867 {
868 struct buffer_head *bh, *tail;
869
870 bh = head;
871 do {
872 tail = bh;
873 bh = bh->b_this_page;
874 } while (bh);
875 tail->b_this_page = head;
876 attach_page_buffers(page, head);
877 }
878
blkdev_max_block(struct block_device * bdev,unsigned int size)879 static sector_t blkdev_max_block(struct block_device *bdev, unsigned int size)
880 {
881 sector_t retval = ~((sector_t)0);
882 loff_t sz = i_size_read(bdev->bd_inode);
883
884 if (sz) {
885 unsigned int sizebits = blksize_bits(size);
886 retval = (sz >> sizebits);
887 }
888 return retval;
889 }
890
891 /*
892 * Initialise the state of a blockdev page's buffers.
893 */
894 static sector_t
init_page_buffers(struct page * page,struct block_device * bdev,sector_t block,int size)895 init_page_buffers(struct page *page, struct block_device *bdev,
896 sector_t block, int size)
897 {
898 struct buffer_head *head = page_buffers(page);
899 struct buffer_head *bh = head;
900 int uptodate = PageUptodate(page);
901 sector_t end_block = blkdev_max_block(I_BDEV(bdev->bd_inode), size);
902
903 do {
904 if (!buffer_mapped(bh)) {
905 bh->b_end_io = NULL;
906 bh->b_private = NULL;
907 bh->b_bdev = bdev;
908 bh->b_blocknr = block;
909 if (uptodate)
910 set_buffer_uptodate(bh);
911 if (block < end_block)
912 set_buffer_mapped(bh);
913 }
914 block++;
915 bh = bh->b_this_page;
916 } while (bh != head);
917
918 /*
919 * Caller needs to validate requested block against end of device.
920 */
921 return end_block;
922 }
923
924 /*
925 * Create the page-cache page that contains the requested block.
926 *
927 * This is used purely for blockdev mappings.
928 */
929 static int
grow_dev_page(struct block_device * bdev,sector_t block,pgoff_t index,int size,int sizebits,gfp_t gfp)930 grow_dev_page(struct block_device *bdev, sector_t block,
931 pgoff_t index, int size, int sizebits, gfp_t gfp)
932 {
933 struct inode *inode = bdev->bd_inode;
934 struct page *page;
935 struct buffer_head *bh;
936 sector_t end_block;
937 int ret = 0; /* Will call free_more_memory() */
938 gfp_t gfp_mask;
939
940 gfp_mask = mapping_gfp_constraint(inode->i_mapping, ~__GFP_FS) | gfp;
941
942 /*
943 * XXX: __getblk_slow() can not really deal with failure and
944 * will endlessly loop on improvised global reclaim. Prefer
945 * looping in the allocator rather than here, at least that
946 * code knows what it's doing.
947 */
948 gfp_mask |= __GFP_NOFAIL;
949
950 page = find_or_create_page(inode->i_mapping, index, gfp_mask);
951
952 BUG_ON(!PageLocked(page));
953
954 if (page_has_buffers(page)) {
955 bh = page_buffers(page);
956 if (bh->b_size == size) {
957 end_block = init_page_buffers(page, bdev,
958 (sector_t)index << sizebits,
959 size);
960 goto done;
961 }
962 if (!try_to_free_buffers(page))
963 goto failed;
964 }
965
966 /*
967 * Allocate some buffers for this page
968 */
969 bh = alloc_page_buffers(page, size, true);
970
971 /*
972 * Link the page to the buffers and initialise them. Take the
973 * lock to be atomic wrt __find_get_block(), which does not
974 * run under the page lock.
975 */
976 spin_lock(&inode->i_mapping->private_lock);
977 link_dev_buffers(page, bh);
978 end_block = init_page_buffers(page, bdev, (sector_t)index << sizebits,
979 size);
980 spin_unlock(&inode->i_mapping->private_lock);
981 done:
982 ret = (block < end_block) ? 1 : -ENXIO;
983 failed:
984 unlock_page(page);
985 put_page(page);
986 return ret;
987 }
988
989 /*
990 * Create buffers for the specified block device block's page. If
991 * that page was dirty, the buffers are set dirty also.
992 */
993 static int
grow_buffers(struct block_device * bdev,sector_t block,int size,gfp_t gfp)994 grow_buffers(struct block_device *bdev, sector_t block, int size, gfp_t gfp)
995 {
996 pgoff_t index;
997 int sizebits;
998
999 sizebits = -1;
1000 do {
1001 sizebits++;
1002 } while ((size << sizebits) < PAGE_SIZE);
1003
1004 index = block >> sizebits;
1005
1006 /*
1007 * Check for a block which wants to lie outside our maximum possible
1008 * pagecache index. (this comparison is done using sector_t types).
1009 */
1010 if (unlikely(index != block >> sizebits)) {
1011 printk(KERN_ERR "%s: requested out-of-range block %llu for "
1012 "device %pg\n",
1013 __func__, (unsigned long long)block,
1014 bdev);
1015 return -EIO;
1016 }
1017
1018 /* Create a page with the proper size buffers.. */
1019 return grow_dev_page(bdev, block, index, size, sizebits, gfp);
1020 }
1021
1022 static struct buffer_head *
__getblk_slow(struct block_device * bdev,sector_t block,unsigned size,gfp_t gfp)1023 __getblk_slow(struct block_device *bdev, sector_t block,
1024 unsigned size, gfp_t gfp)
1025 {
1026 /* Size must be multiple of hard sectorsize */
1027 if (unlikely(size & (bdev_logical_block_size(bdev)-1) ||
1028 (size < 512 || size > PAGE_SIZE))) {
1029 printk(KERN_ERR "getblk(): invalid block size %d requested\n",
1030 size);
1031 printk(KERN_ERR "logical block size: %d\n",
1032 bdev_logical_block_size(bdev));
1033
1034 dump_stack();
1035 return NULL;
1036 }
1037
1038 for (;;) {
1039 struct buffer_head *bh;
1040 int ret;
1041
1042 bh = __find_get_block(bdev, block, size);
1043 if (bh)
1044 return bh;
1045
1046 ret = grow_buffers(bdev, block, size, gfp);
1047 if (ret < 0)
1048 return NULL;
1049 }
1050 }
1051
1052 /*
1053 * The relationship between dirty buffers and dirty pages:
1054 *
1055 * Whenever a page has any dirty buffers, the page's dirty bit is set, and
1056 * the page is tagged dirty in the page cache.
1057 *
1058 * At all times, the dirtiness of the buffers represents the dirtiness of
1059 * subsections of the page. If the page has buffers, the page dirty bit is
1060 * merely a hint about the true dirty state.
1061 *
1062 * When a page is set dirty in its entirety, all its buffers are marked dirty
1063 * (if the page has buffers).
1064 *
1065 * When a buffer is marked dirty, its page is dirtied, but the page's other
1066 * buffers are not.
1067 *
1068 * Also. When blockdev buffers are explicitly read with bread(), they
1069 * individually become uptodate. But their backing page remains not
1070 * uptodate - even if all of its buffers are uptodate. A subsequent
1071 * block_read_full_page() against that page will discover all the uptodate
1072 * buffers, will set the page uptodate and will perform no I/O.
1073 */
1074
1075 /**
1076 * mark_buffer_dirty - mark a buffer_head as needing writeout
1077 * @bh: the buffer_head to mark dirty
1078 *
1079 * mark_buffer_dirty() will set the dirty bit against the buffer, then set
1080 * its backing page dirty, then tag the page as dirty in the page cache
1081 * and then attach the address_space's inode to its superblock's dirty
1082 * inode list.
1083 *
1084 * mark_buffer_dirty() is atomic. It takes bh->b_page->mapping->private_lock,
1085 * i_pages lock and mapping->host->i_lock.
1086 */
mark_buffer_dirty(struct buffer_head * bh)1087 void mark_buffer_dirty(struct buffer_head *bh)
1088 {
1089 WARN_ON_ONCE(!buffer_uptodate(bh));
1090
1091 trace_block_dirty_buffer(bh);
1092
1093 /*
1094 * Very *carefully* optimize the it-is-already-dirty case.
1095 *
1096 * Don't let the final "is it dirty" escape to before we
1097 * perhaps modified the buffer.
1098 */
1099 if (buffer_dirty(bh)) {
1100 smp_mb();
1101 if (buffer_dirty(bh))
1102 return;
1103 }
1104
1105 if (!test_set_buffer_dirty(bh)) {
1106 struct page *page = bh->b_page;
1107 struct address_space *mapping = NULL;
1108
1109 lock_page_memcg(page);
1110 if (!TestSetPageDirty(page)) {
1111 mapping = page_mapping(page);
1112 if (mapping)
1113 __set_page_dirty(page, mapping, 0);
1114 }
1115 unlock_page_memcg(page);
1116 if (mapping)
1117 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
1118 }
1119 }
1120 EXPORT_SYMBOL(mark_buffer_dirty);
1121
mark_buffer_write_io_error(struct buffer_head * bh)1122 void mark_buffer_write_io_error(struct buffer_head *bh)
1123 {
1124 set_buffer_write_io_error(bh);
1125 /* FIXME: do we need to set this in both places? */
1126 if (bh->b_page && bh->b_page->mapping)
1127 mapping_set_error(bh->b_page->mapping, -EIO);
1128 if (bh->b_assoc_map)
1129 mapping_set_error(bh->b_assoc_map, -EIO);
1130 }
1131 EXPORT_SYMBOL(mark_buffer_write_io_error);
1132
1133 /*
1134 * Decrement a buffer_head's reference count. If all buffers against a page
1135 * have zero reference count, are clean and unlocked, and if the page is clean
1136 * and unlocked then try_to_free_buffers() may strip the buffers from the page
1137 * in preparation for freeing it (sometimes, rarely, buffers are removed from
1138 * a page but it ends up not being freed, and buffers may later be reattached).
1139 */
__brelse(struct buffer_head * buf)1140 void __brelse(struct buffer_head * buf)
1141 {
1142 if (atomic_read(&buf->b_count)) {
1143 put_bh(buf);
1144 return;
1145 }
1146 WARN(1, KERN_ERR "VFS: brelse: Trying to free free buffer\n");
1147 }
1148 EXPORT_SYMBOL(__brelse);
1149
1150 /*
1151 * bforget() is like brelse(), except it discards any
1152 * potentially dirty data.
1153 */
__bforget(struct buffer_head * bh)1154 void __bforget(struct buffer_head *bh)
1155 {
1156 clear_buffer_dirty(bh);
1157 if (bh->b_assoc_map) {
1158 struct address_space *buffer_mapping = bh->b_page->mapping;
1159
1160 spin_lock(&buffer_mapping->private_lock);
1161 list_del_init(&bh->b_assoc_buffers);
1162 bh->b_assoc_map = NULL;
1163 spin_unlock(&buffer_mapping->private_lock);
1164 }
1165 __brelse(bh);
1166 }
1167 EXPORT_SYMBOL(__bforget);
1168
__bread_slow(struct buffer_head * bh)1169 static struct buffer_head *__bread_slow(struct buffer_head *bh)
1170 {
1171 lock_buffer(bh);
1172 if (buffer_uptodate(bh)) {
1173 unlock_buffer(bh);
1174 return bh;
1175 } else {
1176 get_bh(bh);
1177 bh->b_end_io = end_buffer_read_sync;
1178 submit_bh(REQ_OP_READ, 0, bh);
1179 wait_on_buffer(bh);
1180 if (buffer_uptodate(bh))
1181 return bh;
1182 }
1183 brelse(bh);
1184 return NULL;
1185 }
1186
1187 /*
1188 * Per-cpu buffer LRU implementation. To reduce the cost of __find_get_block().
1189 * The bhs[] array is sorted - newest buffer is at bhs[0]. Buffers have their
1190 * refcount elevated by one when they're in an LRU. A buffer can only appear
1191 * once in a particular CPU's LRU. A single buffer can be present in multiple
1192 * CPU's LRUs at the same time.
1193 *
1194 * This is a transparent caching front-end to sb_bread(), sb_getblk() and
1195 * sb_find_get_block().
1196 *
1197 * The LRUs themselves only need locking against invalidate_bh_lrus. We use
1198 * a local interrupt disable for that.
1199 */
1200
1201 #define BH_LRU_SIZE 16
1202
1203 struct bh_lru {
1204 struct buffer_head *bhs[BH_LRU_SIZE];
1205 };
1206
1207 static DEFINE_PER_CPU(struct bh_lru, bh_lrus) = {{ NULL }};
1208
1209 #ifdef CONFIG_SMP
1210 #define bh_lru_lock() local_irq_disable()
1211 #define bh_lru_unlock() local_irq_enable()
1212 #else
1213 #define bh_lru_lock() preempt_disable()
1214 #define bh_lru_unlock() preempt_enable()
1215 #endif
1216
check_irqs_on(void)1217 static inline void check_irqs_on(void)
1218 {
1219 #ifdef irqs_disabled
1220 BUG_ON(irqs_disabled());
1221 #endif
1222 }
1223
1224 /*
1225 * Install a buffer_head into this cpu's LRU. If not already in the LRU, it is
1226 * inserted at the front, and the buffer_head at the back if any is evicted.
1227 * Or, if already in the LRU it is moved to the front.
1228 */
bh_lru_install(struct buffer_head * bh)1229 static void bh_lru_install(struct buffer_head *bh)
1230 {
1231 struct buffer_head *evictee = bh;
1232 struct bh_lru *b;
1233 int i;
1234
1235 check_irqs_on();
1236 bh_lru_lock();
1237
1238 b = this_cpu_ptr(&bh_lrus);
1239 for (i = 0; i < BH_LRU_SIZE; i++) {
1240 swap(evictee, b->bhs[i]);
1241 if (evictee == bh) {
1242 bh_lru_unlock();
1243 return;
1244 }
1245 }
1246
1247 get_bh(bh);
1248 bh_lru_unlock();
1249 brelse(evictee);
1250 }
1251
1252 /*
1253 * Look up the bh in this cpu's LRU. If it's there, move it to the head.
1254 */
1255 static struct buffer_head *
lookup_bh_lru(struct block_device * bdev,sector_t block,unsigned size)1256 lookup_bh_lru(struct block_device *bdev, sector_t block, unsigned size)
1257 {
1258 struct buffer_head *ret = NULL;
1259 unsigned int i;
1260
1261 check_irqs_on();
1262 bh_lru_lock();
1263 for (i = 0; i < BH_LRU_SIZE; i++) {
1264 struct buffer_head *bh = __this_cpu_read(bh_lrus.bhs[i]);
1265
1266 if (bh && bh->b_blocknr == block && bh->b_bdev == bdev &&
1267 bh->b_size == size) {
1268 if (i) {
1269 while (i) {
1270 __this_cpu_write(bh_lrus.bhs[i],
1271 __this_cpu_read(bh_lrus.bhs[i - 1]));
1272 i--;
1273 }
1274 __this_cpu_write(bh_lrus.bhs[0], bh);
1275 }
1276 get_bh(bh);
1277 ret = bh;
1278 break;
1279 }
1280 }
1281 bh_lru_unlock();
1282 return ret;
1283 }
1284
1285 /*
1286 * Perform a pagecache lookup for the matching buffer. If it's there, refresh
1287 * it in the LRU and mark it as accessed. If it is not present then return
1288 * NULL
1289 */
1290 struct buffer_head *
__find_get_block(struct block_device * bdev,sector_t block,unsigned size)1291 __find_get_block(struct block_device *bdev, sector_t block, unsigned size)
1292 {
1293 struct buffer_head *bh = lookup_bh_lru(bdev, block, size);
1294
1295 if (bh == NULL) {
1296 /* __find_get_block_slow will mark the page accessed */
1297 bh = __find_get_block_slow(bdev, block);
1298 if (bh)
1299 bh_lru_install(bh);
1300 } else
1301 touch_buffer(bh);
1302
1303 return bh;
1304 }
1305 EXPORT_SYMBOL(__find_get_block);
1306
1307 /*
1308 * __getblk_gfp() will locate (and, if necessary, create) the buffer_head
1309 * which corresponds to the passed block_device, block and size. The
1310 * returned buffer has its reference count incremented.
1311 *
1312 * __getblk_gfp() will lock up the machine if grow_dev_page's
1313 * try_to_free_buffers() attempt is failing. FIXME, perhaps?
1314 */
1315 struct buffer_head *
__getblk_gfp(struct block_device * bdev,sector_t block,unsigned size,gfp_t gfp)1316 __getblk_gfp(struct block_device *bdev, sector_t block,
1317 unsigned size, gfp_t gfp)
1318 {
1319 struct buffer_head *bh = __find_get_block(bdev, block, size);
1320
1321 might_sleep();
1322 if (bh == NULL)
1323 bh = __getblk_slow(bdev, block, size, gfp);
1324 return bh;
1325 }
1326 EXPORT_SYMBOL(__getblk_gfp);
1327
1328 /*
1329 * Do async read-ahead on a buffer..
1330 */
__breadahead(struct block_device * bdev,sector_t block,unsigned size)1331 void __breadahead(struct block_device *bdev, sector_t block, unsigned size)
1332 {
1333 struct buffer_head *bh = __getblk(bdev, block, size);
1334 if (likely(bh)) {
1335 ll_rw_block(REQ_OP_READ, REQ_RAHEAD, 1, &bh);
1336 brelse(bh);
1337 }
1338 }
1339 EXPORT_SYMBOL(__breadahead);
1340
__breadahead_gfp(struct block_device * bdev,sector_t block,unsigned size,gfp_t gfp)1341 void __breadahead_gfp(struct block_device *bdev, sector_t block, unsigned size,
1342 gfp_t gfp)
1343 {
1344 struct buffer_head *bh = __getblk_gfp(bdev, block, size, gfp);
1345 if (likely(bh)) {
1346 ll_rw_block(REQ_OP_READ, REQ_RAHEAD, 1, &bh);
1347 brelse(bh);
1348 }
1349 }
1350 EXPORT_SYMBOL(__breadahead_gfp);
1351
1352 /**
1353 * __bread_gfp() - reads a specified block and returns the bh
1354 * @bdev: the block_device to read from
1355 * @block: number of block
1356 * @size: size (in bytes) to read
1357 * @gfp: page allocation flag
1358 *
1359 * Reads a specified block, and returns buffer head that contains it.
1360 * The page cache can be allocated from non-movable area
1361 * not to prevent page migration if you set gfp to zero.
1362 * It returns NULL if the block was unreadable.
1363 */
1364 struct buffer_head *
__bread_gfp(struct block_device * bdev,sector_t block,unsigned size,gfp_t gfp)1365 __bread_gfp(struct block_device *bdev, sector_t block,
1366 unsigned size, gfp_t gfp)
1367 {
1368 struct buffer_head *bh = __getblk_gfp(bdev, block, size, gfp);
1369
1370 if (likely(bh) && !buffer_uptodate(bh))
1371 bh = __bread_slow(bh);
1372 return bh;
1373 }
1374 EXPORT_SYMBOL(__bread_gfp);
1375
1376 /*
1377 * invalidate_bh_lrus() is called rarely - but not only at unmount.
1378 * This doesn't race because it runs in each cpu either in irq
1379 * or with preempt disabled.
1380 */
invalidate_bh_lru(void * arg)1381 static void invalidate_bh_lru(void *arg)
1382 {
1383 struct bh_lru *b = &get_cpu_var(bh_lrus);
1384 int i;
1385
1386 for (i = 0; i < BH_LRU_SIZE; i++) {
1387 brelse(b->bhs[i]);
1388 b->bhs[i] = NULL;
1389 }
1390 put_cpu_var(bh_lrus);
1391 }
1392
has_bh_in_lru(int cpu,void * dummy)1393 static bool has_bh_in_lru(int cpu, void *dummy)
1394 {
1395 struct bh_lru *b = per_cpu_ptr(&bh_lrus, cpu);
1396 int i;
1397
1398 for (i = 0; i < BH_LRU_SIZE; i++) {
1399 if (b->bhs[i])
1400 return 1;
1401 }
1402
1403 return 0;
1404 }
1405
invalidate_bh_lrus(void)1406 void invalidate_bh_lrus(void)
1407 {
1408 on_each_cpu_cond(has_bh_in_lru, invalidate_bh_lru, NULL, 1, GFP_KERNEL);
1409 }
1410 EXPORT_SYMBOL_GPL(invalidate_bh_lrus);
1411
set_bh_page(struct buffer_head * bh,struct page * page,unsigned long offset)1412 void set_bh_page(struct buffer_head *bh,
1413 struct page *page, unsigned long offset)
1414 {
1415 bh->b_page = page;
1416 BUG_ON(offset >= PAGE_SIZE);
1417 if (PageHighMem(page))
1418 /*
1419 * This catches illegal uses and preserves the offset:
1420 */
1421 bh->b_data = (char *)(0 + offset);
1422 else
1423 bh->b_data = page_address(page) + offset;
1424 }
1425 EXPORT_SYMBOL(set_bh_page);
1426
1427 /*
1428 * Called when truncating a buffer on a page completely.
1429 */
1430
1431 /* Bits that are cleared during an invalidate */
1432 #define BUFFER_FLAGS_DISCARD \
1433 (1 << BH_Mapped | 1 << BH_New | 1 << BH_Req | \
1434 1 << BH_Delay | 1 << BH_Unwritten)
1435
discard_buffer(struct buffer_head * bh)1436 static void discard_buffer(struct buffer_head * bh)
1437 {
1438 unsigned long b_state, b_state_old;
1439
1440 lock_buffer(bh);
1441 clear_buffer_dirty(bh);
1442 bh->b_bdev = NULL;
1443 b_state = bh->b_state;
1444 for (;;) {
1445 b_state_old = cmpxchg(&bh->b_state, b_state,
1446 (b_state & ~BUFFER_FLAGS_DISCARD));
1447 if (b_state_old == b_state)
1448 break;
1449 b_state = b_state_old;
1450 }
1451 unlock_buffer(bh);
1452 }
1453
1454 /**
1455 * block_invalidatepage - invalidate part or all of a buffer-backed page
1456 *
1457 * @page: the page which is affected
1458 * @offset: start of the range to invalidate
1459 * @length: length of the range to invalidate
1460 *
1461 * block_invalidatepage() is called when all or part of the page has become
1462 * invalidated by a truncate operation.
1463 *
1464 * block_invalidatepage() does not have to release all buffers, but it must
1465 * ensure that no dirty buffer is left outside @offset and that no I/O
1466 * is underway against any of the blocks which are outside the truncation
1467 * point. Because the caller is about to free (and possibly reuse) those
1468 * blocks on-disk.
1469 */
block_invalidatepage(struct page * page,unsigned int offset,unsigned int length)1470 void block_invalidatepage(struct page *page, unsigned int offset,
1471 unsigned int length)
1472 {
1473 struct buffer_head *head, *bh, *next;
1474 unsigned int curr_off = 0;
1475 unsigned int stop = length + offset;
1476
1477 BUG_ON(!PageLocked(page));
1478 if (!page_has_buffers(page))
1479 goto out;
1480
1481 /*
1482 * Check for overflow
1483 */
1484 BUG_ON(stop > PAGE_SIZE || stop < length);
1485
1486 head = page_buffers(page);
1487 bh = head;
1488 do {
1489 unsigned int next_off = curr_off + bh->b_size;
1490 next = bh->b_this_page;
1491
1492 /*
1493 * Are we still fully in range ?
1494 */
1495 if (next_off > stop)
1496 goto out;
1497
1498 /*
1499 * is this block fully invalidated?
1500 */
1501 if (offset <= curr_off)
1502 discard_buffer(bh);
1503 curr_off = next_off;
1504 bh = next;
1505 } while (bh != head);
1506
1507 /*
1508 * We release buffers only if the entire page is being invalidated.
1509 * The get_block cached value has been unconditionally invalidated,
1510 * so real IO is not possible anymore.
1511 */
1512 if (length == PAGE_SIZE)
1513 try_to_release_page(page, 0);
1514 out:
1515 return;
1516 }
1517 EXPORT_SYMBOL(block_invalidatepage);
1518
1519
1520 /*
1521 * We attach and possibly dirty the buffers atomically wrt
1522 * __set_page_dirty_buffers() via private_lock. try_to_free_buffers
1523 * is already excluded via the page lock.
1524 */
create_empty_buffers(struct page * page,unsigned long blocksize,unsigned long b_state)1525 void create_empty_buffers(struct page *page,
1526 unsigned long blocksize, unsigned long b_state)
1527 {
1528 struct buffer_head *bh, *head, *tail;
1529
1530 head = alloc_page_buffers(page, blocksize, true);
1531 bh = head;
1532 do {
1533 bh->b_state |= b_state;
1534 tail = bh;
1535 bh = bh->b_this_page;
1536 } while (bh);
1537 tail->b_this_page = head;
1538
1539 spin_lock(&page->mapping->private_lock);
1540 if (PageUptodate(page) || PageDirty(page)) {
1541 bh = head;
1542 do {
1543 if (PageDirty(page))
1544 set_buffer_dirty(bh);
1545 if (PageUptodate(page))
1546 set_buffer_uptodate(bh);
1547 bh = bh->b_this_page;
1548 } while (bh != head);
1549 }
1550 attach_page_buffers(page, head);
1551 spin_unlock(&page->mapping->private_lock);
1552 }
1553 EXPORT_SYMBOL(create_empty_buffers);
1554
1555 /**
1556 * clean_bdev_aliases: clean a range of buffers in block device
1557 * @bdev: Block device to clean buffers in
1558 * @block: Start of a range of blocks to clean
1559 * @len: Number of blocks to clean
1560 *
1561 * We are taking a range of blocks for data and we don't want writeback of any
1562 * buffer-cache aliases starting from return from this function and until the
1563 * moment when something will explicitly mark the buffer dirty (hopefully that
1564 * will not happen until we will free that block ;-) We don't even need to mark
1565 * it not-uptodate - nobody can expect anything from a newly allocated buffer
1566 * anyway. We used to use unmap_buffer() for such invalidation, but that was
1567 * wrong. We definitely don't want to mark the alias unmapped, for example - it
1568 * would confuse anyone who might pick it with bread() afterwards...
1569 *
1570 * Also.. Note that bforget() doesn't lock the buffer. So there can be
1571 * writeout I/O going on against recently-freed buffers. We don't wait on that
1572 * I/O in bforget() - it's more efficient to wait on the I/O only if we really
1573 * need to. That happens here.
1574 */
clean_bdev_aliases(struct block_device * bdev,sector_t block,sector_t len)1575 void clean_bdev_aliases(struct block_device *bdev, sector_t block, sector_t len)
1576 {
1577 struct inode *bd_inode = bdev->bd_inode;
1578 struct address_space *bd_mapping = bd_inode->i_mapping;
1579 struct pagevec pvec;
1580 pgoff_t index = block >> (PAGE_SHIFT - bd_inode->i_blkbits);
1581 pgoff_t end;
1582 int i, count;
1583 struct buffer_head *bh;
1584 struct buffer_head *head;
1585
1586 end = (block + len - 1) >> (PAGE_SHIFT - bd_inode->i_blkbits);
1587 pagevec_init(&pvec);
1588 while (pagevec_lookup_range(&pvec, bd_mapping, &index, end)) {
1589 count = pagevec_count(&pvec);
1590 for (i = 0; i < count; i++) {
1591 struct page *page = pvec.pages[i];
1592
1593 if (!page_has_buffers(page))
1594 continue;
1595 /*
1596 * We use page lock instead of bd_mapping->private_lock
1597 * to pin buffers here since we can afford to sleep and
1598 * it scales better than a global spinlock lock.
1599 */
1600 lock_page(page);
1601 /* Recheck when the page is locked which pins bhs */
1602 if (!page_has_buffers(page))
1603 goto unlock_page;
1604 head = page_buffers(page);
1605 bh = head;
1606 do {
1607 if (!buffer_mapped(bh) || (bh->b_blocknr < block))
1608 goto next;
1609 if (bh->b_blocknr >= block + len)
1610 break;
1611 clear_buffer_dirty(bh);
1612 wait_on_buffer(bh);
1613 clear_buffer_req(bh);
1614 next:
1615 bh = bh->b_this_page;
1616 } while (bh != head);
1617 unlock_page:
1618 unlock_page(page);
1619 }
1620 pagevec_release(&pvec);
1621 cond_resched();
1622 /* End of range already reached? */
1623 if (index > end || !index)
1624 break;
1625 }
1626 }
1627 EXPORT_SYMBOL(clean_bdev_aliases);
1628
1629 /*
1630 * Size is a power-of-two in the range 512..PAGE_SIZE,
1631 * and the case we care about most is PAGE_SIZE.
1632 *
1633 * So this *could* possibly be written with those
1634 * constraints in mind (relevant mostly if some
1635 * architecture has a slow bit-scan instruction)
1636 */
block_size_bits(unsigned int blocksize)1637 static inline int block_size_bits(unsigned int blocksize)
1638 {
1639 return ilog2(blocksize);
1640 }
1641
create_page_buffers(struct page * page,struct inode * inode,unsigned int b_state)1642 static struct buffer_head *create_page_buffers(struct page *page, struct inode *inode, unsigned int b_state)
1643 {
1644 BUG_ON(!PageLocked(page));
1645
1646 if (!page_has_buffers(page))
1647 create_empty_buffers(page, 1 << READ_ONCE(inode->i_blkbits),
1648 b_state);
1649 return page_buffers(page);
1650 }
1651
1652 /*
1653 * NOTE! All mapped/uptodate combinations are valid:
1654 *
1655 * Mapped Uptodate Meaning
1656 *
1657 * No No "unknown" - must do get_block()
1658 * No Yes "hole" - zero-filled
1659 * Yes No "allocated" - allocated on disk, not read in
1660 * Yes Yes "valid" - allocated and up-to-date in memory.
1661 *
1662 * "Dirty" is valid only with the last case (mapped+uptodate).
1663 */
1664
1665 /*
1666 * While block_write_full_page is writing back the dirty buffers under
1667 * the page lock, whoever dirtied the buffers may decide to clean them
1668 * again at any time. We handle that by only looking at the buffer
1669 * state inside lock_buffer().
1670 *
1671 * If block_write_full_page() is called for regular writeback
1672 * (wbc->sync_mode == WB_SYNC_NONE) then it will redirty a page which has a
1673 * locked buffer. This only can happen if someone has written the buffer
1674 * directly, with submit_bh(). At the address_space level PageWriteback
1675 * prevents this contention from occurring.
1676 *
1677 * If block_write_full_page() is called with wbc->sync_mode ==
1678 * WB_SYNC_ALL, the writes are posted using REQ_SYNC; this
1679 * causes the writes to be flagged as synchronous writes.
1680 */
__block_write_full_page(struct inode * inode,struct page * page,get_block_t * get_block,struct writeback_control * wbc,bh_end_io_t * handler)1681 int __block_write_full_page(struct inode *inode, struct page *page,
1682 get_block_t *get_block, struct writeback_control *wbc,
1683 bh_end_io_t *handler)
1684 {
1685 int err;
1686 sector_t block;
1687 sector_t last_block;
1688 struct buffer_head *bh, *head;
1689 unsigned int blocksize, bbits;
1690 int nr_underway = 0;
1691 int write_flags = wbc_to_write_flags(wbc);
1692
1693 head = create_page_buffers(page, inode,
1694 (1 << BH_Dirty)|(1 << BH_Uptodate));
1695
1696 /*
1697 * Be very careful. We have no exclusion from __set_page_dirty_buffers
1698 * here, and the (potentially unmapped) buffers may become dirty at
1699 * any time. If a buffer becomes dirty here after we've inspected it
1700 * then we just miss that fact, and the page stays dirty.
1701 *
1702 * Buffers outside i_size may be dirtied by __set_page_dirty_buffers;
1703 * handle that here by just cleaning them.
1704 */
1705
1706 bh = head;
1707 blocksize = bh->b_size;
1708 bbits = block_size_bits(blocksize);
1709
1710 block = (sector_t)page->index << (PAGE_SHIFT - bbits);
1711 last_block = (i_size_read(inode) - 1) >> bbits;
1712
1713 /*
1714 * Get all the dirty buffers mapped to disk addresses and
1715 * handle any aliases from the underlying blockdev's mapping.
1716 */
1717 do {
1718 if (block > last_block) {
1719 /*
1720 * mapped buffers outside i_size will occur, because
1721 * this page can be outside i_size when there is a
1722 * truncate in progress.
1723 */
1724 /*
1725 * The buffer was zeroed by block_write_full_page()
1726 */
1727 clear_buffer_dirty(bh);
1728 set_buffer_uptodate(bh);
1729 } else if ((!buffer_mapped(bh) || buffer_delay(bh)) &&
1730 buffer_dirty(bh)) {
1731 WARN_ON(bh->b_size != blocksize);
1732 err = get_block(inode, block, bh, 1);
1733 if (err)
1734 goto recover;
1735 clear_buffer_delay(bh);
1736 if (buffer_new(bh)) {
1737 /* blockdev mappings never come here */
1738 clear_buffer_new(bh);
1739 clean_bdev_bh_alias(bh);
1740 }
1741 }
1742 bh = bh->b_this_page;
1743 block++;
1744 } while (bh != head);
1745
1746 do {
1747 if (!buffer_mapped(bh))
1748 continue;
1749 /*
1750 * If it's a fully non-blocking write attempt and we cannot
1751 * lock the buffer then redirty the page. Note that this can
1752 * potentially cause a busy-wait loop from writeback threads
1753 * and kswapd activity, but those code paths have their own
1754 * higher-level throttling.
1755 */
1756 if (wbc->sync_mode != WB_SYNC_NONE) {
1757 lock_buffer(bh);
1758 } else if (!trylock_buffer(bh)) {
1759 redirty_page_for_writepage(wbc, page);
1760 continue;
1761 }
1762 if (test_clear_buffer_dirty(bh)) {
1763 mark_buffer_async_write_endio(bh, handler);
1764 } else {
1765 unlock_buffer(bh);
1766 }
1767 } while ((bh = bh->b_this_page) != head);
1768
1769 /*
1770 * The page and its buffers are protected by PageWriteback(), so we can
1771 * drop the bh refcounts early.
1772 */
1773 BUG_ON(PageWriteback(page));
1774 set_page_writeback(page);
1775
1776 do {
1777 struct buffer_head *next = bh->b_this_page;
1778 if (buffer_async_write(bh)) {
1779 submit_bh_wbc(REQ_OP_WRITE, write_flags, bh,
1780 inode->i_write_hint, wbc);
1781 nr_underway++;
1782 }
1783 bh = next;
1784 } while (bh != head);
1785 unlock_page(page);
1786
1787 err = 0;
1788 done:
1789 if (nr_underway == 0) {
1790 /*
1791 * The page was marked dirty, but the buffers were
1792 * clean. Someone wrote them back by hand with
1793 * ll_rw_block/submit_bh. A rare case.
1794 */
1795 end_page_writeback(page);
1796
1797 /*
1798 * The page and buffer_heads can be released at any time from
1799 * here on.
1800 */
1801 }
1802 return err;
1803
1804 recover:
1805 /*
1806 * ENOSPC, or some other error. We may already have added some
1807 * blocks to the file, so we need to write these out to avoid
1808 * exposing stale data.
1809 * The page is currently locked and not marked for writeback
1810 */
1811 bh = head;
1812 /* Recovery: lock and submit the mapped buffers */
1813 do {
1814 if (buffer_mapped(bh) && buffer_dirty(bh) &&
1815 !buffer_delay(bh)) {
1816 lock_buffer(bh);
1817 mark_buffer_async_write_endio(bh, handler);
1818 } else {
1819 /*
1820 * The buffer may have been set dirty during
1821 * attachment to a dirty page.
1822 */
1823 clear_buffer_dirty(bh);
1824 }
1825 } while ((bh = bh->b_this_page) != head);
1826 SetPageError(page);
1827 BUG_ON(PageWriteback(page));
1828 mapping_set_error(page->mapping, err);
1829 set_page_writeback(page);
1830 do {
1831 struct buffer_head *next = bh->b_this_page;
1832 if (buffer_async_write(bh)) {
1833 clear_buffer_dirty(bh);
1834 submit_bh_wbc(REQ_OP_WRITE, write_flags, bh,
1835 inode->i_write_hint, wbc);
1836 nr_underway++;
1837 }
1838 bh = next;
1839 } while (bh != head);
1840 unlock_page(page);
1841 goto done;
1842 }
1843 EXPORT_SYMBOL(__block_write_full_page);
1844
1845 /*
1846 * If a page has any new buffers, zero them out here, and mark them uptodate
1847 * and dirty so they'll be written out (in order to prevent uninitialised
1848 * block data from leaking). And clear the new bit.
1849 */
page_zero_new_buffers(struct page * page,unsigned from,unsigned to)1850 void page_zero_new_buffers(struct page *page, unsigned from, unsigned to)
1851 {
1852 unsigned int block_start, block_end;
1853 struct buffer_head *head, *bh;
1854
1855 BUG_ON(!PageLocked(page));
1856 if (!page_has_buffers(page))
1857 return;
1858
1859 bh = head = page_buffers(page);
1860 block_start = 0;
1861 do {
1862 block_end = block_start + bh->b_size;
1863
1864 if (buffer_new(bh)) {
1865 if (block_end > from && block_start < to) {
1866 if (!PageUptodate(page)) {
1867 unsigned start, size;
1868
1869 start = max(from, block_start);
1870 size = min(to, block_end) - start;
1871
1872 zero_user(page, start, size);
1873 set_buffer_uptodate(bh);
1874 }
1875
1876 clear_buffer_new(bh);
1877 mark_buffer_dirty(bh);
1878 }
1879 }
1880
1881 block_start = block_end;
1882 bh = bh->b_this_page;
1883 } while (bh != head);
1884 }
1885 EXPORT_SYMBOL(page_zero_new_buffers);
1886
1887 static void
iomap_to_bh(struct inode * inode,sector_t block,struct buffer_head * bh,struct iomap * iomap)1888 iomap_to_bh(struct inode *inode, sector_t block, struct buffer_head *bh,
1889 struct iomap *iomap)
1890 {
1891 loff_t offset = block << inode->i_blkbits;
1892
1893 bh->b_bdev = iomap->bdev;
1894
1895 /*
1896 * Block points to offset in file we need to map, iomap contains
1897 * the offset at which the map starts. If the map ends before the
1898 * current block, then do not map the buffer and let the caller
1899 * handle it.
1900 */
1901 BUG_ON(offset >= iomap->offset + iomap->length);
1902
1903 switch (iomap->type) {
1904 case IOMAP_HOLE:
1905 /*
1906 * If the buffer is not up to date or beyond the current EOF,
1907 * we need to mark it as new to ensure sub-block zeroing is
1908 * executed if necessary.
1909 */
1910 if (!buffer_uptodate(bh) ||
1911 (offset >= i_size_read(inode)))
1912 set_buffer_new(bh);
1913 break;
1914 case IOMAP_DELALLOC:
1915 if (!buffer_uptodate(bh) ||
1916 (offset >= i_size_read(inode)))
1917 set_buffer_new(bh);
1918 set_buffer_uptodate(bh);
1919 set_buffer_mapped(bh);
1920 set_buffer_delay(bh);
1921 break;
1922 case IOMAP_UNWRITTEN:
1923 /*
1924 * For unwritten regions, we always need to ensure that regions
1925 * in the block we are not writing to are zeroed. Mark the
1926 * buffer as new to ensure this.
1927 */
1928 set_buffer_new(bh);
1929 set_buffer_unwritten(bh);
1930 /* FALLTHRU */
1931 case IOMAP_MAPPED:
1932 if ((iomap->flags & IOMAP_F_NEW) ||
1933 offset >= i_size_read(inode))
1934 set_buffer_new(bh);
1935 bh->b_blocknr = (iomap->addr + offset - iomap->offset) >>
1936 inode->i_blkbits;
1937 set_buffer_mapped(bh);
1938 break;
1939 }
1940 }
1941
__block_write_begin_int(struct page * page,loff_t pos,unsigned len,get_block_t * get_block,struct iomap * iomap)1942 int __block_write_begin_int(struct page *page, loff_t pos, unsigned len,
1943 get_block_t *get_block, struct iomap *iomap)
1944 {
1945 unsigned from = pos & (PAGE_SIZE - 1);
1946 unsigned to = from + len;
1947 struct inode *inode = page->mapping->host;
1948 unsigned block_start, block_end;
1949 sector_t block;
1950 int err = 0;
1951 unsigned blocksize, bbits;
1952 struct buffer_head *bh, *head, *wait[2], **wait_bh=wait;
1953
1954 BUG_ON(!PageLocked(page));
1955 BUG_ON(from > PAGE_SIZE);
1956 BUG_ON(to > PAGE_SIZE);
1957 BUG_ON(from > to);
1958
1959 head = create_page_buffers(page, inode, 0);
1960 blocksize = head->b_size;
1961 bbits = block_size_bits(blocksize);
1962
1963 block = (sector_t)page->index << (PAGE_SHIFT - bbits);
1964
1965 for(bh = head, block_start = 0; bh != head || !block_start;
1966 block++, block_start=block_end, bh = bh->b_this_page) {
1967 block_end = block_start + blocksize;
1968 if (block_end <= from || block_start >= to) {
1969 if (PageUptodate(page)) {
1970 if (!buffer_uptodate(bh))
1971 set_buffer_uptodate(bh);
1972 }
1973 continue;
1974 }
1975 if (buffer_new(bh))
1976 clear_buffer_new(bh);
1977 if (!buffer_mapped(bh)) {
1978 WARN_ON(bh->b_size != blocksize);
1979 if (get_block) {
1980 err = get_block(inode, block, bh, 1);
1981 if (err)
1982 break;
1983 } else {
1984 iomap_to_bh(inode, block, bh, iomap);
1985 }
1986
1987 if (buffer_new(bh)) {
1988 clean_bdev_bh_alias(bh);
1989 if (PageUptodate(page)) {
1990 clear_buffer_new(bh);
1991 set_buffer_uptodate(bh);
1992 mark_buffer_dirty(bh);
1993 continue;
1994 }
1995 if (block_end > to || block_start < from)
1996 zero_user_segments(page,
1997 to, block_end,
1998 block_start, from);
1999 continue;
2000 }
2001 }
2002 if (PageUptodate(page)) {
2003 if (!buffer_uptodate(bh))
2004 set_buffer_uptodate(bh);
2005 continue;
2006 }
2007 if (!buffer_uptodate(bh) && !buffer_delay(bh) &&
2008 !buffer_unwritten(bh) &&
2009 (block_start < from || block_end > to)) {
2010 ll_rw_block(REQ_OP_READ, 0, 1, &bh);
2011 *wait_bh++=bh;
2012 }
2013 }
2014 /*
2015 * If we issued read requests - let them complete.
2016 */
2017 while(wait_bh > wait) {
2018 wait_on_buffer(*--wait_bh);
2019 if (!buffer_uptodate(*wait_bh))
2020 err = -EIO;
2021 }
2022 if (unlikely(err))
2023 page_zero_new_buffers(page, from, to);
2024 return err;
2025 }
2026
__block_write_begin(struct page * page,loff_t pos,unsigned len,get_block_t * get_block)2027 int __block_write_begin(struct page *page, loff_t pos, unsigned len,
2028 get_block_t *get_block)
2029 {
2030 return __block_write_begin_int(page, pos, len, get_block, NULL);
2031 }
2032 EXPORT_SYMBOL(__block_write_begin);
2033
__block_commit_write(struct inode * inode,struct page * page,unsigned from,unsigned to)2034 static int __block_commit_write(struct inode *inode, struct page *page,
2035 unsigned from, unsigned to)
2036 {
2037 unsigned block_start, block_end;
2038 int partial = 0;
2039 unsigned blocksize;
2040 struct buffer_head *bh, *head;
2041
2042 bh = head = page_buffers(page);
2043 blocksize = bh->b_size;
2044
2045 block_start = 0;
2046 do {
2047 block_end = block_start + blocksize;
2048 if (block_end <= from || block_start >= to) {
2049 if (!buffer_uptodate(bh))
2050 partial = 1;
2051 } else {
2052 set_buffer_uptodate(bh);
2053 mark_buffer_dirty(bh);
2054 }
2055 clear_buffer_new(bh);
2056
2057 block_start = block_end;
2058 bh = bh->b_this_page;
2059 } while (bh != head);
2060
2061 /*
2062 * If this is a partial write which happened to make all buffers
2063 * uptodate then we can optimize away a bogus readpage() for
2064 * the next read(). Here we 'discover' whether the page went
2065 * uptodate as a result of this (potentially partial) write.
2066 */
2067 if (!partial)
2068 SetPageUptodate(page);
2069 return 0;
2070 }
2071
2072 /*
2073 * block_write_begin takes care of the basic task of block allocation and
2074 * bringing partial write blocks uptodate first.
2075 *
2076 * The filesystem needs to handle block truncation upon failure.
2077 */
block_write_begin(struct address_space * mapping,loff_t pos,unsigned len,unsigned flags,struct page ** pagep,get_block_t * get_block)2078 int block_write_begin(struct address_space *mapping, loff_t pos, unsigned len,
2079 unsigned flags, struct page **pagep, get_block_t *get_block)
2080 {
2081 pgoff_t index = pos >> PAGE_SHIFT;
2082 struct page *page;
2083 int status;
2084
2085 page = grab_cache_page_write_begin(mapping, index, flags);
2086 if (!page)
2087 return -ENOMEM;
2088
2089 status = __block_write_begin(page, pos, len, get_block);
2090 if (unlikely(status)) {
2091 unlock_page(page);
2092 put_page(page);
2093 page = NULL;
2094 }
2095
2096 *pagep = page;
2097 return status;
2098 }
2099 EXPORT_SYMBOL(block_write_begin);
2100
block_write_end(struct file * file,struct address_space * mapping,loff_t pos,unsigned len,unsigned copied,struct page * page,void * fsdata)2101 int block_write_end(struct file *file, struct address_space *mapping,
2102 loff_t pos, unsigned len, unsigned copied,
2103 struct page *page, void *fsdata)
2104 {
2105 struct inode *inode = mapping->host;
2106 unsigned start;
2107
2108 start = pos & (PAGE_SIZE - 1);
2109
2110 if (unlikely(copied < len)) {
2111 /*
2112 * The buffers that were written will now be uptodate, so we
2113 * don't have to worry about a readpage reading them and
2114 * overwriting a partial write. However if we have encountered
2115 * a short write and only partially written into a buffer, it
2116 * will not be marked uptodate, so a readpage might come in and
2117 * destroy our partial write.
2118 *
2119 * Do the simplest thing, and just treat any short write to a
2120 * non uptodate page as a zero-length write, and force the
2121 * caller to redo the whole thing.
2122 */
2123 if (!PageUptodate(page))
2124 copied = 0;
2125
2126 page_zero_new_buffers(page, start+copied, start+len);
2127 }
2128 flush_dcache_page(page);
2129
2130 /* This could be a short (even 0-length) commit */
2131 __block_commit_write(inode, page, start, start+copied);
2132
2133 return copied;
2134 }
2135 EXPORT_SYMBOL(block_write_end);
2136
generic_write_end(struct file * file,struct address_space * mapping,loff_t pos,unsigned len,unsigned copied,struct page * page,void * fsdata)2137 int generic_write_end(struct file *file, struct address_space *mapping,
2138 loff_t pos, unsigned len, unsigned copied,
2139 struct page *page, void *fsdata)
2140 {
2141 struct inode *inode = mapping->host;
2142 loff_t old_size = inode->i_size;
2143 bool i_size_changed = false;
2144
2145 copied = block_write_end(file, mapping, pos, len, copied, page, fsdata);
2146
2147 /*
2148 * No need to use i_size_read() here, the i_size cannot change under us
2149 * because we hold i_rwsem.
2150 *
2151 * But it's important to update i_size while still holding page lock:
2152 * page writeout could otherwise come in and zero beyond i_size.
2153 */
2154 if (pos + copied > inode->i_size) {
2155 i_size_write(inode, pos + copied);
2156 i_size_changed = true;
2157 }
2158
2159 unlock_page(page);
2160 put_page(page);
2161
2162 if (old_size < pos)
2163 pagecache_isize_extended(inode, old_size, pos);
2164 /*
2165 * Don't mark the inode dirty under page lock. First, it unnecessarily
2166 * makes the holding time of page lock longer. Second, it forces lock
2167 * ordering of page lock and transaction start for journaling
2168 * filesystems.
2169 */
2170 if (i_size_changed)
2171 mark_inode_dirty(inode);
2172 return copied;
2173 }
2174 EXPORT_SYMBOL(generic_write_end);
2175
2176 /*
2177 * block_is_partially_uptodate checks whether buffers within a page are
2178 * uptodate or not.
2179 *
2180 * Returns true if all buffers which correspond to a file portion
2181 * we want to read are uptodate.
2182 */
block_is_partially_uptodate(struct page * page,unsigned long from,unsigned long count)2183 int block_is_partially_uptodate(struct page *page, unsigned long from,
2184 unsigned long count)
2185 {
2186 unsigned block_start, block_end, blocksize;
2187 unsigned to;
2188 struct buffer_head *bh, *head;
2189 int ret = 1;
2190
2191 if (!page_has_buffers(page))
2192 return 0;
2193
2194 head = page_buffers(page);
2195 blocksize = head->b_size;
2196 to = min_t(unsigned, PAGE_SIZE - from, count);
2197 to = from + to;
2198 if (from < blocksize && to > PAGE_SIZE - blocksize)
2199 return 0;
2200
2201 bh = head;
2202 block_start = 0;
2203 do {
2204 block_end = block_start + blocksize;
2205 if (block_end > from && block_start < to) {
2206 if (!buffer_uptodate(bh)) {
2207 ret = 0;
2208 break;
2209 }
2210 if (block_end >= to)
2211 break;
2212 }
2213 block_start = block_end;
2214 bh = bh->b_this_page;
2215 } while (bh != head);
2216
2217 return ret;
2218 }
2219 EXPORT_SYMBOL(block_is_partially_uptodate);
2220
2221 /*
2222 * Generic "read page" function for block devices that have the normal
2223 * get_block functionality. This is most of the block device filesystems.
2224 * Reads the page asynchronously --- the unlock_buffer() and
2225 * set/clear_buffer_uptodate() functions propagate buffer state into the
2226 * page struct once IO has completed.
2227 */
block_read_full_page(struct page * page,get_block_t * get_block)2228 int block_read_full_page(struct page *page, get_block_t *get_block)
2229 {
2230 struct inode *inode = page->mapping->host;
2231 sector_t iblock, lblock;
2232 struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE];
2233 unsigned int blocksize, bbits;
2234 int nr, i;
2235 int fully_mapped = 1;
2236
2237 head = create_page_buffers(page, inode, 0);
2238 blocksize = head->b_size;
2239 bbits = block_size_bits(blocksize);
2240
2241 iblock = (sector_t)page->index << (PAGE_SHIFT - bbits);
2242 lblock = (i_size_read(inode)+blocksize-1) >> bbits;
2243 bh = head;
2244 nr = 0;
2245 i = 0;
2246
2247 do {
2248 if (buffer_uptodate(bh))
2249 continue;
2250
2251 if (!buffer_mapped(bh)) {
2252 int err = 0;
2253
2254 fully_mapped = 0;
2255 if (iblock < lblock) {
2256 WARN_ON(bh->b_size != blocksize);
2257 err = get_block(inode, iblock, bh, 0);
2258 if (err)
2259 SetPageError(page);
2260 }
2261 if (!buffer_mapped(bh)) {
2262 zero_user(page, i * blocksize, blocksize);
2263 if (!err)
2264 set_buffer_uptodate(bh);
2265 continue;
2266 }
2267 /*
2268 * get_block() might have updated the buffer
2269 * synchronously
2270 */
2271 if (buffer_uptodate(bh))
2272 continue;
2273 }
2274 arr[nr++] = bh;
2275 } while (i++, iblock++, (bh = bh->b_this_page) != head);
2276
2277 if (fully_mapped)
2278 SetPageMappedToDisk(page);
2279
2280 if (!nr) {
2281 /*
2282 * All buffers are uptodate - we can set the page uptodate
2283 * as well. But not if get_block() returned an error.
2284 */
2285 if (!PageError(page))
2286 SetPageUptodate(page);
2287 unlock_page(page);
2288 return 0;
2289 }
2290
2291 /* Stage two: lock the buffers */
2292 for (i = 0; i < nr; i++) {
2293 bh = arr[i];
2294 lock_buffer(bh);
2295 mark_buffer_async_read(bh);
2296 }
2297
2298 /*
2299 * Stage 3: start the IO. Check for uptodateness
2300 * inside the buffer lock in case another process reading
2301 * the underlying blockdev brought it uptodate (the sct fix).
2302 */
2303 for (i = 0; i < nr; i++) {
2304 bh = arr[i];
2305 if (buffer_uptodate(bh))
2306 end_buffer_async_read(bh, 1);
2307 else
2308 submit_bh(REQ_OP_READ, 0, bh);
2309 }
2310 return 0;
2311 }
2312 EXPORT_SYMBOL(block_read_full_page);
2313
2314 /* utility function for filesystems that need to do work on expanding
2315 * truncates. Uses filesystem pagecache writes to allow the filesystem to
2316 * deal with the hole.
2317 */
generic_cont_expand_simple(struct inode * inode,loff_t size)2318 int generic_cont_expand_simple(struct inode *inode, loff_t size)
2319 {
2320 struct address_space *mapping = inode->i_mapping;
2321 struct page *page;
2322 void *fsdata = NULL;
2323 int err;
2324
2325 err = inode_newsize_ok(inode, size);
2326 if (err)
2327 goto out;
2328
2329 err = pagecache_write_begin(NULL, mapping, size, 0,
2330 AOP_FLAG_CONT_EXPAND, &page, &fsdata);
2331 if (err)
2332 goto out;
2333
2334 err = pagecache_write_end(NULL, mapping, size, 0, 0, page, fsdata);
2335 BUG_ON(err > 0);
2336
2337 out:
2338 return err;
2339 }
2340 EXPORT_SYMBOL(generic_cont_expand_simple);
2341
cont_expand_zero(struct file * file,struct address_space * mapping,loff_t pos,loff_t * bytes)2342 static int cont_expand_zero(struct file *file, struct address_space *mapping,
2343 loff_t pos, loff_t *bytes)
2344 {
2345 struct inode *inode = mapping->host;
2346 unsigned int blocksize = i_blocksize(inode);
2347 struct page *page;
2348 void *fsdata = NULL;
2349 pgoff_t index, curidx;
2350 loff_t curpos;
2351 unsigned zerofrom, offset, len;
2352 int err = 0;
2353
2354 index = pos >> PAGE_SHIFT;
2355 offset = pos & ~PAGE_MASK;
2356
2357 while (index > (curidx = (curpos = *bytes)>>PAGE_SHIFT)) {
2358 zerofrom = curpos & ~PAGE_MASK;
2359 if (zerofrom & (blocksize-1)) {
2360 *bytes |= (blocksize-1);
2361 (*bytes)++;
2362 }
2363 len = PAGE_SIZE - zerofrom;
2364
2365 err = pagecache_write_begin(file, mapping, curpos, len, 0,
2366 &page, &fsdata);
2367 if (err)
2368 goto out;
2369 zero_user(page, zerofrom, len);
2370 err = pagecache_write_end(file, mapping, curpos, len, len,
2371 page, fsdata);
2372 if (err < 0)
2373 goto out;
2374 BUG_ON(err != len);
2375 err = 0;
2376
2377 balance_dirty_pages_ratelimited(mapping);
2378
2379 if (fatal_signal_pending(current)) {
2380 err = -EINTR;
2381 goto out;
2382 }
2383 }
2384
2385 /* page covers the boundary, find the boundary offset */
2386 if (index == curidx) {
2387 zerofrom = curpos & ~PAGE_MASK;
2388 /* if we will expand the thing last block will be filled */
2389 if (offset <= zerofrom) {
2390 goto out;
2391 }
2392 if (zerofrom & (blocksize-1)) {
2393 *bytes |= (blocksize-1);
2394 (*bytes)++;
2395 }
2396 len = offset - zerofrom;
2397
2398 err = pagecache_write_begin(file, mapping, curpos, len, 0,
2399 &page, &fsdata);
2400 if (err)
2401 goto out;
2402 zero_user(page, zerofrom, len);
2403 err = pagecache_write_end(file, mapping, curpos, len, len,
2404 page, fsdata);
2405 if (err < 0)
2406 goto out;
2407 BUG_ON(err != len);
2408 err = 0;
2409 }
2410 out:
2411 return err;
2412 }
2413
2414 /*
2415 * For moronic filesystems that do not allow holes in file.
2416 * We may have to extend the file.
2417 */
cont_write_begin(struct file * file,struct address_space * mapping,loff_t pos,unsigned len,unsigned flags,struct page ** pagep,void ** fsdata,get_block_t * get_block,loff_t * bytes)2418 int cont_write_begin(struct file *file, struct address_space *mapping,
2419 loff_t pos, unsigned len, unsigned flags,
2420 struct page **pagep, void **fsdata,
2421 get_block_t *get_block, loff_t *bytes)
2422 {
2423 struct inode *inode = mapping->host;
2424 unsigned int blocksize = i_blocksize(inode);
2425 unsigned int zerofrom;
2426 int err;
2427
2428 err = cont_expand_zero(file, mapping, pos, bytes);
2429 if (err)
2430 return err;
2431
2432 zerofrom = *bytes & ~PAGE_MASK;
2433 if (pos+len > *bytes && zerofrom & (blocksize-1)) {
2434 *bytes |= (blocksize-1);
2435 (*bytes)++;
2436 }
2437
2438 return block_write_begin(mapping, pos, len, flags, pagep, get_block);
2439 }
2440 EXPORT_SYMBOL(cont_write_begin);
2441
block_commit_write(struct page * page,unsigned from,unsigned to)2442 int block_commit_write(struct page *page, unsigned from, unsigned to)
2443 {
2444 struct inode *inode = page->mapping->host;
2445 __block_commit_write(inode,page,from,to);
2446 return 0;
2447 }
2448 EXPORT_SYMBOL(block_commit_write);
2449
2450 /*
2451 * block_page_mkwrite() is not allowed to change the file size as it gets
2452 * called from a page fault handler when a page is first dirtied. Hence we must
2453 * be careful to check for EOF conditions here. We set the page up correctly
2454 * for a written page which means we get ENOSPC checking when writing into
2455 * holes and correct delalloc and unwritten extent mapping on filesystems that
2456 * support these features.
2457 *
2458 * We are not allowed to take the i_mutex here so we have to play games to
2459 * protect against truncate races as the page could now be beyond EOF. Because
2460 * truncate writes the inode size before removing pages, once we have the
2461 * page lock we can determine safely if the page is beyond EOF. If it is not
2462 * beyond EOF, then the page is guaranteed safe against truncation until we
2463 * unlock the page.
2464 *
2465 * Direct callers of this function should protect against filesystem freezing
2466 * using sb_start_pagefault() - sb_end_pagefault() functions.
2467 */
block_page_mkwrite(struct vm_area_struct * vma,struct vm_fault * vmf,get_block_t get_block)2468 int block_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf,
2469 get_block_t get_block)
2470 {
2471 struct page *page = vmf->page;
2472 struct inode *inode = file_inode(vma->vm_file);
2473 unsigned long end;
2474 loff_t size;
2475 int ret;
2476
2477 lock_page(page);
2478 size = i_size_read(inode);
2479 if ((page->mapping != inode->i_mapping) ||
2480 (page_offset(page) > size)) {
2481 /* We overload EFAULT to mean page got truncated */
2482 ret = -EFAULT;
2483 goto out_unlock;
2484 }
2485
2486 /* page is wholly or partially inside EOF */
2487 if (((page->index + 1) << PAGE_SHIFT) > size)
2488 end = size & ~PAGE_MASK;
2489 else
2490 end = PAGE_SIZE;
2491
2492 ret = __block_write_begin(page, 0, end, get_block);
2493 if (!ret)
2494 ret = block_commit_write(page, 0, end);
2495
2496 if (unlikely(ret < 0))
2497 goto out_unlock;
2498 set_page_dirty(page);
2499 wait_for_stable_page(page);
2500 return 0;
2501 out_unlock:
2502 unlock_page(page);
2503 return ret;
2504 }
2505 EXPORT_SYMBOL(block_page_mkwrite);
2506
2507 /*
2508 * nobh_write_begin()'s prereads are special: the buffer_heads are freed
2509 * immediately, while under the page lock. So it needs a special end_io
2510 * handler which does not touch the bh after unlocking it.
2511 */
end_buffer_read_nobh(struct buffer_head * bh,int uptodate)2512 static void end_buffer_read_nobh(struct buffer_head *bh, int uptodate)
2513 {
2514 __end_buffer_read_notouch(bh, uptodate);
2515 }
2516
2517 /*
2518 * Attach the singly-linked list of buffers created by nobh_write_begin, to
2519 * the page (converting it to circular linked list and taking care of page
2520 * dirty races).
2521 */
attach_nobh_buffers(struct page * page,struct buffer_head * head)2522 static void attach_nobh_buffers(struct page *page, struct buffer_head *head)
2523 {
2524 struct buffer_head *bh;
2525
2526 BUG_ON(!PageLocked(page));
2527
2528 spin_lock(&page->mapping->private_lock);
2529 bh = head;
2530 do {
2531 if (PageDirty(page))
2532 set_buffer_dirty(bh);
2533 if (!bh->b_this_page)
2534 bh->b_this_page = head;
2535 bh = bh->b_this_page;
2536 } while (bh != head);
2537 attach_page_buffers(page, head);
2538 spin_unlock(&page->mapping->private_lock);
2539 }
2540
2541 /*
2542 * On entry, the page is fully not uptodate.
2543 * On exit the page is fully uptodate in the areas outside (from,to)
2544 * The filesystem needs to handle block truncation upon failure.
2545 */
nobh_write_begin(struct address_space * mapping,loff_t pos,unsigned len,unsigned flags,struct page ** pagep,void ** fsdata,get_block_t * get_block)2546 int nobh_write_begin(struct address_space *mapping,
2547 loff_t pos, unsigned len, unsigned flags,
2548 struct page **pagep, void **fsdata,
2549 get_block_t *get_block)
2550 {
2551 struct inode *inode = mapping->host;
2552 const unsigned blkbits = inode->i_blkbits;
2553 const unsigned blocksize = 1 << blkbits;
2554 struct buffer_head *head, *bh;
2555 struct page *page;
2556 pgoff_t index;
2557 unsigned from, to;
2558 unsigned block_in_page;
2559 unsigned block_start, block_end;
2560 sector_t block_in_file;
2561 int nr_reads = 0;
2562 int ret = 0;
2563 int is_mapped_to_disk = 1;
2564
2565 index = pos >> PAGE_SHIFT;
2566 from = pos & (PAGE_SIZE - 1);
2567 to = from + len;
2568
2569 page = grab_cache_page_write_begin(mapping, index, flags);
2570 if (!page)
2571 return -ENOMEM;
2572 *pagep = page;
2573 *fsdata = NULL;
2574
2575 if (page_has_buffers(page)) {
2576 ret = __block_write_begin(page, pos, len, get_block);
2577 if (unlikely(ret))
2578 goto out_release;
2579 return ret;
2580 }
2581
2582 if (PageMappedToDisk(page))
2583 return 0;
2584
2585 /*
2586 * Allocate buffers so that we can keep track of state, and potentially
2587 * attach them to the page if an error occurs. In the common case of
2588 * no error, they will just be freed again without ever being attached
2589 * to the page (which is all OK, because we're under the page lock).
2590 *
2591 * Be careful: the buffer linked list is a NULL terminated one, rather
2592 * than the circular one we're used to.
2593 */
2594 head = alloc_page_buffers(page, blocksize, false);
2595 if (!head) {
2596 ret = -ENOMEM;
2597 goto out_release;
2598 }
2599
2600 block_in_file = (sector_t)page->index << (PAGE_SHIFT - blkbits);
2601
2602 /*
2603 * We loop across all blocks in the page, whether or not they are
2604 * part of the affected region. This is so we can discover if the
2605 * page is fully mapped-to-disk.
2606 */
2607 for (block_start = 0, block_in_page = 0, bh = head;
2608 block_start < PAGE_SIZE;
2609 block_in_page++, block_start += blocksize, bh = bh->b_this_page) {
2610 int create;
2611
2612 block_end = block_start + blocksize;
2613 bh->b_state = 0;
2614 create = 1;
2615 if (block_start >= to)
2616 create = 0;
2617 ret = get_block(inode, block_in_file + block_in_page,
2618 bh, create);
2619 if (ret)
2620 goto failed;
2621 if (!buffer_mapped(bh))
2622 is_mapped_to_disk = 0;
2623 if (buffer_new(bh))
2624 clean_bdev_bh_alias(bh);
2625 if (PageUptodate(page)) {
2626 set_buffer_uptodate(bh);
2627 continue;
2628 }
2629 if (buffer_new(bh) || !buffer_mapped(bh)) {
2630 zero_user_segments(page, block_start, from,
2631 to, block_end);
2632 continue;
2633 }
2634 if (buffer_uptodate(bh))
2635 continue; /* reiserfs does this */
2636 if (block_start < from || block_end > to) {
2637 lock_buffer(bh);
2638 bh->b_end_io = end_buffer_read_nobh;
2639 submit_bh(REQ_OP_READ, 0, bh);
2640 nr_reads++;
2641 }
2642 }
2643
2644 if (nr_reads) {
2645 /*
2646 * The page is locked, so these buffers are protected from
2647 * any VM or truncate activity. Hence we don't need to care
2648 * for the buffer_head refcounts.
2649 */
2650 for (bh = head; bh; bh = bh->b_this_page) {
2651 wait_on_buffer(bh);
2652 if (!buffer_uptodate(bh))
2653 ret = -EIO;
2654 }
2655 if (ret)
2656 goto failed;
2657 }
2658
2659 if (is_mapped_to_disk)
2660 SetPageMappedToDisk(page);
2661
2662 *fsdata = head; /* to be released by nobh_write_end */
2663
2664 return 0;
2665
2666 failed:
2667 BUG_ON(!ret);
2668 /*
2669 * Error recovery is a bit difficult. We need to zero out blocks that
2670 * were newly allocated, and dirty them to ensure they get written out.
2671 * Buffers need to be attached to the page at this point, otherwise
2672 * the handling of potential IO errors during writeout would be hard
2673 * (could try doing synchronous writeout, but what if that fails too?)
2674 */
2675 attach_nobh_buffers(page, head);
2676 page_zero_new_buffers(page, from, to);
2677
2678 out_release:
2679 unlock_page(page);
2680 put_page(page);
2681 *pagep = NULL;
2682
2683 return ret;
2684 }
2685 EXPORT_SYMBOL(nobh_write_begin);
2686
nobh_write_end(struct file * file,struct address_space * mapping,loff_t pos,unsigned len,unsigned copied,struct page * page,void * fsdata)2687 int nobh_write_end(struct file *file, struct address_space *mapping,
2688 loff_t pos, unsigned len, unsigned copied,
2689 struct page *page, void *fsdata)
2690 {
2691 struct inode *inode = page->mapping->host;
2692 struct buffer_head *head = fsdata;
2693 struct buffer_head *bh;
2694 BUG_ON(fsdata != NULL && page_has_buffers(page));
2695
2696 if (unlikely(copied < len) && head)
2697 attach_nobh_buffers(page, head);
2698 if (page_has_buffers(page))
2699 return generic_write_end(file, mapping, pos, len,
2700 copied, page, fsdata);
2701
2702 SetPageUptodate(page);
2703 set_page_dirty(page);
2704 if (pos+copied > inode->i_size) {
2705 i_size_write(inode, pos+copied);
2706 mark_inode_dirty(inode);
2707 }
2708
2709 unlock_page(page);
2710 put_page(page);
2711
2712 while (head) {
2713 bh = head;
2714 head = head->b_this_page;
2715 free_buffer_head(bh);
2716 }
2717
2718 return copied;
2719 }
2720 EXPORT_SYMBOL(nobh_write_end);
2721
2722 /*
2723 * nobh_writepage() - based on block_full_write_page() except
2724 * that it tries to operate without attaching bufferheads to
2725 * the page.
2726 */
nobh_writepage(struct page * page,get_block_t * get_block,struct writeback_control * wbc)2727 int nobh_writepage(struct page *page, get_block_t *get_block,
2728 struct writeback_control *wbc)
2729 {
2730 struct inode * const inode = page->mapping->host;
2731 loff_t i_size = i_size_read(inode);
2732 const pgoff_t end_index = i_size >> PAGE_SHIFT;
2733 unsigned offset;
2734 int ret;
2735
2736 /* Is the page fully inside i_size? */
2737 if (page->index < end_index)
2738 goto out;
2739
2740 /* Is the page fully outside i_size? (truncate in progress) */
2741 offset = i_size & (PAGE_SIZE-1);
2742 if (page->index >= end_index+1 || !offset) {
2743 unlock_page(page);
2744 return 0; /* don't care */
2745 }
2746
2747 /*
2748 * The page straddles i_size. It must be zeroed out on each and every
2749 * writepage invocation because it may be mmapped. "A file is mapped
2750 * in multiples of the page size. For a file that is not a multiple of
2751 * the page size, the remaining memory is zeroed when mapped, and
2752 * writes to that region are not written out to the file."
2753 */
2754 zero_user_segment(page, offset, PAGE_SIZE);
2755 out:
2756 ret = mpage_writepage(page, get_block, wbc);
2757 if (ret == -EAGAIN)
2758 ret = __block_write_full_page(inode, page, get_block, wbc,
2759 end_buffer_async_write);
2760 return ret;
2761 }
2762 EXPORT_SYMBOL(nobh_writepage);
2763
nobh_truncate_page(struct address_space * mapping,loff_t from,get_block_t * get_block)2764 int nobh_truncate_page(struct address_space *mapping,
2765 loff_t from, get_block_t *get_block)
2766 {
2767 pgoff_t index = from >> PAGE_SHIFT;
2768 unsigned offset = from & (PAGE_SIZE-1);
2769 unsigned blocksize;
2770 sector_t iblock;
2771 unsigned length, pos;
2772 struct inode *inode = mapping->host;
2773 struct page *page;
2774 struct buffer_head map_bh;
2775 int err;
2776
2777 blocksize = i_blocksize(inode);
2778 length = offset & (blocksize - 1);
2779
2780 /* Block boundary? Nothing to do */
2781 if (!length)
2782 return 0;
2783
2784 length = blocksize - length;
2785 iblock = (sector_t)index << (PAGE_SHIFT - inode->i_blkbits);
2786
2787 page = grab_cache_page(mapping, index);
2788 err = -ENOMEM;
2789 if (!page)
2790 goto out;
2791
2792 if (page_has_buffers(page)) {
2793 has_buffers:
2794 unlock_page(page);
2795 put_page(page);
2796 return block_truncate_page(mapping, from, get_block);
2797 }
2798
2799 /* Find the buffer that contains "offset" */
2800 pos = blocksize;
2801 while (offset >= pos) {
2802 iblock++;
2803 pos += blocksize;
2804 }
2805
2806 map_bh.b_size = blocksize;
2807 map_bh.b_state = 0;
2808 err = get_block(inode, iblock, &map_bh, 0);
2809 if (err)
2810 goto unlock;
2811 /* unmapped? It's a hole - nothing to do */
2812 if (!buffer_mapped(&map_bh))
2813 goto unlock;
2814
2815 /* Ok, it's mapped. Make sure it's up-to-date */
2816 if (!PageUptodate(page)) {
2817 err = mapping->a_ops->readpage(NULL, page);
2818 if (err) {
2819 put_page(page);
2820 goto out;
2821 }
2822 lock_page(page);
2823 if (!PageUptodate(page)) {
2824 err = -EIO;
2825 goto unlock;
2826 }
2827 if (page_has_buffers(page))
2828 goto has_buffers;
2829 }
2830 zero_user(page, offset, length);
2831 set_page_dirty(page);
2832 err = 0;
2833
2834 unlock:
2835 unlock_page(page);
2836 put_page(page);
2837 out:
2838 return err;
2839 }
2840 EXPORT_SYMBOL(nobh_truncate_page);
2841
block_truncate_page(struct address_space * mapping,loff_t from,get_block_t * get_block)2842 int block_truncate_page(struct address_space *mapping,
2843 loff_t from, get_block_t *get_block)
2844 {
2845 pgoff_t index = from >> PAGE_SHIFT;
2846 unsigned offset = from & (PAGE_SIZE-1);
2847 unsigned blocksize;
2848 sector_t iblock;
2849 unsigned length, pos;
2850 struct inode *inode = mapping->host;
2851 struct page *page;
2852 struct buffer_head *bh;
2853 int err;
2854
2855 blocksize = i_blocksize(inode);
2856 length = offset & (blocksize - 1);
2857
2858 /* Block boundary? Nothing to do */
2859 if (!length)
2860 return 0;
2861
2862 length = blocksize - length;
2863 iblock = (sector_t)index << (PAGE_SHIFT - inode->i_blkbits);
2864
2865 page = grab_cache_page(mapping, index);
2866 err = -ENOMEM;
2867 if (!page)
2868 goto out;
2869
2870 if (!page_has_buffers(page))
2871 create_empty_buffers(page, blocksize, 0);
2872
2873 /* Find the buffer that contains "offset" */
2874 bh = page_buffers(page);
2875 pos = blocksize;
2876 while (offset >= pos) {
2877 bh = bh->b_this_page;
2878 iblock++;
2879 pos += blocksize;
2880 }
2881
2882 err = 0;
2883 if (!buffer_mapped(bh)) {
2884 WARN_ON(bh->b_size != blocksize);
2885 err = get_block(inode, iblock, bh, 0);
2886 if (err)
2887 goto unlock;
2888 /* unmapped? It's a hole - nothing to do */
2889 if (!buffer_mapped(bh))
2890 goto unlock;
2891 }
2892
2893 /* Ok, it's mapped. Make sure it's up-to-date */
2894 if (PageUptodate(page))
2895 set_buffer_uptodate(bh);
2896
2897 if (!buffer_uptodate(bh) && !buffer_delay(bh) && !buffer_unwritten(bh)) {
2898 err = -EIO;
2899 ll_rw_block(REQ_OP_READ, 0, 1, &bh);
2900 wait_on_buffer(bh);
2901 /* Uhhuh. Read error. Complain and punt. */
2902 if (!buffer_uptodate(bh))
2903 goto unlock;
2904 }
2905
2906 zero_user(page, offset, length);
2907 mark_buffer_dirty(bh);
2908 err = 0;
2909
2910 unlock:
2911 unlock_page(page);
2912 put_page(page);
2913 out:
2914 return err;
2915 }
2916 EXPORT_SYMBOL(block_truncate_page);
2917
2918 /*
2919 * The generic ->writepage function for buffer-backed address_spaces
2920 */
block_write_full_page(struct page * page,get_block_t * get_block,struct writeback_control * wbc)2921 int block_write_full_page(struct page *page, get_block_t *get_block,
2922 struct writeback_control *wbc)
2923 {
2924 struct inode * const inode = page->mapping->host;
2925 loff_t i_size = i_size_read(inode);
2926 const pgoff_t end_index = i_size >> PAGE_SHIFT;
2927 unsigned offset;
2928
2929 /* Is the page fully inside i_size? */
2930 if (page->index < end_index)
2931 return __block_write_full_page(inode, page, get_block, wbc,
2932 end_buffer_async_write);
2933
2934 /* Is the page fully outside i_size? (truncate in progress) */
2935 offset = i_size & (PAGE_SIZE-1);
2936 if (page->index >= end_index+1 || !offset) {
2937 unlock_page(page);
2938 return 0; /* don't care */
2939 }
2940
2941 /*
2942 * The page straddles i_size. It must be zeroed out on each and every
2943 * writepage invocation because it may be mmapped. "A file is mapped
2944 * in multiples of the page size. For a file that is not a multiple of
2945 * the page size, the remaining memory is zeroed when mapped, and
2946 * writes to that region are not written out to the file."
2947 */
2948 zero_user_segment(page, offset, PAGE_SIZE);
2949 return __block_write_full_page(inode, page, get_block, wbc,
2950 end_buffer_async_write);
2951 }
2952 EXPORT_SYMBOL(block_write_full_page);
2953
generic_block_bmap(struct address_space * mapping,sector_t block,get_block_t * get_block)2954 sector_t generic_block_bmap(struct address_space *mapping, sector_t block,
2955 get_block_t *get_block)
2956 {
2957 struct inode *inode = mapping->host;
2958 struct buffer_head tmp = {
2959 .b_size = i_blocksize(inode),
2960 };
2961
2962 get_block(inode, block, &tmp, 0);
2963 return tmp.b_blocknr;
2964 }
2965 EXPORT_SYMBOL(generic_block_bmap);
2966
end_bio_bh_io_sync(struct bio * bio)2967 static void end_bio_bh_io_sync(struct bio *bio)
2968 {
2969 struct buffer_head *bh = bio->bi_private;
2970
2971 if (unlikely(bio_flagged(bio, BIO_QUIET)))
2972 set_bit(BH_Quiet, &bh->b_state);
2973
2974 bh->b_end_io(bh, !bio->bi_status);
2975 bio_put(bio);
2976 }
2977
2978 /*
2979 * This allows us to do IO even on the odd last sectors
2980 * of a device, even if the block size is some multiple
2981 * of the physical sector size.
2982 *
2983 * We'll just truncate the bio to the size of the device,
2984 * and clear the end of the buffer head manually.
2985 *
2986 * Truly out-of-range accesses will turn into actual IO
2987 * errors, this only handles the "we need to be able to
2988 * do IO at the final sector" case.
2989 */
guard_bio_eod(struct bio * bio)2990 void guard_bio_eod(struct bio *bio)
2991 {
2992 sector_t maxsector;
2993 struct hd_struct *part;
2994
2995 rcu_read_lock();
2996 part = __disk_get_part(bio->bi_disk, bio->bi_partno);
2997 if (part)
2998 maxsector = part_nr_sects_read(part);
2999 else
3000 maxsector = get_capacity(bio->bi_disk);
3001 rcu_read_unlock();
3002
3003 if (!maxsector)
3004 return;
3005
3006 /*
3007 * If the *whole* IO is past the end of the device,
3008 * let it through, and the IO layer will turn it into
3009 * an EIO.
3010 */
3011 if (unlikely(bio->bi_iter.bi_sector >= maxsector))
3012 return;
3013
3014 maxsector -= bio->bi_iter.bi_sector;
3015 if (likely((bio->bi_iter.bi_size >> 9) <= maxsector))
3016 return;
3017
3018 bio_truncate(bio, maxsector << 9);
3019 }
3020
submit_bh_wbc(int op,int op_flags,struct buffer_head * bh,enum rw_hint write_hint,struct writeback_control * wbc)3021 static int submit_bh_wbc(int op, int op_flags, struct buffer_head *bh,
3022 enum rw_hint write_hint, struct writeback_control *wbc)
3023 {
3024 struct bio *bio;
3025
3026 BUG_ON(!buffer_locked(bh));
3027 BUG_ON(!buffer_mapped(bh));
3028 BUG_ON(!bh->b_end_io);
3029 BUG_ON(buffer_delay(bh));
3030 BUG_ON(buffer_unwritten(bh));
3031
3032 /*
3033 * Only clear out a write error when rewriting
3034 */
3035 if (test_set_buffer_req(bh) && (op == REQ_OP_WRITE))
3036 clear_buffer_write_io_error(bh);
3037
3038 /*
3039 * from here on down, it's all bio -- do the initial mapping,
3040 * submit_bio -> generic_make_request may further map this bio around
3041 */
3042 bio = bio_alloc(GFP_NOIO, 1);
3043
3044 fscrypt_set_bio_crypt_ctx_bh(bio, bh, GFP_NOIO);
3045
3046 bio->bi_iter.bi_sector = bh->b_blocknr * (bh->b_size >> 9);
3047 bio_set_dev(bio, bh->b_bdev);
3048 bio->bi_write_hint = write_hint;
3049
3050 bio_add_page(bio, bh->b_page, bh->b_size, bh_offset(bh));
3051 BUG_ON(bio->bi_iter.bi_size != bh->b_size);
3052
3053 bio->bi_end_io = end_bio_bh_io_sync;
3054 bio->bi_private = bh;
3055
3056 if (buffer_meta(bh))
3057 op_flags |= REQ_META;
3058 if (buffer_prio(bh))
3059 op_flags |= REQ_PRIO;
3060 bio_set_op_attrs(bio, op, op_flags);
3061
3062 /* Take care of bh's that straddle the end of the device */
3063 guard_bio_eod(bio);
3064
3065 if (wbc) {
3066 wbc_init_bio(wbc, bio);
3067 wbc_account_cgroup_owner(wbc, bh->b_page, bh->b_size);
3068 }
3069
3070 submit_bio(bio);
3071 return 0;
3072 }
3073
submit_bh(int op,int op_flags,struct buffer_head * bh)3074 int submit_bh(int op, int op_flags, struct buffer_head *bh)
3075 {
3076 return submit_bh_wbc(op, op_flags, bh, 0, NULL);
3077 }
3078 EXPORT_SYMBOL(submit_bh);
3079
3080 /**
3081 * ll_rw_block: low-level access to block devices (DEPRECATED)
3082 * @op: whether to %READ or %WRITE
3083 * @op_flags: req_flag_bits
3084 * @nr: number of &struct buffer_heads in the array
3085 * @bhs: array of pointers to &struct buffer_head
3086 *
3087 * ll_rw_block() takes an array of pointers to &struct buffer_heads, and
3088 * requests an I/O operation on them, either a %REQ_OP_READ or a %REQ_OP_WRITE.
3089 * @op_flags contains flags modifying the detailed I/O behavior, most notably
3090 * %REQ_RAHEAD.
3091 *
3092 * This function drops any buffer that it cannot get a lock on (with the
3093 * BH_Lock state bit), any buffer that appears to be clean when doing a write
3094 * request, and any buffer that appears to be up-to-date when doing read
3095 * request. Further it marks as clean buffers that are processed for
3096 * writing (the buffer cache won't assume that they are actually clean
3097 * until the buffer gets unlocked).
3098 *
3099 * ll_rw_block sets b_end_io to simple completion handler that marks
3100 * the buffer up-to-date (if appropriate), unlocks the buffer and wakes
3101 * any waiters.
3102 *
3103 * All of the buffers must be for the same device, and must also be a
3104 * multiple of the current approved size for the device.
3105 */
ll_rw_block(int op,int op_flags,int nr,struct buffer_head * bhs[])3106 void ll_rw_block(int op, int op_flags, int nr, struct buffer_head *bhs[])
3107 {
3108 int i;
3109
3110 for (i = 0; i < nr; i++) {
3111 struct buffer_head *bh = bhs[i];
3112
3113 if (!trylock_buffer(bh))
3114 continue;
3115 if (op == WRITE) {
3116 if (test_clear_buffer_dirty(bh)) {
3117 bh->b_end_io = end_buffer_write_sync;
3118 get_bh(bh);
3119 submit_bh(op, op_flags, bh);
3120 continue;
3121 }
3122 } else {
3123 if (!buffer_uptodate(bh)) {
3124 bh->b_end_io = end_buffer_read_sync;
3125 get_bh(bh);
3126 submit_bh(op, op_flags, bh);
3127 continue;
3128 }
3129 }
3130 unlock_buffer(bh);
3131 }
3132 }
3133 EXPORT_SYMBOL(ll_rw_block);
3134
write_dirty_buffer(struct buffer_head * bh,int op_flags)3135 void write_dirty_buffer(struct buffer_head *bh, int op_flags)
3136 {
3137 lock_buffer(bh);
3138 if (!test_clear_buffer_dirty(bh)) {
3139 unlock_buffer(bh);
3140 return;
3141 }
3142 bh->b_end_io = end_buffer_write_sync;
3143 get_bh(bh);
3144 submit_bh(REQ_OP_WRITE, op_flags, bh);
3145 }
3146 EXPORT_SYMBOL(write_dirty_buffer);
3147
3148 /*
3149 * For a data-integrity writeout, we need to wait upon any in-progress I/O
3150 * and then start new I/O and then wait upon it. The caller must have a ref on
3151 * the buffer_head.
3152 */
__sync_dirty_buffer(struct buffer_head * bh,int op_flags)3153 int __sync_dirty_buffer(struct buffer_head *bh, int op_flags)
3154 {
3155 int ret = 0;
3156
3157 WARN_ON(atomic_read(&bh->b_count) < 1);
3158 lock_buffer(bh);
3159 if (test_clear_buffer_dirty(bh)) {
3160 /*
3161 * The bh should be mapped, but it might not be if the
3162 * device was hot-removed. Not much we can do but fail the I/O.
3163 */
3164 if (!buffer_mapped(bh)) {
3165 unlock_buffer(bh);
3166 return -EIO;
3167 }
3168
3169 get_bh(bh);
3170 bh->b_end_io = end_buffer_write_sync;
3171 ret = submit_bh(REQ_OP_WRITE, op_flags, bh);
3172 wait_on_buffer(bh);
3173 if (!ret && !buffer_uptodate(bh))
3174 ret = -EIO;
3175 } else {
3176 unlock_buffer(bh);
3177 }
3178 return ret;
3179 }
3180 EXPORT_SYMBOL(__sync_dirty_buffer);
3181
sync_dirty_buffer(struct buffer_head * bh)3182 int sync_dirty_buffer(struct buffer_head *bh)
3183 {
3184 return __sync_dirty_buffer(bh, REQ_SYNC);
3185 }
3186 EXPORT_SYMBOL(sync_dirty_buffer);
3187
3188 /*
3189 * try_to_free_buffers() checks if all the buffers on this particular page
3190 * are unused, and releases them if so.
3191 *
3192 * Exclusion against try_to_free_buffers may be obtained by either
3193 * locking the page or by holding its mapping's private_lock.
3194 *
3195 * If the page is dirty but all the buffers are clean then we need to
3196 * be sure to mark the page clean as well. This is because the page
3197 * may be against a block device, and a later reattachment of buffers
3198 * to a dirty page will set *all* buffers dirty. Which would corrupt
3199 * filesystem data on the same device.
3200 *
3201 * The same applies to regular filesystem pages: if all the buffers are
3202 * clean then we set the page clean and proceed. To do that, we require
3203 * total exclusion from __set_page_dirty_buffers(). That is obtained with
3204 * private_lock.
3205 *
3206 * try_to_free_buffers() is non-blocking.
3207 */
buffer_busy(struct buffer_head * bh)3208 static inline int buffer_busy(struct buffer_head *bh)
3209 {
3210 return atomic_read(&bh->b_count) |
3211 (bh->b_state & ((1 << BH_Dirty) | (1 << BH_Lock)));
3212 }
3213
3214 static int
drop_buffers(struct page * page,struct buffer_head ** buffers_to_free)3215 drop_buffers(struct page *page, struct buffer_head **buffers_to_free)
3216 {
3217 struct buffer_head *head = page_buffers(page);
3218 struct buffer_head *bh;
3219
3220 bh = head;
3221 do {
3222 if (buffer_busy(bh))
3223 goto failed;
3224 bh = bh->b_this_page;
3225 } while (bh != head);
3226
3227 do {
3228 struct buffer_head *next = bh->b_this_page;
3229
3230 if (bh->b_assoc_map)
3231 __remove_assoc_queue(bh);
3232 bh = next;
3233 } while (bh != head);
3234 *buffers_to_free = head;
3235 __clear_page_buffers(page);
3236 return 1;
3237 failed:
3238 return 0;
3239 }
3240
try_to_free_buffers(struct page * page)3241 int try_to_free_buffers(struct page *page)
3242 {
3243 struct address_space * const mapping = page->mapping;
3244 struct buffer_head *buffers_to_free = NULL;
3245 int ret = 0;
3246
3247 BUG_ON(!PageLocked(page));
3248 if (PageWriteback(page))
3249 return 0;
3250
3251 if (mapping == NULL) { /* can this still happen? */
3252 ret = drop_buffers(page, &buffers_to_free);
3253 goto out;
3254 }
3255
3256 spin_lock(&mapping->private_lock);
3257 ret = drop_buffers(page, &buffers_to_free);
3258
3259 /*
3260 * If the filesystem writes its buffers by hand (eg ext3)
3261 * then we can have clean buffers against a dirty page. We
3262 * clean the page here; otherwise the VM will never notice
3263 * that the filesystem did any IO at all.
3264 *
3265 * Also, during truncate, discard_buffer will have marked all
3266 * the page's buffers clean. We discover that here and clean
3267 * the page also.
3268 *
3269 * private_lock must be held over this entire operation in order
3270 * to synchronise against __set_page_dirty_buffers and prevent the
3271 * dirty bit from being lost.
3272 */
3273 if (ret)
3274 cancel_dirty_page(page);
3275 spin_unlock(&mapping->private_lock);
3276 out:
3277 if (buffers_to_free) {
3278 struct buffer_head *bh = buffers_to_free;
3279
3280 do {
3281 struct buffer_head *next = bh->b_this_page;
3282 free_buffer_head(bh);
3283 bh = next;
3284 } while (bh != buffers_to_free);
3285 }
3286 return ret;
3287 }
3288 EXPORT_SYMBOL(try_to_free_buffers);
3289
3290 /*
3291 * There are no bdflush tunables left. But distributions are
3292 * still running obsolete flush daemons, so we terminate them here.
3293 *
3294 * Use of bdflush() is deprecated and will be removed in a future kernel.
3295 * The `flush-X' kernel threads fully replace bdflush daemons and this call.
3296 */
SYSCALL_DEFINE2(bdflush,int,func,long,data)3297 SYSCALL_DEFINE2(bdflush, int, func, long, data)
3298 {
3299 static int msg_count;
3300
3301 if (!capable(CAP_SYS_ADMIN))
3302 return -EPERM;
3303
3304 if (msg_count < 5) {
3305 msg_count++;
3306 printk(KERN_INFO
3307 "warning: process `%s' used the obsolete bdflush"
3308 " system call\n", current->comm);
3309 printk(KERN_INFO "Fix your initscripts?\n");
3310 }
3311
3312 if (func == 1)
3313 do_exit(0);
3314 return 0;
3315 }
3316
3317 /*
3318 * Buffer-head allocation
3319 */
3320 static struct kmem_cache *bh_cachep __read_mostly;
3321
3322 /*
3323 * Once the number of bh's in the machine exceeds this level, we start
3324 * stripping them in writeback.
3325 */
3326 static unsigned long max_buffer_heads;
3327
3328 int buffer_heads_over_limit;
3329
3330 struct bh_accounting {
3331 int nr; /* Number of live bh's */
3332 int ratelimit; /* Limit cacheline bouncing */
3333 };
3334
3335 static DEFINE_PER_CPU(struct bh_accounting, bh_accounting) = {0, 0};
3336
recalc_bh_state(void)3337 static void recalc_bh_state(void)
3338 {
3339 int i;
3340 int tot = 0;
3341
3342 if (__this_cpu_inc_return(bh_accounting.ratelimit) - 1 < 4096)
3343 return;
3344 __this_cpu_write(bh_accounting.ratelimit, 0);
3345 for_each_online_cpu(i)
3346 tot += per_cpu(bh_accounting, i).nr;
3347 buffer_heads_over_limit = (tot > max_buffer_heads);
3348 }
3349
alloc_buffer_head(gfp_t gfp_flags)3350 struct buffer_head *alloc_buffer_head(gfp_t gfp_flags)
3351 {
3352 struct buffer_head *ret = kmem_cache_zalloc(bh_cachep, gfp_flags);
3353 if (ret) {
3354 INIT_LIST_HEAD(&ret->b_assoc_buffers);
3355 preempt_disable();
3356 __this_cpu_inc(bh_accounting.nr);
3357 recalc_bh_state();
3358 preempt_enable();
3359 }
3360 return ret;
3361 }
3362 EXPORT_SYMBOL(alloc_buffer_head);
3363
free_buffer_head(struct buffer_head * bh)3364 void free_buffer_head(struct buffer_head *bh)
3365 {
3366 BUG_ON(!list_empty(&bh->b_assoc_buffers));
3367 kmem_cache_free(bh_cachep, bh);
3368 preempt_disable();
3369 __this_cpu_dec(bh_accounting.nr);
3370 recalc_bh_state();
3371 preempt_enable();
3372 }
3373 EXPORT_SYMBOL(free_buffer_head);
3374
buffer_exit_cpu_dead(unsigned int cpu)3375 static int buffer_exit_cpu_dead(unsigned int cpu)
3376 {
3377 int i;
3378 struct bh_lru *b = &per_cpu(bh_lrus, cpu);
3379
3380 for (i = 0; i < BH_LRU_SIZE; i++) {
3381 brelse(b->bhs[i]);
3382 b->bhs[i] = NULL;
3383 }
3384 this_cpu_add(bh_accounting.nr, per_cpu(bh_accounting, cpu).nr);
3385 per_cpu(bh_accounting, cpu).nr = 0;
3386 return 0;
3387 }
3388
3389 /**
3390 * bh_uptodate_or_lock - Test whether the buffer is uptodate
3391 * @bh: struct buffer_head
3392 *
3393 * Return true if the buffer is up-to-date and false,
3394 * with the buffer locked, if not.
3395 */
bh_uptodate_or_lock(struct buffer_head * bh)3396 int bh_uptodate_or_lock(struct buffer_head *bh)
3397 {
3398 if (!buffer_uptodate(bh)) {
3399 lock_buffer(bh);
3400 if (!buffer_uptodate(bh))
3401 return 0;
3402 unlock_buffer(bh);
3403 }
3404 return 1;
3405 }
3406 EXPORT_SYMBOL(bh_uptodate_or_lock);
3407
3408 /**
3409 * bh_submit_read - Submit a locked buffer for reading
3410 * @bh: struct buffer_head
3411 *
3412 * Returns zero on success and -EIO on error.
3413 */
bh_submit_read(struct buffer_head * bh)3414 int bh_submit_read(struct buffer_head *bh)
3415 {
3416 BUG_ON(!buffer_locked(bh));
3417
3418 if (buffer_uptodate(bh)) {
3419 unlock_buffer(bh);
3420 return 0;
3421 }
3422
3423 get_bh(bh);
3424 bh->b_end_io = end_buffer_read_sync;
3425 submit_bh(REQ_OP_READ, 0, bh);
3426 wait_on_buffer(bh);
3427 if (buffer_uptodate(bh))
3428 return 0;
3429 return -EIO;
3430 }
3431 EXPORT_SYMBOL(bh_submit_read);
3432
buffer_init(void)3433 void __init buffer_init(void)
3434 {
3435 unsigned long nrpages;
3436 int ret;
3437
3438 bh_cachep = kmem_cache_create("buffer_head",
3439 sizeof(struct buffer_head), 0,
3440 (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
3441 SLAB_MEM_SPREAD),
3442 NULL);
3443
3444 /*
3445 * Limit the bh occupancy to 10% of ZONE_NORMAL
3446 */
3447 nrpages = (nr_free_buffer_pages() * 10) / 100;
3448 max_buffer_heads = nrpages * (PAGE_SIZE / sizeof(struct buffer_head));
3449 ret = cpuhp_setup_state_nocalls(CPUHP_FS_BUFF_DEAD, "fs/buffer:dead",
3450 NULL, buffer_exit_cpu_dead);
3451 WARN_ON(ret < 0);
3452 }
3453