From patchwork Sat Dec 3 20:39:54 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zygo Blaxell X-Patchwork-Id: 9459789 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 708316071C for ; Sat, 3 Dec 2016 20:41:46 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 568F12831C for ; Sat, 3 Dec 2016 20:41:46 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 48C5E283D4; Sat, 3 Dec 2016 20:41:46 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C8C922831C for ; Sat, 3 Dec 2016 20:41:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751076AbcLCUkU (ORCPT ); Sat, 3 Dec 2016 15:40:20 -0500 Received: from startkeylogger.hungrycats.org ([207.192.69.118]:52852 "EHLO neville.hungrycats.org" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1750916AbcLCUkU (ORCPT ); Sat, 3 Dec 2016 15:40:20 -0500 X-Envelope-Mail-From: zblaxell@waya.furryterror.org X-Envelope-Mail-From: zblaxell@waya.furryterror.org Received: from waya.furryterror.org (waya.vpn7.hungrycats.org [10.132.226.63]) by neville.hungrycats.org (Postfix) with ESMTP id 6A2566078C; Sat, 3 Dec 2016 15:40:06 -0500 (EST) Received: from zblaxell by waya.furryterror.org with local (Exim 4.84_2) (envelope-from ) id 1cDH6H-0001kj-F9; Sat, 03 Dec 2016 15:40:01 -0500 From: Zygo Blaxell To: linux-btrfs@vger.kernel.org Subject: [PATCH v2] btrfs-progs: utils: negative numbers are more plausible than sizes over 8 EiB Date: Sat, 3 Dec 2016 15:39:54 -0500 Message-Id: <1480797594-2337-1-git-send-email-ce3g8jdj@umail.furryterror.org> X-Mailer: git-send-email 2.1.4 Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP I got tired of seeing "16.00EiB" whenever btrfs-progs encounters a negative size value, e.g. during resize: Unallocated: /dev/mapper/datamd18 16.00EiB This version is much more useful: Unallocated: /dev/mapper/datamd18 -26.29GiB Signed-off-by: Zygo Blaxell Reviewed-by: Omar Sandoval --- v2: change the function prototype so it's easier to see that the mangling implied by the name "pretty" includes "reinterpretation of the u64 value as a signed quantity." --- utils.c | 12 ++++++------ utils.h | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/utils.c b/utils.c index 69b580a..07e8443 100644 --- a/utils.c +++ b/utils.c @@ -2575,7 +2575,7 @@ out: * Note: this function uses a static per-thread buffer. Do not call this * function more than 10 times within one argument list! */ -const char *pretty_size_mode(u64 size, unsigned mode) +const char *pretty_size_mode(s64 size, unsigned mode) { static __thread int ps_index = 0; static __thread char ps_array[10][32]; @@ -2594,20 +2594,20 @@ static const char* unit_suffix_binary[] = static const char* unit_suffix_decimal[] = { "B", "kB", "MB", "GB", "TB", "PB", "EB"}; -int pretty_size_snprintf(u64 size, char *str, size_t str_size, unsigned unit_mode) +int pretty_size_snprintf(s64 size, char *str, size_t str_size, unsigned unit_mode) { int num_divs; float fraction; - u64 base = 0; + s64 base = 0; int mult = 0; const char** suffix = NULL; - u64 last_size; + s64 last_size; if (str_size == 0) return 0; if ((unit_mode & ~UNITS_MODE_MASK) == UNITS_RAW) { - snprintf(str, str_size, "%llu", size); + snprintf(str, str_size, "%lld", size); return 0; } @@ -2642,7 +2642,7 @@ int pretty_size_snprintf(u64 size, char *str, size_t str_size, unsigned unit_mod num_divs = 0; break; default: - while (size >= mult) { + while ((size < 0 ? -size : size) >= mult) { last_size = size; size /= mult; num_divs++; diff --git a/utils.h b/utils.h index 366ca29..525bde9 100644 --- a/utils.h +++ b/utils.h @@ -174,9 +174,9 @@ int check_mounted_where(int fd, const char *file, char *where, int size, int btrfs_device_already_in_root(struct btrfs_root *root, int fd, int super_offset); -int pretty_size_snprintf(u64 size, char *str, size_t str_bytes, unsigned unit_mode); +int pretty_size_snprintf(s64 size, char *str, size_t str_bytes, unsigned unit_mode); #define pretty_size(size) pretty_size_mode(size, UNITS_DEFAULT) -const char *pretty_size_mode(u64 size, unsigned mode); +const char *pretty_size_mode(s64 size, unsigned mode); u64 parse_size(char *s); u64 parse_qgroupid(const char *p);