Index: usr.bin/Makefile =================================================================== --- usr.bin/Makefile +++ usr.bin/Makefile @@ -43,6 +43,7 @@ find \ fmt \ fold \ + fspacectl \ fstat \ fsync \ gcore \ Index: usr.bin/fspacectl/Makefile =================================================================== --- /dev/null +++ usr.bin/fspacectl/Makefile @@ -0,0 +1,8 @@ +# $FreeBSD$ + +.include + +PROG= fspacectl +LIBADD= util + +.include Index: usr.bin/fspacectl/Makefile.depend =================================================================== --- /dev/null +++ usr.bin/fspacectl/Makefile.depend @@ -0,0 +1,20 @@ +# $FreeBSD$ +# Autogenerated - do NOT edit! + +DIRDEPS = \ + include \ + include/xlocale \ + lib/${CSU_DIR} \ + lib/libc \ + lib/libcompiler_rt \ + lib/libutil \ + usr/include/machine.amd64 \ + usr/include/sys.amd64 \ + usr/include/x86.amd64 \ + + +.include + +.if ${DEP_RELDIR} == ${_DEP_RELDIR} +# local dependencies - needed for -jN in clean tree +.endif Index: usr.bin/fspacectl/fspacectl.1 =================================================================== --- /dev/null +++ usr.bin/fspacectl/fspacectl.1 @@ -0,0 +1,107 @@ +.\" +.\" Copyright (c) 2021 The FreeBSD Foundation +.\" +.\" This manual page was written by Ka Ho Ng +.\" under sponsorship from the FreeBSD Foundation. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.\" $FreeBSD$ +.\" +.Dd Aug 16, 2021 +.Dt FSPACECTL 1 +.Os +.Sh NAME +.Nm fspacectl +.Nd perform space management in a file +.Sh SYNOPSIS +.Nm +.Fl d +.Bk -words +.Oo +.Fl o Xo +.Sm off +.Ar offset +.Op Cm K | k | M | m | G | g | T | t +.Sm on +.Xc +.Oc +.Fl l Xo +.Sm off +.Ar length +.Op Cm K | k | M | m | G | g | T | t +.Sm on +.Xc +.Ek +.Ar file +.Sh DESCRIPTION +The +.Nm +utility performs space management with the given offset and the length over a regular file given on the command-line. +.Pp +The following operations are supported and must be one of: +.Bl -tag -width indent +.It Fl d +Zero a region in the specified file. +If the underlying file system of the given file supports hole-punching, +file system space deallocation may be performed in the operation region. +.El +.Pp +The following options are available: +.Bl -tag -width indent +.It Fl o Ar offset +The space management operation is performed at the given +.Ar offset +bytes in the file. +If this option is not specified, the operation is performed at the beginning of the file. +.It Fl l Ar length +The length of the operation range in bytes. This option must always be specified. +.El +.Pp +The +.Ar offset +and +.Ar length +arguments may be suffixed with one of +.Cm K , +.Cm M , +.Cm G +or +.Cm T +(either upper or lower case) to indicate a multiple of +Kilobytes, Megabytes, Gigabytes or Terabytes +respectively. +.Sh SEE ALSO +.Xr dd 1 , +.Xr truncate 1 , +.Xr fspacectl 2 , +.Xr fpathconf 2 +.Sh HISTORY +The +.Nm +utility first appeared in +.Fx 14 . +.Sh AUTHORS +The +.Nm +utility was written by +.An Ka Ho Ng Aq Mt khng@FreeBSD.org . Index: usr.bin/fspacectl/fspacectl.c =================================================================== --- /dev/null +++ usr.bin/fspacectl/fspacectl.c @@ -0,0 +1,116 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause-FreeBSD + * + * Copyright (c) 2021 The FreeBSD Foundation + * + * This software was developed by Ka Ho Ng + * under sponsorship from the FreeBSD Foundation. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + */ + +#include +__FBSDID("$FreeBSD$"); + +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +static void usage(void); + +int +main(int argc, char **argv) +{ + int fd; + bool do_dealloc; + int ch, error; + char *fname; + off_t offset, length; + uint64_t uol; + struct spacectl_range sr; + + fd = -1; + do_dealloc = false; + error = 0; + offset = 0; + length = -1; + while ((ch = getopt(argc, argv, "do:l:")) != -1) + switch (ch) { + case 'd': + do_dealloc = true; + break; + case 'o': + if (expand_number(optarg, &uol) == -1 || + (off_t)uol < 0) + errx(EXIT_FAILURE, + "invalid offset argument `%s'", optarg); + + offset = uol; + break; + case 'l': + if (expand_number(optarg, &uol) == -1 || + (off_t)uol < 0) + errx(EXIT_FAILURE, + "invalid length argument `%s'", optarg); + + length = uol; + break; + default: + usage(); + } + + argv += optind; + argc -= optind; + + if (do_dealloc == false || length == -1 || argc < 1) + usage(); + + fname = *argv; + if ((fd = open(fname, O_WRONLY)) == -1) + errx(EXIT_FAILURE, "%s", fname); + + sr.r_len = length; + sr.r_offset = offset; + while ((error = fspacectl(fd, SPACECTL_DEALLOC, &sr, 0, &sr)) != -1 && + sr.r_len != 0); + if (error == -1) + warn("%s", fname); + close(fd); + + return error ? EXIT_FAILURE : EXIT_SUCCESS; +} + +static void +usage(void) +{ + fprintf(stderr, + "usage: fspacectl -d [-o offset[K|k|M|m|G|g|T|t]] -l length[K|k|M|m|G|g|T|t] file\n"); + exit(EXIT_FAILURE); +}