]>
Commit | Line | Data |
---|---|---|
6231b320 | 1 | $afsbin="/moira/bin"; |
2 | $vos="$afsbin/vos"; | |
3 | $pts="$afsbin/pts"; | |
4 | $fs="$afsbin/fs"; | |
5 | ||
c09dcc8c | 6 | $afs_data="/moira/afs/afs_data"; |
6231b320 | 7 | $afs_save="$afs_data.tmp"; |
8 | ||
9 | $LOCK_EX=2; | |
10 | $LOCK_UN=8; | |
11 | ||
12 | # File format: | |
13 | # cell server partition total used alloc | |
14 | ||
15 | # Locking/re-write algorithm: | |
16 | # 1. Open the data file. | |
17 | # 2. Obtain a lock on the data file. | |
18 | # 3. Check for the existence of a temporary data file - die if it exists. | |
19 | # 4. Save current contents into temporary data file. | |
20 | # 5. Re-write output (with line-buffering). | |
21 | # 6. Unlink temporary file. | |
22 | # 7. Unlock data file. | |
23 | # 8. Close the data file. | |
24 | ||
25 | ||
26 | sub afs_lock | |
27 | { | |
28 | open(SRV,"+<$afs_data") || die "Unable to open $afs_data\n"; | |
29 | select((select(SRV), $|=1)[$[]); | |
30 | flock(SRV, $LOCK_EX) || die "Unable to lock $afs_data\n"; | |
31 | die "Temporary status file: $afs_save exists... abortin\n" | |
32 | if (-f $afs_save); | |
33 | open(SRV2, ">$afs_save"); | |
34 | @afs_data = <SRV>; | |
35 | print SRV2 @afs_data; | |
36 | close(SRV2); | |
37 | seek(SRV, 0, 0); | |
38 | } | |
39 | ||
40 | sub afs_unlock | |
41 | { | |
42 | unlink($afs_save); | |
43 | close(SRV); | |
44 | } | |
45 | ||
46 | # Find server/partition for allocation. | |
47 | # | |
48 | # Best fit algorithm used: | |
49 | # max[ (2*free space) - (unused quota) ] | |
50 | # = max(2*total - usage - alloc) | |
51 | # | |
52 | # Note: This routine does not actually adjust the quota; the caller | |
53 | # should use afs_quota_adj(); | |
54 | ||
55 | sub afs_find | |
56 | { | |
57 | local($cell,$type,$quota) = @_; | |
58 | local($j); | |
59 | local(@max); | |
60 | ||
61 | &afs_lock; | |
62 | chop(@afs_data); | |
63 | ||
64 | for (@afs_data) { | |
65 | local ($a, $asrv, $apart, $t, $total, $used, $alloc) = split(/\s+/,$_); | |
66 | next if ($a ne $cell || !$total || $type !~ /$t/); | |
67 | $j = 2*$total - $used - $alloc; | |
68 | @max = ($asrv,$apart,$j) if (! @max || $j > $max[2]); | |
69 | } | |
70 | ||
71 | # truncate(SRV, 0); | |
72 | # for (@afs_data) { | |
73 | # ($a, $asrv, $apart, $t, $total, $used, $alloc) = split(/\s+/,$_); | |
74 | # if ($a eq $cell && $asrv eq $max[0] && $apart eq $max[1]) { | |
75 | # $alloc += $quota; | |
76 | # $_ = join(' ',$a,$asrv,$apart,$t, $total,$used,$alloc); | |
77 | # } | |
78 | # print SRV "$_\n"; | |
79 | # } | |
80 | ||
81 | &afs_unlock; | |
82 | return(@max); | |
83 | } | |
84 | ||
85 | # | |
86 | # Quota adjustments | |
87 | # | |
88 | sub afs_quota_adj | |
89 | { | |
90 | local($cell,$asrv,$apart,$adj) = @_; | |
91 | ||
92 | &afs_lock; | |
93 | chop(@afs_data); | |
94 | truncate(SRV, 0); | |
95 | for (@afs_data) { | |
96 | local ($c, $as, $ap, $t, $total, $used, $alloc) = split(/\s+/,$_); | |
97 | if ($c eq $cell && $as eq $asrv && $ap eq $apart) { | |
98 | $alloc += $adj; | |
99 | $_ = join(' ',$c,$asrv,$apart,$t,$total,$used,$alloc); | |
100 | } | |
101 | print SRV "$_\n"; | |
102 | } | |
103 | &afs_unlock; | |
104 | } | |
105 | ||
106 | ||
107 | # | |
108 | # Nightly reaper - Recompute all usages/allocations. | |
109 | # | |
110 | sub afs_nightly | |
111 | { | |
112 | local ($key, @vos, %used,%alloc,%total, %a); | |
113 | local ($c,$as,$ap,$total,$used,$alloc); | |
114 | ||
115 | &afs_lock; | |
116 | truncate(SRV, 0); | |
117 | for (@afs_data) { | |
118 | $as = $ap = 0; | |
119 | if ($_ !~ /^\#/) { | |
120 | ($c,$as,$ap,$t,$total,$used,$alloc) = split(/\s+/,$_); | |
121 | ($as) = gethostbyname($as); | |
122 | $c =~ tr/a-z/A-Z/; | |
123 | $as =~ tr/a-z/A-Z/; | |
124 | $ap =~ s:^([^/]):/vicep\1:; | |
125 | } | |
126 | if (!$as || !$ap) { | |
127 | print SRV "$_\n"; next; | |
128 | } | |
129 | ||
130 | $key = "$c $as $ap"; | |
131 | $total{$key} = $used{$key} = $alloc{$key} = 0; | |
132 | $type{$key} = $t; | |
133 | ||
134 | open(VOS,"$vos partinfo $as $ap -cell $c -noauth|"); | |
135 | chop(@vos = <VOS>); | |
136 | close(VOS); | |
137 | next if ($?); | |
138 | @vos = split(/\s+/,$vos[0]); | |
139 | $total{$key} = pop(@vos); | |
140 | $used{$key} = $total{$key}-$vos[5]; | |
141 | ||
142 | open(VOS,"$vos listvol $as $ap -cell $c -long -noauth|"); | |
143 | @vos = <VOS>; | |
144 | close(VOS); | |
145 | next if ($?); | |
146 | ||
147 | while ($_ = shift(@vos)) { | |
148 | next unless /On-line/; | |
149 | local($vn,$id) = split(/\s+/, $_); | |
150 | shift(@vos); | |
151 | local($p,$p,$p) = split(/\s+/, shift(@vos)); | |
152 | local($q,$q,$q) = split(/\s+/, shift(@vos)); | |
153 | if ($id==$p || !$a{$p}) { | |
154 | $a{$p} = $q; | |
155 | } | |
156 | } | |
157 | for $p (keys %a) { $alloc{$key} += $a{$p}; delete $a{$p}; } | |
158 | } | |
159 | ||
160 | for (sort keys %total) { | |
161 | print SRV "$_ $type{$_} $total{$_} $used{$_} $alloc{$_}\n"; | |
162 | delete $type{$_}; | |
163 | delete $total{$_}; | |
164 | delete $used{$_}; | |
165 | delete $alloc{$_}; | |
166 | } | |
167 | ||
168 | &afs_unlock; | |
169 | } |