1 vfs: add support for a lazytime mount option
3 Add a new mount option which enables a new "lazytime" mode. This mode
4 causes atime, mtime, and ctime updates to only be made to the
5 in-memory version of the inode. The on-disk times will only get
6 updated when (a) if the inode needs to be updated for some non-time
7 related change, (b) if userspace calls fsync(), syncfs() or sync(), or
8 (c) just before an undeleted inode is evicted from memory.
10 This is OK according to POSIX because there are no guarantees after a
11 crash unless userspace explicitly requests via a fsync(2) call.
13 For workloads which feature a large number of random write to a
14 preallocated file, the lazytime mount option significantly reduces
15 writes to the inode table. The repeated 4k writes to a single block
16 will result in undesirable stress on flash devices and SMR disk
17 drives. Even on conventional HDD's, the repeated writes to the inode
18 table block will trigger Adjacent Track Interference (ATI) remediation
19 latencies, which very negatively impact long tail latencies --- which
20 is a very big deal for web serving tiers (for example).
22 Google-Bug-Id: 18297052
24 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
26 fs/fs-writeback.c | 52 ++++++++++++++++++++++++++++++++++--------
27 fs/gfs2/file.c | 4 ++--
28 fs/inode.c | 34 +++++++++++++++++++--------
31 fs/proc_namespace.c | 1 +
33 include/linux/backing-dev.h | 1 +
34 include/linux/fs.h | 4 ++++
35 include/trace/events/writeback.h | 53 ++++++++++++++++++++++++++++++++++++++++++-
36 include/uapi/linux/fs.h | 4 +++-
37 mm/backing-dev.c | 10 ++++++--
38 12 files changed, 148 insertions(+), 27 deletions(-)
40 diff --git a/fs/fs-writeback.c b/fs/fs-writeback.c
41 index ef9bef1..29a5dfe 100644
42 --- a/fs/fs-writeback.c
43 +++ b/fs/fs-writeback.c
44 @@ -247,14 +247,19 @@ static bool inode_dirtied_after(struct inode *inode, unsigned long t)
48 +#define EXPIRE_DIRTY_ATIME 0x0001
51 * Move expired (dirtied before work->older_than_this) dirty inodes from
52 * @delaying_queue to @dispatch_queue.
54 static int move_expired_inodes(struct list_head *delaying_queue,
55 struct list_head *dispatch_queue,
57 struct wb_writeback_work *work)
59 + unsigned long *older_than_this = NULL;
60 + unsigned long expire_time;
62 struct list_head *pos, *node;
63 struct super_block *sb = NULL;
64 @@ -262,13 +267,21 @@ static int move_expired_inodes(struct list_head *delaying_queue,
68 + if ((flags & EXPIRE_DIRTY_ATIME) == 0)
69 + older_than_this = work->older_than_this;
70 + else if ((work->reason == WB_REASON_SYNC) == 0) {
71 + expire_time = jiffies - (HZ * 86400);
72 + older_than_this = &expire_time;
74 while (!list_empty(delaying_queue)) {
75 inode = wb_inode(delaying_queue->prev);
76 - if (work->older_than_this &&
77 - inode_dirtied_after(inode, *work->older_than_this))
78 + if (older_than_this &&
79 + inode_dirtied_after(inode, *older_than_this))
81 list_move(&inode->i_wb_list, &tmp);
83 + if (flags & EXPIRE_DIRTY_ATIME)
84 + set_bit(__I_DIRTY_TIME_EXPIRED, &inode->i_state);
85 if (sb_is_blkdev_sb(inode->i_sb))
87 if (sb && sb != inode->i_sb)
88 @@ -309,9 +322,12 @@ out:
89 static void queue_io(struct bdi_writeback *wb, struct wb_writeback_work *work)
93 assert_spin_locked(&wb->list_lock);
94 list_splice_init(&wb->b_more_io, &wb->b_io);
95 - moved = move_expired_inodes(&wb->b_dirty, &wb->b_io, work);
96 + moved = move_expired_inodes(&wb->b_dirty, &wb->b_io, 0, work);
97 + moved += move_expired_inodes(&wb->b_dirty_time, &wb->b_io,
98 + EXPIRE_DIRTY_ATIME, work);
99 trace_writeback_queue_io(wb, work, moved);
102 @@ -435,6 +451,8 @@ static void requeue_inode(struct inode *inode, struct bdi_writeback *wb,
103 * updates after data IO completion.
105 redirty_tail(inode, wb);
106 + } else if (inode->i_state & I_DIRTY_TIME) {
107 + list_move(&inode->i_wb_list, &wb->b_dirty_time);
109 /* The inode is clean. Remove from writeback lists. */
110 list_del_init(&inode->i_wb_list);
111 @@ -482,11 +500,18 @@ __writeback_single_inode(struct inode *inode, struct writeback_control *wbc)
112 /* Clear I_DIRTY_PAGES if we've written out all dirty pages */
113 if (!mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
114 inode->i_state &= ~I_DIRTY_PAGES;
115 - dirty = inode->i_state & I_DIRTY;
116 - inode->i_state &= ~(I_DIRTY_SYNC | I_DIRTY_DATASYNC);
117 + dirty = inode->i_state & (I_DIRTY_SYNC | I_DIRTY_DATASYNC);
118 + if ((dirty && (inode->i_state & I_DIRTY_TIME)) ||
119 + (inode->i_state & I_DIRTY_TIME_EXPIRED)) {
120 + dirty |= I_DIRTY_TIME | I_DIRTY_TIME_EXPIRED;
121 + trace_writeback_lazytime(inode);
123 + inode->i_state &= ~dirty;
124 spin_unlock(&inode->i_lock);
125 + if (dirty & I_DIRTY_TIME)
126 + mark_inode_dirty_sync(inode);
127 /* Don't write the inode if only I_DIRTY_PAGES was set */
128 - if (dirty & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) {
130 int err = write_inode(inode, wbc);
133 @@ -534,7 +559,7 @@ writeback_single_inode(struct inode *inode, struct bdi_writeback *wb,
134 * make sure inode is on some writeback list and leave it there unless
135 * we have completely cleaned the inode.
137 - if (!(inode->i_state & I_DIRTY) &&
138 + if (!(inode->i_state & I_DIRTY_ALL) &&
139 (wbc->sync_mode != WB_SYNC_ALL ||
140 !mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK)))
142 @@ -549,7 +574,7 @@ writeback_single_inode(struct inode *inode, struct bdi_writeback *wb,
143 * If inode is clean, remove it from writeback lists. Otherwise don't
144 * touch it. See comment above for explanation.
146 - if (!(inode->i_state & I_DIRTY))
147 + if (!(inode->i_state & I_DIRTY_ALL))
148 list_del_init(&inode->i_wb_list);
149 spin_unlock(&wb->list_lock);
150 inode_sync_complete(inode);
151 @@ -691,7 +716,7 @@ static long writeback_sb_inodes(struct super_block *sb,
152 wrote += write_chunk - wbc.nr_to_write;
153 spin_lock(&wb->list_lock);
154 spin_lock(&inode->i_lock);
155 - if (!(inode->i_state & I_DIRTY))
156 + if (!(inode->i_state & I_DIRTY_ALL))
158 requeue_inode(inode, wb, &wbc);
159 inode_sync_complete(inode);
160 @@ -1134,6 +1159,8 @@ void __mark_inode_dirty(struct inode *inode, int flags)
161 struct super_block *sb = inode->i_sb;
162 struct backing_dev_info *bdi = NULL;
164 + trace_writeback_mark_inode_dirty(inode, flags);
167 * Don't do this for I_DIRTY_PAGES - that doesn't actually
168 * dirty the inode itself
169 @@ -1164,6 +1191,10 @@ void __mark_inode_dirty(struct inode *inode, int flags)
170 if ((inode->i_state & flags) != flags) {
171 const int was_dirty = inode->i_state & I_DIRTY;
173 + if ((flags & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) &&
174 + (inode->i_state & I_DIRTY_TIME))
175 + inode->i_state &= ~I_DIRTY_TIME;
177 inode->i_state |= flags;
180 @@ -1210,7 +1241,8 @@ void __mark_inode_dirty(struct inode *inode, int flags)
183 inode->dirtied_when = jiffies;
184 - list_move(&inode->i_wb_list, &bdi->wb.b_dirty);
185 + list_move(&inode->i_wb_list, (flags & I_DIRTY_TIME) ?
186 + &bdi->wb.b_dirty_time : &bdi->wb.b_dirty);
187 spin_unlock(&bdi->wb.list_lock);
190 diff --git a/fs/gfs2/file.c b/fs/gfs2/file.c
191 index 80dd44d..e584bf9 100644
194 @@ -654,7 +654,7 @@ static int gfs2_fsync(struct file *file, loff_t start, loff_t end,
196 struct address_space *mapping = file->f_mapping;
197 struct inode *inode = mapping->host;
198 - int sync_state = inode->i_state & I_DIRTY;
199 + int sync_state = inode->i_state & I_DIRTY_ALL;
200 struct gfs2_inode *ip = GFS2_I(inode);
201 int ret = 0, ret1 = 0;
203 @@ -667,7 +667,7 @@ static int gfs2_fsync(struct file *file, loff_t start, loff_t end,
204 if (!gfs2_is_jdata(ip))
205 sync_state &= ~I_DIRTY_PAGES;
207 - sync_state &= ~I_DIRTY_SYNC;
208 + sync_state &= ~(I_DIRTY_SYNC | I_DIRTY_TIME);
211 ret = sync_inode_metadata(inode, 1);
212 diff --git a/fs/inode.c b/fs/inode.c
213 index 26753ba..ceba9c5 100644
217 #include <linux/buffer_head.h> /* for inode_has_buffers */
218 #include <linux/ratelimit.h>
219 #include <linux/list_lru.h>
220 +#include <trace/events/writeback.h>
221 #include "internal.h"
225 * inode_sb_list_lock protects:
226 * sb->s_inodes, inode->i_sb_list
227 * bdi->wb.list_lock protects:
228 - * bdi->wb.b_{dirty,io,more_io}, inode->i_wb_list
229 + * bdi->wb.b_{dirty,io,more_io,dirty_time}, inode->i_wb_list
230 * inode_hash_lock protects:
231 * inode_hashtable, inode->i_hash
233 @@ -414,7 +415,8 @@ static void inode_lru_list_add(struct inode *inode)
235 void inode_add_lru(struct inode *inode)
237 - if (!(inode->i_state & (I_DIRTY | I_SYNC | I_FREEING | I_WILL_FREE)) &&
238 + if (!(inode->i_state & (I_DIRTY_ALL | I_SYNC |
239 + I_FREEING | I_WILL_FREE)) &&
240 !atomic_read(&inode->i_count) && inode->i_sb->s_flags & MS_ACTIVE)
241 inode_lru_list_add(inode);
243 @@ -645,7 +647,7 @@ int invalidate_inodes(struct super_block *sb, bool kill_dirty)
244 spin_unlock(&inode->i_lock);
247 - if (inode->i_state & I_DIRTY && !kill_dirty) {
248 + if (inode->i_state & I_DIRTY_ALL && !kill_dirty) {
249 spin_unlock(&inode->i_lock);
252 @@ -1430,11 +1432,20 @@ static void iput_final(struct inode *inode)
254 void iput(struct inode *inode)
257 - BUG_ON(inode->i_state & I_CLEAR);
259 - if (atomic_dec_and_lock(&inode->i_count, &inode->i_lock))
263 + BUG_ON(inode->i_state & I_CLEAR);
265 + if (atomic_dec_and_lock(&inode->i_count, &inode->i_lock)) {
266 + if (inode->i_nlink && (inode->i_state & I_DIRTY_TIME)) {
267 + atomic_inc(&inode->i_count);
268 + inode->i_state &= ~I_DIRTY_TIME;
269 + spin_unlock(&inode->i_lock);
270 + trace_writeback_lazytime_iput(inode);
271 + mark_inode_dirty_sync(inode);
278 @@ -1510,7 +1521,12 @@ static int update_time(struct inode *inode, struct timespec *time, int flags)
279 inode->i_ctime = *time;
281 inode->i_mtime = *time;
282 - mark_inode_dirty_sync(inode);
284 + if ((inode->i_sb->s_flags & MS_LAZYTIME) && !(flags & S_VERSION) &&
285 + !(inode->i_state & I_DIRTY))
286 + __mark_inode_dirty(inode, I_DIRTY_TIME);
288 + __mark_inode_dirty(inode, I_DIRTY_SYNC);
292 diff --git a/fs/jfs/file.c b/fs/jfs/file.c
293 index 33aa0cc..10815f8 100644
296 @@ -39,7 +39,7 @@ int jfs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
299 mutex_lock(&inode->i_mutex);
300 - if (!(inode->i_state & I_DIRTY) ||
301 + if (!(inode->i_state & I_DIRTY_ALL) ||
302 (datasync && !(inode->i_state & I_DIRTY_DATASYNC))) {
303 /* Make sure committed changes hit the disk */
304 jfs_flush_journal(JFS_SBI(inode->i_sb)->log, 1);
305 diff --git a/fs/libfs.c b/fs/libfs.c
306 index 171d284..7cb9cef 100644
309 @@ -948,7 +948,7 @@ int __generic_file_fsync(struct file *file, loff_t start, loff_t end,
311 mutex_lock(&inode->i_mutex);
312 ret = sync_mapping_buffers(inode->i_mapping);
313 - if (!(inode->i_state & I_DIRTY))
314 + if (!(inode->i_state & I_DIRTY_ALL))
316 if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
318 diff --git a/fs/proc_namespace.c b/fs/proc_namespace.c
319 index 73ca174..f98234a 100644
320 --- a/fs/proc_namespace.c
321 +++ b/fs/proc_namespace.c
322 @@ -44,6 +44,7 @@ static int show_sb_opts(struct seq_file *m, struct super_block *sb)
323 { MS_SYNCHRONOUS, ",sync" },
324 { MS_DIRSYNC, ",dirsync" },
325 { MS_MANDLOCK, ",mand" },
326 + { MS_LAZYTIME, ",lazytime" },
329 const struct proc_fs_info *fs_infop;
330 diff --git a/fs/sync.c b/fs/sync.c
331 index bdc729d..6ac7bf0 100644
334 @@ -177,8 +177,16 @@ SYSCALL_DEFINE1(syncfs, int, fd)
336 int vfs_fsync_range(struct file *file, loff_t start, loff_t end, int datasync)
338 + struct inode *inode = file->f_mapping->host;
340 if (!file->f_op->fsync)
342 + if (!datasync && (inode->i_state & I_DIRTY_TIME)) {
343 + spin_lock(&inode->i_lock);
344 + inode->i_state &= ~I_DIRTY_TIME;
345 + spin_unlock(&inode->i_lock);
346 + mark_inode_dirty_sync(inode);
348 return file->f_op->fsync(file, start, end, datasync);
350 EXPORT_SYMBOL(vfs_fsync_range);
351 diff --git a/include/linux/backing-dev.h b/include/linux/backing-dev.h
352 index 5da6012..4cdf733 100644
353 --- a/include/linux/backing-dev.h
354 +++ b/include/linux/backing-dev.h
355 @@ -55,6 +55,7 @@ struct bdi_writeback {
356 struct list_head b_dirty; /* dirty inodes */
357 struct list_head b_io; /* parked for writeback */
358 struct list_head b_more_io; /* parked for more writeback */
359 + struct list_head b_dirty_time; /* time stamps are dirty */
360 spinlock_t list_lock; /* protects the b_* lists */
363 diff --git a/include/linux/fs.h b/include/linux/fs.h
364 index 9ab779e..bb4e878 100644
365 --- a/include/linux/fs.h
366 +++ b/include/linux/fs.h
367 @@ -1720,8 +1720,12 @@ struct super_operations {
368 #define __I_DIO_WAKEUP 9
369 #define I_DIO_WAKEUP (1 << I_DIO_WAKEUP)
370 #define I_LINKABLE (1 << 10)
371 +#define I_DIRTY_TIME (1 << 11)
372 +#define __I_DIRTY_TIME_EXPIRED 12
373 +#define I_DIRTY_TIME_EXPIRED (1 << __I_DIRTY_TIME_EXPIRED)
375 #define I_DIRTY (I_DIRTY_SYNC | I_DIRTY_DATASYNC | I_DIRTY_PAGES)
376 +#define I_DIRTY_ALL (I_DIRTY | I_DIRTY_TIME)
378 extern void __mark_inode_dirty(struct inode *, int);
379 static inline void mark_inode_dirty(struct inode *inode)
380 diff --git a/include/trace/events/writeback.h b/include/trace/events/writeback.h
381 index cee02d6..1a90d28 100644
382 --- a/include/trace/events/writeback.h
383 +++ b/include/trace/events/writeback.h
385 {I_FREEING, "I_FREEING"}, \
386 {I_CLEAR, "I_CLEAR"}, \
387 {I_SYNC, "I_SYNC"}, \
388 + {I_DIRTY_TIME, "I_DIRTY_TIME"}, \
389 + {I_DIRTY_TIME_EXPIRED, "I_DIRTY_TIME_EXPIRED"}, \
390 {I_REFERENCED, "I_REFERENCED"} \
393 @@ -68,6 +70,7 @@ DECLARE_EVENT_CLASS(writeback_dirty_inode_template,
395 __array(char, name, 32)
396 __field(unsigned long, ino)
397 + __field(unsigned long, state)
398 __field(unsigned long, flags)
401 @@ -78,16 +81,25 @@ DECLARE_EVENT_CLASS(writeback_dirty_inode_template,
402 strncpy(__entry->name,
403 bdi->dev ? dev_name(bdi->dev) : "(unknown)", 32);
404 __entry->ino = inode->i_ino;
405 + __entry->state = inode->i_state;
406 __entry->flags = flags;
409 - TP_printk("bdi %s: ino=%lu flags=%s",
410 + TP_printk("bdi %s: ino=%lu state=%s flags=%s",
413 + show_inode_state(__entry->state),
414 show_inode_state(__entry->flags)
418 +DEFINE_EVENT(writeback_dirty_inode_template, writeback_mark_inode_dirty,
420 + TP_PROTO(struct inode *inode, int flags),
422 + TP_ARGS(inode, flags)
425 DEFINE_EVENT(writeback_dirty_inode_template, writeback_dirty_inode_start,
427 TP_PROTO(struct inode *inode, int flags),
428 @@ -598,6 +610,45 @@ DEFINE_EVENT(writeback_single_inode_template, writeback_single_inode,
429 TP_ARGS(inode, wbc, nr_to_write)
432 +DECLARE_EVENT_CLASS(writeback_lazytime_template,
433 + TP_PROTO(struct inode *inode),
438 + __field( dev_t, dev )
439 + __field(unsigned long, ino )
440 + __field(unsigned long, state )
441 + __field( __u16, mode )
442 + __field(unsigned long, dirtied_when )
446 + __entry->dev = inode->i_sb->s_dev;
447 + __entry->ino = inode->i_ino;
448 + __entry->state = inode->i_state;
449 + __entry->mode = inode->i_mode;
450 + __entry->dirtied_when = inode->dirtied_when;
453 + TP_printk("dev %d,%d ino %lu dirtied %lu state %s mode 0%o",
454 + MAJOR(__entry->dev), MINOR(__entry->dev),
455 + __entry->ino, __entry->dirtied_when,
456 + show_inode_state(__entry->state), __entry->mode)
459 +DEFINE_EVENT(writeback_lazytime_template, writeback_lazytime,
460 + TP_PROTO(struct inode *inode),
465 +DEFINE_EVENT(writeback_lazytime_template, writeback_lazytime_iput,
466 + TP_PROTO(struct inode *inode),
471 #endif /* _TRACE_WRITEBACK_H */
473 /* This part must be outside protection */
474 diff --git a/include/uapi/linux/fs.h b/include/uapi/linux/fs.h
475 index 3735fa0..9b964a5 100644
476 --- a/include/uapi/linux/fs.h
477 +++ b/include/uapi/linux/fs.h
478 @@ -90,6 +90,7 @@ struct inodes_stat_t {
479 #define MS_KERNMOUNT (1<<22) /* this is a kern_mount call */
480 #define MS_I_VERSION (1<<23) /* Update inode I_version field */
481 #define MS_STRICTATIME (1<<24) /* Always perform atime updates */
482 +#define MS_LAZYTIME (1<<25) /* Update the on-disk [acm]times lazily */
484 /* These sb flags are internal to the kernel */
485 #define MS_NOSEC (1<<28)
486 @@ -100,7 +101,8 @@ struct inodes_stat_t {
488 * Superblock flags that can be altered by MS_REMOUNT
490 -#define MS_RMT_MASK (MS_RDONLY|MS_SYNCHRONOUS|MS_MANDLOCK|MS_I_VERSION)
491 +#define MS_RMT_MASK (MS_RDONLY|MS_SYNCHRONOUS|MS_MANDLOCK|MS_I_VERSION|\
495 * Old magic mount flag and mask
496 diff --git a/mm/backing-dev.c b/mm/backing-dev.c
497 index 0ae0df5..915feea 100644
498 --- a/mm/backing-dev.c
499 +++ b/mm/backing-dev.c
500 @@ -69,10 +69,10 @@ static int bdi_debug_stats_show(struct seq_file *m, void *v)
501 unsigned long background_thresh;
502 unsigned long dirty_thresh;
503 unsigned long bdi_thresh;
504 - unsigned long nr_dirty, nr_io, nr_more_io;
505 + unsigned long nr_dirty, nr_io, nr_more_io, nr_dirty_time;
508 - nr_dirty = nr_io = nr_more_io = 0;
509 + nr_dirty = nr_io = nr_more_io = nr_dirty_time = 0;
510 spin_lock(&wb->list_lock);
511 list_for_each_entry(inode, &wb->b_dirty, i_wb_list)
513 @@ -80,6 +80,9 @@ static int bdi_debug_stats_show(struct seq_file *m, void *v)
515 list_for_each_entry(inode, &wb->b_more_io, i_wb_list)
517 + list_for_each_entry(inode, &wb->b_dirty_time, i_wb_list)
518 + if (inode->i_state & I_DIRTY_TIME)
520 spin_unlock(&wb->list_lock);
522 global_dirty_limits(&background_thresh, &dirty_thresh);
523 @@ -98,6 +101,7 @@ static int bdi_debug_stats_show(struct seq_file *m, void *v)
527 + "b_dirty_time: %10lu\n"
530 (unsigned long) K(bdi_stat(bdi, BDI_WRITEBACK)),
531 @@ -111,6 +115,7 @@ static int bdi_debug_stats_show(struct seq_file *m, void *v)
536 !list_empty(&bdi->bdi_list), bdi->state);
539 @@ -418,6 +423,7 @@ static void bdi_wb_init(struct bdi_writeback *wb, struct backing_dev_info *bdi)
540 INIT_LIST_HEAD(&wb->b_dirty);
541 INIT_LIST_HEAD(&wb->b_io);
542 INIT_LIST_HEAD(&wb->b_more_io);
543 + INIT_LIST_HEAD(&wb->b_dirty_time);
544 spin_lock_init(&wb->list_lock);
545 INIT_DELAYED_WORK(&wb->dwork, bdi_writeback_workfn);