3

When writing software which needs to cache data on disk, is there a way in libc, or a way which is specific to a certain file system (such as ext4), to create a file and flag it as suitable to be deleted automatically (by the kernel) if the partition becomes almost full?

There’s something similar for memory pages: madvise(…, MADV_FREE).

Some systems achieve this by writing a daemon which monitors the partition fullness, and which manually deletes certain pre-determined paths once it exceeds a certain fill level. I’d like to avoid this if possible, as it’s not very scalable: each application would have to notify the daemon of new cache paths as they are created, which may be frequently. If this were in-kernel, a single flag could be held on each inode indicating whether it’s a cache file.

Having a standardised daemon for this would be acceptable as well. At the moment it seems like different major systems integrators all invent their own.

Philip Withnall
  • 5,293
  • 14
  • 28
  • 1
    I do not think that this exists. However, what makes you think that an in-kernel solution would scale better than a userspace daemon? Sure, it'd probably be slightly faster, but I do not see why it'd *scale* differently. If you are concerned about noticing too late when the filesystem runs full, check out [fanotify](http://man7.org/linux/man-pages/man7/fanotify.7.html). – Phillip Jul 07 '17 at 15:40
  • @Phillip: I think on most systems, `fanotify` would produce a lot more CPU load than just polling every 1 second or something. Unless you have a lot of separate filesystems which are mostly-idle. I guess you could use `fanotify` without actually walking through a whole buffer of events. Just as a way to sleep during periods of no activity. But I'd guess that during activity, forcing the kernel to write to the fanotify buffer has non-zero overhead even if you don't look at the data. – Peter Cordes Jul 09 '17 at 09:22
  • I’ve expanded on what I meant by ‘scalable’ in the question. – Philip Withnall Jul 10 '17 at 09:58

1 Answers1

-1

You can use crontab job, and look for specific file extension and delete it. You can even filter based on time and leave the files created in last n minutes. If you are ok with this, let me know, I will add more details here.

macfij
  • 3,093
  • 1
  • 19
  • 24
  • That’s a fairly terrible solution which involves polling the file system regularly to see if it’s full. This is functionality which should be triggered by the file system becoming full, at the kernel level. – Philip Withnall Jul 28 '17 at 14:02