34
34
* Copyright (c) 2019, loli10K <[email protected] >
35
35
* Copyright (c) 2021, Colm Buckley <[email protected] >
36
36
* Copyright (c) 2021, 2023, Klara Inc.
37
- * Copyright [ 2021] Hewlett Packard Enterprise Development LP
37
+ * Copyright (c) 2021, 2025 Hewlett Packard Enterprise Development LP.
38
38
*/
39
39
40
40
#include <assert.h>
@@ -510,16 +510,16 @@ get_usage(zpool_help_t idx)
510
510
case HELP_REOPEN :
511
511
return (gettext ("\treopen [-n] <pool>\n" ));
512
512
case HELP_INITIALIZE :
513
- return (gettext ("\tinitialize [-c | -s | -u] [-w] <pool> "
513
+ return (gettext ("\tinitialize [-c | -s | -u] [-w] <-a | pool> "
514
514
"[<device> ...]\n" ));
515
515
case HELP_SCRUB :
516
- return (gettext ("\tscrub [-e | -s | -p | -C] [-w] "
516
+ return (gettext ("\tscrub [-e | -s | -p | -C] [-w] <-a | pool> "
517
517
"<pool> ...\n" ));
518
518
case HELP_RESILVER :
519
519
return (gettext ("\tresilver <pool> ...\n" ));
520
520
case HELP_TRIM :
521
- return (gettext ("\ttrim [-dw] [-r <rate>] [-c | -s] <pool> "
522
- "[<device> ...]\n" ));
521
+ return (gettext ("\ttrim [-dw] [-r <rate>] [-c | -s] "
522
+ "<-a | pool> [<device> ...]\n" ));
523
523
case HELP_STATUS :
524
524
return (gettext ("\tstatus [-DdegiLPpstvx] "
525
525
"[-c script1[,script2,...]] ...\n"
@@ -560,33 +560,6 @@ get_usage(zpool_help_t idx)
560
560
}
561
561
}
562
562
563
- static void
564
- zpool_collect_leaves (zpool_handle_t * zhp , nvlist_t * nvroot , nvlist_t * res )
565
- {
566
- uint_t children = 0 ;
567
- nvlist_t * * child ;
568
- uint_t i ;
569
-
570
- (void ) nvlist_lookup_nvlist_array (nvroot , ZPOOL_CONFIG_CHILDREN ,
571
- & child , & children );
572
-
573
- if (children == 0 ) {
574
- char * path = zpool_vdev_name (g_zfs , zhp , nvroot ,
575
- VDEV_NAME_PATH );
576
-
577
- if (strcmp (path , VDEV_TYPE_INDIRECT ) != 0 &&
578
- strcmp (path , VDEV_TYPE_HOLE ) != 0 )
579
- fnvlist_add_boolean (res , path );
580
-
581
- free (path );
582
- return ;
583
- }
584
-
585
- for (i = 0 ; i < children ; i ++ ) {
586
- zpool_collect_leaves (zhp , child [i ], res );
587
- }
588
- }
589
-
590
563
/*
591
564
* Callback routine that will print out a pool property value.
592
565
*/
@@ -794,34 +767,38 @@ zpool_do_initialize(int argc, char **argv)
794
767
int c ;
795
768
char * poolname ;
796
769
zpool_handle_t * zhp ;
797
- nvlist_t * vdevs ;
798
770
int err = 0 ;
799
771
boolean_t wait = B_FALSE ;
772
+ boolean_t initialize_all = B_FALSE ;
800
773
801
774
struct option long_options [] = {
802
775
{"cancel" , no_argument , NULL , 'c' },
803
776
{"suspend" , no_argument , NULL , 's' },
804
777
{"uninit" , no_argument , NULL , 'u' },
805
778
{"wait" , no_argument , NULL , 'w' },
779
+ {"all" , no_argument , NULL , 'a' },
806
780
{0 , 0 , 0 , 0 }
807
781
};
808
782
809
783
pool_initialize_func_t cmd_type = POOL_INITIALIZE_START ;
810
- while ((c = getopt_long (argc , argv , "csuw " , long_options ,
784
+ while ((c = getopt_long (argc , argv , "acsuw " , long_options ,
811
785
NULL )) != -1 ) {
812
786
switch (c ) {
787
+ case 'a' :
788
+ initialize_all = B_TRUE ;
789
+ break ;
813
790
case 'c' :
814
- if (cmd_type != POOL_INITIALIZE_START &&
815
- cmd_type != POOL_INITIALIZE_CANCEL ) {
791
+ if (( cmd_type != POOL_INITIALIZE_START &&
792
+ cmd_type != POOL_INITIALIZE_CANCEL )) {
816
793
(void ) fprintf (stderr , gettext ("-c cannot be "
817
794
"combined with other options\n" ));
818
795
usage (B_FALSE );
819
796
}
820
797
cmd_type = POOL_INITIALIZE_CANCEL ;
821
798
break ;
822
799
case 's' :
823
- if (cmd_type != POOL_INITIALIZE_START &&
824
- cmd_type != POOL_INITIALIZE_SUSPEND ) {
800
+ if (( cmd_type != POOL_INITIALIZE_START &&
801
+ cmd_type != POOL_INITIALIZE_SUSPEND )) {
825
802
(void ) fprintf (stderr , gettext ("-s cannot be "
826
803
"combined with other options\n" ));
827
804
usage (B_FALSE );
@@ -856,7 +833,18 @@ zpool_do_initialize(int argc, char **argv)
856
833
argc -= optind ;
857
834
argv += optind ;
858
835
859
- if (argc < 1 ) {
836
+ initialize_cbdata_t cbdata = {
837
+ .wait = wait ,
838
+ .cmd_type = cmd_type
839
+ };
840
+
841
+ if (initialize_all && argc > 0 ) {
842
+ (void ) fprintf (stderr , gettext ("-a cannot be combined with "
843
+ "individual zpools or vdevs\n" ));
844
+ usage (B_FALSE );
845
+ }
846
+
847
+ if (argc < 1 && !initialize_all ) {
860
848
(void ) fprintf (stderr , gettext ("missing pool name argument\n" ));
861
849
usage (B_FALSE );
862
850
return (-1 );
@@ -868,30 +856,35 @@ zpool_do_initialize(int argc, char **argv)
868
856
usage (B_FALSE );
869
857
}
870
858
871
- poolname = argv [ 0 ];
872
- zhp = zpool_open ( g_zfs , poolname );
873
- if ( zhp == NULL )
874
- return ( -1 );
875
-
876
- vdevs = fnvlist_alloc ();
877
- if ( argc == 1 ) {
878
- /* no individual leaf vdevs specified, so add them all */
879
- nvlist_t * config = zpool_get_config ( zhp , NULL );
880
- nvlist_t * nvroot = fnvlist_lookup_nvlist ( config ,
881
- ZPOOL_CONFIG_VDEV_TREE );
882
- zpool_collect_leaves (zhp , nvroot , vdevs );
859
+ if ( argc == 0 && initialize_all ) {
860
+ /* Initilize each pool recursively */
861
+ err = for_each_pool ( argc , argv , B_TRUE , NULL , ZFS_TYPE_POOL ,
862
+ B_FALSE , zpool_initialize_one , & cbdata );
863
+ return ( err );
864
+ } else if ( argc == 1 ) {
865
+ /* no individual leaf vdevs specified, initialize the pool */
866
+ poolname = argv [ 0 ];
867
+ zhp = zpool_open ( g_zfs , poolname );
868
+ if ( zhp == NULL )
869
+ return ( -1 );
870
+ err = zpool_initialize_one (zhp , & cbdata );
883
871
} else {
872
+ /* individual leaf vdevs specified, initialize them */
873
+ poolname = argv [0 ];
874
+ zhp = zpool_open (g_zfs , poolname );
875
+ if (zhp == NULL )
876
+ return (-1 );
877
+ nvlist_t * vdevs = fnvlist_alloc ();
884
878
for (int i = 1 ; i < argc ; i ++ ) {
885
879
fnvlist_add_boolean (vdevs , argv [i ]);
886
880
}
881
+ if (wait )
882
+ err = zpool_initialize_wait (zhp , cmd_type , vdevs );
883
+ else
884
+ err = zpool_initialize (zhp , cmd_type , vdevs );
885
+ fnvlist_free (vdevs );
887
886
}
888
887
889
- if (wait )
890
- err = zpool_initialize_wait (zhp , cmd_type , vdevs );
891
- else
892
- err = zpool_initialize (zhp , cmd_type , vdevs );
893
-
894
- fnvlist_free (vdevs );
895
888
zpool_close (zhp );
896
889
897
890
return (err );
@@ -8454,10 +8447,14 @@ zpool_do_scrub(int argc, char **argv)
8454
8447
boolean_t is_pause = B_FALSE ;
8455
8448
boolean_t is_stop = B_FALSE ;
8456
8449
boolean_t is_txg_continue = B_FALSE ;
8450
+ boolean_t scrub_all = B_FALSE ;
8457
8451
8458
8452
/* check options */
8459
- while ((c = getopt (argc , argv , "spweC " )) != -1 ) {
8453
+ while ((c = getopt (argc , argv , "aspweC " )) != -1 ) {
8460
8454
switch (c ) {
8455
+ case 'a' :
8456
+ scrub_all = B_TRUE ;
8457
+ break ;
8461
8458
case 'e' :
8462
8459
is_error_scrub = B_TRUE ;
8463
8460
break ;
@@ -8521,7 +8518,7 @@ zpool_do_scrub(int argc, char **argv)
8521
8518
argc -= optind ;
8522
8519
argv += optind ;
8523
8520
8524
- if (argc < 1 ) {
8521
+ if (argc < 1 && ! scrub_all ) {
8525
8522
(void ) fprintf (stderr , gettext ("missing pool name argument\n" ));
8526
8523
usage (B_FALSE );
8527
8524
}
@@ -8593,18 +8590,24 @@ zpool_do_trim(int argc, char **argv)
8593
8590
{"rate" , required_argument , NULL , 'r' },
8594
8591
{"suspend" , no_argument , NULL , 's' },
8595
8592
{"wait" , no_argument , NULL , 'w' },
8593
+ {"all" , no_argument , NULL , 'a' },
8596
8594
{0 , 0 , 0 , 0 }
8597
8595
};
8598
8596
8599
8597
pool_trim_func_t cmd_type = POOL_TRIM_START ;
8600
8598
uint64_t rate = 0 ;
8601
8599
boolean_t secure = B_FALSE ;
8602
8600
boolean_t wait = B_FALSE ;
8601
+ boolean_t trimall = B_FALSE ;
8602
+ int error ;
8603
8603
8604
8604
int c ;
8605
- while ((c = getopt_long (argc , argv , "cdr :sw" , long_options , NULL ))
8605
+ while ((c = getopt_long (argc , argv , "acdr :sw" , long_options , NULL ))
8606
8606
!= -1 ) {
8607
8607
switch (c ) {
8608
+ case 'a' :
8609
+ trimall = B_TRUE ;
8610
+ break ;
8608
8611
case 'c' :
8609
8612
if (cmd_type != POOL_TRIM_START &&
8610
8613
cmd_type != POOL_TRIM_CANCEL ) {
@@ -8663,49 +8666,65 @@ zpool_do_trim(int argc, char **argv)
8663
8666
argc -= optind ;
8664
8667
argv += optind ;
8665
8668
8666
- if (argc < 1 ) {
8669
+ trimflags_t trim_flags = {
8670
+ .secure = secure ,
8671
+ .rate = rate ,
8672
+ .wait = wait ,
8673
+ };
8674
+
8675
+ trim_cbdata_t cbdata = {
8676
+ .trim_flags = trim_flags ,
8677
+ .cmd_type = cmd_type
8678
+ };
8679
+
8680
+ if (argc < 1 && !trimall ) {
8667
8681
(void ) fprintf (stderr , gettext ("missing pool name argument\n" ));
8668
8682
usage (B_FALSE );
8669
8683
return (-1 );
8670
8684
}
8671
8685
8672
8686
if (wait && (cmd_type != POOL_TRIM_START )) {
8673
8687
(void ) fprintf (stderr , gettext ("-w cannot be used with -c or "
8674
- "-s\n" ));
8688
+ "-s options \n" ));
8675
8689
usage (B_FALSE );
8676
8690
}
8677
8691
8678
- char * poolname = argv [0 ];
8679
- zpool_handle_t * zhp = zpool_open (g_zfs , poolname );
8680
- if (zhp == NULL )
8681
- return (-1 );
8682
-
8683
- trimflags_t trim_flags = {
8684
- .secure = secure ,
8685
- .rate = rate ,
8686
- .wait = wait ,
8687
- };
8692
+ if (trimall && argc > 0 ) {
8693
+ (void ) fprintf (stderr , gettext ("-a cannot be combined with "
8694
+ "individual zpools or vdevs\n" ));
8695
+ usage (B_FALSE );
8696
+ }
8688
8697
8689
- nvlist_t * vdevs = fnvlist_alloc ();
8690
- if (argc == 1 ) {
8698
+ if (argc == 0 && trimall ) {
8699
+ cbdata .trim_flags .fullpool = B_TRUE ;
8700
+ /* Trim each pool recursively */
8701
+ error = for_each_pool (argc , argv , B_TRUE , NULL , ZFS_TYPE_POOL ,
8702
+ B_FALSE , zpool_trim_one , & cbdata );
8703
+ } else if (argc == 1 ) {
8704
+ char * poolname = argv [0 ];
8705
+ zpool_handle_t * zhp = zpool_open (g_zfs , poolname );
8706
+ if (zhp == NULL )
8707
+ return (-1 );
8691
8708
/* no individual leaf vdevs specified, so add them all */
8692
- nvlist_t * config = zpool_get_config (zhp , NULL );
8693
- nvlist_t * nvroot = fnvlist_lookup_nvlist (config ,
8694
- ZPOOL_CONFIG_VDEV_TREE );
8695
- zpool_collect_leaves (zhp , nvroot , vdevs );
8696
- trim_flags .fullpool = B_TRUE ;
8709
+ error = zpool_trim_one (zhp , & cbdata );
8710
+ zpool_close (zhp );
8697
8711
} else {
8698
- trim_flags .fullpool = B_FALSE ;
8712
+ char * poolname = argv [0 ];
8713
+ zpool_handle_t * zhp = zpool_open (g_zfs , poolname );
8714
+ if (zhp == NULL )
8715
+ return (-1 );
8716
+ /* leaf vdevs specified, trim only those */
8717
+ cbdata .trim_flags .fullpool = B_FALSE ;
8718
+ nvlist_t * vdevs = fnvlist_alloc ();
8699
8719
for (int i = 1 ; i < argc ; i ++ ) {
8700
8720
fnvlist_add_boolean (vdevs , argv [i ]);
8701
8721
}
8722
+ error = zpool_trim (zhp , cbdata .cmd_type , vdevs ,
8723
+ & cbdata .trim_flags );
8724
+ fnvlist_free (vdevs );
8725
+ zpool_close (zhp );
8702
8726
}
8703
8727
8704
- int error = zpool_trim (zhp , cmd_type , vdevs , & trim_flags );
8705
-
8706
- fnvlist_free (vdevs );
8707
- zpool_close (zhp );
8708
-
8709
8728
return (error );
8710
8729
}
8711
8730
0 commit comments