]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - fs/xfs/xfs_bmap_util.c
UBUNTU: Ubuntu-4.13.0-45.50
[mirror_ubuntu-artful-kernel.git] / fs / xfs / xfs_bmap_util.c
1 /*
2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
3 * Copyright (c) 2012 Red Hat, Inc.
4 * 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"
21 #include "xfs_shared.h"
22 #include "xfs_format.h"
23 #include "xfs_log_format.h"
24 #include "xfs_trans_resv.h"
25 #include "xfs_bit.h"
26 #include "xfs_mount.h"
27 #include "xfs_da_format.h"
28 #include "xfs_defer.h"
29 #include "xfs_inode.h"
30 #include "xfs_btree.h"
31 #include "xfs_trans.h"
32 #include "xfs_extfree_item.h"
33 #include "xfs_alloc.h"
34 #include "xfs_bmap.h"
35 #include "xfs_bmap_util.h"
36 #include "xfs_bmap_btree.h"
37 #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"
42 #include "xfs_icache.h"
43 #include "xfs_log.h"
44 #include "xfs_rmap_btree.h"
45 #include "xfs_iomap.h"
46 #include "xfs_reflink.h"
47 #include "xfs_refcount.h"
48
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 */
56 xfs_daddr_t
57 xfs_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 /*
65 * 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 */
71 int
72 xfs_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);
80
81 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),
84 GFP_NOFS, 0);
85 }
86
87 #ifdef CONFIG_XFS_RT
88 int
89 xfs_bmap_rtalloc(
90 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
91 {
92 int error; /* error return value */
93 xfs_mount_t *mp; /* mount point structure */
94 xfs_extlen_t prod = 0; /* product factor for allocators */
95 xfs_extlen_t ralen = 0; /* realtime allocation length */
96 xfs_extlen_t align; /* minimum allocation alignment */
97 xfs_rtblock_t rtb;
98
99 mp = ap->ip->i_mount;
100 align = xfs_get_extsz_hint(ap->ip);
101 prod = align / mp->m_sb.sb_rextsize;
102 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
103 align, 1, ap->eof, 0,
104 ap->conv, &ap->offset, &ap->length);
105 if (error)
106 return error;
107 ASSERT(ap->length);
108 ASSERT(ap->length % mp->m_sb.sb_rextsize == 0);
109
110 /*
111 * If the offset & length are not perfectly aligned
112 * then kill prod, it will just get us in trouble.
113 */
114 if (do_mod(ap->offset, align) || ap->length % align)
115 prod = 1;
116 /*
117 * Set ralen to be the actual requested length in rtextents.
118 */
119 ralen = ap->length / mp->m_sb.sb_rextsize;
120 /*
121 * If the old value was close enough to MAXEXTLEN that
122 * we rounded up to it, cut it back so it's valid again.
123 * Note that if it's a really large request (bigger than
124 * MAXEXTLEN), we don't hear about that number, and can't
125 * adjust the starting point to match it.
126 */
127 if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN)
128 ralen = MAXEXTLEN / mp->m_sb.sb_rextsize;
129
130 /*
131 * Lock out modifications to both the RT bitmap and summary inodes
132 */
133 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
134 xfs_trans_ijoin(ap->tp, mp->m_rbmip, XFS_ILOCK_EXCL);
135 xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
136 xfs_trans_ijoin(ap->tp, mp->m_rsumip, XFS_ILOCK_EXCL);
137
138 /*
139 * If it's an allocation to an empty file at offset 0,
140 * pick an extent that will space things out in the rt area.
141 */
142 if (ap->eof && ap->offset == 0) {
143 xfs_rtblock_t uninitialized_var(rtx); /* realtime extent no */
144
145 error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
146 if (error)
147 return error;
148 ap->blkno = rtx * mp->m_sb.sb_rextsize;
149 } else {
150 ap->blkno = 0;
151 }
152
153 xfs_bmap_adjacent(ap);
154
155 /*
156 * Realtime allocation, done through xfs_rtallocate_extent.
157 */
158 do_div(ap->blkno, mp->m_sb.sb_rextsize);
159 rtb = ap->blkno;
160 ap->length = ralen;
161 error = xfs_rtallocate_extent(ap->tp, ap->blkno, 1, ap->length,
162 &ralen, ap->wasdel, prod, &rtb);
163 if (error)
164 return error;
165
166 ap->blkno = rtb;
167 if (ap->blkno != NULLFSBLOCK) {
168 ap->blkno *= mp->m_sb.sb_rextsize;
169 ralen *= mp->m_sb.sb_rextsize;
170 ap->length = ralen;
171 ap->ip->i_d.di_nblocks += ralen;
172 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
173 if (ap->wasdel)
174 ap->ip->i_delayed_blks -= ralen;
175 /*
176 * Adjust the disk quota also. This was reserved
177 * earlier.
178 */
179 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
180 ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
181 XFS_TRANS_DQ_RTBCOUNT, (long) ralen);
182
183 /* Zero the extent if we were asked to do so */
184 if (ap->datatype & XFS_ALLOC_USERDATA_ZERO) {
185 error = xfs_zero_extent(ap->ip, ap->blkno, ap->length);
186 if (error)
187 return error;
188 }
189 } else {
190 ap->length = 0;
191 }
192 return 0;
193 }
194 #endif /* CONFIG_XFS_RT */
195
196 /*
197 * Check if the endoff is outside the last extent. If so the caller will grow
198 * the allocation to a stripe unit boundary. All offsets are considered outside
199 * the end of file for an empty fork, so 1 is returned in *eof in that case.
200 */
201 int
202 xfs_bmap_eof(
203 struct xfs_inode *ip,
204 xfs_fileoff_t endoff,
205 int whichfork,
206 int *eof)
207 {
208 struct xfs_bmbt_irec rec;
209 int error;
210
211 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, eof);
212 if (error || *eof)
213 return error;
214
215 *eof = endoff >= rec.br_startoff + rec.br_blockcount;
216 return 0;
217 }
218
219 /*
220 * Extent tree block counting routines.
221 */
222
223 /*
224 * Count leaf blocks given a range of extent records. Delayed allocation
225 * extents are not counted towards the totals.
226 */
227 STATIC void
228 xfs_bmap_count_leaves(
229 struct xfs_ifork *ifp,
230 xfs_extnum_t *numrecs,
231 xfs_filblks_t *count)
232 {
233 xfs_extnum_t i;
234 xfs_extnum_t nr_exts = xfs_iext_count(ifp);
235
236 for (i = 0; i < nr_exts; i++) {
237 xfs_bmbt_rec_host_t *frp = xfs_iext_get_ext(ifp, i);
238 if (!isnullstartblock(xfs_bmbt_get_startblock(frp))) {
239 (*numrecs)++;
240 *count += xfs_bmbt_get_blockcount(frp);
241 }
242 }
243 }
244
245 /*
246 * Count leaf blocks given a range of extent records originally
247 * in btree format.
248 */
249 STATIC void
250 xfs_bmap_disk_count_leaves(
251 struct xfs_mount *mp,
252 struct xfs_btree_block *block,
253 int numrecs,
254 xfs_filblks_t *count)
255 {
256 int b;
257 xfs_bmbt_rec_t *frp;
258
259 for (b = 1; b <= numrecs; b++) {
260 frp = XFS_BMBT_REC_ADDR(mp, block, b);
261 *count += xfs_bmbt_disk_get_blockcount(frp);
262 }
263 }
264
265 /*
266 * Recursively walks each level of a btree
267 * to count total fsblocks in use.
268 */
269 STATIC int
270 xfs_bmap_count_tree(
271 struct xfs_mount *mp,
272 struct xfs_trans *tp,
273 struct xfs_ifork *ifp,
274 xfs_fsblock_t blockno,
275 int levelin,
276 xfs_extnum_t *nextents,
277 xfs_filblks_t *count)
278 {
279 int error;
280 struct xfs_buf *bp, *nbp;
281 int level = levelin;
282 __be64 *pp;
283 xfs_fsblock_t bno = blockno;
284 xfs_fsblock_t nextbno;
285 struct xfs_btree_block *block, *nextblock;
286 int numrecs;
287
288 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF,
289 &xfs_bmbt_buf_ops);
290 if (error)
291 return error;
292 *count += 1;
293 block = XFS_BUF_TO_BLOCK(bp);
294
295 if (--level) {
296 /* Not at node above leaves, count this level of nodes */
297 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
298 while (nextbno != NULLFSBLOCK) {
299 error = xfs_btree_read_bufl(mp, tp, nextbno, 0, &nbp,
300 XFS_BMAP_BTREE_REF,
301 &xfs_bmbt_buf_ops);
302 if (error)
303 return error;
304 *count += 1;
305 nextblock = XFS_BUF_TO_BLOCK(nbp);
306 nextbno = be64_to_cpu(nextblock->bb_u.l.bb_rightsib);
307 xfs_trans_brelse(tp, nbp);
308 }
309
310 /* Dive to the next level */
311 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
312 bno = be64_to_cpu(*pp);
313 error = xfs_bmap_count_tree(mp, tp, ifp, bno, level, nextents,
314 count);
315 if (error) {
316 xfs_trans_brelse(tp, bp);
317 XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
318 XFS_ERRLEVEL_LOW, mp);
319 return -EFSCORRUPTED;
320 }
321 xfs_trans_brelse(tp, bp);
322 } else {
323 /* count all level 1 nodes and their leaves */
324 for (;;) {
325 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
326 numrecs = be16_to_cpu(block->bb_numrecs);
327 (*nextents) += numrecs;
328 xfs_bmap_disk_count_leaves(mp, block, numrecs, count);
329 xfs_trans_brelse(tp, bp);
330 if (nextbno == NULLFSBLOCK)
331 break;
332 bno = nextbno;
333 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
334 XFS_BMAP_BTREE_REF,
335 &xfs_bmbt_buf_ops);
336 if (error)
337 return error;
338 *count += 1;
339 block = XFS_BUF_TO_BLOCK(bp);
340 }
341 }
342 return 0;
343 }
344
345 /*
346 * Count fsblocks of the given fork. Delayed allocation extents are
347 * not counted towards the totals.
348 */
349 int
350 xfs_bmap_count_blocks(
351 struct xfs_trans *tp,
352 struct xfs_inode *ip,
353 int whichfork,
354 xfs_extnum_t *nextents,
355 xfs_filblks_t *count)
356 {
357 struct xfs_mount *mp; /* file system mount structure */
358 __be64 *pp; /* pointer to block address */
359 struct xfs_btree_block *block; /* current btree block */
360 struct xfs_ifork *ifp; /* fork structure */
361 xfs_fsblock_t bno; /* block # of "block" */
362 int level; /* btree level, for checking */
363 int error;
364
365 bno = NULLFSBLOCK;
366 mp = ip->i_mount;
367 *nextents = 0;
368 *count = 0;
369 ifp = XFS_IFORK_PTR(ip, whichfork);
370 if (!ifp)
371 return 0;
372
373 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
374 case XFS_DINODE_FMT_EXTENTS:
375 xfs_bmap_count_leaves(ifp, nextents, count);
376 return 0;
377 case XFS_DINODE_FMT_BTREE:
378 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
379 error = xfs_iread_extents(tp, ip, whichfork);
380 if (error)
381 return error;
382 }
383
384 /*
385 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
386 */
387 block = ifp->if_broot;
388 level = be16_to_cpu(block->bb_level);
389 ASSERT(level > 0);
390 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
391 bno = be64_to_cpu(*pp);
392 ASSERT(bno != NULLFSBLOCK);
393 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
394 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
395
396 error = xfs_bmap_count_tree(mp, tp, ifp, bno, level,
397 nextents, count);
398 if (error) {
399 XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)",
400 XFS_ERRLEVEL_LOW, mp);
401 return -EFSCORRUPTED;
402 }
403 return 0;
404 }
405
406 return 0;
407 }
408
409 /*
410 * returns 1 for success, 0 if we failed to map the extent.
411 */
412 STATIC int
413 xfs_getbmapx_fix_eof_hole(
414 xfs_inode_t *ip, /* xfs incore inode pointer */
415 int whichfork,
416 struct getbmapx *out, /* output structure */
417 int prealloced, /* this is a file with
418 * preallocated data space */
419 int64_t end, /* last block requested */
420 xfs_fsblock_t startblock,
421 bool moretocome)
422 {
423 int64_t fixlen;
424 xfs_mount_t *mp; /* file system mount point */
425 xfs_ifork_t *ifp; /* inode fork pointer */
426 xfs_extnum_t lastx; /* last extent pointer */
427 xfs_fileoff_t fileblock;
428
429 if (startblock == HOLESTARTBLOCK) {
430 mp = ip->i_mount;
431 out->bmv_block = -1;
432 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, XFS_ISIZE(ip)));
433 fixlen -= out->bmv_offset;
434 if (prealloced && out->bmv_offset + out->bmv_length == end) {
435 /* Came to hole at EOF. Trim it. */
436 if (fixlen <= 0)
437 return 0;
438 out->bmv_length = fixlen;
439 }
440 } else {
441 if (startblock == DELAYSTARTBLOCK)
442 out->bmv_block = -2;
443 else
444 out->bmv_block = xfs_fsb_to_db(ip, startblock);
445 fileblock = XFS_BB_TO_FSB(ip->i_mount, out->bmv_offset);
446 ifp = XFS_IFORK_PTR(ip, whichfork);
447 if (!moretocome &&
448 xfs_iext_bno_to_ext(ifp, fileblock, &lastx) &&
449 (lastx == xfs_iext_count(ifp) - 1))
450 out->bmv_oflags |= BMV_OF_LAST;
451 }
452
453 return 1;
454 }
455
456 /* Adjust the reported bmap around shared/unshared extent transitions. */
457 STATIC int
458 xfs_getbmap_adjust_shared(
459 struct xfs_inode *ip,
460 int whichfork,
461 struct xfs_bmbt_irec *map,
462 struct getbmapx *out,
463 struct xfs_bmbt_irec *next_map)
464 {
465 struct xfs_mount *mp = ip->i_mount;
466 xfs_agnumber_t agno;
467 xfs_agblock_t agbno;
468 xfs_agblock_t ebno;
469 xfs_extlen_t elen;
470 xfs_extlen_t nlen;
471 int error;
472
473 next_map->br_startblock = NULLFSBLOCK;
474 next_map->br_startoff = NULLFILEOFF;
475 next_map->br_blockcount = 0;
476
477 /* Only written data blocks can be shared. */
478 if (!xfs_is_reflink_inode(ip) ||
479 whichfork != XFS_DATA_FORK ||
480 !xfs_bmap_is_real_extent(map))
481 return 0;
482
483 agno = XFS_FSB_TO_AGNO(mp, map->br_startblock);
484 agbno = XFS_FSB_TO_AGBNO(mp, map->br_startblock);
485 error = xfs_reflink_find_shared(mp, NULL, agno, agbno,
486 map->br_blockcount, &ebno, &elen, true);
487 if (error)
488 return error;
489
490 if (ebno == NULLAGBLOCK) {
491 /* No shared blocks at all. */
492 return 0;
493 } else if (agbno == ebno) {
494 /*
495 * Shared extent at (agbno, elen). Shrink the reported
496 * extent length and prepare to move the start of map[i]
497 * to agbno+elen, with the aim of (re)formatting the new
498 * map[i] the next time through the inner loop.
499 */
500 out->bmv_length = XFS_FSB_TO_BB(mp, elen);
501 out->bmv_oflags |= BMV_OF_SHARED;
502 if (elen != map->br_blockcount) {
503 *next_map = *map;
504 next_map->br_startblock += elen;
505 next_map->br_startoff += elen;
506 next_map->br_blockcount -= elen;
507 }
508 map->br_blockcount -= elen;
509 } else {
510 /*
511 * There's an unshared extent (agbno, ebno - agbno)
512 * followed by shared extent at (ebno, elen). Shrink
513 * the reported extent length to cover only the unshared
514 * extent and prepare to move up the start of map[i] to
515 * ebno, with the aim of (re)formatting the new map[i]
516 * the next time through the inner loop.
517 */
518 *next_map = *map;
519 nlen = ebno - agbno;
520 out->bmv_length = XFS_FSB_TO_BB(mp, nlen);
521 next_map->br_startblock += nlen;
522 next_map->br_startoff += nlen;
523 next_map->br_blockcount -= nlen;
524 map->br_blockcount -= nlen;
525 }
526
527 return 0;
528 }
529
530 /*
531 * Get inode's extents as described in bmv, and format for output.
532 * Calls formatter to fill the user's buffer until all extents
533 * are mapped, until the passed-in bmv->bmv_count slots have
534 * been filled, or until the formatter short-circuits the loop,
535 * if it is tracking filled-in extents on its own.
536 */
537 int /* error code */
538 xfs_getbmap(
539 xfs_inode_t *ip,
540 struct getbmapx *bmv, /* user bmap structure */
541 xfs_bmap_format_t formatter, /* format to user */
542 void *arg) /* formatter arg */
543 {
544 int64_t bmvend; /* last block requested */
545 int error = 0; /* return value */
546 int64_t fixlen; /* length for -1 case */
547 int i; /* extent number */
548 int lock; /* lock state */
549 xfs_bmbt_irec_t *map; /* buffer for user's data */
550 xfs_mount_t *mp; /* file system mount point */
551 int nex; /* # of user extents can do */
552 int subnex; /* # of bmapi's can do */
553 int nmap; /* number of map entries */
554 struct getbmapx *out; /* output structure */
555 int whichfork; /* data or attr fork */
556 int prealloced; /* this is a file with
557 * preallocated data space */
558 int iflags; /* interface flags */
559 int bmapi_flags; /* flags for xfs_bmapi */
560 int cur_ext = 0;
561 struct xfs_bmbt_irec inject_map;
562
563 mp = ip->i_mount;
564 iflags = bmv->bmv_iflags;
565
566 #ifndef DEBUG
567 /* Only allow CoW fork queries if we're debugging. */
568 if (iflags & BMV_IF_COWFORK)
569 return -EINVAL;
570 #endif
571 if ((iflags & BMV_IF_ATTRFORK) && (iflags & BMV_IF_COWFORK))
572 return -EINVAL;
573
574 if (iflags & BMV_IF_ATTRFORK)
575 whichfork = XFS_ATTR_FORK;
576 else if (iflags & BMV_IF_COWFORK)
577 whichfork = XFS_COW_FORK;
578 else
579 whichfork = XFS_DATA_FORK;
580
581 switch (whichfork) {
582 case XFS_ATTR_FORK:
583 if (XFS_IFORK_Q(ip)) {
584 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS &&
585 ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE &&
586 ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
587 return -EINVAL;
588 } else if (unlikely(
589 ip->i_d.di_aformat != 0 &&
590 ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) {
591 XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW,
592 ip->i_mount);
593 return -EFSCORRUPTED;
594 }
595
596 prealloced = 0;
597 fixlen = 1LL << 32;
598 break;
599 case XFS_COW_FORK:
600 if (ip->i_cformat != XFS_DINODE_FMT_EXTENTS)
601 return -EINVAL;
602
603 if (xfs_get_cowextsz_hint(ip)) {
604 prealloced = 1;
605 fixlen = mp->m_super->s_maxbytes;
606 } else {
607 prealloced = 0;
608 fixlen = XFS_ISIZE(ip);
609 }
610 break;
611 default:
612 /* Local format data forks report no extents. */
613 if (ip->i_d.di_format == XFS_DINODE_FMT_LOCAL) {
614 bmv->bmv_entries = 0;
615 return 0;
616 }
617 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
618 ip->i_d.di_format != XFS_DINODE_FMT_BTREE)
619 return -EINVAL;
620
621 if (xfs_get_extsz_hint(ip) ||
622 ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){
623 prealloced = 1;
624 fixlen = mp->m_super->s_maxbytes;
625 } else {
626 prealloced = 0;
627 fixlen = XFS_ISIZE(ip);
628 }
629 break;
630 }
631
632 if (bmv->bmv_length == -1) {
633 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen));
634 bmv->bmv_length =
635 max_t(int64_t, fixlen - bmv->bmv_offset, 0);
636 } else if (bmv->bmv_length == 0) {
637 bmv->bmv_entries = 0;
638 return 0;
639 } else if (bmv->bmv_length < 0) {
640 return -EINVAL;
641 }
642
643 nex = bmv->bmv_count - 1;
644 if (nex <= 0)
645 return -EINVAL;
646 bmvend = bmv->bmv_offset + bmv->bmv_length;
647
648
649 if (bmv->bmv_count > ULONG_MAX / sizeof(struct getbmapx))
650 return -ENOMEM;
651 out = kmem_zalloc_large(bmv->bmv_count * sizeof(struct getbmapx), 0);
652 if (!out)
653 return -ENOMEM;
654
655 xfs_ilock(ip, XFS_IOLOCK_SHARED);
656 switch (whichfork) {
657 case XFS_DATA_FORK:
658 if (!(iflags & BMV_IF_DELALLOC) &&
659 (ip->i_delayed_blks || XFS_ISIZE(ip) > ip->i_d.di_size)) {
660 error = filemap_write_and_wait(VFS_I(ip)->i_mapping);
661 if (error)
662 goto out_unlock_iolock;
663
664 /*
665 * Even after flushing the inode, there can still be
666 * delalloc blocks on the inode beyond EOF due to
667 * speculative preallocation. These are not removed
668 * until the release function is called or the inode
669 * is inactivated. Hence we cannot assert here that
670 * ip->i_delayed_blks == 0.
671 */
672 }
673
674 lock = xfs_ilock_data_map_shared(ip);
675 break;
676 case XFS_COW_FORK:
677 lock = XFS_ILOCK_SHARED;
678 xfs_ilock(ip, lock);
679 break;
680 case XFS_ATTR_FORK:
681 lock = xfs_ilock_attr_map_shared(ip);
682 break;
683 }
684
685 /*
686 * Don't let nex be bigger than the number of extents
687 * we can have assuming alternating holes and real extents.
688 */
689 if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1)
690 nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1;
691
692 bmapi_flags = xfs_bmapi_aflag(whichfork);
693 if (!(iflags & BMV_IF_PREALLOC))
694 bmapi_flags |= XFS_BMAPI_IGSTATE;
695
696 /*
697 * Allocate enough space to handle "subnex" maps at a time.
698 */
699 error = -ENOMEM;
700 subnex = 16;
701 map = kmem_alloc(subnex * sizeof(*map), KM_MAYFAIL | KM_NOFS);
702 if (!map)
703 goto out_unlock_ilock;
704
705 bmv->bmv_entries = 0;
706
707 if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0 &&
708 (whichfork == XFS_ATTR_FORK || !(iflags & BMV_IF_DELALLOC))) {
709 error = 0;
710 goto out_free_map;
711 }
712
713 do {
714 nmap = (nex> subnex) ? subnex : nex;
715 error = xfs_bmapi_read(ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset),
716 XFS_BB_TO_FSB(mp, bmv->bmv_length),
717 map, &nmap, bmapi_flags);
718 if (error)
719 goto out_free_map;
720 ASSERT(nmap <= subnex);
721
722 for (i = 0; i < nmap && bmv->bmv_length &&
723 cur_ext < bmv->bmv_count - 1; i++) {
724 out[cur_ext].bmv_oflags = 0;
725 if (map[i].br_state == XFS_EXT_UNWRITTEN)
726 out[cur_ext].bmv_oflags |= BMV_OF_PREALLOC;
727 else if (map[i].br_startblock == DELAYSTARTBLOCK)
728 out[cur_ext].bmv_oflags |= BMV_OF_DELALLOC;
729 out[cur_ext].bmv_offset =
730 XFS_FSB_TO_BB(mp, map[i].br_startoff);
731 out[cur_ext].bmv_length =
732 XFS_FSB_TO_BB(mp, map[i].br_blockcount);
733 out[cur_ext].bmv_unused1 = 0;
734 out[cur_ext].bmv_unused2 = 0;
735
736 /*
737 * delayed allocation extents that start beyond EOF can
738 * occur due to speculative EOF allocation when the
739 * delalloc extent is larger than the largest freespace
740 * extent at conversion time. These extents cannot be
741 * converted by data writeback, so can exist here even
742 * if we are not supposed to be finding delalloc
743 * extents.
744 */
745 if (map[i].br_startblock == DELAYSTARTBLOCK &&
746 map[i].br_startoff < XFS_B_TO_FSB(mp, XFS_ISIZE(ip)))
747 ASSERT((iflags & BMV_IF_DELALLOC) != 0);
748
749 if (map[i].br_startblock == HOLESTARTBLOCK &&
750 whichfork == XFS_ATTR_FORK) {
751 /* came to the end of attribute fork */
752 out[cur_ext].bmv_oflags |= BMV_OF_LAST;
753 goto out_free_map;
754 }
755
756 /* Is this a shared block? */
757 error = xfs_getbmap_adjust_shared(ip, whichfork,
758 &map[i], &out[cur_ext], &inject_map);
759 if (error)
760 goto out_free_map;
761
762 if (!xfs_getbmapx_fix_eof_hole(ip, whichfork,
763 &out[cur_ext], prealloced, bmvend,
764 map[i].br_startblock,
765 inject_map.br_startblock != NULLFSBLOCK))
766 goto out_free_map;
767
768 bmv->bmv_offset =
769 out[cur_ext].bmv_offset +
770 out[cur_ext].bmv_length;
771 bmv->bmv_length =
772 max_t(int64_t, 0, bmvend - bmv->bmv_offset);
773
774 /*
775 * In case we don't want to return the hole,
776 * don't increase cur_ext so that we can reuse
777 * it in the next loop.
778 */
779 if ((iflags & BMV_IF_NO_HOLES) &&
780 map[i].br_startblock == HOLESTARTBLOCK) {
781 memset(&out[cur_ext], 0, sizeof(out[cur_ext]));
782 continue;
783 }
784
785 /*
786 * In order to report shared extents accurately,
787 * we report each distinct shared/unshared part
788 * of a single bmbt record using multiple bmap
789 * extents. To make that happen, we iterate the
790 * same map array item multiple times, each
791 * time trimming out the subextent that we just
792 * reported.
793 *
794 * Because of this, we must check the out array
795 * index (cur_ext) directly against bmv_count-1
796 * to avoid overflows.
797 */
798 if (inject_map.br_startblock != NULLFSBLOCK) {
799 map[i] = inject_map;
800 i--;
801 }
802 bmv->bmv_entries++;
803 cur_ext++;
804 }
805 } while (nmap && bmv->bmv_length && cur_ext < bmv->bmv_count - 1);
806
807 out_free_map:
808 kmem_free(map);
809 out_unlock_ilock:
810 xfs_iunlock(ip, lock);
811 out_unlock_iolock:
812 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
813
814 for (i = 0; i < cur_ext; i++) {
815 /* format results & advance arg */
816 error = formatter(&arg, &out[i]);
817 if (error)
818 break;
819 }
820
821 kmem_free(out);
822 return error;
823 }
824
825 /*
826 * dead simple method of punching delalyed allocation blocks from a range in
827 * the inode. Walks a block at a time so will be slow, but is only executed in
828 * rare error cases so the overhead is not critical. This will always punch out
829 * both the start and end blocks, even if the ranges only partially overlap
830 * them, so it is up to the caller to ensure that partial blocks are not
831 * passed in.
832 */
833 int
834 xfs_bmap_punch_delalloc_range(
835 struct xfs_inode *ip,
836 xfs_fileoff_t start_fsb,
837 xfs_fileoff_t length)
838 {
839 xfs_fileoff_t remaining = length;
840 int error = 0;
841
842 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
843
844 do {
845 int done;
846 xfs_bmbt_irec_t imap;
847 int nimaps = 1;
848 xfs_fsblock_t firstblock;
849 struct xfs_defer_ops dfops;
850
851 /*
852 * Map the range first and check that it is a delalloc extent
853 * before trying to unmap the range. Otherwise we will be
854 * trying to remove a real extent (which requires a
855 * transaction) or a hole, which is probably a bad idea...
856 */
857 error = xfs_bmapi_read(ip, start_fsb, 1, &imap, &nimaps,
858 XFS_BMAPI_ENTIRE);
859
860 if (error) {
861 /* something screwed, just bail */
862 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
863 xfs_alert(ip->i_mount,
864 "Failed delalloc mapping lookup ino %lld fsb %lld.",
865 ip->i_ino, start_fsb);
866 }
867 break;
868 }
869 if (!nimaps) {
870 /* nothing there */
871 goto next_block;
872 }
873 if (imap.br_startblock != DELAYSTARTBLOCK) {
874 /* been converted, ignore */
875 goto next_block;
876 }
877 WARN_ON(imap.br_blockcount == 0);
878
879 /*
880 * Note: while we initialise the firstblock/dfops pair, they
881 * should never be used because blocks should never be
882 * allocated or freed for a delalloc extent and hence we need
883 * don't cancel or finish them after the xfs_bunmapi() call.
884 */
885 xfs_defer_init(&dfops, &firstblock);
886 error = xfs_bunmapi(NULL, ip, start_fsb, 1, 0, 1, &firstblock,
887 &dfops, &done);
888 if (error)
889 break;
890
891 ASSERT(!xfs_defer_has_unfinished_work(&dfops));
892 next_block:
893 start_fsb++;
894 remaining--;
895 } while(remaining > 0);
896
897 return error;
898 }
899
900 /*
901 * Test whether it is appropriate to check an inode for and free post EOF
902 * blocks. The 'force' parameter determines whether we should also consider
903 * regular files that are marked preallocated or append-only.
904 */
905 bool
906 xfs_can_free_eofblocks(struct xfs_inode *ip, bool force)
907 {
908 /* prealloc/delalloc exists only on regular files */
909 if (!S_ISREG(VFS_I(ip)->i_mode))
910 return false;
911
912 /*
913 * Zero sized files with no cached pages and delalloc blocks will not
914 * have speculative prealloc/delalloc blocks to remove.
915 */
916 if (VFS_I(ip)->i_size == 0 &&
917 VFS_I(ip)->i_mapping->nrpages == 0 &&
918 ip->i_delayed_blks == 0)
919 return false;
920
921 /* If we haven't read in the extent list, then don't do it now. */
922 if (!(ip->i_df.if_flags & XFS_IFEXTENTS))
923 return false;
924
925 /*
926 * Do not free real preallocated or append-only files unless the file
927 * has delalloc blocks and we are forced to remove them.
928 */
929 if (ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND))
930 if (!force || ip->i_delayed_blks == 0)
931 return false;
932
933 return true;
934 }
935
936 /*
937 * This is called to free any blocks beyond eof. The caller must hold
938 * IOLOCK_EXCL unless we are in the inode reclaim path and have the only
939 * reference to the inode.
940 */
941 int
942 xfs_free_eofblocks(
943 struct xfs_inode *ip)
944 {
945 struct xfs_trans *tp;
946 int error;
947 xfs_fileoff_t end_fsb;
948 xfs_fileoff_t last_fsb;
949 xfs_filblks_t map_len;
950 int nimaps;
951 struct xfs_bmbt_irec imap;
952 struct xfs_mount *mp = ip->i_mount;
953
954 /*
955 * Figure out if there are any blocks beyond the end
956 * of the file. If not, then there is nothing to do.
957 */
958 end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_ISIZE(ip));
959 last_fsb = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
960 if (last_fsb <= end_fsb)
961 return 0;
962 map_len = last_fsb - end_fsb;
963
964 nimaps = 1;
965 xfs_ilock(ip, XFS_ILOCK_SHARED);
966 error = xfs_bmapi_read(ip, end_fsb, map_len, &imap, &nimaps, 0);
967 xfs_iunlock(ip, XFS_ILOCK_SHARED);
968
969 /*
970 * If there are blocks after the end of file, truncate the file to its
971 * current size to free them up.
972 */
973 if (!error && (nimaps != 0) &&
974 (imap.br_startblock != HOLESTARTBLOCK ||
975 ip->i_delayed_blks)) {
976 /*
977 * Attach the dquots to the inode up front.
978 */
979 error = xfs_qm_dqattach(ip, 0);
980 if (error)
981 return error;
982
983 /* wait on dio to ensure i_size has settled */
984 inode_dio_wait(VFS_I(ip));
985
986 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0,
987 &tp);
988 if (error) {
989 ASSERT(XFS_FORCED_SHUTDOWN(mp));
990 return error;
991 }
992
993 xfs_ilock(ip, XFS_ILOCK_EXCL);
994 xfs_trans_ijoin(tp, ip, 0);
995
996 /*
997 * Do not update the on-disk file size. If we update the
998 * on-disk file size and then the system crashes before the
999 * contents of the file are flushed to disk then the files
1000 * may be full of holes (ie NULL files bug).
1001 */
1002 error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK,
1003 XFS_ISIZE(ip));
1004 if (error) {
1005 /*
1006 * If we get an error at this point we simply don't
1007 * bother truncating the file.
1008 */
1009 xfs_trans_cancel(tp);
1010 } else {
1011 error = xfs_trans_commit(tp);
1012 if (!error)
1013 xfs_inode_clear_eofblocks_tag(ip);
1014 }
1015
1016 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1017 }
1018 return error;
1019 }
1020
1021 int
1022 xfs_alloc_file_space(
1023 struct xfs_inode *ip,
1024 xfs_off_t offset,
1025 xfs_off_t len,
1026 int alloc_type)
1027 {
1028 xfs_mount_t *mp = ip->i_mount;
1029 xfs_off_t count;
1030 xfs_filblks_t allocated_fsb;
1031 xfs_filblks_t allocatesize_fsb;
1032 xfs_extlen_t extsz, temp;
1033 xfs_fileoff_t startoffset_fsb;
1034 xfs_fsblock_t firstfsb;
1035 int nimaps;
1036 int quota_flag;
1037 int rt;
1038 xfs_trans_t *tp;
1039 xfs_bmbt_irec_t imaps[1], *imapp;
1040 struct xfs_defer_ops dfops;
1041 uint qblocks, resblks, resrtextents;
1042 int error;
1043
1044 trace_xfs_alloc_file_space(ip);
1045
1046 if (XFS_FORCED_SHUTDOWN(mp))
1047 return -EIO;
1048
1049 error = xfs_qm_dqattach(ip, 0);
1050 if (error)
1051 return error;
1052
1053 if (len <= 0)
1054 return -EINVAL;
1055
1056 rt = XFS_IS_REALTIME_INODE(ip);
1057 extsz = xfs_get_extsz_hint(ip);
1058
1059 count = len;
1060 imapp = &imaps[0];
1061 nimaps = 1;
1062 startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
1063 allocatesize_fsb = XFS_B_TO_FSB(mp, count);
1064
1065 /*
1066 * Allocate file space until done or until there is an error
1067 */
1068 while (allocatesize_fsb && !error) {
1069 xfs_fileoff_t s, e;
1070
1071 /*
1072 * Determine space reservations for data/realtime.
1073 */
1074 if (unlikely(extsz)) {
1075 s = startoffset_fsb;
1076 do_div(s, extsz);
1077 s *= extsz;
1078 e = startoffset_fsb + allocatesize_fsb;
1079 if ((temp = do_mod(startoffset_fsb, extsz)))
1080 e += temp;
1081 if ((temp = do_mod(e, extsz)))
1082 e += extsz - temp;
1083 } else {
1084 s = 0;
1085 e = allocatesize_fsb;
1086 }
1087
1088 /*
1089 * The transaction reservation is limited to a 32-bit block
1090 * count, hence we need to limit the number of blocks we are
1091 * trying to reserve to avoid an overflow. We can't allocate
1092 * more than @nimaps extents, and an extent is limited on disk
1093 * to MAXEXTLEN (21 bits), so use that to enforce the limit.
1094 */
1095 resblks = min_t(xfs_fileoff_t, (e - s), (MAXEXTLEN * nimaps));
1096 if (unlikely(rt)) {
1097 resrtextents = qblocks = resblks;
1098 resrtextents /= mp->m_sb.sb_rextsize;
1099 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
1100 quota_flag = XFS_QMOPT_RES_RTBLKS;
1101 } else {
1102 resrtextents = 0;
1103 resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resblks);
1104 quota_flag = XFS_QMOPT_RES_REGBLKS;
1105 }
1106
1107 /*
1108 * Allocate and setup the transaction.
1109 */
1110 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks,
1111 resrtextents, 0, &tp);
1112
1113 /*
1114 * Check for running out of space
1115 */
1116 if (error) {
1117 /*
1118 * Free the transaction structure.
1119 */
1120 ASSERT(error == -ENOSPC || XFS_FORCED_SHUTDOWN(mp));
1121 break;
1122 }
1123 xfs_ilock(ip, XFS_ILOCK_EXCL);
1124 error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks,
1125 0, quota_flag);
1126 if (error)
1127 goto error1;
1128
1129 xfs_trans_ijoin(tp, ip, 0);
1130
1131 xfs_defer_init(&dfops, &firstfsb);
1132 error = xfs_bmapi_write(tp, ip, startoffset_fsb,
1133 allocatesize_fsb, alloc_type, &firstfsb,
1134 resblks, imapp, &nimaps, &dfops);
1135 if (error)
1136 goto error0;
1137
1138 /*
1139 * Complete the transaction
1140 */
1141 error = xfs_defer_finish(&tp, &dfops, NULL);
1142 if (error)
1143 goto error0;
1144
1145 error = xfs_trans_commit(tp);
1146 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1147 if (error)
1148 break;
1149
1150 allocated_fsb = imapp->br_blockcount;
1151
1152 if (nimaps == 0) {
1153 error = -ENOSPC;
1154 break;
1155 }
1156
1157 startoffset_fsb += allocated_fsb;
1158 allocatesize_fsb -= allocated_fsb;
1159 }
1160
1161 return error;
1162
1163 error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
1164 xfs_defer_cancel(&dfops);
1165 xfs_trans_unreserve_quota_nblks(tp, ip, (long)qblocks, 0, quota_flag);
1166
1167 error1: /* Just cancel transaction */
1168 xfs_trans_cancel(tp);
1169 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1170 return error;
1171 }
1172
1173 static int
1174 xfs_unmap_extent(
1175 struct xfs_inode *ip,
1176 xfs_fileoff_t startoffset_fsb,
1177 xfs_filblks_t len_fsb,
1178 int *done)
1179 {
1180 struct xfs_mount *mp = ip->i_mount;
1181 struct xfs_trans *tp;
1182 struct xfs_defer_ops dfops;
1183 xfs_fsblock_t firstfsb;
1184 uint resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
1185 int error;
1186
1187 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0, &tp);
1188 if (error) {
1189 ASSERT(error == -ENOSPC || XFS_FORCED_SHUTDOWN(mp));
1190 return error;
1191 }
1192
1193 xfs_ilock(ip, XFS_ILOCK_EXCL);
1194 error = xfs_trans_reserve_quota(tp, mp, ip->i_udquot, ip->i_gdquot,
1195 ip->i_pdquot, resblks, 0, XFS_QMOPT_RES_REGBLKS);
1196 if (error)
1197 goto out_trans_cancel;
1198
1199 xfs_trans_ijoin(tp, ip, 0);
1200
1201 xfs_defer_init(&dfops, &firstfsb);
1202 error = xfs_bunmapi(tp, ip, startoffset_fsb, len_fsb, 0, 2, &firstfsb,
1203 &dfops, done);
1204 if (error)
1205 goto out_bmap_cancel;
1206
1207 error = xfs_defer_finish(&tp, &dfops, ip);
1208 if (error)
1209 goto out_bmap_cancel;
1210
1211 error = xfs_trans_commit(tp);
1212 out_unlock:
1213 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1214 return error;
1215
1216 out_bmap_cancel:
1217 xfs_defer_cancel(&dfops);
1218 out_trans_cancel:
1219 xfs_trans_cancel(tp);
1220 goto out_unlock;
1221 }
1222
1223 static int
1224 xfs_adjust_extent_unmap_boundaries(
1225 struct xfs_inode *ip,
1226 xfs_fileoff_t *startoffset_fsb,
1227 xfs_fileoff_t *endoffset_fsb)
1228 {
1229 struct xfs_mount *mp = ip->i_mount;
1230 struct xfs_bmbt_irec imap;
1231 int nimap, error;
1232 xfs_extlen_t mod = 0;
1233
1234 nimap = 1;
1235 error = xfs_bmapi_read(ip, *startoffset_fsb, 1, &imap, &nimap, 0);
1236 if (error)
1237 return error;
1238
1239 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
1240 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
1241 mod = do_mod(imap.br_startblock, mp->m_sb.sb_rextsize);
1242 if (mod)
1243 *startoffset_fsb += mp->m_sb.sb_rextsize - mod;
1244 }
1245
1246 nimap = 1;
1247 error = xfs_bmapi_read(ip, *endoffset_fsb - 1, 1, &imap, &nimap, 0);
1248 if (error)
1249 return error;
1250
1251 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
1252 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
1253 mod++;
1254 if (mod && mod != mp->m_sb.sb_rextsize)
1255 *endoffset_fsb -= mod;
1256 }
1257
1258 return 0;
1259 }
1260
1261 static int
1262 xfs_flush_unmap_range(
1263 struct xfs_inode *ip,
1264 xfs_off_t offset,
1265 xfs_off_t len)
1266 {
1267 struct xfs_mount *mp = ip->i_mount;
1268 struct inode *inode = VFS_I(ip);
1269 xfs_off_t rounding, start, end;
1270 int error;
1271
1272 /* wait for the completion of any pending DIOs */
1273 inode_dio_wait(inode);
1274
1275 rounding = max_t(xfs_off_t, 1 << mp->m_sb.sb_blocklog, PAGE_SIZE);
1276 start = round_down(offset, rounding);
1277 end = round_up(offset + len, rounding) - 1;
1278
1279 error = filemap_write_and_wait_range(inode->i_mapping, start, end);
1280 if (error)
1281 return error;
1282 truncate_pagecache_range(inode, start, end);
1283 return 0;
1284 }
1285
1286 int
1287 xfs_free_file_space(
1288 struct xfs_inode *ip,
1289 xfs_off_t offset,
1290 xfs_off_t len)
1291 {
1292 struct xfs_mount *mp = ip->i_mount;
1293 xfs_fileoff_t startoffset_fsb;
1294 xfs_fileoff_t endoffset_fsb;
1295 int done = 0, error;
1296
1297 trace_xfs_free_file_space(ip);
1298
1299 error = xfs_qm_dqattach(ip, 0);
1300 if (error)
1301 return error;
1302
1303 if (len <= 0) /* if nothing being freed */
1304 return 0;
1305
1306 error = xfs_flush_unmap_range(ip, offset, len);
1307 if (error)
1308 return error;
1309
1310 startoffset_fsb = XFS_B_TO_FSB(mp, offset);
1311 endoffset_fsb = XFS_B_TO_FSBT(mp, offset + len);
1312
1313 /*
1314 * Need to zero the stuff we're not freeing, on disk. If it's a RT file
1315 * and we can't use unwritten extents then we actually need to ensure
1316 * to zero the whole extent, otherwise we just need to take of block
1317 * boundaries, and xfs_bunmapi will handle the rest.
1318 */
1319 if (XFS_IS_REALTIME_INODE(ip) &&
1320 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
1321 error = xfs_adjust_extent_unmap_boundaries(ip, &startoffset_fsb,
1322 &endoffset_fsb);
1323 if (error)
1324 return error;
1325 }
1326
1327 if (endoffset_fsb > startoffset_fsb) {
1328 while (!done) {
1329 error = xfs_unmap_extent(ip, startoffset_fsb,
1330 endoffset_fsb - startoffset_fsb, &done);
1331 if (error)
1332 return error;
1333 }
1334 }
1335
1336 /*
1337 * Now that we've unmap all full blocks we'll have to zero out any
1338 * partial block at the beginning and/or end. xfs_zero_range is
1339 * smart enough to skip any holes, including those we just created,
1340 * but we must take care not to zero beyond EOF and enlarge i_size.
1341 */
1342
1343 if (offset >= XFS_ISIZE(ip))
1344 return 0;
1345
1346 if (offset + len > XFS_ISIZE(ip))
1347 len = XFS_ISIZE(ip) - offset;
1348
1349 return xfs_zero_range(ip, offset, len, NULL);
1350 }
1351
1352 /*
1353 * Preallocate and zero a range of a file. This mechanism has the allocation
1354 * semantics of fallocate and in addition converts data in the range to zeroes.
1355 */
1356 int
1357 xfs_zero_file_space(
1358 struct xfs_inode *ip,
1359 xfs_off_t offset,
1360 xfs_off_t len)
1361 {
1362 struct xfs_mount *mp = ip->i_mount;
1363 uint blksize;
1364 int error;
1365
1366 trace_xfs_zero_file_space(ip);
1367
1368 blksize = 1 << mp->m_sb.sb_blocklog;
1369
1370 /*
1371 * Punch a hole and prealloc the range. We use hole punch rather than
1372 * unwritten extent conversion for two reasons:
1373 *
1374 * 1.) Hole punch handles partial block zeroing for us.
1375 *
1376 * 2.) If prealloc returns ENOSPC, the file range is still zero-valued
1377 * by virtue of the hole punch.
1378 */
1379 error = xfs_free_file_space(ip, offset, len);
1380 if (error)
1381 goto out;
1382
1383 error = xfs_alloc_file_space(ip, round_down(offset, blksize),
1384 round_up(offset + len, blksize) -
1385 round_down(offset, blksize),
1386 XFS_BMAPI_PREALLOC);
1387 out:
1388 return error;
1389
1390 }
1391
1392 /*
1393 * @next_fsb will keep track of the extent currently undergoing shift.
1394 * @stop_fsb will keep track of the extent at which we have to stop.
1395 * If we are shifting left, we will start with block (offset + len) and
1396 * shift each extent till last extent.
1397 * If we are shifting right, we will start with last extent inside file space
1398 * and continue until we reach the block corresponding to offset.
1399 */
1400 static int
1401 xfs_shift_file_space(
1402 struct xfs_inode *ip,
1403 xfs_off_t offset,
1404 xfs_off_t len,
1405 enum shift_direction direction)
1406 {
1407 int done = 0;
1408 struct xfs_mount *mp = ip->i_mount;
1409 struct xfs_trans *tp;
1410 int error;
1411 struct xfs_defer_ops dfops;
1412 xfs_fsblock_t first_block;
1413 xfs_fileoff_t stop_fsb;
1414 xfs_fileoff_t next_fsb;
1415 xfs_fileoff_t shift_fsb;
1416 uint resblks;
1417
1418 ASSERT(direction == SHIFT_LEFT || direction == SHIFT_RIGHT);
1419
1420 if (direction == SHIFT_LEFT) {
1421 /*
1422 * Reserve blocks to cover potential extent merges after left
1423 * shift operations.
1424 */
1425 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
1426 next_fsb = XFS_B_TO_FSB(mp, offset + len);
1427 stop_fsb = XFS_B_TO_FSB(mp, VFS_I(ip)->i_size);
1428 } else {
1429 /*
1430 * If right shift, delegate the work of initialization of
1431 * next_fsb to xfs_bmap_shift_extent as it has ilock held.
1432 */
1433 resblks = 0;
1434 next_fsb = NULLFSBLOCK;
1435 stop_fsb = XFS_B_TO_FSB(mp, offset);
1436 }
1437
1438 shift_fsb = XFS_B_TO_FSB(mp, len);
1439
1440 /*
1441 * Trim eofblocks to avoid shifting uninitialized post-eof preallocation
1442 * into the accessible region of the file.
1443 */
1444 if (xfs_can_free_eofblocks(ip, true)) {
1445 error = xfs_free_eofblocks(ip);
1446 if (error)
1447 return error;
1448 }
1449
1450 /*
1451 * Writeback and invalidate cache for the remainder of the file as we're
1452 * about to shift down every extent from offset to EOF.
1453 */
1454 error = filemap_write_and_wait_range(VFS_I(ip)->i_mapping,
1455 offset, -1);
1456 if (error)
1457 return error;
1458 error = invalidate_inode_pages2_range(VFS_I(ip)->i_mapping,
1459 offset >> PAGE_SHIFT, -1);
1460 if (error)
1461 return error;
1462
1463 /*
1464 * Clean out anything hanging around in the cow fork now that
1465 * we've flushed all the dirty data out to disk to avoid having
1466 * CoW extents at the wrong offsets.
1467 */
1468 if (xfs_is_reflink_inode(ip)) {
1469 error = xfs_reflink_cancel_cow_range(ip, offset, NULLFILEOFF,
1470 true);
1471 if (error)
1472 return error;
1473 }
1474
1475 /*
1476 * The extent shifting code works on extent granularity. So, if
1477 * stop_fsb is not the starting block of extent, we need to split
1478 * the extent at stop_fsb.
1479 */
1480 if (direction == SHIFT_RIGHT) {
1481 error = xfs_bmap_split_extent(ip, stop_fsb);
1482 if (error)
1483 return error;
1484 }
1485
1486 while (!error && !done) {
1487 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0,
1488 &tp);
1489 if (error)
1490 break;
1491
1492 xfs_ilock(ip, XFS_ILOCK_EXCL);
1493 error = xfs_trans_reserve_quota(tp, mp, ip->i_udquot,
1494 ip->i_gdquot, ip->i_pdquot, resblks, 0,
1495 XFS_QMOPT_RES_REGBLKS);
1496 if (error)
1497 goto out_trans_cancel;
1498
1499 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1500
1501 xfs_defer_init(&dfops, &first_block);
1502
1503 /*
1504 * We are using the write transaction in which max 2 bmbt
1505 * updates are allowed
1506 */
1507 error = xfs_bmap_shift_extents(tp, ip, &next_fsb, shift_fsb,
1508 &done, stop_fsb, &first_block, &dfops,
1509 direction, XFS_BMAP_MAX_SHIFT_EXTENTS);
1510 if (error)
1511 goto out_bmap_cancel;
1512
1513 error = xfs_defer_finish(&tp, &dfops, NULL);
1514 if (error)
1515 goto out_bmap_cancel;
1516
1517 error = xfs_trans_commit(tp);
1518 }
1519
1520 return error;
1521
1522 out_bmap_cancel:
1523 xfs_defer_cancel(&dfops);
1524 out_trans_cancel:
1525 xfs_trans_cancel(tp);
1526 return error;
1527 }
1528
1529 /*
1530 * xfs_collapse_file_space()
1531 * This routine frees disk space and shift extent for the given file.
1532 * The first thing we do is to free data blocks in the specified range
1533 * by calling xfs_free_file_space(). It would also sync dirty data
1534 * and invalidate page cache over the region on which collapse range
1535 * is working. And Shift extent records to the left to cover a hole.
1536 * RETURNS:
1537 * 0 on success
1538 * errno on error
1539 *
1540 */
1541 int
1542 xfs_collapse_file_space(
1543 struct xfs_inode *ip,
1544 xfs_off_t offset,
1545 xfs_off_t len)
1546 {
1547 int error;
1548
1549 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
1550 trace_xfs_collapse_file_space(ip);
1551
1552 error = xfs_free_file_space(ip, offset, len);
1553 if (error)
1554 return error;
1555
1556 return xfs_shift_file_space(ip, offset, len, SHIFT_LEFT);
1557 }
1558
1559 /*
1560 * xfs_insert_file_space()
1561 * This routine create hole space by shifting extents for the given file.
1562 * The first thing we do is to sync dirty data and invalidate page cache
1563 * over the region on which insert range is working. And split an extent
1564 * to two extents at given offset by calling xfs_bmap_split_extent.
1565 * And shift all extent records which are laying between [offset,
1566 * last allocated extent] to the right to reserve hole range.
1567 * RETURNS:
1568 * 0 on success
1569 * errno on error
1570 */
1571 int
1572 xfs_insert_file_space(
1573 struct xfs_inode *ip,
1574 loff_t offset,
1575 loff_t len)
1576 {
1577 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
1578 trace_xfs_insert_file_space(ip);
1579
1580 return xfs_shift_file_space(ip, offset, len, SHIFT_RIGHT);
1581 }
1582
1583 /*
1584 * We need to check that the format of the data fork in the temporary inode is
1585 * valid for the target inode before doing the swap. This is not a problem with
1586 * attr1 because of the fixed fork offset, but attr2 has a dynamically sized
1587 * data fork depending on the space the attribute fork is taking so we can get
1588 * invalid formats on the target inode.
1589 *
1590 * E.g. target has space for 7 extents in extent format, temp inode only has
1591 * space for 6. If we defragment down to 7 extents, then the tmp format is a
1592 * btree, but when swapped it needs to be in extent format. Hence we can't just
1593 * blindly swap data forks on attr2 filesystems.
1594 *
1595 * Note that we check the swap in both directions so that we don't end up with
1596 * a corrupt temporary inode, either.
1597 *
1598 * Note that fixing the way xfs_fsr sets up the attribute fork in the source
1599 * inode will prevent this situation from occurring, so all we do here is
1600 * reject and log the attempt. basically we are putting the responsibility on
1601 * userspace to get this right.
1602 */
1603 static int
1604 xfs_swap_extents_check_format(
1605 struct xfs_inode *ip, /* target inode */
1606 struct xfs_inode *tip) /* tmp inode */
1607 {
1608
1609 /* Should never get a local format */
1610 if (ip->i_d.di_format == XFS_DINODE_FMT_LOCAL ||
1611 tip->i_d.di_format == XFS_DINODE_FMT_LOCAL)
1612 return -EINVAL;
1613
1614 /*
1615 * if the target inode has less extents that then temporary inode then
1616 * why did userspace call us?
1617 */
1618 if (ip->i_d.di_nextents < tip->i_d.di_nextents)
1619 return -EINVAL;
1620
1621 /*
1622 * If we have to use the (expensive) rmap swap method, we can
1623 * handle any number of extents and any format.
1624 */
1625 if (xfs_sb_version_hasrmapbt(&ip->i_mount->m_sb))
1626 return 0;
1627
1628 /*
1629 * if the target inode is in extent form and the temp inode is in btree
1630 * form then we will end up with the target inode in the wrong format
1631 * as we already know there are less extents in the temp inode.
1632 */
1633 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1634 tip->i_d.di_format == XFS_DINODE_FMT_BTREE)
1635 return -EINVAL;
1636
1637 /* Check temp in extent form to max in target */
1638 if (tip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1639 XFS_IFORK_NEXTENTS(tip, XFS_DATA_FORK) >
1640 XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK))
1641 return -EINVAL;
1642
1643 /* Check target in extent form to max in temp */
1644 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1645 XFS_IFORK_NEXTENTS(ip, XFS_DATA_FORK) >
1646 XFS_IFORK_MAXEXT(tip, XFS_DATA_FORK))
1647 return -EINVAL;
1648
1649 /*
1650 * If we are in a btree format, check that the temp root block will fit
1651 * in the target and that it has enough extents to be in btree format
1652 * in the target.
1653 *
1654 * Note that we have to be careful to allow btree->extent conversions
1655 * (a common defrag case) which will occur when the temp inode is in
1656 * extent format...
1657 */
1658 if (tip->i_d.di_format == XFS_DINODE_FMT_BTREE) {
1659 if (XFS_IFORK_Q(ip) &&
1660 XFS_BMAP_BMDR_SPACE(tip->i_df.if_broot) > XFS_IFORK_BOFF(ip))
1661 return -EINVAL;
1662 if (XFS_IFORK_NEXTENTS(tip, XFS_DATA_FORK) <=
1663 XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK))
1664 return -EINVAL;
1665 }
1666
1667 /* Reciprocal target->temp btree format checks */
1668 if (ip->i_d.di_format == XFS_DINODE_FMT_BTREE) {
1669 if (XFS_IFORK_Q(tip) &&
1670 XFS_BMAP_BMDR_SPACE(ip->i_df.if_broot) > XFS_IFORK_BOFF(tip))
1671 return -EINVAL;
1672 if (XFS_IFORK_NEXTENTS(ip, XFS_DATA_FORK) <=
1673 XFS_IFORK_MAXEXT(tip, XFS_DATA_FORK))
1674 return -EINVAL;
1675 }
1676
1677 return 0;
1678 }
1679
1680 static int
1681 xfs_swap_extent_flush(
1682 struct xfs_inode *ip)
1683 {
1684 int error;
1685
1686 error = filemap_write_and_wait(VFS_I(ip)->i_mapping);
1687 if (error)
1688 return error;
1689 truncate_pagecache_range(VFS_I(ip), 0, -1);
1690
1691 /* Verify O_DIRECT for ftmp */
1692 if (VFS_I(ip)->i_mapping->nrpages)
1693 return -EINVAL;
1694 return 0;
1695 }
1696
1697 /*
1698 * Move extents from one file to another, when rmap is enabled.
1699 */
1700 STATIC int
1701 xfs_swap_extent_rmap(
1702 struct xfs_trans **tpp,
1703 struct xfs_inode *ip,
1704 struct xfs_inode *tip)
1705 {
1706 struct xfs_bmbt_irec irec;
1707 struct xfs_bmbt_irec uirec;
1708 struct xfs_bmbt_irec tirec;
1709 xfs_fileoff_t offset_fsb;
1710 xfs_fileoff_t end_fsb;
1711 xfs_filblks_t count_fsb;
1712 xfs_fsblock_t firstfsb;
1713 struct xfs_defer_ops dfops;
1714 int error;
1715 xfs_filblks_t ilen;
1716 xfs_filblks_t rlen;
1717 int nimaps;
1718 uint64_t tip_flags2;
1719
1720 /*
1721 * If the source file has shared blocks, we must flag the donor
1722 * file as having shared blocks so that we get the shared-block
1723 * rmap functions when we go to fix up the rmaps. The flags
1724 * will be switch for reals later.
1725 */
1726 tip_flags2 = tip->i_d.di_flags2;
1727 if (ip->i_d.di_flags2 & XFS_DIFLAG2_REFLINK)
1728 tip->i_d.di_flags2 |= XFS_DIFLAG2_REFLINK;
1729
1730 offset_fsb = 0;
1731 end_fsb = XFS_B_TO_FSB(ip->i_mount, i_size_read(VFS_I(ip)));
1732 count_fsb = (xfs_filblks_t)(end_fsb - offset_fsb);
1733
1734 while (count_fsb) {
1735 /* Read extent from the donor file */
1736 nimaps = 1;
1737 error = xfs_bmapi_read(tip, offset_fsb, count_fsb, &tirec,
1738 &nimaps, 0);
1739 if (error)
1740 goto out;
1741 ASSERT(nimaps == 1);
1742 ASSERT(tirec.br_startblock != DELAYSTARTBLOCK);
1743
1744 trace_xfs_swap_extent_rmap_remap(tip, &tirec);
1745 ilen = tirec.br_blockcount;
1746
1747 /* Unmap the old blocks in the source file. */
1748 while (tirec.br_blockcount) {
1749 xfs_defer_init(&dfops, &firstfsb);
1750 trace_xfs_swap_extent_rmap_remap_piece(tip, &tirec);
1751
1752 /* Read extent from the source file */
1753 nimaps = 1;
1754 error = xfs_bmapi_read(ip, tirec.br_startoff,
1755 tirec.br_blockcount, &irec,
1756 &nimaps, 0);
1757 if (error)
1758 goto out_defer;
1759 ASSERT(nimaps == 1);
1760 ASSERT(tirec.br_startoff == irec.br_startoff);
1761 trace_xfs_swap_extent_rmap_remap_piece(ip, &irec);
1762
1763 /* Trim the extent. */
1764 uirec = tirec;
1765 uirec.br_blockcount = rlen = min_t(xfs_filblks_t,
1766 tirec.br_blockcount,
1767 irec.br_blockcount);
1768 trace_xfs_swap_extent_rmap_remap_piece(tip, &uirec);
1769
1770 /* Remove the mapping from the donor file. */
1771 error = xfs_bmap_unmap_extent((*tpp)->t_mountp, &dfops,
1772 tip, &uirec);
1773 if (error)
1774 goto out_defer;
1775
1776 /* Remove the mapping from the source file. */
1777 error = xfs_bmap_unmap_extent((*tpp)->t_mountp, &dfops,
1778 ip, &irec);
1779 if (error)
1780 goto out_defer;
1781
1782 /* Map the donor file's blocks into the source file. */
1783 error = xfs_bmap_map_extent((*tpp)->t_mountp, &dfops,
1784 ip, &uirec);
1785 if (error)
1786 goto out_defer;
1787
1788 /* Map the source file's blocks into the donor file. */
1789 error = xfs_bmap_map_extent((*tpp)->t_mountp, &dfops,
1790 tip, &irec);
1791 if (error)
1792 goto out_defer;
1793
1794 error = xfs_defer_finish(tpp, &dfops, ip);
1795 if (error)
1796 goto out_defer;
1797
1798 tirec.br_startoff += rlen;
1799 if (tirec.br_startblock != HOLESTARTBLOCK &&
1800 tirec.br_startblock != DELAYSTARTBLOCK)
1801 tirec.br_startblock += rlen;
1802 tirec.br_blockcount -= rlen;
1803 }
1804
1805 /* Roll on... */
1806 count_fsb -= ilen;
1807 offset_fsb += ilen;
1808 }
1809
1810 tip->i_d.di_flags2 = tip_flags2;
1811 return 0;
1812
1813 out_defer:
1814 xfs_defer_cancel(&dfops);
1815 out:
1816 trace_xfs_swap_extent_rmap_error(ip, error, _RET_IP_);
1817 tip->i_d.di_flags2 = tip_flags2;
1818 return error;
1819 }
1820
1821 /* Swap the extents of two files by swapping data forks. */
1822 STATIC int
1823 xfs_swap_extent_forks(
1824 struct xfs_trans *tp,
1825 struct xfs_inode *ip,
1826 struct xfs_inode *tip,
1827 int *src_log_flags,
1828 int *target_log_flags)
1829 {
1830 struct xfs_ifork tempifp, *ifp, *tifp;
1831 xfs_filblks_t aforkblks = 0;
1832 xfs_filblks_t taforkblks = 0;
1833 xfs_extnum_t junk;
1834 xfs_extnum_t nextents;
1835 uint64_t tmp;
1836 int error;
1837
1838 /*
1839 * Count the number of extended attribute blocks
1840 */
1841 if ( ((XFS_IFORK_Q(ip) != 0) && (ip->i_d.di_anextents > 0)) &&
1842 (ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)) {
1843 error = xfs_bmap_count_blocks(tp, ip, XFS_ATTR_FORK, &junk,
1844 &aforkblks);
1845 if (error)
1846 return error;
1847 }
1848 if ( ((XFS_IFORK_Q(tip) != 0) && (tip->i_d.di_anextents > 0)) &&
1849 (tip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)) {
1850 error = xfs_bmap_count_blocks(tp, tip, XFS_ATTR_FORK, &junk,
1851 &taforkblks);
1852 if (error)
1853 return error;
1854 }
1855
1856 /*
1857 * Btree format (v3) inodes have the inode number stamped in the bmbt
1858 * block headers. We can't start changing the bmbt blocks until the
1859 * inode owner change is logged so recovery does the right thing in the
1860 * event of a crash. Set the owner change log flags now and leave the
1861 * bmbt scan as the last step.
1862 */
1863 if (ip->i_d.di_version == 3 &&
1864 ip->i_d.di_format == XFS_DINODE_FMT_BTREE)
1865 (*target_log_flags) |= XFS_ILOG_DOWNER;
1866 if (tip->i_d.di_version == 3 &&
1867 tip->i_d.di_format == XFS_DINODE_FMT_BTREE)
1868 (*src_log_flags) |= XFS_ILOG_DOWNER;
1869
1870 /*
1871 * Swap the data forks of the inodes
1872 */
1873 ifp = &ip->i_df;
1874 tifp = &tip->i_df;
1875 tempifp = *ifp; /* struct copy */
1876 *ifp = *tifp; /* struct copy */
1877 *tifp = tempifp; /* struct copy */
1878
1879 /*
1880 * Fix the on-disk inode values
1881 */
1882 tmp = (uint64_t)ip->i_d.di_nblocks;
1883 ip->i_d.di_nblocks = tip->i_d.di_nblocks - taforkblks + aforkblks;
1884 tip->i_d.di_nblocks = tmp + taforkblks - aforkblks;
1885
1886 tmp = (uint64_t) ip->i_d.di_nextents;
1887 ip->i_d.di_nextents = tip->i_d.di_nextents;
1888 tip->i_d.di_nextents = tmp;
1889
1890 tmp = (uint64_t) ip->i_d.di_format;
1891 ip->i_d.di_format = tip->i_d.di_format;
1892 tip->i_d.di_format = tmp;
1893
1894 /*
1895 * The extents in the source inode could still contain speculative
1896 * preallocation beyond EOF (e.g. the file is open but not modified
1897 * while defrag is in progress). In that case, we need to copy over the
1898 * number of delalloc blocks the data fork in the source inode is
1899 * tracking beyond EOF so that when the fork is truncated away when the
1900 * temporary inode is unlinked we don't underrun the i_delayed_blks
1901 * counter on that inode.
1902 */
1903 ASSERT(tip->i_delayed_blks == 0);
1904 tip->i_delayed_blks = ip->i_delayed_blks;
1905 ip->i_delayed_blks = 0;
1906
1907 switch (ip->i_d.di_format) {
1908 case XFS_DINODE_FMT_EXTENTS:
1909 /*
1910 * If the extents fit in the inode, fix the pointer. Otherwise
1911 * it's already NULL or pointing to the extent.
1912 */
1913 nextents = xfs_iext_count(&ip->i_df);
1914 if (nextents <= XFS_INLINE_EXTS)
1915 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
1916 (*src_log_flags) |= XFS_ILOG_DEXT;
1917 break;
1918 case XFS_DINODE_FMT_BTREE:
1919 ASSERT(ip->i_d.di_version < 3 ||
1920 (*src_log_flags & XFS_ILOG_DOWNER));
1921 (*src_log_flags) |= XFS_ILOG_DBROOT;
1922 break;
1923 }
1924
1925 switch (tip->i_d.di_format) {
1926 case XFS_DINODE_FMT_EXTENTS:
1927 /*
1928 * If the extents fit in the inode, fix the pointer. Otherwise
1929 * it's already NULL or pointing to the extent.
1930 */
1931 nextents = xfs_iext_count(&tip->i_df);
1932 if (nextents <= XFS_INLINE_EXTS)
1933 tifp->if_u1.if_extents = tifp->if_u2.if_inline_ext;
1934 (*target_log_flags) |= XFS_ILOG_DEXT;
1935 break;
1936 case XFS_DINODE_FMT_BTREE:
1937 (*target_log_flags) |= XFS_ILOG_DBROOT;
1938 ASSERT(tip->i_d.di_version < 3 ||
1939 (*target_log_flags & XFS_ILOG_DOWNER));
1940 break;
1941 }
1942
1943 return 0;
1944 }
1945
1946 /*
1947 * Fix up the owners of the bmbt blocks to refer to the current inode. The
1948 * change owner scan attempts to order all modified buffers in the current
1949 * transaction. In the event of ordered buffer failure, the offending buffer is
1950 * physically logged as a fallback and the scan returns -EAGAIN. We must roll
1951 * the transaction in this case to replenish the fallback log reservation and
1952 * restart the scan. This process repeats until the scan completes.
1953 */
1954 static int
1955 xfs_swap_change_owner(
1956 struct xfs_trans **tpp,
1957 struct xfs_inode *ip,
1958 struct xfs_inode *tmpip)
1959 {
1960 int error;
1961 struct xfs_trans *tp = *tpp;
1962
1963 do {
1964 error = xfs_bmbt_change_owner(tp, ip, XFS_DATA_FORK, ip->i_ino,
1965 NULL);
1966 /* success or fatal error */
1967 if (error != -EAGAIN)
1968 break;
1969
1970 error = xfs_trans_roll(tpp, NULL);
1971 if (error)
1972 break;
1973 tp = *tpp;
1974
1975 /*
1976 * Redirty both inodes so they can relog and keep the log tail
1977 * moving forward.
1978 */
1979 xfs_trans_ijoin(tp, ip, 0);
1980 xfs_trans_ijoin(tp, tmpip, 0);
1981 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1982 xfs_trans_log_inode(tp, tmpip, XFS_ILOG_CORE);
1983 } while (true);
1984
1985 return error;
1986 }
1987
1988 int
1989 xfs_swap_extents(
1990 struct xfs_inode *ip, /* target inode */
1991 struct xfs_inode *tip, /* tmp inode */
1992 struct xfs_swapext *sxp)
1993 {
1994 struct xfs_mount *mp = ip->i_mount;
1995 struct xfs_trans *tp;
1996 struct xfs_bstat *sbp = &sxp->sx_stat;
1997 int src_log_flags, target_log_flags;
1998 int error = 0;
1999 int lock_flags;
2000 struct xfs_ifork *cowfp;
2001 uint64_t f;
2002 int resblks = 0;
2003
2004 /*
2005 * Lock the inodes against other IO, page faults and truncate to
2006 * begin with. Then we can ensure the inodes are flushed and have no
2007 * page cache safely. Once we have done this we can take the ilocks and
2008 * do the rest of the checks.
2009 */
2010 lock_two_nondirectories(VFS_I(ip), VFS_I(tip));
2011 lock_flags = XFS_MMAPLOCK_EXCL;
2012 xfs_lock_two_inodes(ip, tip, XFS_MMAPLOCK_EXCL);
2013
2014 /* Verify that both files have the same format */
2015 if ((VFS_I(ip)->i_mode & S_IFMT) != (VFS_I(tip)->i_mode & S_IFMT)) {
2016 error = -EINVAL;
2017 goto out_unlock;
2018 }
2019
2020 /* Verify both files are either real-time or non-realtime */
2021 if (XFS_IS_REALTIME_INODE(ip) != XFS_IS_REALTIME_INODE(tip)) {
2022 error = -EINVAL;
2023 goto out_unlock;
2024 }
2025
2026 error = xfs_swap_extent_flush(ip);
2027 if (error)
2028 goto out_unlock;
2029 error = xfs_swap_extent_flush(tip);
2030 if (error)
2031 goto out_unlock;
2032
2033 /*
2034 * Extent "swapping" with rmap requires a permanent reservation and
2035 * a block reservation because it's really just a remap operation
2036 * performed with log redo items!
2037 */
2038 if (xfs_sb_version_hasrmapbt(&mp->m_sb)) {
2039 /*
2040 * Conceptually this shouldn't affect the shape of either
2041 * bmbt, but since we atomically move extents one by one,
2042 * we reserve enough space to rebuild both trees.
2043 */
2044 resblks = XFS_SWAP_RMAP_SPACE_RES(mp,
2045 XFS_IFORK_NEXTENTS(ip, XFS_DATA_FORK),
2046 XFS_DATA_FORK) +
2047 XFS_SWAP_RMAP_SPACE_RES(mp,
2048 XFS_IFORK_NEXTENTS(tip, XFS_DATA_FORK),
2049 XFS_DATA_FORK);
2050 }
2051 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, resblks, 0, 0, &tp);
2052 if (error)
2053 goto out_unlock;
2054
2055 /*
2056 * Lock and join the inodes to the tansaction so that transaction commit
2057 * or cancel will unlock the inodes from this point onwards.
2058 */
2059 xfs_lock_two_inodes(ip, tip, XFS_ILOCK_EXCL);
2060 lock_flags |= XFS_ILOCK_EXCL;
2061 xfs_trans_ijoin(tp, ip, 0);
2062 xfs_trans_ijoin(tp, tip, 0);
2063
2064
2065 /* Verify all data are being swapped */
2066 if (sxp->sx_offset != 0 ||
2067 sxp->sx_length != ip->i_d.di_size ||
2068 sxp->sx_length != tip->i_d.di_size) {
2069 error = -EFAULT;
2070 goto out_trans_cancel;
2071 }
2072
2073 trace_xfs_swap_extent_before(ip, 0);
2074 trace_xfs_swap_extent_before(tip, 1);
2075
2076 /* check inode formats now that data is flushed */
2077 error = xfs_swap_extents_check_format(ip, tip);
2078 if (error) {
2079 xfs_notice(mp,
2080 "%s: inode 0x%llx format is incompatible for exchanging.",
2081 __func__, ip->i_ino);
2082 goto out_trans_cancel;
2083 }
2084
2085 /*
2086 * Compare the current change & modify times with that
2087 * passed in. If they differ, we abort this swap.
2088 * This is the mechanism used to ensure the calling
2089 * process that the file was not changed out from
2090 * under it.
2091 */
2092 if ((sbp->bs_ctime.tv_sec != VFS_I(ip)->i_ctime.tv_sec) ||
2093 (sbp->bs_ctime.tv_nsec != VFS_I(ip)->i_ctime.tv_nsec) ||
2094 (sbp->bs_mtime.tv_sec != VFS_I(ip)->i_mtime.tv_sec) ||
2095 (sbp->bs_mtime.tv_nsec != VFS_I(ip)->i_mtime.tv_nsec)) {
2096 error = -EBUSY;
2097 goto out_trans_cancel;
2098 }
2099
2100 /*
2101 * Note the trickiness in setting the log flags - we set the owner log
2102 * flag on the opposite inode (i.e. the inode we are setting the new
2103 * owner to be) because once we swap the forks and log that, log
2104 * recovery is going to see the fork as owned by the swapped inode,
2105 * not the pre-swapped inodes.
2106 */
2107 src_log_flags = XFS_ILOG_CORE;
2108 target_log_flags = XFS_ILOG_CORE;
2109
2110 if (xfs_sb_version_hasrmapbt(&mp->m_sb))
2111 error = xfs_swap_extent_rmap(&tp, ip, tip);
2112 else
2113 error = xfs_swap_extent_forks(tp, ip, tip, &src_log_flags,
2114 &target_log_flags);
2115 if (error)
2116 goto out_trans_cancel;
2117
2118 /* Do we have to swap reflink flags? */
2119 if ((ip->i_d.di_flags2 & XFS_DIFLAG2_REFLINK) ^
2120 (tip->i_d.di_flags2 & XFS_DIFLAG2_REFLINK)) {
2121 f = ip->i_d.di_flags2 & XFS_DIFLAG2_REFLINK;
2122 ip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK;
2123 ip->i_d.di_flags2 |= tip->i_d.di_flags2 & XFS_DIFLAG2_REFLINK;
2124 tip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK;
2125 tip->i_d.di_flags2 |= f & XFS_DIFLAG2_REFLINK;
2126 }
2127
2128 /* Swap the cow forks. */
2129 if (xfs_sb_version_hasreflink(&mp->m_sb)) {
2130 xfs_extnum_t extnum;
2131
2132 ASSERT(ip->i_cformat == XFS_DINODE_FMT_EXTENTS);
2133 ASSERT(tip->i_cformat == XFS_DINODE_FMT_EXTENTS);
2134
2135 extnum = ip->i_cnextents;
2136 ip->i_cnextents = tip->i_cnextents;
2137 tip->i_cnextents = extnum;
2138
2139 cowfp = ip->i_cowfp;
2140 ip->i_cowfp = tip->i_cowfp;
2141 tip->i_cowfp = cowfp;
2142
2143 if (ip->i_cowfp && ip->i_cnextents)
2144 xfs_inode_set_cowblocks_tag(ip);
2145 else
2146 xfs_inode_clear_cowblocks_tag(ip);
2147 if (tip->i_cowfp && tip->i_cnextents)
2148 xfs_inode_set_cowblocks_tag(tip);
2149 else
2150 xfs_inode_clear_cowblocks_tag(tip);
2151 }
2152
2153 xfs_trans_log_inode(tp, ip, src_log_flags);
2154 xfs_trans_log_inode(tp, tip, target_log_flags);
2155
2156 /*
2157 * The extent forks have been swapped, but crc=1,rmapbt=0 filesystems
2158 * have inode number owner values in the bmbt blocks that still refer to
2159 * the old inode. Scan each bmbt to fix up the owner values with the
2160 * inode number of the current inode.
2161 */
2162 if (src_log_flags & XFS_ILOG_DOWNER) {
2163 error = xfs_swap_change_owner(&tp, ip, tip);
2164 if (error)
2165 goto out_trans_cancel;
2166 }
2167 if (target_log_flags & XFS_ILOG_DOWNER) {
2168 error = xfs_swap_change_owner(&tp, tip, ip);
2169 if (error)
2170 goto out_trans_cancel;
2171 }
2172
2173 /*
2174 * If this is a synchronous mount, make sure that the
2175 * transaction goes to disk before returning to the user.
2176 */
2177 if (mp->m_flags & XFS_MOUNT_WSYNC)
2178 xfs_trans_set_sync(tp);
2179
2180 error = xfs_trans_commit(tp);
2181
2182 trace_xfs_swap_extent_after(ip, 0);
2183 trace_xfs_swap_extent_after(tip, 1);
2184
2185 out_unlock:
2186 xfs_iunlock(ip, lock_flags);
2187 xfs_iunlock(tip, lock_flags);
2188 unlock_two_nondirectories(VFS_I(ip), VFS_I(tip));
2189 return error;
2190
2191 out_trans_cancel:
2192 xfs_trans_cancel(tp);
2193 goto out_unlock;
2194 }