2 * Copyright 2012 Google, Inc.
4 * This software is licensed under the terms of the GNU General Public
5 * License version 2, as published by the Free Software Foundation, and
6 * may be copied, distributed, and modified under those terms.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 * The boot cache device mapper reads a set of contiguously stored sectors.
15 * These sectors are copies of the sectors read during an earlier boot. Only
16 * small reads (less than some number of sectors) are selected for the cache,
17 * since this results in the highest benefit.
19 * The data for the boot cache consists of three sections:
20 * a header, the sector trace and the cache sectors.
21 * These are stored after the file system in the same partition.
23 * The boot cache is created by separate user process that reads a
24 * sector trace created if the boot cache is invalid.
26 #include <linux/async.h>
27 #include <linux/atomic.h>
28 #include <linux/delay.h>
29 #include <linux/device-mapper.h>
30 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/mutex.h>
33 #include <linux/sched.h>
34 #include <linux/slab.h>
35 #include <linux/workqueue.h>
38 #include "dm-bootcache.h"
40 #define DM_MSG_PREFIX "bootcache"
42 #define DEFAULT_MAX_PAGES 50000
43 #define DEFAULT_SIZE_LIMIT 128
44 #define DEFAULT_MAX_TRACE (1 << 13)
45 #define MAX_TRACE (1 << 20)
46 #define DEV_MODE FMODE_READ
47 #define SECTOR_SIZE (1 << SECTOR_SHIFT)
48 #define SECTORS_PER_PAGE (PAGE_SIZE / SECTOR_SIZE)
59 struct bootcache_waiter {
60 struct completion completion;
64 struct bootcache_args {
65 /* Device being cached. The boot cache also stores its cache here. */
68 /* Identifies the data on the device. eg root hex digest from verity */
69 const char *signature;
71 /* Sector start of cache on device */
74 /* Max num of pages to cache */
77 /* Reads this size or larger will not be cached */
80 /* Maximum number of trace records to collect */
84 struct bootcache_stats {
85 unsigned num_requests; /* Read requests */
86 unsigned num_hits; /* Number of hits */
87 unsigned overlapped; /* Blocks used while reading rest */
90 struct bootcache_page {
91 struct bootcache_page *next;
93 u64 sector; /* first sector in set of sectors in this page */
97 struct bootcache_sector_map {
98 u32 num_buckets; /* Number of buckets for hash */
99 u32 num_pages; /* Number of pages of sectors */
100 struct bootcache_page *pages; /* Cache of pages of sectors */
101 struct bootcache_page *nextpage;/* Next page entry to add */
102 struct bootcache_page **bucket; /* Hash buckets */
106 const char *name; /* Taken from device being cached */
107 struct bootcache_stats stats;
108 struct bootcache_args args;
109 sector_t begin; /* Beginning sector of underlying device */
110 sector_t len; /* Length in sectors of underlying device */
111 atomic_t state; /* Cache state - needs atomic read */
112 spinlock_t trace_lock; /* Spin lock for trace table */
113 struct bootcache_trace *trace; /* Trace of blocks read during boot */
114 u32 trace_next; /* Next element to fill for tracing */
115 u32 max_io; /* Max pages we can read/write */
116 bool is_valid; /* The cache is valid */
117 bool is_free; /* The cache data has been freed */
118 struct kref kref; /* Protects in-flight operations */
119 struct dm_target *ti; /* Device in device mapper */
120 struct bio_set *bio_set; /* Set of bios for reading blocks */
121 struct dm_dev *dev; /* Device for both cache and data */
122 struct delayed_work work; /* Work that needs a thread */
123 struct mutex cache_lock; /* Locks everything in cache struct */
124 struct completion init_complete; /* Wait for initialization */
125 struct bootcache_sector_map sectors; /* Table of pages of sectors */
126 /* Sysfs files for managing the block cache */
127 struct bin_attribute valid; /* 1 -> valid 0 -> build cache */
128 struct bin_attribute free; /* Write '1' to free cache */
129 struct bin_attribute header; /* Content for bootcache header */
130 struct bin_attribute blocktrace;/* Trace of blocks accessed */
131 /* Computed hdr to be compared with on disk header. */
132 struct bootcache_hdr hdr;
135 static inline u64 bytes_to_pages(u64 bytes)
137 return (bytes + PAGE_SIZE - 1) >> PAGE_SHIFT;
140 static inline u64 sectors_to_pages(u64 sectors)
142 return sectors >> (PAGE_SHIFT - SECTOR_SHIFT);
145 static inline u64 pages_to_sectors(u64 pages)
147 return pages << (PAGE_SHIFT - SECTOR_SHIFT);
150 static void bootcache_bio_destructor(struct bio *bio)
152 struct bootcache *cache = bio->bi_private;
154 bio_free(bio, cache->bio_set);
157 static inline struct bootcache_page **bootcache_hash(
158 struct bootcache_sector_map *map,
161 return &map->bucket[(u32)sector % map->num_buckets];
164 static struct bootcache_page *bootcache_get_chunk(
165 struct bootcache_sector_map *map,
168 struct bootcache_page *next;
170 next = *bootcache_hash(map, sector);
172 if (sector == next->sector) {
183 struct bootcache_page *bootcache_new_chunk(struct bootcache_sector_map *map,
186 struct bootcache_page **bucket = bootcache_hash(map, sector);
187 struct bootcache_page *p;
189 if (map->nextpage == &map->pages[map->num_pages]) {
190 DMWARN("block cache full");
194 p->page = alloc_page(GFP_KERNEL);
201 static int build_sector_map(struct bootcache_sector_map *map, u32 num_pages)
203 map->num_pages = num_pages;
204 map->num_buckets = num_pages * 3 / 2;
205 map->bucket = kzalloc(map->num_buckets * sizeof(*map->bucket),
208 DMERR("build_sector_maps kzalloc buckets");
211 map->pages = kzalloc(num_pages * sizeof(*map->pages), GFP_KERNEL);
214 DMERR("build_sector_maps kzalloc pages");
217 map->nextpage = map->pages;
221 static void bootcache_free_sector_map(struct bootcache_sector_map *map)
223 struct bootcache_page *p;
225 for (p = map->pages; p < map->nextpage; p++)
227 __free_pages(p->page, 0);
235 static int bootcache_create_bin_file(struct bootcache *cache,
236 struct bin_attribute *attr, char *name, ssize_t size,
237 ssize_t (*read)(struct file *, struct kobject *,
238 struct bin_attribute *, char *, loff_t, size_t),
239 ssize_t (*write)(struct file *, struct kobject *,
240 struct bin_attribute *, char *, loff_t, size_t))
246 attr->attr.name = name;
247 attr->attr.mode = write ? 0644 : 0444;
252 rc = sysfs_create_bin_file(dm_kobject(dm_table_get_md(
253 cache->ti->table)), attr);
255 DMERR("sysfs_create_bin_file %s: %d", name, rc);
260 * bootcache_remove_bin_file uses the file name as flag
261 * to determine if the sysfs file has been created.
263 static void bootcache_remove_bin_file(struct bootcache *cache,
264 struct bin_attribute *attr)
266 if (attr->attr.name) {
267 sysfs_remove_bin_file(dm_kobject(dm_table_get_md(
268 cache->ti->table)), attr);
269 attr->attr.name = NULL;
274 * bootcache_remove_all_files removes all the sysfs files
275 * that have been created and only the ones that have been
278 static void bootcache_remove_all_files(struct bootcache *cache)
280 bootcache_remove_bin_file(cache, &cache->blocktrace);
281 bootcache_remove_bin_file(cache, &cache->header);
282 bootcache_remove_bin_file(cache, &cache->free);
283 bootcache_remove_bin_file(cache, &cache->valid);
286 static void bootcache_free_resources(struct kref *kref)
288 struct bootcache *cache = container_of(kref, struct bootcache,
290 /* Will hang if we try to remove cache->free here */
291 bootcache_remove_bin_file(cache, &cache->blocktrace);
292 bootcache_remove_bin_file(cache, &cache->header);
293 bootcache_remove_bin_file(cache, &cache->valid);
294 bootcache_free_sector_map(&cache->sectors);
300 * bootcache_get_ino returns the inode number of the bio if it has one.
301 * If not, it returns 0, an illegal inode number.
302 * When the bio is sent down for I/O, these fields don't change
303 * while the I/O is pending.
305 static unsigned long bootcache_get_ino(struct bio *bio)
311 if (!bio->bi_io_vec->bv_page)
313 if (!bio->bi_io_vec->bv_page->mapping)
315 if (!bio->bi_io_vec->bv_page->mapping->host)
317 return bio->bi_io_vec->bv_page->mapping->host->i_ino;
320 static void bootcache_record(struct bootcache *cache, struct bio *bio)
322 u64 sector = bio->bi_sector;
323 u64 count = to_sector(bio->bi_size);
324 struct bootcache_trace *tr;
328 spin_lock(&cache->trace_lock);
329 if (cache->trace_next < cache->args.max_trace) {
330 tr = &cache->trace[cache->trace_next];
333 tr->ino = bootcache_get_ino(bio);
336 spin_unlock(&cache->trace_lock);
339 static bool is_in_cache(struct bootcache *cache, struct bio *bio)
341 u64 sector = bio->bi_sector;
342 u32 count = bytes_to_pages(bio->bi_size);
345 for (i = 0; i < count; i++, sector += SECTORS_PER_PAGE) {
346 if (!bootcache_get_chunk(&cache->sectors, sector))
349 ++cache->stats.num_hits;
353 static void bootcache_read_from_cache(struct bootcache *cache, struct bio *bio)
355 struct bootcache_page *bp;
356 u64 sector = bio->bi_sector;
357 u32 count = bytes_to_pages(bio->bi_size);
362 for (i = 0; i < count; i++, sector += SECTORS_PER_PAGE) {
363 bp = bootcache_get_chunk(&cache->sectors, sector);
366 * Should have found it because we just
367 * looked for it before calling this code
369 DMCRIT("Didn't find block %llx", sector);
372 dst = kmap_atomic(bio_iovec_idx(bio, i)->bv_page);
373 src = kmap_atomic(bp->page);
374 memcpy(dst, src, PAGE_SIZE);
378 set_bit(BIO_UPTODATE, &bio->bi_flags);
379 bio->bi_end_io(bio, 0);
382 static void bootcache_read(struct bootcache *cache, struct bio *bio)
386 bio->bi_bdev = cache->dev->bdev;
387 /* Only record reads below the given size */
388 if ((atomic_read(&cache->state) == BC_BYPASS) ||
389 (to_sector(bio->bi_size) > cache->args.size_limit)) {
390 generic_make_request(bio);
393 kref_get(&cache->kref);
395 state = atomic_read(&cache->state);
398 wait_for_completion(&cache->init_complete);
401 bootcache_record(cache, bio);
402 generic_make_request(bio);
405 ++cache->stats.overlapped;
408 if (is_in_cache(cache, bio))
409 bootcache_read_from_cache(cache, bio);
411 generic_make_request(bio);
414 generic_make_request(bio);
417 DMCRIT("unknown state %d", state);
421 ++cache->stats.num_requests;
422 if (cache->stats.num_requests % 1000 == 0) {
423 DMINFO("hits = %u / %u",
424 cache->stats.num_hits,
425 cache->stats.num_requests);
427 kref_put(&cache->kref, bootcache_free_resources);
430 static ssize_t valid_read(struct file *file, struct kobject *kobp,
431 struct bin_attribute *bin_attr, char *buf,
432 loff_t pos, size_t count)
434 struct bootcache *cache = container_of(bin_attr, struct bootcache,
437 if (pos > 0 || count == 0)
439 buf[0] = cache->is_valid ? '1' : '0';
443 static ssize_t free_read(struct file *file, struct kobject *kobp,
444 struct bin_attribute *bin_attr, char *buf,
445 loff_t pos, size_t count)
447 struct bootcache *cache = container_of(bin_attr, struct bootcache,
450 if (pos > 0 || count == 0)
452 buf[0] = cache->is_free ? '1' : '0';
456 static ssize_t free_write(struct file *file, struct kobject *kobp,
457 struct bin_attribute *bin_attr, char *buf,
458 loff_t pos, size_t count)
460 struct bootcache *cache = container_of(bin_attr, struct bootcache,
464 mutex_lock(&cache->cache_lock);
465 if (cache->is_free) {
469 atomic_set(&cache->state, BC_BYPASS);
471 * Once BC_BYPASS is set, the system
472 * should drain quickly.
474 kref_put(&cache->kref, bootcache_free_resources);
476 /* Tell caller we wrote everything */
479 mutex_unlock(&cache->cache_lock);
483 static ssize_t header_read(struct file *file, struct kobject *kobp,
484 struct bin_attribute *bin_attr, char *buf,
485 loff_t pos, size_t count)
487 struct bootcache *cache = container_of(bin_attr, struct bootcache,
490 return memory_read_from_buffer(buf, count, &pos, &cache->hdr,
494 static ssize_t blocktrace_read(struct file *file, struct kobject *kobp,
495 struct bin_attribute *bin_attr, char *buf,
496 loff_t pos, size_t count)
498 struct bootcache *cache = container_of(bin_attr, struct bootcache,
504 kref_get(&cache->kref);
505 if (atomic_read(&cache->state) != BC_TRACING) {
509 data = (char *)cache->trace;
511 spin_lock(&cache->trace_lock);
512 next = cache->trace_next;
513 spin_unlock(&cache->trace_lock);
515 size = next * sizeof(struct bootcache_trace);
517 err = memory_read_from_buffer(buf, count, &pos, data, size);
519 kref_put(&cache->kref, bootcache_free_resources);
523 static int bootcache_init_sysfs(struct bootcache *cache, struct dm_target *ti)
527 rc = bootcache_create_bin_file(cache, &cache->valid, "valid",
528 3, valid_read, NULL);
531 rc = bootcache_create_bin_file(cache, &cache->free, "free",
532 3, free_read, free_write);
535 rc = bootcache_create_bin_file(cache, &cache->header, "header",
536 sizeof(cache->hdr), header_read, NULL);
539 rc = bootcache_create_bin_file(cache, &cache->blocktrace, "blocktrace",
540 cache->args.max_trace * sizeof(struct bootcache_trace),
541 blocktrace_read, NULL);
546 bootcache_remove_all_files(cache);
550 static void bootcache_read_sectors_end(struct bio *bio, int error)
552 struct bootcache_waiter *waiter = bio->bi_private;
554 if (unlikely(error)) {
555 waiter->error = error;
556 DMERR("Error occurred in bootcache_read_sectors:"
558 error, (u64)bio->bi_sector, bio->bi_size);
560 complete(&waiter->completion);
563 static int bootcache_read_sectors(struct bootcache *cache)
565 struct bootcache_waiter waiter;
567 struct bootcache_page *p;
568 struct bootcache_page *start_page;
569 struct bio_vec *bvec;
570 sector_t sector = cache->args.cache_start + cache->hdr.sectors_meta +
572 u32 max_io = cache->max_io;
573 u32 numpages = cache->sectors.num_pages;
574 u32 chunks_to_read = (numpages + max_io - 1) / max_io;
579 bio = bio_alloc_bioset(GFP_KERNEL, max_io, cache->bio_set);
580 if (unlikely(!bio)) {
581 DMERR("Out of memory bio_alloc_bioset");
584 bio->bi_private = &waiter;
585 bio->bi_destructor = bootcache_bio_destructor;
586 p = cache->sectors.pages;
587 for (i = 0; i < chunks_to_read; i++) {
589 bio->bi_bdev = cache->dev->bdev;
590 bio->bi_end_io = bootcache_read_sectors_end;
592 bio->bi_sector = sector;
593 bvec = bio->bi_io_vec;
595 for (j = 0; j < max_io; j++, bvec++, p++) {
596 if (p == cache->sectors.nextpage)
598 bvec->bv_page = p->page;
600 bvec->bv_len = PAGE_SIZE;
602 bio->bi_size = j * PAGE_SIZE;
605 init_completion(&waiter.completion);
607 generic_make_request(bio);
608 wait_for_completion(&waiter.completion);
614 for (j = 0; j < max_io; j++, p++) {
615 if (p == cache->sectors.nextpage)
619 sector += pages_to_sectors(j);
621 bio->bi_private = cache;
623 atomic_set(&cache->state, BC_FILLED);
627 static void bootcache_dev_read_end(struct bio *bio, int error)
629 struct bootcache_waiter *waiter = bio->bi_private;
631 if (unlikely(error)) {
632 waiter->error = error;
633 DMERR("Error occurred in bootcache_dev_read: %d (%llx, %x)",
634 error, (u64)bio->bi_sector, bio->bi_size);
636 complete(&waiter->completion);
639 static int bootcache_dev_read(struct bootcache *cache, void *data,
642 struct bootcache_waiter waiter;
644 struct bio_vec *bvec;
645 int pages_to_read = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
646 int max_io = cache->max_io;
655 if (pages_to_read < max_io)
656 num_pages = pages_to_read;
659 bio = bio_alloc_bioset(GFP_KERNEL, num_pages, cache->bio_set);
660 if (unlikely(!bio)) {
661 DMERR("Out of memory bio_alloc_bioset");
664 bvec = bio->bi_io_vec;
665 for (i = 0; i < num_pages; i++, bvec++)
666 bvec->bv_page = alloc_page(GFP_KERNEL);
667 bio->bi_private = &waiter;
668 bio->bi_destructor = bootcache_bio_destructor;
671 if (pages_to_read < max_io)
672 max_io = pages_to_read;
674 bio->bi_bdev = cache->dev->bdev;
675 bio->bi_end_io = bootcache_dev_read_end;
677 bio->bi_sector = sector;
678 bvec = bio->bi_io_vec;
679 for (i = 0; i < max_io; i++, bvec++) {
681 bvec->bv_len = PAGE_SIZE;
683 pages_to_read -= max_io;
684 bio->bi_size = max_io * PAGE_SIZE;
685 bio->bi_vcnt = max_io;
687 init_completion(&waiter.completion);
689 generic_make_request(bio);
690 wait_for_completion(&waiter.completion);
695 for (i = 0; i < max_io; i++) {
696 bytes_to_copy = (len < PAGE_SIZE) ? len : PAGE_SIZE;
697 src = kmap_atomic(bio_iovec_idx(bio, i)->bv_page);
698 memcpy(dst, src, bytes_to_copy);
700 len -= bytes_to_copy;
703 dst += bytes_to_copy;
705 sector += pages_to_sectors(max_io);
708 bvec = bio->bi_io_vec;
709 for (i = 0; i < num_pages; i++, bvec++)
710 __free_pages(bvec->bv_page, 0);
711 bio->bi_private = cache;
716 static int is_valid_hdr(struct bootcache *cache, struct bootcache_hdr *hdr)
718 if (hdr->magic != BOOTCACHE_MAGIC)
720 if (hdr->version != BOOTCACHE_VERSION)
722 if (hdr->max_sectors != cache->hdr.max_sectors)
724 if (hdr->max_hw_sectors != cache->hdr.max_hw_sectors)
726 if (strncmp(hdr->date, __DATE__, sizeof(hdr->date)) != 0)
728 if (strncmp(hdr->time, __TIME__, sizeof(hdr->time)) != 0)
730 if (strncmp(hdr->signature, cache->hdr.signature,
731 sizeof(hdr->signature)) != 0)
736 static int read_trace(struct bootcache *cache)
744 size_trace = sizeof(*cache->trace) * cache->hdr.num_trace_recs;
745 cache->trace = kzalloc(size_trace, GFP_KERNEL);
747 DMERR("read_trace out of memory");
750 rc = bootcache_dev_read(cache, cache->trace, size_trace,
751 cache->hdr.sector + SECTORS_PER_PAGE);
753 DMERR("bootcache_dev_read trace %d", rc);
756 for (i = 0; i < cache->hdr.num_trace_recs; i++) {
757 struct bootcache_trace *tr;
758 tr = &cache->trace[i];
759 for (j = 0; j < tr->count; j += SECTORS_PER_PAGE) {
760 bootcache_new_chunk(&cache->sectors, tr->sector + j);
770 * Reads the bootcache header from disk, checks if it is valid
772 * read the sector trace from disk
773 * build hash table for sector trace on page boundaries
774 * begin reading in sectors to be cached
776 * setup to capture trace of sectors
778 * on error: by pass boot cache
780 static void bootcache_start(struct work_struct *work)
782 struct bootcache *cache = container_of(work, struct bootcache,
784 struct bootcache_hdr hdr;
787 rc = bootcache_dev_read(cache, &hdr, sizeof(hdr), cache->hdr.sector);
789 DMERR("bootcache_dev_read hdr %d", rc);
792 if (is_valid_hdr(cache, &hdr)) {
794 memcpy(&cache->hdr, &hdr, sizeof(cache->hdr));
795 rc = build_sector_map(&cache->sectors,
796 sectors_to_pages(cache->hdr.sectors_data));
799 rc = read_trace(cache);
802 atomic_set(&cache->state, BC_FILLING);
803 rc = bootcache_read_sectors(cache);
807 atomic_set(&cache->state, BC_TRACING);
808 cache->trace = kzalloc(sizeof(*cache->trace) *
809 cache->args.max_trace, GFP_KERNEL);
811 DMERR("cache->trace out of memory");
816 complete_all(&cache->init_complete);
819 DMERR("error occured starting bootcache, setting to by pass mode");
820 atomic_set(&cache->state, BC_BYPASS);
825 * bootcache_max_io determines the maximum number of pages that can
826 * be passed in one read request to the underlying device.
827 * @cache: the max_sectors and max_hw_sectors must
829 * @proposed_max_io: maxium number of pages the caller wants
832 * Returns maximum number of pages that can be read but
833 * no more than proposed_max_io
835 static u32 bootcache_max_io(struct bootcache *cache, u32 proposed_max_io)
840 max_sectors = min(cache->hdr.max_sectors, cache->hdr.max_hw_sectors);
841 max_pages = sectors_to_pages(max_sectors);
842 if (proposed_max_io < max_pages)
843 max_pages = proposed_max_io;
847 static void bootcache_init_hdr(struct bootcache_hdr *hdr, u64 cache_start,
848 struct block_device *bdev, const char *signature)
850 hdr->sector = cache_start;
851 hdr->magic = BOOTCACHE_MAGIC;
852 hdr->version = BOOTCACHE_VERSION;
853 strncpy(hdr->date, __DATE__, sizeof(hdr->date));
854 strncpy(hdr->time, __TIME__, sizeof(hdr->time));
855 hdr->state = BC_INIT;
856 hdr->max_hw_sectors = queue_max_hw_sectors(bdev_get_queue(bdev));
857 hdr->max_sectors = queue_max_sectors(bdev_get_queue(bdev));
858 strncpy(hdr->signature, signature, sizeof(hdr->signature));
862 * match_dev_by_uuid - callback for finding a partition using its uuid
863 * @dev: device passed in by the caller
864 * @uuid_data: opaque pointer to a uuid packed by part_pack_uuid().
866 * Returns 1 if the device matches, and 0 otherwise.
868 static int match_dev_by_uuid(struct device *dev, void *uuid_data)
870 u8 *uuid = uuid_data;
871 struct hd_struct *part = dev_to_part(dev);
876 if (memcmp(uuid, part->info->uuid, sizeof(part->info->uuid)))
885 * dm_get_device_by_uuid: claim a device using its UUID
886 * @ti: current dm_target
887 * @uuid_string: 36 byte UUID hex encoded
888 * (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)
889 * @dev_start: offset in sectors passed to dm_get_device
890 * @dev_len: length in sectors passed to dm_get_device
891 * @dm_dev: dm_dev to populate
893 * Wraps dm_get_device allowing it to use a unique partition id
894 * to find a given partition on any drive. This code is based on
895 * printk_all_partitions in that it walks all of the registered
898 * N.B., uuid_string is not checked for safety just strlen().
900 static int dm_get_device_by_uuid(struct dm_target *ti, const char *uuid_str,
901 sector_t dev_start, sector_t dev_len,
902 struct dm_dev **dm_dev)
904 struct device *dev = NULL;
906 char devt_buf[BDEVT_SIZE];
908 size_t uuid_length = strlen(uuid_str);
910 if (uuid_length < 36)
912 /* Pack the requested UUID in the expected format. */
913 part_pack_uuid(uuid_str, uuid);
915 dev = class_find_device(&block_class, NULL, uuid, &match_dev_by_uuid);
922 /* The caller may specify +/-%u after the UUID if they want a partition
923 * before or after the one identified.
925 if (uuid_length > 36) {
926 unsigned int part_offset;
928 unsigned minor = MINOR(devt);
929 if (sscanf(uuid_str + 36, "%c%u", &sign, &part_offset) == 2) {
931 minor += part_offset;
932 } else if (sign == '-') {
933 minor -= part_offset;
935 DMWARN("Trailing characters after UUID: %s\n",
938 devt = MKDEV(MAJOR(devt), minor);
942 /* Construct the dev name to pass to dm_get_device. dm_get_device
943 * doesn't support being passed a dev_t.
945 snprintf(devt_buf, sizeof(devt_buf), "%u:%u",
946 MAJOR(devt), MINOR(devt));
948 /* TODO(wad) to make this generic we could also pass in the mode. */
949 if (!dm_get_device(ti, devt_buf, dm_table_get_mode(ti->table), dm_dev))
952 ti->error = "Failed to acquire device";
953 DMDEBUG("Failed to acquire discovered device %s", devt_buf);
956 ti->error = "Bad UUID";
957 DMDEBUG("Supplied value '%s' is an invalid UUID", uuid_str);
960 DMDEBUG("No matching partition for GUID: %s", uuid_str);
961 ti->error = "No matching GUID";
965 static int bootcache_get_device(
966 struct dm_target *ti,
970 struct dm_dev **dm_dev)
973 /* Try the normal path first since if everything is ready, it
974 * will be the fastest.
976 if (!dm_get_device(ti, devname,
977 dm_table_get_mode(ti->table), dm_dev))
980 /* Try the device by partition UUID */
981 if (!dm_get_device_by_uuid(ti, devname, dev_start, dev_len,
985 /* No need to be too aggressive since this is a slow path. */
987 } while (driver_probe_done() != 0 || *dm_dev == NULL);
988 async_synchronize_full();
993 * bootcache_ctr - Construct a boot cache
994 * @ti: Target being created
995 * @argc: Number of elements in argv
996 * @argv: Vector of arguments - All arguments are positional, this
997 * means that to set a particular argument, all of its
998 * predecessors must be present.
1000 * Accepts the folowing parametes [defaults in brackets]:
1001 * @device: Device being cached. The boot cache is alsoe stored here.
1002 * @cache_start: Sector start on the device for the boot cache.
1003 * @signature: Signature to determine if cache is valid.
1004 * @size_limit: In sectors, max size reads to include in cache [128]
1005 * @max_trace: Number of entries in block trace made during boot [8192]
1006 * @max_pages: Maximum number of pages to cache in memory [50000]
1009 * [<dev> [<cache_start> [<sig> [<size_limit> [<max_trace> [<max_limit>]]]]]]
1012 * 0f5dbd05-c063-a848-a296-b8b8c2c24b28+1 1741200 10e8...78 80 64000 60000
1014 static int bootcache_ctr(struct dm_target *ti, unsigned argc, char **argv)
1016 struct bootcache *cache = NULL;
1017 const char *signature = NULL;
1018 const char *device = NULL;
1019 u64 cache_start = 0;
1020 u64 max_pages = DEFAULT_MAX_PAGES;
1021 u64 size_limit = DEFAULT_SIZE_LIMIT;
1022 u64 max_trace = DEFAULT_MAX_TRACE;
1028 if (strict_strtoull(argv[1], 10, &cache_start)) {
1029 ti->error = "Invalid cache_start";
1033 signature = argv[2];
1035 if (strict_strtoull(argv[3], 10, &size_limit)) {
1036 ti->error = "Invalid size_limit";
1040 if (strict_strtoull(argv[4], 10, &max_trace)) {
1041 ti->error = "Invalid max_trace";
1045 if (strict_strtoull(argv[5], 10, &max_pages)) {
1046 ti->error = "Invalid max_pages";
1050 #define NEEDARG(n) \
1052 ti->error = "Missing argument: " #n; \
1058 NEEDARG(cache_start);
1061 if ((dm_table_get_mode(ti->table) & DEV_MODE) != DEV_MODE) {
1062 ti->error = "Must be created read only.";
1066 cache = kzalloc(sizeof(*cache), GFP_KERNEL);
1069 init_completion(&cache->init_complete);
1072 cache->args.device = device;
1073 cache->args.signature = signature;
1074 cache->args.cache_start = cache_start;
1075 cache->args.max_pages = max_pages;
1076 cache->args.size_limit = size_limit;
1077 if (max_trace > MAX_TRACE) {
1078 DMWARN("max_trace too large %llu, setting to %d\n",
1079 max_trace, MAX_TRACE);
1080 max_trace = MAX_TRACE;
1082 cache->args.max_trace = max_trace;
1084 cache->begin = ti->begin;
1085 cache->len = ti->len;
1087 atomic_set(&cache->state, BC_INIT);
1088 kref_init(&cache->kref);
1089 mutex_init(&cache->cache_lock);
1090 spin_lock_init(&cache->trace_lock);
1092 /* For the name, use the device default with / changed to _ */
1093 cache->name = dm_disk(dm_table_get_md(ti->table))->disk_name;
1095 if (bootcache_init_sysfs(cache, ti))
1098 rc = bootcache_get_device(ti, device,
1099 ti->begin, ti->len, &cache->dev);
1101 DMERR("Failed to acquire device '%s': %d", device, rc);
1102 ti->error = "Device lookup failed";
1106 bootcache_init_hdr(&cache->hdr, cache_start,
1107 cache->dev->bdev, signature);
1108 cache->max_io = bootcache_max_io(cache, BIO_MAX_PAGES);
1110 /* Allocate the bioset used for request padding */
1111 cache->bio_set = bioset_create(cache->max_io * 4, 0);
1112 if (!cache->bio_set) {
1113 ti->error = "Cannot allocate verity bioset";
1117 ti->num_flush_requests = 1;
1118 ti->private = cache;
1121 char vdev[BDEVNAME_SIZE];
1122 bdevname(cache->dev->bdev, vdev);
1123 DMINFO("dev:%s", vdev);
1125 INIT_WORK(&cache->work.work, bootcache_start);
1126 schedule_work(&cache->work.work);
1128 DMINFO("cache:%p", cache);
1132 dm_put_device(ti, cache->dev);
1134 bootcache_remove_all_files(cache);
1136 kfree(cache); /* hash is not secret so no need to zero */
1141 static int bootcache_status(struct dm_target *ti, status_type_t type,
1142 char *result, uint maxlen)
1144 struct bootcache *cache = (struct bootcache *) ti->private;
1146 char vdev[BDEVNAME_SIZE];
1149 case STATUSTYPE_INFO:
1151 cache->stats.num_requests,
1152 cache->stats.num_hits,
1153 cache->stats.overlapped);
1156 case STATUSTYPE_TABLE:
1157 bdevname(cache->dev->bdev, vdev);
1158 DMEMIT("/dev/%s signature=%s cache_start=%llu max_pages=%llu"
1159 " size_limit=%llu max_trace=%llu\n",
1161 cache->args.signature,
1162 cache->args.cache_start,
1163 cache->args.max_pages,
1164 cache->args.size_limit,
1165 cache->args.max_trace);
1171 static void bootcache_dtr(struct dm_target *ti)
1173 struct bootcache *cache = (struct bootcache *)ti->private;
1175 DMDEBUG("Destroying bio set");
1176 bioset_free(cache->bio_set);
1178 DMDEBUG("Putting dev");
1179 dm_put_device(ti, cache->dev);
1181 DMDEBUG("Remove sysfs files");
1182 bootcache_remove_all_files(cache);
1184 DMDEBUG("Destroying config");
1188 static int bootcache_map(struct dm_target *ti, struct bio *bio,
1189 union map_info *map_context)
1191 bootcache_read(ti->private, bio);
1192 return DM_MAPIO_SUBMITTED;
1195 static int bootcache_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
1196 struct bio_vec *biovec, int max_size)
1198 struct bootcache *cache = ti->private;
1199 struct request_queue *q = bdev_get_queue(cache->dev->bdev);
1201 if (!q->merge_bvec_fn)
1204 bvm->bi_bdev = cache->dev->bdev;
1205 bvm->bi_sector = cache->begin +
1206 bvm->bi_sector - ti->begin;
1208 /* Optionally, this could just return 0 to stick to single pages. */
1209 return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
1212 static int bootcache_iterate_devices(struct dm_target *ti,
1213 iterate_devices_callout_fn fn, void *data)
1215 struct bootcache *cache = ti->private;
1217 return fn(ti, cache->dev, cache->begin, ti->len, data);
1220 static void bootcache_io_hints(struct dm_target *ti,
1221 struct queue_limits *limits)
1223 limits->logical_block_size = PAGE_SIZE;
1224 limits->physical_block_size = PAGE_SIZE;
1225 blk_limits_io_min(limits, PAGE_SIZE);
1228 static struct target_type bootcache_target = {
1229 .name = "bootcache",
1230 .version = {0, 1, 0},
1231 .module = THIS_MODULE,
1232 .ctr = bootcache_ctr,
1233 .dtr = bootcache_dtr,
1234 .map = bootcache_map,
1235 .merge = bootcache_merge,
1236 .status = bootcache_status,
1237 .iterate_devices = bootcache_iterate_devices,
1238 .io_hints = bootcache_io_hints,
1241 static int __init dm_bootcache_init(void)
1245 rc = dm_register_target(&bootcache_target);
1247 DMERR("register failed %d", rc);
1248 goto register_failed;
1251 DMINFO("version %u.%u.%u loaded", bootcache_target.version[0],
1252 bootcache_target.version[1], bootcache_target.version[2]);
1260 static void __exit dm_bootcache_exit(void)
1262 dm_unregister_target(&bootcache_target);
1265 module_init(dm_bootcache_init);
1266 module_exit(dm_bootcache_exit);
1268 MODULE_AUTHOR("Paul Taysom <taysom@chromium.org>");
1269 MODULE_DESCRIPTION(DM_NAME "read cache");
1270 MODULE_LICENSE("GPL");