1 /* Copyright (c) 2010 The Regents of the University of California
2 * Barret Rhoden <brho@cs.berkeley.edu>
3 * See LICENSE for details.
5 * Block devices and generic blockdev infrastructure */
11 #include <page_alloc.h>
13 /* These two are needed for the fake interrupt */
17 struct file_operations block_f_op;
18 struct page_map_operations block_pm_op;
19 struct kmem_cache *breq_kcache;
23 breq_kcache = kmem_cache_create("block_reqs", sizeof(struct block_request),
24 __alignof__(struct block_request), 0, 0, 0);
25 bh_kcache = kmem_cache_create("buffer_heads", sizeof(struct buffer_head),
26 __alignof__(struct buffer_head), 0, 0, 0);
29 /* Now probe for and init the block device for the ext2 ram disk */
30 extern uint8_t _binary_mnt_ext2fs_img_size[];
31 extern uint8_t _binary_mnt_ext2fs_img_start[];
32 /* Build and init the block device */
33 struct block_device *ram_bd = kmalloc(sizeof(struct block_device), 0);
34 memset(ram_bd, 0, sizeof(struct block_device));
36 ram_bd->b_sector_sz = 512;
37 ram_bd->b_nr_sector = (unsigned long)_binary_mnt_ext2fs_img_size / 512;
38 kref_init(&ram_bd->b_kref, fake_release, 1);
39 pm_init(&ram_bd->b_pm, &block_pm_op, ram_bd);
40 ram_bd->b_data = _binary_mnt_ext2fs_img_start;
41 strncpy(ram_bd->b_name, "RAMDISK", BDEV_INLINE_NAME);
42 ram_bd->b_name[BDEV_INLINE_NAME - 1] = '\0';
43 /* Connect it to the file system */
44 struct file *ram_bf = make_device("/dev/ramdisk", S_IRUSR | S_IWUSR,
45 __S_IFBLK, &block_f_op);
46 /* make sure the inode tracks the right pm (not it's internal one) */
47 ram_bf->f_dentry->d_inode->i_mapping = &ram_bd->b_pm;
48 ram_bf->f_dentry->d_inode->i_bdev = ram_bd; /* this holds the bd kref */
49 kref_put(&ram_bf->f_kref);
50 #endif /* CONFIG_EXT2FS */
53 /* Generic helper, returns a kref'd reference out of principle. */
54 struct block_device *get_bdev(char *path)
56 struct block_device *bdev;
58 block_f = do_file_open(path, O_RDWR, 0);
60 bdev = block_f->f_dentry->d_inode->i_bdev;
61 kref_get(&bdev->b_kref, 1);
62 kref_put(&block_f->f_kref);
66 /* Frees all the BHs associated with page. There could be 0, to deal with one
67 * that wasn't UPTODATE. Don't call this on a page that isn't a PG_BUFFER.
68 * Note, these are not a circular LL (for now). */
69 void free_bhs(struct page *page)
71 struct buffer_head *bh, *next;
72 assert(atomic_read(&page->pg_flags) & PG_BUFFER);
73 bh = (struct buffer_head*)page->pg_private;
77 kmem_cache_free(bh_kcache, bh);
80 page->pg_private = 0; /* catch bugs */
83 /* This ultimately will handle the actual request processing, all the way down
84 * to the driver, and will deal with blocking. For now, we just fulfill the
85 * request right away (RAM based block devs). */
86 int bdev_submit_request(struct block_device *bdev, struct block_request *breq)
89 unsigned long first_sector;
90 unsigned int nr_sector;
92 for (int i = 0; i < breq->nr_bhs; i++) {
93 first_sector = breq->bhs[i]->bh_sector;
94 nr_sector = breq->bhs[i]->bh_nr_sector;
95 /* Sectors are indexed starting with 0, for now. */
96 if (first_sector + nr_sector > bdev->b_nr_sector) {
97 warn("Exceeding the num sectors!");
100 if (breq->flags & BREQ_READ) {
101 dst = breq->bhs[i]->bh_buffer;
102 src = bdev->b_data + (first_sector << SECTOR_SZ_LOG);
103 } else if (breq->flags & BREQ_WRITE) {
104 dst = bdev->b_data + (first_sector << SECTOR_SZ_LOG);
105 src = breq->bhs[i]->bh_buffer;
107 panic("Need a request type!\n");
109 memcpy(dst, src, nr_sector << SECTOR_SZ_LOG);
111 /* Faking the device interrupt with an alarm */
112 void breq_handler(struct alarm_waiter *waiter)
114 /* In the future, we'll need to figure out which breq this was in
116 struct block_request *breq = (struct block_request*)waiter->data;
118 breq->callback(breq);
121 struct timer_chain *tchain = &per_cpu_info[core_id()].tchain;
122 struct alarm_waiter *waiter = kmalloc(sizeof(struct alarm_waiter), 0);
123 init_awaiter(waiter, breq_handler);
124 /* Stitch things up, so we know how to find things later */
127 set_awaiter_rel(waiter, 5000);
128 set_alarm(tchain, waiter);
132 /* Helper method, unblocks someone blocked on sleep_on_breq(). */
133 void generic_breq_done(struct block_request *breq)
135 int8_t irq_state = 0;
136 if (!sem_up_irqsave(&breq->sem, &irq_state)) {
137 /* This shouldn't happen anymore. Let brho know if it does. */
138 warn("[kernel] no one waiting on breq %p", breq);
142 /* Helper, pairs with generic_breq_done(). Note we sleep here on a semaphore
143 * instead of faking it with an alarm. Ideally, this code will be the same even
144 * for real block devices (that don't fake things with timer interrupts). */
145 void sleep_on_breq(struct block_request *breq)
147 int8_t irq_state = 0;
148 /* Since printk takes a while, this may make you lose the race */
149 printd("Sleeping on breq %p\n", breq);
150 assert(irq_is_enabled());
151 sem_down_irqsave(&breq->sem, &irq_state);
154 /* This just tells the page cache that it is 'up to date'. Due to the nature of
155 * the blocks in the page cache, we don't actually read the items in on
156 * readpage, we read them in when a specific block is there */
157 int block_readpage(struct page_map *pm, struct page *page)
159 atomic_or(&page->pg_flags, PG_UPTODATE);
163 /* Returns a BH pointing to the buffer where blk_num from bdev is located (given
164 * blocks of size blk_sz). This uses the page cache for the page allocations
165 * and evictions, but only caches blocks that are requested. Check the docs for
166 * more info. The BH isn't refcounted, but a page refcnt is returned. Call
167 * put_block (nand/xor dirty block).
169 * Note we're using the lock_page() to sync (which is what we do with the page
170 * cache too. It's not ideal, but keeps things simpler for now.
172 * Also note we're a little inconsistent with the use of sector sizes in certain
173 * files. We'll sort it eventually. */
174 struct buffer_head *bdev_get_buffer(struct block_device *bdev,
175 unsigned long blk_num, unsigned int blk_sz)
178 struct page_map *pm = &bdev->b_pm;
179 struct buffer_head *bh, *new, *prev, **next_loc;
180 struct block_request *breq;
182 unsigned int blk_per_pg = PGSIZE / blk_sz;
183 unsigned int sct_per_blk = blk_sz / bdev->b_sector_sz;
184 unsigned int blk_offset = (blk_num % blk_per_pg) * blk_sz;
186 assert(blk_offset < PGSIZE);
188 warn("Asking for the 0th block of a bdev...");
189 /* Make sure there's a page in the page cache. Should always be one. */
190 error = pm_load_page(pm, blk_num / blk_per_pg, &page);
192 panic("Failed to load page! (%d)", error);
193 my_buf = page2kva(page) + blk_offset;
194 atomic_or(&page->pg_flags, PG_BUFFER);
196 bh = (struct buffer_head*)page->pg_private;
198 /* look through all the BHs for ours, stopping if we go too far. */
200 if (bh->bh_buffer == my_buf) {
202 } else if (bh->bh_buffer > my_buf) {
208 /* At this point, bh points to the one beyond our space (or 0), and prev is
209 * either the one before us or 0. We make a BH, and try to insert */
210 new = kmem_cache_alloc(bh_kcache, 0);
212 new->bh_page = page; /* weak ref */
213 new->bh_buffer = my_buf;
216 new->bh_bdev = bdev; /* uncounted ref */
217 new->bh_sector = blk_num * sct_per_blk;
218 new->bh_nr_sector = sct_per_blk;
219 /* Try to insert the new one in place. If it fails, retry the whole "find
220 * the bh" process. This should be rare, so no sense optimizing it. */
221 next_loc = prev ? &prev->bh_next : (struct buffer_head**)&page->pg_private;
222 /* Normally, there'd be an ABA problem here, but we never actually remove
223 * bhs from the chain until the whole page gets cleaned up, which can't
224 * happen while we hold a reference to the page. */
225 if (!atomic_cas_ptr((void**)next_loc, bh, new)) {
226 kmem_cache_free(bh_kcache, new);
231 /* At this point, we have the BH for our buf, but it might not be up to
232 * date, and there might be someone else trying to update it. */
233 /* is it already here and up to date? if so, we're done */
234 if (bh->bh_flags & BH_UPTODATE)
236 /* if not, try to lock the page (could BLOCK). Using this for syncing. */
238 /* double check, are we up to date? if so, we're done */
239 if (bh->bh_flags & BH_UPTODATE) {
243 /* if we're here, the page is locked by us, we need to read the block */
244 breq = kmem_cache_alloc(breq_kcache, 0);
246 breq->flags = BREQ_READ;
247 breq->callback = generic_breq_done;
249 sem_init_irqsave(&breq->sem, 0);
250 breq->bhs = breq->local_bhs;
253 error = bdev_submit_request(bdev, breq);
256 kmem_cache_free(breq_kcache, breq);
257 /* after the data is read, we mark it up to date and unlock the page. */
258 bh->bh_flags |= BH_UPTODATE;
263 /* Will dirty the block/BH/page for the given block/buffer. Will have to be
264 * careful with the page reclaimer - if someone holds a reference, they can
266 void bdev_dirty_buffer(struct buffer_head *bh)
268 struct page *page = bh->bh_page;
269 /* TODO: race on flag modification */
270 bh->bh_flags |= BH_DIRTY;
271 atomic_or(&page->pg_flags, PG_DIRTY);
274 /* Decrefs the buffer from bdev_get_buffer(). Call this when you no longer
275 * reference your block/buffer. For now, we do refcnting on the page, since the
276 * reclaiming will be in page sized chunks from the page cache. */
277 void bdev_put_buffer(struct buffer_head *bh)
279 pm_put_page(bh->bh_page);
282 /* Block device page map ops: */
283 struct page_map_operations block_pm_op = {
287 /* Block device file ops: for now, we don't let you do much of anything */
288 struct file_operations block_f_op = {
292 kfs_readdir, /* this will fail gracefully */
297 0, /* fsync - makes no sense */