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