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",
24 sizeof(struct block_request),
25 __alignof__(struct block_request), 0,
27 bh_kcache = kmem_cache_create("buffer_heads",
28 sizeof(struct buffer_head),
29 __alignof__(struct buffer_head), 0,
33 /* Now probe for and init the block device for the ext2 ram disk */
34 extern uint8_t _binary_mnt_ext2fs_img_size[];
35 extern uint8_t _binary_mnt_ext2fs_img_start[];
36 /* Build and init the block device */
37 struct block_device *ram_bd = kmalloc(sizeof(struct block_device), 0);
38 memset(ram_bd, 0, sizeof(struct block_device));
40 ram_bd->b_sector_sz = 512;
41 ram_bd->b_nr_sector = (unsigned long)_binary_mnt_ext2fs_img_size / 512;
42 kref_init(&ram_bd->b_kref, fake_release, 1);
43 pm_init(&ram_bd->b_pm, &block_pm_op, ram_bd);
44 ram_bd->b_data = _binary_mnt_ext2fs_img_start;
45 strlcpy(ram_bd->b_name, "RAMDISK", BDEV_INLINE_NAME);
46 /* Connect it to the file system */
47 struct file *ram_bf = make_device("/dev_vfs/ramdisk", S_IRUSR | S_IWUSR,
48 __S_IFBLK, &block_f_op);
49 /* make sure the inode tracks the right pm (not it's internal one) */
50 ram_bf->f_dentry->d_inode->i_mapping = &ram_bd->b_pm;
51 ram_bf->f_dentry->d_inode->i_bdev = ram_bd; /* this holds the bd kref */
52 kref_put(&ram_bf->f_kref);
53 #endif /* CONFIG_EXT2FS */
56 /* Generic helper, returns a kref'd reference out of principle. */
57 struct block_device *get_bdev(char *path)
59 struct block_device *bdev;
61 block_f = do_file_open(path, O_RDWR, 0);
63 bdev = block_f->f_dentry->d_inode->i_bdev;
64 kref_get(&bdev->b_kref, 1);
65 kref_put(&block_f->f_kref);
69 /* Frees all the BHs associated with page. There could be 0, to deal with one
70 * that wasn't UPTODATE. Don't call this on a page that isn't a PG_BUFFER.
71 * Note, these are not a circular LL (for now). */
72 void free_bhs(struct page *page)
74 struct buffer_head *bh, *next;
75 assert(atomic_read(&page->pg_flags) & PG_BUFFER);
76 bh = (struct buffer_head*)page->pg_private;
80 kmem_cache_free(bh_kcache, bh);
83 page->pg_private = 0; /* catch bugs */
86 /* This ultimately will handle the actual request processing, all the way down
87 * to the driver, and will deal with blocking. For now, we just fulfill the
88 * request right away (RAM based block devs). */
89 int bdev_submit_request(struct block_device *bdev, struct block_request *breq)
92 unsigned long first_sector;
93 unsigned int nr_sector;
95 for (int i = 0; i < breq->nr_bhs; i++) {
96 first_sector = breq->bhs[i]->bh_sector;
97 nr_sector = breq->bhs[i]->bh_nr_sector;
98 /* Sectors are indexed starting with 0, for now. */
99 if (first_sector + nr_sector > bdev->b_nr_sector) {
100 warn("Exceeding the num sectors!");
103 if (breq->flags & BREQ_READ) {
104 dst = breq->bhs[i]->bh_buffer;
105 src = bdev->b_data + (first_sector << SECTOR_SZ_LOG);
106 } else if (breq->flags & BREQ_WRITE) {
107 dst = bdev->b_data + (first_sector << SECTOR_SZ_LOG);
108 src = breq->bhs[i]->bh_buffer;
110 panic("Need a request type!\n");
112 memcpy(dst, src, nr_sector << SECTOR_SZ_LOG);
114 /* Faking the device interrupt with an alarm */
115 void breq_handler(struct alarm_waiter *waiter)
117 /* In the future, we'll need to figure out which breq this was in
119 struct block_request *breq = (struct block_request*)waiter->data;
121 breq->callback(breq);
124 struct timer_chain *tchain = &per_cpu_info[core_id()].tchain;
125 struct alarm_waiter *waiter = kmalloc(sizeof(struct alarm_waiter), 0);
126 init_awaiter(waiter, breq_handler);
127 /* Stitch things up, so we know how to find things later */
130 set_awaiter_rel(waiter, 5000);
131 set_alarm(tchain, waiter);
135 /* Helper method, unblocks someone blocked on sleep_on_breq(). */
136 void generic_breq_done(struct block_request *breq)
138 int8_t irq_state = 0;
139 if (!sem_up_irqsave(&breq->sem, &irq_state)) {
140 /* This shouldn't happen anymore. Let brho know if it does. */
141 warn("[kernel] no one waiting on breq %p", breq);
145 /* Helper, pairs with generic_breq_done(). Note we sleep here on a semaphore
146 * instead of faking it with an alarm. Ideally, this code will be the same even
147 * for real block devices (that don't fake things with timer interrupts). */
148 void sleep_on_breq(struct block_request *breq)
150 int8_t irq_state = 0;
151 /* Since printk takes a while, this may make you lose the race */
152 printd("Sleeping on breq %p\n", breq);
153 assert(irq_is_enabled());
154 sem_down_irqsave(&breq->sem, &irq_state);
157 /* This just tells the page cache that it is 'up to date'. Due to the nature of
158 * the blocks in the page cache, we don't actually read the items in on
159 * readpage, we read them in when a specific block is there */
160 int block_readpage(struct page_map *pm, struct page *page)
162 atomic_or(&page->pg_flags, PG_UPTODATE);
166 /* Returns a BH pointing to the buffer where blk_num from bdev is located (given
167 * blocks of size blk_sz). This uses the page cache for the page allocations
168 * and evictions, but only caches blocks that are requested. Check the docs for
169 * more info. The BH isn't refcounted, but a page refcnt is returned. Call
170 * put_block (nand/xor dirty block).
172 * Note we're using the lock_page() to sync (which is what we do with the page
173 * cache too. It's not ideal, but keeps things simpler for now.
175 * Also note we're a little inconsistent with the use of sector sizes in certain
176 * files. We'll sort it eventually. */
177 struct buffer_head *bdev_get_buffer(struct block_device *bdev,
178 unsigned long blk_num, unsigned int blk_sz)
181 struct page_map *pm = &bdev->b_pm;
182 struct buffer_head *bh, *new, *prev, **next_loc;
183 struct block_request *breq;
185 unsigned int blk_per_pg = PGSIZE / blk_sz;
186 unsigned int sct_per_blk = blk_sz / bdev->b_sector_sz;
187 unsigned int blk_offset = (blk_num % blk_per_pg) * blk_sz;
189 assert(blk_offset < PGSIZE);
191 warn("Asking for the 0th block of a bdev...");
192 /* Make sure there's a page in the page cache. Should always be one. */
193 error = pm_load_page(pm, blk_num / blk_per_pg, &page);
195 panic("Failed to load page! (%d)", error);
196 my_buf = page2kva(page) + blk_offset;
197 atomic_or(&page->pg_flags, PG_BUFFER);
199 bh = (struct buffer_head*)page->pg_private;
201 /* look through all the BHs for ours, stopping if we go too far. */
203 if (bh->bh_buffer == my_buf) {
205 } else if (bh->bh_buffer > my_buf) {
211 /* At this point, bh points to the one beyond our space (or 0), and prev is
212 * either the one before us or 0. We make a BH, and try to insert */
213 new = kmem_cache_alloc(bh_kcache, 0);
215 new->bh_page = page; /* weak ref */
216 new->bh_buffer = my_buf;
219 new->bh_bdev = bdev; /* uncounted ref */
220 new->bh_sector = blk_num * sct_per_blk;
221 new->bh_nr_sector = sct_per_blk;
222 /* Try to insert the new one in place. If it fails, retry the whole "find
223 * the bh" process. This should be rare, so no sense optimizing it. */
224 next_loc = prev ? &prev->bh_next : (struct buffer_head**)&page->pg_private;
225 /* Normally, there'd be an ABA problem here, but we never actually remove
226 * bhs from the chain until the whole page gets cleaned up, which can't
227 * happen while we hold a reference to the page. */
228 if (!atomic_cas_ptr((void**)next_loc, bh, new)) {
229 kmem_cache_free(bh_kcache, new);
234 /* At this point, we have the BH for our buf, but it might not be up to
235 * date, and there might be someone else trying to update it. */
236 /* is it already here and up to date? if so, we're done */
237 if (bh->bh_flags & BH_UPTODATE)
239 /* if not, try to lock the page (could BLOCK). Using this for syncing. */
241 /* double check, are we up to date? if so, we're done */
242 if (bh->bh_flags & BH_UPTODATE) {
246 /* if we're here, the page is locked by us, we need to read the block */
247 breq = kmem_cache_alloc(breq_kcache, 0);
249 breq->flags = BREQ_READ;
250 breq->callback = generic_breq_done;
252 sem_init_irqsave(&breq->sem, 0);
253 breq->bhs = breq->local_bhs;
256 error = bdev_submit_request(bdev, breq);
259 kmem_cache_free(breq_kcache, breq);
260 /* after the data is read, we mark it up to date and unlock the page. */
261 bh->bh_flags |= BH_UPTODATE;
266 /* Will dirty the block/BH/page for the given block/buffer. Will have to be
267 * careful with the page reclaimer - if someone holds a reference, they can
269 void bdev_dirty_buffer(struct buffer_head *bh)
271 struct page *page = bh->bh_page;
272 /* TODO: race on flag modification */
273 bh->bh_flags |= BH_DIRTY;
274 atomic_or(&page->pg_flags, PG_DIRTY);
277 /* Decrefs the buffer from bdev_get_buffer(). Call this when you no longer
278 * reference your block/buffer. For now, we do refcnting on the page, since the
279 * reclaiming will be in page sized chunks from the page cache. */
280 void bdev_put_buffer(struct buffer_head *bh)
282 pm_put_page(bh->bh_page);
285 /* Block device page map ops: */
286 struct page_map_operations block_pm_op = {
290 /* Block device file ops: for now, we don't let you do much of anything */
291 struct file_operations block_f_op = {
295 kfs_readdir, /* this will fail gracefully */
300 0, /* fsync - makes no sense */