ext4: clear buffer_uninit flag when submitting IO
[pandora-kernel.git] / fs / ext4 / page-io.c
1 /*
2  * linux/fs/ext4/page-io.c
3  *
4  * This contains the new page_io functions for ext4
5  *
6  * Written by Theodore Ts'o, 2010.
7  */
8
9 #include <linux/fs.h>
10 #include <linux/time.h>
11 #include <linux/jbd2.h>
12 #include <linux/highuid.h>
13 #include <linux/pagemap.h>
14 #include <linux/quotaops.h>
15 #include <linux/string.h>
16 #include <linux/buffer_head.h>
17 #include <linux/writeback.h>
18 #include <linux/pagevec.h>
19 #include <linux/mpage.h>
20 #include <linux/namei.h>
21 #include <linux/uio.h>
22 #include <linux/bio.h>
23 #include <linux/workqueue.h>
24 #include <linux/kernel.h>
25 #include <linux/slab.h>
26 #include <linux/mm.h>
27
28 #include "ext4_jbd2.h"
29 #include "xattr.h"
30 #include "acl.h"
31
32 static struct kmem_cache *io_end_cachep;
33
34 int __init ext4_init_pageio(void)
35 {
36         io_end_cachep = KMEM_CACHE(ext4_io_end, SLAB_RECLAIM_ACCOUNT);
37         if (io_end_cachep == NULL)
38                 return -ENOMEM;
39         return 0;
40 }
41
42 void ext4_exit_pageio(void)
43 {
44         kmem_cache_destroy(io_end_cachep);
45 }
46
47 /*
48  * This function is called by ext4_evict_inode() to make sure there is
49  * no more pending I/O completion work left to do.
50  */
51 void ext4_ioend_shutdown(struct inode *inode)
52 {
53         wait_queue_head_t *wq = ext4_ioend_wq(inode);
54
55         wait_event(*wq, (atomic_read(&EXT4_I(inode)->i_ioend_count) == 0));
56         /*
57          * We need to make sure the work structure is finished being
58          * used before we let the inode get destroyed.
59          */
60         if (work_pending(&EXT4_I(inode)->i_unwritten_work))
61                 cancel_work_sync(&EXT4_I(inode)->i_unwritten_work);
62 }
63
64 static void ext4_release_io_end(ext4_io_end_t *io_end)
65 {
66         BUG_ON(!list_empty(&io_end->list));
67         BUG_ON(io_end->flag & EXT4_IO_END_UNWRITTEN);
68
69         if (atomic_dec_and_test(&EXT4_I(io_end->inode)->i_ioend_count))
70                 wake_up_all(ext4_ioend_wq(io_end->inode));
71         if (io_end->flag & EXT4_IO_END_DIRECT)
72                 inode_dio_done(io_end->inode);
73         if (io_end->iocb)
74                 aio_complete(io_end->iocb, io_end->result, 0);
75         kmem_cache_free(io_end_cachep, io_end);
76 }
77
78 static void ext4_clear_io_unwritten_flag(ext4_io_end_t *io_end)
79 {
80         struct inode *inode = io_end->inode;
81
82         io_end->flag &= ~EXT4_IO_END_UNWRITTEN;
83         /* Wake up anyone waiting on unwritten extent conversion */
84         if (atomic_dec_and_test(&EXT4_I(inode)->i_unwritten))
85                 wake_up_all(ext4_ioend_wq(inode));
86 }
87
88 /* check a range of space and convert unwritten extents to written. */
89 static int ext4_end_io(ext4_io_end_t *io)
90 {
91         struct inode *inode = io->inode;
92         loff_t offset = io->offset;
93         ssize_t size = io->size;
94         int ret = 0;
95
96         ext4_debug("ext4_end_io_nolock: io 0x%p from inode %lu,list->next 0x%p,"
97                    "list->prev 0x%p\n",
98                    io, inode->i_ino, io->list.next, io->list.prev);
99
100         ret = ext4_convert_unwritten_extents(inode, offset, size);
101         if (ret < 0) {
102                 ext4_msg(inode->i_sb, KERN_EMERG,
103                          "failed to convert unwritten extents to written "
104                          "extents -- potential data loss!  "
105                          "(inode %lu, offset %llu, size %zd, error %d)",
106                          inode->i_ino, offset, size, ret);
107         }
108         ext4_clear_io_unwritten_flag(io);
109         ext4_release_io_end(io);
110         return ret;
111 }
112
113 static void dump_completed_IO(struct inode *inode)
114 {
115 #ifdef  EXT4FS_DEBUG
116         struct list_head *cur, *before, *after;
117         ext4_io_end_t *io, *io0, *io1;
118
119         if (list_empty(&EXT4_I(inode)->i_completed_io_list)) {
120                 ext4_debug("inode %lu completed_io list is empty\n",
121                            inode->i_ino);
122                 return;
123         }
124
125         ext4_debug("Dump inode %lu completed_io list\n", inode->i_ino);
126         list_for_each_entry(io, &EXT4_I(inode)->i_completed_io_list, list) {
127                 cur = &io->list;
128                 before = cur->prev;
129                 io0 = container_of(before, ext4_io_end_t, list);
130                 after = cur->next;
131                 io1 = container_of(after, ext4_io_end_t, list);
132
133                 ext4_debug("io 0x%p from inode %lu,prev 0x%p,next 0x%p\n",
134                             io, inode->i_ino, io0, io1);
135         }
136 #endif
137 }
138
139 /* Add the io_end to per-inode completed end_io list. */
140 static void ext4_add_complete_io(ext4_io_end_t *io_end)
141 {
142         struct ext4_inode_info *ei = EXT4_I(io_end->inode);
143         struct workqueue_struct *wq;
144         unsigned long flags;
145
146         BUG_ON(!(io_end->flag & EXT4_IO_END_UNWRITTEN));
147         wq = EXT4_SB(io_end->inode->i_sb)->dio_unwritten_wq;
148
149         spin_lock_irqsave(&ei->i_completed_io_lock, flags);
150         if (list_empty(&ei->i_completed_io_list))
151                 queue_work(wq, &ei->i_unwritten_work);
152         list_add_tail(&io_end->list, &ei->i_completed_io_list);
153         spin_unlock_irqrestore(&ei->i_completed_io_lock, flags);
154 }
155
156 static int ext4_do_flush_completed_IO(struct inode *inode)
157 {
158         ext4_io_end_t *io;
159         struct list_head unwritten;
160         unsigned long flags;
161         struct ext4_inode_info *ei = EXT4_I(inode);
162         int err, ret = 0;
163
164         spin_lock_irqsave(&ei->i_completed_io_lock, flags);
165         dump_completed_IO(inode);
166         list_replace_init(&ei->i_completed_io_list, &unwritten);
167         spin_unlock_irqrestore(&ei->i_completed_io_lock, flags);
168
169         while (!list_empty(&unwritten)) {
170                 io = list_entry(unwritten.next, ext4_io_end_t, list);
171                 BUG_ON(!(io->flag & EXT4_IO_END_UNWRITTEN));
172                 list_del_init(&io->list);
173
174                 err = ext4_end_io(io);
175                 if (unlikely(!ret && err))
176                         ret = err;
177         }
178         return ret;
179 }
180
181 /*
182  * work on completed aio dio IO, to convert unwritten extents to extents
183  */
184 void ext4_end_io_work(struct work_struct *work)
185 {
186         struct ext4_inode_info *ei = container_of(work, struct ext4_inode_info,
187                                                   i_unwritten_work);
188         ext4_do_flush_completed_IO(&ei->vfs_inode);
189 }
190
191 int ext4_flush_unwritten_io(struct inode *inode)
192 {
193         int ret;
194         WARN_ON_ONCE(!mutex_is_locked(&inode->i_mutex) &&
195                      !(inode->i_state & I_FREEING));
196         ret = ext4_do_flush_completed_IO(inode);
197         ext4_unwritten_wait(inode);
198         return ret;
199 }
200
201 ext4_io_end_t *ext4_init_io_end(struct inode *inode, gfp_t flags)
202 {
203         ext4_io_end_t *io = kmem_cache_zalloc(io_end_cachep, flags);
204         if (io) {
205                 atomic_inc(&EXT4_I(inode)->i_ioend_count);
206                 io->inode = inode;
207                 INIT_LIST_HEAD(&io->list);
208                 atomic_set(&io->count, 1);
209         }
210         return io;
211 }
212
213 void ext4_put_io_end_defer(ext4_io_end_t *io_end)
214 {
215         if (atomic_dec_and_test(&io_end->count)) {
216                 if (!(io_end->flag & EXT4_IO_END_UNWRITTEN) || !io_end->size) {
217                         ext4_release_io_end(io_end);
218                         return;
219                 }
220                 ext4_add_complete_io(io_end);
221         }
222 }
223
224 int ext4_put_io_end(ext4_io_end_t *io_end)
225 {
226         int err = 0;
227
228         if (atomic_dec_and_test(&io_end->count)) {
229                 if (io_end->flag & EXT4_IO_END_UNWRITTEN) {
230                         err = ext4_convert_unwritten_extents(io_end->inode,
231                                                 io_end->offset, io_end->size);
232                         ext4_clear_io_unwritten_flag(io_end);
233                 }
234                 ext4_release_io_end(io_end);
235         }
236         return err;
237 }
238
239 ext4_io_end_t *ext4_get_io_end(ext4_io_end_t *io_end)
240 {
241         atomic_inc(&io_end->count);
242         return io_end;
243 }
244
245 /*
246  * Print an buffer I/O error compatible with the fs/buffer.c.  This
247  * provides compatibility with dmesg scrapers that look for a specific
248  * buffer I/O error message.  We really need a unified error reporting
249  * structure to userspace ala Digital Unix's uerf system, but it's
250  * probably not going to happen in my lifetime, due to LKML politics...
251  */
252 static void buffer_io_error(struct buffer_head *bh)
253 {
254         char b[BDEVNAME_SIZE];
255         printk(KERN_ERR "Buffer I/O error on device %s, logical block %llu\n",
256                         bdevname(bh->b_bdev, b),
257                         (unsigned long long)bh->b_blocknr);
258 }
259
260 static void ext4_end_bio(struct bio *bio, int error)
261 {
262         ext4_io_end_t *io_end = bio->bi_private;
263         struct inode *inode;
264         int i;
265         int blocksize;
266         sector_t bi_sector = bio->bi_sector;
267
268         BUG_ON(!io_end);
269         inode = io_end->inode;
270         blocksize = 1 << inode->i_blkbits;
271         bio->bi_private = NULL;
272         bio->bi_end_io = NULL;
273         if (test_bit(BIO_UPTODATE, &bio->bi_flags))
274                 error = 0;
275         for (i = 0; i < bio->bi_vcnt; i++) {
276                 struct bio_vec *bvec = &bio->bi_io_vec[i];
277                 struct page *page = bvec->bv_page;
278                 struct buffer_head *bh, *head;
279                 unsigned bio_start = bvec->bv_offset;
280                 unsigned bio_end = bio_start + bvec->bv_len;
281                 unsigned under_io = 0;
282                 unsigned long flags;
283
284                 if (!page)
285                         continue;
286
287                 if (error) {
288                         SetPageError(page);
289                         set_bit(AS_EIO, &page->mapping->flags);
290                 }
291                 bh = head = page_buffers(page);
292                 /*
293                  * We check all buffers in the page under BH_Uptodate_Lock
294                  * to avoid races with other end io clearing async_write flags
295                  */
296                 local_irq_save(flags);
297                 bit_spin_lock(BH_Uptodate_Lock, &head->b_state);
298                 do {
299                         if (bh_offset(bh) < bio_start ||
300                             bh_offset(bh) + blocksize > bio_end) {
301                                 if (buffer_async_write(bh))
302                                         under_io++;
303                                 continue;
304                         }
305                         clear_buffer_async_write(bh);
306                         if (error)
307                                 buffer_io_error(bh);
308                 } while ((bh = bh->b_this_page) != head);
309                 bit_spin_unlock(BH_Uptodate_Lock, &head->b_state);
310                 local_irq_restore(flags);
311                 if (!under_io)
312                         end_page_writeback(page);
313         }
314         bio_put(bio);
315
316         if (error) {
317                 io_end->flag |= EXT4_IO_END_ERROR;
318                 ext4_warning(inode->i_sb, "I/O error writing to inode %lu "
319                              "(offset %llu size %ld starting block %llu)",
320                              inode->i_ino,
321                              (unsigned long long) io_end->offset,
322                              (long) io_end->size,
323                              (unsigned long long)
324                              bi_sector >> (inode->i_blkbits - 9));
325         }
326
327         ext4_put_io_end_defer(io_end);
328 }
329
330 void ext4_io_submit(struct ext4_io_submit *io)
331 {
332         struct bio *bio = io->io_bio;
333
334         if (bio) {
335                 bio_get(io->io_bio);
336                 submit_bio(io->io_op, io->io_bio);
337                 BUG_ON(bio_flagged(io->io_bio, BIO_EOPNOTSUPP));
338                 bio_put(io->io_bio);
339         }
340         io->io_bio = NULL;
341 }
342
343 void ext4_io_submit_init(struct ext4_io_submit *io,
344                          struct writeback_control *wbc)
345 {
346         io->io_op = (wbc->sync_mode == WB_SYNC_ALL ?  WRITE_SYNC : WRITE);
347         io->io_bio = NULL;
348         io->io_end = NULL;
349 }
350
351 static int io_submit_init_bio(struct ext4_io_submit *io,
352                               struct buffer_head *bh)
353 {
354         int nvecs = bio_get_nr_vecs(bh->b_bdev);
355         struct bio *bio;
356
357         bio = bio_alloc(GFP_NOIO, min(nvecs, BIO_MAX_PAGES));
358         bio->bi_sector = bh->b_blocknr * (bh->b_size >> 9);
359         bio->bi_bdev = bh->b_bdev;
360         bio->bi_end_io = ext4_end_bio;
361         bio->bi_private = ext4_get_io_end(io->io_end);
362         if (!io->io_end->size)
363                 io->io_end->offset = (bh->b_page->index << PAGE_CACHE_SHIFT)
364                                      + bh_offset(bh);
365         io->io_bio = bio;
366         io->io_next_block = bh->b_blocknr;
367         return 0;
368 }
369
370 static int io_submit_add_bh(struct ext4_io_submit *io,
371                             struct inode *inode,
372                             struct buffer_head *bh)
373 {
374         ext4_io_end_t *io_end;
375         int ret;
376
377         if (io->io_bio && bh->b_blocknr != io->io_next_block) {
378 submit_and_retry:
379                 ext4_io_submit(io);
380         }
381         if (io->io_bio == NULL) {
382                 ret = io_submit_init_bio(io, bh);
383                 if (ret)
384                         return ret;
385         }
386         ret = bio_add_page(io->io_bio, bh->b_page, bh->b_size, bh_offset(bh));
387         if (ret != bh->b_size)
388                 goto submit_and_retry;
389         io_end = io->io_end;
390         if (test_clear_buffer_uninit(bh))
391                 ext4_set_io_unwritten_flag(inode, io_end);
392         io_end->size += bh->b_size;
393         io->io_next_block++;
394         return 0;
395 }
396
397 int ext4_bio_write_page(struct ext4_io_submit *io,
398                         struct page *page,
399                         int len,
400                         struct writeback_control *wbc)
401 {
402         struct inode *inode = page->mapping->host;
403         unsigned block_start, blocksize;
404         struct buffer_head *bh, *head;
405         int ret = 0;
406         int nr_submitted = 0;
407
408         blocksize = 1 << inode->i_blkbits;
409
410         BUG_ON(!PageLocked(page));
411         BUG_ON(PageWriteback(page));
412
413         set_page_writeback(page);
414         ClearPageError(page);
415
416         /*
417          * In the first loop we prepare and mark buffers to submit. We have to
418          * mark all buffers in the page before submitting so that
419          * end_page_writeback() cannot be called from ext4_bio_end_io() when IO
420          * on the first buffer finishes and we are still working on submitting
421          * the second buffer.
422          */
423         bh = head = page_buffers(page);
424         do {
425                 block_start = bh_offset(bh);
426                 if (block_start >= len) {
427                         /*
428                          * Comments copied from block_write_full_page_endio:
429                          *
430                          * The page straddles i_size.  It must be zeroed out on
431                          * each and every writepage invocation because it may
432                          * be mmapped.  "A file is mapped in multiples of the
433                          * page size.  For a file that is not a multiple of
434                          * the  page size, the remaining memory is zeroed when
435                          * mapped, and writes to that region are not written
436                          * out to the file."
437                          */
438                         zero_user_segment(page, block_start,
439                                           block_start + blocksize);
440                         clear_buffer_dirty(bh);
441                         set_buffer_uptodate(bh);
442                         continue;
443                 }
444                 if (!buffer_dirty(bh) || buffer_delay(bh) ||
445                     !buffer_mapped(bh) || buffer_unwritten(bh)) {
446                         /* A hole? We can safely clear the dirty bit */
447                         if (!buffer_mapped(bh))
448                                 clear_buffer_dirty(bh);
449                         if (io->io_bio)
450                                 ext4_io_submit(io);
451                         continue;
452                 }
453                 if (buffer_new(bh)) {
454                         clear_buffer_new(bh);
455                         unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);
456                 }
457                 set_buffer_async_write(bh);
458         } while ((bh = bh->b_this_page) != head);
459
460         /* Now submit buffers to write */
461         bh = head = page_buffers(page);
462         do {
463                 if (!buffer_async_write(bh))
464                         continue;
465                 ret = io_submit_add_bh(io, inode, bh);
466                 if (ret) {
467                         /*
468                          * We only get here on ENOMEM.  Not much else
469                          * we can do but mark the page as dirty, and
470                          * better luck next time.
471                          */
472                         redirty_page_for_writepage(wbc, page);
473                         break;
474                 }
475                 nr_submitted++;
476                 clear_buffer_dirty(bh);
477         } while ((bh = bh->b_this_page) != head);
478
479         /* Error stopped previous loop? Clean up buffers... */
480         if (ret) {
481                 do {
482                         clear_buffer_async_write(bh);
483                         bh = bh->b_this_page;
484                 } while (bh != head);
485         }
486         unlock_page(page);
487         /* Nothing submitted - we have to end page writeback */
488         if (!nr_submitted)
489                 end_page_writeback(page);
490         return ret;
491 }