🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: muc.lists.freebsd.fs
1 messages
1 total messages Started by Andrea Cocito Mon, 19 Oct 2026 17:20
Re: Terminal number of files in a ZFS directory.
#10
Author: Andrea Cocito
Date: Mon, 19 Oct 2026 17:20
141 lines
12693 bytes
--Apple-Mail-D0EAEF74-9365-4C2E-A783-81E19A4B6647
Content-Type: text/html;
	charset=utf-8
Content-Transfer-Encoding: quoted-printable

<html class=3D"apple-mail-supports-explicit-dark-mode"><head><meta http-equi=
v=3D"content-type" content=3D"text/html; charset=3Dutf-8"></head><body dir=3D=
"auto"><br id=3D"lineBreakAtBeginningOfSignature"><div dir=3D"ltr">Sent from=
 my iPhone</div><div dir=3D"ltr"><br><blockquote type=3D"cite">On 19 Oct 202=
6, at 14:52, Frank Leonhardt &lt;freebsd-doc@fjl.co.uk&gt; wrote:<br><br></b=
lockquote></div><blockquote type=3D"cite"><div dir=3D"ltr">=EF=BB=BF

 =20
    <meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3DUTF-8"=
>
 =20
 =20
    <div class=3D"moz-cite-prefix">On 19/10/2026 00:57, Zaphod Beeblebrox
      wrote:<br>
    </div>
    <blockquote type=3D"cite" cite=3D"mid:CACpH0Md47U9Gd=3DjAr=3D02SGvZSb1j0=
EuC2BSKOL2cZXpMpJbq7A@mail.gmail.com">
      <meta http-equiv=3D"content-type" content=3D"text/html; charset=3DUTF-=
8">
      <div dir=3D"ltr">
        <div>The&nbsp;TLDR is "I don't know, but I have such a directory".</=
div>
        <div><br>
        </div>
        <div>The directory developed on a jail.&nbsp; &nbsp;It was the maild=
rop
          directory for a postfix no longer running as the server had
          become a jail and no longer needed it's own mail service.&nbsp; Th=
e
          maildrop, however, continued to receive cron generated
          periodic mail for years.&nbsp; So that's 2 per day plus another on=
e
          per week, IIRC.&nbsp; That would be about 5980 email per year.&nbs=
p;
          Now... give it 15 years ... 89700?&nbsp; That doesn't seem like
          enough.&nbsp; There must have been more crap.</div>
        <div><br>
        </div>
        <div>On the jail server, the zfs filesystem only occupies about
          10G.&nbsp; echo * on the directory runs at 100% cpu for over a
          week.&nbsp; So I reboot (to stop it... it's stuck in kernel) and
          zfs send it to a NVME only system (my desktop) and try again.&nbsp=
;
          I was thinking that much faster disk could compensate for
          abominably bad big-O behavior.&nbsp; But this two ran for a week
          without finishing.</div>
        <div><br>
        </div>
        <div>What are the limits we're running into here.&nbsp; This is
          academic.&nbsp; I can solve the problem (already have).&nbsp; I st=
ill
          don't know what the limit is.&nbsp; Somewhere around September
          15th, this directory started sticking finds and whatnot --- so
          it tipped over from working to broken.&nbsp; I have the filesystem=
,
          BTW...&nbsp;</div>
        <div><br>
        </div>
        <div>What is this limit that toggles zfs from working large
          directory to completely broken sysadmin problem?</div>
        <div><br>
        </div>
      </div>
    </blockquote>
    <p>As Daniel Kalchev <a class=3D"moz-txt-link-rfc2396E" href=3D"mailto:d=
aniel@digsys.bg">&lt;daniel@digsys.bg&gt;</a> has said, you can have
      a stupid number of files in a directory and it still works, so I
      decided to have a look-see in the OpenZFS source. I suspected that
      the limit might be kernel rather than ZFS related, and this
      appears to be true.</p>
    <p>Firstly, a directory might contain subdriectories so you're going
      to hit a problem with the link count wrapping (.. - parent
      directory). On Linux this is defined in&nbsp;include/os/linux/zfs/sys/=
zfs_vfsops_os.h
      - ZFS_LINK_MAX =3D 2^31 =E2=88=92 1. According to the comments
      inode-&gt;i_nlink is an unsigned int. I'll leave that one to the
      penguinistas.</p>
    <p>On FreeBSD ZFS_LINK_MAX (defined in zfs_znode_impl.h) is set to
      UINT64_MAX. So Linux will fail at mere 4 Billion directories but
      on FreeBSD that's barely getting started.</p>
    <p>So what about files?</p>
    <p>Well there's going to be st_size (from struct stat) to worry
      about, which maps on to z_size in ZFS's in-memory znode (i.e.
      znode_t). That's also uint64_t. For a regular file this holds the
      file size but *I THINK* for a directory it holds the entry count
      instead. Either way, it does set a limit on the number of files
      one way or another, 2^64 is a very big number.<br>
      <br>
      Incidentally, you can get this number using stat rather than ls
      (or echo) - try "stat -f %z bigdir" which is the standard way of
      getting the number of files in a directory without counting them
      in a shell script, but subtract two for . and .. (I think it's
      stat -c %s bigdir on Linux but don't quote me).<br>
      <br>
    </p>
    <p>But... I think the real limit might be ZAP objects, the ZFS
      hashed key-value store. A directory ZAP object internally. What is
      the limit there? Not so neat as an integer to overflow, and I'm
      now out of my depth but what it looks like...</p>
    <p>The main hash table has 28 bit hash values, but the system will
      stop growing it at 28-2 =3D 26 bit entries (i.e. 2^26). These point
      to leaf buckets of 16K, which can hold, I don't know, 200 entries
      on average? Say 10 billion files.</p>
    <p>Because it's all hashed, unlike UFS2, getting a file out of an
      enormous directory is cheap - especially if the matadata is in the
      ARC. If you do max-out the ARC capacity it's gonna slow down, but
      what do you expect?</p>
    <p>I can see lots of problems with large directories slowing down
      (no kidding) but they're not ZFS specific. For example, readdir
      will return files in random order that have to be sorted in
      userland. Any change to a directory locks the whole directory
      (that's a VFS thing in the kernel) so add that to the write
      amplification from dirtying leaf nodes in the ZAP as it rebalances
      - I don't *know* but it all smells like trouble and when the ARC
      is exhausted then performance will go off a cliff. The same as if
      a zpool is &gt;80% full. My guess is this is what you were seeing
      - performance off a cliff-edge rather than a hard limit.</p>
    <p>Regards, Frank.</p>
    <p><br>
    </p>
 =20

</div></blockquote><br><div>Hi,</div><div><br></div><div>IMHO =E2=80=9Cecho *=
=E2=80=9D is more a shell expansion or argv[] building issue than a filesyst=
em one.&nbsp;</div><div><br></div><div>I would give a try to =E2=80=9Cfind d=
ir |wc -l=E2=80=9D.&nbsp;</div><div><br></div><div>To cleanup I bet find -rm=
 would work.&nbsp;</div><div><br></div><div>Cheers,</div><div><br></div><div=
>A.&nbsp;</div></body></html>=

--Apple-Mail-D0EAEF74-9365-4C2E-A783-81E19A4B6647--


--
Posted automagically by a mail2news gateway at muc.de e.V.
Please direct questions, flames, donations, etc. to news-admin@muc.de
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads