日本免费全黄少妇一区二区三区-高清无码一区二区三区四区-欧美中文字幕日韩在线观看-国产福利诱惑在线网站-国产中文字幕一区在线-亚洲欧美精品日韩一区-久久国产精品国产精品国产-国产精久久久久久一区二区三区-欧美亚洲国产精品久久久久

詳解Linux 2.6內(nèi)核新文件系統(tǒng)變化機制( 三 )


atomic_t;;;;;count/* reference count */
struct inotify_device*dev;/* associated device */
struct inode;*inode; /* associated inode */
s32;;wd;;;/* watch descriptor */
u32;;mask;/* event mask for this watch */
};

d_list 指向所有 inotify_device 組成的列表的,i_list 指向所有被監(jiān)視 inode 組成的列表,count 是引用計數(shù),dev 指向該 watch 所在的 inotify 實例對應(yīng)的 inotify_device 結(jié)構(gòu),inode 指向該 watch 要監(jiān)視的 inode,wd 是分配給該 watch 的描述符,mask 是該 watch 的事件掩碼,表示它對哪些文件系統(tǒng)事件感興趣 。
結(jié)構(gòu) inotify_device 在用戶態(tài)調(diào)用 inotify_init() 時創(chuàng)建,當(dāng)關(guān)閉 inotify_init()返回的文件描述符時將被釋放 。結(jié)構(gòu) inotify_watch 在用戶態(tài)調(diào)用 inotify_add_watch()時創(chuàng)建,在用戶態(tài)調(diào)用 inotify_rm_watch() 或 close(fd) 時被釋放 。
無論是目錄還是文件,在內(nèi)核中都對應(yīng)一個 inode 結(jié)構(gòu),inotify 系統(tǒng)在 inode 結(jié)構(gòu)中增加了兩個字段:

#ifdef CONFIG_INOTIFY
struct list_head inotify_watches; /* watches on this inode */
struct semaphore inotify_sem; /* protects the watches list */
#endif

inotify_watches 是在被監(jiān)視目標上的 watch 列表,每當(dāng)用戶調(diào)用 inotify_add_watch()時,內(nèi)核就為添加的 watch 創(chuàng)建一個 inotify_watch 結(jié)構(gòu),并把它插入到被監(jiān)視目標對應(yīng)的 inode 的 inotify_watches 列表 。inotify_sem 用于同步對 inotify_watches 列表的訪問 。當(dāng)文件系統(tǒng)發(fā)生第一部分提到的事件之一時,相應(yīng)的文件系統(tǒng)代碼將顯示調(diào)用fsnotify_* 來把相應(yīng)的事件報告給 inotify 系統(tǒng),其中*號就是相應(yīng)的事件名,目前實現(xiàn)包括:
fsnotify_move,文件從一個目錄移動到另一個目錄
fsnotify_nameremove,文件從目錄中刪除
fsnotify_inoderemove,自刪除
fsnotify_create,創(chuàng)建新文件
fsnotify_mkdir,創(chuàng)建新目錄
fsnotify_access,文件被讀
fsnotify_modify,文件被寫
fsnotify_open,文件被打開
fsnotify_close,文件被關(guān)閉
fsnotify_xattr,文件的擴展屬性被修改
fsnotify_change,文件被修改或原數(shù)據(jù)被修改
有一個例外情況,就是 inotify_unmount_inodes,它會在文件系統(tǒng)被 umount 時調(diào)用來通知 umount 事件給 inotify 系統(tǒng) 。
以上提到的通知函數(shù)最后都調(diào)用 inotify_inode_queue_event(inotify_unmount_inodes直接調(diào)用 inotify_dev_queue_event ),該函數(shù)首先判斷對應(yīng)的inode是否被監(jiān)視,這通過查看 inotify_watches 列表是否為空來實現(xiàn),如果發(fā)現(xiàn) inode 沒有被監(jiān)視,什么也不做,立刻返回,反之,遍歷 inotify_watches 列表,看是否當(dāng)前的文件操作事件被某個 watch 監(jiān)視,如果是,調(diào)用 inotify_dev_queue_event,否則,返回 。函數(shù)inotify_dev_queue_event 首先判斷該事件是否是上一個事件的重復(fù),如果是就丟棄該事件并返回,否則,它判斷是否 inotify 實例即 inotify_device 的事件隊列是否溢出,如果溢出,產(chǎn)生一個溢出事件,否則產(chǎn)生一個當(dāng)前的文件操作事件,這些事件通過kernel_event 構(gòu)建,kernel_event 將創(chuàng)建一個 inotify_kernel_event 結(jié)構(gòu),然后把該結(jié)構(gòu)插入到對應(yīng)的 inotify_device 的 events 事件列表,然后喚醒等待在inotify_device 結(jié)構(gòu)中的 wq 指向的等待隊列 。想監(jiān)視文件系統(tǒng)事件的用戶態(tài)進程在inotify 實例(即 inotify_init() 返回的文件描述符)上調(diào)用 read 時但沒有事件時就掛在等待隊列 wq 上 。
四、使用示例
下面是一個使用 inotify 來監(jiān)視文件系統(tǒng)事件的例子:


#include
#include
#include
_syscall0(int, inotify_init)
_syscall3(int, inotify_add_watch, int, fd, const char *, path, __u32, mask)
_syscall2(int, inotify_rm_watch, int, fd, __u32, mask)
char * monitored_files[] = {
"./tmp_file",
"./tmp_dir",
"/mnt/sda3/windows_file"

推薦閱讀