blob: 7d26933a542f5dd0a3993b42c978ee1c2442ddbf [file] [log] [blame]
Dave Chinner0b61f8a2018-06-05 19:42:14 -07001// SPDX-License-Identifier: GPL-2.0
Dave Chinner68988112013-08-12 20:49:42 +10002/*
3 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
Dave Chinnerc24b5df2013-08-12 20:49:45 +10004 * Copyright (c) 2012 Red Hat, Inc.
Dave Chinner68988112013-08-12 20:49:42 +10005 * All Rights Reserved.
Dave Chinner68988112013-08-12 20:49:42 +10006 */
7#include "xfs.h"
8#include "xfs_fs.h"
Dave Chinner70a98832013-10-23 10:36:05 +11009#include "xfs_shared.h"
Dave Chinner239880e2013-10-23 10:50:10 +110010#include "xfs_format.h"
11#include "xfs_log_format.h"
12#include "xfs_trans_resv.h"
Dave Chinner68988112013-08-12 20:49:42 +100013#include "xfs_bit.h"
Dave Chinner68988112013-08-12 20:49:42 +100014#include "xfs_mount.h"
Dave Chinner57062782013-10-15 09:17:51 +110015#include "xfs_da_format.h"
Darrick J. Wong3ab78df2016-08-03 11:15:38 +100016#include "xfs_defer.h"
Dave Chinner68988112013-08-12 20:49:42 +100017#include "xfs_inode.h"
18#include "xfs_btree.h"
Dave Chinner239880e2013-10-23 10:50:10 +110019#include "xfs_trans.h"
Dave Chinner68988112013-08-12 20:49:42 +100020#include "xfs_extfree_item.h"
21#include "xfs_alloc.h"
22#include "xfs_bmap.h"
23#include "xfs_bmap_util.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110024#include "xfs_bmap_btree.h"
Dave Chinner68988112013-08-12 20:49:42 +100025#include "xfs_rtalloc.h"
26#include "xfs_error.h"
27#include "xfs_quota.h"
28#include "xfs_trans_space.h"
29#include "xfs_trace.h"
Dave Chinnerc24b5df2013-08-12 20:49:45 +100030#include "xfs_icache.h"
Dave Chinner239880e2013-10-23 10:50:10 +110031#include "xfs_log.h"
Darrick J. Wong9c194642016-08-03 12:16:05 +100032#include "xfs_rmap_btree.h"
Darrick J. Wongf86f4032016-10-03 09:11:41 -070033#include "xfs_iomap.h"
34#include "xfs_reflink.h"
35#include "xfs_refcount.h"
Dave Chinner68988112013-08-12 20:49:42 +100036
37/* Kernel only BMAP related definitions and functions */
38
39/*
40 * Convert the given file system block to a disk block. We have to treat it
41 * differently based on whether the file is a real time file or not, because the
42 * bmap code does.
43 */
44xfs_daddr_t
45xfs_fsb_to_db(struct xfs_inode *ip, xfs_fsblock_t fsb)
46{
47 return (XFS_IS_REALTIME_INODE(ip) ? \
48 (xfs_daddr_t)XFS_FSB_TO_BB((ip)->i_mount, (fsb)) : \
49 XFS_FSB_TO_DADDR((ip)->i_mount, (fsb)));
50}
51
52/*
Dave Chinner3fbbbea2015-11-03 12:27:22 +110053 * Routine to zero an extent on disk allocated to the specific inode.
54 *
55 * The VFS functions take a linearised filesystem block offset, so we have to
56 * convert the sparse xfs fsb to the right format first.
57 * VFS types are real funky, too.
58 */
59int
60xfs_zero_extent(
61 struct xfs_inode *ip,
62 xfs_fsblock_t start_fsb,
63 xfs_off_t count_fsb)
64{
65 struct xfs_mount *mp = ip->i_mount;
66 xfs_daddr_t sector = xfs_fsb_to_db(ip, start_fsb);
67 sector_t block = XFS_BB_TO_FSBT(mp, sector);
Dave Chinner3fbbbea2015-11-03 12:27:22 +110068
Matthew Wilcox3dc29162016-03-15 11:20:41 -060069 return blkdev_issue_zeroout(xfs_find_bdev_for_inode(VFS_I(ip)),
70 block << (mp->m_super->s_blocksize_bits - 9),
71 count_fsb << (mp->m_super->s_blocksize_bits - 9),
Christoph Hellwigee472d82017-04-05 19:21:08 +020072 GFP_NOFS, 0);
Dave Chinner3fbbbea2015-11-03 12:27:22 +110073}
74
Dave Chinnerbb9c2e52017-10-09 11:37:22 -070075#ifdef CONFIG_XFS_RT
Dave Chinner68988112013-08-12 20:49:42 +100076int
77xfs_bmap_rtalloc(
78 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
79{
Dave Chinner68988112013-08-12 20:49:42 +100080 int error; /* error return value */
81 xfs_mount_t *mp; /* mount point structure */
82 xfs_extlen_t prod = 0; /* product factor for allocators */
83 xfs_extlen_t ralen = 0; /* realtime allocation length */
84 xfs_extlen_t align; /* minimum allocation alignment */
85 xfs_rtblock_t rtb;
86
87 mp = ap->ip->i_mount;
88 align = xfs_get_extsz_hint(ap->ip);
89 prod = align / mp->m_sb.sb_rextsize;
90 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
91 align, 1, ap->eof, 0,
92 ap->conv, &ap->offset, &ap->length);
93 if (error)
94 return error;
95 ASSERT(ap->length);
96 ASSERT(ap->length % mp->m_sb.sb_rextsize == 0);
97
98 /*
99 * If the offset & length are not perfectly aligned
100 * then kill prod, it will just get us in trouble.
101 */
102 if (do_mod(ap->offset, align) || ap->length % align)
103 prod = 1;
104 /*
105 * Set ralen to be the actual requested length in rtextents.
106 */
107 ralen = ap->length / mp->m_sb.sb_rextsize;
108 /*
109 * If the old value was close enough to MAXEXTLEN that
110 * we rounded up to it, cut it back so it's valid again.
111 * Note that if it's a really large request (bigger than
112 * MAXEXTLEN), we don't hear about that number, and can't
113 * adjust the starting point to match it.
114 */
115 if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN)
116 ralen = MAXEXTLEN / mp->m_sb.sb_rextsize;
117
118 /*
Dave Chinner4b680af2016-02-08 10:46:51 +1100119 * Lock out modifications to both the RT bitmap and summary inodes
Dave Chinner68988112013-08-12 20:49:42 +1000120 */
Darrick J. Wongf4a06602016-08-03 11:00:42 +1000121 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
Dave Chinner68988112013-08-12 20:49:42 +1000122 xfs_trans_ijoin(ap->tp, mp->m_rbmip, XFS_ILOCK_EXCL);
Darrick J. Wongf4a06602016-08-03 11:00:42 +1000123 xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
Dave Chinner4b680af2016-02-08 10:46:51 +1100124 xfs_trans_ijoin(ap->tp, mp->m_rsumip, XFS_ILOCK_EXCL);
Dave Chinner68988112013-08-12 20:49:42 +1000125
126 /*
127 * If it's an allocation to an empty file at offset 0,
128 * pick an extent that will space things out in the rt area.
129 */
130 if (ap->eof && ap->offset == 0) {
131 xfs_rtblock_t uninitialized_var(rtx); /* realtime extent no */
132
133 error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
134 if (error)
135 return error;
136 ap->blkno = rtx * mp->m_sb.sb_rextsize;
137 } else {
138 ap->blkno = 0;
139 }
140
141 xfs_bmap_adjacent(ap);
142
143 /*
144 * Realtime allocation, done through xfs_rtallocate_extent.
145 */
Dave Chinner68988112013-08-12 20:49:42 +1000146 do_div(ap->blkno, mp->m_sb.sb_rextsize);
147 rtb = ap->blkno;
148 ap->length = ralen;
Christoph Hellwig089ec2f2017-02-17 08:21:06 -0800149 error = xfs_rtallocate_extent(ap->tp, ap->blkno, 1, ap->length,
150 &ralen, ap->wasdel, prod, &rtb);
151 if (error)
Dave Chinner68988112013-08-12 20:49:42 +1000152 return error;
Christoph Hellwig089ec2f2017-02-17 08:21:06 -0800153
Dave Chinner68988112013-08-12 20:49:42 +1000154 ap->blkno = rtb;
155 if (ap->blkno != NULLFSBLOCK) {
156 ap->blkno *= mp->m_sb.sb_rextsize;
157 ralen *= mp->m_sb.sb_rextsize;
158 ap->length = ralen;
159 ap->ip->i_d.di_nblocks += ralen;
160 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
161 if (ap->wasdel)
162 ap->ip->i_delayed_blks -= ralen;
163 /*
164 * Adjust the disk quota also. This was reserved
165 * earlier.
166 */
167 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
168 ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
169 XFS_TRANS_DQ_RTBCOUNT, (long) ralen);
Dave Chinner3fbbbea2015-11-03 12:27:22 +1100170
171 /* Zero the extent if we were asked to do so */
Dave Chinner292378e2016-09-26 08:21:28 +1000172 if (ap->datatype & XFS_ALLOC_USERDATA_ZERO) {
Dave Chinner3fbbbea2015-11-03 12:27:22 +1100173 error = xfs_zero_extent(ap->ip, ap->blkno, ap->length);
174 if (error)
175 return error;
176 }
Dave Chinner68988112013-08-12 20:49:42 +1000177 } else {
178 ap->length = 0;
179 }
180 return 0;
181}
Dave Chinnerbb9c2e52017-10-09 11:37:22 -0700182#endif /* CONFIG_XFS_RT */
Dave Chinner68988112013-08-12 20:49:42 +1000183
184/*
Dave Chinner68988112013-08-12 20:49:42 +1000185 * Check if the endoff is outside the last extent. If so the caller will grow
186 * the allocation to a stripe unit boundary. All offsets are considered outside
187 * the end of file for an empty fork, so 1 is returned in *eof in that case.
188 */
189int
190xfs_bmap_eof(
191 struct xfs_inode *ip,
192 xfs_fileoff_t endoff,
193 int whichfork,
194 int *eof)
195{
196 struct xfs_bmbt_irec rec;
197 int error;
198
199 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, eof);
200 if (error || *eof)
201 return error;
202
203 *eof = endoff >= rec.br_startoff + rec.br_blockcount;
204 return 0;
205}
206
207/*
208 * Extent tree block counting routines.
209 */
210
211/*
Darrick J. Wongd29cb3e2017-06-16 11:00:12 -0700212 * Count leaf blocks given a range of extent records. Delayed allocation
213 * extents are not counted towards the totals.
Dave Chinner68988112013-08-12 20:49:42 +1000214 */
Christoph Hellwige17a5c62017-08-29 15:44:14 -0700215xfs_extnum_t
Dave Chinner68988112013-08-12 20:49:42 +1000216xfs_bmap_count_leaves(
Darrick J. Wongd29cb3e2017-06-16 11:00:12 -0700217 struct xfs_ifork *ifp,
Darrick J. Wonge7f5d5c2017-06-16 11:00:12 -0700218 xfs_filblks_t *count)
Dave Chinner68988112013-08-12 20:49:42 +1000219{
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700220 struct xfs_iext_cursor icur;
Christoph Hellwige17a5c62017-08-29 15:44:14 -0700221 struct xfs_bmbt_irec got;
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700222 xfs_extnum_t numrecs = 0;
Dave Chinner68988112013-08-12 20:49:42 +1000223
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700224 for_each_xfs_iext(ifp, &icur, &got) {
Christoph Hellwige17a5c62017-08-29 15:44:14 -0700225 if (!isnullstartblock(got.br_startblock)) {
226 *count += got.br_blockcount;
227 numrecs++;
Darrick J. Wongd29cb3e2017-06-16 11:00:12 -0700228 }
Dave Chinner68988112013-08-12 20:49:42 +1000229 }
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700230
Christoph Hellwige17a5c62017-08-29 15:44:14 -0700231 return numrecs;
Dave Chinner68988112013-08-12 20:49:42 +1000232}
233
234/*
235 * Count leaf blocks given a range of extent records originally
236 * in btree format.
237 */
238STATIC void
239xfs_bmap_disk_count_leaves(
240 struct xfs_mount *mp,
241 struct xfs_btree_block *block,
242 int numrecs,
Darrick J. Wonge7f5d5c2017-06-16 11:00:12 -0700243 xfs_filblks_t *count)
Dave Chinner68988112013-08-12 20:49:42 +1000244{
245 int b;
246 xfs_bmbt_rec_t *frp;
247
248 for (b = 1; b <= numrecs; b++) {
249 frp = XFS_BMBT_REC_ADDR(mp, block, b);
250 *count += xfs_bmbt_disk_get_blockcount(frp);
251 }
252}
253
254/*
255 * Recursively walks each level of a btree
Zhi Yong Wu8be11e92013-08-12 03:14:52 +0000256 * to count total fsblocks in use.
Dave Chinner68988112013-08-12 20:49:42 +1000257 */
Darrick J. Wonge7f5d5c2017-06-16 11:00:12 -0700258STATIC int
Dave Chinner68988112013-08-12 20:49:42 +1000259xfs_bmap_count_tree(
Darrick J. Wonge7f5d5c2017-06-16 11:00:12 -0700260 struct xfs_mount *mp,
261 struct xfs_trans *tp,
262 struct xfs_ifork *ifp,
263 xfs_fsblock_t blockno,
264 int levelin,
265 xfs_extnum_t *nextents,
266 xfs_filblks_t *count)
Dave Chinner68988112013-08-12 20:49:42 +1000267{
268 int error;
Darrick J. Wonge7f5d5c2017-06-16 11:00:12 -0700269 struct xfs_buf *bp, *nbp;
Dave Chinner68988112013-08-12 20:49:42 +1000270 int level = levelin;
271 __be64 *pp;
272 xfs_fsblock_t bno = blockno;
273 xfs_fsblock_t nextbno;
274 struct xfs_btree_block *block, *nextblock;
275 int numrecs;
276
277 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF,
278 &xfs_bmbt_buf_ops);
279 if (error)
280 return error;
281 *count += 1;
282 block = XFS_BUF_TO_BLOCK(bp);
283
284 if (--level) {
285 /* Not at node above leaves, count this level of nodes */
286 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
287 while (nextbno != NULLFSBLOCK) {
288 error = xfs_btree_read_bufl(mp, tp, nextbno, 0, &nbp,
289 XFS_BMAP_BTREE_REF,
290 &xfs_bmbt_buf_ops);
291 if (error)
292 return error;
293 *count += 1;
294 nextblock = XFS_BUF_TO_BLOCK(nbp);
295 nextbno = be64_to_cpu(nextblock->bb_u.l.bb_rightsib);
296 xfs_trans_brelse(tp, nbp);
297 }
298
299 /* Dive to the next level */
300 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
301 bno = be64_to_cpu(*pp);
Darrick J. Wonge7f5d5c2017-06-16 11:00:12 -0700302 error = xfs_bmap_count_tree(mp, tp, ifp, bno, level, nextents,
303 count);
304 if (error) {
Dave Chinner68988112013-08-12 20:49:42 +1000305 xfs_trans_brelse(tp, bp);
306 XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
307 XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +1000308 return -EFSCORRUPTED;
Dave Chinner68988112013-08-12 20:49:42 +1000309 }
310 xfs_trans_brelse(tp, bp);
311 } else {
312 /* count all level 1 nodes and their leaves */
313 for (;;) {
314 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
315 numrecs = be16_to_cpu(block->bb_numrecs);
Darrick J. Wonge7f5d5c2017-06-16 11:00:12 -0700316 (*nextents) += numrecs;
Dave Chinner68988112013-08-12 20:49:42 +1000317 xfs_bmap_disk_count_leaves(mp, block, numrecs, count);
318 xfs_trans_brelse(tp, bp);
319 if (nextbno == NULLFSBLOCK)
320 break;
321 bno = nextbno;
322 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
323 XFS_BMAP_BTREE_REF,
324 &xfs_bmbt_buf_ops);
325 if (error)
326 return error;
327 *count += 1;
328 block = XFS_BUF_TO_BLOCK(bp);
329 }
330 }
331 return 0;
332}
333
334/*
Darrick J. Wongd29cb3e2017-06-16 11:00:12 -0700335 * Count fsblocks of the given fork. Delayed allocation extents are
336 * not counted towards the totals.
Dave Chinner68988112013-08-12 20:49:42 +1000337 */
Darrick J. Wonge7f5d5c2017-06-16 11:00:12 -0700338int
Dave Chinner68988112013-08-12 20:49:42 +1000339xfs_bmap_count_blocks(
Darrick J. Wonge7f5d5c2017-06-16 11:00:12 -0700340 struct xfs_trans *tp,
341 struct xfs_inode *ip,
342 int whichfork,
343 xfs_extnum_t *nextents,
344 xfs_filblks_t *count)
Dave Chinner68988112013-08-12 20:49:42 +1000345{
Darrick J. Wonge7f5d5c2017-06-16 11:00:12 -0700346 struct xfs_mount *mp; /* file system mount structure */
Dave Chinner68988112013-08-12 20:49:42 +1000347 __be64 *pp; /* pointer to block address */
Darrick J. Wonge7f5d5c2017-06-16 11:00:12 -0700348 struct xfs_btree_block *block; /* current btree block */
349 struct xfs_ifork *ifp; /* fork structure */
350 xfs_fsblock_t bno; /* block # of "block" */
351 int level; /* btree level, for checking */
352 int error;
Dave Chinner68988112013-08-12 20:49:42 +1000353
354 bno = NULLFSBLOCK;
355 mp = ip->i_mount;
Darrick J. Wonge7f5d5c2017-06-16 11:00:12 -0700356 *nextents = 0;
357 *count = 0;
Dave Chinner68988112013-08-12 20:49:42 +1000358 ifp = XFS_IFORK_PTR(ip, whichfork);
Darrick J. Wonge7f5d5c2017-06-16 11:00:12 -0700359 if (!ifp)
Dave Chinner68988112013-08-12 20:49:42 +1000360 return 0;
Dave Chinner68988112013-08-12 20:49:42 +1000361
Darrick J. Wonge7f5d5c2017-06-16 11:00:12 -0700362 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
363 case XFS_DINODE_FMT_EXTENTS:
Christoph Hellwige17a5c62017-08-29 15:44:14 -0700364 *nextents = xfs_bmap_count_leaves(ifp, count);
Darrick J. Wonge7f5d5c2017-06-16 11:00:12 -0700365 return 0;
366 case XFS_DINODE_FMT_BTREE:
367 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
368 error = xfs_iread_extents(tp, ip, whichfork);
369 if (error)
370 return error;
371 }
Dave Chinner68988112013-08-12 20:49:42 +1000372
Darrick J. Wonge7f5d5c2017-06-16 11:00:12 -0700373 /*
374 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
375 */
376 block = ifp->if_broot;
377 level = be16_to_cpu(block->bb_level);
378 ASSERT(level > 0);
379 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
380 bno = be64_to_cpu(*pp);
381 ASSERT(bno != NULLFSBLOCK);
382 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
383 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
384
385 error = xfs_bmap_count_tree(mp, tp, ifp, bno, level,
386 nextents, count);
387 if (error) {
388 XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)",
389 XFS_ERRLEVEL_LOW, mp);
390 return -EFSCORRUPTED;
391 }
392 return 0;
Dave Chinner68988112013-08-12 20:49:42 +1000393 }
394
395 return 0;
396}
397
Christoph Hellwigabbf9e82017-10-17 14:16:18 -0700398static int
399xfs_getbmap_report_one(
400 struct xfs_inode *ip,
401 struct getbmapx *bmv,
Christoph Hellwig232b51942017-10-17 14:16:19 -0700402 struct kgetbmap *out,
Christoph Hellwigabbf9e82017-10-17 14:16:18 -0700403 int64_t bmv_end,
404 struct xfs_bmbt_irec *got)
Dave Chinner68988112013-08-12 20:49:42 +1000405{
Christoph Hellwig232b51942017-10-17 14:16:19 -0700406 struct kgetbmap *p = out + bmv->bmv_entries;
Christoph Hellwigabbf9e82017-10-17 14:16:18 -0700407 bool shared = false, trimmed = false;
408 int error;
Dave Chinner68988112013-08-12 20:49:42 +1000409
Christoph Hellwigabbf9e82017-10-17 14:16:18 -0700410 error = xfs_reflink_trim_around_shared(ip, got, &shared, &trimmed);
Darrick J. Wongf86f4032016-10-03 09:11:41 -0700411 if (error)
412 return error;
413
Christoph Hellwigabbf9e82017-10-17 14:16:18 -0700414 if (isnullstartblock(got->br_startblock) ||
415 got->br_startblock == DELAYSTARTBLOCK) {
Darrick J. Wongf86f4032016-10-03 09:11:41 -0700416 /*
Christoph Hellwigabbf9e82017-10-17 14:16:18 -0700417 * Delalloc extents that start beyond EOF can occur due to
418 * speculative EOF allocation when the delalloc extent is larger
419 * than the largest freespace extent at conversion time. These
420 * extents cannot be converted by data writeback, so can exist
421 * here even if we are not supposed to be finding delalloc
422 * extents.
Darrick J. Wongf86f4032016-10-03 09:11:41 -0700423 */
Christoph Hellwigabbf9e82017-10-17 14:16:18 -0700424 if (got->br_startoff < XFS_B_TO_FSB(ip->i_mount, XFS_ISIZE(ip)))
425 ASSERT((bmv->bmv_iflags & BMV_IF_DELALLOC) != 0);
426
427 p->bmv_oflags |= BMV_OF_DELALLOC;
428 p->bmv_block = -2;
Darrick J. Wongf86f4032016-10-03 09:11:41 -0700429 } else {
Christoph Hellwigabbf9e82017-10-17 14:16:18 -0700430 p->bmv_block = xfs_fsb_to_db(ip, got->br_startblock);
Darrick J. Wongf86f4032016-10-03 09:11:41 -0700431 }
432
Christoph Hellwigabbf9e82017-10-17 14:16:18 -0700433 if (got->br_state == XFS_EXT_UNWRITTEN &&
434 (bmv->bmv_iflags & BMV_IF_PREALLOC))
435 p->bmv_oflags |= BMV_OF_PREALLOC;
436
437 if (shared)
438 p->bmv_oflags |= BMV_OF_SHARED;
439
440 p->bmv_offset = XFS_FSB_TO_BB(ip->i_mount, got->br_startoff);
441 p->bmv_length = XFS_FSB_TO_BB(ip->i_mount, got->br_blockcount);
442
443 bmv->bmv_offset = p->bmv_offset + p->bmv_length;
444 bmv->bmv_length = max(0LL, bmv_end - bmv->bmv_offset);
445 bmv->bmv_entries++;
Darrick J. Wongf86f4032016-10-03 09:11:41 -0700446 return 0;
447}
448
Christoph Hellwigabbf9e82017-10-17 14:16:18 -0700449static void
450xfs_getbmap_report_hole(
451 struct xfs_inode *ip,
452 struct getbmapx *bmv,
Christoph Hellwig232b51942017-10-17 14:16:19 -0700453 struct kgetbmap *out,
Christoph Hellwigabbf9e82017-10-17 14:16:18 -0700454 int64_t bmv_end,
455 xfs_fileoff_t bno,
456 xfs_fileoff_t end)
457{
Christoph Hellwig232b51942017-10-17 14:16:19 -0700458 struct kgetbmap *p = out + bmv->bmv_entries;
Christoph Hellwigabbf9e82017-10-17 14:16:18 -0700459
460 if (bmv->bmv_iflags & BMV_IF_NO_HOLES)
461 return;
462
463 p->bmv_block = -1;
464 p->bmv_offset = XFS_FSB_TO_BB(ip->i_mount, bno);
465 p->bmv_length = XFS_FSB_TO_BB(ip->i_mount, end - bno);
466
467 bmv->bmv_offset = p->bmv_offset + p->bmv_length;
468 bmv->bmv_length = max(0LL, bmv_end - bmv->bmv_offset);
469 bmv->bmv_entries++;
470}
471
472static inline bool
473xfs_getbmap_full(
474 struct getbmapx *bmv)
475{
476 return bmv->bmv_length == 0 || bmv->bmv_entries >= bmv->bmv_count - 1;
477}
478
479static bool
480xfs_getbmap_next_rec(
481 struct xfs_bmbt_irec *rec,
482 xfs_fileoff_t total_end)
483{
484 xfs_fileoff_t end = rec->br_startoff + rec->br_blockcount;
485
486 if (end == total_end)
487 return false;
488
489 rec->br_startoff += rec->br_blockcount;
490 if (!isnullstartblock(rec->br_startblock) &&
491 rec->br_startblock != DELAYSTARTBLOCK)
492 rec->br_startblock += rec->br_blockcount;
493 rec->br_blockcount = total_end - end;
494 return true;
495}
496
Dave Chinner68988112013-08-12 20:49:42 +1000497/*
498 * Get inode's extents as described in bmv, and format for output.
499 * Calls formatter to fill the user's buffer until all extents
500 * are mapped, until the passed-in bmv->bmv_count slots have
501 * been filled, or until the formatter short-circuits the loop,
502 * if it is tracking filled-in extents on its own.
503 */
504int /* error code */
505xfs_getbmap(
Christoph Hellwig232b51942017-10-17 14:16:19 -0700506 struct xfs_inode *ip,
Dave Chinner68988112013-08-12 20:49:42 +1000507 struct getbmapx *bmv, /* user bmap structure */
Christoph Hellwig232b51942017-10-17 14:16:19 -0700508 struct kgetbmap *out)
Dave Chinner68988112013-08-12 20:49:42 +1000509{
Christoph Hellwigabbf9e82017-10-17 14:16:18 -0700510 struct xfs_mount *mp = ip->i_mount;
511 int iflags = bmv->bmv_iflags;
Christoph Hellwig232b51942017-10-17 14:16:19 -0700512 int whichfork, lock, error = 0;
Christoph Hellwigabbf9e82017-10-17 14:16:18 -0700513 int64_t bmv_end, max_len;
514 xfs_fileoff_t bno, first_bno;
515 struct xfs_ifork *ifp;
Christoph Hellwigabbf9e82017-10-17 14:16:18 -0700516 struct xfs_bmbt_irec got, rec;
517 xfs_filblks_t len;
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700518 struct xfs_iext_cursor icur;
Dave Chinner68988112013-08-12 20:49:42 +1000519
Christoph Hellwig232b51942017-10-17 14:16:19 -0700520 if (bmv->bmv_iflags & ~BMV_IF_VALID)
521 return -EINVAL;
Darrick J. Wongf86f4032016-10-03 09:11:41 -0700522#ifndef DEBUG
523 /* Only allow CoW fork queries if we're debugging. */
524 if (iflags & BMV_IF_COWFORK)
525 return -EINVAL;
526#endif
527 if ((iflags & BMV_IF_ATTRFORK) && (iflags & BMV_IF_COWFORK))
528 return -EINVAL;
529
Christoph Hellwigabbf9e82017-10-17 14:16:18 -0700530 if (bmv->bmv_length < -1)
531 return -EINVAL;
Christoph Hellwigabbf9e82017-10-17 14:16:18 -0700532 bmv->bmv_entries = 0;
533 if (bmv->bmv_length == 0)
534 return 0;
535
Darrick J. Wongf86f4032016-10-03 09:11:41 -0700536 if (iflags & BMV_IF_ATTRFORK)
537 whichfork = XFS_ATTR_FORK;
538 else if (iflags & BMV_IF_COWFORK)
539 whichfork = XFS_COW_FORK;
540 else
541 whichfork = XFS_DATA_FORK;
Christoph Hellwigabbf9e82017-10-17 14:16:18 -0700542 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner68988112013-08-12 20:49:42 +1000543
544 xfs_ilock(ip, XFS_IOLOCK_SHARED);
Darrick J. Wongf86f4032016-10-03 09:11:41 -0700545 switch (whichfork) {
Christoph Hellwigabbf9e82017-10-17 14:16:18 -0700546 case XFS_ATTR_FORK:
547 if (!XFS_IFORK_Q(ip))
548 goto out_unlock_iolock;
549
550 max_len = 1LL << 32;
551 lock = xfs_ilock_attr_map_shared(ip);
552 break;
553 case XFS_COW_FORK:
554 /* No CoW fork? Just return */
555 if (!ifp)
556 goto out_unlock_iolock;
557
558 if (xfs_get_cowextsz_hint(ip))
559 max_len = mp->m_super->s_maxbytes;
560 else
561 max_len = XFS_ISIZE(ip);
562
563 lock = XFS_ILOCK_SHARED;
564 xfs_ilock(ip, lock);
565 break;
Darrick J. Wongf86f4032016-10-03 09:11:41 -0700566 case XFS_DATA_FORK:
Christoph Hellwigefa70be2013-12-18 02:14:39 -0800567 if (!(iflags & BMV_IF_DELALLOC) &&
568 (ip->i_delayed_blks || XFS_ISIZE(ip) > ip->i_d.di_size)) {
Dave Chinner24513372014-06-25 14:58:08 +1000569 error = filemap_write_and_wait(VFS_I(ip)->i_mapping);
Dave Chinner68988112013-08-12 20:49:42 +1000570 if (error)
571 goto out_unlock_iolock;
Dave Chinner68988112013-08-12 20:49:42 +1000572
Christoph Hellwigefa70be2013-12-18 02:14:39 -0800573 /*
574 * Even after flushing the inode, there can still be
575 * delalloc blocks on the inode beyond EOF due to
576 * speculative preallocation. These are not removed
577 * until the release function is called or the inode
578 * is inactivated. Hence we cannot assert here that
579 * ip->i_delayed_blks == 0.
580 */
581 }
582
Christoph Hellwigabbf9e82017-10-17 14:16:18 -0700583 if (xfs_get_extsz_hint(ip) ||
584 (ip->i_d.di_flags &
585 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))
586 max_len = mp->m_super->s_maxbytes;
587 else
588 max_len = XFS_ISIZE(ip);
589
Christoph Hellwigefa70be2013-12-18 02:14:39 -0800590 lock = xfs_ilock_data_map_shared(ip);
Darrick J. Wongf86f4032016-10-03 09:11:41 -0700591 break;
Christoph Hellwigefa70be2013-12-18 02:14:39 -0800592 }
Dave Chinner68988112013-08-12 20:49:42 +1000593
Christoph Hellwigabbf9e82017-10-17 14:16:18 -0700594 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
595 case XFS_DINODE_FMT_EXTENTS:
596 case XFS_DINODE_FMT_BTREE:
597 break;
598 case XFS_DINODE_FMT_LOCAL:
599 /* Local format inode forks report no extents. */
Dave Chinner68988112013-08-12 20:49:42 +1000600 goto out_unlock_ilock;
Christoph Hellwigabbf9e82017-10-17 14:16:18 -0700601 default:
602 error = -EINVAL;
603 goto out_unlock_ilock;
Dave Chinner68988112013-08-12 20:49:42 +1000604 }
605
Christoph Hellwigabbf9e82017-10-17 14:16:18 -0700606 if (bmv->bmv_length == -1) {
607 max_len = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, max_len));
608 bmv->bmv_length = max(0LL, max_len - bmv->bmv_offset);
609 }
610
611 bmv_end = bmv->bmv_offset + bmv->bmv_length;
612
613 first_bno = bno = XFS_BB_TO_FSBT(mp, bmv->bmv_offset);
614 len = XFS_BB_TO_FSB(mp, bmv->bmv_length);
615
616 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
617 error = xfs_iread_extents(NULL, ip, whichfork);
Dave Chinner68988112013-08-12 20:49:42 +1000618 if (error)
Christoph Hellwigabbf9e82017-10-17 14:16:18 -0700619 goto out_unlock_ilock;
620 }
Dave Chinner68988112013-08-12 20:49:42 +1000621
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700622 if (!xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) {
Christoph Hellwigabbf9e82017-10-17 14:16:18 -0700623 /*
624 * Report a whole-file hole if the delalloc flag is set to
625 * stay compatible with the old implementation.
626 */
627 if (iflags & BMV_IF_DELALLOC)
628 xfs_getbmap_report_hole(ip, bmv, out, bmv_end, bno,
629 XFS_B_TO_FSB(mp, XFS_ISIZE(ip)));
630 goto out_unlock_ilock;
631 }
Dave Chinner68988112013-08-12 20:49:42 +1000632
Christoph Hellwigabbf9e82017-10-17 14:16:18 -0700633 while (!xfs_getbmap_full(bmv)) {
634 xfs_trim_extent(&got, first_bno, len);
Dave Chinner68988112013-08-12 20:49:42 +1000635
Christoph Hellwigabbf9e82017-10-17 14:16:18 -0700636 /*
637 * Report an entry for a hole if this extent doesn't directly
638 * follow the previous one.
639 */
640 if (got.br_startoff > bno) {
641 xfs_getbmap_report_hole(ip, bmv, out, bmv_end, bno,
642 got.br_startoff);
643 if (xfs_getbmap_full(bmv))
644 break;
Dave Chinner68988112013-08-12 20:49:42 +1000645 }
Dave Chinner68988112013-08-12 20:49:42 +1000646
Christoph Hellwigabbf9e82017-10-17 14:16:18 -0700647 /*
648 * In order to report shared extents accurately, we report each
649 * distinct shared / unshared part of a single bmbt record with
650 * an individual getbmapx record.
651 */
652 bno = got.br_startoff + got.br_blockcount;
653 rec = got;
654 do {
655 error = xfs_getbmap_report_one(ip, bmv, out, bmv_end,
656 &rec);
657 if (error || xfs_getbmap_full(bmv))
658 goto out_unlock_ilock;
659 } while (xfs_getbmap_next_rec(&rec, bno));
660
Christoph Hellwigb2b17122017-11-03 10:34:43 -0700661 if (!xfs_iext_next_extent(ifp, &icur, &got)) {
Christoph Hellwigabbf9e82017-10-17 14:16:18 -0700662 xfs_fileoff_t end = XFS_B_TO_FSB(mp, XFS_ISIZE(ip));
663
664 out[bmv->bmv_entries - 1].bmv_oflags |= BMV_OF_LAST;
665
666 if (whichfork != XFS_ATTR_FORK && bno < end &&
667 !xfs_getbmap_full(bmv)) {
668 xfs_getbmap_report_hole(ip, bmv, out, bmv_end,
669 bno, end);
670 }
671 break;
672 }
673
674 if (bno >= first_bno + len)
675 break;
676 }
677
678out_unlock_ilock:
Christoph Hellwig01f4f322013-12-06 12:30:08 -0800679 xfs_iunlock(ip, lock);
Christoph Hellwigabbf9e82017-10-17 14:16:18 -0700680out_unlock_iolock:
Dave Chinner68988112013-08-12 20:49:42 +1000681 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
Dave Chinner68988112013-08-12 20:49:42 +1000682 return error;
683}
684
685/*
686 * dead simple method of punching delalyed allocation blocks from a range in
687 * the inode. Walks a block at a time so will be slow, but is only executed in
Zhi Yong Wuad4809b2013-08-12 03:14:55 +0000688 * rare error cases so the overhead is not critical. This will always punch out
Dave Chinner68988112013-08-12 20:49:42 +1000689 * both the start and end blocks, even if the ranges only partially overlap
690 * them, so it is up to the caller to ensure that partial blocks are not
691 * passed in.
692 */
693int
694xfs_bmap_punch_delalloc_range(
695 struct xfs_inode *ip,
696 xfs_fileoff_t start_fsb,
697 xfs_fileoff_t length)
698{
699 xfs_fileoff_t remaining = length;
700 int error = 0;
701
702 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
703
704 do {
705 int done;
706 xfs_bmbt_irec_t imap;
707 int nimaps = 1;
708 xfs_fsblock_t firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000709 struct xfs_defer_ops dfops;
Dave Chinner68988112013-08-12 20:49:42 +1000710
711 /*
712 * Map the range first and check that it is a delalloc extent
713 * before trying to unmap the range. Otherwise we will be
714 * trying to remove a real extent (which requires a
715 * transaction) or a hole, which is probably a bad idea...
716 */
717 error = xfs_bmapi_read(ip, start_fsb, 1, &imap, &nimaps,
718 XFS_BMAPI_ENTIRE);
719
720 if (error) {
721 /* something screwed, just bail */
722 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
723 xfs_alert(ip->i_mount,
724 "Failed delalloc mapping lookup ino %lld fsb %lld.",
725 ip->i_ino, start_fsb);
726 }
727 break;
728 }
729 if (!nimaps) {
730 /* nothing there */
731 goto next_block;
732 }
733 if (imap.br_startblock != DELAYSTARTBLOCK) {
734 /* been converted, ignore */
735 goto next_block;
736 }
737 WARN_ON(imap.br_blockcount == 0);
738
739 /*
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000740 * Note: while we initialise the firstblock/dfops pair, they
Dave Chinner68988112013-08-12 20:49:42 +1000741 * should never be used because blocks should never be
742 * allocated or freed for a delalloc extent and hence we need
743 * don't cancel or finish them after the xfs_bunmapi() call.
744 */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000745 xfs_defer_init(&dfops, &firstblock);
Dave Chinner68988112013-08-12 20:49:42 +1000746 error = xfs_bunmapi(NULL, ip, start_fsb, 1, 0, 1, &firstblock,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000747 &dfops, &done);
Dave Chinner68988112013-08-12 20:49:42 +1000748 if (error)
749 break;
750
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000751 ASSERT(!xfs_defer_has_unfinished_work(&dfops));
Dave Chinner68988112013-08-12 20:49:42 +1000752next_block:
753 start_fsb++;
754 remaining--;
755 } while(remaining > 0);
756
757 return error;
758}
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000759
760/*
761 * Test whether it is appropriate to check an inode for and free post EOF
762 * blocks. The 'force' parameter determines whether we should also consider
763 * regular files that are marked preallocated or append-only.
764 */
765bool
766xfs_can_free_eofblocks(struct xfs_inode *ip, bool force)
767{
768 /* prealloc/delalloc exists only on regular files */
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100769 if (!S_ISREG(VFS_I(ip)->i_mode))
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000770 return false;
771
772 /*
773 * Zero sized files with no cached pages and delalloc blocks will not
774 * have speculative prealloc/delalloc blocks to remove.
775 */
776 if (VFS_I(ip)->i_size == 0 &&
Dave Chinner2667c6f2014-08-04 13:23:15 +1000777 VFS_I(ip)->i_mapping->nrpages == 0 &&
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000778 ip->i_delayed_blks == 0)
779 return false;
780
781 /* If we haven't read in the extent list, then don't do it now. */
782 if (!(ip->i_df.if_flags & XFS_IFEXTENTS))
783 return false;
784
785 /*
786 * Do not free real preallocated or append-only files unless the file
787 * has delalloc blocks and we are forced to remove them.
788 */
789 if (ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND))
790 if (!force || ip->i_delayed_blks == 0)
791 return false;
792
793 return true;
794}
795
796/*
Brian Foster3b4683c2017-04-11 10:50:05 -0700797 * This is called to free any blocks beyond eof. The caller must hold
798 * IOLOCK_EXCL unless we are in the inode reclaim path and have the only
799 * reference to the inode.
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000800 */
801int
802xfs_free_eofblocks(
Brian Fostera36b9262017-01-27 23:22:55 -0800803 struct xfs_inode *ip)
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000804{
Brian Fostera36b9262017-01-27 23:22:55 -0800805 struct xfs_trans *tp;
806 int error;
807 xfs_fileoff_t end_fsb;
808 xfs_fileoff_t last_fsb;
809 xfs_filblks_t map_len;
810 int nimaps;
811 struct xfs_bmbt_irec imap;
812 struct xfs_mount *mp = ip->i_mount;
813
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000814 /*
815 * Figure out if there are any blocks beyond the end
816 * of the file. If not, then there is nothing to do.
817 */
818 end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_ISIZE(ip));
819 last_fsb = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
820 if (last_fsb <= end_fsb)
821 return 0;
822 map_len = last_fsb - end_fsb;
823
824 nimaps = 1;
825 xfs_ilock(ip, XFS_ILOCK_SHARED);
826 error = xfs_bmapi_read(ip, end_fsb, map_len, &imap, &nimaps, 0);
827 xfs_iunlock(ip, XFS_ILOCK_SHARED);
828
Brian Fostera36b9262017-01-27 23:22:55 -0800829 /*
830 * If there are blocks after the end of file, truncate the file to its
831 * current size to free them up.
832 */
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000833 if (!error && (nimaps != 0) &&
834 (imap.br_startblock != HOLESTARTBLOCK ||
835 ip->i_delayed_blks)) {
836 /*
837 * Attach the dquots to the inode up front.
838 */
Darrick J. Wongc14cfcc2018-05-04 15:30:21 -0700839 error = xfs_qm_dqattach(ip);
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000840 if (error)
841 return error;
842
Brian Fostere4229d6b2017-01-27 23:22:57 -0800843 /* wait on dio to ensure i_size has settled */
844 inode_dio_wait(VFS_I(ip));
845
Christoph Hellwig253f4912016-04-06 09:19:55 +1000846 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0,
847 &tp);
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000848 if (error) {
849 ASSERT(XFS_FORCED_SHUTDOWN(mp));
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000850 return error;
851 }
852
853 xfs_ilock(ip, XFS_ILOCK_EXCL);
854 xfs_trans_ijoin(tp, ip, 0);
855
856 /*
857 * Do not update the on-disk file size. If we update the
858 * on-disk file size and then the system crashes before the
859 * contents of the file are flushed to disk then the files
860 * may be full of holes (ie NULL files bug).
861 */
Brian Foster4e529332018-05-10 09:35:42 -0700862 error = xfs_itruncate_extents_flags(&tp, ip, XFS_DATA_FORK,
863 XFS_ISIZE(ip), XFS_BMAPI_NODISCARD);
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000864 if (error) {
865 /*
866 * If we get an error at this point we simply don't
867 * bother truncating the file.
868 */
Christoph Hellwig4906e212015-06-04 13:47:56 +1000869 xfs_trans_cancel(tp);
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000870 } else {
Christoph Hellwig70393312015-06-04 13:48:08 +1000871 error = xfs_trans_commit(tp);
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000872 if (!error)
873 xfs_inode_clear_eofblocks_tag(ip);
874 }
875
876 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000877 }
878 return error;
879}
880
Christoph Hellwig83aee9e2013-10-12 00:55:07 -0700881int
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000882xfs_alloc_file_space(
Christoph Hellwig83aee9e2013-10-12 00:55:07 -0700883 struct xfs_inode *ip,
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000884 xfs_off_t offset,
885 xfs_off_t len,
Christoph Hellwig5f8aca82013-10-12 00:55:06 -0700886 int alloc_type)
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000887{
888 xfs_mount_t *mp = ip->i_mount;
889 xfs_off_t count;
890 xfs_filblks_t allocated_fsb;
891 xfs_filblks_t allocatesize_fsb;
892 xfs_extlen_t extsz, temp;
893 xfs_fileoff_t startoffset_fsb;
894 xfs_fsblock_t firstfsb;
895 int nimaps;
896 int quota_flag;
897 int rt;
898 xfs_trans_t *tp;
899 xfs_bmbt_irec_t imaps[1], *imapp;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000900 struct xfs_defer_ops dfops;
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000901 uint qblocks, resblks, resrtextents;
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000902 int error;
903
904 trace_xfs_alloc_file_space(ip);
905
906 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +1000907 return -EIO;
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000908
Darrick J. Wongc14cfcc2018-05-04 15:30:21 -0700909 error = xfs_qm_dqattach(ip);
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000910 if (error)
911 return error;
912
913 if (len <= 0)
Dave Chinner24513372014-06-25 14:58:08 +1000914 return -EINVAL;
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000915
916 rt = XFS_IS_REALTIME_INODE(ip);
917 extsz = xfs_get_extsz_hint(ip);
918
919 count = len;
920 imapp = &imaps[0];
921 nimaps = 1;
922 startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
923 allocatesize_fsb = XFS_B_TO_FSB(mp, count);
924
925 /*
926 * Allocate file space until done or until there is an error
927 */
928 while (allocatesize_fsb && !error) {
929 xfs_fileoff_t s, e;
930
931 /*
932 * Determine space reservations for data/realtime.
933 */
934 if (unlikely(extsz)) {
935 s = startoffset_fsb;
936 do_div(s, extsz);
937 s *= extsz;
938 e = startoffset_fsb + allocatesize_fsb;
939 if ((temp = do_mod(startoffset_fsb, extsz)))
940 e += temp;
941 if ((temp = do_mod(e, extsz)))
942 e += extsz - temp;
943 } else {
944 s = 0;
945 e = allocatesize_fsb;
946 }
947
948 /*
949 * The transaction reservation is limited to a 32-bit block
950 * count, hence we need to limit the number of blocks we are
951 * trying to reserve to avoid an overflow. We can't allocate
952 * more than @nimaps extents, and an extent is limited on disk
953 * to MAXEXTLEN (21 bits), so use that to enforce the limit.
954 */
955 resblks = min_t(xfs_fileoff_t, (e - s), (MAXEXTLEN * nimaps));
956 if (unlikely(rt)) {
957 resrtextents = qblocks = resblks;
958 resrtextents /= mp->m_sb.sb_rextsize;
959 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
960 quota_flag = XFS_QMOPT_RES_RTBLKS;
961 } else {
962 resrtextents = 0;
963 resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resblks);
964 quota_flag = XFS_QMOPT_RES_REGBLKS;
965 }
966
967 /*
968 * Allocate and setup the transaction.
969 */
Christoph Hellwig253f4912016-04-06 09:19:55 +1000970 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks,
971 resrtextents, 0, &tp);
972
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000973 /*
974 * Check for running out of space
975 */
976 if (error) {
977 /*
978 * Free the transaction structure.
979 */
Dave Chinner24513372014-06-25 14:58:08 +1000980 ASSERT(error == -ENOSPC || XFS_FORCED_SHUTDOWN(mp));
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000981 break;
982 }
983 xfs_ilock(ip, XFS_ILOCK_EXCL);
984 error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks,
985 0, quota_flag);
986 if (error)
987 goto error1;
988
989 xfs_trans_ijoin(tp, ip, 0);
990
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000991 xfs_defer_init(&dfops, &firstfsb);
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000992 error = xfs_bmapi_write(tp, ip, startoffset_fsb,
993 allocatesize_fsb, alloc_type, &firstfsb,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000994 resblks, imapp, &nimaps, &dfops);
Eric Sandeenf6106ef2016-01-11 11:34:01 +1100995 if (error)
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000996 goto error0;
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000997
998 /*
999 * Complete the transaction
1000 */
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001001 error = xfs_defer_finish(&tp, &dfops);
Eric Sandeenf6106ef2016-01-11 11:34:01 +11001002 if (error)
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001003 goto error0;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001004
Christoph Hellwig70393312015-06-04 13:48:08 +10001005 error = xfs_trans_commit(tp);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001006 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Eric Sandeenf6106ef2016-01-11 11:34:01 +11001007 if (error)
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001008 break;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001009
1010 allocated_fsb = imapp->br_blockcount;
1011
1012 if (nimaps == 0) {
Dave Chinner24513372014-06-25 14:58:08 +10001013 error = -ENOSPC;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001014 break;
1015 }
1016
1017 startoffset_fsb += allocated_fsb;
1018 allocatesize_fsb -= allocated_fsb;
1019 }
1020
1021 return error;
1022
1023error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001024 xfs_defer_cancel(&dfops);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001025 xfs_trans_unreserve_quota_nblks(tp, ip, (long)qblocks, 0, quota_flag);
1026
1027error1: /* Just cancel transaction */
Christoph Hellwig4906e212015-06-04 13:47:56 +10001028 xfs_trans_cancel(tp);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001029 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1030 return error;
1031}
1032
Christoph Hellwigbdb0d042016-06-21 10:00:55 +10001033static int
1034xfs_unmap_extent(
1035 struct xfs_inode *ip,
1036 xfs_fileoff_t startoffset_fsb,
1037 xfs_filblks_t len_fsb,
1038 int *done)
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001039{
Christoph Hellwigbdb0d042016-06-21 10:00:55 +10001040 struct xfs_mount *mp = ip->i_mount;
1041 struct xfs_trans *tp;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001042 struct xfs_defer_ops dfops;
Christoph Hellwigbdb0d042016-06-21 10:00:55 +10001043 xfs_fsblock_t firstfsb;
1044 uint resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
1045 int error;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001046
Christoph Hellwigbdb0d042016-06-21 10:00:55 +10001047 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0, &tp);
1048 if (error) {
1049 ASSERT(error == -ENOSPC || XFS_FORCED_SHUTDOWN(mp));
1050 return error;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001051 }
Christoph Hellwigbdb0d042016-06-21 10:00:55 +10001052
1053 xfs_ilock(ip, XFS_ILOCK_EXCL);
1054 error = xfs_trans_reserve_quota(tp, mp, ip->i_udquot, ip->i_gdquot,
1055 ip->i_pdquot, resblks, 0, XFS_QMOPT_RES_REGBLKS);
1056 if (error)
1057 goto out_trans_cancel;
1058
1059 xfs_trans_ijoin(tp, ip, 0);
1060
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001061 xfs_defer_init(&dfops, &firstfsb);
Christoph Hellwigbdb0d042016-06-21 10:00:55 +10001062 error = xfs_bunmapi(tp, ip, startoffset_fsb, len_fsb, 0, 2, &firstfsb,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001063 &dfops, done);
Christoph Hellwigbdb0d042016-06-21 10:00:55 +10001064 if (error)
1065 goto out_bmap_cancel;
1066
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001067 xfs_defer_ijoin(&dfops, ip);
1068 error = xfs_defer_finish(&tp, &dfops);
Christoph Hellwigbdb0d042016-06-21 10:00:55 +10001069 if (error)
1070 goto out_bmap_cancel;
1071
1072 error = xfs_trans_commit(tp);
1073out_unlock:
1074 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001075 return error;
Christoph Hellwigbdb0d042016-06-21 10:00:55 +10001076
1077out_bmap_cancel:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001078 xfs_defer_cancel(&dfops);
Christoph Hellwigbdb0d042016-06-21 10:00:55 +10001079out_trans_cancel:
1080 xfs_trans_cancel(tp);
1081 goto out_unlock;
1082}
1083
1084static int
1085xfs_adjust_extent_unmap_boundaries(
1086 struct xfs_inode *ip,
1087 xfs_fileoff_t *startoffset_fsb,
1088 xfs_fileoff_t *endoffset_fsb)
1089{
1090 struct xfs_mount *mp = ip->i_mount;
1091 struct xfs_bmbt_irec imap;
1092 int nimap, error;
1093 xfs_extlen_t mod = 0;
1094
1095 nimap = 1;
1096 error = xfs_bmapi_read(ip, *startoffset_fsb, 1, &imap, &nimap, 0);
1097 if (error)
1098 return error;
1099
1100 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
Christoph Hellwigbdb0d042016-06-21 10:00:55 +10001101 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
Eric Sandeen4f1adf32017-04-19 15:19:32 -07001102 mod = do_mod(imap.br_startblock, mp->m_sb.sb_rextsize);
Christoph Hellwigbdb0d042016-06-21 10:00:55 +10001103 if (mod)
1104 *startoffset_fsb += mp->m_sb.sb_rextsize - mod;
1105 }
1106
1107 nimap = 1;
1108 error = xfs_bmapi_read(ip, *endoffset_fsb - 1, 1, &imap, &nimap, 0);
1109 if (error)
1110 return error;
1111
1112 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
1113 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
1114 mod++;
1115 if (mod && mod != mp->m_sb.sb_rextsize)
1116 *endoffset_fsb -= mod;
1117 }
1118
1119 return 0;
1120}
1121
1122static int
1123xfs_flush_unmap_range(
1124 struct xfs_inode *ip,
1125 xfs_off_t offset,
1126 xfs_off_t len)
1127{
1128 struct xfs_mount *mp = ip->i_mount;
1129 struct inode *inode = VFS_I(ip);
1130 xfs_off_t rounding, start, end;
1131 int error;
1132
1133 /* wait for the completion of any pending DIOs */
1134 inode_dio_wait(inode);
1135
1136 rounding = max_t(xfs_off_t, 1 << mp->m_sb.sb_blocklog, PAGE_SIZE);
1137 start = round_down(offset, rounding);
1138 end = round_up(offset + len, rounding) - 1;
1139
1140 error = filemap_write_and_wait_range(inode->i_mapping, start, end);
1141 if (error)
1142 return error;
1143 truncate_pagecache_range(inode, start, end);
1144 return 0;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001145}
1146
Christoph Hellwig83aee9e2013-10-12 00:55:07 -07001147int
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001148xfs_free_file_space(
Christoph Hellwig83aee9e2013-10-12 00:55:07 -07001149 struct xfs_inode *ip,
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001150 xfs_off_t offset,
Christoph Hellwig5f8aca82013-10-12 00:55:06 -07001151 xfs_off_t len)
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001152{
Christoph Hellwigbdb0d042016-06-21 10:00:55 +10001153 struct xfs_mount *mp = ip->i_mount;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001154 xfs_fileoff_t startoffset_fsb;
Christoph Hellwigbdb0d042016-06-21 10:00:55 +10001155 xfs_fileoff_t endoffset_fsb;
Christoph Hellwig3c2bdc92016-06-21 10:02:23 +10001156 int done = 0, error;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001157
1158 trace_xfs_free_file_space(ip);
1159
Darrick J. Wongc14cfcc2018-05-04 15:30:21 -07001160 error = xfs_qm_dqattach(ip);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001161 if (error)
1162 return error;
1163
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001164 if (len <= 0) /* if nothing being freed */
Christoph Hellwigbdb0d042016-06-21 10:00:55 +10001165 return 0;
1166
1167 error = xfs_flush_unmap_range(ip, offset, len);
1168 if (error)
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001169 return error;
Christoph Hellwigbdb0d042016-06-21 10:00:55 +10001170
1171 startoffset_fsb = XFS_B_TO_FSB(mp, offset);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001172 endoffset_fsb = XFS_B_TO_FSBT(mp, offset + len);
1173
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001174 /*
Christoph Hellwigbdb0d042016-06-21 10:00:55 +10001175 * Need to zero the stuff we're not freeing, on disk. If it's a RT file
1176 * and we can't use unwritten extents then we actually need to ensure
1177 * to zero the whole extent, otherwise we just need to take of block
1178 * boundaries, and xfs_bunmapi will handle the rest.
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001179 */
Christoph Hellwigbdb0d042016-06-21 10:00:55 +10001180 if (XFS_IS_REALTIME_INODE(ip) &&
1181 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
1182 error = xfs_adjust_extent_unmap_boundaries(ip, &startoffset_fsb,
1183 &endoffset_fsb);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001184 if (error)
Christoph Hellwigbdb0d042016-06-21 10:00:55 +10001185 return error;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001186 }
Christoph Hellwigbdb0d042016-06-21 10:00:55 +10001187
Christoph Hellwig3c2bdc92016-06-21 10:02:23 +10001188 if (endoffset_fsb > startoffset_fsb) {
1189 while (!done) {
1190 error = xfs_unmap_extent(ip, startoffset_fsb,
1191 endoffset_fsb - startoffset_fsb, &done);
1192 if (error)
1193 return error;
1194 }
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001195 }
1196
1197 /*
Christoph Hellwig3c2bdc92016-06-21 10:02:23 +10001198 * Now that we've unmap all full blocks we'll have to zero out any
Christoph Hellwigf5c547172018-03-13 23:15:32 -07001199 * partial block at the beginning and/or end. iomap_zero_range is smart
1200 * enough to skip any holes, including those we just created, but we
1201 * must take care not to zero beyond EOF and enlarge i_size.
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001202 */
Calvin Owens3dd09d52017-04-03 12:22:29 -07001203 if (offset >= XFS_ISIZE(ip))
1204 return 0;
Calvin Owens3dd09d52017-04-03 12:22:29 -07001205 if (offset + len > XFS_ISIZE(ip))
1206 len = XFS_ISIZE(ip) - offset;
Christoph Hellwigf5c547172018-03-13 23:15:32 -07001207 return iomap_zero_range(VFS_I(ip), offset, len, NULL, &xfs_iomap_ops);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001208}
1209
Brian Foster5d11fb42014-10-30 10:35:11 +11001210/*
1211 * Preallocate and zero a range of a file. This mechanism has the allocation
1212 * semantics of fallocate and in addition converts data in the range to zeroes.
1213 */
Christoph Hellwig865e9442013-10-12 00:55:08 -07001214int
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001215xfs_zero_file_space(
1216 struct xfs_inode *ip,
1217 xfs_off_t offset,
Christoph Hellwig5f8aca82013-10-12 00:55:06 -07001218 xfs_off_t len)
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001219{
1220 struct xfs_mount *mp = ip->i_mount;
Brian Foster5d11fb42014-10-30 10:35:11 +11001221 uint blksize;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001222 int error;
1223
Dave Chinner897b73b2014-04-14 18:15:11 +10001224 trace_xfs_zero_file_space(ip);
1225
Brian Foster5d11fb42014-10-30 10:35:11 +11001226 blksize = 1 << mp->m_sb.sb_blocklog;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001227
1228 /*
Brian Foster5d11fb42014-10-30 10:35:11 +11001229 * Punch a hole and prealloc the range. We use hole punch rather than
1230 * unwritten extent conversion for two reasons:
1231 *
1232 * 1.) Hole punch handles partial block zeroing for us.
1233 *
1234 * 2.) If prealloc returns ENOSPC, the file range is still zero-valued
1235 * by virtue of the hole punch.
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001236 */
Brian Foster5d11fb42014-10-30 10:35:11 +11001237 error = xfs_free_file_space(ip, offset, len);
1238 if (error)
1239 goto out;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001240
Brian Foster5d11fb42014-10-30 10:35:11 +11001241 error = xfs_alloc_file_space(ip, round_down(offset, blksize),
1242 round_up(offset + len, blksize) -
1243 round_down(offset, blksize),
1244 XFS_BMAPI_PREALLOC);
Christoph Hellwig5f8aca82013-10-12 00:55:06 -07001245out:
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001246 return error;
1247
1248}
1249
kbuild test robot72c1a732015-04-13 11:25:04 +10001250static int
Christoph Hellwig4ed36c62017-10-19 11:07:10 -07001251xfs_prepare_shift(
1252 struct xfs_inode *ip,
1253 loff_t offset)
Namjae Jeone1d8fb82014-02-24 10:58:19 +11001254{
Namjae Jeone1d8fb82014-02-24 10:58:19 +11001255 int error;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11001256
Brian Fosterf71721d2014-09-23 15:39:05 +10001257 /*
1258 * Trim eofblocks to avoid shifting uninitialized post-eof preallocation
1259 * into the accessible region of the file.
1260 */
Brian Foster41b9d722014-09-02 12:12:53 +10001261 if (xfs_can_free_eofblocks(ip, true)) {
Brian Fostera36b9262017-01-27 23:22:55 -08001262 error = xfs_free_eofblocks(ip);
Brian Foster41b9d722014-09-02 12:12:53 +10001263 if (error)
1264 return error;
1265 }
Dave Chinner1669a8c2014-09-02 12:12:53 +10001266
Brian Fosterf71721d2014-09-23 15:39:05 +10001267 /*
1268 * Writeback and invalidate cache for the remainder of the file as we're
Namjae Jeona904b1c2015-03-25 15:08:56 +11001269 * about to shift down every extent from offset to EOF.
Brian Fosterf71721d2014-09-23 15:39:05 +10001270 */
Christoph Hellwig4ed36c62017-10-19 11:07:10 -07001271 error = filemap_write_and_wait_range(VFS_I(ip)->i_mapping, offset, -1);
Brian Fosterf71721d2014-09-23 15:39:05 +10001272 if (error)
1273 return error;
1274 error = invalidate_inode_pages2_range(VFS_I(ip)->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001275 offset >> PAGE_SHIFT, -1);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11001276 if (error)
1277 return error;
1278
Namjae Jeona904b1c2015-03-25 15:08:56 +11001279 /*
Darrick J. Wong3af423b2017-09-18 09:41:17 -07001280 * Clean out anything hanging around in the cow fork now that
1281 * we've flushed all the dirty data out to disk to avoid having
1282 * CoW extents at the wrong offsets.
1283 */
1284 if (xfs_is_reflink_inode(ip)) {
1285 error = xfs_reflink_cancel_cow_range(ip, offset, NULLFILEOFF,
1286 true);
1287 if (error)
1288 return error;
1289 }
1290
Christoph Hellwig4ed36c62017-10-19 11:07:10 -07001291 return 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11001292}
1293
1294/*
Namjae Jeona904b1c2015-03-25 15:08:56 +11001295 * xfs_collapse_file_space()
1296 * This routine frees disk space and shift extent for the given file.
1297 * The first thing we do is to free data blocks in the specified range
1298 * by calling xfs_free_file_space(). It would also sync dirty data
1299 * and invalidate page cache over the region on which collapse range
1300 * is working. And Shift extent records to the left to cover a hole.
1301 * RETURNS:
1302 * 0 on success
1303 * errno on error
1304 *
1305 */
1306int
1307xfs_collapse_file_space(
1308 struct xfs_inode *ip,
1309 xfs_off_t offset,
1310 xfs_off_t len)
1311{
Christoph Hellwig4ed36c62017-10-19 11:07:10 -07001312 struct xfs_mount *mp = ip->i_mount;
1313 struct xfs_trans *tp;
1314 int error;
1315 struct xfs_defer_ops dfops;
1316 xfs_fsblock_t first_block;
Christoph Hellwig4ed36c62017-10-19 11:07:10 -07001317 xfs_fileoff_t next_fsb = XFS_B_TO_FSB(mp, offset + len);
1318 xfs_fileoff_t shift_fsb = XFS_B_TO_FSB(mp, len);
1319 uint resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07001320 bool done = false;
Namjae Jeona904b1c2015-03-25 15:08:56 +11001321
1322 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
Christoph Hellwig9ad1a23a2017-10-23 16:32:38 -07001323 ASSERT(xfs_isilocked(ip, XFS_MMAPLOCK_EXCL));
1324
Namjae Jeona904b1c2015-03-25 15:08:56 +11001325 trace_xfs_collapse_file_space(ip);
1326
1327 error = xfs_free_file_space(ip, offset, len);
1328 if (error)
1329 return error;
1330
Christoph Hellwig4ed36c62017-10-19 11:07:10 -07001331 error = xfs_prepare_shift(ip, offset);
1332 if (error)
1333 return error;
1334
1335 while (!error && !done) {
1336 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0,
1337 &tp);
1338 if (error)
1339 break;
1340
1341 xfs_ilock(ip, XFS_ILOCK_EXCL);
1342 error = xfs_trans_reserve_quota(tp, mp, ip->i_udquot,
1343 ip->i_gdquot, ip->i_pdquot, resblks, 0,
1344 XFS_QMOPT_RES_REGBLKS);
1345 if (error)
1346 goto out_trans_cancel;
1347 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1348
1349 xfs_defer_init(&dfops, &first_block);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07001350 error = xfs_bmap_collapse_extents(tp, ip, &next_fsb, shift_fsb,
Eric Sandeena1f69412018-04-06 10:09:42 -07001351 &done, &first_block, &dfops);
Christoph Hellwig4ed36c62017-10-19 11:07:10 -07001352 if (error)
1353 goto out_bmap_cancel;
1354
1355 error = xfs_defer_finish(&tp, &dfops);
1356 if (error)
1357 goto out_bmap_cancel;
1358 error = xfs_trans_commit(tp);
1359 }
1360
1361 return error;
1362
1363out_bmap_cancel:
1364 xfs_defer_cancel(&dfops);
1365out_trans_cancel:
1366 xfs_trans_cancel(tp);
1367 return error;
Namjae Jeona904b1c2015-03-25 15:08:56 +11001368}
1369
1370/*
1371 * xfs_insert_file_space()
1372 * This routine create hole space by shifting extents for the given file.
1373 * The first thing we do is to sync dirty data and invalidate page cache
1374 * over the region on which insert range is working. And split an extent
1375 * to two extents at given offset by calling xfs_bmap_split_extent.
1376 * And shift all extent records which are laying between [offset,
1377 * last allocated extent] to the right to reserve hole range.
1378 * RETURNS:
1379 * 0 on success
1380 * errno on error
1381 */
1382int
1383xfs_insert_file_space(
1384 struct xfs_inode *ip,
1385 loff_t offset,
1386 loff_t len)
1387{
Christoph Hellwig4ed36c62017-10-19 11:07:10 -07001388 struct xfs_mount *mp = ip->i_mount;
1389 struct xfs_trans *tp;
1390 int error;
1391 struct xfs_defer_ops dfops;
1392 xfs_fsblock_t first_block;
1393 xfs_fileoff_t stop_fsb = XFS_B_TO_FSB(mp, offset);
1394 xfs_fileoff_t next_fsb = NULLFSBLOCK;
1395 xfs_fileoff_t shift_fsb = XFS_B_TO_FSB(mp, len);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07001396 bool done = false;
Christoph Hellwig4ed36c62017-10-19 11:07:10 -07001397
Namjae Jeona904b1c2015-03-25 15:08:56 +11001398 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
Christoph Hellwig9ad1a23a2017-10-23 16:32:38 -07001399 ASSERT(xfs_isilocked(ip, XFS_MMAPLOCK_EXCL));
1400
Namjae Jeona904b1c2015-03-25 15:08:56 +11001401 trace_xfs_insert_file_space(ip);
1402
Christoph Hellwig4ed36c62017-10-19 11:07:10 -07001403 error = xfs_prepare_shift(ip, offset);
1404 if (error)
1405 return error;
1406
1407 /*
1408 * The extent shifting code works on extent granularity. So, if stop_fsb
1409 * is not the starting block of extent, we need to split the extent at
1410 * stop_fsb.
1411 */
1412 error = xfs_bmap_split_extent(ip, stop_fsb);
1413 if (error)
1414 return error;
1415
1416 while (!error && !done) {
1417 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0, 0,
1418 &tp);
1419 if (error)
1420 break;
1421
1422 xfs_ilock(ip, XFS_ILOCK_EXCL);
1423 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1424 xfs_defer_init(&dfops, &first_block);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07001425 error = xfs_bmap_insert_extents(tp, ip, &next_fsb, shift_fsb,
1426 &done, stop_fsb, &first_block, &dfops);
Christoph Hellwig4ed36c62017-10-19 11:07:10 -07001427 if (error)
1428 goto out_bmap_cancel;
1429
1430 error = xfs_defer_finish(&tp, &dfops);
1431 if (error)
1432 goto out_bmap_cancel;
1433 error = xfs_trans_commit(tp);
1434 }
1435
1436 return error;
1437
1438out_bmap_cancel:
1439 xfs_defer_cancel(&dfops);
1440 xfs_trans_cancel(tp);
1441 return error;
Namjae Jeona904b1c2015-03-25 15:08:56 +11001442}
1443
1444/*
Dave Chinnera133d952013-08-12 20:49:48 +10001445 * We need to check that the format of the data fork in the temporary inode is
1446 * valid for the target inode before doing the swap. This is not a problem with
1447 * attr1 because of the fixed fork offset, but attr2 has a dynamically sized
1448 * data fork depending on the space the attribute fork is taking so we can get
1449 * invalid formats on the target inode.
1450 *
1451 * E.g. target has space for 7 extents in extent format, temp inode only has
1452 * space for 6. If we defragment down to 7 extents, then the tmp format is a
1453 * btree, but when swapped it needs to be in extent format. Hence we can't just
1454 * blindly swap data forks on attr2 filesystems.
1455 *
1456 * Note that we check the swap in both directions so that we don't end up with
1457 * a corrupt temporary inode, either.
1458 *
1459 * Note that fixing the way xfs_fsr sets up the attribute fork in the source
1460 * inode will prevent this situation from occurring, so all we do here is
1461 * reject and log the attempt. basically we are putting the responsibility on
1462 * userspace to get this right.
1463 */
1464static int
1465xfs_swap_extents_check_format(
Darrick J. Wonge06259a2016-10-03 09:11:52 -07001466 struct xfs_inode *ip, /* target inode */
1467 struct xfs_inode *tip) /* tmp inode */
Dave Chinnera133d952013-08-12 20:49:48 +10001468{
1469
1470 /* Should never get a local format */
1471 if (ip->i_d.di_format == XFS_DINODE_FMT_LOCAL ||
1472 tip->i_d.di_format == XFS_DINODE_FMT_LOCAL)
Dave Chinner24513372014-06-25 14:58:08 +10001473 return -EINVAL;
Dave Chinnera133d952013-08-12 20:49:48 +10001474
1475 /*
1476 * if the target inode has less extents that then temporary inode then
1477 * why did userspace call us?
1478 */
1479 if (ip->i_d.di_nextents < tip->i_d.di_nextents)
Dave Chinner24513372014-06-25 14:58:08 +10001480 return -EINVAL;
Dave Chinnera133d952013-08-12 20:49:48 +10001481
1482 /*
Darrick J. Wong1f08af52016-10-03 09:11:53 -07001483 * If we have to use the (expensive) rmap swap method, we can
1484 * handle any number of extents and any format.
1485 */
1486 if (xfs_sb_version_hasrmapbt(&ip->i_mount->m_sb))
1487 return 0;
1488
1489 /*
Dave Chinnera133d952013-08-12 20:49:48 +10001490 * if the target inode is in extent form and the temp inode is in btree
1491 * form then we will end up with the target inode in the wrong format
1492 * as we already know there are less extents in the temp inode.
1493 */
1494 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1495 tip->i_d.di_format == XFS_DINODE_FMT_BTREE)
Dave Chinner24513372014-06-25 14:58:08 +10001496 return -EINVAL;
Dave Chinnera133d952013-08-12 20:49:48 +10001497
1498 /* Check temp in extent form to max in target */
1499 if (tip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1500 XFS_IFORK_NEXTENTS(tip, XFS_DATA_FORK) >
1501 XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK))
Dave Chinner24513372014-06-25 14:58:08 +10001502 return -EINVAL;
Dave Chinnera133d952013-08-12 20:49:48 +10001503
1504 /* Check target in extent form to max in temp */
1505 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1506 XFS_IFORK_NEXTENTS(ip, XFS_DATA_FORK) >
1507 XFS_IFORK_MAXEXT(tip, XFS_DATA_FORK))
Dave Chinner24513372014-06-25 14:58:08 +10001508 return -EINVAL;
Dave Chinnera133d952013-08-12 20:49:48 +10001509
1510 /*
1511 * If we are in a btree format, check that the temp root block will fit
1512 * in the target and that it has enough extents to be in btree format
1513 * in the target.
1514 *
1515 * Note that we have to be careful to allow btree->extent conversions
1516 * (a common defrag case) which will occur when the temp inode is in
1517 * extent format...
1518 */
1519 if (tip->i_d.di_format == XFS_DINODE_FMT_BTREE) {
Arnd Bergmann0cbe48c2017-06-14 21:35:34 -07001520 if (XFS_IFORK_Q(ip) &&
Dave Chinnera133d952013-08-12 20:49:48 +10001521 XFS_BMAP_BMDR_SPACE(tip->i_df.if_broot) > XFS_IFORK_BOFF(ip))
Dave Chinner24513372014-06-25 14:58:08 +10001522 return -EINVAL;
Dave Chinnera133d952013-08-12 20:49:48 +10001523 if (XFS_IFORK_NEXTENTS(tip, XFS_DATA_FORK) <=
1524 XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK))
Dave Chinner24513372014-06-25 14:58:08 +10001525 return -EINVAL;
Dave Chinnera133d952013-08-12 20:49:48 +10001526 }
1527
1528 /* Reciprocal target->temp btree format checks */
1529 if (ip->i_d.di_format == XFS_DINODE_FMT_BTREE) {
Arnd Bergmann0cbe48c2017-06-14 21:35:34 -07001530 if (XFS_IFORK_Q(tip) &&
Dave Chinnera133d952013-08-12 20:49:48 +10001531 XFS_BMAP_BMDR_SPACE(ip->i_df.if_broot) > XFS_IFORK_BOFF(tip))
Dave Chinner24513372014-06-25 14:58:08 +10001532 return -EINVAL;
Dave Chinnera133d952013-08-12 20:49:48 +10001533 if (XFS_IFORK_NEXTENTS(ip, XFS_DATA_FORK) <=
1534 XFS_IFORK_MAXEXT(tip, XFS_DATA_FORK))
Dave Chinner24513372014-06-25 14:58:08 +10001535 return -EINVAL;
Dave Chinnera133d952013-08-12 20:49:48 +10001536 }
1537
1538 return 0;
1539}
1540
Dave Chinner7abbb8f2014-09-23 16:20:11 +10001541static int
Dave Chinner4ef897a2014-08-04 13:44:08 +10001542xfs_swap_extent_flush(
1543 struct xfs_inode *ip)
1544{
1545 int error;
1546
1547 error = filemap_write_and_wait(VFS_I(ip)->i_mapping);
1548 if (error)
1549 return error;
1550 truncate_pagecache_range(VFS_I(ip), 0, -1);
1551
1552 /* Verify O_DIRECT for ftmp */
1553 if (VFS_I(ip)->i_mapping->nrpages)
1554 return -EINVAL;
Dave Chinner4ef897a2014-08-04 13:44:08 +10001555 return 0;
1556}
1557
Darrick J. Wong1f08af52016-10-03 09:11:53 -07001558/*
1559 * Move extents from one file to another, when rmap is enabled.
1560 */
1561STATIC int
1562xfs_swap_extent_rmap(
1563 struct xfs_trans **tpp,
1564 struct xfs_inode *ip,
1565 struct xfs_inode *tip)
1566{
1567 struct xfs_bmbt_irec irec;
1568 struct xfs_bmbt_irec uirec;
1569 struct xfs_bmbt_irec tirec;
1570 xfs_fileoff_t offset_fsb;
1571 xfs_fileoff_t end_fsb;
1572 xfs_filblks_t count_fsb;
1573 xfs_fsblock_t firstfsb;
1574 struct xfs_defer_ops dfops;
1575 int error;
1576 xfs_filblks_t ilen;
1577 xfs_filblks_t rlen;
1578 int nimaps;
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07001579 uint64_t tip_flags2;
Darrick J. Wong1f08af52016-10-03 09:11:53 -07001580
1581 /*
1582 * If the source file has shared blocks, we must flag the donor
1583 * file as having shared blocks so that we get the shared-block
1584 * rmap functions when we go to fix up the rmaps. The flags
1585 * will be switch for reals later.
1586 */
1587 tip_flags2 = tip->i_d.di_flags2;
1588 if (ip->i_d.di_flags2 & XFS_DIFLAG2_REFLINK)
1589 tip->i_d.di_flags2 |= XFS_DIFLAG2_REFLINK;
1590
1591 offset_fsb = 0;
1592 end_fsb = XFS_B_TO_FSB(ip->i_mount, i_size_read(VFS_I(ip)));
1593 count_fsb = (xfs_filblks_t)(end_fsb - offset_fsb);
1594
1595 while (count_fsb) {
1596 /* Read extent from the donor file */
1597 nimaps = 1;
1598 error = xfs_bmapi_read(tip, offset_fsb, count_fsb, &tirec,
1599 &nimaps, 0);
1600 if (error)
1601 goto out;
1602 ASSERT(nimaps == 1);
1603 ASSERT(tirec.br_startblock != DELAYSTARTBLOCK);
1604
1605 trace_xfs_swap_extent_rmap_remap(tip, &tirec);
1606 ilen = tirec.br_blockcount;
1607
1608 /* Unmap the old blocks in the source file. */
1609 while (tirec.br_blockcount) {
1610 xfs_defer_init(&dfops, &firstfsb);
1611 trace_xfs_swap_extent_rmap_remap_piece(tip, &tirec);
1612
1613 /* Read extent from the source file */
1614 nimaps = 1;
1615 error = xfs_bmapi_read(ip, tirec.br_startoff,
1616 tirec.br_blockcount, &irec,
1617 &nimaps, 0);
1618 if (error)
1619 goto out_defer;
1620 ASSERT(nimaps == 1);
1621 ASSERT(tirec.br_startoff == irec.br_startoff);
1622 trace_xfs_swap_extent_rmap_remap_piece(ip, &irec);
1623
1624 /* Trim the extent. */
1625 uirec = tirec;
1626 uirec.br_blockcount = rlen = min_t(xfs_filblks_t,
1627 tirec.br_blockcount,
1628 irec.br_blockcount);
1629 trace_xfs_swap_extent_rmap_remap_piece(tip, &uirec);
1630
1631 /* Remove the mapping from the donor file. */
1632 error = xfs_bmap_unmap_extent((*tpp)->t_mountp, &dfops,
1633 tip, &uirec);
1634 if (error)
1635 goto out_defer;
1636
1637 /* Remove the mapping from the source file. */
1638 error = xfs_bmap_unmap_extent((*tpp)->t_mountp, &dfops,
1639 ip, &irec);
1640 if (error)
1641 goto out_defer;
1642
1643 /* Map the donor file's blocks into the source file. */
1644 error = xfs_bmap_map_extent((*tpp)->t_mountp, &dfops,
1645 ip, &uirec);
1646 if (error)
1647 goto out_defer;
1648
1649 /* Map the source file's blocks into the donor file. */
1650 error = xfs_bmap_map_extent((*tpp)->t_mountp, &dfops,
1651 tip, &irec);
1652 if (error)
1653 goto out_defer;
1654
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001655 xfs_defer_ijoin(&dfops, ip);
1656 error = xfs_defer_finish(tpp, &dfops);
Darrick J. Wong1f08af52016-10-03 09:11:53 -07001657 if (error)
1658 goto out_defer;
1659
1660 tirec.br_startoff += rlen;
1661 if (tirec.br_startblock != HOLESTARTBLOCK &&
1662 tirec.br_startblock != DELAYSTARTBLOCK)
1663 tirec.br_startblock += rlen;
1664 tirec.br_blockcount -= rlen;
1665 }
1666
1667 /* Roll on... */
1668 count_fsb -= ilen;
1669 offset_fsb += ilen;
1670 }
1671
1672 tip->i_d.di_flags2 = tip_flags2;
1673 return 0;
1674
1675out_defer:
1676 xfs_defer_cancel(&dfops);
1677out:
1678 trace_xfs_swap_extent_rmap_error(ip, error, _RET_IP_);
1679 tip->i_d.di_flags2 = tip_flags2;
1680 return error;
1681}
1682
Darrick J. Wong39aff5f2016-10-03 09:11:53 -07001683/* Swap the extents of two files by swapping data forks. */
1684STATIC int
1685xfs_swap_extent_forks(
1686 struct xfs_trans *tp,
1687 struct xfs_inode *ip,
1688 struct xfs_inode *tip,
1689 int *src_log_flags,
1690 int *target_log_flags)
1691{
1692 struct xfs_ifork tempifp, *ifp, *tifp;
Darrick J. Wonge7f5d5c2017-06-16 11:00:12 -07001693 xfs_filblks_t aforkblks = 0;
1694 xfs_filblks_t taforkblks = 0;
1695 xfs_extnum_t junk;
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07001696 uint64_t tmp;
Darrick J. Wong39aff5f2016-10-03 09:11:53 -07001697 int error;
1698
1699 /*
1700 * Count the number of extended attribute blocks
1701 */
1702 if ( ((XFS_IFORK_Q(ip) != 0) && (ip->i_d.di_anextents > 0)) &&
1703 (ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)) {
Darrick J. Wonge7f5d5c2017-06-16 11:00:12 -07001704 error = xfs_bmap_count_blocks(tp, ip, XFS_ATTR_FORK, &junk,
Darrick J. Wong39aff5f2016-10-03 09:11:53 -07001705 &aforkblks);
1706 if (error)
1707 return error;
1708 }
1709 if ( ((XFS_IFORK_Q(tip) != 0) && (tip->i_d.di_anextents > 0)) &&
1710 (tip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)) {
Darrick J. Wonge7f5d5c2017-06-16 11:00:12 -07001711 error = xfs_bmap_count_blocks(tp, tip, XFS_ATTR_FORK, &junk,
Darrick J. Wong39aff5f2016-10-03 09:11:53 -07001712 &taforkblks);
1713 if (error)
1714 return error;
1715 }
1716
1717 /*
Brian Foster6fb10d62017-08-29 10:08:39 -07001718 * Btree format (v3) inodes have the inode number stamped in the bmbt
1719 * block headers. We can't start changing the bmbt blocks until the
1720 * inode owner change is logged so recovery does the right thing in the
1721 * event of a crash. Set the owner change log flags now and leave the
1722 * bmbt scan as the last step.
Darrick J. Wong39aff5f2016-10-03 09:11:53 -07001723 */
1724 if (ip->i_d.di_version == 3 &&
Brian Foster6fb10d62017-08-29 10:08:39 -07001725 ip->i_d.di_format == XFS_DINODE_FMT_BTREE)
Darrick J. Wong39aff5f2016-10-03 09:11:53 -07001726 (*target_log_flags) |= XFS_ILOG_DOWNER;
Darrick J. Wong39aff5f2016-10-03 09:11:53 -07001727 if (tip->i_d.di_version == 3 &&
Brian Foster6fb10d62017-08-29 10:08:39 -07001728 tip->i_d.di_format == XFS_DINODE_FMT_BTREE)
Darrick J. Wong39aff5f2016-10-03 09:11:53 -07001729 (*src_log_flags) |= XFS_ILOG_DOWNER;
Darrick J. Wong39aff5f2016-10-03 09:11:53 -07001730
1731 /*
1732 * Swap the data forks of the inodes
1733 */
1734 ifp = &ip->i_df;
1735 tifp = &tip->i_df;
1736 tempifp = *ifp; /* struct copy */
1737 *ifp = *tifp; /* struct copy */
1738 *tifp = tempifp; /* struct copy */
1739
1740 /*
1741 * Fix the on-disk inode values
1742 */
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07001743 tmp = (uint64_t)ip->i_d.di_nblocks;
Darrick J. Wong39aff5f2016-10-03 09:11:53 -07001744 ip->i_d.di_nblocks = tip->i_d.di_nblocks - taforkblks + aforkblks;
1745 tip->i_d.di_nblocks = tmp + taforkblks - aforkblks;
1746
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07001747 tmp = (uint64_t) ip->i_d.di_nextents;
Darrick J. Wong39aff5f2016-10-03 09:11:53 -07001748 ip->i_d.di_nextents = tip->i_d.di_nextents;
1749 tip->i_d.di_nextents = tmp;
1750
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07001751 tmp = (uint64_t) ip->i_d.di_format;
Darrick J. Wong39aff5f2016-10-03 09:11:53 -07001752 ip->i_d.di_format = tip->i_d.di_format;
1753 tip->i_d.di_format = tmp;
1754
1755 /*
1756 * The extents in the source inode could still contain speculative
1757 * preallocation beyond EOF (e.g. the file is open but not modified
1758 * while defrag is in progress). In that case, we need to copy over the
1759 * number of delalloc blocks the data fork in the source inode is
1760 * tracking beyond EOF so that when the fork is truncated away when the
1761 * temporary inode is unlinked we don't underrun the i_delayed_blks
1762 * counter on that inode.
1763 */
1764 ASSERT(tip->i_delayed_blks == 0);
1765 tip->i_delayed_blks = ip->i_delayed_blks;
1766 ip->i_delayed_blks = 0;
1767
1768 switch (ip->i_d.di_format) {
1769 case XFS_DINODE_FMT_EXTENTS:
Darrick J. Wong39aff5f2016-10-03 09:11:53 -07001770 (*src_log_flags) |= XFS_ILOG_DEXT;
1771 break;
1772 case XFS_DINODE_FMT_BTREE:
1773 ASSERT(ip->i_d.di_version < 3 ||
1774 (*src_log_flags & XFS_ILOG_DOWNER));
1775 (*src_log_flags) |= XFS_ILOG_DBROOT;
1776 break;
1777 }
1778
1779 switch (tip->i_d.di_format) {
1780 case XFS_DINODE_FMT_EXTENTS:
Darrick J. Wong39aff5f2016-10-03 09:11:53 -07001781 (*target_log_flags) |= XFS_ILOG_DEXT;
1782 break;
1783 case XFS_DINODE_FMT_BTREE:
1784 (*target_log_flags) |= XFS_ILOG_DBROOT;
1785 ASSERT(tip->i_d.di_version < 3 ||
1786 (*target_log_flags & XFS_ILOG_DOWNER));
1787 break;
1788 }
1789
1790 return 0;
1791}
1792
Brian Foster2dd3d702017-08-29 10:08:40 -07001793/*
1794 * Fix up the owners of the bmbt blocks to refer to the current inode. The
1795 * change owner scan attempts to order all modified buffers in the current
1796 * transaction. In the event of ordered buffer failure, the offending buffer is
1797 * physically logged as a fallback and the scan returns -EAGAIN. We must roll
1798 * the transaction in this case to replenish the fallback log reservation and
1799 * restart the scan. This process repeats until the scan completes.
1800 */
1801static int
1802xfs_swap_change_owner(
1803 struct xfs_trans **tpp,
1804 struct xfs_inode *ip,
1805 struct xfs_inode *tmpip)
1806{
1807 int error;
1808 struct xfs_trans *tp = *tpp;
1809
1810 do {
1811 error = xfs_bmbt_change_owner(tp, ip, XFS_DATA_FORK, ip->i_ino,
1812 NULL);
1813 /* success or fatal error */
1814 if (error != -EAGAIN)
1815 break;
1816
1817 error = xfs_trans_roll(tpp);
1818 if (error)
1819 break;
1820 tp = *tpp;
1821
1822 /*
1823 * Redirty both inodes so they can relog and keep the log tail
1824 * moving forward.
1825 */
1826 xfs_trans_ijoin(tp, ip, 0);
1827 xfs_trans_ijoin(tp, tmpip, 0);
1828 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1829 xfs_trans_log_inode(tp, tmpip, XFS_ILOG_CORE);
1830 } while (true);
1831
1832 return error;
1833}
1834
Dave Chinner4ef897a2014-08-04 13:44:08 +10001835int
Dave Chinnera133d952013-08-12 20:49:48 +10001836xfs_swap_extents(
Darrick J. Wonge06259a2016-10-03 09:11:52 -07001837 struct xfs_inode *ip, /* target inode */
1838 struct xfs_inode *tip, /* tmp inode */
1839 struct xfs_swapext *sxp)
Dave Chinnera133d952013-08-12 20:49:48 +10001840{
Darrick J. Wonge06259a2016-10-03 09:11:52 -07001841 struct xfs_mount *mp = ip->i_mount;
1842 struct xfs_trans *tp;
1843 struct xfs_bstat *sbp = &sxp->sx_stat;
Darrick J. Wonge06259a2016-10-03 09:11:52 -07001844 int src_log_flags, target_log_flags;
1845 int error = 0;
Darrick J. Wonge06259a2016-10-03 09:11:52 -07001846 int lock_flags;
Darrick J. Wongf0bc4d12016-10-03 09:11:42 -07001847 struct xfs_ifork *cowfp;
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07001848 uint64_t f;
Brian Foster2dd3d702017-08-29 10:08:40 -07001849 int resblks = 0;
Dave Chinnera133d952013-08-12 20:49:48 +10001850
Dave Chinnera133d952013-08-12 20:49:48 +10001851 /*
Dave Chinner723cac42015-02-23 21:47:29 +11001852 * Lock the inodes against other IO, page faults and truncate to
1853 * begin with. Then we can ensure the inodes are flushed and have no
1854 * page cache safely. Once we have done this we can take the ilocks and
1855 * do the rest of the checks.
Dave Chinnera133d952013-08-12 20:49:48 +10001856 */
Christoph Hellwig65523212016-11-30 14:33:25 +11001857 lock_two_nondirectories(VFS_I(ip), VFS_I(tip));
1858 lock_flags = XFS_MMAPLOCK_EXCL;
Darrick J. Wong7c2d2382018-01-26 15:27:33 -08001859 xfs_lock_two_inodes(ip, XFS_MMAPLOCK_EXCL, tip, XFS_MMAPLOCK_EXCL);
Dave Chinnera133d952013-08-12 20:49:48 +10001860
1861 /* Verify that both files have the same format */
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001862 if ((VFS_I(ip)->i_mode & S_IFMT) != (VFS_I(tip)->i_mode & S_IFMT)) {
Dave Chinner24513372014-06-25 14:58:08 +10001863 error = -EINVAL;
Dave Chinnera133d952013-08-12 20:49:48 +10001864 goto out_unlock;
1865 }
1866
1867 /* Verify both files are either real-time or non-realtime */
1868 if (XFS_IS_REALTIME_INODE(ip) != XFS_IS_REALTIME_INODE(tip)) {
Dave Chinner24513372014-06-25 14:58:08 +10001869 error = -EINVAL;
Dave Chinnera133d952013-08-12 20:49:48 +10001870 goto out_unlock;
1871 }
1872
Dave Chinner4ef897a2014-08-04 13:44:08 +10001873 error = xfs_swap_extent_flush(ip);
Dave Chinnera133d952013-08-12 20:49:48 +10001874 if (error)
1875 goto out_unlock;
Dave Chinner4ef897a2014-08-04 13:44:08 +10001876 error = xfs_swap_extent_flush(tip);
1877 if (error)
1878 goto out_unlock;
Dave Chinnera133d952013-08-12 20:49:48 +10001879
Darrick J. Wong1f08af52016-10-03 09:11:53 -07001880 /*
1881 * Extent "swapping" with rmap requires a permanent reservation and
1882 * a block reservation because it's really just a remap operation
1883 * performed with log redo items!
1884 */
1885 if (xfs_sb_version_hasrmapbt(&mp->m_sb)) {
Brian Fosterb3fed432018-03-09 14:01:58 -08001886 int w = XFS_DATA_FORK;
1887 uint32_t ipnext = XFS_IFORK_NEXTENTS(ip, w);
1888 uint32_t tipnext = XFS_IFORK_NEXTENTS(tip, w);
1889
Darrick J. Wong1f08af52016-10-03 09:11:53 -07001890 /*
Brian Fosterb3fed432018-03-09 14:01:58 -08001891 * Conceptually this shouldn't affect the shape of either bmbt,
1892 * but since we atomically move extents one by one, we reserve
1893 * enough space to rebuild both trees.
Darrick J. Wong1f08af52016-10-03 09:11:53 -07001894 */
Brian Fosterb3fed432018-03-09 14:01:58 -08001895 resblks = XFS_SWAP_RMAP_SPACE_RES(mp, ipnext, w);
1896 resblks += XFS_SWAP_RMAP_SPACE_RES(mp, tipnext, w);
1897
1898 /*
1899 * Handle the corner case where either inode might straddle the
1900 * btree format boundary. If so, the inode could bounce between
1901 * btree <-> extent format on unmap -> remap cycles, freeing and
1902 * allocating a bmapbt block each time.
1903 */
1904 if (ipnext == (XFS_IFORK_MAXEXT(ip, w) + 1))
1905 resblks += XFS_IFORK_MAXEXT(ip, w);
1906 if (tipnext == (XFS_IFORK_MAXEXT(tip, w) + 1))
1907 resblks += XFS_IFORK_MAXEXT(tip, w);
Brian Foster2dd3d702017-08-29 10:08:40 -07001908 }
1909 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0, &tp);
Christoph Hellwig253f4912016-04-06 09:19:55 +10001910 if (error)
Dave Chinnera133d952013-08-12 20:49:48 +10001911 goto out_unlock;
Dave Chinner723cac42015-02-23 21:47:29 +11001912
1913 /*
1914 * Lock and join the inodes to the tansaction so that transaction commit
1915 * or cancel will unlock the inodes from this point onwards.
1916 */
Darrick J. Wong7c2d2382018-01-26 15:27:33 -08001917 xfs_lock_two_inodes(ip, XFS_ILOCK_EXCL, tip, XFS_ILOCK_EXCL);
Dave Chinner4ef897a2014-08-04 13:44:08 +10001918 lock_flags |= XFS_ILOCK_EXCL;
Darrick J. Wong39aff5f2016-10-03 09:11:53 -07001919 xfs_trans_ijoin(tp, ip, 0);
1920 xfs_trans_ijoin(tp, tip, 0);
Dave Chinner723cac42015-02-23 21:47:29 +11001921
Dave Chinnera133d952013-08-12 20:49:48 +10001922
1923 /* Verify all data are being swapped */
1924 if (sxp->sx_offset != 0 ||
1925 sxp->sx_length != ip->i_d.di_size ||
1926 sxp->sx_length != tip->i_d.di_size) {
Dave Chinner24513372014-06-25 14:58:08 +10001927 error = -EFAULT;
Dave Chinner4ef897a2014-08-04 13:44:08 +10001928 goto out_trans_cancel;
Dave Chinnera133d952013-08-12 20:49:48 +10001929 }
1930
1931 trace_xfs_swap_extent_before(ip, 0);
1932 trace_xfs_swap_extent_before(tip, 1);
1933
1934 /* check inode formats now that data is flushed */
1935 error = xfs_swap_extents_check_format(ip, tip);
1936 if (error) {
1937 xfs_notice(mp,
1938 "%s: inode 0x%llx format is incompatible for exchanging.",
1939 __func__, ip->i_ino);
Dave Chinner4ef897a2014-08-04 13:44:08 +10001940 goto out_trans_cancel;
Dave Chinnera133d952013-08-12 20:49:48 +10001941 }
1942
1943 /*
1944 * Compare the current change & modify times with that
1945 * passed in. If they differ, we abort this swap.
1946 * This is the mechanism used to ensure the calling
1947 * process that the file was not changed out from
1948 * under it.
1949 */
1950 if ((sbp->bs_ctime.tv_sec != VFS_I(ip)->i_ctime.tv_sec) ||
1951 (sbp->bs_ctime.tv_nsec != VFS_I(ip)->i_ctime.tv_nsec) ||
1952 (sbp->bs_mtime.tv_sec != VFS_I(ip)->i_mtime.tv_sec) ||
1953 (sbp->bs_mtime.tv_nsec != VFS_I(ip)->i_mtime.tv_nsec)) {
Dave Chinner24513372014-06-25 14:58:08 +10001954 error = -EBUSY;
Dave Chinner81217682014-08-04 13:29:32 +10001955 goto out_trans_cancel;
Dave Chinnera133d952013-08-12 20:49:48 +10001956 }
Dave Chinnera133d952013-08-12 20:49:48 +10001957
Dave Chinner21b5c972013-08-30 10:23:44 +10001958 /*
Dave Chinner21b5c972013-08-30 10:23:44 +10001959 * Note the trickiness in setting the log flags - we set the owner log
1960 * flag on the opposite inode (i.e. the inode we are setting the new
1961 * owner to be) because once we swap the forks and log that, log
1962 * recovery is going to see the fork as owned by the swapped inode,
1963 * not the pre-swapped inodes.
1964 */
1965 src_log_flags = XFS_ILOG_CORE;
1966 target_log_flags = XFS_ILOG_CORE;
Dave Chinner21b5c972013-08-30 10:23:44 +10001967
Darrick J. Wong1f08af52016-10-03 09:11:53 -07001968 if (xfs_sb_version_hasrmapbt(&mp->m_sb))
1969 error = xfs_swap_extent_rmap(&tp, ip, tip);
1970 else
1971 error = xfs_swap_extent_forks(tp, ip, tip, &src_log_flags,
1972 &target_log_flags);
Darrick J. Wong39aff5f2016-10-03 09:11:53 -07001973 if (error)
1974 goto out_trans_cancel;
Dave Chinnera133d952013-08-12 20:49:48 +10001975
Darrick J. Wongf0bc4d12016-10-03 09:11:42 -07001976 /* Do we have to swap reflink flags? */
1977 if ((ip->i_d.di_flags2 & XFS_DIFLAG2_REFLINK) ^
1978 (tip->i_d.di_flags2 & XFS_DIFLAG2_REFLINK)) {
1979 f = ip->i_d.di_flags2 & XFS_DIFLAG2_REFLINK;
1980 ip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK;
1981 ip->i_d.di_flags2 |= tip->i_d.di_flags2 & XFS_DIFLAG2_REFLINK;
1982 tip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK;
1983 tip->i_d.di_flags2 |= f & XFS_DIFLAG2_REFLINK;
Darrick J. Wong52bfcdd2017-09-18 09:41:18 -07001984 }
1985
1986 /* Swap the cow forks. */
1987 if (xfs_sb_version_hasreflink(&mp->m_sb)) {
1988 xfs_extnum_t extnum;
1989
1990 ASSERT(ip->i_cformat == XFS_DINODE_FMT_EXTENTS);
1991 ASSERT(tip->i_cformat == XFS_DINODE_FMT_EXTENTS);
1992
1993 extnum = ip->i_cnextents;
1994 ip->i_cnextents = tip->i_cnextents;
1995 tip->i_cnextents = extnum;
1996
Darrick J. Wongf0bc4d12016-10-03 09:11:42 -07001997 cowfp = ip->i_cowfp;
1998 ip->i_cowfp = tip->i_cowfp;
1999 tip->i_cowfp = cowfp;
Darrick J. Wong52bfcdd2017-09-18 09:41:18 -07002000
Christoph Hellwig5bcffe32018-03-13 23:15:30 -07002001 if (ip->i_cowfp && ip->i_cowfp->if_bytes)
Darrick J. Wong52bfcdd2017-09-18 09:41:18 -07002002 xfs_inode_set_cowblocks_tag(ip);
2003 else
2004 xfs_inode_clear_cowblocks_tag(ip);
Christoph Hellwig5bcffe32018-03-13 23:15:30 -07002005 if (tip->i_cowfp && tip->i_cowfp->if_bytes)
Darrick J. Wong52bfcdd2017-09-18 09:41:18 -07002006 xfs_inode_set_cowblocks_tag(tip);
2007 else
2008 xfs_inode_clear_cowblocks_tag(tip);
Darrick J. Wongf0bc4d12016-10-03 09:11:42 -07002009 }
2010
Dave Chinnera133d952013-08-12 20:49:48 +10002011 xfs_trans_log_inode(tp, ip, src_log_flags);
2012 xfs_trans_log_inode(tp, tip, target_log_flags);
2013
2014 /*
Brian Foster6fb10d62017-08-29 10:08:39 -07002015 * The extent forks have been swapped, but crc=1,rmapbt=0 filesystems
2016 * have inode number owner values in the bmbt blocks that still refer to
2017 * the old inode. Scan each bmbt to fix up the owner values with the
2018 * inode number of the current inode.
2019 */
2020 if (src_log_flags & XFS_ILOG_DOWNER) {
Brian Foster2dd3d702017-08-29 10:08:40 -07002021 error = xfs_swap_change_owner(&tp, ip, tip);
Brian Foster6fb10d62017-08-29 10:08:39 -07002022 if (error)
2023 goto out_trans_cancel;
2024 }
2025 if (target_log_flags & XFS_ILOG_DOWNER) {
Brian Foster2dd3d702017-08-29 10:08:40 -07002026 error = xfs_swap_change_owner(&tp, tip, ip);
Brian Foster6fb10d62017-08-29 10:08:39 -07002027 if (error)
2028 goto out_trans_cancel;
2029 }
2030
2031 /*
Dave Chinnera133d952013-08-12 20:49:48 +10002032 * If this is a synchronous mount, make sure that the
2033 * transaction goes to disk before returning to the user.
2034 */
2035 if (mp->m_flags & XFS_MOUNT_WSYNC)
2036 xfs_trans_set_sync(tp);
2037
Christoph Hellwig70393312015-06-04 13:48:08 +10002038 error = xfs_trans_commit(tp);
Dave Chinnera133d952013-08-12 20:49:48 +10002039
2040 trace_xfs_swap_extent_after(ip, 0);
2041 trace_xfs_swap_extent_after(tip, 1);
Darrick J. Wong39aff5f2016-10-03 09:11:53 -07002042
Christoph Hellwig65523212016-11-30 14:33:25 +11002043out_unlock:
Darrick J. Wong39aff5f2016-10-03 09:11:53 -07002044 xfs_iunlock(ip, lock_flags);
2045 xfs_iunlock(tip, lock_flags);
Christoph Hellwig65523212016-11-30 14:33:25 +11002046 unlock_two_nondirectories(VFS_I(ip), VFS_I(tip));
Dave Chinnera133d952013-08-12 20:49:48 +10002047 return error;
2048
Darrick J. Wong39aff5f2016-10-03 09:11:53 -07002049out_trans_cancel:
2050 xfs_trans_cancel(tp);
Christoph Hellwig65523212016-11-30 14:33:25 +11002051 goto out_unlock;
Dave Chinnera133d952013-08-12 20:49:48 +10002052}