转载时请注明出处和作者联系方式 文章出处:http://www.limodev.cn/blog 作者联系方式:李先静 <xianjimli at hotmail dot com>
手 机内置FLASH容量有限,在磁盘空间不足的情况下,应该提醒用户进行磁盘清理。这个处理在哪里做比较好呢?每次写入数据时由调用者检测显然是不合理的, 因为处理的太多了,何况修改SQLITE等第三方程序也是不明智的,那样会给升级版本带来麻烦。比较好的办法是在文件系统中做处理,最近同事修改了 yaffs2支持磁盘满通知功能。做法如下:
在yaffs_fs.c中: #include <net/sock.h> <linux/netlink.h>
#define DISK_FULL_MSG_SIZE 128 #define NETLINK_DISK_FULL 2
static struct sock * yaffs_sock;
在init_yaffs_fs中: if((yaffs_sock = netlink_kernel_create(NETLINK_DISK_FULL,1,255);">NULL,THIS_MODULE)) == NULL) { printk(KERN_INFO"netlink_kernel_create fail./n"); }
在exit_yaffs_fs中: if(yaffs_sock != NULL) { sock_release(yaffs_sock); yaffs_sock = NULL; } 增加两个函数:
void yaffs_notify_space_full(const char* partition,char* type,87);">int totalchunk,87);">int freechunk) { size_t len = 0; char *scratch = NULL; struct sk_buff *skb = NULL;
totalchunk >>= 10; freechunk >>= 10;
len = DISK_FULL_MSG_SIZE; skb = alloc_skb(len,GFP_KERNEL); if (skb) { scratch = skb_put(skb,len); sprintf(scratch,255);">"diskevent: type=%s total=%dKB free=KB partition=", type,totalchunk,freechunk,partition);
NETLINK_CB(skb).dst_group = 1; netlink_broadcast(yaffs_sock,skb,255);">0,GFP_KERNEL); }
return; }
void yaffs_notify_app_if_space_full(yaffs_Device * dev) { if (yaffs_sock) { char* type = NULL; int totalchunk = (dev->endBlock - dev->startBlock + 1) * dev->nChunksPerBlock * dev->nDataBytesPerChunk; int freechunk = yaffs_GetNumberOfFreeChunks(dev) * dev->nDataBytesPerChunk;
if (freechunk < totalchunk / 100) { type = "full"; } else if (freechunk < totalchunk * 5 / "low"; }
if(type != NULL) { yaffs_notify_space_full(dev->name,type,freechunk); } }
return; }
在yaffs_AllocateChunk中: yaffs_notify_app_if_space_full(dev);
NETLINK是Linux提供的一种用于内核与用户空间进程通信的方式,使用简单,传输效率高,hotplug事件也是通过这种方式通知udev的。
用户空间监听磁盘满事件的实现很简单,我提供了一个示例,有兴趣的朋友可以到这里下载。
欢迎到Linux mobile development上交流 (编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|