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