Detection and Correction of Illegal Hard Links

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 <file_name> 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.

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

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.

 
#!/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 }') 
        [[ "$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: hard.links.32.cmd ITEM: FAX
Dated: 98/10/27~00:00 Category: cmd
This HTML file was generated 99/06/24~12:42:07
Comments or suggestions?
Contact us