aboutsummaryrefslogtreecommitdiffstats
path: root/fs/xfs/xfs_bmap.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/xfs/xfs_bmap.c')
-rw-r--r--fs/xfs/xfs_bmap.c804
1 files changed, 9 insertions, 795 deletions
diff --git a/fs/xfs/xfs_bmap.c b/fs/xfs/xfs_bmap.c
index 72a2eea597e9..1f09fafa07fa 100644
--- a/fs/xfs/xfs_bmap.c
+++ b/fs/xfs/xfs_bmap.c
@@ -40,6 +40,7 @@
40#include "xfs_extfree_item.h" 40#include "xfs_extfree_item.h"
41#include "xfs_alloc.h" 41#include "xfs_alloc.h"
42#include "xfs_bmap.h" 42#include "xfs_bmap.h"
43#include "xfs_bmap_util.h"
43#include "xfs_rtalloc.h" 44#include "xfs_rtalloc.h"
44#include "xfs_error.h" 45#include "xfs_error.h"
45#include "xfs_attr_leaf.h" 46#include "xfs_attr_leaf.h"
@@ -109,19 +110,6 @@ xfs_bmap_compute_maxlevels(
109 mp->m_bm_maxlevels[whichfork] = level; 110 mp->m_bm_maxlevels[whichfork] = level;
110} 111}
111 112
112/*
113 * Convert the given file system block to a disk block. We have to treat it
114 * differently based on whether the file is a real time file or not, because the
115 * bmap code does.
116 */
117xfs_daddr_t
118xfs_fsb_to_db(struct xfs_inode *ip, xfs_fsblock_t fsb)
119{
120 return (XFS_IS_REALTIME_INODE(ip) ? \
121 (xfs_daddr_t)XFS_FSB_TO_BB((ip)->i_mount, (fsb)) : \
122 XFS_FSB_TO_DADDR((ip)->i_mount, (fsb)));
123}
124
125STATIC int /* error */ 113STATIC int /* error */
126xfs_bmbt_lookup_eq( 114xfs_bmbt_lookup_eq(
127 struct xfs_btree_cur *cur, 115 struct xfs_btree_cur *cur,
@@ -264,173 +252,6 @@ xfs_bmap_forkoff_reset(
264} 252}
265 253
266/* 254/*
267 * Extent tree block counting routines.
268 */
269
270/*
271 * Count leaf blocks given a range of extent records.
272 */
273STATIC void
274xfs_bmap_count_leaves(
275 xfs_ifork_t *ifp,
276 xfs_extnum_t idx,
277 int numrecs,
278 int *count)
279{
280 int b;
281
282 for (b = 0; b < numrecs; b++) {
283 xfs_bmbt_rec_host_t *frp = xfs_iext_get_ext(ifp, idx + b);
284 *count += xfs_bmbt_get_blockcount(frp);
285 }
286}
287
288/*
289 * Count leaf blocks given a range of extent records originally
290 * in btree format.
291 */
292STATIC void
293xfs_bmap_disk_count_leaves(
294 struct xfs_mount *mp,
295 struct xfs_btree_block *block,
296 int numrecs,
297 int *count)
298{
299 int b;
300 xfs_bmbt_rec_t *frp;
301
302 for (b = 1; b <= numrecs; b++) {
303 frp = XFS_BMBT_REC_ADDR(mp, block, b);
304 *count += xfs_bmbt_disk_get_blockcount(frp);
305 }
306}
307
308/*
309 * Recursively walks each level of a btree
310 * to count total fsblocks is use.
311 */
312STATIC int /* error */
313xfs_bmap_count_tree(
314 xfs_mount_t *mp, /* file system mount point */
315 xfs_trans_t *tp, /* transaction pointer */
316 xfs_ifork_t *ifp, /* inode fork pointer */
317 xfs_fsblock_t blockno, /* file system block number */
318 int levelin, /* level in btree */
319 int *count) /* Count of blocks */
320{
321 int error;
322 xfs_buf_t *bp, *nbp;
323 int level = levelin;
324 __be64 *pp;
325 xfs_fsblock_t bno = blockno;
326 xfs_fsblock_t nextbno;
327 struct xfs_btree_block *block, *nextblock;
328 int numrecs;
329
330 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF,
331 &xfs_bmbt_buf_ops);
332 if (error)
333 return error;
334 *count += 1;
335 block = XFS_BUF_TO_BLOCK(bp);
336
337 if (--level) {
338 /* Not at node above leaves, count this level of nodes */
339 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
340 while (nextbno != NULLFSBLOCK) {
341 error = xfs_btree_read_bufl(mp, tp, nextbno, 0, &nbp,
342 XFS_BMAP_BTREE_REF,
343 &xfs_bmbt_buf_ops);
344 if (error)
345 return error;
346 *count += 1;
347 nextblock = XFS_BUF_TO_BLOCK(nbp);
348 nextbno = be64_to_cpu(nextblock->bb_u.l.bb_rightsib);
349 xfs_trans_brelse(tp, nbp);
350 }
351
352 /* Dive to the next level */
353 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
354 bno = be64_to_cpu(*pp);
355 if (unlikely((error =
356 xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) {
357 xfs_trans_brelse(tp, bp);
358 XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
359 XFS_ERRLEVEL_LOW, mp);
360 return XFS_ERROR(EFSCORRUPTED);
361 }
362 xfs_trans_brelse(tp, bp);
363 } else {
364 /* count all level 1 nodes and their leaves */
365 for (;;) {
366 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
367 numrecs = be16_to_cpu(block->bb_numrecs);
368 xfs_bmap_disk_count_leaves(mp, block, numrecs, count);
369 xfs_trans_brelse(tp, bp);
370 if (nextbno == NULLFSBLOCK)
371 break;
372 bno = nextbno;
373 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
374 XFS_BMAP_BTREE_REF,
375 &xfs_bmbt_buf_ops);
376 if (error)
377 return error;
378 *count += 1;
379 block = XFS_BUF_TO_BLOCK(bp);
380 }
381 }
382 return 0;
383}
384
385/*
386 * Count fsblocks of the given fork.
387 */
388int /* error */
389xfs_bmap_count_blocks(
390 xfs_trans_t *tp, /* transaction pointer */
391 xfs_inode_t *ip, /* incore inode */
392 int whichfork, /* data or attr fork */
393 int *count) /* out: count of blocks */
394{
395 struct xfs_btree_block *block; /* current btree block */
396 xfs_fsblock_t bno; /* block # of "block" */
397 xfs_ifork_t *ifp; /* fork structure */
398 int level; /* btree level, for checking */
399 xfs_mount_t *mp; /* file system mount structure */
400 __be64 *pp; /* pointer to block address */
401
402 bno = NULLFSBLOCK;
403 mp = ip->i_mount;
404 ifp = XFS_IFORK_PTR(ip, whichfork);
405 if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) {
406 xfs_bmap_count_leaves(ifp, 0,
407 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t),
408 count);
409 return 0;
410 }
411
412 /*
413 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
414 */
415 block = ifp->if_broot;
416 level = be16_to_cpu(block->bb_level);
417 ASSERT(level > 0);
418 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
419 bno = be64_to_cpu(*pp);
420 ASSERT(bno != NULLDFSBNO);
421 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
422 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
423
424 if (unlikely(xfs_bmap_count_tree(mp, tp, ifp, bno, level, count) < 0)) {
425 XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW,
426 mp);
427 return XFS_ERROR(EFSCORRUPTED);
428 }
429
430 return 0;
431}
432
433/*
434 * Debug/sanity checking code 255 * Debug/sanity checking code
435 */ 256 */
436 257
@@ -824,7 +645,7 @@ xfs_bmap_add_free(
824 * Remove the entry "free" from the free item list. Prev points to the 645 * Remove the entry "free" from the free item list. Prev points to the
825 * previous entry, unless "free" is the head of the list. 646 * previous entry, unless "free" is the head of the list.
826 */ 647 */
827STATIC void 648void
828xfs_bmap_del_free( 649xfs_bmap_del_free(
829 xfs_bmap_free_t *flist, /* free item list header */ 650 xfs_bmap_free_t *flist, /* free item list header */
830 xfs_bmap_free_item_t *prev, /* previous item on list, if any */ 651 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
@@ -838,92 +659,6 @@ xfs_bmap_del_free(
838 kmem_zone_free(xfs_bmap_free_item_zone, free); 659 kmem_zone_free(xfs_bmap_free_item_zone, free);
839} 660}
840 661
841
842/*
843 * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi
844 * caller. Frees all the extents that need freeing, which must be done
845 * last due to locking considerations. We never free any extents in
846 * the first transaction.
847 *
848 * Return 1 if the given transaction was committed and a new one
849 * started, and 0 otherwise in the committed parameter.
850 */
851int /* error */
852xfs_bmap_finish(
853 xfs_trans_t **tp, /* transaction pointer addr */
854 xfs_bmap_free_t *flist, /* i/o: list extents to free */
855 int *committed) /* xact committed or not */
856{
857 xfs_efd_log_item_t *efd; /* extent free data */
858 xfs_efi_log_item_t *efi; /* extent free intention */
859 int error; /* error return value */
860 xfs_bmap_free_item_t *free; /* free extent item */
861 unsigned int logres; /* new log reservation */
862 unsigned int logcount; /* new log count */
863 xfs_mount_t *mp; /* filesystem mount structure */
864 xfs_bmap_free_item_t *next; /* next item on free list */
865 xfs_trans_t *ntp; /* new transaction pointer */
866
867 ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
868 if (flist->xbf_count == 0) {
869 *committed = 0;
870 return 0;
871 }
872 ntp = *tp;
873 efi = xfs_trans_get_efi(ntp, flist->xbf_count);
874 for (free = flist->xbf_first; free; free = free->xbfi_next)
875 xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock,
876 free->xbfi_blockcount);
877 logres = ntp->t_log_res;
878 logcount = ntp->t_log_count;
879 ntp = xfs_trans_dup(*tp);
880 error = xfs_trans_commit(*tp, 0);
881 *tp = ntp;
882 *committed = 1;
883 /*
884 * We have a new transaction, so we should return committed=1,
885 * even though we're returning an error.
886 */
887 if (error)
888 return error;
889
890 /*
891 * transaction commit worked ok so we can drop the extra ticket
892 * reference that we gained in xfs_trans_dup()
893 */
894 xfs_log_ticket_put(ntp->t_ticket);
895
896 if ((error = xfs_trans_reserve(ntp, 0, logres, 0, XFS_TRANS_PERM_LOG_RES,
897 logcount)))
898 return error;
899 efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count);
900 for (free = flist->xbf_first; free != NULL; free = next) {
901 next = free->xbfi_next;
902 if ((error = xfs_free_extent(ntp, free->xbfi_startblock,
903 free->xbfi_blockcount))) {
904 /*
905 * The bmap free list will be cleaned up at a
906 * higher level. The EFI will be canceled when
907 * this transaction is aborted.
908 * Need to force shutdown here to make sure it
909 * happens, since this transaction may not be
910 * dirty yet.
911 */
912 mp = ntp->t_mountp;
913 if (!XFS_FORCED_SHUTDOWN(mp))
914 xfs_force_shutdown(mp,
915 (error == EFSCORRUPTED) ?
916 SHUTDOWN_CORRUPT_INCORE :
917 SHUTDOWN_META_IO_ERROR);
918 return error;
919 }
920 xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock,
921 free->xbfi_blockcount);
922 xfs_bmap_del_free(flist, NULL, free);
923 }
924 return 0;
925}
926
927/* 662/*
928 * Free up any items left in the list. 663 * Free up any items left in the list.
929 */ 664 */
@@ -1864,7 +1599,7 @@ xfs_bmap_last_before(
1864 return 0; 1599 return 0;
1865} 1600}
1866 1601
1867STATIC int 1602int
1868xfs_bmap_last_extent( 1603xfs_bmap_last_extent(
1869 struct xfs_trans *tp, 1604 struct xfs_trans *tp,
1870 struct xfs_inode *ip, 1605 struct xfs_inode *ip,
@@ -1928,29 +1663,6 @@ xfs_bmap_isaeof(
1928} 1663}
1929 1664
1930/* 1665/*
1931 * Check if the endoff is outside the last extent. If so the caller will grow
1932 * the allocation to a stripe unit boundary. All offsets are considered outside
1933 * the end of file for an empty fork, so 1 is returned in *eof in that case.
1934 */
1935int
1936xfs_bmap_eof(
1937 struct xfs_inode *ip,
1938 xfs_fileoff_t endoff,
1939 int whichfork,
1940 int *eof)
1941{
1942 struct xfs_bmbt_irec rec;
1943 int error;
1944
1945 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, eof);
1946 if (error || *eof)
1947 return error;
1948
1949 *eof = endoff >= rec.br_startoff + rec.br_blockcount;
1950 return 0;
1951}
1952
1953/*
1954 * Returns the file-relative block number of the first block past eof in 1666 * Returns the file-relative block number of the first block past eof in
1955 * the file. This is not based on i_size, it is based on the extent records. 1667 * the file. This is not based on i_size, it is based on the extent records.
1956 * Returns 0 for local files, as they do not have extent records. 1668 * Returns 0 for local files, as they do not have extent records.
@@ -3489,7 +3201,7 @@ done:
3489/* 3201/*
3490 * Adjust the size of the new extent based on di_extsize and rt extsize. 3202 * Adjust the size of the new extent based on di_extsize and rt extsize.
3491 */ 3203 */
3492STATIC int 3204int
3493xfs_bmap_extsize_align( 3205xfs_bmap_extsize_align(
3494 xfs_mount_t *mp, 3206 xfs_mount_t *mp,
3495 xfs_bmbt_irec_t *gotp, /* next extent pointer */ 3207 xfs_bmbt_irec_t *gotp, /* next extent pointer */
@@ -3651,9 +3363,9 @@ xfs_bmap_extsize_align(
3651 3363
3652#define XFS_ALLOC_GAP_UNITS 4 3364#define XFS_ALLOC_GAP_UNITS 4
3653 3365
3654STATIC void 3366void
3655xfs_bmap_adjacent( 3367xfs_bmap_adjacent(
3656 xfs_bmalloca_t *ap) /* bmap alloc argument struct */ 3368 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
3657{ 3369{
3658 xfs_fsblock_t adjust; /* adjustment to block numbers */ 3370 xfs_fsblock_t adjust; /* adjustment to block numbers */
3659 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */ 3371 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
@@ -3800,109 +3512,6 @@ xfs_bmap_adjacent(
3800} 3512}
3801 3513
3802STATIC int 3514STATIC int
3803xfs_bmap_rtalloc(
3804 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
3805{
3806 xfs_alloctype_t atype = 0; /* type for allocation routines */
3807 int error; /* error return value */
3808 xfs_mount_t *mp; /* mount point structure */
3809 xfs_extlen_t prod = 0; /* product factor for allocators */
3810 xfs_extlen_t ralen = 0; /* realtime allocation length */
3811 xfs_extlen_t align; /* minimum allocation alignment */
3812 xfs_rtblock_t rtb;
3813
3814 mp = ap->ip->i_mount;
3815 align = xfs_get_extsz_hint(ap->ip);
3816 prod = align / mp->m_sb.sb_rextsize;
3817 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
3818 align, 1, ap->eof, 0,
3819 ap->conv, &ap->offset, &ap->length);
3820 if (error)
3821 return error;
3822 ASSERT(ap->length);
3823 ASSERT(ap->length % mp->m_sb.sb_rextsize == 0);
3824
3825 /*
3826 * If the offset & length are not perfectly aligned
3827 * then kill prod, it will just get us in trouble.
3828 */
3829 if (do_mod(ap->offset, align) || ap->length % align)
3830 prod = 1;
3831 /*
3832 * Set ralen to be the actual requested length in rtextents.
3833 */
3834 ralen = ap->length / mp->m_sb.sb_rextsize;
3835 /*
3836 * If the old value was close enough to MAXEXTLEN that
3837 * we rounded up to it, cut it back so it's valid again.
3838 * Note that if it's a really large request (bigger than
3839 * MAXEXTLEN), we don't hear about that number, and can't
3840 * adjust the starting point to match it.
3841 */
3842 if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN)
3843 ralen = MAXEXTLEN / mp->m_sb.sb_rextsize;
3844
3845 /*
3846 * Lock out other modifications to the RT bitmap inode.
3847 */
3848 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL);
3849 xfs_trans_ijoin(ap->tp, mp->m_rbmip, XFS_ILOCK_EXCL);
3850
3851 /*
3852 * If it's an allocation to an empty file at offset 0,
3853 * pick an extent that will space things out in the rt area.
3854 */
3855 if (ap->eof && ap->offset == 0) {
3856 xfs_rtblock_t uninitialized_var(rtx); /* realtime extent no */
3857
3858 error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
3859 if (error)
3860 return error;
3861 ap->blkno = rtx * mp->m_sb.sb_rextsize;
3862 } else {
3863 ap->blkno = 0;
3864 }
3865
3866 xfs_bmap_adjacent(ap);
3867
3868 /*
3869 * Realtime allocation, done through xfs_rtallocate_extent.
3870 */
3871 atype = ap->blkno == 0 ? XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO;
3872 do_div(ap->blkno, mp->m_sb.sb_rextsize);
3873 rtb = ap->blkno;
3874 ap->length = ralen;
3875 if ((error = xfs_rtallocate_extent(ap->tp, ap->blkno, 1, ap->length,
3876 &ralen, atype, ap->wasdel, prod, &rtb)))
3877 return error;
3878 if (rtb == NULLFSBLOCK && prod > 1 &&
3879 (error = xfs_rtallocate_extent(ap->tp, ap->blkno, 1,
3880 ap->length, &ralen, atype,
3881 ap->wasdel, 1, &rtb)))
3882 return error;
3883 ap->blkno = rtb;
3884 if (ap->blkno != NULLFSBLOCK) {
3885 ap->blkno *= mp->m_sb.sb_rextsize;
3886 ralen *= mp->m_sb.sb_rextsize;
3887 ap->length = ralen;
3888 ap->ip->i_d.di_nblocks += ralen;
3889 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3890 if (ap->wasdel)
3891 ap->ip->i_delayed_blks -= ralen;
3892 /*
3893 * Adjust the disk quota also. This was reserved
3894 * earlier.
3895 */
3896 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
3897 ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
3898 XFS_TRANS_DQ_RTBCOUNT, (long) ralen);
3899 } else {
3900 ap->length = 0;
3901 }
3902 return 0;
3903}
3904
3905STATIC int
3906xfs_bmap_btalloc_nullfb( 3515xfs_bmap_btalloc_nullfb(
3907 struct xfs_bmalloca *ap, 3516 struct xfs_bmalloca *ap,
3908 struct xfs_alloc_arg *args, 3517 struct xfs_alloc_arg *args,
@@ -4019,7 +3628,7 @@ xfs_bmap_btalloc_nullfb(
4019 3628
4020STATIC int 3629STATIC int
4021xfs_bmap_btalloc( 3630xfs_bmap_btalloc(
4022 xfs_bmalloca_t *ap) /* bmap alloc argument struct */ 3631 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
4023{ 3632{
4024 xfs_mount_t *mp; /* mount point structure */ 3633 xfs_mount_t *mp; /* mount point structure */
4025 xfs_alloctype_t atype = 0; /* type for allocation routines */ 3634 xfs_alloctype_t atype = 0; /* type for allocation routines */
@@ -4251,7 +3860,7 @@ xfs_bmap_btalloc(
4251 */ 3860 */
4252STATIC int 3861STATIC int
4253xfs_bmap_alloc( 3862xfs_bmap_alloc(
4254 xfs_bmalloca_t *ap) /* bmap alloc argument struct */ 3863 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
4255{ 3864{
4256 if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata) 3865 if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata)
4257 return xfs_bmap_rtalloc(ap); 3866 return xfs_bmap_rtalloc(ap);
@@ -4639,7 +4248,7 @@ xfs_bmapi_delay(
4639} 4248}
4640 4249
4641 4250
4642STATIC int 4251int
4643__xfs_bmapi_allocate( 4252__xfs_bmapi_allocate(
4644 struct xfs_bmalloca *bma) 4253 struct xfs_bmalloca *bma)
4645{ 4254{
@@ -4757,45 +4366,6 @@ __xfs_bmapi_allocate(
4757 return 0; 4366 return 0;
4758} 4367}
4759 4368
4760static void
4761xfs_bmapi_allocate_worker(
4762 struct work_struct *work)
4763{
4764 struct xfs_bmalloca *args = container_of(work,
4765 struct xfs_bmalloca, work);
4766 unsigned long pflags;
4767
4768 /* we are in a transaction context here */
4769 current_set_flags_nested(&pflags, PF_FSTRANS);
4770
4771 args->result = __xfs_bmapi_allocate(args);
4772 complete(args->done);
4773
4774 current_restore_flags_nested(&pflags, PF_FSTRANS);
4775}
4776
4777/*
4778 * Some allocation requests often come in with little stack to work on. Push
4779 * them off to a worker thread so there is lots of stack to use. Otherwise just
4780 * call directly to avoid the context switch overhead here.
4781 */
4782int
4783xfs_bmapi_allocate(
4784 struct xfs_bmalloca *args)
4785{
4786 DECLARE_COMPLETION_ONSTACK(done);
4787
4788 if (!args->stack_switch)
4789 return __xfs_bmapi_allocate(args);
4790
4791
4792 args->done = &done;
4793 INIT_WORK_ONSTACK(&args->work, xfs_bmapi_allocate_worker);
4794 queue_work(xfs_alloc_wq, &args->work);
4795 wait_for_completion(&done);
4796 return args->result;
4797}
4798
4799STATIC int 4369STATIC int
4800xfs_bmapi_convert_unwritten( 4370xfs_bmapi_convert_unwritten(
4801 struct xfs_bmalloca *bma, 4371 struct xfs_bmalloca *bma,
@@ -5790,359 +5360,3 @@ error0:
5790 } 5360 }
5791 return error; 5361 return error;
5792} 5362}
5793
5794/*
5795 * returns 1 for success, 0 if we failed to map the extent.
5796 */
5797STATIC int
5798xfs_getbmapx_fix_eof_hole(
5799 xfs_inode_t *ip, /* xfs incore inode pointer */
5800 struct getbmapx *out, /* output structure */
5801 int prealloced, /* this is a file with
5802 * preallocated data space */
5803 __int64_t end, /* last block requested */
5804 xfs_fsblock_t startblock)
5805{
5806 __int64_t fixlen;
5807 xfs_mount_t *mp; /* file system mount point */
5808 xfs_ifork_t *ifp; /* inode fork pointer */
5809 xfs_extnum_t lastx; /* last extent pointer */
5810 xfs_fileoff_t fileblock;
5811
5812 if (startblock == HOLESTARTBLOCK) {
5813 mp = ip->i_mount;
5814 out->bmv_block = -1;
5815 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, XFS_ISIZE(ip)));
5816 fixlen -= out->bmv_offset;
5817 if (prealloced && out->bmv_offset + out->bmv_length == end) {
5818 /* Came to hole at EOF. Trim it. */
5819 if (fixlen <= 0)
5820 return 0;
5821 out->bmv_length = fixlen;
5822 }
5823 } else {
5824 if (startblock == DELAYSTARTBLOCK)
5825 out->bmv_block = -2;
5826 else
5827 out->bmv_block = xfs_fsb_to_db(ip, startblock);
5828 fileblock = XFS_BB_TO_FSB(ip->i_mount, out->bmv_offset);
5829 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
5830 if (xfs_iext_bno_to_ext(ifp, fileblock, &lastx) &&
5831 (lastx == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))-1))
5832 out->bmv_oflags |= BMV_OF_LAST;
5833 }
5834
5835 return 1;
5836}
5837
5838/*
5839 * Get inode's extents as described in bmv, and format for output.
5840 * Calls formatter to fill the user's buffer until all extents
5841 * are mapped, until the passed-in bmv->bmv_count slots have
5842 * been filled, or until the formatter short-circuits the loop,
5843 * if it is tracking filled-in extents on its own.
5844 */
5845int /* error code */
5846xfs_getbmap(
5847 xfs_inode_t *ip,
5848 struct getbmapx *bmv, /* user bmap structure */
5849 xfs_bmap_format_t formatter, /* format to user */
5850 void *arg) /* formatter arg */
5851{
5852 __int64_t bmvend; /* last block requested */
5853 int error = 0; /* return value */
5854 __int64_t fixlen; /* length for -1 case */
5855 int i; /* extent number */
5856 int lock; /* lock state */
5857 xfs_bmbt_irec_t *map; /* buffer for user's data */
5858 xfs_mount_t *mp; /* file system mount point */
5859 int nex; /* # of user extents can do */
5860 int nexleft; /* # of user extents left */
5861 int subnex; /* # of bmapi's can do */
5862 int nmap; /* number of map entries */
5863 struct getbmapx *out; /* output structure */
5864 int whichfork; /* data or attr fork */
5865 int prealloced; /* this is a file with
5866 * preallocated data space */
5867 int iflags; /* interface flags */
5868 int bmapi_flags; /* flags for xfs_bmapi */
5869 int cur_ext = 0;
5870
5871 mp = ip->i_mount;
5872 iflags = bmv->bmv_iflags;
5873 whichfork = iflags & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK;
5874
5875 if (whichfork == XFS_ATTR_FORK) {
5876 if (XFS_IFORK_Q(ip)) {
5877 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS &&
5878 ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE &&
5879 ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
5880 return XFS_ERROR(EINVAL);
5881 } else if (unlikely(
5882 ip->i_d.di_aformat != 0 &&
5883 ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) {
5884 XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW,
5885 ip->i_mount);
5886 return XFS_ERROR(EFSCORRUPTED);
5887 }
5888
5889 prealloced = 0;
5890 fixlen = 1LL << 32;
5891 } else {
5892 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
5893 ip->i_d.di_format != XFS_DINODE_FMT_BTREE &&
5894 ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
5895 return XFS_ERROR(EINVAL);
5896
5897 if (xfs_get_extsz_hint(ip) ||
5898 ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){
5899 prealloced = 1;
5900 fixlen = mp->m_super->s_maxbytes;
5901 } else {
5902 prealloced = 0;
5903 fixlen = XFS_ISIZE(ip);
5904 }
5905 }
5906
5907 if (bmv->bmv_length == -1) {
5908 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen));
5909 bmv->bmv_length =
5910 max_t(__int64_t, fixlen - bmv->bmv_offset, 0);
5911 } else if (bmv->bmv_length == 0) {
5912 bmv->bmv_entries = 0;
5913 return 0;
5914 } else if (bmv->bmv_length < 0) {
5915 return XFS_ERROR(EINVAL);
5916 }
5917
5918 nex = bmv->bmv_count - 1;
5919 if (nex <= 0)
5920 return XFS_ERROR(EINVAL);
5921 bmvend = bmv->bmv_offset + bmv->bmv_length;
5922
5923
5924 if (bmv->bmv_count > ULONG_MAX / sizeof(struct getbmapx))
5925 return XFS_ERROR(ENOMEM);
5926 out = kmem_zalloc(bmv->bmv_count * sizeof(struct getbmapx), KM_MAYFAIL);
5927 if (!out) {
5928 out = kmem_zalloc_large(bmv->bmv_count *
5929 sizeof(struct getbmapx));
5930 if (!out)
5931 return XFS_ERROR(ENOMEM);
5932 }
5933
5934 xfs_ilock(ip, XFS_IOLOCK_SHARED);
5935 if (whichfork == XFS_DATA_FORK && !(iflags & BMV_IF_DELALLOC)) {
5936 if (ip->i_delayed_blks || XFS_ISIZE(ip) > ip->i_d.di_size) {
5937 error = -filemap_write_and_wait(VFS_I(ip)->i_mapping);
5938 if (error)
5939 goto out_unlock_iolock;
5940 }
5941 /*
5942 * even after flushing the inode, there can still be delalloc
5943 * blocks on the inode beyond EOF due to speculative
5944 * preallocation. These are not removed until the release
5945 * function is called or the inode is inactivated. Hence we
5946 * cannot assert here that ip->i_delayed_blks == 0.
5947 */
5948 }
5949
5950 lock = xfs_ilock_map_shared(ip);
5951
5952 /*
5953 * Don't let nex be bigger than the number of extents
5954 * we can have assuming alternating holes and real extents.
5955 */
5956 if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1)
5957 nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1;
5958
5959 bmapi_flags = xfs_bmapi_aflag(whichfork);
5960 if (!(iflags & BMV_IF_PREALLOC))
5961 bmapi_flags |= XFS_BMAPI_IGSTATE;
5962
5963 /*
5964 * Allocate enough space to handle "subnex" maps at a time.
5965 */
5966 error = ENOMEM;
5967 subnex = 16;
5968 map = kmem_alloc(subnex * sizeof(*map), KM_MAYFAIL | KM_NOFS);
5969 if (!map)
5970 goto out_unlock_ilock;
5971
5972 bmv->bmv_entries = 0;
5973
5974 if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0 &&
5975 (whichfork == XFS_ATTR_FORK || !(iflags & BMV_IF_DELALLOC))) {
5976 error = 0;
5977 goto out_free_map;
5978 }
5979
5980 nexleft = nex;
5981
5982 do {
5983 nmap = (nexleft > subnex) ? subnex : nexleft;
5984 error = xfs_bmapi_read(ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset),
5985 XFS_BB_TO_FSB(mp, bmv->bmv_length),
5986 map, &nmap, bmapi_flags);
5987 if (error)
5988 goto out_free_map;
5989 ASSERT(nmap <= subnex);
5990
5991 for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) {
5992 out[cur_ext].bmv_oflags = 0;
5993 if (map[i].br_state == XFS_EXT_UNWRITTEN)
5994 out[cur_ext].bmv_oflags |= BMV_OF_PREALLOC;
5995 else if (map[i].br_startblock == DELAYSTARTBLOCK)
5996 out[cur_ext].bmv_oflags |= BMV_OF_DELALLOC;
5997 out[cur_ext].bmv_offset =
5998 XFS_FSB_TO_BB(mp, map[i].br_startoff);
5999 out[cur_ext].bmv_length =
6000 XFS_FSB_TO_BB(mp, map[i].br_blockcount);
6001 out[cur_ext].bmv_unused1 = 0;
6002 out[cur_ext].bmv_unused2 = 0;
6003
6004 /*
6005 * delayed allocation extents that start beyond EOF can
6006 * occur due to speculative EOF allocation when the
6007 * delalloc extent is larger than the largest freespace
6008 * extent at conversion time. These extents cannot be
6009 * converted by data writeback, so can exist here even
6010 * if we are not supposed to be finding delalloc
6011 * extents.
6012 */
6013 if (map[i].br_startblock == DELAYSTARTBLOCK &&
6014 map[i].br_startoff <= XFS_B_TO_FSB(mp, XFS_ISIZE(ip)))
6015 ASSERT((iflags & BMV_IF_DELALLOC) != 0);
6016
6017 if (map[i].br_startblock == HOLESTARTBLOCK &&
6018 whichfork == XFS_ATTR_FORK) {
6019 /* came to the end of attribute fork */
6020 out[cur_ext].bmv_oflags |= BMV_OF_LAST;
6021 goto out_free_map;
6022 }
6023
6024 if (!xfs_getbmapx_fix_eof_hole(ip, &out[cur_ext],
6025 prealloced, bmvend,
6026 map[i].br_startblock))
6027 goto out_free_map;
6028
6029 bmv->bmv_offset =
6030 out[cur_ext].bmv_offset +
6031 out[cur_ext].bmv_length;
6032 bmv->bmv_length =
6033 max_t(__int64_t, 0, bmvend - bmv->bmv_offset);
6034
6035 /*
6036 * In case we don't want to return the hole,
6037 * don't increase cur_ext so that we can reuse
6038 * it in the next loop.
6039 */
6040 if ((iflags & BMV_IF_NO_HOLES) &&
6041 map[i].br_startblock == HOLESTARTBLOCK) {
6042 memset(&out[cur_ext], 0, sizeof(out[cur_ext]));
6043 continue;
6044 }
6045
6046 nexleft--;
6047 bmv->bmv_entries++;
6048 cur_ext++;
6049 }
6050 } while (nmap && nexleft && bmv->bmv_length);
6051
6052 out_free_map:
6053 kmem_free(map);
6054 out_unlock_ilock:
6055 xfs_iunlock_map_shared(ip, lock);
6056 out_unlock_iolock:
6057 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
6058
6059 for (i = 0; i < cur_ext; i++) {
6060 int full = 0; /* user array is full */
6061
6062 /* format results & advance arg */
6063 error = formatter(&arg, &out[i], &full);
6064 if (error || full)
6065 break;
6066 }
6067
6068 if (is_vmalloc_addr(out))
6069 kmem_free_large(out);
6070 else
6071 kmem_free(out);
6072 return error;
6073}
6074
6075/*
6076 * dead simple method of punching delalyed allocation blocks from a range in
6077 * the inode. Walks a block at a time so will be slow, but is only executed in
6078 * rare error cases so the overhead is not critical. This will alays punch out
6079 * both the start and end blocks, even if the ranges only partially overlap
6080 * them, so it is up to the caller to ensure that partial blocks are not
6081 * passed in.
6082 */
6083int
6084xfs_bmap_punch_delalloc_range(
6085 struct xfs_inode *ip,
6086 xfs_fileoff_t start_fsb,
6087 xfs_fileoff_t length)
6088{
6089 xfs_fileoff_t remaining = length;
6090 int error = 0;
6091
6092 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
6093
6094 do {
6095 int done;
6096 xfs_bmbt_irec_t imap;
6097 int nimaps = 1;
6098 xfs_fsblock_t firstblock;
6099 xfs_bmap_free_t flist;
6100
6101 /*
6102 * Map the range first and check that it is a delalloc extent
6103 * before trying to unmap the range. Otherwise we will be
6104 * trying to remove a real extent (which requires a
6105 * transaction) or a hole, which is probably a bad idea...
6106 */
6107 error = xfs_bmapi_read(ip, start_fsb, 1, &imap, &nimaps,
6108 XFS_BMAPI_ENTIRE);
6109
6110 if (error) {
6111 /* something screwed, just bail */
6112 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
6113 xfs_alert(ip->i_mount,
6114 "Failed delalloc mapping lookup ino %lld fsb %lld.",
6115 ip->i_ino, start_fsb);
6116 }
6117 break;
6118 }
6119 if (!nimaps) {
6120 /* nothing there */
6121 goto next_block;
6122 }
6123 if (imap.br_startblock != DELAYSTARTBLOCK) {
6124 /* been converted, ignore */
6125 goto next_block;
6126 }
6127 WARN_ON(imap.br_blockcount == 0);
6128
6129 /*
6130 * Note: while we initialise the firstblock/flist pair, they
6131 * should never be used because blocks should never be
6132 * allocated or freed for a delalloc extent and hence we need
6133 * don't cancel or finish them after the xfs_bunmapi() call.
6134 */
6135 xfs_bmap_init(&flist, &firstblock);
6136 error = xfs_bunmapi(NULL, ip, start_fsb, 1, 0, 1, &firstblock,
6137 &flist, &done);
6138 if (error)
6139 break;
6140
6141 ASSERT(!flist.xbf_count && !flist.xbf_first);
6142next_block:
6143 start_fsb++;
6144 remaining--;
6145 } while(remaining > 0);
6146
6147 return error;
6148}