The Ext3 filesystem sub-directory limit

I recently came across a problem on a site where a client could no longer upload images through their site. After digging through the logs, I found the relevant error:

Errno::EMLINK (Too many links - /usr/lib/ruby/1.8/fileutils.rb:243:in `mkdir'

This error was occurring when the system tried to make a new directory to store the uploaded images in. After researching (googling) the error, I find out that the Linux ext3 filesystem can only have a maximum of 32,000 links per inode which means a directory can only have 31,998 sub-directories. It just so happens that this particularly directory had exactly 31,998 directories in it.

When a user uploaded an image, multiple sizes were generated, and a directory was created to hold all the images for a particular item. The directory could never be created due to a filesystem error, so the upload was failing. To fix this problem, you can use a different filesystem without this limitation or I believe you can patch ext3 to support more inode links. But of course the easiest solution is partitioning your data, which is what I did. Fixing this problem required some relatively minor re-architecture of the app by partitioning the items into different sub-directories to avoid the ext3 limit.