]> andersk Git - moira.git/blob - incremental/afs_utils.pl
Added a couple functions: afs_vname, afs_partinfo
[moira.git] / incremental / afs_utils.pl
1 $afsbin="/moira/bin";
2 $vos="$afsbin/vos";
3 $pts="$afsbin/pts";
4 $fs="$afsbin/fs";
5
6 $afs_data="/moira/afs/afs_data";
7 $afs_save="$afs_data.tmp";
8
9 $LOCK_EX=2;
10 $LOCK_UN=8;
11
12 %vtypes_ATHENA_MIT_EDU =
13     ("ACTIVITY", "activity",
14      "APROJ", "aproj",
15      "AREF", "aref",
16      "CONTRIB", "contrib",
17      "COURSE", "course",
18      "HOMEDIR", "user",
19      "PROJECT", "project",
20      "REF", "ref",
21      "SW", "sw",
22      "SYSTEM", "system",
23      "UROP", "urop",
24      );
25
26 # Function: afs_vname
27 # Returns the canonical volume name for (locker,type,cell)
28 sub afs_vname
29 {
30     local($name,$type,$cell) = @_;
31
32     $vtype = eval "\$vtypes_$cell{$type}";
33     return "" unless $vtype;
34
35     $vname = $vtype . "." . $name;
36     $vname =~ s/[^-A-Za-z0-9_.]//g;             # strip out illegal characters
37     return $vname;
38 }
39
40
41 # File format:
42 #    cell server partition total used alloc
43
44 # Locking/re-write algorithm:
45 # 1. Open the data file.
46 # 2. Obtain a lock on the data file.
47 # 3. Check for the existence of a temporary data file - die if it exists.
48 # 4. Save current contents into temporary data file.
49 # 5. Re-write output (with line-buffering).
50 # 6. Unlink temporary file.
51 # 7. Unlock data file.
52 # 8. Close the data file.
53
54
55 sub afs_lock
56 {
57     open(SRV,"+<$afs_data") || die "Unable to open $afs_data\n";
58     select((select(SRV), $|=1)[$[]);
59     flock(SRV, $LOCK_EX) || die "Unable to lock $afs_data\n";
60     die "Temporary status file: $afs_save exists... aborting\n"
61         if (-f $afs_save);
62     open(SRV2, ">$afs_save");
63     @afs_data = <SRV>;
64     print SRV2 @afs_data;
65     close(SRV2);
66     seek(SRV, 0, 0);
67 }
68
69 sub afs_unlock
70 {
71     unlink($afs_save);
72     close(SRV);
73 }
74
75 # Function: afs_find
76 # Finds server/partition for allocation.
77 #
78 # Best fit algorithm used:
79 #    max[ (2*free space) - (unused quota) ]
80 #    = max(2*total - usage - alloc)
81 #
82 # Note: This routine does not actually adjust the quota; the caller
83 # should use afs_quota_adj();
84 sub afs_find
85 {
86     local($cell,$type,$quota) = @_;
87     local($i, $j, $vos, $a);
88     local(@max) = '';
89
90     &afs_lock;
91     chop(@afs_data);
92
93     for $i ($[ .. $#afs_data) {
94         ($a, $asrv, $apart, $t, $total, $used, $alloc) =
95             split(/\s+/, $afs_data[$i]);
96         next if ($a ne $cell || !$total || $type !~ /$t/);
97         $alloc = $used if ($alloc < $used);
98         $j = 2*$total - $used - $alloc;
99         if (! @max || $j > $max[2]) {
100             ($total, $used) = &afs_partinfo($asrv, $apart, $cell);
101             next if ($?);
102             $afs_data[$i]=join(' ',$cell,$asrv,$apart,$t,$total,$used,$alloc);
103             @max = ($asrv,$apart,$j);
104         }
105     }
106
107     &afs_unlock;
108     return(@max);
109 }
110
111
112 # Function: afs_quota_adj
113 # Adjusts the quota allocation for a given server/partition
114 sub afs_quota_adj
115 {
116     local($cell,$asrv,$apart,$adj) = @_;
117     local($found) = 0;
118
119     &afs_lock;
120     chop(@afs_data);
121     truncate(SRV, 0);
122     for (@afs_data) {
123         local ($c, $as, $ap, $t, $total, $used, $alloc) = split(/\s+/,$_);
124         if ($c eq $cell && $as eq $asrv && $ap eq $apart) {
125             $alloc += $adj;
126             $_ = join(' ',$c,$asrv,$apart,$t,$total,$used,$alloc);
127             $found = 1;
128         }
129         print SRV "$_\n";
130     }
131     &afs_unlock;
132     return($found);
133 }
134
135
136 sub afs_partinfo
137 {
138     local($as, $ap, $c) = @_;
139     local(@vos, $total, $used);
140
141     open(VOS,"$vos partinfo $as $ap -cell $c -noauth|");
142     chop(@vos = <VOS>);
143     close(VOS);
144     return "" if ($?);
145
146     @vos = split(/\s+/,$vos[0]);
147     $total = pop(@vos);
148     $used = $total-$vos[5];
149     return ($total,$used);
150 }
This page took 0.049791 seconds and 5 git commands to generate.