blob: f75c7c99cb630912dc596ef628a782c5fdec4fa4 [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 Chinner70a9883c2013-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 Chinner150d5be2016-02-15 17:21:12 +1100729static int
730xfs_writepage_submit(
Dave Chinnerfbcc0252016-02-15 17:21:19 +1100731 struct xfs_writepage_ctx *wpc,
Dave Chinner150d5be2016-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 Chinner150d5be2016-02-15 17:21:12 +1100741
Dave Chinnerfbcc0252016-02-15 17:21:19 +1100742 if (wpc->iohead) {
Dave Chinner150d5be2016-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 Chinner150d5be2016-02-15 17:21:12 +1100745 blk_finish_plug(&plug);
746 }
747 return status;
748}
749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750/*
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000751 * Write out a dirty page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 *
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000753 * For delalloc space on the page we need to allocate space and flush it.
754 * For unwritten space on the page we need to start the conversion to
755 * regular allocated space.
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000756 * For any other dirty buffer heads on the page we should flush them.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758STATIC int
Dave Chinnerfbcc0252016-02-15 17:21:19 +1100759xfs_do_writepage(
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000760 struct page *page,
Dave Chinnerfbcc0252016-02-15 17:21:19 +1100761 struct writeback_control *wbc,
762 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
Dave Chinnerfbcc0252016-02-15 17:21:19 +1100764 struct xfs_writepage_ctx *wpc = data;
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000765 struct inode *inode = page->mapping->host;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100766 struct buffer_head *bh, *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 loff_t offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 __uint64_t end_offset;
Dave Chinnerad689722016-02-15 17:21:31 +1100769 pgoff_t end_index;
Christoph Hellwiged1e7b72010-12-10 08:42:22 +0000770 ssize_t len;
Dave Chinnerfbcc0252016-02-15 17:21:19 +1100771 int err, uptodate = 1;
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000772 int count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Lukas Czerner34097df2013-05-21 23:58:01 -0400774 trace_xfs_writepage(inode, page, 0, 0);
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000775
Christoph Hellwig20cb52e2010-06-24 09:46:01 +1000776 ASSERT(page_has_buffers(page));
777
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000778 /*
779 * Refuse to write the page out if we are called from reclaim context.
780 *
Christoph Hellwigd4f7a5c2010-06-28 10:34:44 -0400781 * This avoids stack overflows when called from deeply used stacks in
782 * random callers for direct reclaim or memcg reclaim. We explicitly
783 * allow reclaim from kswapd as the stack usage there is relatively low.
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000784 *
Mel Gorman94054fa2011-10-31 17:07:45 -0700785 * This should never happen except in the case of a VM regression so
786 * warn about it.
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000787 */
Mel Gorman94054fa2011-10-31 17:07:45 -0700788 if (WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD)) ==
789 PF_MEMALLOC))
Christoph Hellwigb5420f22010-08-24 11:47:51 +1000790 goto redirty;
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000791
792 /*
Christoph Hellwig680a6472011-07-08 14:34:05 +0200793 * Given that we do not allow direct reclaim to call us, we should
794 * never be called while in a filesystem transaction.
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000795 */
Christoph Hellwig448011e2014-06-06 16:05:15 +1000796 if (WARN_ON_ONCE(current->flags & PF_FSTRANS))
Christoph Hellwigb5420f22010-08-24 11:47:51 +1000797 goto redirty;
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000798
Jie Liu8695d272014-05-20 08:24:26 +1000799 /*
Dave Chinnerad689722016-02-15 17:21:31 +1100800 * Is this page beyond the end of the file?
801 *
Jie Liu8695d272014-05-20 08:24:26 +1000802 * The page index is less than the end_index, adjust the end_offset
803 * to the highest offset that this page should represent.
804 * -----------------------------------------------------
805 * | file mapping | <EOF> |
806 * -----------------------------------------------------
807 * | Page ... | Page N-2 | Page N-1 | Page N | |
808 * ^--------------------------------^----------|--------
809 * | desired writeback range | see else |
810 * ---------------------------------^------------------|
811 */
Dave Chinnerad689722016-02-15 17:21:31 +1100812 offset = i_size_read(inode);
813 end_index = offset >> PAGE_CACHE_SHIFT;
Jie Liu8695d272014-05-20 08:24:26 +1000814 if (page->index < end_index)
815 end_offset = (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT;
816 else {
817 /*
818 * Check whether the page to write out is beyond or straddles
819 * i_size or not.
820 * -------------------------------------------------------
821 * | file mapping | <EOF> |
822 * -------------------------------------------------------
823 * | Page ... | Page N-2 | Page N-1 | Page N | Beyond |
824 * ^--------------------------------^-----------|---------
825 * | | Straddles |
826 * ---------------------------------^-----------|--------|
827 */
Christoph Hellwig6b7a03f2012-07-03 12:20:00 -0400828 unsigned offset_into_page = offset & (PAGE_CACHE_SIZE - 1);
829
830 /*
Jan Karaff9a28f2013-03-14 14:30:54 +0100831 * Skip the page if it is fully outside i_size, e.g. due to a
832 * truncate operation that is in progress. We must redirty the
833 * page so that reclaim stops reclaiming it. Otherwise
834 * xfs_vm_releasepage() is called on it and gets confused.
Jie Liu8695d272014-05-20 08:24:26 +1000835 *
836 * Note that the end_index is unsigned long, it would overflow
837 * if the given offset is greater than 16TB on 32-bit system
838 * and if we do check the page is fully outside i_size or not
839 * via "if (page->index >= end_index + 1)" as "end_index + 1"
840 * will be evaluated to 0. Hence this page will be redirtied
841 * and be written out repeatedly which would result in an
842 * infinite loop, the user program that perform this operation
843 * will hang. Instead, we can verify this situation by checking
844 * if the page to write is totally beyond the i_size or if it's
845 * offset is just equal to the EOF.
Christoph Hellwig6b7a03f2012-07-03 12:20:00 -0400846 */
Jie Liu8695d272014-05-20 08:24:26 +1000847 if (page->index > end_index ||
848 (page->index == end_index && offset_into_page == 0))
Jan Karaff9a28f2013-03-14 14:30:54 +0100849 goto redirty;
Christoph Hellwig6b7a03f2012-07-03 12:20:00 -0400850
851 /*
852 * The page straddles i_size. It must be zeroed out on each
853 * and every writepage invocation because it may be mmapped.
854 * "A file is mapped in multiples of the page size. For a file
Jie Liu8695d272014-05-20 08:24:26 +1000855 * that is not a multiple of the page size, the remaining
Christoph Hellwig6b7a03f2012-07-03 12:20:00 -0400856 * memory is zeroed when mapped, and writes to that region are
857 * not written out to the file."
858 */
859 zero_user_segment(page, offset_into_page, PAGE_CACHE_SIZE);
Jie Liu8695d272014-05-20 08:24:26 +1000860
861 /* Adjust the end_offset to the end of file */
862 end_offset = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 }
864
Nathan Scott24e17b52005-05-05 13:33:20 -0700865 len = 1 << inode->i_blkbits;
Nathan Scott24e17b52005-05-05 13:33:20 -0700866
Nathan Scott24e17b52005-05-05 13:33:20 -0700867 bh = head = page_buffers(page);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100868 offset = page_offset(page);
Christoph Hellwiga206c812010-12-10 08:42:20 +0000869
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 do {
871 if (offset >= end_offset)
872 break;
873 if (!buffer_uptodate(bh))
874 uptodate = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Eric Sandeen3d9b02e2010-06-24 09:45:30 +1000876 /*
Christoph Hellwigece413f2010-11-10 21:39:11 +0000877 * set_page_dirty dirties all buffers in a page, independent
878 * of their state. The dirty state however is entirely
879 * meaningless for holes (!mapped && uptodate), so skip
880 * buffers covering holes here.
Eric Sandeen3d9b02e2010-06-24 09:45:30 +1000881 */
882 if (!buffer_mapped(bh) && buffer_uptodate(bh)) {
Dave Chinnerfbcc0252016-02-15 17:21:19 +1100883 wpc->imap_valid = false;
Eric Sandeen3d9b02e2010-06-24 09:45:30 +1000884 continue;
885 }
886
Christoph Hellwigaeea1b12010-12-10 08:42:24 +0000887 if (buffer_unwritten(bh)) {
Dave Chinnerfbcc0252016-02-15 17:21:19 +1100888 if (wpc->io_type != XFS_IO_UNWRITTEN) {
889 wpc->io_type = XFS_IO_UNWRITTEN;
890 wpc->imap_valid = false;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100891 }
Christoph Hellwigaeea1b12010-12-10 08:42:24 +0000892 } else if (buffer_delay(bh)) {
Dave Chinnerfbcc0252016-02-15 17:21:19 +1100893 if (wpc->io_type != XFS_IO_DELALLOC) {
894 wpc->io_type = XFS_IO_DELALLOC;
895 wpc->imap_valid = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 }
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000897 } else if (buffer_uptodate(bh)) {
Dave Chinnerfbcc0252016-02-15 17:21:19 +1100898 if (wpc->io_type != XFS_IO_OVERWRITE) {
899 wpc->io_type = XFS_IO_OVERWRITE;
900 wpc->imap_valid = false;
Christoph Hellwig85da94c2010-12-10 08:42:16 +0000901 }
Christoph Hellwigaeea1b12010-12-10 08:42:24 +0000902 } else {
Alain Renaud7d0fa3e2012-06-08 15:34:46 -0400903 if (PageUptodate(page))
Christoph Hellwigaeea1b12010-12-10 08:42:24 +0000904 ASSERT(buffer_mapped(bh));
Alain Renaud7d0fa3e2012-06-08 15:34:46 -0400905 /*
906 * This buffer is not uptodate and will not be
907 * written to disk. Ensure that we will put any
908 * subsequent writeable buffers into a new
909 * ioend.
910 */
Dave Chinnerfbcc0252016-02-15 17:21:19 +1100911 wpc->imap_valid = 0;
Christoph Hellwigaeea1b12010-12-10 08:42:24 +0000912 continue;
913 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Dave Chinnerfbcc0252016-02-15 17:21:19 +1100915 if (wpc->imap_valid)
916 wpc->imap_valid = xfs_imap_valid(inode, &wpc->imap, offset);
917 if (!wpc->imap_valid) {
918 err = xfs_map_blocks(inode, offset, &wpc->imap,
919 wpc->io_type);
Christoph Hellwigaeea1b12010-12-10 08:42:24 +0000920 if (err)
921 goto error;
Dave Chinnerfbcc0252016-02-15 17:21:19 +1100922 wpc->imap_valid = xfs_imap_valid(inode, &wpc->imap, offset);
Christoph Hellwigaeea1b12010-12-10 08:42:24 +0000923 }
Dave Chinnerfbcc0252016-02-15 17:21:19 +1100924 if (wpc->imap_valid) {
Christoph Hellwigecff71e2010-12-10 08:42:25 +0000925 lock_buffer(bh);
Dave Chinnerfbcc0252016-02-15 17:21:19 +1100926 if (wpc->io_type != XFS_IO_OVERWRITE)
927 xfs_map_at_offset(inode, bh, &wpc->imap, offset);
928 xfs_add_to_ioend(inode, bh, offset, wpc);
Christoph Hellwigaeea1b12010-12-10 08:42:24 +0000929 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 }
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100931
Dave Chinnerfbcc0252016-02-15 17:21:19 +1100932 if (!wpc->iohead)
933 wpc->iohead = wpc->ioend;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100934
935 } while (offset += len, ((bh = bh->b_this_page) != head));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
937 if (uptodate && bh == head)
938 SetPageUptodate(page);
939
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000940 xfs_start_page_writeback(page, 1, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Dave Chinnerad689722016-02-15 17:21:31 +1100942 ASSERT(wpc->iohead || !count);
Dave Chinnerfbcc0252016-02-15 17:21:19 +1100943 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945error:
Dave Chinner150d5be2016-02-15 17:21:12 +1100946 /*
947 * On error, we have to fail the iohead here because we buffers locked
948 * in the ioend chain. If we don't do this, we'll deadlock invalidating
949 * the page as that tries to lock the buffers on the page. Also, because
Dave Chinnerfbcc0252016-02-15 17:21:19 +1100950 * we may have set pages under writeback, we have to make sure we run
951 * IO completion to mark the error state of the IO appropriately, so we
952 * can't cancel the ioend directly here. That means we have to mark this
953 * page as under writeback if we included any buffers from it in the
954 * ioend chain so that completion treats it correctly.
955 *
956 * If we didn't include the page in the ioend, then we can simply
957 * discard and unlock it as there are no other users of the page or it's
958 * buffers right now. The caller will still need to trigger submission
959 * of outstanding ioends on the writepage context so they are treated
960 * correctly on error.
Dave Chinner150d5be2016-02-15 17:21:12 +1100961 */
962 if (count)
963 xfs_start_page_writeback(page, 0, count);
Dave Chinnerfbcc0252016-02-15 17:21:19 +1100964 else {
Dave Chinner150d5be2016-02-15 17:21:12 +1100965 xfs_aops_discard_page(page);
966 ClearPageUptodate(page);
967 unlock_page(page);
968 }
969 mapping_set_error(page->mapping, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 return err;
Nathan Scottf51623b2006-03-14 13:26:27 +1100971
Christoph Hellwigb5420f22010-08-24 11:47:51 +1000972redirty:
Nathan Scottf51623b2006-03-14 13:26:27 +1100973 redirty_page_for_writepage(wbc, page);
974 unlock_page(page);
975 return 0;
Nathan Scottf51623b2006-03-14 13:26:27 +1100976}
977
Nathan Scott7d4fb402006-06-09 15:27:16 +1000978STATIC int
Dave Chinnerfbcc0252016-02-15 17:21:19 +1100979xfs_vm_writepage(
980 struct page *page,
981 struct writeback_control *wbc)
982{
983 struct xfs_writepage_ctx wpc = {
984 .io_type = XFS_IO_INVALID,
985 };
986 int ret;
987
988 ret = xfs_do_writepage(page, wbc, &wpc);
989 return xfs_writepage_submit(&wpc, wbc, ret);
990}
991
992STATIC int
Nathan Scott7d4fb402006-06-09 15:27:16 +1000993xfs_vm_writepages(
994 struct address_space *mapping,
995 struct writeback_control *wbc)
996{
Dave Chinnerfbcc0252016-02-15 17:21:19 +1100997 struct xfs_writepage_ctx wpc = {
998 .io_type = XFS_IO_INVALID,
999 };
1000 int ret;
1001
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +10001002 xfs_iflags_clear(XFS_I(mapping->host), XFS_ITRUNCATED);
Dave Chinnerfbcc0252016-02-15 17:21:19 +11001003 ret = write_cache_pages(mapping, wbc, xfs_do_writepage, &wpc);
1004 return xfs_writepage_submit(&wpc, wbc, ret);
Nathan Scott7d4fb402006-06-09 15:27:16 +10001005}
1006
Nathan Scottf51623b2006-03-14 13:26:27 +11001007/*
1008 * Called to move a page into cleanable state - and from there
Christoph Hellwig89f3b3632010-06-24 09:45:48 +10001009 * to be released. The page should already be clean. We always
Nathan Scottf51623b2006-03-14 13:26:27 +11001010 * have buffer heads in this call.
1011 *
Christoph Hellwig89f3b3632010-06-24 09:45:48 +10001012 * Returns 1 if the page is ok to release, 0 otherwise.
Nathan Scottf51623b2006-03-14 13:26:27 +11001013 */
1014STATIC int
Nathan Scott238f4c52006-03-17 17:26:25 +11001015xfs_vm_releasepage(
Nathan Scottf51623b2006-03-14 13:26:27 +11001016 struct page *page,
1017 gfp_t gfp_mask)
1018{
Christoph Hellwig20cb52e2010-06-24 09:46:01 +10001019 int delalloc, unwritten;
Nathan Scottf51623b2006-03-14 13:26:27 +11001020
Lukas Czerner34097df2013-05-21 23:58:01 -04001021 trace_xfs_releasepage(page->mapping->host, page, 0, 0);
Nathan Scott238f4c52006-03-17 17:26:25 +11001022
Christoph Hellwig20cb52e2010-06-24 09:46:01 +10001023 xfs_count_page_state(page, &delalloc, &unwritten);
Nathan Scottf51623b2006-03-14 13:26:27 +11001024
Christoph Hellwig448011e2014-06-06 16:05:15 +10001025 if (WARN_ON_ONCE(delalloc))
Christoph Hellwig89f3b3632010-06-24 09:45:48 +10001026 return 0;
Christoph Hellwig448011e2014-06-06 16:05:15 +10001027 if (WARN_ON_ONCE(unwritten))
Nathan Scottf51623b2006-03-14 13:26:27 +11001028 return 0;
1029
Nathan Scottf51623b2006-03-14 13:26:27 +11001030 return try_to_free_buffers(page);
1031}
1032
Dave Chinnera7193702015-04-16 21:57:48 +10001033/*
Dave Chinnera06c2772015-04-16 22:00:00 +10001034 * When we map a DIO buffer, we may need to attach an ioend that describes the
1035 * type of write IO we are doing. This passes to the completion function the
1036 * operations it needs to perform. If the mapping is for an overwrite wholly
1037 * within the EOF then we don't need an ioend and so we don't allocate one.
1038 * This avoids the unnecessary overhead of allocating and freeing ioends for
1039 * workloads that don't require transactions on IO completion.
Dave Chinnerd5cc2e32015-04-16 21:59:07 +10001040 *
1041 * If we get multiple mappings in a single IO, we might be mapping different
1042 * types. But because the direct IO can only have a single private pointer, we
1043 * need to ensure that:
1044 *
Dave Chinnera06c2772015-04-16 22:00:00 +10001045 * a) i) the ioend spans the entire region of unwritten mappings; or
1046 * ii) the ioend spans all the mappings that cross or are beyond EOF; and
Dave Chinnerd5cc2e32015-04-16 21:59:07 +10001047 * b) if it contains unwritten extents, it is *permanently* marked as such
1048 *
1049 * We could do this by chaining ioends like buffered IO does, but we only
1050 * actually get one IO completion callback from the direct IO, and that spans
1051 * the entire IO regardless of how many mappings and IOs are needed to complete
1052 * the DIO. There is only going to be one reference to the ioend and its life
1053 * cycle is constrained by the DIO completion code. hence we don't need
1054 * reference counting here.
Dave Chinner3e12dbb2015-11-03 12:27:22 +11001055 *
1056 * Note that for DIO, an IO to the highest supported file block offset (i.e.
1057 * 2^63 - 1FSB bytes) will result in the offset + count overflowing a signed 64
1058 * bit variable. Hence if we see this overflow, we have to assume that the IO is
1059 * extending the file size. We won't know for sure until IO completion is run
1060 * and the actual max write offset is communicated to the IO completion
1061 * routine.
1062 *
1063 * For DAX page faults, we are preparing to never see unwritten extents here,
1064 * nor should we ever extend the inode size. Hence we will soon have nothing to
1065 * do here for this case, ensuring we don't have to provide an IO completion
1066 * callback to free an ioend that we don't actually need for a fault into the
1067 * page at offset (2^63 - 1FSB) bytes.
Dave Chinnera7193702015-04-16 21:57:48 +10001068 */
Dave Chinner3e12dbb2015-11-03 12:27:22 +11001069
Dave Chinnera7193702015-04-16 21:57:48 +10001070static void
1071xfs_map_direct(
1072 struct inode *inode,
1073 struct buffer_head *bh_result,
1074 struct xfs_bmbt_irec *imap,
Dave Chinner3e12dbb2015-11-03 12:27:22 +11001075 xfs_off_t offset,
1076 bool dax_fault)
Dave Chinnera7193702015-04-16 21:57:48 +10001077{
Dave Chinnerd5cc2e32015-04-16 21:59:07 +10001078 struct xfs_ioend *ioend;
1079 xfs_off_t size = bh_result->b_size;
1080 int type;
1081
1082 if (ISUNWRITTEN(imap))
1083 type = XFS_IO_UNWRITTEN;
1084 else
1085 type = XFS_IO_OVERWRITE;
1086
1087 trace_xfs_gbmap_direct(XFS_I(inode), offset, size, type, imap);
1088
Dave Chinner3e12dbb2015-11-03 12:27:22 +11001089 if (dax_fault) {
1090 ASSERT(type == XFS_IO_OVERWRITE);
1091 trace_xfs_gbmap_direct_none(XFS_I(inode), offset, size, type,
1092 imap);
1093 return;
1094 }
Dave Chinner3e12dbb2015-11-03 12:27:22 +11001095
Dave Chinnerd5cc2e32015-04-16 21:59:07 +10001096 if (bh_result->b_private) {
1097 ioend = bh_result->b_private;
1098 ASSERT(ioend->io_size > 0);
1099 ASSERT(offset >= ioend->io_offset);
1100 if (offset + size > ioend->io_offset + ioend->io_size)
1101 ioend->io_size = offset - ioend->io_offset + size;
1102
1103 if (type == XFS_IO_UNWRITTEN && type != ioend->io_type)
1104 ioend->io_type = XFS_IO_UNWRITTEN;
1105
1106 trace_xfs_gbmap_direct_update(XFS_I(inode), ioend->io_offset,
1107 ioend->io_size, ioend->io_type,
1108 imap);
Dave Chinnera06c2772015-04-16 22:00:00 +10001109 } else if (type == XFS_IO_UNWRITTEN ||
Dave Chinner3e12dbb2015-11-03 12:27:22 +11001110 offset + size > i_size_read(inode) ||
1111 offset + size < 0) {
Dave Chinnerd5cc2e32015-04-16 21:59:07 +10001112 ioend = xfs_alloc_ioend(inode, type);
1113 ioend->io_offset = offset;
1114 ioend->io_size = size;
Dave Chinnera06c2772015-04-16 22:00:00 +10001115
Dave Chinnerd5cc2e32015-04-16 21:59:07 +10001116 bh_result->b_private = ioend;
Dave Chinnera06c2772015-04-16 22:00:00 +10001117 set_buffer_defer_completion(bh_result);
Dave Chinnerd5cc2e32015-04-16 21:59:07 +10001118
1119 trace_xfs_gbmap_direct_new(XFS_I(inode), offset, size, type,
1120 imap);
Dave Chinnera06c2772015-04-16 22:00:00 +10001121 } else {
1122 trace_xfs_gbmap_direct_none(XFS_I(inode), offset, size, type,
1123 imap);
Dave Chinnera7193702015-04-16 21:57:48 +10001124 }
1125}
1126
Dave Chinner1fdca9c2015-04-16 21:58:21 +10001127/*
1128 * If this is O_DIRECT or the mpage code calling tell them how large the mapping
1129 * is, so that we can avoid repeated get_blocks calls.
1130 *
1131 * If the mapping spans EOF, then we have to break the mapping up as the mapping
1132 * for blocks beyond EOF must be marked new so that sub block regions can be
1133 * correctly zeroed. We can't do this for mappings within EOF unless the mapping
1134 * was just allocated or is unwritten, otherwise the callers would overwrite
1135 * existing data with zeros. Hence we have to split the mapping into a range up
1136 * to and including EOF, and a second mapping for beyond EOF.
1137 */
1138static void
1139xfs_map_trim_size(
1140 struct inode *inode,
1141 sector_t iblock,
1142 struct buffer_head *bh_result,
1143 struct xfs_bmbt_irec *imap,
1144 xfs_off_t offset,
1145 ssize_t size)
1146{
1147 xfs_off_t mapping_size;
1148
1149 mapping_size = imap->br_startoff + imap->br_blockcount - iblock;
1150 mapping_size <<= inode->i_blkbits;
1151
1152 ASSERT(mapping_size > 0);
1153 if (mapping_size > size)
1154 mapping_size = size;
1155 if (offset < i_size_read(inode) &&
1156 offset + mapping_size >= i_size_read(inode)) {
1157 /* limit mapping to block that spans EOF */
1158 mapping_size = roundup_64(i_size_read(inode) - offset,
1159 1 << inode->i_blkbits);
1160 }
1161 if (mapping_size > LONG_MAX)
1162 mapping_size = LONG_MAX;
1163
1164 bh_result->b_size = mapping_size;
1165}
1166
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167STATIC int
Nathan Scottc2536662006-03-29 10:44:40 +10001168__xfs_get_blocks(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 struct inode *inode,
1170 sector_t iblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 struct buffer_head *bh_result,
1172 int create,
Dave Chinner3e12dbb2015-11-03 12:27:22 +11001173 bool direct,
1174 bool dax_fault)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175{
Christoph Hellwiga206c812010-12-10 08:42:20 +00001176 struct xfs_inode *ip = XFS_I(inode);
1177 struct xfs_mount *mp = ip->i_mount;
1178 xfs_fileoff_t offset_fsb, end_fsb;
1179 int error = 0;
1180 int lockmode = 0;
Christoph Hellwig207d0412010-04-28 12:28:56 +00001181 struct xfs_bmbt_irec imap;
Christoph Hellwiga206c812010-12-10 08:42:20 +00001182 int nimaps = 1;
Nathan Scottfdc7ed72005-11-02 15:13:13 +11001183 xfs_off_t offset;
1184 ssize_t size;
Christoph Hellwig207d0412010-04-28 12:28:56 +00001185 int new = 0;
Christoph Hellwiga206c812010-12-10 08:42:20 +00001186
1187 if (XFS_FORCED_SHUTDOWN(mp))
Eric Sandeenb474c7a2014-06-22 15:04:54 +10001188 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
Nathan Scottfdc7ed72005-11-02 15:13:13 +11001190 offset = (xfs_off_t)iblock << inode->i_blkbits;
Nathan Scottc2536662006-03-29 10:44:40 +10001191 ASSERT(bh_result->b_size >= (1 << inode->i_blkbits));
1192 size = bh_result->b_size;
Lachlan McIlroy364f3582008-09-17 16:50:14 +10001193
1194 if (!create && direct && offset >= i_size_read(inode))
1195 return 0;
1196
Dave Chinner507630b2012-03-27 10:34:50 -04001197 /*
1198 * Direct I/O is usually done on preallocated files, so try getting
1199 * a block mapping without an exclusive lock first. For buffered
1200 * writes we already have the exclusive iolock anyway, so avoiding
1201 * a lock roundtrip here by taking the ilock exclusive from the
1202 * beginning is a useful micro optimization.
1203 */
1204 if (create && !direct) {
Christoph Hellwiga206c812010-12-10 08:42:20 +00001205 lockmode = XFS_ILOCK_EXCL;
1206 xfs_ilock(ip, lockmode);
1207 } else {
Christoph Hellwig309ecac2013-12-06 12:30:09 -08001208 lockmode = xfs_ilock_data_map_shared(ip);
Christoph Hellwiga206c812010-12-10 08:42:20 +00001209 }
Christoph Hellwigf2bde9b2010-06-24 11:44:35 +10001210
Dave Chinnerd2c28192012-06-08 15:44:53 +10001211 ASSERT(offset <= mp->m_super->s_maxbytes);
1212 if (offset + size > mp->m_super->s_maxbytes)
1213 size = mp->m_super->s_maxbytes - offset;
Christoph Hellwiga206c812010-12-10 08:42:20 +00001214 end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + size);
1215 offset_fsb = XFS_B_TO_FSBT(mp, offset);
1216
Dave Chinner5c8ed202011-09-18 20:40:45 +00001217 error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb,
1218 &imap, &nimaps, XFS_BMAPI_ENTIRE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 if (error)
Christoph Hellwiga206c812010-12-10 08:42:20 +00001220 goto out_unlock;
1221
Dave Chinner1ca19152015-11-03 12:37:00 +11001222 /* for DAX, we convert unwritten extents directly */
Christoph Hellwiga206c812010-12-10 08:42:20 +00001223 if (create &&
1224 (!nimaps ||
1225 (imap.br_startblock == HOLESTARTBLOCK ||
Dave Chinner1ca19152015-11-03 12:37:00 +11001226 imap.br_startblock == DELAYSTARTBLOCK) ||
1227 (IS_DAX(inode) && ISUNWRITTEN(&imap)))) {
Dave Chinneraff3a9e2012-04-23 15:58:44 +10001228 if (direct || xfs_get_extsz_hint(ip)) {
Dave Chinner507630b2012-03-27 10:34:50 -04001229 /*
Brian Foster009c6e82015-10-12 15:34:20 +11001230 * xfs_iomap_write_direct() expects the shared lock. It
1231 * is unlocked on return.
Dave Chinner507630b2012-03-27 10:34:50 -04001232 */
Brian Foster009c6e82015-10-12 15:34:20 +11001233 if (lockmode == XFS_ILOCK_EXCL)
1234 xfs_ilock_demote(ip, lockmode);
1235
Christoph Hellwiga206c812010-12-10 08:42:20 +00001236 error = xfs_iomap_write_direct(ip, offset, size,
1237 &imap, nimaps);
Dave Chinner507630b2012-03-27 10:34:50 -04001238 if (error)
Dave Chinner24513372014-06-25 14:58:08 +10001239 return error;
Dave Chinnerd3bc8152012-04-27 19:45:21 +10001240 new = 1;
Dave Chinner6b698ed2015-06-04 09:18:53 +10001241
Christoph Hellwiga206c812010-12-10 08:42:20 +00001242 } else {
Dave Chinner507630b2012-03-27 10:34:50 -04001243 /*
1244 * Delalloc reservations do not require a transaction,
Dave Chinnerd3bc8152012-04-27 19:45:21 +10001245 * we can go on without dropping the lock here. If we
1246 * are allocating a new delalloc block, make sure that
1247 * we set the new flag so that we mark the buffer new so
1248 * that we know that it is newly allocated if the write
1249 * fails.
Dave Chinner507630b2012-03-27 10:34:50 -04001250 */
Dave Chinnerd3bc8152012-04-27 19:45:21 +10001251 if (nimaps && imap.br_startblock == HOLESTARTBLOCK)
1252 new = 1;
Christoph Hellwiga206c812010-12-10 08:42:20 +00001253 error = xfs_iomap_write_delay(ip, offset, size, &imap);
Dave Chinner507630b2012-03-27 10:34:50 -04001254 if (error)
1255 goto out_unlock;
1256
1257 xfs_iunlock(ip, lockmode);
Christoph Hellwiga206c812010-12-10 08:42:20 +00001258 }
Dave Chinnerd5cc2e32015-04-16 21:59:07 +10001259 trace_xfs_get_blocks_alloc(ip, offset, size,
1260 ISUNWRITTEN(&imap) ? XFS_IO_UNWRITTEN
1261 : XFS_IO_DELALLOC, &imap);
Christoph Hellwiga206c812010-12-10 08:42:20 +00001262 } else if (nimaps) {
Dave Chinnerd5cc2e32015-04-16 21:59:07 +10001263 trace_xfs_get_blocks_found(ip, offset, size,
1264 ISUNWRITTEN(&imap) ? XFS_IO_UNWRITTEN
1265 : XFS_IO_OVERWRITE, &imap);
Dave Chinner507630b2012-03-27 10:34:50 -04001266 xfs_iunlock(ip, lockmode);
Christoph Hellwiga206c812010-12-10 08:42:20 +00001267 } else {
1268 trace_xfs_get_blocks_notfound(ip, offset, size);
1269 goto out_unlock;
1270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
Dave Chinner1ca19152015-11-03 12:37:00 +11001272 if (IS_DAX(inode) && create) {
1273 ASSERT(!ISUNWRITTEN(&imap));
1274 /* zeroing is not needed at a higher layer */
1275 new = 0;
1276 }
1277
Dave Chinner1fdca9c2015-04-16 21:58:21 +10001278 /* trim mapping down to size requested */
1279 if (direct || size > (1 << inode->i_blkbits))
1280 xfs_map_trim_size(inode, iblock, bh_result,
1281 &imap, offset, size);
1282
Dave Chinnera7193702015-04-16 21:57:48 +10001283 /*
1284 * For unwritten extents do not report a disk address in the buffered
1285 * read case (treat as if we're reading into a hole).
1286 */
Christoph Hellwig207d0412010-04-28 12:28:56 +00001287 if (imap.br_startblock != HOLESTARTBLOCK &&
Dave Chinnera7193702015-04-16 21:57:48 +10001288 imap.br_startblock != DELAYSTARTBLOCK &&
1289 (create || !ISUNWRITTEN(&imap))) {
1290 xfs_map_buffer(inode, bh_result, &imap, offset);
1291 if (ISUNWRITTEN(&imap))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 set_buffer_unwritten(bh_result);
Dave Chinnera7193702015-04-16 21:57:48 +10001293 /* direct IO needs special help */
1294 if (create && direct)
Dave Chinner3e12dbb2015-11-03 12:27:22 +11001295 xfs_map_direct(inode, bh_result, &imap, offset,
1296 dax_fault);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 }
1298
Nathan Scottc2536662006-03-29 10:44:40 +10001299 /*
1300 * If this is a realtime file, data may be on a different device.
1301 * to that pointed to from the buffer_head b_bdev currently.
1302 */
Christoph Hellwig046f1682010-04-28 12:28:52 +00001303 bh_result->b_bdev = xfs_find_bdev_for_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
Nathan Scottc2536662006-03-29 10:44:40 +10001305 /*
David Chinner549054a2007-02-10 18:36:35 +11001306 * If we previously allocated a block out beyond eof and we are now
1307 * coming back to use it then we will need to flag it as new even if it
1308 * has a disk address.
1309 *
1310 * With sub-block writes into unwritten extents we also need to mark
1311 * the buffer as new so that the unwritten parts of the buffer gets
1312 * correctly zeroed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 */
1314 if (create &&
1315 ((!buffer_mapped(bh_result) && !buffer_uptodate(bh_result)) ||
David Chinner549054a2007-02-10 18:36:35 +11001316 (offset >= i_size_read(inode)) ||
Christoph Hellwig207d0412010-04-28 12:28:56 +00001317 (new || ISUNWRITTEN(&imap))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 set_buffer_new(bh_result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
Christoph Hellwig207d0412010-04-28 12:28:56 +00001320 if (imap.br_startblock == DELAYSTARTBLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 BUG_ON(direct);
1322 if (create) {
1323 set_buffer_uptodate(bh_result);
1324 set_buffer_mapped(bh_result);
1325 set_buffer_delay(bh_result);
1326 }
1327 }
1328
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 return 0;
Christoph Hellwiga206c812010-12-10 08:42:20 +00001330
1331out_unlock:
1332 xfs_iunlock(ip, lockmode);
Dave Chinner24513372014-06-25 14:58:08 +10001333 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334}
1335
1336int
Nathan Scottc2536662006-03-29 10:44:40 +10001337xfs_get_blocks(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 struct inode *inode,
1339 sector_t iblock,
1340 struct buffer_head *bh_result,
1341 int create)
1342{
Dave Chinner3e12dbb2015-11-03 12:27:22 +11001343 return __xfs_get_blocks(inode, iblock, bh_result, create, false, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344}
1345
Dave Chinner6b698ed2015-06-04 09:18:53 +10001346int
Nathan Scotte4c573b2006-03-14 13:54:26 +11001347xfs_get_blocks_direct(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 struct inode *inode,
1349 sector_t iblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 struct buffer_head *bh_result,
1351 int create)
1352{
Dave Chinner3e12dbb2015-11-03 12:27:22 +11001353 return __xfs_get_blocks(inode, iblock, bh_result, create, true, false);
1354}
1355
1356int
1357xfs_get_blocks_dax_fault(
1358 struct inode *inode,
1359 sector_t iblock,
1360 struct buffer_head *bh_result,
1361 int create)
1362{
1363 return __xfs_get_blocks(inode, iblock, bh_result, create, true, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364}
1365
Dave Chinner6b698ed2015-06-04 09:18:53 +10001366static void
1367__xfs_end_io_direct_write(
1368 struct inode *inode,
1369 struct xfs_ioend *ioend,
Christoph Hellwig209fb872010-07-18 21:17:11 +00001370 loff_t offset,
Dave Chinner6b698ed2015-06-04 09:18:53 +10001371 ssize_t size)
Christoph Hellwigf0973862005-09-05 08:22:52 +10001372{
Dave Chinner6b698ed2015-06-04 09:18:53 +10001373 struct xfs_mount *mp = XFS_I(inode)->i_mount;
Christoph Hellwig2ba66232015-02-02 10:02:09 +11001374
Dave Chinner6b698ed2015-06-04 09:18:53 +10001375 if (XFS_FORCED_SHUTDOWN(mp) || ioend->io_error)
Dave Chinner6dfa1b62015-04-16 21:59:34 +10001376 goto out_end_io;
Christoph Hellwigf0973862005-09-05 08:22:52 +10001377
1378 /*
Dave Chinnerd5cc2e32015-04-16 21:59:07 +10001379 * dio completion end_io functions are only called on writes if more
1380 * than 0 bytes was written.
Christoph Hellwig2813d682011-12-18 20:00:12 +00001381 */
Dave Chinnerd5cc2e32015-04-16 21:59:07 +10001382 ASSERT(size > 0);
1383
1384 /*
1385 * The ioend only maps whole blocks, while the IO may be sector aligned.
Dave Chinnera06c2772015-04-16 22:00:00 +10001386 * Hence the ioend offset/size may not match the IO offset/size exactly.
1387 * Because we don't map overwrites within EOF into the ioend, the offset
1388 * may not match, but only if the endio spans EOF. Either way, write
1389 * the IO sizes into the ioend so that completion processing does the
1390 * right thing.
Dave Chinnerd5cc2e32015-04-16 21:59:07 +10001391 */
Dave Chinnerd5cc2e32015-04-16 21:59:07 +10001392 ASSERT(offset + size <= ioend->io_offset + ioend->io_size);
1393 ioend->io_size = size;
1394 ioend->io_offset = offset;
Christoph Hellwigf0973862005-09-05 08:22:52 +10001395
1396 /*
Dave Chinner6dfa1b62015-04-16 21:59:34 +10001397 * The ioend tells us whether we are doing unwritten extent conversion
1398 * or an append transaction that updates the on-disk file size. These
1399 * cases are the only cases where we should *potentially* be needing
Dave Chinnera06c2772015-04-16 22:00:00 +10001400 * to update the VFS inode size.
Dave Chinner6dfa1b62015-04-16 21:59:34 +10001401 *
1402 * We need to update the in-core inode size here so that we don't end up
Dave Chinnera06c2772015-04-16 22:00:00 +10001403 * with the on-disk inode size being outside the in-core inode size. We
1404 * have no other method of updating EOF for AIO, so always do it here
1405 * if necessary.
Dave Chinnerb9d59842015-04-16 22:03:07 +10001406 *
1407 * We need to lock the test/set EOF update as we can be racing with
1408 * other IO completions here to update the EOF. Failing to serialise
1409 * here can result in EOF moving backwards and Bad Things Happen when
1410 * that occurs.
Christoph Hellwig2813d682011-12-18 20:00:12 +00001411 */
Dave Chinner6b698ed2015-06-04 09:18:53 +10001412 spin_lock(&XFS_I(inode)->i_flags_lock);
Christoph Hellwig2ba66232015-02-02 10:02:09 +11001413 if (offset + size > i_size_read(inode))
1414 i_size_write(inode, offset + size);
Dave Chinner6b698ed2015-06-04 09:18:53 +10001415 spin_unlock(&XFS_I(inode)->i_flags_lock);
Christoph Hellwig2813d682011-12-18 20:00:12 +00001416
1417 /*
Dave Chinner6dfa1b62015-04-16 21:59:34 +10001418 * If we are doing an append IO that needs to update the EOF on disk,
1419 * do the transaction reserve now so we can use common end io
1420 * processing. Stashing the error (if there is one) in the ioend will
1421 * result in the ioend processing passing on the error if it is
1422 * possible as we can't return it from here.
Christoph Hellwigf0973862005-09-05 08:22:52 +10001423 */
Dave Chinnera06c2772015-04-16 22:00:00 +10001424 if (ioend->io_type == XFS_IO_OVERWRITE)
Dave Chinner6dfa1b62015-04-16 21:59:34 +10001425 ioend->io_error = xfs_setfilesize_trans_alloc(ioend);
Christoph Hellwig40e2e972010-07-18 21:17:09 +00001426
Dave Chinner6dfa1b62015-04-16 21:59:34 +10001427out_end_io:
1428 xfs_end_io(&ioend->io_work);
1429 return;
Christoph Hellwigf0973862005-09-05 08:22:52 +10001430}
1431
Dave Chinner6b698ed2015-06-04 09:18:53 +10001432/*
1433 * Complete a direct I/O write request.
1434 *
1435 * The ioend structure is passed from __xfs_get_blocks() to tell us what to do.
1436 * If no ioend exists (i.e. @private == NULL) then the write IO is an overwrite
1437 * wholly within the EOF and so there is nothing for us to do. Note that in this
1438 * case the completion can be called in interrupt context, whereas if we have an
1439 * ioend we will always be called in task context (i.e. from a workqueue).
1440 */
1441STATIC void
1442xfs_end_io_direct_write(
1443 struct kiocb *iocb,
1444 loff_t offset,
1445 ssize_t size,
1446 void *private)
1447{
1448 struct inode *inode = file_inode(iocb->ki_filp);
1449 struct xfs_ioend *ioend = private;
1450
1451 trace_xfs_gbmap_direct_endio(XFS_I(inode), offset, size,
1452 ioend ? ioend->io_type : 0, NULL);
1453
1454 if (!ioend) {
1455 ASSERT(offset + size <= i_size_read(inode));
1456 return;
1457 }
1458
1459 __xfs_end_io_direct_write(inode, ioend, offset, size);
1460}
1461
Dave Chinner6e1ba0b2015-06-04 09:19:15 +10001462static inline ssize_t
1463xfs_vm_do_dio(
1464 struct inode *inode,
1465 struct kiocb *iocb,
1466 struct iov_iter *iter,
1467 loff_t offset,
1468 void (*endio)(struct kiocb *iocb,
1469 loff_t offset,
1470 ssize_t size,
1471 void *private),
1472 int flags)
1473{
1474 struct block_device *bdev;
1475
1476 if (IS_DAX(inode))
1477 return dax_do_io(iocb, inode, iter, offset,
1478 xfs_get_blocks_direct, endio, 0);
1479
1480 bdev = xfs_find_bdev_for_inode(inode);
1481 return __blockdev_direct_IO(iocb, inode, bdev, iter, offset,
1482 xfs_get_blocks_direct, endio, NULL, flags);
1483}
1484
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485STATIC ssize_t
Nathan Scotte4c573b2006-03-14 13:54:26 +11001486xfs_vm_direct_IO(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 struct kiocb *iocb,
Al Virod8d3d942014-03-04 21:27:34 -05001488 struct iov_iter *iter,
1489 loff_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490{
Christoph Hellwig209fb872010-07-18 21:17:11 +00001491 struct inode *inode = iocb->ki_filp->f_mapping->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
Dave Chinner6e1ba0b2015-06-04 09:19:15 +10001493 if (iov_iter_rw(iter) == WRITE)
1494 return xfs_vm_do_dio(inode, iocb, iter, offset,
1495 xfs_end_io_direct_write, DIO_ASYNC_EXTEND);
1496 return xfs_vm_do_dio(inode, iocb, iter, offset, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497}
1498
Dave Chinnerd3bc8152012-04-27 19:45:21 +10001499/*
1500 * Punch out the delalloc blocks we have already allocated.
1501 *
1502 * Don't bother with xfs_setattr given that nothing can have made it to disk yet
1503 * as the page is still locked at this point.
1504 */
Christoph Hellwigfa9b2272010-06-14 05:17:31 -04001505STATIC void
Dave Chinnerd3bc8152012-04-27 19:45:21 +10001506xfs_vm_kill_delalloc_range(
1507 struct inode *inode,
1508 loff_t start,
1509 loff_t end)
Christoph Hellwigfa9b2272010-06-14 05:17:31 -04001510{
Dave Chinnerd3bc8152012-04-27 19:45:21 +10001511 struct xfs_inode *ip = XFS_I(inode);
1512 xfs_fileoff_t start_fsb;
1513 xfs_fileoff_t end_fsb;
1514 int error;
Christoph Hellwigfa9b2272010-06-14 05:17:31 -04001515
Dave Chinnerd3bc8152012-04-27 19:45:21 +10001516 start_fsb = XFS_B_TO_FSB(ip->i_mount, start);
1517 end_fsb = XFS_B_TO_FSB(ip->i_mount, end);
1518 if (end_fsb <= start_fsb)
1519 return;
Dave Chinnerc726de42010-11-30 15:14:39 +11001520
Dave Chinnerd3bc8152012-04-27 19:45:21 +10001521 xfs_ilock(ip, XFS_ILOCK_EXCL);
1522 error = xfs_bmap_punch_delalloc_range(ip, start_fsb,
1523 end_fsb - start_fsb);
1524 if (error) {
1525 /* something screwed, just bail */
1526 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
1527 xfs_alert(ip->i_mount,
1528 "xfs_vm_write_failed: unable to clean up ino %lld",
1529 ip->i_ino);
Dave Chinnerc726de42010-11-30 15:14:39 +11001530 }
Christoph Hellwigfa9b2272010-06-14 05:17:31 -04001531 }
Dave Chinnerd3bc8152012-04-27 19:45:21 +10001532 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Christoph Hellwigfa9b2272010-06-14 05:17:31 -04001533}
1534
Dave Chinnerd3bc8152012-04-27 19:45:21 +10001535STATIC void
1536xfs_vm_write_failed(
1537 struct inode *inode,
1538 struct page *page,
1539 loff_t pos,
1540 unsigned len)
1541{
Jie Liu58e59852013-07-16 13:11:16 +08001542 loff_t block_offset;
Dave Chinnerd3bc8152012-04-27 19:45:21 +10001543 loff_t block_start;
1544 loff_t block_end;
1545 loff_t from = pos & (PAGE_CACHE_SIZE - 1);
1546 loff_t to = from + len;
1547 struct buffer_head *bh, *head;
1548
Jie Liu58e59852013-07-16 13:11:16 +08001549 /*
1550 * The request pos offset might be 32 or 64 bit, this is all fine
1551 * on 64-bit platform. However, for 64-bit pos request on 32-bit
1552 * platform, the high 32-bit will be masked off if we evaluate the
1553 * block_offset via (pos & PAGE_MASK) because the PAGE_MASK is
1554 * 0xfffff000 as an unsigned long, hence the result is incorrect
1555 * which could cause the following ASSERT failed in most cases.
1556 * In order to avoid this, we can evaluate the block_offset of the
1557 * start of the page by using shifts rather than masks the mismatch
1558 * problem.
1559 */
1560 block_offset = (pos >> PAGE_CACHE_SHIFT) << PAGE_CACHE_SHIFT;
1561
Dave Chinnerd3bc8152012-04-27 19:45:21 +10001562 ASSERT(block_offset + from == pos);
1563
1564 head = page_buffers(page);
1565 block_start = 0;
1566 for (bh = head; bh != head || !block_start;
1567 bh = bh->b_this_page, block_start = block_end,
1568 block_offset += bh->b_size) {
1569 block_end = block_start + bh->b_size;
1570
1571 /* skip buffers before the write */
1572 if (block_end <= from)
1573 continue;
1574
1575 /* if the buffer is after the write, we're done */
1576 if (block_start >= to)
1577 break;
1578
1579 if (!buffer_delay(bh))
1580 continue;
1581
1582 if (!buffer_new(bh) && block_offset < i_size_read(inode))
1583 continue;
1584
1585 xfs_vm_kill_delalloc_range(inode, block_offset,
1586 block_offset + bh->b_size);
Dave Chinner4ab9ed52014-04-14 18:11:58 +10001587
1588 /*
1589 * This buffer does not contain data anymore. make sure anyone
1590 * who finds it knows that for certain.
1591 */
1592 clear_buffer_delay(bh);
1593 clear_buffer_uptodate(bh);
1594 clear_buffer_mapped(bh);
1595 clear_buffer_new(bh);
1596 clear_buffer_dirty(bh);
Dave Chinnerd3bc8152012-04-27 19:45:21 +10001597 }
1598
1599}
1600
1601/*
1602 * This used to call block_write_begin(), but it unlocks and releases the page
1603 * on error, and we need that page to be able to punch stale delalloc blocks out
1604 * on failure. hence we copy-n-waste it here and call xfs_vm_write_failed() at
1605 * the appropriate point.
1606 */
Nathan Scottf51623b2006-03-14 13:26:27 +11001607STATIC int
Nick Piggind79689c2007-10-16 01:25:06 -07001608xfs_vm_write_begin(
Nathan Scottf51623b2006-03-14 13:26:27 +11001609 struct file *file,
Nick Piggind79689c2007-10-16 01:25:06 -07001610 struct address_space *mapping,
1611 loff_t pos,
1612 unsigned len,
1613 unsigned flags,
1614 struct page **pagep,
1615 void **fsdata)
Nathan Scottf51623b2006-03-14 13:26:27 +11001616{
Dave Chinnerd3bc8152012-04-27 19:45:21 +10001617 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
1618 struct page *page;
1619 int status;
Christoph Hellwig155130a2010-06-04 11:29:58 +02001620
Dave Chinnerd3bc8152012-04-27 19:45:21 +10001621 ASSERT(len <= PAGE_CACHE_SIZE);
1622
Dave Chinnerad22c7a2013-10-29 22:11:57 +11001623 page = grab_cache_page_write_begin(mapping, index, flags);
Dave Chinnerd3bc8152012-04-27 19:45:21 +10001624 if (!page)
1625 return -ENOMEM;
1626
1627 status = __block_write_begin(page, pos, len, xfs_get_blocks);
1628 if (unlikely(status)) {
1629 struct inode *inode = mapping->host;
Dave Chinner72ab70a2014-04-14 18:13:29 +10001630 size_t isize = i_size_read(inode);
Dave Chinnerd3bc8152012-04-27 19:45:21 +10001631
1632 xfs_vm_write_failed(inode, page, pos, len);
1633 unlock_page(page);
1634
Dave Chinner72ab70a2014-04-14 18:13:29 +10001635 /*
1636 * If the write is beyond EOF, we only want to kill blocks
1637 * allocated in this write, not blocks that were previously
1638 * written successfully.
1639 */
1640 if (pos + len > isize) {
1641 ssize_t start = max_t(ssize_t, pos, isize);
1642
1643 truncate_pagecache_range(inode, start, pos + len);
1644 }
Dave Chinnerd3bc8152012-04-27 19:45:21 +10001645
1646 page_cache_release(page);
1647 page = NULL;
1648 }
1649
1650 *pagep = page;
1651 return status;
Christoph Hellwigfa9b2272010-06-14 05:17:31 -04001652}
Christoph Hellwig155130a2010-06-04 11:29:58 +02001653
Dave Chinnerd3bc8152012-04-27 19:45:21 +10001654/*
Dave Chinneraad3f372014-04-14 18:14:11 +10001655 * On failure, we only need to kill delalloc blocks beyond EOF in the range of
1656 * this specific write because they will never be written. Previous writes
1657 * beyond EOF where block allocation succeeded do not need to be trashed, so
1658 * only new blocks from this write should be trashed. For blocks within
1659 * EOF, generic_write_end() zeros them so they are safe to leave alone and be
1660 * written with all the other valid data.
Dave Chinnerd3bc8152012-04-27 19:45:21 +10001661 */
Christoph Hellwigfa9b2272010-06-14 05:17:31 -04001662STATIC int
1663xfs_vm_write_end(
1664 struct file *file,
1665 struct address_space *mapping,
1666 loff_t pos,
1667 unsigned len,
1668 unsigned copied,
1669 struct page *page,
1670 void *fsdata)
1671{
1672 int ret;
1673
Dave Chinnerd3bc8152012-04-27 19:45:21 +10001674 ASSERT(len <= PAGE_CACHE_SIZE);
1675
Christoph Hellwigfa9b2272010-06-14 05:17:31 -04001676 ret = generic_write_end(file, mapping, pos, len, copied, page, fsdata);
Dave Chinnerd3bc8152012-04-27 19:45:21 +10001677 if (unlikely(ret < len)) {
1678 struct inode *inode = mapping->host;
1679 size_t isize = i_size_read(inode);
1680 loff_t to = pos + len;
1681
1682 if (to > isize) {
Dave Chinneraad3f372014-04-14 18:14:11 +10001683 /* only kill blocks in this write beyond EOF */
1684 if (pos > isize)
1685 isize = pos;
Dave Chinnerd3bc8152012-04-27 19:45:21 +10001686 xfs_vm_kill_delalloc_range(inode, isize, to);
Dave Chinneraad3f372014-04-14 18:14:11 +10001687 truncate_pagecache_range(inode, isize, to);
Dave Chinnerd3bc8152012-04-27 19:45:21 +10001688 }
1689 }
Christoph Hellwig155130a2010-06-04 11:29:58 +02001690 return ret;
Nathan Scottf51623b2006-03-14 13:26:27 +11001691}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
1693STATIC sector_t
Nathan Scotte4c573b2006-03-14 13:54:26 +11001694xfs_vm_bmap(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 struct address_space *mapping,
1696 sector_t block)
1697{
1698 struct inode *inode = (struct inode *)mapping->host;
Christoph Hellwig739bfb22007-08-29 10:58:01 +10001699 struct xfs_inode *ip = XFS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
Christoph Hellwigcca28fb2010-06-24 11:57:09 +10001701 trace_xfs_vm_bmap(XFS_I(inode));
Christoph Hellwig126468b2008-03-06 13:44:57 +11001702 xfs_ilock(ip, XFS_IOLOCK_SHARED);
Dave Chinner4bc1ea62012-11-12 22:53:56 +11001703 filemap_write_and_wait(mapping);
Christoph Hellwig126468b2008-03-06 13:44:57 +11001704 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
Nathan Scottc2536662006-03-29 10:44:40 +10001705 return generic_block_bmap(mapping, block, xfs_get_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706}
1707
1708STATIC int
Nathan Scotte4c573b2006-03-14 13:54:26 +11001709xfs_vm_readpage(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 struct file *unused,
1711 struct page *page)
1712{
Dave Chinner121e2132016-01-08 11:28:35 +11001713 trace_xfs_vm_readpage(page->mapping->host, 1);
Nathan Scottc2536662006-03-29 10:44:40 +10001714 return mpage_readpage(page, xfs_get_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715}
1716
1717STATIC int
Nathan Scotte4c573b2006-03-14 13:54:26 +11001718xfs_vm_readpages(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 struct file *unused,
1720 struct address_space *mapping,
1721 struct list_head *pages,
1722 unsigned nr_pages)
1723{
Dave Chinner121e2132016-01-08 11:28:35 +11001724 trace_xfs_vm_readpages(mapping->host, nr_pages);
Nathan Scottc2536662006-03-29 10:44:40 +10001725 return mpage_readpages(mapping, pages, nr_pages, xfs_get_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726}
1727
Dave Chinner22e757a2014-09-02 12:12:51 +10001728/*
1729 * This is basically a copy of __set_page_dirty_buffers() with one
1730 * small tweak: buffers beyond EOF do not get marked dirty. If we mark them
1731 * dirty, we'll never be able to clean them because we don't write buffers
1732 * beyond EOF, and that means we can't invalidate pages that span EOF
1733 * that have been marked dirty. Further, the dirty state can leak into
1734 * the file interior if the file is extended, resulting in all sorts of
1735 * bad things happening as the state does not match the underlying data.
1736 *
1737 * XXX: this really indicates that bufferheads in XFS need to die. Warts like
1738 * this only exist because of bufferheads and how the generic code manages them.
1739 */
1740STATIC int
1741xfs_vm_set_page_dirty(
1742 struct page *page)
1743{
1744 struct address_space *mapping = page->mapping;
1745 struct inode *inode = mapping->host;
1746 loff_t end_offset;
1747 loff_t offset;
1748 int newly_dirty;
Greg Thelenc4843a72015-05-22 17:13:16 -04001749 struct mem_cgroup *memcg;
Dave Chinner22e757a2014-09-02 12:12:51 +10001750
1751 if (unlikely(!mapping))
1752 return !TestSetPageDirty(page);
1753
1754 end_offset = i_size_read(inode);
1755 offset = page_offset(page);
1756
1757 spin_lock(&mapping->private_lock);
1758 if (page_has_buffers(page)) {
1759 struct buffer_head *head = page_buffers(page);
1760 struct buffer_head *bh = head;
1761
1762 do {
1763 if (offset < end_offset)
1764 set_buffer_dirty(bh);
1765 bh = bh->b_this_page;
1766 offset += 1 << inode->i_blkbits;
1767 } while (bh != head);
1768 }
Greg Thelenc4843a72015-05-22 17:13:16 -04001769 /*
1770 * Use mem_group_begin_page_stat() to keep PageDirty synchronized with
1771 * per-memcg dirty page counters.
1772 */
1773 memcg = mem_cgroup_begin_page_stat(page);
Dave Chinner22e757a2014-09-02 12:12:51 +10001774 newly_dirty = !TestSetPageDirty(page);
1775 spin_unlock(&mapping->private_lock);
1776
1777 if (newly_dirty) {
1778 /* sigh - __set_page_dirty() is static, so copy it here, too */
1779 unsigned long flags;
1780
1781 spin_lock_irqsave(&mapping->tree_lock, flags);
1782 if (page->mapping) { /* Race with truncate? */
1783 WARN_ON_ONCE(!PageUptodate(page));
Greg Thelenc4843a72015-05-22 17:13:16 -04001784 account_page_dirtied(page, mapping, memcg);
Dave Chinner22e757a2014-09-02 12:12:51 +10001785 radix_tree_tag_set(&mapping->page_tree,
1786 page_index(page), PAGECACHE_TAG_DIRTY);
1787 }
1788 spin_unlock_irqrestore(&mapping->tree_lock, flags);
Dave Chinner22e757a2014-09-02 12:12:51 +10001789 }
Greg Thelenc4843a72015-05-22 17:13:16 -04001790 mem_cgroup_end_page_stat(memcg);
1791 if (newly_dirty)
1792 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
Dave Chinner22e757a2014-09-02 12:12:51 +10001793 return newly_dirty;
1794}
1795
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07001796const struct address_space_operations xfs_address_space_operations = {
Nathan Scotte4c573b2006-03-14 13:54:26 +11001797 .readpage = xfs_vm_readpage,
1798 .readpages = xfs_vm_readpages,
1799 .writepage = xfs_vm_writepage,
Nathan Scott7d4fb402006-06-09 15:27:16 +10001800 .writepages = xfs_vm_writepages,
Dave Chinner22e757a2014-09-02 12:12:51 +10001801 .set_page_dirty = xfs_vm_set_page_dirty,
Nathan Scott238f4c52006-03-17 17:26:25 +11001802 .releasepage = xfs_vm_releasepage,
1803 .invalidatepage = xfs_vm_invalidatepage,
Nick Piggind79689c2007-10-16 01:25:06 -07001804 .write_begin = xfs_vm_write_begin,
Christoph Hellwigfa9b2272010-06-14 05:17:31 -04001805 .write_end = xfs_vm_write_end,
Nathan Scotte4c573b2006-03-14 13:54:26 +11001806 .bmap = xfs_vm_bmap,
1807 .direct_IO = xfs_vm_direct_IO,
Christoph Lametere965f962006-02-01 03:05:41 -08001808 .migratepage = buffer_migrate_page,
Hisashi Hifumibddaafa2009-03-29 09:53:38 +02001809 .is_partially_uptodate = block_is_partially_uptodate,
Andi Kleenaa261f52009-09-16 11:50:16 +02001810 .error_remove_page = generic_error_remove_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811};