+
+static int dev2major(int d)
+{
+ if (d >= 0)
+ return MD_MAJOR;
+ else
+ return get_mdp_major();
+}
+
+static int dev2minor(int d)
+{
+ if (d >= 0)
+ return d;
+ return (-1-d) << MdpMinorShift;
+}
+
+int find_free_devnum(int use_partitions)
+{
+ int devnum;
+ for (devnum = 127; devnum != 128;
+ devnum = devnum ? devnum-1 : (1<<22)-1) {
+ char *dn;
+ int _devnum;
+
+ _devnum = use_partitions ? (-1-devnum) : devnum;
+ if (mddev_busy(_devnum))
+ continue;
+ /* make sure it is new to /dev too, at least as a
+ * non-standard */
+ dn = map_dev(dev2major(_devnum), dev2minor(_devnum), 0);
+ if (dn && ! is_standard(dn, NULL))
+ continue;
+ break;
+ }
+ if (devnum == 128)
+ return NoMdDev;
+ return use_partitions ? (-1-devnum) : devnum;
+}
+
+