Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1jC5Im-0002TZ-AB for mharc-grub-devel@gnu.org; Wed, 11 Mar 2020 13:37:52 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:33364) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jC5Ii-0002Nk-OP for grub-devel@gnu.org; Wed, 11 Mar 2020 13:37:50 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jC5Ih-00007a-DC for grub-devel@gnu.org; Wed, 11 Mar 2020 13:37:48 -0400 Received: from mail-pl1-x642.google.com ([2607:f8b0:4864:20::642]:39429) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1jC5Ih-00007A-6y for grub-devel@gnu.org; Wed, 11 Mar 2020 13:37:47 -0400 Received: by mail-pl1-x642.google.com with SMTP id j20so1419470pll.6 for ; Wed, 11 Mar 2020 10:37:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=delphix.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=yTMT2+xCdOV+dVgqIn7Bd/bLApuyDldu9A9rDRKUOYE=; b=Su3qOxC78c70jDxpiMFJKmFZxljOptJE42BY24nFHxIIEPjeHy+yxt5uLV0sMi20YR TjoBXags1wL9sCtV9FGL9pCpdaUTfATmeV/oXkrkorfHbxuVvLsgOfRdyTOzCYdKuaSB I+BfWJIKIjhElBm9wxR/0fbVi+083j6axeEJI= 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:mime-version:content-transfer-encoding; bh=yTMT2+xCdOV+dVgqIn7Bd/bLApuyDldu9A9rDRKUOYE=; b=W37eTWYP7WQw80gbuQNMNOb3hgFIVHLOVuMUdySKlLEwYzEfNf0VAV+qZQtgjKaljy otgwy8tRFZEl2BsKTRX97sObjoxfCZ6ztyCxTGBlVteJcj0a5YfuprMPlcKSjKQAXyvV wsU0+3qApLJjvQZn4tnj7TJTQJkb8GR3l6+Cgp3dbfTXnYU59b0fxLTo10VsAGPvApCo xxLiPgy2H7t63cdeIXYmzxstV9jL/ze305odXnkrmQ3fFDFS8dbdACJ0cviwIrwFLARe 8c+c+QxP5djL6ambXMkqg2klpoLdXIcfMwUSfQgeSi8RrjStsqHN2fzwkkFNbEizMADr 7zJw== X-Gm-Message-State: ANhLgQ1oHpVdC48Z4FbK5ch2dNMi0kTdFFCoGYAdl5My/UtSUD9jJLRm 05F33bmqrloLlE3FDLJzqRyoe8o0kALFwVbdTGsQ/iFevkz8iybAgUiQDzYwo++2yKqdJS5y4Za wEcUd/+RQYyUklpZ0snyrpFtX0JbtP6UkNg/f/03TQrgwhVFEtiYrHA== X-Google-Smtp-Source: ADFU+vsGpiu1Vl4M3TWMK1cVe3PSYGmrrkRFcwVarE8/CTlnJ0z+eBbuvvuYjht/8b+ska81msgHqA== X-Received: by 2002:a17:902:6bcb:: with SMTP id m11mr4162554plt.10.1583948265730; Wed, 11 Mar 2020 10:37:45 -0700 (PDT) Received: from localhost.localdomain (c-73-162-153-225.hsd1.ca.comcast.net. [73.162.153.225]) by smtp.gmail.com with ESMTPSA id b3sm6342060pjq.38.2020.03.11.10.37.44 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 11 Mar 2020 10:37:45 -0700 (PDT) From: Paul Dagnelie To: grub-devel@gnu.org Cc: mahrens@delphix.com, behlendorf1@llnl.gov, george.wilson@delphix.com, sebastien.roy@delphix.com, Paul Dagnelie Subject: [PATCH 2/7] Add support for special envblk functions in GRUB Date: Wed, 11 Mar 2020 10:37:10 -0700 Message-Id: <20200311173715.22274-3-pcd@delphix.com> X-Mailer: git-send-email 2.19.0 In-Reply-To: <20200311173715.22274-1-pcd@delphix.com> References: <20200311173715.22274-1-pcd@delphix.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::642 X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 11 Mar 2020 17:37:50 -0000 This patch introducces the idea of envblk functions to GRUB. These functions are used to interact with a grubenv file stored not in the usual grub tree, but instead in a special filesystem-managed region that can more easily be accessed at boot time. These functions interface between calling code and the per-fs logic, an interface for which is also added as part of this patch. Signed-off-by: Paul Dagnelie --- grub-core/kern/file.c | 30 ++++++++++++++++++++++++++++-- include/grub/file.h | 10 ++++++++++ include/grub/fs.h | 12 ++++++++++++ 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/grub-core/kern/file.c b/grub-core/kern/file.c index 75eb5e2fa..f929f61a0 100644 --- a/grub-core/kern/file.c +++ b/grub-core/kern/file.c @@ -149,6 +149,27 @@ grub_file_open (const char *name, enum grub_file_type type) return 0; } +grub_file_t +grub_file_envblk_open (grub_fs_t fs, grub_device_t device, enum grub_file_type type) +{ + grub_file_t file = NULL; + file = (grub_file_t) grub_zalloc (sizeof (*file)); + if (! file) + return NULL; + file->device = device; + file->special = 1; + + if ((fs->fs_envblk_open) (file) != GRUB_ERR_NONE) { + grub_free(file); + return NULL; + } + + file->fs = fs; + grub_errno = GRUB_ERR_NONE; + + return grub_apply_file_filters(file, type, NULL); +} + grub_disk_read_hook_t grub_file_progress_hook; grub_ssize_t @@ -185,7 +206,10 @@ grub_file_read (grub_file_t file, void *buf, grub_size_t len) file->read_hook_data = file; file->progress_offset = file->offset; } - res = (file->fs->fs_read) (file, buf, len); + if (grub_file_envblk (file)) + res = (file->fs->fs_envblk_read) (file, buf, len); + else + res = (file->fs->fs_read) (file, buf, len); file->read_hook = read_hook; file->read_hook_data = read_hook_data; if (res > 0) @@ -197,7 +221,9 @@ grub_file_read (grub_file_t file, void *buf, grub_size_t len) grub_err_t grub_file_close (grub_file_t file) { - if (file->fs->fs_close) + if (grub_file_envblk (file) && file->fs->fs_envblk_close) + (file->fs->fs_envblk_close) (file); + else if (file->fs->fs_close) (file->fs->fs_close) (file); if (file->device) diff --git a/include/grub/file.h b/include/grub/file.h index 31567483c..1cc688f55 100644 --- a/include/grub/file.h +++ b/include/grub/file.h @@ -170,6 +170,9 @@ struct grub_file /* Caller-specific data passed to the read hook. */ void *read_hook_data; + + /* If the file is an FS's special file, which uses separate functions for interaction. */ + int special; }; typedef struct grub_file *grub_file_t; @@ -211,6 +214,7 @@ grub_ssize_t EXPORT_FUNC(grub_file_read) (grub_file_t file, void *buf, grub_size_t len); grub_off_t EXPORT_FUNC(grub_file_seek) (grub_file_t file, grub_off_t offset); grub_err_t EXPORT_FUNC(grub_file_close) (grub_file_t file); +grub_file_t EXPORT_FUNC(grub_file_envblk_open) (grub_fs_t fs, grub_device_t device, enum grub_file_type type); /* Return value of grub_file_size() in case file size is unknown. */ #define GRUB_FILE_SIZE_UNKNOWN 0xffffffffffffffffULL @@ -233,6 +237,12 @@ grub_file_seekable (const grub_file_t file) return !file->not_easily_seekable; } +static inline int +grub_file_envblk (const grub_file_t file) +{ + return file->special; +} + grub_file_t grub_file_offset_open (grub_file_t parent, enum grub_file_type type, grub_off_t start, grub_off_t size); diff --git a/include/grub/fs.h b/include/grub/fs.h index 302e48d4b..ec1a7a50e 100644 --- a/include/grub/fs.h +++ b/include/grub/fs.h @@ -96,6 +96,18 @@ struct grub_fs /* Whether blocklist installs have a chance to work. */ int blocklist_install; #endif + + /* + * The envblk functions are defined on filesystems that need to handle + * grub-writable files in a special way. This is most commonly the case for + * CoW filesystems like btrfs and ZFS. The normal read and close functions + * should detect that they are being called on a special file and act + * appropriately. + */ + grub_err_t (*fs_envblk_open) (struct grub_file *file); + grub_ssize_t (*fs_envblk_read) (struct grub_file *file, char *buf, grub_size_t len); + grub_err_t (*fs_envblk_write) (struct grub_file *file, char *buf, grub_size_t len); + grub_err_t (*fs_envblk_close) (struct grub_file *file); }; typedef struct grub_fs *grub_fs_t; -- 2.19.0