From owner-svn-src-all@freebsd.org Tue Aug 18 14:10:06 2015 Return-Path: Delivered-To: svn-src-all@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 84CFC9BC570; Tue, 18 Aug 2015 14:10:06 +0000 (UTC) (envelope-from avg@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2001:1900:2254:2068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 7429B2C3; Tue, 18 Aug 2015 14:10:06 +0000 (UTC) (envelope-from avg@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.70]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id t7IEA66Z052256; Tue, 18 Aug 2015 14:10:06 GMT (envelope-from avg@FreeBSD.org) Received: (from avg@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id t7IEA6hU052254; Tue, 18 Aug 2015 14:10:06 GMT (envelope-from avg@FreeBSD.org) Message-Id: <201508181410.t7IEA6hU052254@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: avg set sender to avg@FreeBSD.org using -f From: Andriy Gapon Date: Tue, 18 Aug 2015 14:10:06 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-vendor@freebsd.org Subject: svn commit: r286889 - vendor-sys/illumos/dist/uts/common/fs/zfs vendor-sys/illumos/dist/uts/common/fs/zfs/sys vendor/illumos/dist/lib/libzfs/common X-SVN-Group: vendor MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 18 Aug 2015 14:10:06 -0000 Author: avg Date: Tue Aug 18 14:10:04 2015 New Revision: 286889 URL: https://svnweb.freebsd.org/changeset/base/286889 Log: 5692 expose the number of hole blocks in a file illumos/illumos-gate@2bcf0248e992f292c7b814458bcdce2f004925d6 https://www.illumos.org/issues/5692 we would like to expose the number of hole (sparse) blocks in a file. this can be useful to for example if you want to fill in the holes with some data; knowing the number of holes in advances allows you to report progress on hole filling. We could use SEEK_HOLE to do that but it would be O(n) where n is the number of holes present in the file. Author: Max Grossman Reviewed by: Adam Leventhal Reviewed by: Matthew Ahrens Reviewed by: Boris Protopopov Approved by: Richard Lowe Modified: vendor/illumos/dist/lib/libzfs/common/libzfs.h vendor/illumos/dist/lib/libzfs/common/libzfs_util.c Changes in other areas also in this revision: Modified: vendor-sys/illumos/dist/uts/common/fs/zfs/dmu.c vendor-sys/illumos/dist/uts/common/fs/zfs/sys/dmu.h vendor-sys/illumos/dist/uts/common/fs/zfs/zfs_vnops.c Modified: vendor/illumos/dist/lib/libzfs/common/libzfs.h ============================================================================== --- vendor/illumos/dist/lib/libzfs/common/libzfs.h Tue Aug 18 13:16:23 2015 (r286888) +++ vendor/illumos/dist/lib/libzfs/common/libzfs.h Tue Aug 18 14:10:04 2015 (r286889) @@ -21,7 +21,7 @@ /* * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. - * Copyright (c) 2013 by Delphix. All rights reserved. + * Copyright (c) 2014 by Delphix. All rights reserved. * Copyright (c) 2012, Joyent, Inc. All rights reserved. * Copyright (c) 2013 Steven Hartland. All rights reserved. * Copyright 2013 Nexenta Systems, Inc. All rights reserved. @@ -764,6 +764,8 @@ extern boolean_t libzfs_fru_compare(libz extern boolean_t libzfs_fru_notself(libzfs_handle_t *, const char *); extern int zpool_fru_set(zpool_handle_t *, uint64_t, const char *); +extern int zfs_get_hole_count(const char *, uint64_t *, uint64_t *); + #ifdef __cplusplus } #endif Modified: vendor/illumos/dist/lib/libzfs/common/libzfs_util.c ============================================================================== --- vendor/illumos/dist/lib/libzfs/common/libzfs_util.c Tue Aug 18 13:16:23 2015 (r286888) +++ vendor/illumos/dist/lib/libzfs/common/libzfs_util.c Tue Aug 18 14:10:04 2015 (r286889) @@ -22,7 +22,7 @@ /* * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2013, Joyent, Inc. All rights reserved. - * Copyright (c) 2012 by Delphix. All rights reserved. + * Copyright (c) 2014 by Delphix. All rights reserved. */ /* @@ -39,6 +39,7 @@ #include #include #include +#include #include #include #include @@ -1499,3 +1500,49 @@ zprop_iter(zprop_func func, void *cb, bo { return (zprop_iter_common(func, cb, show_all, ordered, type)); } + +/* + * zfs_get_hole_count retrieves the number of holes (blocks which are + * zero-filled) in the specified file using the _FIO_COUNT_FILLED ioctl. It + * also optionally fetches the block size when bs is non-NULL. With hole count + * and block size the full space consumed by the holes of a file can be + * calculated. + * + * On success, zero is returned, the count argument is set to the + * number of holes, and the bs argument is set to the block size (if it is + * not NULL). On error, a non-zero errno is returned and the values in count + * and bs are undefined. + */ +int +zfs_get_hole_count(const char *path, uint64_t *count, uint64_t *bs) { + int fd, err; + struct stat64 ss; + uint64_t fill; + + fd = open(path, O_RDONLY | O_LARGEFILE); + if (fd == -1) + return (errno); + + if (ioctl(fd, _FIO_COUNT_FILLED, &fill) == -1) { + err = errno; + (void) close(fd); + return (err); + } + + if (fstat64(fd, &ss) == -1) { + err = errno; + (void) close(fd); + return (err); + } + + *count = (ss.st_size + ss.st_blksize - 1) / ss.st_blksize - fill; + VERIFY3S(*count, >=, 0); + if (bs != NULL) { + *bs = ss.st_blksize; + } + + if (close(fd) == -1) { + return (errno); + } + return (0); +}