免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: turbo99cn
打印 上一主题 下一主题

进程所在的文件系统是根据什么确定的? [复制链接]

论坛徽章:
0
11 [报告]
发表于 2006-01-06 15:53 |只看该作者
不知道你能否理解这句话,它标识的并不是文件系统是ext2还是ext3,它只是.....唉,不说了,地球人都明白!

论坛徽章:
0
12 [报告]
发表于 2006-01-06 15:57 |只看该作者
还要说一下,我说"它有病啊"说的是那个进程有病啊,并没有人身攻击的意图,不要误会!!!!

难道幽默一下,也要招人非议,唉“做人难,难做人啊”

论坛徽章:
0
13 [报告]
发表于 2006-01-06 16:15 |只看该作者
哦,那我再看看,呵呵 !呵呵

论坛徽章:
0
14 [报告]
发表于 2006-01-06 16:29 |只看该作者
task_struct ->struct fs *fs_struct->struct vfsmount * rootmnt, * pwdmnt, * altrootmnt-->
struct super_block *mnt_sb-->struct file_system_type *s_type-->const char *name.

不知道这样子能不能得到文件系统的名字啊?
要不要写个模块试试阿?

论坛徽章:
0
15 [报告]
发表于 2006-01-06 16:50 |只看该作者
"进程所在文件系统"一说欠考虑,进程是在执行中的程序.而众所周知,程序是在内存中执行呢...
内存好像只分段不分文件系统吧?

论坛徽章:
0
16 [报告]
发表于 2006-01-06 16:59 |只看该作者
那当前进程的根目录是怎么回事?

论坛徽章:
0
17 [报告]
发表于 2006-01-06 17:01 |只看该作者
原帖由 艾斯尼勒 于 2006-1-6 16:50 发表
"进程所在文件系统"一说欠考虑,进程是在执行中的程序.而众所周知,程序是在内存中执行呢...
内存好像只分段不分文件系统吧?


进程需要打开文件,需要对文件进行open、write, 这些open、write等方法, VFS层的函数执行完毕之后,需要调用该“被打开文件”所在的文件系统(例如ext3、vfat等)定义的方法──如果有的话。具体文件系统也可以不提供这些方法的实现,仅依靠VFS。

论坛徽章:
0
18 [报告]
发表于 2006-01-06 17:38 |只看该作者
good explanation!

论坛徽章:
0
19 [报告]
发表于 2006-01-06 19:53 |只看该作者

回复 3楼 turbo99cn 的帖子

每个进程有一个namespace(current->namespace),
即进程所看到的文件系统树,
(different processes have different views of the filesystem)
在这个namespace下,进程可以改变根目录等属性,

进程的fs,应该从namespace来,
内核通过set_fs_root()把init进程的fs相关值设置为它的namespace->root,
见init_mount_tree()

