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