linux内核源码阅读之facebook硬盘加速flashcache之五
1221static void
1222flashcache_read(struct cache_c *dmc, struct bio *bio)
1223{
1224 int index;
1225 int res;
1226 struct cacheblock *cacheblk;
1227 int queued;
1228
1229 DPRINTK("Got a %s for %llu %u bytes)",
1230 (bio_rw(bio) == READ ? "READ":"READA"),
1231 bio->bi_sector, bio->bi_size);
1232
1233 spin_lock_irq(&dmc->cache_spin_lock);
1234 res = flashcache_lookup(dmc, bio, &index);
1235 /*
1236 * Handle Cache Hit case first.
1237 * We need to handle 2 cases, BUSY and !BUSY. If BUSY, we enqueue the
1238 * bio for later.
1239 */
1240 if (res > 0) {
1241 cacheblk = &dmc->cache[index];
1242 if ((cacheblk->cache_state & VALID) &&
1243 (cacheblk->dbn == bio->bi_sector)) {
1244 flashcache_read_hit(dmc, bio, index);
1245 return;
1246 }
1247 }
1248 /*
1249 * In all cases except for a cache hit (and VALID), test for potential
1250 * invalidations that we need to do.
1251 */
1252 queued = flashcache_inval_blocks(dmc, bio);
1253 if (queued) {
1254 if (unlikely(queued < 0))
1255 flashcache_bio_endio(bio, -EIO);
1256 spin_unlock_irq(&dmc->cache_spin_lock);
1257 return;
1258 }
1259 if (res == -1 || flashcache_uncacheable(dmc)) {
1260 /* No room or non-cacheable */
1261 spin_unlock_irq(&dmc->cache_spin_lock);
1262 DPRINTK("Cache read: Block %llu(%lu):%s",
1263 bio->bi_sector, bio->bi_size, "CACHE MISS & NO ROOM");
1264 if (res == -1)
1265 flashcache_clean_set(dmc, hash_block(dmc, bio->bi_sector));
1266 /* Start uncached IO */
1267 flashcache_start_uncached_io(dmc, bio);
1268 return;
1269 }
1270 /*
1271 * (res == INVALID) Cache Miss
1272 * And we found cache blocks to replace
1273 * Claim the cache blocks before giving up the spinlock
1274 */
1275 if (dmc->cache[index].cache_state & VALID)
1276 dmc->replace++;
1277 else
1278 dmc->cached_blocks++;
1279 dmc->cache[index].cache_state = VALID | DISKREADINPROG;
1280 dmc->cache[index].dbn = bio->bi_sector;
1281 spin_unlock_irq(&dmc->cache_spin_lock);
1282
1283 DPRINTK("Cache read: Block %llu(%lu), index = %d:%s",
1284 bio->bi_sector, bio->bi_size, index, "CACHE MISS & REPLACE");
1285 flashcache_read_miss(dmc, bio, index);
1286}
1119static void
1120flashcache_read_hit(struct cache_c *dmc, struct bio* bio, int index)
1121{
1122 struct cacheblock *cacheblk;
1123 struct pending_job *pjob;
1124
1125 cacheblk = &dmc->cache[index];
1126 if (!(cacheblk->cache_state & BLOCK_IO_INPROG) && (cacheblk->head == NULL)) {
1127 struct kcached_job *job;
1128
1129 cacheblk->cache_state |= CACHEREADINPROG;
1130 dmc->read_hits++;
1131 spin_unlock_irq(&dmc->cache_spin_lock);
1132 DPRINTK("Cache read: Block %llu(%lu), index = %d:%s",
1133 bio->bi_sector, bio->bi_size, index, "CACHE HIT");
1134 job = new_kcached_job(dmc, bio, index);
1135 if (unlikely(sysctl_flashcache_error_inject & READ_HIT_JOB_ALLOC_FAIL)) {
1136 if (job)
1137 flashcache_free_cache_job(job);
1138 job = NULL;
1139 sysctl_flashcache_error_inject &= ~READ_HIT_JOB_ALLOC_FAIL;
1140 }
1141 if (unlikely(job == NULL)) {
1142 /*
1143 * We have a read hit, and can't allocate a job.
1144 * Since we dropped the spinlock, we have to drain any
1145 * pending jobs.
1146 */
1147 DMERR("flashcache: Read (hit) failed ! Can't allocate memory for cache IO, block %lu",
1148 cacheblk->dbn);
1149 flashcache_bio_endio(bio, -EIO);
1150 spin_lock_irq(&dmc->cache_spin_lock);
1151 flashcache_free_pending_jobs(dmc, cacheblk, -EIO);
1152 cacheblk->cache_state &= ~(BLOCK_IO_INPROG);
1153 spin_unlock_irq(&dmc->cache_spin_lock);
1154 } else {
1155 job->action = READCACHE; /* Fetch data from cache */
1156 atomic_inc(&dmc->nr_jobs);
1157 dmc->ssd_reads++;
1158 dm_io_async_bvec(1, &job->cache, READ,
1159 bio->bi_io_vec + bio->bi_idx,
1160 flashcache_io_callback, job);
1161 flashcache_unplug_device(dmc->cache_dev->bdev);
1162 }
1163 } else {
1164 pjob = flashcache_alloc_pending_job(dmc);
1165 if (unlikely(sysctl_flashcache_error_inject & READ_HIT_PENDING_JOB_ALLOC_FAIL)) {
1166 if (pjob) {
1167 flashcache_free_pending_job(pjob);
1168 pjob = NULL;
1169 }
1170 sysctl_flashcache_error_inject &= ~READ_HIT_PENDING_JOB_ALLOC_FAIL;
1171 }
1172 if (pjob == NULL)
1173 flashcache_bio_endio(bio, -EIO);
1174 else
1175 flashcache_enq_pending(dmc, bio, index, READCACHE, pjob);
1176 spin_unlock_irq(&dmc->cache_spin_lock);
1177 }
1178}
首先获取这个cache块管理结构,第1126行判断cache块不忙的情况,进入1129行设置状态为从cache读,第1134行创建一个kcached_job,在1141行申请kcached_job失败时就对bio返回失败。申请成功到1155行将kcached_job设置为READCACHE,再调用dm_io_async_bvec下发请求,当请求回来时就会调用这里设置的回调函数flashcache_io_callback。再继续看读SSD返回是怎么处理的?我们只看该函数是对READCACHE的处理:
151 case READCACHE:
152 DPRINTK("flashcache_io_callback: READCACHE %d",
153 index);
154 spin_lock_irqsave(&dmc->cache_spin_lock, flags);
155 if (unlikely(sysctl_flashcache_error_inject & READCACHE_ERROR)) {
156 job->error = error = -EIO;
157 sysctl_flashcache_error_inject &= ~READCACHE_ERROR;
158 }
159 VERIFY(cacheblk->cache_state & CACHEREADINPROG);
160 spin_unlock_irqrestore(&dmc->cache_spin_lock, flags);
161 if (unlikely(error))
162 dmc->ssd_read_errors++;
163#ifdef FLASHCACHE_DO_CHECKSUMS
164 if (likely(error == 0)) {
165 if (flashcache_validate_checksum(job)) {
166 DMERR("flashcache_io_callback: Checksum mismatch at disk offset %lu",
167 job->disk.sector);
168 error = -EIO;
169 }
170 }
171#endif
172 flashcache_bio_endio(bio, error);
173 break;
实际上真正有意义的就是第172行,将请求done回去了。这样我们就完成的一次读命中的处理。
1180static void
1181flashcache_read_miss(struct cache_c *dmc, struct bio* bio,
1182 int index)
1183{
1184 struct kcached_job *job;
1185 struct cacheblock *cacheblk = &dmc->cache[index];
1186
1187 job = new_kcached_job(dmc, bio, index);
1188 if (unlikely(sysctl_flashcache_error_inject & READ_MISS_JOB_ALLOC_FAIL)) {
1189 if (job)
1190 flashcache_free_cache_job(job);
1191 job = NULL;
1192 sysctl_flashcache_error_inject &= ~READ_MISS_JOB_ALLOC_FAIL;
1193 }
1194 if (unlikely(job == NULL)) {
1195 /*
1196 * We have a read miss, and can't allocate a job.
1197 * Since we dropped the spinlock, we have to drain any
1198 * pending jobs.
1199 */
1200 DMERR("flashcache: Read (miss) failed ! Can't allocate memory for cache IO, block %lu",
1201 cacheblk->dbn);
1202 flashcache_bio_endio(bio, -EIO);
1203 spin_lock_irq(&dmc->cache_spin_lock);
1204 dmc->cached_blocks--;
1205 cacheblk->cache_state &= ~VALID;
1206 cacheblk->cache_state |= INVALID;
1207 flashcache_free_pending_jobs(dmc, cacheblk, -EIO);
1208 cacheblk->cache_state &= ~(BLOCK_IO_INPROG);
1209 spin_unlock_irq(&dmc->cache_spin_lock);
1210 } else {
1211 job->action = READDISK; /* Fetch data from the source device */
1212 atomic_inc(&dmc->nr_jobs);
1213 dmc->disk_reads++;
1214 dm_io_async_bvec(1, &job->disk, READ,
1215 bio->bi_io_vec + bio->bi_idx,
1216 flashcache_io_callback, job);
1217 flashcache_clean_set(dmc, index / dmc->assoc);
1218 }
1219}
113void
114flashcache_io_callback(unsigned long error, void *context)
115{
116 struct kcached_job *job = (struct kcached_job *) context;
117 struct cache_c *dmc = job->dmc;
118 struct bio *bio;
119 unsigned long flags;
120 int index = job->index;
121 struct cacheblock *cacheblk = &dmc->cache[index];
122
123 VERIFY(index != -1);
124 bio = job->bio;
125 VERIFY(bio != NULL);
126 if (error)
127 DMERR("flashcache_io_callback: io error %ld block %lu action %d",
128 error, job->disk.sector, job->action);
129 job->error = error;
130 switch (job->action) {
131 case READDISK:
132 DPRINTK("flashcache_io_callback: READDISK %d",
133 index);
134 spin_lock_irqsave(&dmc->cache_spin_lock, flags);
135 if (unlikely(sysctl_flashcache_error_inject & READDISK_ERROR)) {
136 job->error = error = -EIO;
137 sysctl_flashcache_error_inject &= ~READDISK_ERROR;
138 }
139 VERIFY(cacheblk->cache_state & DISKREADINPROG);
140 spin_unlock_irqrestore(&dmc->cache_spin_lock, flags);
141 if (likely(error == 0)) {
142 /* Kick off the write to the cache */
143 job->action = READFILL;
144 flashcache_enqueue_readfill(dmc, job);
145 return;
146 } else {
147 dmc->disk_read_errors++;
148 flashcache_bio_endio(bio, error);
149 }
150 break; 174 case READFILL:
175 DPRINTK("flashcache_io_callback: READFILL %d",
176 index);
177 spin_lock_irqsave(&dmc->cache_spin_lock, flags);
178 if (unlikely(sysctl_flashcache_error_inject & READFILL_ERROR)) {
179 job->error = error = -EIO;
180 sysctl_flashcache_error_inject &= ~READFILL_ERROR;
181 }
182 if (unlikely(error))
183 dmc->ssd_write_errors++;
184 VERIFY(cacheblk->cache_state & DISKREADINPROG);
185 spin_unlock_irqrestore(&dmc->cache_spin_lock, flags);
186 flashcache_bio_endio(bio, error);
187 break;
linux内核源码阅读之facebook硬盘加速flashcache之五的更多相关文章
- linux内核源码阅读之facebook硬盘加速flashcache之八
前面我们的分析中重点关注正常的数据流程,这一小节关注如果有异常,那么流程是怎么走完的呢? 1)创建新任务时kcached_job申请不到 2)读写命中时cache块为忙 3)系统关机时处理,系统开机时 ...
- linux内核源码阅读之facebook硬盘加速flashcache之三
上一节讲到在刷缓存的时候会调用new_kcahed_job创建kcached_job,由此我们也可以看到cache数据块与磁盘数据的对应关系.上一篇:http://blog.csdn.net/lium ...
- linux内核源码阅读之facebook硬盘加速flashcache之四
这一小节介绍一下flashcache读写入口和读写的基础实现. 首先,不管是模块还是程序,必须先找到入口,用户态代码会经常去先看main函数,内核看module_init,同样看IO流时候也要找到入口 ...
- linux内核源码阅读之facebook硬盘加速flashcache之二
flashcache数据结构都在flashcache.h文件中,但在看数据结构之前,需要先过一遍flashcache是什么,要完成哪些功能?如果是自己设计这样一个系统的话,大概要怎么设计. 前面讲过, ...
- linux内核源码阅读之facebook硬盘加速flashcache之六
其实到目前为止,如果对读流程已经能轻松地看懂了,那么写流程不需要太多脑细胞.我觉得再写下去没有太大的必要了,后面想想为了保持flashcache完整性,还是写出来吧.接着到写流程: 1530stati ...
- linux内核源码阅读之facebook硬盘加速利器flashcache
从来没有写过源码阅读,这种感觉越来越强烈,虽然劣于文笔,但还是下定决心认真写一回. 源代码下载请参见上一篇flashcache之我见 http://blog.csdn.net/liumangxiong ...
- ubuntu下linux内核源码阅读工具和调试方法总结
http://blog.chinaunix.net/uid-20940095-id-66148.html 一 linux内核源码阅读工具 windows下当然首选source insight, 但是l ...
- Linux内核源码阅读记录一之分析存储在不同段中的函数调用过程
在写驱动的过程中,对于入口函数与出口函数我们会用一句话来修饰他们:module_init与module_exit,那会什么经过修饰后,内核就能狗调用我们编写的入口函数与出口函数呢?下面就来分析内核调用 ...
- Linux内核源码分析
Linux源码下载: https://www.kernel.org/ https://git.kernel.org/ Linux内核源码阅读以及工具(转): https://blog.csdn.net ...
随机推荐
- Scrapy入门程序点评
1,引言 在<Scrapy的架构初探>一文,我基于爬虫开发的经验对Scrapy官网文章作了点评和解读,事件驱动的异步处理架构.极强的模块化等是个绝好的框架,接着我细读了官网的<Scr ...
- 使用Python把Gtest XML测试结果转换为HTML格式
在最近的测试中,使用gtest测试框架对c语言代码进行测试,结果以XML文件来保存,但是测试结果的查阅和分析非常不方便.便想着把xml的结果直接转为HTML文件,方便和Jenkins系统对接显示.因现 ...
- 让QT对话框显示中文
http://blog.sina.com.cn/s/blog_8254427901011fxv.html (1)从QT安装目录下面将文件“qt_zh_CN.qm”复制一份到你的项目目录下. 例如,我是 ...
- 对简单的正则表达式的理解V1.0
[^<]* 我得理解也是基本来自官方的解释 [] 我理解是它其中的内容,是指内容哦, 内容是可以选择的 字符 集合 ,比如说 @"<div style="color: ...
- sshd服务---暴力破解应对策略
sshd服务暴力破解步骤 sshd暴力破解方法 防止暴力破解调优 1. 变更默认端口 2. 变更root用户 3. 日志监控-->防止暴力破解(fail2ban应用) fail2ban详解 在初 ...
- Android技术宅:自制USB OTG数据线
作为一名Android技术宅,USB OTG是你必须了解的,所谓USB OTG就是你可以利用手机或平板上用来充电.与电脑传输数据的micro USB接口来连接其他USB外设,如游戏手柄.鼠标.键盘.U ...
- 用Python写的一个多线程机器人聊天程序
本人是从事php开发的, 近来想通过php实现即时通讯(兼容windows).后来发现实现起来特别麻烦, 就想到python.听说这家伙在什么地方都能发挥作用.所以想用python来做通讯模块...所 ...
- (Pre sell) ZOPO ZP998 (C2 II) 5.5 inch smart phone True Octa Core MTK6592 1920X1080 FHD screen 401 ppi 2GB/32GB 14.0Mp camera-in Mobile Phones from Electronics on Aliexpress.com
(Pre sell) ZOPO ZP998 (C2 II) 5.5 inch smart phone True Octa Core MTK6592 1920X1080 FHD screen 401 p ...
- MySQL- 锁机制及MyISAM表锁
锁是计算机协调多个进程或线程并发访问某一资源的机制.在数据库中,除传统的计算资源(如CPU.RAM.I/O等)的争用以外,数据也是一种供许 多用户 共享的资源.如何保证数据并发访问的一致性.有效性是所 ...
- Redmine email配置
很简单,先安装sendmail apt-get install sendmail 然后在redmine目录的配置文件conf/configuration.yml中取消几行注释: # ==== Send ...