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