Received: from list by lists.gnu.org with archive (Exim 4.71) id 1U2JsX-0007aH-MC for mharc-grub-devel@gnu.org; Mon, 04 Feb 2013 06:06:25 -0500 Received: from eggs.gnu.org ([208.118.235.92]:57095) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1U2JsS-0007Za-5u for grub-devel@gnu.org; Mon, 04 Feb 2013 06:06:23 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1U2JsO-0008OL-Ec for grub-devel@gnu.org; Mon, 04 Feb 2013 06:06:20 -0500 Received: from mmserver.massimo-maggi.eu ([94.23.66.90]:34923 helo=mmserver.localdomain) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1U2JsO-0008OB-50 for grub-devel@gnu.org; Mon, 04 Feb 2013 06:06:16 -0500 Received: from [IPv6:2001:b05:f:ffff::25] (unknown [IPv6:2001:b05:f:ffff::25]) by mmserver.localdomain (Postfix) with ESMTPSA id C0B263C0 for ; Mon, 4 Feb 2013 12:06:14 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=massimo-maggi.eu; s=mmserver; t=1359975975; bh=kYEK2FBFmSJTq7wz3DAT8pc/Tsw3FrhsGSgyfa55zpk=; h=Date:From:To:Subject:References:In-Reply-To; b=QmpVREK18++rXqVC3xdxbOhy2vjXKKw8CSGA5f1yKtJqg4CJZ10TkBfgdzIyxNNEr P3jm+8dWL5mZBzp73rbqznMrMVeraP63wpTcWd/zNQXl1YlnYeQtm+Mxck7YLm0Hbq 4k2Lx4gPe+dgr6ZVbdFm8Nr88kErmIsco+qi6b2k= Message-ID: <510F96E7.2040404@massimo-maggi.eu> Date: Mon, 04 Feb 2013 12:09:27 +0100 From: Massimo Maggi User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130128 Thunderbird/17.0.2 MIME-Version: 1.0 To: grub-devel@gnu.org Subject: [PATCH 3/5] Refactoring of function nvlist_find_value References: In-Reply-To: X-Enigmail-Version: 1.5 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="----enig2HFGTIXOJOEIHOXDBMKVV" X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 94.23.66.90 X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list Reply-To: The development of GNU GRUB List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 04 Feb 2013 11:06:24 -0000 This is an OpenPGP/MIME signed message (RFC 4880 and 3156) ------enig2HFGTIXOJOEIHOXDBMKVV Content-Type: multipart/mixed; boundary="------------090205050601090903090104" This is a multi-part message in MIME format. --------------090205050601090903090104 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable It's needed to allow iteration of nvlists in other functions. Ported to GRUB2 from IllumOS changeset 13700 2889e2596bd6 Author: Christopher Siden Ported-by: Massimo Maggi --- grub-core/fs/zfs/zfs.c | 230 +++++++++++++++++++++++++++++++++---------------- 1 file changed, 158 insertions(+), 72 deletions(-) diff --git a/grub-core/fs/zfs/zfs.c b/grub-core/fs/zfs/zfs.c index 3ea9c95..7f76445 100644 --- a/grub-core/fs/zfs/zfs.c +++ b/grub-core/fs/zfs/zfs.c @@ -2,6 +2,7 @@ * GRUB -- GRand Unified Bootloader * Copyright (C) 1999,2000,2001,2002,2003,2004,2009,2010,2011 Free Software Foundation, Inc. * Copyright 2010 Sun Microsystems, Inc. + * Copyright (c) 2012 by Delphix. All rights reserved. * * GRUB is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by= @@ -768,6 +769,155 @@ fill_vdev_info (struct grub_zfs_data *data, } /* + * For a given XDR packed nvlist, verify the first 4 bytes and move on. + * + * An XDR packed nvlist is encoded as (comments from nvs_xdr_create) : + * + * encoding method/host endian (4 bytes) + * nvl_version (4 bytes) + * nvl_nvflag (4 bytes) + * encoded nvpairs: + * encoded size of the nvpair (4 bytes) + * decoded size of the nvpair (4 bytes) + * name string size (4 bytes) + * name string data (sizeof(NV_ALIGN4(string)) + * data type (4 bytes) + * # of elements in the nvpair (4 bytes) + * data + * 2 zero's for the last nvpair + * (end of the entire list) (8 bytes) + * + */ + +/* + * The nvlist_next_nvpair() function returns a handle to the next nvpair in the + * list following nvpair. If nvpair is NULL, the first pair is returned.= If + * nvpair is the last pair in the nvlist, NULL is returned. + */ +static const char * +nvlist_next_nvpair(const char *nvl, const char *nvpair) +{ + const char *nvp; + int encode_size; + int name_len; + if (nvl =3D=3D NULL) + return (NULL); + + if (nvpair =3D=3D NULL) { + /* skip over header, nvl_version and nvl_nvflag */ + nvpair =3D nvl + 4 * 3; + } else { + /* skip to the next nvpair */ + encode_size =3D grub_be_to_cpu32 (grub_get_unaligned32(nvpair)); + nvpair +=3D encode_size; + } + /* 8 bytes of 0 marks the end of the list */ + if (*(grub_uint64_t*)nvpair =3D=3D 0) + return (NULL); + /*consistency checks*/ + if (nvpair + 4 * 3 >=3D nvl + VDEV_PHYS_SIZE) + { + grub_dprintf ("zfs", "nvlist overflow\n"); + grub_error (GRUB_ERR_BAD_FS, "incorrect nvlist"); + return (NULL); + } + encode_size =3D grub_be_to_cpu32 (grub_get_unaligned32(nvpair)); + + nvp =3D nvpair + 4*2; + name_len =3D grub_be_to_cpu32 (grub_get_unaligned32 (nvp)); + nvp +=3D 4; + + nvp =3D nvp + ((name_len + 3) & ~3); // align + if (nvp + 4 >=3D nvl + VDEV_PHYS_SIZE + || encode_size < 0 + || nvp + 4 + encode_size > nvl + VDEV_PHYS_SIZE) + { + grub_dprintf ("zfs", "nvlist overflow\n"); + grub_error (GRUB_ERR_BAD_FS, "incorrect nvlist"); + return (NULL); + } + /* end consistency checks */ + + return (nvpair); +} +/* + * This function returns 0 on success and 1 on failure. On success, a string + * containing the name of nvpair is saved in buf. + */ +static int +nvpair_name(const char *nvp, char **buf, int* buflen) +{ + int len; + + /* skip over encode/decode size */ + nvp +=3D 4 * 2; + + len =3D grub_be_to_cpu32 (grub_get_unaligned32 (nvp)); + nvp=3Dnvp+4; +=09 + *buf=3D(char*)nvp; + *buflen=3Dlen; + + return (0); +} +/* + * This function retrieves the value of the nvpair in the form of enumerated + * type data_type_t. + */ +static int +nvpair_type(const char *nvp) +{ + int name_len, type; + + /* skip over encode/decode size */ + nvp +=3D 4 * 2; + + /* skip over name_len */ + name_len =3D grub_be_to_cpu32 (grub_get_unaligned32 (nvp)); + nvp +=3D 4; + + /* skip over name */ + nvp =3D nvp + ((name_len + 3) & ~3); /* align */ + + type =3D grub_be_to_cpu32 (grub_get_unaligned32 (nvp)); + + return (type); +} +static int +nvpair_value(const char *nvp,char **val, + grub_size_t *size_out, grub_size_t *nelm_out) +{ + int name_len,nelm,encode_size; + + /* skip over encode/decode size */ + encode_size =3D grub_be_to_cpu32 (grub_get_unaligned32(nvp)); + nvp +=3D 8; + + /* skip over name_len */ + name_len =3D grub_be_to_cpu32 (grub_get_unaligned32 (nvp)); + nvp +=3D 4; + + /* skip over name */ + nvp =3D nvp + ((name_len + 3) & ~3); /* align */ +=09 + /* skip over type */ + nvp +=3D 4; + nelm =3D grub_be_to_cpu32 (grub_get_unaligned32 (nvp)); + nvp +=3D4; + if (nelm < 1) + { + grub_error (GRUB_ERR_BAD_FS, "empty nvpair"); + return 0; + } + *val =3D (char *) nvp; + *size_out =3D encode_size; + if (nelm_out) + *nelm_out =3D nelm; + + return 1; +} + +/* * Check the disk label information and retrieve needed vdev name-value pairs. * */ @@ -3064,34 +3214,14 @@ dnode_get_fullpath (const char *fullpath, struct subvolume *subvol, return err; } -/* - * For a given XDR packed nvlist, verify the first 4 bytes and move on. - * - * An XDR packed nvlist is encoded as (comments from nvs_xdr_create) : - * - * encoding method/host endian (4 bytes) - * nvl_version (4 bytes) - * nvl_nvflag (4 bytes) - * encoded nvpairs: - * encoded size of the nvpair (4 bytes) - * decoded size of the nvpair (4 bytes) - * name string size (4 bytes) - * name string data (sizeof(NV_ALIGN4(string)) - * data type (4 bytes) - * # of elements in the nvpair (4 bytes) - * data - * 2 zero's for the last nvpair - * (end of the entire list) (8 bytes) - * - */ - static int nvlist_find_value (const char *nvlist_in, const char *name, int valtype, char **val, grub_size_t *size_out, grub_size_t *nelm_out) { - int name_len, type, encode_size; - const char *nvpair, *nvp_name, *nvlist =3D nvlist_in; + int name_len, type ; + const char *nvpair=3DNULL,*nvlist=3Dnvlist_in; + char *nvp_name; /* Verify if the 1st and 2nd byte in the nvlist are valid. */ /* NOTE: independently of what endianness header announces all @@ -3104,62 +3234,18 @@ nvlist_find_value (const char *nvlist_in, const char *name, return 0; } - /* skip the header, nvl_version, and nvl_nvflag */ - nvlist =3D nvlist + 4 * 3; /* * Loop thru the nvpair list * The XDR representation of an integer is in big-endian byte order. */ - while ((encode_size =3D grub_be_to_cpu32 (grub_get_unaligned32 (nvlist= )))) + while ((nvpair=3Dnvlist_next_nvpair(nvlist,nvpair))) { - int nelm; - - if (nvlist + 4 * 4 >=3D nvlist_in + VDEV_PHYS_SIZE) + nvpair_name(nvpair,&nvp_name,&name_len); + type =3D nvpair_type(nvpair); + if ((grub_strncmp (nvp_name, name, grub_strlen(name)) =3D=3D 0) &&= type =3D=3D valtype) { - grub_dprintf ("zfs", "nvlist overflow\n"); - grub_error (GRUB_ERR_BAD_FS, "incorrect nvlist"); - return 0; - } - - nvpair =3D nvlist + 4 * 2; /* skip the encode/decode size */ - - name_len =3D grub_be_to_cpu32 (grub_get_unaligned32 (nvpair)); - nvpair +=3D 4; - - nvp_name =3D nvpair; - nvpair =3D nvpair + ((name_len + 3) & ~3); /* align */ - - if (nvpair + 8 >=3D nvlist_in + VDEV_PHYS_SIZE - || encode_size < 0 - || nvpair + 8 + encode_size > nvlist_in + VDEV_PHYS_SIZE) - { - grub_dprintf ("zfs", "nvlist overflow\n"); - grub_error (GRUB_ERR_BAD_FS, "incorrect nvlist"); - return 0; + return nvpair_value(nvpair,val,size_out,nelm_out); } - - type =3D grub_be_to_cpu32 (grub_get_unaligned32 (nvpair)); - nvpair +=3D 4; - - nelm =3D grub_be_to_cpu32 (grub_get_unaligned32 (nvpair)); - if (nelm < 1) - { - grub_error (GRUB_ERR_BAD_FS, "empty nvpair"); - return 0; - } - - nvpair +=3D 4; - - if ((grub_strncmp (nvp_name, name, name_len) =3D=3D 0) && type =3D= =3D valtype) - { - *val =3D (char *) nvpair; - *size_out =3D encode_size; - if (nelm_out) - *nelm_out =3D nelm; - return 1; - } - - nvlist +=3D encode_size; /* goto the next nvpair */ } return 0; } --=20 1.8.1.1 --------------090205050601090903090104 Content-Type: text/plain; charset=ISO-8859-1; name="Attached Message Part" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="Attached Message Part" --------------090205050601090903090104-- ------enig2HFGTIXOJOEIHOXDBMKVV Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.19 (GNU/Linux) Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/ iQEcBAEBAgAGBQJRD5boAAoJEEO073X3hHDODPEH/RMG7EqPoa9NUFln7ixsoSCj eQyTdAwSNV0vm28p+CA0wWTpEFhNIiqIDWVYWSpI7cFstkQNEgoMZHarH8C0fd/9 Btc3iDYFaQNrnek6Am5agWcK7vXx8TmZNvxULdbg/K5HXM+vTiitIcrKifaW/s7f 1h1ywftFdexfcNHgBylwX2ndRzOlFpKqUp0duin+NnkCk95tCFja9YoEYgX0Y6aF d7AnhsjFLEO+YFvIOsAqj5F10+h42XRhzb4NOPBI8YbSLBGaTp9QndB/YEy2201I 1goTXSeIbwyN6ioOwtau+wflbrJJPeJoJaynbiGdWC3Qt3U7wCUkVAIxLWNGzck= =jrWo -----END PGP SIGNATURE----- ------enig2HFGTIXOJOEIHOXDBMKVV--