ext4: handle writeback of inodes which are being freed
[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 #define WQ_HASH_SZ              37
36 #define to_ioend_wq(v)  (&ioend_wq[((unsigned long)v) % WQ_HASH_SZ])
37 static wait_queue_head_t ioend_wq[WQ_HASH_SZ];
38
39 int __init ext4_init_pageio(void)
40 {
41         int i;
42
43         io_page_cachep = KMEM_CACHE(ext4_io_page, SLAB_RECLAIM_ACCOUNT);
44         if (io_page_cachep == NULL)
45                 return -ENOMEM;
46         io_end_cachep = KMEM_CACHE(ext4_io_end, SLAB_RECLAIM_ACCOUNT);
47         if (io_page_cachep == NULL) {
48                 kmem_cache_destroy(io_page_cachep);
49                 return -ENOMEM;
50         }
51         for (i = 0; i < WQ_HASH_SZ; i++)
52                 init_waitqueue_head(&ioend_wq[i]);
53
54         return 0;
55 }
56
57 void ext4_exit_pageio(void)
58 {
59         kmem_cache_destroy(io_end_cachep);
60         kmem_cache_destroy(io_page_cachep);
61 }
62
63 void ext4_ioend_wait(struct inode *inode)
64 {
65         wait_queue_head_t *wq = to_ioend_wq(inode);
66
67         wait_event(*wq, (atomic_read(&EXT4_I(inode)->i_ioend_count) == 0));
68 }
69
70 void ext4_free_io_end(ext4_io_end_t *io)
71 {
72         int i;
73         wait_queue_head_t *wq;
74
75         BUG_ON(!io);
76         if (io->page)
77                 put_page(io->page);
78         for (i = 0; i < io->num_io_pages; i++) {
79                 if (--io->pages[i]->p_count == 0) {
80                         struct page *page = io->pages[i]->p_page;
81
82                         end_page_writeback(page);
83                         put_page(page);
84                         kmem_cache_free(io_page_cachep, io->pages[i]);
85                 }
86         }
87         io->num_io_pages = 0;
88         wq = to_ioend_wq(io->inode);
89         if (atomic_dec_and_test(&EXT4_I(io->inode)->i_ioend_count) &&
90             waitqueue_active(wq))
91                 wake_up_all(wq);
92         kmem_cache_free(io_end_cachep, io);
93 }
94
95 /*
96  * check a range of space and convert unwritten extents to written.
97  */
98 int ext4_end_io_nolock(ext4_io_end_t *io)
99 {
100         struct inode *inode = io->inode;
101         loff_t offset = io->offset;
102         ssize_t size = io->size;
103         int ret = 0;
104
105         ext4_debug("ext4_end_io_nolock: io 0x%p from inode %lu,list->next 0x%p,"
106                    "list->prev 0x%p\n",
107                    io, inode->i_ino, io->list.next, io->list.prev);
108
109         if (list_empty(&io->list))
110                 return ret;
111
112         if (!(io->flag & EXT4_IO_END_UNWRITTEN))
113                 return ret;
114
115         ret = ext4_convert_unwritten_extents(inode, offset, size);
116         if (ret < 0) {
117                 printk(KERN_EMERG "%s: failed to convert unwritten "
118                         "extents to written extents, error is %d "
119                         "io is still on inode %lu aio dio list\n",
120                        __func__, ret, inode->i_ino);
121                 return ret;
122         }
123
124         if (io->iocb)
125                 aio_complete(io->iocb, io->result, 0);
126         /* clear the DIO AIO unwritten flag */
127         io->flag &= ~EXT4_IO_END_UNWRITTEN;
128         return ret;
129 }
130
131 /*
132  * work on completed aio dio IO, to convert unwritten extents to extents
133  */
134 static void ext4_end_io_work(struct work_struct *work)
135 {
136         ext4_io_end_t           *io = container_of(work, ext4_io_end_t, work);
137         struct inode            *inode = io->inode;
138         struct ext4_inode_info  *ei = EXT4_I(inode);
139         unsigned long           flags;
140         int                     ret;
141
142         mutex_lock(&inode->i_mutex);
143         ret = ext4_end_io_nolock(io);
144         if (ret < 0) {
145                 mutex_unlock(&inode->i_mutex);
146                 return;
147         }
148
149         spin_lock_irqsave(&ei->i_completed_io_lock, flags);
150         if (!list_empty(&io->list))
151                 list_del_init(&io->list);
152         spin_unlock_irqrestore(&ei->i_completed_io_lock, flags);
153         mutex_unlock(&inode->i_mutex);
154         ext4_free_io_end(io);
155 }
156
157 ext4_io_end_t *ext4_init_io_end(struct inode *inode, gfp_t flags)
158 {
159         ext4_io_end_t *io = NULL;
160
161         io = kmem_cache_alloc(io_end_cachep, flags);
162         if (io) {
163                 memset(io, 0, sizeof(*io));
164                 atomic_inc(&EXT4_I(inode)->i_ioend_count);
165                 io->inode = inode;
166                 INIT_WORK(&io->work, ext4_end_io_work);
167                 INIT_LIST_HEAD(&io->list);
168         }
169         return io;
170 }
171
172 /*
173  * Print an buffer I/O error compatible with the fs/buffer.c.  This
174  * provides compatibility with dmesg scrapers that look for a specific
175  * buffer I/O error message.  We really need a unified error reporting
176  * structure to userspace ala Digital Unix's uerf system, but it's
177  * probably not going to happen in my lifetime, due to LKML politics...
178  */
179 static void buffer_io_error(struct buffer_head *bh)
180 {
181         char b[BDEVNAME_SIZE];
182         printk(KERN_ERR "Buffer I/O error on device %s, logical block %llu\n",
183                         bdevname(bh->b_bdev, b),
184                         (unsigned long long)bh->b_blocknr);
185 }
186
187 static void ext4_end_bio(struct bio *bio, int error)
188 {
189         ext4_io_end_t *io_end = bio->bi_private;
190         struct workqueue_struct *wq;
191         struct inode *inode;
192         unsigned long flags;
193         int i;
194
195         BUG_ON(!io_end);
196         bio->bi_private = NULL;
197         bio->bi_end_io = NULL;
198         if (test_bit(BIO_UPTODATE, &bio->bi_flags))
199                 error = 0;
200         bio_put(bio);
201
202         for (i = 0; i < io_end->num_io_pages; i++) {
203                 struct page *page = io_end->pages[i]->p_page;
204                 struct buffer_head *bh, *head;
205                 int partial_write = 0;
206
207                 head = page_buffers(page);
208                 if (error)
209                         SetPageError(page);
210                 BUG_ON(!head);
211                 if (head->b_size == PAGE_CACHE_SIZE)
212                         clear_buffer_dirty(head);
213                 else {
214                         loff_t offset;
215                         loff_t io_end_offset = io_end->offset + io_end->size;
216
217                         offset = (sector_t) page->index << PAGE_CACHE_SHIFT;
218                         bh = head;
219                         do {
220                                 if ((offset >= io_end->offset) &&
221                                     (offset+bh->b_size <= io_end_offset)) {
222                                         if (error)
223                                                 buffer_io_error(bh);
224
225                                         clear_buffer_dirty(bh);
226                                 }
227                                 if (buffer_delay(bh))
228                                         partial_write = 1;
229                                 else if (!buffer_mapped(bh))
230                                         clear_buffer_dirty(bh);
231                                 else if (buffer_dirty(bh))
232                                         partial_write = 1;
233                                 offset += bh->b_size;
234                                 bh = bh->b_this_page;
235                         } while (bh != head);
236                 }
237
238                 if (--io_end->pages[i]->p_count == 0) {
239                         struct page *page = io_end->pages[i]->p_page;
240
241                         end_page_writeback(page);
242                         put_page(page);
243                         kmem_cache_free(io_page_cachep, io_end->pages[i]);
244                 }
245
246                 /*
247                  * If this is a partial write which happened to make
248                  * all buffers uptodate then we can optimize away a
249                  * bogus readpage() for the next read(). Here we
250                  * 'discover' whether the page went uptodate as a
251                  * result of this (potentially partial) write.
252                  */
253                 if (!partial_write)
254                         SetPageUptodate(page);
255         }
256         io_end->num_io_pages = 0;
257         inode = io_end->inode;
258
259         if (error) {
260                 io_end->flag |= EXT4_IO_END_ERROR;
261                 ext4_warning(inode->i_sb, "I/O error writing to inode %lu "
262                              "(offset %llu size %ld starting block %llu)",
263                              inode->i_ino,
264                              (unsigned long long) io_end->offset,
265                              (long) io_end->size,
266                              (unsigned long long)
267                              bio->bi_sector >> (inode->i_blkbits - 9));
268         }
269
270         /* Add the io_end to per-inode completed io list*/
271         spin_lock_irqsave(&EXT4_I(inode)->i_completed_io_lock, flags);
272         list_add_tail(&io_end->list, &EXT4_I(inode)->i_completed_io_list);
273         spin_unlock_irqrestore(&EXT4_I(inode)->i_completed_io_lock, flags);
274
275         wq = EXT4_SB(inode->i_sb)->dio_unwritten_wq;
276         /* queue the work to convert unwritten extents to written */
277         queue_work(wq, &io_end->work);
278 }
279
280 void ext4_io_submit(struct ext4_io_submit *io)
281 {
282         struct bio *bio = io->io_bio;
283
284         if (bio) {
285                 bio_get(io->io_bio);
286                 submit_bio(io->io_op, io->io_bio);
287                 BUG_ON(bio_flagged(io->io_bio, BIO_EOPNOTSUPP));
288                 bio_put(io->io_bio);
289         }
290         io->io_bio = 0;
291         io->io_op = 0;
292         io->io_end = 0;
293 }
294
295 static int io_submit_init(struct ext4_io_submit *io,
296                           struct inode *inode,
297                           struct writeback_control *wbc,
298                           struct buffer_head *bh)
299 {
300         ext4_io_end_t *io_end;
301         struct page *page = bh->b_page;
302         int nvecs = bio_get_nr_vecs(bh->b_bdev);
303         struct bio *bio;
304
305         io_end = ext4_init_io_end(inode, GFP_NOFS);
306         if (!io_end)
307                 return -ENOMEM;
308         do {
309                 bio = bio_alloc(GFP_NOIO, nvecs);
310                 nvecs >>= 1;
311         } while (bio == NULL);
312
313         bio->bi_sector = bh->b_blocknr * (bh->b_size >> 9);
314         bio->bi_bdev = bh->b_bdev;
315         bio->bi_private = io->io_end = io_end;
316         bio->bi_end_io = ext4_end_bio;
317
318         io_end->offset = (page->index << PAGE_CACHE_SHIFT) + bh_offset(bh);
319
320         io->io_bio = bio;
321         io->io_op = (wbc->sync_mode == WB_SYNC_ALL ?
322                         WRITE_SYNC_PLUG : WRITE);
323         io->io_next_block = bh->b_blocknr;
324         return 0;
325 }
326
327 static int io_submit_add_bh(struct ext4_io_submit *io,
328                             struct ext4_io_page *io_page,
329                             struct inode *inode,
330                             struct writeback_control *wbc,
331                             struct buffer_head *bh)
332 {
333         ext4_io_end_t *io_end;
334         int ret;
335
336         if (buffer_new(bh)) {
337                 clear_buffer_new(bh);
338                 unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);
339         }
340
341         if (!buffer_mapped(bh) || buffer_delay(bh)) {
342                 if (!buffer_mapped(bh))
343                         clear_buffer_dirty(bh);
344                 if (io->io_bio)
345                         ext4_io_submit(io);
346                 return 0;
347         }
348
349         if (io->io_bio && bh->b_blocknr != io->io_next_block) {
350 submit_and_retry:
351                 ext4_io_submit(io);
352         }
353         if (io->io_bio == NULL) {
354                 ret = io_submit_init(io, inode, wbc, bh);
355                 if (ret)
356                         return ret;
357         }
358         io_end = io->io_end;
359         if ((io_end->num_io_pages >= MAX_IO_PAGES) &&
360             (io_end->pages[io_end->num_io_pages-1] != io_page))
361                 goto submit_and_retry;
362         if (buffer_uninit(bh))
363                 io->io_end->flag |= EXT4_IO_END_UNWRITTEN;
364         io->io_end->size += bh->b_size;
365         io->io_next_block++;
366         ret = bio_add_page(io->io_bio, bh->b_page, bh->b_size, bh_offset(bh));
367         if (ret != bh->b_size)
368                 goto submit_and_retry;
369         if ((io_end->num_io_pages == 0) ||
370             (io_end->pages[io_end->num_io_pages-1] != io_page)) {
371                 io_end->pages[io_end->num_io_pages++] = io_page;
372                 io_page->p_count++;
373         }
374         return 0;
375 }
376
377 int ext4_bio_write_page(struct ext4_io_submit *io,
378                         struct page *page,
379                         int len,
380                         struct writeback_control *wbc)
381 {
382         struct inode *inode = page->mapping->host;
383         unsigned block_start, block_end, blocksize;
384         struct ext4_io_page *io_page;
385         struct buffer_head *bh, *head;
386         int ret = 0;
387
388         blocksize = 1 << inode->i_blkbits;
389
390         BUG_ON(PageWriteback(page));
391         set_page_writeback(page);
392         ClearPageError(page);
393
394         io_page = kmem_cache_alloc(io_page_cachep, GFP_NOFS);
395         if (!io_page) {
396                 set_page_dirty(page);
397                 unlock_page(page);
398                 return -ENOMEM;
399         }
400         io_page->p_page = page;
401         io_page->p_count = 0;
402         get_page(page);
403
404         for (bh = head = page_buffers(page), block_start = 0;
405              bh != head || !block_start;
406              block_start = block_end, bh = bh->b_this_page) {
407                 block_end = block_start + blocksize;
408                 if (block_start >= len) {
409                         clear_buffer_dirty(bh);
410                         set_buffer_uptodate(bh);
411                         continue;
412                 }
413                 ret = io_submit_add_bh(io, io_page, inode, wbc, bh);
414                 if (ret) {
415                         /*
416                          * We only get here on ENOMEM.  Not much else
417                          * we can do but mark the page as dirty, and
418                          * better luck next time.
419                          */
420                         set_page_dirty(page);
421                         break;
422                 }
423         }
424         unlock_page(page);
425         /*
426          * If the page was truncated before we could do the writeback,
427          * or we had a memory allocation error while trying to write
428          * the first buffer head, we won't have submitted any pages for
429          * I/O.  In that case we need to make sure we've cleared the
430          * PageWriteback bit from the page to prevent the system from
431          * wedging later on.
432          */
433         if (io_page->p_count == 0) {
434                 put_page(page);
435                 end_page_writeback(page);
436                 kmem_cache_free(io_page_cachep, io_page);
437         }
438         return ret;
439 }