linux-bk/fs/fs-writeback.c
<<
>>
Prefs
   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>
  17#include <linux/spinlock.h>
  18#include <linux/sched.h>
  19#include <linux/fs.h>
  20#include <linux/mm.h>
  21#include <linux/writeback.h>
  22#include <linux/blkdev.h>
  23#include <linux/backing-dev.h>
  24#include <linux/buffer_head.h>
  25
  26extern struct super_block *blockdev_superblock;
  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 */
  48void __mark_inode_dirty(struct inode *inode, int flags)
  49{
  50        struct super_block *sb = inode->i_sb;
  51
  52        /*
  53         * Don't do this for I_DIRTY_PAGES - that doesn't actually
  54         * dirty the inode itself
  55         */
  56        if (flags & (I_DIRTY_SYNC | I_DIRTY_DATASYNC)) {
  57                if (sb->s_op->dirty_inode)
  58                        sb->s_op->dirty_inode(inode);
  59        }
  60
  61        /*
  62         * make sure that changes are seen by all cpus before we test i_state
  63         * -- mikulas
  64         */
  65        smp_mb();
  66
  67        /* avoid the locking if we can */
  68        if ((inode->i_state & flags) == flags)
  69                return;
  70
  71        spin_lock(&inode_lock);
  72        if ((inode->i_state & flags) != flags) {
  73                const int was_dirty = inode->i_state & I_DIRTY;
  74                struct address_space *mapping = inode->i_mapping;
  75
  76                inode->i_state |= flags;
  77
  78                /*
  79                 * If the inode is locked, just update its dirty state. 
  80                 * The unlocker will place the inode on the appropriate
  81                 * superblock list, based upon its state.
  82                 */
  83                if (inode->i_state & I_LOCK)
  84                        goto out;
  85
  86                /*
  87                 * Only add valid (hashed) inodes to the superblock's
  88                 * dirty list.  Add blockdev inodes as well.
  89                 */
  90                if (!S_ISBLK(inode->i_mode)) {
  91                        if (hlist_unhashed(&inode->i_hash))
  92                                goto out;
  93                        if (inode->i_state & (I_FREEING|I_CLEAR))
  94                                goto out;
  95                }
  96
  97                /*
  98                 * If the inode was already on s_dirty or s_io, don't
  99                 * reposition it (that would break s_dirty time-ordering).
 100                 */
 101                if (!was_dirty) {
 102                        mapping->dirtied_when = jiffies|1; /* 0 is special */
 103                        list_move(&inode->i_list, &sb->s_dirty);
 104                }
 105        }
 106out:
 107        spin_unlock(&inode_lock);
 108}
 109
 110EXPORT_SYMBOL(__mark_inode_dirty);
 111
 112static void write_inode(struct inode *inode, int sync)
 113{
 114        if (inode->i_sb->s_op->write_inode && !is_bad_inode(inode))
 115                inode->i_sb->s_op->write_inode(inode, sync);
 116}
 117
 118/*
 119 * Write a single inode's dirty pages and inode data out to disk.
 120 * If `wait' is set, wait on the writeout.
 121 *
 122 * The whole writeout design is quite complex and fragile.  We want to avoid
 123 * starvation of particular inodes when others are being redirtied, prevent
 124 * livelocks, etc.
 125 *
 126 * So what we do is to move all pages which are to be written from dirty_pages
 127 * onto io_pages.  And keep on writing io_pages until it's empty.  Refusing to
 128 * move more pages onto io_pages until io_pages is empty.  Once that point has
 129 * been reached, we are ready to take another pass across the inode's dirty
 130 * pages.
 131 *
 132 * Called under inode_lock.
 133 */
 134static void
 135__sync_single_inode(struct inode *inode, struct writeback_control *wbc)
 136{
 137        unsigned dirty;
 138        struct address_space *mapping = inode->i_mapping;
 139        struct super_block *sb = inode->i_sb;
 140        int wait = wbc->sync_mode == WB_SYNC_ALL;
 141
 142        BUG_ON(inode->i_state & I_LOCK);
 143
 144        /* Set I_LOCK, reset I_DIRTY */
 145        dirty = inode->i_state & I_DIRTY;
 146        inode->i_state |= I_LOCK;
 147        inode->i_state &= ~I_DIRTY;
 148
 149        /*
 150         * smp_rmb(); note: if you remove write_lock below, you must add this.
 151         * mark_inode_dirty doesn't take spinlock, make sure that inode is not
 152         * read speculatively by this cpu before &= ~I_DIRTY  -- mikulas
 153         */
 154
 155        spin_lock(&mapping->page_lock);
 156        if (wait || !wbc->for_kupdate || list_empty(&mapping->io_pages))
 157                list_splice_init(&mapping->dirty_pages, &mapping->io_pages);
 158        spin_unlock(&mapping->page_lock);
 159        spin_unlock(&inode_lock);
 160
 161        do_writepages(mapping, wbc);
 162
 163        /* Don't write the inode if only I_DIRTY_PAGES was set */
 164        if (dirty & (I_DIRTY_SYNC | I_DIRTY_DATASYNC))
 165                write_inode(inode, wait);
 166
 167        if (wait)
 168                filemap_fdatawait(mapping);
 169
 170        spin_lock(&inode_lock);
 171        inode->i_state &= ~I_LOCK;
 172        if (!(inode->i_state & I_FREEING)) {
 173                if (!list_empty(&mapping->io_pages)) {
 174                        /* Needs more writeback */
 175                        inode->i_state |= I_DIRTY_PAGES;
 176                } else if (!list_empty(&mapping->dirty_pages)) {
 177                        /* Redirtied */
 178                        inode->i_state |= I_DIRTY_PAGES;
 179                        mapping->dirtied_when = jiffies|1;
 180                        list_move(&inode->i_list, &sb->s_dirty);
 181                } else if (inode->i_state & I_DIRTY) {
 182                        /* Redirtied */
 183                        mapping->dirtied_when = jiffies|1;
 184                        list_move(&inode->i_list, &sb->s_dirty);
 185                } else if (atomic_read(&inode->i_count)) {
 186                        mapping->dirtied_when = 0;
 187                        list_move(&inode->i_list, &inode_in_use);
 188                } else {
 189                        mapping->dirtied_when = 0;
 190                        list_move(&inode->i_list, &inode_unused);
 191                }
 192        }
 193        wake_up_inode(inode);
 194}
 195
 196/*
 197 * Write out an inode's dirty pages.  Called under inode_lock.
 198 */
 199static void
 200__writeback_single_inode(struct inode *inode,
 201                        struct writeback_control *wbc)
 202{
 203        if ((wbc->sync_mode != WB_SYNC_ALL) && (inode->i_state & I_LOCK)) {
 204                list_move(&inode->i_list, &inode->i_sb->s_dirty);
 205                return;
 206        }
 207
 208        /*
 209         * It's a data-integrity sync.  We must wait.
 210         */
 211        while (inode->i_state & I_LOCK) {
 212                __iget(inode);
 213                spin_unlock(&inode_lock);
 214                __wait_on_inode(inode);
 215                iput(inode);
 216                spin_lock(&inode_lock);
 217        }
 218        __sync_single_inode(inode, wbc);
 219}
 220
 221/*
 222 * Write out a superblock's list of dirty inodes.  A wait will be performed
 223 * upon no inodes, all inodes or the final one, depending upon sync_mode.
 224 *
 225 * If older_than_this is non-NULL, then only write out mappings which
 226 * had their first dirtying at a time earlier than *older_than_this.
 227 *
 228 * If we're a pdlfush thread, then implement pdflush collision avoidance
 229 * against the entire list.
 230 *
 231 * WB_SYNC_HOLD is a hack for sys_sync(): reattach the inode to sb->s_dirty so
 232 * that it can be located for waiting on in __writeback_single_inode().
 233 *
 234 * Called under inode_lock.
 235 *
 236 * If `bdi' is non-zero then we're being asked to writeback a specific queue.
 237 * This function assumes that the blockdev superblock's inodes are backed by
 238 * a variety of queues, so all inodes are searched.  For other superblocks,
 239 * assume that all inodes are backed by the same queue.
 240 *
 241 * FIXME: this linear search could get expensive with many fileystems.  But
 242 * how to fix?  We need to go from an address_space to all inodes which share
 243 * a queue with that address_space.  (Easy: have a global "dirty superblocks"
 244 * list).
 245 *
 246 * The inodes to be written are parked on sb->s_io.  They are moved back onto
 247 * sb->s_dirty as they are selected for writing.  This way, none can be missed
 248 * on the writer throttling path, and we get decent balancing between many
 249 * throttled threads: we don't want them all piling up on __wait_on_inode.
 250 */
 251static void
 252sync_sb_inodes(struct super_block *sb, struct writeback_control *wbc)
 253{
 254        const unsigned long start = jiffies;    /* livelock avoidance */
 255
 256        if (!wbc->for_kupdate || list_empty(&sb->s_io))
 257                list_splice_init(&sb->s_dirty, &sb->s_io);
 258
 259        while (!list_empty(&sb->s_io)) {
 260                struct inode *inode = list_entry(sb->s_io.prev,
 261                                                struct inode, i_list);
 262                struct address_space *mapping = inode->i_mapping;
 263                struct backing_dev_info *bdi = mapping->backing_dev_info;
 264
 265                if (bdi->memory_backed) {
 266                        if (sb == blockdev_superblock) {
 267                                /*
 268                                 * Dirty memory-backed blockdev: the ramdisk
 269                                 * driver does this.
 270                                 */
 271                                list_move(&inode->i_list, &sb->s_dirty);
 272                                continue;
 273                        }
 274                        /*
 275                         * Assume that all inodes on this superblock are memory
 276                         * backed.  Skip the superblock.
 277                         */
 278                        break;
 279                }
 280
 281                if (wbc->nonblocking && bdi_write_congested(bdi)) {
 282                        wbc->encountered_congestion = 1;
 283                        if (sb != blockdev_superblock)
 284                                break;          /* Skip a congested fs */
 285                        list_move(&inode->i_list, &sb->s_dirty);
 286                        continue;               /* Skip a congested blockdev */
 287                }
 288
 289                if (wbc->bdi && bdi != wbc->bdi) {
 290                        if (sb != blockdev_superblock)
 291                                break;          /* fs has the wrong queue */
 292                        list_move(&inode->i_list, &sb->s_dirty);
 293                        continue;               /* blockdev has wrong queue */
 294                }
 295
 296                /* Was this inode dirtied after sync_sb_inodes was called? */
 297                if (time_after(mapping->dirtied_when, start))
 298                        break;
 299
 300                /* Was this inode dirtied too recently? */
 301                if (wbc->older_than_this && time_after(mapping->dirtied_when,
 302                                                *wbc->older_than_this))
 303                        break;
 304
 305                /* Is another pdflush already flushing this queue? */
 306                if (current_is_pdflush() && !writeback_acquire(bdi))
 307                        break;
 308
 309                BUG_ON(inode->i_state & I_FREEING);
 310                __iget(inode);
 311                __writeback_single_inode(inode, wbc);
 312                if (wbc->sync_mode == WB_SYNC_HOLD) {
 313                        mapping->dirtied_when = jiffies|1;
 314                        list_move(&inode->i_list, &sb->s_dirty);
 315                }
 316                if (current_is_pdflush())
 317                        writeback_release(bdi);
 318                spin_unlock(&inode_lock);
 319                iput(inode);
 320                spin_lock(&inode_lock);
 321                if (wbc->nr_to_write <= 0)
 322                        break;
 323        }
 324        return;         /* Leave any unwritten inodes on s_io */
 325}
 326
 327/*
 328 * Start writeback of dirty pagecache data against all unlocked inodes.
 329 *
 330 * Note:
 331 * We don't need to grab a reference to superblock here. If it has non-empty
 332 * ->s_dirty it's hadn't been killed yet and kill_super() won't proceed
 333 * past sync_inodes_sb() until both the ->s_dirty and ->s_io lists are
 334 * empty. Since __sync_single_inode() regains inode_lock before it finally moves
 335 * inode from superblock lists we are OK.
 336 *
 337 * If `older_than_this' is non-zero then only flush inodes which have a
 338 * flushtime older than *older_than_this.
 339 *
 340 * If `bdi' is non-zero then we will scan the first inode against each
 341 * superblock until we find the matching ones.  One group will be the dirty
 342 * inodes against a filesystem.  Then when we hit the dummy blockdev superblock,
 343 * sync_sb_inodes will seekout the blockdev which matches `bdi'.  Maybe not
 344 * super-efficient but we're about to do a ton of I/O...
 345 */
 346void
 347writeback_inodes(struct writeback_control *wbc)
 348{
 349        struct super_block *sb;
 350
 351        spin_lock(&inode_lock);
 352        spin_lock(&sb_lock);
 353        sb = sb_entry(super_blocks.prev);
 354        for (; sb != sb_entry(&super_blocks); sb = sb_entry(sb->s_list.prev)) {
 355                if (!list_empty(&sb->s_dirty) || !list_empty(&sb->s_io)) {
 356                        spin_unlock(&sb_lock);
 357                        sync_sb_inodes(sb, wbc);
 358                        spin_lock(&sb_lock);
 359                }
 360                if (wbc->nr_to_write <= 0)
 361                        break;
 362        }
 363        spin_unlock(&sb_lock);
 364        spin_unlock(&inode_lock);
 365}
 366
 367/*
 368 * writeback and wait upon the filesystem's dirty inodes.  The caller will
 369 * do this in two passes - one to write, and one to wait.  WB_SYNC_HOLD is
 370 * used to park the written inodes on sb->s_dirty for the wait pass.
 371 *
 372 * A finite limit is set on the number of pages which will be written.
 373 * To prevent infinite livelock of sys_sync().
 374 *
 375 * We add in the number of potentially dirty inodes, because each inode write
 376 * can dirty pagecache in the underlying blockdev.
 377 */
 378void sync_inodes_sb(struct super_block *sb, int wait)
 379{
 380        struct page_state ps;
 381        struct writeback_control wbc = {
 382                .bdi            = NULL,
 383                .sync_mode      = wait ? WB_SYNC_ALL : WB_SYNC_HOLD,
 384                .older_than_this = NULL,
 385                .nr_to_write    = 0,
 386        };
 387
 388        get_page_state(&ps);
 389        wbc.nr_to_write = ps.nr_dirty + ps.nr_unstable +
 390                        (inodes_stat.nr_inodes - inodes_stat.nr_unused) +
 391                        ps.nr_dirty + ps.nr_unstable;
 392        wbc.nr_to_write += wbc.nr_to_write / 2;         /* Bit more for luck */
 393        spin_lock(&inode_lock);
 394        sync_sb_inodes(sb, &wbc);
 395        spin_unlock(&inode_lock);
 396}
 397
 398/*
 399 * Rather lame livelock avoidance.
 400 */
 401static void set_sb_syncing(int val)
 402{
 403        struct super_block *sb;
 404        spin_lock(&sb_lock);
 405        sb = sb_entry(super_blocks.prev);
 406        for (; sb != sb_entry(&super_blocks); sb = sb_entry(sb->s_list.prev)) {
 407                sb->s_syncing = val;
 408        }
 409        spin_unlock(&sb_lock);
 410}
 411
 412/*
 413 * Find a superblock with inodes that need to be synced
 414 */
 415static struct super_block *get_super_to_sync(void)
 416{
 417        struct super_block *sb;
 418restart:
 419        spin_lock(&sb_lock);
 420        sb = sb_entry(super_blocks.prev);
 421        for (; sb != sb_entry(&super_blocks); sb = sb_entry(sb->s_list.prev)) {
 422                if (sb->s_syncing)
 423                        continue;
 424                sb->s_syncing = 1;
 425                sb->s_count++;
 426                spin_unlock(&sb_lock);
 427                down_read(&sb->s_umount);
 428                if (!sb->s_root) {
 429                        drop_super(sb);
 430                        goto restart;
 431                }
 432                return sb;
 433        }
 434        spin_unlock(&sb_lock);
 435        return NULL;
 436}
 437
 438/**
 439 * sync_inodes
 440 *
 441 * sync_inodes() goes through each super block's dirty inode list, writes the
 442 * inodes out, waits on the writeout and puts the inodes back on the normal
 443 * list.
 444 *
 445 * This is for sys_sync().  fsync_dev() uses the same algorithm.  The subtle
 446 * part of the sync functions is that the blockdev "superblock" is processed
 447 * last.  This is because the write_inode() function of a typical fs will
 448 * perform no I/O, but will mark buffers in the blockdev mapping as dirty.
 449 * What we want to do is to perform all that dirtying first, and then write
 450 * back all those inode blocks via the blockdev mapping in one sweep.  So the
 451 * additional (somewhat redundant) sync_blockdev() calls here are to make
 452 * sure that really happens.  Because if we call sync_inodes_sb(wait=1) with
 453 * outstanding dirty inodes, the writeback goes block-at-a-time within the
 454 * filesystem's write_inode().  This is extremely slow.
 455 */
 456void sync_inodes(int wait)
 457{
 458        struct super_block *sb;
 459
 460        set_sb_syncing(0);
 461        while ((sb = get_super_to_sync()) != NULL) {
 462                sync_inodes_sb(sb, 0);
 463                sync_blockdev(sb->s_bdev);
 464                drop_super(sb);
 465        }
 466        if (wait) {
 467                set_sb_syncing(0);
 468                while ((sb = get_super_to_sync()) != NULL) {
 469                        sync_inodes_sb(sb, 1);
 470                        sync_blockdev(sb->s_bdev);
 471                        drop_super(sb);
 472                }
 473        }
 474}
 475
 476/**
 477 *      write_inode_now -       write an inode to disk
 478 *      @inode: inode to write to disk
 479 *      @sync: whether the write should be synchronous or not
 480 *
 481 *      This function commits an inode to disk immediately if it is
 482 *      dirty. This is primarily needed by knfsd.
 483 */
 484 
 485void write_inode_now(struct inode *inode, int sync)
 486{
 487        struct writeback_control wbc = {
 488                .nr_to_write = LONG_MAX,
 489                .sync_mode = WB_SYNC_ALL,
 490        };
 491
 492        spin_lock(&inode_lock);
 493        __writeback_single_inode(inode, &wbc);
 494        spin_unlock(&inode_lock);
 495        if (sync)
 496                wait_on_inode(inode);
 497}
 498
 499EXPORT_SYMBOL(write_inode_now);
 500
 501/**
 502 * generic_osync_inode - flush all dirty data for a given inode to disk
 503 * @inode: inode to write
 504 * @what:  what to write and wait upon
 505 *
 506 * This can be called by file_write functions for files which have the
 507 * O_SYNC flag set, to flush dirty writes to disk.
 508 *
 509 * @what is a bitmask, specifying which part of the inode's data should be
 510 * written and waited upon:
 511 *
 512 *    OSYNC_DATA:     i_mapping's dirty data
 513 *    OSYNC_METADATA: the buffers at i_mapping->private_list
 514 *    OSYNC_INODE:    the inode itself
 515 */
 516
 517int generic_osync_inode(struct inode *inode, int what)
 518{
 519        int err = 0;
 520        int need_write_inode_now = 0;
 521        int err2;
 522
 523        current->flags |= PF_SYNCWRITE;
 524        if (what & OSYNC_DATA)
 525                err = filemap_fdatawrite(inode->i_mapping);
 526        if (what & (OSYNC_METADATA|OSYNC_DATA)) {
 527                err2 = sync_mapping_buffers(inode->i_mapping);
 528                if (!err)
 529                        err = err2;
 530        }
 531        if (what & OSYNC_DATA) {
 532                err2 = filemap_fdatawait(inode->i_mapping);
 533                if (!err)
 534                        err = err2;
 535        }
 536        current->flags &= ~PF_SYNCWRITE;
 537
 538        spin_lock(&inode_lock);
 539        if ((inode->i_state & I_DIRTY) &&
 540            ((what & OSYNC_INODE) || (inode->i_state & I_DIRTY_DATASYNC)))
 541                need_write_inode_now = 1;
 542        spin_unlock(&inode_lock);
 543
 544        if (need_write_inode_now)
 545                write_inode_now(inode, 1);
 546        else
 547                wait_on_inode(inode);
 548
 549        return err;
 550}
 551
 552EXPORT_SYMBOL(generic_osync_inode);
 553
 554/**
 555 * writeback_acquire: attempt to get exclusive writeback access to a device
 556 * @bdi: the device's backing_dev_info structure
 557 *
 558 * It is a waste of resources to have more than one pdflush thread blocked on
 559 * a single request queue.  Exclusion at the request_queue level is obtained
 560 * via a flag in the request_queue's backing_dev_info.state.
 561 *
 562 * Non-request_queue-backed address_spaces will share default_backing_dev_info,
 563 * unless they implement their own.  Which is somewhat inefficient, as this
 564 * may prevent concurrent writeback against multiple devices.
 565 */
 566int writeback_acquire(struct backing_dev_info *bdi)
 567{
 568        return !test_and_set_bit(BDI_pdflush, &bdi->state);
 569}
 570
 571/**
 572 * writeback_in_progress: determine whether there is writeback in progress
 573 *                        against a backing device.
 574 * @bdi: the device's backing_dev_info structure.
 575 */
 576int writeback_in_progress(struct backing_dev_info *bdi)
 577{
 578        return test_bit(BDI_pdflush, &bdi->state);
 579}
 580
 581/**
 582 * writeback_release: relinquish exclusive writeback access against a device.
 583 * @bdi: the device's backing_dev_info structure
 584 */
 585void writeback_release(struct backing_dev_info *bdi)
 586{
 587        BUG_ON(!writeback_in_progress(bdi));
 588        clear_bit(BDI_pdflush, &bdi->state);
 589}
 590
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.