blob: 745492b6c66674c8b945bdd3875f043565e07e68 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110021#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_sb.h"
Nathan Scotta844f452005-11-02 14:38:42 +110023#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_trans.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_mount.h"
26#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_dinode.h"
28#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110029#include "xfs_alloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_error.h"
31#include "xfs_rw.h"
32#include "xfs_iomap.h"
Christoph Hellwig739bfb22007-08-29 10:58:01 +100033#include "xfs_vnodeops.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000034#include "xfs_trace.h"
Dave Chinner3ed3a432010-03-05 02:00:42 +000035#include "xfs_bmap.h"
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/mpage.h>
Christoph Hellwig10ce4442006-01-11 20:48:14 +110038#include <linux/pagevec.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/writeback.h>
40
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000041void
Nathan Scottf51623b2006-03-14 13:26:27 +110042xfs_count_page_state(
43 struct page *page,
44 int *delalloc,
Nathan Scottf51623b2006-03-14 13:26:27 +110045 int *unwritten)
46{
47 struct buffer_head *bh, *head;
48
Christoph Hellwig20cb52e2010-06-24 09:46:01 +100049 *delalloc = *unwritten = 0;
Nathan Scottf51623b2006-03-14 13:26:27 +110050
51 bh = head = page_buffers(page);
52 do {
Christoph Hellwig20cb52e2010-06-24 09:46:01 +100053 if (buffer_unwritten(bh))
Nathan Scottf51623b2006-03-14 13:26:27 +110054 (*unwritten) = 1;
55 else if (buffer_delay(bh))
56 (*delalloc) = 1;
57 } while ((bh = bh->b_this_page) != head);
58}
59
Christoph Hellwig6214ed42007-09-14 15:23:17 +100060STATIC struct block_device *
61xfs_find_bdev_for_inode(
Christoph Hellwig046f1682010-04-28 12:28:52 +000062 struct inode *inode)
Christoph Hellwig6214ed42007-09-14 15:23:17 +100063{
Christoph Hellwig046f1682010-04-28 12:28:52 +000064 struct xfs_inode *ip = XFS_I(inode);
Christoph Hellwig6214ed42007-09-14 15:23:17 +100065 struct xfs_mount *mp = ip->i_mount;
66
Eric Sandeen71ddabb2007-11-23 16:29:42 +110067 if (XFS_IS_REALTIME_INODE(ip))
Christoph Hellwig6214ed42007-09-14 15:23:17 +100068 return mp->m_rtdev_targp->bt_bdev;
69 else
70 return mp->m_ddev_targp->bt_bdev;
71}
72
Christoph Hellwig0829c362005-09-02 16:58:49 +100073/*
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +110074 * We're now finished for good with this ioend structure.
75 * Update the page state via the associated buffer_heads,
76 * release holds on the inode and bio, and finally free
77 * up memory. Do not use the ioend after this.
78 */
Christoph Hellwig0829c362005-09-02 16:58:49 +100079STATIC void
80xfs_destroy_ioend(
81 xfs_ioend_t *ioend)
82{
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +110083 struct buffer_head *bh, *next;
84
85 for (bh = ioend->io_buffer_head; bh; bh = next) {
86 next = bh->b_private;
Nathan Scott7d04a332006-06-09 14:58:38 +100087 bh->b_end_io(bh, !ioend->io_error);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +110088 }
Christoph Hellwig583fa582008-12-03 12:20:38 +010089
Christoph Hellwigc859cdd2011-08-23 08:28:10 +000090 if (ioend->io_iocb) {
Christoph Hellwig04f658e2011-08-24 05:59:25 +000091 if (ioend->io_isasync) {
92 aio_complete(ioend->io_iocb, ioend->io_error ?
93 ioend->io_error : ioend->io_result, 0);
94 }
Christoph Hellwigc859cdd2011-08-23 08:28:10 +000095 inode_dio_done(ioend->io_inode);
96 }
Christoph Hellwig4a06fd22011-08-23 08:28:13 +000097
Christoph Hellwig0829c362005-09-02 16:58:49 +100098 mempool_free(ioend, xfs_ioend_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
100
101/*
Christoph Hellwigfc0063c2011-08-23 08:28:11 +0000102 * Fast and loose check if this write could update the on-disk inode size.
103 */
104static inline bool xfs_ioend_is_append(struct xfs_ioend *ioend)
105{
106 return ioend->io_offset + ioend->io_size >
107 XFS_I(ioend->io_inode)->i_d.di_size;
108}
109
110/*
Christoph Hellwig2813d682011-12-18 20:00:12 +0000111 * Update on-disk file size now that data has been written to disk.
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000112 */
Christoph Hellwigaa6bf012012-02-29 09:53:48 +0000113STATIC void
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000114xfs_setfilesize(
Christoph Hellwigaa6bf012012-02-29 09:53:48 +0000115 struct xfs_ioend *ioend)
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000116{
Christoph Hellwigaa6bf012012-02-29 09:53:48 +0000117 struct xfs_inode *ip = XFS_I(ioend->io_inode);
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000118 xfs_fsize_t isize;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000119
Christoph Hellwigaa6bf012012-02-29 09:53:48 +0000120 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig6923e682012-02-29 09:53:49 +0000121 isize = xfs_new_eof(ip, ioend->io_offset + ioend->io_size);
Dave Chinner932640e2009-10-06 20:29:29 +0000122 if (isize) {
Dave Chinner55fb25d52011-07-18 03:40:19 +0000123 trace_xfs_setfilesize(ip, ioend->io_offset, ioend->io_size);
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000124 ip->i_d.di_size = isize;
Christoph Hellwig66d834e2010-02-15 09:44:49 +0000125 xfs_mark_inode_dirty(ip);
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000126 }
127
128 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig0829c362005-09-02 16:58:49 +1000129}
130
131/*
Christoph Hellwig209fb872010-07-18 21:17:11 +0000132 * Schedule IO completion handling on the final put of an ioend.
Christoph Hellwigfc0063c2011-08-23 08:28:11 +0000133 *
134 * If there is no work to do we might as well call it a day and free the
135 * ioend right now.
Dave Chinnerc626d172009-04-06 18:42:11 +0200136 */
137STATIC void
138xfs_finish_ioend(
Christoph Hellwig209fb872010-07-18 21:17:11 +0000139 struct xfs_ioend *ioend)
Dave Chinnerc626d172009-04-06 18:42:11 +0200140{
141 if (atomic_dec_and_test(&ioend->io_remaining)) {
Christoph Hellwigaa6bf012012-02-29 09:53:48 +0000142 struct xfs_mount *mp = XFS_I(ioend->io_inode)->i_mount;
143
Christoph Hellwig209fb872010-07-18 21:17:11 +0000144 if (ioend->io_type == IO_UNWRITTEN)
Christoph Hellwigaa6bf012012-02-29 09:53:48 +0000145 queue_work(mp->m_unwritten_workqueue, &ioend->io_work);
Christoph Hellwigfc0063c2011-08-23 08:28:11 +0000146 else if (xfs_ioend_is_append(ioend))
Christoph Hellwigaa6bf012012-02-29 09:53:48 +0000147 queue_work(mp->m_data_workqueue, &ioend->io_work);
Christoph Hellwigfc0063c2011-08-23 08:28:11 +0000148 else
149 xfs_destroy_ioend(ioend);
Dave Chinnerc626d172009-04-06 18:42:11 +0200150 }
151}
152
153/*
Dave Chinner77d7a0c2010-02-17 05:36:29 +0000154 * IO write completion.
155 */
156STATIC void
157xfs_end_io(
158 struct work_struct *work)
159{
160 xfs_ioend_t *ioend = container_of(work, xfs_ioend_t, io_work);
161 struct xfs_inode *ip = XFS_I(ioend->io_inode);
Dave Chinner69418932010-03-04 00:57:09 +0000162 int error = 0;
Dave Chinner77d7a0c2010-02-17 05:36:29 +0000163
Christoph Hellwig04f658e2011-08-24 05:59:25 +0000164 if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
Christoph Hellwig810627d2011-11-08 08:56:15 +0000165 ioend->io_error = -EIO;
Christoph Hellwig04f658e2011-08-24 05:59:25 +0000166 goto done;
167 }
168 if (ioend->io_error)
169 goto done;
170
Dave Chinner77d7a0c2010-02-17 05:36:29 +0000171 /*
172 * For unwritten extents we need to issue transactions to convert a
173 * range to normal written extens after the data I/O has finished.
174 */
Christoph Hellwig04f658e2011-08-24 05:59:25 +0000175 if (ioend->io_type == IO_UNWRITTEN) {
Dave Chinner77d7a0c2010-02-17 05:36:29 +0000176 error = xfs_iomap_write_unwritten(ip, ioend->io_offset,
177 ioend->io_size);
Christoph Hellwig04f658e2011-08-24 05:59:25 +0000178 if (error) {
179 ioend->io_error = -error;
180 goto done;
181 }
Dave Chinner77d7a0c2010-02-17 05:36:29 +0000182 }
183
184 /*
185 * We might have to update the on-disk file size after extending
186 * writes.
187 */
Christoph Hellwigaa6bf012012-02-29 09:53:48 +0000188 xfs_setfilesize(ioend);
Christoph Hellwig04f658e2011-08-24 05:59:25 +0000189done:
Christoph Hellwigaa6bf012012-02-29 09:53:48 +0000190 xfs_destroy_ioend(ioend);
Dave Chinner77d7a0c2010-02-17 05:36:29 +0000191}
192
193/*
Christoph Hellwig209fb872010-07-18 21:17:11 +0000194 * Call IO completion handling in caller context on the final put of an ioend.
195 */
196STATIC void
197xfs_finish_ioend_sync(
198 struct xfs_ioend *ioend)
199{
200 if (atomic_dec_and_test(&ioend->io_remaining))
201 xfs_end_io(&ioend->io_work);
202}
203
204/*
Christoph Hellwig0829c362005-09-02 16:58:49 +1000205 * Allocate and initialise an IO completion structure.
206 * We need to track unwritten extent write completion here initially.
207 * We'll need to extend this for updating the ondisk inode size later
208 * (vs. incore size).
209 */
210STATIC xfs_ioend_t *
211xfs_alloc_ioend(
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100212 struct inode *inode,
213 unsigned int type)
Christoph Hellwig0829c362005-09-02 16:58:49 +1000214{
215 xfs_ioend_t *ioend;
216
217 ioend = mempool_alloc(xfs_ioend_pool, GFP_NOFS);
218
219 /*
220 * Set the count to 1 initially, which will prevent an I/O
221 * completion callback from happening before we have started
222 * all the I/O from calling the completion routine too early.
223 */
224 atomic_set(&ioend->io_remaining, 1);
Christoph Hellwigc859cdd2011-08-23 08:28:10 +0000225 ioend->io_isasync = 0;
Nathan Scott7d04a332006-06-09 14:58:38 +1000226 ioend->io_error = 0;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100227 ioend->io_list = NULL;
228 ioend->io_type = type;
Christoph Hellwigb677c212007-08-29 11:46:28 +1000229 ioend->io_inode = inode;
Christoph Hellwigc1a073b2005-09-05 08:23:35 +1000230 ioend->io_buffer_head = NULL;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100231 ioend->io_buffer_tail = NULL;
Christoph Hellwig0829c362005-09-02 16:58:49 +1000232 ioend->io_offset = 0;
233 ioend->io_size = 0;
Christoph Hellwigfb511f22010-07-18 21:17:10 +0000234 ioend->io_iocb = NULL;
235 ioend->io_result = 0;
Christoph Hellwig0829c362005-09-02 16:58:49 +1000236
Christoph Hellwig5ec4fab2009-10-30 09:11:47 +0000237 INIT_WORK(&ioend->io_work, xfs_end_io);
Christoph Hellwig0829c362005-09-02 16:58:49 +1000238 return ioend;
239}
240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241STATIC int
242xfs_map_blocks(
243 struct inode *inode,
244 loff_t offset,
Christoph Hellwig207d0412010-04-28 12:28:56 +0000245 struct xfs_bmbt_irec *imap,
Christoph Hellwiga206c812010-12-10 08:42:20 +0000246 int type,
247 int nonblocking)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
Christoph Hellwiga206c812010-12-10 08:42:20 +0000249 struct xfs_inode *ip = XFS_I(inode);
250 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwiged1e7b72010-12-10 08:42:22 +0000251 ssize_t count = 1 << inode->i_blkbits;
Christoph Hellwiga206c812010-12-10 08:42:20 +0000252 xfs_fileoff_t offset_fsb, end_fsb;
253 int error = 0;
Christoph Hellwiga206c812010-12-10 08:42:20 +0000254 int bmapi_flags = XFS_BMAPI_ENTIRE;
255 int nimaps = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Christoph Hellwiga206c812010-12-10 08:42:20 +0000257 if (XFS_FORCED_SHUTDOWN(mp))
258 return -XFS_ERROR(EIO);
259
Christoph Hellwig8ff29572010-12-10 08:42:21 +0000260 if (type == IO_UNWRITTEN)
Christoph Hellwiga206c812010-12-10 08:42:20 +0000261 bmapi_flags |= XFS_BMAPI_IGSTATE;
Christoph Hellwiga206c812010-12-10 08:42:20 +0000262
Christoph Hellwig8ff29572010-12-10 08:42:21 +0000263 if (!xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
264 if (nonblocking)
265 return -XFS_ERROR(EAGAIN);
266 xfs_ilock(ip, XFS_ILOCK_SHARED);
Christoph Hellwiga206c812010-12-10 08:42:20 +0000267 }
268
Christoph Hellwig8ff29572010-12-10 08:42:21 +0000269 ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
270 (ip->i_df.if_flags & XFS_IFEXTENTS));
Christoph Hellwiga206c812010-12-10 08:42:20 +0000271 ASSERT(offset <= mp->m_maxioffset);
Christoph Hellwig8ff29572010-12-10 08:42:21 +0000272
Christoph Hellwiga206c812010-12-10 08:42:20 +0000273 if (offset + count > mp->m_maxioffset)
274 count = mp->m_maxioffset - offset;
275 end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
276 offset_fsb = XFS_B_TO_FSBT(mp, offset);
Dave Chinner5c8ed202011-09-18 20:40:45 +0000277 error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb,
278 imap, &nimaps, bmapi_flags);
Christoph Hellwig8ff29572010-12-10 08:42:21 +0000279 xfs_iunlock(ip, XFS_ILOCK_SHARED);
280
Christoph Hellwiga206c812010-12-10 08:42:20 +0000281 if (error)
Christoph Hellwig8ff29572010-12-10 08:42:21 +0000282 return -XFS_ERROR(error);
Christoph Hellwiga206c812010-12-10 08:42:20 +0000283
Christoph Hellwig8ff29572010-12-10 08:42:21 +0000284 if (type == IO_DELALLOC &&
285 (!nimaps || isnullstartblock(imap->br_startblock))) {
Christoph Hellwiga206c812010-12-10 08:42:20 +0000286 error = xfs_iomap_write_allocate(ip, offset, count, imap);
287 if (!error)
288 trace_xfs_map_blocks_alloc(ip, offset, count, type, imap);
Christoph Hellwig8ff29572010-12-10 08:42:21 +0000289 return -XFS_ERROR(error);
Christoph Hellwiga206c812010-12-10 08:42:20 +0000290 }
291
Christoph Hellwig8ff29572010-12-10 08:42:21 +0000292#ifdef DEBUG
293 if (type == IO_UNWRITTEN) {
294 ASSERT(nimaps);
295 ASSERT(imap->br_startblock != HOLESTARTBLOCK);
296 ASSERT(imap->br_startblock != DELAYSTARTBLOCK);
297 }
298#endif
299 if (nimaps)
300 trace_xfs_map_blocks_found(ip, offset, count, type, imap);
301 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302}
303
Christoph Hellwigb8f82a42009-11-14 16:17:22 +0000304STATIC int
Christoph Hellwig558e6892010-04-28 12:28:58 +0000305xfs_imap_valid(
Christoph Hellwig8699bb02010-04-28 12:28:54 +0000306 struct inode *inode,
Christoph Hellwig207d0412010-04-28 12:28:56 +0000307 struct xfs_bmbt_irec *imap,
Christoph Hellwig558e6892010-04-28 12:28:58 +0000308 xfs_off_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
Christoph Hellwig558e6892010-04-28 12:28:58 +0000310 offset >>= inode->i_blkbits;
Christoph Hellwig8699bb02010-04-28 12:28:54 +0000311
Christoph Hellwig558e6892010-04-28 12:28:58 +0000312 return offset >= imap->br_startoff &&
313 offset < imap->br_startoff + imap->br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314}
315
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100316/*
317 * BIO completion handler for buffered IO.
318 */
Al Viro782e3b32007-10-12 07:17:47 +0100319STATIC void
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100320xfs_end_bio(
321 struct bio *bio,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100322 int error)
323{
324 xfs_ioend_t *ioend = bio->bi_private;
325
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100326 ASSERT(atomic_read(&bio->bi_cnt) >= 1);
Nathan Scott7d04a332006-06-09 14:58:38 +1000327 ioend->io_error = test_bit(BIO_UPTODATE, &bio->bi_flags) ? 0 : error;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100328
329 /* Toss bio and pass work off to an xfsdatad thread */
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100330 bio->bi_private = NULL;
331 bio->bi_end_io = NULL;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100332 bio_put(bio);
Nathan Scott7d04a332006-06-09 14:58:38 +1000333
Christoph Hellwig209fb872010-07-18 21:17:11 +0000334 xfs_finish_ioend(ioend);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100335}
336
337STATIC void
338xfs_submit_ioend_bio(
Christoph Hellwig06342cf2009-10-30 09:09:15 +0000339 struct writeback_control *wbc,
340 xfs_ioend_t *ioend,
341 struct bio *bio)
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100342{
Christoph Hellwig6923e682012-02-29 09:53:49 +0000343 struct xfs_inode *ip = XFS_I(ioend->io_inode);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100344 atomic_inc(&ioend->io_remaining);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100345 bio->bi_private = ioend;
346 bio->bi_end_io = xfs_end_bio;
347
Dave Chinner932640e2009-10-06 20:29:29 +0000348 /*
349 * If the I/O is beyond EOF we mark the inode dirty immediately
350 * but don't update the inode size until I/O completion.
351 */
Christoph Hellwig6923e682012-02-29 09:53:49 +0000352 if (xfs_new_eof(ip, ioend->io_offset + ioend->io_size))
353 xfs_mark_inode_dirty(ip);
Dave Chinner932640e2009-10-06 20:29:29 +0000354
Jens Axboe721a9602011-03-09 11:56:30 +0100355 submit_bio(wbc->sync_mode == WB_SYNC_ALL ? WRITE_SYNC : WRITE, bio);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100356}
357
358STATIC struct bio *
359xfs_alloc_ioend_bio(
360 struct buffer_head *bh)
361{
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100362 int nvecs = bio_get_nr_vecs(bh->b_bdev);
Christoph Hellwig221cb252010-12-10 08:42:17 +0000363 struct bio *bio = bio_alloc(GFP_NOIO, nvecs);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100364
365 ASSERT(bio->bi_private == NULL);
366 bio->bi_sector = bh->b_blocknr * (bh->b_size >> 9);
367 bio->bi_bdev = bh->b_bdev;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100368 return bio;
369}
370
371STATIC void
372xfs_start_buffer_writeback(
373 struct buffer_head *bh)
374{
375 ASSERT(buffer_mapped(bh));
376 ASSERT(buffer_locked(bh));
377 ASSERT(!buffer_delay(bh));
378 ASSERT(!buffer_unwritten(bh));
379
380 mark_buffer_async_write(bh);
381 set_buffer_uptodate(bh);
382 clear_buffer_dirty(bh);
383}
384
385STATIC void
386xfs_start_page_writeback(
387 struct page *page,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100388 int clear_dirty,
389 int buffers)
390{
391 ASSERT(PageLocked(page));
392 ASSERT(!PageWriteback(page));
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100393 if (clear_dirty)
David Chinner92132022006-12-21 10:24:01 +1100394 clear_page_dirty_for_io(page);
395 set_page_writeback(page);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100396 unlock_page(page);
Fengguang Wu1f7decf2007-10-16 23:30:42 -0700397 /* If no buffers on the page are to be written, finish it here */
398 if (!buffers)
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100399 end_page_writeback(page);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100400}
401
402static inline int bio_add_buffer(struct bio *bio, struct buffer_head *bh)
403{
404 return bio_add_page(bio, bh->b_page, bh->b_size, bh_offset(bh));
405}
406
407/*
David Chinnerd88992f2006-01-18 13:38:12 +1100408 * Submit all of the bios for all of the ioends we have saved up, covering the
409 * initial writepage page and also any probed pages.
410 *
411 * Because we may have multiple ioends spanning a page, we need to start
412 * writeback on all the buffers before we submit them for I/O. If we mark the
413 * buffers as we got, then we can end up with a page that only has buffers
414 * marked async write and I/O complete on can occur before we mark the other
415 * buffers async write.
416 *
417 * The end result of this is that we trip a bug in end_page_writeback() because
418 * we call it twice for the one page as the code in end_buffer_async_write()
419 * assumes that all buffers on the page are started at the same time.
420 *
421 * The fix is two passes across the ioend list - one to start writeback on the
Nathan Scottc41564b2006-03-29 08:55:14 +1000422 * buffer_heads, and then submit them for I/O on the second pass.
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100423 */
424STATIC void
425xfs_submit_ioend(
Christoph Hellwig06342cf2009-10-30 09:09:15 +0000426 struct writeback_control *wbc,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100427 xfs_ioend_t *ioend)
428{
David Chinnerd88992f2006-01-18 13:38:12 +1100429 xfs_ioend_t *head = ioend;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100430 xfs_ioend_t *next;
431 struct buffer_head *bh;
432 struct bio *bio;
433 sector_t lastblock = 0;
434
David Chinnerd88992f2006-01-18 13:38:12 +1100435 /* Pass 1 - start writeback */
436 do {
437 next = ioend->io_list;
Christoph Hellwig221cb252010-12-10 08:42:17 +0000438 for (bh = ioend->io_buffer_head; bh; bh = bh->b_private)
David Chinnerd88992f2006-01-18 13:38:12 +1100439 xfs_start_buffer_writeback(bh);
David Chinnerd88992f2006-01-18 13:38:12 +1100440 } while ((ioend = next) != NULL);
441
442 /* Pass 2 - submit I/O */
443 ioend = head;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100444 do {
445 next = ioend->io_list;
446 bio = NULL;
447
448 for (bh = ioend->io_buffer_head; bh; bh = bh->b_private) {
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100449
450 if (!bio) {
451 retry:
452 bio = xfs_alloc_ioend_bio(bh);
453 } else if (bh->b_blocknr != lastblock + 1) {
Christoph Hellwig06342cf2009-10-30 09:09:15 +0000454 xfs_submit_ioend_bio(wbc, ioend, bio);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100455 goto retry;
456 }
457
458 if (bio_add_buffer(bio, bh) != bh->b_size) {
Christoph Hellwig06342cf2009-10-30 09:09:15 +0000459 xfs_submit_ioend_bio(wbc, ioend, bio);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100460 goto retry;
461 }
462
463 lastblock = bh->b_blocknr;
464 }
465 if (bio)
Christoph Hellwig06342cf2009-10-30 09:09:15 +0000466 xfs_submit_ioend_bio(wbc, ioend, bio);
Christoph Hellwig209fb872010-07-18 21:17:11 +0000467 xfs_finish_ioend(ioend);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100468 } while ((ioend = next) != NULL);
469}
470
471/*
472 * Cancel submission of all buffer_heads so far in this endio.
473 * Toss the endio too. Only ever called for the initial page
474 * in a writepage request, so only ever one page.
475 */
476STATIC void
477xfs_cancel_ioend(
478 xfs_ioend_t *ioend)
479{
480 xfs_ioend_t *next;
481 struct buffer_head *bh, *next_bh;
482
483 do {
484 next = ioend->io_list;
485 bh = ioend->io_buffer_head;
486 do {
487 next_bh = bh->b_private;
488 clear_buffer_async_write(bh);
489 unlock_buffer(bh);
490 } while ((bh = next_bh) != NULL);
491
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100492 mempool_free(ioend, xfs_ioend_pool);
493 } while ((ioend = next) != NULL);
494}
495
496/*
497 * Test to see if we've been building up a completion structure for
498 * earlier buffers -- if so, we try to append to this ioend if we
499 * can, otherwise we finish off any current ioend and start another.
500 * Return true if we've finished the given ioend.
501 */
502STATIC void
503xfs_add_to_ioend(
504 struct inode *inode,
505 struct buffer_head *bh,
Christoph Hellwig7336cea2006-01-11 20:49:16 +1100506 xfs_off_t offset,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100507 unsigned int type,
508 xfs_ioend_t **result,
509 int need_ioend)
510{
511 xfs_ioend_t *ioend = *result;
512
513 if (!ioend || need_ioend || type != ioend->io_type) {
514 xfs_ioend_t *previous = *result;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100515
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100516 ioend = xfs_alloc_ioend(inode, type);
517 ioend->io_offset = offset;
518 ioend->io_buffer_head = bh;
519 ioend->io_buffer_tail = bh;
520 if (previous)
521 previous->io_list = ioend;
522 *result = ioend;
523 } else {
524 ioend->io_buffer_tail->b_private = bh;
525 ioend->io_buffer_tail = bh;
526 }
527
528 bh->b_private = NULL;
529 ioend->io_size += bh->b_size;
530}
531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532STATIC void
Nathan Scott87cbc492006-03-14 13:26:43 +1100533xfs_map_buffer(
Christoph Hellwig046f1682010-04-28 12:28:52 +0000534 struct inode *inode,
Nathan Scott87cbc492006-03-14 13:26:43 +1100535 struct buffer_head *bh,
Christoph Hellwig207d0412010-04-28 12:28:56 +0000536 struct xfs_bmbt_irec *imap,
Christoph Hellwig046f1682010-04-28 12:28:52 +0000537 xfs_off_t offset)
Nathan Scott87cbc492006-03-14 13:26:43 +1100538{
539 sector_t bn;
Christoph Hellwig8699bb02010-04-28 12:28:54 +0000540 struct xfs_mount *m = XFS_I(inode)->i_mount;
Christoph Hellwig207d0412010-04-28 12:28:56 +0000541 xfs_off_t iomap_offset = XFS_FSB_TO_B(m, imap->br_startoff);
542 xfs_daddr_t iomap_bn = xfs_fsb_to_db(XFS_I(inode), imap->br_startblock);
Nathan Scott87cbc492006-03-14 13:26:43 +1100543
Christoph Hellwig207d0412010-04-28 12:28:56 +0000544 ASSERT(imap->br_startblock != HOLESTARTBLOCK);
545 ASSERT(imap->br_startblock != DELAYSTARTBLOCK);
Nathan Scott87cbc492006-03-14 13:26:43 +1100546
Christoph Hellwige5131822010-04-28 12:28:55 +0000547 bn = (iomap_bn >> (inode->i_blkbits - BBSHIFT)) +
Christoph Hellwig8699bb02010-04-28 12:28:54 +0000548 ((offset - iomap_offset) >> inode->i_blkbits);
Nathan Scott87cbc492006-03-14 13:26:43 +1100549
Christoph Hellwig046f1682010-04-28 12:28:52 +0000550 ASSERT(bn || XFS_IS_REALTIME_INODE(XFS_I(inode)));
Nathan Scott87cbc492006-03-14 13:26:43 +1100551
552 bh->b_blocknr = bn;
553 set_buffer_mapped(bh);
554}
555
556STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557xfs_map_at_offset(
Christoph Hellwig046f1682010-04-28 12:28:52 +0000558 struct inode *inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 struct buffer_head *bh,
Christoph Hellwig207d0412010-04-28 12:28:56 +0000560 struct xfs_bmbt_irec *imap,
Christoph Hellwig046f1682010-04-28 12:28:52 +0000561 xfs_off_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
Christoph Hellwig207d0412010-04-28 12:28:56 +0000563 ASSERT(imap->br_startblock != HOLESTARTBLOCK);
564 ASSERT(imap->br_startblock != DELAYSTARTBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Christoph Hellwig207d0412010-04-28 12:28:56 +0000566 xfs_map_buffer(inode, bh, imap, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 set_buffer_mapped(bh);
568 clear_buffer_delay(bh);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100569 clear_buffer_unwritten(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570}
571
572/*
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100573 * Test if a given page is suitable for writing as part of an unwritten
574 * or delayed allocate extent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 */
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100576STATIC int
577xfs_is_delayed_page(
578 struct page *page,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100579 unsigned int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 if (PageWriteback(page))
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100582 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584 if (page->mapping && page_has_buffers(page)) {
585 struct buffer_head *bh, *head;
586 int acceptable = 0;
587
588 bh = head = page_buffers(page);
589 do {
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100590 if (buffer_unwritten(bh))
Christoph Hellwig34a52c62010-04-28 12:28:57 +0000591 acceptable = (type == IO_UNWRITTEN);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100592 else if (buffer_delay(bh))
Christoph Hellwiga206c812010-12-10 08:42:20 +0000593 acceptable = (type == IO_DELALLOC);
David Chinner2ddee842006-03-22 12:47:40 +1100594 else if (buffer_dirty(bh) && buffer_mapped(bh))
Christoph Hellwiga206c812010-12-10 08:42:20 +0000595 acceptable = (type == IO_OVERWRITE);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100596 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 } while ((bh = bh->b_this_page) != head);
599
600 if (acceptable)
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100601 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 }
603
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100604 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605}
606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607/*
608 * Allocate & map buffers for page given the extent map. Write it out.
609 * except for the original page of a writepage, this is called on
610 * delalloc/unwritten pages only, for the original page it is possible
611 * that the page has no mapping at all.
612 */
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100613STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614xfs_convert_page(
615 struct inode *inode,
616 struct page *page,
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100617 loff_t tindex,
Christoph Hellwig207d0412010-04-28 12:28:56 +0000618 struct xfs_bmbt_irec *imap,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100619 xfs_ioend_t **ioendp,
Christoph Hellwig2fa24f92010-12-10 08:42:23 +0000620 struct writeback_control *wbc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100622 struct buffer_head *bh, *head;
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100623 xfs_off_t end_offset;
624 unsigned long p_offset;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100625 unsigned int type;
Nathan Scott24e17b52005-05-05 13:33:20 -0700626 int len, page_dirty;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100627 int count = 0, done = 0, uptodate = 1;
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100628 xfs_off_t offset = page_offset(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100630 if (page->index != tindex)
631 goto fail;
Nick Piggin529ae9a2008-08-02 12:01:03 +0200632 if (!trylock_page(page))
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100633 goto fail;
634 if (PageWriteback(page))
635 goto fail_unlock_page;
636 if (page->mapping != inode->i_mapping)
637 goto fail_unlock_page;
638 if (!xfs_is_delayed_page(page, (*ioendp)->io_type))
639 goto fail_unlock_page;
640
Nathan Scott24e17b52005-05-05 13:33:20 -0700641 /*
642 * page_dirty is initially a count of buffers on the page before
Nathan Scottc41564b2006-03-29 08:55:14 +1000643 * EOF and is decremented as we move each into a cleanable state.
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100644 *
645 * Derivation:
646 *
647 * End offset is the highest offset that this page should represent.
648 * If we are on the last page, (end_offset & (PAGE_CACHE_SIZE - 1))
649 * will evaluate non-zero and be less than PAGE_CACHE_SIZE and
650 * hence give us the correct page_dirty count. On any other page,
651 * it will be zero and in that case we need page_dirty to be the
652 * count of buffers on the page.
Nathan Scott24e17b52005-05-05 13:33:20 -0700653 */
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100654 end_offset = min_t(unsigned long long,
655 (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT,
656 i_size_read(inode));
657
Nathan Scott24e17b52005-05-05 13:33:20 -0700658 len = 1 << inode->i_blkbits;
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100659 p_offset = min_t(unsigned long, end_offset & (PAGE_CACHE_SIZE - 1),
660 PAGE_CACHE_SIZE);
661 p_offset = p_offset ? roundup(p_offset, len) : PAGE_CACHE_SIZE;
662 page_dirty = p_offset / len;
Nathan Scott24e17b52005-05-05 13:33:20 -0700663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 bh = head = page_buffers(page);
665 do {
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100666 if (offset >= end_offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 break;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100668 if (!buffer_uptodate(bh))
669 uptodate = 0;
670 if (!(PageUptodate(page) || buffer_uptodate(bh))) {
671 done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 continue;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100673 }
674
Christoph Hellwig2fa24f92010-12-10 08:42:23 +0000675 if (buffer_unwritten(bh) || buffer_delay(bh) ||
676 buffer_mapped(bh)) {
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100677 if (buffer_unwritten(bh))
Christoph Hellwig34a52c62010-04-28 12:28:57 +0000678 type = IO_UNWRITTEN;
Christoph Hellwig2fa24f92010-12-10 08:42:23 +0000679 else if (buffer_delay(bh))
Christoph Hellwiga206c812010-12-10 08:42:20 +0000680 type = IO_DELALLOC;
Christoph Hellwig2fa24f92010-12-10 08:42:23 +0000681 else
682 type = IO_OVERWRITE;
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100683
Christoph Hellwig558e6892010-04-28 12:28:58 +0000684 if (!xfs_imap_valid(inode, imap, offset)) {
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100685 done = 1;
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100686 continue;
687 }
688
Christoph Hellwigecff71e2010-12-10 08:42:25 +0000689 lock_buffer(bh);
690 if (type != IO_OVERWRITE)
Christoph Hellwig2fa24f92010-12-10 08:42:23 +0000691 xfs_map_at_offset(inode, bh, imap, offset);
Christoph Hellwig89f3b362010-06-24 09:45:48 +1000692 xfs_add_to_ioend(inode, bh, offset, type,
693 ioendp, done);
694
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100695 page_dirty--;
696 count++;
697 } else {
Christoph Hellwig2fa24f92010-12-10 08:42:23 +0000698 done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 }
Christoph Hellwig7336cea2006-01-11 20:49:16 +1100700 } while (offset += len, (bh = bh->b_this_page) != head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100702 if (uptodate && bh == head)
703 SetPageUptodate(page);
704
Christoph Hellwig89f3b362010-06-24 09:45:48 +1000705 if (count) {
Dave Chinnerefceab12010-08-24 11:44:56 +1000706 if (--wbc->nr_to_write <= 0 &&
707 wbc->sync_mode == WB_SYNC_NONE)
Christoph Hellwig89f3b362010-06-24 09:45:48 +1000708 done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 }
Christoph Hellwig89f3b362010-06-24 09:45:48 +1000710 xfs_start_page_writeback(page, !page_dirty, count);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100711
712 return done;
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100713 fail_unlock_page:
714 unlock_page(page);
715 fail:
716 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717}
718
719/*
720 * Convert & write out a cluster of pages in the same extent as defined
721 * by mp and following the start page.
722 */
723STATIC void
724xfs_cluster_write(
725 struct inode *inode,
726 pgoff_t tindex,
Christoph Hellwig207d0412010-04-28 12:28:56 +0000727 struct xfs_bmbt_irec *imap,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100728 xfs_ioend_t **ioendp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 struct writeback_control *wbc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 pgoff_t tlast)
731{
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100732 struct pagevec pvec;
733 int done = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100735 pagevec_init(&pvec, 0);
736 while (!done && tindex <= tlast) {
737 unsigned len = min_t(pgoff_t, PAGEVEC_SIZE, tlast - tindex + 1);
738
739 if (!pagevec_lookup(&pvec, inode->i_mapping, tindex, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 break;
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100741
742 for (i = 0; i < pagevec_count(&pvec); i++) {
743 done = xfs_convert_page(inode, pvec.pages[i], tindex++,
Christoph Hellwig2fa24f92010-12-10 08:42:23 +0000744 imap, ioendp, wbc);
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100745 if (done)
746 break;
747 }
748
749 pagevec_release(&pvec);
750 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 }
752}
753
Dave Chinner3ed3a432010-03-05 02:00:42 +0000754STATIC void
755xfs_vm_invalidatepage(
756 struct page *page,
757 unsigned long offset)
758{
759 trace_xfs_invalidatepage(page->mapping->host, page, offset);
760 block_invalidatepage(page, offset);
761}
762
763/*
764 * If the page has delalloc buffers on it, we need to punch them out before we
765 * invalidate the page. If we don't, we leave a stale delalloc mapping on the
766 * inode that can trip a BUG() in xfs_get_blocks() later on if a direct IO read
767 * is done on that same region - the delalloc extent is returned when none is
768 * supposed to be there.
769 *
770 * We prevent this by truncating away the delalloc regions on the page before
771 * invalidating it. Because they are delalloc, we can do this without needing a
772 * transaction. Indeed - if we get ENOSPC errors, we have to be able to do this
773 * truncation without a transaction as there is no space left for block
774 * reservation (typically why we see a ENOSPC in writeback).
775 *
776 * This is not a performance critical path, so for now just do the punching a
777 * buffer head at a time.
778 */
779STATIC void
780xfs_aops_discard_page(
781 struct page *page)
782{
783 struct inode *inode = page->mapping->host;
784 struct xfs_inode *ip = XFS_I(inode);
785 struct buffer_head *bh, *head;
786 loff_t offset = page_offset(page);
Dave Chinner3ed3a432010-03-05 02:00:42 +0000787
Christoph Hellwiga206c812010-12-10 08:42:20 +0000788 if (!xfs_is_delayed_page(page, IO_DELALLOC))
Dave Chinner3ed3a432010-03-05 02:00:42 +0000789 goto out_invalidate;
790
Dave Chinnere8c37532010-03-15 02:36:35 +0000791 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
792 goto out_invalidate;
793
Dave Chinner4f107002011-03-07 10:00:35 +1100794 xfs_alert(ip->i_mount,
Dave Chinner3ed3a432010-03-05 02:00:42 +0000795 "page discard on page %p, inode 0x%llx, offset %llu.",
796 page, ip->i_ino, offset);
797
798 xfs_ilock(ip, XFS_ILOCK_EXCL);
799 bh = head = page_buffers(page);
800 do {
Dave Chinner3ed3a432010-03-05 02:00:42 +0000801 int error;
Dave Chinnerc726de42010-11-30 15:14:39 +1100802 xfs_fileoff_t start_fsb;
Dave Chinner3ed3a432010-03-05 02:00:42 +0000803
804 if (!buffer_delay(bh))
805 goto next_buffer;
806
Dave Chinnerc726de42010-11-30 15:14:39 +1100807 start_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
808 error = xfs_bmap_punch_delalloc_range(ip, start_fsb, 1);
Dave Chinner3ed3a432010-03-05 02:00:42 +0000809 if (error) {
810 /* something screwed, just bail */
Dave Chinnere8c37532010-03-15 02:36:35 +0000811 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
Dave Chinner4f107002011-03-07 10:00:35 +1100812 xfs_alert(ip->i_mount,
Dave Chinner3ed3a432010-03-05 02:00:42 +0000813 "page discard unable to remove delalloc mapping.");
Dave Chinnere8c37532010-03-15 02:36:35 +0000814 }
Dave Chinner3ed3a432010-03-05 02:00:42 +0000815 break;
816 }
817next_buffer:
Dave Chinnerc726de42010-11-30 15:14:39 +1100818 offset += 1 << inode->i_blkbits;
Dave Chinner3ed3a432010-03-05 02:00:42 +0000819
820 } while ((bh = bh->b_this_page) != head);
821
822 xfs_iunlock(ip, XFS_ILOCK_EXCL);
823out_invalidate:
824 xfs_vm_invalidatepage(page, 0);
825 return;
826}
827
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828/*
Christoph Hellwig89f3b362010-06-24 09:45:48 +1000829 * Write out a dirty page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 *
Christoph Hellwig89f3b362010-06-24 09:45:48 +1000831 * For delalloc space on the page we need to allocate space and flush it.
832 * For unwritten space on the page we need to start the conversion to
833 * regular allocated space.
Christoph Hellwig89f3b362010-06-24 09:45:48 +1000834 * For any other dirty buffer heads on the page we should flush them.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836STATIC int
Christoph Hellwig89f3b362010-06-24 09:45:48 +1000837xfs_vm_writepage(
838 struct page *page,
839 struct writeback_control *wbc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
Christoph Hellwig89f3b362010-06-24 09:45:48 +1000841 struct inode *inode = page->mapping->host;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100842 struct buffer_head *bh, *head;
Christoph Hellwig207d0412010-04-28 12:28:56 +0000843 struct xfs_bmbt_irec imap;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100844 xfs_ioend_t *ioend = NULL, *iohead = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 loff_t offset;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100846 unsigned int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 __uint64_t end_offset;
Christoph Hellwigbd1556a2010-04-28 12:29:00 +0000848 pgoff_t end_index, last_index;
Christoph Hellwiged1e7b72010-12-10 08:42:22 +0000849 ssize_t len;
Christoph Hellwiga206c812010-12-10 08:42:20 +0000850 int err, imap_valid = 0, uptodate = 1;
Christoph Hellwig89f3b362010-06-24 09:45:48 +1000851 int count = 0;
Christoph Hellwiga206c812010-12-10 08:42:20 +0000852 int nonblocking = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
Christoph Hellwig89f3b362010-06-24 09:45:48 +1000854 trace_xfs_writepage(inode, page, 0);
855
Christoph Hellwig20cb52e2010-06-24 09:46:01 +1000856 ASSERT(page_has_buffers(page));
857
Christoph Hellwig89f3b362010-06-24 09:45:48 +1000858 /*
859 * Refuse to write the page out if we are called from reclaim context.
860 *
Christoph Hellwigd4f7a5c2010-06-28 10:34:44 -0400861 * This avoids stack overflows when called from deeply used stacks in
862 * random callers for direct reclaim or memcg reclaim. We explicitly
863 * allow reclaim from kswapd as the stack usage there is relatively low.
Christoph Hellwig89f3b362010-06-24 09:45:48 +1000864 *
Mel Gorman94054fa2011-10-31 17:07:45 -0700865 * This should never happen except in the case of a VM regression so
866 * warn about it.
Christoph Hellwig89f3b362010-06-24 09:45:48 +1000867 */
Mel Gorman94054fa2011-10-31 17:07:45 -0700868 if (WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD)) ==
869 PF_MEMALLOC))
Christoph Hellwigb5420f22010-08-24 11:47:51 +1000870 goto redirty;
Christoph Hellwig89f3b362010-06-24 09:45:48 +1000871
872 /*
Christoph Hellwig680a6472011-07-08 14:34:05 +0200873 * Given that we do not allow direct reclaim to call us, we should
874 * never be called while in a filesystem transaction.
Christoph Hellwig89f3b362010-06-24 09:45:48 +1000875 */
Christoph Hellwig680a6472011-07-08 14:34:05 +0200876 if (WARN_ON(current->flags & PF_FSTRANS))
Christoph Hellwigb5420f22010-08-24 11:47:51 +1000877 goto redirty;
Christoph Hellwig89f3b362010-06-24 09:45:48 +1000878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 /* Is this page beyond the end of the file? */
880 offset = i_size_read(inode);
881 end_index = offset >> PAGE_CACHE_SHIFT;
882 last_index = (offset - 1) >> PAGE_CACHE_SHIFT;
883 if (page->index >= end_index) {
884 if ((page->index >= end_index + 1) ||
885 !(i_size_read(inode) & (PAGE_CACHE_SIZE - 1))) {
Christoph Hellwig89f3b362010-06-24 09:45:48 +1000886 unlock_page(page);
Nathan Scott19d5bcf2005-11-02 15:14:09 +1100887 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 }
889 }
890
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100891 end_offset = min_t(unsigned long long,
Christoph Hellwig20cb52e2010-06-24 09:46:01 +1000892 (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT,
893 offset);
Nathan Scott24e17b52005-05-05 13:33:20 -0700894 len = 1 << inode->i_blkbits;
Nathan Scott24e17b52005-05-05 13:33:20 -0700895
Nathan Scott24e17b52005-05-05 13:33:20 -0700896 bh = head = page_buffers(page);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100897 offset = page_offset(page);
Christoph Hellwiga206c812010-12-10 08:42:20 +0000898 type = IO_OVERWRITE;
899
Christoph Hellwigdbcdde32011-07-08 14:34:14 +0200900 if (wbc->sync_mode == WB_SYNC_NONE)
Christoph Hellwiga206c812010-12-10 08:42:20 +0000901 nonblocking = 1;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 do {
Christoph Hellwig6ac72482010-12-10 08:42:18 +0000904 int new_ioend = 0;
905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 if (offset >= end_offset)
907 break;
908 if (!buffer_uptodate(bh))
909 uptodate = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Eric Sandeen3d9b02e2010-06-24 09:45:30 +1000911 /*
Christoph Hellwigece413f2010-11-10 21:39:11 +0000912 * set_page_dirty dirties all buffers in a page, independent
913 * of their state. The dirty state however is entirely
914 * meaningless for holes (!mapped && uptodate), so skip
915 * buffers covering holes here.
Eric Sandeen3d9b02e2010-06-24 09:45:30 +1000916 */
917 if (!buffer_mapped(bh) && buffer_uptodate(bh)) {
Eric Sandeen3d9b02e2010-06-24 09:45:30 +1000918 imap_valid = 0;
919 continue;
920 }
921
Christoph Hellwigaeea1b12010-12-10 08:42:24 +0000922 if (buffer_unwritten(bh)) {
923 if (type != IO_UNWRITTEN) {
924 type = IO_UNWRITTEN;
925 imap_valid = 0;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100926 }
Christoph Hellwigaeea1b12010-12-10 08:42:24 +0000927 } else if (buffer_delay(bh)) {
928 if (type != IO_DELALLOC) {
929 type = IO_DELALLOC;
930 imap_valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 }
Christoph Hellwig89f3b362010-06-24 09:45:48 +1000932 } else if (buffer_uptodate(bh)) {
Christoph Hellwiga206c812010-12-10 08:42:20 +0000933 if (type != IO_OVERWRITE) {
934 type = IO_OVERWRITE;
Christoph Hellwig85da94c2010-12-10 08:42:16 +0000935 imap_valid = 0;
936 }
Christoph Hellwigaeea1b12010-12-10 08:42:24 +0000937 } else {
938 if (PageUptodate(page)) {
939 ASSERT(buffer_mapped(bh));
940 imap_valid = 0;
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100941 }
Christoph Hellwigaeea1b12010-12-10 08:42:24 +0000942 continue;
943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Christoph Hellwigaeea1b12010-12-10 08:42:24 +0000945 if (imap_valid)
946 imap_valid = xfs_imap_valid(inode, &imap, offset);
947 if (!imap_valid) {
948 /*
949 * If we didn't have a valid mapping then we need to
950 * put the new mapping into a separate ioend structure.
951 * This ensures non-contiguous extents always have
952 * separate ioends, which is particularly important
953 * for unwritten extent conversion at I/O completion
954 * time.
955 */
956 new_ioend = 1;
957 err = xfs_map_blocks(inode, offset, &imap, type,
958 nonblocking);
959 if (err)
960 goto error;
961 imap_valid = xfs_imap_valid(inode, &imap, offset);
962 }
963 if (imap_valid) {
Christoph Hellwigecff71e2010-12-10 08:42:25 +0000964 lock_buffer(bh);
965 if (type != IO_OVERWRITE)
Christoph Hellwigaeea1b12010-12-10 08:42:24 +0000966 xfs_map_at_offset(inode, bh, &imap, offset);
967 xfs_add_to_ioend(inode, bh, offset, type, &ioend,
968 new_ioend);
969 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 }
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100971
972 if (!iohead)
973 iohead = ioend;
974
975 } while (offset += len, ((bh = bh->b_this_page) != head));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
977 if (uptodate && bh == head)
978 SetPageUptodate(page);
979
Christoph Hellwig89f3b362010-06-24 09:45:48 +1000980 xfs_start_page_writeback(page, 1, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Christoph Hellwig558e6892010-04-28 12:28:58 +0000982 if (ioend && imap_valid) {
Christoph Hellwigbd1556a2010-04-28 12:29:00 +0000983 xfs_off_t end_index;
Christoph Hellwig8699bb02010-04-28 12:28:54 +0000984
Christoph Hellwigbd1556a2010-04-28 12:29:00 +0000985 end_index = imap.br_startoff + imap.br_blockcount;
986
987 /* to bytes */
988 end_index <<= inode->i_blkbits;
989
990 /* to pages */
991 end_index = (end_index - 1) >> PAGE_CACHE_SHIFT;
992
993 /* check against file size */
994 if (end_index > last_index)
995 end_index = last_index;
996
Christoph Hellwig207d0412010-04-28 12:28:56 +0000997 xfs_cluster_write(inode, page->index + 1, &imap, &ioend,
Christoph Hellwig2fa24f92010-12-10 08:42:23 +0000998 wbc, end_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 }
1000
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001001 if (iohead)
Christoph Hellwig06342cf2009-10-30 09:09:15 +00001002 xfs_submit_ioend(wbc, iohead);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001003
Christoph Hellwig89f3b362010-06-24 09:45:48 +10001004 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
1006error:
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001007 if (iohead)
1008 xfs_cancel_ioend(iohead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
Christoph Hellwigb5420f22010-08-24 11:47:51 +10001010 if (err == -EAGAIN)
1011 goto redirty;
1012
Christoph Hellwig20cb52e2010-06-24 09:46:01 +10001013 xfs_aops_discard_page(page);
Christoph Hellwig89f3b362010-06-24 09:45:48 +10001014 ClearPageUptodate(page);
1015 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 return err;
Nathan Scottf51623b2006-03-14 13:26:27 +11001017
Christoph Hellwigb5420f22010-08-24 11:47:51 +10001018redirty:
Nathan Scottf51623b2006-03-14 13:26:27 +11001019 redirty_page_for_writepage(wbc, page);
1020 unlock_page(page);
1021 return 0;
Nathan Scottf51623b2006-03-14 13:26:27 +11001022}
1023
Nathan Scott7d4fb402006-06-09 15:27:16 +10001024STATIC int
1025xfs_vm_writepages(
1026 struct address_space *mapping,
1027 struct writeback_control *wbc)
1028{
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +10001029 xfs_iflags_clear(XFS_I(mapping->host), XFS_ITRUNCATED);
Nathan Scott7d4fb402006-06-09 15:27:16 +10001030 return generic_writepages(mapping, wbc);
1031}
1032
Nathan Scottf51623b2006-03-14 13:26:27 +11001033/*
1034 * Called to move a page into cleanable state - and from there
Christoph Hellwig89f3b362010-06-24 09:45:48 +10001035 * to be released. The page should already be clean. We always
Nathan Scottf51623b2006-03-14 13:26:27 +11001036 * have buffer heads in this call.
1037 *
Christoph Hellwig89f3b362010-06-24 09:45:48 +10001038 * Returns 1 if the page is ok to release, 0 otherwise.
Nathan Scottf51623b2006-03-14 13:26:27 +11001039 */
1040STATIC int
Nathan Scott238f4c52006-03-17 17:26:25 +11001041xfs_vm_releasepage(
Nathan Scottf51623b2006-03-14 13:26:27 +11001042 struct page *page,
1043 gfp_t gfp_mask)
1044{
Christoph Hellwig20cb52e2010-06-24 09:46:01 +10001045 int delalloc, unwritten;
Nathan Scottf51623b2006-03-14 13:26:27 +11001046
Christoph Hellwig89f3b362010-06-24 09:45:48 +10001047 trace_xfs_releasepage(page->mapping->host, page, 0);
Nathan Scott238f4c52006-03-17 17:26:25 +11001048
Christoph Hellwig20cb52e2010-06-24 09:46:01 +10001049 xfs_count_page_state(page, &delalloc, &unwritten);
Nathan Scottf51623b2006-03-14 13:26:27 +11001050
Christoph Hellwig89f3b362010-06-24 09:45:48 +10001051 if (WARN_ON(delalloc))
1052 return 0;
1053 if (WARN_ON(unwritten))
Nathan Scottf51623b2006-03-14 13:26:27 +11001054 return 0;
1055
Nathan Scottf51623b2006-03-14 13:26:27 +11001056 return try_to_free_buffers(page);
1057}
1058
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059STATIC int
Nathan Scottc2536662006-03-29 10:44:40 +10001060__xfs_get_blocks(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 struct inode *inode,
1062 sector_t iblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 struct buffer_head *bh_result,
1064 int create,
Christoph Hellwigf2bde9b2010-06-24 11:44:35 +10001065 int direct)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066{
Christoph Hellwiga206c812010-12-10 08:42:20 +00001067 struct xfs_inode *ip = XFS_I(inode);
1068 struct xfs_mount *mp = ip->i_mount;
1069 xfs_fileoff_t offset_fsb, end_fsb;
1070 int error = 0;
1071 int lockmode = 0;
Christoph Hellwig207d0412010-04-28 12:28:56 +00001072 struct xfs_bmbt_irec imap;
Christoph Hellwiga206c812010-12-10 08:42:20 +00001073 int nimaps = 1;
Nathan Scottfdc7ed72005-11-02 15:13:13 +11001074 xfs_off_t offset;
1075 ssize_t size;
Christoph Hellwig207d0412010-04-28 12:28:56 +00001076 int new = 0;
Christoph Hellwiga206c812010-12-10 08:42:20 +00001077
1078 if (XFS_FORCED_SHUTDOWN(mp))
1079 return -XFS_ERROR(EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Nathan Scottfdc7ed72005-11-02 15:13:13 +11001081 offset = (xfs_off_t)iblock << inode->i_blkbits;
Nathan Scottc2536662006-03-29 10:44:40 +10001082 ASSERT(bh_result->b_size >= (1 << inode->i_blkbits));
1083 size = bh_result->b_size;
Lachlan McIlroy364f3582008-09-17 16:50:14 +10001084
1085 if (!create && direct && offset >= i_size_read(inode))
1086 return 0;
1087
Christoph Hellwiga206c812010-12-10 08:42:20 +00001088 if (create) {
1089 lockmode = XFS_ILOCK_EXCL;
1090 xfs_ilock(ip, lockmode);
1091 } else {
1092 lockmode = xfs_ilock_map_shared(ip);
1093 }
Christoph Hellwigf2bde9b2010-06-24 11:44:35 +10001094
Christoph Hellwiga206c812010-12-10 08:42:20 +00001095 ASSERT(offset <= mp->m_maxioffset);
1096 if (offset + size > mp->m_maxioffset)
1097 size = mp->m_maxioffset - offset;
1098 end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + size);
1099 offset_fsb = XFS_B_TO_FSBT(mp, offset);
1100
Dave Chinner5c8ed202011-09-18 20:40:45 +00001101 error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb,
1102 &imap, &nimaps, XFS_BMAPI_ENTIRE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 if (error)
Christoph Hellwiga206c812010-12-10 08:42:20 +00001104 goto out_unlock;
1105
1106 if (create &&
1107 (!nimaps ||
1108 (imap.br_startblock == HOLESTARTBLOCK ||
1109 imap.br_startblock == DELAYSTARTBLOCK))) {
1110 if (direct) {
1111 error = xfs_iomap_write_direct(ip, offset, size,
1112 &imap, nimaps);
1113 } else {
1114 error = xfs_iomap_write_delay(ip, offset, size, &imap);
1115 }
1116 if (error)
1117 goto out_unlock;
1118
1119 trace_xfs_get_blocks_alloc(ip, offset, size, 0, &imap);
1120 } else if (nimaps) {
1121 trace_xfs_get_blocks_found(ip, offset, size, 0, &imap);
1122 } else {
1123 trace_xfs_get_blocks_notfound(ip, offset, size);
1124 goto out_unlock;
1125 }
1126 xfs_iunlock(ip, lockmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Christoph Hellwig207d0412010-04-28 12:28:56 +00001128 if (imap.br_startblock != HOLESTARTBLOCK &&
1129 imap.br_startblock != DELAYSTARTBLOCK) {
Nathan Scott87cbc492006-03-14 13:26:43 +11001130 /*
1131 * For unwritten extents do not report a disk address on
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 * the read case (treat as if we're reading into a hole).
1133 */
Christoph Hellwig207d0412010-04-28 12:28:56 +00001134 if (create || !ISUNWRITTEN(&imap))
1135 xfs_map_buffer(inode, bh_result, &imap, offset);
1136 if (create && ISUNWRITTEN(&imap)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 if (direct)
1138 bh_result->b_private = inode;
1139 set_buffer_unwritten(bh_result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 }
1141 }
1142
Nathan Scottc2536662006-03-29 10:44:40 +10001143 /*
1144 * If this is a realtime file, data may be on a different device.
1145 * to that pointed to from the buffer_head b_bdev currently.
1146 */
Christoph Hellwig046f1682010-04-28 12:28:52 +00001147 bh_result->b_bdev = xfs_find_bdev_for_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
Nathan Scottc2536662006-03-29 10:44:40 +10001149 /*
David Chinner549054a2007-02-10 18:36:35 +11001150 * If we previously allocated a block out beyond eof and we are now
1151 * coming back to use it then we will need to flag it as new even if it
1152 * has a disk address.
1153 *
1154 * With sub-block writes into unwritten extents we also need to mark
1155 * the buffer as new so that the unwritten parts of the buffer gets
1156 * correctly zeroed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 */
1158 if (create &&
1159 ((!buffer_mapped(bh_result) && !buffer_uptodate(bh_result)) ||
David Chinner549054a2007-02-10 18:36:35 +11001160 (offset >= i_size_read(inode)) ||
Christoph Hellwig207d0412010-04-28 12:28:56 +00001161 (new || ISUNWRITTEN(&imap))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 set_buffer_new(bh_result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Christoph Hellwig207d0412010-04-28 12:28:56 +00001164 if (imap.br_startblock == DELAYSTARTBLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 BUG_ON(direct);
1166 if (create) {
1167 set_buffer_uptodate(bh_result);
1168 set_buffer_mapped(bh_result);
1169 set_buffer_delay(bh_result);
1170 }
1171 }
1172
Christoph Hellwig2b8f12b2010-04-28 12:28:59 +00001173 /*
1174 * If this is O_DIRECT or the mpage code calling tell them how large
1175 * the mapping is, so that we can avoid repeated get_blocks calls.
1176 */
Nathan Scottc2536662006-03-29 10:44:40 +10001177 if (direct || size > (1 << inode->i_blkbits)) {
Christoph Hellwig2b8f12b2010-04-28 12:28:59 +00001178 xfs_off_t mapping_size;
Christoph Hellwig9563b3d2010-04-28 12:28:53 +00001179
Christoph Hellwig2b8f12b2010-04-28 12:28:59 +00001180 mapping_size = imap.br_startoff + imap.br_blockcount - iblock;
1181 mapping_size <<= inode->i_blkbits;
1182
1183 ASSERT(mapping_size > 0);
1184 if (mapping_size > size)
1185 mapping_size = size;
1186 if (mapping_size > LONG_MAX)
1187 mapping_size = LONG_MAX;
1188
1189 bh_result->b_size = mapping_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 }
1191
1192 return 0;
Christoph Hellwiga206c812010-12-10 08:42:20 +00001193
1194out_unlock:
1195 xfs_iunlock(ip, lockmode);
1196 return -error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197}
1198
1199int
Nathan Scottc2536662006-03-29 10:44:40 +10001200xfs_get_blocks(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 struct inode *inode,
1202 sector_t iblock,
1203 struct buffer_head *bh_result,
1204 int create)
1205{
Christoph Hellwigf2bde9b2010-06-24 11:44:35 +10001206 return __xfs_get_blocks(inode, iblock, bh_result, create, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207}
1208
1209STATIC int
Nathan Scotte4c573b2006-03-14 13:54:26 +11001210xfs_get_blocks_direct(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 struct inode *inode,
1212 sector_t iblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 struct buffer_head *bh_result,
1214 int create)
1215{
Christoph Hellwigf2bde9b2010-06-24 11:44:35 +10001216 return __xfs_get_blocks(inode, iblock, bh_result, create, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217}
1218
Christoph Hellwig209fb872010-07-18 21:17:11 +00001219/*
1220 * Complete a direct I/O write request.
1221 *
1222 * If the private argument is non-NULL __xfs_get_blocks signals us that we
1223 * need to issue a transaction to convert the range from unwritten to written
1224 * extents. In case this is regular synchronous I/O we just call xfs_end_io
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001225 * to do this and we are done. But in case this was a successful AIO
Christoph Hellwig209fb872010-07-18 21:17:11 +00001226 * request this handler is called from interrupt context, from which we
1227 * can't start transactions. In that case offload the I/O completion to
1228 * the workqueues we also use for buffered I/O completion.
1229 */
Christoph Hellwigf0973862005-09-05 08:22:52 +10001230STATIC void
Christoph Hellwig209fb872010-07-18 21:17:11 +00001231xfs_end_io_direct_write(
1232 struct kiocb *iocb,
1233 loff_t offset,
1234 ssize_t size,
1235 void *private,
1236 int ret,
1237 bool is_async)
Christoph Hellwigf0973862005-09-05 08:22:52 +10001238{
Christoph Hellwig209fb872010-07-18 21:17:11 +00001239 struct xfs_ioend *ioend = iocb->private;
Christoph Hellwigf0973862005-09-05 08:22:52 +10001240
1241 /*
Christoph Hellwig2813d682011-12-18 20:00:12 +00001242 * While the generic direct I/O code updates the inode size, it does
1243 * so only after the end_io handler is called, which means our
1244 * end_io handler thinks the on-disk size is outside the in-core
1245 * size. To prevent this just update it a little bit earlier here.
1246 */
1247 if (offset + size > i_size_read(ioend->io_inode))
1248 i_size_write(ioend->io_inode, offset + size);
1249
1250 /*
Nathan Scottc41564b2006-03-29 08:55:14 +10001251 * blockdev_direct_IO can return an error even after the I/O
Christoph Hellwigf0973862005-09-05 08:22:52 +10001252 * completion handler was called. Thus we need to protect
1253 * against double-freeing.
1254 */
1255 iocb->private = NULL;
Christoph Hellwig40e2e972010-07-18 21:17:09 +00001256
Christoph Hellwig209fb872010-07-18 21:17:11 +00001257 ioend->io_offset = offset;
1258 ioend->io_size = size;
Christoph Hellwigc859cdd2011-08-23 08:28:10 +00001259 ioend->io_iocb = iocb;
1260 ioend->io_result = ret;
Christoph Hellwig209fb872010-07-18 21:17:11 +00001261 if (private && size > 0)
1262 ioend->io_type = IO_UNWRITTEN;
1263
1264 if (is_async) {
Christoph Hellwigc859cdd2011-08-23 08:28:10 +00001265 ioend->io_isasync = 1;
Christoph Hellwig209fb872010-07-18 21:17:11 +00001266 xfs_finish_ioend(ioend);
1267 } else {
1268 xfs_finish_ioend_sync(ioend);
1269 }
Christoph Hellwigf0973862005-09-05 08:22:52 +10001270}
1271
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272STATIC ssize_t
Nathan Scotte4c573b2006-03-14 13:54:26 +11001273xfs_vm_direct_IO(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 int rw,
1275 struct kiocb *iocb,
1276 const struct iovec *iov,
1277 loff_t offset,
1278 unsigned long nr_segs)
1279{
Christoph Hellwig209fb872010-07-18 21:17:11 +00001280 struct inode *inode = iocb->ki_filp->f_mapping->host;
1281 struct block_device *bdev = xfs_find_bdev_for_inode(inode);
1282 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
Christoph Hellwig209fb872010-07-18 21:17:11 +00001284 if (rw & WRITE) {
Christoph Hellwiga206c812010-12-10 08:42:20 +00001285 iocb->private = xfs_alloc_ioend(inode, IO_DIRECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
Christoph Hellwigeafdc7d2010-06-04 11:29:53 +02001287 ret = __blockdev_direct_IO(rw, iocb, inode, bdev, iov,
1288 offset, nr_segs,
1289 xfs_get_blocks_direct,
1290 xfs_end_io_direct_write, NULL, 0);
Christoph Hellwig209fb872010-07-18 21:17:11 +00001291 if (ret != -EIOCBQUEUED && iocb->private)
1292 xfs_destroy_ioend(iocb->private);
1293 } else {
Christoph Hellwigeafdc7d2010-06-04 11:29:53 +02001294 ret = __blockdev_direct_IO(rw, iocb, inode, bdev, iov,
1295 offset, nr_segs,
1296 xfs_get_blocks_direct,
1297 NULL, NULL, 0);
Christoph Hellwig209fb872010-07-18 21:17:11 +00001298 }
Christoph Hellwig5fe878a2009-12-15 16:47:50 -08001299
Christoph Hellwigf0973862005-09-05 08:22:52 +10001300 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301}
1302
Christoph Hellwigfa9b2272010-06-14 05:17:31 -04001303STATIC void
1304xfs_vm_write_failed(
1305 struct address_space *mapping,
1306 loff_t to)
1307{
1308 struct inode *inode = mapping->host;
1309
1310 if (to > inode->i_size) {
Dave Chinnerc726de42010-11-30 15:14:39 +11001311 /*
Christoph Hellwig2813d682011-12-18 20:00:12 +00001312 * Punch out the delalloc blocks we have already allocated.
1313 *
1314 * Don't bother with xfs_setattr given that nothing can have
1315 * made it to disk yet as the page is still locked at this
1316 * point.
Dave Chinnerc726de42010-11-30 15:14:39 +11001317 */
1318 struct xfs_inode *ip = XFS_I(inode);
1319 xfs_fileoff_t start_fsb;
1320 xfs_fileoff_t end_fsb;
1321 int error;
1322
1323 truncate_pagecache(inode, to, inode->i_size);
1324
1325 /*
1326 * Check if there are any blocks that are outside of i_size
1327 * that need to be trimmed back.
1328 */
1329 start_fsb = XFS_B_TO_FSB(ip->i_mount, inode->i_size) + 1;
1330 end_fsb = XFS_B_TO_FSB(ip->i_mount, to);
1331 if (end_fsb <= start_fsb)
1332 return;
1333
1334 xfs_ilock(ip, XFS_ILOCK_EXCL);
1335 error = xfs_bmap_punch_delalloc_range(ip, start_fsb,
1336 end_fsb - start_fsb);
1337 if (error) {
1338 /* something screwed, just bail */
1339 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
Dave Chinner4f107002011-03-07 10:00:35 +11001340 xfs_alert(ip->i_mount,
Dave Chinnerc726de42010-11-30 15:14:39 +11001341 "xfs_vm_write_failed: unable to clean up ino %lld",
1342 ip->i_ino);
1343 }
1344 }
1345 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Christoph Hellwigfa9b2272010-06-14 05:17:31 -04001346 }
1347}
1348
Nathan Scottf51623b2006-03-14 13:26:27 +11001349STATIC int
Nick Piggind79689c2007-10-16 01:25:06 -07001350xfs_vm_write_begin(
Nathan Scottf51623b2006-03-14 13:26:27 +11001351 struct file *file,
Nick Piggind79689c2007-10-16 01:25:06 -07001352 struct address_space *mapping,
1353 loff_t pos,
1354 unsigned len,
1355 unsigned flags,
1356 struct page **pagep,
1357 void **fsdata)
Nathan Scottf51623b2006-03-14 13:26:27 +11001358{
Christoph Hellwig155130a2010-06-04 11:29:58 +02001359 int ret;
1360
1361 ret = block_write_begin(mapping, pos, len, flags | AOP_FLAG_NOFS,
1362 pagep, xfs_get_blocks);
Christoph Hellwigfa9b2272010-06-14 05:17:31 -04001363 if (unlikely(ret))
1364 xfs_vm_write_failed(mapping, pos + len);
1365 return ret;
1366}
Christoph Hellwig155130a2010-06-04 11:29:58 +02001367
Christoph Hellwigfa9b2272010-06-14 05:17:31 -04001368STATIC int
1369xfs_vm_write_end(
1370 struct file *file,
1371 struct address_space *mapping,
1372 loff_t pos,
1373 unsigned len,
1374 unsigned copied,
1375 struct page *page,
1376 void *fsdata)
1377{
1378 int ret;
1379
1380 ret = generic_write_end(file, mapping, pos, len, copied, page, fsdata);
1381 if (unlikely(ret < len))
1382 xfs_vm_write_failed(mapping, pos + len);
Christoph Hellwig155130a2010-06-04 11:29:58 +02001383 return ret;
Nathan Scottf51623b2006-03-14 13:26:27 +11001384}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
1386STATIC sector_t
Nathan Scotte4c573b2006-03-14 13:54:26 +11001387xfs_vm_bmap(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 struct address_space *mapping,
1389 sector_t block)
1390{
1391 struct inode *inode = (struct inode *)mapping->host;
Christoph Hellwig739bfb22007-08-29 10:58:01 +10001392 struct xfs_inode *ip = XFS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Christoph Hellwigcca28fb2010-06-24 11:57:09 +10001394 trace_xfs_vm_bmap(XFS_I(inode));
Christoph Hellwig126468b2008-03-06 13:44:57 +11001395 xfs_ilock(ip, XFS_IOLOCK_SHARED);
Christoph Hellwig739bfb22007-08-29 10:58:01 +10001396 xfs_flush_pages(ip, (xfs_off_t)0, -1, 0, FI_REMAPF);
Christoph Hellwig126468b2008-03-06 13:44:57 +11001397 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
Nathan Scottc2536662006-03-29 10:44:40 +10001398 return generic_block_bmap(mapping, block, xfs_get_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399}
1400
1401STATIC int
Nathan Scotte4c573b2006-03-14 13:54:26 +11001402xfs_vm_readpage(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 struct file *unused,
1404 struct page *page)
1405{
Nathan Scottc2536662006-03-29 10:44:40 +10001406 return mpage_readpage(page, xfs_get_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407}
1408
1409STATIC int
Nathan Scotte4c573b2006-03-14 13:54:26 +11001410xfs_vm_readpages(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 struct file *unused,
1412 struct address_space *mapping,
1413 struct list_head *pages,
1414 unsigned nr_pages)
1415{
Nathan Scottc2536662006-03-29 10:44:40 +10001416 return mpage_readpages(mapping, pages, nr_pages, xfs_get_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417}
1418
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07001419const struct address_space_operations xfs_address_space_operations = {
Nathan Scotte4c573b2006-03-14 13:54:26 +11001420 .readpage = xfs_vm_readpage,
1421 .readpages = xfs_vm_readpages,
1422 .writepage = xfs_vm_writepage,
Nathan Scott7d4fb402006-06-09 15:27:16 +10001423 .writepages = xfs_vm_writepages,
Nathan Scott238f4c52006-03-17 17:26:25 +11001424 .releasepage = xfs_vm_releasepage,
1425 .invalidatepage = xfs_vm_invalidatepage,
Nick Piggind79689c2007-10-16 01:25:06 -07001426 .write_begin = xfs_vm_write_begin,
Christoph Hellwigfa9b2272010-06-14 05:17:31 -04001427 .write_end = xfs_vm_write_end,
Nathan Scotte4c573b2006-03-14 13:54:26 +11001428 .bmap = xfs_vm_bmap,
1429 .direct_IO = xfs_vm_direct_IO,
Christoph Lametere965f962006-02-01 03:05:41 -08001430 .migratepage = buffer_migrate_page,
Hisashi Hifumibddaafa2009-03-29 09:53:38 +02001431 .is_partially_uptodate = block_is_partially_uptodate,
Andi Kleenaa261f52009-09-16 11:50:16 +02001432 .error_remove_page = generic_error_remove_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433};