01/26/96 Detection and Correction of Illegal Hard Links SPECIAL NOTICES Information in this document is correct to the best of our knowledge at the time of this writing. Please send feedback by fax to "AIXServ Information" at (512) 823-4009. Please use this information with care. IBM will not be responsible for damages of any kind resulting from its use. The use of this information is the sole responsibility of the customer and depends on the customer's ability to eval- uate and integrate this information into the customer's operational environment. +----------------------------------------------------------+ | | | NOTE: The information in this document is NOT appli- | | cable for AIX 4.1. | | | +----------------------------------------------------------+ ABOUT THIS DOCUMENT This document describes the cause, symptom, detection, and correction of illegal hard links in AIX 3.2. CAUSE OF AN ILLEGAL HARD LINK Hard links can be made with both the ln and link commands, but only the link command allows the creation of illegal hard links. The link command can only be run by root. One use of the link command is to repair file systems. For example, it can be used to restore a missing "." or ".." entry. Its ability to do this is what gives it the ability to create illegal hard links. SYMPTOM OF AN ILLEGAL HARD LINK Certain illegal hard links between directories can cause the find command to report find: 0652-019 The status on is not valid on all files and directories it finds after the illegal hard link. Note that there may be other causes of this error, such as a missing "." or ".." entry. DETECTION METHODS One method for finding the first illegal hard link is to run "ls -f1" in the directory where the error first occurs to see the entries in the order that find sees them. The directory entry before the first entry that gets the find 0652-019 error should be the one that has the illegal hard link somewhere in its tree. Detection and Correction of Illegal Hard Links 1 01/26/96 The above method only finds the first illegal hard link. There may be others, and the following script will find them all. The technique of the script is to traverse the directory structure of all local file systems (not including NFS mounted file systems and AFS), comparing the i-node number of each directory's parent to the i-node number of its ".." directory. The i-node numbers will be different for an illegal hard link that produces the 0652-019 error. Note that the "." and ".." directories qualify as this type of hard link -- they are a special case that is not illegal and is not checked by the shell script. Recommended Use of Shell Script o Run as root. o Run on the root directory o Mount all local file systems so each will be checked. o Redirect the output to a file. A very rough estimate of the output file size: a test on a small installation 530 used 60 KB and a test on a medium-sized installation on a 560 with 1.5 MB of disk space took 500 KB for the output file. o Example use of command: hard.links / >/tmp/hard.links.out Script Output Look in the output file for "<>". If illegal hard links are found, a message is printed surrounded by a border of "<>"s. Also, the shell script checks to see that the "." and ".." directories exist for every directory and prints a message surrounded by a border of "<>"s if either of these entries is missing. If the ".." entry is missing, the illegal hard link test cannot be done, so after restoring the ".." entry, you should run the shell script again on the directory that had the missing ".." entry. The Script Please note that page headers and footers may appear in the following code. They should be removed before the code is used. Detection and Correction of Illegal Hard Links 2 01/26/96 #!/bin/ksh Dir=${1:?Give the name of the top of the directory tree to check} typeset -i NumDirsTotal=0 ProcessDir() { typeset Dir typeset -i NumSubDirs=0 Dir="$1$2" for SubDir in $(ls -lA "$Dir" | grep '^d' | rev | cut -f1 -d' ' | rev) do FileSystem=$(df $Dir/$SubDir | tail -1 | awk '{ print $1 }') RemoteHost=${FileSystem##*:} RemoteFileSystem=${FileSystem%%:*} if [[ $RemoteHost != $RemoteFileSystem ]] then # remotely mounted file system echo "$Dir/$SubDir (skipping remote file system > $FileSystem)" elif [[ $FileSystem = "AFS" ]] then # AFS file system echo "$Dir/$SubDir (skipping AFS file system)" else if [[ ! -d $Dir/$SubDir/. ]] then echo "<><><><><><><><><><><><><><><><><><><><><><><>" echo "<> <>" echo '<> The "." directory is missing from the <>' echo '<> directory listed above. <>' echo "<> <>" echo "<><><><><><><><><><><><><><><><><><><><><><><>" fi if [[ ! -d $Dir/$SubDir/.. ]] then echo "<><><><><><><><><><><><><><><><><><><><><><><><>" echo "<> <>" echo '<> The ".." directory is missing from the <>' echo '<> directory listed above. <>' echo "<> <>" echo "<><><><><><><><><><><><><><><><><><><><><><><><>" else ParentInode=$(ls -id "$Dir" | awk '{ print $1 }') Detection and Correction of Illegal Hard Links 3 01/26/96 [[ "$Dir" = "/" ]] && Dir="" DotDotInode=$(ls -id "$Dir/$SubDir/.." | awk '{ print $1 }') echo "$Dir/$SubDir (i-nodes) P:$ParentInode "..":$DotDotInode" if [[ $ParentInode -ne $DotDotInode ]] then echo "<><><><><><><><><><><><><><><><><><><><><><><><>" echo "<> <>" echo "<> Probable illegal directory-to-directory <>" echo "<> hard link by the directory listed above. <>" echo "<> Remove with unlink. <>" echo "<> <>" echo "<><><><><><><><><><><><><><><><><><><><><><><><>" fi fi ProcessDir "$Dir" "/$SubDir" fi (( NumSubDirs = $NumSubDirs + 1 )) done # echo "-- Number of directories in $Dir/: $NumSubDirs" (( NumDirsTotal = $NumDirsTotal + $NumSubDirs )) } ProcessDir "" "$Dir" # echo "-- Number of directories total: $NumDirsTotal" exit 0 CORRECTION METHODS Once you have identified an illegal hard link, you can fix it by unlinking it with the unlink command and, if the link is needed, create a symbolic link with the "ln -s" command. If a "." or ".." entry is missing, you can restore it with the link command. Also, a "cp -pr" of a directory (or file system) seems to repair illegal hard links (by making a copy instead of a hard link) and missing "." and ".." entries. Detection and Correction of Illegal Hard Links 4 01/26/96 READER'S COMMENTS Please fax this form to (512) 823-4009, attention "AIXServ Informa- tion". You may also e-mail comments to: elizabet@austin.ibm.com. These comments should include the same customer information requested below. Use this form to tell us what you think about this document. If you have found errors in it, or if you want to express your opinion about it (such as organization, subject matter, appearance) or make sug- gestions for improvement, this is the form to use. If you need technical assistance, contact your local branch office, point of sale, or 1-800-CALL-AIX (for information about support offer- ings). These services may be billable. Faxes on a variety of sub- jects may be ordered free of charge from 1-800-IBM-4FAX. Outside the U.S. call 415-855-4329 using a fax machine phone. When you send comments to IBM, you grant IBM a nonexclusive right to use or distribute your comments in any way it believes appropriate without incurring any obligation to you. NOTE: If you have a problem report or item number, supplying that number may help us determine why a procedure did or did not work in your specific situation. Problem Report or Item #: Branch Office or Customer #: Be sure to print your name and fax number below if you would like a reply: Name: Fax Number: ______________________________________________________________________ ______________________________________________________________________ ______________________________________________________________________ ______________________________________________________________________ ______________________________________________________________________ ______________________________________________________________________ ______________________________________________________________________ ______________________________________________________________________ ______________________________________________________________________ ______________________________________________________________________ ______________________________________________________________________ ______________________________________________________________________ END OF DOCUMENT (hard.links.cmd) Detection and Correction of Illegal Hard Links 5