]> andersk Git - moira.git/blob - incremental/afs_utils.pl
f64579b0f1dbb5be8bca5c7b02f4416bf9ccafd1
[moira.git] / incremental / afs_utils.pl
1 # $Header$
2
3 $afsbin="/moira/bin";
4 $vos="$afsbin/vos";
5 $pts="$afsbin/pts";
6 $fs="$afsbin/fs";
7
8 $afs_data="/moira/afs/afs_data";
9 $afs_save="$afs_data.tmp";
10
11 $LOCK_EX=2;
12 $LOCK_UN=8;
13
14 %vtypes_ATHENA_MIT_EDU =
15     ("ACTIVITY", "activity",
16      "APROJ", "aproj",
17      "AREF", "aref",
18      "CONTRIB", "contrib",
19      "COURSE", "course",
20      "HOMEDIR", "user",
21      "LEASE", "dept",
22      "PROJECT", "project",
23      "REF", "ref",
24      "SW", "sw",
25      "SYSTEM", "system",
26      "UROP", "urop",
27      );
28
29 # File format:
30 #    cell server partition total used alloc
31
32 # Locking/re-write algorithm:
33 # 1. Open the data file.
34 # 2. Obtain a lock on the data file.
35 # 3. Check for the existence of a temporary data file - die if it exists.
36 # 4. Save current contents into temporary data file.
37 # 5. Re-write output (with line-buffering).
38 # 6. Unlink temporary file.
39 # 7. Unlock data file.
40 # 8. Close the data file.
41
42
43 sub afs_lock
44 {
45     open(SRV,"+<$afs_data") || die "Unable to open $afs_data\n";
46     select((select(SRV), $|=1)[$[]);
47     flock(SRV, $LOCK_EX) || die "Unable to lock $afs_data\n";
48     die "Temporary status file: $afs_save exists... aborting\n"
49         if (-f $afs_save);
50     open(SRV2, ">$afs_save");
51     @afs_data = <SRV>;
52     print SRV2 @afs_data;
53     close(SRV2);
54     seek(SRV, 0, 0);
55 }
56
57 sub afs_unlock
58 {
59     unlink($afs_save);
60     close(SRV);
61 }
62
63 # Find server/partition for allocation.
64 #
65 # Best fit algorithm used:
66 #    max[ (2*free space) - (unused quota) ]
67 #    = max(2*total - usage - alloc)
68 #
69 # Note: This routine does not actually adjust the quota; the caller
70 # should use afs_quota_adj();
71
72 sub afs_find
73 {
74     local($cell,$type,$quota,@except) = @_;
75     local($j,$k);
76     local(@max) = ("", "", -10000000);
77
78     &afs_lock;
79     chop(@afs_data);
80
81   sloop:
82     for (@afs_data) {
83         local ($a, $asrv, $apart, $t, $total, $used, $alloc) = split(/\s+/,$_);
84         next if ($a ne $cell || !$total || $type !~ /$t/);
85         for $j (@except) {
86             next sloop if ($j eq $asrv);
87         }
88         $alloc = $used if ($alloc < $used);
89         $j = 2*$total - $used - $alloc;
90         @max = ($asrv,$apart,$j) if (! @max || $j > $max[2]);
91     }
92
93     &afs_unlock;
94     return(@max);
95 }
96
97 #
98 # Quota adjustments
99 #
100 sub afs_quota_adj
101 {
102     local($cell,$asrv,$apart,$adj) = @_;
103     local($found) = 0;
104
105     &afs_lock;
106     chop(@afs_data);
107     truncate(SRV, 0);
108     for (@afs_data) {
109         local ($c, $as, $ap, $t, $total, $used, $alloc) = split(/\s+/,$_);
110         if ($c eq $cell && $as eq $asrv && $ap eq $apart) {
111             $alloc += $adj;
112             $_ = join(' ',$c,$asrv,$apart,$t,$total,$used,$alloc);
113             $found = 1;
114         }
115         print SRV "$_\n";
116     }
117     &afs_unlock;
118     return($found);
119 }
This page took 0.042255 seconds and 3 git commands to generate.