-
Notifications
You must be signed in to change notification settings - Fork 141
/
udiskslinuxdriveata.c
2484 lines (2212 loc) · 88.3 KB
/
udiskslinuxdriveata.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
*
* Copyright (C) 2007-2010 David Zeuthen <zeuthen@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "config.h"
#include <glib/gi18n-lib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <pwd.h>
#include <grp.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <mntent.h>
#include <glib/gstdio.h>
#include <errno.h>
#include <atasmart.h>
#include "udiskslogging.h"
#include "udiskslinuxprovider.h"
#include "udiskslinuxdriveobject.h"
#include "udiskslinuxdriveata.h"
#include "udiskslinuxblockobject.h"
#include "udisksdaemon.h"
#include "udisksdaemonutil.h"
#include "udisksbasejob.h"
#include "udiskssimplejob.h"
#include "udisksthreadedjob.h"
#include "udisksata.h"
#include "udiskslinuxdevice.h"
#include "udisksconfigmanager.h"
/**
* SECTION:udiskslinuxdriveata
* @title: UDisksLinuxDriveAta
* @short_description: Linux implementation of #UDisksDriveAta
*
* This type provides an implementation of the #UDisksDriveAta
* interface on Linux.
*/
typedef struct _UDisksLinuxDriveAtaClass UDisksLinuxDriveAtaClass;
/**
* UDisksLinuxDriveAta:
*
* The #UDisksLinuxDriveAta structure contains only private data and should
* only be accessed using the provided API.
*/
struct _UDisksLinuxDriveAta
{
UDisksDriveAtaSkeleton parent_instance;
gboolean smart_is_from_blob;
guint64 smart_updated;
gboolean smart_failing;
gdouble smart_temperature;
guint64 smart_power_on_seconds;
gint smart_num_attributes_failing;
gint smart_num_attributes_failed_in_the_past;
gint64 smart_num_bad_sectors;
const gchar *smart_selftest_status;
gint smart_selftest_percent_remaining;
GVariant *smart_attributes;
UDisksThreadedJob *selftest_job;
gboolean secure_erase_in_progress;
unsigned long drive_read, drive_write;
gboolean standby_enabled;
};
struct _UDisksLinuxDriveAtaClass
{
UDisksDriveAtaSkeletonClass parent_class;
};
static void drive_ata_iface_init (UDisksDriveAtaIface *iface);
G_DEFINE_TYPE_WITH_CODE (UDisksLinuxDriveAta, udisks_linux_drive_ata, UDISKS_TYPE_DRIVE_ATA_SKELETON,
G_IMPLEMENT_INTERFACE (UDISKS_TYPE_DRIVE_ATA, drive_ata_iface_init));
G_LOCK_DEFINE_STATIC (object_lock);
/* ---------------------------------------------------------------------------------------------------- */
static void
udisks_linux_drive_ata_finalize (GObject *object)
{
UDisksLinuxDriveAta *drive = UDISKS_LINUX_DRIVE_ATA (object);
if (drive->smart_attributes != NULL)
g_variant_unref (drive->smart_attributes);
if (G_OBJECT_CLASS (udisks_linux_drive_ata_parent_class)->finalize != NULL)
G_OBJECT_CLASS (udisks_linux_drive_ata_parent_class)->finalize (object);
}
static void
udisks_linux_drive_ata_init (UDisksLinuxDriveAta *drive)
{
g_dbus_interface_skeleton_set_flags (G_DBUS_INTERFACE_SKELETON (drive),
G_DBUS_INTERFACE_SKELETON_FLAGS_HANDLE_METHOD_INVOCATIONS_IN_THREAD);
}
static void
udisks_linux_drive_ata_class_init (UDisksLinuxDriveAtaClass *klass)
{
GObjectClass *gobject_class;
gobject_class = G_OBJECT_CLASS (klass);
gobject_class->finalize = udisks_linux_drive_ata_finalize;
}
/**
* udisks_linux_drive_ata_new:
*
* Creates a new #UDisksLinuxDriveAta instance.
*
* Returns: A new #UDisksLinuxDriveAta. Free with g_object_unref().
*/
UDisksDriveAta *
udisks_linux_drive_ata_new (void)
{
return UDISKS_DRIVE_ATA (g_object_new (UDISKS_TYPE_LINUX_DRIVE_ATA,
NULL));
}
/* ---------------------------------------------------------------------------------------------------- */
/* may be called from *any* thread when the SMART data has been updated */
static void
update_smart (UDisksLinuxDriveAta *drive,
UDisksLinuxDevice *device)
{
gboolean supported = FALSE;
gboolean enabled = FALSE;
guint64 updated = 0;
gboolean failing = FALSE;
gdouble temperature = 0.0;
guint64 power_on_seconds = 0;
const gchar *selftest_status = NULL;
gint selftest_percent_remaining = -1;
gint num_attributes_failing = -1;
gint num_attributes_failed_in_the_past = -1;
gint64 num_bad_sectors = 1;
guint16 word_82 = 0;
guint16 word_85 = 0;
/* ATA8: 7.16 IDENTIFY DEVICE - ECh, PIO Data-In - Table 29 IDENTIFY DEVICE data */
word_82 = udisks_ata_identify_get_word (device->ata_identify_device_data, 82);
word_85 = udisks_ata_identify_get_word (device->ata_identify_device_data, 85);
supported = word_82 & (1<<0);
enabled = word_85 & (1<<0);
G_LOCK (object_lock);
if ((drive->smart_is_from_blob || enabled) && drive->smart_updated > 0)
{
if (drive->smart_is_from_blob)
supported = enabled = TRUE;
updated = drive->smart_updated;
failing = drive->smart_failing;
temperature = drive->smart_temperature;
power_on_seconds = drive->smart_power_on_seconds;
num_attributes_failing = drive->smart_num_attributes_failing;
num_attributes_failed_in_the_past = drive->smart_num_attributes_failed_in_the_past;
num_bad_sectors = drive->smart_num_bad_sectors;
selftest_status = drive->smart_selftest_status;
selftest_percent_remaining = drive->smart_selftest_percent_remaining;
}
G_UNLOCK (object_lock);
if (selftest_status == NULL)
selftest_status = "";
g_object_freeze_notify (G_OBJECT (drive));
udisks_drive_ata_set_smart_supported (UDISKS_DRIVE_ATA (drive), supported);
udisks_drive_ata_set_smart_enabled (UDISKS_DRIVE_ATA (drive), enabled);
udisks_drive_ata_set_smart_updated (UDISKS_DRIVE_ATA (drive), updated);
udisks_drive_ata_set_smart_failing (UDISKS_DRIVE_ATA (drive), failing);
udisks_drive_ata_set_smart_temperature (UDISKS_DRIVE_ATA (drive), temperature);
udisks_drive_ata_set_smart_power_on_seconds (UDISKS_DRIVE_ATA (drive), power_on_seconds);
udisks_drive_ata_set_smart_num_attributes_failing (UDISKS_DRIVE_ATA (drive), num_attributes_failing);
udisks_drive_ata_set_smart_num_attributes_failed_in_the_past (UDISKS_DRIVE_ATA (drive), num_attributes_failed_in_the_past);
udisks_drive_ata_set_smart_num_bad_sectors (UDISKS_DRIVE_ATA (drive), num_bad_sectors);
udisks_drive_ata_set_smart_selftest_status (UDISKS_DRIVE_ATA (drive), selftest_status);
udisks_drive_ata_set_smart_selftest_percent_remaining (UDISKS_DRIVE_ATA (drive), selftest_percent_remaining);
g_object_thaw_notify (G_OBJECT (drive));
}
/* ---------------------------------------------------------------------------------------------------- */
static void
update_pm (UDisksLinuxDriveAta *drive,
UDisksLinuxDevice *device)
{
gboolean pm_supported = FALSE;
gboolean pm_enabled = FALSE;
gboolean apm_supported = FALSE;
gboolean apm_enabled = FALSE;
gboolean aam_supported = FALSE;
gboolean aam_enabled = FALSE;
gboolean write_cache_supported = FALSE;
gboolean write_cache_enabled = FALSE;
gboolean read_lookahead_supported = FALSE;
gboolean read_lookahead_enabled = FALSE;
gint aam_vendor_recommended_value = 0;
guint16 word_82 = 0;
guint16 word_83 = 0;
guint16 word_85 = 0;
guint16 word_86 = 0;
guint16 word_94 = 0;
/* ATA8: 7.16 IDENTIFY DEVICE - ECh, PIO Data-In - Table 29 IDENTIFY DEVICE data */
word_82 = udisks_ata_identify_get_word (device->ata_identify_device_data, 82);
word_83 = udisks_ata_identify_get_word (device->ata_identify_device_data, 83);
word_85 = udisks_ata_identify_get_word (device->ata_identify_device_data, 85);
word_86 = udisks_ata_identify_get_word (device->ata_identify_device_data, 86);
word_94 = udisks_ata_identify_get_word (device->ata_identify_device_data, 94);
pm_supported = word_82 & (1<<3);
pm_enabled = word_85 & (1<<3);
apm_supported = word_83 & (1<<3);
apm_enabled = word_86 & (1<<3);
aam_supported = word_83 & (1<<9);
aam_enabled = word_86 & (1<<9);
if (aam_supported)
aam_vendor_recommended_value = (word_94 >> 8);
write_cache_supported = word_82 & (1<<5);
write_cache_enabled = word_85 & (1<<5);
read_lookahead_supported = word_82 & (1<<6);
read_lookahead_enabled = word_85 & (1<<6);
g_object_freeze_notify (G_OBJECT (drive));
udisks_drive_ata_set_pm_supported (UDISKS_DRIVE_ATA (drive), !!pm_supported);
udisks_drive_ata_set_pm_enabled (UDISKS_DRIVE_ATA (drive), !!pm_enabled);
udisks_drive_ata_set_apm_supported (UDISKS_DRIVE_ATA (drive), !!apm_supported);
udisks_drive_ata_set_apm_enabled (UDISKS_DRIVE_ATA (drive), !!apm_enabled);
udisks_drive_ata_set_aam_supported (UDISKS_DRIVE_ATA (drive), !!aam_supported);
udisks_drive_ata_set_aam_enabled (UDISKS_DRIVE_ATA (drive), !!aam_enabled);
udisks_drive_ata_set_aam_vendor_recommended_value (UDISKS_DRIVE_ATA (drive), aam_vendor_recommended_value);
udisks_drive_ata_set_write_cache_supported (UDISKS_DRIVE_ATA (drive), !!write_cache_supported);
udisks_drive_ata_set_write_cache_enabled (UDISKS_DRIVE_ATA (drive), !!write_cache_enabled);
udisks_drive_ata_set_read_lookahead_supported (UDISKS_DRIVE_ATA (drive), !!read_lookahead_supported);
udisks_drive_ata_set_read_lookahead_enabled (UDISKS_DRIVE_ATA (drive), !!read_lookahead_enabled);
g_object_thaw_notify (G_OBJECT (drive));
}
/* ---------------------------------------------------------------------------------------------------- */
static void
update_security (UDisksLinuxDriveAta *drive,
UDisksLinuxDevice *device)
{
gint erase_unit = 0;
gint enhanced_erase_unit = 0;
gboolean frozen = FALSE;
gboolean security_supported = FALSE;
G_GNUC_UNUSED gboolean security_enabled = FALSE;
guint16 word_82 = 0;
guint16 word_85 = 0;
guint16 word_89 = 0;
guint16 word_90 = 0;
guint16 word_128 = 0;
/* ATA8: 7.16 IDENTIFY DEVICE - ECh, PIO Data-In - Table 29 IDENTIFY DEVICE data */
word_82 = udisks_ata_identify_get_word (device->ata_identify_device_data, 82);
word_85 = udisks_ata_identify_get_word (device->ata_identify_device_data, 85);
word_89 = udisks_ata_identify_get_word (device->ata_identify_device_data, 89);
word_90 = udisks_ata_identify_get_word (device->ata_identify_device_data, 90);
word_128 = udisks_ata_identify_get_word (device->ata_identify_device_data, 128);
security_supported = word_82 & (1<<1);
security_enabled = word_85 & (1<<1);
if (security_supported)
{
erase_unit = (word_89 & 0xff) * 2;
enhanced_erase_unit = (word_90 & 0xff) * 2;
}
frozen = word_128 & (1<<3);
g_object_freeze_notify (G_OBJECT (drive));
/* TODO: export Security{Supported,Enabled} properties
udisks_drive_ata_set_security_supported (UDISKS_DRIVE_ATA (drive), !!security_supported);
udisks_drive_ata_set_security_enabled (UDISKS_DRIVE_ATA (drive), !!security_enabled);
*/
udisks_drive_ata_set_security_erase_unit_minutes (UDISKS_DRIVE_ATA (drive), erase_unit);
udisks_drive_ata_set_security_enhanced_erase_unit_minutes (UDISKS_DRIVE_ATA (drive), enhanced_erase_unit);
udisks_drive_ata_set_security_frozen (UDISKS_DRIVE_ATA (drive), !!frozen);
g_object_thaw_notify (G_OBJECT (drive));
}
/* ---------------------------------------------------------------------------------------------------- */
/**
* udisks_linux_drive_ata_update:
* @drive: A #UDisksLinuxDriveAta.
* @object: The enclosing #UDisksLinuxDriveObject instance.
*
* Updates the interface.
*
* Returns: %TRUE if configuration has changed, %FALSE otherwise.
*/
gboolean
udisks_linux_drive_ata_update (UDisksLinuxDriveAta *drive,
UDisksLinuxDriveObject *object)
{
UDisksLinuxDevice *device;
device = udisks_linux_drive_object_get_device (object, TRUE /* get_hw */);
if (device == NULL)
goto out;
update_smart (drive, device);
update_pm (drive, device);
update_security (drive, device);
out:
/* ensure property changes are sent before the method return */
g_dbus_interface_skeleton_flush (G_DBUS_INTERFACE_SKELETON (drive));
if (device != NULL)
g_object_unref (device);
return FALSE;
}
/* ---------------------------------------------------------------------------------------------------- */
typedef struct
{
GVariantBuilder builder;
gint num_attributes_failing;
gint num_attributes_failed_in_the_past;
} ParseData;
static void
parse_attr_cb (SkDisk *d,
const SkSmartAttributeParsedData *a,
void *user_data)
{
ParseData *data = user_data;
gboolean failed = FALSE;
gboolean failed_in_the_past = FALSE;
gint current, worst, threshold;
current = a->current_value_valid ? a->current_value : -1;
worst = a->worst_value_valid ? a->worst_value : -1;
threshold = a->threshold_valid ? a->threshold : -1;
g_variant_builder_add (&data->builder,
"(ysqiiixia{sv})",
a->id,
a->name,
a->flags,
current,
worst,
threshold,
a->pretty_value, a->pretty_unit,
NULL); /* expansion unused for now */
if (current > 0 && threshold > 0 && current <= threshold)
failed = TRUE;
if (worst > 0 && threshold > 0 && worst <= threshold)
failed_in_the_past = TRUE;
if (failed)
data->num_attributes_failing += 1;
if (failed_in_the_past)
data->num_attributes_failed_in_the_past += 1;
}
static const gchar *
selftest_status_to_string (SkSmartSelfTestExecutionStatus status)
{
const gchar *ret;
switch (status)
{
case SK_SMART_SELF_TEST_EXECUTION_STATUS_SUCCESS_OR_NEVER:
ret = "success";
break;
case SK_SMART_SELF_TEST_EXECUTION_STATUS_ABORTED:
ret = "aborted";
break;
case SK_SMART_SELF_TEST_EXECUTION_STATUS_INTERRUPTED:
ret = "interrupted";
break;
case SK_SMART_SELF_TEST_EXECUTION_STATUS_FATAL:
ret = "fatal";
break;
case SK_SMART_SELF_TEST_EXECUTION_STATUS_ERROR_UNKNOWN:
ret = "error_unknown";
break;
case SK_SMART_SELF_TEST_EXECUTION_STATUS_ERROR_ELECTRICAL:
ret = "error_electrical";
break;
case SK_SMART_SELF_TEST_EXECUTION_STATUS_ERROR_SERVO:
ret = "error_servo";
break;
case SK_SMART_SELF_TEST_EXECUTION_STATUS_ERROR_READ:
ret = "error_read";
break;
case SK_SMART_SELF_TEST_EXECUTION_STATUS_ERROR_HANDLING:
ret = "error_handling";
break;
case SK_SMART_SELF_TEST_EXECUTION_STATUS_INPROGRESS:
ret = "inprogress";
break;
default:
ret = "";
break;
}
return ret;
}
static gboolean update_io_stats (UDisksLinuxDriveAta *drive, UDisksLinuxDevice *device)
{
const gchar *drivepath = g_udev_device_get_sysfs_path (device->udev_device);
gchar statpath[PATH_MAX];
unsigned long drive_read, drive_write;
FILE *statf;
gboolean noio = FALSE;
snprintf (statpath, sizeof(statpath), "%s/stat", drivepath);
statf = fopen (statpath, "r");
if (statf == NULL)
{
udisks_warning ("Failed to open %s\n", statpath);
}
else
{
if (fscanf (statf, "%lu %*u %*u %*u %lu", &drive_read, &drive_write) != 2)
{
udisks_warning ("Failed to read %s\n", statpath);
}
else
{
noio = drive_read == drive->drive_read && drive_write == drive->drive_write;
udisks_debug ("drive_read=%lu, drive_write=%lu, old_drive_read=%lu, old_drive_write=%lu\n",
drive_read, drive_write, drive->drive_read, drive->drive_write);
drive->drive_read = drive_read;
drive->drive_write = drive_write;
}
fclose (statf);
}
return noio;
}
/**
* udisks_linux_drive_ata_refresh_smart_sync:
* @drive: The #UDisksLinuxDriveAta to refresh.
* @nowakeup: If %TRUE, will not wake up the disk if asleep.
* @simulate_path: If not %NULL, the path of a file with a libatasmart blob to use.
* @cancellable: A #GCancellable or %NULL.
* @error: Return location for error.
*
* Synchronously refreshes ATA S.M.A.R.T. data on @drive using one of
* the physical drives associated with it. The calling thread is
* blocked until the data has been obtained.
*
* If @nowake is %TRUE and the disk is in a sleep state this fails
* with %UDISKS_ERROR_WOULD_WAKEUP.
*
* This may only be called if @drive has been associated with a
* #UDisksLinuxDriveObject instance.
*
* This method may be called from any thread.
*
* Returns: %TRUE if the operation succeeded, %FALSE if @error is set.
*/
gboolean
udisks_linux_drive_ata_refresh_smart_sync (UDisksLinuxDriveAta *drive,
gboolean nowakeup,
const gchar *simulate_path,
GCancellable *cancellable,
GError **error)
{
UDisksLinuxDriveObject *object;
UDisksLinuxDevice *device = NULL;
gboolean ret = FALSE;
SkDisk *d = NULL;
SkBool awake;
SkBool good;
uint64_t temp_mkelvin = 0;
uint64_t power_on_msec = 0;
uint64_t num_bad_sectors = 0;
const SkSmartParsedData *data;
ParseData parse_data;
object = udisks_daemon_util_dup_object (drive, error);
if (object == NULL)
goto out;
if (drive->secure_erase_in_progress)
{
g_set_error (error, UDISKS_ERROR, UDISKS_ERROR_DEVICE_BUSY,
"Secure erase in progress");
goto out;
}
device = udisks_linux_drive_object_get_device (object, TRUE /* get_hw */);
g_assert (device != NULL);
/* TODO: use cancellable */
if (simulate_path != NULL)
{
gchar *blob;
gsize blob_len;
if (!g_file_get_contents (simulate_path,
&blob,
&blob_len,
error))
{
goto out;
}
if (sk_disk_open (NULL, &d) != 0)
{
g_set_error (error,
UDISKS_ERROR,
UDISKS_ERROR_FAILED,
"sk_disk_open: %m");
g_free (blob);
goto out;
}
if (sk_disk_set_blob (d, blob, blob_len) != 0)
{
g_set_error (error,
UDISKS_ERROR,
UDISKS_ERROR_FAILED,
"sk_disk_set_blob: %m");
g_free (blob);
goto out;
}
g_free (blob);
}
else
{
guchar count;
gboolean noio = FALSE;
if (drive->standby_enabled)
noio = update_io_stats (drive, device);
if (!udisks_ata_get_pm_state (g_udev_device_get_device_file (device->udev_device), error, &count))
goto out;
awake = count == 0xFF || count == 0x80;
/* don't wake up disk unless specically asked to */
if (nowakeup && (!awake || noio))
{
g_set_error (error,
UDISKS_ERROR,
UDISKS_ERROR_WOULD_WAKEUP,
"Disk is in sleep mode and the nowakeup option was passed");
goto out;
}
}
if (sk_disk_open (g_udev_device_get_device_file (device->udev_device), &d) != 0)
{
g_set_error (error,
UDISKS_ERROR,
UDISKS_ERROR_FAILED,
"sk_disk_open: %m");
goto out;
}
if (sk_disk_smart_read_data (d) != 0)
{
g_set_error (error,
UDISKS_ERROR,
UDISKS_ERROR_FAILED,
"sk_disk_smart_read_data: %m");
goto out;
}
if (sk_disk_smart_status (d, &good) != 0)
{
g_set_error (error,
UDISKS_ERROR,
UDISKS_ERROR_FAILED,
"sk_disk_smart_status: %m");
goto out;
}
if (sk_disk_smart_parse (d, &data) != 0)
{
g_set_error (error,
UDISKS_ERROR,
UDISKS_ERROR_FAILED,
"sk_disk_smart_parse: %m");
goto out;
}
/* don't care if these are failing or not */
sk_disk_smart_get_temperature (d, &temp_mkelvin);
sk_disk_smart_get_power_on (d, &power_on_msec);
sk_disk_smart_get_bad (d, &num_bad_sectors);
memset (&parse_data, 0, sizeof (ParseData));
g_variant_builder_init (&parse_data.builder, G_VARIANT_TYPE ("a(ysqiiixia{sv})"));
sk_disk_smart_parse_attributes (d, parse_attr_cb, &parse_data);
G_LOCK (object_lock);
drive->smart_is_from_blob = (simulate_path != NULL);
drive->smart_updated = time (NULL);
drive->smart_failing = !good;
drive->smart_temperature = temp_mkelvin / 1000.0;
drive->smart_power_on_seconds = power_on_msec / 1000.0;
drive->smart_num_attributes_failing = parse_data.num_attributes_failing;
drive->smart_num_attributes_failed_in_the_past = parse_data.num_attributes_failed_in_the_past;
drive->smart_num_bad_sectors = num_bad_sectors;
drive->smart_selftest_status = selftest_status_to_string (data->self_test_execution_status);
drive->smart_selftest_percent_remaining = data->self_test_execution_percent_remaining;
if (drive->smart_attributes != NULL)
g_variant_unref (drive->smart_attributes);
drive->smart_attributes = g_variant_ref_sink (g_variant_builder_end (&parse_data.builder));
G_UNLOCK (object_lock);
update_smart (drive, device);
ret = TRUE;
/* update stats again to account for the IO we just did to read the SMART info */
update_io_stats (drive, device);
/* ensure property changes are sent before the method return */
g_dbus_interface_skeleton_flush (G_DBUS_INTERFACE_SKELETON (drive));
out:
g_clear_object (&device);
if (d != NULL)
sk_disk_free (d);
g_clear_object (&object);
return ret;
}
/* ---------------------------------------------------------------------------------------------------- */
/**
* udisks_linux_drive_ata_smart_selftest_sync:
* @drive: A #UDisksLinuxDriveAta.
* @type: The type of selftest to run.
* @cancellable: (allow-none): A #GCancellable that can be used to cancel the operation or %NULL.
* @error: Return location for error or %NULL.
*
* Starts (or aborts) a SMART self-test on @drive. Valid values for
* @type includes 'short', 'extended', 'conveyance' and 'abort'.
*
* The calling thread is blocked while sending the command to the
* drive but will return immediately after the drive acknowledges the
* command.
*
* Returns: %TRUE if the operation succeed, %FALSE if @error is set.
*/
gboolean
udisks_linux_drive_ata_smart_selftest_sync (UDisksLinuxDriveAta *drive,
const gchar *type,
GCancellable *cancellable,
GError **error)
{
UDisksLinuxDriveObject *object;
UDisksLinuxDevice *device = NULL;
SkDisk *d = NULL;
gboolean ret = FALSE;
SkSmartSelfTest test;
object = udisks_daemon_util_dup_object (drive, error);
if (object == NULL)
goto out;
device = udisks_linux_drive_object_get_device (object, TRUE /* get_hw */);
g_assert (device != NULL);
if (g_strcmp0 (type, "short") == 0)
test = SK_SMART_SELF_TEST_SHORT;
else if (g_strcmp0 (type, "extended") == 0)
test = SK_SMART_SELF_TEST_EXTENDED;
else if (g_strcmp0 (type, "conveyance") == 0)
test = SK_SMART_SELF_TEST_CONVEYANCE;
else if (g_strcmp0 (type, "abort") == 0)
test = SK_SMART_SELF_TEST_ABORT;
else
{
g_set_error (error,
UDISKS_ERROR,
UDISKS_ERROR_FAILED,
"unknown type %s", type);
goto out;
}
if (sk_disk_open (g_udev_device_get_device_file (device->udev_device), &d) != 0)
{
g_set_error (error,
UDISKS_ERROR,
UDISKS_ERROR_FAILED,
"sk_disk_open: %m");
goto out;
}
if (sk_disk_smart_self_test (d, test) != 0)
{
g_set_error (error,
UDISKS_ERROR,
UDISKS_ERROR_FAILED,
"sk_disk_smart_self_test: %m");
goto out;
}
ret = TRUE;
out:
g_clear_object (&device);
if (d != NULL)
sk_disk_free (d);
g_clear_object (&object);
return ret;
}
/* ---------------------------------------------------------------------------------------------------- */
static gboolean
handle_smart_update (UDisksDriveAta *_drive,
GDBusMethodInvocation *invocation,
GVariant *options)
{
UDisksLinuxDriveAta *drive = UDISKS_LINUX_DRIVE_ATA (_drive);
UDisksLinuxDriveObject *object;
UDisksLinuxBlockObject *block_object = NULL;
UDisksDaemon *daemon;
gboolean nowakeup = FALSE;
const gchar *atasmart_blob = NULL;
GError *error;
const gchar *message;
const gchar *action_id;
daemon = NULL;
error = NULL;
object = udisks_daemon_util_dup_object (drive, &error);
if (object == NULL)
{
g_dbus_method_invocation_take_error (invocation, error);
goto out;
}
daemon = udisks_linux_drive_object_get_daemon (object);
block_object = udisks_linux_drive_object_get_block (object, TRUE);
if (block_object == NULL)
{
g_dbus_method_invocation_return_error (invocation,
UDISKS_ERROR,
UDISKS_ERROR_FAILED,
"Unable to find physical block device for drive");
goto out;
}
g_variant_lookup (options, "nowakeup", "b", &nowakeup);
g_variant_lookup (options, "atasmart_blob", "s", &atasmart_blob);
/* Translators: Shown in authentication dialog when the user
* refreshes SMART data from a disk.
*
* Do not translate $(drive), it's a placeholder and
* will be replaced by the name of the drive/device in question
*/
message = N_("Authentication is required to update SMART data from $(drive)");
action_id = "org.freedesktop.udisks2.ata-smart-update";
if (atasmart_blob != NULL)
{
/* Translators: Shown in authentication dialog when the user
* tries to simulate SMART data from a libatasmart blob.
*
* Do not translate $(drive), it's a placeholder and
* will be replaced by the name of the drive/device in question
*/
message = N_("Authentication is required to set SMART data from a blob on $(drive)");
action_id = "org.freedesktop.udisks2.ata-smart-simulate";
}
else
{
if (!udisks_drive_ata_get_smart_supported (UDISKS_DRIVE_ATA (drive)))
{
g_dbus_method_invocation_return_error (invocation,
UDISKS_ERROR,
UDISKS_ERROR_FAILED,
"SMART is not supported");
goto out;
}
if (!udisks_drive_ata_get_smart_enabled (UDISKS_DRIVE_ATA (drive)))
{
g_dbus_method_invocation_return_error (invocation,
UDISKS_ERROR,
UDISKS_ERROR_FAILED,
"SMART is not enabled");
goto out;
}
}
/* Check that the user is authorized */
if (!udisks_daemon_util_check_authorization_sync (daemon,
UDISKS_OBJECT (block_object),
action_id,
options,
message,
invocation))
goto out;
error = NULL;
if (!udisks_linux_drive_ata_refresh_smart_sync (drive,
nowakeup,
atasmart_blob,
NULL, /* cancellable */
&error))
{
udisks_debug ("Error updating ATA smart for %s: %s (%s, %d)",
g_dbus_object_get_object_path (G_DBUS_OBJECT (object)),
error->message, g_quark_to_string (error->domain), error->code);
g_dbus_method_invocation_take_error (invocation, error);
goto out;
}
udisks_drive_ata_complete_smart_update (UDISKS_DRIVE_ATA (drive), invocation);
out:
g_clear_object (&block_object);
g_clear_object (&object);
return TRUE; /* returning TRUE means that we handled the method invocation */
}
/* ---------------------------------------------------------------------------------------------------- */
static gboolean
handle_smart_get_attributes (UDisksDriveAta *_drive,
GDBusMethodInvocation *invocation,
GVariant *options)
{
UDisksLinuxDriveAta *drive = UDISKS_LINUX_DRIVE_ATA (_drive);
G_LOCK (object_lock);
if (drive->smart_attributes == NULL)
{
g_dbus_method_invocation_return_error (invocation,
UDISKS_ERROR,
UDISKS_ERROR_FAILED,
"SMART data not collected");
}
else
{
udisks_drive_ata_complete_smart_get_attributes (UDISKS_DRIVE_ATA (drive), invocation,
drive->smart_attributes);
}
G_UNLOCK (object_lock);
return TRUE; /* returning TRUE means that we handled the method invocation */
}
/* ---------------------------------------------------------------------------------------------------- */
static gboolean
handle_smart_selftest_abort (UDisksDriveAta *_drive,
GDBusMethodInvocation *invocation,
GVariant *options)
{
UDisksLinuxDriveObject *object;
UDisksLinuxBlockObject *block_object = NULL;
UDisksDaemon *daemon;
UDisksLinuxDriveAta *drive = UDISKS_LINUX_DRIVE_ATA (_drive);
GError *error;
error = NULL;
object = udisks_daemon_util_dup_object (drive, &error);
if (object == NULL)
{
g_dbus_method_invocation_take_error (invocation, error);
goto out;
}
daemon = udisks_linux_drive_object_get_daemon (object);
block_object = udisks_linux_drive_object_get_block (object, TRUE);
if (block_object == NULL)
{
g_dbus_method_invocation_return_error (invocation,
UDISKS_ERROR,
UDISKS_ERROR_FAILED,
"Unable to find physical block device for drive");
goto out;
}
if (!udisks_drive_ata_get_smart_supported (UDISKS_DRIVE_ATA (drive)) ||
!udisks_drive_ata_get_smart_enabled (UDISKS_DRIVE_ATA (drive)))
{
g_dbus_method_invocation_return_error (invocation,
UDISKS_ERROR,
UDISKS_ERROR_FAILED,
"SMART is not supported or enabled");
goto out;
}
if (!udisks_daemon_util_check_authorization_sync (daemon,
UDISKS_OBJECT (block_object),
"org.freedesktop.udisks2.ata-smart-selftest",
options,
/* Translators: Shown in authentication dialog when the user
* aborts a running SMART self-test.
*
* Do not translate $(drive), it's a placeholder and
* will be replaced by the name of the drive/device in question
*/
N_("Authentication is required to abort a SMART self-test on $(drive)"),
invocation))
goto out;
error = NULL;
if (!udisks_linux_drive_ata_smart_selftest_sync (drive,
"abort",
NULL, /* cancellable */
&error))
{
udisks_warning ("Error aborting SMART selftest for %s: %s (%s, %d)",
g_dbus_object_get_object_path (G_DBUS_OBJECT (object)),
error->message, g_quark_to_string (error->domain), error->code);
g_dbus_method_invocation_take_error (invocation, error);
goto out;
}
/* This wakes up the selftest thread */
G_LOCK (object_lock);
if (drive->selftest_job != NULL)
{
g_cancellable_cancel (udisks_base_job_get_cancellable (UDISKS_BASE_JOB (drive->selftest_job)));
}
G_UNLOCK (object_lock);
/* TODO: wait for the selftest thread to terminate */
error = NULL;
if (!udisks_linux_drive_ata_refresh_smart_sync (drive,
FALSE, /* nowakeup */
NULL, /* blob */
NULL, /* cancellable */
&error))
{
udisks_warning ("Error updating ATA smart for %s: %s (%s, %d)",
g_dbus_object_get_object_path (G_DBUS_OBJECT (object)),
error->message, g_quark_to_string (error->domain), error->code);
g_dbus_method_invocation_take_error (invocation, error);
goto out;
}
udisks_drive_ata_complete_smart_selftest_abort (UDISKS_DRIVE_ATA (drive), invocation);
out:
g_clear_object (&object);
g_clear_object (&block_object);
return TRUE; /* returning TRUE means that we handled the method invocation */
}
/* ---------------------------------------------------------------------------------------------------- */
static gboolean
selftest_job_func (UDisksThreadedJob *job,
GCancellable *cancellable,
gpointer user_data,
GError **error)
{
UDisksLinuxDriveAta *drive = UDISKS_LINUX_DRIVE_ATA (user_data);
UDisksLinuxDriveObject *object;
gboolean ret = FALSE;
object = udisks_daemon_util_dup_object (drive, error);
if (object == NULL)
goto out;
udisks_job_set_progress_valid (UDISKS_JOB (job), TRUE);
udisks_job_set_progress (UDISKS_JOB (job), 0.0);