blob: 8e11b07bb28152c8afc6d465743d25a24c1f0713 [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 }
Christoph Hellwig84803fb2012-02-29 09:53:50 +0000182 } else {
183 /*
184 * We might have to update the on-disk file size after
185 * extending writes.
186 */
187 xfs_setfilesize(ioend);
Dave Chinner77d7a0c2010-02-17 05:36:29 +0000188 }
189
Christoph Hellwig04f658e2011-08-24 05:59:25 +0000190done:
Christoph Hellwigaa6bf012012-02-29 09:53:48 +0000191 xfs_destroy_ioend(ioend);
Dave Chinner77d7a0c2010-02-17 05:36:29 +0000192}
193
194/*
Christoph Hellwig209fb872010-07-18 21:17:11 +0000195 * Call IO completion handling in caller context on the final put of an ioend.
196 */
197STATIC void
198xfs_finish_ioend_sync(
199 struct xfs_ioend *ioend)
200{
201 if (atomic_dec_and_test(&ioend->io_remaining))
202 xfs_end_io(&ioend->io_work);
203}
204
205/*
Christoph Hellwig0829c362005-09-02 16:58:49 +1000206 * Allocate and initialise an IO completion structure.
207 * We need to track unwritten extent write completion here initially.
208 * We'll need to extend this for updating the ondisk inode size later
209 * (vs. incore size).
210 */
211STATIC xfs_ioend_t *
212xfs_alloc_ioend(
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100213 struct inode *inode,
214 unsigned int type)
Christoph Hellwig0829c362005-09-02 16:58:49 +1000215{
216 xfs_ioend_t *ioend;
217
218 ioend = mempool_alloc(xfs_ioend_pool, GFP_NOFS);
219
220 /*
221 * Set the count to 1 initially, which will prevent an I/O
222 * completion callback from happening before we have started
223 * all the I/O from calling the completion routine too early.
224 */
225 atomic_set(&ioend->io_remaining, 1);
Christoph Hellwigc859cdd2011-08-23 08:28:10 +0000226 ioend->io_isasync = 0;
Nathan Scott7d04a332006-06-09 14:58:38 +1000227 ioend->io_error = 0;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100228 ioend->io_list = NULL;
229 ioend->io_type = type;
Christoph Hellwigb677c212007-08-29 11:46:28 +1000230 ioend->io_inode = inode;
Christoph Hellwigc1a073b2005-09-05 08:23:35 +1000231 ioend->io_buffer_head = NULL;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100232 ioend->io_buffer_tail = NULL;
Christoph Hellwig0829c362005-09-02 16:58:49 +1000233 ioend->io_offset = 0;
234 ioend->io_size = 0;
Christoph Hellwigfb511f22010-07-18 21:17:10 +0000235 ioend->io_iocb = NULL;
236 ioend->io_result = 0;
Christoph Hellwig0829c362005-09-02 16:58:49 +1000237
Christoph Hellwig5ec4fab2009-10-30 09:11:47 +0000238 INIT_WORK(&ioend->io_work, xfs_end_io);
Christoph Hellwig0829c362005-09-02 16:58:49 +1000239 return ioend;
240}
241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242STATIC int
243xfs_map_blocks(
244 struct inode *inode,
245 loff_t offset,
Christoph Hellwig207d0412010-04-28 12:28:56 +0000246 struct xfs_bmbt_irec *imap,
Christoph Hellwiga206c812010-12-10 08:42:20 +0000247 int type,
248 int nonblocking)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
Christoph Hellwiga206c812010-12-10 08:42:20 +0000250 struct xfs_inode *ip = XFS_I(inode);
251 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwiged1e7b72010-12-10 08:42:22 +0000252 ssize_t count = 1 << inode->i_blkbits;
Christoph Hellwiga206c812010-12-10 08:42:20 +0000253 xfs_fileoff_t offset_fsb, end_fsb;
254 int error = 0;
Christoph Hellwiga206c812010-12-10 08:42:20 +0000255 int bmapi_flags = XFS_BMAPI_ENTIRE;
256 int nimaps = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Christoph Hellwiga206c812010-12-10 08:42:20 +0000258 if (XFS_FORCED_SHUTDOWN(mp))
259 return -XFS_ERROR(EIO);
260
Christoph Hellwig8ff29572010-12-10 08:42:21 +0000261 if (type == IO_UNWRITTEN)
Christoph Hellwiga206c812010-12-10 08:42:20 +0000262 bmapi_flags |= XFS_BMAPI_IGSTATE;
Christoph Hellwiga206c812010-12-10 08:42:20 +0000263
Christoph Hellwig8ff29572010-12-10 08:42:21 +0000264 if (!xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
265 if (nonblocking)
266 return -XFS_ERROR(EAGAIN);
267 xfs_ilock(ip, XFS_ILOCK_SHARED);
Christoph Hellwiga206c812010-12-10 08:42:20 +0000268 }
269
Christoph Hellwig8ff29572010-12-10 08:42:21 +0000270 ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
271 (ip->i_df.if_flags & XFS_IFEXTENTS));
Christoph Hellwiga206c812010-12-10 08:42:20 +0000272 ASSERT(offset <= mp->m_maxioffset);
Christoph Hellwig8ff29572010-12-10 08:42:21 +0000273
Christoph Hellwiga206c812010-12-10 08:42:20 +0000274 if (offset + count > mp->m_maxioffset)
275 count = mp->m_maxioffset - offset;
276 end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
277 offset_fsb = XFS_B_TO_FSBT(mp, offset);
Dave Chinner5c8ed202011-09-18 20:40:45 +0000278 error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb,
279 imap, &nimaps, bmapi_flags);
Christoph Hellwig8ff29572010-12-10 08:42:21 +0000280 xfs_iunlock(ip, XFS_ILOCK_SHARED);
281
Christoph Hellwiga206c812010-12-10 08:42:20 +0000282 if (error)
Christoph Hellwig8ff29572010-12-10 08:42:21 +0000283 return -XFS_ERROR(error);
Christoph Hellwiga206c812010-12-10 08:42:20 +0000284
Christoph Hellwig8ff29572010-12-10 08:42:21 +0000285 if (type == IO_DELALLOC &&
286 (!nimaps || isnullstartblock(imap->br_startblock))) {
Christoph Hellwiga206c812010-12-10 08:42:20 +0000287 error = xfs_iomap_write_allocate(ip, offset, count, imap);
288 if (!error)
289 trace_xfs_map_blocks_alloc(ip, offset, count, type, imap);
Christoph Hellwig8ff29572010-12-10 08:42:21 +0000290 return -XFS_ERROR(error);
Christoph Hellwiga206c812010-12-10 08:42:20 +0000291 }
292
Christoph Hellwig8ff29572010-12-10 08:42:21 +0000293#ifdef DEBUG
294 if (type == IO_UNWRITTEN) {
295 ASSERT(nimaps);
296 ASSERT(imap->br_startblock != HOLESTARTBLOCK);
297 ASSERT(imap->br_startblock != DELAYSTARTBLOCK);
298 }
299#endif
300 if (nimaps)
301 trace_xfs_map_blocks_found(ip, offset, count, type, imap);
302 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
304
Christoph Hellwigb8f82a42009-11-14 16:17:22 +0000305STATIC int
Christoph Hellwig558e6892010-04-28 12:28:58 +0000306xfs_imap_valid(
Christoph Hellwig8699bb02010-04-28 12:28:54 +0000307 struct inode *inode,
Christoph Hellwig207d0412010-04-28 12:28:56 +0000308 struct xfs_bmbt_irec *imap,
Christoph Hellwig558e6892010-04-28 12:28:58 +0000309 xfs_off_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
Christoph Hellwig558e6892010-04-28 12:28:58 +0000311 offset >>= inode->i_blkbits;
Christoph Hellwig8699bb02010-04-28 12:28:54 +0000312
Christoph Hellwig558e6892010-04-28 12:28:58 +0000313 return offset >= imap->br_startoff &&
314 offset < imap->br_startoff + imap->br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315}
316
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100317/*
318 * BIO completion handler for buffered IO.
319 */
Al Viro782e3b32007-10-12 07:17:47 +0100320STATIC void
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100321xfs_end_bio(
322 struct bio *bio,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100323 int error)
324{
325 xfs_ioend_t *ioend = bio->bi_private;
326
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100327 ASSERT(atomic_read(&bio->bi_cnt) >= 1);
Nathan Scott7d04a332006-06-09 14:58:38 +1000328 ioend->io_error = test_bit(BIO_UPTODATE, &bio->bi_flags) ? 0 : error;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100329
330 /* Toss bio and pass work off to an xfsdatad thread */
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100331 bio->bi_private = NULL;
332 bio->bi_end_io = NULL;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100333 bio_put(bio);
Nathan Scott7d04a332006-06-09 14:58:38 +1000334
Christoph Hellwig209fb872010-07-18 21:17:11 +0000335 xfs_finish_ioend(ioend);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100336}
337
338STATIC void
339xfs_submit_ioend_bio(
Christoph Hellwig06342cf2009-10-30 09:09:15 +0000340 struct writeback_control *wbc,
341 xfs_ioend_t *ioend,
342 struct bio *bio)
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100343{
Christoph Hellwig6923e682012-02-29 09:53:49 +0000344 struct xfs_inode *ip = XFS_I(ioend->io_inode);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100345 atomic_inc(&ioend->io_remaining);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100346 bio->bi_private = ioend;
347 bio->bi_end_io = xfs_end_bio;
348
Dave Chinner932640e2009-10-06 20:29:29 +0000349 /*
350 * If the I/O is beyond EOF we mark the inode dirty immediately
351 * but don't update the inode size until I/O completion.
352 */
Christoph Hellwig6923e682012-02-29 09:53:49 +0000353 if (xfs_new_eof(ip, ioend->io_offset + ioend->io_size))
354 xfs_mark_inode_dirty(ip);
Dave Chinner932640e2009-10-06 20:29:29 +0000355
Jens Axboe721a9602011-03-09 11:56:30 +0100356 submit_bio(wbc->sync_mode == WB_SYNC_ALL ? WRITE_SYNC : WRITE, bio);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100357}
358
359STATIC struct bio *
360xfs_alloc_ioend_bio(
361 struct buffer_head *bh)
362{
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100363 int nvecs = bio_get_nr_vecs(bh->b_bdev);
Christoph Hellwig221cb252010-12-10 08:42:17 +0000364 struct bio *bio = bio_alloc(GFP_NOIO, nvecs);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100365
366 ASSERT(bio->bi_private == NULL);
367 bio->bi_sector = bh->b_blocknr * (bh->b_size >> 9);
368 bio->bi_bdev = bh->b_bdev;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100369 return bio;
370}
371
372STATIC void
373xfs_start_buffer_writeback(
374 struct buffer_head *bh)
375{
376 ASSERT(buffer_mapped(bh));
377 ASSERT(buffer_locked(bh));
378 ASSERT(!buffer_delay(bh));
379 ASSERT(!buffer_unwritten(bh));
380
381 mark_buffer_async_write(bh);
382 set_buffer_uptodate(bh);
383 clear_buffer_dirty(bh);
384}
385
386STATIC void
387xfs_start_page_writeback(
388 struct page *page,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100389 int clear_dirty,
390 int buffers)
391{
392 ASSERT(PageLocked(page));
393 ASSERT(!PageWriteback(page));
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100394 if (clear_dirty)
David Chinner92132022006-12-21 10:24:01 +1100395 clear_page_dirty_for_io(page);
396 set_page_writeback(page);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100397 unlock_page(page);
Fengguang Wu1f7decf2007-10-16 23:30:42 -0700398 /* If no buffers on the page are to be written, finish it here */
399 if (!buffers)
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100400 end_page_writeback(page);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100401}
402
403static inline int bio_add_buffer(struct bio *bio, struct buffer_head *bh)
404{
405 return bio_add_page(bio, bh->b_page, bh->b_size, bh_offset(bh));
406}
407
408/*
David Chinnerd88992f2006-01-18 13:38:12 +1100409 * Submit all of the bios for all of the ioends we have saved up, covering the
410 * initial writepage page and also any probed pages.
411 *
412 * Because we may have multiple ioends spanning a page, we need to start
413 * writeback on all the buffers before we submit them for I/O. If we mark the
414 * buffers as we got, then we can end up with a page that only has buffers
415 * marked async write and I/O complete on can occur before we mark the other
416 * buffers async write.
417 *
418 * The end result of this is that we trip a bug in end_page_writeback() because
419 * we call it twice for the one page as the code in end_buffer_async_write()
420 * assumes that all buffers on the page are started at the same time.
421 *
422 * The fix is two passes across the ioend list - one to start writeback on the
Nathan Scottc41564b2006-03-29 08:55:14 +1000423 * buffer_heads, and then submit them for I/O on the second pass.
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100424 */
425STATIC void
426xfs_submit_ioend(
Christoph Hellwig06342cf2009-10-30 09:09:15 +0000427 struct writeback_control *wbc,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100428 xfs_ioend_t *ioend)
429{
David Chinnerd88992f2006-01-18 13:38:12 +1100430 xfs_ioend_t *head = ioend;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100431 xfs_ioend_t *next;
432 struct buffer_head *bh;
433 struct bio *bio;
434 sector_t lastblock = 0;
435
David Chinnerd88992f2006-01-18 13:38:12 +1100436 /* Pass 1 - start writeback */
437 do {
438 next = ioend->io_list;
Christoph Hellwig221cb252010-12-10 08:42:17 +0000439 for (bh = ioend->io_buffer_head; bh; bh = bh->b_private)
David Chinnerd88992f2006-01-18 13:38:12 +1100440 xfs_start_buffer_writeback(bh);
David Chinnerd88992f2006-01-18 13:38:12 +1100441 } while ((ioend = next) != NULL);
442
443 /* Pass 2 - submit I/O */
444 ioend = head;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100445 do {
446 next = ioend->io_list;
447 bio = NULL;
448
449 for (bh = ioend->io_buffer_head; bh; bh = bh->b_private) {
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100450
451 if (!bio) {
452 retry:
453 bio = xfs_alloc_ioend_bio(bh);
454 } else if (bh->b_blocknr != lastblock + 1) {
Christoph Hellwig06342cf2009-10-30 09:09:15 +0000455 xfs_submit_ioend_bio(wbc, ioend, bio);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100456 goto retry;
457 }
458
459 if (bio_add_buffer(bio, bh) != bh->b_size) {
Christoph Hellwig06342cf2009-10-30 09:09:15 +0000460 xfs_submit_ioend_bio(wbc, ioend, bio);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100461 goto retry;
462 }
463
464 lastblock = bh->b_blocknr;
465 }
466 if (bio)
Christoph Hellwig06342cf2009-10-30 09:09:15 +0000467 xfs_submit_ioend_bio(wbc, ioend, bio);
Christoph Hellwig209fb872010-07-18 21:17:11 +0000468 xfs_finish_ioend(ioend);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100469 } while ((ioend = next) != NULL);
470}
471
472/*
473 * Cancel submission of all buffer_heads so far in this endio.
474 * Toss the endio too. Only ever called for the initial page
475 * in a writepage request, so only ever one page.
476 */
477STATIC void
478xfs_cancel_ioend(
479 xfs_ioend_t *ioend)
480{
481 xfs_ioend_t *next;
482 struct buffer_head *bh, *next_bh;
483
484 do {
485 next = ioend->io_list;
486 bh = ioend->io_buffer_head;
487 do {
488 next_bh = bh->b_private;
489 clear_buffer_async_write(bh);
490 unlock_buffer(bh);
491 } while ((bh = next_bh) != NULL);
492
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100493 mempool_free(ioend, xfs_ioend_pool);
494 } while ((ioend = next) != NULL);
495}
496
497/*
498 * Test to see if we've been building up a completion structure for
499 * earlier buffers -- if so, we try to append to this ioend if we
500 * can, otherwise we finish off any current ioend and start another.
501 * Return true if we've finished the given ioend.
502 */
503STATIC void
504xfs_add_to_ioend(
505 struct inode *inode,
506 struct buffer_head *bh,
Christoph Hellwig7336cea2006-01-11 20:49:16 +1100507 xfs_off_t offset,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100508 unsigned int type,
509 xfs_ioend_t **result,
510 int need_ioend)
511{
512 xfs_ioend_t *ioend = *result;
513
514 if (!ioend || need_ioend || type != ioend->io_type) {
515 xfs_ioend_t *previous = *result;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100516
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100517 ioend = xfs_alloc_ioend(inode, type);
518 ioend->io_offset = offset;
519 ioend->io_buffer_head = bh;
520 ioend->io_buffer_tail = bh;
521 if (previous)
522 previous->io_list = ioend;
523 *result = ioend;
524 } else {
525 ioend->io_buffer_tail->b_private = bh;
526 ioend->io_buffer_tail = bh;
527 }
528
529 bh->b_private = NULL;
530 ioend->io_size += bh->b_size;
531}
532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533STATIC void
Nathan Scott87cbc492006-03-14 13:26:43 +1100534xfs_map_buffer(
Christoph Hellwig046f1682010-04-28 12:28:52 +0000535 struct inode *inode,
Nathan Scott87cbc492006-03-14 13:26:43 +1100536 struct buffer_head *bh,
Christoph Hellwig207d0412010-04-28 12:28:56 +0000537 struct xfs_bmbt_irec *imap,
Christoph Hellwig046f1682010-04-28 12:28:52 +0000538 xfs_off_t offset)
Nathan Scott87cbc492006-03-14 13:26:43 +1100539{
540 sector_t bn;
Christoph Hellwig8699bb02010-04-28 12:28:54 +0000541 struct xfs_mount *m = XFS_I(inode)->i_mount;
Christoph Hellwig207d0412010-04-28 12:28:56 +0000542 xfs_off_t iomap_offset = XFS_FSB_TO_B(m, imap->br_startoff);
543 xfs_daddr_t iomap_bn = xfs_fsb_to_db(XFS_I(inode), imap->br_startblock);
Nathan Scott87cbc492006-03-14 13:26:43 +1100544
Christoph Hellwig207d0412010-04-28 12:28:56 +0000545 ASSERT(imap->br_startblock != HOLESTARTBLOCK);
546 ASSERT(imap->br_startblock != DELAYSTARTBLOCK);
Nathan Scott87cbc492006-03-14 13:26:43 +1100547
Christoph Hellwige5131822010-04-28 12:28:55 +0000548 bn = (iomap_bn >> (inode->i_blkbits - BBSHIFT)) +
Christoph Hellwig8699bb02010-04-28 12:28:54 +0000549 ((offset - iomap_offset) >> inode->i_blkbits);
Nathan Scott87cbc492006-03-14 13:26:43 +1100550
Christoph Hellwig046f1682010-04-28 12:28:52 +0000551 ASSERT(bn || XFS_IS_REALTIME_INODE(XFS_I(inode)));
Nathan Scott87cbc492006-03-14 13:26:43 +1100552
553 bh->b_blocknr = bn;
554 set_buffer_mapped(bh);
555}
556
557STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558xfs_map_at_offset(
Christoph Hellwig046f1682010-04-28 12:28:52 +0000559 struct inode *inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 struct buffer_head *bh,
Christoph Hellwig207d0412010-04-28 12:28:56 +0000561 struct xfs_bmbt_irec *imap,
Christoph Hellwig046f1682010-04-28 12:28:52 +0000562 xfs_off_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Christoph Hellwig207d0412010-04-28 12:28:56 +0000564 ASSERT(imap->br_startblock != HOLESTARTBLOCK);
565 ASSERT(imap->br_startblock != DELAYSTARTBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Christoph Hellwig207d0412010-04-28 12:28:56 +0000567 xfs_map_buffer(inode, bh, imap, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 set_buffer_mapped(bh);
569 clear_buffer_delay(bh);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100570 clear_buffer_unwritten(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571}
572
573/*
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100574 * Test if a given page is suitable for writing as part of an unwritten
575 * or delayed allocate extent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 */
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100577STATIC int
578xfs_is_delayed_page(
579 struct page *page,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100580 unsigned int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 if (PageWriteback(page))
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100583 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585 if (page->mapping && page_has_buffers(page)) {
586 struct buffer_head *bh, *head;
587 int acceptable = 0;
588
589 bh = head = page_buffers(page);
590 do {
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100591 if (buffer_unwritten(bh))
Christoph Hellwig34a52c62010-04-28 12:28:57 +0000592 acceptable = (type == IO_UNWRITTEN);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100593 else if (buffer_delay(bh))
Christoph Hellwiga206c812010-12-10 08:42:20 +0000594 acceptable = (type == IO_DELALLOC);
David Chinner2ddee842006-03-22 12:47:40 +1100595 else if (buffer_dirty(bh) && buffer_mapped(bh))
Christoph Hellwiga206c812010-12-10 08:42:20 +0000596 acceptable = (type == IO_OVERWRITE);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100597 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 } while ((bh = bh->b_this_page) != head);
600
601 if (acceptable)
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100602 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 }
604
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100605 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606}
607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608/*
609 * Allocate & map buffers for page given the extent map. Write it out.
610 * except for the original page of a writepage, this is called on
611 * delalloc/unwritten pages only, for the original page it is possible
612 * that the page has no mapping at all.
613 */
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100614STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615xfs_convert_page(
616 struct inode *inode,
617 struct page *page,
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100618 loff_t tindex,
Christoph Hellwig207d0412010-04-28 12:28:56 +0000619 struct xfs_bmbt_irec *imap,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100620 xfs_ioend_t **ioendp,
Christoph Hellwig2fa24f92010-12-10 08:42:23 +0000621 struct writeback_control *wbc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100623 struct buffer_head *bh, *head;
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100624 xfs_off_t end_offset;
625 unsigned long p_offset;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100626 unsigned int type;
Nathan Scott24e17b52005-05-05 13:33:20 -0700627 int len, page_dirty;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100628 int count = 0, done = 0, uptodate = 1;
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100629 xfs_off_t offset = page_offset(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100631 if (page->index != tindex)
632 goto fail;
Nick Piggin529ae9a2008-08-02 12:01:03 +0200633 if (!trylock_page(page))
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100634 goto fail;
635 if (PageWriteback(page))
636 goto fail_unlock_page;
637 if (page->mapping != inode->i_mapping)
638 goto fail_unlock_page;
639 if (!xfs_is_delayed_page(page, (*ioendp)->io_type))
640 goto fail_unlock_page;
641
Nathan Scott24e17b52005-05-05 13:33:20 -0700642 /*
643 * page_dirty is initially a count of buffers on the page before
Nathan Scottc41564b2006-03-29 08:55:14 +1000644 * EOF and is decremented as we move each into a cleanable state.
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100645 *
646 * Derivation:
647 *
648 * End offset is the highest offset that this page should represent.
649 * If we are on the last page, (end_offset & (PAGE_CACHE_SIZE - 1))
650 * will evaluate non-zero and be less than PAGE_CACHE_SIZE and
651 * hence give us the correct page_dirty count. On any other page,
652 * it will be zero and in that case we need page_dirty to be the
653 * count of buffers on the page.
Nathan Scott24e17b52005-05-05 13:33:20 -0700654 */
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100655 end_offset = min_t(unsigned long long,
656 (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT,
657 i_size_read(inode));
658
Nathan Scott24e17b52005-05-05 13:33:20 -0700659 len = 1 << inode->i_blkbits;
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100660 p_offset = min_t(unsigned long, end_offset & (PAGE_CACHE_SIZE - 1),
661 PAGE_CACHE_SIZE);
662 p_offset = p_offset ? roundup(p_offset, len) : PAGE_CACHE_SIZE;
663 page_dirty = p_offset / len;
Nathan Scott24e17b52005-05-05 13:33:20 -0700664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 bh = head = page_buffers(page);
666 do {
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100667 if (offset >= end_offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 break;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100669 if (!buffer_uptodate(bh))
670 uptodate = 0;
671 if (!(PageUptodate(page) || buffer_uptodate(bh))) {
672 done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 continue;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100674 }
675
Christoph Hellwig2fa24f92010-12-10 08:42:23 +0000676 if (buffer_unwritten(bh) || buffer_delay(bh) ||
677 buffer_mapped(bh)) {
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100678 if (buffer_unwritten(bh))
Christoph Hellwig34a52c62010-04-28 12:28:57 +0000679 type = IO_UNWRITTEN;
Christoph Hellwig2fa24f92010-12-10 08:42:23 +0000680 else if (buffer_delay(bh))
Christoph Hellwiga206c812010-12-10 08:42:20 +0000681 type = IO_DELALLOC;
Christoph Hellwig2fa24f92010-12-10 08:42:23 +0000682 else
683 type = IO_OVERWRITE;
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100684
Christoph Hellwig558e6892010-04-28 12:28:58 +0000685 if (!xfs_imap_valid(inode, imap, offset)) {
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100686 done = 1;
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100687 continue;
688 }
689
Christoph Hellwigecff71e2010-12-10 08:42:25 +0000690 lock_buffer(bh);
691 if (type != IO_OVERWRITE)
Christoph Hellwig2fa24f92010-12-10 08:42:23 +0000692 xfs_map_at_offset(inode, bh, imap, offset);
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000693 xfs_add_to_ioend(inode, bh, offset, type,
694 ioendp, done);
695
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100696 page_dirty--;
697 count++;
698 } else {
Christoph Hellwig2fa24f92010-12-10 08:42:23 +0000699 done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 }
Christoph Hellwig7336cea2006-01-11 20:49:16 +1100701 } while (offset += len, (bh = bh->b_this_page) != head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100703 if (uptodate && bh == head)
704 SetPageUptodate(page);
705
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000706 if (count) {
Dave Chinnerefceab12010-08-24 11:44:56 +1000707 if (--wbc->nr_to_write <= 0 &&
708 wbc->sync_mode == WB_SYNC_NONE)
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000709 done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 }
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000711 xfs_start_page_writeback(page, !page_dirty, count);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100712
713 return done;
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100714 fail_unlock_page:
715 unlock_page(page);
716 fail:
717 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718}
719
720/*
721 * Convert & write out a cluster of pages in the same extent as defined
722 * by mp and following the start page.
723 */
724STATIC void
725xfs_cluster_write(
726 struct inode *inode,
727 pgoff_t tindex,
Christoph Hellwig207d0412010-04-28 12:28:56 +0000728 struct xfs_bmbt_irec *imap,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100729 xfs_ioend_t **ioendp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 struct writeback_control *wbc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 pgoff_t tlast)
732{
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100733 struct pagevec pvec;
734 int done = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100736 pagevec_init(&pvec, 0);
737 while (!done && tindex <= tlast) {
738 unsigned len = min_t(pgoff_t, PAGEVEC_SIZE, tlast - tindex + 1);
739
740 if (!pagevec_lookup(&pvec, inode->i_mapping, tindex, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 break;
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100742
743 for (i = 0; i < pagevec_count(&pvec); i++) {
744 done = xfs_convert_page(inode, pvec.pages[i], tindex++,
Christoph Hellwig2fa24f92010-12-10 08:42:23 +0000745 imap, ioendp, wbc);
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100746 if (done)
747 break;
748 }
749
750 pagevec_release(&pvec);
751 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 }
753}
754
Dave Chinner3ed3a432010-03-05 02:00:42 +0000755STATIC void
756xfs_vm_invalidatepage(
757 struct page *page,
758 unsigned long offset)
759{
760 trace_xfs_invalidatepage(page->mapping->host, page, offset);
761 block_invalidatepage(page, offset);
762}
763
764/*
765 * If the page has delalloc buffers on it, we need to punch them out before we
766 * invalidate the page. If we don't, we leave a stale delalloc mapping on the
767 * inode that can trip a BUG() in xfs_get_blocks() later on if a direct IO read
768 * is done on that same region - the delalloc extent is returned when none is
769 * supposed to be there.
770 *
771 * We prevent this by truncating away the delalloc regions on the page before
772 * invalidating it. Because they are delalloc, we can do this without needing a
773 * transaction. Indeed - if we get ENOSPC errors, we have to be able to do this
774 * truncation without a transaction as there is no space left for block
775 * reservation (typically why we see a ENOSPC in writeback).
776 *
777 * This is not a performance critical path, so for now just do the punching a
778 * buffer head at a time.
779 */
780STATIC void
781xfs_aops_discard_page(
782 struct page *page)
783{
784 struct inode *inode = page->mapping->host;
785 struct xfs_inode *ip = XFS_I(inode);
786 struct buffer_head *bh, *head;
787 loff_t offset = page_offset(page);
Dave Chinner3ed3a432010-03-05 02:00:42 +0000788
Christoph Hellwiga206c812010-12-10 08:42:20 +0000789 if (!xfs_is_delayed_page(page, IO_DELALLOC))
Dave Chinner3ed3a432010-03-05 02:00:42 +0000790 goto out_invalidate;
791
Dave Chinnere8c37532010-03-15 02:36:35 +0000792 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
793 goto out_invalidate;
794
Dave Chinner4f107002011-03-07 10:00:35 +1100795 xfs_alert(ip->i_mount,
Dave Chinner3ed3a432010-03-05 02:00:42 +0000796 "page discard on page %p, inode 0x%llx, offset %llu.",
797 page, ip->i_ino, offset);
798
799 xfs_ilock(ip, XFS_ILOCK_EXCL);
800 bh = head = page_buffers(page);
801 do {
Dave Chinner3ed3a432010-03-05 02:00:42 +0000802 int error;
Dave Chinnerc726de42010-11-30 15:14:39 +1100803 xfs_fileoff_t start_fsb;
Dave Chinner3ed3a432010-03-05 02:00:42 +0000804
805 if (!buffer_delay(bh))
806 goto next_buffer;
807
Dave Chinnerc726de42010-11-30 15:14:39 +1100808 start_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
809 error = xfs_bmap_punch_delalloc_range(ip, start_fsb, 1);
Dave Chinner3ed3a432010-03-05 02:00:42 +0000810 if (error) {
811 /* something screwed, just bail */
Dave Chinnere8c37532010-03-15 02:36:35 +0000812 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
Dave Chinner4f107002011-03-07 10:00:35 +1100813 xfs_alert(ip->i_mount,
Dave Chinner3ed3a432010-03-05 02:00:42 +0000814 "page discard unable to remove delalloc mapping.");
Dave Chinnere8c37532010-03-15 02:36:35 +0000815 }
Dave Chinner3ed3a432010-03-05 02:00:42 +0000816 break;
817 }
818next_buffer:
Dave Chinnerc726de42010-11-30 15:14:39 +1100819 offset += 1 << inode->i_blkbits;
Dave Chinner3ed3a432010-03-05 02:00:42 +0000820
821 } while ((bh = bh->b_this_page) != head);
822
823 xfs_iunlock(ip, XFS_ILOCK_EXCL);
824out_invalidate:
825 xfs_vm_invalidatepage(page, 0);
826 return;
827}
828
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829/*
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000830 * Write out a dirty page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 *
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000832 * For delalloc space on the page we need to allocate space and flush it.
833 * For unwritten space on the page we need to start the conversion to
834 * regular allocated space.
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000835 * For any other dirty buffer heads on the page we should flush them.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837STATIC int
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000838xfs_vm_writepage(
839 struct page *page,
840 struct writeback_control *wbc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000842 struct inode *inode = page->mapping->host;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100843 struct buffer_head *bh, *head;
Christoph Hellwig207d0412010-04-28 12:28:56 +0000844 struct xfs_bmbt_irec imap;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100845 xfs_ioend_t *ioend = NULL, *iohead = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 loff_t offset;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100847 unsigned int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 __uint64_t end_offset;
Christoph Hellwigbd1556a2010-04-28 12:29:00 +0000849 pgoff_t end_index, last_index;
Christoph Hellwiged1e7b72010-12-10 08:42:22 +0000850 ssize_t len;
Christoph Hellwiga206c812010-12-10 08:42:20 +0000851 int err, imap_valid = 0, uptodate = 1;
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000852 int count = 0;
Christoph Hellwiga206c812010-12-10 08:42:20 +0000853 int nonblocking = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000855 trace_xfs_writepage(inode, page, 0);
856
Christoph Hellwig20cb52e2010-06-24 09:46:01 +1000857 ASSERT(page_has_buffers(page));
858
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000859 /*
860 * Refuse to write the page out if we are called from reclaim context.
861 *
Christoph Hellwigd4f7a5c2010-06-28 10:34:44 -0400862 * This avoids stack overflows when called from deeply used stacks in
863 * random callers for direct reclaim or memcg reclaim. We explicitly
864 * allow reclaim from kswapd as the stack usage there is relatively low.
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000865 *
Mel Gorman94054fa2011-10-31 17:07:45 -0700866 * This should never happen except in the case of a VM regression so
867 * warn about it.
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000868 */
Mel Gorman94054fa2011-10-31 17:07:45 -0700869 if (WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD)) ==
870 PF_MEMALLOC))
Christoph Hellwigb5420f22010-08-24 11:47:51 +1000871 goto redirty;
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000872
873 /*
Christoph Hellwig680a6472011-07-08 14:34:05 +0200874 * Given that we do not allow direct reclaim to call us, we should
875 * never be called while in a filesystem transaction.
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000876 */
Christoph Hellwig680a6472011-07-08 14:34:05 +0200877 if (WARN_ON(current->flags & PF_FSTRANS))
Christoph Hellwigb5420f22010-08-24 11:47:51 +1000878 goto redirty;
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 /* Is this page beyond the end of the file? */
881 offset = i_size_read(inode);
882 end_index = offset >> PAGE_CACHE_SHIFT;
883 last_index = (offset - 1) >> PAGE_CACHE_SHIFT;
884 if (page->index >= end_index) {
885 if ((page->index >= end_index + 1) ||
886 !(i_size_read(inode) & (PAGE_CACHE_SIZE - 1))) {
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000887 unlock_page(page);
Nathan Scott19d5bcf2005-11-02 15:14:09 +1100888 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 }
890 }
891
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100892 end_offset = min_t(unsigned long long,
Christoph Hellwig20cb52e2010-06-24 09:46:01 +1000893 (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT,
894 offset);
Nathan Scott24e17b52005-05-05 13:33:20 -0700895 len = 1 << inode->i_blkbits;
Nathan Scott24e17b52005-05-05 13:33:20 -0700896
Nathan Scott24e17b52005-05-05 13:33:20 -0700897 bh = head = page_buffers(page);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100898 offset = page_offset(page);
Christoph Hellwiga206c812010-12-10 08:42:20 +0000899 type = IO_OVERWRITE;
900
Christoph Hellwigdbcdde32011-07-08 14:34:14 +0200901 if (wbc->sync_mode == WB_SYNC_NONE)
Christoph Hellwiga206c812010-12-10 08:42:20 +0000902 nonblocking = 1;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100903
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 do {
Christoph Hellwig6ac72482010-12-10 08:42:18 +0000905 int new_ioend = 0;
906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 if (offset >= end_offset)
908 break;
909 if (!buffer_uptodate(bh))
910 uptodate = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Eric Sandeen3d9b02e2010-06-24 09:45:30 +1000912 /*
Christoph Hellwigece413f2010-11-10 21:39:11 +0000913 * set_page_dirty dirties all buffers in a page, independent
914 * of their state. The dirty state however is entirely
915 * meaningless for holes (!mapped && uptodate), so skip
916 * buffers covering holes here.
Eric Sandeen3d9b02e2010-06-24 09:45:30 +1000917 */
918 if (!buffer_mapped(bh) && buffer_uptodate(bh)) {
Eric Sandeen3d9b02e2010-06-24 09:45:30 +1000919 imap_valid = 0;
920 continue;
921 }
922
Christoph Hellwigaeea1b12010-12-10 08:42:24 +0000923 if (buffer_unwritten(bh)) {
924 if (type != IO_UNWRITTEN) {
925 type = IO_UNWRITTEN;
926 imap_valid = 0;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100927 }
Christoph Hellwigaeea1b12010-12-10 08:42:24 +0000928 } else if (buffer_delay(bh)) {
929 if (type != IO_DELALLOC) {
930 type = IO_DELALLOC;
931 imap_valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 }
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000933 } else if (buffer_uptodate(bh)) {
Christoph Hellwiga206c812010-12-10 08:42:20 +0000934 if (type != IO_OVERWRITE) {
935 type = IO_OVERWRITE;
Christoph Hellwig85da94c2010-12-10 08:42:16 +0000936 imap_valid = 0;
937 }
Christoph Hellwigaeea1b12010-12-10 08:42:24 +0000938 } else {
939 if (PageUptodate(page)) {
940 ASSERT(buffer_mapped(bh));
941 imap_valid = 0;
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100942 }
Christoph Hellwigaeea1b12010-12-10 08:42:24 +0000943 continue;
944 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Christoph Hellwigaeea1b12010-12-10 08:42:24 +0000946 if (imap_valid)
947 imap_valid = xfs_imap_valid(inode, &imap, offset);
948 if (!imap_valid) {
949 /*
950 * If we didn't have a valid mapping then we need to
951 * put the new mapping into a separate ioend structure.
952 * This ensures non-contiguous extents always have
953 * separate ioends, which is particularly important
954 * for unwritten extent conversion at I/O completion
955 * time.
956 */
957 new_ioend = 1;
958 err = xfs_map_blocks(inode, offset, &imap, type,
959 nonblocking);
960 if (err)
961 goto error;
962 imap_valid = xfs_imap_valid(inode, &imap, offset);
963 }
964 if (imap_valid) {
Christoph Hellwigecff71e2010-12-10 08:42:25 +0000965 lock_buffer(bh);
966 if (type != IO_OVERWRITE)
Christoph Hellwigaeea1b12010-12-10 08:42:24 +0000967 xfs_map_at_offset(inode, bh, &imap, offset);
968 xfs_add_to_ioend(inode, bh, offset, type, &ioend,
969 new_ioend);
970 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 }
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100972
973 if (!iohead)
974 iohead = ioend;
975
976 } while (offset += len, ((bh = bh->b_this_page) != head));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
978 if (uptodate && bh == head)
979 SetPageUptodate(page);
980
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000981 xfs_start_page_writeback(page, 1, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Christoph Hellwig558e6892010-04-28 12:28:58 +0000983 if (ioend && imap_valid) {
Christoph Hellwigbd1556a2010-04-28 12:29:00 +0000984 xfs_off_t end_index;
Christoph Hellwig8699bb02010-04-28 12:28:54 +0000985
Christoph Hellwigbd1556a2010-04-28 12:29:00 +0000986 end_index = imap.br_startoff + imap.br_blockcount;
987
988 /* to bytes */
989 end_index <<= inode->i_blkbits;
990
991 /* to pages */
992 end_index = (end_index - 1) >> PAGE_CACHE_SHIFT;
993
994 /* check against file size */
995 if (end_index > last_index)
996 end_index = last_index;
997
Christoph Hellwig207d0412010-04-28 12:28:56 +0000998 xfs_cluster_write(inode, page->index + 1, &imap, &ioend,
Christoph Hellwig2fa24f92010-12-10 08:42:23 +0000999 wbc, end_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 }
1001
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001002 if (iohead)
Christoph Hellwig06342cf2009-10-30 09:09:15 +00001003 xfs_submit_ioend(wbc, iohead);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001004
Christoph Hellwig89f3b3632010-06-24 09:45:48 +10001005 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
1007error:
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001008 if (iohead)
1009 xfs_cancel_ioend(iohead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Christoph Hellwigb5420f22010-08-24 11:47:51 +10001011 if (err == -EAGAIN)
1012 goto redirty;
1013
Christoph Hellwig20cb52e2010-06-24 09:46:01 +10001014 xfs_aops_discard_page(page);
Christoph Hellwig89f3b3632010-06-24 09:45:48 +10001015 ClearPageUptodate(page);
1016 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 return err;
Nathan Scottf51623b2006-03-14 13:26:27 +11001018
Christoph Hellwigb5420f22010-08-24 11:47:51 +10001019redirty:
Nathan Scottf51623b2006-03-14 13:26:27 +11001020 redirty_page_for_writepage(wbc, page);
1021 unlock_page(page);
1022 return 0;
Nathan Scottf51623b2006-03-14 13:26:27 +11001023}
1024
Nathan Scott7d4fb402006-06-09 15:27:16 +10001025STATIC int
1026xfs_vm_writepages(
1027 struct address_space *mapping,
1028 struct writeback_control *wbc)
1029{
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +10001030 xfs_iflags_clear(XFS_I(mapping->host), XFS_ITRUNCATED);
Nathan Scott7d4fb402006-06-09 15:27:16 +10001031 return generic_writepages(mapping, wbc);
1032}
1033
Nathan Scottf51623b2006-03-14 13:26:27 +11001034/*
1035 * Called to move a page into cleanable state - and from there
Christoph Hellwig89f3b3632010-06-24 09:45:48 +10001036 * to be released. The page should already be clean. We always
Nathan Scottf51623b2006-03-14 13:26:27 +11001037 * have buffer heads in this call.
1038 *
Christoph Hellwig89f3b3632010-06-24 09:45:48 +10001039 * Returns 1 if the page is ok to release, 0 otherwise.
Nathan Scottf51623b2006-03-14 13:26:27 +11001040 */
1041STATIC int
Nathan Scott238f4c52006-03-17 17:26:25 +11001042xfs_vm_releasepage(
Nathan Scottf51623b2006-03-14 13:26:27 +11001043 struct page *page,
1044 gfp_t gfp_mask)
1045{
Christoph Hellwig20cb52e2010-06-24 09:46:01 +10001046 int delalloc, unwritten;
Nathan Scottf51623b2006-03-14 13:26:27 +11001047
Christoph Hellwig89f3b3632010-06-24 09:45:48 +10001048 trace_xfs_releasepage(page->mapping->host, page, 0);
Nathan Scott238f4c52006-03-17 17:26:25 +11001049
Christoph Hellwig20cb52e2010-06-24 09:46:01 +10001050 xfs_count_page_state(page, &delalloc, &unwritten);
Nathan Scottf51623b2006-03-14 13:26:27 +11001051
Christoph Hellwig89f3b3632010-06-24 09:45:48 +10001052 if (WARN_ON(delalloc))
1053 return 0;
1054 if (WARN_ON(unwritten))
Nathan Scottf51623b2006-03-14 13:26:27 +11001055 return 0;
1056
Nathan Scottf51623b2006-03-14 13:26:27 +11001057 return try_to_free_buffers(page);
1058}
1059
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060STATIC int
Nathan Scottc2536662006-03-29 10:44:40 +10001061__xfs_get_blocks(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 struct inode *inode,
1063 sector_t iblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 struct buffer_head *bh_result,
1065 int create,
Christoph Hellwigf2bde9b2010-06-24 11:44:35 +10001066 int direct)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067{
Christoph Hellwiga206c812010-12-10 08:42:20 +00001068 struct xfs_inode *ip = XFS_I(inode);
1069 struct xfs_mount *mp = ip->i_mount;
1070 xfs_fileoff_t offset_fsb, end_fsb;
1071 int error = 0;
1072 int lockmode = 0;
Christoph Hellwig207d0412010-04-28 12:28:56 +00001073 struct xfs_bmbt_irec imap;
Christoph Hellwiga206c812010-12-10 08:42:20 +00001074 int nimaps = 1;
Nathan Scottfdc7ed72005-11-02 15:13:13 +11001075 xfs_off_t offset;
1076 ssize_t size;
Christoph Hellwig207d0412010-04-28 12:28:56 +00001077 int new = 0;
Christoph Hellwiga206c812010-12-10 08:42:20 +00001078
1079 if (XFS_FORCED_SHUTDOWN(mp))
1080 return -XFS_ERROR(EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
Nathan Scottfdc7ed72005-11-02 15:13:13 +11001082 offset = (xfs_off_t)iblock << inode->i_blkbits;
Nathan Scottc2536662006-03-29 10:44:40 +10001083 ASSERT(bh_result->b_size >= (1 << inode->i_blkbits));
1084 size = bh_result->b_size;
Lachlan McIlroy364f3582008-09-17 16:50:14 +10001085
1086 if (!create && direct && offset >= i_size_read(inode))
1087 return 0;
1088
Christoph Hellwiga206c812010-12-10 08:42:20 +00001089 if (create) {
1090 lockmode = XFS_ILOCK_EXCL;
1091 xfs_ilock(ip, lockmode);
1092 } else {
1093 lockmode = xfs_ilock_map_shared(ip);
1094 }
Christoph Hellwigf2bde9b2010-06-24 11:44:35 +10001095
Christoph Hellwiga206c812010-12-10 08:42:20 +00001096 ASSERT(offset <= mp->m_maxioffset);
1097 if (offset + size > mp->m_maxioffset)
1098 size = mp->m_maxioffset - offset;
1099 end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + size);
1100 offset_fsb = XFS_B_TO_FSBT(mp, offset);
1101
Dave Chinner5c8ed202011-09-18 20:40:45 +00001102 error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb,
1103 &imap, &nimaps, XFS_BMAPI_ENTIRE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 if (error)
Christoph Hellwiga206c812010-12-10 08:42:20 +00001105 goto out_unlock;
1106
1107 if (create &&
1108 (!nimaps ||
1109 (imap.br_startblock == HOLESTARTBLOCK ||
1110 imap.br_startblock == DELAYSTARTBLOCK))) {
1111 if (direct) {
1112 error = xfs_iomap_write_direct(ip, offset, size,
1113 &imap, nimaps);
1114 } else {
1115 error = xfs_iomap_write_delay(ip, offset, size, &imap);
1116 }
1117 if (error)
1118 goto out_unlock;
1119
1120 trace_xfs_get_blocks_alloc(ip, offset, size, 0, &imap);
1121 } else if (nimaps) {
1122 trace_xfs_get_blocks_found(ip, offset, size, 0, &imap);
1123 } else {
1124 trace_xfs_get_blocks_notfound(ip, offset, size);
1125 goto out_unlock;
1126 }
1127 xfs_iunlock(ip, lockmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Christoph Hellwig207d0412010-04-28 12:28:56 +00001129 if (imap.br_startblock != HOLESTARTBLOCK &&
1130 imap.br_startblock != DELAYSTARTBLOCK) {
Nathan Scott87cbc492006-03-14 13:26:43 +11001131 /*
1132 * For unwritten extents do not report a disk address on
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 * the read case (treat as if we're reading into a hole).
1134 */
Christoph Hellwig207d0412010-04-28 12:28:56 +00001135 if (create || !ISUNWRITTEN(&imap))
1136 xfs_map_buffer(inode, bh_result, &imap, offset);
1137 if (create && ISUNWRITTEN(&imap)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 if (direct)
1139 bh_result->b_private = inode;
1140 set_buffer_unwritten(bh_result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 }
1142 }
1143
Nathan Scottc2536662006-03-29 10:44:40 +10001144 /*
1145 * If this is a realtime file, data may be on a different device.
1146 * to that pointed to from the buffer_head b_bdev currently.
1147 */
Christoph Hellwig046f1682010-04-28 12:28:52 +00001148 bh_result->b_bdev = xfs_find_bdev_for_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
Nathan Scottc2536662006-03-29 10:44:40 +10001150 /*
David Chinner549054a2007-02-10 18:36:35 +11001151 * If we previously allocated a block out beyond eof and we are now
1152 * coming back to use it then we will need to flag it as new even if it
1153 * has a disk address.
1154 *
1155 * With sub-block writes into unwritten extents we also need to mark
1156 * the buffer as new so that the unwritten parts of the buffer gets
1157 * correctly zeroed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 */
1159 if (create &&
1160 ((!buffer_mapped(bh_result) && !buffer_uptodate(bh_result)) ||
David Chinner549054a2007-02-10 18:36:35 +11001161 (offset >= i_size_read(inode)) ||
Christoph Hellwig207d0412010-04-28 12:28:56 +00001162 (new || ISUNWRITTEN(&imap))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 set_buffer_new(bh_result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
Christoph Hellwig207d0412010-04-28 12:28:56 +00001165 if (imap.br_startblock == DELAYSTARTBLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 BUG_ON(direct);
1167 if (create) {
1168 set_buffer_uptodate(bh_result);
1169 set_buffer_mapped(bh_result);
1170 set_buffer_delay(bh_result);
1171 }
1172 }
1173
Christoph Hellwig2b8f12b2010-04-28 12:28:59 +00001174 /*
1175 * If this is O_DIRECT or the mpage code calling tell them how large
1176 * the mapping is, so that we can avoid repeated get_blocks calls.
1177 */
Nathan Scottc2536662006-03-29 10:44:40 +10001178 if (direct || size > (1 << inode->i_blkbits)) {
Christoph Hellwig2b8f12b2010-04-28 12:28:59 +00001179 xfs_off_t mapping_size;
Christoph Hellwig9563b3d2010-04-28 12:28:53 +00001180
Christoph Hellwig2b8f12b2010-04-28 12:28:59 +00001181 mapping_size = imap.br_startoff + imap.br_blockcount - iblock;
1182 mapping_size <<= inode->i_blkbits;
1183
1184 ASSERT(mapping_size > 0);
1185 if (mapping_size > size)
1186 mapping_size = size;
1187 if (mapping_size > LONG_MAX)
1188 mapping_size = LONG_MAX;
1189
1190 bh_result->b_size = mapping_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 }
1192
1193 return 0;
Christoph Hellwiga206c812010-12-10 08:42:20 +00001194
1195out_unlock:
1196 xfs_iunlock(ip, lockmode);
1197 return -error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198}
1199
1200int
Nathan Scottc2536662006-03-29 10:44:40 +10001201xfs_get_blocks(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 struct inode *inode,
1203 sector_t iblock,
1204 struct buffer_head *bh_result,
1205 int create)
1206{
Christoph Hellwigf2bde9b2010-06-24 11:44:35 +10001207 return __xfs_get_blocks(inode, iblock, bh_result, create, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208}
1209
1210STATIC int
Nathan Scotte4c573b2006-03-14 13:54:26 +11001211xfs_get_blocks_direct(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 struct inode *inode,
1213 sector_t iblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 struct buffer_head *bh_result,
1215 int create)
1216{
Christoph Hellwigf2bde9b2010-06-24 11:44:35 +10001217 return __xfs_get_blocks(inode, iblock, bh_result, create, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218}
1219
Christoph Hellwig209fb872010-07-18 21:17:11 +00001220/*
1221 * Complete a direct I/O write request.
1222 *
1223 * If the private argument is non-NULL __xfs_get_blocks signals us that we
1224 * need to issue a transaction to convert the range from unwritten to written
1225 * extents. In case this is regular synchronous I/O we just call xfs_end_io
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001226 * to do this and we are done. But in case this was a successful AIO
Christoph Hellwig209fb872010-07-18 21:17:11 +00001227 * request this handler is called from interrupt context, from which we
1228 * can't start transactions. In that case offload the I/O completion to
1229 * the workqueues we also use for buffered I/O completion.
1230 */
Christoph Hellwigf0973862005-09-05 08:22:52 +10001231STATIC void
Christoph Hellwig209fb872010-07-18 21:17:11 +00001232xfs_end_io_direct_write(
1233 struct kiocb *iocb,
1234 loff_t offset,
1235 ssize_t size,
1236 void *private,
1237 int ret,
1238 bool is_async)
Christoph Hellwigf0973862005-09-05 08:22:52 +10001239{
Christoph Hellwig209fb872010-07-18 21:17:11 +00001240 struct xfs_ioend *ioend = iocb->private;
Christoph Hellwigf0973862005-09-05 08:22:52 +10001241
1242 /*
Christoph Hellwig2813d682011-12-18 20:00:12 +00001243 * While the generic direct I/O code updates the inode size, it does
1244 * so only after the end_io handler is called, which means our
1245 * end_io handler thinks the on-disk size is outside the in-core
1246 * size. To prevent this just update it a little bit earlier here.
1247 */
1248 if (offset + size > i_size_read(ioend->io_inode))
1249 i_size_write(ioend->io_inode, offset + size);
1250
1251 /*
Nathan Scottc41564b2006-03-29 08:55:14 +10001252 * blockdev_direct_IO can return an error even after the I/O
Christoph Hellwigf0973862005-09-05 08:22:52 +10001253 * completion handler was called. Thus we need to protect
1254 * against double-freeing.
1255 */
1256 iocb->private = NULL;
Christoph Hellwig40e2e972010-07-18 21:17:09 +00001257
Christoph Hellwig209fb872010-07-18 21:17:11 +00001258 ioend->io_offset = offset;
1259 ioend->io_size = size;
Christoph Hellwigc859cdd2011-08-23 08:28:10 +00001260 ioend->io_iocb = iocb;
1261 ioend->io_result = ret;
Christoph Hellwig209fb872010-07-18 21:17:11 +00001262 if (private && size > 0)
1263 ioend->io_type = IO_UNWRITTEN;
1264
1265 if (is_async) {
Christoph Hellwigc859cdd2011-08-23 08:28:10 +00001266 ioend->io_isasync = 1;
Christoph Hellwig209fb872010-07-18 21:17:11 +00001267 xfs_finish_ioend(ioend);
1268 } else {
1269 xfs_finish_ioend_sync(ioend);
1270 }
Christoph Hellwigf0973862005-09-05 08:22:52 +10001271}
1272
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273STATIC ssize_t
Nathan Scotte4c573b2006-03-14 13:54:26 +11001274xfs_vm_direct_IO(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 int rw,
1276 struct kiocb *iocb,
1277 const struct iovec *iov,
1278 loff_t offset,
1279 unsigned long nr_segs)
1280{
Christoph Hellwig209fb872010-07-18 21:17:11 +00001281 struct inode *inode = iocb->ki_filp->f_mapping->host;
1282 struct block_device *bdev = xfs_find_bdev_for_inode(inode);
1283 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
Christoph Hellwig209fb872010-07-18 21:17:11 +00001285 if (rw & WRITE) {
Christoph Hellwiga206c812010-12-10 08:42:20 +00001286 iocb->private = xfs_alloc_ioend(inode, IO_DIRECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
Christoph Hellwigeafdc7d2010-06-04 11:29:53 +02001288 ret = __blockdev_direct_IO(rw, iocb, inode, bdev, iov,
1289 offset, nr_segs,
1290 xfs_get_blocks_direct,
1291 xfs_end_io_direct_write, NULL, 0);
Christoph Hellwig209fb872010-07-18 21:17:11 +00001292 if (ret != -EIOCBQUEUED && iocb->private)
1293 xfs_destroy_ioend(iocb->private);
1294 } else {
Christoph Hellwigeafdc7d2010-06-04 11:29:53 +02001295 ret = __blockdev_direct_IO(rw, iocb, inode, bdev, iov,
1296 offset, nr_segs,
1297 xfs_get_blocks_direct,
1298 NULL, NULL, 0);
Christoph Hellwig209fb872010-07-18 21:17:11 +00001299 }
Christoph Hellwig5fe878ae2009-12-15 16:47:50 -08001300
Christoph Hellwigf0973862005-09-05 08:22:52 +10001301 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302}
1303
Christoph Hellwigfa9b2272010-06-14 05:17:31 -04001304STATIC void
1305xfs_vm_write_failed(
1306 struct address_space *mapping,
1307 loff_t to)
1308{
1309 struct inode *inode = mapping->host;
1310
1311 if (to > inode->i_size) {
Dave Chinnerc726de42010-11-30 15:14:39 +11001312 /*
Christoph Hellwig2813d682011-12-18 20:00:12 +00001313 * Punch out the delalloc blocks we have already allocated.
1314 *
1315 * Don't bother with xfs_setattr given that nothing can have
1316 * made it to disk yet as the page is still locked at this
1317 * point.
Dave Chinnerc726de42010-11-30 15:14:39 +11001318 */
1319 struct xfs_inode *ip = XFS_I(inode);
1320 xfs_fileoff_t start_fsb;
1321 xfs_fileoff_t end_fsb;
1322 int error;
1323
1324 truncate_pagecache(inode, to, inode->i_size);
1325
1326 /*
1327 * Check if there are any blocks that are outside of i_size
1328 * that need to be trimmed back.
1329 */
1330 start_fsb = XFS_B_TO_FSB(ip->i_mount, inode->i_size) + 1;
1331 end_fsb = XFS_B_TO_FSB(ip->i_mount, to);
1332 if (end_fsb <= start_fsb)
1333 return;
1334
1335 xfs_ilock(ip, XFS_ILOCK_EXCL);
1336 error = xfs_bmap_punch_delalloc_range(ip, start_fsb,
1337 end_fsb - start_fsb);
1338 if (error) {
1339 /* something screwed, just bail */
1340 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
Dave Chinner4f107002011-03-07 10:00:35 +11001341 xfs_alert(ip->i_mount,
Dave Chinnerc726de42010-11-30 15:14:39 +11001342 "xfs_vm_write_failed: unable to clean up ino %lld",
1343 ip->i_ino);
1344 }
1345 }
1346 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Christoph Hellwigfa9b2272010-06-14 05:17:31 -04001347 }
1348}
1349
Nathan Scottf51623b2006-03-14 13:26:27 +11001350STATIC int
Nick Piggind79689c2007-10-16 01:25:06 -07001351xfs_vm_write_begin(
Nathan Scottf51623b2006-03-14 13:26:27 +11001352 struct file *file,
Nick Piggind79689c2007-10-16 01:25:06 -07001353 struct address_space *mapping,
1354 loff_t pos,
1355 unsigned len,
1356 unsigned flags,
1357 struct page **pagep,
1358 void **fsdata)
Nathan Scottf51623b2006-03-14 13:26:27 +11001359{
Christoph Hellwig155130a2010-06-04 11:29:58 +02001360 int ret;
1361
1362 ret = block_write_begin(mapping, pos, len, flags | AOP_FLAG_NOFS,
1363 pagep, xfs_get_blocks);
Christoph Hellwigfa9b2272010-06-14 05:17:31 -04001364 if (unlikely(ret))
1365 xfs_vm_write_failed(mapping, pos + len);
1366 return ret;
1367}
Christoph Hellwig155130a2010-06-04 11:29:58 +02001368
Christoph Hellwigfa9b2272010-06-14 05:17:31 -04001369STATIC int
1370xfs_vm_write_end(
1371 struct file *file,
1372 struct address_space *mapping,
1373 loff_t pos,
1374 unsigned len,
1375 unsigned copied,
1376 struct page *page,
1377 void *fsdata)
1378{
1379 int ret;
1380
1381 ret = generic_write_end(file, mapping, pos, len, copied, page, fsdata);
1382 if (unlikely(ret < len))
1383 xfs_vm_write_failed(mapping, pos + len);
Christoph Hellwig155130a2010-06-04 11:29:58 +02001384 return ret;
Nathan Scottf51623b2006-03-14 13:26:27 +11001385}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
1387STATIC sector_t
Nathan Scotte4c573b2006-03-14 13:54:26 +11001388xfs_vm_bmap(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 struct address_space *mapping,
1390 sector_t block)
1391{
1392 struct inode *inode = (struct inode *)mapping->host;
Christoph Hellwig739bfb22007-08-29 10:58:01 +10001393 struct xfs_inode *ip = XFS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
Christoph Hellwigcca28fb2010-06-24 11:57:09 +10001395 trace_xfs_vm_bmap(XFS_I(inode));
Christoph Hellwig126468b2008-03-06 13:44:57 +11001396 xfs_ilock(ip, XFS_IOLOCK_SHARED);
Christoph Hellwig739bfb22007-08-29 10:58:01 +10001397 xfs_flush_pages(ip, (xfs_off_t)0, -1, 0, FI_REMAPF);
Christoph Hellwig126468b2008-03-06 13:44:57 +11001398 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
Nathan Scottc2536662006-03-29 10:44:40 +10001399 return generic_block_bmap(mapping, block, xfs_get_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400}
1401
1402STATIC int
Nathan Scotte4c573b2006-03-14 13:54:26 +11001403xfs_vm_readpage(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 struct file *unused,
1405 struct page *page)
1406{
Nathan Scottc2536662006-03-29 10:44:40 +10001407 return mpage_readpage(page, xfs_get_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408}
1409
1410STATIC int
Nathan Scotte4c573b2006-03-14 13:54:26 +11001411xfs_vm_readpages(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 struct file *unused,
1413 struct address_space *mapping,
1414 struct list_head *pages,
1415 unsigned nr_pages)
1416{
Nathan Scottc2536662006-03-29 10:44:40 +10001417 return mpage_readpages(mapping, pages, nr_pages, xfs_get_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418}
1419
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07001420const struct address_space_operations xfs_address_space_operations = {
Nathan Scotte4c573b2006-03-14 13:54:26 +11001421 .readpage = xfs_vm_readpage,
1422 .readpages = xfs_vm_readpages,
1423 .writepage = xfs_vm_writepage,
Nathan Scott7d4fb402006-06-09 15:27:16 +10001424 .writepages = xfs_vm_writepages,
Nathan Scott238f4c52006-03-17 17:26:25 +11001425 .releasepage = xfs_vm_releasepage,
1426 .invalidatepage = xfs_vm_invalidatepage,
Nick Piggind79689c2007-10-16 01:25:06 -07001427 .write_begin = xfs_vm_write_begin,
Christoph Hellwigfa9b2272010-06-14 05:17:31 -04001428 .write_end = xfs_vm_write_end,
Nathan Scotte4c573b2006-03-14 13:54:26 +11001429 .bmap = xfs_vm_bmap,
1430 .direct_IO = xfs_vm_direct_IO,
Christoph Lametere965f962006-02-01 03:05:41 -08001431 .migratepage = buffer_migrate_page,
Hisashi Hifumibddaafa2009-03-29 09:53:38 +02001432 .is_partially_uptodate = block_is_partially_uptodate,
Andi Kleenaa261f52009-09-16 11:50:16 +02001433 .error_remove_page = generic_error_remove_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434};