Linux那些事兒之我是SCSI硬碟(4)三座大山(一)

來源:互聯網
上載者:User

好不容易結束了sd_spinup_disk(),馬上我們就遇到了三座大山.它們是sd_read_capacity(),sd_read_write_protect_flag(),sd_read_cache_type(),要繼續往下看,我們不得不先推翻這三座大山.舊的三座大山已經在毛主席的英明領導下成功推翻了,但是今天我們的人民卻身處新三座大山的壓迫之下,眼前這三個函數堪比臭名昭著的房改醫改教改.要知道整個sd.c這個檔案也不過是1900行,可是光這三個函數就佔了360行,你不服不行啊!

第一座大山,sd_read_capacity.   1130 /*   1131 * read disk capacity   1132 */   1133 static void   1134 sd_read_capacity(struct scsi_disk *sdkp, unsigned char *buffer)   1135 {   1136         unsigned char cmd[16];   1137         int the_result, retries;   1138         int sector_size = 0;   1139         int longrc = 0;   1140         struct scsi_sense_hdr sshdr;   1141         int sense_valid = 0;   1142         struct scsi_device *sdp = sdkp->device;   1143   1144 repeat:   1145         retries = 3;   1146         do {   1147                 if (longrc) {   1148                         memset((void *) cmd, 0, 16);   1149                         cmd[0] = SERVICE_ACTION_IN;   1150                         cmd[1] = SAI_READ_CAPACITY_16;   1151                         cmd[13] = 12;   1152                         memset((void *) buffer, 0, 12);   1153                 } else {   1154                         cmd[0] = READ_CAPACITY;   1155                         memset((void *) &cmd[1], 0, 9);   1156                         memset((void *) buffer, 0, 8);   1157                 }   1158   1159                 the_result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE,   1160                                               buffer, longrc ? 12 : 8, &sshdr,   1161                                               SD_TIMEOUT, SD_MAX_RETRIES);   1162   1163                 if (media_not_present(sdkp, &sshdr))   1164                         return;   1165   1166                 if (the_result)   1167                         sense_valid = scsi_sense_valid(&sshdr);   1168                 retries--;   1169   1170         } while (the_result && retries);   1171   1172         if (the_result && !longrc) {   1173                 sd_printk(KERN_NOTICE, sdkp, "READ CAPACITY failed/n");   1174                 sd_print_result(sdkp, the_result);   1175                 if (driver_byte(the_result) & DRIVER_SENSE)   1176                         sd_print_sense_hdr(sdkp, &sshdr);   1177                 else   1178                         sd_printk(KERN_NOTICE, sdkp, "Sense not available./n");   1179   1180                 /* Set dirty bit for removable devices if not ready -   1181                  * sometimes drives will not report this properly. */   1182                 if (sdp->removable &&   1183                     sense_valid && sshdr.sense_key == NOT_READY)   1184                         sdp->changed = 1;   1185   1186                 /* Either no media are present but the drive didn't tell us,   1187                    or they are present but the read capacity command fails */   1188                 /* sdkp->media_present = 0; -- not always correct */   1189                 sdkp->capacity = 0; /* unknown mapped to zero - as usual */   1190   1191                 return;   1192         } else if (the_result && longrc) {   1193                 /* READ CAPACITY(16) has been failed */   1194                 sd_printk(KERN_NOTICE, sdkp, "READ CAPACITY(16) failed/n");   1195                 sd_print_result(sdkp, the_result);   1196                 sd_printk(KERN_NOTICE, sdkp, "Use 0xffffffff as device size/n");   1197   1198                 sdkp->capacity = 1 + (sector_t) 0xffffffff;   1199                 goto got_data;   1200         }   1201   1202         if (!longrc) {   1203                 sector_size = (buffer[4] << 24) |   1204                         (buffer[5] << 16) | (buffer[6] << 8) | buffer[7];   1205                 if (buffer[0] == 0xff && buffer[1] == 0xff &&   1206                     buffer[2] == 0xff && buffer[3] == 0xff) {   1207                         if(sizeof(sdkp->capacity) > 4) {   1208                                 sd_printk(KERN_NOTICE, sdkp, "Very big device. "   1209                                           "Trying to use READ CAPACITY(16)./n");   1210                                 longrc = 1;   1211                                 goto repeat;   1212                         }   1213                         sd_printk(KERN_ERR, sdkp, "Too big for this kernel. Use "   1214                                   "a kernel compiled with support for large "   1215                                   "block devices./n");   1216                         sdkp->capacity = 0;   1217                         goto got_data;   1218                 }   1219                 sdkp->capacity = 1 + (((sector_t)buffer[0] << 24) |   1220                         (buffer[1] << 16) |   1221                         (buffer[2] << 8) |   1222                         buffer[3]);   1223         } else {   1224                 sdkp->capacity = 1 + (((u64)buffer[0] << 56) |   1225                         ((u64)buffer[1] << 48) |   1226                         ((u64)buffer[2] << 40) |   1227                         ((u64)buffer[3] << 32) |   1228                         ((sector_t)buffer[4] << 24) |   1229                         ((sector_t)buffer[5] << 16) |   1230                         ((sector_t)buffer[6] << 8) |   1231                         (sector_t)buffer[7]);   1232   1233                 sector_size = (buffer[8] << 24) |   1234                         (buffer[9] << 16) | (buffer[10] << 8) | buffer[11];   1235         }   1236   1237         /* Some devices return the total number of sectors, not the   1238          * highest sector number. Make the necessary adjustment. */   1239         if (sdp->fix_capacity) {   1240                 --sdkp->capacity;   1241   1242         /* Some devices have version which report the correct sizes   1243          * and others which do not. We guess size according to a heuristic   1244          * and err on the side of lowering the capacity. */   1245         } else {   1246                 if (sdp->guess_capacity)   1247                         if (sdkp->capacity & 0x01) /* odd sizes are odd */   1248                                 --sdkp->capacity;   1249         }   1250   1251 got_data:   1252         if (sector_size == 0) {   1253                 sector_size = 512;   1254                 sd_printk(KERN_NOTICE, sdkp, "Sector size 0 reported, "   1255                           "assuming 512./n");   1256         }   1257   1258         if (sector_size != 512 &&   1259             sector_size != 1024 &&   1260             sector_size != 2048 &&   1261             sector_size != 4096 &&   1262             sector_size != 256) {   1263                 sd_printk(KERN_NOTICE, sdkp, "Unsupported sector size %d./n",   1264                           sector_size);   1265                 /*   1266                  * The user might want to re-format the drive with   1267                  * a supported sectorsize. Once this happens, it   1268                  * would be relatively trivial to set the thing up.   1269                  * For this reason, we leave the thing in the table.   1270                  */   1271                 sdkp->capacity = 0;   1272                 /*   1273                  * set a bogus sector size so the normal read/write   1274                  * logic in the block layer will eventually refuse any   1275                  * request on this device without tripping over power   1276                  * of two sector size assumptions   1277                  */   1278                 sector_size = 512;   1279         }   1280         {   1281                /*   1282                  * The msdos fs needs to know the hardware sector size   1283                  * So I have created this table. See ll_rw_blk.c   1284                  * Jacques Gelinas (Jacques@solucorp.qc.ca)   1285                  */   1286                 int hard_sector = sector_size;   1287                 sector_t sz = (sdkp->capacity/2) * (hard_sector/256);   1288                 request_queue_t *queue = sdp->request_queue;   1289                 sector_t mb = sz;  1290   1291                 blk_queue_hardsect_size(queue, hard_sector);   1292                 /* avoid 64-bit division on 32-bit platforms */   1293                 sector_div(sz, 625);   1294                 mb -= sz - 974;   1295                 sector_div(mb, 1950);   1296   1297                 sd_printk(KERN_NOTICE, sdkp,   1298                           "%llu %d-byte hardware sectors (%llu MB)/n",   1299                           (unsigned long long)sdkp->capacity,   1300                           hard_sector, (unsigned long long)mb);   1301         }   1302   1303         /* Rescale capacity to 512-byte units */   1304         if (sector_size == 4096)   1305                 sdkp->capacity <<= 3;   1306         else if (sector_size == 2048)   1307                 sdkp->capacity <<= 2;   1308         else if (sector_size == 1024)   1309                 sdkp->capacity <<= 1;   1310         else if (sector_size == 256)   1311                 sdkp->capacity >>= 1;   1312   1313         sdkp->device->sector_size = sector_size;   1314 }洋洋洒洒200餘行.簡而言之,這個函數用一句話來表達就是知道這個磁碟的容量,或者專業一點說,發送READ CAPACITY命令.而熟悉SCSI命令集的兄弟們應該知道,很多SCSI命令都有至少兩種版本,不同版本的命令格式會不一樣,當然返回的資訊量也不盡相同,比如READ CAPACITY命令就有10個位元組的和16個位元組的兩個版本.在SBC-2的5.10節和5.11節分別介紹的是READ CAPACITY(10) command和READ CAPACITY(16) command.後者比前者多一個保護資訊.但是在我們讀之前我們並不知道該用哪個命令,所以這裡的基本思路就是先用短命令,如果失敗了就試一下長命令,這就是1211行goto repeat的目的.在goto repeat之前1210行設定了longrc為1.我們這裡先給出來自SBC-2中對READ CAPACITY命令的格式定義:我們可以用一個執行個體來描述這個命令,sg_readcap可以手工發送READ CAPACITY命令.下面是針對我的一個號稱128M的隨身碟發送這個命令的結果.[root@localhost ~]# sg_readcap /dev/sdcRead Capacity results:   Last logical block address=257535 (0x3edff), Number of blocks=257536   Logical block length=512 bytesHence:   Device size: 131858432 bytes, 125.8 MiB, 0.13 GB與此同時,我們結合代碼來看,這個函數實際上比較麻煩的地方在於對buffer數組的判斷.實際上buffer數組裝載了READ CAPACITY命令的返回資訊.而我們從1203行開始判斷,首先我們知道這個buffer是我們在sd_revalidate_disk()中申請的.其大小為SD_BUF_SIZE,即512個位元組.那麼這個buffer的資料究竟是什麼模樣呢?SBC-2中Table-35對READ CAPACITY(10)的返回資料給出了

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.