Skip to content
Snippets Groups Projects
request.c 33 KiB
Newer Older
  • Learn to ignore specific revisions
  • Kent Overstreet's avatar
    Kent Overstreet committed
    			/* Also need to send a flush to the backing device */
    
    			struct bio *flush = bio_alloc_bioset(0, GFP_NOIO,
    							     dc->disk.bio_split);
    
    			flush->bi_rw	= WRITE_FLUSH;
    			flush->bi_bdev	= bio->bi_bdev;
    			flush->bi_end_io = request_endio;
    			flush->bi_private = cl;
    
    			closure_bio_submit(flush, cl, s->d);
    
    Kent Overstreet's avatar
    Kent Overstreet committed
    		} else {
    			s->op.cache_bio = bio;
    		}
    
    	}
    out:
    	closure_call(&s->op.cl, bch_insert_data, NULL, cl);
    	continue_at(cl, cached_dev_write_complete, NULL);
    skip:
    	s->op.skip = true;
    	s->op.cache_bio = s->orig_bio;
    	bio_get(s->op.cache_bio);
    
    	if ((bio->bi_rw & REQ_DISCARD) &&
    	    !blk_queue_discard(bdev_get_queue(dc->bdev)))
    		goto out;
    
    	closure_bio_submit(bio, cl, s->d);
    	goto out;
    }
    
    static void request_nodata(struct cached_dev *dc, struct search *s)
    {
    	struct closure *cl = &s->cl;
    	struct bio *bio = &s->bio.bio;
    
    	if (bio->bi_rw & REQ_DISCARD) {
    		request_write(dc, s);
    		return;
    	}
    
    	if (s->op.flush_journal)
    		bch_journal_meta(s->op.c, cl);
    
    	closure_bio_submit(bio, cl, s->d);
    
    	continue_at(cl, cached_dev_bio_complete, NULL);
    }
    
    /* Cached devices - read & write stuff */
    
    
    unsigned bch_get_congested(struct cache_set *c)
    
    	long rand;
    
    
    	if (!c->congested_read_threshold_us &&
    	    !c->congested_write_threshold_us)
    		return 0;
    
    	i = (local_clock_us() - c->congested_last_us) / 1024;
    	if (i < 0)
    		return 0;
    
    	i += atomic_read(&c->congested);
    	if (i >= 0)
    		return 0;
    
    	i += CONGESTED_MAX;
    
    
    	if (i > 0)
    		i = fract_exp_two(i, 6);
    
    	rand = get_random_int();
    	i -= bitmap_weight(&rand, BITS_PER_LONG);
    
    	return i > 0 ? i : 1;
    
    }
    
    static void add_sequential(struct task_struct *t)
    {
    	ewma_add(t->sequential_io_avg,
    		 t->sequential_io, 8, 0);
    
    	t->sequential_io = 0;
    }
    
    
    static struct hlist_head *iohash(struct cached_dev *dc, uint64_t k)
    
    	return &dc->io_hash[hash_64(k, RECENT_IO_BITS)];
    }
    
    static void check_should_skip(struct cached_dev *dc, struct search *s)
    {
    
    	struct cache_set *c = s->op.c;
    	struct bio *bio = &s->bio.bio;
    	unsigned mode = cache_mode(dc, bio);
    
    	unsigned sectors, congested = bch_get_congested(c);
    
    
    	if (atomic_read(&dc->disk.detaching) ||
    	    c->gc_stats.in_use > CUTOFF_CACHE_ADD ||
    	    (bio->bi_rw & REQ_DISCARD))
    		goto skip;
    
    	if (mode == CACHE_MODE_NONE ||
    	    (mode == CACHE_MODE_WRITEAROUND &&
    	     (bio->bi_rw & REQ_WRITE)))
    		goto skip;
    
    	if (bio->bi_sector   & (c->sb.block_size - 1) ||
    	    bio_sectors(bio) & (c->sb.block_size - 1)) {
    		pr_debug("skipping unaligned io");
    		goto skip;
    	}
    
    
    	if (!congested && !dc->sequential_cutoff)
    		goto rescale;
    
    	if (!congested &&
    	    mode == CACHE_MODE_WRITEBACK &&
    	    (bio->bi_rw & REQ_WRITE) &&
    	    (bio->bi_rw & REQ_SYNC))
    		goto rescale;
    
    
    	if (dc->sequential_merge) {
    		struct io *i;
    
    		spin_lock(&dc->io_lock);
    
    
    		hlist_for_each_entry(i, iohash(dc, bio->bi_sector), hash)
    
    			if (i->last == bio->bi_sector &&
    			    time_before(jiffies, i->jiffies))
    				goto found;
    
    		i = list_first_entry(&dc->io_lru, struct io, lru);
    
    		add_sequential(s->task);
    		i->sequential = 0;
    found:
    		if (i->sequential + bio->bi_size > i->sequential)
    			i->sequential	+= bio->bi_size;
    
    
    		i->last			 = bio_end_sector(bio);
    
    		i->jiffies		 = jiffies + msecs_to_jiffies(5000);
    		s->task->sequential_io	 = i->sequential;
    
    		hlist_del(&i->hash);
    
    		hlist_add_head(&i->hash, iohash(dc, i->last));
    
    		list_move_tail(&i->lru, &dc->io_lru);
    
    		spin_unlock(&dc->io_lock);
    	} else {
    		s->task->sequential_io = bio->bi_size;
    
    		add_sequential(s->task);
    	}
    
    
    	sectors = max(s->task->sequential_io,
    		      s->task->sequential_io_avg) >> 9;
    
    	if (dc->sequential_cutoff &&
    	    sectors >= dc->sequential_cutoff >> 9) {
    		trace_bcache_bypass_sequential(s->orig_bio);
    
    		goto skip;
    
    	}
    
    	if (congested && sectors >= congested) {
    		trace_bcache_bypass_congested(s->orig_bio);
    		goto skip;
    	}
    
    
    rescale:
    	bch_rescale_priorities(c, bio_sectors(bio));
    	return;
    skip:
    	bch_mark_sectors_bypassed(s, bio_sectors(bio));
    	s->op.skip = true;
    }
    
    static void cached_dev_make_request(struct request_queue *q, struct bio *bio)
    {
    	struct search *s;
    	struct bcache_device *d = bio->bi_bdev->bd_disk->private_data;
    	struct cached_dev *dc = container_of(d, struct cached_dev, disk);
    	int cpu, rw = bio_data_dir(bio);
    
    	cpu = part_stat_lock();
    	part_stat_inc(cpu, &d->disk->part0, ios[rw]);
    	part_stat_add(cpu, &d->disk->part0, sectors[rw], bio_sectors(bio));
    	part_stat_unlock();
    
    	bio->bi_bdev = dc->bdev;
    
    	bio->bi_sector += dc->sb.data_offset;
    
    
    	if (cached_dev_get(dc)) {
    		s = search_alloc(bio, d);
    		trace_bcache_request_start(s, bio);
    
    		if (!bio_has_data(bio))
    			request_nodata(dc, s);
    		else if (rw)
    			request_write(dc, s);
    		else
    			request_read(dc, s);
    	} else {
    		if ((bio->bi_rw & REQ_DISCARD) &&
    		    !blk_queue_discard(bdev_get_queue(dc->bdev)))
    			bio_endio(bio, 0);
    		else
    			bch_generic_make_request(bio, &d->bio_split_hook);
    	}
    }
    
    static int cached_dev_ioctl(struct bcache_device *d, fmode_t mode,
    			    unsigned int cmd, unsigned long arg)
    {
    	struct cached_dev *dc = container_of(d, struct cached_dev, disk);
    	return __blkdev_driver_ioctl(dc->bdev, mode, cmd, arg);
    }
    
    static int cached_dev_congested(void *data, int bits)
    {
    	struct bcache_device *d = data;
    	struct cached_dev *dc = container_of(d, struct cached_dev, disk);
    	struct request_queue *q = bdev_get_queue(dc->bdev);
    	int ret = 0;
    
    	if (bdi_congested(&q->backing_dev_info, bits))
    		return 1;
    
    	if (cached_dev_get(dc)) {
    		unsigned i;
    		struct cache *ca;
    
    		for_each_cache(ca, d->c, i) {
    			q = bdev_get_queue(ca->bdev);
    			ret |= bdi_congested(&q->backing_dev_info, bits);
    		}
    
    		cached_dev_put(dc);
    	}
    
    	return ret;
    }
    
    void bch_cached_dev_request_init(struct cached_dev *dc)
    {
    	struct gendisk *g = dc->disk.disk;
    
    	g->queue->make_request_fn		= cached_dev_make_request;
    	g->queue->backing_dev_info.congested_fn = cached_dev_congested;
    	dc->disk.cache_miss			= cached_dev_cache_miss;
    	dc->disk.ioctl				= cached_dev_ioctl;
    }
    
    /* Flash backed devices */
    
    static int flash_dev_cache_miss(struct btree *b, struct search *s,
    				struct bio *bio, unsigned sectors)
    {
    
    	struct bio_vec *bv;
    	int i;
    
    
    	/* Zero fill bio */
    
    
    	bio_for_each_segment(bv, bio, i) {
    
    		unsigned j = min(bv->bv_len >> 9, sectors);
    
    		void *p = kmap(bv->bv_page);
    		memset(p + bv->bv_offset, 0, j << 9);
    		kunmap(bv->bv_page);
    
    
    		sectors	-= j;
    
    	bio_advance(bio, min(sectors << 9, bio->bi_size));
    
    	if (!bio->bi_size)
    		s->op.lookup_done = true;
    
    
    	return 0;
    }
    
    static void flash_dev_make_request(struct request_queue *q, struct bio *bio)
    {
    	struct search *s;
    	struct closure *cl;
    	struct bcache_device *d = bio->bi_bdev->bd_disk->private_data;
    	int cpu, rw = bio_data_dir(bio);
    
    	cpu = part_stat_lock();
    	part_stat_inc(cpu, &d->disk->part0, ios[rw]);
    	part_stat_add(cpu, &d->disk->part0, sectors[rw], bio_sectors(bio));
    	part_stat_unlock();
    
    	s = search_alloc(bio, d);
    	cl = &s->cl;
    	bio = &s->bio.bio;
    
    	trace_bcache_request_start(s, bio);
    
    	if (bio_has_data(bio) && !rw) {
    		closure_call(&s->op.cl, btree_read_async, NULL, cl);
    	} else if (bio_has_data(bio) || s->op.skip) {
    		bch_keybuf_check_overlapping(&s->op.c->moving_gc_keys,
    
    					&KEY(d->id, bio->bi_sector, 0),
    					&KEY(d->id, bio_end_sector(bio), 0));
    
    
    		s->writeback	= true;
    		s->op.cache_bio	= bio;
    
    		closure_call(&s->op.cl, bch_insert_data, NULL, cl);
    	} else {
    		/* No data - probably a cache flush */
    		if (s->op.flush_journal)
    			bch_journal_meta(s->op.c, cl);
    	}
    
    	continue_at(cl, search_free, NULL);
    }
    
    static int flash_dev_ioctl(struct bcache_device *d, fmode_t mode,
    			   unsigned int cmd, unsigned long arg)
    {
    	return -ENOTTY;
    }
    
    static int flash_dev_congested(void *data, int bits)
    {
    	struct bcache_device *d = data;
    	struct request_queue *q;
    	struct cache *ca;
    	unsigned i;
    	int ret = 0;
    
    	for_each_cache(ca, d->c, i) {
    		q = bdev_get_queue(ca->bdev);
    		ret |= bdi_congested(&q->backing_dev_info, bits);
    	}
    
    	return ret;
    }
    
    void bch_flash_dev_request_init(struct bcache_device *d)
    {
    	struct gendisk *g = d->disk;
    
    	g->queue->make_request_fn		= flash_dev_make_request;
    	g->queue->backing_dev_info.congested_fn = flash_dev_congested;
    	d->cache_miss				= flash_dev_cache_miss;
    	d->ioctl				= flash_dev_ioctl;
    }
    
    void bch_request_exit(void)
    {
    #ifdef CONFIG_CGROUP_BCACHE
    	cgroup_unload_subsys(&bcache_subsys);
    #endif
    	if (bch_search_cache)
    		kmem_cache_destroy(bch_search_cache);
    }
    
    int __init bch_request_init(void)
    {
    	bch_search_cache = KMEM_CACHE(search, 0);
    	if (!bch_search_cache)
    		return -ENOMEM;
    
    #ifdef CONFIG_CGROUP_BCACHE
    	cgroup_load_subsys(&bcache_subsys);
    	init_bch_cgroup(&bcache_default_cgroup);
    
    	cgroup_add_cftypes(&bcache_subsys, bch_files);
    #endif
    	return 0;
    }