From patchwork Thu Feb 15 19:05:01 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Omar Sandoval X-Patchwork-Id: 10223421 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 C2D47602CB for ; Thu, 15 Feb 2018 19:06:33 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B38A929485 for ; Thu, 15 Feb 2018 19:06:33 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A82072948F; Thu, 15 Feb 2018 19:06:33 +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.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID 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 3F61C29485 for ; Thu, 15 Feb 2018 19:06:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1161220AbeBOTGa (ORCPT ); Thu, 15 Feb 2018 14:06:30 -0500 Received: from mail-pg0-f66.google.com ([74.125.83.66]:40437 "EHLO mail-pg0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1161132AbeBOTFp (ORCPT ); Thu, 15 Feb 2018 14:05:45 -0500 Received: by mail-pg0-f66.google.com with SMTP id g2so499442pgn.7 for ; Thu, 15 Feb 2018 11:05:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=osandov-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :in-reply-to:references; bh=+KlNY/FzdWBh2bdWgoKVjlY82/tdcHDQu/8/xq1MwOk=; b=j2JjaFSxR3Y1/0/nV+KGpCGW/htnbces7riXaMWjjVwrgvwatIvaRo5cpdcVEKQ1qK DYH0N9LXgYhu4DSDWF8K9VkVlP4ilwRPNb5tH9nkzq399dRxPiZIs434MpigL/Bley9i lWRBhikIrW76o3qh1tbazQ4GTJwA7wd1T2P7NSpavb8b75HCzWskhHFdgcfR02Kdqf5h a/2NMSCxcHctkY6lrIoiYcsGBRC3gXb8LIzhuDn2rIHDYTiP237qzm9ljFRkm7Oqyj9h CG09FrGtRqSLdG3TV7nWReYeEhaGOUKJUNO86FSCSGu3ijWsWN6R0bUFiV8ozBwFCM0S MCJA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:in-reply-to:references; bh=+KlNY/FzdWBh2bdWgoKVjlY82/tdcHDQu/8/xq1MwOk=; b=pCrD0UxHbsT6fdIWDRV5BHv5hqSNT+9Pu/jgASxedA7vNFhGuk1AuvEnYYpT+OptGi Ih3YCn0bxRqlcmi8ME4l89de0ODPJnOnpZW3KFkMgOSOkBSFVptew/dOJxwKpyK7y+bj Efw09WksQGm9ch93CMOc1/Ff2zKAPK5rDDme7QO1VLsy/E6+OUSl2KAx51H3MjD2onlX GO75a9uaKcY7ssoD6VaCM20rcHo2+yol5hiu/BHvzm+QSM66NKltRKuy0NWFI9+qgLXt yyMLS1O45JYh+CjDVw3iS64ilXibV9bIMHFv44PEvtpxfpYVKg0y/nmMYOtStDj0howh aKDQ== X-Gm-Message-State: APf1xPBe2GSyHCZlL2ny+Zu2EMVa5XiNpJdUt6b3N0ZUnvMcMifhJ/Id yG9grhL5x3qUgo/uKXYqyI3qR9FGaUM= X-Google-Smtp-Source: AH8x2260QGRoCBXIAWDaEBLe97PCOekyXAoiRjnbfWvRdM/LUv3TTuEpJ7qN9lldYy4x1DKB3zJo/Q== X-Received: by 10.101.75.137 with SMTP id t9mr3113418pgq.232.1518721544316; Thu, 15 Feb 2018 11:05:44 -0800 (PST) Received: from vader.thefacebook.com ([2620:10d:c090:200::6:4a19]) by smtp.gmail.com with ESMTPSA id p1sm40467428pgr.44.2018.02.15.11.05.43 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 15 Feb 2018 11:05:43 -0800 (PST) From: Omar Sandoval To: linux-btrfs@vger.kernel.org Cc: kernel-team@fb.com Subject: [PATCH v2 16/27] btrfs-progs: use libbtrfsutil for read-only property Date: Thu, 15 Feb 2018 11:05:01 -0800 Message-Id: X-Mailer: git-send-email 2.16.1 In-Reply-To: References: In-Reply-To: References: 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 From: Omar Sandoval Signed-off-by: Omar Sandoval --- messages.h | 13 ++++++++++++ props.c | 69 +++++++++++++++++++++++--------------------------------------- 2 files changed, 38 insertions(+), 44 deletions(-) diff --git a/messages.h b/messages.h index 4999c7b9..004d5167 100644 --- a/messages.h +++ b/messages.h @@ -54,6 +54,19 @@ DO_ABORT_ON_ERROR; \ } while (0) +#define error_btrfs_util(err) \ + do { \ + const char *errno_str = strerror(errno); \ + const char *lib_str = btrfs_util_strerror(err) \ + PRINT_TRACE_ON_ERROR; \ + PRINT_VERBOSE_ERROR; \ + if (lib_str && strcmp(errno_str, lib_str) != 0) \ + __btrfs_error("%s: %s", lib_str, errno_str); \ + else \ + __btrfs_error("%s", errno_str); \ + DO_ABORT_ON_ERROR; \ + } while (0) + #define warning(fmt, ...) \ do { \ PRINT_TRACE_ON_ERROR; \ diff --git a/props.c b/props.c index cddbd927..e4edba06 100644 --- a/props.c +++ b/props.c @@ -21,6 +21,8 @@ #include #include +#include + #include "ctree.h" #include "commands.h" #include "utils.h" @@ -41,56 +43,35 @@ static int prop_read_only(enum prop_object_type type, const char *name, const char *value) { - int ret = 0; - int fd = -1; - u64 flags = 0; - - fd = open(object, O_RDONLY); - if (fd < 0) { - ret = -errno; - error("failed to open %s: %s", object, strerror(-ret)); - goto out; - } + enum btrfs_util_error err; + bool read_only; - ret = ioctl(fd, BTRFS_IOC_SUBVOL_GETFLAGS, &flags); - if (ret < 0) { - ret = -errno; - error("failed to get flags for %s: %s", object, - strerror(-ret)); - goto out; - } - - if (!value) { - if (flags & BTRFS_SUBVOL_RDONLY) - fprintf(stdout, "ro=true\n"); - else - fprintf(stdout, "ro=false\n"); - ret = 0; - goto out; - } + if (value) { + if (!strcmp(value, "true")) { + read_only = true; + } else if (!strcmp(value, "false")) { + read_only = false; + } else { + error("invalid value for property: %s", value); + return -EINVAL; + } - if (!strcmp(value, "true")) { - flags |= BTRFS_SUBVOL_RDONLY; - } else if (!strcmp(value, "false")) { - flags = flags & ~BTRFS_SUBVOL_RDONLY; + err = btrfs_util_set_subvolume_read_only(object, read_only); + if (err) { + error_btrfs_util(err); + return -errno; + } } else { - ret = -EINVAL; - error("invalid value for property: %s", value); - goto out; - } + err = btrfs_util_get_subvolume_read_only(object, &read_only); + if (err) { + error_btrfs_util(err); + return -errno; + } - ret = ioctl(fd, BTRFS_IOC_SUBVOL_SETFLAGS, &flags); - if (ret < 0) { - ret = -errno; - error("failed to set flags for %s: %s", object, - strerror(-ret)); - goto out; + printf("ro=%s\n", read_only ? "true" : "false"); } -out: - if (fd != -1) - close(fd); - return ret; + return 0; } static int prop_label(enum prop_object_type type,