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