News for hackers. Stuff that matters.
Re:No Space Left on Device by JonRB on Tuesday September 06, 2016 @02:04AM (#12433904)
During my young naïve programming days (which coming to think of it haven't really ended) I thought to myself "Why bother using some third party database when I could just store user information in text files, the OS handles that sort of thing really well, right?". I looked into it as much as I could be bothered and found no huge red flags.

After a few years of this server merrily creating a new file for each user I found that my disk was full, as this poster did. After a lot of hunting around, I found out that I had hit my inode limit, a concept I was completely unaware of.

I know for sure I won't be making THAT mistake again.

Re:No Space Left on Device by Johnny555 on Tuesday September 06, 2016 @02:49AM (#12434045)
I did the same thing to support about 10 million customer websites, but had the opposite experience - it worked pretty well, we used UFS and a 4 level directory structure (i.e. customer "apple" went into '/a/p/p/l/apple' and just wrote a simple apache module to map URL's to this directory structure.(using a hash of the filename would have given more even distribution, but this naive algorithm worked pretty effectively)

It was much easier than coding a database back-end, with no need to run a database server. And, when we needed to scale beyond one machine, it was trivial to use a central NFS server (read-only NFS worked really well for this) and multiple front-end web servers.

Though we did know how many files we'd be hosting, so we sized the number of bytes per inode appropriately.

Re:No Space Left on Device by oxplot on Tuesday September 06, 2016 @03:19AM (#12434174)
Standard practice to store a large number of files but in such a way that they don't end up in a single directory, is to create a crypto hash of your object names and use the first n characters as the directory name. Properties of hash functions will ensure that the files are distributed evenly between all the directories.

git is an example of a program that uses this technique (among others) to store blobs. In case of git, the blob names are already SHA-1 hashes.

Re:No Space Left on Device by majewsky on Tuesday September 06, 2016 @04:26AM (#12434398)
You don't actually need a cryptographic hash here [1]. I'd rather use a fast hash like Murmur instead.

[1] Exception: I don't know how prone Murmur is to hash collisions, so if collision-based attacks are part of your threat model, a randomly salted cryptographic hash might be appropriate.

Re:No Space Left on Device by jfoutz on Tuesday September 06, 2016 @02:24AM (#12433956)
Completely reasonable thing to do, for a little while. Ycombinator itself did this initially. The key is knowing you'll need to do something better soon.
Re:No Space Left on Device by bArray on Tuesday September 06, 2016 @02:17AM (#12433931)
"After a lot of hunting around, I found out that I had hit my inode limit, a concept I was completely unaware of."

What was the number you hit?

I have a flat file system for something I'm prototyping. The files are in a database with a random SHA256 filename associated with them. I was thinking about doing what Git does with creating folders for parts of the string to alleviate the number of files in one single directory. I'm sure that makes file retrieval a prettier process.

Do all the files for have to be in the same directory for the iNode limit to be reached or just there in general?

NOTE: I ask because the prototype isn't ever meant to go into production, but my customer keeps getting confused and I'm concerned this very important system may inherit (or even use) my design.

Re:No Space Left on Device by LukeShu on Tuesday September 06, 2016 @02:22AM (#12433945)
It's a global limit per filesystem. You can check what the limits are for your filesysems, and what the current use is with `df -i`.

The thing that git does is for speed.

Re:No Space Left on Device by bArray on Tuesday September 06, 2016 @02:32AM (#12433982)
I've apparently used 1241165 out of 19005440, or about 6.53% on my development machine. It's not inconceivable to have 19 million files, though, if you had one per user. That's not great.

How is that number calculated? It's less than 2^25 but greater than 2^24, so I imagine it's even awkward to store. I guess that has something to do with the file table size for HDD - you can't have more nodes than you have addressable space for those nodes.

I knew Git did it for speed, but I was wondering whether it was also about the number of files that can exist in a single directory.

EDIT: Corrected percentage.

Re:No Space Left on Device by LukeShu on Tuesday September 06, 2016 @02:48AM (#12434042)
It's a property of the filesystem that is set when it was created. In the case of the mkfs.ext{2,3,4}, you can set the number of inodes with the -N flag, but by default, it is the size of the filesystem divided by inode_ratio (probably 4096).
Re:No Space Left on Device by caf on Tuesday September 06, 2016 @03:10AM (#12434137)
This seems to be one of those bits of old UNIX lore that's being lost - it used to be fairly well-known in sysadmin circles that if you were going to be storing a lot of tiny files, you should create your filesystem with a higher than usual number of inodes (the classic case was an NNTP spool mount).
Re:No Space Left on Device by gioele on Tuesday September 06, 2016 @05:05AM (#12434529)
> if you were going to be storing a lot of tiny files, you should create your filesystem with a higher than usual number of inodes (the classic case was an NNTP spool mount).

I wonder how many people understand what the term "news server" of the partition wizard in the Debian installer means.

Re:No Space Left on Device by bArray on Tuesday September 06, 2016 @03:17AM (#12434165)
"This seems to be one of those bits of old UNIX lore that's being lost"

Hopefully not now, this is why we all appreciate people sharing these sorts of information :)

Re:No Space Left on Device by technofiend on Tuesday September 06, 2016 @11:28AM (#12436688)
I literally just solved an issue this weekend that was inode exhaustion: there were years of snapshots and traces laying around and the DBAs were confused as to why the database couldn't create a directory to start and was exiting with "No space left on device" when there was 5% storage free.

That lead to the second discussion of why you don't fix it by piping filenames one by one to rm, but either use find's inbuilt unlink/rm if there is one or xargs to rm if not.

Re:No Space Left on Device by greggyb on Tuesday September 06, 2016 @01:09PM (#12437492)
Is that second discussion just a performance thing, or are there filesystem implications for one or the other?
Re:No Space Left on Device by technofiend on Tuesday September 06, 2016 @11:43PM (#12440852)
Just speed: you don't spin up a new process for each file removed if you use xargs.
Re:No Space Left on Device by bArray on Tuesday September 06, 2016 @02:58AM (#12434081)
Seems to be about 16k for the ratio? 19,000,000 * 16,000 ~= 300GB - which is about the size of my disk partition.

That's crazy. Thanks for that information.

Re:No Space Left on Device by danieldk on Tuesday September 06, 2016 @03:08AM (#12434126)
Your filesystem's block size is probably 4KiB. So, it's one inode per for blocks.

That's crazy.

Why? Do you expect to store many files less than 16KiB?

Re:No Space Left on Device by bArray on Tuesday September 06, 2016 @03:17AM (#12434166)
Each user has some small arbitrary information associated with them, with potentially tens of millions of users (and no doubt more in the future). There's not really a structure due to the nature of the project, each user needs personalised information and structure associated to them.

I don't think that amount of information is really suitable for a database? It may be larger in some cases too, so there's not guarantees I can even make about size.

Re:No Space Left on Device by Xylakant on Tuesday September 06, 2016 @11:51AM (#12436872)
Document stores shine in that case. Depending on the exact case something like postgres's HSTORE, couchdb, couchbase, mongodb or similar would work. They're all capable of storing arbitrary json docs under a given key and efficiently retrieve it. Partial indexing is possible as well.
Re:No Space Left on Device by danieldk on Tuesday September 06, 2016 @03:19AM (#12434178)
Ah, sorry, I missed that you were still referring to the 'filesystem as a database'-project :).
Re:No Space Left on Device by danieldk on Tuesday September 06, 2016 @03:03AM (#12434109)
How is that number calculated? It's less than 2^25 but greater than 2^24

It depends on the filesytem, but it is typically a function of filesystem size (and in ext, the number of block groups and inodes per block group size). To take an example:

    sudo tune2fs -l <device> | egrep -i  'group'
    Blocks per group:         32768
    Fragments per group:      32768
    Inodes per group:         8192
So, we have got 1 inode per 4 blocks. This particular filesystem has 79472640 blocks (of 4KiB) and 19873792 inodes. This corresponds to the 4:1 ratio: 79472640 / 19873792 =~ 4.
Re:No Space Left on Device by bArray on Tuesday September 06, 2016 @03:08AM (#12434128)
Okay, that makes sense - thanks! I guess there's an upper limit to that number too? It's probably squeezing into a 32 or 64 bit number?
Re:No Space Left on Device by danieldk on Tuesday September 06, 2016 @03:16AM (#12434163)
There is probably a upper limit, but practically you don't want more inodes than blocks. Since a non-empty file at least takes up one block, assuming that your files are all at least 1 byte, you couldn't use more inodes than blocks anyway.

(The exception are empty files, because on most filesystems an empty file does not use any data blocks, but possibly an inode to store file size, permissions, etc.)

Re:No Space Left on Device by majewsky on Tuesday September 06, 2016 @04:32AM (#12434419)
If I'm not mistaken, some file systems store file contents smaller than a block directly inside the respective directory entry.
Re:No Space Left on Device by fnj on Tuesday September 06, 2016 @02:58AM (#12434083)
That's 6.53%, not 0.0653%. Bit of a difference.
Re:No Space Left on Device by bArray on Tuesday September 06, 2016 @03:06AM (#12434117)
Forgetting to times by hundred will eventually kill me, I swear. I'll update it!
Re:No Space Left on Device by ptman on Tuesday September 06, 2016 @05:02AM (#12434520)
I really like the fact that Mac OS X df(1) by defaults shows inode stats

https://developer.apple.com/legacy/library/documentation/Dar...

Maybe Linux and the BSDs could adopt that as well.

Re:No Space Left on Device by lotyrin on Tuesday September 06, 2016 @02:24AM (#12433957)
The whole FS has a limited number of inodes, additionally some FS have limited number of files or directories inside a directory. If you don't control the FS chosen by your customer, I'd design conservatively (find the dumbest set of limits, stay under them).
Re:No Space Left on Device by jbpetersen on Tuesday September 06, 2016 @02:50AM (#12434046)
Now I'm wondering whether that makes for a good design assuming you're using a filesystem that surpasses that limitation.

Btrfs?

Re:No Space Left on Device by rbanffy on Tuesday September 06, 2016 @03:19AM (#12434179)
It's worth checking that out, but I suspect the folder solution described on a sibling comment is a better solution. If a file system gets too full, it's trivial to move the files in a folder to another file system an mount it where the folder originally was. Also, spreading the files across multiple NFS mounts is also trivial.
Re:No Space Left on Device by dvcrn on Tuesday September 06, 2016 @02:31AM (#12433979)
inodes are by the way a great topic to talk about in a job interview. People working with unix based systems should know about them, or at least encountered them once in their career.

Another good one that even less people know about is the sticky bit.

Re:No Space Left on Device by danieldk on Tuesday September 06, 2016 @03:22AM (#12434184)
Indeed. One of the types questions were people typically bomb (this is also one in Google's telephone interview arsenal): where is a filename stored?

People who have the vague notion of 'an inode stores metadata' will usually answer this incorrectly.