blob: 6f5c95f94add8d7e122b1ab5a9706102ae22c383 [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_shared.h"
Dave Chinner239880e2013-10-23 10:50:10 +110020#include "xfs_format.h"
21#include "xfs_log_format.h"
22#include "xfs_trans_resv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_inode.h"
Dave Chinner239880e2013-10-23 10:50:10 +110025#include "xfs_trans.h"
Christoph Hellwig281627d2012-03-13 08:41:05 +000026#include "xfs_inode_item.h"
Nathan Scotta844f452005-11-02 14:38:42 +110027#include "xfs_alloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_iomap.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000030#include "xfs_trace.h"
Dave Chinner3ed3a432010-03-05 02:00:42 +000031#include "xfs_bmap.h"
Dave Chinner68988112013-08-12 20:49:42 +100032#include "xfs_bmap_util.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110033#include "xfs_bmap_btree.h"
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/mpage.h>
Christoph Hellwig10ce4442006-01-11 20:48:14 +110036#include <linux/pagevec.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/writeback.h>
38
Dave Chinnerfbcc0252016-02-15 17:21:19 +110039/*
40 * structure owned by writepages passed to individual writepage calls
41 */
42struct xfs_writepage_ctx {
43 struct xfs_bmbt_irec imap;
44 bool imap_valid;
45 unsigned int io_type;
46 struct xfs_ioend *iohead;
47 struct xfs_ioend *ioend;
48 sector_t last_block;
49};
50
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000051void
Nathan Scottf51623b2006-03-14 13:26:27 +110052xfs_count_page_state(
53 struct page *page,
54 int *delalloc,
Nathan Scottf51623b2006-03-14 13:26:27 +110055 int *unwritten)
56{
57 struct buffer_head *bh, *head;
58
Christoph Hellwig20cb52e2010-06-24 09:46:01 +100059 *delalloc = *unwritten = 0;
Nathan Scottf51623b2006-03-14 13:26:27 +110060
61 bh = head = page_buffers(page);
62 do {
Christoph Hellwig20cb52e2010-06-24 09:46:01 +100063 if (buffer_unwritten(bh))
Nathan Scottf51623b2006-03-14 13:26:27 +110064 (*unwritten) = 1;
65 else if (buffer_delay(bh))
66 (*delalloc) = 1;
67 } while ((bh = bh->b_this_page) != head);
68}
69
Christoph Hellwig6214ed42007-09-14 15:23:17 +100070STATIC struct block_device *
71xfs_find_bdev_for_inode(
Christoph Hellwig046f1682010-04-28 12:28:52 +000072 struct inode *inode)
Christoph Hellwig6214ed42007-09-14 15:23:17 +100073{
Christoph Hellwig046f1682010-04-28 12:28:52 +000074 struct xfs_inode *ip = XFS_I(inode);
Christoph Hellwig6214ed42007-09-14 15:23:17 +100075 struct xfs_mount *mp = ip->i_mount;
76
Eric Sandeen71ddabb2007-11-23 16:29:42 +110077 if (XFS_IS_REALTIME_INODE(ip))
Christoph Hellwig6214ed42007-09-14 15:23:17 +100078 return mp->m_rtdev_targp->bt_bdev;
79 else
80 return mp->m_ddev_targp->bt_bdev;
81}
82
Christoph Hellwig0829c362005-09-02 16:58:49 +100083/*
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +110084 * We're now finished for good with this ioend structure.
85 * Update the page state via the associated buffer_heads,
86 * release holds on the inode and bio, and finally free
87 * up memory. Do not use the ioend after this.
88 */
Christoph Hellwig0829c362005-09-02 16:58:49 +100089STATIC void
90xfs_destroy_ioend(
91 xfs_ioend_t *ioend)
92{
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +110093 struct buffer_head *bh, *next;
94
95 for (bh = ioend->io_buffer_head; bh; bh = next) {
96 next = bh->b_private;
Nathan Scott7d04a332006-06-09 14:58:38 +100097 bh->b_end_io(bh, !ioend->io_error);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +110098 }
Christoph Hellwig583fa582008-12-03 12:20:38 +010099
Christoph Hellwig0829c362005-09-02 16:58:49 +1000100 mempool_free(ioend, xfs_ioend_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101}
102
103/*
Christoph Hellwigfc0063c2011-08-23 08:28:11 +0000104 * Fast and loose check if this write could update the on-disk inode size.
105 */
106static inline bool xfs_ioend_is_append(struct xfs_ioend *ioend)
107{
108 return ioend->io_offset + ioend->io_size >
109 XFS_I(ioend->io_inode)->i_d.di_size;
110}
111
Christoph Hellwig281627d2012-03-13 08:41:05 +0000112STATIC int
113xfs_setfilesize_trans_alloc(
114 struct xfs_ioend *ioend)
115{
116 struct xfs_mount *mp = XFS_I(ioend->io_inode)->i_mount;
117 struct xfs_trans *tp;
118 int error;
119
120 tp = xfs_trans_alloc(mp, XFS_TRANS_FSYNC_TS);
121
Jie Liu3d3c8b52013-08-12 20:49:59 +1000122 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_fsyncts, 0, 0);
Christoph Hellwig281627d2012-03-13 08:41:05 +0000123 if (error) {
Christoph Hellwig4906e212015-06-04 13:47:56 +1000124 xfs_trans_cancel(tp);
Christoph Hellwig281627d2012-03-13 08:41:05 +0000125 return error;
126 }
127
128 ioend->io_append_trans = tp;
129
130 /*
Dave Chinner437a2552012-11-28 13:01:00 +1100131 * We may pass freeze protection with a transaction. So tell lockdep
Jan Karad9457dc2012-06-12 16:20:39 +0200132 * we released it.
133 */
Oleg Nesterovbee91822015-07-19 23:48:20 +0200134 __sb_writers_release(ioend->io_inode->i_sb, SB_FREEZE_FS);
Jan Karad9457dc2012-06-12 16:20:39 +0200135 /*
Christoph Hellwig281627d2012-03-13 08:41:05 +0000136 * We hand off the transaction to the completion thread now, so
137 * clear the flag here.
138 */
139 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
140 return 0;
141}
142
Christoph Hellwigfc0063c2011-08-23 08:28:11 +0000143/*
Christoph Hellwig2813d682011-12-18 20:00:12 +0000144 * Update on-disk file size now that data has been written to disk.
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000145 */
Christoph Hellwig281627d2012-03-13 08:41:05 +0000146STATIC int
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000147xfs_setfilesize(
Christoph Hellwig2ba66232015-02-02 10:02:09 +1100148 struct xfs_inode *ip,
149 struct xfs_trans *tp,
150 xfs_off_t offset,
151 size_t size)
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000152{
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000153 xfs_fsize_t isize;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000154
Christoph Hellwigaa6bf012012-02-29 09:53:48 +0000155 xfs_ilock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig2ba66232015-02-02 10:02:09 +1100156 isize = xfs_new_eof(ip, offset + size);
Christoph Hellwig281627d2012-03-13 08:41:05 +0000157 if (!isize) {
158 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Christoph Hellwig4906e212015-06-04 13:47:56 +1000159 xfs_trans_cancel(tp);
Christoph Hellwig281627d2012-03-13 08:41:05 +0000160 return 0;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000161 }
162
Christoph Hellwig2ba66232015-02-02 10:02:09 +1100163 trace_xfs_setfilesize(ip, offset, size);
Christoph Hellwig281627d2012-03-13 08:41:05 +0000164
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
Christoph Hellwig70393312015-06-04 13:48:08 +1000169 return xfs_trans_commit(tp);
Christoph Hellwig0829c362005-09-02 16:58:49 +1000170}
171
Christoph Hellwig2ba66232015-02-02 10:02:09 +1100172STATIC int
173xfs_setfilesize_ioend(
174 struct xfs_ioend *ioend)
175{
176 struct xfs_inode *ip = XFS_I(ioend->io_inode);
177 struct xfs_trans *tp = ioend->io_append_trans;
178
179 /*
180 * The transaction may have been allocated in the I/O submission thread,
181 * thus we need to mark ourselves as being in a transaction manually.
182 * Similarly for freeze protection.
183 */
184 current_set_flags_nested(&tp->t_pflags, PF_FSTRANS);
Oleg Nesterovbee91822015-07-19 23:48:20 +0200185 __sb_writers_acquired(VFS_I(ip)->i_sb, SB_FREEZE_FS);
Christoph Hellwig2ba66232015-02-02 10:02:09 +1100186
Zhaohongjiang5cb13dc2015-10-12 15:28:39 +1100187 /* we abort the update if there was an IO error */
188 if (ioend->io_error) {
189 xfs_trans_cancel(tp);
190 return ioend->io_error;
191 }
192
Christoph Hellwig2ba66232015-02-02 10:02:09 +1100193 return xfs_setfilesize(ip, tp, ioend->io_offset, ioend->io_size);
194}
195
Christoph Hellwig0829c362005-09-02 16:58:49 +1000196/*
Christoph Hellwig209fb872010-07-18 21:17:11 +0000197 * Schedule IO completion handling on the final put of an ioend.
Christoph Hellwigfc0063c2011-08-23 08:28:11 +0000198 *
199 * If there is no work to do we might as well call it a day and free the
200 * ioend right now.
Dave Chinnerc626d172009-04-06 18:42:11 +0200201 */
202STATIC void
203xfs_finish_ioend(
Christoph Hellwig209fb872010-07-18 21:17:11 +0000204 struct xfs_ioend *ioend)
Dave Chinnerc626d172009-04-06 18:42:11 +0200205{
206 if (atomic_dec_and_test(&ioend->io_remaining)) {
Christoph Hellwigaa6bf012012-02-29 09:53:48 +0000207 struct xfs_mount *mp = XFS_I(ioend->io_inode)->i_mount;
208
Alain Renaud0d882a32012-05-22 15:56:21 -0500209 if (ioend->io_type == XFS_IO_UNWRITTEN)
Christoph Hellwigaa6bf012012-02-29 09:53:48 +0000210 queue_work(mp->m_unwritten_workqueue, &ioend->io_work);
Christoph Hellwig2ba66232015-02-02 10:02:09 +1100211 else if (ioend->io_append_trans)
Christoph Hellwigaa6bf012012-02-29 09:53:48 +0000212 queue_work(mp->m_data_workqueue, &ioend->io_work);
Christoph Hellwigfc0063c2011-08-23 08:28:11 +0000213 else
214 xfs_destroy_ioend(ioend);
Dave Chinnerc626d172009-04-06 18:42:11 +0200215 }
216}
217
218/*
Dave Chinner77d7a0c2010-02-17 05:36:29 +0000219 * IO write completion.
220 */
221STATIC void
222xfs_end_io(
223 struct work_struct *work)
224{
225 xfs_ioend_t *ioend = container_of(work, xfs_ioend_t, io_work);
226 struct xfs_inode *ip = XFS_I(ioend->io_inode);
Dave Chinner69418932010-03-04 00:57:09 +0000227 int error = 0;
Dave Chinner77d7a0c2010-02-17 05:36:29 +0000228
Christoph Hellwig04f658e2011-08-24 05:59:25 +0000229 if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
Christoph Hellwig810627d2011-11-08 08:56:15 +0000230 ioend->io_error = -EIO;
Christoph Hellwig04f658e2011-08-24 05:59:25 +0000231 goto done;
232 }
Christoph Hellwig04f658e2011-08-24 05:59:25 +0000233
Dave Chinner77d7a0c2010-02-17 05:36:29 +0000234 /*
235 * For unwritten extents we need to issue transactions to convert a
236 * range to normal written extens after the data I/O has finished.
Zhaohongjiang5cb13dc2015-10-12 15:28:39 +1100237 * Detecting and handling completion IO errors is done individually
238 * for each case as different cleanup operations need to be performed
239 * on error.
Dave Chinner77d7a0c2010-02-17 05:36:29 +0000240 */
Alain Renaud0d882a32012-05-22 15:56:21 -0500241 if (ioend->io_type == XFS_IO_UNWRITTEN) {
Zhaohongjiang5cb13dc2015-10-12 15:28:39 +1100242 if (ioend->io_error)
243 goto done;
Dave Chinner77d7a0c2010-02-17 05:36:29 +0000244 error = xfs_iomap_write_unwritten(ip, ioend->io_offset,
Dave Chinner437a2552012-11-28 13:01:00 +1100245 ioend->io_size);
Christoph Hellwig281627d2012-03-13 08:41:05 +0000246 } else if (ioend->io_append_trans) {
Christoph Hellwig2ba66232015-02-02 10:02:09 +1100247 error = xfs_setfilesize_ioend(ioend);
Christoph Hellwig84803fb2012-02-29 09:53:50 +0000248 } else {
Christoph Hellwig281627d2012-03-13 08:41:05 +0000249 ASSERT(!xfs_ioend_is_append(ioend));
Dave Chinner77d7a0c2010-02-17 05:36:29 +0000250 }
251
Christoph Hellwig04f658e2011-08-24 05:59:25 +0000252done:
Dave Chinner437a2552012-11-28 13:01:00 +1100253 if (error)
Dave Chinner24513372014-06-25 14:58:08 +1000254 ioend->io_error = error;
Christoph Hellwigaa6bf012012-02-29 09:53:48 +0000255 xfs_destroy_ioend(ioend);
Dave Chinner77d7a0c2010-02-17 05:36:29 +0000256}
257
258/*
Christoph Hellwig0829c362005-09-02 16:58:49 +1000259 * Allocate and initialise an IO completion structure.
260 * We need to track unwritten extent write completion here initially.
261 * We'll need to extend this for updating the ondisk inode size later
262 * (vs. incore size).
263 */
264STATIC xfs_ioend_t *
265xfs_alloc_ioend(
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100266 struct inode *inode,
267 unsigned int type)
Christoph Hellwig0829c362005-09-02 16:58:49 +1000268{
269 xfs_ioend_t *ioend;
270
271 ioend = mempool_alloc(xfs_ioend_pool, GFP_NOFS);
272
273 /*
274 * Set the count to 1 initially, which will prevent an I/O
275 * completion callback from happening before we have started
276 * all the I/O from calling the completion routine too early.
277 */
278 atomic_set(&ioend->io_remaining, 1);
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,
Dave Chinner988ef922016-02-15 17:20:50 +1100298 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
Christoph Hellwiga206c812010-12-10 08:42:20 +0000300 struct xfs_inode *ip = XFS_I(inode);
301 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwiged1e7b72010-12-10 08:42:22 +0000302 ssize_t count = 1 << inode->i_blkbits;
Christoph Hellwiga206c812010-12-10 08:42:20 +0000303 xfs_fileoff_t offset_fsb, end_fsb;
304 int error = 0;
Christoph Hellwiga206c812010-12-10 08:42:20 +0000305 int bmapi_flags = XFS_BMAPI_ENTIRE;
306 int nimaps = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Christoph Hellwiga206c812010-12-10 08:42:20 +0000308 if (XFS_FORCED_SHUTDOWN(mp))
Eric Sandeenb474c7a2014-06-22 15:04:54 +1000309 return -EIO;
Christoph Hellwiga206c812010-12-10 08:42:20 +0000310
Alain Renaud0d882a32012-05-22 15:56:21 -0500311 if (type == XFS_IO_UNWRITTEN)
Christoph Hellwiga206c812010-12-10 08:42:20 +0000312 bmapi_flags |= XFS_BMAPI_IGSTATE;
Christoph Hellwiga206c812010-12-10 08:42:20 +0000313
Dave Chinner988ef922016-02-15 17:20:50 +1100314 xfs_ilock(ip, XFS_ILOCK_SHARED);
Christoph Hellwig8ff29572010-12-10 08:42:21 +0000315 ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
316 (ip->i_df.if_flags & XFS_IFEXTENTS));
Dave Chinnerd2c28192012-06-08 15:44:53 +1000317 ASSERT(offset <= mp->m_super->s_maxbytes);
Christoph Hellwig8ff29572010-12-10 08:42:21 +0000318
Dave Chinnerd2c28192012-06-08 15:44:53 +1000319 if (offset + count > mp->m_super->s_maxbytes)
320 count = mp->m_super->s_maxbytes - offset;
Christoph Hellwiga206c812010-12-10 08:42:20 +0000321 end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
322 offset_fsb = XFS_B_TO_FSBT(mp, offset);
Dave Chinner5c8ed202011-09-18 20:40:45 +0000323 error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb,
324 imap, &nimaps, bmapi_flags);
Christoph Hellwig8ff29572010-12-10 08:42:21 +0000325 xfs_iunlock(ip, XFS_ILOCK_SHARED);
326
Christoph Hellwiga206c812010-12-10 08:42:20 +0000327 if (error)
Dave Chinner24513372014-06-25 14:58:08 +1000328 return error;
Christoph Hellwiga206c812010-12-10 08:42:20 +0000329
Alain Renaud0d882a32012-05-22 15:56:21 -0500330 if (type == XFS_IO_DELALLOC &&
Christoph Hellwig8ff29572010-12-10 08:42:21 +0000331 (!nimaps || isnullstartblock(imap->br_startblock))) {
Jie Liu0799a3e2013-09-29 18:56:04 +0800332 error = xfs_iomap_write_allocate(ip, offset, imap);
Christoph Hellwiga206c812010-12-10 08:42:20 +0000333 if (!error)
334 trace_xfs_map_blocks_alloc(ip, offset, count, type, imap);
Dave Chinner24513372014-06-25 14:58:08 +1000335 return error;
Christoph Hellwiga206c812010-12-10 08:42:20 +0000336 }
337
Christoph Hellwig8ff29572010-12-10 08:42:21 +0000338#ifdef DEBUG
Alain Renaud0d882a32012-05-22 15:56:21 -0500339 if (type == XFS_IO_UNWRITTEN) {
Christoph Hellwig8ff29572010-12-10 08:42:21 +0000340 ASSERT(nimaps);
341 ASSERT(imap->br_startblock != HOLESTARTBLOCK);
342 ASSERT(imap->br_startblock != DELAYSTARTBLOCK);
343 }
344#endif
345 if (nimaps)
346 trace_xfs_map_blocks_found(ip, offset, count, type, imap);
347 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348}
349
Dave Chinnerfbcc0252016-02-15 17:21:19 +1100350STATIC bool
Christoph Hellwig558e6892010-04-28 12:28:58 +0000351xfs_imap_valid(
Christoph Hellwig8699bb02010-04-28 12:28:54 +0000352 struct inode *inode,
Christoph Hellwig207d0412010-04-28 12:28:56 +0000353 struct xfs_bmbt_irec *imap,
Christoph Hellwig558e6892010-04-28 12:28:58 +0000354 xfs_off_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
Christoph Hellwig558e6892010-04-28 12:28:58 +0000356 offset >>= inode->i_blkbits;
Christoph Hellwig8699bb02010-04-28 12:28:54 +0000357
Christoph Hellwig558e6892010-04-28 12:28:58 +0000358 return offset >= imap->br_startoff &&
359 offset < imap->br_startoff + imap->br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360}
361
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100362/*
363 * BIO completion handler for buffered IO.
364 */
Al Viro782e3b32007-10-12 07:17:47 +0100365STATIC void
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100366xfs_end_bio(
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200367 struct bio *bio)
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100368{
369 xfs_ioend_t *ioend = bio->bi_private;
370
Linus Torvalds77a78802015-09-07 13:28:32 -0700371 if (!ioend->io_error)
372 ioend->io_error = bio->bi_error;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100373
374 /* Toss bio and pass work off to an xfsdatad thread */
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100375 bio->bi_private = NULL;
376 bio->bi_end_io = NULL;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100377 bio_put(bio);
Nathan Scott7d04a332006-06-09 14:58:38 +1000378
Christoph Hellwig209fb872010-07-18 21:17:11 +0000379 xfs_finish_ioend(ioend);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100380}
381
382STATIC void
383xfs_submit_ioend_bio(
Christoph Hellwig06342cf2009-10-30 09:09:15 +0000384 struct writeback_control *wbc,
385 xfs_ioend_t *ioend,
386 struct bio *bio)
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100387{
388 atomic_inc(&ioend->io_remaining);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100389 bio->bi_private = ioend;
390 bio->bi_end_io = xfs_end_bio;
Jens Axboe721a9602011-03-09 11:56:30 +0100391 submit_bio(wbc->sync_mode == WB_SYNC_ALL ? WRITE_SYNC : WRITE, bio);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100392}
393
394STATIC struct bio *
395xfs_alloc_ioend_bio(
396 struct buffer_head *bh)
397{
Kent Overstreetb54ffb72015-05-19 14:31:01 +0200398 struct bio *bio = bio_alloc(GFP_NOIO, BIO_MAX_PAGES);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100399
400 ASSERT(bio->bi_private == NULL);
Kent Overstreet4f024f32013-10-11 15:44:27 -0700401 bio->bi_iter.bi_sector = bh->b_blocknr * (bh->b_size >> 9);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100402 bio->bi_bdev = bh->b_bdev;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100403 return bio;
404}
405
406STATIC void
407xfs_start_buffer_writeback(
408 struct buffer_head *bh)
409{
410 ASSERT(buffer_mapped(bh));
411 ASSERT(buffer_locked(bh));
412 ASSERT(!buffer_delay(bh));
413 ASSERT(!buffer_unwritten(bh));
414
415 mark_buffer_async_write(bh);
416 set_buffer_uptodate(bh);
417 clear_buffer_dirty(bh);
418}
419
420STATIC void
421xfs_start_page_writeback(
422 struct page *page,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100423 int clear_dirty,
424 int buffers)
425{
426 ASSERT(PageLocked(page));
427 ASSERT(!PageWriteback(page));
Dave Chinner0d085a52014-09-23 15:36:27 +1000428
429 /*
430 * if the page was not fully cleaned, we need to ensure that the higher
431 * layers come back to it correctly. That means we need to keep the page
432 * dirty, and for WB_SYNC_ALL writeback we need to ensure the
433 * PAGECACHE_TAG_TOWRITE index mark is not removed so another attempt to
434 * write this page in this writeback sweep will be made.
435 */
436 if (clear_dirty) {
David Chinner92132022006-12-21 10:24:01 +1100437 clear_page_dirty_for_io(page);
Dave Chinner0d085a52014-09-23 15:36:27 +1000438 set_page_writeback(page);
439 } else
440 set_page_writeback_keepwrite(page);
441
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100442 unlock_page(page);
Dave Chinner0d085a52014-09-23 15:36:27 +1000443
Fengguang Wu1f7decf2007-10-16 23:30:42 -0700444 /* If no buffers on the page are to be written, finish it here */
445 if (!buffers)
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100446 end_page_writeback(page);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100447}
448
Zhi Yong Wuc7c1a7d2013-08-07 10:11:09 +0000449static inline int xfs_bio_add_buffer(struct bio *bio, struct buffer_head *bh)
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100450{
451 return bio_add_page(bio, bh->b_page, bh->b_size, bh_offset(bh));
452}
453
454/*
David Chinnerd88992f2006-01-18 13:38:12 +1100455 * Submit all of the bios for all of the ioends we have saved up, covering the
456 * initial writepage page and also any probed pages.
457 *
458 * Because we may have multiple ioends spanning a page, we need to start
459 * writeback on all the buffers before we submit them for I/O. If we mark the
460 * buffers as we got, then we can end up with a page that only has buffers
461 * marked async write and I/O complete on can occur before we mark the other
462 * buffers async write.
463 *
464 * The end result of this is that we trip a bug in end_page_writeback() because
465 * we call it twice for the one page as the code in end_buffer_async_write()
466 * assumes that all buffers on the page are started at the same time.
467 *
468 * The fix is two passes across the ioend list - one to start writeback on the
Nathan Scottc41564b2006-03-29 08:55:14 +1000469 * buffer_heads, and then submit them for I/O on the second pass.
Dave Chinner7bf7f352012-11-12 22:09:45 +1100470 *
471 * If @fail is non-zero, it means that we have a situation where some part of
472 * the submission process has failed after we have marked paged for writeback
473 * and unlocked them. In this situation, we need to fail the ioend chain rather
474 * than submit it to IO. This typically only happens on a filesystem shutdown.
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100475 */
476STATIC void
477xfs_submit_ioend(
Christoph Hellwig06342cf2009-10-30 09:09:15 +0000478 struct writeback_control *wbc,
Dave Chinner7bf7f352012-11-12 22:09:45 +1100479 xfs_ioend_t *ioend,
480 int fail)
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100481{
David Chinnerd88992f2006-01-18 13:38:12 +1100482 xfs_ioend_t *head = ioend;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100483 xfs_ioend_t *next;
484 struct buffer_head *bh;
485 struct bio *bio;
486 sector_t lastblock = 0;
487
David Chinnerd88992f2006-01-18 13:38:12 +1100488 /* Pass 1 - start writeback */
489 do {
490 next = ioend->io_list;
Christoph Hellwig221cb252010-12-10 08:42:17 +0000491 for (bh = ioend->io_buffer_head; bh; bh = bh->b_private)
David Chinnerd88992f2006-01-18 13:38:12 +1100492 xfs_start_buffer_writeback(bh);
David Chinnerd88992f2006-01-18 13:38:12 +1100493 } while ((ioend = next) != NULL);
494
495 /* Pass 2 - submit I/O */
496 ioend = head;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100497 do {
498 next = ioend->io_list;
499 bio = NULL;
500
Dave Chinner7bf7f352012-11-12 22:09:45 +1100501 /*
502 * If we are failing the IO now, just mark the ioend with an
503 * error and finish it. This will run IO completion immediately
504 * as there is only one reference to the ioend at this point in
505 * time.
506 */
507 if (fail) {
Dave Chinner24513372014-06-25 14:58:08 +1000508 ioend->io_error = fail;
Dave Chinner7bf7f352012-11-12 22:09:45 +1100509 xfs_finish_ioend(ioend);
510 continue;
511 }
512
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100513 for (bh = ioend->io_buffer_head; bh; bh = bh->b_private) {
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100514
515 if (!bio) {
516 retry:
517 bio = xfs_alloc_ioend_bio(bh);
518 } else if (bh->b_blocknr != lastblock + 1) {
Christoph Hellwig06342cf2009-10-30 09:09:15 +0000519 xfs_submit_ioend_bio(wbc, ioend, bio);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100520 goto retry;
521 }
522
Zhi Yong Wuc7c1a7d2013-08-07 10:11:09 +0000523 if (xfs_bio_add_buffer(bio, bh) != bh->b_size) {
Christoph Hellwig06342cf2009-10-30 09:09:15 +0000524 xfs_submit_ioend_bio(wbc, ioend, bio);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100525 goto retry;
526 }
527
528 lastblock = bh->b_blocknr;
529 }
530 if (bio)
Christoph Hellwig06342cf2009-10-30 09:09:15 +0000531 xfs_submit_ioend_bio(wbc, ioend, bio);
Christoph Hellwig209fb872010-07-18 21:17:11 +0000532 xfs_finish_ioend(ioend);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100533 } while ((ioend = next) != NULL);
534}
535
536/*
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100537 * Test to see if we've been building up a completion structure for
538 * earlier buffers -- if so, we try to append to this ioend if we
539 * can, otherwise we finish off any current ioend and start another.
540 * Return true if we've finished the given ioend.
541 */
542STATIC void
543xfs_add_to_ioend(
544 struct inode *inode,
545 struct buffer_head *bh,
Christoph Hellwig7336cea2006-01-11 20:49:16 +1100546 xfs_off_t offset,
Dave Chinnerfbcc0252016-02-15 17:21:19 +1100547 struct xfs_writepage_ctx *wpc)
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100548{
Dave Chinnerfbcc0252016-02-15 17:21:19 +1100549 if (!wpc->ioend || wpc->io_type != wpc->ioend->io_type ||
550 bh->b_blocknr != wpc->last_block + 1) {
551 struct xfs_ioend *new;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100552
Dave Chinnerfbcc0252016-02-15 17:21:19 +1100553 new = xfs_alloc_ioend(inode, wpc->io_type);
554 new->io_offset = offset;
555 new->io_buffer_head = bh;
556 new->io_buffer_tail = bh;
557 if (wpc->ioend)
558 wpc->ioend->io_list = new;
559 wpc->ioend = new;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100560 } else {
Dave Chinnerfbcc0252016-02-15 17:21:19 +1100561 wpc->ioend->io_buffer_tail->b_private = bh;
562 wpc->ioend->io_buffer_tail = bh;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100563 }
564
565 bh->b_private = NULL;
Dave Chinnerfbcc0252016-02-15 17:21:19 +1100566 wpc->ioend->io_size += bh->b_size;
567 wpc->last_block = bh->b_blocknr;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100568}
569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570STATIC void
Nathan Scott87cbc492006-03-14 13:26:43 +1100571xfs_map_buffer(
Christoph Hellwig046f1682010-04-28 12:28:52 +0000572 struct inode *inode,
Nathan Scott87cbc492006-03-14 13:26:43 +1100573 struct buffer_head *bh,
Christoph Hellwig207d0412010-04-28 12:28:56 +0000574 struct xfs_bmbt_irec *imap,
Christoph Hellwig046f1682010-04-28 12:28:52 +0000575 xfs_off_t offset)
Nathan Scott87cbc492006-03-14 13:26:43 +1100576{
577 sector_t bn;
Christoph Hellwig8699bb02010-04-28 12:28:54 +0000578 struct xfs_mount *m = XFS_I(inode)->i_mount;
Christoph Hellwig207d0412010-04-28 12:28:56 +0000579 xfs_off_t iomap_offset = XFS_FSB_TO_B(m, imap->br_startoff);
580 xfs_daddr_t iomap_bn = xfs_fsb_to_db(XFS_I(inode), imap->br_startblock);
Nathan Scott87cbc492006-03-14 13:26:43 +1100581
Christoph Hellwig207d0412010-04-28 12:28:56 +0000582 ASSERT(imap->br_startblock != HOLESTARTBLOCK);
583 ASSERT(imap->br_startblock != DELAYSTARTBLOCK);
Nathan Scott87cbc492006-03-14 13:26:43 +1100584
Christoph Hellwige5131822010-04-28 12:28:55 +0000585 bn = (iomap_bn >> (inode->i_blkbits - BBSHIFT)) +
Christoph Hellwig8699bb02010-04-28 12:28:54 +0000586 ((offset - iomap_offset) >> inode->i_blkbits);
Nathan Scott87cbc492006-03-14 13:26:43 +1100587
Christoph Hellwig046f1682010-04-28 12:28:52 +0000588 ASSERT(bn || XFS_IS_REALTIME_INODE(XFS_I(inode)));
Nathan Scott87cbc492006-03-14 13:26:43 +1100589
590 bh->b_blocknr = bn;
591 set_buffer_mapped(bh);
592}
593
594STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595xfs_map_at_offset(
Christoph Hellwig046f1682010-04-28 12:28:52 +0000596 struct inode *inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 struct buffer_head *bh,
Christoph Hellwig207d0412010-04-28 12:28:56 +0000598 struct xfs_bmbt_irec *imap,
Christoph Hellwig046f1682010-04-28 12:28:52 +0000599 xfs_off_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
Christoph Hellwig207d0412010-04-28 12:28:56 +0000601 ASSERT(imap->br_startblock != HOLESTARTBLOCK);
602 ASSERT(imap->br_startblock != DELAYSTARTBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Christoph Hellwig207d0412010-04-28 12:28:56 +0000604 xfs_map_buffer(inode, bh, imap, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 set_buffer_mapped(bh);
606 clear_buffer_delay(bh);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100607 clear_buffer_unwritten(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608}
609
610/*
Dave Chinnera49935f2014-03-07 16:19:14 +1100611 * Test if a given page contains at least one buffer of a given @type.
612 * If @check_all_buffers is true, then we walk all the buffers in the page to
613 * try to find one of the type passed in. If it is not set, then the caller only
614 * needs to check the first buffer on the page for a match.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 */
Dave Chinnera49935f2014-03-07 16:19:14 +1100616STATIC bool
Dave Chinner6ffc4db2012-04-23 15:58:43 +1000617xfs_check_page_type(
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100618 struct page *page,
Dave Chinnera49935f2014-03-07 16:19:14 +1100619 unsigned int type,
620 bool check_all_buffers)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
Dave Chinnera49935f2014-03-07 16:19:14 +1100622 struct buffer_head *bh;
623 struct buffer_head *head;
624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 if (PageWriteback(page))
Dave Chinnera49935f2014-03-07 16:19:14 +1100626 return false;
627 if (!page->mapping)
628 return false;
629 if (!page_has_buffers(page))
630 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Dave Chinnera49935f2014-03-07 16:19:14 +1100632 bh = head = page_buffers(page);
633 do {
634 if (buffer_unwritten(bh)) {
635 if (type == XFS_IO_UNWRITTEN)
636 return true;
637 } else if (buffer_delay(bh)) {
Dan Carpenter805eeb82014-04-04 06:56:30 +1100638 if (type == XFS_IO_DELALLOC)
Dave Chinnera49935f2014-03-07 16:19:14 +1100639 return true;
640 } else if (buffer_dirty(bh) && buffer_mapped(bh)) {
Dan Carpenter805eeb82014-04-04 06:56:30 +1100641 if (type == XFS_IO_OVERWRITE)
Dave Chinnera49935f2014-03-07 16:19:14 +1100642 return true;
643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Dave Chinnera49935f2014-03-07 16:19:14 +1100645 /* If we are only checking the first buffer, we are done now. */
646 if (!check_all_buffers)
647 break;
648 } while ((bh = bh->b_this_page) != head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Dave Chinnera49935f2014-03-07 16:19:14 +1100650 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651}
652
Dave Chinner3ed3a432010-03-05 02:00:42 +0000653STATIC void
654xfs_vm_invalidatepage(
655 struct page *page,
Lukas Czernerd47992f2013-05-21 23:17:23 -0400656 unsigned int offset,
657 unsigned int length)
Dave Chinner3ed3a432010-03-05 02:00:42 +0000658{
Lukas Czerner34097df2013-05-21 23:58:01 -0400659 trace_xfs_invalidatepage(page->mapping->host, page, offset,
660 length);
661 block_invalidatepage(page, offset, length);
Dave Chinner3ed3a432010-03-05 02:00:42 +0000662}
663
664/*
665 * If the page has delalloc buffers on it, we need to punch them out before we
666 * invalidate the page. If we don't, we leave a stale delalloc mapping on the
667 * inode that can trip a BUG() in xfs_get_blocks() later on if a direct IO read
668 * is done on that same region - the delalloc extent is returned when none is
669 * supposed to be there.
670 *
671 * We prevent this by truncating away the delalloc regions on the page before
672 * invalidating it. Because they are delalloc, we can do this without needing a
673 * transaction. Indeed - if we get ENOSPC errors, we have to be able to do this
674 * truncation without a transaction as there is no space left for block
675 * reservation (typically why we see a ENOSPC in writeback).
676 *
677 * This is not a performance critical path, so for now just do the punching a
678 * buffer head at a time.
679 */
680STATIC void
681xfs_aops_discard_page(
682 struct page *page)
683{
684 struct inode *inode = page->mapping->host;
685 struct xfs_inode *ip = XFS_I(inode);
686 struct buffer_head *bh, *head;
687 loff_t offset = page_offset(page);
Dave Chinner3ed3a432010-03-05 02:00:42 +0000688
Dave Chinnera49935f2014-03-07 16:19:14 +1100689 if (!xfs_check_page_type(page, XFS_IO_DELALLOC, true))
Dave Chinner3ed3a432010-03-05 02:00:42 +0000690 goto out_invalidate;
691
Dave Chinnere8c37532010-03-15 02:36:35 +0000692 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
693 goto out_invalidate;
694
Dave Chinner4f107002011-03-07 10:00:35 +1100695 xfs_alert(ip->i_mount,
Dave Chinner3ed3a432010-03-05 02:00:42 +0000696 "page discard on page %p, inode 0x%llx, offset %llu.",
697 page, ip->i_ino, offset);
698
699 xfs_ilock(ip, XFS_ILOCK_EXCL);
700 bh = head = page_buffers(page);
701 do {
Dave Chinner3ed3a432010-03-05 02:00:42 +0000702 int error;
Dave Chinnerc726de42010-11-30 15:14:39 +1100703 xfs_fileoff_t start_fsb;
Dave Chinner3ed3a432010-03-05 02:00:42 +0000704
705 if (!buffer_delay(bh))
706 goto next_buffer;
707
Dave Chinnerc726de42010-11-30 15:14:39 +1100708 start_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
709 error = xfs_bmap_punch_delalloc_range(ip, start_fsb, 1);
Dave Chinner3ed3a432010-03-05 02:00:42 +0000710 if (error) {
711 /* something screwed, just bail */
Dave Chinnere8c37532010-03-15 02:36:35 +0000712 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
Dave Chinner4f107002011-03-07 10:00:35 +1100713 xfs_alert(ip->i_mount,
Dave Chinner3ed3a432010-03-05 02:00:42 +0000714 "page discard unable to remove delalloc mapping.");
Dave Chinnere8c37532010-03-15 02:36:35 +0000715 }
Dave Chinner3ed3a432010-03-05 02:00:42 +0000716 break;
717 }
718next_buffer:
Dave Chinnerc726de42010-11-30 15:14:39 +1100719 offset += 1 << inode->i_blkbits;
Dave Chinner3ed3a432010-03-05 02:00:42 +0000720
721 } while ((bh = bh->b_this_page) != head);
722
723 xfs_iunlock(ip, XFS_ILOCK_EXCL);
724out_invalidate:
Lukas Czernerd47992f2013-05-21 23:17:23 -0400725 xfs_vm_invalidatepage(page, 0, PAGE_CACHE_SIZE);
Dave Chinner3ed3a432010-03-05 02:00:42 +0000726 return;
727}
728
Dave Chinner150d5be02016-02-15 17:21:12 +1100729static int
730xfs_writepage_submit(
Dave Chinnerfbcc0252016-02-15 17:21:19 +1100731 struct xfs_writepage_ctx *wpc,
Dave Chinner150d5be02016-02-15 17:21:12 +1100732 struct writeback_control *wbc,
733 int status)
734{
735 struct blk_plug plug;
736
737 /* Reserve log space if we might write beyond the on-disk inode size. */
Dave Chinnerfbcc0252016-02-15 17:21:19 +1100738 if (!status && wpc->ioend && wpc->ioend->io_type != XFS_IO_UNWRITTEN &&
739 xfs_ioend_is_append(wpc->ioend))
740 status = xfs_setfilesize_trans_alloc(wpc->ioend);
Dave Chinner150d5be02016-02-15 17:21:12 +1100741
Dave Chinnerfbcc0252016-02-15 17:21:19 +1100742 if (wpc->iohead) {
Dave Chinner150d5be02016-02-15 17:21:12 +1100743 blk_start_plug(&plug);
Dave Chinnerfbcc0252016-02-15 17:21:19 +1100744 xfs_submit_ioend(wbc, wpc->iohead, status);
Dave Chinner150d5be02016-02-15 17:21:12 +1100745 blk_finish_plug(&plug);
746 }
747 return status;
748}
749
Dave Chinnerbfce7d22016-02-15 17:21:37 +1100750static int
751xfs_writepage_map(
752 struct xfs_writepage_ctx *wpc,
753 struct inode *inode,
754 struct page *page,
755 loff_t offset,
756 __uint64_t end_offset)
757{
758 struct buffer_head *bh, *head;
759 ssize_t len = 1 << inode->i_blkbits;
760 int error = 0;
761 int uptodate = 1;
762 int count = 0;
763
764 bh = head = page_buffers(page);
765 offset = page_offset(page);
766
767 do {
768 if (offset >= end_offset)
769 break;
770 if (!buffer_uptodate(bh))
771 uptodate = 0;
772
773 /*
774 * set_page_dirty dirties all buffers in a page, independent
775 * of their state. The dirty state however is entirely
776 * meaningless for holes (!mapped && uptodate), so skip
777 * buffers covering holes here.
778 */
779 if (!buffer_mapped(bh) && buffer_uptodate(bh)) {
780 wpc->imap_valid = false;
781 continue;
782 }
783
784 if (buffer_unwritten(bh)) {
785 if (wpc->io_type != XFS_IO_UNWRITTEN) {
786 wpc->io_type = XFS_IO_UNWRITTEN;
787 wpc->imap_valid = false;
788 }
789 } else if (buffer_delay(bh)) {
790 if (wpc->io_type != XFS_IO_DELALLOC) {
791 wpc->io_type = XFS_IO_DELALLOC;
792 wpc->imap_valid = false;
793 }
794 } else if (buffer_uptodate(bh)) {
795 if (wpc->io_type != XFS_IO_OVERWRITE) {
796 wpc->io_type = XFS_IO_OVERWRITE;
797 wpc->imap_valid = false;
798 }
799 } else {
800 if (PageUptodate(page))
801 ASSERT(buffer_mapped(bh));
802 /*
803 * This buffer is not uptodate and will not be
804 * written to disk. Ensure that we will put any
805 * subsequent writeable buffers into a new
806 * ioend.
807 */
808 wpc->imap_valid = false;
809 continue;
810 }
811
812 if (wpc->imap_valid)
813 wpc->imap_valid = xfs_imap_valid(inode, &wpc->imap,
814 offset);
815 if (!wpc->imap_valid) {
816 error = xfs_map_blocks(inode, offset, &wpc->imap,
817 wpc->io_type);
818 if (error)
819 goto out_error;
820 wpc->imap_valid = xfs_imap_valid(inode, &wpc->imap,
821 offset);
822 }
823 if (wpc->imap_valid) {
824 lock_buffer(bh);
825 if (wpc->io_type != XFS_IO_OVERWRITE)
826 xfs_map_at_offset(inode, bh, &wpc->imap, offset);
827 xfs_add_to_ioend(inode, bh, offset, wpc);
828 count++;
829 }
830
831 if (!wpc->iohead)
832 wpc->iohead = wpc->ioend;
833
834 } while (offset += len, ((bh = bh->b_this_page) != head));
835
836 if (uptodate && bh == head)
837 SetPageUptodate(page);
838
839 xfs_start_page_writeback(page, 1, count);
840 ASSERT(wpc->iohead || !count);
841 return 0;
842
843out_error:
844 /*
845 * On error, we have to fail the iohead here because we locked buffers
846 * in the ioend chain. If we don't do this, we'll deadlock invalidating
847 * the page as that tries to lock the buffers on the page. Also, because
848 * we may have set pages under writeback, we have to make sure we run
849 * IO completion to mark the error state of the IO appropriately, so we
850 * can't cancel the ioend directly here. That means we have to mark this
851 * page as under writeback if we included any buffers from it in the
852 * ioend chain so that completion treats it correctly.
853 *
854 * If we didn't include the page in the ioend, then we can simply
855 * discard and unlock it as there are no other users of the page or it's
856 * buffers right now. The caller will still need to trigger submission
857 * of outstanding ioends on the writepage context so they are treated
858 * correctly on error.
859 */
860 if (count)
861 xfs_start_page_writeback(page, 0, count);
862 else {
863 xfs_aops_discard_page(page);
864 ClearPageUptodate(page);
865 unlock_page(page);
866 }
867 mapping_set_error(page->mapping, error);
868 return error;
869}
870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871/*
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000872 * Write out a dirty page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 *
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000874 * For delalloc space on the page we need to allocate space and flush it.
875 * For unwritten space on the page we need to start the conversion to
876 * regular allocated space.
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000877 * For any other dirty buffer heads on the page we should flush them.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879STATIC int
Dave Chinnerfbcc0252016-02-15 17:21:19 +1100880xfs_do_writepage(
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000881 struct page *page,
Dave Chinnerfbcc0252016-02-15 17:21:19 +1100882 struct writeback_control *wbc,
883 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884{
Dave Chinnerfbcc0252016-02-15 17:21:19 +1100885 struct xfs_writepage_ctx *wpc = data;
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000886 struct inode *inode = page->mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 loff_t offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 __uint64_t end_offset;
Dave Chinnerad689722016-02-15 17:21:31 +1100889 pgoff_t end_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Lukas Czerner34097df2013-05-21 23:58:01 -0400891 trace_xfs_writepage(inode, page, 0, 0);
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000892
Christoph Hellwig20cb52e2010-06-24 09:46:01 +1000893 ASSERT(page_has_buffers(page));
894
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000895 /*
896 * Refuse to write the page out if we are called from reclaim context.
897 *
Christoph Hellwigd4f7a5c2010-06-28 10:34:44 -0400898 * This avoids stack overflows when called from deeply used stacks in
899 * random callers for direct reclaim or memcg reclaim. We explicitly
900 * allow reclaim from kswapd as the stack usage there is relatively low.
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000901 *
Mel Gorman94054fa2011-10-31 17:07:45 -0700902 * This should never happen except in the case of a VM regression so
903 * warn about it.
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000904 */
Mel Gorman94054fa2011-10-31 17:07:45 -0700905 if (WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD)) ==
906 PF_MEMALLOC))
Christoph Hellwigb5420f22010-08-24 11:47:51 +1000907 goto redirty;
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000908
909 /*
Christoph Hellwig680a6472011-07-08 14:34:05 +0200910 * Given that we do not allow direct reclaim to call us, we should
911 * never be called while in a filesystem transaction.
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000912 */
Christoph Hellwig448011e2014-06-06 16:05:15 +1000913 if (WARN_ON_ONCE(current->flags & PF_FSTRANS))
Christoph Hellwigb5420f22010-08-24 11:47:51 +1000914 goto redirty;
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000915
Jie Liu8695d272014-05-20 08:24:26 +1000916 /*
Dave Chinnerad689722016-02-15 17:21:31 +1100917 * Is this page beyond the end of the file?
918 *
Jie Liu8695d272014-05-20 08:24:26 +1000919 * The page index is less than the end_index, adjust the end_offset
920 * to the highest offset that this page should represent.
921 * -----------------------------------------------------
922 * | file mapping | <EOF> |
923 * -----------------------------------------------------
924 * | Page ... | Page N-2 | Page N-1 | Page N | |
925 * ^--------------------------------^----------|--------
926 * | desired writeback range | see else |
927 * ---------------------------------^------------------|
928 */
Dave Chinnerad689722016-02-15 17:21:31 +1100929 offset = i_size_read(inode);
930 end_index = offset >> PAGE_CACHE_SHIFT;
Jie Liu8695d272014-05-20 08:24:26 +1000931 if (page->index < end_index)
932 end_offset = (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT;
933 else {
934 /*
935 * Check whether the page to write out is beyond or straddles
936 * i_size or not.
937 * -------------------------------------------------------
938 * | file mapping | <EOF> |
939 * -------------------------------------------------------
940 * | Page ... | Page N-2 | Page N-1 | Page N | Beyond |
941 * ^--------------------------------^-----------|---------
942 * | | Straddles |
943 * ---------------------------------^-----------|--------|
944 */
Christoph Hellwig6b7a03f2012-07-03 12:20:00 -0400945 unsigned offset_into_page = offset & (PAGE_CACHE_SIZE - 1);
946
947 /*
Jan Karaff9a28f2013-03-14 14:30:54 +0100948 * Skip the page if it is fully outside i_size, e.g. due to a
949 * truncate operation that is in progress. We must redirty the
950 * page so that reclaim stops reclaiming it. Otherwise
951 * xfs_vm_releasepage() is called on it and gets confused.
Jie Liu8695d272014-05-20 08:24:26 +1000952 *
953 * Note that the end_index is unsigned long, it would overflow
954 * if the given offset is greater than 16TB on 32-bit system
955 * and if we do check the page is fully outside i_size or not
956 * via "if (page->index >= end_index + 1)" as "end_index + 1"
957 * will be evaluated to 0. Hence this page will be redirtied
958 * and be written out repeatedly which would result in an
959 * infinite loop, the user program that perform this operation
960 * will hang. Instead, we can verify this situation by checking
961 * if the page to write is totally beyond the i_size or if it's
962 * offset is just equal to the EOF.
Christoph Hellwig6b7a03f2012-07-03 12:20:00 -0400963 */
Jie Liu8695d272014-05-20 08:24:26 +1000964 if (page->index > end_index ||
965 (page->index == end_index && offset_into_page == 0))
Jan Karaff9a28f2013-03-14 14:30:54 +0100966 goto redirty;
Christoph Hellwig6b7a03f2012-07-03 12:20:00 -0400967
968 /*
969 * The page straddles i_size. It must be zeroed out on each
970 * and every writepage invocation because it may be mmapped.
971 * "A file is mapped in multiples of the page size. For a file
Jie Liu8695d272014-05-20 08:24:26 +1000972 * that is not a multiple of the page size, the remaining
Christoph Hellwig6b7a03f2012-07-03 12:20:00 -0400973 * memory is zeroed when mapped, and writes to that region are
974 * not written out to the file."
975 */
976 zero_user_segment(page, offset_into_page, PAGE_CACHE_SIZE);
Jie Liu8695d272014-05-20 08:24:26 +1000977
978 /* Adjust the end_offset to the end of file */
979 end_offset = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 }
981
Dave Chinnerbfce7d22016-02-15 17:21:37 +1100982 return xfs_writepage_map(wpc, inode, page, offset, end_offset);
Nathan Scottf51623b2006-03-14 13:26:27 +1100983
Christoph Hellwigb5420f22010-08-24 11:47:51 +1000984redirty:
Nathan Scottf51623b2006-03-14 13:26:27 +1100985 redirty_page_for_writepage(wbc, page);
986 unlock_page(page);
987 return 0;
Nathan Scottf51623b2006-03-14 13:26:27 +1100988}
989
Nathan Scott7d4fb402006-06-09 15:27:16 +1000990STATIC int
Dave Chinnerfbcc0252016-02-15 17:21:19 +1100991xfs_vm_writepage(
992 struct page *page,
993 struct writeback_control *wbc)
994{
995 struct xfs_writepage_ctx wpc = {
996 .io_type = XFS_IO_INVALID,
997 };
998 int ret;
999
1000 ret = xfs_do_writepage(page, wbc, &wpc);
1001 return xfs_writepage_submit(&wpc, wbc, ret);
1002}
1003
1004STATIC int
Nathan Scott7d4fb402006-06-09 15:27:16 +10001005xfs_vm_writepages(
1006 struct address_space *mapping,
1007 struct writeback_control *wbc)
1008{
Dave Chinnerfbcc0252016-02-15 17:21:19 +11001009 struct xfs_writepage_ctx wpc = {
1010 .io_type = XFS_IO_INVALID,
1011 };
1012 int ret;
1013
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +10001014 xfs_iflags_clear(XFS_I(mapping->host), XFS_ITRUNCATED);
Dave Chinnerfbcc0252016-02-15 17:21:19 +11001015 ret = write_cache_pages(mapping, wbc, xfs_do_writepage, &wpc);
1016 return xfs_writepage_submit(&wpc, wbc, ret);
Nathan Scott7d4fb402006-06-09 15:27:16 +10001017}
1018
Nathan Scottf51623b2006-03-14 13:26:27 +11001019/*
1020 * Called to move a page into cleanable state - and from there
Christoph Hellwig89f3b3632010-06-24 09:45:48 +10001021 * to be released. The page should already be clean. We always
Nathan Scottf51623b2006-03-14 13:26:27 +11001022 * have buffer heads in this call.
1023 *
Christoph Hellwig89f3b3632010-06-24 09:45:48 +10001024 * Returns 1 if the page is ok to release, 0 otherwise.
Nathan Scottf51623b2006-03-14 13:26:27 +11001025 */
1026STATIC int
Nathan Scott238f4c52006-03-17 17:26:25 +11001027xfs_vm_releasepage(
Nathan Scottf51623b2006-03-14 13:26:27 +11001028 struct page *page,
1029 gfp_t gfp_mask)
1030{
Christoph Hellwig20cb52e2010-06-24 09:46:01 +10001031 int delalloc, unwritten;
Nathan Scottf51623b2006-03-14 13:26:27 +11001032
Lukas Czerner34097df2013-05-21 23:58:01 -04001033 trace_xfs_releasepage(page->mapping->host, page, 0, 0);
Nathan Scott238f4c52006-03-17 17:26:25 +11001034
Christoph Hellwig20cb52e2010-06-24 09:46:01 +10001035 xfs_count_page_state(page, &delalloc, &unwritten);
Nathan Scottf51623b2006-03-14 13:26:27 +11001036
Christoph Hellwig448011e2014-06-06 16:05:15 +10001037 if (WARN_ON_ONCE(delalloc))
Christoph Hellwig89f3b3632010-06-24 09:45:48 +10001038 return 0;
Christoph Hellwig448011e2014-06-06 16:05:15 +10001039 if (WARN_ON_ONCE(unwritten))
Nathan Scottf51623b2006-03-14 13:26:27 +11001040 return 0;
1041
Nathan Scottf51623b2006-03-14 13:26:27 +11001042 return try_to_free_buffers(page);
1043}
1044
Dave Chinnera7193702015-04-16 21:57:48 +10001045/*
Dave Chinnera06c2772015-04-16 22:00:00 +10001046 * When we map a DIO buffer, we may need to attach an ioend that describes the
1047 * type of write IO we are doing. This passes to the completion function the
1048 * operations it needs to perform. If the mapping is for an overwrite wholly
1049 * within the EOF then we don't need an ioend and so we don't allocate one.
1050 * This avoids the unnecessary overhead of allocating and freeing ioends for
1051 * workloads that don't require transactions on IO completion.
Dave Chinnerd5cc2e32015-04-16 21:59:07 +10001052 *
1053 * If we get multiple mappings in a single IO, we might be mapping different
1054 * types. But because the direct IO can only have a single private pointer, we
1055 * need to ensure that:
1056 *
Dave Chinnera06c2772015-04-16 22:00:00 +10001057 * a) i) the ioend spans the entire region of unwritten mappings; or
1058 * ii) the ioend spans all the mappings that cross or are beyond EOF; and
Dave Chinnerd5cc2e32015-04-16 21:59:07 +10001059 * b) if it contains unwritten extents, it is *permanently* marked as such
1060 *
1061 * We could do this by chaining ioends like buffered IO does, but we only
1062 * actually get one IO completion callback from the direct IO, and that spans
1063 * the entire IO regardless of how many mappings and IOs are needed to complete
1064 * the DIO. There is only going to be one reference to the ioend and its life
1065 * cycle is constrained by the DIO completion code. hence we don't need
1066 * reference counting here.
Dave Chinner3e12dbb2015-11-03 12:27:22 +11001067 *
1068 * Note that for DIO, an IO to the highest supported file block offset (i.e.
1069 * 2^63 - 1FSB bytes) will result in the offset + count overflowing a signed 64
1070 * bit variable. Hence if we see this overflow, we have to assume that the IO is
1071 * extending the file size. We won't know for sure until IO completion is run
1072 * and the actual max write offset is communicated to the IO completion
1073 * routine.
1074 *
1075 * For DAX page faults, we are preparing to never see unwritten extents here,
1076 * nor should we ever extend the inode size. Hence we will soon have nothing to
1077 * do here for this case, ensuring we don't have to provide an IO completion
1078 * callback to free an ioend that we don't actually need for a fault into the
1079 * page at offset (2^63 - 1FSB) bytes.
Dave Chinnera7193702015-04-16 21:57:48 +10001080 */
Dave Chinner3e12dbb2015-11-03 12:27:22 +11001081
Dave Chinnera7193702015-04-16 21:57:48 +10001082static void
1083xfs_map_direct(
1084 struct inode *inode,
1085 struct buffer_head *bh_result,
1086 struct xfs_bmbt_irec *imap,
Dave Chinner3e12dbb2015-11-03 12:27:22 +11001087 xfs_off_t offset,
1088 bool dax_fault)
Dave Chinnera7193702015-04-16 21:57:48 +10001089{
Dave Chinnerd5cc2e32015-04-16 21:59:07 +10001090 struct xfs_ioend *ioend;
1091 xfs_off_t size = bh_result->b_size;
1092 int type;
1093
1094 if (ISUNWRITTEN(imap))
1095 type = XFS_IO_UNWRITTEN;
1096 else
1097 type = XFS_IO_OVERWRITE;
1098
1099 trace_xfs_gbmap_direct(XFS_I(inode), offset, size, type, imap);
1100
Dave Chinner3e12dbb2015-11-03 12:27:22 +11001101 if (dax_fault) {
1102 ASSERT(type == XFS_IO_OVERWRITE);
1103 trace_xfs_gbmap_direct_none(XFS_I(inode), offset, size, type,
1104 imap);
1105 return;
1106 }
Dave Chinner3e12dbb2015-11-03 12:27:22 +11001107
Dave Chinnerd5cc2e32015-04-16 21:59:07 +10001108 if (bh_result->b_private) {
1109 ioend = bh_result->b_private;
1110 ASSERT(ioend->io_size > 0);
1111 ASSERT(offset >= ioend->io_offset);
1112 if (offset + size > ioend->io_offset + ioend->io_size)
1113 ioend->io_size = offset - ioend->io_offset + size;
1114
1115 if (type == XFS_IO_UNWRITTEN && type != ioend->io_type)
1116 ioend->io_type = XFS_IO_UNWRITTEN;
1117
1118 trace_xfs_gbmap_direct_update(XFS_I(inode), ioend->io_offset,
1119 ioend->io_size, ioend->io_type,
1120 imap);
Dave Chinnera06c2772015-04-16 22:00:00 +10001121 } else if (type == XFS_IO_UNWRITTEN ||
Dave Chinner3e12dbb2015-11-03 12:27:22 +11001122 offset + size > i_size_read(inode) ||
1123 offset + size < 0) {
Dave Chinnerd5cc2e32015-04-16 21:59:07 +10001124 ioend = xfs_alloc_ioend(inode, type);
1125 ioend->io_offset = offset;
1126 ioend->io_size = size;
Dave Chinnera06c2772015-04-16 22:00:00 +10001127
Dave Chinnerd5cc2e32015-04-16 21:59:07 +10001128 bh_result->b_private = ioend;
Dave Chinnera06c2772015-04-16 22:00:00 +10001129 set_buffer_defer_completion(bh_result);
Dave Chinnerd5cc2e32015-04-16 21:59:07 +10001130
1131 trace_xfs_gbmap_direct_new(XFS_I(inode), offset, size, type,
1132 imap);
Dave Chinnera06c2772015-04-16 22:00:00 +10001133 } else {
1134 trace_xfs_gbmap_direct_none(XFS_I(inode), offset, size, type,
1135 imap);
Dave Chinnera7193702015-04-16 21:57:48 +10001136 }
1137}
1138
Dave Chinner1fdca9c2015-04-16 21:58:21 +10001139/*
1140 * If this is O_DIRECT or the mpage code calling tell them how large the mapping
1141 * is, so that we can avoid repeated get_blocks calls.
1142 *
1143 * If the mapping spans EOF, then we have to break the mapping up as the mapping
1144 * for blocks beyond EOF must be marked new so that sub block regions can be
1145 * correctly zeroed. We can't do this for mappings within EOF unless the mapping
1146 * was just allocated or is unwritten, otherwise the callers would overwrite
1147 * existing data with zeros. Hence we have to split the mapping into a range up
1148 * to and including EOF, and a second mapping for beyond EOF.
1149 */
1150static void
1151xfs_map_trim_size(
1152 struct inode *inode,
1153 sector_t iblock,
1154 struct buffer_head *bh_result,
1155 struct xfs_bmbt_irec *imap,
1156 xfs_off_t offset,
1157 ssize_t size)
1158{
1159 xfs_off_t mapping_size;
1160
1161 mapping_size = imap->br_startoff + imap->br_blockcount - iblock;
1162 mapping_size <<= inode->i_blkbits;
1163
1164 ASSERT(mapping_size > 0);
1165 if (mapping_size > size)
1166 mapping_size = size;
1167 if (offset < i_size_read(inode) &&
1168 offset + mapping_size >= i_size_read(inode)) {
1169 /* limit mapping to block that spans EOF */
1170 mapping_size = roundup_64(i_size_read(inode) - offset,
1171 1 << inode->i_blkbits);
1172 }
1173 if (mapping_size > LONG_MAX)
1174 mapping_size = LONG_MAX;
1175
1176 bh_result->b_size = mapping_size;
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,
Dave Chinner3e12dbb2015-11-03 12:27:22 +11001185 bool direct,
1186 bool dax_fault)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187{
Christoph Hellwiga206c812010-12-10 08:42:20 +00001188 struct xfs_inode *ip = XFS_I(inode);
1189 struct xfs_mount *mp = ip->i_mount;
1190 xfs_fileoff_t offset_fsb, end_fsb;
1191 int error = 0;
1192 int lockmode = 0;
Christoph Hellwig207d0412010-04-28 12:28:56 +00001193 struct xfs_bmbt_irec imap;
Christoph Hellwiga206c812010-12-10 08:42:20 +00001194 int nimaps = 1;
Nathan Scottfdc7ed72005-11-02 15:13:13 +11001195 xfs_off_t offset;
1196 ssize_t size;
Christoph Hellwig207d0412010-04-28 12:28:56 +00001197 int new = 0;
Christoph Hellwiga206c812010-12-10 08:42:20 +00001198
1199 if (XFS_FORCED_SHUTDOWN(mp))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10001200 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
Nathan Scottfdc7ed72005-11-02 15:13:13 +11001202 offset = (xfs_off_t)iblock << inode->i_blkbits;
Nathan Scottc2536662006-03-29 10:44:40 +10001203 ASSERT(bh_result->b_size >= (1 << inode->i_blkbits));
1204 size = bh_result->b_size;
Lachlan McIlroy364f3582008-09-17 16:50:14 +10001205
1206 if (!create && direct && offset >= i_size_read(inode))
1207 return 0;
1208
Dave Chinner507630b2012-03-27 10:34:50 -04001209 /*
1210 * Direct I/O is usually done on preallocated files, so try getting
1211 * a block mapping without an exclusive lock first. For buffered
1212 * writes we already have the exclusive iolock anyway, so avoiding
1213 * a lock roundtrip here by taking the ilock exclusive from the
1214 * beginning is a useful micro optimization.
1215 */
1216 if (create && !direct) {
Christoph Hellwiga206c812010-12-10 08:42:20 +00001217 lockmode = XFS_ILOCK_EXCL;
1218 xfs_ilock(ip, lockmode);
1219 } else {
Christoph Hellwig309ecac82013-12-06 12:30:09 -08001220 lockmode = xfs_ilock_data_map_shared(ip);
Christoph Hellwiga206c812010-12-10 08:42:20 +00001221 }
Christoph Hellwigf2bde9b2010-06-24 11:44:35 +10001222
Dave Chinnerd2c28192012-06-08 15:44:53 +10001223 ASSERT(offset <= mp->m_super->s_maxbytes);
1224 if (offset + size > mp->m_super->s_maxbytes)
1225 size = mp->m_super->s_maxbytes - offset;
Christoph Hellwiga206c812010-12-10 08:42:20 +00001226 end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + size);
1227 offset_fsb = XFS_B_TO_FSBT(mp, offset);
1228
Dave Chinner5c8ed202011-09-18 20:40:45 +00001229 error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb,
1230 &imap, &nimaps, XFS_BMAPI_ENTIRE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 if (error)
Christoph Hellwiga206c812010-12-10 08:42:20 +00001232 goto out_unlock;
1233
Dave Chinner1ca19152015-11-03 12:37:00 +11001234 /* for DAX, we convert unwritten extents directly */
Christoph Hellwiga206c812010-12-10 08:42:20 +00001235 if (create &&
1236 (!nimaps ||
1237 (imap.br_startblock == HOLESTARTBLOCK ||
Dave Chinner1ca19152015-11-03 12:37:00 +11001238 imap.br_startblock == DELAYSTARTBLOCK) ||
1239 (IS_DAX(inode) && ISUNWRITTEN(&imap)))) {
Dave Chinneraff3a9e2012-04-23 15:58:44 +10001240 if (direct || xfs_get_extsz_hint(ip)) {
Dave Chinner507630b2012-03-27 10:34:50 -04001241 /*
Brian Foster009c6e82015-10-12 15:34:20 +11001242 * xfs_iomap_write_direct() expects the shared lock. It
1243 * is unlocked on return.
Dave Chinner507630b2012-03-27 10:34:50 -04001244 */
Brian Foster009c6e82015-10-12 15:34:20 +11001245 if (lockmode == XFS_ILOCK_EXCL)
1246 xfs_ilock_demote(ip, lockmode);
1247
Christoph Hellwiga206c812010-12-10 08:42:20 +00001248 error = xfs_iomap_write_direct(ip, offset, size,
1249 &imap, nimaps);
Dave Chinner507630b2012-03-27 10:34:50 -04001250 if (error)
Dave Chinner24513372014-06-25 14:58:08 +10001251 return error;
Dave Chinnerd3bc8152012-04-27 19:45:21 +10001252 new = 1;
Dave Chinner6b698ed2015-06-04 09:18:53 +10001253
Christoph Hellwiga206c812010-12-10 08:42:20 +00001254 } else {
Dave Chinner507630b2012-03-27 10:34:50 -04001255 /*
1256 * Delalloc reservations do not require a transaction,
Dave Chinnerd3bc8152012-04-27 19:45:21 +10001257 * we can go on without dropping the lock here. If we
1258 * are allocating a new delalloc block, make sure that
1259 * we set the new flag so that we mark the buffer new so
1260 * that we know that it is newly allocated if the write
1261 * fails.
Dave Chinner507630b2012-03-27 10:34:50 -04001262 */
Dave Chinnerd3bc8152012-04-27 19:45:21 +10001263 if (nimaps && imap.br_startblock == HOLESTARTBLOCK)
1264 new = 1;
Christoph Hellwiga206c812010-12-10 08:42:20 +00001265 error = xfs_iomap_write_delay(ip, offset, size, &imap);
Dave Chinner507630b2012-03-27 10:34:50 -04001266 if (error)
1267 goto out_unlock;
1268
1269 xfs_iunlock(ip, lockmode);
Christoph Hellwiga206c812010-12-10 08:42:20 +00001270 }
Dave Chinnerd5cc2e32015-04-16 21:59:07 +10001271 trace_xfs_get_blocks_alloc(ip, offset, size,
1272 ISUNWRITTEN(&imap) ? XFS_IO_UNWRITTEN
1273 : XFS_IO_DELALLOC, &imap);
Christoph Hellwiga206c812010-12-10 08:42:20 +00001274 } else if (nimaps) {
Dave Chinnerd5cc2e32015-04-16 21:59:07 +10001275 trace_xfs_get_blocks_found(ip, offset, size,
1276 ISUNWRITTEN(&imap) ? XFS_IO_UNWRITTEN
1277 : XFS_IO_OVERWRITE, &imap);
Dave Chinner507630b2012-03-27 10:34:50 -04001278 xfs_iunlock(ip, lockmode);
Christoph Hellwiga206c812010-12-10 08:42:20 +00001279 } else {
1280 trace_xfs_get_blocks_notfound(ip, offset, size);
1281 goto out_unlock;
1282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
Dave Chinner1ca19152015-11-03 12:37:00 +11001284 if (IS_DAX(inode) && create) {
1285 ASSERT(!ISUNWRITTEN(&imap));
1286 /* zeroing is not needed at a higher layer */
1287 new = 0;
1288 }
1289
Dave Chinner1fdca9c2015-04-16 21:58:21 +10001290 /* trim mapping down to size requested */
1291 if (direct || size > (1 << inode->i_blkbits))
1292 xfs_map_trim_size(inode, iblock, bh_result,
1293 &imap, offset, size);
1294
Dave Chinnera7193702015-04-16 21:57:48 +10001295 /*
1296 * For unwritten extents do not report a disk address in the buffered
1297 * read case (treat as if we're reading into a hole).
1298 */
Christoph Hellwig207d0412010-04-28 12:28:56 +00001299 if (imap.br_startblock != HOLESTARTBLOCK &&
Dave Chinnera7193702015-04-16 21:57:48 +10001300 imap.br_startblock != DELAYSTARTBLOCK &&
1301 (create || !ISUNWRITTEN(&imap))) {
1302 xfs_map_buffer(inode, bh_result, &imap, offset);
1303 if (ISUNWRITTEN(&imap))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 set_buffer_unwritten(bh_result);
Dave Chinnera7193702015-04-16 21:57:48 +10001305 /* direct IO needs special help */
1306 if (create && direct)
Dave Chinner3e12dbb2015-11-03 12:27:22 +11001307 xfs_map_direct(inode, bh_result, &imap, offset,
1308 dax_fault);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 }
1310
Nathan Scottc2536662006-03-29 10:44:40 +10001311 /*
1312 * If this is a realtime file, data may be on a different device.
1313 * to that pointed to from the buffer_head b_bdev currently.
1314 */
Christoph Hellwig046f1682010-04-28 12:28:52 +00001315 bh_result->b_bdev = xfs_find_bdev_for_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
Nathan Scottc2536662006-03-29 10:44:40 +10001317 /*
David Chinner549054a2007-02-10 18:36:35 +11001318 * If we previously allocated a block out beyond eof and we are now
1319 * coming back to use it then we will need to flag it as new even if it
1320 * has a disk address.
1321 *
1322 * With sub-block writes into unwritten extents we also need to mark
1323 * the buffer as new so that the unwritten parts of the buffer gets
1324 * correctly zeroed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 */
1326 if (create &&
1327 ((!buffer_mapped(bh_result) && !buffer_uptodate(bh_result)) ||
David Chinner549054a2007-02-10 18:36:35 +11001328 (offset >= i_size_read(inode)) ||
Christoph Hellwig207d0412010-04-28 12:28:56 +00001329 (new || ISUNWRITTEN(&imap))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 set_buffer_new(bh_result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
Christoph Hellwig207d0412010-04-28 12:28:56 +00001332 if (imap.br_startblock == DELAYSTARTBLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 BUG_ON(direct);
1334 if (create) {
1335 set_buffer_uptodate(bh_result);
1336 set_buffer_mapped(bh_result);
1337 set_buffer_delay(bh_result);
1338 }
1339 }
1340
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 return 0;
Christoph Hellwiga206c812010-12-10 08:42:20 +00001342
1343out_unlock:
1344 xfs_iunlock(ip, lockmode);
Dave Chinner24513372014-06-25 14:58:08 +10001345 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346}
1347
1348int
Nathan Scottc2536662006-03-29 10:44:40 +10001349xfs_get_blocks(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 struct inode *inode,
1351 sector_t iblock,
1352 struct buffer_head *bh_result,
1353 int create)
1354{
Dave Chinner3e12dbb2015-11-03 12:27:22 +11001355 return __xfs_get_blocks(inode, iblock, bh_result, create, false, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356}
1357
Dave Chinner6b698ed2015-06-04 09:18:53 +10001358int
Nathan Scotte4c573b2006-03-14 13:54:26 +11001359xfs_get_blocks_direct(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 struct inode *inode,
1361 sector_t iblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 struct buffer_head *bh_result,
1363 int create)
1364{
Dave Chinner3e12dbb2015-11-03 12:27:22 +11001365 return __xfs_get_blocks(inode, iblock, bh_result, create, true, false);
1366}
1367
1368int
1369xfs_get_blocks_dax_fault(
1370 struct inode *inode,
1371 sector_t iblock,
1372 struct buffer_head *bh_result,
1373 int create)
1374{
1375 return __xfs_get_blocks(inode, iblock, bh_result, create, true, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376}
1377
Dave Chinner6b698ed2015-06-04 09:18:53 +10001378static void
1379__xfs_end_io_direct_write(
1380 struct inode *inode,
1381 struct xfs_ioend *ioend,
Christoph Hellwig209fb872010-07-18 21:17:11 +00001382 loff_t offset,
Dave Chinner6b698ed2015-06-04 09:18:53 +10001383 ssize_t size)
Christoph Hellwigf0973862005-09-05 08:22:52 +10001384{
Dave Chinner6b698ed2015-06-04 09:18:53 +10001385 struct xfs_mount *mp = XFS_I(inode)->i_mount;
Christoph Hellwig2ba66232015-02-02 10:02:09 +11001386
Dave Chinner6b698ed2015-06-04 09:18:53 +10001387 if (XFS_FORCED_SHUTDOWN(mp) || ioend->io_error)
Dave Chinner6dfa1b62015-04-16 21:59:34 +10001388 goto out_end_io;
Christoph Hellwigf0973862005-09-05 08:22:52 +10001389
1390 /*
Dave Chinnerd5cc2e32015-04-16 21:59:07 +10001391 * dio completion end_io functions are only called on writes if more
1392 * than 0 bytes was written.
Christoph Hellwig2813d682011-12-18 20:00:12 +00001393 */
Dave Chinnerd5cc2e32015-04-16 21:59:07 +10001394 ASSERT(size > 0);
1395
1396 /*
1397 * The ioend only maps whole blocks, while the IO may be sector aligned.
Dave Chinnera06c2772015-04-16 22:00:00 +10001398 * Hence the ioend offset/size may not match the IO offset/size exactly.
1399 * Because we don't map overwrites within EOF into the ioend, the offset
1400 * may not match, but only if the endio spans EOF. Either way, write
1401 * the IO sizes into the ioend so that completion processing does the
1402 * right thing.
Dave Chinnerd5cc2e32015-04-16 21:59:07 +10001403 */
Dave Chinnerd5cc2e32015-04-16 21:59:07 +10001404 ASSERT(offset + size <= ioend->io_offset + ioend->io_size);
1405 ioend->io_size = size;
1406 ioend->io_offset = offset;
Christoph Hellwigf0973862005-09-05 08:22:52 +10001407
1408 /*
Dave Chinner6dfa1b62015-04-16 21:59:34 +10001409 * The ioend tells us whether we are doing unwritten extent conversion
1410 * or an append transaction that updates the on-disk file size. These
1411 * cases are the only cases where we should *potentially* be needing
Dave Chinnera06c2772015-04-16 22:00:00 +10001412 * to update the VFS inode size.
Dave Chinner6dfa1b62015-04-16 21:59:34 +10001413 *
1414 * We need to update the in-core inode size here so that we don't end up
Dave Chinnera06c2772015-04-16 22:00:00 +10001415 * with the on-disk inode size being outside the in-core inode size. We
1416 * have no other method of updating EOF for AIO, so always do it here
1417 * if necessary.
Dave Chinnerb9d59842015-04-16 22:03:07 +10001418 *
1419 * We need to lock the test/set EOF update as we can be racing with
1420 * other IO completions here to update the EOF. Failing to serialise
1421 * here can result in EOF moving backwards and Bad Things Happen when
1422 * that occurs.
Christoph Hellwig2813d682011-12-18 20:00:12 +00001423 */
Dave Chinner6b698ed2015-06-04 09:18:53 +10001424 spin_lock(&XFS_I(inode)->i_flags_lock);
Christoph Hellwig2ba66232015-02-02 10:02:09 +11001425 if (offset + size > i_size_read(inode))
1426 i_size_write(inode, offset + size);
Dave Chinner6b698ed2015-06-04 09:18:53 +10001427 spin_unlock(&XFS_I(inode)->i_flags_lock);
Christoph Hellwig2813d682011-12-18 20:00:12 +00001428
1429 /*
Dave Chinner6dfa1b62015-04-16 21:59:34 +10001430 * If we are doing an append IO that needs to update the EOF on disk,
1431 * do the transaction reserve now so we can use common end io
1432 * processing. Stashing the error (if there is one) in the ioend will
1433 * result in the ioend processing passing on the error if it is
1434 * possible as we can't return it from here.
Christoph Hellwigf0973862005-09-05 08:22:52 +10001435 */
Dave Chinnera06c2772015-04-16 22:00:00 +10001436 if (ioend->io_type == XFS_IO_OVERWRITE)
Dave Chinner6dfa1b62015-04-16 21:59:34 +10001437 ioend->io_error = xfs_setfilesize_trans_alloc(ioend);
Christoph Hellwig40e2e972010-07-18 21:17:09 +00001438
Dave Chinner6dfa1b62015-04-16 21:59:34 +10001439out_end_io:
1440 xfs_end_io(&ioend->io_work);
1441 return;
Christoph Hellwigf0973862005-09-05 08:22:52 +10001442}
1443
Dave Chinner6b698ed2015-06-04 09:18:53 +10001444/*
1445 * Complete a direct I/O write request.
1446 *
1447 * The ioend structure is passed from __xfs_get_blocks() to tell us what to do.
1448 * If no ioend exists (i.e. @private == NULL) then the write IO is an overwrite
1449 * wholly within the EOF and so there is nothing for us to do. Note that in this
1450 * case the completion can be called in interrupt context, whereas if we have an
1451 * ioend we will always be called in task context (i.e. from a workqueue).
1452 */
1453STATIC void
1454xfs_end_io_direct_write(
1455 struct kiocb *iocb,
1456 loff_t offset,
1457 ssize_t size,
1458 void *private)
1459{
1460 struct inode *inode = file_inode(iocb->ki_filp);
1461 struct xfs_ioend *ioend = private;
1462
1463 trace_xfs_gbmap_direct_endio(XFS_I(inode), offset, size,
1464 ioend ? ioend->io_type : 0, NULL);
1465
1466 if (!ioend) {
1467 ASSERT(offset + size <= i_size_read(inode));
1468 return;
1469 }
1470
1471 __xfs_end_io_direct_write(inode, ioend, offset, size);
1472}
1473
Dave Chinner6e1ba0b2015-06-04 09:19:15 +10001474static inline ssize_t
1475xfs_vm_do_dio(
1476 struct inode *inode,
1477 struct kiocb *iocb,
1478 struct iov_iter *iter,
1479 loff_t offset,
1480 void (*endio)(struct kiocb *iocb,
1481 loff_t offset,
1482 ssize_t size,
1483 void *private),
1484 int flags)
1485{
1486 struct block_device *bdev;
1487
1488 if (IS_DAX(inode))
1489 return dax_do_io(iocb, inode, iter, offset,
1490 xfs_get_blocks_direct, endio, 0);
1491
1492 bdev = xfs_find_bdev_for_inode(inode);
1493 return __blockdev_direct_IO(iocb, inode, bdev, iter, offset,
1494 xfs_get_blocks_direct, endio, NULL, flags);
1495}
1496
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497STATIC ssize_t
Nathan Scotte4c573b2006-03-14 13:54:26 +11001498xfs_vm_direct_IO(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 struct kiocb *iocb,
Al Virod8d3d942014-03-04 21:27:34 -05001500 struct iov_iter *iter,
1501 loff_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502{
Christoph Hellwig209fb872010-07-18 21:17:11 +00001503 struct inode *inode = iocb->ki_filp->f_mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
Dave Chinner6e1ba0b2015-06-04 09:19:15 +10001505 if (iov_iter_rw(iter) == WRITE)
1506 return xfs_vm_do_dio(inode, iocb, iter, offset,
1507 xfs_end_io_direct_write, DIO_ASYNC_EXTEND);
1508 return xfs_vm_do_dio(inode, iocb, iter, offset, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509}
1510
Dave Chinnerd3bc8152012-04-27 19:45:21 +10001511/*
1512 * Punch out the delalloc blocks we have already allocated.
1513 *
1514 * Don't bother with xfs_setattr given that nothing can have made it to disk yet
1515 * as the page is still locked at this point.
1516 */
Christoph Hellwigfa9b2272010-06-14 05:17:31 -04001517STATIC void
Dave Chinnerd3bc8152012-04-27 19:45:21 +10001518xfs_vm_kill_delalloc_range(
1519 struct inode *inode,
1520 loff_t start,
1521 loff_t end)
Christoph Hellwigfa9b2272010-06-14 05:17:31 -04001522{
Dave Chinnerd3bc8152012-04-27 19:45:21 +10001523 struct xfs_inode *ip = XFS_I(inode);
1524 xfs_fileoff_t start_fsb;
1525 xfs_fileoff_t end_fsb;
1526 int error;
Christoph Hellwigfa9b2272010-06-14 05:17:31 -04001527
Dave Chinnerd3bc8152012-04-27 19:45:21 +10001528 start_fsb = XFS_B_TO_FSB(ip->i_mount, start);
1529 end_fsb = XFS_B_TO_FSB(ip->i_mount, end);
1530 if (end_fsb <= start_fsb)
1531 return;
Dave Chinnerc726de42010-11-30 15:14:39 +11001532
Dave Chinnerd3bc8152012-04-27 19:45:21 +10001533 xfs_ilock(ip, XFS_ILOCK_EXCL);
1534 error = xfs_bmap_punch_delalloc_range(ip, start_fsb,
1535 end_fsb - start_fsb);
1536 if (error) {
1537 /* something screwed, just bail */
1538 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
1539 xfs_alert(ip->i_mount,
1540 "xfs_vm_write_failed: unable to clean up ino %lld",
1541 ip->i_ino);
Dave Chinnerc726de42010-11-30 15:14:39 +11001542 }
Christoph Hellwigfa9b2272010-06-14 05:17:31 -04001543 }
Dave Chinnerd3bc8152012-04-27 19:45:21 +10001544 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Christoph Hellwigfa9b2272010-06-14 05:17:31 -04001545}
1546
Dave Chinnerd3bc8152012-04-27 19:45:21 +10001547STATIC void
1548xfs_vm_write_failed(
1549 struct inode *inode,
1550 struct page *page,
1551 loff_t pos,
1552 unsigned len)
1553{
Jie Liu58e59852013-07-16 13:11:16 +08001554 loff_t block_offset;
Dave Chinnerd3bc8152012-04-27 19:45:21 +10001555 loff_t block_start;
1556 loff_t block_end;
1557 loff_t from = pos & (PAGE_CACHE_SIZE - 1);
1558 loff_t to = from + len;
1559 struct buffer_head *bh, *head;
1560
Jie Liu58e59852013-07-16 13:11:16 +08001561 /*
1562 * The request pos offset might be 32 or 64 bit, this is all fine
1563 * on 64-bit platform. However, for 64-bit pos request on 32-bit
1564 * platform, the high 32-bit will be masked off if we evaluate the
1565 * block_offset via (pos & PAGE_MASK) because the PAGE_MASK is
1566 * 0xfffff000 as an unsigned long, hence the result is incorrect
1567 * which could cause the following ASSERT failed in most cases.
1568 * In order to avoid this, we can evaluate the block_offset of the
1569 * start of the page by using shifts rather than masks the mismatch
1570 * problem.
1571 */
1572 block_offset = (pos >> PAGE_CACHE_SHIFT) << PAGE_CACHE_SHIFT;
1573
Dave Chinnerd3bc8152012-04-27 19:45:21 +10001574 ASSERT(block_offset + from == pos);
1575
1576 head = page_buffers(page);
1577 block_start = 0;
1578 for (bh = head; bh != head || !block_start;
1579 bh = bh->b_this_page, block_start = block_end,
1580 block_offset += bh->b_size) {
1581 block_end = block_start + bh->b_size;
1582
1583 /* skip buffers before the write */
1584 if (block_end <= from)
1585 continue;
1586
1587 /* if the buffer is after the write, we're done */
1588 if (block_start >= to)
1589 break;
1590
1591 if (!buffer_delay(bh))
1592 continue;
1593
1594 if (!buffer_new(bh) && block_offset < i_size_read(inode))
1595 continue;
1596
1597 xfs_vm_kill_delalloc_range(inode, block_offset,
1598 block_offset + bh->b_size);
Dave Chinner4ab9ed52014-04-14 18:11:58 +10001599
1600 /*
1601 * This buffer does not contain data anymore. make sure anyone
1602 * who finds it knows that for certain.
1603 */
1604 clear_buffer_delay(bh);
1605 clear_buffer_uptodate(bh);
1606 clear_buffer_mapped(bh);
1607 clear_buffer_new(bh);
1608 clear_buffer_dirty(bh);
Dave Chinnerd3bc8152012-04-27 19:45:21 +10001609 }
1610
1611}
1612
1613/*
1614 * This used to call block_write_begin(), but it unlocks and releases the page
1615 * on error, and we need that page to be able to punch stale delalloc blocks out
1616 * on failure. hence we copy-n-waste it here and call xfs_vm_write_failed() at
1617 * the appropriate point.
1618 */
Nathan Scottf51623b2006-03-14 13:26:27 +11001619STATIC int
Nick Piggind79689c2007-10-16 01:25:06 -07001620xfs_vm_write_begin(
Nathan Scottf51623b2006-03-14 13:26:27 +11001621 struct file *file,
Nick Piggind79689c2007-10-16 01:25:06 -07001622 struct address_space *mapping,
1623 loff_t pos,
1624 unsigned len,
1625 unsigned flags,
1626 struct page **pagep,
1627 void **fsdata)
Nathan Scottf51623b2006-03-14 13:26:27 +11001628{
Dave Chinnerd3bc8152012-04-27 19:45:21 +10001629 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
1630 struct page *page;
1631 int status;
Christoph Hellwig155130a2010-06-04 11:29:58 +02001632
Dave Chinnerd3bc8152012-04-27 19:45:21 +10001633 ASSERT(len <= PAGE_CACHE_SIZE);
1634
Dave Chinnerad22c7a2013-10-29 22:11:57 +11001635 page = grab_cache_page_write_begin(mapping, index, flags);
Dave Chinnerd3bc8152012-04-27 19:45:21 +10001636 if (!page)
1637 return -ENOMEM;
1638
1639 status = __block_write_begin(page, pos, len, xfs_get_blocks);
1640 if (unlikely(status)) {
1641 struct inode *inode = mapping->host;
Dave Chinner72ab70a2014-04-14 18:13:29 +10001642 size_t isize = i_size_read(inode);
Dave Chinnerd3bc8152012-04-27 19:45:21 +10001643
1644 xfs_vm_write_failed(inode, page, pos, len);
1645 unlock_page(page);
1646
Dave Chinner72ab70a2014-04-14 18:13:29 +10001647 /*
1648 * If the write is beyond EOF, we only want to kill blocks
1649 * allocated in this write, not blocks that were previously
1650 * written successfully.
1651 */
1652 if (pos + len > isize) {
1653 ssize_t start = max_t(ssize_t, pos, isize);
1654
1655 truncate_pagecache_range(inode, start, pos + len);
1656 }
Dave Chinnerd3bc8152012-04-27 19:45:21 +10001657
1658 page_cache_release(page);
1659 page = NULL;
1660 }
1661
1662 *pagep = page;
1663 return status;
Christoph Hellwigfa9b2272010-06-14 05:17:31 -04001664}
Christoph Hellwig155130a2010-06-04 11:29:58 +02001665
Dave Chinnerd3bc8152012-04-27 19:45:21 +10001666/*
Dave Chinneraad3f372014-04-14 18:14:11 +10001667 * On failure, we only need to kill delalloc blocks beyond EOF in the range of
1668 * this specific write because they will never be written. Previous writes
1669 * beyond EOF where block allocation succeeded do not need to be trashed, so
1670 * only new blocks from this write should be trashed. For blocks within
1671 * EOF, generic_write_end() zeros them so they are safe to leave alone and be
1672 * written with all the other valid data.
Dave Chinnerd3bc8152012-04-27 19:45:21 +10001673 */
Christoph Hellwigfa9b2272010-06-14 05:17:31 -04001674STATIC int
1675xfs_vm_write_end(
1676 struct file *file,
1677 struct address_space *mapping,
1678 loff_t pos,
1679 unsigned len,
1680 unsigned copied,
1681 struct page *page,
1682 void *fsdata)
1683{
1684 int ret;
1685
Dave Chinnerd3bc8152012-04-27 19:45:21 +10001686 ASSERT(len <= PAGE_CACHE_SIZE);
1687
Christoph Hellwigfa9b2272010-06-14 05:17:31 -04001688 ret = generic_write_end(file, mapping, pos, len, copied, page, fsdata);
Dave Chinnerd3bc8152012-04-27 19:45:21 +10001689 if (unlikely(ret < len)) {
1690 struct inode *inode = mapping->host;
1691 size_t isize = i_size_read(inode);
1692 loff_t to = pos + len;
1693
1694 if (to > isize) {
Dave Chinneraad3f372014-04-14 18:14:11 +10001695 /* only kill blocks in this write beyond EOF */
1696 if (pos > isize)
1697 isize = pos;
Dave Chinnerd3bc8152012-04-27 19:45:21 +10001698 xfs_vm_kill_delalloc_range(inode, isize, to);
Dave Chinneraad3f372014-04-14 18:14:11 +10001699 truncate_pagecache_range(inode, isize, to);
Dave Chinnerd3bc8152012-04-27 19:45:21 +10001700 }
1701 }
Christoph Hellwig155130a2010-06-04 11:29:58 +02001702 return ret;
Nathan Scottf51623b2006-03-14 13:26:27 +11001703}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704
1705STATIC sector_t
Nathan Scotte4c573b2006-03-14 13:54:26 +11001706xfs_vm_bmap(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 struct address_space *mapping,
1708 sector_t block)
1709{
1710 struct inode *inode = (struct inode *)mapping->host;
Christoph Hellwig739bfb22007-08-29 10:58:01 +10001711 struct xfs_inode *ip = XFS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
Christoph Hellwigcca28fb2010-06-24 11:57:09 +10001713 trace_xfs_vm_bmap(XFS_I(inode));
Christoph Hellwig126468b2008-03-06 13:44:57 +11001714 xfs_ilock(ip, XFS_IOLOCK_SHARED);
Dave Chinner4bc1ea62012-11-12 22:53:56 +11001715 filemap_write_and_wait(mapping);
Christoph Hellwig126468b2008-03-06 13:44:57 +11001716 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
Nathan Scottc2536662006-03-29 10:44:40 +10001717 return generic_block_bmap(mapping, block, xfs_get_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718}
1719
1720STATIC int
Nathan Scotte4c573b2006-03-14 13:54:26 +11001721xfs_vm_readpage(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 struct file *unused,
1723 struct page *page)
1724{
Dave Chinner121e2132016-01-08 11:28:35 +11001725 trace_xfs_vm_readpage(page->mapping->host, 1);
Nathan Scottc2536662006-03-29 10:44:40 +10001726 return mpage_readpage(page, xfs_get_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727}
1728
1729STATIC int
Nathan Scotte4c573b2006-03-14 13:54:26 +11001730xfs_vm_readpages(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 struct file *unused,
1732 struct address_space *mapping,
1733 struct list_head *pages,
1734 unsigned nr_pages)
1735{
Dave Chinner121e2132016-01-08 11:28:35 +11001736 trace_xfs_vm_readpages(mapping->host, nr_pages);
Nathan Scottc2536662006-03-29 10:44:40 +10001737 return mpage_readpages(mapping, pages, nr_pages, xfs_get_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738}
1739
Dave Chinner22e757a2014-09-02 12:12:51 +10001740/*
1741 * This is basically a copy of __set_page_dirty_buffers() with one
1742 * small tweak: buffers beyond EOF do not get marked dirty. If we mark them
1743 * dirty, we'll never be able to clean them because we don't write buffers
1744 * beyond EOF, and that means we can't invalidate pages that span EOF
1745 * that have been marked dirty. Further, the dirty state can leak into
1746 * the file interior if the file is extended, resulting in all sorts of
1747 * bad things happening as the state does not match the underlying data.
1748 *
1749 * XXX: this really indicates that bufferheads in XFS need to die. Warts like
1750 * this only exist because of bufferheads and how the generic code manages them.
1751 */
1752STATIC int
1753xfs_vm_set_page_dirty(
1754 struct page *page)
1755{
1756 struct address_space *mapping = page->mapping;
1757 struct inode *inode = mapping->host;
1758 loff_t end_offset;
1759 loff_t offset;
1760 int newly_dirty;
Greg Thelenc4843a72015-05-22 17:13:16 -04001761 struct mem_cgroup *memcg;
Dave Chinner22e757a2014-09-02 12:12:51 +10001762
1763 if (unlikely(!mapping))
1764 return !TestSetPageDirty(page);
1765
1766 end_offset = i_size_read(inode);
1767 offset = page_offset(page);
1768
1769 spin_lock(&mapping->private_lock);
1770 if (page_has_buffers(page)) {
1771 struct buffer_head *head = page_buffers(page);
1772 struct buffer_head *bh = head;
1773
1774 do {
1775 if (offset < end_offset)
1776 set_buffer_dirty(bh);
1777 bh = bh->b_this_page;
1778 offset += 1 << inode->i_blkbits;
1779 } while (bh != head);
1780 }
Greg Thelenc4843a72015-05-22 17:13:16 -04001781 /*
1782 * Use mem_group_begin_page_stat() to keep PageDirty synchronized with
1783 * per-memcg dirty page counters.
1784 */
1785 memcg = mem_cgroup_begin_page_stat(page);
Dave Chinner22e757a2014-09-02 12:12:51 +10001786 newly_dirty = !TestSetPageDirty(page);
1787 spin_unlock(&mapping->private_lock);
1788
1789 if (newly_dirty) {
1790 /* sigh - __set_page_dirty() is static, so copy it here, too */
1791 unsigned long flags;
1792
1793 spin_lock_irqsave(&mapping->tree_lock, flags);
1794 if (page->mapping) { /* Race with truncate? */
1795 WARN_ON_ONCE(!PageUptodate(page));
Greg Thelenc4843a72015-05-22 17:13:16 -04001796 account_page_dirtied(page, mapping, memcg);
Dave Chinner22e757a2014-09-02 12:12:51 +10001797 radix_tree_tag_set(&mapping->page_tree,
1798 page_index(page), PAGECACHE_TAG_DIRTY);
1799 }
1800 spin_unlock_irqrestore(&mapping->tree_lock, flags);
Dave Chinner22e757a2014-09-02 12:12:51 +10001801 }
Greg Thelenc4843a72015-05-22 17:13:16 -04001802 mem_cgroup_end_page_stat(memcg);
1803 if (newly_dirty)
1804 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
Dave Chinner22e757a2014-09-02 12:12:51 +10001805 return newly_dirty;
1806}
1807
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07001808const struct address_space_operations xfs_address_space_operations = {
Nathan Scotte4c573b2006-03-14 13:54:26 +11001809 .readpage = xfs_vm_readpage,
1810 .readpages = xfs_vm_readpages,
1811 .writepage = xfs_vm_writepage,
Nathan Scott7d4fb402006-06-09 15:27:16 +10001812 .writepages = xfs_vm_writepages,
Dave Chinner22e757a2014-09-02 12:12:51 +10001813 .set_page_dirty = xfs_vm_set_page_dirty,
Nathan Scott238f4c52006-03-17 17:26:25 +11001814 .releasepage = xfs_vm_releasepage,
1815 .invalidatepage = xfs_vm_invalidatepage,
Nick Piggind79689c2007-10-16 01:25:06 -07001816 .write_begin = xfs_vm_write_begin,
Christoph Hellwigfa9b2272010-06-14 05:17:31 -04001817 .write_end = xfs_vm_write_end,
Nathan Scotte4c573b2006-03-14 13:54:26 +11001818 .bmap = xfs_vm_bmap,
1819 .direct_IO = xfs_vm_direct_IO,
Christoph Lametere965f962006-02-01 03:05:41 -08001820 .migratepage = buffer_migrate_page,
Hisashi Hifumibddaafa2009-03-29 09:53:38 +02001821 .is_partially_uptodate = block_is_partially_uptodate,
Andi Kleenaa261f52009-09-16 11:50:16 +02001822 .error_remove_page = generic_error_remove_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823};