]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - sysfs.c
Create.c: fix uclibc build
[thirdparty/mdadm.git] / sysfs.c
diff --git a/sysfs.c b/sysfs.c
index decb02b8d80f08b2c7e7f3e409c64e094c283472..20fe1e9efaeda3c9de931a0a21d5c78751aee7ad 100644 (file)
--- a/sysfs.c
+++ b/sysfs.c
@@ -74,6 +74,29 @@ void sysfs_free(struct mdinfo *sra)
        }
 }
 
+/**
+ * sysfs_get_container_devnm() - extract container device name.
+ * @mdi: md_info describes member array, with GET_VERSION option.
+ * @buf: buf to fill, must be MD_NAME_MAX.
+ *
+ * External array version is in format {/,-}<container_devnm>/<array_index>
+ * Extract container_devnm from it and safe it in @buf.
+ */
+void sysfs_get_container_devnm(struct mdinfo *mdi, char *buf)
+{
+       char *p;
+
+       assert(is_subarray(mdi->text_version));
+
+       /* Skip first special sign */
+       snprintf(buf, MD_NAME_MAX, "%s", mdi->text_version + 1);
+
+       /* Remove array index */
+       p = strchr(buf, '/');
+       if (p)
+               *p = 0;
+}
+
 int sysfs_open(char *devnm, char *devname, char *attr)
 {
        char fname[MAX_SYSFS_PATH_LEN];
@@ -148,7 +171,7 @@ struct mdinfo *sysfs_read(int fd, char *devnm, unsigned long options)
                strcpy(base, "metadata_version");
                if (load_sys(fname, buf, sizeof(buf)))
                        goto abort;
-               if (strncmp(buf, "none", 4) == 0) {
+               if (str_is_none(buf) == true) {
                        sra->array.major_version =
                                sra->array.minor_version = -1;
                        strcpy(sra->text_version, "");
@@ -244,7 +267,7 @@ struct mdinfo *sysfs_read(int fd, char *devnm, unsigned long options)
                        goto abort;
                if (strncmp(buf, "file", 4) == 0)
                        sra->bitmap_offset = 1;
-               else if (strncmp(buf, "none", 4) == 0)
+               else if (str_is_none(buf) == true)
                        sra->bitmap_offset = 0;
                else if (buf[0] == '+')
                        sra->bitmap_offset = strtol(buf+1, NULL, 10);
@@ -655,7 +678,7 @@ int sysfs_set_safemode(struct mdinfo *sra, unsigned long ms)
        return sysfs_set_str(sra, NULL, "safe_mode_delay", delay);
 }
 
-int sysfs_set_array(struct mdinfo *info, int vers)
+int sysfs_set_array(struct mdinfo *info)
 {
        int rv = 0;
        char ver[100];
@@ -664,7 +687,7 @@ int sysfs_set_array(struct mdinfo *info, int vers)
        ver[0] = 0;
        if (info->array.major_version == -1 &&
            info->array.minor_version == -2) {
-               char buf[1024];
+               char buf[SYSFS_MAX_BUF_SIZE];
 
                strcat(strcpy(ver, "external:"), info->text_version);
 
@@ -675,13 +698,11 @@ int sysfs_set_array(struct mdinfo *info, int vers)
                 * version first, and preserve the flag
                 */
                if (sysfs_get_str(info, NULL, "metadata_version",
-                                 buf, 1024) > 0)
+                                 buf, sizeof(buf)) > 0)
                        if (strlen(buf) >= 9 && buf[9] == '-')
                                ver[9] = '-';
 
-               if ((vers % 100) < 2 ||
-                   sysfs_set_str(info, NULL, "metadata_version",
-                                 ver) < 0) {
+               if (sysfs_set_str(info, NULL, "metadata_version", ver) < 0) {
                        pr_err("This kernel does not support external metadata.\n");
                        return 1;
                }
@@ -900,11 +921,11 @@ int sysfs_freeze_array(struct mdinfo *sra)
         * return 0 if this kernel doesn't support 'frozen'
         * return 1 if it worked.
         */
-       char buf[20];
+       char buf[SYSFS_MAX_BUF_SIZE];
 
        if (!sysfs_attribute_available(sra, NULL, "sync_action"))
                return 1; /* no sync_action == frozen */
-       if (sysfs_get_str(sra, NULL, "sync_action", buf, 20) <= 0)
+       if (sysfs_get_str(sra, NULL, "sync_action", buf, sizeof(buf)) <= 0)
                return 0;
        if (strcmp(buf, "frozen\n") == 0)
                /* Already frozen */
@@ -1100,3 +1121,32 @@ void sysfsline(char *line)
        sr->next = sysfs_rules;
        sysfs_rules = sr;
 }
+
+/**
+ * sysfs_is_libata_allow_tpm_enabled() - check if libata allow_tmp is enabled.
+ * @verbose: verbose flag.
+ *
+ * Check if libata allow_tmp flag is set, this is required for SATA Opal Security commands to work.
+ *
+ * Return: true if allow_tpm enable, false otherwise.
+ */
+bool sysfs_is_libata_allow_tpm_enabled(const int verbose)
+{
+       const char *path = "/sys/module/libata/parameters/allow_tpm";
+       const char *expected_value = "1";
+       int fd = open(path, O_RDONLY);
+       char buf[3];
+
+       if (!is_fd_valid(fd)) {
+               pr_vrb("Failed open file descriptor to %s. Cannot check libata allow_tpm param.\n",
+                      path);
+               return false;
+       }
+
+       sysfs_fd_get_str(fd, buf, sizeof(buf));
+       close(fd);
+
+       if (strncmp(buf, expected_value, 1) == 0)
+               return true;
+       return false;
+}