blob: 540a01742c6d85a10a9842e354ebdde0587eaa43 [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/*
Dave Chinner932640e2009-10-06 20:29:29 +0000102 * If the end of the current ioend is beyond the current EOF,
103 * return the new EOF value, otherwise zero.
104 */
105STATIC xfs_fsize_t
106xfs_ioend_new_eof(
107 xfs_ioend_t *ioend)
108{
109 xfs_inode_t *ip = XFS_I(ioend->io_inode);
110 xfs_fsize_t isize;
111 xfs_fsize_t bsize;
112
113 bsize = ioend->io_offset + ioend->io_size;
Christoph Hellwig2813d682011-12-18 20:00:12 +0000114 isize = MIN(i_size_read(VFS_I(ip)), bsize);
Dave Chinner932640e2009-10-06 20:29:29 +0000115 return isize > ip->i_d.di_size ? isize : 0;
116}
117
118/*
Christoph Hellwigfc0063c2011-08-23 08:28:11 +0000119 * Fast and loose check if this write could update the on-disk inode size.
120 */
121static inline bool xfs_ioend_is_append(struct xfs_ioend *ioend)
122{
123 return ioend->io_offset + ioend->io_size >
124 XFS_I(ioend->io_inode)->i_d.di_size;
125}
126
127/*
Christoph Hellwig2813d682011-12-18 20:00:12 +0000128 * Update on-disk file size now that data has been written to disk.
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000129 */
Christoph Hellwigaa6bf012012-02-29 09:53:48 +0000130STATIC void
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000131xfs_setfilesize(
Christoph Hellwigaa6bf012012-02-29 09:53:48 +0000132 struct xfs_ioend *ioend)
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000133{
Christoph Hellwigaa6bf012012-02-29 09:53:48 +0000134 struct xfs_inode *ip = XFS_I(ioend->io_inode);
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000135 xfs_fsize_t isize;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000136
Christoph Hellwigaa6bf012012-02-29 09:53:48 +0000137 xfs_ilock(ip, XFS_ILOCK_EXCL);
Dave Chinner932640e2009-10-06 20:29:29 +0000138 isize = xfs_ioend_new_eof(ioend);
139 if (isize) {
Dave Chinner55fb25d52011-07-18 03:40:19 +0000140 trace_xfs_setfilesize(ip, ioend->io_offset, ioend->io_size);
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000141 ip->i_d.di_size = isize;
Christoph Hellwig66d834e2010-02-15 09:44:49 +0000142 xfs_mark_inode_dirty(ip);
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000143 }
144
145 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig0829c362005-09-02 16:58:49 +1000146}
147
148/*
Christoph Hellwig209fb872010-07-18 21:17:11 +0000149 * Schedule IO completion handling on the final put of an ioend.
Christoph Hellwigfc0063c2011-08-23 08:28:11 +0000150 *
151 * If there is no work to do we might as well call it a day and free the
152 * ioend right now.
Dave Chinnerc626d172009-04-06 18:42:11 +0200153 */
154STATIC void
155xfs_finish_ioend(
Christoph Hellwig209fb872010-07-18 21:17:11 +0000156 struct xfs_ioend *ioend)
Dave Chinnerc626d172009-04-06 18:42:11 +0200157{
158 if (atomic_dec_and_test(&ioend->io_remaining)) {
Christoph Hellwigaa6bf012012-02-29 09:53:48 +0000159 struct xfs_mount *mp = XFS_I(ioend->io_inode)->i_mount;
160
Christoph Hellwig209fb872010-07-18 21:17:11 +0000161 if (ioend->io_type == IO_UNWRITTEN)
Christoph Hellwigaa6bf012012-02-29 09:53:48 +0000162 queue_work(mp->m_unwritten_workqueue, &ioend->io_work);
Christoph Hellwigfc0063c2011-08-23 08:28:11 +0000163 else if (xfs_ioend_is_append(ioend))
Christoph Hellwigaa6bf012012-02-29 09:53:48 +0000164 queue_work(mp->m_data_workqueue, &ioend->io_work);
Christoph Hellwigfc0063c2011-08-23 08:28:11 +0000165 else
166 xfs_destroy_ioend(ioend);
Dave Chinnerc626d172009-04-06 18:42:11 +0200167 }
168}
169
170/*
Dave Chinner77d7a0c2010-02-17 05:36:29 +0000171 * IO write completion.
172 */
173STATIC void
174xfs_end_io(
175 struct work_struct *work)
176{
177 xfs_ioend_t *ioend = container_of(work, xfs_ioend_t, io_work);
178 struct xfs_inode *ip = XFS_I(ioend->io_inode);
Dave Chinner69418932010-03-04 00:57:09 +0000179 int error = 0;
Dave Chinner77d7a0c2010-02-17 05:36:29 +0000180
Christoph Hellwig04f658e2011-08-24 05:59:25 +0000181 if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
Christoph Hellwig810627d2011-11-08 08:56:15 +0000182 ioend->io_error = -EIO;
Christoph Hellwig04f658e2011-08-24 05:59:25 +0000183 goto done;
184 }
185 if (ioend->io_error)
186 goto done;
187
Dave Chinner77d7a0c2010-02-17 05:36:29 +0000188 /*
189 * For unwritten extents we need to issue transactions to convert a
190 * range to normal written extens after the data I/O has finished.
191 */
Christoph Hellwig04f658e2011-08-24 05:59:25 +0000192 if (ioend->io_type == IO_UNWRITTEN) {
Dave Chinner77d7a0c2010-02-17 05:36:29 +0000193 error = xfs_iomap_write_unwritten(ip, ioend->io_offset,
194 ioend->io_size);
Christoph Hellwig04f658e2011-08-24 05:59:25 +0000195 if (error) {
196 ioend->io_error = -error;
197 goto done;
198 }
Dave Chinner77d7a0c2010-02-17 05:36:29 +0000199 }
200
201 /*
202 * We might have to update the on-disk file size after extending
203 * writes.
204 */
Christoph Hellwigaa6bf012012-02-29 09:53:48 +0000205 xfs_setfilesize(ioend);
Christoph Hellwig04f658e2011-08-24 05:59:25 +0000206done:
Christoph Hellwigaa6bf012012-02-29 09:53:48 +0000207 xfs_destroy_ioend(ioend);
Dave Chinner77d7a0c2010-02-17 05:36:29 +0000208}
209
210/*
Christoph Hellwig209fb872010-07-18 21:17:11 +0000211 * Call IO completion handling in caller context on the final put of an ioend.
212 */
213STATIC void
214xfs_finish_ioend_sync(
215 struct xfs_ioend *ioend)
216{
217 if (atomic_dec_and_test(&ioend->io_remaining))
218 xfs_end_io(&ioend->io_work);
219}
220
221/*
Christoph Hellwig0829c362005-09-02 16:58:49 +1000222 * Allocate and initialise an IO completion structure.
223 * We need to track unwritten extent write completion here initially.
224 * We'll need to extend this for updating the ondisk inode size later
225 * (vs. incore size).
226 */
227STATIC xfs_ioend_t *
228xfs_alloc_ioend(
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100229 struct inode *inode,
230 unsigned int type)
Christoph Hellwig0829c362005-09-02 16:58:49 +1000231{
232 xfs_ioend_t *ioend;
233
234 ioend = mempool_alloc(xfs_ioend_pool, GFP_NOFS);
235
236 /*
237 * Set the count to 1 initially, which will prevent an I/O
238 * completion callback from happening before we have started
239 * all the I/O from calling the completion routine too early.
240 */
241 atomic_set(&ioend->io_remaining, 1);
Christoph Hellwigc859cdd2011-08-23 08:28:10 +0000242 ioend->io_isasync = 0;
Nathan Scott7d04a332006-06-09 14:58:38 +1000243 ioend->io_error = 0;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100244 ioend->io_list = NULL;
245 ioend->io_type = type;
Christoph Hellwigb677c212007-08-29 11:46:28 +1000246 ioend->io_inode = inode;
Christoph Hellwigc1a073b2005-09-05 08:23:35 +1000247 ioend->io_buffer_head = NULL;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100248 ioend->io_buffer_tail = NULL;
Christoph Hellwig0829c362005-09-02 16:58:49 +1000249 ioend->io_offset = 0;
250 ioend->io_size = 0;
Christoph Hellwigfb511f22010-07-18 21:17:10 +0000251 ioend->io_iocb = NULL;
252 ioend->io_result = 0;
Christoph Hellwig0829c362005-09-02 16:58:49 +1000253
Christoph Hellwig5ec4fab2009-10-30 09:11:47 +0000254 INIT_WORK(&ioend->io_work, xfs_end_io);
Christoph Hellwig0829c362005-09-02 16:58:49 +1000255 return ioend;
256}
257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258STATIC int
259xfs_map_blocks(
260 struct inode *inode,
261 loff_t offset,
Christoph Hellwig207d0412010-04-28 12:28:56 +0000262 struct xfs_bmbt_irec *imap,
Christoph Hellwiga206c812010-12-10 08:42:20 +0000263 int type,
264 int nonblocking)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Christoph Hellwiga206c812010-12-10 08:42:20 +0000266 struct xfs_inode *ip = XFS_I(inode);
267 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwiged1e7b72010-12-10 08:42:22 +0000268 ssize_t count = 1 << inode->i_blkbits;
Christoph Hellwiga206c812010-12-10 08:42:20 +0000269 xfs_fileoff_t offset_fsb, end_fsb;
270 int error = 0;
Christoph Hellwiga206c812010-12-10 08:42:20 +0000271 int bmapi_flags = XFS_BMAPI_ENTIRE;
272 int nimaps = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Christoph Hellwiga206c812010-12-10 08:42:20 +0000274 if (XFS_FORCED_SHUTDOWN(mp))
275 return -XFS_ERROR(EIO);
276
Christoph Hellwig8ff29572010-12-10 08:42:21 +0000277 if (type == IO_UNWRITTEN)
Christoph Hellwiga206c812010-12-10 08:42:20 +0000278 bmapi_flags |= XFS_BMAPI_IGSTATE;
Christoph Hellwiga206c812010-12-10 08:42:20 +0000279
Christoph Hellwig8ff29572010-12-10 08:42:21 +0000280 if (!xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
281 if (nonblocking)
282 return -XFS_ERROR(EAGAIN);
283 xfs_ilock(ip, XFS_ILOCK_SHARED);
Christoph Hellwiga206c812010-12-10 08:42:20 +0000284 }
285
Christoph Hellwig8ff29572010-12-10 08:42:21 +0000286 ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
287 (ip->i_df.if_flags & XFS_IFEXTENTS));
Christoph Hellwiga206c812010-12-10 08:42:20 +0000288 ASSERT(offset <= mp->m_maxioffset);
Christoph Hellwig8ff29572010-12-10 08:42:21 +0000289
Christoph Hellwiga206c812010-12-10 08:42:20 +0000290 if (offset + count > mp->m_maxioffset)
291 count = mp->m_maxioffset - offset;
292 end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
293 offset_fsb = XFS_B_TO_FSBT(mp, offset);
Dave Chinner5c8ed202011-09-18 20:40:45 +0000294 error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb,
295 imap, &nimaps, bmapi_flags);
Christoph Hellwig8ff29572010-12-10 08:42:21 +0000296 xfs_iunlock(ip, XFS_ILOCK_SHARED);
297
Christoph Hellwiga206c812010-12-10 08:42:20 +0000298 if (error)
Christoph Hellwig8ff29572010-12-10 08:42:21 +0000299 return -XFS_ERROR(error);
Christoph Hellwiga206c812010-12-10 08:42:20 +0000300
Christoph Hellwig8ff29572010-12-10 08:42:21 +0000301 if (type == IO_DELALLOC &&
302 (!nimaps || isnullstartblock(imap->br_startblock))) {
Christoph Hellwiga206c812010-12-10 08:42:20 +0000303 error = xfs_iomap_write_allocate(ip, offset, count, imap);
304 if (!error)
305 trace_xfs_map_blocks_alloc(ip, offset, count, type, imap);
Christoph Hellwig8ff29572010-12-10 08:42:21 +0000306 return -XFS_ERROR(error);
Christoph Hellwiga206c812010-12-10 08:42:20 +0000307 }
308
Christoph Hellwig8ff29572010-12-10 08:42:21 +0000309#ifdef DEBUG
310 if (type == IO_UNWRITTEN) {
311 ASSERT(nimaps);
312 ASSERT(imap->br_startblock != HOLESTARTBLOCK);
313 ASSERT(imap->br_startblock != DELAYSTARTBLOCK);
314 }
315#endif
316 if (nimaps)
317 trace_xfs_map_blocks_found(ip, offset, count, type, imap);
318 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319}
320
Christoph Hellwigb8f82a42009-11-14 16:17:22 +0000321STATIC int
Christoph Hellwig558e6892010-04-28 12:28:58 +0000322xfs_imap_valid(
Christoph Hellwig8699bb02010-04-28 12:28:54 +0000323 struct inode *inode,
Christoph Hellwig207d0412010-04-28 12:28:56 +0000324 struct xfs_bmbt_irec *imap,
Christoph Hellwig558e6892010-04-28 12:28:58 +0000325 xfs_off_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
Christoph Hellwig558e6892010-04-28 12:28:58 +0000327 offset >>= inode->i_blkbits;
Christoph Hellwig8699bb02010-04-28 12:28:54 +0000328
Christoph Hellwig558e6892010-04-28 12:28:58 +0000329 return offset >= imap->br_startoff &&
330 offset < imap->br_startoff + imap->br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331}
332
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100333/*
334 * BIO completion handler for buffered IO.
335 */
Al Viro782e3b32007-10-12 07:17:47 +0100336STATIC void
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100337xfs_end_bio(
338 struct bio *bio,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100339 int error)
340{
341 xfs_ioend_t *ioend = bio->bi_private;
342
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100343 ASSERT(atomic_read(&bio->bi_cnt) >= 1);
Nathan Scott7d04a332006-06-09 14:58:38 +1000344 ioend->io_error = test_bit(BIO_UPTODATE, &bio->bi_flags) ? 0 : error;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100345
346 /* Toss bio and pass work off to an xfsdatad thread */
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100347 bio->bi_private = NULL;
348 bio->bi_end_io = NULL;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100349 bio_put(bio);
Nathan Scott7d04a332006-06-09 14:58:38 +1000350
Christoph Hellwig209fb872010-07-18 21:17:11 +0000351 xfs_finish_ioend(ioend);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100352}
353
354STATIC void
355xfs_submit_ioend_bio(
Christoph Hellwig06342cf2009-10-30 09:09:15 +0000356 struct writeback_control *wbc,
357 xfs_ioend_t *ioend,
358 struct bio *bio)
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100359{
360 atomic_inc(&ioend->io_remaining);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100361 bio->bi_private = ioend;
362 bio->bi_end_io = xfs_end_bio;
363
Dave Chinner932640e2009-10-06 20:29:29 +0000364 /*
365 * If the I/O is beyond EOF we mark the inode dirty immediately
366 * but don't update the inode size until I/O completion.
367 */
368 if (xfs_ioend_new_eof(ioend))
Christoph Hellwig66d834e2010-02-15 09:44:49 +0000369 xfs_mark_inode_dirty(XFS_I(ioend->io_inode));
Dave Chinner932640e2009-10-06 20:29:29 +0000370
Jens Axboe721a9602011-03-09 11:56:30 +0100371 submit_bio(wbc->sync_mode == WB_SYNC_ALL ? WRITE_SYNC : WRITE, bio);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100372}
373
374STATIC struct bio *
375xfs_alloc_ioend_bio(
376 struct buffer_head *bh)
377{
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100378 int nvecs = bio_get_nr_vecs(bh->b_bdev);
Christoph Hellwig221cb252010-12-10 08:42:17 +0000379 struct bio *bio = bio_alloc(GFP_NOIO, nvecs);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100380
381 ASSERT(bio->bi_private == NULL);
382 bio->bi_sector = bh->b_blocknr * (bh->b_size >> 9);
383 bio->bi_bdev = bh->b_bdev;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100384 return bio;
385}
386
387STATIC void
388xfs_start_buffer_writeback(
389 struct buffer_head *bh)
390{
391 ASSERT(buffer_mapped(bh));
392 ASSERT(buffer_locked(bh));
393 ASSERT(!buffer_delay(bh));
394 ASSERT(!buffer_unwritten(bh));
395
396 mark_buffer_async_write(bh);
397 set_buffer_uptodate(bh);
398 clear_buffer_dirty(bh);
399}
400
401STATIC void
402xfs_start_page_writeback(
403 struct page *page,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100404 int clear_dirty,
405 int buffers)
406{
407 ASSERT(PageLocked(page));
408 ASSERT(!PageWriteback(page));
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100409 if (clear_dirty)
David Chinner92132022006-12-21 10:24:01 +1100410 clear_page_dirty_for_io(page);
411 set_page_writeback(page);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100412 unlock_page(page);
Fengguang Wu1f7decf2007-10-16 23:30:42 -0700413 /* If no buffers on the page are to be written, finish it here */
414 if (!buffers)
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100415 end_page_writeback(page);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100416}
417
418static inline int bio_add_buffer(struct bio *bio, struct buffer_head *bh)
419{
420 return bio_add_page(bio, bh->b_page, bh->b_size, bh_offset(bh));
421}
422
423/*
David Chinnerd88992f2006-01-18 13:38:12 +1100424 * Submit all of the bios for all of the ioends we have saved up, covering the
425 * initial writepage page and also any probed pages.
426 *
427 * Because we may have multiple ioends spanning a page, we need to start
428 * writeback on all the buffers before we submit them for I/O. If we mark the
429 * buffers as we got, then we can end up with a page that only has buffers
430 * marked async write and I/O complete on can occur before we mark the other
431 * buffers async write.
432 *
433 * The end result of this is that we trip a bug in end_page_writeback() because
434 * we call it twice for the one page as the code in end_buffer_async_write()
435 * assumes that all buffers on the page are started at the same time.
436 *
437 * The fix is two passes across the ioend list - one to start writeback on the
Nathan Scottc41564b2006-03-29 08:55:14 +1000438 * buffer_heads, and then submit them for I/O on the second pass.
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100439 */
440STATIC void
441xfs_submit_ioend(
Christoph Hellwig06342cf2009-10-30 09:09:15 +0000442 struct writeback_control *wbc,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100443 xfs_ioend_t *ioend)
444{
David Chinnerd88992f2006-01-18 13:38:12 +1100445 xfs_ioend_t *head = ioend;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100446 xfs_ioend_t *next;
447 struct buffer_head *bh;
448 struct bio *bio;
449 sector_t lastblock = 0;
450
David Chinnerd88992f2006-01-18 13:38:12 +1100451 /* Pass 1 - start writeback */
452 do {
453 next = ioend->io_list;
Christoph Hellwig221cb252010-12-10 08:42:17 +0000454 for (bh = ioend->io_buffer_head; bh; bh = bh->b_private)
David Chinnerd88992f2006-01-18 13:38:12 +1100455 xfs_start_buffer_writeback(bh);
David Chinnerd88992f2006-01-18 13:38:12 +1100456 } while ((ioend = next) != NULL);
457
458 /* Pass 2 - submit I/O */
459 ioend = head;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100460 do {
461 next = ioend->io_list;
462 bio = NULL;
463
464 for (bh = ioend->io_buffer_head; bh; bh = bh->b_private) {
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100465
466 if (!bio) {
467 retry:
468 bio = xfs_alloc_ioend_bio(bh);
469 } else if (bh->b_blocknr != lastblock + 1) {
Christoph Hellwig06342cf2009-10-30 09:09:15 +0000470 xfs_submit_ioend_bio(wbc, ioend, bio);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100471 goto retry;
472 }
473
474 if (bio_add_buffer(bio, bh) != bh->b_size) {
Christoph Hellwig06342cf2009-10-30 09:09:15 +0000475 xfs_submit_ioend_bio(wbc, ioend, bio);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100476 goto retry;
477 }
478
479 lastblock = bh->b_blocknr;
480 }
481 if (bio)
Christoph Hellwig06342cf2009-10-30 09:09:15 +0000482 xfs_submit_ioend_bio(wbc, ioend, bio);
Christoph Hellwig209fb872010-07-18 21:17:11 +0000483 xfs_finish_ioend(ioend);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100484 } while ((ioend = next) != NULL);
485}
486
487/*
488 * Cancel submission of all buffer_heads so far in this endio.
489 * Toss the endio too. Only ever called for the initial page
490 * in a writepage request, so only ever one page.
491 */
492STATIC void
493xfs_cancel_ioend(
494 xfs_ioend_t *ioend)
495{
496 xfs_ioend_t *next;
497 struct buffer_head *bh, *next_bh;
498
499 do {
500 next = ioend->io_list;
501 bh = ioend->io_buffer_head;
502 do {
503 next_bh = bh->b_private;
504 clear_buffer_async_write(bh);
505 unlock_buffer(bh);
506 } while ((bh = next_bh) != NULL);
507
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100508 mempool_free(ioend, xfs_ioend_pool);
509 } while ((ioend = next) != NULL);
510}
511
512/*
513 * Test to see if we've been building up a completion structure for
514 * earlier buffers -- if so, we try to append to this ioend if we
515 * can, otherwise we finish off any current ioend and start another.
516 * Return true if we've finished the given ioend.
517 */
518STATIC void
519xfs_add_to_ioend(
520 struct inode *inode,
521 struct buffer_head *bh,
Christoph Hellwig7336cea2006-01-11 20:49:16 +1100522 xfs_off_t offset,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100523 unsigned int type,
524 xfs_ioend_t **result,
525 int need_ioend)
526{
527 xfs_ioend_t *ioend = *result;
528
529 if (!ioend || need_ioend || type != ioend->io_type) {
530 xfs_ioend_t *previous = *result;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100531
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100532 ioend = xfs_alloc_ioend(inode, type);
533 ioend->io_offset = offset;
534 ioend->io_buffer_head = bh;
535 ioend->io_buffer_tail = bh;
536 if (previous)
537 previous->io_list = ioend;
538 *result = ioend;
539 } else {
540 ioend->io_buffer_tail->b_private = bh;
541 ioend->io_buffer_tail = bh;
542 }
543
544 bh->b_private = NULL;
545 ioend->io_size += bh->b_size;
546}
547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548STATIC void
Nathan Scott87cbc492006-03-14 13:26:43 +1100549xfs_map_buffer(
Christoph Hellwig046f1682010-04-28 12:28:52 +0000550 struct inode *inode,
Nathan Scott87cbc492006-03-14 13:26:43 +1100551 struct buffer_head *bh,
Christoph Hellwig207d0412010-04-28 12:28:56 +0000552 struct xfs_bmbt_irec *imap,
Christoph Hellwig046f1682010-04-28 12:28:52 +0000553 xfs_off_t offset)
Nathan Scott87cbc492006-03-14 13:26:43 +1100554{
555 sector_t bn;
Christoph Hellwig8699bb02010-04-28 12:28:54 +0000556 struct xfs_mount *m = XFS_I(inode)->i_mount;
Christoph Hellwig207d0412010-04-28 12:28:56 +0000557 xfs_off_t iomap_offset = XFS_FSB_TO_B(m, imap->br_startoff);
558 xfs_daddr_t iomap_bn = xfs_fsb_to_db(XFS_I(inode), imap->br_startblock);
Nathan Scott87cbc492006-03-14 13:26:43 +1100559
Christoph Hellwig207d0412010-04-28 12:28:56 +0000560 ASSERT(imap->br_startblock != HOLESTARTBLOCK);
561 ASSERT(imap->br_startblock != DELAYSTARTBLOCK);
Nathan Scott87cbc492006-03-14 13:26:43 +1100562
Christoph Hellwige5131822010-04-28 12:28:55 +0000563 bn = (iomap_bn >> (inode->i_blkbits - BBSHIFT)) +
Christoph Hellwig8699bb02010-04-28 12:28:54 +0000564 ((offset - iomap_offset) >> inode->i_blkbits);
Nathan Scott87cbc492006-03-14 13:26:43 +1100565
Christoph Hellwig046f1682010-04-28 12:28:52 +0000566 ASSERT(bn || XFS_IS_REALTIME_INODE(XFS_I(inode)));
Nathan Scott87cbc492006-03-14 13:26:43 +1100567
568 bh->b_blocknr = bn;
569 set_buffer_mapped(bh);
570}
571
572STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573xfs_map_at_offset(
Christoph Hellwig046f1682010-04-28 12:28:52 +0000574 struct inode *inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 struct buffer_head *bh,
Christoph Hellwig207d0412010-04-28 12:28:56 +0000576 struct xfs_bmbt_irec *imap,
Christoph Hellwig046f1682010-04-28 12:28:52 +0000577 xfs_off_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
Christoph Hellwig207d0412010-04-28 12:28:56 +0000579 ASSERT(imap->br_startblock != HOLESTARTBLOCK);
580 ASSERT(imap->br_startblock != DELAYSTARTBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Christoph Hellwig207d0412010-04-28 12:28:56 +0000582 xfs_map_buffer(inode, bh, imap, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 set_buffer_mapped(bh);
584 clear_buffer_delay(bh);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100585 clear_buffer_unwritten(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586}
587
588/*
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100589 * Test if a given page is suitable for writing as part of an unwritten
590 * or delayed allocate extent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 */
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100592STATIC int
593xfs_is_delayed_page(
594 struct page *page,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100595 unsigned int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 if (PageWriteback(page))
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100598 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600 if (page->mapping && page_has_buffers(page)) {
601 struct buffer_head *bh, *head;
602 int acceptable = 0;
603
604 bh = head = page_buffers(page);
605 do {
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100606 if (buffer_unwritten(bh))
Christoph Hellwig34a52c62010-04-28 12:28:57 +0000607 acceptable = (type == IO_UNWRITTEN);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100608 else if (buffer_delay(bh))
Christoph Hellwiga206c812010-12-10 08:42:20 +0000609 acceptable = (type == IO_DELALLOC);
David Chinner2ddee842006-03-22 12:47:40 +1100610 else if (buffer_dirty(bh) && buffer_mapped(bh))
Christoph Hellwiga206c812010-12-10 08:42:20 +0000611 acceptable = (type == IO_OVERWRITE);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100612 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 } while ((bh = bh->b_this_page) != head);
615
616 if (acceptable)
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100617 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 }
619
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100620 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621}
622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623/*
624 * Allocate & map buffers for page given the extent map. Write it out.
625 * except for the original page of a writepage, this is called on
626 * delalloc/unwritten pages only, for the original page it is possible
627 * that the page has no mapping at all.
628 */
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100629STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630xfs_convert_page(
631 struct inode *inode,
632 struct page *page,
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100633 loff_t tindex,
Christoph Hellwig207d0412010-04-28 12:28:56 +0000634 struct xfs_bmbt_irec *imap,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100635 xfs_ioend_t **ioendp,
Christoph Hellwig2fa24f92010-12-10 08:42:23 +0000636 struct writeback_control *wbc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100638 struct buffer_head *bh, *head;
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100639 xfs_off_t end_offset;
640 unsigned long p_offset;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100641 unsigned int type;
Nathan Scott24e17b52005-05-05 13:33:20 -0700642 int len, page_dirty;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100643 int count = 0, done = 0, uptodate = 1;
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100644 xfs_off_t offset = page_offset(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100646 if (page->index != tindex)
647 goto fail;
Nick Piggin529ae9a2008-08-02 12:01:03 +0200648 if (!trylock_page(page))
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100649 goto fail;
650 if (PageWriteback(page))
651 goto fail_unlock_page;
652 if (page->mapping != inode->i_mapping)
653 goto fail_unlock_page;
654 if (!xfs_is_delayed_page(page, (*ioendp)->io_type))
655 goto fail_unlock_page;
656
Nathan Scott24e17b52005-05-05 13:33:20 -0700657 /*
658 * page_dirty is initially a count of buffers on the page before
Nathan Scottc41564b2006-03-29 08:55:14 +1000659 * EOF and is decremented as we move each into a cleanable state.
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100660 *
661 * Derivation:
662 *
663 * End offset is the highest offset that this page should represent.
664 * If we are on the last page, (end_offset & (PAGE_CACHE_SIZE - 1))
665 * will evaluate non-zero and be less than PAGE_CACHE_SIZE and
666 * hence give us the correct page_dirty count. On any other page,
667 * it will be zero and in that case we need page_dirty to be the
668 * count of buffers on the page.
Nathan Scott24e17b52005-05-05 13:33:20 -0700669 */
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100670 end_offset = min_t(unsigned long long,
671 (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT,
672 i_size_read(inode));
673
Nathan Scott24e17b52005-05-05 13:33:20 -0700674 len = 1 << inode->i_blkbits;
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100675 p_offset = min_t(unsigned long, end_offset & (PAGE_CACHE_SIZE - 1),
676 PAGE_CACHE_SIZE);
677 p_offset = p_offset ? roundup(p_offset, len) : PAGE_CACHE_SIZE;
678 page_dirty = p_offset / len;
Nathan Scott24e17b52005-05-05 13:33:20 -0700679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 bh = head = page_buffers(page);
681 do {
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100682 if (offset >= end_offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 break;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100684 if (!buffer_uptodate(bh))
685 uptodate = 0;
686 if (!(PageUptodate(page) || buffer_uptodate(bh))) {
687 done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 continue;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100689 }
690
Christoph Hellwig2fa24f92010-12-10 08:42:23 +0000691 if (buffer_unwritten(bh) || buffer_delay(bh) ||
692 buffer_mapped(bh)) {
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100693 if (buffer_unwritten(bh))
Christoph Hellwig34a52c62010-04-28 12:28:57 +0000694 type = IO_UNWRITTEN;
Christoph Hellwig2fa24f92010-12-10 08:42:23 +0000695 else if (buffer_delay(bh))
Christoph Hellwiga206c812010-12-10 08:42:20 +0000696 type = IO_DELALLOC;
Christoph Hellwig2fa24f92010-12-10 08:42:23 +0000697 else
698 type = IO_OVERWRITE;
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100699
Christoph Hellwig558e6892010-04-28 12:28:58 +0000700 if (!xfs_imap_valid(inode, imap, offset)) {
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100701 done = 1;
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100702 continue;
703 }
704
Christoph Hellwigecff71e2010-12-10 08:42:25 +0000705 lock_buffer(bh);
706 if (type != IO_OVERWRITE)
Christoph Hellwig2fa24f92010-12-10 08:42:23 +0000707 xfs_map_at_offset(inode, bh, imap, offset);
Christoph Hellwig89f3b362010-06-24 09:45:48 +1000708 xfs_add_to_ioend(inode, bh, offset, type,
709 ioendp, done);
710
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100711 page_dirty--;
712 count++;
713 } else {
Christoph Hellwig2fa24f92010-12-10 08:42:23 +0000714 done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 }
Christoph Hellwig7336cea2006-01-11 20:49:16 +1100716 } while (offset += len, (bh = bh->b_this_page) != head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100718 if (uptodate && bh == head)
719 SetPageUptodate(page);
720
Christoph Hellwig89f3b362010-06-24 09:45:48 +1000721 if (count) {
Dave Chinnerefceab12010-08-24 11:44:56 +1000722 if (--wbc->nr_to_write <= 0 &&
723 wbc->sync_mode == WB_SYNC_NONE)
Christoph Hellwig89f3b362010-06-24 09:45:48 +1000724 done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 }
Christoph Hellwig89f3b362010-06-24 09:45:48 +1000726 xfs_start_page_writeback(page, !page_dirty, count);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100727
728 return done;
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100729 fail_unlock_page:
730 unlock_page(page);
731 fail:
732 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733}
734
735/*
736 * Convert & write out a cluster of pages in the same extent as defined
737 * by mp and following the start page.
738 */
739STATIC void
740xfs_cluster_write(
741 struct inode *inode,
742 pgoff_t tindex,
Christoph Hellwig207d0412010-04-28 12:28:56 +0000743 struct xfs_bmbt_irec *imap,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100744 xfs_ioend_t **ioendp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 struct writeback_control *wbc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 pgoff_t tlast)
747{
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100748 struct pagevec pvec;
749 int done = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100751 pagevec_init(&pvec, 0);
752 while (!done && tindex <= tlast) {
753 unsigned len = min_t(pgoff_t, PAGEVEC_SIZE, tlast - tindex + 1);
754
755 if (!pagevec_lookup(&pvec, inode->i_mapping, tindex, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 break;
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100757
758 for (i = 0; i < pagevec_count(&pvec); i++) {
759 done = xfs_convert_page(inode, pvec.pages[i], tindex++,
Christoph Hellwig2fa24f92010-12-10 08:42:23 +0000760 imap, ioendp, wbc);
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100761 if (done)
762 break;
763 }
764
765 pagevec_release(&pvec);
766 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 }
768}
769
Dave Chinner3ed3a432010-03-05 02:00:42 +0000770STATIC void
771xfs_vm_invalidatepage(
772 struct page *page,
773 unsigned long offset)
774{
775 trace_xfs_invalidatepage(page->mapping->host, page, offset);
776 block_invalidatepage(page, offset);
777}
778
779/*
780 * If the page has delalloc buffers on it, we need to punch them out before we
781 * invalidate the page. If we don't, we leave a stale delalloc mapping on the
782 * inode that can trip a BUG() in xfs_get_blocks() later on if a direct IO read
783 * is done on that same region - the delalloc extent is returned when none is
784 * supposed to be there.
785 *
786 * We prevent this by truncating away the delalloc regions on the page before
787 * invalidating it. Because they are delalloc, we can do this without needing a
788 * transaction. Indeed - if we get ENOSPC errors, we have to be able to do this
789 * truncation without a transaction as there is no space left for block
790 * reservation (typically why we see a ENOSPC in writeback).
791 *
792 * This is not a performance critical path, so for now just do the punching a
793 * buffer head at a time.
794 */
795STATIC void
796xfs_aops_discard_page(
797 struct page *page)
798{
799 struct inode *inode = page->mapping->host;
800 struct xfs_inode *ip = XFS_I(inode);
801 struct buffer_head *bh, *head;
802 loff_t offset = page_offset(page);
Dave Chinner3ed3a432010-03-05 02:00:42 +0000803
Christoph Hellwiga206c812010-12-10 08:42:20 +0000804 if (!xfs_is_delayed_page(page, IO_DELALLOC))
Dave Chinner3ed3a432010-03-05 02:00:42 +0000805 goto out_invalidate;
806
Dave Chinnere8c37532010-03-15 02:36:35 +0000807 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
808 goto out_invalidate;
809
Dave Chinner4f107002011-03-07 10:00:35 +1100810 xfs_alert(ip->i_mount,
Dave Chinner3ed3a432010-03-05 02:00:42 +0000811 "page discard on page %p, inode 0x%llx, offset %llu.",
812 page, ip->i_ino, offset);
813
814 xfs_ilock(ip, XFS_ILOCK_EXCL);
815 bh = head = page_buffers(page);
816 do {
Dave Chinner3ed3a432010-03-05 02:00:42 +0000817 int error;
Dave Chinnerc726de42010-11-30 15:14:39 +1100818 xfs_fileoff_t start_fsb;
Dave Chinner3ed3a432010-03-05 02:00:42 +0000819
820 if (!buffer_delay(bh))
821 goto next_buffer;
822
Dave Chinnerc726de42010-11-30 15:14:39 +1100823 start_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
824 error = xfs_bmap_punch_delalloc_range(ip, start_fsb, 1);
Dave Chinner3ed3a432010-03-05 02:00:42 +0000825 if (error) {
826 /* something screwed, just bail */
Dave Chinnere8c37532010-03-15 02:36:35 +0000827 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
Dave Chinner4f107002011-03-07 10:00:35 +1100828 xfs_alert(ip->i_mount,
Dave Chinner3ed3a432010-03-05 02:00:42 +0000829 "page discard unable to remove delalloc mapping.");
Dave Chinnere8c37532010-03-15 02:36:35 +0000830 }
Dave Chinner3ed3a432010-03-05 02:00:42 +0000831 break;
832 }
833next_buffer:
Dave Chinnerc726de42010-11-30 15:14:39 +1100834 offset += 1 << inode->i_blkbits;
Dave Chinner3ed3a432010-03-05 02:00:42 +0000835
836 } while ((bh = bh->b_this_page) != head);
837
838 xfs_iunlock(ip, XFS_ILOCK_EXCL);
839out_invalidate:
840 xfs_vm_invalidatepage(page, 0);
841 return;
842}
843
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844/*
Christoph Hellwig89f3b362010-06-24 09:45:48 +1000845 * Write out a dirty page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 *
Christoph Hellwig89f3b362010-06-24 09:45:48 +1000847 * For delalloc space on the page we need to allocate space and flush it.
848 * For unwritten space on the page we need to start the conversion to
849 * regular allocated space.
Christoph Hellwig89f3b362010-06-24 09:45:48 +1000850 * For any other dirty buffer heads on the page we should flush them.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852STATIC int
Christoph Hellwig89f3b362010-06-24 09:45:48 +1000853xfs_vm_writepage(
854 struct page *page,
855 struct writeback_control *wbc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856{
Christoph Hellwig89f3b362010-06-24 09:45:48 +1000857 struct inode *inode = page->mapping->host;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100858 struct buffer_head *bh, *head;
Christoph Hellwig207d0412010-04-28 12:28:56 +0000859 struct xfs_bmbt_irec imap;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100860 xfs_ioend_t *ioend = NULL, *iohead = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 loff_t offset;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100862 unsigned int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 __uint64_t end_offset;
Christoph Hellwigbd1556a2010-04-28 12:29:00 +0000864 pgoff_t end_index, last_index;
Christoph Hellwiged1e7b72010-12-10 08:42:22 +0000865 ssize_t len;
Christoph Hellwiga206c812010-12-10 08:42:20 +0000866 int err, imap_valid = 0, uptodate = 1;
Christoph Hellwig89f3b362010-06-24 09:45:48 +1000867 int count = 0;
Christoph Hellwiga206c812010-12-10 08:42:20 +0000868 int nonblocking = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Christoph Hellwig89f3b362010-06-24 09:45:48 +1000870 trace_xfs_writepage(inode, page, 0);
871
Christoph Hellwig20cb52e2010-06-24 09:46:01 +1000872 ASSERT(page_has_buffers(page));
873
Christoph Hellwig89f3b362010-06-24 09:45:48 +1000874 /*
875 * Refuse to write the page out if we are called from reclaim context.
876 *
Christoph Hellwigd4f7a5c2010-06-28 10:34:44 -0400877 * This avoids stack overflows when called from deeply used stacks in
878 * random callers for direct reclaim or memcg reclaim. We explicitly
879 * allow reclaim from kswapd as the stack usage there is relatively low.
Christoph Hellwig89f3b362010-06-24 09:45:48 +1000880 *
Mel Gorman94054fa2011-10-31 17:07:45 -0700881 * This should never happen except in the case of a VM regression so
882 * warn about it.
Christoph Hellwig89f3b362010-06-24 09:45:48 +1000883 */
Mel Gorman94054fa2011-10-31 17:07:45 -0700884 if (WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD)) ==
885 PF_MEMALLOC))
Christoph Hellwigb5420f22010-08-24 11:47:51 +1000886 goto redirty;
Christoph Hellwig89f3b362010-06-24 09:45:48 +1000887
888 /*
Christoph Hellwig680a6472011-07-08 14:34:05 +0200889 * Given that we do not allow direct reclaim to call us, we should
890 * never be called while in a filesystem transaction.
Christoph Hellwig89f3b362010-06-24 09:45:48 +1000891 */
Christoph Hellwig680a6472011-07-08 14:34:05 +0200892 if (WARN_ON(current->flags & PF_FSTRANS))
Christoph Hellwigb5420f22010-08-24 11:47:51 +1000893 goto redirty;
Christoph Hellwig89f3b362010-06-24 09:45:48 +1000894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 /* Is this page beyond the end of the file? */
896 offset = i_size_read(inode);
897 end_index = offset >> PAGE_CACHE_SHIFT;
898 last_index = (offset - 1) >> PAGE_CACHE_SHIFT;
899 if (page->index >= end_index) {
900 if ((page->index >= end_index + 1) ||
901 !(i_size_read(inode) & (PAGE_CACHE_SIZE - 1))) {
Christoph Hellwig89f3b362010-06-24 09:45:48 +1000902 unlock_page(page);
Nathan Scott19d5bcf2005-11-02 15:14:09 +1100903 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 }
905 }
906
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100907 end_offset = min_t(unsigned long long,
Christoph Hellwig20cb52e2010-06-24 09:46:01 +1000908 (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT,
909 offset);
Nathan Scott24e17b52005-05-05 13:33:20 -0700910 len = 1 << inode->i_blkbits;
Nathan Scott24e17b52005-05-05 13:33:20 -0700911
Nathan Scott24e17b52005-05-05 13:33:20 -0700912 bh = head = page_buffers(page);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100913 offset = page_offset(page);
Christoph Hellwiga206c812010-12-10 08:42:20 +0000914 type = IO_OVERWRITE;
915
Christoph Hellwigdbcdde32011-07-08 14:34:14 +0200916 if (wbc->sync_mode == WB_SYNC_NONE)
Christoph Hellwiga206c812010-12-10 08:42:20 +0000917 nonblocking = 1;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100918
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 do {
Christoph Hellwig6ac72482010-12-10 08:42:18 +0000920 int new_ioend = 0;
921
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 if (offset >= end_offset)
923 break;
924 if (!buffer_uptodate(bh))
925 uptodate = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Eric Sandeen3d9b02e2010-06-24 09:45:30 +1000927 /*
Christoph Hellwigece413f2010-11-10 21:39:11 +0000928 * set_page_dirty dirties all buffers in a page, independent
929 * of their state. The dirty state however is entirely
930 * meaningless for holes (!mapped && uptodate), so skip
931 * buffers covering holes here.
Eric Sandeen3d9b02e2010-06-24 09:45:30 +1000932 */
933 if (!buffer_mapped(bh) && buffer_uptodate(bh)) {
Eric Sandeen3d9b02e2010-06-24 09:45:30 +1000934 imap_valid = 0;
935 continue;
936 }
937
Christoph Hellwigaeea1b12010-12-10 08:42:24 +0000938 if (buffer_unwritten(bh)) {
939 if (type != IO_UNWRITTEN) {
940 type = IO_UNWRITTEN;
941 imap_valid = 0;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100942 }
Christoph Hellwigaeea1b12010-12-10 08:42:24 +0000943 } else if (buffer_delay(bh)) {
944 if (type != IO_DELALLOC) {
945 type = IO_DELALLOC;
946 imap_valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 }
Christoph Hellwig89f3b362010-06-24 09:45:48 +1000948 } else if (buffer_uptodate(bh)) {
Christoph Hellwiga206c812010-12-10 08:42:20 +0000949 if (type != IO_OVERWRITE) {
950 type = IO_OVERWRITE;
Christoph Hellwig85da94c2010-12-10 08:42:16 +0000951 imap_valid = 0;
952 }
Christoph Hellwigaeea1b12010-12-10 08:42:24 +0000953 } else {
954 if (PageUptodate(page)) {
955 ASSERT(buffer_mapped(bh));
956 imap_valid = 0;
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100957 }
Christoph Hellwigaeea1b12010-12-10 08:42:24 +0000958 continue;
959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Christoph Hellwigaeea1b12010-12-10 08:42:24 +0000961 if (imap_valid)
962 imap_valid = xfs_imap_valid(inode, &imap, offset);
963 if (!imap_valid) {
964 /*
965 * If we didn't have a valid mapping then we need to
966 * put the new mapping into a separate ioend structure.
967 * This ensures non-contiguous extents always have
968 * separate ioends, which is particularly important
969 * for unwritten extent conversion at I/O completion
970 * time.
971 */
972 new_ioend = 1;
973 err = xfs_map_blocks(inode, offset, &imap, type,
974 nonblocking);
975 if (err)
976 goto error;
977 imap_valid = xfs_imap_valid(inode, &imap, offset);
978 }
979 if (imap_valid) {
Christoph Hellwigecff71e2010-12-10 08:42:25 +0000980 lock_buffer(bh);
981 if (type != IO_OVERWRITE)
Christoph Hellwigaeea1b12010-12-10 08:42:24 +0000982 xfs_map_at_offset(inode, bh, &imap, offset);
983 xfs_add_to_ioend(inode, bh, offset, type, &ioend,
984 new_ioend);
985 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 }
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100987
988 if (!iohead)
989 iohead = ioend;
990
991 } while (offset += len, ((bh = bh->b_this_page) != head));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
993 if (uptodate && bh == head)
994 SetPageUptodate(page);
995
Christoph Hellwig89f3b362010-06-24 09:45:48 +1000996 xfs_start_page_writeback(page, 1, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
Christoph Hellwig558e6892010-04-28 12:28:58 +0000998 if (ioend && imap_valid) {
Christoph Hellwigbd1556a2010-04-28 12:29:00 +0000999 xfs_off_t end_index;
Christoph Hellwig8699bb02010-04-28 12:28:54 +00001000
Christoph Hellwigbd1556a2010-04-28 12:29:00 +00001001 end_index = imap.br_startoff + imap.br_blockcount;
1002
1003 /* to bytes */
1004 end_index <<= inode->i_blkbits;
1005
1006 /* to pages */
1007 end_index = (end_index - 1) >> PAGE_CACHE_SHIFT;
1008
1009 /* check against file size */
1010 if (end_index > last_index)
1011 end_index = last_index;
1012
Christoph Hellwig207d0412010-04-28 12:28:56 +00001013 xfs_cluster_write(inode, page->index + 1, &imap, &ioend,
Christoph Hellwig2fa24f92010-12-10 08:42:23 +00001014 wbc, end_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 }
1016
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001017 if (iohead)
Christoph Hellwig06342cf2009-10-30 09:09:15 +00001018 xfs_submit_ioend(wbc, iohead);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001019
Christoph Hellwig89f3b362010-06-24 09:45:48 +10001020 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
1022error:
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001023 if (iohead)
1024 xfs_cancel_ioend(iohead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Christoph Hellwigb5420f22010-08-24 11:47:51 +10001026 if (err == -EAGAIN)
1027 goto redirty;
1028
Christoph Hellwig20cb52e2010-06-24 09:46:01 +10001029 xfs_aops_discard_page(page);
Christoph Hellwig89f3b362010-06-24 09:45:48 +10001030 ClearPageUptodate(page);
1031 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 return err;
Nathan Scottf51623b2006-03-14 13:26:27 +11001033
Christoph Hellwigb5420f22010-08-24 11:47:51 +10001034redirty:
Nathan Scottf51623b2006-03-14 13:26:27 +11001035 redirty_page_for_writepage(wbc, page);
1036 unlock_page(page);
1037 return 0;
Nathan Scottf51623b2006-03-14 13:26:27 +11001038}
1039
Nathan Scott7d4fb402006-06-09 15:27:16 +10001040STATIC int
1041xfs_vm_writepages(
1042 struct address_space *mapping,
1043 struct writeback_control *wbc)
1044{
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +10001045 xfs_iflags_clear(XFS_I(mapping->host), XFS_ITRUNCATED);
Nathan Scott7d4fb402006-06-09 15:27:16 +10001046 return generic_writepages(mapping, wbc);
1047}
1048
Nathan Scottf51623b2006-03-14 13:26:27 +11001049/*
1050 * Called to move a page into cleanable state - and from there
Christoph Hellwig89f3b362010-06-24 09:45:48 +10001051 * to be released. The page should already be clean. We always
Nathan Scottf51623b2006-03-14 13:26:27 +11001052 * have buffer heads in this call.
1053 *
Christoph Hellwig89f3b362010-06-24 09:45:48 +10001054 * Returns 1 if the page is ok to release, 0 otherwise.
Nathan Scottf51623b2006-03-14 13:26:27 +11001055 */
1056STATIC int
Nathan Scott238f4c52006-03-17 17:26:25 +11001057xfs_vm_releasepage(
Nathan Scottf51623b2006-03-14 13:26:27 +11001058 struct page *page,
1059 gfp_t gfp_mask)
1060{
Christoph Hellwig20cb52e2010-06-24 09:46:01 +10001061 int delalloc, unwritten;
Nathan Scottf51623b2006-03-14 13:26:27 +11001062
Christoph Hellwig89f3b362010-06-24 09:45:48 +10001063 trace_xfs_releasepage(page->mapping->host, page, 0);
Nathan Scott238f4c52006-03-17 17:26:25 +11001064
Christoph Hellwig20cb52e2010-06-24 09:46:01 +10001065 xfs_count_page_state(page, &delalloc, &unwritten);
Nathan Scottf51623b2006-03-14 13:26:27 +11001066
Christoph Hellwig89f3b362010-06-24 09:45:48 +10001067 if (WARN_ON(delalloc))
1068 return 0;
1069 if (WARN_ON(unwritten))
Nathan Scottf51623b2006-03-14 13:26:27 +11001070 return 0;
1071
Nathan Scottf51623b2006-03-14 13:26:27 +11001072 return try_to_free_buffers(page);
1073}
1074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075STATIC int
Nathan Scottc2536662006-03-29 10:44:40 +10001076__xfs_get_blocks(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 struct inode *inode,
1078 sector_t iblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 struct buffer_head *bh_result,
1080 int create,
Christoph Hellwigf2bde9b2010-06-24 11:44:35 +10001081 int direct)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082{
Christoph Hellwiga206c812010-12-10 08:42:20 +00001083 struct xfs_inode *ip = XFS_I(inode);
1084 struct xfs_mount *mp = ip->i_mount;
1085 xfs_fileoff_t offset_fsb, end_fsb;
1086 int error = 0;
1087 int lockmode = 0;
Christoph Hellwig207d0412010-04-28 12:28:56 +00001088 struct xfs_bmbt_irec imap;
Christoph Hellwiga206c812010-12-10 08:42:20 +00001089 int nimaps = 1;
Nathan Scottfdc7ed72005-11-02 15:13:13 +11001090 xfs_off_t offset;
1091 ssize_t size;
Christoph Hellwig207d0412010-04-28 12:28:56 +00001092 int new = 0;
Christoph Hellwiga206c812010-12-10 08:42:20 +00001093
1094 if (XFS_FORCED_SHUTDOWN(mp))
1095 return -XFS_ERROR(EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
Nathan Scottfdc7ed72005-11-02 15:13:13 +11001097 offset = (xfs_off_t)iblock << inode->i_blkbits;
Nathan Scottc2536662006-03-29 10:44:40 +10001098 ASSERT(bh_result->b_size >= (1 << inode->i_blkbits));
1099 size = bh_result->b_size;
Lachlan McIlroy364f3582008-09-17 16:50:14 +10001100
1101 if (!create && direct && offset >= i_size_read(inode))
1102 return 0;
1103
Christoph Hellwiga206c812010-12-10 08:42:20 +00001104 if (create) {
1105 lockmode = XFS_ILOCK_EXCL;
1106 xfs_ilock(ip, lockmode);
1107 } else {
1108 lockmode = xfs_ilock_map_shared(ip);
1109 }
Christoph Hellwigf2bde9b2010-06-24 11:44:35 +10001110
Christoph Hellwiga206c812010-12-10 08:42:20 +00001111 ASSERT(offset <= mp->m_maxioffset);
1112 if (offset + size > mp->m_maxioffset)
1113 size = mp->m_maxioffset - offset;
1114 end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + size);
1115 offset_fsb = XFS_B_TO_FSBT(mp, offset);
1116
Dave Chinner5c8ed202011-09-18 20:40:45 +00001117 error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb,
1118 &imap, &nimaps, XFS_BMAPI_ENTIRE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 if (error)
Christoph Hellwiga206c812010-12-10 08:42:20 +00001120 goto out_unlock;
1121
1122 if (create &&
1123 (!nimaps ||
1124 (imap.br_startblock == HOLESTARTBLOCK ||
1125 imap.br_startblock == DELAYSTARTBLOCK))) {
1126 if (direct) {
1127 error = xfs_iomap_write_direct(ip, offset, size,
1128 &imap, nimaps);
1129 } else {
1130 error = xfs_iomap_write_delay(ip, offset, size, &imap);
1131 }
1132 if (error)
1133 goto out_unlock;
1134
1135 trace_xfs_get_blocks_alloc(ip, offset, size, 0, &imap);
1136 } else if (nimaps) {
1137 trace_xfs_get_blocks_found(ip, offset, size, 0, &imap);
1138 } else {
1139 trace_xfs_get_blocks_notfound(ip, offset, size);
1140 goto out_unlock;
1141 }
1142 xfs_iunlock(ip, lockmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
Christoph Hellwig207d0412010-04-28 12:28:56 +00001144 if (imap.br_startblock != HOLESTARTBLOCK &&
1145 imap.br_startblock != DELAYSTARTBLOCK) {
Nathan Scott87cbc492006-03-14 13:26:43 +11001146 /*
1147 * For unwritten extents do not report a disk address on
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 * the read case (treat as if we're reading into a hole).
1149 */
Christoph Hellwig207d0412010-04-28 12:28:56 +00001150 if (create || !ISUNWRITTEN(&imap))
1151 xfs_map_buffer(inode, bh_result, &imap, offset);
1152 if (create && ISUNWRITTEN(&imap)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 if (direct)
1154 bh_result->b_private = inode;
1155 set_buffer_unwritten(bh_result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 }
1157 }
1158
Nathan Scottc2536662006-03-29 10:44:40 +10001159 /*
1160 * If this is a realtime file, data may be on a different device.
1161 * to that pointed to from the buffer_head b_bdev currently.
1162 */
Christoph Hellwig046f1682010-04-28 12:28:52 +00001163 bh_result->b_bdev = xfs_find_bdev_for_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
Nathan Scottc2536662006-03-29 10:44:40 +10001165 /*
David Chinner549054a2007-02-10 18:36:35 +11001166 * If we previously allocated a block out beyond eof and we are now
1167 * coming back to use it then we will need to flag it as new even if it
1168 * has a disk address.
1169 *
1170 * With sub-block writes into unwritten extents we also need to mark
1171 * the buffer as new so that the unwritten parts of the buffer gets
1172 * correctly zeroed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 */
1174 if (create &&
1175 ((!buffer_mapped(bh_result) && !buffer_uptodate(bh_result)) ||
David Chinner549054a2007-02-10 18:36:35 +11001176 (offset >= i_size_read(inode)) ||
Christoph Hellwig207d0412010-04-28 12:28:56 +00001177 (new || ISUNWRITTEN(&imap))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 set_buffer_new(bh_result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
Christoph Hellwig207d0412010-04-28 12:28:56 +00001180 if (imap.br_startblock == DELAYSTARTBLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 BUG_ON(direct);
1182 if (create) {
1183 set_buffer_uptodate(bh_result);
1184 set_buffer_mapped(bh_result);
1185 set_buffer_delay(bh_result);
1186 }
1187 }
1188
Christoph Hellwig2b8f12b2010-04-28 12:28:59 +00001189 /*
1190 * If this is O_DIRECT or the mpage code calling tell them how large
1191 * the mapping is, so that we can avoid repeated get_blocks calls.
1192 */
Nathan Scottc2536662006-03-29 10:44:40 +10001193 if (direct || size > (1 << inode->i_blkbits)) {
Christoph Hellwig2b8f12b2010-04-28 12:28:59 +00001194 xfs_off_t mapping_size;
Christoph Hellwig9563b3d2010-04-28 12:28:53 +00001195
Christoph Hellwig2b8f12b2010-04-28 12:28:59 +00001196 mapping_size = imap.br_startoff + imap.br_blockcount - iblock;
1197 mapping_size <<= inode->i_blkbits;
1198
1199 ASSERT(mapping_size > 0);
1200 if (mapping_size > size)
1201 mapping_size = size;
1202 if (mapping_size > LONG_MAX)
1203 mapping_size = LONG_MAX;
1204
1205 bh_result->b_size = mapping_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 }
1207
1208 return 0;
Christoph Hellwiga206c812010-12-10 08:42:20 +00001209
1210out_unlock:
1211 xfs_iunlock(ip, lockmode);
1212 return -error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213}
1214
1215int
Nathan Scottc2536662006-03-29 10:44:40 +10001216xfs_get_blocks(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 struct inode *inode,
1218 sector_t iblock,
1219 struct buffer_head *bh_result,
1220 int create)
1221{
Christoph Hellwigf2bde9b2010-06-24 11:44:35 +10001222 return __xfs_get_blocks(inode, iblock, bh_result, create, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223}
1224
1225STATIC int
Nathan Scotte4c573b2006-03-14 13:54:26 +11001226xfs_get_blocks_direct(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 struct inode *inode,
1228 sector_t iblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 struct buffer_head *bh_result,
1230 int create)
1231{
Christoph Hellwigf2bde9b2010-06-24 11:44:35 +10001232 return __xfs_get_blocks(inode, iblock, bh_result, create, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233}
1234
Christoph Hellwig209fb872010-07-18 21:17:11 +00001235/*
1236 * Complete a direct I/O write request.
1237 *
1238 * If the private argument is non-NULL __xfs_get_blocks signals us that we
1239 * need to issue a transaction to convert the range from unwritten to written
1240 * extents. In case this is regular synchronous I/O we just call xfs_end_io
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001241 * to do this and we are done. But in case this was a successful AIO
Christoph Hellwig209fb872010-07-18 21:17:11 +00001242 * request this handler is called from interrupt context, from which we
1243 * can't start transactions. In that case offload the I/O completion to
1244 * the workqueues we also use for buffered I/O completion.
1245 */
Christoph Hellwigf0973862005-09-05 08:22:52 +10001246STATIC void
Christoph Hellwig209fb872010-07-18 21:17:11 +00001247xfs_end_io_direct_write(
1248 struct kiocb *iocb,
1249 loff_t offset,
1250 ssize_t size,
1251 void *private,
1252 int ret,
1253 bool is_async)
Christoph Hellwigf0973862005-09-05 08:22:52 +10001254{
Christoph Hellwig209fb872010-07-18 21:17:11 +00001255 struct xfs_ioend *ioend = iocb->private;
Christoph Hellwigf0973862005-09-05 08:22:52 +10001256
1257 /*
Christoph Hellwig2813d682011-12-18 20:00:12 +00001258 * While the generic direct I/O code updates the inode size, it does
1259 * so only after the end_io handler is called, which means our
1260 * end_io handler thinks the on-disk size is outside the in-core
1261 * size. To prevent this just update it a little bit earlier here.
1262 */
1263 if (offset + size > i_size_read(ioend->io_inode))
1264 i_size_write(ioend->io_inode, offset + size);
1265
1266 /*
Nathan Scottc41564b2006-03-29 08:55:14 +10001267 * blockdev_direct_IO can return an error even after the I/O
Christoph Hellwigf0973862005-09-05 08:22:52 +10001268 * completion handler was called. Thus we need to protect
1269 * against double-freeing.
1270 */
1271 iocb->private = NULL;
Christoph Hellwig40e2e972010-07-18 21:17:09 +00001272
Christoph Hellwig209fb872010-07-18 21:17:11 +00001273 ioend->io_offset = offset;
1274 ioend->io_size = size;
Christoph Hellwigc859cdd2011-08-23 08:28:10 +00001275 ioend->io_iocb = iocb;
1276 ioend->io_result = ret;
Christoph Hellwig209fb872010-07-18 21:17:11 +00001277 if (private && size > 0)
1278 ioend->io_type = IO_UNWRITTEN;
1279
1280 if (is_async) {
Christoph Hellwigc859cdd2011-08-23 08:28:10 +00001281 ioend->io_isasync = 1;
Christoph Hellwig209fb872010-07-18 21:17:11 +00001282 xfs_finish_ioend(ioend);
1283 } else {
1284 xfs_finish_ioend_sync(ioend);
1285 }
Christoph Hellwigf0973862005-09-05 08:22:52 +10001286}
1287
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288STATIC ssize_t
Nathan Scotte4c573b2006-03-14 13:54:26 +11001289xfs_vm_direct_IO(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 int rw,
1291 struct kiocb *iocb,
1292 const struct iovec *iov,
1293 loff_t offset,
1294 unsigned long nr_segs)
1295{
Christoph Hellwig209fb872010-07-18 21:17:11 +00001296 struct inode *inode = iocb->ki_filp->f_mapping->host;
1297 struct block_device *bdev = xfs_find_bdev_for_inode(inode);
1298 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Christoph Hellwig209fb872010-07-18 21:17:11 +00001300 if (rw & WRITE) {
Christoph Hellwiga206c812010-12-10 08:42:20 +00001301 iocb->private = xfs_alloc_ioend(inode, IO_DIRECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
Christoph Hellwigeafdc7d2010-06-04 11:29:53 +02001303 ret = __blockdev_direct_IO(rw, iocb, inode, bdev, iov,
1304 offset, nr_segs,
1305 xfs_get_blocks_direct,
1306 xfs_end_io_direct_write, NULL, 0);
Christoph Hellwig209fb872010-07-18 21:17:11 +00001307 if (ret != -EIOCBQUEUED && iocb->private)
1308 xfs_destroy_ioend(iocb->private);
1309 } else {
Christoph Hellwigeafdc7d2010-06-04 11:29:53 +02001310 ret = __blockdev_direct_IO(rw, iocb, inode, bdev, iov,
1311 offset, nr_segs,
1312 xfs_get_blocks_direct,
1313 NULL, NULL, 0);
Christoph Hellwig209fb872010-07-18 21:17:11 +00001314 }
Christoph Hellwig5fe878a2009-12-15 16:47:50 -08001315
Christoph Hellwigf0973862005-09-05 08:22:52 +10001316 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317}
1318
Christoph Hellwigfa9b2272010-06-14 05:17:31 -04001319STATIC void
1320xfs_vm_write_failed(
1321 struct address_space *mapping,
1322 loff_t to)
1323{
1324 struct inode *inode = mapping->host;
1325
1326 if (to > inode->i_size) {
Dave Chinnerc726de42010-11-30 15:14:39 +11001327 /*
Christoph Hellwig2813d682011-12-18 20:00:12 +00001328 * Punch out the delalloc blocks we have already allocated.
1329 *
1330 * Don't bother with xfs_setattr given that nothing can have
1331 * made it to disk yet as the page is still locked at this
1332 * point.
Dave Chinnerc726de42010-11-30 15:14:39 +11001333 */
1334 struct xfs_inode *ip = XFS_I(inode);
1335 xfs_fileoff_t start_fsb;
1336 xfs_fileoff_t end_fsb;
1337 int error;
1338
1339 truncate_pagecache(inode, to, inode->i_size);
1340
1341 /*
1342 * Check if there are any blocks that are outside of i_size
1343 * that need to be trimmed back.
1344 */
1345 start_fsb = XFS_B_TO_FSB(ip->i_mount, inode->i_size) + 1;
1346 end_fsb = XFS_B_TO_FSB(ip->i_mount, to);
1347 if (end_fsb <= start_fsb)
1348 return;
1349
1350 xfs_ilock(ip, XFS_ILOCK_EXCL);
1351 error = xfs_bmap_punch_delalloc_range(ip, start_fsb,
1352 end_fsb - start_fsb);
1353 if (error) {
1354 /* something screwed, just bail */
1355 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
Dave Chinner4f107002011-03-07 10:00:35 +11001356 xfs_alert(ip->i_mount,
Dave Chinnerc726de42010-11-30 15:14:39 +11001357 "xfs_vm_write_failed: unable to clean up ino %lld",
1358 ip->i_ino);
1359 }
1360 }
1361 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Christoph Hellwigfa9b2272010-06-14 05:17:31 -04001362 }
1363}
1364
Nathan Scottf51623b2006-03-14 13:26:27 +11001365STATIC int
Nick Piggind79689c2007-10-16 01:25:06 -07001366xfs_vm_write_begin(
Nathan Scottf51623b2006-03-14 13:26:27 +11001367 struct file *file,
Nick Piggind79689c2007-10-16 01:25:06 -07001368 struct address_space *mapping,
1369 loff_t pos,
1370 unsigned len,
1371 unsigned flags,
1372 struct page **pagep,
1373 void **fsdata)
Nathan Scottf51623b2006-03-14 13:26:27 +11001374{
Christoph Hellwig155130a2010-06-04 11:29:58 +02001375 int ret;
1376
1377 ret = block_write_begin(mapping, pos, len, flags | AOP_FLAG_NOFS,
1378 pagep, xfs_get_blocks);
Christoph Hellwigfa9b2272010-06-14 05:17:31 -04001379 if (unlikely(ret))
1380 xfs_vm_write_failed(mapping, pos + len);
1381 return ret;
1382}
Christoph Hellwig155130a2010-06-04 11:29:58 +02001383
Christoph Hellwigfa9b2272010-06-14 05:17:31 -04001384STATIC int
1385xfs_vm_write_end(
1386 struct file *file,
1387 struct address_space *mapping,
1388 loff_t pos,
1389 unsigned len,
1390 unsigned copied,
1391 struct page *page,
1392 void *fsdata)
1393{
1394 int ret;
1395
1396 ret = generic_write_end(file, mapping, pos, len, copied, page, fsdata);
1397 if (unlikely(ret < len))
1398 xfs_vm_write_failed(mapping, pos + len);
Christoph Hellwig155130a2010-06-04 11:29:58 +02001399 return ret;
Nathan Scottf51623b2006-03-14 13:26:27 +11001400}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
1402STATIC sector_t
Nathan Scotte4c573b2006-03-14 13:54:26 +11001403xfs_vm_bmap(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 struct address_space *mapping,
1405 sector_t block)
1406{
1407 struct inode *inode = (struct inode *)mapping->host;
Christoph Hellwig739bfb22007-08-29 10:58:01 +10001408 struct xfs_inode *ip = XFS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Christoph Hellwigcca28fb2010-06-24 11:57:09 +10001410 trace_xfs_vm_bmap(XFS_I(inode));
Christoph Hellwig126468b2008-03-06 13:44:57 +11001411 xfs_ilock(ip, XFS_IOLOCK_SHARED);
Christoph Hellwig739bfb22007-08-29 10:58:01 +10001412 xfs_flush_pages(ip, (xfs_off_t)0, -1, 0, FI_REMAPF);
Christoph Hellwig126468b2008-03-06 13:44:57 +11001413 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
Nathan Scottc2536662006-03-29 10:44:40 +10001414 return generic_block_bmap(mapping, block, xfs_get_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415}
1416
1417STATIC int
Nathan Scotte4c573b2006-03-14 13:54:26 +11001418xfs_vm_readpage(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 struct file *unused,
1420 struct page *page)
1421{
Nathan Scottc2536662006-03-29 10:44:40 +10001422 return mpage_readpage(page, xfs_get_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423}
1424
1425STATIC int
Nathan Scotte4c573b2006-03-14 13:54:26 +11001426xfs_vm_readpages(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 struct file *unused,
1428 struct address_space *mapping,
1429 struct list_head *pages,
1430 unsigned nr_pages)
1431{
Nathan Scottc2536662006-03-29 10:44:40 +10001432 return mpage_readpages(mapping, pages, nr_pages, xfs_get_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433}
1434
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07001435const struct address_space_operations xfs_address_space_operations = {
Nathan Scotte4c573b2006-03-14 13:54:26 +11001436 .readpage = xfs_vm_readpage,
1437 .readpages = xfs_vm_readpages,
1438 .writepage = xfs_vm_writepage,
Nathan Scott7d4fb402006-06-09 15:27:16 +10001439 .writepages = xfs_vm_writepages,
Nathan Scott238f4c52006-03-17 17:26:25 +11001440 .releasepage = xfs_vm_releasepage,
1441 .invalidatepage = xfs_vm_invalidatepage,
Nick Piggind79689c2007-10-16 01:25:06 -07001442 .write_begin = xfs_vm_write_begin,
Christoph Hellwigfa9b2272010-06-14 05:17:31 -04001443 .write_end = xfs_vm_write_end,
Nathan Scotte4c573b2006-03-14 13:54:26 +11001444 .bmap = xfs_vm_bmap,
1445 .direct_IO = xfs_vm_direct_IO,
Christoph Lametere965f962006-02-01 03:05:41 -08001446 .migratepage = buffer_migrate_page,
Hisashi Hifumibddaafa2009-03-29 09:53:38 +02001447 .is_partially_uptodate = block_is_partially_uptodate,
Andi Kleenaa261f52009-09-16 11:50:16 +02001448 .error_remove_page = generic_error_remove_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449};