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