Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1758370AbcCCWMJ (ORCPT ); Thu, 3 Mar 2016 17:12:09 -0500 Received: from mail-wm0-f54.google.com ([74.125.82.54]:36722 "EHLO mail-wm0-f54.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752989AbcCCWMH (ORCPT ); Thu, 3 Mar 2016 17:12:07 -0500 MIME-Version: 1.0 In-Reply-To: <20160303200009.GA25327@kroah.com> References: <20160303012530.GA23521@kroah.com> <20160303171846.GC20826@kroah.com> <20160303200009.GA25327@kroah.com> Date: Thu, 3 Mar 2016 15:12:04 -0700 X-Google-Sender-Auth: mfKBOeQlHaaFai27dm2YvRmr_nQ Message-ID: Subject: Re: allocate an official device major number for virtio device? From: Jeff Sharkey To: Greg Kroah-Hartman Cc: Jin Qian , "Ning, Yu" , "linux-kernel@vger.kernel.org" , David Turner , "pprabhu@google.com" Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 3816 Lines: 108 Adding a bit more context, in Android when we receive a uevent about a new disk we use a separate userspace utility (sgdisk) to inspect the partitions it contains, and we construct our own mknod() for the partitions that we're interested in. (We're doing this manually because Android doesn't have devtmpfs.) Disks with GPT can have up to 128 partitions, but when mknod()'ing the partition devices I need to carefully ignore any partitions beyond the maximum number of partitions supported by the underlying device system, otherwise I risk jumping into the next disk. The maximum number of partitions appears to be well-known for certain major device numbers (Documentation/devices.txt says 15 for SCSI, or reading perdev_minors for MMC). Is there a good way to determine this upper limit for disks surfaced through virtio_blk? Currently I've been using the major device number as a heuristic, which is what kicked off this entire discussion. j On Thu, Mar 3, 2016 at 1:00 PM, Greg Kroah-Hartman wrote: > On Thu, Mar 03, 2016 at 11:32:00AM -0800, Jin Qian wrote: >> On Thu, Mar 3, 2016 at 9:18 AM, Greg Kroah-Hartman >> wrote: >> > A: No. >> > Q: Should I include quotations after my reply? >> > >> > http://daringfireball.net/2007/07/on_top >> > >> > On Thu, Mar 03, 2016 at 03:52:20AM +0000, Ning, Yu wrote: >> >> Well, virtio_blk does use dynamic major number allocation, but the >> >> allocated block major just happens to fall in the "experimental" range >> >> (240-254)... >> > >> > That all depends on what else is registered in the system at the moment. >> > >> >> In more detail: >> >> >> >> virtio_blk calls register_blkdev() with major = 0 in init() (drivers/block/virtio_blk.c:872): >> >> >> >> major = register_blkdev(0, "virtblk"); >> >> >> >> This line has been there since day one. And register_blkdev() implements dynamic major allocation pretty straightforwardly: >> >> >> >> /* temporary */ >> >> if (major == 0) { >> >> for (index = ARRAY_SIZE(major_names)-1; index > 0; index--) { >> >> if (major_names[index] == NULL) >> >> break; >> >> } >> >> >> >> So it goes from index = 254 to 1 and picks the first unused. >> >> Apparently, there's a good chance that the allocated major is between >> >> 240-254 (although lower numbers are also possible, theoretically). >> >> Indeed, we always get 253 for virtio_blk with the x86_64 Android >> >> emulator kernel. >> >> >> >> But "dynamic" means we can't rely on checking major == 253 to detect >> >> virtio_blk. >> > >> > Nor should you, why would you care? >> > >> >> That's why we are doing a fnmatch() using pattern >> >> /sys/devices/*/block/vd* instead. Is that the recommended approach? >> > >> > Yes, or just look at the device node that is already created in /dev/ >> > for you automatially by devtmpfs. Doesn't that work as expected today? >> > >> > I still don't understand the issue you are having here at all, sorry. >> >> Maybe Jeff can comment in more detail about the issue and which method >> is preferred? >> >> Just to summarize, we have a few ways to detect the device >> >> 1. match /sys/devices/... > > /sys/dev/block/ you mean. > >> 2. match /dev/... > > Yes. > >> 3. use uevent rule to detect virtio device creation > > Yes. > >> 4. check a fixed major# > > Don't do that, it's not going to work as you don't have an assigned > major number. > > But again, what do you mean by "detect the device"? What do you want to > do here, just open/mount it? Why don't the existing tools just handle > this automatically for you? > > What do you want to do when you "detect the device"? What needs this? > > thanks, > > greg k-h -- Jeff Sharkey jsharkey@android.com