blob: 2c61e9622f5754ec7431126a93cf0eab810dc3ac [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"
Dave Chinner70a98832013-10-23 10:36:05 +110019#include "xfs_format.h"
20#include "xfs_shared.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "xfs_sb.h"
Nathan Scotta844f452005-11-02 14:38:42 +110022#include "xfs_ag.h"
Dave Chinner70a98832013-10-23 10:36:05 +110023#include "xfs_log.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"
Christoph Hellwig281627d2012-03-13 08:41:05 +000029#include "xfs_inode_item.h"
Nathan Scotta844f452005-11-02 14:38:42 +110030#include "xfs_alloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_iomap.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000033#include "xfs_trace.h"
Dave Chinner3ed3a432010-03-05 02:00:42 +000034#include "xfs_bmap.h"
Dave Chinner68988112013-08-12 20:49:42 +100035#include "xfs_bmap_util.h"
Kent Overstreeta27bb332013-05-07 16:19:08 -070036#include <linux/aio.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/mpage.h>
Christoph Hellwig10ce4442006-01-11 20:48:14 +110039#include <linux/pagevec.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/writeback.h>
41
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000042void
Nathan Scottf51623b2006-03-14 13:26:27 +110043xfs_count_page_state(
44 struct page *page,
45 int *delalloc,
Nathan Scottf51623b2006-03-14 13:26:27 +110046 int *unwritten)
47{
48 struct buffer_head *bh, *head;
49
Christoph Hellwig20cb52e2010-06-24 09:46:01 +100050 *delalloc = *unwritten = 0;
Nathan Scottf51623b2006-03-14 13:26:27 +110051
52 bh = head = page_buffers(page);
53 do {
Christoph Hellwig20cb52e2010-06-24 09:46:01 +100054 if (buffer_unwritten(bh))
Nathan Scottf51623b2006-03-14 13:26:27 +110055 (*unwritten) = 1;
56 else if (buffer_delay(bh))
57 (*delalloc) = 1;
58 } while ((bh = bh->b_this_page) != head);
59}
60
Christoph Hellwig6214ed42007-09-14 15:23:17 +100061STATIC struct block_device *
62xfs_find_bdev_for_inode(
Christoph Hellwig046f1682010-04-28 12:28:52 +000063 struct inode *inode)
Christoph Hellwig6214ed42007-09-14 15:23:17 +100064{
Christoph Hellwig046f1682010-04-28 12:28:52 +000065 struct xfs_inode *ip = XFS_I(inode);
Christoph Hellwig6214ed42007-09-14 15:23:17 +100066 struct xfs_mount *mp = ip->i_mount;
67
Eric Sandeen71ddabb2007-11-23 16:29:42 +110068 if (XFS_IS_REALTIME_INODE(ip))
Christoph Hellwig6214ed42007-09-14 15:23:17 +100069 return mp->m_rtdev_targp->bt_bdev;
70 else
71 return mp->m_ddev_targp->bt_bdev;
72}
73
Christoph Hellwig0829c362005-09-02 16:58:49 +100074/*
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +110075 * We're now finished for good with this ioend structure.
76 * Update the page state via the associated buffer_heads,
77 * release holds on the inode and bio, and finally free
78 * up memory. Do not use the ioend after this.
79 */
Christoph Hellwig0829c362005-09-02 16:58:49 +100080STATIC void
81xfs_destroy_ioend(
82 xfs_ioend_t *ioend)
83{
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +110084 struct buffer_head *bh, *next;
85
86 for (bh = ioend->io_buffer_head; bh; bh = next) {
87 next = bh->b_private;
Nathan Scott7d04a332006-06-09 14:58:38 +100088 bh->b_end_io(bh, !ioend->io_error);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +110089 }
Christoph Hellwig583fa582008-12-03 12:20:38 +010090
Christoph Hellwig0829c362005-09-02 16:58:49 +100091 mempool_free(ioend, xfs_ioend_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092}
93
94/*
Christoph Hellwigfc0063c2011-08-23 08:28:11 +000095 * Fast and loose check if this write could update the on-disk inode size.
96 */
97static inline bool xfs_ioend_is_append(struct xfs_ioend *ioend)
98{
99 return ioend->io_offset + ioend->io_size >
100 XFS_I(ioend->io_inode)->i_d.di_size;
101}
102
Christoph Hellwig281627d2012-03-13 08:41:05 +0000103STATIC int
104xfs_setfilesize_trans_alloc(
105 struct xfs_ioend *ioend)
106{
107 struct xfs_mount *mp = XFS_I(ioend->io_inode)->i_mount;
108 struct xfs_trans *tp;
109 int error;
110
111 tp = xfs_trans_alloc(mp, XFS_TRANS_FSYNC_TS);
112
Jie Liu3d3c8b52013-08-12 20:49:59 +1000113 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_fsyncts, 0, 0);
Christoph Hellwig281627d2012-03-13 08:41:05 +0000114 if (error) {
115 xfs_trans_cancel(tp, 0);
116 return error;
117 }
118
119 ioend->io_append_trans = tp;
120
121 /*
Dave Chinner437a2552012-11-28 13:01:00 +1100122 * We may pass freeze protection with a transaction. So tell lockdep
Jan Karad9457dc2012-06-12 16:20:39 +0200123 * we released it.
124 */
125 rwsem_release(&ioend->io_inode->i_sb->s_writers.lock_map[SB_FREEZE_FS-1],
126 1, _THIS_IP_);
127 /*
Christoph Hellwig281627d2012-03-13 08:41:05 +0000128 * We hand off the transaction to the completion thread now, so
129 * clear the flag here.
130 */
131 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
132 return 0;
133}
134
Christoph Hellwigfc0063c2011-08-23 08:28:11 +0000135/*
Christoph Hellwig2813d682011-12-18 20:00:12 +0000136 * Update on-disk file size now that data has been written to disk.
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000137 */
Christoph Hellwig281627d2012-03-13 08:41:05 +0000138STATIC int
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000139xfs_setfilesize(
Christoph Hellwigaa6bf012012-02-29 09:53:48 +0000140 struct xfs_ioend *ioend)
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000141{
Christoph Hellwigaa6bf012012-02-29 09:53:48 +0000142 struct xfs_inode *ip = XFS_I(ioend->io_inode);
Christoph Hellwig281627d2012-03-13 08:41:05 +0000143 struct xfs_trans *tp = ioend->io_append_trans;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000144 xfs_fsize_t isize;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000145
Christoph Hellwig281627d2012-03-13 08:41:05 +0000146 /*
Dave Chinner437a2552012-11-28 13:01:00 +1100147 * The transaction may have been allocated in the I/O submission thread,
148 * thus we need to mark ourselves as beeing in a transaction manually.
149 * Similarly for freeze protection.
Christoph Hellwig281627d2012-03-13 08:41:05 +0000150 */
151 current_set_flags_nested(&tp->t_pflags, PF_FSTRANS);
Dave Chinner437a2552012-11-28 13:01:00 +1100152 rwsem_acquire_read(&VFS_I(ip)->i_sb->s_writers.lock_map[SB_FREEZE_FS-1],
153 0, 1, _THIS_IP_);
Christoph Hellwig281627d2012-03-13 08:41:05 +0000154
Christoph Hellwigaa6bf012012-02-29 09:53:48 +0000155 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig6923e682012-02-29 09:53:49 +0000156 isize = xfs_new_eof(ip, ioend->io_offset + ioend->io_size);
Christoph Hellwig281627d2012-03-13 08:41:05 +0000157 if (!isize) {
158 xfs_iunlock(ip, XFS_ILOCK_EXCL);
159 xfs_trans_cancel(tp, 0);
160 return 0;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000161 }
162
Christoph Hellwig281627d2012-03-13 08:41:05 +0000163 trace_xfs_setfilesize(ip, ioend->io_offset, ioend->io_size);
164
165 ip->i_d.di_size = isize;
166 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
167 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
168
169 return xfs_trans_commit(tp, 0);
Christoph Hellwig0829c362005-09-02 16:58:49 +1000170}
171
172/*
Christoph Hellwig209fb872010-07-18 21:17:11 +0000173 * Schedule IO completion handling on the final put of an ioend.
Christoph Hellwigfc0063c2011-08-23 08:28:11 +0000174 *
175 * If there is no work to do we might as well call it a day and free the
176 * ioend right now.
Dave Chinnerc626d172009-04-06 18:42:11 +0200177 */
178STATIC void
179xfs_finish_ioend(
Christoph Hellwig209fb872010-07-18 21:17:11 +0000180 struct xfs_ioend *ioend)
Dave Chinnerc626d172009-04-06 18:42:11 +0200181{
182 if (atomic_dec_and_test(&ioend->io_remaining)) {
Christoph Hellwigaa6bf012012-02-29 09:53:48 +0000183 struct xfs_mount *mp = XFS_I(ioend->io_inode)->i_mount;
184
Alain Renaud0d882a32012-05-22 15:56:21 -0500185 if (ioend->io_type == XFS_IO_UNWRITTEN)
Christoph Hellwigaa6bf012012-02-29 09:53:48 +0000186 queue_work(mp->m_unwritten_workqueue, &ioend->io_work);
Dave Chinner437a2552012-11-28 13:01:00 +1100187 else if (ioend->io_append_trans ||
188 (ioend->io_isdirect && xfs_ioend_is_append(ioend)))
Christoph Hellwigaa6bf012012-02-29 09:53:48 +0000189 queue_work(mp->m_data_workqueue, &ioend->io_work);
Christoph Hellwigfc0063c2011-08-23 08:28:11 +0000190 else
191 xfs_destroy_ioend(ioend);
Dave Chinnerc626d172009-04-06 18:42:11 +0200192 }
193}
194
195/*
Dave Chinner77d7a0c2010-02-17 05:36:29 +0000196 * IO write completion.
197 */
198STATIC void
199xfs_end_io(
200 struct work_struct *work)
201{
202 xfs_ioend_t *ioend = container_of(work, xfs_ioend_t, io_work);
203 struct xfs_inode *ip = XFS_I(ioend->io_inode);
Dave Chinner69418932010-03-04 00:57:09 +0000204 int error = 0;
Dave Chinner77d7a0c2010-02-17 05:36:29 +0000205
Christoph Hellwig04f658e2011-08-24 05:59:25 +0000206 if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
Christoph Hellwig810627d2011-11-08 08:56:15 +0000207 ioend->io_error = -EIO;
Christoph Hellwig04f658e2011-08-24 05:59:25 +0000208 goto done;
209 }
210 if (ioend->io_error)
211 goto done;
212
Dave Chinner77d7a0c2010-02-17 05:36:29 +0000213 /*
214 * For unwritten extents we need to issue transactions to convert a
215 * range to normal written extens after the data I/O has finished.
216 */
Alain Renaud0d882a32012-05-22 15:56:21 -0500217 if (ioend->io_type == XFS_IO_UNWRITTEN) {
Dave Chinner77d7a0c2010-02-17 05:36:29 +0000218 error = xfs_iomap_write_unwritten(ip, ioend->io_offset,
Dave Chinner437a2552012-11-28 13:01:00 +1100219 ioend->io_size);
220 } else if (ioend->io_isdirect && xfs_ioend_is_append(ioend)) {
221 /*
222 * For direct I/O we do not know if we need to allocate blocks
223 * or not so we can't preallocate an append transaction as that
224 * results in nested reservations and log space deadlocks. Hence
225 * allocate the transaction here. While this is sub-optimal and
226 * can block IO completion for some time, we're stuck with doing
227 * it this way until we can pass the ioend to the direct IO
228 * allocation callbacks and avoid nesting that way.
229 */
230 error = xfs_setfilesize_trans_alloc(ioend);
231 if (error)
Christoph Hellwig04f658e2011-08-24 05:59:25 +0000232 goto done;
Dave Chinner437a2552012-11-28 13:01:00 +1100233 error = xfs_setfilesize(ioend);
Christoph Hellwig281627d2012-03-13 08:41:05 +0000234 } else if (ioend->io_append_trans) {
235 error = xfs_setfilesize(ioend);
Christoph Hellwig84803fb2012-02-29 09:53:50 +0000236 } else {
Christoph Hellwig281627d2012-03-13 08:41:05 +0000237 ASSERT(!xfs_ioend_is_append(ioend));
Dave Chinner77d7a0c2010-02-17 05:36:29 +0000238 }
239
Christoph Hellwig04f658e2011-08-24 05:59:25 +0000240done:
Dave Chinner437a2552012-11-28 13:01:00 +1100241 if (error)
242 ioend->io_error = -error;
Christoph Hellwigaa6bf012012-02-29 09:53:48 +0000243 xfs_destroy_ioend(ioend);
Dave Chinner77d7a0c2010-02-17 05:36:29 +0000244}
245
246/*
Christoph Hellwig209fb872010-07-18 21:17:11 +0000247 * Call IO completion handling in caller context on the final put of an ioend.
248 */
249STATIC void
250xfs_finish_ioend_sync(
251 struct xfs_ioend *ioend)
252{
253 if (atomic_dec_and_test(&ioend->io_remaining))
254 xfs_end_io(&ioend->io_work);
255}
256
257/*
Christoph Hellwig0829c362005-09-02 16:58:49 +1000258 * Allocate and initialise an IO completion structure.
259 * We need to track unwritten extent write completion here initially.
260 * We'll need to extend this for updating the ondisk inode size later
261 * (vs. incore size).
262 */
263STATIC xfs_ioend_t *
264xfs_alloc_ioend(
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100265 struct inode *inode,
266 unsigned int type)
Christoph Hellwig0829c362005-09-02 16:58:49 +1000267{
268 xfs_ioend_t *ioend;
269
270 ioend = mempool_alloc(xfs_ioend_pool, GFP_NOFS);
271
272 /*
273 * Set the count to 1 initially, which will prevent an I/O
274 * completion callback from happening before we have started
275 * all the I/O from calling the completion routine too early.
276 */
277 atomic_set(&ioend->io_remaining, 1);
Christoph Hellwig281627d2012-03-13 08:41:05 +0000278 ioend->io_isdirect = 0;
Nathan Scott7d04a332006-06-09 14:58:38 +1000279 ioend->io_error = 0;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100280 ioend->io_list = NULL;
281 ioend->io_type = type;
Christoph Hellwigb677c212007-08-29 11:46:28 +1000282 ioend->io_inode = inode;
Christoph Hellwigc1a073b2005-09-05 08:23:35 +1000283 ioend->io_buffer_head = NULL;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100284 ioend->io_buffer_tail = NULL;
Christoph Hellwig0829c362005-09-02 16:58:49 +1000285 ioend->io_offset = 0;
286 ioend->io_size = 0;
Christoph Hellwig281627d2012-03-13 08:41:05 +0000287 ioend->io_append_trans = NULL;
Christoph Hellwig0829c362005-09-02 16:58:49 +1000288
Christoph Hellwig5ec4fab2009-10-30 09:11:47 +0000289 INIT_WORK(&ioend->io_work, xfs_end_io);
Christoph Hellwig0829c362005-09-02 16:58:49 +1000290 return ioend;
291}
292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293STATIC int
294xfs_map_blocks(
295 struct inode *inode,
296 loff_t offset,
Christoph Hellwig207d0412010-04-28 12:28:56 +0000297 struct xfs_bmbt_irec *imap,
Christoph Hellwiga206c812010-12-10 08:42:20 +0000298 int type,
299 int nonblocking)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
Christoph Hellwiga206c812010-12-10 08:42:20 +0000301 struct xfs_inode *ip = XFS_I(inode);
302 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwiged1e7b72010-12-10 08:42:22 +0000303 ssize_t count = 1 << inode->i_blkbits;
Christoph Hellwiga206c812010-12-10 08:42:20 +0000304 xfs_fileoff_t offset_fsb, end_fsb;
305 int error = 0;
Christoph Hellwiga206c812010-12-10 08:42:20 +0000306 int bmapi_flags = XFS_BMAPI_ENTIRE;
307 int nimaps = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Christoph Hellwiga206c812010-12-10 08:42:20 +0000309 if (XFS_FORCED_SHUTDOWN(mp))
310 return -XFS_ERROR(EIO);
311
Alain Renaud0d882a32012-05-22 15:56:21 -0500312 if (type == XFS_IO_UNWRITTEN)
Christoph Hellwiga206c812010-12-10 08:42:20 +0000313 bmapi_flags |= XFS_BMAPI_IGSTATE;
Christoph Hellwiga206c812010-12-10 08:42:20 +0000314
Christoph Hellwig8ff29572010-12-10 08:42:21 +0000315 if (!xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
316 if (nonblocking)
317 return -XFS_ERROR(EAGAIN);
318 xfs_ilock(ip, XFS_ILOCK_SHARED);
Christoph Hellwiga206c812010-12-10 08:42:20 +0000319 }
320
Christoph Hellwig8ff29572010-12-10 08:42:21 +0000321 ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
322 (ip->i_df.if_flags & XFS_IFEXTENTS));
Dave Chinnerd2c28192012-06-08 15:44:53 +1000323 ASSERT(offset <= mp->m_super->s_maxbytes);
Christoph Hellwig8ff29572010-12-10 08:42:21 +0000324
Dave Chinnerd2c28192012-06-08 15:44:53 +1000325 if (offset + count > mp->m_super->s_maxbytes)
326 count = mp->m_super->s_maxbytes - offset;
Christoph Hellwiga206c812010-12-10 08:42:20 +0000327 end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
328 offset_fsb = XFS_B_TO_FSBT(mp, offset);
Dave Chinner5c8ed202011-09-18 20:40:45 +0000329 error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb,
330 imap, &nimaps, bmapi_flags);
Christoph Hellwig8ff29572010-12-10 08:42:21 +0000331 xfs_iunlock(ip, XFS_ILOCK_SHARED);
332
Christoph Hellwiga206c812010-12-10 08:42:20 +0000333 if (error)
Christoph Hellwig8ff29572010-12-10 08:42:21 +0000334 return -XFS_ERROR(error);
Christoph Hellwiga206c812010-12-10 08:42:20 +0000335
Alain Renaud0d882a32012-05-22 15:56:21 -0500336 if (type == XFS_IO_DELALLOC &&
Christoph Hellwig8ff29572010-12-10 08:42:21 +0000337 (!nimaps || isnullstartblock(imap->br_startblock))) {
Jie Liu0799a3e2013-09-29 18:56:04 +0800338 error = xfs_iomap_write_allocate(ip, offset, imap);
Christoph Hellwiga206c812010-12-10 08:42:20 +0000339 if (!error)
340 trace_xfs_map_blocks_alloc(ip, offset, count, type, imap);
Christoph Hellwig8ff29572010-12-10 08:42:21 +0000341 return -XFS_ERROR(error);
Christoph Hellwiga206c812010-12-10 08:42:20 +0000342 }
343
Christoph Hellwig8ff29572010-12-10 08:42:21 +0000344#ifdef DEBUG
Alain Renaud0d882a32012-05-22 15:56:21 -0500345 if (type == XFS_IO_UNWRITTEN) {
Christoph Hellwig8ff29572010-12-10 08:42:21 +0000346 ASSERT(nimaps);
347 ASSERT(imap->br_startblock != HOLESTARTBLOCK);
348 ASSERT(imap->br_startblock != DELAYSTARTBLOCK);
349 }
350#endif
351 if (nimaps)
352 trace_xfs_map_blocks_found(ip, offset, count, type, imap);
353 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354}
355
Christoph Hellwigb8f82a42009-11-14 16:17:22 +0000356STATIC int
Christoph Hellwig558e6892010-04-28 12:28:58 +0000357xfs_imap_valid(
Christoph Hellwig8699bb02010-04-28 12:28:54 +0000358 struct inode *inode,
Christoph Hellwig207d0412010-04-28 12:28:56 +0000359 struct xfs_bmbt_irec *imap,
Christoph Hellwig558e6892010-04-28 12:28:58 +0000360 xfs_off_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
Christoph Hellwig558e6892010-04-28 12:28:58 +0000362 offset >>= inode->i_blkbits;
Christoph Hellwig8699bb02010-04-28 12:28:54 +0000363
Christoph Hellwig558e6892010-04-28 12:28:58 +0000364 return offset >= imap->br_startoff &&
365 offset < imap->br_startoff + imap->br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366}
367
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100368/*
369 * BIO completion handler for buffered IO.
370 */
Al Viro782e3b32007-10-12 07:17:47 +0100371STATIC void
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100372xfs_end_bio(
373 struct bio *bio,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100374 int error)
375{
376 xfs_ioend_t *ioend = bio->bi_private;
377
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100378 ASSERT(atomic_read(&bio->bi_cnt) >= 1);
Nathan Scott7d04a332006-06-09 14:58:38 +1000379 ioend->io_error = test_bit(BIO_UPTODATE, &bio->bi_flags) ? 0 : error;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100380
381 /* Toss bio and pass work off to an xfsdatad thread */
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100382 bio->bi_private = NULL;
383 bio->bi_end_io = NULL;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100384 bio_put(bio);
Nathan Scott7d04a332006-06-09 14:58:38 +1000385
Christoph Hellwig209fb872010-07-18 21:17:11 +0000386 xfs_finish_ioend(ioend);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100387}
388
389STATIC void
390xfs_submit_ioend_bio(
Christoph Hellwig06342cf2009-10-30 09:09:15 +0000391 struct writeback_control *wbc,
392 xfs_ioend_t *ioend,
393 struct bio *bio)
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100394{
395 atomic_inc(&ioend->io_remaining);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100396 bio->bi_private = ioend;
397 bio->bi_end_io = xfs_end_bio;
Jens Axboe721a9602011-03-09 11:56:30 +0100398 submit_bio(wbc->sync_mode == WB_SYNC_ALL ? WRITE_SYNC : WRITE, bio);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100399}
400
401STATIC struct bio *
402xfs_alloc_ioend_bio(
403 struct buffer_head *bh)
404{
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100405 int nvecs = bio_get_nr_vecs(bh->b_bdev);
Christoph Hellwig221cb252010-12-10 08:42:17 +0000406 struct bio *bio = bio_alloc(GFP_NOIO, nvecs);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100407
408 ASSERT(bio->bi_private == NULL);
409 bio->bi_sector = bh->b_blocknr * (bh->b_size >> 9);
410 bio->bi_bdev = bh->b_bdev;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100411 return bio;
412}
413
414STATIC void
415xfs_start_buffer_writeback(
416 struct buffer_head *bh)
417{
418 ASSERT(buffer_mapped(bh));
419 ASSERT(buffer_locked(bh));
420 ASSERT(!buffer_delay(bh));
421 ASSERT(!buffer_unwritten(bh));
422
423 mark_buffer_async_write(bh);
424 set_buffer_uptodate(bh);
425 clear_buffer_dirty(bh);
426}
427
428STATIC void
429xfs_start_page_writeback(
430 struct page *page,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100431 int clear_dirty,
432 int buffers)
433{
434 ASSERT(PageLocked(page));
435 ASSERT(!PageWriteback(page));
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100436 if (clear_dirty)
David Chinner92132022006-12-21 10:24:01 +1100437 clear_page_dirty_for_io(page);
438 set_page_writeback(page);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100439 unlock_page(page);
Fengguang Wu1f7decf2007-10-16 23:30:42 -0700440 /* If no buffers on the page are to be written, finish it here */
441 if (!buffers)
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100442 end_page_writeback(page);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100443}
444
Zhi Yong Wuc7c1a7d2013-08-07 10:11:09 +0000445static inline int xfs_bio_add_buffer(struct bio *bio, struct buffer_head *bh)
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100446{
447 return bio_add_page(bio, bh->b_page, bh->b_size, bh_offset(bh));
448}
449
450/*
David Chinnerd88992f2006-01-18 13:38:12 +1100451 * Submit all of the bios for all of the ioends we have saved up, covering the
452 * initial writepage page and also any probed pages.
453 *
454 * Because we may have multiple ioends spanning a page, we need to start
455 * writeback on all the buffers before we submit them for I/O. If we mark the
456 * buffers as we got, then we can end up with a page that only has buffers
457 * marked async write and I/O complete on can occur before we mark the other
458 * buffers async write.
459 *
460 * The end result of this is that we trip a bug in end_page_writeback() because
461 * we call it twice for the one page as the code in end_buffer_async_write()
462 * assumes that all buffers on the page are started at the same time.
463 *
464 * The fix is two passes across the ioend list - one to start writeback on the
Nathan Scottc41564b2006-03-29 08:55:14 +1000465 * buffer_heads, and then submit them for I/O on the second pass.
Dave Chinner7bf7f352012-11-12 22:09:45 +1100466 *
467 * If @fail is non-zero, it means that we have a situation where some part of
468 * the submission process has failed after we have marked paged for writeback
469 * and unlocked them. In this situation, we need to fail the ioend chain rather
470 * than submit it to IO. This typically only happens on a filesystem shutdown.
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100471 */
472STATIC void
473xfs_submit_ioend(
Christoph Hellwig06342cf2009-10-30 09:09:15 +0000474 struct writeback_control *wbc,
Dave Chinner7bf7f352012-11-12 22:09:45 +1100475 xfs_ioend_t *ioend,
476 int fail)
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100477{
David Chinnerd88992f2006-01-18 13:38:12 +1100478 xfs_ioend_t *head = ioend;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100479 xfs_ioend_t *next;
480 struct buffer_head *bh;
481 struct bio *bio;
482 sector_t lastblock = 0;
483
David Chinnerd88992f2006-01-18 13:38:12 +1100484 /* Pass 1 - start writeback */
485 do {
486 next = ioend->io_list;
Christoph Hellwig221cb252010-12-10 08:42:17 +0000487 for (bh = ioend->io_buffer_head; bh; bh = bh->b_private)
David Chinnerd88992f2006-01-18 13:38:12 +1100488 xfs_start_buffer_writeback(bh);
David Chinnerd88992f2006-01-18 13:38:12 +1100489 } while ((ioend = next) != NULL);
490
491 /* Pass 2 - submit I/O */
492 ioend = head;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100493 do {
494 next = ioend->io_list;
495 bio = NULL;
496
Dave Chinner7bf7f352012-11-12 22:09:45 +1100497 /*
498 * If we are failing the IO now, just mark the ioend with an
499 * error and finish it. This will run IO completion immediately
500 * as there is only one reference to the ioend at this point in
501 * time.
502 */
503 if (fail) {
504 ioend->io_error = -fail;
505 xfs_finish_ioend(ioend);
506 continue;
507 }
508
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100509 for (bh = ioend->io_buffer_head; bh; bh = bh->b_private) {
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100510
511 if (!bio) {
512 retry:
513 bio = xfs_alloc_ioend_bio(bh);
514 } else if (bh->b_blocknr != lastblock + 1) {
Christoph Hellwig06342cf2009-10-30 09:09:15 +0000515 xfs_submit_ioend_bio(wbc, ioend, bio);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100516 goto retry;
517 }
518
Zhi Yong Wuc7c1a7d2013-08-07 10:11:09 +0000519 if (xfs_bio_add_buffer(bio, bh) != bh->b_size) {
Christoph Hellwig06342cf2009-10-30 09:09:15 +0000520 xfs_submit_ioend_bio(wbc, ioend, bio);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100521 goto retry;
522 }
523
524 lastblock = bh->b_blocknr;
525 }
526 if (bio)
Christoph Hellwig06342cf2009-10-30 09:09:15 +0000527 xfs_submit_ioend_bio(wbc, ioend, bio);
Christoph Hellwig209fb872010-07-18 21:17:11 +0000528 xfs_finish_ioend(ioend);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100529 } while ((ioend = next) != NULL);
530}
531
532/*
533 * Cancel submission of all buffer_heads so far in this endio.
534 * Toss the endio too. Only ever called for the initial page
535 * in a writepage request, so only ever one page.
536 */
537STATIC void
538xfs_cancel_ioend(
539 xfs_ioend_t *ioend)
540{
541 xfs_ioend_t *next;
542 struct buffer_head *bh, *next_bh;
543
544 do {
545 next = ioend->io_list;
546 bh = ioend->io_buffer_head;
547 do {
548 next_bh = bh->b_private;
549 clear_buffer_async_write(bh);
550 unlock_buffer(bh);
551 } while ((bh = next_bh) != NULL);
552
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100553 mempool_free(ioend, xfs_ioend_pool);
554 } while ((ioend = next) != NULL);
555}
556
557/*
558 * Test to see if we've been building up a completion structure for
559 * earlier buffers -- if so, we try to append to this ioend if we
560 * can, otherwise we finish off any current ioend and start another.
561 * Return true if we've finished the given ioend.
562 */
563STATIC void
564xfs_add_to_ioend(
565 struct inode *inode,
566 struct buffer_head *bh,
Christoph Hellwig7336cea2006-01-11 20:49:16 +1100567 xfs_off_t offset,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100568 unsigned int type,
569 xfs_ioend_t **result,
570 int need_ioend)
571{
572 xfs_ioend_t *ioend = *result;
573
574 if (!ioend || need_ioend || type != ioend->io_type) {
575 xfs_ioend_t *previous = *result;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100576
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100577 ioend = xfs_alloc_ioend(inode, type);
578 ioend->io_offset = offset;
579 ioend->io_buffer_head = bh;
580 ioend->io_buffer_tail = bh;
581 if (previous)
582 previous->io_list = ioend;
583 *result = ioend;
584 } else {
585 ioend->io_buffer_tail->b_private = bh;
586 ioend->io_buffer_tail = bh;
587 }
588
589 bh->b_private = NULL;
590 ioend->io_size += bh->b_size;
591}
592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593STATIC void
Nathan Scott87cbc492006-03-14 13:26:43 +1100594xfs_map_buffer(
Christoph Hellwig046f1682010-04-28 12:28:52 +0000595 struct inode *inode,
Nathan Scott87cbc492006-03-14 13:26:43 +1100596 struct buffer_head *bh,
Christoph Hellwig207d0412010-04-28 12:28:56 +0000597 struct xfs_bmbt_irec *imap,
Christoph Hellwig046f1682010-04-28 12:28:52 +0000598 xfs_off_t offset)
Nathan Scott87cbc492006-03-14 13:26:43 +1100599{
600 sector_t bn;
Christoph Hellwig8699bb02010-04-28 12:28:54 +0000601 struct xfs_mount *m = XFS_I(inode)->i_mount;
Christoph Hellwig207d0412010-04-28 12:28:56 +0000602 xfs_off_t iomap_offset = XFS_FSB_TO_B(m, imap->br_startoff);
603 xfs_daddr_t iomap_bn = xfs_fsb_to_db(XFS_I(inode), imap->br_startblock);
Nathan Scott87cbc492006-03-14 13:26:43 +1100604
Christoph Hellwig207d0412010-04-28 12:28:56 +0000605 ASSERT(imap->br_startblock != HOLESTARTBLOCK);
606 ASSERT(imap->br_startblock != DELAYSTARTBLOCK);
Nathan Scott87cbc492006-03-14 13:26:43 +1100607
Christoph Hellwige5131822010-04-28 12:28:55 +0000608 bn = (iomap_bn >> (inode->i_blkbits - BBSHIFT)) +
Christoph Hellwig8699bb02010-04-28 12:28:54 +0000609 ((offset - iomap_offset) >> inode->i_blkbits);
Nathan Scott87cbc492006-03-14 13:26:43 +1100610
Christoph Hellwig046f1682010-04-28 12:28:52 +0000611 ASSERT(bn || XFS_IS_REALTIME_INODE(XFS_I(inode)));
Nathan Scott87cbc492006-03-14 13:26:43 +1100612
613 bh->b_blocknr = bn;
614 set_buffer_mapped(bh);
615}
616
617STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618xfs_map_at_offset(
Christoph Hellwig046f1682010-04-28 12:28:52 +0000619 struct inode *inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 struct buffer_head *bh,
Christoph Hellwig207d0412010-04-28 12:28:56 +0000621 struct xfs_bmbt_irec *imap,
Christoph Hellwig046f1682010-04-28 12:28:52 +0000622 xfs_off_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
Christoph Hellwig207d0412010-04-28 12:28:56 +0000624 ASSERT(imap->br_startblock != HOLESTARTBLOCK);
625 ASSERT(imap->br_startblock != DELAYSTARTBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Christoph Hellwig207d0412010-04-28 12:28:56 +0000627 xfs_map_buffer(inode, bh, imap, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 set_buffer_mapped(bh);
629 clear_buffer_delay(bh);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100630 clear_buffer_unwritten(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631}
632
633/*
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100634 * Test if a given page is suitable for writing as part of an unwritten
635 * or delayed allocate extent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 */
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100637STATIC int
Dave Chinner6ffc4db2012-04-23 15:58:43 +1000638xfs_check_page_type(
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100639 struct page *page,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100640 unsigned int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 if (PageWriteback(page))
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100643 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
645 if (page->mapping && page_has_buffers(page)) {
646 struct buffer_head *bh, *head;
647 int acceptable = 0;
648
649 bh = head = page_buffers(page);
650 do {
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100651 if (buffer_unwritten(bh))
Alain Renaud0d882a32012-05-22 15:56:21 -0500652 acceptable += (type == XFS_IO_UNWRITTEN);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100653 else if (buffer_delay(bh))
Alain Renaud0d882a32012-05-22 15:56:21 -0500654 acceptable += (type == XFS_IO_DELALLOC);
David Chinner2ddee842006-03-22 12:47:40 +1100655 else if (buffer_dirty(bh) && buffer_mapped(bh))
Alain Renaud0d882a32012-05-22 15:56:21 -0500656 acceptable += (type == XFS_IO_OVERWRITE);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100657 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 } while ((bh = bh->b_this_page) != head);
660
661 if (acceptable)
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100662 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 }
664
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100665 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666}
667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668/*
669 * Allocate & map buffers for page given the extent map. Write it out.
670 * except for the original page of a writepage, this is called on
671 * delalloc/unwritten pages only, for the original page it is possible
672 * that the page has no mapping at all.
673 */
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100674STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675xfs_convert_page(
676 struct inode *inode,
677 struct page *page,
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100678 loff_t tindex,
Christoph Hellwig207d0412010-04-28 12:28:56 +0000679 struct xfs_bmbt_irec *imap,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100680 xfs_ioend_t **ioendp,
Christoph Hellwig2fa24f92010-12-10 08:42:23 +0000681 struct writeback_control *wbc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100683 struct buffer_head *bh, *head;
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100684 xfs_off_t end_offset;
685 unsigned long p_offset;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100686 unsigned int type;
Nathan Scott24e17b52005-05-05 13:33:20 -0700687 int len, page_dirty;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100688 int count = 0, done = 0, uptodate = 1;
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100689 xfs_off_t offset = page_offset(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100691 if (page->index != tindex)
692 goto fail;
Nick Piggin529ae9a2008-08-02 12:01:03 +0200693 if (!trylock_page(page))
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100694 goto fail;
695 if (PageWriteback(page))
696 goto fail_unlock_page;
697 if (page->mapping != inode->i_mapping)
698 goto fail_unlock_page;
Dave Chinner6ffc4db2012-04-23 15:58:43 +1000699 if (!xfs_check_page_type(page, (*ioendp)->io_type))
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100700 goto fail_unlock_page;
701
Nathan Scott24e17b52005-05-05 13:33:20 -0700702 /*
703 * page_dirty is initially a count of buffers on the page before
Nathan Scottc41564b2006-03-29 08:55:14 +1000704 * EOF and is decremented as we move each into a cleanable state.
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100705 *
706 * Derivation:
707 *
708 * End offset is the highest offset that this page should represent.
709 * If we are on the last page, (end_offset & (PAGE_CACHE_SIZE - 1))
710 * will evaluate non-zero and be less than PAGE_CACHE_SIZE and
711 * hence give us the correct page_dirty count. On any other page,
712 * it will be zero and in that case we need page_dirty to be the
713 * count of buffers on the page.
Nathan Scott24e17b52005-05-05 13:33:20 -0700714 */
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100715 end_offset = min_t(unsigned long long,
716 (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT,
717 i_size_read(inode));
718
Dave Chinner480d7462013-05-20 09:51:08 +1000719 /*
720 * If the current map does not span the entire page we are about to try
721 * to write, then give up. The only way we can write a page that spans
722 * multiple mappings in a single writeback iteration is via the
723 * xfs_vm_writepage() function. Data integrity writeback requires the
724 * entire page to be written in a single attempt, otherwise the part of
725 * the page we don't write here doesn't get written as part of the data
726 * integrity sync.
727 *
728 * For normal writeback, we also don't attempt to write partial pages
729 * here as it simply means that write_cache_pages() will see it under
730 * writeback and ignore the page until some point in the future, at
731 * which time this will be the only page in the file that needs
732 * writeback. Hence for more optimal IO patterns, we should always
733 * avoid partial page writeback due to multiple mappings on a page here.
734 */
735 if (!xfs_imap_valid(inode, imap, end_offset))
736 goto fail_unlock_page;
737
Nathan Scott24e17b52005-05-05 13:33:20 -0700738 len = 1 << inode->i_blkbits;
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100739 p_offset = min_t(unsigned long, end_offset & (PAGE_CACHE_SIZE - 1),
740 PAGE_CACHE_SIZE);
741 p_offset = p_offset ? roundup(p_offset, len) : PAGE_CACHE_SIZE;
742 page_dirty = p_offset / len;
Nathan Scott24e17b52005-05-05 13:33:20 -0700743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 bh = head = page_buffers(page);
745 do {
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100746 if (offset >= end_offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 break;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100748 if (!buffer_uptodate(bh))
749 uptodate = 0;
750 if (!(PageUptodate(page) || buffer_uptodate(bh))) {
751 done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 continue;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100753 }
754
Christoph Hellwig2fa24f92010-12-10 08:42:23 +0000755 if (buffer_unwritten(bh) || buffer_delay(bh) ||
756 buffer_mapped(bh)) {
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100757 if (buffer_unwritten(bh))
Alain Renaud0d882a32012-05-22 15:56:21 -0500758 type = XFS_IO_UNWRITTEN;
Christoph Hellwig2fa24f92010-12-10 08:42:23 +0000759 else if (buffer_delay(bh))
Alain Renaud0d882a32012-05-22 15:56:21 -0500760 type = XFS_IO_DELALLOC;
Christoph Hellwig2fa24f92010-12-10 08:42:23 +0000761 else
Alain Renaud0d882a32012-05-22 15:56:21 -0500762 type = XFS_IO_OVERWRITE;
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100763
Christoph Hellwig558e6892010-04-28 12:28:58 +0000764 if (!xfs_imap_valid(inode, imap, offset)) {
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100765 done = 1;
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100766 continue;
767 }
768
Christoph Hellwigecff71e2010-12-10 08:42:25 +0000769 lock_buffer(bh);
Alain Renaud0d882a32012-05-22 15:56:21 -0500770 if (type != XFS_IO_OVERWRITE)
Christoph Hellwig2fa24f92010-12-10 08:42:23 +0000771 xfs_map_at_offset(inode, bh, imap, offset);
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000772 xfs_add_to_ioend(inode, bh, offset, type,
773 ioendp, done);
774
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100775 page_dirty--;
776 count++;
777 } else {
Christoph Hellwig2fa24f92010-12-10 08:42:23 +0000778 done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 }
Christoph Hellwig7336cea2006-01-11 20:49:16 +1100780 } while (offset += len, (bh = bh->b_this_page) != head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100782 if (uptodate && bh == head)
783 SetPageUptodate(page);
784
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000785 if (count) {
Dave Chinnerefceab12010-08-24 11:44:56 +1000786 if (--wbc->nr_to_write <= 0 &&
787 wbc->sync_mode == WB_SYNC_NONE)
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000788 done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 }
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000790 xfs_start_page_writeback(page, !page_dirty, count);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100791
792 return done;
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100793 fail_unlock_page:
794 unlock_page(page);
795 fail:
796 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797}
798
799/*
800 * Convert & write out a cluster of pages in the same extent as defined
801 * by mp and following the start page.
802 */
803STATIC void
804xfs_cluster_write(
805 struct inode *inode,
806 pgoff_t tindex,
Christoph Hellwig207d0412010-04-28 12:28:56 +0000807 struct xfs_bmbt_irec *imap,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100808 xfs_ioend_t **ioendp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 struct writeback_control *wbc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 pgoff_t tlast)
811{
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100812 struct pagevec pvec;
813 int done = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100815 pagevec_init(&pvec, 0);
816 while (!done && tindex <= tlast) {
817 unsigned len = min_t(pgoff_t, PAGEVEC_SIZE, tlast - tindex + 1);
818
819 if (!pagevec_lookup(&pvec, inode->i_mapping, tindex, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 break;
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100821
822 for (i = 0; i < pagevec_count(&pvec); i++) {
823 done = xfs_convert_page(inode, pvec.pages[i], tindex++,
Christoph Hellwig2fa24f92010-12-10 08:42:23 +0000824 imap, ioendp, wbc);
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100825 if (done)
826 break;
827 }
828
829 pagevec_release(&pvec);
830 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 }
832}
833
Dave Chinner3ed3a432010-03-05 02:00:42 +0000834STATIC void
835xfs_vm_invalidatepage(
836 struct page *page,
Lukas Czernerd47992f2013-05-21 23:17:23 -0400837 unsigned int offset,
838 unsigned int length)
Dave Chinner3ed3a432010-03-05 02:00:42 +0000839{
Lukas Czerner34097df2013-05-21 23:58:01 -0400840 trace_xfs_invalidatepage(page->mapping->host, page, offset,
841 length);
842 block_invalidatepage(page, offset, length);
Dave Chinner3ed3a432010-03-05 02:00:42 +0000843}
844
845/*
846 * If the page has delalloc buffers on it, we need to punch them out before we
847 * invalidate the page. If we don't, we leave a stale delalloc mapping on the
848 * inode that can trip a BUG() in xfs_get_blocks() later on if a direct IO read
849 * is done on that same region - the delalloc extent is returned when none is
850 * supposed to be there.
851 *
852 * We prevent this by truncating away the delalloc regions on the page before
853 * invalidating it. Because they are delalloc, we can do this without needing a
854 * transaction. Indeed - if we get ENOSPC errors, we have to be able to do this
855 * truncation without a transaction as there is no space left for block
856 * reservation (typically why we see a ENOSPC in writeback).
857 *
858 * This is not a performance critical path, so for now just do the punching a
859 * buffer head at a time.
860 */
861STATIC void
862xfs_aops_discard_page(
863 struct page *page)
864{
865 struct inode *inode = page->mapping->host;
866 struct xfs_inode *ip = XFS_I(inode);
867 struct buffer_head *bh, *head;
868 loff_t offset = page_offset(page);
Dave Chinner3ed3a432010-03-05 02:00:42 +0000869
Alain Renaud0d882a32012-05-22 15:56:21 -0500870 if (!xfs_check_page_type(page, XFS_IO_DELALLOC))
Dave Chinner3ed3a432010-03-05 02:00:42 +0000871 goto out_invalidate;
872
Dave Chinnere8c37532010-03-15 02:36:35 +0000873 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
874 goto out_invalidate;
875
Dave Chinner4f107002011-03-07 10:00:35 +1100876 xfs_alert(ip->i_mount,
Dave Chinner3ed3a432010-03-05 02:00:42 +0000877 "page discard on page %p, inode 0x%llx, offset %llu.",
878 page, ip->i_ino, offset);
879
880 xfs_ilock(ip, XFS_ILOCK_EXCL);
881 bh = head = page_buffers(page);
882 do {
Dave Chinner3ed3a432010-03-05 02:00:42 +0000883 int error;
Dave Chinnerc726de42010-11-30 15:14:39 +1100884 xfs_fileoff_t start_fsb;
Dave Chinner3ed3a432010-03-05 02:00:42 +0000885
886 if (!buffer_delay(bh))
887 goto next_buffer;
888
Dave Chinnerc726de42010-11-30 15:14:39 +1100889 start_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
890 error = xfs_bmap_punch_delalloc_range(ip, start_fsb, 1);
Dave Chinner3ed3a432010-03-05 02:00:42 +0000891 if (error) {
892 /* something screwed, just bail */
Dave Chinnere8c37532010-03-15 02:36:35 +0000893 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
Dave Chinner4f107002011-03-07 10:00:35 +1100894 xfs_alert(ip->i_mount,
Dave Chinner3ed3a432010-03-05 02:00:42 +0000895 "page discard unable to remove delalloc mapping.");
Dave Chinnere8c37532010-03-15 02:36:35 +0000896 }
Dave Chinner3ed3a432010-03-05 02:00:42 +0000897 break;
898 }
899next_buffer:
Dave Chinnerc726de42010-11-30 15:14:39 +1100900 offset += 1 << inode->i_blkbits;
Dave Chinner3ed3a432010-03-05 02:00:42 +0000901
902 } while ((bh = bh->b_this_page) != head);
903
904 xfs_iunlock(ip, XFS_ILOCK_EXCL);
905out_invalidate:
Lukas Czernerd47992f2013-05-21 23:17:23 -0400906 xfs_vm_invalidatepage(page, 0, PAGE_CACHE_SIZE);
Dave Chinner3ed3a432010-03-05 02:00:42 +0000907 return;
908}
909
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910/*
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000911 * Write out a dirty page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 *
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000913 * For delalloc space on the page we need to allocate space and flush it.
914 * For unwritten space on the page we need to start the conversion to
915 * regular allocated space.
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000916 * For any other dirty buffer heads on the page we should flush them.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918STATIC int
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000919xfs_vm_writepage(
920 struct page *page,
921 struct writeback_control *wbc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000923 struct inode *inode = page->mapping->host;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100924 struct buffer_head *bh, *head;
Christoph Hellwig207d0412010-04-28 12:28:56 +0000925 struct xfs_bmbt_irec imap;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100926 xfs_ioend_t *ioend = NULL, *iohead = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 loff_t offset;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100928 unsigned int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 __uint64_t end_offset;
Christoph Hellwigbd1556a2010-04-28 12:29:00 +0000930 pgoff_t end_index, last_index;
Christoph Hellwiged1e7b72010-12-10 08:42:22 +0000931 ssize_t len;
Christoph Hellwiga206c812010-12-10 08:42:20 +0000932 int err, imap_valid = 0, uptodate = 1;
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000933 int count = 0;
Christoph Hellwiga206c812010-12-10 08:42:20 +0000934 int nonblocking = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Lukas Czerner34097df2013-05-21 23:58:01 -0400936 trace_xfs_writepage(inode, page, 0, 0);
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000937
Christoph Hellwig20cb52e2010-06-24 09:46:01 +1000938 ASSERT(page_has_buffers(page));
939
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000940 /*
941 * Refuse to write the page out if we are called from reclaim context.
942 *
Christoph Hellwigd4f7a5c2010-06-28 10:34:44 -0400943 * This avoids stack overflows when called from deeply used stacks in
944 * random callers for direct reclaim or memcg reclaim. We explicitly
945 * allow reclaim from kswapd as the stack usage there is relatively low.
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000946 *
Mel Gorman94054fa2011-10-31 17:07:45 -0700947 * This should never happen except in the case of a VM regression so
948 * warn about it.
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000949 */
Mel Gorman94054fa2011-10-31 17:07:45 -0700950 if (WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD)) ==
951 PF_MEMALLOC))
Christoph Hellwigb5420f22010-08-24 11:47:51 +1000952 goto redirty;
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000953
954 /*
Christoph Hellwig680a6472011-07-08 14:34:05 +0200955 * Given that we do not allow direct reclaim to call us, we should
956 * never be called while in a filesystem transaction.
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000957 */
Christoph Hellwig680a6472011-07-08 14:34:05 +0200958 if (WARN_ON(current->flags & PF_FSTRANS))
Christoph Hellwigb5420f22010-08-24 11:47:51 +1000959 goto redirty;
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000960
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 /* Is this page beyond the end of the file? */
962 offset = i_size_read(inode);
963 end_index = offset >> PAGE_CACHE_SHIFT;
964 last_index = (offset - 1) >> PAGE_CACHE_SHIFT;
965 if (page->index >= end_index) {
Christoph Hellwig6b7a03f2012-07-03 12:20:00 -0400966 unsigned offset_into_page = offset & (PAGE_CACHE_SIZE - 1);
967
968 /*
Jan Karaff9a28f2013-03-14 14:30:54 +0100969 * Skip the page if it is fully outside i_size, e.g. due to a
970 * truncate operation that is in progress. We must redirty the
971 * page so that reclaim stops reclaiming it. Otherwise
972 * xfs_vm_releasepage() is called on it and gets confused.
Christoph Hellwig6b7a03f2012-07-03 12:20:00 -0400973 */
Jan Karaff9a28f2013-03-14 14:30:54 +0100974 if (page->index >= end_index + 1 || offset_into_page == 0)
975 goto redirty;
Christoph Hellwig6b7a03f2012-07-03 12:20:00 -0400976
977 /*
978 * The page straddles i_size. It must be zeroed out on each
979 * and every writepage invocation because it may be mmapped.
980 * "A file is mapped in multiples of the page size. For a file
981 * that is not a multiple of the page size, the remaining
982 * memory is zeroed when mapped, and writes to that region are
983 * not written out to the file."
984 */
985 zero_user_segment(page, offset_into_page, PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 }
987
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100988 end_offset = min_t(unsigned long long,
Christoph Hellwig20cb52e2010-06-24 09:46:01 +1000989 (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT,
990 offset);
Nathan Scott24e17b52005-05-05 13:33:20 -0700991 len = 1 << inode->i_blkbits;
Nathan Scott24e17b52005-05-05 13:33:20 -0700992
Nathan Scott24e17b52005-05-05 13:33:20 -0700993 bh = head = page_buffers(page);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100994 offset = page_offset(page);
Alain Renaud0d882a32012-05-22 15:56:21 -0500995 type = XFS_IO_OVERWRITE;
Christoph Hellwiga206c812010-12-10 08:42:20 +0000996
Christoph Hellwigdbcdde32011-07-08 14:34:14 +0200997 if (wbc->sync_mode == WB_SYNC_NONE)
Christoph Hellwiga206c812010-12-10 08:42:20 +0000998 nonblocking = 1;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100999
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 do {
Christoph Hellwig6ac72482010-12-10 08:42:18 +00001001 int new_ioend = 0;
1002
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 if (offset >= end_offset)
1004 break;
1005 if (!buffer_uptodate(bh))
1006 uptodate = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Eric Sandeen3d9b02e2010-06-24 09:45:30 +10001008 /*
Christoph Hellwigece413f2010-11-10 21:39:11 +00001009 * set_page_dirty dirties all buffers in a page, independent
1010 * of their state. The dirty state however is entirely
1011 * meaningless for holes (!mapped && uptodate), so skip
1012 * buffers covering holes here.
Eric Sandeen3d9b02e2010-06-24 09:45:30 +10001013 */
1014 if (!buffer_mapped(bh) && buffer_uptodate(bh)) {
Eric Sandeen3d9b02e2010-06-24 09:45:30 +10001015 imap_valid = 0;
1016 continue;
1017 }
1018
Christoph Hellwigaeea1b12010-12-10 08:42:24 +00001019 if (buffer_unwritten(bh)) {
Alain Renaud0d882a32012-05-22 15:56:21 -05001020 if (type != XFS_IO_UNWRITTEN) {
1021 type = XFS_IO_UNWRITTEN;
Christoph Hellwigaeea1b12010-12-10 08:42:24 +00001022 imap_valid = 0;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001023 }
Christoph Hellwigaeea1b12010-12-10 08:42:24 +00001024 } else if (buffer_delay(bh)) {
Alain Renaud0d882a32012-05-22 15:56:21 -05001025 if (type != XFS_IO_DELALLOC) {
1026 type = XFS_IO_DELALLOC;
Christoph Hellwigaeea1b12010-12-10 08:42:24 +00001027 imap_valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 }
Christoph Hellwig89f3b3632010-06-24 09:45:48 +10001029 } else if (buffer_uptodate(bh)) {
Alain Renaud0d882a32012-05-22 15:56:21 -05001030 if (type != XFS_IO_OVERWRITE) {
1031 type = XFS_IO_OVERWRITE;
Christoph Hellwig85da94c2010-12-10 08:42:16 +00001032 imap_valid = 0;
1033 }
Christoph Hellwigaeea1b12010-12-10 08:42:24 +00001034 } else {
Alain Renaud7d0fa3e2012-06-08 15:34:46 -04001035 if (PageUptodate(page))
Christoph Hellwigaeea1b12010-12-10 08:42:24 +00001036 ASSERT(buffer_mapped(bh));
Alain Renaud7d0fa3e2012-06-08 15:34:46 -04001037 /*
1038 * This buffer is not uptodate and will not be
1039 * written to disk. Ensure that we will put any
1040 * subsequent writeable buffers into a new
1041 * ioend.
1042 */
1043 imap_valid = 0;
Christoph Hellwigaeea1b12010-12-10 08:42:24 +00001044 continue;
1045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Christoph Hellwigaeea1b12010-12-10 08:42:24 +00001047 if (imap_valid)
1048 imap_valid = xfs_imap_valid(inode, &imap, offset);
1049 if (!imap_valid) {
1050 /*
1051 * If we didn't have a valid mapping then we need to
1052 * put the new mapping into a separate ioend structure.
1053 * This ensures non-contiguous extents always have
1054 * separate ioends, which is particularly important
1055 * for unwritten extent conversion at I/O completion
1056 * time.
1057 */
1058 new_ioend = 1;
1059 err = xfs_map_blocks(inode, offset, &imap, type,
1060 nonblocking);
1061 if (err)
1062 goto error;
1063 imap_valid = xfs_imap_valid(inode, &imap, offset);
1064 }
1065 if (imap_valid) {
Christoph Hellwigecff71e2010-12-10 08:42:25 +00001066 lock_buffer(bh);
Alain Renaud0d882a32012-05-22 15:56:21 -05001067 if (type != XFS_IO_OVERWRITE)
Christoph Hellwigaeea1b12010-12-10 08:42:24 +00001068 xfs_map_at_offset(inode, bh, &imap, offset);
1069 xfs_add_to_ioend(inode, bh, offset, type, &ioend,
1070 new_ioend);
1071 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 }
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001073
1074 if (!iohead)
1075 iohead = ioend;
1076
1077 } while (offset += len, ((bh = bh->b_this_page) != head));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
1079 if (uptodate && bh == head)
1080 SetPageUptodate(page);
1081
Christoph Hellwig89f3b3632010-06-24 09:45:48 +10001082 xfs_start_page_writeback(page, 1, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
Dave Chinner7bf7f352012-11-12 22:09:45 +11001084 /* if there is no IO to be submitted for this page, we are done */
1085 if (!ioend)
1086 return 0;
1087
1088 ASSERT(iohead);
1089
1090 /*
1091 * Any errors from this point onwards need tobe reported through the IO
1092 * completion path as we have marked the initial page as under writeback
1093 * and unlocked it.
1094 */
1095 if (imap_valid) {
Christoph Hellwigbd1556a2010-04-28 12:29:00 +00001096 xfs_off_t end_index;
Christoph Hellwig8699bb02010-04-28 12:28:54 +00001097
Christoph Hellwigbd1556a2010-04-28 12:29:00 +00001098 end_index = imap.br_startoff + imap.br_blockcount;
1099
1100 /* to bytes */
1101 end_index <<= inode->i_blkbits;
1102
1103 /* to pages */
1104 end_index = (end_index - 1) >> PAGE_CACHE_SHIFT;
1105
1106 /* check against file size */
1107 if (end_index > last_index)
1108 end_index = last_index;
1109
Christoph Hellwig207d0412010-04-28 12:28:56 +00001110 xfs_cluster_write(inode, page->index + 1, &imap, &ioend,
Christoph Hellwig2fa24f92010-12-10 08:42:23 +00001111 wbc, end_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 }
1113
Christoph Hellwig281627d2012-03-13 08:41:05 +00001114
Dave Chinner7bf7f352012-11-12 22:09:45 +11001115 /*
1116 * Reserve log space if we might write beyond the on-disk inode size.
1117 */
1118 err = 0;
1119 if (ioend->io_type != XFS_IO_UNWRITTEN && xfs_ioend_is_append(ioend))
1120 err = xfs_setfilesize_trans_alloc(ioend);
1121
1122 xfs_submit_ioend(wbc, iohead, err);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001123
Christoph Hellwig89f3b3632010-06-24 09:45:48 +10001124 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
1126error:
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001127 if (iohead)
1128 xfs_cancel_ioend(iohead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
Christoph Hellwigb5420f22010-08-24 11:47:51 +10001130 if (err == -EAGAIN)
1131 goto redirty;
1132
Christoph Hellwig20cb52e2010-06-24 09:46:01 +10001133 xfs_aops_discard_page(page);
Christoph Hellwig89f3b3632010-06-24 09:45:48 +10001134 ClearPageUptodate(page);
1135 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 return err;
Nathan Scottf51623b2006-03-14 13:26:27 +11001137
Christoph Hellwigb5420f22010-08-24 11:47:51 +10001138redirty:
Nathan Scottf51623b2006-03-14 13:26:27 +11001139 redirty_page_for_writepage(wbc, page);
1140 unlock_page(page);
1141 return 0;
Nathan Scottf51623b2006-03-14 13:26:27 +11001142}
1143
Nathan Scott7d4fb402006-06-09 15:27:16 +10001144STATIC int
1145xfs_vm_writepages(
1146 struct address_space *mapping,
1147 struct writeback_control *wbc)
1148{
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +10001149 xfs_iflags_clear(XFS_I(mapping->host), XFS_ITRUNCATED);
Nathan Scott7d4fb402006-06-09 15:27:16 +10001150 return generic_writepages(mapping, wbc);
1151}
1152
Nathan Scottf51623b2006-03-14 13:26:27 +11001153/*
1154 * Called to move a page into cleanable state - and from there
Christoph Hellwig89f3b3632010-06-24 09:45:48 +10001155 * to be released. The page should already be clean. We always
Nathan Scottf51623b2006-03-14 13:26:27 +11001156 * have buffer heads in this call.
1157 *
Christoph Hellwig89f3b3632010-06-24 09:45:48 +10001158 * Returns 1 if the page is ok to release, 0 otherwise.
Nathan Scottf51623b2006-03-14 13:26:27 +11001159 */
1160STATIC int
Nathan Scott238f4c52006-03-17 17:26:25 +11001161xfs_vm_releasepage(
Nathan Scottf51623b2006-03-14 13:26:27 +11001162 struct page *page,
1163 gfp_t gfp_mask)
1164{
Christoph Hellwig20cb52e2010-06-24 09:46:01 +10001165 int delalloc, unwritten;
Nathan Scottf51623b2006-03-14 13:26:27 +11001166
Lukas Czerner34097df2013-05-21 23:58:01 -04001167 trace_xfs_releasepage(page->mapping->host, page, 0, 0);
Nathan Scott238f4c52006-03-17 17:26:25 +11001168
Christoph Hellwig20cb52e2010-06-24 09:46:01 +10001169 xfs_count_page_state(page, &delalloc, &unwritten);
Nathan Scottf51623b2006-03-14 13:26:27 +11001170
Christoph Hellwig89f3b3632010-06-24 09:45:48 +10001171 if (WARN_ON(delalloc))
1172 return 0;
1173 if (WARN_ON(unwritten))
Nathan Scottf51623b2006-03-14 13:26:27 +11001174 return 0;
1175
Nathan Scottf51623b2006-03-14 13:26:27 +11001176 return try_to_free_buffers(page);
1177}
1178
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179STATIC int
Nathan Scottc2536662006-03-29 10:44:40 +10001180__xfs_get_blocks(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 struct inode *inode,
1182 sector_t iblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 struct buffer_head *bh_result,
1184 int create,
Christoph Hellwigf2bde9b2010-06-24 11:44:35 +10001185 int direct)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186{
Christoph Hellwiga206c812010-12-10 08:42:20 +00001187 struct xfs_inode *ip = XFS_I(inode);
1188 struct xfs_mount *mp = ip->i_mount;
1189 xfs_fileoff_t offset_fsb, end_fsb;
1190 int error = 0;
1191 int lockmode = 0;
Christoph Hellwig207d0412010-04-28 12:28:56 +00001192 struct xfs_bmbt_irec imap;
Christoph Hellwiga206c812010-12-10 08:42:20 +00001193 int nimaps = 1;
Nathan Scottfdc7ed72005-11-02 15:13:13 +11001194 xfs_off_t offset;
1195 ssize_t size;
Christoph Hellwig207d0412010-04-28 12:28:56 +00001196 int new = 0;
Christoph Hellwiga206c812010-12-10 08:42:20 +00001197
1198 if (XFS_FORCED_SHUTDOWN(mp))
1199 return -XFS_ERROR(EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
Nathan Scottfdc7ed72005-11-02 15:13:13 +11001201 offset = (xfs_off_t)iblock << inode->i_blkbits;
Nathan Scottc2536662006-03-29 10:44:40 +10001202 ASSERT(bh_result->b_size >= (1 << inode->i_blkbits));
1203 size = bh_result->b_size;
Lachlan McIlroy364f3582008-09-17 16:50:14 +10001204
1205 if (!create && direct && offset >= i_size_read(inode))
1206 return 0;
1207
Dave Chinner507630b2012-03-27 10:34:50 -04001208 /*
1209 * Direct I/O is usually done on preallocated files, so try getting
1210 * a block mapping without an exclusive lock first. For buffered
1211 * writes we already have the exclusive iolock anyway, so avoiding
1212 * a lock roundtrip here by taking the ilock exclusive from the
1213 * beginning is a useful micro optimization.
1214 */
1215 if (create && !direct) {
Christoph Hellwiga206c812010-12-10 08:42:20 +00001216 lockmode = XFS_ILOCK_EXCL;
1217 xfs_ilock(ip, lockmode);
1218 } else {
1219 lockmode = xfs_ilock_map_shared(ip);
1220 }
Christoph Hellwigf2bde9b2010-06-24 11:44:35 +10001221
Dave Chinnerd2c28192012-06-08 15:44:53 +10001222 ASSERT(offset <= mp->m_super->s_maxbytes);
1223 if (offset + size > mp->m_super->s_maxbytes)
1224 size = mp->m_super->s_maxbytes - offset;
Christoph Hellwiga206c812010-12-10 08:42:20 +00001225 end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + size);
1226 offset_fsb = XFS_B_TO_FSBT(mp, offset);
1227
Dave Chinner5c8ed202011-09-18 20:40:45 +00001228 error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb,
1229 &imap, &nimaps, XFS_BMAPI_ENTIRE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 if (error)
Christoph Hellwiga206c812010-12-10 08:42:20 +00001231 goto out_unlock;
1232
1233 if (create &&
1234 (!nimaps ||
1235 (imap.br_startblock == HOLESTARTBLOCK ||
1236 imap.br_startblock == DELAYSTARTBLOCK))) {
Dave Chinneraff3a9e2012-04-23 15:58:44 +10001237 if (direct || xfs_get_extsz_hint(ip)) {
Dave Chinner507630b2012-03-27 10:34:50 -04001238 /*
1239 * Drop the ilock in preparation for starting the block
1240 * allocation transaction. It will be retaken
1241 * exclusively inside xfs_iomap_write_direct for the
1242 * actual allocation.
1243 */
1244 xfs_iunlock(ip, lockmode);
Christoph Hellwiga206c812010-12-10 08:42:20 +00001245 error = xfs_iomap_write_direct(ip, offset, size,
1246 &imap, nimaps);
Dave Chinner507630b2012-03-27 10:34:50 -04001247 if (error)
1248 return -error;
Dave Chinnerd3bc8152012-04-27 19:45:21 +10001249 new = 1;
Christoph Hellwiga206c812010-12-10 08:42:20 +00001250 } else {
Dave Chinner507630b2012-03-27 10:34:50 -04001251 /*
1252 * Delalloc reservations do not require a transaction,
Dave Chinnerd3bc8152012-04-27 19:45:21 +10001253 * we can go on without dropping the lock here. If we
1254 * are allocating a new delalloc block, make sure that
1255 * we set the new flag so that we mark the buffer new so
1256 * that we know that it is newly allocated if the write
1257 * fails.
Dave Chinner507630b2012-03-27 10:34:50 -04001258 */
Dave Chinnerd3bc8152012-04-27 19:45:21 +10001259 if (nimaps && imap.br_startblock == HOLESTARTBLOCK)
1260 new = 1;
Christoph Hellwiga206c812010-12-10 08:42:20 +00001261 error = xfs_iomap_write_delay(ip, offset, size, &imap);
Dave Chinner507630b2012-03-27 10:34:50 -04001262 if (error)
1263 goto out_unlock;
1264
1265 xfs_iunlock(ip, lockmode);
Christoph Hellwiga206c812010-12-10 08:42:20 +00001266 }
Christoph Hellwiga206c812010-12-10 08:42:20 +00001267
1268 trace_xfs_get_blocks_alloc(ip, offset, size, 0, &imap);
1269 } else if (nimaps) {
1270 trace_xfs_get_blocks_found(ip, offset, size, 0, &imap);
Dave Chinner507630b2012-03-27 10:34:50 -04001271 xfs_iunlock(ip, lockmode);
Christoph Hellwiga206c812010-12-10 08:42:20 +00001272 } else {
1273 trace_xfs_get_blocks_notfound(ip, offset, size);
1274 goto out_unlock;
1275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
Christoph Hellwig207d0412010-04-28 12:28:56 +00001277 if (imap.br_startblock != HOLESTARTBLOCK &&
1278 imap.br_startblock != DELAYSTARTBLOCK) {
Nathan Scott87cbc492006-03-14 13:26:43 +11001279 /*
1280 * For unwritten extents do not report a disk address on
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 * the read case (treat as if we're reading into a hole).
1282 */
Christoph Hellwig207d0412010-04-28 12:28:56 +00001283 if (create || !ISUNWRITTEN(&imap))
1284 xfs_map_buffer(inode, bh_result, &imap, offset);
1285 if (create && ISUNWRITTEN(&imap)) {
Christoph Hellwig7b7a8662013-09-04 15:04:39 +02001286 if (direct) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 bh_result->b_private = inode;
Christoph Hellwig7b7a8662013-09-04 15:04:39 +02001288 set_buffer_defer_completion(bh_result);
1289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 set_buffer_unwritten(bh_result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 }
1292 }
1293
Nathan Scottc2536662006-03-29 10:44:40 +10001294 /*
1295 * If this is a realtime file, data may be on a different device.
1296 * to that pointed to from the buffer_head b_bdev currently.
1297 */
Christoph Hellwig046f1682010-04-28 12:28:52 +00001298 bh_result->b_bdev = xfs_find_bdev_for_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Nathan Scottc2536662006-03-29 10:44:40 +10001300 /*
David Chinner549054a2007-02-10 18:36:35 +11001301 * If we previously allocated a block out beyond eof and we are now
1302 * coming back to use it then we will need to flag it as new even if it
1303 * has a disk address.
1304 *
1305 * With sub-block writes into unwritten extents we also need to mark
1306 * the buffer as new so that the unwritten parts of the buffer gets
1307 * correctly zeroed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 */
1309 if (create &&
1310 ((!buffer_mapped(bh_result) && !buffer_uptodate(bh_result)) ||
David Chinner549054a2007-02-10 18:36:35 +11001311 (offset >= i_size_read(inode)) ||
Christoph Hellwig207d0412010-04-28 12:28:56 +00001312 (new || ISUNWRITTEN(&imap))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 set_buffer_new(bh_result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
Christoph Hellwig207d0412010-04-28 12:28:56 +00001315 if (imap.br_startblock == DELAYSTARTBLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 BUG_ON(direct);
1317 if (create) {
1318 set_buffer_uptodate(bh_result);
1319 set_buffer_mapped(bh_result);
1320 set_buffer_delay(bh_result);
1321 }
1322 }
1323
Christoph Hellwig2b8f12b2010-04-28 12:28:59 +00001324 /*
1325 * If this is O_DIRECT or the mpage code calling tell them how large
1326 * the mapping is, so that we can avoid repeated get_blocks calls.
1327 */
Nathan Scottc2536662006-03-29 10:44:40 +10001328 if (direct || size > (1 << inode->i_blkbits)) {
Christoph Hellwig2b8f12b2010-04-28 12:28:59 +00001329 xfs_off_t mapping_size;
Christoph Hellwig9563b3d2010-04-28 12:28:53 +00001330
Christoph Hellwig2b8f12b2010-04-28 12:28:59 +00001331 mapping_size = imap.br_startoff + imap.br_blockcount - iblock;
1332 mapping_size <<= inode->i_blkbits;
1333
1334 ASSERT(mapping_size > 0);
1335 if (mapping_size > size)
1336 mapping_size = size;
1337 if (mapping_size > LONG_MAX)
1338 mapping_size = LONG_MAX;
1339
1340 bh_result->b_size = mapping_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 }
1342
1343 return 0;
Christoph Hellwiga206c812010-12-10 08:42:20 +00001344
1345out_unlock:
1346 xfs_iunlock(ip, lockmode);
1347 return -error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348}
1349
1350int
Nathan Scottc2536662006-03-29 10:44:40 +10001351xfs_get_blocks(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 struct inode *inode,
1353 sector_t iblock,
1354 struct buffer_head *bh_result,
1355 int create)
1356{
Christoph Hellwigf2bde9b2010-06-24 11:44:35 +10001357 return __xfs_get_blocks(inode, iblock, bh_result, create, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358}
1359
1360STATIC int
Nathan Scotte4c573b2006-03-14 13:54:26 +11001361xfs_get_blocks_direct(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 struct inode *inode,
1363 sector_t iblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 struct buffer_head *bh_result,
1365 int create)
1366{
Christoph Hellwigf2bde9b2010-06-24 11:44:35 +10001367 return __xfs_get_blocks(inode, iblock, bh_result, create, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368}
1369
Christoph Hellwig209fb872010-07-18 21:17:11 +00001370/*
1371 * Complete a direct I/O write request.
1372 *
1373 * If the private argument is non-NULL __xfs_get_blocks signals us that we
1374 * need to issue a transaction to convert the range from unwritten to written
1375 * extents. In case this is regular synchronous I/O we just call xfs_end_io
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001376 * to do this and we are done. But in case this was a successful AIO
Christoph Hellwig209fb872010-07-18 21:17:11 +00001377 * request this handler is called from interrupt context, from which we
1378 * can't start transactions. In that case offload the I/O completion to
1379 * the workqueues we also use for buffered I/O completion.
1380 */
Christoph Hellwigf0973862005-09-05 08:22:52 +10001381STATIC void
Christoph Hellwig209fb872010-07-18 21:17:11 +00001382xfs_end_io_direct_write(
1383 struct kiocb *iocb,
1384 loff_t offset,
1385 ssize_t size,
Christoph Hellwig7b7a8662013-09-04 15:04:39 +02001386 void *private)
Christoph Hellwigf0973862005-09-05 08:22:52 +10001387{
Christoph Hellwig209fb872010-07-18 21:17:11 +00001388 struct xfs_ioend *ioend = iocb->private;
Christoph Hellwigf0973862005-09-05 08:22:52 +10001389
1390 /*
Christoph Hellwig2813d682011-12-18 20:00:12 +00001391 * While the generic direct I/O code updates the inode size, it does
1392 * so only after the end_io handler is called, which means our
1393 * end_io handler thinks the on-disk size is outside the in-core
1394 * size. To prevent this just update it a little bit earlier here.
1395 */
1396 if (offset + size > i_size_read(ioend->io_inode))
1397 i_size_write(ioend->io_inode, offset + size);
1398
1399 /*
Nathan Scottc41564b2006-03-29 08:55:14 +10001400 * blockdev_direct_IO can return an error even after the I/O
Christoph Hellwigf0973862005-09-05 08:22:52 +10001401 * completion handler was called. Thus we need to protect
1402 * against double-freeing.
1403 */
1404 iocb->private = NULL;
Christoph Hellwig40e2e972010-07-18 21:17:09 +00001405
Christoph Hellwig209fb872010-07-18 21:17:11 +00001406 ioend->io_offset = offset;
1407 ioend->io_size = size;
1408 if (private && size > 0)
Alain Renaud0d882a32012-05-22 15:56:21 -05001409 ioend->io_type = XFS_IO_UNWRITTEN;
Christoph Hellwig209fb872010-07-18 21:17:11 +00001410
Christoph Hellwig7b7a8662013-09-04 15:04:39 +02001411 xfs_finish_ioend_sync(ioend);
Christoph Hellwigf0973862005-09-05 08:22:52 +10001412}
1413
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414STATIC ssize_t
Nathan Scotte4c573b2006-03-14 13:54:26 +11001415xfs_vm_direct_IO(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 int rw,
1417 struct kiocb *iocb,
1418 const struct iovec *iov,
1419 loff_t offset,
1420 unsigned long nr_segs)
1421{
Christoph Hellwig209fb872010-07-18 21:17:11 +00001422 struct inode *inode = iocb->ki_filp->f_mapping->host;
1423 struct block_device *bdev = xfs_find_bdev_for_inode(inode);
Christoph Hellwig281627d2012-03-13 08:41:05 +00001424 struct xfs_ioend *ioend = NULL;
Christoph Hellwig209fb872010-07-18 21:17:11 +00001425 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
Christoph Hellwig209fb872010-07-18 21:17:11 +00001427 if (rw & WRITE) {
Christoph Hellwig281627d2012-03-13 08:41:05 +00001428 size_t size = iov_length(iov, nr_segs);
1429
1430 /*
Dave Chinner437a2552012-11-28 13:01:00 +11001431 * We cannot preallocate a size update transaction here as we
1432 * don't know whether allocation is necessary or not. Hence we
1433 * can only tell IO completion that one is necessary if we are
1434 * not doing unwritten extent conversion.
Christoph Hellwig281627d2012-03-13 08:41:05 +00001435 */
Alain Renaud0d882a32012-05-22 15:56:21 -05001436 iocb->private = ioend = xfs_alloc_ioend(inode, XFS_IO_DIRECT);
Dave Chinner437a2552012-11-28 13:01:00 +11001437 if (offset + size > XFS_I(inode)->i_d.di_size)
Christoph Hellwig281627d2012-03-13 08:41:05 +00001438 ioend->io_isdirect = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
Christoph Hellwigeafdc7d2010-06-04 11:29:53 +02001440 ret = __blockdev_direct_IO(rw, iocb, inode, bdev, iov,
1441 offset, nr_segs,
1442 xfs_get_blocks_direct,
1443 xfs_end_io_direct_write, NULL, 0);
Christoph Hellwig209fb872010-07-18 21:17:11 +00001444 if (ret != -EIOCBQUEUED && iocb->private)
Dave Chinner437a2552012-11-28 13:01:00 +11001445 goto out_destroy_ioend;
Christoph Hellwig209fb872010-07-18 21:17:11 +00001446 } else {
Christoph Hellwigeafdc7d2010-06-04 11:29:53 +02001447 ret = __blockdev_direct_IO(rw, iocb, inode, bdev, iov,
1448 offset, nr_segs,
1449 xfs_get_blocks_direct,
1450 NULL, NULL, 0);
Christoph Hellwig209fb872010-07-18 21:17:11 +00001451 }
Christoph Hellwig5fe878ae2009-12-15 16:47:50 -08001452
Christoph Hellwigf0973862005-09-05 08:22:52 +10001453 return ret;
Christoph Hellwig281627d2012-03-13 08:41:05 +00001454
Christoph Hellwig281627d2012-03-13 08:41:05 +00001455out_destroy_ioend:
1456 xfs_destroy_ioend(ioend);
1457 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458}
1459
Dave Chinnerd3bc8152012-04-27 19:45:21 +10001460/*
1461 * Punch out the delalloc blocks we have already allocated.
1462 *
1463 * Don't bother with xfs_setattr given that nothing can have made it to disk yet
1464 * as the page is still locked at this point.
1465 */
Christoph Hellwigfa9b2272010-06-14 05:17:31 -04001466STATIC void
Dave Chinnerd3bc8152012-04-27 19:45:21 +10001467xfs_vm_kill_delalloc_range(
1468 struct inode *inode,
1469 loff_t start,
1470 loff_t end)
Christoph Hellwigfa9b2272010-06-14 05:17:31 -04001471{
Dave Chinnerd3bc8152012-04-27 19:45:21 +10001472 struct xfs_inode *ip = XFS_I(inode);
1473 xfs_fileoff_t start_fsb;
1474 xfs_fileoff_t end_fsb;
1475 int error;
Christoph Hellwigfa9b2272010-06-14 05:17:31 -04001476
Dave Chinnerd3bc8152012-04-27 19:45:21 +10001477 start_fsb = XFS_B_TO_FSB(ip->i_mount, start);
1478 end_fsb = XFS_B_TO_FSB(ip->i_mount, end);
1479 if (end_fsb <= start_fsb)
1480 return;
Dave Chinnerc726de42010-11-30 15:14:39 +11001481
Dave Chinnerd3bc8152012-04-27 19:45:21 +10001482 xfs_ilock(ip, XFS_ILOCK_EXCL);
1483 error = xfs_bmap_punch_delalloc_range(ip, start_fsb,
1484 end_fsb - start_fsb);
1485 if (error) {
1486 /* something screwed, just bail */
1487 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
1488 xfs_alert(ip->i_mount,
1489 "xfs_vm_write_failed: unable to clean up ino %lld",
1490 ip->i_ino);
Dave Chinnerc726de42010-11-30 15:14:39 +11001491 }
Christoph Hellwigfa9b2272010-06-14 05:17:31 -04001492 }
Dave Chinnerd3bc8152012-04-27 19:45:21 +10001493 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Christoph Hellwigfa9b2272010-06-14 05:17:31 -04001494}
1495
Dave Chinnerd3bc8152012-04-27 19:45:21 +10001496STATIC void
1497xfs_vm_write_failed(
1498 struct inode *inode,
1499 struct page *page,
1500 loff_t pos,
1501 unsigned len)
1502{
Jie Liu58e59852013-07-16 13:11:16 +08001503 loff_t block_offset;
Dave Chinnerd3bc8152012-04-27 19:45:21 +10001504 loff_t block_start;
1505 loff_t block_end;
1506 loff_t from = pos & (PAGE_CACHE_SIZE - 1);
1507 loff_t to = from + len;
1508 struct buffer_head *bh, *head;
1509
Jie Liu58e59852013-07-16 13:11:16 +08001510 /*
1511 * The request pos offset might be 32 or 64 bit, this is all fine
1512 * on 64-bit platform. However, for 64-bit pos request on 32-bit
1513 * platform, the high 32-bit will be masked off if we evaluate the
1514 * block_offset via (pos & PAGE_MASK) because the PAGE_MASK is
1515 * 0xfffff000 as an unsigned long, hence the result is incorrect
1516 * which could cause the following ASSERT failed in most cases.
1517 * In order to avoid this, we can evaluate the block_offset of the
1518 * start of the page by using shifts rather than masks the mismatch
1519 * problem.
1520 */
1521 block_offset = (pos >> PAGE_CACHE_SHIFT) << PAGE_CACHE_SHIFT;
1522
Dave Chinnerd3bc8152012-04-27 19:45:21 +10001523 ASSERT(block_offset + from == pos);
1524
1525 head = page_buffers(page);
1526 block_start = 0;
1527 for (bh = head; bh != head || !block_start;
1528 bh = bh->b_this_page, block_start = block_end,
1529 block_offset += bh->b_size) {
1530 block_end = block_start + bh->b_size;
1531
1532 /* skip buffers before the write */
1533 if (block_end <= from)
1534 continue;
1535
1536 /* if the buffer is after the write, we're done */
1537 if (block_start >= to)
1538 break;
1539
1540 if (!buffer_delay(bh))
1541 continue;
1542
1543 if (!buffer_new(bh) && block_offset < i_size_read(inode))
1544 continue;
1545
1546 xfs_vm_kill_delalloc_range(inode, block_offset,
1547 block_offset + bh->b_size);
1548 }
1549
1550}
1551
1552/*
1553 * This used to call block_write_begin(), but it unlocks and releases the page
1554 * on error, and we need that page to be able to punch stale delalloc blocks out
1555 * on failure. hence we copy-n-waste it here and call xfs_vm_write_failed() at
1556 * the appropriate point.
1557 */
Nathan Scottf51623b2006-03-14 13:26:27 +11001558STATIC int
Nick Piggind79689c2007-10-16 01:25:06 -07001559xfs_vm_write_begin(
Nathan Scottf51623b2006-03-14 13:26:27 +11001560 struct file *file,
Nick Piggind79689c2007-10-16 01:25:06 -07001561 struct address_space *mapping,
1562 loff_t pos,
1563 unsigned len,
1564 unsigned flags,
1565 struct page **pagep,
1566 void **fsdata)
Nathan Scottf51623b2006-03-14 13:26:27 +11001567{
Dave Chinnerd3bc8152012-04-27 19:45:21 +10001568 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
1569 struct page *page;
1570 int status;
Christoph Hellwig155130a2010-06-04 11:29:58 +02001571
Dave Chinnerd3bc8152012-04-27 19:45:21 +10001572 ASSERT(len <= PAGE_CACHE_SIZE);
1573
1574 page = grab_cache_page_write_begin(mapping, index,
1575 flags | AOP_FLAG_NOFS);
1576 if (!page)
1577 return -ENOMEM;
1578
1579 status = __block_write_begin(page, pos, len, xfs_get_blocks);
1580 if (unlikely(status)) {
1581 struct inode *inode = mapping->host;
1582
1583 xfs_vm_write_failed(inode, page, pos, len);
1584 unlock_page(page);
1585
1586 if (pos + len > i_size_read(inode))
Kirill A. Shutemov7caef262013-09-12 15:13:56 -07001587 truncate_pagecache(inode, i_size_read(inode));
Dave Chinnerd3bc8152012-04-27 19:45:21 +10001588
1589 page_cache_release(page);
1590 page = NULL;
1591 }
1592
1593 *pagep = page;
1594 return status;
Christoph Hellwigfa9b2272010-06-14 05:17:31 -04001595}
Christoph Hellwig155130a2010-06-04 11:29:58 +02001596
Dave Chinnerd3bc8152012-04-27 19:45:21 +10001597/*
1598 * On failure, we only need to kill delalloc blocks beyond EOF because they
1599 * will never be written. For blocks within EOF, generic_write_end() zeros them
1600 * so they are safe to leave alone and be written with all the other valid data.
1601 */
Christoph Hellwigfa9b2272010-06-14 05:17:31 -04001602STATIC int
1603xfs_vm_write_end(
1604 struct file *file,
1605 struct address_space *mapping,
1606 loff_t pos,
1607 unsigned len,
1608 unsigned copied,
1609 struct page *page,
1610 void *fsdata)
1611{
1612 int ret;
1613
Dave Chinnerd3bc8152012-04-27 19:45:21 +10001614 ASSERT(len <= PAGE_CACHE_SIZE);
1615
Christoph Hellwigfa9b2272010-06-14 05:17:31 -04001616 ret = generic_write_end(file, mapping, pos, len, copied, page, fsdata);
Dave Chinnerd3bc8152012-04-27 19:45:21 +10001617 if (unlikely(ret < len)) {
1618 struct inode *inode = mapping->host;
1619 size_t isize = i_size_read(inode);
1620 loff_t to = pos + len;
1621
1622 if (to > isize) {
Kirill A. Shutemov7caef262013-09-12 15:13:56 -07001623 truncate_pagecache(inode, isize);
Dave Chinnerd3bc8152012-04-27 19:45:21 +10001624 xfs_vm_kill_delalloc_range(inode, isize, to);
1625 }
1626 }
Christoph Hellwig155130a2010-06-04 11:29:58 +02001627 return ret;
Nathan Scottf51623b2006-03-14 13:26:27 +11001628}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
1630STATIC sector_t
Nathan Scotte4c573b2006-03-14 13:54:26 +11001631xfs_vm_bmap(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 struct address_space *mapping,
1633 sector_t block)
1634{
1635 struct inode *inode = (struct inode *)mapping->host;
Christoph Hellwig739bfb22007-08-29 10:58:01 +10001636 struct xfs_inode *ip = XFS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
Christoph Hellwigcca28fb2010-06-24 11:57:09 +10001638 trace_xfs_vm_bmap(XFS_I(inode));
Christoph Hellwig126468b2008-03-06 13:44:57 +11001639 xfs_ilock(ip, XFS_IOLOCK_SHARED);
Dave Chinner4bc1ea62012-11-12 22:53:56 +11001640 filemap_write_and_wait(mapping);
Christoph Hellwig126468b2008-03-06 13:44:57 +11001641 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
Nathan Scottc2536662006-03-29 10:44:40 +10001642 return generic_block_bmap(mapping, block, xfs_get_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643}
1644
1645STATIC int
Nathan Scotte4c573b2006-03-14 13:54:26 +11001646xfs_vm_readpage(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 struct file *unused,
1648 struct page *page)
1649{
Nathan Scottc2536662006-03-29 10:44:40 +10001650 return mpage_readpage(page, xfs_get_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651}
1652
1653STATIC int
Nathan Scotte4c573b2006-03-14 13:54:26 +11001654xfs_vm_readpages(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 struct file *unused,
1656 struct address_space *mapping,
1657 struct list_head *pages,
1658 unsigned nr_pages)
1659{
Nathan Scottc2536662006-03-29 10:44:40 +10001660 return mpage_readpages(mapping, pages, nr_pages, xfs_get_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661}
1662
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07001663const struct address_space_operations xfs_address_space_operations = {
Nathan Scotte4c573b2006-03-14 13:54:26 +11001664 .readpage = xfs_vm_readpage,
1665 .readpages = xfs_vm_readpages,
1666 .writepage = xfs_vm_writepage,
Nathan Scott7d4fb402006-06-09 15:27:16 +10001667 .writepages = xfs_vm_writepages,
Nathan Scott238f4c52006-03-17 17:26:25 +11001668 .releasepage = xfs_vm_releasepage,
1669 .invalidatepage = xfs_vm_invalidatepage,
Nick Piggind79689c2007-10-16 01:25:06 -07001670 .write_begin = xfs_vm_write_begin,
Christoph Hellwigfa9b2272010-06-14 05:17:31 -04001671 .write_end = xfs_vm_write_end,
Nathan Scotte4c573b2006-03-14 13:54:26 +11001672 .bmap = xfs_vm_bmap,
1673 .direct_IO = xfs_vm_direct_IO,
Christoph Lametere965f962006-02-01 03:05:41 -08001674 .migratepage = buffer_migrate_page,
Hisashi Hifumibddaafa2009-03-29 09:53:38 +02001675 .is_partially_uptodate = block_is_partially_uptodate,
Andi Kleenaa261f52009-09-16 11:50:16 +02001676 .error_remove_page = generic_error_remove_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677};