[25547] | 1 | # $HeadURL: svn+ssh://svn.mit.edu/moira/trunk/moira/incremental/afs/afs_utils.pl $ $Id: afs_utils.pl 4079 2012-05-23 22:42:25Z jweiss $ |
---|
[23095] | 2 | |
---|
| 3 | use Fcntl; |
---|
| 4 | |
---|
[24319] | 5 | $aklog="/bin/athena/aklog"; |
---|
| 6 | if ( ! -x $aklog ) { $aklog="/usr/bin/aklog"; } |
---|
[23095] | 7 | |
---|
[24319] | 8 | system("$aklog"); |
---|
| 9 | |
---|
[23095] | 10 | $afsbin="/moira/bin"; |
---|
| 11 | $vos="$afsbin/vos"; |
---|
| 12 | $pts="$afsbin/pts"; |
---|
| 13 | $fs="$afsbin/fs"; |
---|
| 14 | $zwrite="/usr/athena/bin/zwrite"; |
---|
[24319] | 15 | if ( ! -x $zwrite ) { $zwrite="/usr/local/bin/zwrite"; } |
---|
[23095] | 16 | |
---|
| 17 | $afs_data="/moira/afs/afs_data"; |
---|
| 18 | $afs_save="$afs_data.tmp"; |
---|
| 19 | |
---|
| 20 | %vtypes_ATHENA_MIT_EDU = |
---|
| 21 | ("ACTIVITY", "activity", |
---|
| 22 | "APROJ", "aproj", |
---|
| 23 | "AREF", "aref", |
---|
| 24 | "CONTRIB", "contrib", |
---|
| 25 | "COURSE", "course", |
---|
| 26 | "HOMEDIR", "user", |
---|
| 27 | "LEASE", "dept", |
---|
| 28 | "ORG", "org", |
---|
| 29 | "PROJECT", "project", |
---|
| 30 | "REF", "ref", |
---|
[25547] | 31 | "SITE", "site", |
---|
[23095] | 32 | "SW", "sw", |
---|
| 33 | "SYSTEM", "system", |
---|
| 34 | "UROP", "urop", |
---|
| 35 | ); |
---|
| 36 | |
---|
| 37 | # File format: |
---|
| 38 | # cell server partition total used alloc |
---|
| 39 | |
---|
| 40 | # Locking/re-write algorithm: |
---|
| 41 | # 1. Open the data file. |
---|
| 42 | # 2. Obtain a lock on the data file. |
---|
| 43 | # 3. Check for the existence of a temporary data file - die if it exists. |
---|
| 44 | # 4. Save current contents into temporary data file. |
---|
| 45 | # 5. Re-write output (with line-buffering). |
---|
| 46 | # 6. Unlink temporary file. |
---|
| 47 | # 7. Unlock data file. |
---|
| 48 | # 8. Close the data file. |
---|
| 49 | |
---|
| 50 | |
---|
| 51 | $flock_t="ssllllllll"; |
---|
| 52 | |
---|
| 53 | sub afs_lock |
---|
| 54 | { |
---|
| 55 | open(SRV,"+<$afs_data") || die "Unable to open $afs_data\n"; |
---|
| 56 | select((select(SRV), $|=1)[$[]); |
---|
| 57 | $flkarr[0]=&F_WRLCK; |
---|
| 58 | $flkarr[1]=$flkarr[2]=$flkarr[3]=$flkarr[4]=$flkarr[5]=$flkarr[6]=0; |
---|
| 59 | $flkarr[7]=$flkarr[8]=$flkarr[9]=0; |
---|
| 60 | $flk=pack($flock_t,@flkarr); |
---|
| 61 | fcntl(SRV, &F_SETLKW, $flk) || die "Unable to lock $afs_data:$!\n"; |
---|
| 62 | die "Temporary status file: $afs_save exists... aborting\n" |
---|
| 63 | if (-f $afs_save); |
---|
| 64 | open(SRV2, ">$afs_save"); |
---|
| 65 | @afs_data = <SRV>; |
---|
| 66 | print SRV2 @afs_data; |
---|
| 67 | close(SRV2); |
---|
| 68 | seek(SRV, 0, 0); |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | sub afs_unlock |
---|
| 72 | { |
---|
| 73 | unlink($afs_save); |
---|
| 74 | close(SRV); |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | # Find server/partition for allocation. |
---|
| 78 | # |
---|
| 79 | # Best fit algorithm used: |
---|
| 80 | # max[ (2*free space) - (unused quota) ] |
---|
| 81 | # = max(2*total - usage - alloc) |
---|
| 82 | # |
---|
| 83 | # Note: This routine does not actually adjust the quota; |
---|
| 84 | # the calling routine should use afs_quota_adj(); |
---|
| 85 | |
---|
| 86 | sub afs_find |
---|
| 87 | { |
---|
| 88 | local($cell,$type,$quota,@except) = @_; |
---|
| 89 | local($j,$k); |
---|
| 90 | local(@max) = ("", "", undef); |
---|
| 91 | |
---|
| 92 | &afs_lock; |
---|
| 93 | chop(@afs_data); |
---|
| 94 | |
---|
| 95 | sloop: |
---|
| 96 | for (@afs_data) { |
---|
| 97 | local ($a, $asrv, $apart, $t, $total, $used, $alloc) = split(/\s+/,$_); |
---|
| 98 | next if ($a ne $cell || !$total || $type !~ /$t/); |
---|
| 99 | for $j (@except) { |
---|
| 100 | next sloop if ($j eq $asrv); |
---|
| 101 | } |
---|
| 102 | $alloc = $used if ($alloc < $used); |
---|
| 103 | $j = 2*$total - $used - $alloc; |
---|
| 104 | @max = ($asrv,$apart,$j) if (!$max[2] || $j > $max[2]); |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | &afs_unlock; |
---|
| 108 | return(@max); |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | # |
---|
| 112 | # Quota adjustments |
---|
| 113 | # |
---|
| 114 | sub afs_quota_adj |
---|
| 115 | { |
---|
| 116 | local($cell,$asrv,$apart,$adj,$dusage) = @_; |
---|
| 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 | $dusage = $used unless ($dusage); |
---|
| 126 | $alloc += $adj; |
---|
| 127 | $_ = join(' ',$c,$asrv,$apart,$t,$total,$dusage,$alloc); |
---|
| 128 | $found = 1; |
---|
| 129 | } |
---|
| 130 | print SRV "$_\n"; |
---|
| 131 | } |
---|
| 132 | &afs_unlock; |
---|
| 133 | return($found); |
---|
| 134 | } |
---|