将 ext4 文件系统的文件名大小限制扩展到 1012 个字符

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

我正在从服务器中提取数据,其中一个文件夹名称超过 256 字节,因此我的 CentOS 抛出名称太长的错误。我在整个互联网上进行了搜索,但找不到任何方法来在 ext2/ext3/ext4 文件系统下创建大小超过 256 字节的 folder/file 名称。

但是,一个解决方案建议在 ext4 旁边创建 reiserfs 文件系统以处理 files\folder 更长的名称。这个解决方案可能有效,但我刚刚在其中一本书中读到,如果需要,可以将文件名大小的限制从 255 个字符扩展到 1012 个字符。

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

来源: Google

但我找不到任何网站解释如何修改文件系统以将大小扩展到 1012?

有人可以帮我吗?

参见 https://en.wikipedia.org/wiki/Comparison_of_file_systems#Limits,没有很多文件系统可以处理超过 255 字节的文件名。

虽然这可能不是对您问题的直接答复,但我认为您不应该尝试走这条路(更改最大长度)。

您从哪个服务器检索文件?为什么在检索时不更改他们的名字? 您确定整个路径名是相关的,如果您提取前 250 个字符或后 250 个字符是否足以无歧义地引用每个文件?

根据您的限制,您有很多选择:

  • 要么生成 "random" 个名字(或连续的名字),然后将旧真名和新假名之间的映射存储在文本文件中
  • 或者将初始名称拆分为 250 个字符的路径元素(或类似的东西)并用它们创建中间目录

您可以在 https://serverfault.com/questions/264339/how-to-increase-filename-size-limit-on-ext3-on-ubuntu

找到类似的讨论

不知道在哪里找到 1012(在 http://e2fsprogs.sourceforge.net/ext2intro.html - 第二个扩展文件系统的设计与实现,ISBN 90-367-0385-9.1995 中提到),但在现代 Linux 内核文件名固定为struct ext2_dir_entry_2,最大255个字符(字节):

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 */
};

struct ext2_dir_entry 的文件名长度更长,但 name_len 的额外字节被重新定义为 file_type

      __le16    name_len;       /* Name length */

因此,ext2 的当前最大文件名长度为 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);

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);

磁盘格式也用 8 位 file_name 描述(file_type 在旧文档中仅使用 3 位 - 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 - 8 位无符号值,表示名称中包含多少字节的字符数据。"

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"