File Creation and Link Management

Before creating a file, many UNIX utilities will invoke the stat() system call to see is the file exists. This will involve the kernel calling the ux_lookup() function. If the file name does not exist, the kernel will store a negative dentry in the dcache. Thus, if there are additional calls to stat() for the same file, the kernel can see that the file doesn't exist without an additional call to the filesystem.

Shown below is the output from the strace command when using the cp command to copy file to foo:

lstat64(“foo”, 0xbffff8a0) = -1 ENOENT (No such file or directory)
stat64(“file”, {st_mode=S_IFREG|0644, st_size=0, …}) = 0
open(“file”, O_RDONLY|O_LARGEFILE) = 3
open(“foo”, O_WRONLY|O_CREAT|O_LARGEFILE, 0100644) = 4

The cp command invokes the stat() system call on both files before calling open() to create the new file.

The following example shows the call to ux_lookup() in response to the cp command calling the stat() system call:

Breakpoint 5, ux_lookup (dip=0xcd73cba0, dentry=0xcb5ed3a0)
                               at ux_dir.c:367
367        struct ux_inode     *uip = (struct ux_inode *)
(gdb) bt
#0  ux_lookup (dip=0xcd73cba0, dentry=0xcb5ed3a0) at ux_dir.c:367
#1  0xc01482c0 in real_lookup (parent=0xcb5ed320, name=0xc97ebf5c,
                                flags=0)
    at namei.c:305
#2  0xc0148ba4 in link_path_walk (name=0xcb0f700b “”, nd=0xc97ebf98)
    at namei.c:590
#3  0xc014943a in __user_walk ( name=0xd0856920 “\22 0D\2 0 5-,K\2 0 5-ÃK\2 0 5-<L\2 0 5-”, flags=9, nd=0xc97ebf98) at namei.c:841 #4 0xc0145807 in ...

Get UNIX Filesystems: Evolution, Design, and Implementation now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.