blob: 0ea70a44c1a7839541b7b9e333fe3c4fd0372c86 [file] [log] [blame]
Dave Chinner68988112013-08-12 20:49:42 +10001/*
2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
Dave Chinnerc24b5df2013-08-12 20:49:45 +10003 * Copyright (c) 2012 Red Hat, Inc.
Dave Chinner68988112013-08-12 20:49:42 +10004 * All Rights Reserved.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it would be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19#include "xfs.h"
20#include "xfs_fs.h"
Dave Chinner70a98832013-10-23 10:36:05 +110021#include "xfs_shared.h"
Dave Chinner239880e2013-10-23 10:50:10 +110022#include "xfs_format.h"
23#include "xfs_log_format.h"
24#include "xfs_trans_resv.h"
Dave Chinner68988112013-08-12 20:49:42 +100025#include "xfs_bit.h"
Dave Chinner68988112013-08-12 20:49:42 +100026#include "xfs_mount.h"
Dave Chinner57062782013-10-15 09:17:51 +110027#include "xfs_da_format.h"
Darrick J. Wong3ab78df2016-08-03 11:15:38 +100028#include "xfs_defer.h"
Dave Chinner68988112013-08-12 20:49:42 +100029#include "xfs_inode.h"
30#include "xfs_btree.h"
Dave Chinner239880e2013-10-23 10:50:10 +110031#include "xfs_trans.h"
Dave Chinner68988112013-08-12 20:49:42 +100032#include "xfs_extfree_item.h"
33#include "xfs_alloc.h"
34#include "xfs_bmap.h"
35#include "xfs_bmap_util.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110036#include "xfs_bmap_btree.h"
Dave Chinner68988112013-08-12 20:49:42 +100037#include "xfs_rtalloc.h"
38#include "xfs_error.h"
39#include "xfs_quota.h"
40#include "xfs_trans_space.h"
41#include "xfs_trace.h"
Dave Chinnerc24b5df2013-08-12 20:49:45 +100042#include "xfs_icache.h"
Dave Chinner239880e2013-10-23 10:50:10 +110043#include "xfs_log.h"
Darrick J. Wong9c194642016-08-03 12:16:05 +100044#include "xfs_rmap_btree.h"
Darrick J. Wongf86f4032016-10-03 09:11:41 -070045#include "xfs_iomap.h"
46#include "xfs_reflink.h"
47#include "xfs_refcount.h"
Dave Chinner68988112013-08-12 20:49:42 +100048
49/* Kernel only BMAP related definitions and functions */
50
51/*
52 * Convert the given file system block to a disk block. We have to treat it
53 * differently based on whether the file is a real time file or not, because the
54 * bmap code does.
55 */
56xfs_daddr_t
57xfs_fsb_to_db(struct xfs_inode *ip, xfs_fsblock_t fsb)
58{
59 return (XFS_IS_REALTIME_INODE(ip) ? \
60 (xfs_daddr_t)XFS_FSB_TO_BB((ip)->i_mount, (fsb)) : \
61 XFS_FSB_TO_DADDR((ip)->i_mount, (fsb)));
62}
63
64/*
Dave Chinner3fbbbea2015-11-03 12:27:22 +110065 * Routine to zero an extent on disk allocated to the specific inode.
66 *
67 * The VFS functions take a linearised filesystem block offset, so we have to
68 * convert the sparse xfs fsb to the right format first.
69 * VFS types are real funky, too.
70 */
71int
72xfs_zero_extent(
73 struct xfs_inode *ip,
74 xfs_fsblock_t start_fsb,
75 xfs_off_t count_fsb)
76{
77 struct xfs_mount *mp = ip->i_mount;
78 xfs_daddr_t sector = xfs_fsb_to_db(ip, start_fsb);
79 sector_t block = XFS_BB_TO_FSBT(mp, sector);
Dave Chinner3fbbbea2015-11-03 12:27:22 +110080
Matthew Wilcox3dc29162016-03-15 11:20:41 -060081 return blkdev_issue_zeroout(xfs_find_bdev_for_inode(VFS_I(ip)),
82 block << (mp->m_super->s_blocksize_bits - 9),
83 count_fsb << (mp->m_super->s_blocksize_bits - 9),
Christoph Hellwigee472d82017-04-05 19:21:08 +020084 GFP_NOFS, 0);
Dave Chinner3fbbbea2015-11-03 12:27:22 +110085}
86
Dave Chinner68988112013-08-12 20:49:42 +100087int
88xfs_bmap_rtalloc(
89 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
90{
Dave Chinner68988112013-08-12 20:49:42 +100091 int error; /* error return value */
92 xfs_mount_t *mp; /* mount point structure */
93 xfs_extlen_t prod = 0; /* product factor for allocators */
94 xfs_extlen_t ralen = 0; /* realtime allocation length */
95 xfs_extlen_t align; /* minimum allocation alignment */
96 xfs_rtblock_t rtb;
97
98 mp = ap->ip->i_mount;
99 align = xfs_get_extsz_hint(ap->ip);
100 prod = align / mp->m_sb.sb_rextsize;
101 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
102 align, 1, ap->eof, 0,
103 ap->conv, &ap->offset, &ap->length);
104 if (error)
105 return error;
106 ASSERT(ap->length);
107 ASSERT(ap->length % mp->m_sb.sb_rextsize == 0);
108
109 /*
110 * If the offset & length are not perfectly aligned
111 * then kill prod, it will just get us in trouble.
112 */
113 if (do_mod(ap->offset, align) || ap->length % align)
114 prod = 1;
115 /*
116 * Set ralen to be the actual requested length in rtextents.
117 */
118 ralen = ap->length / mp->m_sb.sb_rextsize;
119 /*
120 * If the old value was close enough to MAXEXTLEN that
121 * we rounded up to it, cut it back so it's valid again.
122 * Note that if it's a really large request (bigger than
123 * MAXEXTLEN), we don't hear about that number, and can't
124 * adjust the starting point to match it.
125 */
126 if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN)
127 ralen = MAXEXTLEN / mp->m_sb.sb_rextsize;
128
129 /*
Dave Chinner4b680af2016-02-08 10:46:51 +1100130 * Lock out modifications to both the RT bitmap and summary inodes
Dave Chinner68988112013-08-12 20:49:42 +1000131 */
Darrick J. Wongf4a06602016-08-03 11:00:42 +1000132 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
Dave Chinner68988112013-08-12 20:49:42 +1000133 xfs_trans_ijoin(ap->tp, mp->m_rbmip, XFS_ILOCK_EXCL);
Darrick J. Wongf4a06602016-08-03 11:00:42 +1000134 xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
Dave Chinner4b680af2016-02-08 10:46:51 +1100135 xfs_trans_ijoin(ap->tp, mp->m_rsumip, XFS_ILOCK_EXCL);
Dave Chinner68988112013-08-12 20:49:42 +1000136
137 /*
138 * If it's an allocation to an empty file at offset 0,
139 * pick an extent that will space things out in the rt area.
140 */
141 if (ap->eof && ap->offset == 0) {
142 xfs_rtblock_t uninitialized_var(rtx); /* realtime extent no */
143
144 error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
145 if (error)
146 return error;
147 ap->blkno = rtx * mp->m_sb.sb_rextsize;
148 } else {
149 ap->blkno = 0;
150 }
151
152 xfs_bmap_adjacent(ap);
153
154 /*
155 * Realtime allocation, done through xfs_rtallocate_extent.
156 */
Dave Chinner68988112013-08-12 20:49:42 +1000157 do_div(ap->blkno, mp->m_sb.sb_rextsize);
158 rtb = ap->blkno;
159 ap->length = ralen;
Christoph Hellwig089ec2f2017-02-17 08:21:06 -0800160 error = xfs_rtallocate_extent(ap->tp, ap->blkno, 1, ap->length,
161 &ralen, ap->wasdel, prod, &rtb);
162 if (error)
Dave Chinner68988112013-08-12 20:49:42 +1000163 return error;
Christoph Hellwig089ec2f2017-02-17 08:21:06 -0800164
Dave Chinner68988112013-08-12 20:49:42 +1000165 ap->blkno = rtb;
166 if (ap->blkno != NULLFSBLOCK) {
167 ap->blkno *= mp->m_sb.sb_rextsize;
168 ralen *= mp->m_sb.sb_rextsize;
169 ap->length = ralen;
170 ap->ip->i_d.di_nblocks += ralen;
171 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
172 if (ap->wasdel)
173 ap->ip->i_delayed_blks -= ralen;
174 /*
175 * Adjust the disk quota also. This was reserved
176 * earlier.
177 */
178 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
179 ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
180 XFS_TRANS_DQ_RTBCOUNT, (long) ralen);
Dave Chinner3fbbbea2015-11-03 12:27:22 +1100181
182 /* Zero the extent if we were asked to do so */
Dave Chinner292378e2016-09-26 08:21:28 +1000183 if (ap->datatype & XFS_ALLOC_USERDATA_ZERO) {
Dave Chinner3fbbbea2015-11-03 12:27:22 +1100184 error = xfs_zero_extent(ap->ip, ap->blkno, ap->length);
185 if (error)
186 return error;
187 }
Dave Chinner68988112013-08-12 20:49:42 +1000188 } else {
189 ap->length = 0;
190 }
191 return 0;
192}
193
194/*
Dave Chinner68988112013-08-12 20:49:42 +1000195 * Check if the endoff is outside the last extent. If so the caller will grow
196 * the allocation to a stripe unit boundary. All offsets are considered outside
197 * the end of file for an empty fork, so 1 is returned in *eof in that case.
198 */
199int
200xfs_bmap_eof(
201 struct xfs_inode *ip,
202 xfs_fileoff_t endoff,
203 int whichfork,
204 int *eof)
205{
206 struct xfs_bmbt_irec rec;
207 int error;
208
209 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, eof);
210 if (error || *eof)
211 return error;
212
213 *eof = endoff >= rec.br_startoff + rec.br_blockcount;
214 return 0;
215}
216
217/*
218 * Extent tree block counting routines.
219 */
220
221/*
222 * Count leaf blocks given a range of extent records.
223 */
224STATIC void
225xfs_bmap_count_leaves(
226 xfs_ifork_t *ifp,
227 xfs_extnum_t idx,
228 int numrecs,
229 int *count)
230{
231 int b;
232
233 for (b = 0; b < numrecs; b++) {
234 xfs_bmbt_rec_host_t *frp = xfs_iext_get_ext(ifp, idx + b);
235 *count += xfs_bmbt_get_blockcount(frp);
236 }
237}
238
239/*
240 * Count leaf blocks given a range of extent records originally
241 * in btree format.
242 */
243STATIC void
244xfs_bmap_disk_count_leaves(
245 struct xfs_mount *mp,
246 struct xfs_btree_block *block,
247 int numrecs,
248 int *count)
249{
250 int b;
251 xfs_bmbt_rec_t *frp;
252
253 for (b = 1; b <= numrecs; b++) {
254 frp = XFS_BMBT_REC_ADDR(mp, block, b);
255 *count += xfs_bmbt_disk_get_blockcount(frp);
256 }
257}
258
259/*
260 * Recursively walks each level of a btree
Zhi Yong Wu8be11e92013-08-12 03:14:52 +0000261 * to count total fsblocks in use.
Dave Chinner68988112013-08-12 20:49:42 +1000262 */
263STATIC int /* error */
264xfs_bmap_count_tree(
265 xfs_mount_t *mp, /* file system mount point */
266 xfs_trans_t *tp, /* transaction pointer */
267 xfs_ifork_t *ifp, /* inode fork pointer */
268 xfs_fsblock_t blockno, /* file system block number */
269 int levelin, /* level in btree */
270 int *count) /* Count of blocks */
271{
272 int error;
273 xfs_buf_t *bp, *nbp;
274 int level = levelin;
275 __be64 *pp;
276 xfs_fsblock_t bno = blockno;
277 xfs_fsblock_t nextbno;
278 struct xfs_btree_block *block, *nextblock;
279 int numrecs;
280
281 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF,
282 &xfs_bmbt_buf_ops);
283 if (error)
284 return error;
285 *count += 1;
286 block = XFS_BUF_TO_BLOCK(bp);
287
288 if (--level) {
289 /* Not at node above leaves, count this level of nodes */
290 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
291 while (nextbno != NULLFSBLOCK) {
292 error = xfs_btree_read_bufl(mp, tp, nextbno, 0, &nbp,
293 XFS_BMAP_BTREE_REF,
294 &xfs_bmbt_buf_ops);
295 if (error)
296 return error;
297 *count += 1;
298 nextblock = XFS_BUF_TO_BLOCK(nbp);
299 nextbno = be64_to_cpu(nextblock->bb_u.l.bb_rightsib);
300 xfs_trans_brelse(tp, nbp);
301 }
302
303 /* Dive to the next level */
304 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
305 bno = be64_to_cpu(*pp);
306 if (unlikely((error =
307 xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) {
308 xfs_trans_brelse(tp, bp);
309 XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
310 XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +1000311 return -EFSCORRUPTED;
Dave Chinner68988112013-08-12 20:49:42 +1000312 }
313 xfs_trans_brelse(tp, bp);
314 } else {
315 /* count all level 1 nodes and their leaves */
316 for (;;) {
317 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
318 numrecs = be16_to_cpu(block->bb_numrecs);
319 xfs_bmap_disk_count_leaves(mp, block, numrecs, count);
320 xfs_trans_brelse(tp, bp);
321 if (nextbno == NULLFSBLOCK)
322 break;
323 bno = nextbno;
324 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
325 XFS_BMAP_BTREE_REF,
326 &xfs_bmbt_buf_ops);
327 if (error)
328 return error;
329 *count += 1;
330 block = XFS_BUF_TO_BLOCK(bp);
331 }
332 }
333 return 0;
334}
335
336/*
337 * Count fsblocks of the given fork.
338 */
Eric Sandeen0d5a75e2016-06-01 17:38:15 +1000339static int /* error */
Dave Chinner68988112013-08-12 20:49:42 +1000340xfs_bmap_count_blocks(
341 xfs_trans_t *tp, /* transaction pointer */
342 xfs_inode_t *ip, /* incore inode */
343 int whichfork, /* data or attr fork */
344 int *count) /* out: count of blocks */
345{
346 struct xfs_btree_block *block; /* current btree block */
347 xfs_fsblock_t bno; /* block # of "block" */
348 xfs_ifork_t *ifp; /* fork structure */
349 int level; /* btree level, for checking */
350 xfs_mount_t *mp; /* file system mount structure */
351 __be64 *pp; /* pointer to block address */
352
353 bno = NULLFSBLOCK;
354 mp = ip->i_mount;
355 ifp = XFS_IFORK_PTR(ip, whichfork);
356 if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) {
Eric Sandeen5d829302016-11-08 12:59:42 +1100357 xfs_bmap_count_leaves(ifp, 0, xfs_iext_count(ifp), count);
Dave Chinner68988112013-08-12 20:49:42 +1000358 return 0;
359 }
360
361 /*
362 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
363 */
364 block = ifp->if_broot;
365 level = be16_to_cpu(block->bb_level);
366 ASSERT(level > 0);
367 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
368 bno = be64_to_cpu(*pp);
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000369 ASSERT(bno != NULLFSBLOCK);
Dave Chinner68988112013-08-12 20:49:42 +1000370 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
371 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
372
373 if (unlikely(xfs_bmap_count_tree(mp, tp, ifp, bno, level, count) < 0)) {
374 XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW,
375 mp);
Dave Chinner24513372014-06-25 14:58:08 +1000376 return -EFSCORRUPTED;
Dave Chinner68988112013-08-12 20:49:42 +1000377 }
378
379 return 0;
380}
381
382/*
383 * returns 1 for success, 0 if we failed to map the extent.
384 */
385STATIC int
386xfs_getbmapx_fix_eof_hole(
387 xfs_inode_t *ip, /* xfs incore inode pointer */
Darrick J. Wongf86f4032016-10-03 09:11:41 -0700388 int whichfork,
Dave Chinner68988112013-08-12 20:49:42 +1000389 struct getbmapx *out, /* output structure */
390 int prealloced, /* this is a file with
391 * preallocated data space */
Darrick J. Wongc8ce5402017-06-16 11:00:05 -0700392 int64_t end, /* last block requested */
Darrick J. Wongf86f4032016-10-03 09:11:41 -0700393 xfs_fsblock_t startblock,
394 bool moretocome)
Dave Chinner68988112013-08-12 20:49:42 +1000395{
Darrick J. Wongc8ce5402017-06-16 11:00:05 -0700396 int64_t fixlen;
Dave Chinner68988112013-08-12 20:49:42 +1000397 xfs_mount_t *mp; /* file system mount point */
398 xfs_ifork_t *ifp; /* inode fork pointer */
399 xfs_extnum_t lastx; /* last extent pointer */
400 xfs_fileoff_t fileblock;
401
402 if (startblock == HOLESTARTBLOCK) {
403 mp = ip->i_mount;
404 out->bmv_block = -1;
405 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, XFS_ISIZE(ip)));
406 fixlen -= out->bmv_offset;
407 if (prealloced && out->bmv_offset + out->bmv_length == end) {
408 /* Came to hole at EOF. Trim it. */
409 if (fixlen <= 0)
410 return 0;
411 out->bmv_length = fixlen;
412 }
413 } else {
414 if (startblock == DELAYSTARTBLOCK)
415 out->bmv_block = -2;
416 else
417 out->bmv_block = xfs_fsb_to_db(ip, startblock);
418 fileblock = XFS_BB_TO_FSB(ip->i_mount, out->bmv_offset);
Darrick J. Wongf86f4032016-10-03 09:11:41 -0700419 ifp = XFS_IFORK_PTR(ip, whichfork);
420 if (!moretocome &&
421 xfs_iext_bno_to_ext(ifp, fileblock, &lastx) &&
Eric Sandeen5d829302016-11-08 12:59:42 +1100422 (lastx == xfs_iext_count(ifp) - 1))
Dave Chinner68988112013-08-12 20:49:42 +1000423 out->bmv_oflags |= BMV_OF_LAST;
424 }
425
426 return 1;
427}
428
Darrick J. Wongf86f4032016-10-03 09:11:41 -0700429/* Adjust the reported bmap around shared/unshared extent transitions. */
430STATIC int
431xfs_getbmap_adjust_shared(
432 struct xfs_inode *ip,
433 int whichfork,
434 struct xfs_bmbt_irec *map,
435 struct getbmapx *out,
436 struct xfs_bmbt_irec *next_map)
437{
438 struct xfs_mount *mp = ip->i_mount;
439 xfs_agnumber_t agno;
440 xfs_agblock_t agbno;
441 xfs_agblock_t ebno;
442 xfs_extlen_t elen;
443 xfs_extlen_t nlen;
444 int error;
445
446 next_map->br_startblock = NULLFSBLOCK;
447 next_map->br_startoff = NULLFILEOFF;
448 next_map->br_blockcount = 0;
449
450 /* Only written data blocks can be shared. */
Christoph Hellwig9c4f29d2017-03-28 14:53:35 -0700451 if (!xfs_is_reflink_inode(ip) ||
452 whichfork != XFS_DATA_FORK ||
453 !xfs_bmap_is_real_extent(map))
Darrick J. Wongf86f4032016-10-03 09:11:41 -0700454 return 0;
455
456 agno = XFS_FSB_TO_AGNO(mp, map->br_startblock);
457 agbno = XFS_FSB_TO_AGBNO(mp, map->br_startblock);
458 error = xfs_reflink_find_shared(mp, agno, agbno, map->br_blockcount,
459 &ebno, &elen, true);
460 if (error)
461 return error;
462
463 if (ebno == NULLAGBLOCK) {
464 /* No shared blocks at all. */
465 return 0;
466 } else if (agbno == ebno) {
467 /*
468 * Shared extent at (agbno, elen). Shrink the reported
469 * extent length and prepare to move the start of map[i]
470 * to agbno+elen, with the aim of (re)formatting the new
471 * map[i] the next time through the inner loop.
472 */
473 out->bmv_length = XFS_FSB_TO_BB(mp, elen);
474 out->bmv_oflags |= BMV_OF_SHARED;
475 if (elen != map->br_blockcount) {
476 *next_map = *map;
477 next_map->br_startblock += elen;
478 next_map->br_startoff += elen;
479 next_map->br_blockcount -= elen;
480 }
481 map->br_blockcount -= elen;
482 } else {
483 /*
484 * There's an unshared extent (agbno, ebno - agbno)
485 * followed by shared extent at (ebno, elen). Shrink
486 * the reported extent length to cover only the unshared
487 * extent and prepare to move up the start of map[i] to
488 * ebno, with the aim of (re)formatting the new map[i]
489 * the next time through the inner loop.
490 */
491 *next_map = *map;
492 nlen = ebno - agbno;
493 out->bmv_length = XFS_FSB_TO_BB(mp, nlen);
494 next_map->br_startblock += nlen;
495 next_map->br_startoff += nlen;
496 next_map->br_blockcount -= nlen;
497 map->br_blockcount -= nlen;
498 }
499
500 return 0;
501}
502
Dave Chinner68988112013-08-12 20:49:42 +1000503/*
504 * Get inode's extents as described in bmv, and format for output.
505 * Calls formatter to fill the user's buffer until all extents
506 * are mapped, until the passed-in bmv->bmv_count slots have
507 * been filled, or until the formatter short-circuits the loop,
508 * if it is tracking filled-in extents on its own.
509 */
510int /* error code */
511xfs_getbmap(
512 xfs_inode_t *ip,
513 struct getbmapx *bmv, /* user bmap structure */
514 xfs_bmap_format_t formatter, /* format to user */
515 void *arg) /* formatter arg */
516{
Darrick J. Wongc8ce5402017-06-16 11:00:05 -0700517 int64_t bmvend; /* last block requested */
Dave Chinner68988112013-08-12 20:49:42 +1000518 int error = 0; /* return value */
Darrick J. Wongc8ce5402017-06-16 11:00:05 -0700519 int64_t fixlen; /* length for -1 case */
Dave Chinner68988112013-08-12 20:49:42 +1000520 int i; /* extent number */
521 int lock; /* lock state */
522 xfs_bmbt_irec_t *map; /* buffer for user's data */
523 xfs_mount_t *mp; /* file system mount point */
524 int nex; /* # of user extents can do */
Dave Chinner68988112013-08-12 20:49:42 +1000525 int subnex; /* # of bmapi's can do */
526 int nmap; /* number of map entries */
527 struct getbmapx *out; /* output structure */
528 int whichfork; /* data or attr fork */
529 int prealloced; /* this is a file with
530 * preallocated data space */
531 int iflags; /* interface flags */
532 int bmapi_flags; /* flags for xfs_bmapi */
533 int cur_ext = 0;
Darrick J. Wongf86f4032016-10-03 09:11:41 -0700534 struct xfs_bmbt_irec inject_map;
Dave Chinner68988112013-08-12 20:49:42 +1000535
536 mp = ip->i_mount;
537 iflags = bmv->bmv_iflags;
Dave Chinner68988112013-08-12 20:49:42 +1000538
Darrick J. Wongf86f4032016-10-03 09:11:41 -0700539#ifndef DEBUG
540 /* Only allow CoW fork queries if we're debugging. */
541 if (iflags & BMV_IF_COWFORK)
542 return -EINVAL;
543#endif
544 if ((iflags & BMV_IF_ATTRFORK) && (iflags & BMV_IF_COWFORK))
545 return -EINVAL;
546
547 if (iflags & BMV_IF_ATTRFORK)
548 whichfork = XFS_ATTR_FORK;
549 else if (iflags & BMV_IF_COWFORK)
550 whichfork = XFS_COW_FORK;
551 else
552 whichfork = XFS_DATA_FORK;
553
554 switch (whichfork) {
555 case XFS_ATTR_FORK:
Dave Chinner68988112013-08-12 20:49:42 +1000556 if (XFS_IFORK_Q(ip)) {
557 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS &&
558 ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE &&
559 ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
Dave Chinner24513372014-06-25 14:58:08 +1000560 return -EINVAL;
Dave Chinner68988112013-08-12 20:49:42 +1000561 } else if (unlikely(
562 ip->i_d.di_aformat != 0 &&
563 ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) {
564 XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW,
565 ip->i_mount);
Dave Chinner24513372014-06-25 14:58:08 +1000566 return -EFSCORRUPTED;
Dave Chinner68988112013-08-12 20:49:42 +1000567 }
568
569 prealloced = 0;
570 fixlen = 1LL << 32;
Darrick J. Wongf86f4032016-10-03 09:11:41 -0700571 break;
572 case XFS_COW_FORK:
573 if (ip->i_cformat != XFS_DINODE_FMT_EXTENTS)
574 return -EINVAL;
575
Darrick J. Wongf7ca3522016-10-03 09:11:43 -0700576 if (xfs_get_cowextsz_hint(ip)) {
577 prealloced = 1;
578 fixlen = mp->m_super->s_maxbytes;
579 } else {
580 prealloced = 0;
581 fixlen = XFS_ISIZE(ip);
582 }
Darrick J. Wongf86f4032016-10-03 09:11:41 -0700583 break;
584 default:
Darrick J. Wong6eadbf42017-05-12 10:44:08 -0700585 /* Local format data forks report no extents. */
586 if (ip->i_d.di_format == XFS_DINODE_FMT_LOCAL) {
587 bmv->bmv_entries = 0;
588 return 0;
589 }
Dave Chinner68988112013-08-12 20:49:42 +1000590 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
Darrick J. Wong6eadbf42017-05-12 10:44:08 -0700591 ip->i_d.di_format != XFS_DINODE_FMT_BTREE)
Dave Chinner24513372014-06-25 14:58:08 +1000592 return -EINVAL;
Dave Chinner68988112013-08-12 20:49:42 +1000593
594 if (xfs_get_extsz_hint(ip) ||
595 ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){
596 prealloced = 1;
597 fixlen = mp->m_super->s_maxbytes;
598 } else {
599 prealloced = 0;
600 fixlen = XFS_ISIZE(ip);
601 }
Darrick J. Wongf86f4032016-10-03 09:11:41 -0700602 break;
Dave Chinner68988112013-08-12 20:49:42 +1000603 }
604
605 if (bmv->bmv_length == -1) {
606 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen));
607 bmv->bmv_length =
Darrick J. Wongc8ce5402017-06-16 11:00:05 -0700608 max_t(int64_t, fixlen - bmv->bmv_offset, 0);
Dave Chinner68988112013-08-12 20:49:42 +1000609 } else if (bmv->bmv_length == 0) {
610 bmv->bmv_entries = 0;
611 return 0;
612 } else if (bmv->bmv_length < 0) {
Dave Chinner24513372014-06-25 14:58:08 +1000613 return -EINVAL;
Dave Chinner68988112013-08-12 20:49:42 +1000614 }
615
616 nex = bmv->bmv_count - 1;
617 if (nex <= 0)
Dave Chinner24513372014-06-25 14:58:08 +1000618 return -EINVAL;
Dave Chinner68988112013-08-12 20:49:42 +1000619 bmvend = bmv->bmv_offset + bmv->bmv_length;
620
621
622 if (bmv->bmv_count > ULONG_MAX / sizeof(struct getbmapx))
Dave Chinner24513372014-06-25 14:58:08 +1000623 return -ENOMEM;
Dave Chinnerfdd3cce2013-09-02 20:53:00 +1000624 out = kmem_zalloc_large(bmv->bmv_count * sizeof(struct getbmapx), 0);
625 if (!out)
Dave Chinner24513372014-06-25 14:58:08 +1000626 return -ENOMEM;
Dave Chinner68988112013-08-12 20:49:42 +1000627
628 xfs_ilock(ip, XFS_IOLOCK_SHARED);
Darrick J. Wongf86f4032016-10-03 09:11:41 -0700629 switch (whichfork) {
630 case XFS_DATA_FORK:
Christoph Hellwigefa70be2013-12-18 02:14:39 -0800631 if (!(iflags & BMV_IF_DELALLOC) &&
632 (ip->i_delayed_blks || XFS_ISIZE(ip) > ip->i_d.di_size)) {
Dave Chinner24513372014-06-25 14:58:08 +1000633 error = filemap_write_and_wait(VFS_I(ip)->i_mapping);
Dave Chinner68988112013-08-12 20:49:42 +1000634 if (error)
635 goto out_unlock_iolock;
Dave Chinner68988112013-08-12 20:49:42 +1000636
Christoph Hellwigefa70be2013-12-18 02:14:39 -0800637 /*
638 * Even after flushing the inode, there can still be
639 * delalloc blocks on the inode beyond EOF due to
640 * speculative preallocation. These are not removed
641 * until the release function is called or the inode
642 * is inactivated. Hence we cannot assert here that
643 * ip->i_delayed_blks == 0.
644 */
645 }
646
647 lock = xfs_ilock_data_map_shared(ip);
Darrick J. Wongf86f4032016-10-03 09:11:41 -0700648 break;
649 case XFS_COW_FORK:
650 lock = XFS_ILOCK_SHARED;
651 xfs_ilock(ip, lock);
652 break;
653 case XFS_ATTR_FORK:
Christoph Hellwigefa70be2013-12-18 02:14:39 -0800654 lock = xfs_ilock_attr_map_shared(ip);
Darrick J. Wongf86f4032016-10-03 09:11:41 -0700655 break;
Christoph Hellwigefa70be2013-12-18 02:14:39 -0800656 }
Dave Chinner68988112013-08-12 20:49:42 +1000657
658 /*
659 * Don't let nex be bigger than the number of extents
660 * we can have assuming alternating holes and real extents.
661 */
662 if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1)
663 nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1;
664
665 bmapi_flags = xfs_bmapi_aflag(whichfork);
666 if (!(iflags & BMV_IF_PREALLOC))
667 bmapi_flags |= XFS_BMAPI_IGSTATE;
668
669 /*
670 * Allocate enough space to handle "subnex" maps at a time.
671 */
Dave Chinner24513372014-06-25 14:58:08 +1000672 error = -ENOMEM;
Dave Chinner68988112013-08-12 20:49:42 +1000673 subnex = 16;
674 map = kmem_alloc(subnex * sizeof(*map), KM_MAYFAIL | KM_NOFS);
675 if (!map)
676 goto out_unlock_ilock;
677
678 bmv->bmv_entries = 0;
679
680 if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0 &&
681 (whichfork == XFS_ATTR_FORK || !(iflags & BMV_IF_DELALLOC))) {
682 error = 0;
683 goto out_free_map;
684 }
685
Dave Chinner68988112013-08-12 20:49:42 +1000686 do {
Darrick J. Wongc364b6d2017-01-26 09:50:30 -0800687 nmap = (nex> subnex) ? subnex : nex;
Dave Chinner68988112013-08-12 20:49:42 +1000688 error = xfs_bmapi_read(ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset),
689 XFS_BB_TO_FSB(mp, bmv->bmv_length),
690 map, &nmap, bmapi_flags);
691 if (error)
692 goto out_free_map;
693 ASSERT(nmap <= subnex);
694
Darrick J. Wongc364b6d2017-01-26 09:50:30 -0800695 for (i = 0; i < nmap && bmv->bmv_length &&
696 cur_ext < bmv->bmv_count - 1; i++) {
Dave Chinner68988112013-08-12 20:49:42 +1000697 out[cur_ext].bmv_oflags = 0;
698 if (map[i].br_state == XFS_EXT_UNWRITTEN)
699 out[cur_ext].bmv_oflags |= BMV_OF_PREALLOC;
700 else if (map[i].br_startblock == DELAYSTARTBLOCK)
701 out[cur_ext].bmv_oflags |= BMV_OF_DELALLOC;
702 out[cur_ext].bmv_offset =
703 XFS_FSB_TO_BB(mp, map[i].br_startoff);
704 out[cur_ext].bmv_length =
705 XFS_FSB_TO_BB(mp, map[i].br_blockcount);
706 out[cur_ext].bmv_unused1 = 0;
707 out[cur_ext].bmv_unused2 = 0;
708
709 /*
710 * delayed allocation extents that start beyond EOF can
711 * occur due to speculative EOF allocation when the
712 * delalloc extent is larger than the largest freespace
713 * extent at conversion time. These extents cannot be
714 * converted by data writeback, so can exist here even
715 * if we are not supposed to be finding delalloc
716 * extents.
717 */
718 if (map[i].br_startblock == DELAYSTARTBLOCK &&
Zorro Lang892d2a52017-05-15 08:40:02 -0700719 map[i].br_startoff < XFS_B_TO_FSB(mp, XFS_ISIZE(ip)))
Dave Chinner68988112013-08-12 20:49:42 +1000720 ASSERT((iflags & BMV_IF_DELALLOC) != 0);
721
722 if (map[i].br_startblock == HOLESTARTBLOCK &&
723 whichfork == XFS_ATTR_FORK) {
724 /* came to the end of attribute fork */
725 out[cur_ext].bmv_oflags |= BMV_OF_LAST;
726 goto out_free_map;
727 }
728
Darrick J. Wongf86f4032016-10-03 09:11:41 -0700729 /* Is this a shared block? */
730 error = xfs_getbmap_adjust_shared(ip, whichfork,
731 &map[i], &out[cur_ext], &inject_map);
732 if (error)
733 goto out_free_map;
734
735 if (!xfs_getbmapx_fix_eof_hole(ip, whichfork,
736 &out[cur_ext], prealloced, bmvend,
737 map[i].br_startblock,
738 inject_map.br_startblock != NULLFSBLOCK))
Dave Chinner68988112013-08-12 20:49:42 +1000739 goto out_free_map;
740
741 bmv->bmv_offset =
742 out[cur_ext].bmv_offset +
743 out[cur_ext].bmv_length;
744 bmv->bmv_length =
Darrick J. Wongc8ce5402017-06-16 11:00:05 -0700745 max_t(int64_t, 0, bmvend - bmv->bmv_offset);
Dave Chinner68988112013-08-12 20:49:42 +1000746
747 /*
748 * In case we don't want to return the hole,
749 * don't increase cur_ext so that we can reuse
750 * it in the next loop.
751 */
752 if ((iflags & BMV_IF_NO_HOLES) &&
753 map[i].br_startblock == HOLESTARTBLOCK) {
754 memset(&out[cur_ext], 0, sizeof(out[cur_ext]));
755 continue;
756 }
757
Darrick J. Wongc364b6d2017-01-26 09:50:30 -0800758 /*
759 * In order to report shared extents accurately,
760 * we report each distinct shared/unshared part
761 * of a single bmbt record using multiple bmap
762 * extents. To make that happen, we iterate the
763 * same map array item multiple times, each
764 * time trimming out the subextent that we just
765 * reported.
766 *
767 * Because of this, we must check the out array
768 * index (cur_ext) directly against bmv_count-1
769 * to avoid overflows.
770 */
Darrick J. Wongf86f4032016-10-03 09:11:41 -0700771 if (inject_map.br_startblock != NULLFSBLOCK) {
772 map[i] = inject_map;
773 i--;
Darrick J. Wongc364b6d2017-01-26 09:50:30 -0800774 }
Dave Chinner68988112013-08-12 20:49:42 +1000775 bmv->bmv_entries++;
776 cur_ext++;
777 }
Darrick J. Wongc364b6d2017-01-26 09:50:30 -0800778 } while (nmap && bmv->bmv_length && cur_ext < bmv->bmv_count - 1);
Dave Chinner68988112013-08-12 20:49:42 +1000779
780 out_free_map:
781 kmem_free(map);
782 out_unlock_ilock:
Christoph Hellwig01f4f322013-12-06 12:30:08 -0800783 xfs_iunlock(ip, lock);
Dave Chinner68988112013-08-12 20:49:42 +1000784 out_unlock_iolock:
785 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
786
787 for (i = 0; i < cur_ext; i++) {
Dave Chinner68988112013-08-12 20:49:42 +1000788 /* format results & advance arg */
Eric Sandeen1dbba082017-01-27 23:24:28 -0800789 error = formatter(&arg, &out[i]);
790 if (error)
Dave Chinner68988112013-08-12 20:49:42 +1000791 break;
792 }
793
Dave Chinnerfdd3cce2013-09-02 20:53:00 +1000794 kmem_free(out);
Dave Chinner68988112013-08-12 20:49:42 +1000795 return error;
796}
797
798/*
799 * dead simple method of punching delalyed allocation blocks from a range in
800 * 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 +0000801 * rare error cases so the overhead is not critical. This will always punch out
Dave Chinner68988112013-08-12 20:49:42 +1000802 * both the start and end blocks, even if the ranges only partially overlap
803 * them, so it is up to the caller to ensure that partial blocks are not
804 * passed in.
805 */
806int
807xfs_bmap_punch_delalloc_range(
808 struct xfs_inode *ip,
809 xfs_fileoff_t start_fsb,
810 xfs_fileoff_t length)
811{
812 xfs_fileoff_t remaining = length;
813 int error = 0;
814
815 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
816
817 do {
818 int done;
819 xfs_bmbt_irec_t imap;
820 int nimaps = 1;
821 xfs_fsblock_t firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000822 struct xfs_defer_ops dfops;
Dave Chinner68988112013-08-12 20:49:42 +1000823
824 /*
825 * Map the range first and check that it is a delalloc extent
826 * before trying to unmap the range. Otherwise we will be
827 * trying to remove a real extent (which requires a
828 * transaction) or a hole, which is probably a bad idea...
829 */
830 error = xfs_bmapi_read(ip, start_fsb, 1, &imap, &nimaps,
831 XFS_BMAPI_ENTIRE);
832
833 if (error) {
834 /* something screwed, just bail */
835 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
836 xfs_alert(ip->i_mount,
837 "Failed delalloc mapping lookup ino %lld fsb %lld.",
838 ip->i_ino, start_fsb);
839 }
840 break;
841 }
842 if (!nimaps) {
843 /* nothing there */
844 goto next_block;
845 }
846 if (imap.br_startblock != DELAYSTARTBLOCK) {
847 /* been converted, ignore */
848 goto next_block;
849 }
850 WARN_ON(imap.br_blockcount == 0);
851
852 /*
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000853 * Note: while we initialise the firstblock/dfops pair, they
Dave Chinner68988112013-08-12 20:49:42 +1000854 * should never be used because blocks should never be
855 * allocated or freed for a delalloc extent and hence we need
856 * don't cancel or finish them after the xfs_bunmapi() call.
857 */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000858 xfs_defer_init(&dfops, &firstblock);
Dave Chinner68988112013-08-12 20:49:42 +1000859 error = xfs_bunmapi(NULL, ip, start_fsb, 1, 0, 1, &firstblock,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000860 &dfops, &done);
Dave Chinner68988112013-08-12 20:49:42 +1000861 if (error)
862 break;
863
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000864 ASSERT(!xfs_defer_has_unfinished_work(&dfops));
Dave Chinner68988112013-08-12 20:49:42 +1000865next_block:
866 start_fsb++;
867 remaining--;
868 } while(remaining > 0);
869
870 return error;
871}
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000872
873/*
874 * Test whether it is appropriate to check an inode for and free post EOF
875 * blocks. The 'force' parameter determines whether we should also consider
876 * regular files that are marked preallocated or append-only.
877 */
878bool
879xfs_can_free_eofblocks(struct xfs_inode *ip, bool force)
880{
881 /* prealloc/delalloc exists only on regular files */
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100882 if (!S_ISREG(VFS_I(ip)->i_mode))
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000883 return false;
884
885 /*
886 * Zero sized files with no cached pages and delalloc blocks will not
887 * have speculative prealloc/delalloc blocks to remove.
888 */
889 if (VFS_I(ip)->i_size == 0 &&
Dave Chinner2667c6f2014-08-04 13:23:15 +1000890 VFS_I(ip)->i_mapping->nrpages == 0 &&
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000891 ip->i_delayed_blks == 0)
892 return false;
893
894 /* If we haven't read in the extent list, then don't do it now. */
895 if (!(ip->i_df.if_flags & XFS_IFEXTENTS))
896 return false;
897
898 /*
899 * Do not free real preallocated or append-only files unless the file
900 * has delalloc blocks and we are forced to remove them.
901 */
902 if (ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND))
903 if (!force || ip->i_delayed_blks == 0)
904 return false;
905
906 return true;
907}
908
909/*
Brian Foster3b4683c2017-04-11 10:50:05 -0700910 * This is called to free any blocks beyond eof. The caller must hold
911 * IOLOCK_EXCL unless we are in the inode reclaim path and have the only
912 * reference to the inode.
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000913 */
914int
915xfs_free_eofblocks(
Brian Fostera36b9262017-01-27 23:22:55 -0800916 struct xfs_inode *ip)
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000917{
Brian Fostera36b9262017-01-27 23:22:55 -0800918 struct xfs_trans *tp;
919 int error;
920 xfs_fileoff_t end_fsb;
921 xfs_fileoff_t last_fsb;
922 xfs_filblks_t map_len;
923 int nimaps;
924 struct xfs_bmbt_irec imap;
925 struct xfs_mount *mp = ip->i_mount;
926
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000927 /*
928 * Figure out if there are any blocks beyond the end
929 * of the file. If not, then there is nothing to do.
930 */
931 end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_ISIZE(ip));
932 last_fsb = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
933 if (last_fsb <= end_fsb)
934 return 0;
935 map_len = last_fsb - end_fsb;
936
937 nimaps = 1;
938 xfs_ilock(ip, XFS_ILOCK_SHARED);
939 error = xfs_bmapi_read(ip, end_fsb, map_len, &imap, &nimaps, 0);
940 xfs_iunlock(ip, XFS_ILOCK_SHARED);
941
Brian Fostera36b9262017-01-27 23:22:55 -0800942 /*
943 * If there are blocks after the end of file, truncate the file to its
944 * current size to free them up.
945 */
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000946 if (!error && (nimaps != 0) &&
947 (imap.br_startblock != HOLESTARTBLOCK ||
948 ip->i_delayed_blks)) {
949 /*
950 * Attach the dquots to the inode up front.
951 */
952 error = xfs_qm_dqattach(ip, 0);
953 if (error)
954 return error;
955
Brian Fostere4229d6b2017-01-27 23:22:57 -0800956 /* wait on dio to ensure i_size has settled */
957 inode_dio_wait(VFS_I(ip));
958
Christoph Hellwig253f4912016-04-06 09:19:55 +1000959 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0,
960 &tp);
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000961 if (error) {
962 ASSERT(XFS_FORCED_SHUTDOWN(mp));
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000963 return error;
964 }
965
966 xfs_ilock(ip, XFS_ILOCK_EXCL);
967 xfs_trans_ijoin(tp, ip, 0);
968
969 /*
970 * Do not update the on-disk file size. If we update the
971 * on-disk file size and then the system crashes before the
972 * contents of the file are flushed to disk then the files
973 * may be full of holes (ie NULL files bug).
974 */
975 error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK,
976 XFS_ISIZE(ip));
977 if (error) {
978 /*
979 * If we get an error at this point we simply don't
980 * bother truncating the file.
981 */
Christoph Hellwig4906e212015-06-04 13:47:56 +1000982 xfs_trans_cancel(tp);
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000983 } else {
Christoph Hellwig70393312015-06-04 13:48:08 +1000984 error = xfs_trans_commit(tp);
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000985 if (!error)
986 xfs_inode_clear_eofblocks_tag(ip);
987 }
988
989 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000990 }
991 return error;
992}
993
Christoph Hellwig83aee9e2013-10-12 00:55:07 -0700994int
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000995xfs_alloc_file_space(
Christoph Hellwig83aee9e2013-10-12 00:55:07 -0700996 struct xfs_inode *ip,
Dave Chinnerc24b5df2013-08-12 20:49:45 +1000997 xfs_off_t offset,
998 xfs_off_t len,
Christoph Hellwig5f8aca82013-10-12 00:55:06 -0700999 int alloc_type)
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001000{
1001 xfs_mount_t *mp = ip->i_mount;
1002 xfs_off_t count;
1003 xfs_filblks_t allocated_fsb;
1004 xfs_filblks_t allocatesize_fsb;
1005 xfs_extlen_t extsz, temp;
1006 xfs_fileoff_t startoffset_fsb;
1007 xfs_fsblock_t firstfsb;
1008 int nimaps;
1009 int quota_flag;
1010 int rt;
1011 xfs_trans_t *tp;
1012 xfs_bmbt_irec_t imaps[1], *imapp;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001013 struct xfs_defer_ops dfops;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001014 uint qblocks, resblks, resrtextents;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001015 int error;
1016
1017 trace_xfs_alloc_file_space(ip);
1018
1019 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10001020 return -EIO;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001021
1022 error = xfs_qm_dqattach(ip, 0);
1023 if (error)
1024 return error;
1025
1026 if (len <= 0)
Dave Chinner24513372014-06-25 14:58:08 +10001027 return -EINVAL;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001028
1029 rt = XFS_IS_REALTIME_INODE(ip);
1030 extsz = xfs_get_extsz_hint(ip);
1031
1032 count = len;
1033 imapp = &imaps[0];
1034 nimaps = 1;
1035 startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
1036 allocatesize_fsb = XFS_B_TO_FSB(mp, count);
1037
1038 /*
1039 * Allocate file space until done or until there is an error
1040 */
1041 while (allocatesize_fsb && !error) {
1042 xfs_fileoff_t s, e;
1043
1044 /*
1045 * Determine space reservations for data/realtime.
1046 */
1047 if (unlikely(extsz)) {
1048 s = startoffset_fsb;
1049 do_div(s, extsz);
1050 s *= extsz;
1051 e = startoffset_fsb + allocatesize_fsb;
1052 if ((temp = do_mod(startoffset_fsb, extsz)))
1053 e += temp;
1054 if ((temp = do_mod(e, extsz)))
1055 e += extsz - temp;
1056 } else {
1057 s = 0;
1058 e = allocatesize_fsb;
1059 }
1060
1061 /*
1062 * The transaction reservation is limited to a 32-bit block
1063 * count, hence we need to limit the number of blocks we are
1064 * trying to reserve to avoid an overflow. We can't allocate
1065 * more than @nimaps extents, and an extent is limited on disk
1066 * to MAXEXTLEN (21 bits), so use that to enforce the limit.
1067 */
1068 resblks = min_t(xfs_fileoff_t, (e - s), (MAXEXTLEN * nimaps));
1069 if (unlikely(rt)) {
1070 resrtextents = qblocks = resblks;
1071 resrtextents /= mp->m_sb.sb_rextsize;
1072 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
1073 quota_flag = XFS_QMOPT_RES_RTBLKS;
1074 } else {
1075 resrtextents = 0;
1076 resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resblks);
1077 quota_flag = XFS_QMOPT_RES_REGBLKS;
1078 }
1079
1080 /*
1081 * Allocate and setup the transaction.
1082 */
Christoph Hellwig253f4912016-04-06 09:19:55 +10001083 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks,
1084 resrtextents, 0, &tp);
1085
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001086 /*
1087 * Check for running out of space
1088 */
1089 if (error) {
1090 /*
1091 * Free the transaction structure.
1092 */
Dave Chinner24513372014-06-25 14:58:08 +10001093 ASSERT(error == -ENOSPC || XFS_FORCED_SHUTDOWN(mp));
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001094 break;
1095 }
1096 xfs_ilock(ip, XFS_ILOCK_EXCL);
1097 error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks,
1098 0, quota_flag);
1099 if (error)
1100 goto error1;
1101
1102 xfs_trans_ijoin(tp, ip, 0);
1103
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001104 xfs_defer_init(&dfops, &firstfsb);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001105 error = xfs_bmapi_write(tp, ip, startoffset_fsb,
1106 allocatesize_fsb, alloc_type, &firstfsb,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001107 resblks, imapp, &nimaps, &dfops);
Eric Sandeenf6106ef2016-01-11 11:34:01 +11001108 if (error)
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001109 goto error0;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001110
1111 /*
1112 * Complete the transaction
1113 */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001114 error = xfs_defer_finish(&tp, &dfops, NULL);
Eric Sandeenf6106ef2016-01-11 11:34:01 +11001115 if (error)
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001116 goto error0;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001117
Christoph Hellwig70393312015-06-04 13:48:08 +10001118 error = xfs_trans_commit(tp);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001119 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Eric Sandeenf6106ef2016-01-11 11:34:01 +11001120 if (error)
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001121 break;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001122
1123 allocated_fsb = imapp->br_blockcount;
1124
1125 if (nimaps == 0) {
Dave Chinner24513372014-06-25 14:58:08 +10001126 error = -ENOSPC;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001127 break;
1128 }
1129
1130 startoffset_fsb += allocated_fsb;
1131 allocatesize_fsb -= allocated_fsb;
1132 }
1133
1134 return error;
1135
1136error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001137 xfs_defer_cancel(&dfops);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001138 xfs_trans_unreserve_quota_nblks(tp, ip, (long)qblocks, 0, quota_flag);
1139
1140error1: /* Just cancel transaction */
Christoph Hellwig4906e212015-06-04 13:47:56 +10001141 xfs_trans_cancel(tp);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001142 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1143 return error;
1144}
1145
Christoph Hellwigbdb0d042016-06-21 10:00:55 +10001146static int
1147xfs_unmap_extent(
1148 struct xfs_inode *ip,
1149 xfs_fileoff_t startoffset_fsb,
1150 xfs_filblks_t len_fsb,
1151 int *done)
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001152{
Christoph Hellwigbdb0d042016-06-21 10:00:55 +10001153 struct xfs_mount *mp = ip->i_mount;
1154 struct xfs_trans *tp;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001155 struct xfs_defer_ops dfops;
Christoph Hellwigbdb0d042016-06-21 10:00:55 +10001156 xfs_fsblock_t firstfsb;
1157 uint resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
1158 int error;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001159
Christoph Hellwigbdb0d042016-06-21 10:00:55 +10001160 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0, &tp);
1161 if (error) {
1162 ASSERT(error == -ENOSPC || XFS_FORCED_SHUTDOWN(mp));
1163 return error;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001164 }
Christoph Hellwigbdb0d042016-06-21 10:00:55 +10001165
1166 xfs_ilock(ip, XFS_ILOCK_EXCL);
1167 error = xfs_trans_reserve_quota(tp, mp, ip->i_udquot, ip->i_gdquot,
1168 ip->i_pdquot, resblks, 0, XFS_QMOPT_RES_REGBLKS);
1169 if (error)
1170 goto out_trans_cancel;
1171
1172 xfs_trans_ijoin(tp, ip, 0);
1173
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001174 xfs_defer_init(&dfops, &firstfsb);
Christoph Hellwigbdb0d042016-06-21 10:00:55 +10001175 error = xfs_bunmapi(tp, ip, startoffset_fsb, len_fsb, 0, 2, &firstfsb,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001176 &dfops, done);
Christoph Hellwigbdb0d042016-06-21 10:00:55 +10001177 if (error)
1178 goto out_bmap_cancel;
1179
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001180 error = xfs_defer_finish(&tp, &dfops, ip);
Christoph Hellwigbdb0d042016-06-21 10:00:55 +10001181 if (error)
1182 goto out_bmap_cancel;
1183
1184 error = xfs_trans_commit(tp);
1185out_unlock:
1186 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001187 return error;
Christoph Hellwigbdb0d042016-06-21 10:00:55 +10001188
1189out_bmap_cancel:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001190 xfs_defer_cancel(&dfops);
Christoph Hellwigbdb0d042016-06-21 10:00:55 +10001191out_trans_cancel:
1192 xfs_trans_cancel(tp);
1193 goto out_unlock;
1194}
1195
1196static int
1197xfs_adjust_extent_unmap_boundaries(
1198 struct xfs_inode *ip,
1199 xfs_fileoff_t *startoffset_fsb,
1200 xfs_fileoff_t *endoffset_fsb)
1201{
1202 struct xfs_mount *mp = ip->i_mount;
1203 struct xfs_bmbt_irec imap;
1204 int nimap, error;
1205 xfs_extlen_t mod = 0;
1206
1207 nimap = 1;
1208 error = xfs_bmapi_read(ip, *startoffset_fsb, 1, &imap, &nimap, 0);
1209 if (error)
1210 return error;
1211
1212 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
Christoph Hellwigbdb0d042016-06-21 10:00:55 +10001213 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
Eric Sandeen4f1adf32017-04-19 15:19:32 -07001214 mod = do_mod(imap.br_startblock, mp->m_sb.sb_rextsize);
Christoph Hellwigbdb0d042016-06-21 10:00:55 +10001215 if (mod)
1216 *startoffset_fsb += mp->m_sb.sb_rextsize - mod;
1217 }
1218
1219 nimap = 1;
1220 error = xfs_bmapi_read(ip, *endoffset_fsb - 1, 1, &imap, &nimap, 0);
1221 if (error)
1222 return error;
1223
1224 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
1225 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
1226 mod++;
1227 if (mod && mod != mp->m_sb.sb_rextsize)
1228 *endoffset_fsb -= mod;
1229 }
1230
1231 return 0;
1232}
1233
1234static int
1235xfs_flush_unmap_range(
1236 struct xfs_inode *ip,
1237 xfs_off_t offset,
1238 xfs_off_t len)
1239{
1240 struct xfs_mount *mp = ip->i_mount;
1241 struct inode *inode = VFS_I(ip);
1242 xfs_off_t rounding, start, end;
1243 int error;
1244
1245 /* wait for the completion of any pending DIOs */
1246 inode_dio_wait(inode);
1247
1248 rounding = max_t(xfs_off_t, 1 << mp->m_sb.sb_blocklog, PAGE_SIZE);
1249 start = round_down(offset, rounding);
1250 end = round_up(offset + len, rounding) - 1;
1251
1252 error = filemap_write_and_wait_range(inode->i_mapping, start, end);
1253 if (error)
1254 return error;
1255 truncate_pagecache_range(inode, start, end);
1256 return 0;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001257}
1258
Christoph Hellwig83aee9e2013-10-12 00:55:07 -07001259int
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001260xfs_free_file_space(
Christoph Hellwig83aee9e2013-10-12 00:55:07 -07001261 struct xfs_inode *ip,
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001262 xfs_off_t offset,
Christoph Hellwig5f8aca82013-10-12 00:55:06 -07001263 xfs_off_t len)
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001264{
Christoph Hellwigbdb0d042016-06-21 10:00:55 +10001265 struct xfs_mount *mp = ip->i_mount;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001266 xfs_fileoff_t startoffset_fsb;
Christoph Hellwigbdb0d042016-06-21 10:00:55 +10001267 xfs_fileoff_t endoffset_fsb;
Christoph Hellwig3c2bdc92016-06-21 10:02:23 +10001268 int done = 0, error;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001269
1270 trace_xfs_free_file_space(ip);
1271
1272 error = xfs_qm_dqattach(ip, 0);
1273 if (error)
1274 return error;
1275
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001276 if (len <= 0) /* if nothing being freed */
Christoph Hellwigbdb0d042016-06-21 10:00:55 +10001277 return 0;
1278
1279 error = xfs_flush_unmap_range(ip, offset, len);
1280 if (error)
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001281 return error;
Christoph Hellwigbdb0d042016-06-21 10:00:55 +10001282
1283 startoffset_fsb = XFS_B_TO_FSB(mp, offset);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001284 endoffset_fsb = XFS_B_TO_FSBT(mp, offset + len);
1285
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001286 /*
Christoph Hellwigbdb0d042016-06-21 10:00:55 +10001287 * Need to zero the stuff we're not freeing, on disk. If it's a RT file
1288 * and we can't use unwritten extents then we actually need to ensure
1289 * to zero the whole extent, otherwise we just need to take of block
1290 * boundaries, and xfs_bunmapi will handle the rest.
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001291 */
Christoph Hellwigbdb0d042016-06-21 10:00:55 +10001292 if (XFS_IS_REALTIME_INODE(ip) &&
1293 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
1294 error = xfs_adjust_extent_unmap_boundaries(ip, &startoffset_fsb,
1295 &endoffset_fsb);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001296 if (error)
Christoph Hellwigbdb0d042016-06-21 10:00:55 +10001297 return error;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001298 }
Christoph Hellwigbdb0d042016-06-21 10:00:55 +10001299
Christoph Hellwig3c2bdc92016-06-21 10:02:23 +10001300 if (endoffset_fsb > startoffset_fsb) {
1301 while (!done) {
1302 error = xfs_unmap_extent(ip, startoffset_fsb,
1303 endoffset_fsb - startoffset_fsb, &done);
1304 if (error)
1305 return error;
1306 }
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001307 }
1308
1309 /*
Christoph Hellwig3c2bdc92016-06-21 10:02:23 +10001310 * Now that we've unmap all full blocks we'll have to zero out any
1311 * partial block at the beginning and/or end. xfs_zero_range is
Calvin Owens3dd09d52017-04-03 12:22:29 -07001312 * smart enough to skip any holes, including those we just created,
1313 * but we must take care not to zero beyond EOF and enlarge i_size.
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001314 */
Calvin Owens3dd09d52017-04-03 12:22:29 -07001315
1316 if (offset >= XFS_ISIZE(ip))
1317 return 0;
1318
1319 if (offset + len > XFS_ISIZE(ip))
1320 len = XFS_ISIZE(ip) - offset;
1321
Christoph Hellwig3c2bdc92016-06-21 10:02:23 +10001322 return xfs_zero_range(ip, offset, len, NULL);
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001323}
1324
Brian Foster5d11fb42014-10-30 10:35:11 +11001325/*
1326 * Preallocate and zero a range of a file. This mechanism has the allocation
1327 * semantics of fallocate and in addition converts data in the range to zeroes.
1328 */
Christoph Hellwig865e9442013-10-12 00:55:08 -07001329int
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001330xfs_zero_file_space(
1331 struct xfs_inode *ip,
1332 xfs_off_t offset,
Christoph Hellwig5f8aca82013-10-12 00:55:06 -07001333 xfs_off_t len)
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001334{
1335 struct xfs_mount *mp = ip->i_mount;
Brian Foster5d11fb42014-10-30 10:35:11 +11001336 uint blksize;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001337 int error;
1338
Dave Chinner897b73b2014-04-14 18:15:11 +10001339 trace_xfs_zero_file_space(ip);
1340
Brian Foster5d11fb42014-10-30 10:35:11 +11001341 blksize = 1 << mp->m_sb.sb_blocklog;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001342
1343 /*
Brian Foster5d11fb42014-10-30 10:35:11 +11001344 * Punch a hole and prealloc the range. We use hole punch rather than
1345 * unwritten extent conversion for two reasons:
1346 *
1347 * 1.) Hole punch handles partial block zeroing for us.
1348 *
1349 * 2.) If prealloc returns ENOSPC, the file range is still zero-valued
1350 * by virtue of the hole punch.
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001351 */
Brian Foster5d11fb42014-10-30 10:35:11 +11001352 error = xfs_free_file_space(ip, offset, len);
1353 if (error)
1354 goto out;
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001355
Brian Foster5d11fb42014-10-30 10:35:11 +11001356 error = xfs_alloc_file_space(ip, round_down(offset, blksize),
1357 round_up(offset + len, blksize) -
1358 round_down(offset, blksize),
1359 XFS_BMAPI_PREALLOC);
Christoph Hellwig5f8aca82013-10-12 00:55:06 -07001360out:
Dave Chinnerc24b5df2013-08-12 20:49:45 +10001361 return error;
1362
1363}
1364
1365/*
Namjae Jeona904b1c2015-03-25 15:08:56 +11001366 * @next_fsb will keep track of the extent currently undergoing shift.
1367 * @stop_fsb will keep track of the extent at which we have to stop.
1368 * If we are shifting left, we will start with block (offset + len) and
1369 * shift each extent till last extent.
1370 * If we are shifting right, we will start with last extent inside file space
1371 * and continue until we reach the block corresponding to offset.
Namjae Jeone1d8fb82014-02-24 10:58:19 +11001372 */
kbuild test robot72c1a732015-04-13 11:25:04 +10001373static int
Namjae Jeona904b1c2015-03-25 15:08:56 +11001374xfs_shift_file_space(
1375 struct xfs_inode *ip,
1376 xfs_off_t offset,
1377 xfs_off_t len,
1378 enum shift_direction direction)
Namjae Jeone1d8fb82014-02-24 10:58:19 +11001379{
1380 int done = 0;
1381 struct xfs_mount *mp = ip->i_mount;
1382 struct xfs_trans *tp;
1383 int error;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001384 struct xfs_defer_ops dfops;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11001385 xfs_fsblock_t first_block;
Namjae Jeona904b1c2015-03-25 15:08:56 +11001386 xfs_fileoff_t stop_fsb;
Brian Foster2c845f52014-09-23 15:37:09 +10001387 xfs_fileoff_t next_fsb;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11001388 xfs_fileoff_t shift_fsb;
Brian Foster48af96a2017-02-15 10:18:10 -08001389 uint resblks;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11001390
Namjae Jeona904b1c2015-03-25 15:08:56 +11001391 ASSERT(direction == SHIFT_LEFT || direction == SHIFT_RIGHT);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11001392
Namjae Jeona904b1c2015-03-25 15:08:56 +11001393 if (direction == SHIFT_LEFT) {
Brian Foster48af96a2017-02-15 10:18:10 -08001394 /*
1395 * Reserve blocks to cover potential extent merges after left
1396 * shift operations.
1397 */
1398 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
Namjae Jeona904b1c2015-03-25 15:08:56 +11001399 next_fsb = XFS_B_TO_FSB(mp, offset + len);
1400 stop_fsb = XFS_B_TO_FSB(mp, VFS_I(ip)->i_size);
1401 } else {
1402 /*
1403 * If right shift, delegate the work of initialization of
1404 * next_fsb to xfs_bmap_shift_extent as it has ilock held.
1405 */
Brian Foster48af96a2017-02-15 10:18:10 -08001406 resblks = 0;
Namjae Jeona904b1c2015-03-25 15:08:56 +11001407 next_fsb = NULLFSBLOCK;
1408 stop_fsb = XFS_B_TO_FSB(mp, offset);
1409 }
Namjae Jeone1d8fb82014-02-24 10:58:19 +11001410
Namjae Jeone1d8fb82014-02-24 10:58:19 +11001411 shift_fsb = XFS_B_TO_FSB(mp, len);
1412
Brian Fosterf71721d2014-09-23 15:39:05 +10001413 /*
1414 * Trim eofblocks to avoid shifting uninitialized post-eof preallocation
1415 * into the accessible region of the file.
1416 */
Brian Foster41b9d722014-09-02 12:12:53 +10001417 if (xfs_can_free_eofblocks(ip, true)) {
Brian Fostera36b9262017-01-27 23:22:55 -08001418 error = xfs_free_eofblocks(ip);
Brian Foster41b9d722014-09-02 12:12:53 +10001419 if (error)
1420 return error;
1421 }
Dave Chinner1669a8c2014-09-02 12:12:53 +10001422
Brian Fosterf71721d2014-09-23 15:39:05 +10001423 /*
1424 * Writeback and invalidate cache for the remainder of the file as we're
Namjae Jeona904b1c2015-03-25 15:08:56 +11001425 * about to shift down every extent from offset to EOF.
Brian Fosterf71721d2014-09-23 15:39:05 +10001426 */
1427 error = filemap_write_and_wait_range(VFS_I(ip)->i_mapping,
Namjae Jeona904b1c2015-03-25 15:08:56 +11001428 offset, -1);
Brian Fosterf71721d2014-09-23 15:39:05 +10001429 if (error)
1430 return error;
1431 error = invalidate_inode_pages2_range(VFS_I(ip)->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001432 offset >> PAGE_SHIFT, -1);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11001433 if (error)
1434 return error;
1435
Namjae Jeona904b1c2015-03-25 15:08:56 +11001436 /*
1437 * The extent shiting code works on extent granularity. So, if
1438 * stop_fsb is not the starting block of extent, we need to split
1439 * the extent at stop_fsb.
1440 */
1441 if (direction == SHIFT_RIGHT) {
1442 error = xfs_bmap_split_extent(ip, stop_fsb);
1443 if (error)
1444 return error;
1445 }
1446
Namjae Jeone1d8fb82014-02-24 10:58:19 +11001447 while (!error && !done) {
Brian Foster48af96a2017-02-15 10:18:10 -08001448 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0,
1449 &tp);
Christoph Hellwig253f4912016-04-06 09:19:55 +10001450 if (error)
Namjae Jeone1d8fb82014-02-24 10:58:19 +11001451 break;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11001452
1453 xfs_ilock(ip, XFS_ILOCK_EXCL);
1454 error = xfs_trans_reserve_quota(tp, mp, ip->i_udquot,
Brian Foster48af96a2017-02-15 10:18:10 -08001455 ip->i_gdquot, ip->i_pdquot, resblks, 0,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11001456 XFS_QMOPT_RES_REGBLKS);
1457 if (error)
Brian Fosterd4a97a02015-08-19 10:01:40 +10001458 goto out_trans_cancel;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11001459
Namjae Jeona904b1c2015-03-25 15:08:56 +11001460 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11001461
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001462 xfs_defer_init(&dfops, &first_block);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11001463
1464 /*
1465 * We are using the write transaction in which max 2 bmbt
1466 * updates are allowed
1467 */
Namjae Jeona904b1c2015-03-25 15:08:56 +11001468 error = xfs_bmap_shift_extents(tp, ip, &next_fsb, shift_fsb,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001469 &done, stop_fsb, &first_block, &dfops,
Namjae Jeona904b1c2015-03-25 15:08:56 +11001470 direction, XFS_BMAP_MAX_SHIFT_EXTENTS);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11001471 if (error)
Brian Fosterd4a97a02015-08-19 10:01:40 +10001472 goto out_bmap_cancel;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11001473
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001474 error = xfs_defer_finish(&tp, &dfops, NULL);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11001475 if (error)
Brian Fosterd4a97a02015-08-19 10:01:40 +10001476 goto out_bmap_cancel;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11001477
Christoph Hellwig70393312015-06-04 13:48:08 +10001478 error = xfs_trans_commit(tp);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11001479 }
1480
1481 return error;
1482
Brian Fosterd4a97a02015-08-19 10:01:40 +10001483out_bmap_cancel:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001484 xfs_defer_cancel(&dfops);
Brian Fosterd4a97a02015-08-19 10:01:40 +10001485out_trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +10001486 xfs_trans_cancel(tp);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11001487 return error;
1488}
1489
1490/*
Namjae Jeona904b1c2015-03-25 15:08:56 +11001491 * xfs_collapse_file_space()
1492 * This routine frees disk space and shift extent for the given file.
1493 * The first thing we do is to free data blocks in the specified range
1494 * by calling xfs_free_file_space(). It would also sync dirty data
1495 * and invalidate page cache over the region on which collapse range
1496 * is working. And Shift extent records to the left to cover a hole.
1497 * RETURNS:
1498 * 0 on success
1499 * errno on error
1500 *
1501 */
1502int
1503xfs_collapse_file_space(
1504 struct xfs_inode *ip,
1505 xfs_off_t offset,
1506 xfs_off_t len)
1507{
1508 int error;
1509
1510 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
1511 trace_xfs_collapse_file_space(ip);
1512
1513 error = xfs_free_file_space(ip, offset, len);
1514 if (error)
1515 return error;
1516
1517 return xfs_shift_file_space(ip, offset, len, SHIFT_LEFT);
1518}
1519
1520/*
1521 * xfs_insert_file_space()
1522 * This routine create hole space by shifting extents for the given file.
1523 * The first thing we do is to sync dirty data and invalidate page cache
1524 * over the region on which insert range is working. And split an extent
1525 * to two extents at given offset by calling xfs_bmap_split_extent.
1526 * And shift all extent records which are laying between [offset,
1527 * last allocated extent] to the right to reserve hole range.
1528 * RETURNS:
1529 * 0 on success
1530 * errno on error
1531 */
1532int
1533xfs_insert_file_space(
1534 struct xfs_inode *ip,
1535 loff_t offset,
1536 loff_t len)
1537{
1538 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
1539 trace_xfs_insert_file_space(ip);
1540
1541 return xfs_shift_file_space(ip, offset, len, SHIFT_RIGHT);
1542}
1543
1544/*
Dave Chinnera133d952013-08-12 20:49:48 +10001545 * We need to check that the format of the data fork in the temporary inode is
1546 * valid for the target inode before doing the swap. This is not a problem with
1547 * attr1 because of the fixed fork offset, but attr2 has a dynamically sized
1548 * data fork depending on the space the attribute fork is taking so we can get
1549 * invalid formats on the target inode.
1550 *
1551 * E.g. target has space for 7 extents in extent format, temp inode only has
1552 * space for 6. If we defragment down to 7 extents, then the tmp format is a
1553 * btree, but when swapped it needs to be in extent format. Hence we can't just
1554 * blindly swap data forks on attr2 filesystems.
1555 *
1556 * Note that we check the swap in both directions so that we don't end up with
1557 * a corrupt temporary inode, either.
1558 *
1559 * Note that fixing the way xfs_fsr sets up the attribute fork in the source
1560 * inode will prevent this situation from occurring, so all we do here is
1561 * reject and log the attempt. basically we are putting the responsibility on
1562 * userspace to get this right.
1563 */
1564static int
1565xfs_swap_extents_check_format(
Darrick J. Wonge06259a2016-10-03 09:11:52 -07001566 struct xfs_inode *ip, /* target inode */
1567 struct xfs_inode *tip) /* tmp inode */
Dave Chinnera133d952013-08-12 20:49:48 +10001568{
1569
1570 /* Should never get a local format */
1571 if (ip->i_d.di_format == XFS_DINODE_FMT_LOCAL ||
1572 tip->i_d.di_format == XFS_DINODE_FMT_LOCAL)
Dave Chinner24513372014-06-25 14:58:08 +10001573 return -EINVAL;
Dave Chinnera133d952013-08-12 20:49:48 +10001574
1575 /*
1576 * if the target inode has less extents that then temporary inode then
1577 * why did userspace call us?
1578 */
1579 if (ip->i_d.di_nextents < tip->i_d.di_nextents)
Dave Chinner24513372014-06-25 14:58:08 +10001580 return -EINVAL;
Dave Chinnera133d952013-08-12 20:49:48 +10001581
1582 /*
Darrick J. Wong1f08af52016-10-03 09:11:53 -07001583 * If we have to use the (expensive) rmap swap method, we can
1584 * handle any number of extents and any format.
1585 */
1586 if (xfs_sb_version_hasrmapbt(&ip->i_mount->m_sb))
1587 return 0;
1588
1589 /*
Dave Chinnera133d952013-08-12 20:49:48 +10001590 * if the target inode is in extent form and the temp inode is in btree
1591 * form then we will end up with the target inode in the wrong format
1592 * as we already know there are less extents in the temp inode.
1593 */
1594 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1595 tip->i_d.di_format == XFS_DINODE_FMT_BTREE)
Dave Chinner24513372014-06-25 14:58:08 +10001596 return -EINVAL;
Dave Chinnera133d952013-08-12 20:49:48 +10001597
1598 /* Check temp in extent form to max in target */
1599 if (tip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1600 XFS_IFORK_NEXTENTS(tip, XFS_DATA_FORK) >
1601 XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK))
Dave Chinner24513372014-06-25 14:58:08 +10001602 return -EINVAL;
Dave Chinnera133d952013-08-12 20:49:48 +10001603
1604 /* Check target in extent form to max in temp */
1605 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1606 XFS_IFORK_NEXTENTS(ip, XFS_DATA_FORK) >
1607 XFS_IFORK_MAXEXT(tip, XFS_DATA_FORK))
Dave Chinner24513372014-06-25 14:58:08 +10001608 return -EINVAL;
Dave Chinnera133d952013-08-12 20:49:48 +10001609
1610 /*
1611 * If we are in a btree format, check that the temp root block will fit
1612 * in the target and that it has enough extents to be in btree format
1613 * in the target.
1614 *
1615 * Note that we have to be careful to allow btree->extent conversions
1616 * (a common defrag case) which will occur when the temp inode is in
1617 * extent format...
1618 */
1619 if (tip->i_d.di_format == XFS_DINODE_FMT_BTREE) {
Arnd Bergmann0cbe48c2017-06-14 21:35:34 -07001620 if (XFS_IFORK_Q(ip) &&
Dave Chinnera133d952013-08-12 20:49:48 +10001621 XFS_BMAP_BMDR_SPACE(tip->i_df.if_broot) > XFS_IFORK_BOFF(ip))
Dave Chinner24513372014-06-25 14:58:08 +10001622 return -EINVAL;
Dave Chinnera133d952013-08-12 20:49:48 +10001623 if (XFS_IFORK_NEXTENTS(tip, XFS_DATA_FORK) <=
1624 XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK))
Dave Chinner24513372014-06-25 14:58:08 +10001625 return -EINVAL;
Dave Chinnera133d952013-08-12 20:49:48 +10001626 }
1627
1628 /* Reciprocal target->temp btree format checks */
1629 if (ip->i_d.di_format == XFS_DINODE_FMT_BTREE) {
Arnd Bergmann0cbe48c2017-06-14 21:35:34 -07001630 if (XFS_IFORK_Q(tip) &&
Dave Chinnera133d952013-08-12 20:49:48 +10001631 XFS_BMAP_BMDR_SPACE(ip->i_df.if_broot) > XFS_IFORK_BOFF(tip))
Dave Chinner24513372014-06-25 14:58:08 +10001632 return -EINVAL;
Dave Chinnera133d952013-08-12 20:49:48 +10001633 if (XFS_IFORK_NEXTENTS(ip, XFS_DATA_FORK) <=
1634 XFS_IFORK_MAXEXT(tip, XFS_DATA_FORK))
Dave Chinner24513372014-06-25 14:58:08 +10001635 return -EINVAL;
Dave Chinnera133d952013-08-12 20:49:48 +10001636 }
1637
1638 return 0;
1639}
1640
Dave Chinner7abbb8f2014-09-23 16:20:11 +10001641static int
Dave Chinner4ef897a2014-08-04 13:44:08 +10001642xfs_swap_extent_flush(
1643 struct xfs_inode *ip)
1644{
1645 int error;
1646
1647 error = filemap_write_and_wait(VFS_I(ip)->i_mapping);
1648 if (error)
1649 return error;
1650 truncate_pagecache_range(VFS_I(ip), 0, -1);
1651
1652 /* Verify O_DIRECT for ftmp */
1653 if (VFS_I(ip)->i_mapping->nrpages)
1654 return -EINVAL;
Dave Chinner4ef897a2014-08-04 13:44:08 +10001655 return 0;
1656}
1657
Darrick J. Wong1f08af52016-10-03 09:11:53 -07001658/*
1659 * Move extents from one file to another, when rmap is enabled.
1660 */
1661STATIC int
1662xfs_swap_extent_rmap(
1663 struct xfs_trans **tpp,
1664 struct xfs_inode *ip,
1665 struct xfs_inode *tip)
1666{
1667 struct xfs_bmbt_irec irec;
1668 struct xfs_bmbt_irec uirec;
1669 struct xfs_bmbt_irec tirec;
1670 xfs_fileoff_t offset_fsb;
1671 xfs_fileoff_t end_fsb;
1672 xfs_filblks_t count_fsb;
1673 xfs_fsblock_t firstfsb;
1674 struct xfs_defer_ops dfops;
1675 int error;
1676 xfs_filblks_t ilen;
1677 xfs_filblks_t rlen;
1678 int nimaps;
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07001679 uint64_t tip_flags2;
Darrick J. Wong1f08af52016-10-03 09:11:53 -07001680
1681 /*
1682 * If the source file has shared blocks, we must flag the donor
1683 * file as having shared blocks so that we get the shared-block
1684 * rmap functions when we go to fix up the rmaps. The flags
1685 * will be switch for reals later.
1686 */
1687 tip_flags2 = tip->i_d.di_flags2;
1688 if (ip->i_d.di_flags2 & XFS_DIFLAG2_REFLINK)
1689 tip->i_d.di_flags2 |= XFS_DIFLAG2_REFLINK;
1690
1691 offset_fsb = 0;
1692 end_fsb = XFS_B_TO_FSB(ip->i_mount, i_size_read(VFS_I(ip)));
1693 count_fsb = (xfs_filblks_t)(end_fsb - offset_fsb);
1694
1695 while (count_fsb) {
1696 /* Read extent from the donor file */
1697 nimaps = 1;
1698 error = xfs_bmapi_read(tip, offset_fsb, count_fsb, &tirec,
1699 &nimaps, 0);
1700 if (error)
1701 goto out;
1702 ASSERT(nimaps == 1);
1703 ASSERT(tirec.br_startblock != DELAYSTARTBLOCK);
1704
1705 trace_xfs_swap_extent_rmap_remap(tip, &tirec);
1706 ilen = tirec.br_blockcount;
1707
1708 /* Unmap the old blocks in the source file. */
1709 while (tirec.br_blockcount) {
1710 xfs_defer_init(&dfops, &firstfsb);
1711 trace_xfs_swap_extent_rmap_remap_piece(tip, &tirec);
1712
1713 /* Read extent from the source file */
1714 nimaps = 1;
1715 error = xfs_bmapi_read(ip, tirec.br_startoff,
1716 tirec.br_blockcount, &irec,
1717 &nimaps, 0);
1718 if (error)
1719 goto out_defer;
1720 ASSERT(nimaps == 1);
1721 ASSERT(tirec.br_startoff == irec.br_startoff);
1722 trace_xfs_swap_extent_rmap_remap_piece(ip, &irec);
1723
1724 /* Trim the extent. */
1725 uirec = tirec;
1726 uirec.br_blockcount = rlen = min_t(xfs_filblks_t,
1727 tirec.br_blockcount,
1728 irec.br_blockcount);
1729 trace_xfs_swap_extent_rmap_remap_piece(tip, &uirec);
1730
1731 /* Remove the mapping from the donor file. */
1732 error = xfs_bmap_unmap_extent((*tpp)->t_mountp, &dfops,
1733 tip, &uirec);
1734 if (error)
1735 goto out_defer;
1736
1737 /* Remove the mapping from the source file. */
1738 error = xfs_bmap_unmap_extent((*tpp)->t_mountp, &dfops,
1739 ip, &irec);
1740 if (error)
1741 goto out_defer;
1742
1743 /* Map the donor file's blocks into the source file. */
1744 error = xfs_bmap_map_extent((*tpp)->t_mountp, &dfops,
1745 ip, &uirec);
1746 if (error)
1747 goto out_defer;
1748
1749 /* Map the source file's blocks into the donor file. */
1750 error = xfs_bmap_map_extent((*tpp)->t_mountp, &dfops,
1751 tip, &irec);
1752 if (error)
1753 goto out_defer;
1754
1755 error = xfs_defer_finish(tpp, &dfops, ip);
1756 if (error)
1757 goto out_defer;
1758
1759 tirec.br_startoff += rlen;
1760 if (tirec.br_startblock != HOLESTARTBLOCK &&
1761 tirec.br_startblock != DELAYSTARTBLOCK)
1762 tirec.br_startblock += rlen;
1763 tirec.br_blockcount -= rlen;
1764 }
1765
1766 /* Roll on... */
1767 count_fsb -= ilen;
1768 offset_fsb += ilen;
1769 }
1770
1771 tip->i_d.di_flags2 = tip_flags2;
1772 return 0;
1773
1774out_defer:
1775 xfs_defer_cancel(&dfops);
1776out:
1777 trace_xfs_swap_extent_rmap_error(ip, error, _RET_IP_);
1778 tip->i_d.di_flags2 = tip_flags2;
1779 return error;
1780}
1781
Darrick J. Wong39aff5f2016-10-03 09:11:53 -07001782/* Swap the extents of two files by swapping data forks. */
1783STATIC int
1784xfs_swap_extent_forks(
1785 struct xfs_trans *tp,
1786 struct xfs_inode *ip,
1787 struct xfs_inode *tip,
1788 int *src_log_flags,
1789 int *target_log_flags)
1790{
1791 struct xfs_ifork tempifp, *ifp, *tifp;
1792 int aforkblks = 0;
1793 int taforkblks = 0;
Eric Sandeen4dfce572016-11-08 12:55:18 +11001794 xfs_extnum_t nextents;
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07001795 uint64_t tmp;
Darrick J. Wong39aff5f2016-10-03 09:11:53 -07001796 int error;
1797
1798 /*
1799 * Count the number of extended attribute blocks
1800 */
1801 if ( ((XFS_IFORK_Q(ip) != 0) && (ip->i_d.di_anextents > 0)) &&
1802 (ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)) {
1803 error = xfs_bmap_count_blocks(tp, ip, XFS_ATTR_FORK,
1804 &aforkblks);
1805 if (error)
1806 return error;
1807 }
1808 if ( ((XFS_IFORK_Q(tip) != 0) && (tip->i_d.di_anextents > 0)) &&
1809 (tip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)) {
1810 error = xfs_bmap_count_blocks(tp, tip, XFS_ATTR_FORK,
1811 &taforkblks);
1812 if (error)
1813 return error;
1814 }
1815
1816 /*
1817 * Before we've swapped the forks, lets set the owners of the forks
1818 * appropriately. We have to do this as we are demand paging the btree
1819 * buffers, and so the validation done on read will expect the owner
1820 * field to be correctly set. Once we change the owners, we can swap the
1821 * inode forks.
1822 */
1823 if (ip->i_d.di_version == 3 &&
1824 ip->i_d.di_format == XFS_DINODE_FMT_BTREE) {
1825 (*target_log_flags) |= XFS_ILOG_DOWNER;
1826 error = xfs_bmbt_change_owner(tp, ip, XFS_DATA_FORK,
1827 tip->i_ino, NULL);
1828 if (error)
1829 return error;
1830 }
1831
1832 if (tip->i_d.di_version == 3 &&
1833 tip->i_d.di_format == XFS_DINODE_FMT_BTREE) {
1834 (*src_log_flags) |= XFS_ILOG_DOWNER;
1835 error = xfs_bmbt_change_owner(tp, tip, XFS_DATA_FORK,
1836 ip->i_ino, NULL);
1837 if (error)
1838 return error;
1839 }
1840
1841 /*
1842 * Swap the data forks of the inodes
1843 */
1844 ifp = &ip->i_df;
1845 tifp = &tip->i_df;
1846 tempifp = *ifp; /* struct copy */
1847 *ifp = *tifp; /* struct copy */
1848 *tifp = tempifp; /* struct copy */
1849
1850 /*
1851 * Fix the on-disk inode values
1852 */
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07001853 tmp = (uint64_t)ip->i_d.di_nblocks;
Darrick J. Wong39aff5f2016-10-03 09:11:53 -07001854 ip->i_d.di_nblocks = tip->i_d.di_nblocks - taforkblks + aforkblks;
1855 tip->i_d.di_nblocks = tmp + taforkblks - aforkblks;
1856
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07001857 tmp = (uint64_t) ip->i_d.di_nextents;
Darrick J. Wong39aff5f2016-10-03 09:11:53 -07001858 ip->i_d.di_nextents = tip->i_d.di_nextents;
1859 tip->i_d.di_nextents = tmp;
1860
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07001861 tmp = (uint64_t) ip->i_d.di_format;
Darrick J. Wong39aff5f2016-10-03 09:11:53 -07001862 ip->i_d.di_format = tip->i_d.di_format;
1863 tip->i_d.di_format = tmp;
1864
1865 /*
1866 * The extents in the source inode could still contain speculative
1867 * preallocation beyond EOF (e.g. the file is open but not modified
1868 * while defrag is in progress). In that case, we need to copy over the
1869 * number of delalloc blocks the data fork in the source inode is
1870 * tracking beyond EOF so that when the fork is truncated away when the
1871 * temporary inode is unlinked we don't underrun the i_delayed_blks
1872 * counter on that inode.
1873 */
1874 ASSERT(tip->i_delayed_blks == 0);
1875 tip->i_delayed_blks = ip->i_delayed_blks;
1876 ip->i_delayed_blks = 0;
1877
1878 switch (ip->i_d.di_format) {
1879 case XFS_DINODE_FMT_EXTENTS:
Eric Sandeen5d829302016-11-08 12:59:42 +11001880 /*
1881 * If the extents fit in the inode, fix the pointer. Otherwise
1882 * it's already NULL or pointing to the extent.
Darrick J. Wong39aff5f2016-10-03 09:11:53 -07001883 */
Eric Sandeen5d829302016-11-08 12:59:42 +11001884 nextents = xfs_iext_count(&ip->i_df);
1885 if (nextents <= XFS_INLINE_EXTS)
1886 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
Darrick J. Wong39aff5f2016-10-03 09:11:53 -07001887 (*src_log_flags) |= XFS_ILOG_DEXT;
1888 break;
1889 case XFS_DINODE_FMT_BTREE:
1890 ASSERT(ip->i_d.di_version < 3 ||
1891 (*src_log_flags & XFS_ILOG_DOWNER));
1892 (*src_log_flags) |= XFS_ILOG_DBROOT;
1893 break;
1894 }
1895
1896 switch (tip->i_d.di_format) {
1897 case XFS_DINODE_FMT_EXTENTS:
Eric Sandeen5d829302016-11-08 12:59:42 +11001898 /*
1899 * If the extents fit in the inode, fix the pointer. Otherwise
1900 * it's already NULL or pointing to the extent.
Darrick J. Wong39aff5f2016-10-03 09:11:53 -07001901 */
Eric Sandeen5d829302016-11-08 12:59:42 +11001902 nextents = xfs_iext_count(&tip->i_df);
1903 if (nextents <= XFS_INLINE_EXTS)
1904 tifp->if_u1.if_extents = tifp->if_u2.if_inline_ext;
Darrick J. Wong39aff5f2016-10-03 09:11:53 -07001905 (*target_log_flags) |= XFS_ILOG_DEXT;
1906 break;
1907 case XFS_DINODE_FMT_BTREE:
1908 (*target_log_flags) |= XFS_ILOG_DBROOT;
1909 ASSERT(tip->i_d.di_version < 3 ||
1910 (*target_log_flags & XFS_ILOG_DOWNER));
1911 break;
1912 }
1913
1914 return 0;
1915}
1916
Dave Chinner4ef897a2014-08-04 13:44:08 +10001917int
Dave Chinnera133d952013-08-12 20:49:48 +10001918xfs_swap_extents(
Darrick J. Wonge06259a2016-10-03 09:11:52 -07001919 struct xfs_inode *ip, /* target inode */
1920 struct xfs_inode *tip, /* tmp inode */
1921 struct xfs_swapext *sxp)
Dave Chinnera133d952013-08-12 20:49:48 +10001922{
Darrick J. Wonge06259a2016-10-03 09:11:52 -07001923 struct xfs_mount *mp = ip->i_mount;
1924 struct xfs_trans *tp;
1925 struct xfs_bstat *sbp = &sxp->sx_stat;
Darrick J. Wonge06259a2016-10-03 09:11:52 -07001926 int src_log_flags, target_log_flags;
1927 int error = 0;
Darrick J. Wonge06259a2016-10-03 09:11:52 -07001928 int lock_flags;
Darrick J. Wongf0bc4d12016-10-03 09:11:42 -07001929 struct xfs_ifork *cowfp;
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07001930 uint64_t f;
Darrick J. Wong1f08af52016-10-03 09:11:53 -07001931 int resblks;
Dave Chinnera133d952013-08-12 20:49:48 +10001932
Dave Chinnera133d952013-08-12 20:49:48 +10001933 /*
Dave Chinner723cac42015-02-23 21:47:29 +11001934 * Lock the inodes against other IO, page faults and truncate to
1935 * begin with. Then we can ensure the inodes are flushed and have no
1936 * page cache safely. Once we have done this we can take the ilocks and
1937 * do the rest of the checks.
Dave Chinnera133d952013-08-12 20:49:48 +10001938 */
Christoph Hellwig65523212016-11-30 14:33:25 +11001939 lock_two_nondirectories(VFS_I(ip), VFS_I(tip));
1940 lock_flags = XFS_MMAPLOCK_EXCL;
Dave Chinner723cac42015-02-23 21:47:29 +11001941 xfs_lock_two_inodes(ip, tip, XFS_MMAPLOCK_EXCL);
Dave Chinnera133d952013-08-12 20:49:48 +10001942
1943 /* Verify that both files have the same format */
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001944 if ((VFS_I(ip)->i_mode & S_IFMT) != (VFS_I(tip)->i_mode & S_IFMT)) {
Dave Chinner24513372014-06-25 14:58:08 +10001945 error = -EINVAL;
Dave Chinnera133d952013-08-12 20:49:48 +10001946 goto out_unlock;
1947 }
1948
1949 /* Verify both files are either real-time or non-realtime */
1950 if (XFS_IS_REALTIME_INODE(ip) != XFS_IS_REALTIME_INODE(tip)) {
Dave Chinner24513372014-06-25 14:58:08 +10001951 error = -EINVAL;
Dave Chinnera133d952013-08-12 20:49:48 +10001952 goto out_unlock;
1953 }
1954
Dave Chinner4ef897a2014-08-04 13:44:08 +10001955 error = xfs_swap_extent_flush(ip);
Dave Chinnera133d952013-08-12 20:49:48 +10001956 if (error)
1957 goto out_unlock;
Dave Chinner4ef897a2014-08-04 13:44:08 +10001958 error = xfs_swap_extent_flush(tip);
1959 if (error)
1960 goto out_unlock;
Dave Chinnera133d952013-08-12 20:49:48 +10001961
Darrick J. Wong1f08af52016-10-03 09:11:53 -07001962 /*
1963 * Extent "swapping" with rmap requires a permanent reservation and
1964 * a block reservation because it's really just a remap operation
1965 * performed with log redo items!
1966 */
1967 if (xfs_sb_version_hasrmapbt(&mp->m_sb)) {
1968 /*
1969 * Conceptually this shouldn't affect the shape of either
1970 * bmbt, but since we atomically move extents one by one,
1971 * we reserve enough space to rebuild both trees.
1972 */
1973 resblks = XFS_SWAP_RMAP_SPACE_RES(mp,
1974 XFS_IFORK_NEXTENTS(ip, XFS_DATA_FORK),
1975 XFS_DATA_FORK) +
1976 XFS_SWAP_RMAP_SPACE_RES(mp,
1977 XFS_IFORK_NEXTENTS(tip, XFS_DATA_FORK),
1978 XFS_DATA_FORK);
1979 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks,
1980 0, 0, &tp);
1981 } else
1982 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0,
1983 0, 0, &tp);
Christoph Hellwig253f4912016-04-06 09:19:55 +10001984 if (error)
Dave Chinnera133d952013-08-12 20:49:48 +10001985 goto out_unlock;
Dave Chinner723cac42015-02-23 21:47:29 +11001986
1987 /*
1988 * Lock and join the inodes to the tansaction so that transaction commit
1989 * or cancel will unlock the inodes from this point onwards.
1990 */
Dave Chinner4ef897a2014-08-04 13:44:08 +10001991 xfs_lock_two_inodes(ip, tip, XFS_ILOCK_EXCL);
1992 lock_flags |= XFS_ILOCK_EXCL;
Darrick J. Wong39aff5f2016-10-03 09:11:53 -07001993 xfs_trans_ijoin(tp, ip, 0);
1994 xfs_trans_ijoin(tp, tip, 0);
Dave Chinner723cac42015-02-23 21:47:29 +11001995
Dave Chinnera133d952013-08-12 20:49:48 +10001996
1997 /* Verify all data are being swapped */
1998 if (sxp->sx_offset != 0 ||
1999 sxp->sx_length != ip->i_d.di_size ||
2000 sxp->sx_length != tip->i_d.di_size) {
Dave Chinner24513372014-06-25 14:58:08 +10002001 error = -EFAULT;
Dave Chinner4ef897a2014-08-04 13:44:08 +10002002 goto out_trans_cancel;
Dave Chinnera133d952013-08-12 20:49:48 +10002003 }
2004
2005 trace_xfs_swap_extent_before(ip, 0);
2006 trace_xfs_swap_extent_before(tip, 1);
2007
2008 /* check inode formats now that data is flushed */
2009 error = xfs_swap_extents_check_format(ip, tip);
2010 if (error) {
2011 xfs_notice(mp,
2012 "%s: inode 0x%llx format is incompatible for exchanging.",
2013 __func__, ip->i_ino);
Dave Chinner4ef897a2014-08-04 13:44:08 +10002014 goto out_trans_cancel;
Dave Chinnera133d952013-08-12 20:49:48 +10002015 }
2016
2017 /*
2018 * Compare the current change & modify times with that
2019 * passed in. If they differ, we abort this swap.
2020 * This is the mechanism used to ensure the calling
2021 * process that the file was not changed out from
2022 * under it.
2023 */
2024 if ((sbp->bs_ctime.tv_sec != VFS_I(ip)->i_ctime.tv_sec) ||
2025 (sbp->bs_ctime.tv_nsec != VFS_I(ip)->i_ctime.tv_nsec) ||
2026 (sbp->bs_mtime.tv_sec != VFS_I(ip)->i_mtime.tv_sec) ||
2027 (sbp->bs_mtime.tv_nsec != VFS_I(ip)->i_mtime.tv_nsec)) {
Dave Chinner24513372014-06-25 14:58:08 +10002028 error = -EBUSY;
Dave Chinner81217682014-08-04 13:29:32 +10002029 goto out_trans_cancel;
Dave Chinnera133d952013-08-12 20:49:48 +10002030 }
Dave Chinnera133d952013-08-12 20:49:48 +10002031
Dave Chinner21b5c972013-08-30 10:23:44 +10002032 /*
Dave Chinner21b5c972013-08-30 10:23:44 +10002033 * Note the trickiness in setting the log flags - we set the owner log
2034 * flag on the opposite inode (i.e. the inode we are setting the new
2035 * owner to be) because once we swap the forks and log that, log
2036 * recovery is going to see the fork as owned by the swapped inode,
2037 * not the pre-swapped inodes.
2038 */
2039 src_log_flags = XFS_ILOG_CORE;
2040 target_log_flags = XFS_ILOG_CORE;
Dave Chinner21b5c972013-08-30 10:23:44 +10002041
Darrick J. Wong1f08af52016-10-03 09:11:53 -07002042 if (xfs_sb_version_hasrmapbt(&mp->m_sb))
2043 error = xfs_swap_extent_rmap(&tp, ip, tip);
2044 else
2045 error = xfs_swap_extent_forks(tp, ip, tip, &src_log_flags,
2046 &target_log_flags);
Darrick J. Wong39aff5f2016-10-03 09:11:53 -07002047 if (error)
2048 goto out_trans_cancel;
Dave Chinnera133d952013-08-12 20:49:48 +10002049
Darrick J. Wongf0bc4d12016-10-03 09:11:42 -07002050 /* Do we have to swap reflink flags? */
2051 if ((ip->i_d.di_flags2 & XFS_DIFLAG2_REFLINK) ^
2052 (tip->i_d.di_flags2 & XFS_DIFLAG2_REFLINK)) {
2053 f = ip->i_d.di_flags2 & XFS_DIFLAG2_REFLINK;
2054 ip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK;
2055 ip->i_d.di_flags2 |= tip->i_d.di_flags2 & XFS_DIFLAG2_REFLINK;
2056 tip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK;
2057 tip->i_d.di_flags2 |= f & XFS_DIFLAG2_REFLINK;
2058 cowfp = ip->i_cowfp;
2059 ip->i_cowfp = tip->i_cowfp;
2060 tip->i_cowfp = cowfp;
Darrick J. Wong83104d42016-10-03 09:11:46 -07002061 xfs_inode_set_cowblocks_tag(ip);
2062 xfs_inode_set_cowblocks_tag(tip);
Darrick J. Wongf0bc4d12016-10-03 09:11:42 -07002063 }
2064
Dave Chinnera133d952013-08-12 20:49:48 +10002065 xfs_trans_log_inode(tp, ip, src_log_flags);
2066 xfs_trans_log_inode(tp, tip, target_log_flags);
2067
2068 /*
2069 * If this is a synchronous mount, make sure that the
2070 * transaction goes to disk before returning to the user.
2071 */
2072 if (mp->m_flags & XFS_MOUNT_WSYNC)
2073 xfs_trans_set_sync(tp);
2074
Christoph Hellwig70393312015-06-04 13:48:08 +10002075 error = xfs_trans_commit(tp);
Dave Chinnera133d952013-08-12 20:49:48 +10002076
2077 trace_xfs_swap_extent_after(ip, 0);
2078 trace_xfs_swap_extent_after(tip, 1);
Darrick J. Wong39aff5f2016-10-03 09:11:53 -07002079
Christoph Hellwig65523212016-11-30 14:33:25 +11002080out_unlock:
Darrick J. Wong39aff5f2016-10-03 09:11:53 -07002081 xfs_iunlock(ip, lock_flags);
2082 xfs_iunlock(tip, lock_flags);
Christoph Hellwig65523212016-11-30 14:33:25 +11002083 unlock_two_nondirectories(VFS_I(ip), VFS_I(tip));
Dave Chinnera133d952013-08-12 20:49:48 +10002084 return error;
2085
Darrick J. Wong39aff5f2016-10-03 09:11:53 -07002086out_trans_cancel:
2087 xfs_trans_cancel(tp);
Christoph Hellwig65523212016-11-30 14:33:25 +11002088 goto out_unlock;
Dave Chinnera133d952013-08-12 20:49:48 +10002089}