Back to home page

OSCL-LXR

 
 

    


0001 SPDX-License-Identifier: GPL-2.0
0002 
0003 Chinese translated version of Documentation/filesystems/sysfs.rst
0004 
0005 If you have any comment or update to the content, please contact the
0006 original document maintainer directly.  However, if you have a problem
0007 communicating in English you can also ask the Chinese maintainer for
0008 help.  Contact the Chinese maintainer if this translation is outdated
0009 or if there is a problem with the translation.
0010 
0011 Maintainer: Patrick Mochel      <mochel@osdl.org>
0012                 Mike Murphy <mamurph@cs.clemson.edu>
0013 Chinese maintainer: Fu Wei <tekkamanninja@gmail.com>
0014 ---------------------------------------------------------------------
0015 Documentation/filesystems/sysfs.rst 的中文翻譯
0016 
0017 如果想評論或更新本文的內容,請直接聯繫原文檔的維護者。如果你使用英文
0018 交流有困難的話,也可以向中文版維護者求助。如果本翻譯更新不及時或者翻
0019 譯存在問題,請聯繫中文版維護者。
0020 英文版維護者: Patrick Mochel    <mochel@osdl.org>
0021                 Mike Murphy <mamurph@cs.clemson.edu>
0022 中文版維護者: 傅煒 Fu Wei <tekkamanninja@gmail.com>
0023 中文版翻譯者: 傅煒 Fu Wei <tekkamanninja@gmail.com>
0024 中文版校譯者: 傅煒 Fu Wei <tekkamanninja@gmail.com>
0025 繁體中文版校譯者:胡皓文 Hu Haowen <src.res@email.cn>
0026 
0027 
0028 以下爲正文
0029 ---------------------------------------------------------------------
0030 sysfs - 用於導出內核對象(kobject)的文件系統
0031 
0032 Patrick Mochel  <mochel@osdl.org>
0033 Mike Murphy <mamurph@cs.clemson.edu>
0034 
0035 修訂:    16 August 2011
0036 原始版本:   10 January 2003
0037 
0038 
0039 sysfs 簡介:
0040 ~~~~~~~~~~
0041 
0042 sysfs 是一個最初基於 ramfs 且位於內存的文件系統。它提供導出內核
0043 數據結構及其屬性,以及它們之間的關聯到用戶空間的方法。
0044 
0045 sysfs 始終與 kobject 的底層結構緊密相關。請閱讀
0046 Documentation/core-api/kobject.rst 文檔以獲得更多關於 kobject 接口的
0047 信息。
0048 
0049 
0050 使用 sysfs
0051 ~~~~~~~~~~~
0052 
0053 只要內核配置中定義了 CONFIG_SYSFS ,sysfs 總是被編譯進內核。你可
0054 通過以下命令掛載它:
0055 
0056     mount -t sysfs sysfs /sys
0057 
0058 
0059 創建目錄
0060 ~~~~~~~~
0061 
0062 任何 kobject 在系統中註冊,就會有一個目錄在 sysfs 中被創建。這個
0063 目錄是作爲該 kobject 的父對象所在目錄的子目錄創建的,以準確地傳遞
0064 內核的對象層次到用戶空間。sysfs 中的頂層目錄代表著內核對象層次的
0065 共同祖先;例如:某些對象屬於某個子系統。
0066 
0067 Sysfs 在與其目錄關聯的 kernfs_node 對象中內部保存一個指向實現
0068 目錄的 kobject 的指針。以前,這個 kobject 指針被 sysfs 直接用於
0069 kobject 文件打開和關閉的引用計數。而現在的 sysfs 實現中,kobject
0070 引用計數只能通過 sysfs_schedule_callback() 函數直接修改。
0071 
0072 
0073 屬性
0074 ~~~~
0075 
0076 kobject 的屬性可在文件系統中以普通文件的形式導出。Sysfs 爲屬性定義
0077 了面向文件 I/O 操作的方法,以提供對內核屬性的讀寫。
0078 
0079 
0080 屬性應爲 ASCII 碼文本文件。以一個文件只存儲一個屬性值爲宜。但一個
0081 文件只包含一個屬性值可能影響效率,所以一個包含相同數據類型的屬性值
0082 數組也被廣泛地接受。
0083 
0084 混合類型、表達多行數據以及一些怪異的數據格式會遭到強烈反對。這樣做是
0085 很丟臉的,而且其代碼會在未通知作者的情況下被重寫。
0086 
0087 
0088 一個簡單的屬性結構定義如下:
0089 
0090 struct attribute {
0091         char                    * name;
0092         struct module           *owner;
0093         umode_t                 mode;
0094 };
0095 
0096 
0097 int sysfs_create_file(struct kobject * kobj, const struct attribute * attr);
0098 void sysfs_remove_file(struct kobject * kobj, const struct attribute * attr);
0099 
0100 
0101 一個單獨的屬性結構並不包含讀寫其屬性值的方法。子系統最好爲增刪特定
0102 對象類型的屬性定義自己的屬性結構體和封裝函數。
0103 
0104 例如:驅動程序模型定義的 device_attribute 結構體如下:
0105 
0106 struct device_attribute {
0107         struct attribute        attr;
0108         ssize_t (*show)(struct device *dev, struct device_attribute *attr,
0109                         char *buf);
0110         ssize_t (*store)(struct device *dev, struct device_attribute *attr,
0111                          const char *buf, size_t count);
0112 };
0113 
0114 int device_create_file(struct device *, const struct device_attribute *);
0115 void device_remove_file(struct device *, const struct device_attribute *);
0116 
0117 爲了定義設備屬性,同時定義了一下輔助宏:
0118 
0119 #define DEVICE_ATTR(_name, _mode, _show, _store) \
0120 struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store)
0121 
0122 例如:聲明
0123 
0124 static DEVICE_ATTR(foo, S_IWUSR | S_IRUGO, show_foo, store_foo);
0125 
0126 等同於如下代碼:
0127 
0128 static struct device_attribute dev_attr_foo = {
0129        .attr    = {
0130                 .name = "foo",
0131                 .mode = S_IWUSR | S_IRUGO,
0132                 .show = show_foo,
0133                 .store = store_foo,
0134         },
0135 };
0136 
0137 
0138 子系統特有的回調函數
0139 ~~~~~~~~~~~~~~~~~~~
0140 
0141 當一個子系統定義一個新的屬性類型時,必須實現一系列的 sysfs 操作,
0142 以幫助讀寫調用實現屬性所有者的顯示和儲存方法。
0143 
0144 struct sysfs_ops {
0145         ssize_t (*show)(struct kobject *, struct attribute *, char *);
0146         ssize_t (*store)(struct kobject *, struct attribute *, const char *, size_t);
0147 };
0148 
0149 [子系統應已經定義了一個 struct kobj_type 結構體作爲這個類型的
0150 描述符,並在此保存 sysfs_ops 的指針。更多的信息參見 kobject 的
0151 文檔]
0152 
0153 sysfs 會爲這個類型調用適當的方法。當一個文件被讀寫時,這個方法會
0154 將一般的kobject 和 attribute 結構體指針轉換爲適當的指針類型後
0155 調用相關聯的函數。
0156 
0157 
0158 示例:
0159 
0160 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
0161 
0162 static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
0163                              char *buf)
0164 {
0165         struct device_attribute *dev_attr = to_dev_attr(attr);
0166         struct device *dev = kobj_to_dev(kobj);
0167         ssize_t ret = -EIO;
0168 
0169         if (dev_attr->show)
0170                 ret = dev_attr->show(dev, dev_attr, buf);
0171         if (ret >= (ssize_t)PAGE_SIZE) {
0172                 printk("dev_attr_show: %pS returned bad count\n",
0173                                 dev_attr->show);
0174         }
0175         return ret;
0176 }
0177 
0178 
0179 
0180 讀寫屬性數據
0181 ~~~~~~~~~~~~
0182 
0183 在聲明屬性時,必須指定 show() 或 store() 方法,以實現屬性的
0184 讀或寫。這些方法的類型應該和以下的設備屬性定義一樣簡單。
0185 
0186 ssize_t (*show)(struct device *dev, struct device_attribute *attr, char *buf);
0187 ssize_t (*store)(struct device *dev, struct device_attribute *attr,
0188                  const char *buf, size_t count);
0189 
0190 也就是說,他們應只以一個處理對象、一個屬性和一個緩衝指針作爲參數。
0191 
0192 sysfs 會分配一個大小爲 (PAGE_SIZE) 的緩衝區並傳遞給這個方法。
0193 Sysfs 將會爲每次讀寫操作調用一次這個方法。這使得這些方法在執行時
0194 會出現以下的行爲:
0195 
0196 - 在讀方面(read(2)),show() 方法應該填充整個緩衝區。回想屬性
0197   應只導出了一個屬性值或是一個同類型屬性值的數組,所以這個代價將
0198   不會不太高。
0199 
0200   這使得用戶空間可以局部地讀和任意的向前搜索整個文件。如果用戶空間
0201   向後搜索到零或使用『0』偏移執行一個pread(2)操作,show()方法將
0202   再次被調用,以重新填充緩存。
0203 
0204 - 在寫方面(write(2)),sysfs 希望在第一次寫操作時得到整個緩衝區。
0205   之後 Sysfs 傳遞整個緩衝區給 store() 方法。
0206 
0207   當要寫 sysfs 文件時,用戶空間進程應首先讀取整個文件,修該想要
0208   改變的值,然後回寫整個緩衝區。
0209 
0210   在讀寫屬性值時,屬性方法的執行應操作相同的緩衝區。
0211 
0212 註記:
0213 
0214 - 寫操作導致的 show() 方法重載,會忽略當前文件位置。
0215 
0216 - 緩衝區應總是 PAGE_SIZE 大小。對於i386,這個值爲4096。
0217 
0218 - show() 方法應該返回寫入緩衝區的字節數,也就是 scnprintf()的
0219   返回值。
0220 
0221 - show() 方法在將格式化返回值返回用戶空間的時候,禁止使用snprintf()。
0222   如果可以保證不會發生緩衝區溢出,可以使用sprintf(),否則必須使用
0223   scnprintf()。
0224 
0225 - store() 應返回緩衝區的已用字節數。如果整個緩存都已填滿,只需返回
0226   count 參數。
0227 
0228 - show() 或 store() 可以返回錯誤值。當得到一個非法值,必須返回一個
0229   錯誤值。
0230 
0231 - 一個傳遞給方法的對象將會通過 sysfs 調用對象內嵌的引用計數固定在
0232   內存中。儘管如此,對象代表的物理實體(如設備)可能已不存在。如有必要,
0233   應該實現一個檢測機制。
0234 
0235 一個簡單的(未經實驗證實的)設備屬性實現如下:
0236 
0237 static ssize_t show_name(struct device *dev, struct device_attribute *attr,
0238                          char *buf)
0239 {
0240         return scnprintf(buf, PAGE_SIZE, "%s\n", dev->name);
0241 }
0242 
0243 static ssize_t store_name(struct device *dev, struct device_attribute *attr,
0244                           const char *buf, size_t count)
0245 {
0246         snprintf(dev->name, sizeof(dev->name), "%.*s",
0247                  (int)min(count, sizeof(dev->name) - 1), buf);
0248         return count;
0249 }
0250 
0251 static DEVICE_ATTR(name, S_IRUGO, show_name, store_name);
0252 
0253 
0254 (注意:真正的實現不允許用戶空間設置設備名。)
0255 
0256 頂層目錄布局
0257 ~~~~~~~~~~~~
0258 
0259 sysfs 目錄的安排顯示了內核數據結構之間的關係。
0260 
0261 頂層 sysfs 目錄如下:
0262 
0263 block/
0264 bus/
0265 class/
0266 dev/
0267 devices/
0268 firmware/
0269 net/
0270 fs/
0271 
0272 devices/ 包含了一個設備樹的文件系統表示。他直接映射了內部的內核
0273 設備樹,反映了設備的層次結構。
0274 
0275 bus/ 包含了內核中各種總線類型的平面目錄布局。每個總線目錄包含兩個
0276 子目錄:
0277 
0278         devices/
0279         drivers/
0280 
0281 devices/ 包含了系統中出現的每個設備的符號連結,他們指向 root/ 下的
0282 設備目錄。
0283 
0284 drivers/ 包含了每個已爲特定總線上的設備而掛載的驅動程序的目錄(這裡
0285 假定驅動沒有跨越多個總線類型)。
0286 
0287 fs/ 包含了一個爲文件系統設立的目錄。現在每個想要導出屬性的文件系統必須
0288 在 fs/ 下創建自己的層次結構(參見Documentation/filesystems/fuse.rst)。
0289 
0290 dev/ 包含兩個子目錄: char/ 和 block/。在這兩個子目錄中,有以
0291 <major>:<minor> 格式命名的符號連結。這些符號連結指向 sysfs 目錄
0292 中相應的設備。/sys/dev 提供一個通過一個 stat(2) 操作結果,查找
0293 設備 sysfs 接口快捷的方法。
0294 
0295 更多有關 driver-model 的特性信息可以在 Documentation/driver-api/driver-model/
0296 中找到。
0297 
0298 
0299 TODO: 完成這一節。
0300 
0301 
0302 當前接口
0303 ~~~~~~~~
0304 
0305 以下的接口層普遍存在於當前的sysfs中:
0306 
0307 - 設備 (include/linux/device.h)
0308 ----------------------------------
0309 結構體:
0310 
0311 struct device_attribute {
0312         struct attribute        attr;
0313         ssize_t (*show)(struct device *dev, struct device_attribute *attr,
0314                         char *buf);
0315         ssize_t (*store)(struct device *dev, struct device_attribute *attr,
0316                          const char *buf, size_t count);
0317 };
0318 
0319 聲明:
0320 
0321 DEVICE_ATTR(_name, _mode, _show, _store);
0322 
0323 增/刪屬性:
0324 
0325 int device_create_file(struct device *dev, const struct device_attribute * attr);
0326 void device_remove_file(struct device *dev, const struct device_attribute * attr);
0327 
0328 
0329 - 總線驅動程序 (include/linux/device.h)
0330 --------------------------------------
0331 結構體:
0332 
0333 struct bus_attribute {
0334         struct attribute        attr;
0335         ssize_t (*show)(struct bus_type *, char * buf);
0336         ssize_t (*store)(struct bus_type *, const char * buf, size_t count);
0337 };
0338 
0339 聲明:
0340 
0341 BUS_ATTR(_name, _mode, _show, _store)
0342 
0343 增/刪屬性:
0344 
0345 int bus_create_file(struct bus_type *, struct bus_attribute *);
0346 void bus_remove_file(struct bus_type *, struct bus_attribute *);
0347 
0348 
0349 - 設備驅動程序 (include/linux/device.h)
0350 -----------------------------------------
0351 
0352 結構體:
0353 
0354 struct driver_attribute {
0355         struct attribute        attr;
0356         ssize_t (*show)(struct device_driver *, char * buf);
0357         ssize_t (*store)(struct device_driver *, const char * buf,
0358                          size_t count);
0359 };
0360 
0361 聲明:
0362 
0363 DRIVER_ATTR(_name, _mode, _show, _store)
0364 
0365 增/刪屬性:
0366 
0367 int driver_create_file(struct device_driver *, const struct driver_attribute *);
0368 void driver_remove_file(struct device_driver *, const struct driver_attribute *);
0369 
0370 
0371 文檔
0372 ~~~~
0373 
0374 sysfs 目錄結構以及其中包含的屬性定義了一個內核與用戶空間之間的 ABI。
0375 對於任何 ABI,其自身的穩定和適當的文檔是非常重要的。所有新的 sysfs
0376 屬性必須在 Documentation/ABI 中有文檔。詳見 Documentation/ABI/README。
0377