Why doesn't device_create return error when a file already exists?

630 views Asked by At

I am writing a PCI driver with a character device for an interface (Linux 4.9.13). Here's the scenario that bothers me:

  1. Run touch /dev/foo0 which creates a normal file in the /dev directory.
  2. Load the driver module. Here's a pseudo code representing what happens there (pretty standard character device registration):

    // When the module is initialized:
    alloc_chrdev_region(&dev, 0, 256, "foo");
    class = class_create(THIS_MODULE, "foo");
    
    // Later, when a suitable PCI device is connected the probe function 
    // calls the following functions:
    cdev_init(dev->md_cdev, &fops);
    dev->md_devnum = MKDEV(major, 0 + index);
    res = cdev_add(dev->md_cdev, dev->md_devnum, 1);
    dev->md_sysfsdev = device_create(class, 0, dev->md_devnum, 0, "foo%d", index);
    

    Details:

    • index is just another free index

What seems weird to me is nothing raises an error that there is already a /dev/foo0 file which is not a character device. I do check all the errors (I think so) but I omitted related code for the sake of conciseness. Everything works as expected if I do not run touch /dev/foo0. Otherwise, I can neither read nor write to the device.

Why is it so? Shouldn't device_create return an error or at least create /dev/foo1 instead?

0

There are 0 answers