免費(fèi)谘詢熱線
13621929115前言sysfs是一個基於RAM的文件係統,它和Kobject一(yī)起,可以將Kernel的數據結構導出到用戶空間,以文件(jiàn)目錄結構的形式,提供對這些數據結(jié)構(以及數據結構的屬性)的訪問支持。
sysfs具(jù)備文件係統的所有屬性,而本文主(zhǔ)要側重其設備模型上海模型公司的特性,因此不會涉及過多的(de)文件係統實(shí)現細節,而隻介紹sysfs在Linux設備模型(xíng)中的作用和使用方法具體包括:sysfs和Kobject的關係attribute的概念
sysfs的文件係統操作接口2. sysfs和Kobject的關係在"Linux設備模型_Kobject”文章中,有提到過,每一個Ko上海模型公司bject,都會對應sysfs中的一個目錄(lù)因此在將Kobject添加到Kernel時,create_dir接(jiē)口會(huì)調用sysfs文件係統的創建目錄接口,創建和Kobject對應的目錄,相關的(de)代碼如下:。
int kobject_add(struct kobject *kobj, struct kobje上海模型公司ct *parent,
const char *fmt, ...)
{
[...]
retval = kobject_add_varg(kobj, parent, fmt, args);
[...]
}
上海模型公司
static __printf(3, 0) int kobject_add_varg(struct kobject *kobj,
struct kobject *parent,
const char *fmt, va_list vargs)
上海(hǎi)模型公司 {
[...]
return kobject_add_internal(kobj);
}
static int kobject_add_internal(struct kobject *kobj)
{
上海模(mó)型公(gōng)司 [...]
error = create_dir(kobj);
[...]
}
static int create_dir(struct kobject *kobj)
{
[...]
上海模型公司 error = sysfs_create_dir_ns(kobj, kobject_namespace(kobj));
[...]
}
3. attribute3.1 attribute的功能概述(shù)在sysfs中,為什麽會有attribute的概(gài)念呢?其實(shí)它是對應(yīng)k上海模型公司object而言的,指的是kobject的“屬性”www.17C.com知道, sysfs中的目錄描述了kobject,而kobject是(shì)特定數據類型變量(如struct device)的體現。
因此kobject的屬性,就是這些變量的屬性它可(kě)以是任何東西,名稱、一個內部(bù)變量、一個(gè)字符串等等而attribute,在sy上海模型公司sfs文件係(xì)統中是以文件的(de)形(xíng)式提供的,即:kobject的所有屬性(xìng),都(dōu)在它對(duì)應的sysfs目錄下以文件(jiàn)的(de)形式呈現。
這些文件一般是可讀(dú)、寫的,而kernel中定(dìng)義了這些屬性的模塊(kuài),會根據用戶空間的讀寫(xiě)操作,記錄和返回這些attribute的值總結一(yī)下:所謂的attibute,就(jiù)是內核空間和用戶空間進(jìn)行上海模型公司信息(xī)交互的一種方法。
例如某個(gè)driver定義了一個變(biàn)量,卻希望用戶空間(jiān)程序可(kě)以修改該變量,以控製driver的(de)運行行為,那麽就可以將(jiāng)該變量以sysfs attribute的形式開放(fàng)出來Linux內核中,attribute分為普通的attribute和二進製attribute,如下:
struct at上海(hǎi)模型公司tribute {
const char *name;
umode_t mode;
#ifdef CONFIG_DEBUG_LOCK_ALLOC
bool ignore_lockdep:1;
struct lock_cl上海模(mó)型公司ass_key *key;
struct lock_class_key skey;
#endif
};
struct bin_attribute {
struct attribute attr;
siz上海模型公司e_t size;
void *private;
struct address_space *(*f_mapping)(void);
ssize_t (*read)(struct file *, struct kobject *, struct bin上海模型公(gōng)司_attribute *,
char *, loff_t, size_t);
ssize_t (*write)(struct file *, struct kobject *, struct bin_attribute *,
char *, loff_上海模型(xíng)公(gōng)司t, size_t);
int (*mmap)(struct file *, struct kobject *, struct bin_attribute *attr,
struct vm_area_struct *vma);
};
struct att上海模型(xíng)公司ribute為普通的attribute,使(shǐ)用該attribute生成的sysfs文(wén)件,隻能用字符串的形式(shì)讀寫(後麵會說為什麽)而struct bin_attribute在struct attribute的基礎上,增加了read、write等函數,因此它所生成的sysfs文件可以用任(rèn)何方式讀寫。
說完基上海模型公司本概念,www.17C.com要問兩個問題:Kernel怎麽把attribute變(biàn)成sysfs中的文件呢?用戶空間對sysfs的(de)文件進行的讀寫操(cāo)作,怎麽傳遞給Kernel呢?下麵來看看這個過程3.2 attibute文件(jiàn)的創(chuàng)建。
在(zài)linux內核中,attibute文件的創建(jiàn)是由fs/sysfs/file.c中sysf上海模型公司(sī)s_create_file接口完成的,該(gāi)接口的實現沒有什麽特(tè)殊之處(chù),大(dà)多(duō)是文(wén)件係統相關(guān)的操作,和設(shè)備模型沒有太(tài)多的關係,這裏先略過不提。
3.3 attibute文件的read和write看到3.1章節struct attribute的原型時,也許www.17C.com會(huì)犯嘀咕,該結構(gòu)很簡單啊,name表示文件名稱,m上海模型公司ode表示文件模式,其它的字段都是內核用於debug Kernel Lock的,那(nà)文件操作的接口在哪裏呢(ne)?
不著急,www.17C.com去fs/sysfs目(mù)錄下看看sysfs相關的代碼(mǎ)邏輯所有的文件(jiàn)係統,都會(huì)定義(yì)一個struct file_operations變量(liàng),用於描述本文件係(xì)統的操作接口,sysfs也不例外:/上海模型公(gōng)司/ 2.6 內核 /fs/sysfs/file.c 新內核已經變化
const struct file_operations sysfs_file_operations = {
.read = sysfs_read_file,
.write = sys上海模型公司fs_write_file,
.llseek = generic_file_llseek,
.open = sysfs_open_file,
.release = sysfs_release,
.poll = sysfs_poll,
上海(hǎi)模型公司 };
/**
* sysfs_read_file - read an attribute.
* @file: file pointer.
* @buf: buffer to fill.
* @count:上海模型(xíng)公司 number of bytes to read.
* @ppos: starting offset in file.
*
* Userspace wants to read an attribute file. The attribute descr上海模型公司iptor
* is in the files ->d_fsdata. The target object is in the directorys
* ->d_fsdata.
*
* We call fill_read_buffer(上海模型公司) to allocate and fill the buffer from the
* objects show() method exactly once (if the read is happening from
* the beginning of the 上海模型(xíng)公司file). That should fill the entire buffer with
* all the data the object has to offer for that attribute.
* We then call flush_read_bu上海模型公司(sī)ffer() to copy the buffer to userspace
* in the increments specified.
*/
static ssize_t
sysfs_read_file(struct file *上海模型(xíng)公司file, char __user *buf, size_t count, loff_t *ppos)
{
struct sysfs_buffer * buffer = file->private_data;
ssize_t retval = 0;
上海模型公司 mutex_lock(&buffer->mutex);
if (buffer->needs_read_fill || *ppos == 0) {
retval = fill_read_buffer(file->f_path.dentry,buffer)上海模型公司;
if (retval)
goto out;
}
pr_debug("%s: count = %zd, ppos = %lld, buf = %s\n",
__func__, count, *ppos, buffer-上海模型公司>page);
retval = simple_read_from_buffer(buf, count, ppos, buffer->page,
buffer->count);
out:
mutex_unlock(&buffer->mu上海模(mó)型公司tex);
return retval;
}
static int fill_read_buffer(struct dentry * dentry, struct sysfs_buffer * buffer)
{
s上海模型公司truct sysfs_dirent *attr_sd = dentry->d_fsdata;
struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
const struct sysfs_ops * ops = b上海模型公司uffer->ops;
[...]
count = ops->show(kobj, attr_sd->s_attr.attr, buffer->page);
[...]
}。
read處理看著很簡單,sysfs_read_file從fil上海模型公司e指針中(zhōng)取一個(gè)私有指針(zhēn)(注:大家可以稍微留一下心,私(sī)有數據的概念,在VFS中使用是非常普遍的),轉換為(wéi)一個struct sysfs_buffer類型的指針,以此為參數(buffer),轉(zhuǎn)身就調用(yòng)fill_read_buffer接口。
而(ér)fill_read_buffer接口,直接從buffer指針中(zhōng)取出上海模(mó)型公司一個struct sysfs_ops指針,調用該指針的show函數,即完成了文件的read操作那麽後續呢?當然是由ops->show接口接著處理咯。
而具體怎麽處理,就是其它模塊(例(lì)如某個driver)的事了,sysfs不再關心(其實,Linux大多的核心代碼(mǎ),都是隻提供架構和機製,具體的實現,也就是上海模型公司苦力,留給(gěi)那些碼農吧!這就是設計的(de)魅力)不過還沒完,這個struct sysfs_ops指針哪(nǎ)來的?好吧,www.17C.com再看看open(sysfs_open_file)接口吧。
static int sysfs_open_file(struct inode *inode, struct file *file)
上海模型公司 {
struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
上海模型(xíng)公司struct sysfs_buffer *buffer;
const struct sysfs_ops *ops;
int error = -EACCES;
char *p;
p = d_path(&file->f_path, las上海模型公司t_sysfs_file, sizeof(last_sysfs_file));
if (!IS_ERR(p))
memmove(last_sysfs_file, p, strlen(p) + 1);
/* need attr_sd for attr 上(shàng)海模型公司and ops, its parent for kobj */
if (!sysfs_get_active(attr_sd))
return -ENODEV;
/* every kobject with an attribute needs a kt上海模型公司ype assigned */
if (kobj->ktype && kobj->ktype->sysfs_ops)
ops = kobj->ktype->sysfs_ops;
else {
WARN(1, KERN_ERR "miss上海模型公司ing sysfs attribute operations for "
"kobject: %s\n", kobject_name(kobj));
goto err_out;
}
/* File needs write suppor上(shàng)海模型公司t.
* The inodes perms must say its ok,
* and we must have a store method.
*/
if (file->f_mode & FMODE_WRITE) {
上海模型公司 if (!(inode->i_mode & S_IWUGO) || !ops->store)
goto err_out;
}
/* File needs read support.
* The inodes perms must s上海模型公司ay its ok, and we there
* must be a show method for it.
*/
if (file->f_mode & FMODE_READ) {
if (!(inode->i_mode & S_IR上海模型公司UGO) || !ops->show)
goto err_out;
}
/* No error? Great, allocate a buffer for the file, and store it
* it in file->pr上海模型公司ivate_data for easy access.
*/
error = -ENOMEM;
buffer = kzalloc(sizeof(struct sysfs_buffer), GFP_KERNEL);
if (!buffer上海模型公司)
goto err_out;
mutex_init(&buffer->mutex);
buffer->needs_read_fill = 1;
buffer->ops = ops;
file->private_dat上(shàng)海模型公司(sī)a = buffer;
/* make sure we have open dirent struct */
error = sysfs_get_open_dirent(attr_sd, buffer);
if (error)
got上海模型公司o err_free;
/* open succeeded, put active references */
sysfs_put_active(attr_sd);
return 0;
err_free:
kfree上海模型公(gōng)司(buffer);
err_out:
sysfs_put_active(attr_sd);
return error;
}
哦,原來和ktype有關係這個指針是從該attribute所(suǒ)從屬的kobject中拿的再去看一下"Linux設備模上海模型公司型_Kobject”中ktype的定義,還真有一個struct sysfs_ops的(de)指針。
www.17C.com注意一下14行的注釋以及其後代碼邏輯,如果從屬(shǔ)的kobject(就(jiù)是attribute文(wén)件所在的目(mù)錄)沒有ktype,或者沒有ktype->sysfs_ops指針,是不允許它(tā)注冊(cè)任何attribute的!
經上海模(mó)型公司(sī)過確認後,sysfs_open_file從ktype中取出struct sysfs_ops指針,並在隨(suí)後的代碼邏輯中,分配一個struct sysfs_buffer類型的指針(buffer),並把struct sysfs_ops指針保存在其(qí)中,隨後(hòu)(注意哦),把buffer指針交給file的priv上海模型公司ate_data,隨後read/write等接口便可以取出使用。
嗯!慣用伎倆!struct sysfs_ops {
ssize_t (*show)(struct kobject *, struct attribute *,char *);
ssize_t (*stor上海模(mó)型公(gōng)司e)(struct kobject *,struct attribute *,const char *, size_t);
};
attribute文件的write過程(chéng)和read類似,這裏就不再多(duō)說另外,上麵隻分析了普通attribute的(de)邏輯,而二進製類型的呢?也類似,去看看fs/s上海模型公司ysfs/bin.c吧(ba),這裏也不說了講到這裏,應該(gāi)已經結束了,事實卻不是如此。
上麵read/write的數據流,隻到kobject(也就是(shì)目錄)級別哦,而真正需要操作的是attribute(文件(jiàn))啊!這中(zhōng)間一定還有一層(céng)轉換!確實,不(bú)過又交給其它模(mó)塊了 下麵www.17C.com通過一個例子,來說明如何轉換的。
4. s上海模型公司ysfs在設備模(mó)型中的應用總結讓我(wǒ)們通過設備模型class.c中有關sysfs的實現(xiàn),來總結一下sysfs的應用(yòng)方(fāng)式首先(xiān),在class.c中,定義了Class所需的ktype以及sysfs_ops類型(xíng)的變量,如下:。
static const struct sysfs_ops class_sysfs_上海模型公司ops = {
.show = class_attr_show,
.store = class_attr_store,
};
static struct kobj_type class_ktype = {
.sysfs上海模型公司_ops = &class_sysfs_ops,
.release = class_release,
.child_ns_type = class_child_ns_type,
};
由前麵章節的描述可知,所有class_type的Kobject下麵的a上海模型公(gōng)司ttribute文件的讀寫操作,都會交給class_attr_show和class_attr_store兩個接口處理以class_attr_show為例:。
#define to_class_attr(_attr) container_of(_attr, struct class_attribute,上海模型公司 attr)
static ssize_t class_attr_show(struct kobject *kobj, struct attribute *attr,
char *buf)
{
struct class_attribu上海模型公司te *class_attr = to_class_attr(attr);
struct subsys_private *cp = to_subsys_private(kobj);
ssize_t ret = -EIO;
if (class_attr上海模型公司->show)
ret = class_attr->show(cp->class, class_attr, buf);
return ret;
}
該(gāi)接口使用container_of從struct attribute類型的指針中取得一個class模塊的自上海模型公司定義指針:struct class_attribute,該指針中(zhōng)包含了class模塊自身的(de)show和store接口。
下(xià)麵是struct class_attribute的聲明:struct class_attribute {
struct attribute attr;
上海模型公司 ssize_t (*show)(struct class *class, struct class_attribute *attr,
char *buf);
ssize_t (*store)(struct class *class, struct class_att上海模型公司ribute *attr,
const char *buf, size_t count);
};
因此,所有需要使用(yòng)attribute的模塊,都不會直(zhí)接定義struct attribute變量,而(ér)是通過一個自定義的數據結(jié)構,該(gāi)數(shù)據結構(gòu)的(de)一個成員是struct attri上海模型公司bute類型(xíng)的變量,並提供show和store回調函(hán)數。
然後在該模塊ktype所對(duì)應的(de)struct sysfs_ops變量中(zhōng),實現該本模塊整體的(de)show和store函數,並在被調用時,轉接到(dào)自定義數據結構(struct class_attribute)中的show和store函數中。
這樣(yàng),每個atr上海模型公司(sī)ribute文件,實際上對應(yīng)到一個自定義數據結構變量(liàng)中了
Copyright © 2002-2020 上海潤之模型設計有限公司 版(bǎn)權(quán)所有 展示模型,展品模型,展廳模型,展示道具,展廳(tīng)展品,展(zhǎn)品道具,模型定製,模型公司,上海模型公司 備案號:滬ICP備20018260號