LCOV - code coverage report
Current view: top level - cmdline - device.c (source / functions) Hit Total Coverage
Test: lcov.info Lines: 409 432 94.7 %
Date: 2026-03-15 15:58:19 Functions: 15 15 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (C) 2015 Andrea Mazzoleni
       3             :  *
       4             :  * This program is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 3 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * This program is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #include "portable.h"
      19             : 
      20             : #include "support.h"
      21             : #include "state.h"
      22             : #include "raid/raid.h"
      23             : 
      24       72715 : void device_name_set(devinfo_t* dev, const char* name, int index)
      25             : {
      26       72715 :         if (index == 0)
      27       29134 :                 pathcpy(dev->name, sizeof(dev->name), name);
      28             :         else
      29       43581 :                 pathprint(dev->name, sizeof(dev->name), "%s/%d", name, index);
      30       72715 : }
      31             : 
      32             : /**
      33             :  * The following are Failure Rate tables computed from the data that
      34             :  * BackBlaze made available at:
      35             :  *
      36             :  * Reliability Data Set For 41,000 Hard Drives Now Open Source
      37             :  * https://www.backblaze.com/blog/hard-drive-data-feb2015/
      38             :  *
      39             :  * Hard Drive Data Sets
      40             :  * https://www.backblaze.com/hard-drive-test-data.html
      41             :  *
      42             :  * Note that in this data:
      43             :  *  - Disks all passed the load-testing and have made it to production,
      44             :  *    and then Dead On Arrival (DOA) failures are excluded.
      45             :  *  - Disks that are predicted to fail by BackBlaze are removed before
      46             :  *    they really fail, not counting as a failure.
      47             :  *
      48             :  * The following tables are computed using the data from 2014-02-14 to
      49             :  * 2014-12-31 because it's the period when more SMART attributes were gathered.
      50             :  *
      51             :  * In this period there are 47322 disks seen, with 1988 removed because failed,
      52             :  * and with 4121 removed because predicted to fail.
      53             :  */
      54             : 
      55             : /**
      56             :  * Number of data points in each table.
      57             :  */
      58             : #define SMART_MEASURES 256
      59             : 
      60             : /*
      61             :  * Divider for SMART attribute 5
      62             :  */
      63             : static unsigned SMART_5_STEP = 1;
      64             : 
      65             : /*
      66             :  * Failure rate for 30 days, for a disk
      67             :  * with SMART attribute 5 at a given value.
      68             :  */
      69             : static double SMART_5_R[SMART_MEASURES] = {
      70             :         0.0026, 0.0748, 0.0919, 0.1013, 0.1079,
      71             :         0.1137, 0.1194, 0.1235, 0.1301, 0.1398,
      72             :         0.1453, 0.1490, 0.1528, 0.1566, 0.1595,
      73             :         0.1635, 0.1656, 0.1701, 0.1718, 0.1740,
      74             :         0.1762, 0.1787, 0.1808, 0.1833, 0.1858,
      75             :         0.1885, 0.1901, 0.1915, 0.1934, 0.1958,
      76             :         0.1975, 0.1993, 0.2014, 0.2048, 0.2068,
      77             :         0.2088, 0.2109, 0.2120, 0.2137, 0.2160,
      78             :         0.2173, 0.2214, 0.2226, 0.2237, 0.2262,
      79             :         0.2277, 0.2292, 0.2304, 0.2338, 0.2369,
      80             :         0.2381, 0.2396, 0.2411, 0.2427, 0.2445,
      81             :         0.2462, 0.2472, 0.2488, 0.2496, 0.2504,
      82             :         0.2514, 0.2525, 0.2535, 0.2544, 0.2554,
      83             :         0.2571, 0.2583, 0.2601, 0.2622, 0.2631,
      84             :         0.2635, 0.2644, 0.2659, 0.2675, 0.2682,
      85             :         0.2692, 0.2701, 0.2707, 0.2712, 0.2726,
      86             :         0.2745, 0.2767, 0.2778, 0.2784, 0.2800,
      87             :         0.2814, 0.2834, 0.2839, 0.2851, 0.2877,
      88             :         0.2883, 0.2891, 0.2900, 0.2907, 0.2916,
      89             :         0.2934, 0.2950, 0.2969, 0.2975, 0.2983,
      90             :         0.2999, 0.3006, 0.3013, 0.3021, 0.3033,
      91             :         0.3054, 0.3066, 0.3074, 0.3082, 0.3094,
      92             :         0.3106, 0.3112, 0.3120, 0.3137, 0.3141,
      93             :         0.3145, 0.3151, 0.3159, 0.3169, 0.3174,
      94             :         0.3181, 0.3194, 0.3215, 0.3219, 0.3231,
      95             :         0.3234, 0.3237, 0.3242, 0.3255, 0.3270,
      96             :         0.3283, 0.3286, 0.3289, 0.3304, 0.3315,
      97             :         0.3322, 0.3347, 0.3361, 0.3382, 0.3384,
      98             :         0.3395, 0.3398, 0.3401, 0.3405, 0.3411,
      99             :         0.3431, 0.3435, 0.3442, 0.3447, 0.3450,
     100             :         0.3455, 0.3464, 0.3472, 0.3486, 0.3497,
     101             :         0.3501, 0.3509, 0.3517, 0.3531, 0.3535,
     102             :         0.3540, 0.3565, 0.3569, 0.3576, 0.3579,
     103             :         0.3584, 0.3590, 0.3594, 0.3599, 0.3621,
     104             :         0.3627, 0.3642, 0.3649, 0.3655, 0.3658,
     105             :         0.3667, 0.3683, 0.3699, 0.3704, 0.3707,
     106             :         0.3711, 0.3715, 0.3718, 0.3721, 0.3727,
     107             :         0.3740, 0.3744, 0.3748, 0.3753, 0.3756,
     108             :         0.3761, 0.3766, 0.3775, 0.3794, 0.3801,
     109             :         0.3804, 0.3813, 0.3817, 0.3823, 0.3831,
     110             :         0.3847, 0.3875, 0.3881, 0.3886, 0.3890,
     111             :         0.3893, 0.3896, 0.3900, 0.3907, 0.3923,
     112             :         0.3925, 0.3933, 0.3936, 0.3961, 0.3971,
     113             :         0.3981, 0.3989, 0.4007, 0.4012, 0.4018,
     114             :         0.4023, 0.4027, 0.4041, 0.4048, 0.4056,
     115             :         0.4073, 0.4079, 0.4086, 0.4104, 0.4107,
     116             :         0.4109, 0.4112, 0.4118, 0.4133, 0.4139,
     117             :         0.4144, 0.4146, 0.4148, 0.4164, 0.4165,
     118             :         0.4174, 0.4191, 0.4197, 0.4201, 0.4204,
     119             :         0.4210, 0.4213, 0.4216, 0.4221, 0.4231,
     120             :         0.4235, 0.4237, 0.4239, 0.4241, 0.4244,
     121             :         0.4249,
     122             : };
     123             : 
     124             : /*
     125             :  * Divider for SMART attribute 187
     126             :  */
     127             : static unsigned SMART_187_STEP = 1;
     128             : 
     129             : /*
     130             :  * Failure rate for 30 days, for a disk
     131             :  * with SMART attribute 187 at a given value.
     132             :  */
     133             : static double SMART_187_R[SMART_MEASURES] = {
     134             :         0.0039, 0.1287, 0.1579, 0.1776, 0.1905,
     135             :         0.2013, 0.2226, 0.3263, 0.3612, 0.3869,
     136             :         0.4086, 0.4292, 0.4559, 0.5278, 0.5593,
     137             :         0.5847, 0.6124, 0.6345, 0.6517, 0.6995,
     138             :         0.7308, 0.7541, 0.7814, 0.8122, 0.8306,
     139             :         0.8839, 0.9100, 0.9505, 0.9906, 1.0254,
     140             :         1.0483, 1.1060, 1.1280, 1.1624, 1.1895,
     141             :         1.2138, 1.2452, 1.2864, 1.3120, 1.3369,
     142             :         1.3705, 1.3894, 1.4055, 1.4218, 1.4434,
     143             :         1.4670, 1.4834, 1.4993, 1.5174, 1.5400,
     144             :         1.5572, 1.5689, 1.5808, 1.6198, 1.6346,
     145             :         1.6405, 1.6570, 1.6618, 1.6755, 1.6877,
     146             :         1.7100, 1.7258, 1.7347, 1.7814, 1.7992,
     147             :         1.8126, 1.8225, 1.8269, 1.8341, 1.8463,
     148             :         1.8765, 1.8850, 1.9005, 1.9281, 1.9398,
     149             :         1.9618, 1.9702, 1.9905, 2.0099, 2.0480,
     150             :         2.0565, 2.0611, 2.0709, 2.0846, 2.0895,
     151             :         2.0958, 2.1008, 2.1055, 2.1097, 2.1235,
     152             :         2.1564, 2.1737, 2.1956, 2.1989, 2.2015,
     153             :         2.2148, 2.2355, 2.2769, 2.2940, 2.3045,
     154             :         2.3096, 2.3139, 2.3344, 2.3669, 2.3779,
     155             :         2.3941, 2.4036, 2.4396, 2.4473, 2.4525,
     156             :         2.4656, 2.4762, 2.4787, 2.5672, 2.5732,
     157             :         2.5755, 2.5794, 2.5886, 2.6100, 2.6144,
     158             :         2.6341, 2.6614, 2.6679, 2.6796, 2.6847,
     159             :         2.6872, 2.6910, 2.6934, 2.6995, 2.7110,
     160             :         2.7179, 2.7204, 2.7232, 2.7282, 2.7355,
     161             :         2.7375, 2.7422, 2.7558, 2.7580, 2.7643,
     162             :         2.7767, 2.7770, 2.8016, 2.9292, 2.9294,
     163             :         2.9337, 2.9364, 2.9409, 2.9436, 2.9457,
     164             :         2.9466, 2.9498, 2.9543, 2.9570, 2.9573,
     165             :         2.9663, 2.9708, 2.9833, 2.9859, 2.9895,
     166             :         2.9907, 2.9932, 2.9935, 3.0021, 3.0035,
     167             :         3.0079, 3.0103, 3.0126, 3.0151, 3.0266,
     168             :         3.0288, 3.0320, 3.0330, 3.0343, 3.0373,
     169             :         3.0387, 3.0438, 3.0570, 3.0579, 3.0616,
     170             :         3.0655, 3.0728, 3.0771, 3.0794, 3.0799,
     171             :         3.0812, 3.1769, 3.1805, 3.1819, 3.1860,
     172             :         3.1869, 3.2004, 3.2016, 3.2025, 3.2070,
     173             :         3.2129, 3.2173, 3.2205, 3.2254, 3.2263,
     174             :         3.2300, 3.2413, 3.2543, 3.2580, 3.2595,
     175             :         3.2611, 3.2624, 3.2787, 3.2798, 3.2809,
     176             :         3.2823, 3.2833, 3.2834, 3.2853, 3.2866,
     177             :         3.3332, 3.3580, 3.3595, 3.3625, 3.3631,
     178             :         3.3667, 3.3702, 3.3737, 3.3742, 3.3747,
     179             :         3.3769, 3.3775, 3.3791, 3.3809, 3.3813,
     180             :         3.3814, 3.3822, 3.3827, 3.3828, 3.3833,
     181             :         3.3833, 3.3843, 3.3882, 3.3963, 3.4047,
     182             :         3.4057, 3.4213, 3.4218, 3.4230, 3.4231,
     183             :         3.4240, 3.4262, 3.4283, 3.4283, 3.4288,
     184             :         3.4293, 3.4302, 3.4317, 3.4478, 3.4486,
     185             :         3.4520,
     186             : };
     187             : 
     188             : /*
     189             :  * Divider for SMART attribute 188
     190             :  */
     191             : static unsigned SMART_188_STEP = 1;
     192             : 
     193             : /*
     194             :  * Failure rate for 30 days, for a disk
     195             :  * with SMART attribute 188 at a given value.
     196             :  */
     197             : static double SMART_188_R[SMART_MEASURES] = {
     198             :         0.0025, 0.0129, 0.0182, 0.0215, 0.0236,
     199             :         0.0257, 0.0279, 0.0308, 0.0341, 0.0382,
     200             :         0.0430, 0.0491, 0.0565, 0.0658, 0.0770,
     201             :         0.0906, 0.1037, 0.1197, 0.1355, 0.1525,
     202             :         0.1686, 0.1864, 0.2011, 0.2157, 0.2281,
     203             :         0.2404, 0.2505, 0.2591, 0.2676, 0.2766,
     204             :         0.2827, 0.2913, 0.2999, 0.3100, 0.3185,
     205             :         0.3298, 0.3361, 0.3446, 0.3506, 0.3665,
     206             :         0.3699, 0.3820, 0.3890, 0.4059, 0.4108,
     207             :         0.4255, 0.4290, 0.4424, 0.4473, 0.4617,
     208             :         0.4667, 0.4770, 0.4829, 0.4977, 0.4997,
     209             :         0.5102, 0.5137, 0.5283, 0.5316, 0.5428,
     210             :         0.5480, 0.5597, 0.5634, 0.5791, 0.5826,
     211             :         0.5929, 0.5945, 0.6025, 0.6102, 0.6175,
     212             :         0.6245, 0.6313, 0.6421, 0.6468, 0.6497,
     213             :         0.6557, 0.6570, 0.6647, 0.6698, 0.6769,
     214             :         0.6849, 0.6884, 0.6925, 0.7025, 0.7073,
     215             :         0.7161, 0.7223, 0.7256, 0.7280, 0.7411,
     216             :         0.7445, 0.7530, 0.7628, 0.7755, 0.7900,
     217             :         0.8006, 0.8050, 0.8098, 0.8132, 0.8192,
     218             :         0.8230, 0.8293, 0.8356, 0.8440, 0.8491,
     219             :         0.8672, 0.8766, 0.8907, 0.8934, 0.8992,
     220             :         0.9062, 0.9111, 0.9209, 0.9290, 0.9329,
     221             :         0.9378, 0.9385, 0.9402, 0.9427, 0.9448,
     222             :         0.9459, 0.9568, 0.9626, 0.9628, 0.9730,
     223             :         0.9765, 0.9797, 0.9825, 0.9873, 0.9902,
     224             :         0.9926, 0.9991, 1.0031, 1.0044, 1.0062,
     225             :         1.0120, 1.0148, 1.0188, 1.0218, 1.0231,
     226             :         1.0249, 1.0277, 1.0335, 1.0355, 1.0417,
     227             :         1.0467, 1.0474, 1.0510, 1.0529, 1.0532,
     228             :         1.0562, 1.0610, 1.0702, 1.0708, 1.0800,
     229             :         1.0804, 1.0845, 1.1120, 1.1191, 1.1225,
     230             :         1.1264, 1.1265, 1.1335, 1.1347, 1.1479,
     231             :         1.1479, 1.1519, 1.1545, 1.1645, 1.1646,
     232             :         1.1647, 1.1649, 1.1678, 1.1713, 1.1723,
     233             :         1.1733, 1.1736, 1.1736, 1.1738, 1.1739,
     234             :         1.1739, 1.1741, 1.1741, 1.1746, 1.1746,
     235             :         1.1748, 1.1750, 1.1760, 1.1794, 1.1854,
     236             :         1.1908, 1.1912, 1.1912, 1.1971, 1.2033,
     237             :         1.2033, 1.2120, 1.2166, 1.2185, 1.2185,
     238             :         1.2189, 1.2211, 1.2226, 1.2234, 1.2320,
     239             :         1.2345, 1.2345, 1.2347, 1.2350, 1.2350,
     240             :         1.2407, 1.2408, 1.2408, 1.2408, 1.2409,
     241             :         1.2460, 1.2518, 1.2519, 1.2519, 1.2519,
     242             :         1.2520, 1.2520, 1.2521, 1.2521, 1.2521,
     243             :         1.2593, 1.2745, 1.2760, 1.2772, 1.2831,
     244             :         1.2833, 1.2890, 1.2906, 1.3166, 1.3201,
     245             :         1.3202, 1.3202, 1.3202, 1.3204, 1.3204,
     246             :         1.3314, 1.3422, 1.3423, 1.3441, 1.3491,
     247             :         1.3583, 1.3602, 1.3606, 1.3636, 1.3650,
     248             :         1.3661, 1.3703, 1.3708, 1.3716, 1.3730,
     249             :         1.3731,
     250             : };
     251             : 
     252             : /*
     253             :  * Divider for SMART attribute 197
     254             :  */
     255             : static unsigned SMART_197_STEP = 1;
     256             : 
     257             : /*
     258             :  * Failure rate for 30 days, for a disk
     259             :  * with SMART attribute 197 at a given value.
     260             :  */
     261             : static double SMART_197_R[SMART_MEASURES] = {
     262             :         0.0028, 0.2972, 0.3883, 0.4363, 0.4644,
     263             :         0.4813, 0.4948, 0.5051, 0.5499, 0.8535,
     264             :         0.8678, 0.8767, 0.8882, 0.8933, 0.9012,
     265             :         0.9076, 0.9368, 1.1946, 1.2000, 1.2110,
     266             :         1.2177, 1.2305, 1.2385, 1.2447, 1.2699,
     267             :         1.4713, 1.4771, 1.4802, 1.4887, 1.5292,
     268             :         1.5384, 1.5442, 1.5645, 1.7700, 1.7755,
     269             :         1.7778, 1.7899, 1.7912, 1.7991, 1.7998,
     270             :         1.8090, 1.9974, 1.9992, 2.0088, 2.0132,
     271             :         2.0146, 2.0161, 2.0171, 2.0273, 2.1845,
     272             :         2.1866, 2.1877, 2.1900, 2.1922, 2.1944,
     273             :         2.1974, 2.2091, 2.3432, 2.3459, 2.3463,
     274             :         2.3468, 2.3496, 2.3503, 2.3533, 2.3593,
     275             :         2.4604, 2.4606, 2.4609, 2.4612, 2.4620,
     276             :         2.4626, 2.4638, 2.4689, 2.5575, 2.5581,
     277             :         2.5586, 2.5586, 2.5588, 2.5602, 2.5602,
     278             :         2.5648, 2.6769, 2.6769, 2.6769, 2.6794,
     279             :         2.6805, 2.6811, 2.6814, 2.6862, 2.7742,
     280             :         2.7755, 2.7771, 2.7780, 2.7790, 2.7797,
     281             :         2.7807, 2.7871, 2.9466, 2.9478, 2.9492,
     282             :         2.9612, 2.9618, 2.9624, 2.9628, 2.9669,
     283             :         3.1467, 3.1481, 3.1494, 3.1499, 3.1504,
     284             :         3.1507, 3.1509, 3.1532, 3.2675, 3.2681,
     285             :         3.2703, 3.2712, 3.2714, 3.2726, 3.2726,
     286             :         3.2743, 3.3376, 3.3379, 3.3382, 3.3397,
     287             :         3.3403, 3.3410, 3.3410, 3.3429, 3.4052,
     288             :         3.4052, 3.4052, 3.4052, 3.4052, 3.4053,
     289             :         3.4053, 3.4075, 3.4616, 3.4616, 3.4616,
     290             :         3.4616, 3.4616, 3.4616, 3.4620, 3.4634,
     291             :         3.4975, 3.4975, 3.4975, 3.4975, 3.4979,
     292             :         3.4979, 3.4979, 3.4998, 3.5489, 3.5489,
     293             :         3.5489, 3.5489, 3.5489, 3.5493, 3.5497,
     294             :         3.5512, 3.5827, 3.5828, 3.5828, 3.5828,
     295             :         3.5828, 3.5828, 3.5828, 3.5844, 3.6251,
     296             :         3.6251, 3.6251, 3.6267, 3.6267, 3.6271,
     297             :         3.6271, 3.6279, 3.6562, 3.6562, 3.6563,
     298             :         3.7206, 3.7242, 3.7332, 3.7332, 3.7346,
     299             :         3.7548, 3.7548, 3.7553, 3.7576, 3.7581,
     300             :         3.7586, 3.7587, 3.7600, 3.7773, 3.7812,
     301             :         3.7836, 3.7841, 3.7842, 3.7851, 3.7856,
     302             :         3.7876, 3.8890, 3.8890, 3.8890, 3.8890,
     303             :         3.8890, 3.8890, 3.8890, 3.8897, 3.9111,
     304             :         3.9114, 3.9114, 3.9114, 3.9114, 3.9114,
     305             :         3.9114, 3.9126, 3.9440, 3.9440, 3.9440,
     306             :         3.9440, 3.9440, 3.9498, 3.9498, 3.9509,
     307             :         3.9783, 3.9783, 3.9784, 3.9784, 3.9784,
     308             :         3.9784, 4.0012, 4.0019, 4.0406, 4.0413,
     309             :         4.0413, 4.0413, 4.0413, 4.0414, 4.0414,
     310             :         4.0421, 4.0552, 4.0552, 4.0558, 4.0558,
     311             :         4.0558, 4.0558, 4.0558, 4.0563, 4.0753,
     312             :         4.0753, 4.0760, 4.1131, 4.1131, 4.1131,
     313             :         4.1131,
     314             : };
     315             : 
     316             : /*
     317             :  * Divider for SMART attribute 198
     318             :  */
     319             : static unsigned SMART_198_STEP = 1;
     320             : 
     321             : /*
     322             :  * Failure rate for 30 days, for a disk
     323             :  * with SMART attribute 198 at a given value.
     324             :  */
     325             : static double SMART_198_R[SMART_MEASURES] = {
     326             :         0.0030, 0.5479, 0.5807, 0.5949, 0.6046,
     327             :         0.6086, 0.6139, 0.6224, 0.6639, 1.0308,
     328             :         1.0329, 1.0364, 1.0371, 1.0387, 1.0399,
     329             :         1.0421, 1.0675, 1.3730, 1.3733, 1.3741,
     330             :         1.3741, 1.3752, 1.3794, 1.3800, 1.3985,
     331             :         1.6291, 1.6303, 1.6309, 1.6352, 1.6384,
     332             :         1.6448, 1.6464, 1.6645, 1.8949, 1.8951,
     333             :         1.8962, 1.9073, 1.9073, 1.9152, 1.9161,
     334             :         1.9240, 2.1308, 2.1315, 2.1328, 2.1328,
     335             :         2.1328, 2.1328, 2.1329, 2.1439, 2.3203,
     336             :         2.3205, 2.3205, 2.3205, 2.3205, 2.3205,
     337             :         2.3205, 2.3265, 2.4729, 2.4729, 2.4729,
     338             :         2.4729, 2.4729, 2.4729, 2.4729, 2.4778,
     339             :         2.5900, 2.5900, 2.5901, 2.5901, 2.5901,
     340             :         2.5901, 2.5901, 2.5949, 2.6964, 2.6965,
     341             :         2.6965, 2.6965, 2.6965, 2.6965, 2.6965,
     342             :         2.7010, 2.8328, 2.8328, 2.8328, 2.8329,
     343             :         2.8329, 2.8329, 2.8329, 2.8366, 2.9405,
     344             :         2.9405, 2.9405, 2.9405, 2.9405, 2.9405,
     345             :         2.9405, 2.9442, 3.1344, 3.1344, 3.1346,
     346             :         3.1463, 3.1463, 3.1463, 3.1463, 3.1493,
     347             :         3.3076, 3.3076, 3.3076, 3.3076, 3.3076,
     348             :         3.3077, 3.3077, 3.3097, 3.4456, 3.4456,
     349             :         3.4456, 3.4456, 3.4456, 3.4456, 3.4456,
     350             :         3.4473, 3.5236, 3.5236, 3.5236, 3.5236,
     351             :         3.5236, 3.5236, 3.5236, 3.5249, 3.6004,
     352             :         3.6004, 3.6004, 3.6004, 3.6004, 3.6004,
     353             :         3.6004, 3.6026, 3.6684, 3.6684, 3.6684,
     354             :         3.6684, 3.6684, 3.6684, 3.6684, 3.6697,
     355             :         3.7121, 3.7121, 3.7121, 3.7121, 3.7121,
     356             :         3.7121, 3.7121, 3.7136, 3.7744, 3.7744,
     357             :         3.7744, 3.7744, 3.7744, 3.7745, 3.7745,
     358             :         3.7756, 3.8151, 3.8151, 3.8151, 3.8151,
     359             :         3.8151, 3.8151, 3.8151, 3.8163, 3.8673,
     360             :         3.8673, 3.8673, 3.8673, 3.8673, 3.8673,
     361             :         3.8673, 3.8680, 3.9044, 3.9044, 3.9044,
     362             :         3.9044, 3.9044, 3.9044, 3.9044, 3.9056,
     363             :         3.9297, 3.9297, 3.9297, 3.9297, 3.9297,
     364             :         3.9297, 3.9297, 3.9305, 3.9494, 3.9494,
     365             :         3.9494, 3.9494, 3.9494, 3.9494, 3.9494,
     366             :         3.9514, 4.0725, 4.0725, 4.0725, 4.0725,
     367             :         4.0725, 4.0725, 4.0725, 4.0731, 4.0990,
     368             :         4.0993, 4.0993, 4.0993, 4.0993, 4.0993,
     369             :         4.0993, 4.1004, 4.1385, 4.1385, 4.1385,
     370             :         4.1386, 4.1386, 4.1387, 4.1387, 4.1398,
     371             :         4.1732, 4.2284, 4.2284, 4.2284, 4.2284,
     372             :         4.2284, 4.2284, 4.2290, 4.2781, 4.2781,
     373             :         4.2963, 4.2963, 4.2963, 4.2963, 4.2963,
     374             :         4.2971, 4.3141, 4.3141, 4.3141, 4.3141,
     375             :         4.3141, 4.3141, 4.3141, 4.3146, 4.3393,
     376             :         4.3393, 4.3393, 4.3393, 4.3393, 4.3393,
     377             :         4.3393,
     378             : };
     379             : 
     380             : /**
     381             :  * Computes the estimated Annual Failure Rate from the specified table.
     382             :  */
     383      350783 : static double smart_afr_value(double* tab, unsigned step, uint64_t value)
     384             : {
     385      350783 :         value /= step;
     386             : 
     387      350783 :         if (value >= SMART_MEASURES)
     388           0 :                 value = SMART_MEASURES - 1;
     389             : 
     390             :         /* table rates are for a month, so we scale to a year */
     391      350783 :         return 365.0 / 30.0 * tab[value];
     392             : }
     393             : 
     394             : /**
     395             :  * Computes the estimated Annual Failure Rate of a set of SMART attributes.
     396             :  *
     397             :  * We define the Annual Failure Rate as the average number of
     398             :  * failures you expect in a year from a disk slot:
     399             :  *
     400             :  *   AFR = 8760/MTBF (Mean Time Between Failures in hours).
     401             :  *
     402             :  * Note that this definition is different from the one given
     403             :  * by Seagate, that defines AFR = 1 - exp(-8760/MTBF), that
     404             :  * instead represents the probability of a failure in the next
     405             :  * year.
     406             :  *
     407             :  * To combine the different AFR from different SMART attributes,
     408             :  * we use the maximum rate reported, and we do not sum them,
     409             :  * because the attributes are not independent.
     410             :  */
     411       72621 : static double smart_afr(devinfo_t* devinfo)
     412             : {
     413       72621 :         double afr = 0;
     414       72621 :         uint64_t mask32 = 0xffffffffU;
     415       72621 :         uint64_t mask16 = 0xffffU;
     416       72621 :         uint64_t* info = devinfo->info;
     417       72621 :         struct smart_attr* smart = devinfo->smart;
     418             : 
     419             :         /* do not estimate for not rotational */
     420       72621 :         if (info[INFO_ROTATION_RATE] == 0)
     421           2 :                 return 0;
     422             : 
     423       72619 :         if (smart[5].raw != SMART_UNASSIGNED) {
     424       70188 :                 double r = smart_afr_value(SMART_5_R, SMART_5_STEP, smart[5].raw & mask32);
     425       70188 :                 if (afr < r)
     426       70188 :                         afr = r;
     427             :         }
     428             : 
     429       72619 :         if (smart[187].raw != SMART_UNASSIGNED) {
     430             :                 /* with some disks, only the lower 16 bits are significant */
     431             :                 /* See: http://web.archive.org/web/20130507072056/http://media.kingston.com/support/downloads/MKP_306_SMART_attribute.pdf */
     432       70155 :                 double r = smart_afr_value(SMART_187_R, SMART_187_STEP, smart[187].raw & mask16);
     433       70155 :                 if (afr < r)
     434       70155 :                         afr = r;
     435             :         }
     436             : 
     437       72619 :         if (
     438             :                 /**
     439             :                  * Don't check Command_Timeout (188) for Seagate disks.
     440             :                  *
     441             :                  * It's reported by users that for Archive SMR (Shingled Magnetic Recording)
     442             :                  * and IronWolf disks to be a not significant test as
     443             :                  * this value increases too often also on sane disks.
     444             :                  */
     445       72619 :                 strncmp(devinfo->model, "ST", 2) != 0 && smart[188].raw != SMART_UNASSIGNED
     446             :         ) {
     447             :                 /* with Seagate disks, there are three different 16 bits value reported */
     448             :                 /* the lowest one is the most significant */
     449       70149 :                 double r = smart_afr_value(SMART_188_R, SMART_188_STEP, smart[188].raw & mask16);
     450       70149 :                 if (afr < r)
     451           0 :                         afr = r;
     452             :         }
     453             : 
     454       72619 :         if (smart[197].raw != SMART_UNASSIGNED) {
     455       70103 :                 double r = smart_afr_value(SMART_197_R, SMART_197_STEP, smart[197].raw & mask32);
     456       70103 :                 if (afr < r)
     457          33 :                         afr = r;
     458             :         }
     459             : 
     460       72619 :         if (smart[198].raw != SMART_UNASSIGNED) {
     461       70188 :                 double r = smart_afr_value(SMART_198_R, SMART_198_STEP, smart[198].raw & mask32);
     462       70188 :                 if (afr < r)
     463          33 :                         afr = r;
     464             :         }
     465             : 
     466       72619 :         return afr;
     467             : }
     468             : 
     469             : /**
     470             :  * Calculates the probability of at least one failure occurring
     471             :  * within a year using a Poisson distribution.
     472             :  *
     473             :  * @param afr The aggregate Annual Failure Rate (lambda).
     474             :  * @return The probability as a double between 0 and 1.
     475             :  */
     476       70248 : static double poisson_prob_at_least_one_failure(double rate)
     477             : {
     478       70248 :         return 1.0 - exp(-rate);
     479             : }
     480             : 
     481             : /**
     482             :  * Probability of having data loss in a RAID system with the specified ::redundancy
     483             :  * supposing the specified ::array_failure_rate, and ::replace_rate.
     484             :  */
     485          18 : static double raid_prob_of_one_or_more_failures(double array_failure_rate, double replace_rate, unsigned n, unsigned redundancy)
     486             : {
     487             :         unsigned i;
     488             :         double MTBF;
     489             :         double MTTR;
     490             :         double MTTDL;
     491             :         double raid_failure_rate;
     492             : 
     493             :         /*
     494             :          * Use the MTTDL model (Mean Time To Data Loss) to estimate the
     495             :          * failure rate of the array.
     496             :          *
     497             :          * See:
     498             :          * Garth Alan Gibson, "Redundant Disk Arrays: Reliable, Parallel Secondary Storage", 1990
     499             :          */
     500             : 
     501             :         /* avoid division by zero */
     502          18 :         if (array_failure_rate == 0)
     503           0 :                 return 0;
     504             : 
     505             :         /* get the Mean Time Between Failure of a single disk */
     506             :         /* from the array failure rate */
     507          18 :         MTBF = n / array_failure_rate;
     508             : 
     509             :         /* get the Mean Time Between Repair (the time that a failed disk is replaced) */
     510             :         /* from the repair rate */
     511          18 :         MTTR = 1.0 / replace_rate;
     512             : 
     513             :         /* use the approximated MTTDL equation */
     514          18 :         MTTDL = pow(MTBF, redundancy + 1) / pow(MTTR, redundancy);
     515          99 :         for (i = 0; i < redundancy + 1; ++i)
     516          81 :                 MTTDL /= n - i;
     517             : 
     518             :         /* the raid failure rate is just the inverse of the MTTDL */
     519          18 :         raid_failure_rate = 1.0 / MTTDL;
     520             : 
     521             :         /* probability of at least one RAID failure */
     522          18 :         return poisson_prob_at_least_one_failure(raid_failure_rate);
     523             : }
     524             : 
     525       72624 : static void state_info_log(devinfo_t* devinfo)
     526             : {
     527             :         char esc_buffer[ESC_MAX];
     528             : 
     529       72624 :         log_tag("info:%s:%s\n", devinfo->file, devinfo->name);
     530       72624 :         if (devinfo->serial[0])
     531       72624 :                 log_tag("attr:%s:%s:serial:%s\n", devinfo->file, devinfo->name, esc_tag(devinfo->serial, esc_buffer));
     532       72624 :         if (devinfo->family[0])
     533       72606 :                 log_tag("attr:%s:%s:family:%s\n", devinfo->file, devinfo->name, esc_tag(devinfo->family, esc_buffer));
     534       72624 :         if (devinfo->model[0])
     535       72624 :                 log_tag("attr:%s:%s:model:%s\n", devinfo->file, devinfo->name, esc_tag(devinfo->model, esc_buffer));
     536       72624 :         if (devinfo->interf[0])
     537       72612 :                 log_tag("attr:%s:%s:interface:%s\n", devinfo->file, devinfo->name, esc_tag(devinfo->interf, esc_buffer));
     538       72624 :         if (devinfo->info[INFO_SIZE] != SMART_UNASSIGNED)
     539       72624 :                 log_tag("attr:%s:%s:size:%" PRIu64 "\n", devinfo->file, devinfo->name, devinfo->info[INFO_SIZE]);
     540       72624 :         if (devinfo->info[INFO_ROTATION_RATE] != SMART_UNASSIGNED)
     541       72624 :                 log_tag("attr:%s:%s:rotationrate:%" PRIu64 "\n", devinfo->file, devinfo->name, devinfo->info[INFO_ROTATION_RATE]);
     542       72624 : }
     543             : 
     544       72624 : static void state_smart_ignore(struct snapraid_state* state, devinfo_t* devinfo)
     545             : {
     546             :         /* clear attributes to ignore */
     547      363120 :         for (int i = 0; i < SMART_IGNORE_MAX; ++i) {
     548      290496 :                 devinfo->smart[state->smartignore[i]].raw = SMART_UNASSIGNED;
     549      290496 :                 devinfo->smart[devinfo->smartignore[i]].raw = SMART_UNASSIGNED;
     550             :         }
     551       72624 : }
     552             : 
     553       72624 : static void state_smart_log(devinfo_t* devinfo, double afr)
     554             : {
     555             :         unsigned j;
     556             : 
     557       72624 :         log_tag("smart:%s:%s\n", devinfo->file, devinfo->name);
     558       72624 :         if (devinfo->smart[SMART_ERROR_PROTOCOL].raw != SMART_UNASSIGNED)
     559       72612 :                 log_tag("attr:%s:%s:error_protocol:%" PRIu64 "\n", devinfo->file, devinfo->name, devinfo->smart[SMART_ERROR_PROTOCOL].raw);
     560       72624 :         if (devinfo->smart[SMART_ERROR_MEDIUM].raw != SMART_UNASSIGNED)
     561       72570 :                 log_tag("attr:%s:%s:error_medium:%" PRIu64 "\n", devinfo->file, devinfo->name, devinfo->smart[SMART_ERROR_MEDIUM].raw);
     562       72624 :         if (devinfo->smart[SMART_WEAR_LEVEL].raw != SMART_UNASSIGNED)
     563       72573 :                 log_tag("attr:%s:%s:wear_level:%" PRIu64 "\n", devinfo->file, devinfo->name, devinfo->smart[SMART_WEAR_LEVEL].raw);
     564       72624 :         if (devinfo->smart[SMART_FLAGS].raw != SMART_UNASSIGNED)
     565       19394 :                 log_tag("attr:%s:%s:flags:%" PRIu64 ":%" PRIx64 "\n", devinfo->file, devinfo->name, devinfo->smart[SMART_FLAGS].raw, devinfo->smart[SMART_FLAGS].raw);
     566             : 
     567       72624 :         if (afr != 0)
     568       70188 :                 log_tag("attr:%s:%s:afr:%g:%g\n", devinfo->file, devinfo->name, afr, poisson_prob_at_least_one_failure(afr));
     569             : 
     570    18664368 :         for (j = 0; j < 256; ++j) {
     571    18591744 :                 struct smart_attr* attr = &devinfo->smart[j];
     572    18591744 :                 if (attr->raw != SMART_UNASSIGNED) {
     573    17891596 :                         const char* type = "";;
     574    17891596 :                         const char* updated = "";
     575    17891596 :                         const char* when_failed = "";
     576             : 
     577    17891596 :                         if (attr->flags & SMART_ATTR_TYPE_PREFAIL)
     578         189 :                                 type = "prefail";
     579    17891407 :                         else if (attr->flags & SMART_ATTR_TYPE_OLDAGE)
     580         570 :                                 type = "oldage";
     581    17891596 :                         if (attr->flags & SMART_ATTR_UPDATE_ALWAYS)
     582         639 :                                 updated = "always";
     583    17890957 :                         else if (attr->flags & SMART_ATTR_UPDATE_OFFLINE)
     584         120 :                                 updated = "offline";
     585    17891596 :                         if (attr->flags & SMART_ATTR_WHEN_FAILED_NOW)
     586           0 :                                 when_failed = "now";
     587    17891596 :                         else if (attr->flags & SMART_ATTR_WHEN_FAILED_PAST)
     588           6 :                                 when_failed = "past";
     589    17891590 :                         else if (attr->flags & SMART_ATTR_WHEN_FAILED_NEVER)
     590         753 :                                 when_failed = "never";
     591             : 
     592    17891596 :                         log_tag("attr:%s:%s:%u:%" PRIu64 ":%" PRIx64 ":%" PRIu64 ":%" PRIu64 ":%" PRIu64 ":%s:%s:%s:%s\n", devinfo->file, devinfo->name, j, attr->raw, attr->raw, attr->norm, attr->worst, attr->thresh, attr->name, type, updated, when_failed);
     593             :                 }
     594             :         }
     595             : 
     596       72624 :         int temp = smart_temp(devinfo);
     597       72624 :         if (temp >= 0)
     598       72612 :                 log_tag("attr:%s:%s:temperature:%d\n", devinfo->file, devinfo->name, temp);
     599       72624 : }
     600             : 
     601           2 : static void state_smart(struct snapraid_state* state, unsigned n, tommy_list* low)
     602             : {
     603             :         tommy_node* i;
     604             :         unsigned j;
     605             :         size_t device_pad;
     606             :         size_t serial_pad;
     607             :         int have_parent;
     608             :         double array_failure_rate;
     609             :         double p_at_least_one_failure;
     610           2 :         int make_it_fail = 0;
     611           2 :         uint64_t mask32 = 0xffffffffU;
     612             : 
     613             :         /* compute lengths for padding */
     614           2 :         device_pad = 0;
     615           2 :         serial_pad = 0;
     616           2 :         have_parent = 0;
     617          46 :         for (i = tommy_list_head(low); i != 0; i = i->next) {
     618             :                 size_t len;
     619          44 :                 devinfo_t* devinfo = i->data;
     620             : 
     621          44 :                 len = strlen(devinfo->file);
     622          44 :                 if (len > device_pad)
     623           2 :                         device_pad = len;
     624             : 
     625          44 :                 len = strlen(devinfo->serial);
     626          44 :                 if (len > serial_pad)
     627           3 :                         serial_pad = len;
     628             : 
     629          44 :                 if (devinfo->parent != 0)
     630          14 :                         have_parent = 1;
     631             :         }
     632             : 
     633           2 :         printf("SnapRAID SMART report:\n");
     634           2 :         printf("\n");
     635           2 :         printf("   Temp");
     636           2 :         printf("  Power");
     637           2 :         printf("   Error");
     638           2 :         printf("   FP");
     639           2 :         printf(" Wear");
     640           2 :         printf(" Size");
     641           2 :         printf("\n");
     642           2 :         printf("      C");
     643           2 :         printf(" OnDays");
     644           2 :         printf("   Count");
     645           2 :         printf("     ");
     646           2 :         printf("Level");
     647           2 :         printf("   TB");
     648           2 :         printf("  "); printl("Serial", serial_pad);
     649           2 :         printf("  "); printl("Device", device_pad);
     650           2 :         printf("  Disk");
     651           2 :         printf("\n");
     652             :         /*      |<##################################################################72>|####80>| */
     653           2 :         printf(" -----------------------------------------------------------------------\n");
     654             : 
     655           2 :         array_failure_rate = 0;
     656          46 :         for (i = tommy_list_head(low); i != 0; i = i->next) {
     657          44 :                 devinfo_t* devinfo = i->data;
     658             :                 double afr;
     659             :                 uint64_t flag;
     660             : 
     661          44 :                 state_smart_ignore(state, devinfo);
     662             : 
     663          44 :                 int temp = smart_temp(devinfo);
     664          44 :                 if (temp >= 0)
     665          44 :                         printf("%7d", temp);
     666             :                 else
     667           0 :                         printf("      -");
     668             : 
     669          44 :                 if (devinfo->smart[SMART_POWER_ON_HOURS].raw != SMART_UNASSIGNED)
     670          43 :                         printf("%7" PRIu64, (devinfo->smart[SMART_POWER_ON_HOURS].raw & mask32) / 24);
     671             :                 else
     672           1 :                         printf("      -");
     673             : 
     674          44 :                 uint64_t error_count = 0;
     675          44 :                 if (devinfo->smart[SMART_ERROR_PROTOCOL].raw != SMART_UNASSIGNED && devinfo->smart[SMART_ERROR_PROTOCOL].raw != 0)
     676           1 :                         error_count += devinfo->smart[SMART_ERROR_PROTOCOL].raw;
     677          44 :                 if (devinfo->smart[SMART_ERROR_MEDIUM].raw != SMART_UNASSIGNED && devinfo->smart[SMART_ERROR_MEDIUM].raw != 0)
     678           0 :                         error_count += devinfo->smart[SMART_ERROR_MEDIUM].raw;
     679          44 :                 if (devinfo->smart[SMART_FLAGS].raw != SMART_UNASSIGNED)
     680          22 :                         flag = devinfo->smart[SMART_FLAGS].raw;
     681             :                 else
     682          22 :                         flag = 0;
     683             : 
     684          44 :                 if (flag & SMARTCTL_FLAG_FAIL)
     685           1 :                         printf("    FAIL");
     686          43 :                 else if (flag & SMARTCTL_FLAG_PREFAIL)
     687           1 :                         printf(" PREFAIL");
     688          42 :                 else if (flag & SMARTCTL_FLAG_PREFAIL_LOGGED)
     689           3 :                         printf(" logfail");
     690          39 :                 else if (error_count != 0) {
     691           1 :                         printf("%8" PRIu64, error_count);
     692          38 :                 } else if (flag & SMARTCTL_FLAG_ERROR_LOGGED)
     693           1 :                         printf("  logerr");
     694          37 :                 else if (flag & SMARTCTL_FLAG_SELFERROR_LOGGED)
     695           1 :                         printf(" selferr");
     696             :                 else
     697          36 :                         printf("       -");
     698             : 
     699             :                 /* if some fail/prefail attribute, make the command to fail */
     700          44 :                 if (flag & (SMARTCTL_FLAG_FAIL | SMARTCTL_FLAG_PREFAIL))
     701           2 :                         make_it_fail = 1;
     702             : 
     703             :                 /* note that in older smartmontools, like 5.x, rotation rate is not present */
     704             :                 /* and then it could remain unassigned */
     705             : 
     706          44 :                 if (flag & (SMARTCTL_FLAG_UNSUPPORTED | SMARTCTL_FLAG_OPEN)) {
     707             :                         /* if error running smartctl, skip AFR estimation */
     708           2 :                         afr = 0;
     709           2 :                         printf("  n/a");
     710          42 :                 } else if (devinfo->info[INFO_ROTATION_RATE] == 0) {
     711             :                         /* if SSD, skip AFR estimation as data is from not SSD disks */
     712           1 :                         afr = 0;
     713           1 :                         printf("  SSD");
     714             :                 } else {
     715          41 :                         afr = smart_afr(devinfo);
     716             : 
     717          41 :                         if (afr == 0) {
     718             :                                 /* this happens only if no data */
     719           1 :                                 printf("    -");
     720             :                         } else {
     721             :                                 /* use only the disks in the array */
     722          40 :                                 if (devinfo->parent != 0 || !have_parent)
     723          40 :                                         array_failure_rate += afr;
     724             : 
     725          40 :                                 printf("%4.0f%%", poisson_prob_at_least_one_failure(afr) * 100);
     726             :                         }
     727             :                 }
     728             : 
     729          44 :                 if (devinfo->smart[SMART_WEAR_LEVEL].raw != SMART_UNASSIGNED) {
     730          31 :                         printf(" %3" PRIu64 "%%", devinfo->smart[SMART_WEAR_LEVEL].raw);
     731             :                 } else {
     732          13 :                         printf("    -");
     733             :                 }
     734             : 
     735          44 :                 if (devinfo->info[INFO_SIZE] != SMART_UNASSIGNED)
     736          44 :                         printf(" %4.1f", devinfo->info[INFO_SIZE] / 1E12);
     737             :                 else
     738           0 :                         printf("    -");
     739             : 
     740          44 :                 printf("  ");
     741          44 :                 if (*devinfo->serial)
     742          44 :                         printl(devinfo->serial, serial_pad);
     743             :                 else
     744           0 :                         printl("-", serial_pad);
     745             : 
     746          44 :                 printf("  ");
     747          44 :                 if (*devinfo->file)
     748          44 :                         printl(devinfo->file, device_pad);
     749             :                 else
     750           0 :                         printl("-", device_pad);
     751             : 
     752          44 :                 printf("  ");
     753          44 :                 if (*devinfo->name)
     754          44 :                         printf("%s", devinfo->name);
     755             :                 else
     756           0 :                         printf("-");
     757             : 
     758          44 :                 printf("\n");
     759             : 
     760          44 :                 state_info_log(devinfo);
     761          44 :                 state_smart_log(devinfo, afr);
     762             :         }
     763             : 
     764           2 :         printf("\n");
     765             : 
     766             :         /*      |<##################################################################72>|####80>| */
     767           2 :         printf("The FP column is the estimated probability (in percentage) that the disk\n");
     768           2 :         printf("is going to fail in the next year.\n");
     769           2 :         printf("\n");
     770             : 
     771             :         /*
     772             :          * The probability of one and of at least one failure is computed assuming
     773             :          * a Poisson distribution with the estimated array failure rate.
     774             :          */
     775           2 :         p_at_least_one_failure = poisson_prob_at_least_one_failure(array_failure_rate);
     776             : 
     777           2 :         printf("Probability that at least one disk is going to fail in the next year is %.0f%%.\n", p_at_least_one_failure * 100);
     778           2 :         log_tag("summary:array_failure:%g:%g\n", array_failure_rate, p_at_least_one_failure);
     779             : 
     780             :         /* print extra stats only in verbose mode */
     781           2 :         if (msg_level < MSG_VERBOSE)
     782           1 :                 goto bail;
     783             : 
     784             :         /*      |<##################################################################72>|####80>| */
     785           1 :         printf("\n");
     786           1 :         printf("Probability of data loss in the next year for different parity and\n");
     787           1 :         printf("combined scrub and repair time:\n");
     788           1 :         printf("\n");
     789           1 :         printf("  Parity  1 Week                1 Month             3 Months\n");
     790           1 :         printf(" -----------------------------------------------------------------------\n");
     791           7 :         for (j = 0; j < RAID_PARITY_MAX; ++j) {
     792           6 :                 printf("%6u", j + 1);
     793           6 :                 printf("    ");
     794           6 :                 printp(raid_prob_of_one_or_more_failures(array_failure_rate, 365.0 / 7, n, j + 1) * 100, 19);
     795           6 :                 printf("    ");
     796           6 :                 printp(raid_prob_of_one_or_more_failures(array_failure_rate, 365.0 / 30, n, j + 1) * 100, 17);
     797           6 :                 printf("    ");
     798           6 :                 printp(raid_prob_of_one_or_more_failures(array_failure_rate, 365.0 / 90, n, j + 1) * 100, 13);
     799           6 :                 printf("\n");
     800             :         }
     801             : 
     802           1 :         printf("\n");
     803             : 
     804             :         /*      |<##################################################################72>|####80>| */
     805           1 :         printf("These values are the probabilities that in the next year you'll have a\n");
     806           1 :         printf("sequence of failures from which the parity won't be able to recover,\n");
     807           1 :         printf("assuming that you regularly scrub and repair the array (as needed)\n");
     808           1 :         printf("within the specified time.\n");
     809             : 
     810           2 : bail:
     811           2 :         if (make_it_fail) {
     812           1 :                 printf("\n");
     813           1 :                 printf("DANGER! SMART is reporting that one or more disks are FAILING!\n");
     814           1 :                 printf("Please take immediate action!\n");
     815           1 :                 exit(EXIT_FAILURE);
     816             :         }
     817           1 : }
     818             : 
     819        2417 : void state_attr(struct snapraid_state* state, tommy_list* low)
     820             : {
     821             :         tommy_node* i;
     822             : 
     823             :         (void)state;
     824             : 
     825       74909 :         for (i = tommy_list_head(low); i != 0; i = i->next) {
     826       72492 :                 devinfo_t* devinfo = i->data;
     827             : 
     828       72492 :                 state_smart_ignore(state, devinfo);
     829             : 
     830       72492 :                 double afr = smart_afr(devinfo);
     831             : 
     832       72492 :                 state_info_log(devinfo);
     833       72492 :                 state_smart_log(devinfo, afr);
     834             :         }
     835        2417 : }
     836             : 
     837           4 : static void state_probe(struct snapraid_state* state, tommy_list* low)
     838             : {
     839             :         tommy_node* i;
     840             :         size_t device_pad;
     841             : 
     842             :         (void)state;
     843             : 
     844             :         /* compute lengths for padding */
     845           4 :         device_pad = 0;
     846          92 :         for (i = tommy_list_head(low); i != 0; i = i->next) {
     847             :                 size_t len;
     848          88 :                 devinfo_t* devinfo = i->data;
     849             : 
     850          88 :                 state_smart_ignore(state, devinfo);
     851             : 
     852          88 :                 len = strlen(devinfo->file);
     853          88 :                 if (len > device_pad)
     854           4 :                         device_pad = len;
     855             : 
     856          88 :                 double afr = smart_afr(devinfo);
     857             : 
     858          88 :                 state_info_log(devinfo);
     859          88 :                 state_smart_log(devinfo, afr);
     860             :         }
     861             : 
     862           4 :         printf("SnapRAID PROBE report:\n");
     863           4 :         printf("\n");
     864           4 :         printf("   State ");
     865           4 :         printf("  ");
     866           4 :         printl("Device", device_pad);
     867           4 :         printf("  Disk");
     868           4 :         printf("\n");
     869             :         /*      |<##################################################################72>|####80>| */
     870           4 :         printf(" -----------------------------------------------------------------------\n");
     871             : 
     872          92 :         for (i = tommy_list_head(low); i != 0; i = i->next) {
     873          88 :                 devinfo_t* devinfo = i->data;
     874             : 
     875          88 :                 printf("  ");
     876             : 
     877          88 :                 switch (devinfo->power) {
     878          12 :                 case POWER_STANDBY : printf("StandBy"); break;
     879          76 :                 case POWER_ACTIVE : printf(" Active"); break;
     880           0 :                 default : printf("Unknown"); break;
     881             :                 }
     882             : 
     883          88 :                 printf("  ");
     884          88 :                 if (*devinfo->file)
     885          88 :                         printl(devinfo->file, device_pad);
     886             :                 else
     887           0 :                         printl("-", device_pad);
     888             : 
     889          88 :                 printf("  ");
     890          88 :                 if (*devinfo->name)
     891          88 :                         printf("%s", devinfo->name);
     892             :                 else
     893           0 :                         printf("-");
     894             : 
     895          88 :                 printf("\n");
     896             : 
     897          88 :                 log_tag("probe:%s:%s:%d\n", devinfo->file, devinfo->name, devinfo->power);
     898             :         }
     899             : 
     900           4 :         printf("\n");
     901           4 : }
     902             : 
     903          10 : static void state_stat(struct snapraid_state* state, tommy_list* high)
     904             : {
     905             :         tommy_node* i;
     906             : 
     907             :         (void)state;
     908             : 
     909             :         /* compute lengths for padding */
     910         210 :         for (i = tommy_list_head(high); i != 0; i = i->next) {
     911         200 :                 devinfo_t* devinfo = i->data;
     912             : 
     913             :                 /* print disk access_stat (not split) */
     914         200 :                 if (devinfo->split == 0 && devinfo->access_stat != 0) {
     915          92 :                         if (devinfo->name[0])
     916          92 :                                 log_tag("stat:%s:%" PRIu64 "\n", devinfo->name, devinfo->access_stat);
     917             :                 }
     918             :         }
     919          10 : }
     920             : 
     921           2 : static void state_devices(struct snapraid_state* state, tommy_list* low)
     922             : {
     923             :         tommy_node* i;
     924             : 
     925             :         (void)state;
     926             : 
     927          46 :         for (i = tommy_list_head(low); i != 0; i = i->next) {
     928          44 :                 devinfo_t* devinfo = i->data;
     929          44 :                 devinfo_t* parent = devinfo->parent;
     930             : 
     931          44 :                 if (parent) {
     932             : #ifdef _WIN32
     933             :                         printf("%" PRIu64 "\t%s\t%08" PRIx64 "\t%s\t%s\n", devinfo->device, devinfo->wfile, parent->device, parent->wfile, parent->name);
     934             : #else
     935          44 :                         printf("%u:%u\t%s\t%u:%u\t%s\t%s\n", major(devinfo->device), minor(devinfo->device), devinfo->file, major(parent->device), minor(parent->device), parent->file, parent->name);
     936             : #endif
     937             :                 }
     938             :         }
     939           2 : }
     940             : 
     941        2419 : int devtest(tommy_list* high, tommy_list* low, int operation)
     942             : {
     943             :         tommy_node* i;
     944             :         unsigned count;
     945             : 
     946        2419 :         if (operation != DEVICE_SMART && operation != DEVICE_PROBE)
     947           0 :                 return -1;
     948             : 
     949             :         /* for each device add some fake data */
     950        2419 :         count = 0;
     951       74989 :         for (i = tommy_list_head(high); i != 0; i = i->next) {
     952       72570 :                 devinfo_t* devinfo = i->data;
     953             :                 devinfo_t* entry;
     954             :                 int j;
     955             : 
     956       72570 :                 ++count;
     957             : 
     958       72570 :                 entry = calloc_nofail(1, sizeof(devinfo_t));
     959             : 
     960             :                 /* fake device number */
     961       72570 :                 entry->device = tommy_strhash_u32(0, devinfo->name);
     962             : 
     963       72570 :                 tommy_list_insert_tail(low, &entry->node, entry);
     964             : 
     965    18650490 :                 for (j = 0; j < 256; ++j) {
     966    18577920 :                         switch (count) {
     967           0 :                         case 0 :
     968           0 :                                 entry->smart[j].raw = 0;
     969           0 :                                 break;
     970      619264 :                         case 1 :
     971      619264 :                                 entry->smart[j].raw = SMART_UNASSIGNED;
     972      619264 :                                 break;
     973    17958656 :                         default :
     974    17958656 :                                 entry->smart[j].raw = 0;
     975    17958656 :                                 break;
     976             :                         }
     977             :                 }
     978             : 
     979       72570 :                 pathprint(entry->serial, sizeof(entry->serial), "FAKE_%s", devinfo->serial);
     980       72570 :                 pathprint(entry->family, sizeof(entry->family), "FAKE_%s", devinfo->family);
     981       72570 :                 pathprint(entry->model, sizeof(entry->model), "FAKE_%s", devinfo->model);
     982       72570 :                 pathprint(entry->interf, sizeof(entry->interf), "FAKE_%s", devinfo->interf);
     983       72570 :                 pathprint(entry->file, sizeof(entry->file), "FAKE_%s", devinfo->file);
     984       72570 :                 pathcpy(entry->name, sizeof(entry->name), devinfo->name);
     985       72570 :                 entry->info[INFO_SIZE] = count * TERA;
     986       72570 :                 entry->info[INFO_ROTATION_RATE] = 7200;
     987       72570 :                 entry->smart[SMART_ERROR_MEDIUM].raw = 0;
     988       72570 :                 entry->smart[SMART_ERROR_PROTOCOL].raw = 0;
     989       72570 :                 entry->smart[SMART_FLAGS].raw = SMART_UNASSIGNED;
     990       72570 :                 entry->smart[SMART_TEMPERATURE_CELSIUS].raw = 27;
     991             : 
     992       72570 :                 switch (count) {
     993        2419 :                 case 3 : entry->smart[SMART_ERROR_PROTOCOL].raw = 1; break;
     994        2419 :                 case 4 : entry->smart[SMART_FLAGS].raw = SMARTCTL_FLAG_UNSUPPORTED; break;
     995        2419 :                 case 5 : entry->smart[SMART_FLAGS].raw = SMARTCTL_FLAG_COMMAND; break;
     996        2419 :                 case 6 : entry->smart[SMART_FLAGS].raw = SMARTCTL_FLAG_OPEN; break;
     997        2419 :                 case 7 : entry->smart[SMART_FLAGS].raw = SMARTCTL_FLAG_FAIL; break;
     998        2419 :                 case 8 : entry->smart[SMART_FLAGS].raw = SMARTCTL_FLAG_PREFAIL; break;
     999        2419 :                 case 9 : entry->smart[SMART_FLAGS].raw = SMARTCTL_FLAG_PREFAIL_LOGGED; break;
    1000        2419 :                 case 10 : entry->smart[SMART_FLAGS].raw = SMARTCTL_FLAG_ERROR_LOGGED; break;
    1001        2419 :                 case 11 : entry->smart[SMART_FLAGS].raw = SMARTCTL_FLAG_SELFERROR_LOGGED; break;
    1002             :                 }
    1003             : 
    1004       72570 :                 entry->power = POWER_ACTIVE;
    1005             :         }
    1006             : 
    1007        2419 :         return 0;
    1008             : }
    1009             : 
    1010          12 : void state_device(struct snapraid_state* state, int operation, tommy_list* filterlist_disk)
    1011             : {
    1012             :         tommy_node* i;
    1013             :         unsigned j;
    1014             :         tommy_list high;
    1015             :         tommy_list low;
    1016             :         int ret;
    1017          12 :         time_t now = time(0);
    1018             : 
    1019          12 :         switch (operation) {
    1020           2 :         case DEVICE_UP : msg_progress("Spinup...\n"); break;
    1021           2 :         case DEVICE_DOWN : msg_progress("Spindown...\n"); break;
    1022             :         }
    1023             : 
    1024          12 :         tommy_list_init(&high);
    1025          12 :         tommy_list_init(&low);
    1026             : 
    1027             :         /* for all disks */
    1028         108 :         for (i = state->disklist; i != 0; i = i->next) {
    1029          96 :                 struct snapraid_disk* disk = i->data;
    1030             :                 devinfo_t* entry;
    1031             : 
    1032          96 :                 if (filterlist_disk != 0 && filter_path(filterlist_disk, 0, disk->name, 0) != 0)
    1033           5 :                         continue;
    1034             : 
    1035          91 :                 entry = calloc_nofail(1, sizeof(devinfo_t));
    1036             : 
    1037          91 :                 entry->device = disk->device;
    1038          91 :                 device_name_set(entry, disk->name, 0);
    1039          91 :                 pathcpy(entry->mount, sizeof(entry->mount), disk->dir);
    1040          91 :                 pathcpy(entry->smartctl, sizeof(entry->smartctl), disk->smartctl);
    1041          91 :                 memcpy(entry->smartignore, disk->smartignore, sizeof(entry->smartignore));
    1042             : 
    1043          91 :                 tommy_list_insert_tail(&high, &entry->node, entry);
    1044             :         }
    1045             : 
    1046             :         /* for all parities */
    1047          60 :         for (j = 0; j < state->level; ++j) {
    1048             :                 devinfo_t* entry;
    1049             :                 devinfo_t* split;
    1050             :                 unsigned s;
    1051             : 
    1052          48 :                 if (filterlist_disk != 0 && filter_path(filterlist_disk, 0, lev_config_name(j), 0) != 0)
    1053           5 :                         continue;
    1054             : 
    1055         179 :                 for (s = 0; s < state->parity[j].split_mac; ++s) {
    1056         136 :                         entry = calloc_nofail(1, sizeof(devinfo_t));
    1057             : 
    1058             :                         /* keep track of the first split */
    1059         136 :                         if (s == 0)
    1060          43 :                                 split = entry;
    1061             :                         else
    1062          93 :                                 entry->split = split;
    1063             : 
    1064         136 :                         entry->device = state->parity[j].split_map[s].device;
    1065         136 :                         device_name_set(entry, lev_config_name(j), s);
    1066         136 :                         pathcpy(entry->mount, sizeof(entry->mount), state->parity[j].split_map[s].path);
    1067         136 :                         pathcpy(entry->smartctl, sizeof(entry->smartctl), state->parity[j].smartctl);
    1068         136 :                         memcpy(entry->smartignore, state->parity[j].smartignore, sizeof(entry->smartignore));
    1069         136 :                         pathcut(entry->mount); /* remove the parity file */
    1070             : 
    1071         136 :                         tommy_list_insert_tail(&high, &entry->node, entry);
    1072             :                 }
    1073             :         }
    1074             : 
    1075             :         /* extra disks are never spundown or spunup */
    1076          12 :         int include_extra = 1;
    1077          12 :         switch (operation) {
    1078           4 :         case DEVICE_UP :
    1079             :         case DEVICE_DOWN :
    1080             :         case DEVICE_DOWNIFUP :
    1081           4 :                 include_extra = 0;
    1082           4 :                 break;
    1083             :         }
    1084          12 :         if (include_extra) {
    1085             :                 /* for all extra disks */
    1086          16 :                 for (i = state->extralist; i != 0; i = i->next) {
    1087           8 :                         struct snapraid_extra* extra = i->data;
    1088             :                         devinfo_t* entry;
    1089             : 
    1090           8 :                         if (filterlist_disk != 0 && filter_path(filterlist_disk, 0, extra->name, 0) != 0)
    1091           0 :                                 continue;
    1092             : 
    1093           8 :                         entry = calloc_nofail(1, sizeof(devinfo_t));
    1094             : 
    1095           8 :                         entry->device = extra->device;
    1096           8 :                         device_name_set(entry, extra->name, 0);
    1097           8 :                         pathcpy(entry->mount, sizeof(entry->mount), extra->dir);
    1098           8 :                         pathcpy(entry->smartctl, sizeof(entry->smartctl), extra->smartctl);
    1099           8 :                         memcpy(entry->smartignore, extra->smartignore, sizeof(extra->smartignore));
    1100             : 
    1101           8 :                         tommy_list_insert_tail(&high, &entry->node, entry);
    1102             :                 }
    1103             :         }
    1104             : 
    1105             :         /* with a GUI always gives time reference */
    1106          12 :         if (state->opt.gui)
    1107           3 :                 log_tag("unixtime:%" PRIi64 "\n", (int64_t)now);
    1108             : 
    1109          12 :         if (state->opt.fake_device) {
    1110           3 :                 ret = devtest(&high, &low, operation);
    1111             :         } else {
    1112           9 :                 ret = devquery(&high, &low, operation);
    1113             :         }
    1114             : 
    1115             :         /* if the list is empty, it's not supported in this platform */
    1116          12 :         if (ret == 0 && tommy_list_empty(&low))
    1117           0 :                 ret = -1;
    1118             : 
    1119          12 :         if (ret != 0) {
    1120           2 :                 const char* ope = 0;
    1121           2 :                 switch (operation) {
    1122           1 :                 case DEVICE_UP : ope = "Spinup"; break;
    1123           1 :                 case DEVICE_DOWN : ope = "Spindown"; break;
    1124           0 :                 case DEVICE_LIST : ope = "Device listing"; break;
    1125           0 :                 case DEVICE_SMART : ope = "Smart"; break;
    1126           0 :                 case DEVICE_PROBE : ope = "Probe"; break;
    1127           0 :                 case DEVICE_DOWNIFUP : ope = "Spindown"; break;
    1128             :                 }
    1129           2 :                 log_fatal(ESOFT, "%s is unsupported in this platform.\n", ope);
    1130             :         } else {
    1131          10 :                 state_stat(state, &high);
    1132             : 
    1133          10 :                 if (operation == DEVICE_SMART)
    1134           2 :                         state_smart(state, state->level + tommy_list_count(&state->disklist), &low);
    1135             : 
    1136           9 :                 if (operation == DEVICE_PROBE)
    1137           4 :                         state_probe(state, &low);
    1138             : 
    1139           9 :                 if (operation == DEVICE_UP)
    1140           1 :                         state_attr(state, &low);
    1141             : 
    1142           9 :                 if (operation == DEVICE_LIST)
    1143           2 :                         state_devices(state, &low);
    1144             :         }
    1145             : 
    1146          11 :         tommy_list_foreach(&high, free);
    1147          11 :         tommy_list_foreach(&low, free);
    1148          11 : }
    1149             : 

Generated by: LCOV version 1.0