ubi: Fix out of bounds write in volume update code
[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/module.h>
10 #include <linux/fs.h>
11 #include <linux/time.h>
12 #include <linux/jbd2.h>
13 #include <linux/highuid.h>
14 #include <linux/pagemap.h>
15 #include <linux/quotaops.h>
16 #include <linux/string.h>
17 #include <linux/buffer_head.h>
18 #include <linux/writeback.h>
19 #include <linux/pagevec.h>
20 #include <linux/mpage.h>
21 #include <linux/namei.h>
22 #include <linux/uio.h>
23 #include <linux/bio.h>
24 #include <linux/workqueue.h>
25 #include <linux/kernel.h>
26 #include <linux/slab.h>
27
28 #include "ext4_jbd2.h"
29 #include "xattr.h"
30 #include "acl.h"
31 #include "ext4_extents.h"
32
33 static struct kmem_cache *io_page_cachep, *io_end_cachep;
34
35 int __init ext4_init_pageio(void)
36 {
37         io_page_cachep = KMEM_CACHE(ext4_io_page, SLAB_RECLAIM_ACCOUNT);
38         if (io_page_cachep == NULL)
39                 return -ENOMEM;
40         io_end_cachep = KMEM_CACHE(ext4_io_end, SLAB_RECLAIM_ACCOUNT);
41         if (io_end_cachep == NULL) {
42                 kmem_cache_destroy(io_page_cachep);
43                 return -ENOMEM;
44         }
45         return 0;
46 }
47
48 void ext4_exit_pageio(void)
49 {
50         kmem_cache_destroy(io_end_cachep);
51         kmem_cache_destroy(io_page_cachep);
52 }
53
54 void ext4_ioend_wait(struct inode *inode)
55 {
56         wait_queue_head_t *wq = ext4_ioend_wq(inode);
57
58         wait_event(*wq, (atomic_read(&EXT4_I(inode)->i_ioend_count) == 0));
59 }
60
61 static void put_io_page(struct ext4_io_page *io_page)
62 {
63         if (atomic_dec_and_test(&io_page->p_count)) {
64                 end_page_writeback(io_page->p_page);
65                 put_page(io_page->p_page);
66                 kmem_cache_free(io_page_cachep, io_page);
67         }
68 }
69
70 void ext4_free_io_end(ext4_io_end_t *io)
71 {
72         int i;
73
74         BUG_ON(!io);
75         if (io->page)
76                 put_page(io->page);
77         for (i = 0; i < io->num_io_pages; i++)
78                 put_io_page(io->pages[i]);
79         io->num_io_pages = 0;
80         if (atomic_dec_and_test(&EXT4_I(io->inode)->i_ioend_count))
81                 wake_up_all(ext4_ioend_wq(io->inode));
82         kmem_cache_free(io_end_cachep, io);
83 }
84
85 /*
86  * check a range of space and convert unwritten extents to written.
87  *
88  * Called with inode->i_mutex; we depend on this when we manipulate
89  * io->flag, since we could otherwise race with ext4_flush_completed_IO()
90  */
91 int ext4_end_io_nolock(ext4_io_end_t *io)
92 {
93         struct inode *inode = io->inode;
94         loff_t offset = io->offset;
95         ssize_t size = io->size;
96         int ret = 0;
97
98         ext4_debug("ext4_end_io_nolock: io 0x%p from inode %lu,list->next 0x%p,"
99                    "list->prev 0x%p\n",
100                    io, inode->i_ino, io->list.next, io->list.prev);
101
102         ret = ext4_convert_unwritten_extents(inode, offset, size);
103         if (ret < 0) {
104                 ext4_msg(inode->i_sb, KERN_EMERG,
105                          "failed to convert unwritten extents to written "
106                          "extents -- potential data loss!  "
107                          "(inode %lu, offset %llu, size %zd, error %d)",
108                          inode->i_ino, offset, size, ret);
109         }
110
111         /* Wake up anyone waiting on unwritten extent conversion */
112         if (atomic_dec_and_test(&EXT4_I(inode)->i_aiodio_unwritten))
113                 wake_up_all(ext4_ioend_wq(io->inode));
114         if (io->flag & EXT4_IO_END_DIRECT)
115                 inode_dio_done(inode);
116         if (io->iocb)
117                 aio_complete(io->iocb, io->result, 0);
118         return ret;
119 }
120
121 /*
122  * work on completed aio dio IO, to convert unwritten extents to extents
123  */
124 static void ext4_end_io_work(struct work_struct *work)
125 {
126         ext4_io_end_t           *io = container_of(work, ext4_io_end_t, work);
127         struct inode            *inode = io->inode;
128         struct ext4_inode_info  *ei = EXT4_I(inode);
129         unsigned long           flags;
130
131         spin_lock_irqsave(&ei->i_completed_io_lock, flags);
132         if (io->flag & EXT4_IO_END_IN_FSYNC)
133                 goto requeue;
134         if (list_empty(&io->list)) {
135                 spin_unlock_irqrestore(&ei->i_completed_io_lock, flags);
136                 goto free;
137         }
138
139         if (!mutex_trylock(&inode->i_mutex)) {
140                 bool was_queued;
141 requeue:
142                 was_queued = !!(io->flag & EXT4_IO_END_QUEUED);
143                 io->flag |= EXT4_IO_END_QUEUED;
144                 spin_unlock_irqrestore(&ei->i_completed_io_lock, flags);
145                 /*
146                  * Requeue the work instead of waiting so that the work
147                  * items queued after this can be processed.
148                  */
149                 queue_work(EXT4_SB(inode->i_sb)->dio_unwritten_wq, &io->work);
150                 /*
151                  * To prevent the ext4-dio-unwritten thread from keeping
152                  * requeueing end_io requests and occupying cpu for too long,
153                  * yield the cpu if it sees an end_io request that has already
154                  * been requeued.
155                  */
156                 if (was_queued)
157                         yield();
158                 return;
159         }
160         list_del_init(&io->list);
161         spin_unlock_irqrestore(&ei->i_completed_io_lock, flags);
162         (void) ext4_end_io_nolock(io);
163         mutex_unlock(&inode->i_mutex);
164 free:
165         ext4_free_io_end(io);
166 }
167
168 ext4_io_end_t *ext4_init_io_end(struct inode *inode, gfp_t flags)
169 {
170         ext4_io_end_t *io = kmem_cache_zalloc(io_end_cachep, flags);
171         if (io) {
172                 atomic_inc(&EXT4_I(inode)->i_ioend_count);
173                 io->inode = inode;
174                 INIT_WORK(&io->work, ext4_end_io_work);
175                 INIT_LIST_HEAD(&io->list);
176         }
177         return io;
178 }
179
180 /*
181  * Print an buffer I/O error compatible with the fs/buffer.c.  This
182  * provides compatibility with dmesg scrapers that look for a specific
183  * buffer I/O error message.  We really need a unified error reporting
184  * structure to userspace ala Digital Unix's uerf system, but it's
185  * probably not going to happen in my lifetime, due to LKML politics...
186  */
187 static void buffer_io_error(struct buffer_head *bh)
188 {
189         char b[BDEVNAME_SIZE];
190         printk(KERN_ERR "Buffer I/O error on device %s, logical block %llu\n",
191                         bdevname(bh->b_bdev, b),
192                         (unsigned long long)bh->b_blocknr);
193 }
194
195 static void ext4_end_bio(struct bio *bio, int error)
196 {
197         ext4_io_end_t *io_end = bio->bi_private;
198         struct workqueue_struct *wq;
199         struct inode *inode;
200         unsigned long flags;
201         int i;
202         sector_t bi_sector = bio->bi_sector;
203
204         BUG_ON(!io_end);
205         bio->bi_private = NULL;
206         bio->bi_end_io = NULL;
207         if (test_bit(BIO_UPTODATE, &bio->bi_flags))
208                 error = 0;
209         bio_put(bio);
210
211         for (i = 0; i < io_end->num_io_pages; i++) {
212                 struct page *page = io_end->pages[i]->p_page;
213                 struct buffer_head *bh, *head;
214                 loff_t offset;
215                 loff_t io_end_offset;
216
217                 if (error) {
218                         SetPageError(page);
219                         set_bit(AS_EIO, &page->mapping->flags);
220                         head = page_buffers(page);
221                         BUG_ON(!head);
222
223                         io_end_offset = io_end->offset + io_end->size;
224
225                         offset = (sector_t) page->index << PAGE_CACHE_SHIFT;
226                         bh = head;
227                         do {
228                                 if ((offset >= io_end->offset) &&
229                                     (offset+bh->b_size <= io_end_offset))
230                                         buffer_io_error(bh);
231
232                                 offset += bh->b_size;
233                                 bh = bh->b_this_page;
234                         } while (bh != head);
235                 }
236
237                 put_io_page(io_end->pages[i]);
238         }
239         io_end->num_io_pages = 0;
240         inode = io_end->inode;
241
242         if (error) {
243                 io_end->flag |= EXT4_IO_END_ERROR;
244                 ext4_warning(inode->i_sb, "I/O error %d writing to inode %lu "
245                              "(offset %llu size %ld starting block %llu)",
246                              error, inode->i_ino,
247                              (unsigned long long) io_end->offset,
248                              (long) io_end->size,
249                              (unsigned long long)
250                              bi_sector >> (inode->i_blkbits - 9));
251                 mapping_set_error(inode->i_mapping, error);
252         }
253
254         if (!(io_end->flag & EXT4_IO_END_UNWRITTEN)) {
255                 ext4_free_io_end(io_end);
256                 return;
257         }
258
259         /* Add the io_end to per-inode completed io list*/
260         spin_lock_irqsave(&EXT4_I(inode)->i_completed_io_lock, flags);
261         list_add_tail(&io_end->list, &EXT4_I(inode)->i_completed_io_list);
262         spin_unlock_irqrestore(&EXT4_I(inode)->i_completed_io_lock, flags);
263
264         wq = EXT4_SB(inode->i_sb)->dio_unwritten_wq;
265         /* queue the work to convert unwritten extents to written */
266         queue_work(wq, &io_end->work);
267 }
268
269 void ext4_io_submit(struct ext4_io_submit *io)
270 {
271         struct bio *bio = io->io_bio;
272
273         if (bio) {
274                 bio_get(io->io_bio);
275                 submit_bio(io->io_op, io->io_bio);
276                 BUG_ON(bio_flagged(io->io_bio, BIO_EOPNOTSUPP));
277                 bio_put(io->io_bio);
278         }
279         io->io_bio = NULL;
280         io->io_op = 0;
281         io->io_end = NULL;
282 }
283
284 static int io_submit_init(struct ext4_io_submit *io,
285                           struct inode *inode,
286                           struct writeback_control *wbc,
287                           struct buffer_head *bh)
288 {
289         ext4_io_end_t *io_end;
290         struct page *page = bh->b_page;
291         int nvecs = bio_get_nr_vecs(bh->b_bdev);
292         struct bio *bio;
293
294         io_end = ext4_init_io_end(inode, GFP_NOFS);
295         if (!io_end)
296                 return -ENOMEM;
297         bio = bio_alloc(GFP_NOIO, min(nvecs, BIO_MAX_PAGES));
298         bio->bi_sector = bh->b_blocknr * (bh->b_size >> 9);
299         bio->bi_bdev = bh->b_bdev;
300         bio->bi_private = io->io_end = io_end;
301         bio->bi_end_io = ext4_end_bio;
302
303         io_end->offset = (page->index << PAGE_CACHE_SHIFT) + bh_offset(bh);
304
305         io->io_bio = bio;
306         io->io_op = (wbc->sync_mode == WB_SYNC_ALL ?  WRITE_SYNC : WRITE);
307         io->io_next_block = bh->b_blocknr;
308         return 0;
309 }
310
311 static int io_submit_add_bh(struct ext4_io_submit *io,
312                             struct ext4_io_page *io_page,
313                             struct inode *inode,
314                             struct writeback_control *wbc,
315                             struct buffer_head *bh)
316 {
317         ext4_io_end_t *io_end;
318         int ret;
319
320         if (buffer_new(bh)) {
321                 clear_buffer_new(bh);
322                 unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);
323         }
324
325         if (!buffer_mapped(bh) || buffer_delay(bh)) {
326                 if (!buffer_mapped(bh))
327                         clear_buffer_dirty(bh);
328                 if (io->io_bio)
329                         ext4_io_submit(io);
330                 return 0;
331         }
332
333         if (io->io_bio && bh->b_blocknr != io->io_next_block) {
334 submit_and_retry:
335                 ext4_io_submit(io);
336         }
337         if (io->io_bio == NULL) {
338                 ret = io_submit_init(io, inode, wbc, bh);
339                 if (ret)
340                         return ret;
341         }
342         io_end = io->io_end;
343         if ((io_end->num_io_pages >= MAX_IO_PAGES) &&
344             (io_end->pages[io_end->num_io_pages-1] != io_page))
345                 goto submit_and_retry;
346         if (buffer_uninit(bh))
347                 ext4_set_io_unwritten_flag(inode, io_end);
348         io->io_end->size += bh->b_size;
349         io->io_next_block++;
350         ret = bio_add_page(io->io_bio, bh->b_page, bh->b_size, bh_offset(bh));
351         if (ret != bh->b_size)
352                 goto submit_and_retry;
353         if ((io_end->num_io_pages == 0) ||
354             (io_end->pages[io_end->num_io_pages-1] != io_page)) {
355                 io_end->pages[io_end->num_io_pages++] = io_page;
356                 atomic_inc(&io_page->p_count);
357         }
358         return 0;
359 }
360
361 int ext4_bio_write_page(struct ext4_io_submit *io,
362                         struct page *page,
363                         int len,
364                         struct writeback_control *wbc)
365 {
366         struct inode *inode = page->mapping->host;
367         unsigned block_start, block_end, blocksize;
368         struct ext4_io_page *io_page;
369         struct buffer_head *bh, *head;
370         int ret = 0;
371
372         blocksize = 1 << inode->i_blkbits;
373
374         BUG_ON(!PageLocked(page));
375         BUG_ON(PageWriteback(page));
376
377         io_page = kmem_cache_alloc(io_page_cachep, GFP_NOFS);
378         if (!io_page) {
379                 set_page_dirty(page);
380                 unlock_page(page);
381                 return -ENOMEM;
382         }
383         io_page->p_page = page;
384         atomic_set(&io_page->p_count, 1);
385         get_page(page);
386         set_page_writeback(page);
387         ClearPageError(page);
388
389         /*
390          * Comments copied from block_write_full_page_endio:
391          *
392          * The page straddles i_size.  It must be zeroed out on each and every
393          * writepage invocation because it may be mmapped.  "A file is mapped
394          * in multiples of the page size.  For a file that is not a multiple of
395          * the page size, the remaining memory is zeroed when mapped, and
396          * writes to that region are not written out to the file."
397          */
398         if (len < PAGE_CACHE_SIZE)
399                 zero_user_segment(page, len, PAGE_CACHE_SIZE);
400
401         for (bh = head = page_buffers(page), block_start = 0;
402              bh != head || !block_start;
403              block_start = block_end, bh = bh->b_this_page) {
404
405                 block_end = block_start + blocksize;
406                 if (block_start >= len) {
407                         clear_buffer_dirty(bh);
408                         set_buffer_uptodate(bh);
409                         continue;
410                 }
411                 clear_buffer_dirty(bh);
412                 ret = io_submit_add_bh(io, io_page, inode, wbc, bh);
413                 if (ret) {
414                         /*
415                          * We only get here on ENOMEM.  Not much else
416                          * we can do but mark the page as dirty, and
417                          * better luck next time.
418                          */
419                         set_page_dirty(page);
420                         break;
421                 }
422         }
423         unlock_page(page);
424         /*
425          * If the page was truncated before we could do the writeback,
426          * or we had a memory allocation error while trying to write
427          * the first buffer head, we won't have submitted any pages for
428          * I/O.  In that case we need to make sure we've cleared the
429          * PageWriteback bit from the page to prevent the system from
430          * wedging later on.
431          */
432         put_io_page(io_page);
433         return ret;
434 }