Scsi - Index of

Scsi - Index of Scsi - Index of

07.06.2013 Views

一下子进程,稍候马上去看父进程. 301 行,一个for 语句死循环,尽管外面的世界很精彩,但是咱们去看看for 里面的世界也不妨. 303 行,down_interruptible()函数,事实上302 行的注释已经告诉咱们,thread 将进入睡眠了...,也许 她累了.down_interruptible 的参数是&us->sema,不陌生吧,我们之前讲信号量讲互斥锁的时候就已经 提过了us->sema.所以这里很简单,就是想获得这把锁,但是别忘了,我们当初就介绍过,这把锁一开始就被 初始化为0 了,也就是说它属于那种指腹为婚的情形,一到这个世界来就告诉别人自己已经是名花有主了. 因 114 此,这里只能进入睡眠,等待一个up()函数去释放锁.谁会调用up()函数呢?暂时先不管它,我们先关注一下 父进程,毕竟我们自己进入了睡眠,而之前我们把父进程唤醒了. 彼岸花的传说(三) 遥想公瑾当年,小乔出嫁了,雄姿英发. 羽扇纶巾,谈笑间,樯橹灰飞烟灭. 故国神游,多情应笑我,早生华发, 人生如梦,一樽还酹江月. 的确,人生如梦,设计Linux 代码的人想必非常认可这种观点,因为他们已然把这种思想融入到了代码中去, 所以在代码里我们常看到睡眠,唤醒,睡眠,唤醒...而作为当年的大学生,Linus 想必也很认同大学生活的现 状,即大学生活就是睡觉,只是有的人两个人睡,有的人一个人睡. 前面已经说了,父进程在函数usb_stor_acquire_resources()里边,810 行,wait_for_completion(&(us->notify)),进入睡眠,而刚才咱们在子进程里已经看 到,complete(&(us->notify))被调用,于是父进程被唤醒,回到usb_stor_acquire_resources()函数中, 往下走,812 行,无它,唯返回耳.返回了0.于是咱们终于回到了storage_probe()函数中来. 1001 行,scsi_add_host()函数被执行,之前申请的us->host 被作为参数传递给她,同时,intf->dev 也 被传递给她,这个冬冬是被用来注册sysfs 的.在scsi_host_alloc 之后,必须执行scsi_add_host(),这 样,scsi 核心层才能够知道有这么一个host 存在.scsi_add_host()成功则返回0,否则返回出错代码.如果 一切顺利,咱们将走到1009 行,别急,先把代码贴出来,这就是storage_probe()函数的最后一小段了: 1007 1008 /* Start up the thread for delayed SCSI-device scanning */ 1009 result = kernel_thread(usb_stor_scan_thread, us, CLONE_VM); 1010 if (result < 0) { 1011 printk(KERN_WARNING USB_STORAGE 1012 "Unable to start the device-scanning thread\n"); 1013 scsi_remove_host(us->host); 1014 goto BadDevice; 1015 } 1016 1017 return 0; 1018 1019 /* We come here if there are any problems */ 1020 BadDevice: 1021 US_DEBUGP("storage_probe() failed\n"); 1022 usb_stor_release_resources(us);

1023 dissociate_dev(us); 1024 return result; 1025 } 1026 115 又一次见到了kernel_thread,不需要更多解释,这里自然还是创建一个内核守护进程,只不过这次是 usb_stor_scan_thread,而上次是usb_stor_control_thread.usb_stor_scan_thread()函数也是定 义于drivers/usb/storage/usb.c 中: 886 /* Thread to carry out delayed SCSI-device scanning */ 887 static int usb_stor_scan_thread(void * __us) 888 { 889 struct us_data *us = (struct us_data *)__us; 890 891 /* 892 * This thread doesn't need any user-level access, 893 * so get rid of all our resources. 894 */ 895 lock_kernel(); 896 daemonize("usb-stor-scan"); 897 unlock_kernel(); 898 899 printk(KERN_DEBUG 900 "usb-storage: device found at %d\n", us->pusb_dev->devnum); 901 902 /* Wait for the timeout to expire or for a disconnect */ 903 if (delay_use > 0) { 904 printk(KERN_DEBUG "usb-storage: waiting for device " 905 "to settle before scanning\n"); 906 retry: 907 wait_event_interruptible_timeout(us->scsi_scan_wait, 908 test_bit(US_FLIDX_DISCONNECTING, &us->flags), 909 delay_use * HZ); 910 if (current->flags & PF_FREEZE) { 911 refrigerator(PF_FREEZE); 912 goto retry; 913 } 914 } 915 916 /* If the device is still connected, perform the scanning */ 917 if (!test_bit(US_FLIDX_DISCONNECTING, &us->flags)) { 918 scsi_scan_host(us->host); 919 printk(KERN_DEBUG "usb-storage: device scan complete\n"); 920 } 921

1023 dissociate_dev(us);<br />

1024 return result;<br />

1025 }<br />

1026<br />

115<br />

又一次见到了kernel_thread,不需要更多解释,这里自然还是创建一个内核守护进程,只不过这次是<br />

usb_stor_scan_thread,而上次是usb_stor_control_thread.usb_stor_scan_thread()函数也是定<br />

义于drivers/usb/storage/usb.c 中:<br />

886 /* Thread to carry out delayed SCSI-device scanning */<br />

887 static int usb_stor_scan_thread(void * __us)<br />

888 {<br />

889 struct us_data *us = (struct us_data *)__us;<br />

890<br />

891 /*<br />

892 * This thread doesn't need any user-level access,<br />

893 * so get rid <strong>of</strong> all our resources.<br />

894 */<br />

895 lock_kernel();<br />

896 daemonize("usb-stor-scan");<br />

897 unlock_kernel();<br />

898<br />

899 printk(KERN_DEBUG<br />

900 "usb-storage: device found at %d\n", us->pusb_dev->devnum);<br />

901<br />

902 /* Wait for the timeout to expire or for a disconnect */<br />

903 if (delay_use > 0) {<br />

904 printk(KERN_DEBUG "usb-storage: waiting for device "<br />

905 "to settle before scanning\n");<br />

906 retry:<br />

907 wait_event_interruptible_timeout(us->scsi_scan_wait,<br />

908 test_bit(US_FLIDX_DISCONNECTING, &us->flags),<br />

909 delay_use * HZ);<br />

910 if (current->flags & PF_FREEZE) {<br />

911 refrigerator(PF_FREEZE);<br />

912 goto retry;<br />

913 }<br />

914 }<br />

915<br />

916 /* If the device is still connected, perform the scanning */<br />

917 if (!test_bit(US_FLIDX_DISCONNECTING, &us->flags)) {<br />

918 scsi_scan_host(us->host);<br />

919 printk(KERN_DEBUG "usb-storage: device scan complete\n");<br />

920 }<br />

921

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!