46
46
#if defined(HAVE_HOST_BLOCK_DEVICE )
47
47
#include <paths.h>
48
48
#include <sys/param.h>
49
+ #include <sys/mount.h>
49
50
#include <IOKit/IOKitLib.h>
50
51
#include <IOKit/IOBSD.h>
51
52
#include <IOKit/storage/IOMediaBSDClient.h>
@@ -1254,6 +1255,15 @@ static void raw_refresh_limits(BlockDriverState *bs, Error **errp)
1254
1255
return ;
1255
1256
}
1256
1257
1258
+ #if defined(__APPLE__ ) && (__MACH__ )
1259
+ struct statfs buf ;
1260
+
1261
+ if (!fstatfs (s -> fd , & buf )) {
1262
+ bs -> bl .opt_transfer = buf .f_iosize ;
1263
+ bs -> bl .pdiscard_alignment = buf .f_bsize ;
1264
+ }
1265
+ #endif
1266
+
1257
1267
if (bs -> sg || S_ISBLK (st .st_mode )) {
1258
1268
int ret = hdev_get_max_hw_transfer (s -> fd , & st );
1259
1269
@@ -1591,6 +1601,7 @@ static int handle_aiocb_rw(void *opaque)
1591
1601
}
1592
1602
}
1593
1603
1604
+ #if defined(CONFIG_FALLOCATE ) || defined(BLKZEROOUT ) || defined(BLKDISCARD )
1594
1605
static int translate_err (int err )
1595
1606
{
1596
1607
if (err == - ENODEV || err == - ENOSYS || err == - EOPNOTSUPP ||
@@ -1599,6 +1610,7 @@ static int translate_err(int err)
1599
1610
}
1600
1611
return err ;
1601
1612
}
1613
+ #endif
1602
1614
1603
1615
#ifdef CONFIG_FALLOCATE
1604
1616
static int do_fallocate (int fd , int mode , off_t offset , off_t len )
@@ -1811,16 +1823,27 @@ static int handle_aiocb_discard(void *opaque)
1811
1823
}
1812
1824
} while (errno == EINTR );
1813
1825
1814
- ret = - errno ;
1826
+ ret = translate_err ( - errno ) ;
1815
1827
#endif
1816
1828
} else {
1817
1829
#ifdef CONFIG_FALLOCATE_PUNCH_HOLE
1818
1830
ret = do_fallocate (s -> fd , FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE ,
1819
1831
aiocb -> aio_offset , aiocb -> aio_nbytes );
1832
+ ret = translate_err (- errno );
1833
+ #elif defined(__APPLE__ ) && (__MACH__ )
1834
+ fpunchhole_t fpunchhole ;
1835
+ fpunchhole .fp_flags = 0 ;
1836
+ fpunchhole .reserved = 0 ;
1837
+ fpunchhole .fp_offset = aiocb -> aio_offset ;
1838
+ fpunchhole .fp_length = aiocb -> aio_nbytes ;
1839
+ if (fcntl (s -> fd , F_PUNCHHOLE , & fpunchhole ) == -1 ) {
1840
+ ret = errno == ENODEV ? - ENOTSUP : - errno ;
1841
+ } else {
1842
+ ret = 0 ;
1843
+ }
1820
1844
#endif
1821
1845
}
1822
1846
1823
- ret = translate_err (ret );
1824
1847
if (ret == - ENOTSUP ) {
1825
1848
s -> has_discard = false;
1826
1849
}
0 commit comments