#!/usr/local/bin/perl
# 
# $Header: gitools/src/asm/asru/1.0/zerofill /main/2 2011/08/08 18:46:49 pvenkatr Exp $
#
# zerofill.pl
# 
# Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved. 
#
#    NAME
#      zerofill.pl - The script used to free space in thin provisioning.
#
#    DESCRIPTION
#      The zerofill script takes all the disks and their ranges as arguments
#      and an argument power which specifies the parallelism to be employed.
#      The unit for ranges is megabytes. It fills up the disks with zeroes 
#      from start_offset to end_offset.
#
#    NOTES
#      usage: zerofill <power> <dsk1> <start_offset1> <end_offset1>
#      <dsk2> <start_offset2> <end_offset2>
#
#    MODIFIED   (MM/DD/YY)
#    moreddy     02/14/10 - Creation
# 

use strict;

########
# NAME
#   zerofill_main
#
# DESCRIPTION
#   This function is the main function. It is the first
#   function that is called.
#
# PARAMETERS
#   @ARGV (IN) - list of all command line arguments for zerofill script.
#
# RETURNS
#   Null.
#
# NOTES
#   This function calls zerofill_execute().
#
########


sub zerofill_main
{
  my ( @dsks, @start, @end );
  my ($i, $power);

  open (TRACEFILE, '>>ASRU.trc');
  if( @ARGV < 3)
  {
    print "Usage: zerofill <power> <dsk1> <start_offset1> "
    ."<end_offset1> <dsk2> <start_offset2> <end_offset2>\n";
    return;
  }
  $power = $ARGV[0];
 
  for($i = 1; $i< $#ARGV+1; $i++)
  {
    push(@dsks, $ARGV[$i]);
    $i += 1;
    if(defined $ARGV[$i])
    {
      push (@start, $ARGV[$i]);
    }
    else
    {
      print "Usage: zerofill <power> <dsk1> <start_offset1> "
      ."<end_offset1> <dsk2> <start_offset2> <end_offset2>\n";
      return;
    }
    $i += 1;
    if(defined $ARGV[$i])
    {
      push (@end, $ARGV[$i]);
    }
    else
    {
      print "Usage: zerofill <power> <dsk1> <start_offset1> "
      ."<end_offset1> <dsk2> <start_offset2> <end_offset2>\n";
      return;
    }
  }
  print TRACEFILE "Completed parsing disk and their ranges which are to ".
         "be zeroed\n";
  zerofill_execute($power, \@dsks, \@start, \@end);
}
zerofill_main();

########
# NAME
#   zerofill_execute
#
# DESCRIPTION
#   This function fill zeroes in the disks from start_offset and end_offset.
#   It fills up zeroes in disks in parallel with power equal to the power given
#   as argument to the script.
#
# PARAMETERS
#   @ARGV (IN) - list of all command line arguments for zerofill script.
#
# RETURNS
#   Null.
#
########
sub zerofill_execute
{
  my ($power, $dsks, $start, $end) = @_;
  my ($i, $k, $free_disk, $fill, $pid, $execute, $power2, $power3);
  my (@children);
  my ($proc) = 0;

  my $dd = "/bin/dd";
  my $zero = "/dev/zero";

   if (! -x $dd)
   {
     print "dd binary not found at $dd. \n";
     return;
   }

  if (! -e $zero)
  {
    print "$zero not found. \n";
    return;
  }

  for ($i = -1; $i < $#{$dsks}; )
  {
    $proc++;

    if($i+$power < $#{$dsks}+1) 
    {
      $power2 = $power;
    }
    else
    {
      $power2 = $#{$dsks} - $i;
    }
    $power3 = $power2;

    print TRACEFILE "Batch number $proc started\n";

    while ($power2 > 0)
    { 
      $i +=1;
      $pid = fork();
      if($pid) 
      {
        # parent
        push(@children, $pid);
      }
      elsif ($pid == 0) 
      {
        # child

        $fill = $$end[$i] - $$start[$i];

        $free_disk = "$dd if=$zero of=$$dsks[$i] seek=$$start[$i] bs=1024k".
                     " count=$fill";
        $free_disk =~ /([^\n^\r^\t]+)/;
        $free_disk = $1;
        print TRACEFILE "Executing $free_disk\n";
        eval
        {
          $execute = `$free_disk`;
        };

        if($@ ne '')
        { 
          print "Error: $@\n";
          return;
        }
 
        exit(0);
      }
      else 
      {
        die "Could not fork new process\n";
      }
      $power2 -= 1;
    }
  
    for($k = 1; $k < $power3+1; $k++)
    {
      waitpid($children[$i - $power3 + $k], 0);
    }
    
    print TRACEFILE "Batch number $proc ended\n";
  }
  close (TRACEFILE);
}