这以后,通过clone(),子进程继承父进程的fs,
如果有必要,通过系统调用chroot(),改变fs结构的值。
请参考一下系统调用chroot(),以及内核的实现sys_chroot()



  1. static void __init init_mount_tree(void)
  2. {
  3.         struct vfsmount *mnt;
  4.         struct namespace *namespace;
  5.         struct task_struct *g, *p;

  6.         mnt = do_kern_mount("rootfs", 0, "rootfs", NULL);
  7.         if (IS_ERR(mnt))
  8.                 panic("Can't create rootfs");
  9.         namespace = kmalloc(sizeof(*namespace), GFP_KERNEL);
  10.         if (!namespace)
  11.                 panic("Can't allocate initial namespace");
  12.         atomic_set(&namespace->count, 1);
  13.         INIT_LIST_HEAD(&namespace->list);
  14.         init_rwsem(&namespace->sem);
  15.         list_add(&mnt->mnt_list, &namespace->list);
  16.         namespace->root = mnt;
  17.         mnt->mnt_namespace = namespace;

  18.         init_task.namespace = namespace;
  19.         read_lock(&tasklist_lock);
  20.         do_each_thread(g, p) {
  21.                 get_namespace(namespace);
  22.                 p->namespace = namespace;
  23.         } while_each_thread(g, p);
  24.         read_unlock(&tasklist_lock);

  25.         set_fs_pwd(current->fs, namespace->root, namespace->root->mnt_root);
  26.         set_fs_root(current->fs, namespace->root, namespace->root->mnt_root);
  27. }

  28. asmlinkage long sys_chroot(const char __user * filename)
  29. {
  30.         struct nameidata nd;
  31.         int error;

  32.         error = __user_walk(filename, LOOKUP_FOLLOW | LOOKUP_DIRECTORY | LOOKUP_NOALT, &nd);
  33.         if (error)
  34.                 goto out;

  35.         error = permission(nd.dentry->d_inode,MAY_EXEC,&nd);
  36.         if (error)
  37.                 goto dput_and_out;

  38.         error = -EPERM;
  39.         if (!capable(CAP_SYS_CHROOT))
  40.                 goto dput_and_out;

  41.         set_fs_root(current->fs, nd.mnt, nd.dentry);
  42.         set_fs_altroot();
  43.         error = 0;
  44. dput_and_out:
  45.         path_release(&nd);
  46. out:
  47.         return error;
  48. }

  49. /*
  50. * Replace the fs->{rootmnt,root} with {mnt,dentry}. Put the old values.
  51. * It can block. Requires the big lock held.
  52. */
  53. void set_fs_root(struct fs_struct *fs, struct vfsmount *mnt,
  54.                  struct dentry *dentry)
  55. {
  56.         struct dentry *old_root;
  57.         struct vfsmount *old_rootmnt;
  58.         write_lock(&fs->lock);
  59.         old_root = fs->root;
  60.         old_rootmnt = fs->rootmnt;
  61.         fs->rootmnt = mntget(mnt);
  62.         fs->root = dget(dentry);
  63.         write_unlock(&fs->lock);
  64.         if (old_root) {
  65.                 dput(old_root);
  66.                 mntput(old_rootmnt);
  67.         }
  68. }

  69. void set_fs_altroot(void)
  70. {
  71.         char *emul = __emul_prefix();
  72.         struct nameidata nd;
  73.         struct vfsmount *mnt = NULL, *oldmnt;
  74.         struct dentry *dentry = NULL, *olddentry;
  75.         int err;

  76.         if (!emul)
  77.                 goto set_it;
  78.         err = path_lookup(emul, LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_NOALT, &nd);
  79.         if (!err) {
  80.                 mnt = nd.mnt;
  81.                 dentry = nd.dentry;
  82.         }
  83. set_it:
  84.         write_lock(&current->fs->lock);
  85.         oldmnt = current->fs->altrootmnt;
  86.         olddentry = current->fs->altroot;
  87.         current->fs->altrootmnt = mnt;
  88.         current->fs->altroot = dentry;
  89.         write_unlock(&current->fs->lock);
  90.         if (olddentry) {
  91.                 dput(olddentry);
  92.                 mntput(oldmnt);
  93.         }
  94. }

复制代码

[ 本帖最后由 wheelz 于 2006-1-6 20:46 编辑 ]

论坛徽章:
0
20 [报告]
发表于 2006-01-08 01:08 |只看该作者
原帖由 albcamus 于 2006-1-6 17:01 发表


进程需要打开文件,需要对文件进行open、write, 这些open、write等方法, VFS层的函数执行完毕之后,需要调用该“被打开文件”所在的文件系统(例如ext3、vfat等)定义的方法──如果有的话。具体文件系统也 ...


原来是这个意思,我觉得若不是我误会楼主了就是楼主的说法还是欠妥呵呵
不过既然linux提供了vfs,那么底层到底什么fs对程序员来说应该当作透明的才对,关心它作什么?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP