From f9bd6ffc2d69fbf7654c64c285fb1ad394e53d25 Mon Sep 17 00:00:00 2001 From: phk Date: Sun, 16 Dec 2007 18:02:37 +0000 Subject: [PATCH] Add a berase() function which uses ioctl(DIOCGDELETE) to erase a slab of the disk. --- lib/libufs/Makefile | 1 + lib/libufs/block.c | 19 +++++++++++++++++++ lib/libufs/bread.3 | 29 +++++++++++++++++++++-------- lib/libufs/libufs.h | 1 + 4 files changed, 42 insertions(+), 8 deletions(-) diff --git a/lib/libufs/Makefile b/lib/libufs/Makefile index 8d1d9acf206b..10726e4247e9 100644 --- a/lib/libufs/Makefile +++ b/lib/libufs/Makefile @@ -8,6 +8,7 @@ INCS= libufs.h MAN= bread.3 cgread.3 libufs.3 sbread.3 ufs_disk_close.3 MLINKS+= bread.3 bwrite.3 +MLINKS+= bread.3 berase.3 MLINKS+= cgread.3 cgread1.3 MLINKS+= cgread.3 cgwrite1.3 MLINKS+= sbread.3 sbwrite.3 diff --git a/lib/libufs/block.c b/lib/libufs/block.c index c4ff30482707..5450a5d57739 100644 --- a/lib/libufs/block.c +++ b/lib/libufs/block.c @@ -30,6 +30,7 @@ __FBSDID("$FreeBSD$"); #include #include +#include #include #include @@ -133,3 +134,21 @@ bwrite(struct uufsd *disk, ufs2_daddr_t blockno, const void *data, size_t size) return (cnt); } + +int +berase(struct uufsd *disk, ufs2_daddr_t blockno, ufs2_daddr_t size) +{ + off_t ioarg[2]; + int rv; + + ERROR(disk, NULL); + rv = ufs_disk_write(disk); + if (rv == -1) { + ERROR(disk, "failed to open disk for writing"); + return(rv); + } + ioarg[0] = blockno * disk->d_bsize; + ioarg[1] = size; + rv = ioctl(disk->d_fd, DIOCGDELETE, ioarg); + return (rv); +} diff --git a/lib/libufs/bread.3 b/lib/libufs/bread.3 index 80aa921f91e6..0a9b862c0ede 100644 --- a/lib/libufs/bread.3 +++ b/lib/libufs/bread.3 @@ -31,12 +31,17 @@ .Fa "struct uufsd *disk" "ufs2_daddr_t blockno" .Fa "const void *data" "size_t size" .Fc +.Ft int +.Fo berase +.Fa "struct uufsd *disk" "ufs2_daddr_t blockno" "ufs2_daddr_t size" +.Fc .Sh DESCRIPTION The -.Fn bread -and +.Fn bread , .Fn bwrite -functions provide a block read and write API for +and +.Fn berase +functions provide a block read, write and erase API for .Xr libufs 3 consumers. They operate on a userland UFS disk structure, and perform the read @@ -50,6 +55,10 @@ and .Fn bwrite functions return the amount read or written, or \-1 in case of any error, including short read. +.Pp +The +.Fn berase +function returns non-zero on error. .Sh ERRORS The function .Fn bread @@ -59,10 +68,6 @@ for any of the errors specified for the library functions .Xr ufs_disk_write 3 or .Xr pread 2 . -Additionally, it may follow the -.Xr libufs 3 -error methodologies in situations where the amount of data read -is not equal to the amount requested, or in case of device error. .Pp The function .Fn bwrite @@ -70,7 +75,15 @@ may fail and set .Va errno for any of the errors specified for the library function .Xr pwrite 2 . -Additionally, it may follow the +.Pp +The function +.Fn berase +may fail and set +.Va errno +for any of the errors specified for the library function +.Xr ioctl 2 . +.Pp +Additionally all three functions may follow the .Xr libufs 3 error methodologies in situations where the amount of data written is not equal to the amount requested, or in case of a device error. diff --git a/lib/libufs/libufs.h b/lib/libufs/libufs.h index 92ab3482a2f7..42a64f76e2b0 100644 --- a/lib/libufs/libufs.h +++ b/lib/libufs/libufs.h @@ -104,6 +104,7 @@ __BEGIN_DECLS */ ssize_t bread(struct uufsd *, ufs2_daddr_t, void *, size_t); ssize_t bwrite(struct uufsd *, ufs2_daddr_t, const void *, size_t); +int berase(struct uufsd *, ufs2_daddr_t, ufs2_daddr_t); /* * cgroup.c