From owner-svn-src-head@FreeBSD.ORG Thu Dec 8 12:31:47 2011 Return-Path: Delivered-To: svn-src-head@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id B1E47106566B; Thu, 8 Dec 2011 12:31:47 +0000 (UTC) (envelope-from rmh@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c]) by mx1.freebsd.org (Postfix) with ESMTP id A1FB68FC19; Thu, 8 Dec 2011 12:31:47 +0000 (UTC) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.4/8.14.4) with ESMTP id pB8CVlU9086769; Thu, 8 Dec 2011 12:31:47 GMT (envelope-from rmh@svn.freebsd.org) Received: (from rmh@localhost) by svn.freebsd.org (8.14.4/8.14.4/Submit) id pB8CVlKf086767; Thu, 8 Dec 2011 12:31:47 GMT (envelope-from rmh@svn.freebsd.org) Message-Id: <201112081231.pB8CVlKf086767@svn.freebsd.org> From: Robert Millan Date: Thu, 8 Dec 2011 12:31:47 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Subject: svn commit: r228349 - head/lib/libufs X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 08 Dec 2011 12:31:47 -0000 Author: rmh Date: Thu Dec 8 12:31:47 2011 New Revision: 228349 URL: http://svn.freebsd.org/changeset/base/228349 Log: Make berase() work on platforms whose kernel lacks DIOCGDELETE ioctl. Approved by: kib (mentor) Modified: head/lib/libufs/block.c Modified: head/lib/libufs/block.c ============================================================================== --- head/lib/libufs/block.c Thu Dec 8 10:42:38 2011 (r228348) +++ head/lib/libufs/block.c Thu Dec 8 12:31:47 2011 (r228349) @@ -139,10 +139,56 @@ bwrite(struct uufsd *disk, ufs2_daddr_t return (cnt); } +#ifdef __FreeBSD_kernel__ + +static int +berase_helper(struct uufsd *disk, ufs2_daddr_t blockno, ufs2_daddr_t size) +{ + off_t ioarg[2]; + + ioarg[0] = blockno * disk->d_bsize; + ioarg[1] = size; + return (ioctl(disk->d_fd, DIOCGDELETE, ioarg)); +} + +#else + +static int +berase_helper(struct uufsd *disk, ufs2_daddr_t blockno, ufs2_daddr_t size) +{ + char *zero_chunk; + off_t offset, zero_chunk_size, pwrite_size; + int rv; + + offset = blockno * disk->d_bsize; + zero_chunk_size = 65536 * disk->d_bsize; + zero_chunk = calloc(1, zero_chunk_size); + if (zero_chunk == NULL) { + ERROR(disk, "failed to allocate memory"); + return (-1); + } + while (size > 0) { + pwrite_size = size; + if (pwrite_size > zero_chunk_size) + pwrite_size = zero_chunk_size; + rv = pwrite(disk->d_fd, zero_chunk, pwrite_size, offset); + if (rv == -1) { + ERROR(disk, "failed writing to disk"); + break; + } + size -= rv; + offset += rv; + rv = 0; + } + free(zero_chunk); + return (rv); +} + +#endif + int berase(struct uufsd *disk, ufs2_daddr_t blockno, ufs2_daddr_t size) { - off_t ioarg[2]; int rv; ERROR(disk, NULL); @@ -151,8 +197,5 @@ berase(struct uufsd *disk, ufs2_daddr_t 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); + return (berase_helper(disk, blockno, size)); }