Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * fs/fs-writeback.c |
| 3 | * |
| 4 | * Copyright (C) 2002, Linus Torvalds. |
| 5 | * |
| 6 | * Contains all the functions related to writing back and waiting |
| 7 | * upon dirty inodes against superblocks, and writing back dirty |
| 8 | * pages against inodes. ie: data writeback. Writeout of the |
| 9 | * inode itself is not handled here. |
| 10 | * |
| 11 | * 10Apr2002 akpm@zip.com.au |
| 12 | * Split out of fs/inode.c |
| 13 | * Additions for address_space-based writeback |
| 14 | */ |
| 15 | |
| 16 | #include <linux/kernel.h> |
Jens Axboe | f5ff842 | 2007-09-21 09:19:54 +0200 | [diff] [blame] | 17 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/spinlock.h> |
| 19 | #include <linux/sched.h> |
| 20 | #include <linux/fs.h> |
| 21 | #include <linux/mm.h> |
| 22 | #include <linux/writeback.h> |
| 23 | #include <linux/blkdev.h> |
| 24 | #include <linux/backing-dev.h> |
| 25 | #include <linux/buffer_head.h> |
David Howells | 07f3f05 | 2006-09-30 20:52:18 +0200 | [diff] [blame] | 26 | #include "internal.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
| 28 | /** |
| 29 | * __mark_inode_dirty - internal function |
| 30 | * @inode: inode to mark |
| 31 | * @flags: what kind of dirty (i.e. I_DIRTY_SYNC) |
| 32 | * Mark an inode as dirty. Callers should use mark_inode_dirty or |
| 33 | * mark_inode_dirty_sync. |
| 34 | * |
| 35 | * Put the inode on the super block's dirty list. |
| 36 | * |
| 37 | * CAREFUL! We mark it dirty unconditionally, but move it onto the |
| 38 | * dirty list only if it is hashed or if it refers to a blockdev. |
| 39 | * If it was not hashed, it will never be added to the dirty list |
| 40 | * even if it is later hashed, as it will have been marked dirty already. |
| 41 | * |
| 42 | * In short, make sure you hash any inodes _before_ you start marking |
| 43 | * them dirty. |
| 44 | * |
| 45 | * This function *must* be atomic for the I_DIRTY_PAGES case - |
| 46 | * set_page_dirty() is called under spinlock in several places. |
| 47 | * |
| 48 | * Note that for blockdevs, inode->dirtied_when represents the dirtying time of |
| 49 | * the block-special inode (/dev/hda1) itself. And the ->dirtied_when field of |
| 50 | * the kernel-internal blockdev inode represents the dirtying time of the |
| 51 | * blockdev's pages. This is why for I_DIRTY_PAGES we always use |
| 52 | * page->mapping->host, so the page-dirtying time is recorded in the internal |
| 53 | * blockdev inode. |
| 54 | */ |
| 55 | void __mark_inode_dirty(struct inode *inode, int flags) |
| 56 | { |
| 57 | struct super_block *sb = inode->i_sb; |
| 58 | |
| 59 | /* |
| 60 | * Don't do this for I_DIRTY_PAGES - that doesn't actually |
| 61 | * dirty the inode itself |
| 62 | */ |
| 63 | if (flags & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) { |
| 64 | if (sb->s_op->dirty_inode) |
| 65 | sb->s_op->dirty_inode(inode); |
| 66 | } |
| 67 | |
| 68 | /* |
| 69 | * make sure that changes are seen by all cpus before we test i_state |
| 70 | * -- mikulas |
| 71 | */ |
| 72 | smp_mb(); |
| 73 | |
| 74 | /* avoid the locking if we can */ |
| 75 | if ((inode->i_state & flags) == flags) |
| 76 | return; |
| 77 | |
| 78 | if (unlikely(block_dump)) { |
| 79 | struct dentry *dentry = NULL; |
| 80 | const char *name = "?"; |
| 81 | |
| 82 | if (!list_empty(&inode->i_dentry)) { |
| 83 | dentry = list_entry(inode->i_dentry.next, |
| 84 | struct dentry, d_alias); |
| 85 | if (dentry && dentry->d_name.name) |
| 86 | name = (const char *) dentry->d_name.name; |
| 87 | } |
| 88 | |
| 89 | if (inode->i_ino || strcmp(inode->i_sb->s_id, "bdev")) |
| 90 | printk(KERN_DEBUG |
| 91 | "%s(%d): dirtied inode %lu (%s) on %s\n", |
| 92 | current->comm, current->pid, inode->i_ino, |
| 93 | name, inode->i_sb->s_id); |
| 94 | } |
| 95 | |
| 96 | spin_lock(&inode_lock); |
| 97 | if ((inode->i_state & flags) != flags) { |
| 98 | const int was_dirty = inode->i_state & I_DIRTY; |
| 99 | |
| 100 | inode->i_state |= flags; |
| 101 | |
| 102 | /* |
| 103 | * If the inode is locked, just update its dirty state. |
| 104 | * The unlocker will place the inode on the appropriate |
| 105 | * superblock list, based upon its state. |
| 106 | */ |
| 107 | if (inode->i_state & I_LOCK) |
| 108 | goto out; |
| 109 | |
| 110 | /* |
| 111 | * Only add valid (hashed) inodes to the superblock's |
| 112 | * dirty list. Add blockdev inodes as well. |
| 113 | */ |
| 114 | if (!S_ISBLK(inode->i_mode)) { |
| 115 | if (hlist_unhashed(&inode->i_hash)) |
| 116 | goto out; |
| 117 | } |
| 118 | if (inode->i_state & (I_FREEING|I_CLEAR)) |
| 119 | goto out; |
| 120 | |
| 121 | /* |
| 122 | * If the inode was already on s_dirty or s_io, don't |
| 123 | * reposition it (that would break s_dirty time-ordering). |
| 124 | */ |
| 125 | if (!was_dirty) { |
| 126 | inode->dirtied_when = jiffies; |
| 127 | list_move(&inode->i_list, &sb->s_dirty); |
| 128 | } |
| 129 | } |
| 130 | out: |
| 131 | spin_unlock(&inode_lock); |
| 132 | } |
| 133 | |
| 134 | EXPORT_SYMBOL(__mark_inode_dirty); |
| 135 | |
| 136 | static int write_inode(struct inode *inode, int sync) |
| 137 | { |
| 138 | if (inode->i_sb->s_op->write_inode && !is_bad_inode(inode)) |
| 139 | return inode->i_sb->s_op->write_inode(inode, sync); |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | /* |
Andrew Morton | 6610a0b | 2007-10-16 23:30:32 -0700 | [diff] [blame] | 144 | * Redirty an inode: set its when-it-was dirtied timestamp and move it to the |
| 145 | * furthest end of its superblock's dirty-inode list. |
| 146 | * |
| 147 | * Before stamping the inode's ->dirtied_when, we check to see whether it is |
| 148 | * already the most-recently-dirtied inode on the s_dirty list. If that is |
| 149 | * the case then the inode must have been redirtied while it was being written |
| 150 | * out and we don't reset its dirtied_when. |
| 151 | */ |
| 152 | static void redirty_tail(struct inode *inode) |
| 153 | { |
| 154 | struct super_block *sb = inode->i_sb; |
| 155 | |
| 156 | if (!list_empty(&sb->s_dirty)) { |
| 157 | struct inode *tail_inode; |
| 158 | |
| 159 | tail_inode = list_entry(sb->s_dirty.next, struct inode, i_list); |
| 160 | if (!time_after_eq(inode->dirtied_when, |
| 161 | tail_inode->dirtied_when)) |
| 162 | inode->dirtied_when = jiffies; |
| 163 | } |
| 164 | list_move(&inode->i_list, &sb->s_dirty); |
| 165 | } |
| 166 | |
| 167 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | * Write a single inode's dirty pages and inode data out to disk. |
| 169 | * If `wait' is set, wait on the writeout. |
| 170 | * |
| 171 | * The whole writeout design is quite complex and fragile. We want to avoid |
| 172 | * starvation of particular inodes when others are being redirtied, prevent |
| 173 | * livelocks, etc. |
| 174 | * |
| 175 | * Called under inode_lock. |
| 176 | */ |
| 177 | static int |
| 178 | __sync_single_inode(struct inode *inode, struct writeback_control *wbc) |
| 179 | { |
| 180 | unsigned dirty; |
| 181 | struct address_space *mapping = inode->i_mapping; |
| 182 | struct super_block *sb = inode->i_sb; |
| 183 | int wait = wbc->sync_mode == WB_SYNC_ALL; |
| 184 | int ret; |
| 185 | |
| 186 | BUG_ON(inode->i_state & I_LOCK); |
| 187 | |
| 188 | /* Set I_LOCK, reset I_DIRTY */ |
| 189 | dirty = inode->i_state & I_DIRTY; |
| 190 | inode->i_state |= I_LOCK; |
| 191 | inode->i_state &= ~I_DIRTY; |
| 192 | |
| 193 | spin_unlock(&inode_lock); |
| 194 | |
| 195 | ret = do_writepages(mapping, wbc); |
| 196 | |
| 197 | /* Don't write the inode if only I_DIRTY_PAGES was set */ |
| 198 | if (dirty & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) { |
| 199 | int err = write_inode(inode, wait); |
| 200 | if (ret == 0) |
| 201 | ret = err; |
| 202 | } |
| 203 | |
| 204 | if (wait) { |
| 205 | int err = filemap_fdatawait(mapping); |
| 206 | if (ret == 0) |
| 207 | ret = err; |
| 208 | } |
| 209 | |
| 210 | spin_lock(&inode_lock); |
| 211 | inode->i_state &= ~I_LOCK; |
| 212 | if (!(inode->i_state & I_FREEING)) { |
| 213 | if (!(inode->i_state & I_DIRTY) && |
| 214 | mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) { |
| 215 | /* |
| 216 | * We didn't write back all the pages. nfs_writepages() |
| 217 | * sometimes bales out without doing anything. Redirty |
| 218 | * the inode. It is still on sb->s_io. |
| 219 | */ |
| 220 | if (wbc->for_kupdate) { |
| 221 | /* |
| 222 | * For the kupdate function we leave the inode |
| 223 | * at the head of sb_dirty so it will get more |
| 224 | * writeout as soon as the queue becomes |
| 225 | * uncongested. |
| 226 | */ |
| 227 | inode->i_state |= I_DIRTY_PAGES; |
| 228 | list_move_tail(&inode->i_list, &sb->s_dirty); |
| 229 | } else { |
| 230 | /* |
| 231 | * Otherwise fully redirty the inode so that |
| 232 | * other inodes on this superblock will get some |
| 233 | * writeout. Otherwise heavy writing to one |
| 234 | * file would indefinitely suspend writeout of |
| 235 | * all the other files. |
| 236 | */ |
| 237 | inode->i_state |= I_DIRTY_PAGES; |
| 238 | inode->dirtied_when = jiffies; |
| 239 | list_move(&inode->i_list, &sb->s_dirty); |
| 240 | } |
| 241 | } else if (inode->i_state & I_DIRTY) { |
| 242 | /* |
| 243 | * Someone redirtied the inode while were writing back |
| 244 | * the pages. |
| 245 | */ |
Andrew Morton | 6610a0b | 2007-10-16 23:30:32 -0700 | [diff] [blame] | 246 | redirty_tail(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | } else if (atomic_read(&inode->i_count)) { |
| 248 | /* |
| 249 | * The inode is clean, inuse |
| 250 | */ |
| 251 | list_move(&inode->i_list, &inode_in_use); |
| 252 | } else { |
| 253 | /* |
| 254 | * The inode is clean, unused |
| 255 | */ |
| 256 | list_move(&inode->i_list, &inode_unused); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | wake_up_inode(inode); |
| 260 | return ret; |
| 261 | } |
| 262 | |
| 263 | /* |
Andrea Arcangeli | 7f04c26 | 2005-10-30 15:03:05 -0800 | [diff] [blame] | 264 | * Write out an inode's dirty pages. Called under inode_lock. Either the |
| 265 | * caller has ref on the inode (either via __iget or via syscall against an fd) |
| 266 | * or the inode has I_WILL_FREE set (via generic_forget_inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | */ |
| 268 | static int |
Andrea Arcangeli | 7f04c26 | 2005-10-30 15:03:05 -0800 | [diff] [blame] | 269 | __writeback_single_inode(struct inode *inode, struct writeback_control *wbc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | { |
| 271 | wait_queue_head_t *wqh; |
| 272 | |
Andrea Arcangeli | 7f04c26 | 2005-10-30 15:03:05 -0800 | [diff] [blame] | 273 | if (!atomic_read(&inode->i_count)) |
Andrea Arcangeli | 659603e | 2005-10-31 14:08:54 -0800 | [diff] [blame] | 274 | WARN_ON(!(inode->i_state & (I_WILL_FREE|I_FREEING))); |
Andrea Arcangeli | 7f04c26 | 2005-10-30 15:03:05 -0800 | [diff] [blame] | 275 | else |
| 276 | WARN_ON(inode->i_state & I_WILL_FREE); |
| 277 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | if ((wbc->sync_mode != WB_SYNC_ALL) && (inode->i_state & I_LOCK)) { |
Linus Torvalds | 4b89eed9 | 2007-01-26 12:53:20 -0800 | [diff] [blame] | 279 | struct address_space *mapping = inode->i_mapping; |
| 280 | int ret; |
| 281 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | list_move(&inode->i_list, &inode->i_sb->s_dirty); |
Linus Torvalds | 4b89eed9 | 2007-01-26 12:53:20 -0800 | [diff] [blame] | 283 | |
| 284 | /* |
| 285 | * Even if we don't actually write the inode itself here, |
| 286 | * we can at least start some of the data writeout.. |
| 287 | */ |
| 288 | spin_unlock(&inode_lock); |
| 289 | ret = do_writepages(mapping, wbc); |
| 290 | spin_lock(&inode_lock); |
| 291 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | /* |
| 295 | * It's a data-integrity sync. We must wait. |
| 296 | */ |
| 297 | if (inode->i_state & I_LOCK) { |
| 298 | DEFINE_WAIT_BIT(wq, &inode->i_state, __I_LOCK); |
| 299 | |
| 300 | wqh = bit_waitqueue(&inode->i_state, __I_LOCK); |
| 301 | do { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | spin_unlock(&inode_lock); |
| 303 | __wait_on_bit(wqh, &wq, inode_wait, |
| 304 | TASK_UNINTERRUPTIBLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | spin_lock(&inode_lock); |
| 306 | } while (inode->i_state & I_LOCK); |
| 307 | } |
| 308 | return __sync_single_inode(inode, wbc); |
| 309 | } |
| 310 | |
| 311 | /* |
| 312 | * Write out a superblock's list of dirty inodes. A wait will be performed |
| 313 | * upon no inodes, all inodes or the final one, depending upon sync_mode. |
| 314 | * |
| 315 | * If older_than_this is non-NULL, then only write out inodes which |
| 316 | * had their first dirtying at a time earlier than *older_than_this. |
| 317 | * |
| 318 | * If we're a pdlfush thread, then implement pdflush collision avoidance |
| 319 | * against the entire list. |
| 320 | * |
| 321 | * WB_SYNC_HOLD is a hack for sys_sync(): reattach the inode to sb->s_dirty so |
| 322 | * that it can be located for waiting on in __writeback_single_inode(). |
| 323 | * |
| 324 | * Called under inode_lock. |
| 325 | * |
| 326 | * If `bdi' is non-zero then we're being asked to writeback a specific queue. |
| 327 | * This function assumes that the blockdev superblock's inodes are backed by |
| 328 | * a variety of queues, so all inodes are searched. For other superblocks, |
| 329 | * assume that all inodes are backed by the same queue. |
| 330 | * |
| 331 | * FIXME: this linear search could get expensive with many fileystems. But |
| 332 | * how to fix? We need to go from an address_space to all inodes which share |
| 333 | * a queue with that address_space. (Easy: have a global "dirty superblocks" |
| 334 | * list). |
| 335 | * |
| 336 | * The inodes to be written are parked on sb->s_io. They are moved back onto |
| 337 | * sb->s_dirty as they are selected for writing. This way, none can be missed |
| 338 | * on the writer throttling path, and we get decent balancing between many |
| 339 | * throttled threads: we don't want them all piling up on __wait_on_inode. |
| 340 | */ |
| 341 | static void |
| 342 | sync_sb_inodes(struct super_block *sb, struct writeback_control *wbc) |
| 343 | { |
| 344 | const unsigned long start = jiffies; /* livelock avoidance */ |
| 345 | |
| 346 | if (!wbc->for_kupdate || list_empty(&sb->s_io)) |
| 347 | list_splice_init(&sb->s_dirty, &sb->s_io); |
| 348 | |
| 349 | while (!list_empty(&sb->s_io)) { |
| 350 | struct inode *inode = list_entry(sb->s_io.prev, |
| 351 | struct inode, i_list); |
| 352 | struct address_space *mapping = inode->i_mapping; |
| 353 | struct backing_dev_info *bdi = mapping->backing_dev_info; |
| 354 | long pages_skipped; |
| 355 | |
| 356 | if (!bdi_cap_writeback_dirty(bdi)) { |
Andrew Morton | 9852a0e7 | 2007-10-16 23:30:33 -0700 | [diff] [blame^] | 357 | redirty_tail(inode); |
David Howells | 7b0de42 | 2006-08-29 19:06:07 +0100 | [diff] [blame] | 358 | if (sb_is_blkdev_sb(sb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | /* |
| 360 | * Dirty memory-backed blockdev: the ramdisk |
| 361 | * driver does this. Skip just this inode |
| 362 | */ |
| 363 | continue; |
| 364 | } |
| 365 | /* |
| 366 | * Dirty memory-backed inode against a filesystem other |
| 367 | * than the kernel-internal bdev filesystem. Skip the |
| 368 | * entire superblock. |
| 369 | */ |
| 370 | break; |
| 371 | } |
| 372 | |
| 373 | if (wbc->nonblocking && bdi_write_congested(bdi)) { |
| 374 | wbc->encountered_congestion = 1; |
David Howells | 7b0de42 | 2006-08-29 19:06:07 +0100 | [diff] [blame] | 375 | if (!sb_is_blkdev_sb(sb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | break; /* Skip a congested fs */ |
| 377 | list_move(&inode->i_list, &sb->s_dirty); |
| 378 | continue; /* Skip a congested blockdev */ |
| 379 | } |
| 380 | |
| 381 | if (wbc->bdi && bdi != wbc->bdi) { |
David Howells | 7b0de42 | 2006-08-29 19:06:07 +0100 | [diff] [blame] | 382 | if (!sb_is_blkdev_sb(sb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | break; /* fs has the wrong queue */ |
| 384 | list_move(&inode->i_list, &sb->s_dirty); |
| 385 | continue; /* blockdev has wrong queue */ |
| 386 | } |
| 387 | |
| 388 | /* Was this inode dirtied after sync_sb_inodes was called? */ |
| 389 | if (time_after(inode->dirtied_when, start)) |
| 390 | break; |
| 391 | |
| 392 | /* Was this inode dirtied too recently? */ |
| 393 | if (wbc->older_than_this && time_after(inode->dirtied_when, |
| 394 | *wbc->older_than_this)) |
| 395 | break; |
| 396 | |
| 397 | /* Is another pdflush already flushing this queue? */ |
| 398 | if (current_is_pdflush() && !writeback_acquire(bdi)) |
| 399 | break; |
| 400 | |
| 401 | BUG_ON(inode->i_state & I_FREEING); |
| 402 | __iget(inode); |
| 403 | pages_skipped = wbc->pages_skipped; |
| 404 | __writeback_single_inode(inode, wbc); |
| 405 | if (wbc->sync_mode == WB_SYNC_HOLD) { |
| 406 | inode->dirtied_when = jiffies; |
| 407 | list_move(&inode->i_list, &sb->s_dirty); |
| 408 | } |
| 409 | if (current_is_pdflush()) |
| 410 | writeback_release(bdi); |
| 411 | if (wbc->pages_skipped != pages_skipped) { |
| 412 | /* |
| 413 | * writeback is not making progress due to locked |
| 414 | * buffers. Skip this inode for now. |
| 415 | */ |
| 416 | list_move(&inode->i_list, &sb->s_dirty); |
| 417 | } |
| 418 | spin_unlock(&inode_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | iput(inode); |
OGAWA Hirofumi | 4ffc844 | 2006-03-25 03:07:44 -0800 | [diff] [blame] | 420 | cond_resched(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | spin_lock(&inode_lock); |
| 422 | if (wbc->nr_to_write <= 0) |
| 423 | break; |
| 424 | } |
| 425 | return; /* Leave any unwritten inodes on s_io */ |
| 426 | } |
| 427 | |
| 428 | /* |
| 429 | * Start writeback of dirty pagecache data against all unlocked inodes. |
| 430 | * |
| 431 | * Note: |
| 432 | * We don't need to grab a reference to superblock here. If it has non-empty |
| 433 | * ->s_dirty it's hadn't been killed yet and kill_super() won't proceed |
| 434 | * past sync_inodes_sb() until both the ->s_dirty and ->s_io lists are |
| 435 | * empty. Since __sync_single_inode() regains inode_lock before it finally moves |
| 436 | * inode from superblock lists we are OK. |
| 437 | * |
| 438 | * If `older_than_this' is non-zero then only flush inodes which have a |
| 439 | * flushtime older than *older_than_this. |
| 440 | * |
| 441 | * If `bdi' is non-zero then we will scan the first inode against each |
| 442 | * superblock until we find the matching ones. One group will be the dirty |
| 443 | * inodes against a filesystem. Then when we hit the dummy blockdev superblock, |
| 444 | * sync_sb_inodes will seekout the blockdev which matches `bdi'. Maybe not |
| 445 | * super-efficient but we're about to do a ton of I/O... |
| 446 | */ |
| 447 | void |
| 448 | writeback_inodes(struct writeback_control *wbc) |
| 449 | { |
| 450 | struct super_block *sb; |
| 451 | |
| 452 | might_sleep(); |
| 453 | spin_lock(&sb_lock); |
| 454 | restart: |
| 455 | sb = sb_entry(super_blocks.prev); |
| 456 | for (; sb != sb_entry(&super_blocks); sb = sb_entry(sb->s_list.prev)) { |
| 457 | if (!list_empty(&sb->s_dirty) || !list_empty(&sb->s_io)) { |
| 458 | /* we're making our own get_super here */ |
| 459 | sb->s_count++; |
| 460 | spin_unlock(&sb_lock); |
| 461 | /* |
| 462 | * If we can't get the readlock, there's no sense in |
| 463 | * waiting around, most of the time the FS is going to |
| 464 | * be unmounted by the time it is released. |
| 465 | */ |
| 466 | if (down_read_trylock(&sb->s_umount)) { |
| 467 | if (sb->s_root) { |
| 468 | spin_lock(&inode_lock); |
| 469 | sync_sb_inodes(sb, wbc); |
| 470 | spin_unlock(&inode_lock); |
| 471 | } |
| 472 | up_read(&sb->s_umount); |
| 473 | } |
| 474 | spin_lock(&sb_lock); |
| 475 | if (__put_super_and_need_restart(sb)) |
| 476 | goto restart; |
| 477 | } |
| 478 | if (wbc->nr_to_write <= 0) |
| 479 | break; |
| 480 | } |
| 481 | spin_unlock(&sb_lock); |
| 482 | } |
| 483 | |
| 484 | /* |
| 485 | * writeback and wait upon the filesystem's dirty inodes. The caller will |
| 486 | * do this in two passes - one to write, and one to wait. WB_SYNC_HOLD is |
| 487 | * used to park the written inodes on sb->s_dirty for the wait pass. |
| 488 | * |
| 489 | * A finite limit is set on the number of pages which will be written. |
| 490 | * To prevent infinite livelock of sys_sync(). |
| 491 | * |
| 492 | * We add in the number of potentially dirty inodes, because each inode write |
| 493 | * can dirty pagecache in the underlying blockdev. |
| 494 | */ |
| 495 | void sync_inodes_sb(struct super_block *sb, int wait) |
| 496 | { |
| 497 | struct writeback_control wbc = { |
| 498 | .sync_mode = wait ? WB_SYNC_ALL : WB_SYNC_HOLD, |
OGAWA Hirofumi | 111ebb6 | 2006-06-23 02:03:26 -0700 | [diff] [blame] | 499 | .range_start = 0, |
| 500 | .range_end = LLONG_MAX, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | }; |
Christoph Lameter | b1e7a8f | 2006-06-30 01:55:39 -0700 | [diff] [blame] | 502 | unsigned long nr_dirty = global_page_state(NR_FILE_DIRTY); |
Christoph Lameter | fd39fc85 | 2006-06-30 01:55:40 -0700 | [diff] [blame] | 503 | unsigned long nr_unstable = global_page_state(NR_UNSTABLE_NFS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | |
| 505 | wbc.nr_to_write = nr_dirty + nr_unstable + |
| 506 | (inodes_stat.nr_inodes - inodes_stat.nr_unused) + |
| 507 | nr_dirty + nr_unstable; |
| 508 | wbc.nr_to_write += wbc.nr_to_write / 2; /* Bit more for luck */ |
| 509 | spin_lock(&inode_lock); |
| 510 | sync_sb_inodes(sb, &wbc); |
| 511 | spin_unlock(&inode_lock); |
| 512 | } |
| 513 | |
| 514 | /* |
| 515 | * Rather lame livelock avoidance. |
| 516 | */ |
| 517 | static void set_sb_syncing(int val) |
| 518 | { |
| 519 | struct super_block *sb; |
| 520 | spin_lock(&sb_lock); |
| 521 | sb = sb_entry(super_blocks.prev); |
| 522 | for (; sb != sb_entry(&super_blocks); sb = sb_entry(sb->s_list.prev)) { |
| 523 | sb->s_syncing = val; |
| 524 | } |
| 525 | spin_unlock(&sb_lock); |
| 526 | } |
| 527 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | /** |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 529 | * sync_inodes - writes all inodes to disk |
| 530 | * @wait: wait for completion |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | * |
| 532 | * sync_inodes() goes through each super block's dirty inode list, writes the |
| 533 | * inodes out, waits on the writeout and puts the inodes back on the normal |
| 534 | * list. |
| 535 | * |
| 536 | * This is for sys_sync(). fsync_dev() uses the same algorithm. The subtle |
| 537 | * part of the sync functions is that the blockdev "superblock" is processed |
| 538 | * last. This is because the write_inode() function of a typical fs will |
| 539 | * perform no I/O, but will mark buffers in the blockdev mapping as dirty. |
| 540 | * What we want to do is to perform all that dirtying first, and then write |
| 541 | * back all those inode blocks via the blockdev mapping in one sweep. So the |
| 542 | * additional (somewhat redundant) sync_blockdev() calls here are to make |
| 543 | * sure that really happens. Because if we call sync_inodes_sb(wait=1) with |
| 544 | * outstanding dirty inodes, the writeback goes block-at-a-time within the |
| 545 | * filesystem's write_inode(). This is extremely slow. |
| 546 | */ |
Kirill Korotaev | 618f063 | 2005-06-23 00:09:54 -0700 | [diff] [blame] | 547 | static void __sync_inodes(int wait) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | { |
| 549 | struct super_block *sb; |
| 550 | |
Kirill Korotaev | 618f063 | 2005-06-23 00:09:54 -0700 | [diff] [blame] | 551 | spin_lock(&sb_lock); |
| 552 | restart: |
| 553 | list_for_each_entry(sb, &super_blocks, s_list) { |
| 554 | if (sb->s_syncing) |
| 555 | continue; |
| 556 | sb->s_syncing = 1; |
| 557 | sb->s_count++; |
| 558 | spin_unlock(&sb_lock); |
| 559 | down_read(&sb->s_umount); |
| 560 | if (sb->s_root) { |
| 561 | sync_inodes_sb(sb, wait); |
| 562 | sync_blockdev(sb->s_bdev); |
| 563 | } |
| 564 | up_read(&sb->s_umount); |
| 565 | spin_lock(&sb_lock); |
| 566 | if (__put_super_and_need_restart(sb)) |
| 567 | goto restart; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | } |
Kirill Korotaev | 618f063 | 2005-06-23 00:09:54 -0700 | [diff] [blame] | 569 | spin_unlock(&sb_lock); |
| 570 | } |
| 571 | |
| 572 | void sync_inodes(int wait) |
| 573 | { |
| 574 | set_sb_syncing(0); |
| 575 | __sync_inodes(0); |
| 576 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | if (wait) { |
| 578 | set_sb_syncing(0); |
Kirill Korotaev | 618f063 | 2005-06-23 00:09:54 -0700 | [diff] [blame] | 579 | __sync_inodes(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | } |
| 581 | } |
| 582 | |
| 583 | /** |
Andrea Arcangeli | 7f04c26 | 2005-10-30 15:03:05 -0800 | [diff] [blame] | 584 | * write_inode_now - write an inode to disk |
| 585 | * @inode: inode to write to disk |
| 586 | * @sync: whether the write should be synchronous or not |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | * |
Andrea Arcangeli | 7f04c26 | 2005-10-30 15:03:05 -0800 | [diff] [blame] | 588 | * This function commits an inode to disk immediately if it is dirty. This is |
| 589 | * primarily needed by knfsd. |
| 590 | * |
| 591 | * The caller must either have a ref on the inode or must have set I_WILL_FREE. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | int write_inode_now(struct inode *inode, int sync) |
| 594 | { |
| 595 | int ret; |
| 596 | struct writeback_control wbc = { |
| 597 | .nr_to_write = LONG_MAX, |
| 598 | .sync_mode = WB_SYNC_ALL, |
OGAWA Hirofumi | 111ebb6 | 2006-06-23 02:03:26 -0700 | [diff] [blame] | 599 | .range_start = 0, |
| 600 | .range_end = LLONG_MAX, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | }; |
| 602 | |
| 603 | if (!mapping_cap_writeback_dirty(inode->i_mapping)) |
Andrew Morton | 49364ce | 2005-11-07 00:59:15 -0800 | [diff] [blame] | 604 | wbc.nr_to_write = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | |
| 606 | might_sleep(); |
| 607 | spin_lock(&inode_lock); |
| 608 | ret = __writeback_single_inode(inode, &wbc); |
| 609 | spin_unlock(&inode_lock); |
| 610 | if (sync) |
| 611 | wait_on_inode(inode); |
| 612 | return ret; |
| 613 | } |
| 614 | EXPORT_SYMBOL(write_inode_now); |
| 615 | |
| 616 | /** |
| 617 | * sync_inode - write an inode and its pages to disk. |
| 618 | * @inode: the inode to sync |
| 619 | * @wbc: controls the writeback mode |
| 620 | * |
| 621 | * sync_inode() will write an inode and its pages to disk. It will also |
| 622 | * correctly update the inode on its superblock's dirty inode lists and will |
| 623 | * update inode->i_state. |
| 624 | * |
| 625 | * The caller must have a ref on the inode. |
| 626 | */ |
| 627 | int sync_inode(struct inode *inode, struct writeback_control *wbc) |
| 628 | { |
| 629 | int ret; |
| 630 | |
| 631 | spin_lock(&inode_lock); |
| 632 | ret = __writeback_single_inode(inode, wbc); |
| 633 | spin_unlock(&inode_lock); |
| 634 | return ret; |
| 635 | } |
| 636 | EXPORT_SYMBOL(sync_inode); |
| 637 | |
| 638 | /** |
| 639 | * generic_osync_inode - flush all dirty data for a given inode to disk |
| 640 | * @inode: inode to write |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 641 | * @mapping: the address_space that should be flushed |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | * @what: what to write and wait upon |
| 643 | * |
| 644 | * This can be called by file_write functions for files which have the |
| 645 | * O_SYNC flag set, to flush dirty writes to disk. |
| 646 | * |
| 647 | * @what is a bitmask, specifying which part of the inode's data should be |
Randy Dunlap | b8887e6 | 2005-11-07 01:01:07 -0800 | [diff] [blame] | 648 | * written and waited upon. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | * |
| 650 | * OSYNC_DATA: i_mapping's dirty data |
| 651 | * OSYNC_METADATA: the buffers at i_mapping->private_list |
| 652 | * OSYNC_INODE: the inode itself |
| 653 | */ |
| 654 | |
| 655 | int generic_osync_inode(struct inode *inode, struct address_space *mapping, int what) |
| 656 | { |
| 657 | int err = 0; |
| 658 | int need_write_inode_now = 0; |
| 659 | int err2; |
| 660 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | if (what & OSYNC_DATA) |
| 662 | err = filemap_fdatawrite(mapping); |
| 663 | if (what & (OSYNC_METADATA|OSYNC_DATA)) { |
| 664 | err2 = sync_mapping_buffers(mapping); |
| 665 | if (!err) |
| 666 | err = err2; |
| 667 | } |
| 668 | if (what & OSYNC_DATA) { |
| 669 | err2 = filemap_fdatawait(mapping); |
| 670 | if (!err) |
| 671 | err = err2; |
| 672 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | |
| 674 | spin_lock(&inode_lock); |
| 675 | if ((inode->i_state & I_DIRTY) && |
| 676 | ((what & OSYNC_INODE) || (inode->i_state & I_DIRTY_DATASYNC))) |
| 677 | need_write_inode_now = 1; |
| 678 | spin_unlock(&inode_lock); |
| 679 | |
| 680 | if (need_write_inode_now) { |
| 681 | err2 = write_inode_now(inode, 1); |
| 682 | if (!err) |
| 683 | err = err2; |
| 684 | } |
| 685 | else |
| 686 | wait_on_inode(inode); |
| 687 | |
| 688 | return err; |
| 689 | } |
| 690 | |
| 691 | EXPORT_SYMBOL(generic_osync_inode); |
| 692 | |
| 693 | /** |
| 694 | * writeback_acquire: attempt to get exclusive writeback access to a device |
| 695 | * @bdi: the device's backing_dev_info structure |
| 696 | * |
| 697 | * It is a waste of resources to have more than one pdflush thread blocked on |
| 698 | * a single request queue. Exclusion at the request_queue level is obtained |
| 699 | * via a flag in the request_queue's backing_dev_info.state. |
| 700 | * |
| 701 | * Non-request_queue-backed address_spaces will share default_backing_dev_info, |
| 702 | * unless they implement their own. Which is somewhat inefficient, as this |
| 703 | * may prevent concurrent writeback against multiple devices. |
| 704 | */ |
| 705 | int writeback_acquire(struct backing_dev_info *bdi) |
| 706 | { |
| 707 | return !test_and_set_bit(BDI_pdflush, &bdi->state); |
| 708 | } |
| 709 | |
| 710 | /** |
| 711 | * writeback_in_progress: determine whether there is writeback in progress |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | * @bdi: the device's backing_dev_info structure. |
Randy Dunlap | b8887e6 | 2005-11-07 01:01:07 -0800 | [diff] [blame] | 713 | * |
| 714 | * Determine whether there is writeback in progress against a backing device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | */ |
| 716 | int writeback_in_progress(struct backing_dev_info *bdi) |
| 717 | { |
| 718 | return test_bit(BDI_pdflush, &bdi->state); |
| 719 | } |
| 720 | |
| 721 | /** |
| 722 | * writeback_release: relinquish exclusive writeback access against a device. |
| 723 | * @bdi: the device's backing_dev_info structure |
| 724 | */ |
| 725 | void writeback_release(struct backing_dev_info *bdi) |
| 726 | { |
| 727 | BUG_ON(!writeback_in_progress(bdi)); |
| 728 | clear_bit(BDI_pdflush, &bdi->state); |
| 729 | } |