fdisk - Use heads = 255 on file images
[dragonfly.git] / sys / vfs / devfs / devfs_helper.c
blob8f3c0e28205b36279ba3e7be471e85c9c550e28d
1 /*
2 * Copyright (c) 2009 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Alex Hornung <ahornung@gmail.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/types.h>
38 #include <sys/param.h>
39 #include <machine/limits.h>
40 #include <sys/devfs.h>
42 MALLOC_DECLARE(M_DEVFS);
45 * DEVFS clone bitmap functions
47 void
48 devfs_clone_bitmap_init(struct devfs_bitmap *bitmap)
50 bitmap->chunks = DEVFS_BITMAP_INITIAL_SIZE;
51 bitmap->bitmap = kmalloc(bitmap->chunks * sizeof(u_long),
52 M_DEVFS, M_WAITOK);
53 memset(bitmap->bitmap, -1, bitmap->chunks * sizeof(u_long));
57 void
58 devfs_clone_bitmap_uninit(struct devfs_bitmap *bitmap)
60 kfree(bitmap->bitmap, M_DEVFS);
64 void
65 devfs_clone_bitmap_resize(struct devfs_bitmap *bitmap, int newchunks)
67 int oldchunks = bitmap->chunks;
69 bitmap->chunks = newchunks + 2;
70 bitmap->bitmap = krealloc(bitmap->bitmap,
71 sizeof(u_long) * bitmap->chunks,
72 M_DEVFS, M_WAITOK);
73 memset(bitmap->bitmap + oldchunks, -1,
74 sizeof(u_long) * (bitmap->chunks - oldchunks));
78 int
79 devfs_clone_bitmap_fff(struct devfs_bitmap *bitmap)
81 u_long curbitmap;
82 int bit, i;
83 int chunks;
85 chunks = bitmap->chunks;
87 for (i = 0; i < chunks + 1; i++) {
88 if (i == chunks)
89 devfs_clone_bitmap_resize(bitmap, i);
90 curbitmap = bitmap->bitmap[i];
92 if (curbitmap) {
93 curbitmap &= (~curbitmap)+1;
94 for (bit = 1; curbitmap != 1; bit++)
95 curbitmap >>= 1;
97 return (bit - 1 + (i << 3) * sizeof(curbitmap));
102 * NOT REACHED
104 panic("devfs_clone_bitmap_fff");
105 return -1;
110 devfs_clone_bitmap_chk(struct devfs_bitmap *bitmap, int unit)
112 int chunk;
114 chunk = unit / (sizeof(u_long) * 8);
115 unit -= chunk * (sizeof(u_long) * 8);
117 if (chunk >= bitmap->chunks)
118 return 1;
120 return !((bitmap->bitmap[chunk]) & (1 << unit));
124 void
125 devfs_clone_bitmap_set(struct devfs_bitmap *bitmap, int unit)
127 int chunk;
129 chunk = unit / (sizeof(u_long) * 8);
130 unit -= chunk * (sizeof(u_long) * 8);
132 if (chunk >= bitmap->chunks)
133 devfs_clone_bitmap_resize(bitmap, chunk);
134 bitmap->bitmap[chunk] &= ~(1<<unit);
138 void
139 devfs_clone_bitmap_rst(struct devfs_bitmap *bitmap, int unit)
141 int chunk;
143 chunk = unit / (sizeof(u_long) * 8);
144 unit -= chunk * (sizeof(u_long) * 8);
146 if (chunk >= bitmap->chunks)
147 return;
148 bitmap->bitmap[chunk] |= (1 << unit);
153 devfs_clone_bitmap_get(struct devfs_bitmap *bitmap, int limit)
155 int unit;
157 unit = devfs_clone_bitmap_fff(bitmap);
158 if ((limit > 0) && (unit > limit))
159 return -1;
160 devfs_clone_bitmap_set(bitmap, unit);
162 return unit;