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 99.9 percentile latencies ---
20 which 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 | 55 +++++++++++++++++++++++++++++++++++++++++++++++++------
27 fs/inode.c | 42 ++++++++++++++++++++++++++++++++++++------
29 fs/logfs/readwrite.c | 2 +-
32 fs/proc_namespace.c | 1 +
33 fs/sync.c | 8 ++++++++
34 fs/ufs/truncate.c | 2 +-
35 include/linux/backing-dev.h | 1 +
36 include/linux/fs.h | 11 +++++++++--
37 include/uapi/linux/fs.h | 1 +
38 mm/backing-dev.c | 9 +++++++--
39 13 files changed, 117 insertions(+), 21 deletions(-)
41 diff --git a/fs/fs-writeback.c b/fs/fs-writeback.c
42 index ef9bef1..ef8c5d8 100644
43 --- a/fs/fs-writeback.c
44 +++ b/fs/fs-writeback.c
45 @@ -397,7 +397,7 @@ static void requeue_inode(struct inode *inode, struct bdi_writeback *wb,
46 * shot. If still dirty, it will be redirty_tail()'ed below. Update
47 * the dirty time to prevent enqueue and sync it again.
49 - if ((inode->i_state & I_DIRTY) &&
50 + if ((inode->i_state & I_DIRTY_WB) &&
51 (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages))
52 inode->dirtied_when = jiffies;
54 @@ -428,13 +428,15 @@ static void requeue_inode(struct inode *inode, struct bdi_writeback *wb,
56 redirty_tail(inode, wb);
58 - } else if (inode->i_state & I_DIRTY) {
59 + } else if (inode->i_state & I_DIRTY_WB) {
61 * Filesystems can dirty the inode during writeback operations,
62 * such as delayed allocation during submission or metadata
63 * updates after data IO completion.
65 redirty_tail(inode, wb);
66 + } else if (inode->i_state & I_DIRTY_TIME) {
67 + list_move(&inode->i_wb_list, &wb->b_dirty_time);
69 /* The inode is clean. Remove from writeback lists. */
70 list_del_init(&inode->i_wb_list);
71 @@ -482,11 +484,11 @@ __writeback_single_inode(struct inode *inode, struct writeback_control *wbc)
72 /* Clear I_DIRTY_PAGES if we've written out all dirty pages */
73 if (!mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
74 inode->i_state &= ~I_DIRTY_PAGES;
75 - dirty = inode->i_state & I_DIRTY;
76 - inode->i_state &= ~(I_DIRTY_SYNC | I_DIRTY_DATASYNC);
77 + dirty = inode->i_state & I_DIRTY_INODE;
78 + inode->i_state &= ~I_DIRTY_INODE;
79 spin_unlock(&inode->i_lock);
80 /* Don't write the inode if only I_DIRTY_PAGES was set */
81 - if (dirty & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) {
83 int err = write_inode(inode, wbc);
86 @@ -1162,7 +1164,7 @@ void __mark_inode_dirty(struct inode *inode, int flags)
88 spin_lock(&inode->i_lock);
89 if ((inode->i_state & flags) != flags) {
90 - const int was_dirty = inode->i_state & I_DIRTY;
91 + const int was_dirty = inode->i_state & I_DIRTY_WB;
93 inode->i_state |= flags;
95 @@ -1224,6 +1226,24 @@ out_unlock_inode:
97 EXPORT_SYMBOL(__mark_inode_dirty);
99 +void inode_requeue_dirtytime(struct inode *inode)
101 + struct backing_dev_info *bdi = inode_to_bdi(inode);
103 + spin_lock(&bdi->wb.list_lock);
104 + spin_lock(&inode->i_lock);
105 + if ((inode->i_state & I_DIRTY_WB) == 0) {
106 + if (inode->i_state & I_DIRTY_TIME)
107 + list_move(&inode->i_wb_list, &bdi->wb.b_dirty_time);
109 + list_del_init(&inode->i_wb_list);
111 + spin_unlock(&inode->i_lock);
112 + spin_unlock(&bdi->wb.list_lock);
115 +EXPORT_SYMBOL(inode_requeue_dirtytime);
117 static void wait_sb_inodes(struct super_block *sb)
119 struct inode *inode, *old_inode = NULL;
120 @@ -1277,6 +1297,28 @@ static void wait_sb_inodes(struct super_block *sb)
125 + * Take all of the indoes on the dirty_time list, and mark them as
126 + * dirty, so they will be written out.
128 +static void flush_sb_dirty_time(struct super_block *sb)
130 + struct bdi_writeback *wb = &sb->s_bdi->wb;
133 + spin_lock(&wb->list_lock);
134 + list_cut_position(&tmp, &wb->b_dirty_time, wb->b_dirty_time.prev);
135 + while (!list_empty(&tmp)) {
136 + struct inode *inode = wb_inode(tmp.prev);
138 + list_del_init(&inode->i_wb_list);
139 + spin_unlock(&wb->list_lock);
140 + mark_inode_dirty_sync(inode);
141 + spin_lock(&wb->list_lock);
143 + spin_unlock(&wb->list_lock);
147 * writeback_inodes_sb_nr - writeback dirty inodes from given super_block
148 * @sb: the superblock
149 @@ -1388,6 +1430,7 @@ void sync_inodes_sb(struct super_block *sb)
151 WARN_ON(!rwsem_is_locked(&sb->s_umount));
153 + flush_sb_dirty_time(sb);
154 bdi_queue_work(sb->s_bdi, &work);
155 wait_for_completion(&done);
157 diff --git a/fs/inode.c b/fs/inode.c
158 index 26753ba..6de5923 100644
162 * inode_sb_list_lock protects:
163 * sb->s_inodes, inode->i_sb_list
164 * bdi->wb.list_lock protects:
165 - * bdi->wb.b_{dirty,io,more_io}, inode->i_wb_list
166 + * bdi->wb.b_{dirty,io,more_io,dirty_time}, inode->i_wb_list
167 * inode_hash_lock protects:
168 * inode_hashtable, inode->i_hash
170 @@ -1430,11 +1430,20 @@ static void iput_final(struct inode *inode)
172 void iput(struct inode *inode)
175 - BUG_ON(inode->i_state & I_CLEAR);
177 - if (atomic_dec_and_lock(&inode->i_count, &inode->i_lock))
181 + BUG_ON(inode->i_state & I_CLEAR);
183 + if (atomic_dec_and_lock(&inode->i_count, &inode->i_lock)) {
184 + if (inode->i_nlink && (inode->i_state & I_DIRTY_TIME)) {
185 + atomic_inc(&inode->i_count);
186 + inode->i_state &= ~I_DIRTY_TIME;
187 + spin_unlock(&inode->i_lock);
188 + if (inode->i_sb->s_op->write_inode)
189 + mark_inode_dirty_sync(inode);
196 @@ -1510,6 +1519,27 @@ static int update_time(struct inode *inode, struct timespec *time, int flags)
197 inode->i_ctime = *time;
199 inode->i_mtime = *time;
201 + if ((inode->i_sb->s_flags & MS_LAZYTIME) &&
202 + !(flags & S_VERSION) &&
203 + !(inode->i_state & (I_DIRTY_SYNC | I_DIRTY_DATASYNC))) {
204 + if (inode->i_state & I_DIRTY_TIME)
206 + spin_lock(&inode->i_lock);
207 + if (inode->i_state & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) {
208 + spin_unlock(&inode->i_lock);
211 + if (inode->i_state & I_DIRTY_TIME) {
212 + spin_unlock(&inode->i_lock);
215 + inode->i_state |= I_DIRTY_TIME;
216 + spin_unlock(&inode->i_lock);
217 + inode_requeue_dirtytime(inode);
221 mark_inode_dirty_sync(inode);
224 diff --git a/fs/libfs.c b/fs/libfs.c
225 index 171d284..b9923b2 100644
228 @@ -1066,7 +1066,7 @@ struct inode *alloc_anon_inode(struct super_block *s)
229 * list because mark_inode_dirty() will think
230 * that it already _is_ on the dirty list.
232 - inode->i_state = I_DIRTY;
233 + inode->i_state = I_DIRTY_WB;
234 inode->i_mode = S_IRUSR | S_IWUSR;
235 inode->i_uid = current_fsuid();
236 inode->i_gid = current_fsgid();
237 diff --git a/fs/logfs/readwrite.c b/fs/logfs/readwrite.c
238 index 380d86e..5521842 100644
239 --- a/fs/logfs/readwrite.c
240 +++ b/fs/logfs/readwrite.c
241 @@ -2187,7 +2187,7 @@ void logfs_evict_inode(struct inode *inode)
242 * aliases, which are moved back. No write to the medium happens.
244 /* Only deleted files may be dirty at this point */
245 - BUG_ON(inode->i_state & I_DIRTY && inode->i_nlink);
246 + BUG_ON(inode->i_state & I_DIRTY_WB && inode->i_nlink);
249 if ((logfs_super(sb)->s_flags & LOGFS_SB_FLAG_SHUTDOWN)) {
250 diff --git a/fs/nfsd/vfs.c b/fs/nfsd/vfs.c
251 index 989129e..818c6fa 100644
254 @@ -915,7 +915,7 @@ static int wait_for_concurrent_writes(struct file *file)
255 dprintk("nfsd: write resume %d\n", task_pid_nr(current));
258 - if (inode->i_state & I_DIRTY) {
259 + if (inode->i_state & I_DIRTY_WB) {
260 dprintk("nfsd: write sync %d\n", task_pid_nr(current));
261 err = vfs_fsync(file, 0);
263 diff --git a/fs/pipe.c b/fs/pipe.c
264 index 21981e5..fc9b923 100644
267 @@ -660,7 +660,7 @@ static struct inode * get_pipe_inode(void)
268 * list because "mark_inode_dirty()" will think
269 * that it already _is_ on the dirty list.
271 - inode->i_state = I_DIRTY;
272 + inode->i_state = I_DIRTY_WB;
273 inode->i_mode = S_IFIFO | S_IRUSR | S_IWUSR;
274 inode->i_uid = current_fsuid();
275 inode->i_gid = current_fsgid();
276 diff --git a/fs/proc_namespace.c b/fs/proc_namespace.c
277 index 73ca174..f98234a 100644
278 --- a/fs/proc_namespace.c
279 +++ b/fs/proc_namespace.c
280 @@ -44,6 +44,7 @@ static int show_sb_opts(struct seq_file *m, struct super_block *sb)
281 { MS_SYNCHRONOUS, ",sync" },
282 { MS_DIRSYNC, ",dirsync" },
283 { MS_MANDLOCK, ",mand" },
284 + { MS_LAZYTIME, ",lazytime" },
287 const struct proc_fs_info *fs_infop;
288 diff --git a/fs/sync.c b/fs/sync.c
289 index bdc729d..6ac7bf0 100644
292 @@ -177,8 +177,16 @@ SYSCALL_DEFINE1(syncfs, int, fd)
294 int vfs_fsync_range(struct file *file, loff_t start, loff_t end, int datasync)
296 + struct inode *inode = file->f_mapping->host;
298 if (!file->f_op->fsync)
300 + if (!datasync && (inode->i_state & I_DIRTY_TIME)) {
301 + spin_lock(&inode->i_lock);
302 + inode->i_state &= ~I_DIRTY_TIME;
303 + spin_unlock(&inode->i_lock);
304 + mark_inode_dirty_sync(inode);
306 return file->f_op->fsync(file, start, end, datasync);
308 EXPORT_SYMBOL(vfs_fsync_range);
309 diff --git a/fs/ufs/truncate.c b/fs/ufs/truncate.c
310 index f04f89f..1d00a09 100644
311 --- a/fs/ufs/truncate.c
312 +++ b/fs/ufs/truncate.c
313 @@ -477,7 +477,7 @@ int ufs_truncate(struct inode *inode, loff_t old_i_size)
314 retry |= ufs_trunc_tindirect (inode);
317 - if (IS_SYNC(inode) && (inode->i_state & I_DIRTY))
318 + if (IS_SYNC(inode) && (inode->i_state & I_DIRTY_WB))
319 ufs_sync_inode (inode);
322 diff --git a/include/linux/backing-dev.h b/include/linux/backing-dev.h
323 index 5da6012..4cdf733 100644
324 --- a/include/linux/backing-dev.h
325 +++ b/include/linux/backing-dev.h
326 @@ -55,6 +55,7 @@ struct bdi_writeback {
327 struct list_head b_dirty; /* dirty inodes */
328 struct list_head b_io; /* parked for writeback */
329 struct list_head b_more_io; /* parked for more writeback */
330 + struct list_head b_dirty_time; /* time stamps are dirty */
331 spinlock_t list_lock; /* protects the b_* lists */
334 diff --git a/include/linux/fs.h b/include/linux/fs.h
335 index 9ab779e..7932482 100644
336 --- a/include/linux/fs.h
337 +++ b/include/linux/fs.h
338 @@ -1720,19 +1720,26 @@ struct super_operations {
339 #define __I_DIO_WAKEUP 9
340 #define I_DIO_WAKEUP (1 << I_DIO_WAKEUP)
341 #define I_LINKABLE (1 << 10)
342 +#define I_DIRTY_TIME (1 << 11)
344 -#define I_DIRTY (I_DIRTY_SYNC | I_DIRTY_DATASYNC | I_DIRTY_PAGES)
345 +/* Inode should be on the b_dirty/b_io/b_more_io lists */
346 +#define I_DIRTY_WB (I_DIRTY_SYNC | I_DIRTY_DATASYNC | I_DIRTY_PAGES)
347 +/* Inode should be on the b_dirty/b_io/b_more_io/b_dirty_time lists */
348 +#define I_DIRTY (I_DIRTY_SYNC | I_DIRTY_DATASYNC | I_DIRTY_PAGES | I_DIRTY_TIME)
349 +/* The inode itself is dirty */
350 +#define I_DIRTY_INODE (I_DIRTY_SYNC | I_DIRTY_DATASYNC | I_DIRTY_TIME)
352 extern void __mark_inode_dirty(struct inode *, int);
353 static inline void mark_inode_dirty(struct inode *inode)
355 - __mark_inode_dirty(inode, I_DIRTY);
356 + __mark_inode_dirty(inode, I_DIRTY_WB);
359 static inline void mark_inode_dirty_sync(struct inode *inode)
361 __mark_inode_dirty(inode, I_DIRTY_SYNC);
363 +extern void inode_requeue_dirtytime(struct inode *);
365 extern void inc_nlink(struct inode *inode);
366 extern void drop_nlink(struct inode *inode);
367 diff --git a/include/uapi/linux/fs.h b/include/uapi/linux/fs.h
368 index 3735fa0..cc9713a 100644
369 --- a/include/uapi/linux/fs.h
370 +++ b/include/uapi/linux/fs.h
371 @@ -90,6 +90,7 @@ struct inodes_stat_t {
372 #define MS_KERNMOUNT (1<<22) /* this is a kern_mount call */
373 #define MS_I_VERSION (1<<23) /* Update inode I_version field */
374 #define MS_STRICTATIME (1<<24) /* Always perform atime updates */
375 +#define MS_LAZYTIME (1<<25) /* Update the on-disk [acm]times lazily */
377 /* These sb flags are internal to the kernel */
378 #define MS_NOSEC (1<<28)
379 diff --git a/mm/backing-dev.c b/mm/backing-dev.c
380 index 0ae0df5..14851fe 100644
381 --- a/mm/backing-dev.c
382 +++ b/mm/backing-dev.c
383 @@ -69,10 +69,10 @@ static int bdi_debug_stats_show(struct seq_file *m, void *v)
384 unsigned long background_thresh;
385 unsigned long dirty_thresh;
386 unsigned long bdi_thresh;
387 - unsigned long nr_dirty, nr_io, nr_more_io;
388 + unsigned long nr_dirty, nr_io, nr_more_io, nr_dirty_time;
391 - nr_dirty = nr_io = nr_more_io = 0;
392 + nr_dirty = nr_io = nr_more_io = nr_dirty_time = 0;
393 spin_lock(&wb->list_lock);
394 list_for_each_entry(inode, &wb->b_dirty, i_wb_list)
396 @@ -80,6 +80,8 @@ static int bdi_debug_stats_show(struct seq_file *m, void *v)
398 list_for_each_entry(inode, &wb->b_more_io, i_wb_list)
400 + list_for_each_entry(inode, &wb->b_dirty_time, i_wb_list)
402 spin_unlock(&wb->list_lock);
404 global_dirty_limits(&background_thresh, &dirty_thresh);
405 @@ -98,6 +100,7 @@ static int bdi_debug_stats_show(struct seq_file *m, void *v)
409 + "b_dirty_time: %10lu\n"
412 (unsigned long) K(bdi_stat(bdi, BDI_WRITEBACK)),
413 @@ -111,6 +114,7 @@ static int bdi_debug_stats_show(struct seq_file *m, void *v)
418 !list_empty(&bdi->bdi_list), bdi->state);
421 @@ -418,6 +422,7 @@ static void bdi_wb_init(struct bdi_writeback *wb, struct backing_dev_info *bdi)
422 INIT_LIST_HEAD(&wb->b_dirty);
423 INIT_LIST_HEAD(&wb->b_io);
424 INIT_LIST_HEAD(&wb->b_more_io);
425 + INIT_LIST_HEAD(&wb->b_dirty_time);
426 spin_lock_init(&wb->list_lock);
427 INIT_DELAYED_WORK(&wb->dwork, bdi_writeback_workfn);