Skip to content

Commit f97142c

Browse files
authored
A couple of small style cleanups
In `zpool_load_compat()`: * initialize `l_features[]` with a loop rather than a static initializer. * don't redefine system constants; use private names instead Rationale here: When an array is initialized using a static {foo}, only the specified members are initialized to the provided values, the rest are initialized to zero. While B_FALSE is of course zero, it feels unsafe to rely on this being true forever, so I'm inclined to sacrifice a few microseconds of runtime here and initialize using a loop. When looking for the correct combination of system constants to use (in open() and mmap()), I prefer to use private constants rather than redefining system ones; due to the small chance that the system ones might be referenced later in the file. So rather than defining O_PATH and MAP_POPULATE, I use distinct constant names. Reviewed-by: Brian Behlendorf <[email protected]> Reviewed-by: John Kennedy <[email protected]> Signed-off-by: Colm Buckley <[email protected]> Closes #12156
1 parent f645d44 commit f97142c

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

lib/libzfs/libzfs_pool.c

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4796,19 +4796,24 @@ zpool_load_compat(const char *compat, boolean_t *features, char *report,
47964796
* as they're only needed if the filename is relative
47974797
* which will be checked during the openat().
47984798
*/
4799-
#ifndef O_PATH
4800-
#define O_PATH O_RDONLY
4799+
4800+
/* O_PATH safer than O_RDONLY if system allows it */
4801+
#if defined(O_PATH)
4802+
#define ZC_DIR_FLAGS (O_DIRECTORY | O_CLOEXEC | O_PATH)
4803+
#else
4804+
#define ZC_DIR_FLAGS (O_DIRECTORY | O_CLOEXEC | O_RDONLY)
48014805
#endif
4802-
sdirfd = open(ZPOOL_SYSCONF_COMPAT_D, O_DIRECTORY | O_PATH | O_CLOEXEC);
4803-
ddirfd = open(ZPOOL_DATA_COMPAT_D, O_DIRECTORY | O_PATH | O_CLOEXEC);
4806+
4807+
sdirfd = open(ZPOOL_SYSCONF_COMPAT_D, ZC_DIR_FLAGS);
4808+
ddirfd = open(ZPOOL_DATA_COMPAT_D, ZC_DIR_FLAGS);
48044809

48054810
(void) strlcpy(l_compat, compat, ZFS_MAXPROPLEN);
48064811

48074812
for (file = strtok_r(l_compat, ",", &ps);
48084813
file != NULL;
48094814
file = strtok_r(NULL, ",", &ps)) {
48104815

4811-
boolean_t l_features[SPA_FEATURES] = {B_FALSE};
4816+
boolean_t l_features[SPA_FEATURES];
48124817

48134818
enum { Z_SYSCONF, Z_DATA } source;
48144819

@@ -4831,14 +4836,18 @@ zpool_load_compat(const char *compat, boolean_t *features, char *report,
48314836
continue;
48324837
}
48334838

4834-
#if !defined(MAP_POPULATE) && defined(MAP_PREFAULT_READ)
4835-
#define MAP_POPULATE MAP_PREFAULT_READ
4836-
#elif !defined(MAP_POPULATE)
4837-
#define MAP_POPULATE 0
4839+
/* Prefault the file if system allows */
4840+
#if defined(MAP_POPULATE)
4841+
#define ZC_MMAP_FLAGS (MAP_PRIVATE | MAP_POPULATE)
4842+
#elif defined(MAP_PREFAULT_READ)
4843+
#define ZC_MMAP_FLAGS (MAP_PRIVATE | MAP_PREFAULT_READ)
4844+
#else
4845+
#define ZC_MMAP_FLAGS (MAP_PRIVATE)
48384846
#endif
4847+
48394848
/* private mmap() so we can strtok safely */
4840-
fc = (char *)mmap(NULL, fs.st_size,
4841-
PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_POPULATE, featfd, 0);
4849+
fc = (char *)mmap(NULL, fs.st_size, PROT_READ | PROT_WRITE,
4850+
ZC_MMAP_FLAGS, featfd, 0);
48424851
(void) close(featfd);
48434852

48444853
/* map ok, and last character == newline? */
@@ -4852,7 +4861,10 @@ zpool_load_compat(const char *compat, boolean_t *features, char *report,
48524861

48534862
ret_nofiles = B_FALSE;
48544863

4855-
/* replace last char with NUL to ensure we have a delimiter */
4864+
for (uint_t i = 0; i < SPA_FEATURES; i++)
4865+
l_features[i] = B_FALSE;
4866+
4867+
/* replace final newline with NULL to ensure string ends */
48564868
fc[fs.st_size - 1] = '\0';
48574869

48584870
for (line = strtok_r(fc, "\n", &ls);

0 commit comments

Comments
 (0)