日本黄色一级经典视频|伊人久久精品视频|亚洲黄色色周成人视频九九九|av免费网址黄色小短片|黄色Av无码亚洲成年人|亚洲1区2区3区无码|真人黄片免费观看|无码一级小说欧美日免费三级|日韩中文字幕91在线看|精品久久久无码中文字幕边打电话

當前位置:首頁 > 嵌入式 > 嵌入式軟件
[導(dǎo)讀]從用戶態(tài)的open到內(nèi)核驅(qū)動實現(xiàn)流程

問題來源:

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

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

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

.lONg sys_open /* 5 */

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

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

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

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

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

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

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

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

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

取得系統(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; // 文件操作標識

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);

// 在這里進行賦值,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è)備驅(qū)動里面的定義的相關(guān)操作聯(lián)系起來了,我們之前在寫驅(qū)動實現(xiàn)的功能操作就被系統(tǒng)通過應(yīng)用層的open 一步一步的調(diào)用到我們自己的open跟相關(guān)其他的操作了。

本站聲明: 本文章由作者或相關(guān)機構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點,本站亦不保證或承諾內(nèi)容真實性等。需要轉(zhuǎn)載請聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請及時聯(lián)系本站刪除( 郵箱:macysun@21ic.com )。
換一批
延伸閱讀
關(guān)閉