新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > 從用戶態(tài)的open到內(nèi)核驅(qū)動實現(xiàn)流程

從用戶態(tài)的open到內(nèi)核驅(qū)動實現(xiàn)流程

作者: 時間:2012-06-25 來源:網(wǎng)絡(luò) 收藏

問題來源:

本文引用地址:http://www.butianyuan.cn/article/148863.htm

在講授Linux初級的時候,我發(fā)現(xiàn)困惑很多同學(xué)的是不真正理解從應(yīng)用層到我們自己所寫的層的調(diào)用過程,所以寫此文章來大概描述。

首先我們知道,在我們目前的Linux系統(tǒng)中,我們大概共約300左右個系統(tǒng)調(diào)用,其中syscall_table.S列出了所有的系統(tǒng)調(diào)用表。

在本文件中記錄了所有當(dāng)前平臺系統(tǒng)中所提供的系統(tǒng)調(diào)用表,其中第五項就包括:

.lONg sys_ /* 5 */

-----------------------------

查看sys_() 函數(shù),我們看到里面所完成的工作為:

1、查看打開的是否是大文件,如果是的話,置大文件標(biāo)志位:O_LARGEFILE

2、做do_sys_()函數(shù)調(diào)用。

3、檢查2的調(diào)用返回值ret是否有效。

-----------------------------

-----------------------------

查看do_sys_open()函數(shù)所完成的工作為:

調(diào)用getname() ,getname函數(shù)主要功能是在使用文件名之前將其拷貝到數(shù)據(jù)區(qū),正常結(jié)束時返回分配的空間首地址,出錯時返回錯誤代碼。

取得系統(tǒng)中可用的文件描述符fd。

調(diào)用do_filp_open()函數(shù),此函數(shù)使用了一個數(shù)據(jù)結(jié)構(gòu)nameidata來描述與文件相關(guān)的文件操作。

STruct nameidata {

struct dentry *dentry; // 目錄數(shù)據(jù)

struct vfsmount *mnt; // 虛擬文件掛載點數(shù)據(jù)

struct qstr last; // hash值

unsigned int flags; // 文件操作標(biāo)識

int last_type; // 類型

unsigned depth;

char *saved_names[MAX_NESteD_LINKS + 1];

union {

struct open_intent open;

} intent; // 專用數(shù)據(jù)

};

-----------------------------

-----------------------------

struct file *do_filp_open(const char * filename, int flags, int mode){

int namei_flags, error;

struct nameidata nd;

namei_flags = flags;

if ((namei_flags+1) O_ACCMODE)

namei_flags++; // 如果flags有O_WRONLY,則增加O_RDONLY

error = open_namei(filename, namei_flags, mode, nd);

// open_namei函數(shù)主要執(zhí)行文件操作的inode部分的打開等操作。

if (!error)

return nameidata_to_filp (nd, flags);

// 把文件的inod相關(guān)信息轉(zhuǎn)換成文件結(jié)構(gòu)。

return ERR_PTR(error); // 返回錯誤代碼

}

-----------------------------

-----------------------------

我們下面來看這個比較關(guān)鍵的函數(shù):nameidata_to_filp():

struct file *(struct nameidata *nd, int flags)

821 {

822 struct file *filp;

823

824 /* Pick up the filp from the open intent */

825 filp = nd->intent.open.file;

// 把相關(guān) file結(jié)構(gòu)的指針賦予 filp。

826 /* Has the filesystem initialised the file for us? */

827 if (filp->f_path.dentry == NULL)

828 filp = __dentry_open(nd->dentry, nd->mnt, flags, filp, NULL);

// ***** 關(guān)鍵函數(shù) ***** //

829 else

830 path_release(nd);

831 return filp;

832 }

-----------------------------

-----------------------------

關(guān)鍵函數(shù):__dentry_open():

static struct file *__dentry_open(struct dentry *dentry, struct vfsmount *mnt,

int flags, struct file *f,

int (*open)(struct inode *, struct file *))

{

......

695 f->f_pos = 0;

696 f->f_op = fops_get(inode->i_fop);

// 在這里進(jìn)行賦值,f->f_op = def_chr_fops,注意上文inode->i_fop中的賦值。

697 file_move(f, inode->i_sb->s_files);

698

699 if (!open f->f_op)

// 在調(diào)用__dentry_open時open賦值為空,所以!open為真。

700 pen = f->f_op->open;

// 在這里將open賦為chrdev_open。

701 if (open) {

702 error = open(inode, f);

// 這里調(diào)用chrdev_open, 參照下文。

703 if (error)

704 goto cleanup_all;

......

}

-----------------------------

-----------------------------

在函數(shù)chrdev_open中(/fs/char_dev.v):

int chrdev_open(struct inode * inode, struct file * filp)

{

......

kobj = kobj_lookup(cdev_map, inode->i_rdev, idx);

// 執(zhí)行kobj_lookup函數(shù),在cdev_map里尋找相應(yīng)的inode->i_rdev設(shè)備。

// cdev_map是一個256個probe結(jié)構(gòu)組成的數(shù)組,用于查找具有相應(yīng)設(shè)備號的設(shè)備。

// inode->i_rdev為設(shè)備號。

new = container_of(kobj, struct cdev, kobj);

//從kobj的位置倒算出cdev的內(nèi)存地址,獲得包含相應(yīng)kobj的cdev。

inode->i_cdev = p = new;

// 到這里p已經(jīng)為我們要的設(shè)備cdev了。

filp->f_op = fops_get(p->ops);

/ /拿到 cdev操作集。

// 至此以后read,write操作都通過file->f_op直接與我們要的設(shè)備操作集掛鉤了。

......

}

-----------------------------

到此,系統(tǒng)通過file->f_op 就與我們在設(shè)備里面的定義的相關(guān)操作聯(lián)系起來了,我們之前在寫驅(qū)動的功能操作就被系統(tǒng)通過應(yīng)用層的open 一步一步的調(diào)用到我們自己的open跟相關(guān)其他的操作了。



評論


相關(guān)推薦

技術(shù)專區(qū)

關(guān)閉