]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - sysfs.c
mdadm.h: Introduced unaligned {get,put}_unaligned{16,32}()
[thirdparty/mdadm.git] / sysfs.c
diff --git a/sysfs.c b/sysfs.c
index c6df9b06cd7988ce9c44bc4b2c0dcefc6db3e97a..2dd9ab63b9ebfd382566866af965fd9ae648e163 100644 (file)
--- a/sysfs.c
+++ b/sysfs.c
@@ -78,7 +78,7 @@ int sysfs_open(char *devnm, char *devname, char *attr)
        return fd;
 }
 
-void sysfs_init_dev(struct mdinfo *mdi, unsigned long devid)
+void sysfs_init_dev(struct mdinfo *mdi, dev_t devid)
 {
        snprintf(mdi->sys_name,
                 sizeof(mdi->sys_name), "dev-%s", devid2kname(devid));
@@ -162,18 +162,12 @@ struct mdinfo *sysfs_read(int fd, char *devnm, unsigned long options)
                        goto abort;
                sra->array.layout = strtoul(buf, NULL, 0);
        }
-       if (options & GET_DISKS) {
+       if (options & (GET_DISKS|GET_STATE)) {
                strcpy(base, "raid_disks");
                if (load_sys(fname, buf, sizeof(buf)))
                        goto abort;
                sra->array.raid_disks = strtoul(buf, NULL, 0);
        }
-       if (options & GET_DEGRADED) {
-               strcpy(base, "degraded");
-               if (load_sys(fname, buf, sizeof(buf)))
-                       goto abort;
-               sra->array.failed_disks = strtoul(buf, NULL, 0);
-       }
        if (options & GET_COMPONENT) {
                strcpy(base, "component_size");
                if (load_sys(fname, buf, sizeof(buf)))
@@ -250,19 +244,15 @@ struct mdinfo *sysfs_read(int fd, char *devnm, unsigned long options)
                if (load_sys(fname, buf, sizeof(buf)))
                        goto abort;
                sra->array_state = map_name(sysfs_array_states, buf);
-               if (sra->array_state == UnSet)
-                       sra->array_state = ARRAY_UNKNOWN_STATE;
        }
 
        if (options & GET_CONSISTENCY_POLICY) {
                strcpy(base, "consistency_policy");
-               if (load_sys(fname, buf, sizeof(buf))) {
+               if (load_sys(fname, buf, sizeof(buf)))
                        sra->consistency_policy = CONSISTENCY_POLICY_UNKNOWN;
-               } else {
-                       sra->consistency_policy = map_name(consistency_policies, buf);
-                       if (sra->consistency_policy == UnSet)
-                               sra->consistency_policy = CONSISTENCY_POLICY_UNKNOWN;
-               }
+               else
+                       sra->consistency_policy = map_name(consistency_policies,
+                                                          buf);
        }
 
        if (! (options & GET_DEVS))
@@ -274,6 +264,9 @@ struct mdinfo *sysfs_read(int fd, char *devnm, unsigned long options)
        if (!dir)
                goto abort;
        sra->array.spare_disks = 0;
+       sra->array.active_disks = 0;
+       sra->array.failed_disks = 0;
+       sra->array.working_disks = 0;
 
        devp = &sra->devs;
        sra->devs = NULL;
@@ -314,23 +307,28 @@ struct mdinfo *sysfs_read(int fd, char *devnm, unsigned long options)
                dev->disk.raid_disk = strtoul(buf, &ep, 10);
                if (*ep) dev->disk.raid_disk = -1;
 
+               sra->array.nr_disks++;
                strcpy(dbase, "block/dev");
                if (load_sys(fname, buf, sizeof(buf))) {
                        /* assume this is a stale reference to a hot
                         * removed device
                         */
-                       free(dev);
-                       continue;
+                       if (!(options & GET_DEVS_ALL)) {
+                               free(dev);
+                               continue;
+                       }
+               } else {
+                       sscanf(buf, "%d:%d", &dev->disk.major, &dev->disk.minor);
                }
-               sra->array.nr_disks++;
-               sscanf(buf, "%d:%d", &dev->disk.major, &dev->disk.minor);
 
-               /* special case check for block devices that can go 'offline' */
-               strcpy(dbase, "block/device/state");
-               if (load_sys(fname, buf, sizeof(buf)) == 0 &&
-                   strncmp(buf, "offline", 7) == 0) {
-                       free(dev);
-                       continue;
+               if (!(options & GET_DEVS_ALL)) {
+                       /* special case check for block devices that can go 'offline' */
+                       strcpy(dbase, "block/device/state");
+                       if (load_sys(fname, buf, sizeof(buf)) == 0 &&
+                           strncmp(buf, "offline", 7) == 0) {
+                               free(dev);
+                               continue;
+                       }
                }
 
                /* finally add this disk to the array */
@@ -360,12 +358,17 @@ struct mdinfo *sysfs_read(int fd, char *devnm, unsigned long options)
                        strcpy(dbase, "state");
                        if (load_sys(fname, buf, sizeof(buf)))
                                goto abort;
-                       if (strstr(buf, "in_sync"))
-                               dev->disk.state |= (1<<MD_DISK_SYNC);
                        if (strstr(buf, "faulty"))
                                dev->disk.state |= (1<<MD_DISK_FAULTY);
-                       if (dev->disk.state == 0)
-                               sra->array.spare_disks++;
+                       else {
+                               sra->array.working_disks++;
+                               if (strstr(buf, "in_sync")) {
+                                       dev->disk.state |= (1<<MD_DISK_SYNC);
+                                       sra->array.active_disks++;
+                               }
+                               if (dev->disk.state == 0)
+                                       sra->array.spare_disks++;
+                       }
                }
                if (options & GET_ERROR) {
                        strcpy(buf, "errors");
@@ -374,6 +377,11 @@ struct mdinfo *sysfs_read(int fd, char *devnm, unsigned long options)
                        dev->errors = strtoul(buf, NULL, 0);
                }
        }
+
+       if ((options & GET_STATE) && sra->array.raid_disks)
+               sra->array.failed_disks = sra->array.raid_disks -
+                       sra->array.active_disks - sra->array.spare_disks;
+
        closedir(dir);
        return sra;
 
@@ -706,8 +714,8 @@ int sysfs_set_array(struct mdinfo *info, int vers)
                if (sysfs_set_str(info, NULL, "consistency_policy",
                                  map_num(consistency_policies,
                                          info->consistency_policy))) {
-                       pr_err("This kernel does not support PPL\n");
-                       return 1;
+                       pr_err("This kernel does not support PPL. Falling back to consistency-policy=resync.\n");
+                       info->consistency_policy = CONSISTENCY_POLICY_RESYNC;
                }
        }
 
@@ -742,7 +750,7 @@ int sysfs_add_disk(struct mdinfo *sra, struct mdinfo *sd, int resume)
        rv = sysfs_set_num(sra, sd, "offset", sd->data_offset);
        rv |= sysfs_set_num(sra, sd, "size", (sd->component_size+1) / 2);
        if (sra->array.level != LEVEL_CONTAINER) {
-               if (sd->consistency_policy == CONSISTENCY_POLICY_PPL) {
+               if (sra->consistency_policy == CONSISTENCY_POLICY_PPL) {
                        rv |= sysfs_set_num(sra, sd, "ppl_sector", sd->ppl_sector);
                        rv |= sysfs_set_num(sra, sd, "ppl_size", sd->ppl_size);
                }