@@ -568,98 +568,95 @@ dmu_prefetch(objset_t *os, uint64_t object, uint64_t offset, uint64_t len)
568
568
* the end so that the file gets shorter over time (if we crashes in the
569
569
* middle, this will leave us in a better state). We find allocated file
570
570
* data by simply searching the allocated level 1 indirects.
571
+ *
572
+ * On input, *start should be the first offset that does not need to be
573
+ * freed (e.g. "offset + length"). On return, *start will be the first
574
+ * offset that should be freed.
571
575
*/
572
576
static int
573
- get_next_chunk (dnode_t * dn , uint64_t * start , uint64_t limit )
577
+ get_next_chunk (dnode_t * dn , uint64_t * start , uint64_t minimum )
574
578
{
575
- uint64_t len = * start - limit ;
576
- uint64_t blkcnt = 0 ;
577
- uint64_t maxblks = DMU_MAX_ACCESS / (1ULL << (dn -> dn_indblkshift + 1 ));
579
+ uint64_t maxblks = DMU_MAX_ACCESS >> (dn -> dn_indblkshift + 1 );
580
+ /* bytes of data covered by a level-1 indirect block */
578
581
uint64_t iblkrange =
579
582
dn -> dn_datablksz * EPB (dn -> dn_indblkshift , SPA_BLKPTRSHIFT );
583
+ uint64_t blks ;
580
584
581
- ASSERT ( limit <= * start );
585
+ ASSERT3U ( minimum , <=, * start );
582
586
583
- if (len <= iblkrange * maxblks ) {
584
- * start = limit ;
587
+ if (* start - minimum <= iblkrange * maxblks ) {
588
+ * start = minimum ;
585
589
return (0 );
586
590
}
587
591
ASSERT (ISP2 (iblkrange ));
588
592
589
- while ( * start > limit && blkcnt < maxblks ) {
593
+ for ( blks = 0 ; * start > minimum && blks < maxblks ; blks ++ ) {
590
594
int err ;
591
595
592
- /* find next allocated L1 indirect */
596
+ /*
597
+ * dnode_next_offset(BACKWARDS) will find an allocated L1
598
+ * indirect block at or before the input offset. We must
599
+ * decrement *start so that it is at the end of the region
600
+ * to search.
601
+ */
602
+ (* start )-- ;
593
603
err = dnode_next_offset (dn ,
594
604
DNODE_FIND_BACKWARDS , start , 2 , 1 , 0 );
595
605
596
- /* if there are no more, then we are done */
606
+ /* if there are no indirect blocks before start, we are done */
597
607
if (err == ESRCH ) {
598
- * start = limit ;
599
- return ( 0 ) ;
600
- } else if (err ) {
608
+ * start = minimum ;
609
+ break ;
610
+ } else if (err != 0 ) {
601
611
return (err );
602
612
}
603
- blkcnt += 1 ;
604
613
605
- /* reset offset to end of "next" block back */
614
+ /* set start to the beginning of this L1 indirect */
606
615
* start = P2ALIGN (* start , iblkrange );
607
- if (* start <= limit )
608
- * start = limit ;
609
- else
610
- * start -= 1 ;
611
616
}
617
+ if (* start < minimum )
618
+ * start = minimum ;
612
619
return (0 );
613
620
}
614
621
615
622
static int
616
623
dmu_free_long_range_impl (objset_t * os , dnode_t * dn , uint64_t offset ,
617
- uint64_t length , boolean_t free_dnode )
624
+ uint64_t length )
618
625
{
619
- dmu_tx_t * tx ;
620
- uint64_t object_size , start , end , len ;
621
- boolean_t trunc = (length == DMU_OBJECT_END );
622
- int align , err ;
623
-
624
- align = 1 << dn -> dn_datablkshift ;
625
- ASSERT (align > 0 );
626
- object_size = align == 1 ? dn -> dn_datablksz :
627
- (dn -> dn_maxblkid + 1 ) << dn -> dn_datablkshift ;
628
-
629
- end = offset + length ;
630
- if (trunc || end > object_size )
631
- end = object_size ;
632
- if (end <= offset )
626
+ uint64_t object_size = (dn -> dn_maxblkid + 1 ) * dn -> dn_datablksz ;
627
+ int err ;
628
+
629
+ if (offset >= object_size )
633
630
return (0 );
634
- length = end - offset ;
635
631
636
- while (length ) {
637
- start = end ;
638
- /* assert(offset <= start) */
639
- err = get_next_chunk (dn , & start , offset );
632
+ if (length == DMU_OBJECT_END || offset + length > object_size )
633
+ length = object_size - offset ;
634
+
635
+ while (length != 0 ) {
636
+ uint64_t chunk_end , chunk_begin ;
637
+ dmu_tx_t * tx ;
638
+
639
+ chunk_end = chunk_begin = offset + length ;
640
+
641
+ /* move chunk_begin backwards to the beginning of this chunk */
642
+ err = get_next_chunk (dn , & chunk_begin , offset );
640
643
if (err )
641
644
return (err );
642
- len = trunc ? DMU_OBJECT_END : end - start ;
645
+ ASSERT3U (chunk_begin , >=, offset );
646
+ ASSERT3U (chunk_begin , <=, chunk_end );
643
647
644
648
tx = dmu_tx_create (os );
645
- dmu_tx_hold_free (tx , dn -> dn_object , start , len );
649
+ dmu_tx_hold_free (tx , dn -> dn_object ,
650
+ chunk_begin , chunk_end - chunk_begin );
646
651
err = dmu_tx_assign (tx , TXG_WAIT );
647
652
if (err ) {
648
653
dmu_tx_abort (tx );
649
654
return (err );
650
655
}
651
-
652
- dnode_free_range (dn , start , trunc ? -1 : len , tx );
653
-
654
- if (start == 0 && free_dnode ) {
655
- ASSERT (trunc );
656
- dnode_free (dn , tx );
657
- }
658
-
659
- length -= end - start ;
660
-
656
+ dnode_free_range (dn , chunk_begin , chunk_end - chunk_begin , tx );
661
657
dmu_tx_commit (tx );
662
- end = start ;
658
+
659
+ length -= chunk_end - chunk_begin ;
663
660
}
664
661
return (0 );
665
662
}
@@ -674,38 +671,32 @@ dmu_free_long_range(objset_t *os, uint64_t object,
674
671
err = dnode_hold (os , object , FTAG , & dn );
675
672
if (err != 0 )
676
673
return (err );
677
- err = dmu_free_long_range_impl (os , dn , offset , length , FALSE );
674
+ err = dmu_free_long_range_impl (os , dn , offset , length );
678
675
dnode_rele (dn , FTAG );
679
676
return (err );
680
677
}
681
678
682
679
int
683
- dmu_free_object (objset_t * os , uint64_t object )
680
+ dmu_free_long_object (objset_t * os , uint64_t object )
684
681
{
685
- dnode_t * dn ;
686
682
dmu_tx_t * tx ;
687
683
int err ;
688
684
689
- err = dnode_hold_impl (os , object , DNODE_MUST_BE_ALLOCATED ,
690
- FTAG , & dn );
685
+ err = dmu_free_long_range (os , object , 0 , DMU_OBJECT_END );
691
686
if (err != 0 )
692
687
return (err );
693
- if (dn -> dn_nlevels == 1 ) {
694
- tx = dmu_tx_create (os );
695
- dmu_tx_hold_bonus (tx , object );
696
- dmu_tx_hold_free (tx , dn -> dn_object , 0 , DMU_OBJECT_END );
697
- err = dmu_tx_assign (tx , TXG_WAIT );
698
- if (err == 0 ) {
699
- dnode_free_range (dn , 0 , DMU_OBJECT_END , tx );
700
- dnode_free (dn , tx );
701
- dmu_tx_commit (tx );
702
- } else {
703
- dmu_tx_abort (tx );
704
- }
688
+
689
+ tx = dmu_tx_create (os );
690
+ dmu_tx_hold_bonus (tx , object );
691
+ dmu_tx_hold_free (tx , object , 0 , DMU_OBJECT_END );
692
+ err = dmu_tx_assign (tx , TXG_WAIT );
693
+ if (err == 0 ) {
694
+ err = dmu_object_free (os , object , tx );
695
+ dmu_tx_commit (tx );
705
696
} else {
706
- err = dmu_free_long_range_impl ( os , dn , 0 , DMU_OBJECT_END , TRUE );
697
+ dmu_tx_abort ( tx );
707
698
}
708
- dnode_rele ( dn , FTAG );
699
+
709
700
return (err );
710
701
}
711
702
@@ -2042,7 +2033,7 @@ EXPORT_SYMBOL(dmu_buf_rele_array);
2042
2033
EXPORT_SYMBOL (dmu_prefetch );
2043
2034
EXPORT_SYMBOL (dmu_free_range );
2044
2035
EXPORT_SYMBOL (dmu_free_long_range );
2045
- EXPORT_SYMBOL (dmu_free_object );
2036
+ EXPORT_SYMBOL (dmu_free_long_object );
2046
2037
EXPORT_SYMBOL (dmu_read );
2047
2038
EXPORT_SYMBOL (dmu_write );
2048
2039
EXPORT_SYMBOL (dmu_prealloc );
0 commit comments