我是我大学系统编程课的老师助手。最近,学生们一直致力于一项涉及复制计划pwd
的任务。
有些学生注意到看似不一致的事情。当他们从readdir条目中读取ino时,它会在他们统计同一目录时提供不同的inode。他们中的许多人都在问为什么。 目录条目的inode不应该指向目标目录所在的inode吗?如果是这样,为什么stat会给出不同的inode? p>
以下是一些示例代码:
#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#define DIR_NAME "~redacted~"
int getReaddirInode(char* entName)
{
DIR* directory;
struct dirent* entry;
ino_t result;
if ((directory = opendir(".")) == NULL)
{
perror("opendir");
exit(1);
}
while ((entry = readdir(directory)) != NULL)
{
if (strcmp(entName, entry->d_name) == 0)
{
result = entry->d_ino;
break;
}
}
if (entry == NULL)
{
fprintf(stderr, "No such directory: %s.\n", entName);
exit(1);
}
if (closedir(directory) == -1)
{
perror("closedir");
exit(1);
}
return result;
}
int getStatInode(char* entName)
{
struct stat buf;
if (stat(entName, &buf) == -1)
{
perror("stat");
exit(1);
}
return buf.st_ino;
}
int main()
{
if (chdir("/home") == -1)
{
perror("chdir");
return 1;
}
printf("readdir (3) gives an inode of:%9d.\n", getReaddirInode(DIR_NAME));
printf("stat (2) gives an inode of: %9d.\n", getStatInode(DIR_NAME));
return 0;
}
输出:
unix3:~ $ ./a.out
readdir (3) gives an inode of: 4053392.
stat (2) gives an inode of: 69205302.
编辑: 我可以确认DIR_NAME是一个挂载点:
unix3:~ $ mount | grep ~redacted~
csc-na01.csc.~redacted~.edu:/student01/student01/0_t/~redacted~ on /home/~redacted~ type nfs (rw,relatime,vers=3,rsize=65536,wsize=65536,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,mountaddr=129.65.158.8,mountvers=3,mountport=635,mountproto=udp,local_lock=none,addr=129.65.158.8)
EDIT2: 在nfs文件系统转换时,inode似乎只会发生变化。我的问题是为什么。 readdir指向的inode和stat指向的inode是什么?
自我发布此消息后,这两个inode在过去4小时内都发生了变化。
我无权卸载。
我检查了从同一地址挂载的另一个目录,并且两个inode都与第一个目录不同,这表明每个目录都有两个对于该目录唯一的inode,但我不明白为什么。
答案 0 :(得分:3)
目录将具有inode:
$ ls -li
total 4
264332 drwx------ 2 attie attie 4096 Nov 3 22:46 mnt
在这种情况下,mnt
目录的inode为264322
。
但是如果我们现在在该目录上挂载文件系统,则inode将会更改:
$ truncate -s $((5 * 1024 * 1024)) myfs.ext2
$ mkfs.ext2 ./myfs.ext2
mke2fs 1.42.13 (17-May-2015)
Discarding device blocks: done
Creating filesystem with 5120 1k blocks and 1280 inodes
Allocating group tables: done
Writing inode tables: done
Writing superblocks and filesystem accounting information: done
$ sudo mount -o loop myfs.ext2 ./mnt
$ ls -li
total 265
2 drwxr-xr-x 3 root root 1024 Nov 3 22:49 mnt
264339 -rw------- 1 attie attie 5242880 Nov 3 22:49 myfs.ext2
现在,mnt
的inode似乎是2
......?! (请注意,所有者/组也已更改)。
如果我们要在此目录上运行您的应用程序(删除chdir()
并将DIR_NAME
更改为mnt
),那么我们会得到一个有趣的结果:
$ ./test
readdir (3) gives an inode of: 264332.
stat (2) gives an inode of: 2.
readdir()
说264332
mnt
目录的inode。stat()
说2
这是有道理的,因为readdir()
正在遍历给定目录的节点,返回每个节点的信息(但它不是完整地检查它们)......而stat()
是返回给定完整路径的信息,包括解析任何挂载点。
在您的情况下,您有一个NFS挂载,远程文件系统挂载在此系统上的目录(具有inode)上...但它也是远程系统上的目录(具有inode)。
我们可以通过以下方式进一步证明这一点:
$ ls -lia /
total 137
2 drwxr-xr-x 25 root root 4096 Oct 11 17:17 .
2 drwxr-xr-x 25 root root 4096 Oct 11 17:17 ..
2097153 drwxr-xr-x 2 root root 12288 Oct 10 13:36 bin
[...]
正如您所看到的,/
上挂载的文件系统的根也具有2
的inode ... inode不是全局唯一,但在文件系统中仅是唯一的。
如果您参与了作业的标记,那么我说这两个答案都是可以接受的......对于那些可能对这个世界来说很陌生的学生来说,这是一个非常微妙和复杂的事情。理解。
可以使用stat()
轻松检查ls -i
答案。
我认为在没有编写一个小应用程序(这不是问题)的情况下检查readdir()
答案会非常繁琐......如果您有权限,可以使用绑定挂载:
$ mkdir mnt2
$ sudo mount -o bind . ./mnt2
$ ls -li
total 285
2 drwxr-xr-x 3 root root 1024 Nov 3 22:49 mnt
264319 drwx------ 4 attie attie 4096 Nov 3 23:09 mnt2
264339 -rw------- 1 attie attie 5242880 Nov 3 22:49 myfs.ext2
264346 -rwx------ 1 attie attie 9160 Nov 3 23:00 test
264349 -rw------- 1 attie attie 956 Nov 3 23:00 test.c
$ ls -li mnt2
total 288
264332 drwx------ 2 attie attie 4096 Nov 3 22:46 mnt
264347 drwx------ 2 attie attie 4096 Nov 3 23:09 mnt2
264339 -rw------- 1 attie attie 5242880 Nov 3 22:49 myfs.ext2
264346 -rwx------ 1 attie attie 9160 Nov 3 23:00 test
264349 -rw------- 1 attie attie 956 Nov 3 23:00 test.c