1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * fs/fs-writeback.c 4 * 5 * Copyright (C) 2002, Linus Torvalds. 6 * 7 * Contains all the functions related to writing back and waiting 8 * upon dirty inodes against superblocks, and writing back dirty 9 * pages against inodes. ie: data writeback. Writeout of the 10 * inode itself is not handled here. 11 * 12 * 10Apr2002 Andrew Morton 13 * Split out of fs/inode.c 14 * Additions for address_space-based writeback 15 */ 16 17 #include <linux/sched/sysctl.h> 18 #include <linux/kernel.h> 19 #include <linux/export.h> 20 #include <linux/spinlock.h> 21 #include <linux/slab.h> 22 #include <linux/sched.h> 23 #include <linux/fs.h> 24 #include <linux/mm.h> 25 #include <linux/pagemap.h> 26 #include <linux/kthread.h> 27 #include <linux/writeback.h> 28 #include <linux/blkdev.h> 29 #include <linux/backing-dev.h> 30 #include <linux/tracepoint.h> 31 #include <linux/device.h> 32 #include <linux/memcontrol.h> 33 #include "internal.h" 34 35 /* 36 * Passed into wb_writeback(), essentially a subset of writeback_control 37 */ 38 struct wb_writeback_work { 39 long nr_pages; 40 struct super_block *sb; 41 enum writeback_sync_modes sync_mode; 42 unsigned int tagged_writepages:1; 43 unsigned int for_kupdate:1; 44 unsigned int range_cyclic:1; 45 unsigned int for_background:1; 46 unsigned int for_sync:1; /* sync(2) WB_SYNC_ALL writeback */ 47 unsigned int auto_free:1; /* free on completion */ 48 enum wb_reason reason; /* why was writeback initiated? */ 49 50 struct list_head list; /* pending work list */ 51 struct wb_completion *done; /* set if the caller waits */ 52 }; 53 54 /* 55 * If an inode is constantly having its pages dirtied, but then the 56 * updates stop dirtytime_expire_interval seconds in the past, it's 57 * possible for the worst case time between when an inode has its 58 * timestamps updated and when they finally get written out to be two 59 * dirtytime_expire_intervals. We set the default to 12 hours (in 60 * seconds), which means most of the time inodes will have their 61 * timestamps written to disk after 12 hours, but in the worst case a 62 * few inodes might not their timestamps updated for 24 hours. 63 */ 64 static unsigned int dirtytime_expire_interval = 12 * 60 * 60; 65 66 static inline struct inode *wb_inode(struct list_head *head) 67 { 68 return list_entry(head, struct inode, i_io_list); 69 } 70 71 /* 72 * Include the creation of the trace points after defining the 73 * wb_writeback_work structure and inline functions so that the definition 74 * remains local to this file. 75 */ 76 #define CREATE_TRACE_POINTS 77 #include <trace/events/writeback.h> 78 79 EXPORT_TRACEPOINT_SYMBOL_GPL(wbc_writepage); 80 81 static bool wb_io_lists_populated(struct bdi_writeback *wb) 82 { 83 if (wb_has_dirty_io(wb)) { 84 return false; 85 } else { 86 set_bit(WB_has_dirty_io, &wb->state); 87 WARN_ON_ONCE(!wb->avg_write_bandwidth); 88 atomic_long_add(wb->avg_write_bandwidth, 89 &wb->bdi->tot_write_bandwidth); 90 return true; 91 } 92 } 93 94 static void wb_io_lists_depopulated(struct bdi_writeback *wb) 95 { 96 if (wb_has_dirty_io(wb) && list_empty(&wb->b_dirty) && 97 list_empty(&wb->b_io) && list_empty(&wb->b_more_io)) { 98 clear_bit(WB_has_dirty_io, &wb->state); 99 WARN_ON_ONCE(atomic_long_sub_return(wb->avg_write_bandwidth, 100 &wb->bdi->tot_write_bandwidth) < 0); 101 } 102 } 103 104 /** 105 * inode_io_list_move_locked - move an inode onto a bdi_writeback IO list 106 * @inode: inode to be moved 107 * @wb: target bdi_writeback 108 * @head: one of @wb->b_{dirty|io|more_io|dirty_time} 109 * 110 * Move @inode->i_io_list to @list of @wb and set %WB_has_dirty_io. 111 * Returns %true if @inode is the first occupant of the !dirty_time IO 112 * lists; otherwise, %false. 113 */ 114 static bool inode_io_list_move_locked(struct inode *inode, 115 struct bdi_writeback *wb, 116 struct list_head *head) 117 { 118 assert_spin_locked(&wb->list_lock); 119 assert_spin_locked(&inode->i_lock); 120 WARN_ON_ONCE(inode_state_read(inode) & I_FREEING); 121 122 list_move(&inode->i_io_list, head); 123 124 /* dirty_time doesn't count as dirty_io until expiration */ 125 if (head != &wb->b_dirty_time) 126 return wb_io_lists_populated(wb); 127 128 wb_io_lists_depopulated(wb); 129 return false; 130 } 131 132 static void wb_wakeup(struct bdi_writeback *wb) 133 { 134 spin_lock_irq(&wb->work_lock); 135 if (test_bit(WB_registered, &wb->state)) 136 mod_delayed_work(bdi_wq, &wb->dwork, 0); 137 spin_unlock_irq(&wb->work_lock); 138 } 139 140 /* 141 * This function is used when the first inode for this wb is marked dirty. It 142 * wakes-up the corresponding bdi thread which should then take care of the 143 * periodic background write-out of dirty inodes. Since the write-out would 144 * starts only 'dirty_writeback_interval' centisecs from now anyway, we just 145 * set up a timer which wakes the bdi thread up later. 146 * 147 * Note, we wouldn't bother setting up the timer, but this function is on the 148 * fast-path (used by '__mark_inode_dirty()'), so we save few context switches 149 * by delaying the wake-up. 150 * 151 * We have to be careful not to postpone flush work if it is scheduled for 152 * earlier. Thus we use queue_delayed_work(). 153 */ 154 static void wb_wakeup_delayed(struct bdi_writeback *wb) 155 { 156 unsigned long timeout; 157 158 timeout = msecs_to_jiffies(dirty_writeback_interval * 10); 159 spin_lock_irq(&wb->work_lock); 160 if (test_bit(WB_registered, &wb->state)) 161 queue_delayed_work(bdi_wq, &wb->dwork, timeout); 162 spin_unlock_irq(&wb->work_lock); 163 } 164 165 static void finish_writeback_work(struct wb_writeback_work *work) 166 { 167 struct wb_completion *done = work->done; 168 169 if (work->auto_free) 170 kfree(work); 171 if (done) { 172 wait_queue_head_t *waitq = done->waitq; 173 174 /* @done can't be accessed after the following dec */ 175 if (atomic_dec_and_test(&done->cnt)) 176 wake_up_all(waitq); 177 } 178 } 179 180 static void wb_queue_work(struct bdi_writeback *wb, 181 struct wb_writeback_work *work) 182 { 183 trace_writeback_queue(wb, work); 184 185 if (work->done) 186 atomic_inc(&work->done->cnt); 187 188 spin_lock_irq(&wb->work_lock); 189 190 if (test_bit(WB_registered, &wb->state)) { 191 list_add_tail(&work->list, &wb->work_list); 192 mod_delayed_work(bdi_wq, &wb->dwork, 0); 193 } else 194 finish_writeback_work(work); 195 196 spin_unlock_irq(&wb->work_lock); 197 } 198 199 static bool wb_wait_for_completion_cb(struct wb_completion *done) 200 { 201 unsigned long timeout = sysctl_hung_task_timeout_secs; 202 unsigned long waited_secs = (jiffies - done->wait_start) / HZ; 203 204 done->progress_stamp = jiffies; 205 if (timeout && (waited_secs > timeout)) 206 pr_info("INFO: The task %s:%d has been waiting for writeback " 207 "completion for more than %lu seconds.", 208 current->comm, current->pid, waited_secs); 209 210 return !atomic_read(&done->cnt); 211 } 212 213 /** 214 * wb_wait_for_completion - wait for completion of bdi_writeback_works 215 * @done: target wb_completion 216 * 217 * Wait for one or more work items issued to @bdi with their ->done field 218 * set to @done, which should have been initialized with 219 * DEFINE_WB_COMPLETION(). This function returns after all such work items 220 * are completed. Work items which are waited upon aren't freed 221 * automatically on completion. 222 */ 223 void wb_wait_for_completion(struct wb_completion *done) 224 { 225 done->wait_start = jiffies; 226 atomic_dec(&done->cnt); /* put down the initial count */ 227 wait_event(*done->waitq, wb_wait_for_completion_cb(done)); 228 } 229 230 #ifdef CONFIG_CGROUP_WRITEBACK 231 232 /* 233 * Parameters for foreign inode detection, see wbc_detach_inode() to see 234 * how they're used. 235 * 236 * These paramters are inherently heuristical as the detection target 237 * itself is fuzzy. All we want to do is detaching an inode from the 238 * current owner if it's being written to by some other cgroups too much. 239 * 240 * The current cgroup writeback is built on the assumption that multiple 241 * cgroups writing to the same inode concurrently is very rare and a mode 242 * of operation which isn't well supported. As such, the goal is not 243 * taking too long when a different cgroup takes over an inode while 244 * avoiding too aggressive flip-flops from occasional foreign writes. 245 * 246 * We record, very roughly, 2s worth of IO time history and if more than 247 * half of that is foreign, trigger the switch. The recording is quantized 248 * to 16 slots. To avoid tiny writes from swinging the decision too much, 249 * writes smaller than 1/8 of avg size are ignored. 250 */ 251 #define WB_FRN_TIME_SHIFT 13 /* 1s = 2^13, upto 8 secs w/ 16bit */ 252 #define WB_FRN_TIME_AVG_SHIFT 3 /* avg = avg * 7/8 + new * 1/8 */ 253 #define WB_FRN_TIME_CUT_DIV 8 /* ignore rounds < avg / 8 */ 254 #define WB_FRN_TIME_PERIOD (2 * (1 << WB_FRN_TIME_SHIFT)) /* 2s */ 255 256 #define WB_FRN_HIST_SLOTS 16 /* inode->i_wb_frn_history is 16bit */ 257 #define WB_FRN_HIST_UNIT (WB_FRN_TIME_PERIOD / WB_FRN_HIST_SLOTS) 258 /* each slot's duration is 2s / 16 */ 259 #define WB_FRN_HIST_THR_SLOTS (WB_FRN_HIST_SLOTS / 2) 260 /* if foreign slots >= 8, switch */ 261 #define WB_FRN_HIST_MAX_SLOTS (WB_FRN_HIST_THR_SLOTS / 2 + 1) 262 /* one round can affect upto 5 slots */ 263 #define WB_FRN_MAX_IN_FLIGHT 1024 /* don't queue too many concurrently */ 264 265 /* 266 * Maximum inodes per isw. A specific value has been chosen to make 267 * struct inode_switch_wbs_context fit into 1024 bytes kmalloc. 268 */ 269 #define WB_MAX_INODES_PER_ISW ((1024UL - sizeof(struct inode_switch_wbs_context)) \ 270 / sizeof(struct inode *)) 271 272 static atomic_t isw_nr_in_flight = ATOMIC_INIT(0); 273 static struct workqueue_struct *isw_wq; 274 275 void __inode_attach_wb(struct inode *inode, struct folio *folio) 276 { 277 struct backing_dev_info *bdi = inode_to_bdi(inode); 278 struct bdi_writeback *wb = NULL; 279 280 if (inode_cgwb_enabled(inode)) { 281 struct cgroup_subsys_state *memcg_css; 282 283 /* must pin memcg_css, see wb_get_create() */ 284 if (folio) 285 memcg_css = get_mem_cgroup_css_from_folio(folio); 286 else 287 memcg_css = task_get_css(current, memory_cgrp_id); 288 wb = wb_get_create(bdi, memcg_css, GFP_ATOMIC); 289 css_put(memcg_css); 290 } 291 292 if (!wb) 293 wb = &bdi->wb; 294 295 /* 296 * There may be multiple instances of this function racing to 297 * update the same inode. Use cmpxchg() to tell the winner. 298 */ 299 if (unlikely(cmpxchg(&inode->i_wb, NULL, wb))) 300 wb_put(wb); 301 } 302 303 /** 304 * inode_cgwb_move_to_attached - put the inode onto wb->b_attached list 305 * @inode: inode of interest with i_lock held 306 * @wb: target bdi_writeback 307 * 308 * Remove the inode from wb's io lists and if necessarily put onto b_attached 309 * list. Only inodes attached to cgwb's are kept on this list. 310 */ 311 static void inode_cgwb_move_to_attached(struct inode *inode, 312 struct bdi_writeback *wb) 313 { 314 assert_spin_locked(&wb->list_lock); 315 assert_spin_locked(&inode->i_lock); 316 WARN_ON_ONCE(inode_state_read(inode) & I_FREEING); 317 318 inode_state_clear(inode, I_SYNC_QUEUED); 319 if (wb != &wb->bdi->wb) 320 list_move(&inode->i_io_list, &wb->b_attached); 321 else 322 list_del_init(&inode->i_io_list); 323 wb_io_lists_depopulated(wb); 324 } 325 326 /** 327 * locked_inode_to_wb_and_lock_list - determine a locked inode's wb and lock it 328 * @inode: inode of interest with i_lock held 329 * 330 * Returns @inode's wb with its list_lock held. @inode->i_lock must be 331 * held on entry and is released on return. The returned wb is guaranteed 332 * to stay @inode's associated wb until its list_lock is released. 333 */ 334 static struct bdi_writeback * 335 locked_inode_to_wb_and_lock_list(struct inode *inode) 336 __releases(&inode->i_lock) 337 __acquires(&wb->list_lock) 338 { 339 while (true) { 340 struct bdi_writeback *wb = inode_to_wb(inode); 341 342 /* 343 * inode_to_wb() association is protected by both 344 * @inode->i_lock and @wb->list_lock but list_lock nests 345 * outside i_lock. Drop i_lock and verify that the 346 * association hasn't changed after acquiring list_lock. 347 */ 348 wb_get(wb); 349 spin_unlock(&inode->i_lock); 350 spin_lock(&wb->list_lock); 351 352 /* i_wb may have changed inbetween, can't use inode_to_wb() */ 353 if (likely(wb == inode->i_wb)) { 354 wb_put(wb); /* @inode already has ref */ 355 return wb; 356 } 357 358 spin_unlock(&wb->list_lock); 359 wb_put(wb); 360 cpu_relax(); 361 spin_lock(&inode->i_lock); 362 } 363 } 364 365 /** 366 * inode_to_wb_and_lock_list - determine an inode's wb and lock it 367 * @inode: inode of interest 368 * 369 * Same as locked_inode_to_wb_and_lock_list() but @inode->i_lock isn't held 370 * on entry. 371 */ 372 static struct bdi_writeback *inode_to_wb_and_lock_list(struct inode *inode) 373 __acquires(&wb->list_lock) 374 { 375 spin_lock(&inode->i_lock); 376 return locked_inode_to_wb_and_lock_list(inode); 377 } 378 379 struct inode_switch_wbs_context { 380 /* List of queued switching contexts for the wb */ 381 struct llist_node list; 382 383 /* 384 * Multiple inodes can be switched at once. The switching procedure 385 * consists of two parts, separated by a RCU grace period. To make 386 * sure that the second part is executed for each inode gone through 387 * the first part, all inode pointers are placed into a NULL-terminated 388 * array embedded into struct inode_switch_wbs_context. Otherwise 389 * an inode could be left in a non-consistent state. 390 */ 391 struct inode *inodes[]; 392 }; 393 394 static void bdi_down_write_wb_switch_rwsem(struct backing_dev_info *bdi) 395 { 396 down_write(&bdi->wb_switch_rwsem); 397 } 398 399 static void bdi_up_write_wb_switch_rwsem(struct backing_dev_info *bdi) 400 { 401 up_write(&bdi->wb_switch_rwsem); 402 } 403 404 static bool inode_do_switch_wbs(struct inode *inode, 405 struct bdi_writeback *old_wb, 406 struct bdi_writeback *new_wb) 407 { 408 struct address_space *mapping = inode->i_mapping; 409 XA_STATE(xas, &mapping->i_pages, 0); 410 struct folio *folio; 411 bool switched = false; 412 413 spin_lock(&inode->i_lock); 414 xa_lock_irq(&mapping->i_pages); 415 416 /* 417 * Once I_FREEING or I_WILL_FREE are visible under i_lock, the eviction 418 * path owns the inode and we shouldn't modify ->i_io_list. 419 */ 420 if (unlikely(inode_state_read(inode) & (I_FREEING | I_WILL_FREE))) 421 goto skip_switch; 422 423 trace_inode_switch_wbs(inode, old_wb, new_wb); 424 425 /* 426 * Count and transfer stats. Note that PAGECACHE_TAG_DIRTY points 427 * to possibly dirty folios while PAGECACHE_TAG_WRITEBACK points to 428 * folios actually under writeback. 429 */ 430 xas_for_each_marked(&xas, folio, ULONG_MAX, PAGECACHE_TAG_DIRTY) { 431 if (folio_test_dirty(folio)) { 432 long nr = folio_nr_pages(folio); 433 wb_stat_mod(old_wb, WB_RECLAIMABLE, -nr); 434 wb_stat_mod(new_wb, WB_RECLAIMABLE, nr); 435 if (folio_test_dropbehind(folio)) { 436 wb_stat_mod(old_wb, WB_DONTCACHE_DIRTY, -nr); 437 wb_stat_mod(new_wb, WB_DONTCACHE_DIRTY, nr); 438 } 439 } 440 } 441 442 xas_set(&xas, 0); 443 xas_for_each_marked(&xas, folio, ULONG_MAX, PAGECACHE_TAG_WRITEBACK) { 444 long nr = folio_nr_pages(folio); 445 WARN_ON_ONCE(!folio_test_writeback(folio)); 446 wb_stat_mod(old_wb, WB_WRITEBACK, -nr); 447 wb_stat_mod(new_wb, WB_WRITEBACK, nr); 448 } 449 450 if (mapping_tagged(mapping, PAGECACHE_TAG_WRITEBACK)) { 451 atomic_dec(&old_wb->writeback_inodes); 452 atomic_inc(&new_wb->writeback_inodes); 453 } 454 455 wb_get(new_wb); 456 457 /* 458 * Transfer to @new_wb's IO list if necessary. If the @inode is dirty, 459 * the specific list @inode was on is ignored and the @inode is put on 460 * ->b_dirty which is always correct including from ->b_dirty_time. 461 * If the @inode was clean, it means it was on the b_attached list, so 462 * move it onto the b_attached list of @new_wb. 463 */ 464 if (!list_empty(&inode->i_io_list)) { 465 inode->i_wb = new_wb; 466 467 if (inode_state_read(inode) & I_DIRTY_ALL) { 468 /* 469 * We need to keep b_dirty list sorted by 470 * dirtied_time_when. However properly sorting the 471 * inode in the list gets too expensive when switching 472 * many inodes. So just attach inode at the end of the 473 * dirty list and clobber the dirtied_time_when. 474 */ 475 inode->dirtied_time_when = jiffies; 476 inode_io_list_move_locked(inode, new_wb, 477 &new_wb->b_dirty); 478 } else { 479 inode_cgwb_move_to_attached(inode, new_wb); 480 } 481 } else { 482 inode->i_wb = new_wb; 483 } 484 485 /* ->i_wb_frn updates may race wbc_detach_inode() but doesn't matter */ 486 inode->i_wb_frn_winner = 0; 487 inode->i_wb_frn_avg_time = 0; 488 inode->i_wb_frn_history = 0; 489 switched = true; 490 skip_switch: 491 /* 492 * Paired with an acquire fence in unlocked_inode_to_wb_begin() and 493 * ensures that the new wb is visible if they see !I_WB_SWITCH. 494 */ 495 smp_wmb(); 496 inode_state_clear(inode, I_WB_SWITCH); 497 498 xa_unlock_irq(&mapping->i_pages); 499 spin_unlock(&inode->i_lock); 500 501 return switched; 502 } 503 504 static inline void cgroup_writeback_pin(struct super_block *sb) 505 { 506 atomic_inc(&sb->s_isw_nr_in_flight); 507 } 508 509 static inline void cgroup_writeback_unpin(struct super_block *sb) 510 { 511 if (atomic_dec_and_test(&sb->s_isw_nr_in_flight)) 512 wake_up_var(&sb->s_isw_nr_in_flight); 513 } 514 515 static inline void cgroup_writeback_drain(struct super_block *sb) 516 { 517 wait_var_event(&sb->s_isw_nr_in_flight, 518 !atomic_read(&sb->s_isw_nr_in_flight)); 519 } 520 521 static void process_inode_switch_wbs(struct bdi_writeback *new_wb, 522 struct inode_switch_wbs_context *isw) 523 { 524 struct backing_dev_info *bdi = inode_to_bdi(isw->inodes[0]); 525 struct bdi_writeback *old_wb = isw->inodes[0]->i_wb; 526 unsigned long nr_switched = 0; 527 struct inode **inodep; 528 529 /* 530 * If @inode switches cgwb membership while sync_inodes_sb() is 531 * being issued, sync_inodes_sb() might miss it. Synchronize. 532 */ 533 down_read(&bdi->wb_switch_rwsem); 534 535 inodep = isw->inodes; 536 /* 537 * By the time control reaches here, RCU grace period has passed 538 * since I_WB_SWITCH assertion and all wb stat update transactions 539 * between unlocked_inode_to_wb_begin/end() are guaranteed to be 540 * synchronizing against the i_pages lock. 541 * 542 * Grabbing old_wb->list_lock, inode->i_lock and the i_pages lock 543 * gives us exclusion against all wb related operations on @inode 544 * including IO list manipulations and stat updates. 545 */ 546 relock: 547 if (old_wb < new_wb) { 548 spin_lock(&old_wb->list_lock); 549 spin_lock_nested(&new_wb->list_lock, SINGLE_DEPTH_NESTING); 550 } else { 551 spin_lock(&new_wb->list_lock); 552 spin_lock_nested(&old_wb->list_lock, SINGLE_DEPTH_NESTING); 553 } 554 555 while (*inodep) { 556 WARN_ON_ONCE((*inodep)->i_wb != old_wb); 557 if (inode_do_switch_wbs(*inodep, old_wb, new_wb)) 558 nr_switched++; 559 inodep++; 560 if (*inodep && need_resched()) { 561 spin_unlock(&new_wb->list_lock); 562 spin_unlock(&old_wb->list_lock); 563 cond_resched(); 564 goto relock; 565 } 566 } 567 568 spin_unlock(&new_wb->list_lock); 569 spin_unlock(&old_wb->list_lock); 570 571 up_read(&bdi->wb_switch_rwsem); 572 573 if (nr_switched) { 574 wb_wakeup(new_wb); 575 wb_put_many(old_wb, nr_switched); 576 } 577 578 for (inodep = isw->inodes; *inodep; inodep++) { 579 struct super_block *sb = (*inodep)->i_sb; 580 581 iput(*inodep); 582 cgroup_writeback_unpin(sb); 583 } 584 wb_put(new_wb); 585 kfree(isw); 586 atomic_dec(&isw_nr_in_flight); 587 } 588 589 void inode_switch_wbs_work_fn(struct work_struct *work) 590 { 591 struct bdi_writeback *new_wb = container_of(work, struct bdi_writeback, 592 switch_work); 593 struct inode_switch_wbs_context *isw, *next_isw; 594 struct llist_node *list; 595 596 list = llist_del_all(&new_wb->switch_wbs_ctxs); 597 /* 598 * Nothing to do? That would be a problem as references held by isw 599 * items protect wb from freeing... 600 */ 601 if (WARN_ON_ONCE(!list)) 602 return; 603 604 /* 605 * Grab our reference to wb so that it cannot get freed under us 606 * after we process all the isw items. 607 */ 608 wb_get(new_wb); 609 /* 610 * In addition to synchronizing among switchers, I_WB_SWITCH 611 * tells the RCU protected stat update paths to grab the i_page 612 * lock so that stat transfer can synchronize against them. 613 * Let's continue after I_WB_SWITCH is guaranteed to be 614 * visible. 615 */ 616 synchronize_rcu(); 617 618 llist_for_each_entry_safe(isw, next_isw, list, list) 619 process_inode_switch_wbs(new_wb, isw); 620 wb_put(new_wb); 621 } 622 623 static bool inode_prepare_wbs_switch(struct inode *inode, 624 struct bdi_writeback *new_wb) 625 { 626 /* Avoid the atomic_inc/smp_mb dance once SB_ACTIVE is gone. */ 627 if (!(inode->i_sb->s_flags & SB_ACTIVE)) 628 return false; 629 630 /* 631 * Pairs with smp_mb() in cgroup_writeback_umount(): the umounter either 632 * sees a non-zero counter and waits, or we see SB_ACTIVE clear below. 633 */ 634 cgroup_writeback_pin(inode->i_sb); 635 smp_mb(); 636 637 if (IS_DAX(inode)) 638 goto out_unpin; 639 640 /* while holding I_WB_SWITCH, no one else can update the association */ 641 spin_lock(&inode->i_lock); 642 if (!(inode->i_sb->s_flags & SB_ACTIVE) || 643 inode_state_read(inode) & (I_WB_SWITCH | I_FREEING | I_WILL_FREE) || 644 inode_to_wb(inode) == new_wb) { 645 spin_unlock(&inode->i_lock); 646 goto out_unpin; 647 } 648 inode_state_set(inode, I_WB_SWITCH); 649 __iget(inode); 650 spin_unlock(&inode->i_lock); 651 652 return true; 653 654 out_unpin: 655 cgroup_writeback_unpin(inode->i_sb); 656 return false; 657 } 658 659 static void wb_queue_isw(struct bdi_writeback *wb, 660 struct inode_switch_wbs_context *isw) 661 { 662 if (llist_add(&isw->list, &wb->switch_wbs_ctxs)) 663 queue_work(isw_wq, &wb->switch_work); 664 } 665 666 /** 667 * inode_switch_wbs - change the wb association of an inode 668 * @inode: target inode 669 * @new_wb_id: ID of the new wb 670 * 671 * Switch @inode's wb association to the wb identified by @new_wb_id. The 672 * switching is performed asynchronously and may fail silently. 673 */ 674 static void inode_switch_wbs(struct inode *inode, int new_wb_id) 675 { 676 struct backing_dev_info *bdi = inode_to_bdi(inode); 677 struct cgroup_subsys_state *memcg_css; 678 struct inode_switch_wbs_context *isw; 679 struct bdi_writeback *new_wb = NULL; 680 681 /* noop if seems to be already in progress */ 682 if (inode_state_read_once(inode) & I_WB_SWITCH) 683 return; 684 685 /* avoid queueing a new switch if too many are already in flight */ 686 if (atomic_read(&isw_nr_in_flight) > WB_FRN_MAX_IN_FLIGHT) 687 return; 688 689 isw = kzalloc_flex(*isw, inodes, 2, GFP_ATOMIC); 690 if (!isw) 691 return; 692 693 atomic_inc(&isw_nr_in_flight); 694 695 /* find and pin the new wb */ 696 rcu_read_lock(); 697 memcg_css = css_from_id(new_wb_id, &memory_cgrp_subsys); 698 if (memcg_css && !css_tryget(memcg_css)) 699 memcg_css = NULL; 700 rcu_read_unlock(); 701 if (!memcg_css) 702 goto out_free; 703 704 new_wb = wb_get_create(bdi, memcg_css, GFP_ATOMIC); 705 css_put(memcg_css); 706 if (!new_wb) 707 goto out_free; 708 709 if (!inode_prepare_wbs_switch(inode, new_wb)) 710 goto out_free; 711 712 isw->inodes[0] = inode; 713 714 trace_inode_switch_wbs_queue(inode->i_wb, new_wb, 1); 715 wb_queue_isw(new_wb, isw); 716 return; 717 718 out_free: 719 atomic_dec(&isw_nr_in_flight); 720 if (new_wb) 721 wb_put(new_wb); 722 kfree(isw); 723 } 724 725 static bool isw_prepare_wbs_switch(struct bdi_writeback *new_wb, 726 struct inode_switch_wbs_context *isw, 727 struct list_head *list, int *nr) 728 { 729 struct inode *inode; 730 731 list_for_each_entry(inode, list, i_io_list) { 732 if (!inode_prepare_wbs_switch(inode, new_wb)) 733 continue; 734 735 isw->inodes[*nr] = inode; 736 (*nr)++; 737 738 if (*nr >= WB_MAX_INODES_PER_ISW - 1) 739 return true; 740 } 741 return false; 742 } 743 744 /** 745 * cleanup_offline_cgwb - detach associated inodes 746 * @wb: target wb 747 * 748 * Switch all inodes attached to @wb to a nearest living ancestor's wb in order 749 * to eventually release the dying @wb. Returns %true if not all inodes were 750 * switched and the function has to be restarted. 751 */ 752 bool cleanup_offline_cgwb(struct bdi_writeback *wb) 753 { 754 struct cgroup_subsys_state *memcg_css; 755 struct inode_switch_wbs_context *isw; 756 struct bdi_writeback *new_wb; 757 int nr; 758 bool restart = false; 759 760 isw = kzalloc_flex(*isw, inodes, WB_MAX_INODES_PER_ISW); 761 if (!isw) 762 return restart; 763 764 atomic_inc(&isw_nr_in_flight); 765 766 for (memcg_css = wb->memcg_css->parent; memcg_css; 767 memcg_css = memcg_css->parent) { 768 new_wb = wb_get_create(wb->bdi, memcg_css, GFP_KERNEL); 769 if (new_wb) 770 break; 771 } 772 if (unlikely(!new_wb)) 773 new_wb = &wb->bdi->wb; /* wb_get() is noop for bdi's wb */ 774 775 nr = 0; 776 spin_lock(&wb->list_lock); 777 /* 778 * In addition to the inodes that have completed writeback, also switch 779 * cgwbs for those inodes only with dirty timestamps. Otherwise, those 780 * inodes won't be written back for a long time when lazytime is 781 * enabled, and thus pinning the dying cgwbs. It won't break the 782 * bandwidth restrictions, as writeback of inode metadata is not 783 * accounted for. 784 */ 785 restart = isw_prepare_wbs_switch(new_wb, isw, &wb->b_attached, &nr); 786 if (!restart) 787 restart = isw_prepare_wbs_switch(new_wb, isw, &wb->b_dirty_time, 788 &nr); 789 spin_unlock(&wb->list_lock); 790 791 /* no attached inodes? bail out */ 792 if (nr == 0) { 793 atomic_dec(&isw_nr_in_flight); 794 wb_put(new_wb); 795 kfree(isw); 796 return restart; 797 } 798 799 trace_inode_switch_wbs_queue(wb, new_wb, nr); 800 wb_queue_isw(new_wb, isw); 801 802 return restart; 803 } 804 805 /** 806 * wbc_attach_and_unlock_inode - associate wbc with target inode and unlock it 807 * @wbc: writeback_control of interest 808 * @inode: target inode 809 * 810 * @inode is locked and about to be written back under the control of @wbc. 811 * Record @inode's writeback context into @wbc and unlock the i_lock. On 812 * writeback completion, wbc_detach_inode() should be called. This is used 813 * to track the cgroup writeback context. 814 */ 815 static void wbc_attach_and_unlock_inode(struct writeback_control *wbc, 816 struct inode *inode) 817 __releases(&inode->i_lock) 818 { 819 if (!inode_cgwb_enabled(inode)) { 820 spin_unlock(&inode->i_lock); 821 return; 822 } 823 824 wbc->wb = inode_to_wb(inode); 825 wbc->inode = inode; 826 827 wbc->wb_id = wbc->wb->memcg_css->id; 828 wbc->wb_lcand_id = inode->i_wb_frn_winner; 829 wbc->wb_tcand_id = 0; 830 wbc->wb_bytes = 0; 831 wbc->wb_lcand_bytes = 0; 832 wbc->wb_tcand_bytes = 0; 833 834 wb_get(wbc->wb); 835 spin_unlock(&inode->i_lock); 836 837 /* 838 * A dying wb indicates that either the blkcg associated with the 839 * memcg changed or the associated memcg is dying. In the first 840 * case, a replacement wb should already be available and we should 841 * refresh the wb immediately. In the second case, trying to 842 * refresh will keep failing. 843 */ 844 if (unlikely(wb_dying(wbc->wb) && !css_is_dying(wbc->wb->memcg_css))) 845 inode_switch_wbs(inode, wbc->wb_id); 846 } 847 848 /** 849 * wbc_attach_fdatawrite_inode - associate wbc and inode for fdatawrite 850 * @wbc: writeback_control of interest 851 * @inode: target inode 852 * 853 * This function is to be used by filemap_writeback(), which is an alternative 854 * entry point into writeback code, and first ensures @inode is associated with 855 * a bdi_writeback and attaches it to @wbc. 856 */ 857 void wbc_attach_fdatawrite_inode(struct writeback_control *wbc, 858 struct inode *inode) 859 { 860 spin_lock(&inode->i_lock); 861 inode_attach_wb(inode, NULL); 862 wbc_attach_and_unlock_inode(wbc, inode); 863 } 864 EXPORT_SYMBOL_GPL(wbc_attach_fdatawrite_inode); 865 866 /** 867 * wbc_detach_inode - disassociate wbc from inode and perform foreign detection 868 * @wbc: writeback_control of the just finished writeback 869 * 870 * To be called after a writeback attempt of an inode finishes and undoes 871 * wbc_attach_and_unlock_inode(). Can be called under any context. 872 * 873 * As concurrent write sharing of an inode is expected to be very rare and 874 * memcg only tracks page ownership on first-use basis severely confining 875 * the usefulness of such sharing, cgroup writeback tracks ownership 876 * per-inode. While the support for concurrent write sharing of an inode 877 * is deemed unnecessary, an inode being written to by different cgroups at 878 * different points in time is a lot more common, and, more importantly, 879 * charging only by first-use can too readily lead to grossly incorrect 880 * behaviors (single foreign page can lead to gigabytes of writeback to be 881 * incorrectly attributed). 882 * 883 * To resolve this issue, cgroup writeback detects the majority dirtier of 884 * an inode and transfers the ownership to it. To avoid unnecessary 885 * oscillation, the detection mechanism keeps track of history and gives 886 * out the switch verdict only if the foreign usage pattern is stable over 887 * a certain amount of time and/or writeback attempts. 888 * 889 * On each writeback attempt, @wbc tries to detect the majority writer 890 * using Boyer-Moore majority vote algorithm. In addition to the byte 891 * count from the majority voting, it also counts the bytes written for the 892 * current wb and the last round's winner wb (max of last round's current 893 * wb, the winner from two rounds ago, and the last round's majority 894 * candidate). Keeping track of the historical winner helps the algorithm 895 * to semi-reliably detect the most active writer even when it's not the 896 * absolute majority. 897 * 898 * Once the winner of the round is determined, whether the winner is 899 * foreign or not and how much IO time the round consumed is recorded in 900 * inode->i_wb_frn_history. If the amount of recorded foreign IO time is 901 * over a certain threshold, the switch verdict is given. 902 */ 903 void wbc_detach_inode(struct writeback_control *wbc) 904 { 905 struct bdi_writeback *wb = wbc->wb; 906 struct inode *inode = wbc->inode; 907 unsigned long avg_time, max_bytes, max_time; 908 u16 history; 909 int max_id; 910 911 if (!wb) 912 return; 913 914 history = inode->i_wb_frn_history; 915 avg_time = inode->i_wb_frn_avg_time; 916 917 /* pick the winner of this round */ 918 if (wbc->wb_bytes >= wbc->wb_lcand_bytes && 919 wbc->wb_bytes >= wbc->wb_tcand_bytes) { 920 max_id = wbc->wb_id; 921 max_bytes = wbc->wb_bytes; 922 } else if (wbc->wb_lcand_bytes >= wbc->wb_tcand_bytes) { 923 max_id = wbc->wb_lcand_id; 924 max_bytes = wbc->wb_lcand_bytes; 925 } else { 926 max_id = wbc->wb_tcand_id; 927 max_bytes = wbc->wb_tcand_bytes; 928 } 929 930 /* 931 * Calculate the amount of IO time the winner consumed and fold it 932 * into the running average kept per inode. If the consumed IO 933 * time is lower than avag / WB_FRN_TIME_CUT_DIV, ignore it for 934 * deciding whether to switch or not. This is to prevent one-off 935 * small dirtiers from skewing the verdict. 936 */ 937 max_time = DIV_ROUND_UP((max_bytes >> PAGE_SHIFT) << WB_FRN_TIME_SHIFT, 938 wb->avg_write_bandwidth); 939 if (avg_time) 940 avg_time += (max_time >> WB_FRN_TIME_AVG_SHIFT) - 941 (avg_time >> WB_FRN_TIME_AVG_SHIFT); 942 else 943 avg_time = max_time; /* immediate catch up on first run */ 944 945 if (max_time >= avg_time / WB_FRN_TIME_CUT_DIV) { 946 int slots; 947 948 /* 949 * The switch verdict is reached if foreign wb's consume 950 * more than a certain proportion of IO time in a 951 * WB_FRN_TIME_PERIOD. This is loosely tracked by 16 slot 952 * history mask where each bit represents one sixteenth of 953 * the period. Determine the number of slots to shift into 954 * history from @max_time. 955 */ 956 slots = min(DIV_ROUND_UP(max_time, WB_FRN_HIST_UNIT), 957 (unsigned long)WB_FRN_HIST_MAX_SLOTS); 958 history <<= slots; 959 if (wbc->wb_id != max_id) 960 history |= (1U << slots) - 1; 961 962 if (history) 963 trace_inode_foreign_history(inode, wbc, history); 964 965 /* 966 * Switch if the current wb isn't the consistent winner. 967 * If there are multiple closely competing dirtiers, the 968 * inode may switch across them repeatedly over time, which 969 * is okay. The main goal is avoiding keeping an inode on 970 * the wrong wb for an extended period of time. 971 */ 972 if (hweight16(history) > WB_FRN_HIST_THR_SLOTS) 973 inode_switch_wbs(inode, max_id); 974 } 975 976 /* 977 * Multiple instances of this function may race to update the 978 * following fields but we don't mind occassional inaccuracies. 979 */ 980 inode->i_wb_frn_winner = max_id; 981 inode->i_wb_frn_avg_time = min(avg_time, (unsigned long)U16_MAX); 982 inode->i_wb_frn_history = history; 983 984 wb_put(wbc->wb); 985 wbc->wb = NULL; 986 } 987 EXPORT_SYMBOL_GPL(wbc_detach_inode); 988 989 /** 990 * wbc_account_cgroup_owner - account writeback to update inode cgroup ownership 991 * @wbc: writeback_control of the writeback in progress 992 * @folio: folio being written out 993 * @bytes: number of bytes being written out 994 * 995 * @bytes from @folio are about to written out during the writeback 996 * controlled by @wbc. Keep the book for foreign inode detection. See 997 * wbc_detach_inode(). 998 */ 999 void wbc_account_cgroup_owner(struct writeback_control *wbc, struct folio *folio, 1000 size_t bytes) 1001 { 1002 struct cgroup_subsys_state *css; 1003 int id; 1004 1005 /* 1006 * pageout() path doesn't attach @wbc to the inode being written 1007 * out. This is intentional as we don't want the function to block 1008 * behind a slow cgroup. Ultimately, we want pageout() to kick off 1009 * regular writeback instead of writing things out itself. 1010 */ 1011 if (!wbc->wb || wbc->no_cgroup_owner) 1012 return; 1013 1014 css = get_mem_cgroup_css_from_folio(folio); 1015 /* dead cgroups shouldn't contribute to inode ownership arbitration */ 1016 if (!css_is_online(css)) 1017 goto out; 1018 1019 id = css->id; 1020 1021 if (id == wbc->wb_id) { 1022 wbc->wb_bytes += bytes; 1023 goto out; 1024 } 1025 1026 if (id == wbc->wb_lcand_id) 1027 wbc->wb_lcand_bytes += bytes; 1028 1029 /* Boyer-Moore majority vote algorithm */ 1030 if (!wbc->wb_tcand_bytes) 1031 wbc->wb_tcand_id = id; 1032 if (id == wbc->wb_tcand_id) 1033 wbc->wb_tcand_bytes += bytes; 1034 else 1035 wbc->wb_tcand_bytes -= min(bytes, wbc->wb_tcand_bytes); 1036 out: 1037 css_put(css); 1038 } 1039 EXPORT_SYMBOL_GPL(wbc_account_cgroup_owner); 1040 1041 /** 1042 * wb_split_bdi_pages - split nr_pages to write according to bandwidth 1043 * @wb: target bdi_writeback to split @nr_pages to 1044 * @nr_pages: number of pages to write for the whole bdi 1045 * 1046 * Split @wb's portion of @nr_pages according to @wb's write bandwidth in 1047 * relation to the total write bandwidth of all wb's w/ dirty inodes on 1048 * @wb->bdi. 1049 */ 1050 static long wb_split_bdi_pages(struct bdi_writeback *wb, long nr_pages) 1051 { 1052 unsigned long this_bw = wb->avg_write_bandwidth; 1053 unsigned long tot_bw = atomic_long_read(&wb->bdi->tot_write_bandwidth); 1054 1055 if (nr_pages == LONG_MAX) 1056 return LONG_MAX; 1057 1058 /* 1059 * This may be called on clean wb's and proportional distribution 1060 * may not make sense, just use the original @nr_pages in those 1061 * cases. In general, we wanna err on the side of writing more. 1062 */ 1063 if (!tot_bw || this_bw >= tot_bw) 1064 return nr_pages; 1065 else 1066 return DIV_ROUND_UP_ULL((u64)nr_pages * this_bw, tot_bw); 1067 } 1068 1069 /** 1070 * bdi_split_work_to_wbs - split a wb_writeback_work to all wb's of a bdi 1071 * @bdi: target backing_dev_info 1072 * @base_work: wb_writeback_work to issue 1073 * @skip_if_busy: skip wb's which already have writeback in progress 1074 * 1075 * Split and issue @base_work to all wb's (bdi_writeback's) of @bdi which 1076 * have dirty inodes. If @base_work->nr_page isn't %LONG_MAX, it's 1077 * distributed to the busy wbs according to each wb's proportion in the 1078 * total active write bandwidth of @bdi. 1079 */ 1080 static void bdi_split_work_to_wbs(struct backing_dev_info *bdi, 1081 struct wb_writeback_work *base_work, 1082 bool skip_if_busy) 1083 { 1084 struct bdi_writeback *last_wb = NULL; 1085 struct bdi_writeback *wb = list_entry(&bdi->wb_list, 1086 struct bdi_writeback, bdi_node); 1087 1088 might_sleep(); 1089 restart: 1090 rcu_read_lock(); 1091 list_for_each_entry_continue_rcu(wb, &bdi->wb_list, bdi_node) { 1092 DEFINE_WB_COMPLETION(fallback_work_done, bdi); 1093 struct wb_writeback_work fallback_work; 1094 struct wb_writeback_work *work; 1095 long nr_pages; 1096 1097 if (last_wb) { 1098 wb_put(last_wb); 1099 last_wb = NULL; 1100 } 1101 1102 /* SYNC_ALL writes out I_DIRTY_TIME too */ 1103 if (!wb_has_dirty_io(wb) && 1104 (base_work->sync_mode == WB_SYNC_NONE || 1105 list_empty(&wb->b_dirty_time))) 1106 continue; 1107 if (skip_if_busy && writeback_in_progress(wb)) 1108 continue; 1109 1110 nr_pages = wb_split_bdi_pages(wb, base_work->nr_pages); 1111 1112 work = kmalloc_obj(*work, GFP_ATOMIC); 1113 if (work) { 1114 *work = *base_work; 1115 work->nr_pages = nr_pages; 1116 work->auto_free = 1; 1117 wb_queue_work(wb, work); 1118 continue; 1119 } 1120 1121 /* 1122 * If wb_tryget fails, the wb has been shutdown, skip it. 1123 * 1124 * Pin @wb so that it stays on @bdi->wb_list. This allows 1125 * continuing iteration from @wb after dropping and 1126 * regrabbing rcu read lock. 1127 */ 1128 if (!wb_tryget(wb)) 1129 continue; 1130 1131 /* alloc failed, execute synchronously using on-stack fallback */ 1132 work = &fallback_work; 1133 *work = *base_work; 1134 work->nr_pages = nr_pages; 1135 work->auto_free = 0; 1136 work->done = &fallback_work_done; 1137 1138 wb_queue_work(wb, work); 1139 last_wb = wb; 1140 1141 rcu_read_unlock(); 1142 wb_wait_for_completion(&fallback_work_done); 1143 goto restart; 1144 } 1145 rcu_read_unlock(); 1146 1147 if (last_wb) 1148 wb_put(last_wb); 1149 } 1150 1151 /** 1152 * cgroup_writeback_by_id - initiate cgroup writeback from bdi and memcg IDs 1153 * @bdi_id: target bdi id 1154 * @memcg_id: target memcg css id 1155 * @reason: reason why some writeback work initiated 1156 * @done: target wb_completion 1157 * 1158 * Initiate flush of the bdi_writeback identified by @bdi_id and @memcg_id 1159 * with the specified parameters. 1160 */ 1161 int cgroup_writeback_by_id(u64 bdi_id, int memcg_id, 1162 enum wb_reason reason, struct wb_completion *done) 1163 { 1164 struct backing_dev_info *bdi; 1165 struct cgroup_subsys_state *memcg_css; 1166 struct bdi_writeback *wb; 1167 struct wb_writeback_work *work; 1168 unsigned long dirty; 1169 int ret; 1170 1171 /* lookup bdi and memcg */ 1172 bdi = bdi_get_by_id(bdi_id); 1173 if (!bdi) 1174 return -ENOENT; 1175 1176 rcu_read_lock(); 1177 memcg_css = css_from_id(memcg_id, &memory_cgrp_subsys); 1178 if (memcg_css && !css_tryget(memcg_css)) 1179 memcg_css = NULL; 1180 rcu_read_unlock(); 1181 if (!memcg_css) { 1182 ret = -ENOENT; 1183 goto out_bdi_put; 1184 } 1185 1186 /* 1187 * And find the associated wb. If the wb isn't there already 1188 * there's nothing to flush, don't create one. 1189 */ 1190 wb = wb_get_lookup(bdi, memcg_css); 1191 if (!wb) { 1192 ret = -ENOENT; 1193 goto out_css_put; 1194 } 1195 1196 /* 1197 * The caller is attempting to write out most of 1198 * the currently dirty pages. Let's take the current dirty page 1199 * count and inflate it by 25% which should be large enough to 1200 * flush out most dirty pages while avoiding getting livelocked by 1201 * concurrent dirtiers. 1202 * 1203 * BTW the memcg stats are flushed periodically and this is best-effort 1204 * estimation, so some potential error is ok. 1205 */ 1206 dirty = memcg_page_state(mem_cgroup_from_css(memcg_css), NR_FILE_DIRTY); 1207 dirty = dirty * 10 / 8; 1208 1209 /* issue the writeback work */ 1210 work = kzalloc_obj(*work, GFP_NOWAIT); 1211 if (work) { 1212 work->nr_pages = dirty; 1213 work->sync_mode = WB_SYNC_NONE; 1214 work->range_cyclic = 1; 1215 work->reason = reason; 1216 work->done = done; 1217 work->auto_free = 1; 1218 wb_queue_work(wb, work); 1219 ret = 0; 1220 } else { 1221 ret = -ENOMEM; 1222 } 1223 1224 wb_put(wb); 1225 out_css_put: 1226 css_put(memcg_css); 1227 out_bdi_put: 1228 bdi_put(bdi); 1229 return ret; 1230 } 1231 1232 /** 1233 * cgroup_writeback_umount - wait for in-flight inode wb switches on @sb 1234 * @sb: target super_block 1235 * 1236 * Wait until every inode wb switch that already passed the SB_ACTIVE 1237 * check on this superblock has been completed by the worker. Since 1238 * SB_ACTIVE is cleared before this is called, no new switches can start 1239 * for @sb, so s_isw_nr_in_flight will monotonically drop to zero. 1240 */ 1241 void cgroup_writeback_umount(struct super_block *sb) 1242 { 1243 if (!(sb->s_bdi->capabilities & BDI_CAP_WRITEBACK)) 1244 return; 1245 1246 /* 1247 * Pairs with smp_mb() in inode_prepare_wbs_switch(): we either observe 1248 * a non-zero counter and wait, or the switcher sees SB_ACTIVE clear 1249 * (cleared by generic_shutdown_super()) and bails before grabbing the 1250 * inode. 1251 */ 1252 smp_mb(); 1253 cgroup_writeback_drain(sb); 1254 } 1255 1256 static int __init cgroup_writeback_init(void) 1257 { 1258 isw_wq = alloc_workqueue("inode_switch_wbs", WQ_PERCPU, 0); 1259 if (!isw_wq) 1260 return -ENOMEM; 1261 return 0; 1262 } 1263 fs_initcall(cgroup_writeback_init); 1264 1265 #else /* CONFIG_CGROUP_WRITEBACK */ 1266 1267 static void bdi_down_write_wb_switch_rwsem(struct backing_dev_info *bdi) { } 1268 static void bdi_up_write_wb_switch_rwsem(struct backing_dev_info *bdi) { } 1269 1270 static void inode_cgwb_move_to_attached(struct inode *inode, 1271 struct bdi_writeback *wb) 1272 { 1273 assert_spin_locked(&wb->list_lock); 1274 assert_spin_locked(&inode->i_lock); 1275 WARN_ON_ONCE(inode_state_read(inode) & I_FREEING); 1276 1277 inode_state_clear(inode, I_SYNC_QUEUED); 1278 list_del_init(&inode->i_io_list); 1279 wb_io_lists_depopulated(wb); 1280 } 1281 1282 static struct bdi_writeback * 1283 locked_inode_to_wb_and_lock_list(struct inode *inode) 1284 __releases(&inode->i_lock) 1285 __acquires(&wb->list_lock) 1286 { 1287 struct bdi_writeback *wb = inode_to_wb(inode); 1288 1289 spin_unlock(&inode->i_lock); 1290 spin_lock(&wb->list_lock); 1291 return wb; 1292 } 1293 1294 static struct bdi_writeback *inode_to_wb_and_lock_list(struct inode *inode) 1295 __acquires(&wb->list_lock) 1296 { 1297 struct bdi_writeback *wb = inode_to_wb(inode); 1298 1299 spin_lock(&wb->list_lock); 1300 return wb; 1301 } 1302 1303 static long wb_split_bdi_pages(struct bdi_writeback *wb, long nr_pages) 1304 { 1305 return nr_pages; 1306 } 1307 1308 static void bdi_split_work_to_wbs(struct backing_dev_info *bdi, 1309 struct wb_writeback_work *base_work, 1310 bool skip_if_busy) 1311 { 1312 might_sleep(); 1313 1314 if (!skip_if_busy || !writeback_in_progress(&bdi->wb)) { 1315 base_work->auto_free = 0; 1316 wb_queue_work(&bdi->wb, base_work); 1317 } 1318 } 1319 1320 static inline void wbc_attach_and_unlock_inode(struct writeback_control *wbc, 1321 struct inode *inode) 1322 __releases(&inode->i_lock) 1323 { 1324 spin_unlock(&inode->i_lock); 1325 } 1326 1327 #endif /* CONFIG_CGROUP_WRITEBACK */ 1328 1329 /* 1330 * Add in the number of potentially dirty inodes, because each inode 1331 * write can dirty pagecache in the underlying blockdev. 1332 */ 1333 static unsigned long get_nr_dirty_pages(void) 1334 { 1335 return global_node_page_state(NR_FILE_DIRTY) + 1336 get_nr_dirty_inodes(); 1337 } 1338 1339 static void wb_start_writeback(struct bdi_writeback *wb, enum wb_reason reason) 1340 { 1341 if (!wb_has_dirty_io(wb)) 1342 return; 1343 1344 /* 1345 * All callers of this function want to start writeback of all 1346 * dirty pages. Places like vmscan can call this at a very 1347 * high frequency, causing pointless allocations of tons of 1348 * work items and keeping the flusher threads busy retrieving 1349 * that work. Ensure that we only allow one of them pending and 1350 * inflight at the time. 1351 */ 1352 if (test_bit(WB_start_all, &wb->state) || 1353 test_and_set_bit(WB_start_all, &wb->state)) 1354 return; 1355 1356 wb->start_all_reason = reason; 1357 wb_wakeup(wb); 1358 } 1359 1360 /** 1361 * wb_start_background_writeback - start background writeback 1362 * @wb: bdi_writback to write from 1363 * 1364 * Description: 1365 * This makes sure WB_SYNC_NONE background writeback happens. When 1366 * this function returns, it is only guaranteed that for given wb 1367 * some IO is happening if we are over background dirty threshold. 1368 * Caller need not hold sb s_umount semaphore. 1369 */ 1370 void wb_start_background_writeback(struct bdi_writeback *wb) 1371 { 1372 /* 1373 * We just wake up the flusher thread. It will perform background 1374 * writeback as soon as there is no other work to do. 1375 */ 1376 trace_writeback_wake_background(wb); 1377 wb_wakeup(wb); 1378 } 1379 1380 /* 1381 * Remove the inode from the writeback list it is on. 1382 */ 1383 void inode_io_list_del(struct inode *inode) 1384 { 1385 struct bdi_writeback *wb; 1386 1387 /* 1388 * FIXME: ext4 can call here from ext4_evict_inode() after evict() already 1389 * unlinked the inode. 1390 */ 1391 if (list_empty_careful(&inode->i_io_list)) 1392 return; 1393 1394 wb = inode_to_wb_and_lock_list(inode); 1395 spin_lock(&inode->i_lock); 1396 1397 inode_state_clear(inode, I_SYNC_QUEUED); 1398 list_del_init(&inode->i_io_list); 1399 wb_io_lists_depopulated(wb); 1400 1401 spin_unlock(&inode->i_lock); 1402 spin_unlock(&wb->list_lock); 1403 } 1404 EXPORT_SYMBOL(inode_io_list_del); 1405 1406 /* 1407 * mark an inode as under writeback on the sb 1408 */ 1409 void sb_mark_inode_writeback(struct inode *inode) 1410 { 1411 struct super_block *sb = inode->i_sb; 1412 unsigned long flags; 1413 1414 if (list_empty(&inode->i_wb_list)) { 1415 spin_lock_irqsave(&sb->s_inode_wblist_lock, flags); 1416 if (list_empty(&inode->i_wb_list)) { 1417 list_add_tail(&inode->i_wb_list, &sb->s_inodes_wb); 1418 trace_sb_mark_inode_writeback(inode); 1419 } 1420 spin_unlock_irqrestore(&sb->s_inode_wblist_lock, flags); 1421 } 1422 } 1423 1424 /* 1425 * clear an inode as under writeback on the sb 1426 */ 1427 void sb_clear_inode_writeback(struct inode *inode) 1428 { 1429 struct super_block *sb = inode->i_sb; 1430 unsigned long flags; 1431 1432 if (!list_empty(&inode->i_wb_list)) { 1433 spin_lock_irqsave(&sb->s_inode_wblist_lock, flags); 1434 if (!list_empty(&inode->i_wb_list)) { 1435 list_del_init(&inode->i_wb_list); 1436 trace_sb_clear_inode_writeback(inode); 1437 } 1438 spin_unlock_irqrestore(&sb->s_inode_wblist_lock, flags); 1439 } 1440 } 1441 1442 /* 1443 * Redirty an inode: set its when-it-was dirtied timestamp and move it to the 1444 * furthest end of its superblock's dirty-inode list. 1445 * 1446 * Before stamping the inode's ->dirtied_when, we check to see whether it is 1447 * already the most-recently-dirtied inode on the b_dirty list. If that is 1448 * the case then the inode must have been redirtied while it was being written 1449 * out and we don't reset its dirtied_when. 1450 */ 1451 static void redirty_tail_locked(struct inode *inode, struct bdi_writeback *wb) 1452 { 1453 assert_spin_locked(&inode->i_lock); 1454 1455 inode_state_clear(inode, I_SYNC_QUEUED); 1456 /* 1457 * When the inode is being freed just don't bother with dirty list 1458 * tracking. Flush worker will ignore this inode anyway and it will 1459 * trigger assertions in inode_io_list_move_locked(). 1460 */ 1461 if (inode_state_read(inode) & I_FREEING) { 1462 list_del_init(&inode->i_io_list); 1463 wb_io_lists_depopulated(wb); 1464 return; 1465 } 1466 if (!list_empty(&wb->b_dirty)) { 1467 struct inode *tail; 1468 1469 tail = wb_inode(wb->b_dirty.next); 1470 if (time_before(inode->dirtied_when, tail->dirtied_when)) 1471 inode->dirtied_when = jiffies; 1472 } 1473 inode_io_list_move_locked(inode, wb, &wb->b_dirty); 1474 } 1475 1476 static void redirty_tail(struct inode *inode, struct bdi_writeback *wb) 1477 { 1478 spin_lock(&inode->i_lock); 1479 redirty_tail_locked(inode, wb); 1480 spin_unlock(&inode->i_lock); 1481 } 1482 1483 /* 1484 * requeue inode for re-scanning after bdi->b_io list is exhausted. 1485 */ 1486 static void requeue_io(struct inode *inode, struct bdi_writeback *wb) 1487 { 1488 inode_io_list_move_locked(inode, wb, &wb->b_more_io); 1489 } 1490 1491 static void inode_sync_complete(struct inode *inode) 1492 { 1493 assert_spin_locked(&inode->i_lock); 1494 1495 inode_state_clear(inode, I_SYNC); 1496 /* If inode is clean an unused, put it into LRU now... */ 1497 inode_lru_list_add(inode); 1498 /* Called with inode->i_lock which ensures memory ordering. */ 1499 inode_wake_up_bit(inode, __I_SYNC); 1500 } 1501 1502 static bool inode_dirtied_after(struct inode *inode, unsigned long t) 1503 { 1504 bool ret = time_after(inode->dirtied_when, t); 1505 #ifndef CONFIG_64BIT 1506 /* 1507 * For inodes being constantly redirtied, dirtied_when can get stuck. 1508 * It _appears_ to be in the future, but is actually in distant past. 1509 * This test is necessary to prevent such wrapped-around relative times 1510 * from permanently stopping the whole bdi writeback. 1511 */ 1512 ret = ret && time_before_eq(inode->dirtied_when, jiffies); 1513 #endif 1514 return ret; 1515 } 1516 1517 /* 1518 * Move expired (dirtied before dirtied_before) dirty inodes from 1519 * @delaying_queue to @dispatch_queue. 1520 */ 1521 static int move_expired_inodes(struct list_head *delaying_queue, 1522 struct list_head *dispatch_queue, 1523 unsigned long dirtied_before) 1524 { 1525 LIST_HEAD(tmp); 1526 struct list_head *pos, *node; 1527 struct super_block *sb = NULL; 1528 struct inode *inode; 1529 int do_sb_sort = 0; 1530 int moved = 0; 1531 1532 while (!list_empty(delaying_queue)) { 1533 inode = wb_inode(delaying_queue->prev); 1534 if (inode_dirtied_after(inode, dirtied_before)) 1535 break; 1536 spin_lock(&inode->i_lock); 1537 list_move(&inode->i_io_list, &tmp); 1538 moved++; 1539 inode_state_set(inode, I_SYNC_QUEUED); 1540 spin_unlock(&inode->i_lock); 1541 if (sb_is_blkdev_sb(inode->i_sb)) 1542 continue; 1543 if (sb && sb != inode->i_sb) 1544 do_sb_sort = 1; 1545 sb = inode->i_sb; 1546 } 1547 1548 /* just one sb in list, splice to dispatch_queue and we're done */ 1549 if (!do_sb_sort) { 1550 list_splice(&tmp, dispatch_queue); 1551 goto out; 1552 } 1553 1554 /* 1555 * Although inode's i_io_list is moved from 'tmp' to 'dispatch_queue', 1556 * we don't take inode->i_lock here because it is just a pointless overhead. 1557 * Inode is already marked as I_SYNC_QUEUED so writeback list handling is 1558 * fully under our control. 1559 */ 1560 while (!list_empty(&tmp)) { 1561 sb = wb_inode(tmp.prev)->i_sb; 1562 list_for_each_prev_safe(pos, node, &tmp) { 1563 inode = wb_inode(pos); 1564 if (inode->i_sb == sb) 1565 list_move(&inode->i_io_list, dispatch_queue); 1566 } 1567 } 1568 out: 1569 return moved; 1570 } 1571 1572 /* 1573 * Queue all expired dirty inodes for io, eldest first. 1574 * Before 1575 * newly dirtied b_dirty b_io b_more_io 1576 * =============> gf edc BA 1577 * After 1578 * newly dirtied b_dirty b_io b_more_io 1579 * =============> g fBAedc 1580 * | 1581 * +--> dequeue for IO 1582 */ 1583 static void queue_io(struct bdi_writeback *wb, struct wb_writeback_work *work, 1584 unsigned long dirtied_before) 1585 { 1586 int moved; 1587 unsigned long time_expire_jif = dirtied_before; 1588 1589 assert_spin_locked(&wb->list_lock); 1590 list_splice_init(&wb->b_more_io, &wb->b_io); 1591 moved = move_expired_inodes(&wb->b_dirty, &wb->b_io, dirtied_before); 1592 if (!work->for_sync) 1593 time_expire_jif = jiffies - dirtytime_expire_interval * HZ; 1594 moved += move_expired_inodes(&wb->b_dirty_time, &wb->b_io, 1595 time_expire_jif); 1596 if (moved) 1597 wb_io_lists_populated(wb); 1598 trace_writeback_queue_io(wb, work, dirtied_before, moved); 1599 } 1600 1601 static int write_inode(struct inode *inode, struct writeback_control *wbc) 1602 { 1603 int ret; 1604 1605 if (inode->i_sb->s_op->write_inode && !is_bad_inode(inode)) { 1606 trace_writeback_write_inode_start(inode, wbc); 1607 ret = inode->i_sb->s_op->write_inode(inode, wbc); 1608 trace_writeback_write_inode(inode, wbc); 1609 return ret; 1610 } 1611 return 0; 1612 } 1613 1614 /* 1615 * Wait for writeback on an inode to complete. Called with i_lock held. 1616 * Caller must make sure inode cannot go away when we drop i_lock. 1617 */ 1618 void inode_wait_for_writeback(struct inode *inode) 1619 { 1620 struct wait_bit_queue_entry wqe; 1621 struct wait_queue_head *wq_head; 1622 1623 assert_spin_locked(&inode->i_lock); 1624 1625 if (!(inode_state_read(inode) & I_SYNC)) 1626 return; 1627 1628 wq_head = inode_bit_waitqueue(&wqe, inode, __I_SYNC); 1629 for (;;) { 1630 prepare_to_wait_event(wq_head, &wqe.wq_entry, TASK_UNINTERRUPTIBLE); 1631 /* Checking I_SYNC with inode->i_lock guarantees memory ordering. */ 1632 if (!(inode_state_read(inode) & I_SYNC)) 1633 break; 1634 spin_unlock(&inode->i_lock); 1635 schedule(); 1636 spin_lock(&inode->i_lock); 1637 } 1638 finish_wait(wq_head, &wqe.wq_entry); 1639 } 1640 1641 /* 1642 * Sleep until I_SYNC is cleared. This function must be called with i_lock 1643 * held and drops it. It is aimed for callers not holding any inode reference 1644 * so once i_lock is dropped, inode can go away. 1645 */ 1646 static void inode_sleep_on_writeback(struct inode *inode) 1647 __releases(inode->i_lock) 1648 { 1649 struct wait_bit_queue_entry wqe; 1650 struct wait_queue_head *wq_head; 1651 bool sleep; 1652 1653 assert_spin_locked(&inode->i_lock); 1654 1655 wq_head = inode_bit_waitqueue(&wqe, inode, __I_SYNC); 1656 prepare_to_wait_event(wq_head, &wqe.wq_entry, TASK_UNINTERRUPTIBLE); 1657 /* Checking I_SYNC with inode->i_lock guarantees memory ordering. */ 1658 sleep = !!(inode_state_read(inode) & I_SYNC); 1659 spin_unlock(&inode->i_lock); 1660 if (sleep) 1661 schedule(); 1662 finish_wait(wq_head, &wqe.wq_entry); 1663 } 1664 1665 /* 1666 * Find proper writeback list for the inode depending on its current state and 1667 * possibly also change of its state while we were doing writeback. Here we 1668 * handle things such as livelock prevention or fairness of writeback among 1669 * inodes. This function can be called only by flusher thread - noone else 1670 * processes all inodes in writeback lists and requeueing inodes behind flusher 1671 * thread's back can have unexpected consequences. 1672 */ 1673 static void requeue_inode(struct inode *inode, struct bdi_writeback *wb, 1674 struct writeback_control *wbc, 1675 unsigned long dirtied_before) 1676 { 1677 if (inode_state_read(inode) & I_FREEING) 1678 return; 1679 1680 /* 1681 * Sync livelock prevention. Each inode is tagged and synced in one 1682 * shot. If still dirty, it will be redirty_tail()'ed below. Update 1683 * the dirty time to prevent enqueue and sync it again. 1684 */ 1685 if ((inode_state_read(inode) & I_DIRTY) && 1686 (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)) 1687 inode->dirtied_when = jiffies; 1688 1689 if (wbc->pages_skipped) { 1690 /* 1691 * Writeback is not making progress due to locked buffers. 1692 * Skip this inode for now. Although having skipped pages 1693 * is odd for clean inodes, it can happen for some 1694 * filesystems so handle that gracefully. 1695 */ 1696 if (inode_state_read(inode) & I_DIRTY_ALL) 1697 redirty_tail_locked(inode, wb); 1698 else 1699 inode_cgwb_move_to_attached(inode, wb); 1700 return; 1701 } 1702 1703 if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY)) { 1704 /* 1705 * We didn't write back all the pages. nfs_writepages() 1706 * sometimes bales out without doing anything. 1707 */ 1708 if (wbc->nr_to_write <= 0 && 1709 !inode_dirtied_after(inode, dirtied_before)) { 1710 /* Slice used up. Queue for next turn. */ 1711 requeue_io(inode, wb); 1712 } else { 1713 /* 1714 * Writeback blocked by something other than 1715 * congestion. Delay the inode for some time to 1716 * avoid spinning on the CPU (100% iowait) 1717 * retrying writeback of the dirty page/inode 1718 * that cannot be performed immediately. 1719 */ 1720 redirty_tail_locked(inode, wb); 1721 } 1722 } else if (inode_state_read(inode) & I_DIRTY) { 1723 /* 1724 * Filesystems can dirty the inode during writeback operations, 1725 * such as delayed allocation during submission or metadata 1726 * updates after data IO completion. 1727 */ 1728 redirty_tail_locked(inode, wb); 1729 } else if (inode_state_read(inode) & I_DIRTY_TIME) { 1730 inode->dirtied_when = jiffies; 1731 inode_io_list_move_locked(inode, wb, &wb->b_dirty_time); 1732 inode_state_clear(inode, I_SYNC_QUEUED); 1733 } else { 1734 /* The inode is clean. Remove from writeback lists. */ 1735 inode_cgwb_move_to_attached(inode, wb); 1736 } 1737 } 1738 1739 static bool __sync_lazytime(struct inode *inode) 1740 { 1741 spin_lock(&inode->i_lock); 1742 if (!(inode_state_read(inode) & I_DIRTY_TIME)) { 1743 spin_unlock(&inode->i_lock); 1744 return false; 1745 } 1746 inode_state_clear(inode, I_DIRTY_TIME); 1747 spin_unlock(&inode->i_lock); 1748 inode->i_op->sync_lazytime(inode); 1749 return true; 1750 } 1751 1752 bool sync_lazytime(struct inode *inode) 1753 { 1754 if (!(inode_state_read_once(inode) & I_DIRTY_TIME)) 1755 return false; 1756 1757 trace_writeback_lazytime(inode); 1758 if (inode->i_op->sync_lazytime) 1759 return __sync_lazytime(inode); 1760 mark_inode_dirty_sync(inode); 1761 return true; 1762 } 1763 1764 /* 1765 * Write out an inode and its dirty pages (or some of its dirty pages, depending 1766 * on @wbc->nr_to_write), and clear the relevant dirty flags from i_state. 1767 * 1768 * This doesn't remove the inode from the writeback list it is on, except 1769 * potentially to move it from b_dirty_time to b_dirty due to timestamp 1770 * expiration. The caller is otherwise responsible for writeback list handling. 1771 * 1772 * The caller is also responsible for setting the I_SYNC flag beforehand and 1773 * calling inode_sync_complete() to clear it afterwards. 1774 */ 1775 static int 1776 __writeback_single_inode(struct inode *inode, struct writeback_control *wbc) 1777 { 1778 struct address_space *mapping = inode->i_mapping; 1779 long nr_to_write = wbc->nr_to_write; 1780 unsigned dirty; 1781 int ret; 1782 1783 WARN_ON(!(inode_state_read_once(inode) & I_SYNC)); 1784 1785 trace_writeback_single_inode_start(inode, wbc, nr_to_write); 1786 1787 ret = do_writepages(mapping, wbc); 1788 1789 /* 1790 * Make sure to wait on the data before writing out the metadata. 1791 * This is important for filesystems that modify metadata on data 1792 * I/O completion. We don't do it for sync(2) writeback because it has a 1793 * separate, external IO completion path and ->sync_fs for guaranteeing 1794 * inode metadata is written back correctly. 1795 */ 1796 if (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync) { 1797 int err = filemap_fdatawait(mapping); 1798 if (ret == 0) 1799 ret = err; 1800 } 1801 1802 /* 1803 * For data integrity writeback, or when the dirty interval expired, 1804 * ask the file system to propagata lazy timestamp updates into real 1805 * dirty state. 1806 */ 1807 if ((inode_state_read_once(inode) & I_DIRTY_TIME) && 1808 (wbc->sync_mode == WB_SYNC_ALL || 1809 time_after(jiffies, inode->dirtied_time_when + 1810 dirtytime_expire_interval * HZ))) 1811 sync_lazytime(inode); 1812 1813 /* 1814 * Get and clear the dirty flags from i_state. This needs to be done 1815 * after calling writepages because some filesystems may redirty the 1816 * inode during writepages due to delalloc. It also needs to be done 1817 * after handling timestamp expiration, as that may dirty the inode too. 1818 */ 1819 spin_lock(&inode->i_lock); 1820 dirty = inode_state_read(inode) & I_DIRTY; 1821 inode_state_clear(inode, dirty); 1822 1823 /* 1824 * Paired with smp_mb() in __mark_inode_dirty(). This allows 1825 * __mark_inode_dirty() to test i_state without grabbing i_lock - 1826 * either they see the I_DIRTY bits cleared or we see the dirtied 1827 * inode. 1828 * 1829 * I_DIRTY_PAGES is always cleared together above even if @mapping 1830 * still has dirty pages. The flag is reinstated after smp_mb() if 1831 * necessary. This guarantees that either __mark_inode_dirty() 1832 * sees clear I_DIRTY_PAGES or we see PAGECACHE_TAG_DIRTY. 1833 */ 1834 smp_mb(); 1835 1836 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) 1837 inode_state_set(inode, I_DIRTY_PAGES); 1838 else if (unlikely(inode_state_read(inode) & I_PINNING_NETFS_WB)) { 1839 if (!(inode_state_read(inode) & I_DIRTY_PAGES)) { 1840 inode_state_clear(inode, I_PINNING_NETFS_WB); 1841 wbc->unpinned_netfs_wb = true; 1842 dirty |= I_PINNING_NETFS_WB; /* Cause write_inode */ 1843 } 1844 } 1845 1846 spin_unlock(&inode->i_lock); 1847 1848 /* Don't write the inode if only I_DIRTY_PAGES was set */ 1849 if (dirty & ~I_DIRTY_PAGES) { 1850 int err = write_inode(inode, wbc); 1851 if (ret == 0) 1852 ret = err; 1853 } 1854 wbc->unpinned_netfs_wb = false; 1855 trace_writeback_single_inode(inode, wbc, nr_to_write); 1856 return ret; 1857 } 1858 1859 /* 1860 * Write out an inode's dirty data and metadata on-demand, i.e. separately from 1861 * the regular batched writeback done by the flusher threads in 1862 * writeback_sb_inodes(). @wbc controls various aspects of the write, such as 1863 * whether it is a data-integrity sync (%WB_SYNC_ALL) or not (%WB_SYNC_NONE). 1864 * 1865 * To prevent the inode from going away, either the caller must have a reference 1866 * to the inode, or the inode must have I_WILL_FREE or I_FREEING set. 1867 */ 1868 static int writeback_single_inode(struct inode *inode, 1869 struct writeback_control *wbc) 1870 { 1871 struct bdi_writeback *wb; 1872 int ret = 0; 1873 1874 spin_lock(&inode->i_lock); 1875 if (!icount_read(inode)) 1876 WARN_ON(!(inode_state_read(inode) & (I_WILL_FREE | I_FREEING))); 1877 else 1878 WARN_ON(inode_state_read(inode) & I_WILL_FREE); 1879 1880 if (inode_state_read(inode) & I_SYNC) { 1881 /* 1882 * Writeback is already running on the inode. For WB_SYNC_NONE, 1883 * that's enough and we can just return. For WB_SYNC_ALL, we 1884 * must wait for the existing writeback to complete, then do 1885 * writeback again if there's anything left. 1886 */ 1887 if (wbc->sync_mode != WB_SYNC_ALL) 1888 goto out; 1889 inode_wait_for_writeback(inode); 1890 } 1891 WARN_ON(inode_state_read(inode) & I_SYNC); 1892 /* 1893 * If the inode is already fully clean, then there's nothing to do. 1894 * 1895 * For data-integrity syncs we also need to check whether any pages are 1896 * still under writeback, e.g. due to prior WB_SYNC_NONE writeback. If 1897 * there are any such pages, we'll need to wait for them. 1898 */ 1899 if (!(inode_state_read(inode) & I_DIRTY_ALL) && 1900 (wbc->sync_mode != WB_SYNC_ALL || 1901 !mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK))) 1902 goto out; 1903 inode_state_set(inode, I_SYNC); 1904 wbc_attach_and_unlock_inode(wbc, inode); 1905 1906 ret = __writeback_single_inode(inode, wbc); 1907 1908 wbc_detach_inode(wbc); 1909 1910 wb = inode_to_wb_and_lock_list(inode); 1911 spin_lock(&inode->i_lock); 1912 /* 1913 * If the inode is freeing, its i_io_list shoudn't be updated 1914 * as it can be finally deleted at this moment. 1915 */ 1916 if (!(inode_state_read(inode) & I_FREEING)) { 1917 /* 1918 * If the inode is now fully clean, then it can be safely 1919 * removed from its writeback list (if any). Otherwise the 1920 * flusher threads are responsible for the writeback lists. 1921 */ 1922 if (!(inode_state_read(inode) & I_DIRTY_ALL)) 1923 inode_cgwb_move_to_attached(inode, wb); 1924 else if (!(inode_state_read(inode) & I_SYNC_QUEUED)) { 1925 if ((inode_state_read(inode) & I_DIRTY)) 1926 redirty_tail_locked(inode, wb); 1927 else if (inode_state_read(inode) & I_DIRTY_TIME) { 1928 inode->dirtied_when = jiffies; 1929 inode_io_list_move_locked(inode, 1930 wb, 1931 &wb->b_dirty_time); 1932 } 1933 } 1934 } 1935 1936 spin_unlock(&wb->list_lock); 1937 inode_sync_complete(inode); 1938 out: 1939 spin_unlock(&inode->i_lock); 1940 return ret; 1941 } 1942 1943 static long writeback_chunk_size(struct super_block *sb, 1944 struct bdi_writeback *wb, struct wb_writeback_work *work) 1945 { 1946 long pages; 1947 1948 /* 1949 * WB_SYNC_ALL mode does livelock avoidance by syncing dirty 1950 * inodes/pages in one big loop. Setting wbc.nr_to_write=LONG_MAX 1951 * here avoids calling into writeback_inodes_wb() more than once. 1952 * 1953 * The intended call sequence for WB_SYNC_ALL writeback is: 1954 * 1955 * wb_writeback() 1956 * writeback_sb_inodes() <== called only once 1957 * write_cache_pages() <== called once for each inode 1958 * (quickly) tag currently dirty pages 1959 * (maybe slowly) sync all tagged pages 1960 */ 1961 if (work->sync_mode == WB_SYNC_ALL || work->tagged_writepages) 1962 return LONG_MAX; 1963 1964 pages = min(wb->avg_write_bandwidth / 2, 1965 global_wb_domain.dirty_limit / DIRTY_SCOPE); 1966 pages = min(pages, work->nr_pages); 1967 return round_down(pages + sb->s_min_writeback_pages, 1968 sb->s_min_writeback_pages); 1969 } 1970 1971 /* 1972 * Write a portion of b_io inodes which belong to @sb. 1973 * 1974 * Return the number of pages and/or inodes written. 1975 * 1976 * NOTE! This is called with wb->list_lock held, and will 1977 * unlock and relock that for each inode it ends up doing 1978 * IO for. 1979 */ 1980 static long writeback_sb_inodes(struct super_block *sb, 1981 struct bdi_writeback *wb, 1982 struct wb_writeback_work *work) 1983 { 1984 struct writeback_control wbc = { 1985 .sync_mode = work->sync_mode, 1986 .tagged_writepages = work->tagged_writepages, 1987 .for_kupdate = work->for_kupdate, 1988 .for_background = work->for_background, 1989 .for_sync = work->for_sync, 1990 .range_cyclic = work->range_cyclic, 1991 .range_start = 0, 1992 .range_end = LLONG_MAX, 1993 }; 1994 unsigned long start_time = jiffies; 1995 unsigned long timeout = sysctl_hung_task_timeout_secs; 1996 long write_chunk; 1997 long total_wrote = 0; /* count both pages and inodes */ 1998 unsigned long dirtied_before = jiffies; 1999 2000 if (work->for_kupdate) 2001 dirtied_before = jiffies - 2002 msecs_to_jiffies(dirty_expire_interval * 10); 2003 2004 while (!list_empty(&wb->b_io)) { 2005 struct inode *inode = wb_inode(wb->b_io.prev); 2006 struct bdi_writeback *tmp_wb; 2007 long wrote; 2008 2009 if (inode->i_sb != sb) { 2010 if (work->sb) { 2011 /* 2012 * We only want to write back data for this 2013 * superblock, move all inodes not belonging 2014 * to it back onto the dirty list. 2015 */ 2016 redirty_tail(inode, wb); 2017 continue; 2018 } 2019 2020 /* 2021 * The inode belongs to a different superblock. 2022 * Bounce back to the caller to unpin this and 2023 * pin the next superblock. 2024 */ 2025 break; 2026 } 2027 2028 /* 2029 * Don't bother with new inodes or inodes being freed, first 2030 * kind does not need periodic writeout yet, and for the latter 2031 * kind writeout is handled by the freer. 2032 */ 2033 spin_lock(&inode->i_lock); 2034 if (inode_state_read(inode) & (I_NEW | I_FREEING | I_WILL_FREE)) { 2035 redirty_tail_locked(inode, wb); 2036 spin_unlock(&inode->i_lock); 2037 continue; 2038 } 2039 if ((inode_state_read(inode) & I_SYNC) && wbc.sync_mode != WB_SYNC_ALL) { 2040 /* 2041 * If this inode is locked for writeback and we are not 2042 * doing writeback-for-data-integrity, move it to 2043 * b_more_io so that writeback can proceed with the 2044 * other inodes on s_io. 2045 * 2046 * We'll have another go at writing back this inode 2047 * when we completed a full scan of b_io. 2048 */ 2049 requeue_io(inode, wb); 2050 spin_unlock(&inode->i_lock); 2051 trace_writeback_sb_inodes_requeue(inode); 2052 continue; 2053 } 2054 spin_unlock(&wb->list_lock); 2055 2056 /* 2057 * We already requeued the inode if it had I_SYNC set and we 2058 * are doing WB_SYNC_NONE writeback. So this catches only the 2059 * WB_SYNC_ALL case. 2060 */ 2061 if (inode_state_read(inode) & I_SYNC) { 2062 /* Wait for I_SYNC. This function drops i_lock... */ 2063 inode_sleep_on_writeback(inode); 2064 /* Inode may be gone, start again */ 2065 spin_lock(&wb->list_lock); 2066 continue; 2067 } 2068 inode_state_set(inode, I_SYNC); 2069 wbc_attach_and_unlock_inode(&wbc, inode); 2070 2071 write_chunk = writeback_chunk_size(inode->i_sb, wb, work); 2072 wbc.nr_to_write = write_chunk; 2073 wbc.pages_skipped = 0; 2074 2075 /* 2076 * We use I_SYNC to pin the inode in memory. While it is set 2077 * evict_inode() will wait so the inode cannot be freed. 2078 */ 2079 __writeback_single_inode(inode, &wbc); 2080 2081 /* Report progress to inform the hung task detector of the progress. */ 2082 if (work->done && work->done->progress_stamp && timeout && 2083 (jiffies - work->done->progress_stamp) > HZ * timeout / 2) 2084 wake_up_all(work->done->waitq); 2085 2086 wbc_detach_inode(&wbc); 2087 work->nr_pages -= write_chunk - wbc.nr_to_write; 2088 wrote = write_chunk - wbc.nr_to_write - wbc.pages_skipped; 2089 wrote = wrote < 0 ? 0 : wrote; 2090 total_wrote += wrote; 2091 2092 if (need_resched()) { 2093 /* 2094 * We're trying to balance between building up a nice 2095 * long list of IOs to improve our merge rate, and 2096 * getting those IOs out quickly for anyone throttling 2097 * in balance_dirty_pages(). cond_resched() doesn't 2098 * unplug, so get our IOs out the door before we 2099 * give up the CPU. 2100 */ 2101 blk_flush_plug(current->plug, false); 2102 cond_resched(); 2103 } 2104 2105 /* 2106 * Requeue @inode if still dirty. Be careful as @inode may 2107 * have been switched to another wb in the meantime. 2108 */ 2109 tmp_wb = inode_to_wb_and_lock_list(inode); 2110 spin_lock(&inode->i_lock); 2111 if (!(inode_state_read(inode) & I_DIRTY_ALL)) 2112 total_wrote++; 2113 requeue_inode(inode, tmp_wb, &wbc, dirtied_before); 2114 inode_sync_complete(inode); 2115 spin_unlock(&inode->i_lock); 2116 2117 if (unlikely(tmp_wb != wb)) { 2118 spin_unlock(&tmp_wb->list_lock); 2119 spin_lock(&wb->list_lock); 2120 } 2121 2122 /* 2123 * bail out to wb_writeback() often enough to check 2124 * background threshold and other termination conditions. 2125 */ 2126 if (total_wrote) { 2127 if (time_is_before_jiffies(start_time + HZ / 10UL)) 2128 break; 2129 if (work->nr_pages <= 0) 2130 break; 2131 } 2132 } 2133 return total_wrote; 2134 } 2135 2136 static long __writeback_inodes_wb(struct bdi_writeback *wb, 2137 struct wb_writeback_work *work) 2138 { 2139 unsigned long start_time = jiffies; 2140 long wrote = 0; 2141 2142 while (!list_empty(&wb->b_io)) { 2143 struct inode *inode = wb_inode(wb->b_io.prev); 2144 struct super_block *sb = inode->i_sb; 2145 2146 if (!super_trylock_shared(sb)) { 2147 /* 2148 * super_trylock_shared() may fail consistently due to 2149 * s_umount being grabbed by someone else. Don't use 2150 * requeue_io() to avoid busy retrying the inode/sb. 2151 */ 2152 redirty_tail(inode, wb); 2153 continue; 2154 } 2155 wrote += writeback_sb_inodes(sb, wb, work); 2156 up_read(&sb->s_umount); 2157 2158 /* refer to the same tests at the end of writeback_sb_inodes */ 2159 if (wrote) { 2160 if (time_is_before_jiffies(start_time + HZ / 10UL)) 2161 break; 2162 if (work->nr_pages <= 0) 2163 break; 2164 } 2165 } 2166 /* Leave any unwritten inodes on b_io */ 2167 return wrote; 2168 } 2169 2170 static long writeback_inodes_wb(struct bdi_writeback *wb, long nr_pages, 2171 enum wb_reason reason) 2172 { 2173 struct wb_writeback_work work = { 2174 .nr_pages = nr_pages, 2175 .sync_mode = WB_SYNC_NONE, 2176 .range_cyclic = 1, 2177 .reason = reason, 2178 }; 2179 struct blk_plug plug; 2180 2181 blk_start_plug(&plug); 2182 spin_lock(&wb->list_lock); 2183 if (list_empty(&wb->b_io)) 2184 queue_io(wb, &work, jiffies); 2185 __writeback_inodes_wb(wb, &work); 2186 spin_unlock(&wb->list_lock); 2187 blk_finish_plug(&plug); 2188 2189 return nr_pages - work.nr_pages; 2190 } 2191 2192 /* 2193 * Explicit flushing or periodic writeback of "old" data. 2194 * 2195 * Define "old": the first time one of an inode's pages is dirtied, we mark the 2196 * dirtying-time in the inode's address_space. So this periodic writeback code 2197 * just walks the superblock inode list, writing back any inodes which are 2198 * older than a specific point in time. 2199 * 2200 * Try to run once per dirty_writeback_interval. But if a writeback event 2201 * takes longer than a dirty_writeback_interval interval, then leave a 2202 * one-second gap. 2203 * 2204 * dirtied_before takes precedence over nr_to_write. So we'll only write back 2205 * all dirty pages if they are all attached to "old" mappings. 2206 */ 2207 static long wb_writeback(struct bdi_writeback *wb, 2208 struct wb_writeback_work *work) 2209 { 2210 long nr_pages = work->nr_pages; 2211 unsigned long dirtied_before = jiffies; 2212 struct inode *inode; 2213 long progress; 2214 struct blk_plug plug; 2215 bool queued = false; 2216 2217 blk_start_plug(&plug); 2218 for (;;) { 2219 /* 2220 * Stop writeback when nr_pages has been consumed 2221 */ 2222 if (work->nr_pages <= 0) 2223 break; 2224 2225 /* 2226 * Background writeout and kupdate-style writeback may 2227 * run forever. Stop them if there is other work to do 2228 * so that e.g. sync can proceed. They'll be restarted 2229 * after the other works are all done. 2230 */ 2231 if ((work->for_background || work->for_kupdate) && 2232 !list_empty(&wb->work_list)) 2233 break; 2234 2235 /* 2236 * For background writeout, stop when we are below the 2237 * background dirty threshold 2238 */ 2239 if (work->for_background && !wb_over_bg_thresh(wb)) 2240 break; 2241 2242 2243 spin_lock(&wb->list_lock); 2244 2245 trace_writeback_start(wb, work); 2246 if (list_empty(&wb->b_io)) { 2247 /* 2248 * Kupdate and background works are special and we want 2249 * to include all inodes that need writing. Livelock 2250 * avoidance is handled by these works yielding to any 2251 * other work so we are safe. 2252 */ 2253 if (work->for_kupdate) { 2254 dirtied_before = jiffies - 2255 msecs_to_jiffies(dirty_expire_interval * 2256 10); 2257 } else if (work->for_background) 2258 dirtied_before = jiffies; 2259 2260 queue_io(wb, work, dirtied_before); 2261 queued = true; 2262 } 2263 if (work->sb) 2264 progress = writeback_sb_inodes(work->sb, wb, work); 2265 else 2266 progress = __writeback_inodes_wb(wb, work); 2267 trace_writeback_written(wb, work); 2268 2269 /* 2270 * Did we write something? Try for more 2271 * 2272 * Dirty inodes are moved to b_io for writeback in batches. 2273 * The completion of the current batch does not necessarily 2274 * mean the overall work is done. So we keep looping as long 2275 * as made some progress on cleaning pages or inodes. 2276 */ 2277 if (progress || !queued) { 2278 spin_unlock(&wb->list_lock); 2279 continue; 2280 } 2281 2282 /* 2283 * No more inodes for IO, bail 2284 */ 2285 if (list_empty(&wb->b_more_io)) { 2286 spin_unlock(&wb->list_lock); 2287 break; 2288 } 2289 2290 /* 2291 * Nothing written. Wait for some inode to 2292 * become available for writeback. Otherwise 2293 * we'll just busyloop. 2294 */ 2295 trace_writeback_wait(wb, work); 2296 inode = wb_inode(wb->b_more_io.prev); 2297 spin_lock(&inode->i_lock); 2298 spin_unlock(&wb->list_lock); 2299 /* This function drops i_lock... */ 2300 inode_sleep_on_writeback(inode); 2301 } 2302 blk_finish_plug(&plug); 2303 2304 return nr_pages - work->nr_pages; 2305 } 2306 2307 /* 2308 * Return the next wb_writeback_work struct that hasn't been processed yet. 2309 */ 2310 static struct wb_writeback_work *get_next_work_item(struct bdi_writeback *wb) 2311 { 2312 struct wb_writeback_work *work = NULL; 2313 2314 spin_lock_irq(&wb->work_lock); 2315 if (!list_empty(&wb->work_list)) { 2316 work = list_entry(wb->work_list.next, 2317 struct wb_writeback_work, list); 2318 list_del_init(&work->list); 2319 } 2320 spin_unlock_irq(&wb->work_lock); 2321 return work; 2322 } 2323 2324 static long wb_check_background_flush(struct bdi_writeback *wb) 2325 { 2326 if (wb_over_bg_thresh(wb)) { 2327 2328 struct wb_writeback_work work = { 2329 .nr_pages = LONG_MAX, 2330 .sync_mode = WB_SYNC_NONE, 2331 .for_background = 1, 2332 .range_cyclic = 1, 2333 .reason = WB_REASON_BACKGROUND, 2334 }; 2335 2336 return wb_writeback(wb, &work); 2337 } 2338 2339 return 0; 2340 } 2341 2342 static long wb_check_old_data_flush(struct bdi_writeback *wb) 2343 { 2344 unsigned long expired; 2345 long nr_pages; 2346 2347 /* 2348 * When set to zero, disable periodic writeback 2349 */ 2350 if (!dirty_writeback_interval) 2351 return 0; 2352 2353 expired = wb->last_old_flush + 2354 msecs_to_jiffies(dirty_writeback_interval * 10); 2355 if (time_before(jiffies, expired)) 2356 return 0; 2357 2358 wb->last_old_flush = jiffies; 2359 nr_pages = get_nr_dirty_pages(); 2360 2361 if (nr_pages) { 2362 struct wb_writeback_work work = { 2363 .nr_pages = nr_pages, 2364 .sync_mode = WB_SYNC_NONE, 2365 .for_kupdate = 1, 2366 .range_cyclic = 1, 2367 .reason = WB_REASON_PERIODIC, 2368 }; 2369 2370 return wb_writeback(wb, &work); 2371 } 2372 2373 return 0; 2374 } 2375 2376 static long wb_check_start_all(struct bdi_writeback *wb) 2377 { 2378 long nr_pages; 2379 2380 if (!test_bit(WB_start_all, &wb->state)) 2381 return 0; 2382 2383 nr_pages = get_nr_dirty_pages(); 2384 if (nr_pages) { 2385 struct wb_writeback_work work = { 2386 .nr_pages = wb_split_bdi_pages(wb, nr_pages), 2387 .sync_mode = WB_SYNC_NONE, 2388 .range_cyclic = 1, 2389 .reason = wb->start_all_reason, 2390 }; 2391 2392 nr_pages = wb_writeback(wb, &work); 2393 } 2394 2395 clear_bit(WB_start_all, &wb->state); 2396 return nr_pages; 2397 } 2398 2399 static long wb_check_start_dontcache(struct bdi_writeback *wb) 2400 { 2401 long nr_pages; 2402 2403 if (!test_and_clear_bit(WB_start_dontcache, &wb->state)) 2404 return 0; 2405 2406 nr_pages = wb_stat_sum(wb, WB_DONTCACHE_DIRTY); 2407 if (nr_pages) { 2408 struct wb_writeback_work work = { 2409 .nr_pages = nr_pages, 2410 .sync_mode = WB_SYNC_NONE, 2411 .range_cyclic = 1, 2412 .reason = WB_REASON_DONTCACHE, 2413 }; 2414 2415 nr_pages = wb_writeback(wb, &work); 2416 } 2417 2418 return nr_pages; 2419 } 2420 2421 /* 2422 * Retrieve work items and do the writeback they describe 2423 */ 2424 static long wb_do_writeback(struct bdi_writeback *wb) 2425 { 2426 struct wb_writeback_work *work; 2427 long wrote = 0; 2428 2429 set_bit(WB_writeback_running, &wb->state); 2430 while ((work = get_next_work_item(wb)) != NULL) { 2431 trace_writeback_exec(wb, work); 2432 wrote += wb_writeback(wb, work); 2433 finish_writeback_work(work); 2434 } 2435 2436 /* 2437 * Check for a flush-everything request 2438 */ 2439 wrote += wb_check_start_all(wb); 2440 2441 /* 2442 * Check for dontcache writeback request 2443 */ 2444 wrote += wb_check_start_dontcache(wb); 2445 2446 /* 2447 * Check for periodic writeback, kupdated() style 2448 */ 2449 wrote += wb_check_old_data_flush(wb); 2450 wrote += wb_check_background_flush(wb); 2451 clear_bit(WB_writeback_running, &wb->state); 2452 2453 return wrote; 2454 } 2455 2456 /* 2457 * Handle writeback of dirty data for the device backed by this bdi. Also 2458 * reschedules periodically and does kupdated style flushing. 2459 */ 2460 void wb_workfn(struct work_struct *work) 2461 { 2462 struct bdi_writeback *wb = container_of(to_delayed_work(work), 2463 struct bdi_writeback, dwork); 2464 long pages_written; 2465 2466 set_worker_desc("flush-%s", bdi_dev_name(wb->bdi)); 2467 2468 if (likely(!current_is_workqueue_rescuer() || 2469 !test_bit(WB_registered, &wb->state))) { 2470 /* 2471 * The normal path. Keep writing back @wb until its 2472 * work_list is empty. Note that this path is also taken 2473 * if @wb is shutting down even when we're running off the 2474 * rescuer as work_list needs to be drained. 2475 */ 2476 do { 2477 pages_written = wb_do_writeback(wb); 2478 trace_writeback_pages_written(pages_written); 2479 } while (!list_empty(&wb->work_list)); 2480 } else { 2481 /* 2482 * bdi_wq can't get enough workers and we're running off 2483 * the emergency worker. Don't hog it. Hopefully, 1024 is 2484 * enough for efficient IO. 2485 */ 2486 pages_written = writeback_inodes_wb(wb, 1024, 2487 WB_REASON_FORKER_THREAD); 2488 trace_writeback_pages_written(pages_written); 2489 } 2490 2491 if (!list_empty(&wb->work_list)) 2492 wb_wakeup(wb); 2493 else if (wb_has_dirty_io(wb) && dirty_writeback_interval) 2494 wb_wakeup_delayed(wb); 2495 } 2496 2497 /* 2498 * Start writeback of all dirty pages on this bdi. 2499 */ 2500 static void __wakeup_flusher_threads_bdi(struct backing_dev_info *bdi, 2501 enum wb_reason reason) 2502 { 2503 struct bdi_writeback *wb; 2504 2505 if (!bdi_has_dirty_io(bdi)) 2506 return; 2507 2508 list_for_each_entry_rcu(wb, &bdi->wb_list, bdi_node) 2509 wb_start_writeback(wb, reason); 2510 } 2511 2512 void wakeup_flusher_threads_bdi(struct backing_dev_info *bdi, 2513 enum wb_reason reason) 2514 { 2515 rcu_read_lock(); 2516 __wakeup_flusher_threads_bdi(bdi, reason); 2517 rcu_read_unlock(); 2518 } 2519 2520 /** 2521 * filemap_dontcache_kick_writeback - kick flusher for IOCB_DONTCACHE writes 2522 * @mapping: address_space that was just written to 2523 * 2524 * Kick the writeback flusher thread to expedite writeback of dontcache dirty 2525 * pages. Queue writeback for the inode's wb for as many pages as there are 2526 * dontcache pages, but don't restrict writeback to dontcache pages only. 2527 * 2528 * This significantly improves performance over either writing all wb's pages 2529 * or writing only dontcache pages. Although it doesn't guarantee quick 2530 * writeback and reclaim of dontcache pages, it keeps the amount of dirty pages 2531 * in check. Over longer term dontcache pages get written and reclaimed by 2532 * background writeback even with this rough heuristic. 2533 */ 2534 void filemap_dontcache_kick_writeback(struct address_space *mapping) 2535 { 2536 struct inode *inode = mapping->host; 2537 struct bdi_writeback *wb; 2538 struct wb_lock_cookie cookie = {}; 2539 bool need_wakeup = false; 2540 2541 wb = unlocked_inode_to_wb_begin(inode, &cookie); 2542 if (wb_has_dirty_io(wb) && 2543 !test_bit(WB_start_dontcache, &wb->state) && 2544 !test_and_set_bit(WB_start_dontcache, &wb->state)) { 2545 wb_get(wb); 2546 need_wakeup = true; 2547 } 2548 unlocked_inode_to_wb_end(inode, &cookie); 2549 2550 if (need_wakeup) { 2551 wb_wakeup(wb); 2552 wb_put(wb); 2553 } 2554 } 2555 EXPORT_SYMBOL_GPL(filemap_dontcache_kick_writeback); 2556 2557 /* 2558 * Wakeup the flusher threads to start writeback of all currently dirty pages 2559 */ 2560 void wakeup_flusher_threads(enum wb_reason reason) 2561 { 2562 struct backing_dev_info *bdi; 2563 2564 /* 2565 * If we are expecting writeback progress we must submit plugged IO. 2566 */ 2567 blk_flush_plug(current->plug, true); 2568 2569 rcu_read_lock(); 2570 list_for_each_entry_rcu(bdi, &bdi_list, bdi_list) 2571 __wakeup_flusher_threads_bdi(bdi, reason); 2572 rcu_read_unlock(); 2573 } 2574 2575 /* 2576 * Wake up bdi's periodically to make sure dirtytime inodes gets 2577 * written back periodically. We deliberately do *not* check the 2578 * b_dirtytime list in wb_has_dirty_io(), since this would cause the 2579 * kernel to be constantly waking up once there are any dirtytime 2580 * inodes on the system. So instead we define a separate delayed work 2581 * function which gets called much more rarely. (By default, only 2582 * once every 12 hours.) 2583 * 2584 * If there is any other write activity going on in the file system, 2585 * this function won't be necessary. But if the only thing that has 2586 * happened on the file system is a dirtytime inode caused by an atime 2587 * update, we need this infrastructure below to make sure that inode 2588 * eventually gets pushed out to disk. 2589 */ 2590 static void wakeup_dirtytime_writeback(struct work_struct *w); 2591 static DECLARE_DELAYED_WORK(dirtytime_work, wakeup_dirtytime_writeback); 2592 2593 static void wakeup_dirtytime_writeback(struct work_struct *w) 2594 { 2595 struct backing_dev_info *bdi; 2596 2597 rcu_read_lock(); 2598 list_for_each_entry_rcu(bdi, &bdi_list, bdi_list) { 2599 struct bdi_writeback *wb; 2600 2601 list_for_each_entry_rcu(wb, &bdi->wb_list, bdi_node) 2602 if (!list_empty(&wb->b_dirty_time)) 2603 wb_wakeup(wb); 2604 } 2605 rcu_read_unlock(); 2606 if (dirtytime_expire_interval) 2607 schedule_delayed_work(&dirtytime_work, 2608 round_jiffies_relative(dirtytime_expire_interval * HZ)); 2609 } 2610 2611 static int dirtytime_interval_handler(const struct ctl_table *table, int write, 2612 void *buffer, size_t *lenp, loff_t *ppos) 2613 { 2614 int ret; 2615 2616 ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos); 2617 if (ret == 0 && write) { 2618 if (dirtytime_expire_interval) 2619 mod_delayed_work(system_percpu_wq, &dirtytime_work, 0); 2620 else 2621 cancel_delayed_work_sync(&dirtytime_work); 2622 } 2623 return ret; 2624 } 2625 2626 static const struct ctl_table vm_fs_writeback_table[] = { 2627 { 2628 .procname = "dirtytime_expire_seconds", 2629 .data = &dirtytime_expire_interval, 2630 .maxlen = sizeof(dirtytime_expire_interval), 2631 .mode = 0644, 2632 .proc_handler = dirtytime_interval_handler, 2633 .extra1 = SYSCTL_ZERO, 2634 }, 2635 }; 2636 2637 static int __init start_dirtytime_writeback(void) 2638 { 2639 if (dirtytime_expire_interval) 2640 schedule_delayed_work(&dirtytime_work, 2641 round_jiffies_relative(dirtytime_expire_interval * HZ)); 2642 register_sysctl_init("vm", vm_fs_writeback_table); 2643 return 0; 2644 } 2645 __initcall(start_dirtytime_writeback); 2646 2647 /** 2648 * __mark_inode_dirty - internal function to mark an inode dirty 2649 * 2650 * @inode: inode to mark 2651 * @flags: what kind of dirty, e.g. I_DIRTY_SYNC. This can be a combination of 2652 * multiple I_DIRTY_* flags, except that I_DIRTY_TIME can't be combined 2653 * with I_DIRTY_PAGES. 2654 * 2655 * Mark an inode as dirty. We notify the filesystem, then update the inode's 2656 * dirty flags. Then, if needed we add the inode to the appropriate dirty list. 2657 * 2658 * Most callers should use mark_inode_dirty() or mark_inode_dirty_sync() 2659 * instead of calling this directly. 2660 * 2661 * CAREFUL! We only add the inode to the dirty list if it is hashed or if it 2662 * refers to a blockdev. Unhashed inodes will never be added to the dirty list 2663 * even if they are later hashed, as they will have been marked dirty already. 2664 * 2665 * In short, ensure you hash any inodes _before_ you start marking them dirty. 2666 * 2667 * Note that for blockdevs, inode->dirtied_when represents the dirtying time of 2668 * the block-special inode (/dev/hda1) itself. And the ->dirtied_when field of 2669 * the kernel-internal blockdev inode represents the dirtying time of the 2670 * blockdev's pages. This is why for I_DIRTY_PAGES we always use 2671 * page->mapping->host, so the page-dirtying time is recorded in the internal 2672 * blockdev inode. 2673 */ 2674 void __mark_inode_dirty(struct inode *inode, int flags) 2675 { 2676 struct super_block *sb = inode->i_sb; 2677 int dirtytime = 0; 2678 struct bdi_writeback *wb = NULL; 2679 2680 trace_writeback_mark_inode_dirty(inode, flags); 2681 2682 if (flags & I_DIRTY_INODE) { 2683 bool was_dirty_time = false; 2684 2685 /* 2686 * Inode timestamp update will piggback on this dirtying. 2687 * We tell ->dirty_inode callback that timestamps need to 2688 * be updated by setting I_DIRTY_TIME in flags. 2689 */ 2690 if (inode_state_read_once(inode) & I_DIRTY_TIME) { 2691 spin_lock(&inode->i_lock); 2692 if (inode_state_read(inode) & I_DIRTY_TIME) { 2693 inode_state_clear(inode, I_DIRTY_TIME); 2694 flags |= I_DIRTY_TIME; 2695 was_dirty_time = true; 2696 } 2697 spin_unlock(&inode->i_lock); 2698 } 2699 2700 /* 2701 * Notify the filesystem about the inode being dirtied, so that 2702 * (if needed) it can update on-disk fields and journal the 2703 * inode. This is only needed when the inode itself is being 2704 * dirtied now. I.e. it's only needed for I_DIRTY_INODE, not 2705 * for just I_DIRTY_PAGES or I_DIRTY_TIME. 2706 */ 2707 trace_writeback_dirty_inode_start(inode, flags); 2708 if (sb->s_op->dirty_inode) { 2709 sb->s_op->dirty_inode(inode, 2710 flags & (I_DIRTY_INODE | I_DIRTY_TIME)); 2711 } else if (was_dirty_time && inode->i_op->sync_lazytime) { 2712 inode->i_op->sync_lazytime(inode); 2713 } 2714 trace_writeback_dirty_inode(inode, flags); 2715 2716 /* I_DIRTY_INODE supersedes I_DIRTY_TIME. */ 2717 flags &= ~I_DIRTY_TIME; 2718 } else { 2719 /* 2720 * Else it's either I_DIRTY_PAGES, I_DIRTY_TIME, or nothing. 2721 * (We don't support setting both I_DIRTY_PAGES and I_DIRTY_TIME 2722 * in one call to __mark_inode_dirty().) 2723 */ 2724 dirtytime = flags & I_DIRTY_TIME; 2725 WARN_ON_ONCE(dirtytime && flags != I_DIRTY_TIME); 2726 } 2727 2728 /* 2729 * Paired with smp_mb() in __writeback_single_inode() for the 2730 * following lockless i_state test. See there for details. 2731 */ 2732 smp_mb(); 2733 2734 if ((inode_state_read_once(inode) & flags) == flags) 2735 return; 2736 2737 spin_lock(&inode->i_lock); 2738 if ((inode_state_read(inode) & flags) != flags) { 2739 const int was_dirty = inode_state_read(inode) & I_DIRTY; 2740 2741 inode_attach_wb(inode, NULL); 2742 2743 inode_state_set(inode, flags); 2744 2745 /* 2746 * Grab inode's wb early because it requires dropping i_lock and we 2747 * need to make sure following checks happen atomically with dirty 2748 * list handling so that we don't move inodes under flush worker's 2749 * hands. 2750 */ 2751 if (!was_dirty) { 2752 wb = locked_inode_to_wb_and_lock_list(inode); 2753 spin_lock(&inode->i_lock); 2754 } 2755 2756 /* 2757 * If the inode is queued for writeback by flush worker, just 2758 * update its dirty state. Once the flush worker is done with 2759 * the inode it will place it on the appropriate superblock 2760 * list, based upon its state. 2761 */ 2762 if (inode_state_read(inode) & I_SYNC_QUEUED) 2763 goto out_unlock; 2764 2765 /* 2766 * Only add valid (hashed) inodes to the superblock's 2767 * dirty list. Add blockdev inodes as well. 2768 */ 2769 if (!S_ISBLK(inode->i_mode)) { 2770 if (inode_unhashed(inode)) 2771 goto out_unlock; 2772 } 2773 if (inode_state_read(inode) & I_FREEING) 2774 goto out_unlock; 2775 2776 /* 2777 * If the inode was already on b_dirty/b_io/b_more_io, don't 2778 * reposition it (that would break b_dirty time-ordering). 2779 */ 2780 if (!was_dirty) { 2781 struct list_head *dirty_list; 2782 bool wakeup_bdi = false; 2783 2784 inode->dirtied_when = jiffies; 2785 if (dirtytime) 2786 inode->dirtied_time_when = jiffies; 2787 2788 if (inode_state_read(inode) & I_DIRTY) 2789 dirty_list = &wb->b_dirty; 2790 else 2791 dirty_list = &wb->b_dirty_time; 2792 2793 wakeup_bdi = inode_io_list_move_locked(inode, wb, 2794 dirty_list); 2795 2796 /* 2797 * If this is the first dirty inode for this bdi, 2798 * we have to wake-up the corresponding bdi thread 2799 * to make sure background write-back happens 2800 * later. 2801 */ 2802 if (wakeup_bdi && 2803 (wb->bdi->capabilities & BDI_CAP_WRITEBACK)) 2804 wb_wakeup_delayed(wb); 2805 2806 spin_unlock(&wb->list_lock); 2807 spin_unlock(&inode->i_lock); 2808 trace_writeback_dirty_inode_enqueue(inode); 2809 2810 return; 2811 } 2812 } 2813 out_unlock: 2814 if (wb) 2815 spin_unlock(&wb->list_lock); 2816 spin_unlock(&inode->i_lock); 2817 } 2818 EXPORT_SYMBOL(__mark_inode_dirty); 2819 2820 /* 2821 * The @s_sync_lock is used to serialise concurrent sync operations 2822 * to avoid lock contention problems with concurrent wait_sb_inodes() calls. 2823 * Concurrent callers will block on the s_sync_lock rather than doing contending 2824 * walks. The queueing maintains sync(2) required behaviour as all the IO that 2825 * has been issued up to the time this function is enter is guaranteed to be 2826 * completed by the time we have gained the lock and waited for all IO that is 2827 * in progress regardless of the order callers are granted the lock. 2828 */ 2829 static void wait_sb_inodes(struct super_block *sb) 2830 { 2831 LIST_HEAD(sync_list); 2832 2833 /* 2834 * We need to be protected against the filesystem going from 2835 * r/o to r/w or vice versa. 2836 */ 2837 WARN_ON(!rwsem_is_locked(&sb->s_umount)); 2838 2839 mutex_lock(&sb->s_sync_lock); 2840 2841 /* 2842 * Splice the writeback list onto a temporary list to avoid waiting on 2843 * inodes that have started writeback after this point. 2844 * 2845 * Use rcu_read_lock() to keep the inodes around until we have a 2846 * reference. s_inode_wblist_lock protects sb->s_inodes_wb as well as 2847 * the local list because inodes can be dropped from either by writeback 2848 * completion. 2849 */ 2850 rcu_read_lock(); 2851 spin_lock_irq(&sb->s_inode_wblist_lock); 2852 list_splice_init(&sb->s_inodes_wb, &sync_list); 2853 2854 /* 2855 * Data integrity sync. Must wait for all pages under writeback, because 2856 * there may have been pages dirtied before our sync call, but which had 2857 * writeout started before we write it out. In which case, the inode 2858 * may not be on the dirty list, but we still have to wait for that 2859 * writeout. 2860 */ 2861 while (!list_empty(&sync_list)) { 2862 struct inode *inode = list_first_entry(&sync_list, struct inode, 2863 i_wb_list); 2864 struct address_space *mapping = inode->i_mapping; 2865 2866 /* 2867 * Move each inode back to the wb list before we drop the lock 2868 * to preserve consistency between i_wb_list and the mapping 2869 * writeback tag. Writeback completion is responsible to remove 2870 * the inode from either list once the writeback tag is cleared. 2871 */ 2872 list_move_tail(&inode->i_wb_list, &sb->s_inodes_wb); 2873 2874 /* 2875 * The mapping can appear untagged while still on-list since we 2876 * do not have the mapping lock. Skip it here, wb completion 2877 * will remove it. 2878 */ 2879 if (!mapping_tagged(mapping, PAGECACHE_TAG_WRITEBACK)) 2880 continue; 2881 2882 spin_unlock_irq(&sb->s_inode_wblist_lock); 2883 2884 spin_lock(&inode->i_lock); 2885 if (inode_state_read(inode) & (I_FREEING | I_WILL_FREE | I_NEW)) { 2886 spin_unlock(&inode->i_lock); 2887 2888 spin_lock_irq(&sb->s_inode_wblist_lock); 2889 continue; 2890 } 2891 __iget(inode); 2892 spin_unlock(&inode->i_lock); 2893 rcu_read_unlock(); 2894 2895 /* 2896 * We keep the error status of individual mapping so that 2897 * applications can catch the writeback error using fsync(2). 2898 * See filemap_fdatawait_keep_errors() for details. 2899 */ 2900 filemap_fdatawait_keep_errors(mapping); 2901 2902 cond_resched(); 2903 2904 iput(inode); 2905 2906 rcu_read_lock(); 2907 spin_lock_irq(&sb->s_inode_wblist_lock); 2908 } 2909 spin_unlock_irq(&sb->s_inode_wblist_lock); 2910 rcu_read_unlock(); 2911 mutex_unlock(&sb->s_sync_lock); 2912 } 2913 2914 static void __writeback_inodes_sb_nr(struct super_block *sb, unsigned long nr, 2915 enum wb_reason reason, bool skip_if_busy) 2916 { 2917 struct backing_dev_info *bdi = sb->s_bdi; 2918 DEFINE_WB_COMPLETION(done, bdi); 2919 struct wb_writeback_work work = { 2920 .sb = sb, 2921 .sync_mode = WB_SYNC_NONE, 2922 .tagged_writepages = 1, 2923 .done = &done, 2924 .nr_pages = nr, 2925 .reason = reason, 2926 }; 2927 2928 if (!bdi_has_dirty_io(bdi) || bdi == &noop_backing_dev_info) 2929 return; 2930 WARN_ON(!rwsem_is_locked(&sb->s_umount)); 2931 2932 bdi_split_work_to_wbs(sb->s_bdi, &work, skip_if_busy); 2933 wb_wait_for_completion(&done); 2934 } 2935 2936 /** 2937 * writeback_inodes_sb_nr - writeback dirty inodes from given super_block 2938 * @sb: the superblock 2939 * @nr: the number of pages to write 2940 * @reason: reason why some writeback work initiated 2941 * 2942 * Start writeback on some inodes on this super_block. No guarantees are made 2943 * on how many (if any) will be written, and this function does not wait 2944 * for IO completion of submitted IO. 2945 */ 2946 void writeback_inodes_sb_nr(struct super_block *sb, 2947 unsigned long nr, 2948 enum wb_reason reason) 2949 { 2950 __writeback_inodes_sb_nr(sb, nr, reason, false); 2951 } 2952 EXPORT_SYMBOL(writeback_inodes_sb_nr); 2953 2954 /** 2955 * writeback_inodes_sb - writeback dirty inodes from given super_block 2956 * @sb: the superblock 2957 * @reason: reason why some writeback work was initiated 2958 * 2959 * Start writeback on some inodes on this super_block. No guarantees are made 2960 * on how many (if any) will be written, and this function does not wait 2961 * for IO completion of submitted IO. 2962 */ 2963 void writeback_inodes_sb(struct super_block *sb, enum wb_reason reason) 2964 { 2965 writeback_inodes_sb_nr(sb, get_nr_dirty_pages(), reason); 2966 } 2967 EXPORT_SYMBOL(writeback_inodes_sb); 2968 2969 /** 2970 * try_to_writeback_inodes_sb - try to start writeback if none underway 2971 * @sb: the superblock 2972 * @reason: reason why some writeback work was initiated 2973 * 2974 * Invoke __writeback_inodes_sb_nr if no writeback is currently underway. 2975 */ 2976 void try_to_writeback_inodes_sb(struct super_block *sb, enum wb_reason reason) 2977 { 2978 if (!down_read_trylock(&sb->s_umount)) 2979 return; 2980 2981 __writeback_inodes_sb_nr(sb, get_nr_dirty_pages(), reason, true); 2982 up_read(&sb->s_umount); 2983 } 2984 EXPORT_SYMBOL(try_to_writeback_inodes_sb); 2985 2986 /** 2987 * sync_inodes_sb - sync sb inode pages 2988 * @sb: the superblock 2989 * 2990 * This function writes and waits on any dirty inode belonging to this 2991 * super_block. 2992 */ 2993 void sync_inodes_sb(struct super_block *sb) 2994 { 2995 struct backing_dev_info *bdi = sb->s_bdi; 2996 DEFINE_WB_COMPLETION(done, bdi); 2997 struct wb_writeback_work work = { 2998 .sb = sb, 2999 .sync_mode = WB_SYNC_ALL, 3000 .nr_pages = LONG_MAX, 3001 .range_cyclic = 0, 3002 .done = &done, 3003 .reason = WB_REASON_SYNC, 3004 .for_sync = 1, 3005 }; 3006 3007 /* 3008 * Can't skip on !bdi_has_dirty() because we should wait for !dirty 3009 * inodes under writeback and I_DIRTY_TIME inodes ignored by 3010 * bdi_has_dirty() need to be written out too. 3011 */ 3012 if (bdi == &noop_backing_dev_info) 3013 return; 3014 3015 /* 3016 * If the superblock has SB_I_NO_DATA_INTEGRITY set, there's no need to 3017 * wait for the writeout to complete, as the filesystem cannot guarantee 3018 * data persistence on sync. Just kick off writeback and return. 3019 */ 3020 if (sb->s_iflags & SB_I_NO_DATA_INTEGRITY) { 3021 wakeup_flusher_threads_bdi(bdi, WB_REASON_SYNC); 3022 return; 3023 } 3024 3025 WARN_ON(!rwsem_is_locked(&sb->s_umount)); 3026 3027 /* protect against inode wb switch, see inode_switch_wbs_work_fn() */ 3028 bdi_down_write_wb_switch_rwsem(bdi); 3029 bdi_split_work_to_wbs(bdi, &work, false); 3030 wb_wait_for_completion(&done); 3031 bdi_up_write_wb_switch_rwsem(bdi); 3032 3033 wait_sb_inodes(sb); 3034 } 3035 EXPORT_SYMBOL(sync_inodes_sb); 3036 3037 /** 3038 * write_inode_now - write an inode to disk 3039 * @inode: inode to write to disk 3040 * @sync: whether the write should be synchronous or not 3041 * 3042 * This function commits an inode to disk immediately if it is dirty. This is 3043 * primarily needed by knfsd. 3044 * 3045 * The caller must either have a ref on the inode or must have set I_WILL_FREE. 3046 */ 3047 int write_inode_now(struct inode *inode, int sync) 3048 { 3049 struct writeback_control wbc = { 3050 .nr_to_write = LONG_MAX, 3051 .sync_mode = sync ? WB_SYNC_ALL : WB_SYNC_NONE, 3052 .range_start = 0, 3053 .range_end = LLONG_MAX, 3054 }; 3055 3056 if (!mapping_can_writeback(inode->i_mapping)) 3057 wbc.nr_to_write = 0; 3058 3059 might_sleep(); 3060 return writeback_single_inode(inode, &wbc); 3061 } 3062 EXPORT_SYMBOL(write_inode_now); 3063 3064 /** 3065 * sync_inode_metadata - write an inode to disk 3066 * @inode: the inode to sync 3067 * @wait: wait for I/O to complete. 3068 * 3069 * Write an inode to disk and adjust its dirty state after completion. 3070 * 3071 * Note: only writes the actual inode, no associated data or other metadata. 3072 */ 3073 int sync_inode_metadata(struct inode *inode, int wait) 3074 { 3075 struct writeback_control wbc = { 3076 .sync_mode = wait ? WB_SYNC_ALL : WB_SYNC_NONE, 3077 .nr_to_write = 0, /* metadata-only */ 3078 }; 3079 3080 return writeback_single_inode(inode, &wbc); 3081 } 3082 EXPORT_SYMBOL(sync_inode_metadata); 3083