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> "
514
- "[<device> ...]\n" ));
513
+ return (gettext ("\tinitialize [-c | -s | -u] [-w] <-a | < pool> "
514
+ "[<device> ...]> \n" ));
515
515
case HELP_SCRUB :
516
- return (gettext ("\tscrub [-e | -s | -p | -C] [-w] "
517
- "<pool> ...\n" ));
516
+ return (gettext ("\tscrub [-e | -s | -p | -C] [-w] <-a | "
517
+ "<pool> [<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,22 +767,26 @@ 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
791
if (cmd_type != POOL_INITIALIZE_START &&
815
792
cmd_type != POOL_INITIALIZE_CANCEL ) {
@@ -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 pools 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 );
@@ -8452,10 +8445,14 @@ zpool_do_scrub(int argc, char **argv)
8452
8445
boolean_t is_pause = B_FALSE ;
8453
8446
boolean_t is_stop = B_FALSE ;
8454
8447
boolean_t is_txg_continue = B_FALSE ;
8448
+ boolean_t scrub_all = B_FALSE ;
8455
8449
8456
8450
/* check options */
8457
- while ((c = getopt (argc , argv , "spweC " )) != -1 ) {
8451
+ while ((c = getopt (argc , argv , "aspweC " )) != -1 ) {
8458
8452
switch (c ) {
8453
+ case 'a' :
8454
+ scrub_all = B_TRUE ;
8455
+ break ;
8459
8456
case 'e' :
8460
8457
is_error_scrub = B_TRUE ;
8461
8458
break ;
@@ -8519,7 +8516,7 @@ zpool_do_scrub(int argc, char **argv)
8519
8516
argc -= optind ;
8520
8517
argv += optind ;
8521
8518
8522
- if (argc < 1 ) {
8519
+ if (argc < 1 && ! scrub_all ) {
8523
8520
(void ) fprintf (stderr , gettext ("missing pool name argument\n" ));
8524
8521
usage (B_FALSE );
8525
8522
}
@@ -8591,18 +8588,24 @@ zpool_do_trim(int argc, char **argv)
8591
8588
{"rate" , required_argument , NULL , 'r' },
8592
8589
{"suspend" , no_argument , NULL , 's' },
8593
8590
{"wait" , no_argument , NULL , 'w' },
8591
+ {"all" , no_argument , NULL , 'a' },
8594
8592
{0 , 0 , 0 , 0 }
8595
8593
};
8596
8594
8597
8595
pool_trim_func_t cmd_type = POOL_TRIM_START ;
8598
8596
uint64_t rate = 0 ;
8599
8597
boolean_t secure = B_FALSE ;
8600
8598
boolean_t wait = B_FALSE ;
8599
+ boolean_t trimall = B_FALSE ;
8600
+ int error ;
8601
8601
8602
8602
int c ;
8603
- while ((c = getopt_long (argc , argv , "cdr :sw" , long_options , NULL ))
8603
+ while ((c = getopt_long (argc , argv , "acdr :sw" , long_options , NULL ))
8604
8604
!= -1 ) {
8605
8605
switch (c ) {
8606
+ case 'a' :
8607
+ trimall = B_TRUE ;
8608
+ break ;
8606
8609
case 'c' :
8607
8610
if (cmd_type != POOL_TRIM_START &&
8608
8611
cmd_type != POOL_TRIM_CANCEL ) {
@@ -8661,49 +8664,65 @@ zpool_do_trim(int argc, char **argv)
8661
8664
argc -= optind ;
8662
8665
argv += optind ;
8663
8666
8664
- if (argc < 1 ) {
8667
+ trimflags_t trim_flags = {
8668
+ .secure = secure ,
8669
+ .rate = rate ,
8670
+ .wait = wait ,
8671
+ };
8672
+
8673
+ trim_cbdata_t cbdata = {
8674
+ .trim_flags = trim_flags ,
8675
+ .cmd_type = cmd_type
8676
+ };
8677
+
8678
+ if (argc < 1 && !trimall ) {
8665
8679
(void ) fprintf (stderr , gettext ("missing pool name argument\n" ));
8666
8680
usage (B_FALSE );
8667
8681
return (-1 );
8668
8682
}
8669
8683
8670
8684
if (wait && (cmd_type != POOL_TRIM_START )) {
8671
8685
(void ) fprintf (stderr , gettext ("-w cannot be used with -c or "
8672
- "-s\n" ));
8686
+ "-s options \n" ));
8673
8687
usage (B_FALSE );
8674
8688
}
8675
8689
8676
- char * poolname = argv [0 ];
8677
- zpool_handle_t * zhp = zpool_open (g_zfs , poolname );
8678
- if (zhp == NULL )
8679
- return (-1 );
8680
-
8681
- trimflags_t trim_flags = {
8682
- .secure = secure ,
8683
- .rate = rate ,
8684
- .wait = wait ,
8685
- };
8690
+ if (trimall && argc > 0 ) {
8691
+ (void ) fprintf (stderr , gettext ("-a cannot be combined with "
8692
+ "individual zpools or vdevs\n" ));
8693
+ usage (B_FALSE );
8694
+ }
8686
8695
8687
- nvlist_t * vdevs = fnvlist_alloc ();
8688
- if (argc == 1 ) {
8696
+ if (argc == 0 && trimall ) {
8697
+ cbdata .trim_flags .fullpool = B_TRUE ;
8698
+ /* Trim each pool recursively */
8699
+ error = for_each_pool (argc , argv , B_TRUE , NULL , ZFS_TYPE_POOL ,
8700
+ B_FALSE , zpool_trim_one , & cbdata );
8701
+ } else if (argc == 1 ) {
8702
+ char * poolname = argv [0 ];
8703
+ zpool_handle_t * zhp = zpool_open (g_zfs , poolname );
8704
+ if (zhp == NULL )
8705
+ return (-1 );
8689
8706
/* no individual leaf vdevs specified, so add them all */
8690
- nvlist_t * config = zpool_get_config (zhp , NULL );
8691
- nvlist_t * nvroot = fnvlist_lookup_nvlist (config ,
8692
- ZPOOL_CONFIG_VDEV_TREE );
8693
- zpool_collect_leaves (zhp , nvroot , vdevs );
8694
- trim_flags .fullpool = B_TRUE ;
8707
+ error = zpool_trim_one (zhp , & cbdata );
8708
+ zpool_close (zhp );
8695
8709
} else {
8696
- trim_flags .fullpool = B_FALSE ;
8710
+ char * poolname = argv [0 ];
8711
+ zpool_handle_t * zhp = zpool_open (g_zfs , poolname );
8712
+ if (zhp == NULL )
8713
+ return (-1 );
8714
+ /* leaf vdevs specified, trim only those */
8715
+ cbdata .trim_flags .fullpool = B_FALSE ;
8716
+ nvlist_t * vdevs = fnvlist_alloc ();
8697
8717
for (int i = 1 ; i < argc ; i ++ ) {
8698
8718
fnvlist_add_boolean (vdevs , argv [i ]);
8699
8719
}
8720
+ error = zpool_trim (zhp , cbdata .cmd_type , vdevs ,
8721
+ & cbdata .trim_flags );
8722
+ fnvlist_free (vdevs );
8723
+ zpool_close (zhp );
8700
8724
}
8701
8725
8702
- int error = zpool_trim (zhp , cmd_type , vdevs , & trim_flags );
8703
-
8704
- fnvlist_free (vdevs );
8705
- zpool_close (zhp );
8706
-
8707
8726
return (error );
8708
8727
}
8709
8728
0 commit comments