Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending ext4 File system's filename size limit to 1012 characters

I am pulling data from a server and one of the folder name is longer than 256 bytes, so my CentOS is throwing an error that the name is too long. I have searched all over the internet but couldn't find any way to create a folder/file name with size of over 256 bytes under ext2/ext3/ext4 file systems.

However, One solution had suggested to create reiserfs file system alongside ext4 to handle files\folder with longer names. This solution might work, but I just read in one of the book that it is possible to extend the limit of filename size from 255 characters to 1012 characters if needed.

The maximal file name size is 255 characters. This limit could be extended to `1012` if needed. 

Source: Google

But I couldn't find any website that explains how the filesystem could be modified to extend the size to 1012?

Can someone please help me with that?

like image 856
Sufiyan Ghori Avatar asked Jan 24 '16 20:01

Sufiyan Ghori


1 Answers

Don't know where 1012 was found (mentioned in http://e2fsprogs.sourceforge.net/ext2intro.html - Design and Implementation of the Second Extended Filesystem, ISBN 90-367-0385-9. 1995), but in modern Linux kernel file name is fixed in struct ext2_dir_entry_2 with maximum of 255 chars (bytes):

https://elixir.bootlin.com/linux/v4.10/source/fs/ext2/ext2.h#L600

/*
 * The new version of the directory entry.  Since EXT2 structures are
 * stored in intel byte order, and the name_len field could never be
 * bigger than 255 chars, it's safe to reclaim the extra byte for the
 * file_type field.
 */
struct ext2_dir_entry_2 {
    __le32  inode;          /* Inode number */
    __le16  rec_len;        /* Directory entry length */
    __u8    name_len;       /* Name length */
    __u8    file_type;
    char    name[];         /* File name, up to EXT2_NAME_LEN */
};

There was struct ext2_dir_entry with longer file name length, but extra byte of name_len was redefined as file_type.

      __le16    name_len;       /* Name length */

So, current maximum file name length for ext2 is 255

https://elixir.bootlin.com/linux/v4.10/source/include/linux/ext2_fs.h#L22

#define EXT2_NAME_LEN 255

https://elixir.bootlin.com/linux/v4.10/source/fs/ext2/namei.c#L62

if (dentry->d_name.len > EXT2_NAME_LEN)
    return ERR_PTR(-ENAMETOOLONG);

Same for ext3/ext4:

https://elixir.bootlin.com/linux/v4.10/source/fs/ext4/ext4.h#L1892

/*
 * Structure of a directory entry
 */
#define EXT4_NAME_LEN 255

https://elixir.bootlin.com/linux/v4.10/source/fs/ext4/namei.c

  * `len <= EXT4_NAME_LEN' is guaranteed by caller.
   if (namelen > EXT4_NAME_LEN)
        return NULL;
   if (dentry->d_name.len > EXT4_NAME_LEN)
       return ERR_PTR(-ENAMETOOLONG);

Ondisk format is described with 8 bit file_name too (file_type uses only 3 bits in older docs - EXT2_FT_MAX, but modern driver will not handle 255+ file names. ext4 has extra FT of 0xde):

http://www.nongnu.org/ext2-doc/ext2.html#IFDIR-NAME-LEN "4.1.3. name_len - 8bit unsigned value indicating how many bytes of character data are contained in the name."

http://cs.smith.edu/~nhowe/262/oldlabs/ext2.html#direntry "The file_type field indicates what kind of file the entry is referring to... The maximum length of a file name is EXT2_NAME_LEN, which is usually 255."

https://oss.oracle.com/projects/ocfs2/dist/documentation/disklayout.pdf#page=16 "__u8 name_len"

like image 191
osgx Avatar answered Sep 30 '22 19:09

osgx