Re: FS: How to get the name of a directory given by an inode

Matthias Urlichs (
Sun, 29 Sep 1996 13:47:33 +0100

In, article <>, (Lars Reuther) writes:
> The only idea I have is to read the parent directory, look for an entry
> pointing to the given inode, read the parent directory of the parent
> directory and so on, until I reach the root directory.

Right. There's no other way.

> I hope this will work, but I think it will take quite a long time to
> read a complex directory structure.

It's also stupid because there's no way to make sure that the path you just
calculated works for non-root users. Or that it works after another milli-
second or so. For instance:

mkdir -p /tmp/1/foo
( sleep 1; foo=1; cd /tmp;
while : ; do bar=$(expr $foo + 1); mv $foo $bar; foo=$bar ; done ) &
cd /tmp/1/foo
sleep 2
/bin/pwd ; sleep 1 ; /bin/pwd ; sleep 1 ; /bin/pwd


Anything that can be invalidated by unrelated and unprivileged user-mode
operations (remember, it doesn't even have to be the same file system) has
no place within the kernel.

/proc/mounts is the only exception ... which could be improved (provide the
major:minor pairinstead of the device name, and return the mount point's
inode (OK, OK -- a file descriptor for it) when you chdir("/proc/mounts/
LINE-NUMBER"), or something along these lines).

> Are there any other (faster) ways to get the name?
Yes. Rethink about why would you need the path in the kernel in the first

Must I hold a candle to my shames?
               --William Shakespeare [The Merchant of Venice]
Matthias Urlichs         \  noris network GmbH  /  Xlink-POP Nürnberg 
Schleiermacherstraße 12   \   Linux+Internet   /   EMail:
90491 Nürnberg (Germany)   \    Consulting+Programming+Networking+etc'ing
   PGP: 1024/4F578875   1B 89 E2 1C 43 EA 80 44  15 D2 29 CF C6 C7 E0 DE
       Click <A HREF="">here</A>.    42