3

All this while I have been thinking that mkdir can only create directories. But I am surprised to see that it was able to create files also under some conditions. I recently started working with cgroups and When I run 'mkdir' command under /cgroup, it created files along with directories.

[abc@master ~]$ which mkdir

/bin/mkdir

[abc@master ~]$ mkdir /cgroup/cpu/group0

[abc@master ~]$ ls /cgroup/cpu/group0/

cgroup.event_control  cpu.cfs_period_us     cpu.rt_period_us      cpu.shares       
notify_on_release     cgroup.procs          cpu.cfs_quota_us      cpu.rt_runtime_us
cpu.stat              tasks.

How is the mkdir command able to create files along with directories?

I can see that this only happens under cgroups. How does the OS distinguish between mkdir under cgroup and mkdir under elsewhere?

I tried to find the answer online but could not find anything really helpful. Any relevant information will be really appreciated.

MelBurslan
  • 6,966
  • In a very handwavy way, mkdir will at some point call filesystem code, and different filesystems can do different things when "creating directories". (I have not double-checked if this is actually how the cgroups files are created.) – Ulrich Schwarz Mar 16 '16 at 21:09

1 Answers1

6

It's a feature of the cgroup filesystem, not a feature of the mkdir command. The mkdir command just invokes the mkdir system call. The mkdir system call invokes the VFS layer in the kernel, which performs some analysis on the command and in particular determines on what filesystem the directory is to be created by analyzing the hierarchy of mount points in the directory tree. Then the VFS layer calls the code in the driver for that filesystem, which is the cgroup filesystem driver.

What a filesystem driver does for each filesystem operation (open file, read, write, create directory, remove directory, etc.) is up to that filesystem driver. A filesystem for on-disk storage will modify the disk content to record the existence of a directory as directed. A network filesystem will send a command over the network. The proc filesystem reports information about processes and about the kernel, and the cgroup filesystem does something similar.

The behavior of the cgroup filesystem is described in the kernel documentation.

New cgroups are created using the mkdir system call or shell command. The properties of a cgroup, such as its flags, are modified by writing to the appropriate file in that cgroups directory, as listed above.

When the cgroup filesystem driver receives an mkdir command at that level of the hierarchy, it interprets that to mean “create a new cgroup” with the given name. When it receives a readdir command (the system call that enumerates directory entries) in /cgroup/cpu0, it lists the cgroups on that CPU. When it receives a readdir command in /cgroup/cpu0/group0, it reports a fixed set of files that reflect that cgroup's parameters as stored in memory. When it receives a read or write command on those files, it reports or modifies the corresponding parameter.

The designers of cgroup chose to have it entirely controlled via the filesystem rather than add new system calls. That makes it easy to use without having to develop much library code and many utilities.