1452 lines · 30.7 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0-only/* * Copyright (C) 2015 IT University of Copenhagen. All rights reserved. * Initial release: Matias Bjorling <m@bjorling.me> */ #define pr_fmt(fmt) "nvm: " fmt #include <linux/list.h>#include <linux/types.h>#include <linux/sem.h>#include <linux/bitmap.h>#include <linux/module.h>#include <linux/moduleparam.h>#include <linux/miscdevice.h>#include <linux/lightnvm.h>#include <linux/sched/sysctl.h> static LIST_HEAD(nvm_tgt_types);static DECLARE_RWSEM(nvm_tgtt_lock);static LIST_HEAD(nvm_devices);static DECLARE_RWSEM(nvm_lock); /* Map between virtual and physical channel and lun */struct nvm_ch_map {	int ch_off;	int num_lun;	int *lun_offs;}; struct nvm_dev_map {	struct nvm_ch_map *chnls;	int num_ch;}; static void nvm_free(struct kref *ref); static struct nvm_target *nvm_find_target(struct nvm_dev *dev, const char *name){	struct nvm_target *tgt; 	list_for_each_entry(tgt, &dev->targets, list)		if (!strcmp(name, tgt->disk->disk_name))			return tgt; 	return NULL;} static bool nvm_target_exists(const char *name){	struct nvm_dev *dev;	struct nvm_target *tgt;	bool ret = false; 	down_write(&nvm_lock);	list_for_each_entry(dev, &nvm_devices, devices) {		mutex_lock(&dev->mlock);		list_for_each_entry(tgt, &dev->targets, list) {			if (!strcmp(name, tgt->disk->disk_name)) {				ret = true;				mutex_unlock(&dev->mlock);				goto out;			}		}		mutex_unlock(&dev->mlock);	} out:	up_write(&nvm_lock);	return ret;} static int nvm_reserve_luns(struct nvm_dev *dev, int lun_begin, int lun_end){	int i; 	for (i = lun_begin; i <= lun_end; i++) {		if (test_and_set_bit(i, dev->lun_map)) {			pr_err("lun %d already allocated\n", i);			goto err;		}	} 	return 0;err:	while (--i >= lun_begin)		clear_bit(i, dev->lun_map); 	return -EBUSY;} static void nvm_release_luns_err(struct nvm_dev *dev, int lun_begin,				 int lun_end){	int i; 	for (i = lun_begin; i <= lun_end; i++)		WARN_ON(!test_and_clear_bit(i, dev->lun_map));} static void nvm_remove_tgt_dev(struct nvm_tgt_dev *tgt_dev, int clear){	struct nvm_dev *dev = tgt_dev->parent;	struct nvm_dev_map *dev_map = tgt_dev->map;	int i, j; 	for (i = 0; i < dev_map->num_ch; i++) {		struct nvm_ch_map *ch_map = &dev_map->chnls[i];		int *lun_offs = ch_map->lun_offs;		int ch = i + ch_map->ch_off; 		if (clear) {			for (j = 0; j < ch_map->num_lun; j++) {				int lun = j + lun_offs[j];				int lunid = (ch * dev->geo.num_lun) + lun; 				WARN_ON(!test_and_clear_bit(lunid,							dev->lun_map));			}		} 		kfree(ch_map->lun_offs);	} 	kfree(dev_map->chnls);	kfree(dev_map); 	kfree(tgt_dev->luns);	kfree(tgt_dev);} static struct nvm_tgt_dev *nvm_create_tgt_dev(struct nvm_dev *dev,					      u16 lun_begin, u16 lun_end,					      u16 op){	struct nvm_tgt_dev *tgt_dev = NULL;	struct nvm_dev_map *dev_rmap = dev->rmap;	struct nvm_dev_map *dev_map;	struct ppa_addr *luns;	int num_lun = lun_end - lun_begin + 1;	int luns_left = num_lun;	int num_ch = num_lun / dev->geo.num_lun;	int num_ch_mod = num_lun % dev->geo.num_lun;	int bch = lun_begin / dev->geo.num_lun;	int blun = lun_begin % dev->geo.num_lun;	int lunid = 0;	int lun_balanced = 1;	int sec_per_lun, prev_num_lun;	int i, j; 	num_ch = (num_ch_mod == 0) ? num_ch : num_ch + 1; 	dev_map = kmalloc(sizeof(struct nvm_dev_map), GFP_KERNEL);	if (!dev_map)		goto err_dev; 	dev_map->chnls = kcalloc(num_ch, sizeof(struct nvm_ch_map), GFP_KERNEL);	if (!dev_map->chnls)		goto err_chnls; 	luns = kcalloc(num_lun, sizeof(struct ppa_addr), GFP_KERNEL);	if (!luns)		goto err_luns; 	prev_num_lun = (luns_left > dev->geo.num_lun) ?					dev->geo.num_lun : luns_left;	for (i = 0; i < num_ch; i++) {		struct nvm_ch_map *ch_rmap = &dev_rmap->chnls[i + bch];		int *lun_roffs = ch_rmap->lun_offs;		struct nvm_ch_map *ch_map = &dev_map->chnls[i];		int *lun_offs;		int luns_in_chnl = (luns_left > dev->geo.num_lun) ?					dev->geo.num_lun : luns_left; 		if (lun_balanced && prev_num_lun != luns_in_chnl)			lun_balanced = 0; 		ch_map->ch_off = ch_rmap->ch_off = bch;		ch_map->num_lun = luns_in_chnl; 		lun_offs = kcalloc(luns_in_chnl, sizeof(int), GFP_KERNEL);		if (!lun_offs)			goto err_ch; 		for (j = 0; j < luns_in_chnl; j++) {			luns[lunid].ppa = 0;			luns[lunid].a.ch = i;			luns[lunid++].a.lun = j; 			lun_offs[j] = blun;			lun_roffs[j + blun] = blun;		} 		ch_map->lun_offs = lun_offs; 		/* when starting a new channel, lun offset is reset */		blun = 0;		luns_left -= luns_in_chnl;	} 	dev_map->num_ch = num_ch; 	tgt_dev = kmalloc(sizeof(struct nvm_tgt_dev), GFP_KERNEL);	if (!tgt_dev)		goto err_ch; 	/* Inherit device geometry from parent */	memcpy(&tgt_dev->geo, &dev->geo, sizeof(struct nvm_geo)); 	/* Target device only owns a portion of the physical device */	tgt_dev->geo.num_ch = num_ch;	tgt_dev->geo.num_lun = (lun_balanced) ? prev_num_lun : -1;	tgt_dev->geo.all_luns = num_lun;	tgt_dev->geo.all_chunks = num_lun * dev->geo.num_chk; 	tgt_dev->geo.op = op; 	sec_per_lun = dev->geo.clba * dev->geo.num_chk;	tgt_dev->geo.total_secs = num_lun * sec_per_lun; 	tgt_dev->q = dev->q;	tgt_dev->map = dev_map;	tgt_dev->luns = luns;	tgt_dev->parent = dev; 	return tgt_dev;err_ch:	while (--i >= 0)		kfree(dev_map->chnls[i].lun_offs);	kfree(luns);err_luns:	kfree(dev_map->chnls);err_chnls:	kfree(dev_map);err_dev:	return tgt_dev;} static struct nvm_tgt_type *__nvm_find_target_type(const char *name){	struct nvm_tgt_type *tt; 	list_for_each_entry(tt, &nvm_tgt_types, list)		if (!strcmp(name, tt->name))			return tt; 	return NULL;} static struct nvm_tgt_type *nvm_find_target_type(const char *name){	struct nvm_tgt_type *tt; 	down_write(&nvm_tgtt_lock);	tt = __nvm_find_target_type(name);	up_write(&nvm_tgtt_lock); 	return tt;} static int nvm_config_check_luns(struct nvm_geo *geo, int lun_begin,				 int lun_end){	if (lun_begin > lun_end || lun_end >= geo->all_luns) {		pr_err("lun out of bound (%u:%u > %u)\n",			lun_begin, lun_end, geo->all_luns - 1);		return -EINVAL;	} 	return 0;} static int __nvm_config_simple(struct nvm_dev *dev,			       struct nvm_ioctl_create_simple *s){	struct nvm_geo *geo = &dev->geo; 	if (s->lun_begin == -1 && s->lun_end == -1) {		s->lun_begin = 0;		s->lun_end = geo->all_luns - 1;	} 	return nvm_config_check_luns(geo, s->lun_begin, s->lun_end);} static int __nvm_config_extended(struct nvm_dev *dev,				 struct nvm_ioctl_create_extended *e){	if (e->lun_begin == 0xFFFF && e->lun_end == 0xFFFF) {		e->lun_begin = 0;		e->lun_end = dev->geo.all_luns - 1;	} 	/* op not set falls into target's default */	if (e->op == 0xFFFF) {		e->op = NVM_TARGET_DEFAULT_OP;	} else if (e->op < NVM_TARGET_MIN_OP || e->op > NVM_TARGET_MAX_OP) {		pr_err("invalid over provisioning value\n");		return -EINVAL;	} 	return nvm_config_check_luns(&dev->geo, e->lun_begin, e->lun_end);} static int nvm_create_tgt(struct nvm_dev *dev, struct nvm_ioctl_create *create){	struct nvm_ioctl_create_extended e;	struct request_queue *tqueue;	struct gendisk *tdisk;	struct nvm_tgt_type *tt;	struct nvm_target *t;	struct nvm_tgt_dev *tgt_dev;	void *targetdata;	unsigned int mdts;	int ret; 	switch (create->conf.type) {	case NVM_CONFIG_TYPE_SIMPLE:		ret = __nvm_config_simple(dev, &create->conf.s);		if (ret)			return ret; 		e.lun_begin = create->conf.s.lun_begin;		e.lun_end = create->conf.s.lun_end;		e.op = NVM_TARGET_DEFAULT_OP;		break;	case NVM_CONFIG_TYPE_EXTENDED:		ret = __nvm_config_extended(dev, &create->conf.e);		if (ret)			return ret; 		e = create->conf.e;		break;	default:		pr_err("config type not valid\n");		return -EINVAL;	} 	tt = nvm_find_target_type(create->tgttype);	if (!tt) {		pr_err("target type %s not found\n", create->tgttype);		return -EINVAL;	} 	if ((tt->flags & NVM_TGT_F_HOST_L2P) != (dev->geo.dom & NVM_RSP_L2P)) {		pr_err("device is incompatible with target L2P type.\n");		return -EINVAL;	} 	if (nvm_target_exists(create->tgtname)) {		pr_err("target name already exists (%s)\n",							create->tgtname);		return -EINVAL;	} 	ret = nvm_reserve_luns(dev, e.lun_begin, e.lun_end);	if (ret)		return ret; 	t = kmalloc(sizeof(struct nvm_target), GFP_KERNEL);	if (!t) {		ret = -ENOMEM;		goto err_reserve;	} 	tgt_dev = nvm_create_tgt_dev(dev, e.lun_begin, e.lun_end, e.op);	if (!tgt_dev) {		pr_err("could not create target device\n");		ret = -ENOMEM;		goto err_t;	} 	tdisk = alloc_disk(0);	if (!tdisk) {		ret = -ENOMEM;		goto err_dev;	} 	tqueue = blk_alloc_queue(dev->q->node);	if (!tqueue) {		ret = -ENOMEM;		goto err_disk;	} 	strlcpy(tdisk->disk_name, create->tgtname, sizeof(tdisk->disk_name));	tdisk->flags = GENHD_FL_EXT_DEVT;	tdisk->major = 0;	tdisk->first_minor = 0;	tdisk->fops = tt->bops;	tdisk->queue = tqueue; 	targetdata = tt->init(tgt_dev, tdisk, create->flags);	if (IS_ERR(targetdata)) {		ret = PTR_ERR(targetdata);		goto err_init;	} 	tdisk->private_data = targetdata;	tqueue->queuedata = targetdata; 	mdts = (dev->geo.csecs >> 9) * NVM_MAX_VLBA;	if (dev->geo.mdts) {		mdts = min_t(u32, dev->geo.mdts,				(dev->geo.csecs >> 9) * NVM_MAX_VLBA);	}	blk_queue_max_hw_sectors(tqueue, mdts); 	set_capacity(tdisk, tt->capacity(targetdata));	add_disk(tdisk); 	if (tt->sysfs_init && tt->sysfs_init(tdisk)) {		ret = -ENOMEM;		goto err_sysfs;	} 	t->type = tt;	t->disk = tdisk;	t->dev = tgt_dev; 	mutex_lock(&dev->mlock);	list_add_tail(&t->list, &dev->targets);	mutex_unlock(&dev->mlock); 	__module_get(tt->owner); 	return 0;err_sysfs:	if (tt->exit)		tt->exit(targetdata, true);err_init:	blk_cleanup_queue(tqueue);	tdisk->queue = NULL;err_disk:	put_disk(tdisk);err_dev:	nvm_remove_tgt_dev(tgt_dev, 0);err_t:	kfree(t);err_reserve:	nvm_release_luns_err(dev, e.lun_begin, e.lun_end);	return ret;} static void __nvm_remove_target(struct nvm_target *t, bool graceful){	struct nvm_tgt_type *tt = t->type;	struct gendisk *tdisk = t->disk;	struct request_queue *q = tdisk->queue; 	del_gendisk(tdisk);	blk_cleanup_queue(q); 	if (tt->sysfs_exit)		tt->sysfs_exit(tdisk); 	if (tt->exit)		tt->exit(tdisk->private_data, graceful); 	nvm_remove_tgt_dev(t->dev, 1);	put_disk(tdisk);	module_put(t->type->owner); 	list_del(&t->list);	kfree(t);} /** * nvm_remove_tgt - Removes a target from the media manager * @remove:	ioctl structure with target name to remove. * * Returns: * 0: on success * 1: on not found * <0: on error */static int nvm_remove_tgt(struct nvm_ioctl_remove *remove){	struct nvm_target *t = NULL;	struct nvm_dev *dev; 	down_read(&nvm_lock);	list_for_each_entry(dev, &nvm_devices, devices) {		mutex_lock(&dev->mlock);		t = nvm_find_target(dev, remove->tgtname);		if (t) {			mutex_unlock(&dev->mlock);			break;		}		mutex_unlock(&dev->mlock);	}	up_read(&nvm_lock); 	if (!t) {		pr_err("failed to remove target %s\n",				remove->tgtname);		return 1;	} 	__nvm_remove_target(t, true);	kref_put(&dev->ref, nvm_free);

Lines 1–500 of 1452.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t