요약·해설과 원문, 전문 번역을 서로 분리했습니다. API 이름, symbol, source path는 원문 표기를 사용합니다.
1. 요약·해설
원문의 핵심 논리와 kernel programming 관점의 보충 설명입니다. 아래의 전문 번역과는 별도로 작성했습니다.
2. 영어 원문 전체
번역 기준이 된 Linux v6.18.37 원문입니다. 줄 번호는 이 버전의 파일 좌표입니다.
원문 전체 펼치기
.. SPDX-License-Identifier: GPL-2.0
===============================================================
Inotify - A Powerful yet Simple File Change Notification System
===============================================================
Document started 15 Mar 2005 by Robert Love <[email protected]>
Document updated 4 Jan 2015 by Zhang Zhen <[email protected]>
- Deleted obsoleted interface, just refer to manpages for user interface.
(i) Rationale
Q:
What is the design decision behind not tying the watch to the open fd of
the watched object?
A:
Watches are associated with an open inotify device, not an open file.
This solves the primary problem with dnotify: keeping the file open pins
the file and thus, worse, pins the mount. Dnotify is therefore infeasible
for use on a desktop system with removable media as the media cannot be
unmounted. Watching a file should not require that it be open.
Q:
What is the design decision behind using an-fd-per-instance as opposed to
an fd-per-watch?
A:
An fd-per-watch quickly consumes more file descriptors than are allowed,
more fd's than are feasible to manage, and more fd's than are optimally
select()-able. Yes, root can bump the per-process fd limit and yes, users
can use epoll, but requiring both is a silly and extraneous requirement.
A watch consumes less memory than an open file, separating the number
spaces is thus sensible. The current design is what user-space developers
want: Users initialize inotify, once, and add n watches, requiring but one
fd and no twiddling with fd limits. Initializing an inotify instance two
thousand times is silly. If we can implement user-space's preferences
cleanly--and we can, the idr layer makes stuff like this trivial--then we
should.
There are other good arguments. With a single fd, there is a single
item to block on, which is mapped to a single queue of events. The single
fd returns all watch events and also any potential out-of-band data. If
every fd was a separate watch,
- There would be no way to get event ordering. Events on file foo and
file bar would pop poll() on both fd's, but there would be no way to tell
which happened first. A single queue trivially gives you ordering. Such
ordering is crucial to existing applications such as Beagle. Imagine
"mv a b ; mv b a" events without ordering.
- We'd have to maintain n fd's and n internal queues with state,
versus just one. It is a lot messier in the kernel. A single, linear
queue is the data structure that makes sense.
- User-space developers prefer the current API. The Beagle guys, for
example, love it. Trust me, I asked. It is not a surprise: Who'd want
to manage and block on 1000 fd's via select?
- No way to get out of band data.
- 1024 is still too low. ;-)
When you talk about designing a file change notification system that
scales to 1000s of directories, juggling 1000s of fd's just does not seem
the right interface. It is too heavy.
Additionally, it _is_ possible to more than one instance and
juggle more than one queue and thus more than one associated fd. There
need not be a one-fd-per-process mapping; it is one-fd-per-queue and a
process can easily want more than one queue.
Q:
Why the system call approach?
A:
The poor user-space interface is the second biggest problem with dnotify.
Signals are a terrible, terrible interface for file notification. Or for
anything, for that matter. The ideal solution, from all perspectives, is a
file descriptor-based one that allows basic file I/O and poll/select.
Obtaining the fd and managing the watches could have been done either via a
device file or a family of new system calls. We decided to implement a
family of system calls because that is the preferred approach for new kernel
interfaces. The only real difference was whether we wanted to use open(2)
and ioctl(2) or a couple of new system calls. System calls beat ioctls.
3. 한국어 전문 번역
영어 원문의 문단 순서와 의미를 유지한 전체 번역입니다. 코드, 함수명, symbol과 URL은 원문 표기를 유지합니다.
Inotify 문서 이력과 범위
1-14Inotify는 강력하면서도 단순한 파일 변경 알림 시스템입니다. 이 문서는 Robert Love `<[email protected]>`가 2005년 3월 15일 시작했습니다.
Zhang Zhen `<[email protected]>`가 2015년 1월 4일 갱신하면서 폐기된 인터페이스 설명을 삭제하고 사용자 인터페이스는 man page를 참조하도록 정리했습니다. 이어지는 내용은 API 사용법이 아니라 설계 근거를 문답 형식으로 설명합니다.
.. SPDX-License-Identifier: GPL-2.0
===============================================================
Inotify - A Powerful yet Simple File Change Notification System
===============================================================
Document started 15 Mar 2005 by Robert Love <[email protected]>
Document updated 4 Jan 2015 by Zhang Zhen <[email protected]>
- Deleted obsoleted interface, just refer to manpages for user interface.
watch를 열린 파일에 묶지 않는 이유
15-33질문은 watch 대상 객체의 열린 file descriptor에 watch를 직접 묶지 않은 설계 이유입니다.
watch는 열린 파일이 아니라 열린 inotify device와 연결됩니다. 이는 dnotify의 핵심 문제를 해결합니다. 감시하려고 파일을 계속 열어 두면 파일이 pin되고, 더 나쁘게는 그 파일이 속한 mount도 pin됩니다.
그 결과 removable media를 unmount할 수 없으므로 dnotify는 이동식 매체가 있는 desktop system에서 사용하기 어렵습니다. 파일을 감시한다는 이유만으로 그 파일을 계속 열어 둘 필요가 없어야 합니다.
watch 수명을 파일의 열린 fd에서 분리합니다.
(i) Rationale
Q:
What is the design decision behind not tying the watch to the open fd of
the watched object?
A:
Watches are associated with an open inotify device, not an open file.
This solves the primary problem with dnotify: keeping the file open pins
the file and thus, worse, pins the mount. Dnotify is therefore infeasible
for use on a desktop system with removable media as the media cannot be
unmounted. Watching a file should not require that it be open.
Q:
What is the design decision behind using an-fd-per-instance as opposed to
an fd-per-watch?
A:
An fd-per-watch quickly consumes more file descriptors than are allowed,
instance마다 하나의 fd를 사용하는 이유
34-78질문은 watch마다 fd 하나를 두지 않고 inotify instance마다 fd 하나를 두는 설계 이유입니다.
watch마다 fd를 만들면 허용 한도보다 많은 file descriptor를 빠르게 소비하고, 관리하기 어려우며, `select()`로 효율적으로 다룰 수 있는 수보다 많아집니다. root가 process별 fd limit을 높이고 사용자가 `epoll`을 쓸 수는 있지만 둘 다 요구하는 것은 불필요하고 어리석은 제약입니다.
watch는 열린 파일보다 메모리를 적게 쓰므로 watch 개수 공간과 fd 개수 공간을 분리하는 것이 합리적입니다. 현재 설계에서는 사용자 공간이 inotify를 한 번 초기화하고 watch `n`개를 추가하므로 fd 하나만 필요하고 fd limit을 조절할 필요도 없습니다. inotify instance를 2,000번 초기화하는 것은 비합리적입니다. 사용자 공간이 원하는 구조를 깨끗하게 구현할 수 있고 `idr` 계층이 이를 간단하게 만들므로 그 선호를 따르는 것이 맞습니다.
단일 fd에는 block할 대상이 하나이고, 이는 단일 event queue에 대응합니다. 그 fd가 모든 watch event와 잠재적인 out-of-band data를 반환합니다.
watch마다 fd가 따로 있으면 첫째, event 순서를 알 수 없습니다. `foo`와 `bar`의 fd가 모두 `poll()`을 깨워도 어느 변경이 먼저였는지 판단할 수 없습니다. 단일 queue는 순서를 자연스럽게 보장하며 Beagle 같은 기존 application에 중요합니다. 순서 정보 없이 `mv a b ; mv b a` event를 처리하는 상황을 생각하면 필요성이 분명합니다.
둘째, 하나 대신 상태를 가진 fd `n`개와 내부 queue `n`개를 유지해야 하므로 kernel 구현이 훨씬 복잡해집니다. 단일 선형 queue가 이 문제에 맞는 data structure입니다.
셋째, 사용자 공간 개발자들은 현재 API를 선호합니다. Beagle 개발자들도 이를 좋아하며, `select`로 1,000개 fd를 관리하고 기다리려는 사람은 드뭅니다. 넷째, watch별 fd 방식에는 out-of-band data를 얻을 방법이 없습니다. 다섯째, fd 1,024개조차 수천 directory 규모에는 부족합니다.
파일 변경 알림을 수천 directory까지 확장하려 할 때 수천 fd를 다루는 interface는 지나치게 무겁습니다. 다만 하나의 process가 inotify instance를 여러 개 만들어 여러 queue와 관련 fd를 운영하는 것은 가능합니다. 관계는 process당 fd 하나가 아니라 queue당 fd 하나이며, process가 queue를 여러 개 원할 수도 있습니다.
watch별 fd 방식과 비교한 핵심 이유입니다.
more fd's than are feasible to manage, and more fd's than are optimally
select()-able. Yes, root can bump the per-process fd limit and yes, users
can use epoll, but requiring both is a silly and extraneous requirement.
A watch consumes less memory than an open file, separating the number
spaces is thus sensible. The current design is what user-space developers
want: Users initialize inotify, once, and add n watches, requiring but one
fd and no twiddling with fd limits. Initializing an inotify instance two
thousand times is silly. If we can implement user-space's preferences
cleanly--and we can, the idr layer makes stuff like this trivial--then we
should.
There are other good arguments. With a single fd, there is a single
item to block on, which is mapped to a single queue of events. The single
fd returns all watch events and also any potential out-of-band data. If
every fd was a separate watch,
- There would be no way to get event ordering. Events on file foo and
file bar would pop poll() on both fd's, but there would be no way to tell
which happened first. A single queue trivially gives you ordering. Such
ordering is crucial to existing applications such as Beagle. Imagine
"mv a b ; mv b a" events without ordering.
- We'd have to maintain n fd's and n internal queues with state,
versus just one. It is a lot messier in the kernel. A single, linear
queue is the data structure that makes sense.
- User-space developers prefer the current API. The Beagle guys, for
example, love it. Trust me, I asked. It is not a surprise: Who'd want
to manage and block on 1000 fd's via select?
- No way to get out of band data.
- 1024 is still too low. ;-)
When you talk about designing a file change notification system that
scales to 1000s of directories, juggling 1000s of fd's just does not seem
the right interface. It is too heavy.
Additionally, it _is_ possible to more than one instance and
juggle more than one queue and thus more than one associated fd. There
need not be a one-fd-per-process mapping; it is one-fd-per-queue and a
process can easily want more than one queue.
Q:
Why the system call approach?
device file 대신 system call을 선택한 이유
79-90질문은 inotify가 system call 방식으로 설계된 이유입니다.
dnotify의 두 번째로 큰 문제는 형편없는 사용자 공간 interface입니다. signal은 파일 알림에 매우 나쁜 interface이며, 원문은 사실상 어떤 용도에도 나쁘다고 강하게 표현합니다.
모든 관점에서 이상적인 해법은 기본 file I/O와 `poll`/`select`를 사용할 수 있는 file descriptor 기반 interface입니다. fd를 얻고 watch를 관리하는 방법은 device file 또는 새 system call 계열 가운데 선택할 수 있었습니다.
새 kernel interface에는 system call이 선호되므로 inotify는 system call 계열을 택했습니다. 실제 선택지는 `open(2)`와 `ioctl(2)`을 사용할지, 몇 개의 새 system call을 사용할지였으며 결론은 system call이 ioctl보다 낫다는 것입니다.
알림 수단과 제어 interface를 분리해 선택한 과정입니다.
A:
The poor user-space interface is the second biggest problem with dnotify.
Signals are a terrible, terrible interface for file notification. Or for
anything, for that matter. The ideal solution, from all perspectives, is a
file descriptor-based one that allows basic file I/O and poll/select.
Obtaining the fd and managing the watches could have been done either via a
device file or a family of new system calls. We decided to implement a
family of system calls because that is the preferred approach for new kernel
interfaces. The only real difference was whether we wanted to use open(2)
and ioctl(2) or a couple of new system calls. System calls beat ioctls.
요약·해설
inotify.rst:1-90Inotify의 핵심 설계는 watch를 대상 파일의 열린 fd와 분리하고, 여러 watch를 한 instance의 단일 fd와 event queue에 모으는 것입니다. 이 구조는 mount pinning, fd 고갈, event 순서 상실, 복잡한 queue 관리 문제를 함께 해결합니다.
사용자 공간에는 signal 대신 기본 I/O와 `poll`/`select`가 가능한 fd를 제공하고, 새 kernel interface를 구성하는 수단으로 device file과 ioctl 대신 system call 계열을 선택했습니다.
감시 대상과 알림 queue를 분리한 구조입니다.