fs-writeback.c (5afced3bf28100d81fb2fe7e98918632a08feaf5) fs-writeback.c (f9cae926f35e8230330f28c7b743ad088611a8de)
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

--- 28 unchanged lines hidden (view full) ---

37#define MIN_WRITEBACK_PAGES (4096UL >> (PAGE_SHIFT - 10))
38
39/*
40 * Passed into wb_writeback(), essentially a subset of writeback_control
41 */
42struct wb_writeback_work {
43 long nr_pages;
44 struct super_block *sb;
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

--- 28 unchanged lines hidden (view full) ---

37#define MIN_WRITEBACK_PAGES (4096UL >> (PAGE_SHIFT - 10))
38
39/*
40 * Passed into wb_writeback(), essentially a subset of writeback_control
41 */
42struct wb_writeback_work {
43 long nr_pages;
44 struct super_block *sb;
45 unsigned long *older_than_this;
46 enum writeback_sync_modes sync_mode;
47 unsigned int tagged_writepages:1;
48 unsigned int for_kupdate:1;
49 unsigned int range_cyclic:1;
50 unsigned int for_background:1;
51 unsigned int for_sync:1; /* sync(2) WB_SYNC_ALL writeback */
52 unsigned int auto_free:1; /* free on completion */
53 enum wb_reason reason; /* why was writeback initiated? */

--- 1175 unchanged lines hidden (view full) ---

1229 ret = ret && time_before_eq(inode->dirtied_when, jiffies);
1230#endif
1231 return ret;
1232}
1233
1234#define EXPIRE_DIRTY_ATIME 0x0001
1235
1236/*
45 enum writeback_sync_modes sync_mode;
46 unsigned int tagged_writepages:1;
47 unsigned int for_kupdate:1;
48 unsigned int range_cyclic:1;
49 unsigned int for_background:1;
50 unsigned int for_sync:1; /* sync(2) WB_SYNC_ALL writeback */
51 unsigned int auto_free:1; /* free on completion */
52 enum wb_reason reason; /* why was writeback initiated? */

--- 1175 unchanged lines hidden (view full) ---

1228 ret = ret && time_before_eq(inode->dirtied_when, jiffies);
1229#endif
1230 return ret;
1231}
1232
1233#define EXPIRE_DIRTY_ATIME 0x0001
1234
1235/*
1237 * Move expired (dirtied before work->older_than_this) dirty inodes from
1236 * Move expired (dirtied before dirtied_before) dirty inodes from
1238 * @delaying_queue to @dispatch_queue.
1239 */
1240static int move_expired_inodes(struct list_head *delaying_queue,
1241 struct list_head *dispatch_queue,
1237 * @delaying_queue to @dispatch_queue.
1238 */
1239static int move_expired_inodes(struct list_head *delaying_queue,
1240 struct list_head *dispatch_queue,
1242 int flags,
1243 struct wb_writeback_work *work)
1241 int flags, unsigned long dirtied_before)
1244{
1242{
1245 unsigned long *older_than_this = NULL;
1246 unsigned long expire_time;
1247 LIST_HEAD(tmp);
1248 struct list_head *pos, *node;
1249 struct super_block *sb = NULL;
1250 struct inode *inode;
1251 int do_sb_sort = 0;
1252 int moved = 0;
1253
1243 LIST_HEAD(tmp);
1244 struct list_head *pos, *node;
1245 struct super_block *sb = NULL;
1246 struct inode *inode;
1247 int do_sb_sort = 0;
1248 int moved = 0;
1249
1254 if ((flags & EXPIRE_DIRTY_ATIME) == 0)
1255 older_than_this = work->older_than_this;
1256 else if (!work->for_sync) {
1257 expire_time = jiffies - (dirtytime_expire_interval * HZ);
1258 older_than_this = &expire_time;
1259 }
1260 while (!list_empty(delaying_queue)) {
1261 inode = wb_inode(delaying_queue->prev);
1250 while (!list_empty(delaying_queue)) {
1251 inode = wb_inode(delaying_queue->prev);
1262 if (older_than_this &&
1263 inode_dirtied_after(inode, *older_than_this))
1252 if (inode_dirtied_after(inode, dirtied_before))
1264 break;
1265 list_move(&inode->i_io_list, &tmp);
1266 moved++;
1267 spin_lock(&inode->i_lock);
1268 if (flags & EXPIRE_DIRTY_ATIME)
1269 inode->i_state |= I_DIRTY_TIME_EXPIRED;
1270 inode->i_state |= I_SYNC_QUEUED;
1271 spin_unlock(&inode->i_lock);

--- 29 unchanged lines hidden (view full) ---

1301 * newly dirtied b_dirty b_io b_more_io
1302 * =============> gf edc BA
1303 * After
1304 * newly dirtied b_dirty b_io b_more_io
1305 * =============> g fBAedc
1306 * |
1307 * +--> dequeue for IO
1308 */
1253 break;
1254 list_move(&inode->i_io_list, &tmp);
1255 moved++;
1256 spin_lock(&inode->i_lock);
1257 if (flags & EXPIRE_DIRTY_ATIME)
1258 inode->i_state |= I_DIRTY_TIME_EXPIRED;
1259 inode->i_state |= I_SYNC_QUEUED;
1260 spin_unlock(&inode->i_lock);

--- 29 unchanged lines hidden (view full) ---

1290 * newly dirtied b_dirty b_io b_more_io
1291 * =============> gf edc BA
1292 * After
1293 * newly dirtied b_dirty b_io b_more_io
1294 * =============> g fBAedc
1295 * |
1296 * +--> dequeue for IO
1297 */
1309static void queue_io(struct bdi_writeback *wb, struct wb_writeback_work *work)
1298static void queue_io(struct bdi_writeback *wb, struct wb_writeback_work *work,
1299 unsigned long dirtied_before)
1310{
1311 int moved;
1300{
1301 int moved;
1302 unsigned long time_expire_jif = dirtied_before;
1312
1313 assert_spin_locked(&wb->list_lock);
1314 list_splice_init(&wb->b_more_io, &wb->b_io);
1303
1304 assert_spin_locked(&wb->list_lock);
1305 list_splice_init(&wb->b_more_io, &wb->b_io);
1315 moved = move_expired_inodes(&wb->b_dirty, &wb->b_io, 0, work);
1306 moved = move_expired_inodes(&wb->b_dirty, &wb->b_io, 0, dirtied_before);
1307 if (!work->for_sync)
1308 time_expire_jif = jiffies - dirtytime_expire_interval * HZ;
1316 moved += move_expired_inodes(&wb->b_dirty_time, &wb->b_io,
1309 moved += move_expired_inodes(&wb->b_dirty_time, &wb->b_io,
1317 EXPIRE_DIRTY_ATIME, work);
1310 EXPIRE_DIRTY_ATIME, time_expire_jif);
1318 if (moved)
1319 wb_io_lists_populated(wb);
1311 if (moved)
1312 wb_io_lists_populated(wb);
1320 trace_writeback_queue_io(wb, work, moved);
1313 trace_writeback_queue_io(wb, work, dirtied_before, moved);
1321}
1322
1323static int write_inode(struct inode *inode, struct writeback_control *wbc)
1324{
1325 int ret;
1326
1327 if (inode->i_sb->s_op->write_inode && !is_bad_inode(inode)) {
1328 trace_writeback_write_inode_start(inode, wbc);

--- 495 unchanged lines hidden (view full) ---

1824 .range_cyclic = 1,
1825 .reason = reason,
1826 };
1827 struct blk_plug plug;
1828
1829 blk_start_plug(&plug);
1830 spin_lock(&wb->list_lock);
1831 if (list_empty(&wb->b_io))
1314}
1315
1316static int write_inode(struct inode *inode, struct writeback_control *wbc)
1317{
1318 int ret;
1319
1320 if (inode->i_sb->s_op->write_inode && !is_bad_inode(inode)) {
1321 trace_writeback_write_inode_start(inode, wbc);

--- 495 unchanged lines hidden (view full) ---

1817 .range_cyclic = 1,
1818 .reason = reason,
1819 };
1820 struct blk_plug plug;
1821
1822 blk_start_plug(&plug);
1823 spin_lock(&wb->list_lock);
1824 if (list_empty(&wb->b_io))
1832 queue_io(wb, &work);
1825 queue_io(wb, &work, jiffies);
1833 __writeback_inodes_wb(wb, &work);
1834 spin_unlock(&wb->list_lock);
1835 blk_finish_plug(&plug);
1836
1837 return nr_pages - work.nr_pages;
1838}
1839
1840/*
1841 * Explicit flushing or periodic writeback of "old" data.
1842 *
1843 * Define "old": the first time one of an inode's pages is dirtied, we mark the
1844 * dirtying-time in the inode's address_space. So this periodic writeback code
1845 * just walks the superblock inode list, writing back any inodes which are
1846 * older than a specific point in time.
1847 *
1848 * Try to run once per dirty_writeback_interval. But if a writeback event
1849 * takes longer than a dirty_writeback_interval interval, then leave a
1850 * one-second gap.
1851 *
1826 __writeback_inodes_wb(wb, &work);
1827 spin_unlock(&wb->list_lock);
1828 blk_finish_plug(&plug);
1829
1830 return nr_pages - work.nr_pages;
1831}
1832
1833/*
1834 * Explicit flushing or periodic writeback of "old" data.
1835 *
1836 * Define "old": the first time one of an inode's pages is dirtied, we mark the
1837 * dirtying-time in the inode's address_space. So this periodic writeback code
1838 * just walks the superblock inode list, writing back any inodes which are
1839 * older than a specific point in time.
1840 *
1841 * Try to run once per dirty_writeback_interval. But if a writeback event
1842 * takes longer than a dirty_writeback_interval interval, then leave a
1843 * one-second gap.
1844 *
1852 * older_than_this takes precedence over nr_to_write. So we'll only write back
1845 * dirtied_before takes precedence over nr_to_write. So we'll only write back
1853 * all dirty pages if they are all attached to "old" mappings.
1854 */
1855static long wb_writeback(struct bdi_writeback *wb,
1856 struct wb_writeback_work *work)
1857{
1858 unsigned long wb_start = jiffies;
1859 long nr_pages = work->nr_pages;
1846 * all dirty pages if they are all attached to "old" mappings.
1847 */
1848static long wb_writeback(struct bdi_writeback *wb,
1849 struct wb_writeback_work *work)
1850{
1851 unsigned long wb_start = jiffies;
1852 long nr_pages = work->nr_pages;
1860 unsigned long oldest_jif;
1853 unsigned long dirtied_before = jiffies;
1861 struct inode *inode;
1862 long progress;
1863 struct blk_plug plug;
1864
1854 struct inode *inode;
1855 long progress;
1856 struct blk_plug plug;
1857
1865 oldest_jif = jiffies;
1866 work->older_than_this = &oldest_jif;
1867
1868 blk_start_plug(&plug);
1869 spin_lock(&wb->list_lock);
1870 for (;;) {
1871 /*
1872 * Stop writeback when nr_pages has been consumed
1873 */
1874 if (work->nr_pages <= 0)
1875 break;

--- 17 unchanged lines hidden (view full) ---

1893
1894 /*
1895 * Kupdate and background works are special and we want to
1896 * include all inodes that need writing. Livelock avoidance is
1897 * handled by these works yielding to any other work so we are
1898 * safe.
1899 */
1900 if (work->for_kupdate) {
1858 blk_start_plug(&plug);
1859 spin_lock(&wb->list_lock);
1860 for (;;) {
1861 /*
1862 * Stop writeback when nr_pages has been consumed
1863 */
1864 if (work->nr_pages <= 0)
1865 break;

--- 17 unchanged lines hidden (view full) ---

1883
1884 /*
1885 * Kupdate and background works are special and we want to
1886 * include all inodes that need writing. Livelock avoidance is
1887 * handled by these works yielding to any other work so we are
1888 * safe.
1889 */
1890 if (work->for_kupdate) {
1901 oldest_jif = jiffies -
1891 dirtied_before = jiffies -
1902 msecs_to_jiffies(dirty_expire_interval * 10);
1903 } else if (work->for_background)
1892 msecs_to_jiffies(dirty_expire_interval * 10);
1893 } else if (work->for_background)
1904 oldest_jif = jiffies;
1894 dirtied_before = jiffies;
1905
1906 trace_writeback_start(wb, work);
1907 if (list_empty(&wb->b_io))
1895
1896 trace_writeback_start(wb, work);
1897 if (list_empty(&wb->b_io))
1908 queue_io(wb, work);
1898 queue_io(wb, work, dirtied_before);
1909 if (work->sb)
1910 progress = writeback_sb_inodes(work->sb, wb, work);
1911 else
1912 progress = __writeback_inodes_wb(wb, work);
1913 trace_writeback_written(wb, work);
1914
1915 wb_update_bandwidth(wb, wb_start);
1916

--- 727 unchanged lines hidden ---
1899 if (work->sb)
1900 progress = writeback_sb_inodes(work->sb, wb, work);
1901 else
1902 progress = __writeback_inodes_wb(wb, work);
1903 trace_writeback_written(wb, work);
1904
1905 wb_update_bandwidth(wb, wb_start);
1906

--- 727 unchanged lines hidden ---