为设备服务的文件系统sysfs--sysfs文件的读写

来源:互联网 发布:鲜活的数据 pdf 编辑:程序博客网 时间:2024/06/15 13:12

4.3     sysfs文件的读写

        sysfs是在内存中存在文件系统,它的文件都只在内存中存在。因此 对文件的读写实际是对内存的读写,不涉及对硬盘的操作
        

4.3.1 读文件的过程的分析

        a
        a

申请内存页函数:

fastcall unsigned long get_zeroed_page(gfp_t gfp_mask){struct page * page;/* * get_zeroed_page() returns a 32-bit address, which cannot represent * a highmem page */BUG_ON((gfp_mask & __GFP_HIGHMEM) != 0);page = alloc_pages(gfp_mask | __GFP_ZERO, 0);if (page)return (unsigned long) page_address(page);return 0;}

分配页面空间函数 alloc_pages:

#ifdef CONFIG_NUMAextern struct page *alloc_pages_current(gfp_t gfp_mask, unsigned order);static inline struct page *alloc_pages(gfp_t gfp_mask, unsigned int order){if (unlikely(order >= MAX_ORDER))return NULL;return alloc_pages_current(gfp_mask, order);}extern struct page *alloc_page_vma(gfp_t gfp_mask,struct vm_area_struct *vma, unsigned long addr);#else#define alloc_pages(gfp_mask, order) \alloc_pages_node(numa_node_id(), gfp_mask, order)#define alloc_page_vma(gfp_mask, vma, addr) alloc_pages(gfp_mask, 0)#endif

mm/Mempolicy.h :

/** * alloc_pages_current - Allocate pages. * *@gfp: *%GFP_USER   user allocation, *      %GFP_KERNEL kernel allocation, *      %GFP_HIGHMEM highmem allocation, *      %GFP_FS     don't call back into a file system. *      %GFP_ATOMIC don't sleep. *@order: Power of two of allocation size in pages. 0 is a single page. * *Allocate a page from the kernel page pool.  When not in *interrupt context and apply the current process NUMA policy. *Returns NULL when no page can be allocated. * *Don't call cpuset_update_task_memory_state() unless *1) it's ok to take cpuset_sem (can WAIT), and *2) allocating for current task (not interrupt). */struct page *alloc_pages_current(gfp_t gfp, unsigned order){struct mempolicy *pol = current->mempolicy;if ((gfp & __GFP_WAIT) && !in_interrupt())cpuset_update_task_memory_state();if (!pol || in_interrupt())pol = &default_policy;if (pol->policy == MPOL_INTERLEAVE)return alloc_page_interleave(gfp, order, interleave_nodes(pol));return __alloc_pages(gfp, order, zonelist_policy(gfp, pol));}EXPORT_SYMBOL(alloc_pages_current);

include/linux/Gfp.h

static inline struct page *alloc_pages_node(int nid, gfp_t gfp_mask,unsigned int order){if (unlikely(order >= MAX_ORDER))return NULL;/* Unknown node is current node */if (nid < 0)nid = numa_node_id();return __alloc_pages(gfp_mask, order,NODE_DATA(nid)->node_zonelists + gfp_zone(gfp_mask));}

a

4.3.2 写文件的过程分析

        普通文件的写函数是sysfs_write_file,它的代码如下:


        a

阅读全文
0 0
原创粉丝点击