pax_global_header00006660000000000000000000000064120224165040014506gustar00rootroot0000000000000052 comment=f9b8981ee131e5eb92fa5e5357ee33c8ef510b8d blktap-dkms-2.0.93/000077500000000000000000000000001202241650400140125ustar00rootroot00000000000000blktap-dkms-2.0.93/COPYING000066400000000000000000000431341202241650400150520ustar00rootroot00000000000000 GNU GENERAL PUBLIC LICENSE Version 2, June 1991 Copyright (C) 1989, 1991 Free Software Foundation, Inc. 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This General Public License applies to most of the Free Software Foundation's software and to any other program whose authors commit to using it. (Some other Free Software Foundation software is covered by the GNU Library General Public License instead.) You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for this service if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. Finally, any free program is threatened constantly by software patents. We wish to avoid the danger that redistributors of a free program will individually obtain patent licenses, in effect making the program proprietary. To prevent this, we have made it clear that any patent must be licensed for everyone's free use or not licensed at all. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any derivative work under copyright law: that is to say, a work containing the Program or a portion of it, either verbatim or with modifications and/or translated into another language. (Hereinafter, translation is included without limitation in the term "modification".) Each licensee is addressed as "you". Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running the Program is not restricted, and the output from the Program is covered only if its contents constitute a work based on the Program (independent of having been made by running the Program). Whether that is true depends on what the Program does. 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and give any other recipients of the Program a copy of this License along with the Program. You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. 2. You may modify your copy or copies of the Program or any portion of it, thus forming a work based on the Program, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions: a) You must cause the modified files to carry prominent notices stating that you changed the files and the date of any change. b) You must cause any work that you distribute or publish, that in whole or in part contains or is derived from the Program or any part thereof, to be licensed as a whole at no charge to all third parties under the terms of this License. c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the most ordinary way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this License. (Exception: if the Program itself is interactive but does not normally print such an announcement, your work based on the Program is not required to print an announcement.) These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Program, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Program, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Program. In addition, mere aggregation of another work not based on the Program with the Program (or with a work based on the Program) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. 3. You may copy and distribute the Program (or a work based on it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you also do one of the following: a) Accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, b) Accompany it with a written offer, valid for at least three years, to give any third party, for a charge no more than your cost of physically performing source distribution, a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, c) Accompany it with the information you received as to the offer to distribute corresponding source code. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form with such an offer, in accord with Subsection b above.) The source code for a work means the preferred form of the work for making modifications to it. For an executable work, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the executable. However, as a special exception, the source code distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable. If distribution of executable or object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place counts as distribution of the source code, even though third parties are not compelled to copy the source along with the object code. 4. You may not copy, modify, sublicense, or distribute the Program except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense or distribute the Program is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. 5. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Program or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Program (or any work based on the Program), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Program or works based on it. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties to this License. 7. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Program at all. For example, if a patent license would not permit royalty-free redistribution of the Program by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Program. If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply and the section as a whole is intended to apply in other circumstances. It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system, which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 8. If the distribution and/or use of the Program is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Program under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. 9. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of this License, you may choose any version ever published by the Free Software Foundation. 10. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) year name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here is a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (which makes passes at compilers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice This General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Library General Public License instead of this License. blktap-dkms-2.0.93/Makefile000066400000000000000000000001161202241650400154500ustar00rootroot00000000000000obj-m := blktap.o blktap-objs := control.o ring.o device.o request.o sysfs.o blktap-dkms-2.0.93/README000066400000000000000000000002241202241650400146700ustar00rootroot00000000000000The kernel module for blktap2 To compile: make -C /lib/modules/`uname -r`/build M=$PWD Tested on debian sid, with linux-image-3.0.0-rc3-686-pae blktap-dkms-2.0.93/blktap.h000066400000000000000000000137401202241650400154450ustar00rootroot00000000000000/* * * Copyright (C) 2011 Citrix Systems Inc. * * This file is part of Blktap2. * * Blktap2 is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 * as published by the Free Software Foundation. * * Blktap2 is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License version 2 for more details. * * You should have received a copy of the GNU General Public License * version 2 along with Blktap2. If not, see * . * * */ #ifndef _BLKTAP_H_ #define _BLKTAP_H_ #include #include #include #include #include #include #include #include "linux-blktap.h" extern int blktap_debug_level; extern int blktap_ring_major; extern int blktap_device_major; #define BTPRINTK(level, tag, force, _f, _a...) \ do { \ if (blktap_debug_level > level && \ (force || printk_ratelimit())) \ printk(tag "%s: " _f, __func__, ##_a); \ } while (0) #define BTDBG(_f, _a...) BTPRINTK(8, KERN_DEBUG, 1, _f, ##_a) #define BTINFO(_f, _a...) BTPRINTK(0, KERN_INFO, 0, _f, ##_a) #define BTWARN(_f, _a...) BTPRINTK(0, KERN_WARNING, 0, _f, ##_a) #define BTERR(_f, _a...) BTPRINTK(0, KERN_ERR, 0, _f, ##_a) #define MAX_BLKTAP_DEVICE 1024 #define BLKTAP_DEVICE 4 #define BLKTAP_DEVICE_CLOSED 5 #define BLKTAP_SHUTDOWN_REQUESTED 8 #define BLKTAP_REQUEST_FREE 0 #define BLKTAP_REQUEST_PENDING 1 struct blktap_device { spinlock_t lock; struct gendisk *gd; }; struct blktap_request; struct blktap_ring { struct task_struct *task; struct vm_area_struct *vma; struct mutex vma_lock; struct blktap_front_ring ring; unsigned long ring_vstart; unsigned long user_vstart; int n_pending; struct blktap_request *pending[BLKTAP_RING_SIZE]; wait_queue_head_t poll_wait; dev_t devno; struct device *dev; }; struct blktap_statistics { unsigned long st_print; int st_rd_req; int st_wr_req; int st_tr_req; int st_oo_req; int st_fl_req; int st_rd_sect; int st_wr_sect; int st_tr_sect; s64 st_rd_cnt; s64 st_rd_sum_usecs; s64 st_rd_max_usecs; s64 st_wr_cnt; s64 st_wr_sum_usecs; s64 st_wr_max_usecs; }; struct blktap_request { struct blktap *tap; struct request *rq; int usr_idx; int operation; struct scatterlist sg_table[BLKTAP_SEGMENT_MAX]; struct page *pages[BLKTAP_SEGMENT_MAX]; int nr_pages; }; #define blktap_for_each_sg(_sg, _req, _i) \ for (_sg = (_req)->sg_table, _i = 0; \ _i < (_req)->nr_pages; \ (_sg)++, (_i)++) struct blktap { int minor; unsigned long dev_inuse; struct blktap_ring ring; struct blktap_device device; struct blktap_page_pool *pool; wait_queue_head_t remove_wait; struct work_struct remove_work; char name[BLKTAP_NAME_MAX]; struct blktap_statistics stats; }; struct blktap_page_pool { struct mempool_s *bufs; spinlock_t lock; struct kobject kobj; wait_queue_head_t wait; }; extern struct mutex blktap_lock; extern struct blktap **blktaps; extern int blktap_max_minor; int blktap_control_destroy_tap(struct blktap *); size_t blktap_control_debug(struct blktap *, char *, size_t); int blktap_ring_init(void); void blktap_ring_exit(void); size_t blktap_ring_debug(struct blktap *, char *, size_t); int blktap_ring_create(struct blktap *); int blktap_ring_destroy(struct blktap *); struct blktap_request *blktap_ring_make_request(struct blktap *); void blktap_ring_free_request(struct blktap *,struct blktap_request *); void blktap_ring_submit_request(struct blktap *, struct blktap_request *); int blktap_ring_map_request(struct blktap *, struct file *, struct blktap_request *); void blktap_ring_unmap_request(struct blktap *, struct blktap_request *); void blktap_ring_set_message(struct blktap *, int); void blktap_ring_kick_user(struct blktap *); int blktap_sysfs_init(void); void blktap_sysfs_exit(void); int blktap_sysfs_create(struct blktap *); void blktap_sysfs_destroy(struct blktap *); int blktap_device_init(void); void blktap_device_exit(void); size_t blktap_device_debug(struct blktap *, char *, size_t); int blktap_device_create(struct blktap *, struct blktap_device_info *); int blktap_device_destroy(struct blktap *); void blktap_device_destroy_sync(struct blktap *); void blktap_device_run_queue(struct blktap *, struct file *); void blktap_device_end_request(struct blktap *, struct blktap_request *, int); int blktap_page_pool_init(struct kobject *); void blktap_page_pool_exit(void); struct blktap_page_pool *blktap_page_pool_get(const char *); size_t blktap_request_debug(struct blktap *, char *, size_t); struct blktap_request *blktap_request_alloc(struct blktap *); int blktap_request_get_pages(struct blktap *, struct blktap_request *, int); void blktap_request_free(struct blktap *, struct blktap_request *); void blktap_request_bounce(struct blktap *, struct blktap_request *, int); #endif blktap-dkms-2.0.93/control.c000066400000000000000000000141241202241650400156400ustar00rootroot00000000000000/* * * Copyright (C) 2011 Citrix Systems Inc. * * This file is part of Blktap2. * * Blktap2 is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 * as published by the Free Software Foundation. * * Blktap2 is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License version 2 for more details. * * You should have received a copy of the GNU General Public License * version 2 along with Blktap2. If not, see * . * * */ #include #include #include #include #include #include #include "blktap.h" DEFINE_MUTEX(blktap_lock); struct blktap **blktaps; int blktap_max_minor; static struct blktap_page_pool *default_pool; static struct blktap * blktap_control_get_minor(void) { int minor; struct blktap *tap; tap = kzalloc(sizeof(*tap), GFP_KERNEL); if (unlikely(!tap)) return NULL; mutex_lock(&blktap_lock); for (minor = 0; minor < blktap_max_minor; minor++) if (!blktaps[minor]) break; if (minor == MAX_BLKTAP_DEVICE) goto fail; if (minor == blktap_max_minor) { void *p; int n; n = min(2 * blktap_max_minor, MAX_BLKTAP_DEVICE); p = krealloc(blktaps, n * sizeof(blktaps[0]), GFP_KERNEL); if (!p) goto fail; blktaps = p; minor = blktap_max_minor; blktap_max_minor = n; memset(&blktaps[minor], 0, (n - minor) * sizeof(blktaps[0])); } tap->minor = minor; blktaps[minor] = tap; __module_get(THIS_MODULE); out: mutex_unlock(&blktap_lock); return tap; fail: mutex_unlock(&blktap_lock); kfree(tap); tap = NULL; goto out; } static void blktap_control_put_minor(struct blktap* tap) { blktaps[tap->minor] = NULL; kfree(tap); module_put(THIS_MODULE); } static struct blktap* blktap_control_create_tap(void) { struct blktap *tap; int err; tap = blktap_control_get_minor(); if (!tap) return NULL; kobject_get(&default_pool->kobj); tap->pool = default_pool; err = blktap_ring_create(tap); if (err) goto fail_tap; err = blktap_sysfs_create(tap); if (err) goto fail_ring; return tap; fail_ring: blktap_ring_destroy(tap); fail_tap: blktap_control_put_minor(tap); return NULL; } int blktap_control_destroy_tap(struct blktap *tap) { int err; err = blktap_ring_destroy(tap); if (err) return err; kobject_put(&tap->pool->kobj); blktap_sysfs_destroy(tap); blktap_control_put_minor(tap); return 0; } static long blktap_control_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) { struct blktap *tap; switch (cmd) { case BLKTAP_IOCTL_ALLOC_TAP: { struct blktap_info info; void __user *ptr = (void __user*)arg; tap = blktap_control_create_tap(); if (!tap) return -ENOMEM; info.ring_major = blktap_ring_major; info.bdev_major = blktap_device_major; info.ring_minor = tap->minor; if (copy_to_user(ptr, &info, sizeof(info))) { blktap_control_destroy_tap(tap); return -EFAULT; } return 0; } case BLKTAP_IOCTL_FREE_TAP: { int minor = arg; if (minor > MAX_BLKTAP_DEVICE) return -EINVAL; tap = blktaps[minor]; if (!tap) return -ENODEV; return blktap_control_destroy_tap(tap); } } return -ENOIOCTLCMD; } static struct file_operations blktap_control_file_operations = { .owner = THIS_MODULE, .unlocked_ioctl = blktap_control_ioctl, }; static struct miscdevice blktap_control = { .minor = MISC_DYNAMIC_MINOR, .name = "blktap-control", .fops = &blktap_control_file_operations, }; static struct device *control_device; static ssize_t blktap_control_show_default_pool(struct device *device, struct device_attribute *attr, char *buf) { return sprintf(buf, "%s", kobject_name(&default_pool->kobj)); } static ssize_t blktap_control_store_default_pool(struct device *device, struct device_attribute *attr, const char *buf, size_t size) { struct blktap_page_pool *pool, *tmp = default_pool; pool = blktap_page_pool_get(buf); if (IS_ERR(pool)) return PTR_ERR(pool); default_pool = pool; kobject_put(&tmp->kobj); return size; } static DEVICE_ATTR(default_pool, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, blktap_control_show_default_pool, blktap_control_store_default_pool); size_t blktap_control_debug(struct blktap *tap, char *buf, size_t size) { char *s = buf, *end = buf + size; s += snprintf(s, end - s, "tap %u:%u name:'%s' flags:%#08lx\n", MAJOR(tap->ring.devno), MINOR(tap->ring.devno), tap->name, tap->dev_inuse); return s - buf; } static int __init blktap_control_init(void) { int err; err = misc_register(&blktap_control); if (err) return err; control_device = blktap_control.this_device; blktap_max_minor = min(64, MAX_BLKTAP_DEVICE); blktaps = kzalloc(blktap_max_minor * sizeof(blktaps[0]), GFP_KERNEL); if (!blktaps) { BTERR("failed to allocate blktap minor map"); return -ENOMEM; } err = blktap_page_pool_init(&control_device->kobj); if (err) return err; default_pool = blktap_page_pool_get("default"); if (!default_pool) return -ENOMEM; err = device_create_file(control_device, &dev_attr_default_pool); if (err) return err; return 0; } static void blktap_control_exit(void) { if (default_pool) { kobject_put(&default_pool->kobj); default_pool = NULL; } blktap_page_pool_exit(); if (blktaps) { kfree(blktaps); blktaps = NULL; } if (control_device) { misc_deregister(&blktap_control); control_device = NULL; } } static void blktap_exit(void) { blktap_control_exit(); blktap_ring_exit(); blktap_sysfs_exit(); blktap_device_exit(); } static int __init blktap_init(void) { int err; err = blktap_device_init(); if (err) goto fail; err = blktap_ring_init(); if (err) goto fail; err = blktap_sysfs_init(); if (err) goto fail; err = blktap_control_init(); if (err) goto fail; return 0; fail: blktap_exit(); return err; } module_init(blktap_init); module_exit(blktap_exit); MODULE_LICENSE("Dual BSD/GPL"); blktap-dkms-2.0.93/device.c000066400000000000000000000333521202241650400154230ustar00rootroot00000000000000/* * * Copyright (C) 2011 Citrix Systems Inc. * * This file is part of Blktap2. * * Blktap2 is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 * as published by the Free Software Foundation. * * Blktap2 is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License version 2 for more details. * * You should have received a copy of the GNU General Public License * version 2 along with Blktap2. If not, see * . * * */ #include #include #include #include #include #include #include #include "blktap.h" int blktap_device_major; #define dev_to_blktap(_dev) container_of(_dev, struct blktap, device) static int blktap_device_open(struct block_device *bdev, fmode_t mode) { struct gendisk *disk = bdev->bd_disk; struct blktap_device *tapdev = disk->private_data; if (!tapdev) return -ENXIO; /* NB. we might have bounced a bd trylock by tapdisk. when * failing for reasons not !tapdev, make sure to kick tapdisk * out of destroy wait state again. */ return 0; } static int blktap_device_release(struct gendisk *disk, fmode_t mode) { struct blktap_device *tapdev = disk->private_data; struct block_device *bdev = bdget_disk(disk, 0); struct blktap *tap = dev_to_blktap(tapdev); bdput(bdev); if (!bdev->bd_openers) { set_bit(BLKTAP_DEVICE_CLOSED, &tap->dev_inuse); blktap_ring_kick_user(tap); } return 0; } static int blktap_device_getgeo(struct block_device *bd, struct hd_geometry *hg) { /* We don't have real geometry info, but let's at least return values consistent with the size of the device */ sector_t nsect = get_capacity(bd->bd_disk); sector_t cylinders = nsect; hg->heads = 0xff; hg->sectors = 0x3f; sector_div(cylinders, hg->heads * hg->sectors); hg->cylinders = cylinders; if ((sector_t)(hg->cylinders + 1) * hg->heads * hg->sectors < nsect) hg->cylinders = 0xffff; return 0; } static int blktap_device_ioctl(struct block_device *bd, fmode_t mode, unsigned command, unsigned long argument) { int i; switch (command) { case CDROMMULTISESSION: BTDBG("FIXME: support multisession CDs later\n"); for (i = 0; i < sizeof(struct cdrom_multisession); i++) if (put_user(0, (char __user *)(argument + i))) return -EFAULT; return 0; case SCSI_IOCTL_GET_IDLUN: if (!access_ok(VERIFY_WRITE, argument, sizeof(struct scsi_idlun))) return -EFAULT; /* return 0 for now. */ __put_user(0, &((struct scsi_idlun __user *)argument)->dev_id); __put_user(0, &((struct scsi_idlun __user *)argument)->host_unique_id); return 0; default: /*printk(KERN_ALERT "ioctl %08x not supported by Xen blkdev\n", command);*/ return -EINVAL; /* same return as native Linux */ } return 0; } static struct block_device_operations blktap_device_file_operations = { .owner = THIS_MODULE, .open = blktap_device_open, .release = blktap_device_release, .ioctl = blktap_device_ioctl, .getgeo = blktap_device_getgeo }; /* NB. __blktap holding the queue lock; blktap where unlocked */ static inline struct request* __blktap_next_queued_rq(struct request_queue *q) { return blk_peek_request(q); } static inline void __blktap_dequeue_rq(struct request *rq) { blk_start_request(rq); } /* NB. err == 0 indicates success, failures < 0 */ static inline void __blktap_end_queued_rq(struct request *rq, int err) { blk_start_request(rq); __blk_end_request(rq, err, blk_rq_bytes(rq)); } static inline void __blktap_end_rq(struct request *rq, int err) { __blk_end_request(rq, err, blk_rq_bytes(rq)); } static inline void blktap_end_rq(struct request *rq, int err) { struct request_queue *q = rq->q; spin_lock_irq(q->queue_lock); __blktap_end_rq(rq, err); spin_unlock_irq(q->queue_lock); } void blktap_device_end_request(struct blktap *tap, struct blktap_request *request, int error) { struct blktap_device *tapdev = &tap->device; struct request *rq = request->rq; blktap_ring_unmap_request(tap, request); blktap_ring_free_request(tap, request); dev_dbg(disk_to_dev(tapdev->gd), "end_request: op=%d error=%d bytes=%d\n", rq_data_dir(rq), error, blk_rq_bytes(rq)); blktap_end_rq(rq, error); } int blktap_device_make_request(struct blktap *tap, struct request *rq, struct file *ring) { struct blktap_device *tapdev = &tap->device; struct blktap_request *request; int nsegs; int err; request = blktap_ring_make_request(tap); if (IS_ERR(request)) { err = PTR_ERR(request); request = NULL; if (err == -ENOSPC || err == -ENOMEM) goto stop; goto fail; } if (rq->cmd_type != REQ_TYPE_FS) { err = -EOPNOTSUPP; goto fail; } if (rq->cmd_flags & REQ_FLUSH) { request->operation = BLKTAP_OP_FLUSH; request->nr_pages = 0; goto submit; } if (rq->cmd_flags & REQ_DISCARD) { request->operation = BLKTAP_OP_TRIM; request->nr_pages = 0; goto submit; } nsegs = blk_rq_map_sg(rq->q, rq, request->sg_table); if (rq_data_dir(rq) == WRITE) request->operation = BLKTAP_OP_WRITE; else request->operation = BLKTAP_OP_READ; err = blktap_request_get_pages(tap, request, nsegs); if (err) goto stop; err = blktap_ring_map_request(tap, ring, request); if (err) goto fail; submit: request->rq = rq; blktap_ring_submit_request(tap, request); return 0; stop: tap->stats.st_oo_req++; err = -EBUSY; _out: if (request) blktap_ring_free_request(tap, request); return err; fail: if (printk_ratelimit()) dev_warn(disk_to_dev(tapdev->gd), "make request: %d, failing\n", err); goto _out; } /* * called from tapdisk context */ void blktap_device_run_queue(struct blktap *tap, struct file *ring) { struct blktap_device *tapdev = &tap->device; struct request_queue *q; struct request *rq; int err; if (!tapdev->gd) return; q = tapdev->gd->queue; spin_lock_irq(&tapdev->lock); queue_flag_clear(QUEUE_FLAG_STOPPED, q); do { rq = __blktap_next_queued_rq(q); if (!rq) break; spin_unlock_irq(&tapdev->lock); err = blktap_device_make_request(tap, rq, ring); spin_lock_irq(&tapdev->lock); if (err == -EBUSY) { blk_stop_queue(q); break; } __blktap_dequeue_rq(rq); if (unlikely(err)) __blktap_end_rq(rq, err); } while (1); spin_unlock_irq(&tapdev->lock); } static void blktap_device_do_request(struct request_queue *rq) { struct blktap_device *tapdev = rq->queuedata; struct blktap *tap = dev_to_blktap(tapdev); blktap_ring_kick_user(tap); } static void blktap_device_configure(struct blktap *tap, struct blktap_device_info *info) { struct blktap_device *tapdev = &tap->device; struct gendisk *gd = tapdev->gd; struct request_queue *rq = gd->queue; struct queue_limits *limits = &rq->limits; set_capacity(gd, info->capacity); set_disk_ro(gd, !!(info->flags & BLKTAP_DEVICE_FLAG_RO)); blk_queue_logical_block_size(rq, info->sector_size); /* Hard sector size and alignment in hardware */ blk_queue_physical_block_size(rq, info->phys_block_size); blk_queue_alignment_offset(rq, info->phys_block_offset); /* Each segment in a request is up to an aligned page in size. */ blk_queue_segment_boundary(rq, PAGE_SIZE - 1); blk_queue_max_segment_size(rq, PAGE_SIZE); /* Ensure a merged request will fit in a single I/O ring slot. */ blk_queue_max_segments(rq, BLKTAP_SEGMENT_MAX); blk_queue_max_segment_size(rq, PAGE_SIZE); /* Make sure buffer addresses are sector-aligned. */ blk_queue_dma_alignment(rq, 511); /* Enable cache control */ if (info->flags & BLKTAP_DEVICE_FLAG_FLUSH) blk_queue_flush(rq, REQ_FLUSH); /* Block discards */ if (info->flags & BLKTAP_DEVICE_FLAG_TRIM) { int trz = !!(info->flags & BLKTAP_DEVICE_FLAG_TRIM_RZ); blk_queue_max_discard_sectors(rq, UINT_MAX); limits->discard_granularity = info->trim_block_size; limits->discard_alignment = info->trim_block_offset; limits->discard_zeroes_data = trz; queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, rq); } } static int blktap_device_validate_info(struct blktap *tap, struct blktap_device_info *info) { struct device *dev = tap->ring.dev; /* sector size is is 2^(n >= 9) */ if (info->sector_size < 512 || !is_power_of_2(info->sector_size)) goto fail; /* make sure capacity won't overflow */ if (!info->capacity || info->capacity > ULLONG_MAX >> ilog2(info->sector_size)) goto fail; /* physical blocks default to logical ones */ if (!(info->flags & BLKTAP_DEVICE_FLAG_PSZ)) { info->phys_block_size = info->sector_size; info->phys_block_offset = 0; } /* phys block size is 2^n and >= logical */ if (info->phys_block_size < info->sector_size || !is_power_of_2(info->phys_block_size)) goto fail; /* alignment offset < physical/logical */ if (info->phys_block_offset % info->sector_size || info->phys_block_offset >= info->phys_block_size) goto fail; /* trim info vs logical addressing */ if (info->flags & BLKTAP_DEVICE_FLAG_TRIM) { if (info->trim_block_size < info->sector_size || !is_power_of_2(info->trim_block_size)) goto fail; if (info->trim_block_offset % info->sector_size || info->trim_block_offset >= info->trim_block_size) goto fail; } return 0; fail: dev_err(dev, "capacity: %llu, sector-size: %u/%u+%u, " "trim: %u+%u flags: %#lx\n", info->capacity, info->sector_size, info->phys_block_size, info->phys_block_offset, info->trim_block_size, info->trim_block_offset, info->flags); return -EINVAL; } int blktap_device_destroy(struct blktap *tap) { struct blktap_device *tapdev = &tap->device; struct block_device *bdev; struct gendisk *gd; int err; gd = tapdev->gd; if (!gd) return 0; bdev = bdget_disk(gd, 0); err = !mutex_trylock(&bdev->bd_mutex); if (err) { /* NB. avoid a deadlock. the last opener syncs the * bdev holding bd_mutex. */ err = -EBUSY; goto out_nolock; } if (bdev->bd_openers) { err = -EBUSY; goto out; } del_gendisk(gd); gd->private_data = NULL; blk_cleanup_queue(gd->queue); put_disk(gd); tapdev->gd = NULL; clear_bit(BLKTAP_DEVICE, &tap->dev_inuse); err = 0; out: mutex_unlock(&bdev->bd_mutex); out_nolock: bdput(bdev); return err; } static void blktap_device_fail_queue(struct blktap *tap) { struct blktap_device *tapdev = &tap->device; struct request_queue *q = tapdev->gd->queue; spin_lock_irq(&tapdev->lock); queue_flag_clear(QUEUE_FLAG_STOPPED, q); do { struct request *rq = __blktap_next_queued_rq(q); if (!rq) break; __blktap_end_queued_rq(rq, -EIO); } while (1); spin_unlock_irq(&tapdev->lock); } static int blktap_device_try_destroy(struct blktap *tap) { int err; err = blktap_device_destroy(tap); if (err) blktap_device_fail_queue(tap); return err; } void blktap_device_destroy_sync(struct blktap *tap) { wait_event(tap->ring.poll_wait, !blktap_device_try_destroy(tap)); } int blktap_device_create(struct blktap *tap, struct blktap_device_info *info) { int minor, err; struct gendisk *gd; struct request_queue *rq; struct blktap_device *tapdev; gd = NULL; rq = NULL; tapdev = &tap->device; minor = tap->minor; if (test_bit(BLKTAP_DEVICE, &tap->dev_inuse)) return -EEXIST; if (blktap_device_validate_info(tap, info)) return -EINVAL; gd = alloc_disk(1); if (!gd) { err = -ENOMEM; goto fail; } if (minor < 26) { sprintf(gd->disk_name, "td%c", 'a' + minor % 26); } else if (minor < (26 + 1) * 26) { sprintf(gd->disk_name, "td%c%c", 'a' + minor / 26 - 1,'a' + minor % 26); } else { const unsigned int m1 = (minor / 26 - 1) / 26 - 1; const unsigned int m2 = (minor / 26 - 1) % 26; const unsigned int m3 = minor % 26; sprintf(gd->disk_name, "td%c%c%c", 'a' + m1, 'a' + m2, 'a' + m3); } gd->major = blktap_device_major; gd->first_minor = minor; gd->fops = &blktap_device_file_operations; gd->private_data = tapdev; spin_lock_init(&tapdev->lock); rq = blk_init_queue(blktap_device_do_request, &tapdev->lock); if (!rq) { err = -ENOMEM; goto fail; } elevator_init(rq, "noop"); gd->queue = rq; rq->queuedata = tapdev; tapdev->gd = gd; blktap_device_configure(tap, info); add_disk(gd); set_bit(BLKTAP_DEVICE, &tap->dev_inuse); dev_info(disk_to_dev(gd), "sector-size: %u/%u+%u capacity: %llu" " discard: %u+%u flush: %#x\n", queue_logical_block_size(rq), queue_physical_block_size(rq), queue_alignment_offset(rq), (unsigned long long)get_capacity(gd), rq->limits.discard_granularity, queue_discard_alignment(rq), rq->flush_flags); return 0; fail: if (gd) del_gendisk(gd); if (rq) blk_cleanup_queue(rq); return err; } size_t blktap_device_debug(struct blktap *tap, char *buf, size_t size) { struct gendisk *disk = tap->device.gd; struct request_queue *q; struct block_device *bdev; char *s = buf, *end = buf + size; if (!disk) return 0; q = disk->queue; s += snprintf(s, end - s, "disk capacity:%llu sector size:%u\n", (unsigned long long)get_capacity(disk), queue_logical_block_size(q)); s += snprintf(s, end - s, "queue flags:%#lx stopped:%d\n", q->queue_flags, blk_queue_stopped(q)); bdev = bdget_disk(disk, 0); if (bdev) { s += snprintf(s, end - s, "bdev openers:%d closed:%d\n", bdev->bd_openers, test_bit(BLKTAP_DEVICE_CLOSED, &tap->dev_inuse)); bdput(bdev); } return s - buf; } int __init blktap_device_init() { int major; /* Dynamically allocate a major for this device */ major = register_blkdev(0, "tapdev"); if (major < 0) { BTERR("Couldn't register blktap device\n"); return -ENOMEM; } blktap_device_major = major; BTINFO("blktap device major %d\n", major); return 0; } void blktap_device_exit(void) { if (blktap_device_major) unregister_blkdev(blktap_device_major, "tapdev"); } blktap-dkms-2.0.93/dkms.conf000066400000000000000000000005141202241650400156170ustar00rootroot00000000000000PACKAGE_VERSION="2.0.93" PACKAGE_NAME="blktap" CLEAN="make -C ${kernel_source_dir} M=${dkms_tree}/${PACKAGE_NAME}/${PACKAGE_VERSION}/build clean" MAKE="make -C ${kernel_source_dir} M=${dkms_tree}/${PACKAGE_NAME}/${PACKAGE_VERSION}/build modules" AUTOINSTALL="yes" BUILT_MODULE_NAME="blktap" DEST_MODULE_LOCATION="/kernel/../extra/" blktap-dkms-2.0.93/linux-blktap.h000066400000000000000000000070131202241650400165760ustar00rootroot00000000000000/* * * Copyright (C) 2011 Citrix Systems Inc. * * This file is part of Blktap2. * * Blktap2 is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 * as published by the Free Software Foundation. * * Blktap2 is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License version 2 for more details. * * You should have received a copy of the GNU General Public License * version 2 along with Blktap2. If not, see * . * * */ #ifndef _LINUX_BLKTAP_H #define _LINUX_BLKTAP_H /* * Control */ #define BLKTAP_IOCTL_RESPOND 1 #define BLKTAP_IOCTL_ALLOC_TAP 200 #define BLKTAP_IOCTL_FREE_TAP 201 #define BLKTAP_IOCTL_CREATE_DEVICE 208 #define BLKTAP_IOCTL_REMOVE_DEVICE 207 #define BLKTAP_DEVICE_FLAG_RO 0x00000001UL /* disk is R/O */ #define BLKTAP_DEVICE_FLAG_PSZ 0x00000002UL /* physical sector size */ #define BLKTAP_DEVICE_FLAG_FLUSH 0x00000004UL /* supports FLUSH */ #define BLKTAP_DEVICE_FLAG_TRIM 0x00000008UL /* supports TRIM */ #define BLKTAP_DEVICE_FLAG_TRIM_RZ 0x00000010UL /* trimmed data reads zero */ struct blktap_info { unsigned int ring_major; unsigned int bdev_major; unsigned int ring_minor; }; struct blktap_device_info { unsigned long long capacity; unsigned int sector_size; unsigned long flags; unsigned int phys_block_size; unsigned int phys_block_offset; unsigned int trim_block_size; unsigned int trim_block_offset; }; /* * I/O ring */ #ifdef __KERNEL__ #define BLKTAP_PAGE_SIZE PAGE_SIZE #endif #include struct blktap_segment { uint32_t __pad; uint8_t first_sect; uint8_t last_sect; }; #define BLKTAP_OP_READ 0 #define BLKTAP_OP_WRITE 1 #define BLKTAP_OP_FLUSH 2 #define BLKTAP_OP_TRIM 3 #define BLKTAP_SEGMENT_MAX 11 struct blktap_ring_rw_request { uint64_t sector_number; struct blktap_segment seg[BLKTAP_SEGMENT_MAX]; }; struct blktap_ring_tr_request { uint64_t sector_number; uint64_t nr_sectors; }; struct blktap_ring_request { uint8_t operation; uint8_t nr_segments; uint16_t __pad; uint64_t id; union { struct blktap_ring_rw_request rw; struct blktap_ring_tr_request tr; } u; }; #define BLKTAP_RSP_EOPNOTSUPP -2 #define BLKTAP_RSP_ERROR -1 #define BLKTAP_RSP_OKAY 0 struct blktap_ring_response { uint64_t id; uint8_t operation; int16_t status; }; DEFINE_RING_TYPES(blktap, struct blktap_ring_request, struct blktap_ring_response); #define BLKTAP_RING_SIZE \ ((int)__RD32((BLKTAP_PAGE_SIZE - \ (size_t)&((struct blktap_sring*)0)->ring) / \ sizeof(((struct blktap_sring *)0)->ring[0]))) /* * Ring messages + old ioctls (DEPRECATED) */ #define BLKTAP_RING_MESSAGE(_sring) \ ((uint8_t*)(&(_sring)->rsp_event + 1)) #define BLKTAP_RING_MESSAGE_CLOSE 3 #define BLKTAP_IOCTL_CREATE_DEVICE_COMPAT 202 #define BLKTAP_NAME_MAX 256 struct blktap2_params { char name[BLKTAP_NAME_MAX]; unsigned long long capacity; unsigned long sector_size; }; #endif /* _LINUX_BLKTAP_H */ blktap-dkms-2.0.93/request.c000066400000000000000000000233671202241650400156610ustar00rootroot00000000000000/* * * Copyright (C) 2011 Citrix Systems Inc. * * This file is part of Blktap2. * * Blktap2 is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 * as published by the Free Software Foundation. * * Blktap2 is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License version 2 for more details. * * You should have received a copy of the GNU General Public License * version 2 along with Blktap2. If not, see * . * * */ #include #include #include #include #include #include #include "blktap.h" /* max pages per shared pool. just to prevent accidental dos. */ #define POOL_MAX_PAGES (256*BLKTAP_SEGMENT_MAX) /* default page pool size. when considering to shrink a shared pool, * note that paused tapdisks may grab a whole lot of pages for a long * time. */ #define POOL_DEFAULT_PAGES (2 * BLKTAP_RING_SIZE * BLKTAP_SEGMENT_MAX) /* max number of pages allocatable per request. */ #define POOL_MAX_REQUEST_PAGES BLKTAP_SEGMENT_MAX /* min request structs per pool. These grow dynamically. */ #define POOL_MIN_REQS BLKTAP_RING_SIZE static struct kset *pool_set; #define kobj_to_pool(_kobj) \ container_of(_kobj, struct blktap_page_pool, kobj) static struct kmem_cache *request_cache; static mempool_t *request_pool; static void __page_pool_wake(struct blktap_page_pool *pool) { mempool_t *mem = pool->bufs; /* NB. slightly wasteful to always wait for a full segment set. but this ensures the next disk makes progress. presently, the repeated request struct alloc/release cycles would otherwise keep everyone spinning. */ if (mem->curr_nr >= POOL_MAX_REQUEST_PAGES) wake_up(&pool->wait); } int blktap_request_get_pages(struct blktap *tap, struct blktap_request *request, int nr_pages) { struct blktap_page_pool *pool = tap->pool; mempool_t *mem = pool->bufs; struct page *page; BUG_ON(request->nr_pages != 0); BUG_ON(nr_pages > POOL_MAX_REQUEST_PAGES); if (mem->curr_nr < nr_pages) return -ENOMEM; /* NB. avoid thundering herds of tapdisks colliding. */ spin_lock(&pool->lock); if (mem->curr_nr < nr_pages) { spin_unlock(&pool->lock); return -ENOMEM; } while (request->nr_pages < nr_pages) { page = mempool_alloc(mem, GFP_NOWAIT); BUG_ON(!page); request->pages[request->nr_pages++] = page; } spin_unlock(&pool->lock); return 0; } static void blktap_request_put_pages(struct blktap *tap, struct blktap_request *request) { struct blktap_page_pool *pool = tap->pool; struct page *page; while (request->nr_pages) { page = request->pages[--request->nr_pages]; mempool_free(page, pool->bufs); } } size_t blktap_request_debug(struct blktap *tap, char *buf, size_t size) { struct blktap_page_pool *pool = tap->pool; mempool_t *mem = pool->bufs; char *s = buf, *end = buf + size; s += snprintf(buf, end - s, "pool:%s pages:%d free:%d\n", kobject_name(&pool->kobj), mem->min_nr, mem->curr_nr); return s - buf; } struct blktap_request* blktap_request_alloc(struct blktap *tap) { struct blktap_request *request; request = mempool_alloc(request_pool, GFP_NOWAIT); if (request) request->tap = tap; return request; } void blktap_request_free(struct blktap *tap, struct blktap_request *request) { blktap_request_put_pages(tap, request); mempool_free(request, request_pool); __page_pool_wake(tap->pool); } static void blktap_request_bounce_seg(struct blktap *tap, struct blktap_request *request, int seg, int write) { struct scatterlist *sg = &request->sg_table[seg]; void *s, *p; BUG_ON(seg >= request->nr_pages); s = sg_virt(sg); p = page_address(request->pages[seg]) + sg->offset; if (write) memcpy(p, s, sg->length); else memcpy(s, p, sg->length); } void blktap_request_bounce(struct blktap *tap, struct blktap_request *request, int write) { int seg; for (seg = 0; seg < request->nr_pages; seg++) blktap_request_bounce_seg(tap, request, seg, write); } static void blktap_request_ctor(void *obj) { struct blktap_request *request = obj; memset(request, 0, sizeof(*request)); sg_init_table(request->sg_table, ARRAY_SIZE(request->sg_table)); } static int blktap_page_pool_resize(struct blktap_page_pool *pool, int target) { mempool_t *bufs = pool->bufs; int err; /* NB. mempool asserts min_nr >= 1 */ target = max(1, target); err = mempool_resize(bufs, target, GFP_KERNEL); if (err) return err; __page_pool_wake(pool); return 0; } struct pool_attribute { struct attribute attr; ssize_t (*show)(struct blktap_page_pool *pool, char *buf); ssize_t (*store)(struct blktap_page_pool *pool, const char *buf, size_t count); }; #define kattr_to_pool_attr(_kattr) \ container_of(_kattr, struct pool_attribute, attr) static ssize_t blktap_page_pool_show_size(struct blktap_page_pool *pool, char *buf) { mempool_t *mem = pool->bufs; return sprintf(buf, "%d", mem->min_nr); } static ssize_t blktap_page_pool_store_size(struct blktap_page_pool *pool, const char *buf, size_t size) { int target; /* * NB. target fixup to avoid undesired results. less than a * full segment set can wedge the disk. much more than a * couple times the physical queue depth is rarely useful. */ target = simple_strtoul(buf, NULL, 0); target = max(POOL_MAX_REQUEST_PAGES, target); target = min(target, POOL_MAX_PAGES); return blktap_page_pool_resize(pool, target) ? : size; } static struct pool_attribute blktap_page_pool_attr_size = __ATTR(size, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, blktap_page_pool_show_size, blktap_page_pool_store_size); static ssize_t blktap_page_pool_show_free(struct blktap_page_pool *pool, char *buf) { mempool_t *mem = pool->bufs; return sprintf(buf, "%d", mem->curr_nr); } static struct pool_attribute blktap_page_pool_attr_free = __ATTR(free, S_IRUSR|S_IRGRP|S_IROTH, blktap_page_pool_show_free, NULL); static struct attribute *blktap_page_pool_attrs[] = { &blktap_page_pool_attr_size.attr, &blktap_page_pool_attr_free.attr, NULL, }; static inline struct kobject* __blktap_kset_find_obj(struct kset *kset, const char *name) { struct kobject *k; struct kobject *ret = NULL; spin_lock(&kset->list_lock); list_for_each_entry(k, &kset->list, entry) { if (kobject_name(k) && !strcmp(kobject_name(k), name)) { ret = kobject_get(k); break; } } spin_unlock(&kset->list_lock); return ret; } static ssize_t blktap_page_pool_show_attr(struct kobject *kobj, struct attribute *kattr, char *buf) { struct blktap_page_pool *pool = kobj_to_pool(kobj); struct pool_attribute *attr = kattr_to_pool_attr(kattr); if (attr->show) return attr->show(pool, buf); return -EIO; } static ssize_t blktap_page_pool_store_attr(struct kobject *kobj, struct attribute *kattr, const char *buf, size_t size) { struct blktap_page_pool *pool = kobj_to_pool(kobj); struct pool_attribute *attr = kattr_to_pool_attr(kattr); if (attr->show) return attr->store(pool, buf, size); return -EIO; } static struct sysfs_ops blktap_page_pool_sysfs_ops = { .show = blktap_page_pool_show_attr, .store = blktap_page_pool_store_attr, }; static void blktap_page_pool_release(struct kobject *kobj) { struct blktap_page_pool *pool = kobj_to_pool(kobj); mempool_destroy(pool->bufs); kfree(pool); } struct kobj_type blktap_page_pool_ktype = { .release = blktap_page_pool_release, .sysfs_ops = &blktap_page_pool_sysfs_ops, .default_attrs = blktap_page_pool_attrs, }; static void* __mempool_page_alloc(gfp_t gfp_mask, void *pool_data) { struct page *page; if (!(gfp_mask & __GFP_WAIT)) return NULL; page = alloc_page(gfp_mask); if (page) SetPageReserved(page); return page; } static void __mempool_page_free(void *element, void *pool_data) { struct page *page = element; ClearPageReserved(page); put_page(page); } static struct kobject* blktap_page_pool_create(const char *name, int nr_pages) { struct blktap_page_pool *pool; int err; pool = kzalloc(sizeof(*pool), GFP_KERNEL); if (!pool) goto fail; spin_lock_init(&pool->lock); init_waitqueue_head(&pool->wait); pool->bufs = mempool_create(nr_pages, __mempool_page_alloc, __mempool_page_free, pool); if (!pool->bufs) goto fail_pool; kobject_init(&pool->kobj, &blktap_page_pool_ktype); pool->kobj.kset = pool_set; err = kobject_add(&pool->kobj, &pool_set->kobj, "%s", name); if (err) goto fail_bufs; return &pool->kobj; kobject_del(&pool->kobj); fail_bufs: mempool_destroy(pool->bufs); fail_pool: kfree(pool); fail: return NULL; } struct blktap_page_pool* blktap_page_pool_get(const char *name) { struct kobject *kobj; kobj = __blktap_kset_find_obj(pool_set, name); if (!kobj) kobj = blktap_page_pool_create(name, POOL_DEFAULT_PAGES); if (!kobj) return ERR_PTR(-ENOMEM); return kobj_to_pool(kobj); } int __init blktap_page_pool_init(struct kobject *parent) { request_cache = kmem_cache_create("blktap-request", sizeof(struct blktap_request), 0, 0, blktap_request_ctor); if (!request_cache) return -ENOMEM; request_pool = mempool_create_slab_pool(POOL_MIN_REQS, request_cache); if (!request_pool) return -ENOMEM; pool_set = kset_create_and_add("pools", NULL, parent); if (!pool_set) return -ENOMEM; return 0; } void blktap_page_pool_exit(void) { if (pool_set) { BUG_ON(!list_empty(&pool_set->list)); kset_unregister(pool_set); pool_set = NULL; } if (request_pool) { mempool_destroy(request_pool); request_pool = NULL; } if (request_cache) { kmem_cache_destroy(request_cache); request_cache = NULL; } } blktap-dkms-2.0.93/ring.c000066400000000000000000000364121202241650400151230ustar00rootroot00000000000000/* * * Copyright (C) 2011 Citrix Systems Inc. * * This file is part of Blktap2. * * Blktap2 is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 * as published by the Free Software Foundation. * * Blktap2 is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License version 2 for more details. * * You should have received a copy of the GNU General Public License * version 2 along with Blktap2. If not, see * . * * */ #include #include #include #include #include #include #include #include "blktap.h" int blktap_ring_major; static struct cdev blktap_ring_cdev; /* * BLKTAP - immediately before the mmap area, * we have a bunch of pages reserved for shared memory rings. */ #define RING_PAGES 1 #define BLKTAP_INFO_SIZE_AT(_memb) \ offsetof(struct blktap_device_info, _memb) + \ sizeof(((struct blktap_device_info*)0)->_memb) static void blktap_ring_read_response(struct blktap *tap, const struct blktap_ring_response *rsp) { struct blktap_ring *ring = &tap->ring; struct blktap_request *request; int usr_idx, err; request = NULL; usr_idx = rsp->id; if (usr_idx < 0 || usr_idx >= BLKTAP_RING_SIZE) { err = -ERANGE; goto invalid; } request = ring->pending[usr_idx]; if (!request) { err = -ESRCH; goto invalid; } if (rsp->operation != request->operation) { err = -EINVAL; goto invalid; } dev_dbg(ring->dev, "request %d [%p] response: %d\n", request->usr_idx, request, rsp->status); err = rsp->status == BLKTAP_RSP_OKAY ? 0 : -EIO; end_request: blktap_device_end_request(tap, request, err); return; invalid: dev_warn(ring->dev, "invalid response, idx:%d status:%d op:%d/%d: err %d\n", usr_idx, rsp->status, rsp->operation, request->operation, err); if (request) goto end_request; } static void blktap_read_ring(struct blktap *tap) { struct blktap_ring *ring = &tap->ring; struct blktap_ring_response rsp; RING_IDX rc, rp; mutex_lock(&ring->vma_lock); if (!ring->vma) { mutex_unlock(&ring->vma_lock); return; } /* for each outstanding message on the ring */ rp = ring->ring.sring->rsp_prod; rmb(); for (rc = ring->ring.rsp_cons; rc != rp; rc++) { memcpy(&rsp, RING_GET_RESPONSE(&ring->ring, rc), sizeof(rsp)); blktap_ring_read_response(tap, &rsp); } ring->ring.rsp_cons = rc; mutex_unlock(&ring->vma_lock); } #define MMAP_VADDR(_start, _req, _seg) \ ((_start) + \ ((_req) * BLKTAP_SEGMENT_MAX * BLKTAP_PAGE_SIZE) + \ ((_seg) * BLKTAP_PAGE_SIZE)) static int blktap_ring_fault(struct vm_area_struct *vma, struct vm_fault *vmf) { return VM_FAULT_SIGBUS; } static void blktap_ring_fail_pending(struct blktap *tap) { struct blktap_ring *ring = &tap->ring; struct blktap_request *request; int usr_idx; for (usr_idx = 0; usr_idx < BLKTAP_RING_SIZE; usr_idx++) { request = ring->pending[usr_idx]; if (!request) continue; blktap_device_end_request(tap, request, -EIO); } } static void blktap_ring_vm_close_sring(struct blktap *tap, struct vm_area_struct *vma) { struct blktap_ring *ring = &tap->ring; struct page *page = virt_to_page(ring->ring.sring); blktap_ring_fail_pending(tap); ClearPageReserved(page); __free_page(page); ring->vma = NULL; if (test_bit(BLKTAP_SHUTDOWN_REQUESTED, &tap->dev_inuse)) blktap_control_destroy_tap(tap); } static void blktap_ring_vm_close(struct vm_area_struct *vma) { struct blktap *tap = vma->vm_private_data; dev_dbg(tap->ring.dev, "vm_close %lx-%lx (%lu) pgoff %lu\n", vma->vm_start, vma->vm_end, vma_pages(vma), vma->vm_pgoff); if (!vma->vm_pgoff) blktap_ring_vm_close_sring(tap, vma); } static struct vm_operations_struct blktap_ring_vm_operations = { .close = blktap_ring_vm_close, .fault = blktap_ring_fault, }; int blktap_ring_map_request(struct blktap *tap, struct file *filp, struct blktap_request *request) { struct blktap_ring *ring = &tap->ring; unsigned long addr, len, pgoff; int read, write, prot, flags; write = request->operation == BLKTAP_OP_WRITE; read = request->operation == BLKTAP_OP_READ; if (write) blktap_request_bounce(tap, request, write); prot = PROT_READ; prot |= read ? PROT_WRITE : 0; flags = MAP_FIXED|MAP_SHARED; addr = MMAP_VADDR(ring->user_vstart, request->usr_idx, 0); len = request->nr_pages << PAGE_SHIFT; pgoff = 1 + request->usr_idx * BLKTAP_SEGMENT_MAX; addr = vm_mmap(filp, addr, len, prot, flags, pgoff << PAGE_SHIFT); return IS_ERR_VALUE(addr) ? addr : 0; } void blktap_ring_unmap_request(struct blktap *tap, struct blktap_request *request) { struct blktap_ring *ring = &tap->ring; unsigned long addr, len; int read, err; read = request->operation == BLKTAP_OP_READ; if (read) blktap_request_bounce(tap, request, !read); addr = MMAP_VADDR(ring->user_vstart, request->usr_idx, 0); len = request->nr_pages << PAGE_SHIFT; err = vm_munmap(addr, len); WARN_ON_ONCE(err); } void blktap_ring_free_request(struct blktap *tap, struct blktap_request *request) { struct blktap_ring *ring = &tap->ring; ring->pending[request->usr_idx] = NULL; ring->n_pending--; blktap_request_free(tap, request); } struct blktap_request* blktap_ring_make_request(struct blktap *tap) { struct blktap_ring *ring = &tap->ring; struct blktap_request *request; int usr_idx; if (RING_FULL(&ring->ring)) return ERR_PTR(-ENOSPC); request = blktap_request_alloc(tap); if (!request) return ERR_PTR(-ENOMEM); for (usr_idx = 0; usr_idx < BLKTAP_RING_SIZE; usr_idx++) if (!ring->pending[usr_idx]) break; BUG_ON(usr_idx >= BLKTAP_RING_SIZE); request->tap = tap; request->usr_idx = usr_idx; ring->pending[usr_idx] = request; ring->n_pending++; return request; } static int blktap_ring_make_rw_request(struct blktap *tap, struct blktap_request *request, struct blktap_ring_request *breq) { struct scatterlist *sg; unsigned int i, nsecs = 0; blktap_for_each_sg(sg, request, i) { struct blktap_segment *seg = &breq->u.rw.seg[i]; int first, count; count = sg->length >> 9; first = sg->offset >> 9; seg->first_sect = first; seg->last_sect = first + count - 1; nsecs += count; } breq->u.rw.sector_number = blk_rq_pos(request->rq); return nsecs; } static int blktap_ring_make_tr_request(struct blktap *tap, struct blktap_request *request, struct blktap_ring_request *breq) { struct bio *bio = request->rq->bio; unsigned int nsecs; breq->u.tr.nr_sectors = nsecs = bio_sectors(bio); breq->u.tr.sector_number = bio->bi_sector; return nsecs; } void blktap_ring_submit_request(struct blktap *tap, struct blktap_request *request) { struct blktap_ring *ring = &tap->ring; struct blktap_ring_request *breq; int nsecs; dev_dbg(ring->dev, "request %d [%p] submit\n", request->usr_idx, request); breq = RING_GET_REQUEST(&ring->ring, ring->ring.req_prod_pvt); breq->id = request->usr_idx; breq->__pad = 0; breq->operation = request->operation; breq->nr_segments = request->nr_pages; switch (breq->operation) { case BLKTAP_OP_READ: nsecs = blktap_ring_make_rw_request(tap, request, breq); tap->stats.st_rd_sect += nsecs; tap->stats.st_rd_req++; break; case BLKTAP_OP_WRITE: nsecs = blktap_ring_make_rw_request(tap, request, breq); tap->stats.st_wr_sect += nsecs; tap->stats.st_wr_req++; break; case BLKTAP_OP_FLUSH: breq->u.rw.sector_number = 0; tap->stats.st_fl_req++; break; case BLKTAP_OP_TRIM: nsecs = blktap_ring_make_tr_request(tap, request, breq); tap->stats.st_tr_sect += nsecs; tap->stats.st_tr_req++; break; default: BUG(); } ring->ring.req_prod_pvt++; } static int blktap_ring_open(struct inode *inode, struct file *filp) { struct blktap *tap = NULL; int minor; minor = iminor(inode); if (minor < blktap_max_minor) tap = blktaps[minor]; if (!tap) return -ENXIO; if (test_bit(BLKTAP_SHUTDOWN_REQUESTED, &tap->dev_inuse)) return -ENXIO; if (tap->ring.task) return -EBUSY; filp->private_data = tap; tap->ring.task = current; return 0; } static int blktap_ring_release(struct inode *inode, struct file *filp) { struct blktap *tap = filp->private_data; blktap_device_destroy_sync(tap); tap->ring.task = NULL; if (test_bit(BLKTAP_SHUTDOWN_REQUESTED, &tap->dev_inuse)) blktap_control_destroy_tap(tap); return 0; } static int blktap_ring_mmap_request(struct blktap *tap, struct vm_area_struct *vma) { struct blktap_ring *ring = &tap->ring; struct blktap_request *request; int usr_idx, seg, err; unsigned long addr, n_segs; usr_idx = vma->vm_pgoff - 1; seg = usr_idx % BLKTAP_SEGMENT_MAX; usr_idx /= BLKTAP_SEGMENT_MAX; request = ring->pending[usr_idx]; if (!request) return -EINVAL; n_segs = request->nr_pages - seg; n_segs = min(n_segs, vma_pages(vma)); for (addr = vma->vm_start; seg < n_segs; seg++, addr += PAGE_SIZE) { struct page *page = request->pages[seg]; dev_dbg(tap->ring.dev, "mmap request %d seg %d addr %lx\n", usr_idx, seg, addr); err = vm_insert_page(vma, addr, page); if (err) return err; } vma->vm_flags |= VM_DONTCOPY; vma->vm_flags |= VM_RESERVED; return 0; } static int blktap_ring_mmap_sring(struct blktap *tap, struct vm_area_struct *vma) { struct blktap_ring *ring = &tap->ring; struct blktap_sring *sring; struct page *page = NULL; int err; if (ring->vma) return -EBUSY; page = alloc_page(GFP_KERNEL|__GFP_ZERO); if (!page) return -ENOMEM; SetPageReserved(page); err = vm_insert_page(vma, vma->vm_start, page); if (err) goto fail; sring = page_address(page); SHARED_RING_INIT(sring); FRONT_RING_INIT(&ring->ring, sring, PAGE_SIZE); ring->ring_vstart = vma->vm_start; ring->user_vstart = ring->ring_vstart + PAGE_SIZE; vma->vm_private_data = tap; vma->vm_flags |= VM_DONTCOPY; vma->vm_flags |= VM_RESERVED; vma->vm_ops = &blktap_ring_vm_operations; ring->vma = vma; return 0; fail: if (page) { ClearPageReserved(page); __free_page(page); } return err; } static int blktap_ring_mmap(struct file *filp, struct vm_area_struct *vma) { struct blktap *tap = filp->private_data; dev_dbg(tap->ring.dev, "mmap %lx-%lx (%lu) pgoff %lu\n", vma->vm_start, vma->vm_end, vma_pages(vma), vma->vm_pgoff); if (!vma->vm_pgoff) return blktap_ring_mmap_sring(tap, vma); return blktap_ring_mmap_request(tap, vma); } static long blktap_ring_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) { struct blktap *tap = filp->private_data; struct blktap_ring *ring = &tap->ring; void __user *ptr = (void *)arg; int err; BTDBG("%d: cmd: %u, arg: %lu\n", tap->minor, cmd, arg); if (!ring->vma || ring->vma->vm_mm != current->mm) return -EACCES; switch(cmd) { case BLKTAP_IOCTL_RESPOND: blktap_read_ring(tap); return 0; case BLKTAP_IOCTL_CREATE_DEVICE_COMPAT: { struct blktap_device_info info; struct blktap2_params params; if (copy_from_user(¶ms, ptr, sizeof(params))) return -EFAULT; info.capacity = params.capacity; info.sector_size = params.sector_size; info.flags = 0; err = blktap_device_create(tap, &info); if (err) return err; if (params.name[0]) { strncpy(tap->name, params.name, sizeof(params.name)); tap->name[sizeof(tap->name)-1] = 0; } return 0; } case BLKTAP_IOCTL_CREATE_DEVICE: { struct blktap_device_info __user *ptr = (void *)arg; struct blktap_device_info info; unsigned long mask; size_t base_sz, sz; mask = BLKTAP_DEVICE_FLAG_RO; mask |= BLKTAP_DEVICE_FLAG_PSZ; mask |= BLKTAP_DEVICE_FLAG_FLUSH; mask |= BLKTAP_DEVICE_FLAG_TRIM; mask |= BLKTAP_DEVICE_FLAG_TRIM_RZ; memset(&info, 0, sizeof(info)); sz = base_sz = BLKTAP_INFO_SIZE_AT(flags); if (copy_from_user(&info, ptr, sz)) return -EFAULT; if ((info.flags & BLKTAP_DEVICE_FLAG_PSZ) != 0) sz = BLKTAP_INFO_SIZE_AT(phys_block_offset); if (info.flags & BLKTAP_DEVICE_FLAG_TRIM) sz = BLKTAP_INFO_SIZE_AT(trim_block_offset); if (sz > base_sz) if (copy_from_user(&info, ptr, sz)) return -EFAULT; if (put_user(info.flags & mask, &ptr->flags)) return -EFAULT; return blktap_device_create(tap, &info); } case BLKTAP_IOCTL_REMOVE_DEVICE: return blktap_device_destroy(tap); } return -ENOTTY; } static unsigned int blktap_ring_poll(struct file *filp, poll_table *wait) { struct blktap *tap = filp->private_data; struct blktap_ring *ring = &tap->ring; int work; poll_wait(filp, &tap->pool->wait, wait); poll_wait(filp, &ring->poll_wait, wait); mutex_lock(&ring->vma_lock); if (ring->vma && tap->device.gd) blktap_device_run_queue(tap, filp); mutex_unlock(&ring->vma_lock); work = ring->ring.req_prod_pvt - ring->ring.sring->req_prod; RING_PUSH_REQUESTS(&ring->ring); if (work || *BLKTAP_RING_MESSAGE(ring->ring.sring) || test_and_clear_bit(BLKTAP_DEVICE_CLOSED, &tap->dev_inuse)) return POLLIN | POLLRDNORM; return 0; } static struct file_operations blktap_ring_file_operations = { .owner = THIS_MODULE, .open = blktap_ring_open, .release = blktap_ring_release, .unlocked_ioctl = blktap_ring_ioctl, .mmap = blktap_ring_mmap, .poll = blktap_ring_poll, }; void blktap_ring_kick_user(struct blktap *tap) { wake_up(&tap->ring.poll_wait); } int blktap_ring_destroy(struct blktap *tap) { struct blktap_ring *ring = &tap->ring; if (ring->task || ring->vma) return -EBUSY; return 0; } int blktap_ring_create(struct blktap *tap) { struct blktap_ring *ring = &tap->ring; init_waitqueue_head(&ring->poll_wait); ring->devno = MKDEV(blktap_ring_major, tap->minor); mutex_init(&ring->vma_lock); return 0; } size_t blktap_ring_debug(struct blktap *tap, char *buf, size_t size) { struct blktap_ring *ring = &tap->ring; char *s = buf, *end = buf + size; int usr_idx; s += snprintf(s, end - s, "begin pending:%d\n", ring->n_pending); for (usr_idx = 0; usr_idx < BLKTAP_RING_SIZE; usr_idx++) { struct blktap_request *request; struct timeval t; request = ring->pending[usr_idx]; if (!request) continue; jiffies_to_timeval(jiffies - request->rq->start_time, &t); s += snprintf(s, end - s, "%02d: usr_idx:%02d " "op:%x nr_pages:%02d time:%lu.%09lu\n", usr_idx, request->usr_idx, request->operation, request->nr_pages, t.tv_sec, t.tv_usec); } s += snprintf(s, end - s, "end pending\n"); return s - buf; } int __init blktap_ring_init(void) { dev_t dev = 0; int err; cdev_init(&blktap_ring_cdev, &blktap_ring_file_operations); blktap_ring_cdev.owner = THIS_MODULE; err = alloc_chrdev_region(&dev, 0, MAX_BLKTAP_DEVICE, "blktap2"); if (err < 0) { BTERR("error registering ring devices: %d\n", err); return err; } err = cdev_add(&blktap_ring_cdev, dev, MAX_BLKTAP_DEVICE); if (err) { BTERR("error adding ring device: %d\n", err); unregister_chrdev_region(dev, MAX_BLKTAP_DEVICE); return err; } blktap_ring_major = MAJOR(dev); BTINFO("blktap ring major: %d\n", blktap_ring_major); return 0; } void blktap_ring_exit(void) { if (!blktap_ring_major) return; cdev_del(&blktap_ring_cdev); unregister_chrdev_region(MKDEV(blktap_ring_major, 0), MAX_BLKTAP_DEVICE); blktap_ring_major = 0; } blktap-dkms-2.0.93/sysfs.c000066400000000000000000000147161202241650400153360ustar00rootroot00000000000000/* * * Copyright (C) 2011 Citrix Systems Inc. * * This file is part of Blktap2. * * Blktap2 is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 * as published by the Free Software Foundation. * * Blktap2 is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License version 2 for more details. * * You should have received a copy of the GNU General Public License * version 2 along with Blktap2. If not, see * . * * */ #include #include #include #include #include #include #include "blktap.h" int blktap_debug_level = 1; static struct class *class; static ssize_t blktap_sysfs_set_name(struct device *dev, struct device_attribute *attr, const char *buf, size_t size) { struct blktap *tap; tap = dev_get_drvdata(dev); if (!tap) return 0; if (size >= BLKTAP_NAME_MAX) return -ENAMETOOLONG; if (strnlen(buf, size) != size) return -EINVAL; strcpy(tap->name, buf); return size; } static ssize_t blktap_sysfs_get_name(struct device *dev, struct device_attribute *attr, char *buf) { struct blktap *tap; ssize_t size; tap = dev_get_drvdata(dev); if (!tap) return 0; if (tap->name[0]) size = sprintf(buf, "%s\n", tap->name); else size = sprintf(buf, "%d\n", tap->minor); return size; } static DEVICE_ATTR(name, S_IRUGO|S_IWUSR, blktap_sysfs_get_name, blktap_sysfs_set_name); static void blktap_sysfs_remove_work(struct work_struct *work) { struct blktap *tap = container_of(work, struct blktap, remove_work); blktap_control_destroy_tap(tap); } static ssize_t blktap_sysfs_remove_device(struct device *dev, struct device_attribute *attr, const char *buf, size_t size) { struct blktap *tap; int err; tap = dev_get_drvdata(dev); if (!tap) return size; if (test_and_set_bit(BLKTAP_SHUTDOWN_REQUESTED, &tap->dev_inuse)) goto wait; if (tap->ring.vma) { struct blktap_sring *sring = tap->ring.ring.sring; *BLKTAP_RING_MESSAGE(sring) = BLKTAP_RING_MESSAGE_CLOSE; blktap_ring_kick_user(tap); } else { INIT_WORK(&tap->remove_work, blktap_sysfs_remove_work); schedule_work(&tap->remove_work); } wait: err = wait_event_interruptible(tap->remove_wait, !dev_get_drvdata(dev)); if (err) return err; return size; } static DEVICE_ATTR(remove, S_IWUSR, NULL, blktap_sysfs_remove_device); static ssize_t blktap_sysfs_debug_device(struct device *dev, struct device_attribute *attr, char *buf) { struct blktap *tap; char *s = buf, *end = buf + PAGE_SIZE; tap = dev_get_drvdata(dev); if (!tap) return 0; s += blktap_control_debug(tap, s, end - s); s += blktap_request_debug(tap, s, end - s); s += blktap_device_debug(tap, s, end - s); s += blktap_ring_debug(tap, s, end - s); return s - buf; } static DEVICE_ATTR(debug, S_IRUGO, blktap_sysfs_debug_device, NULL); static ssize_t blktap_sysfs_show_task(struct device *dev, struct device_attribute *attr, char *buf) { struct blktap *tap; ssize_t rv = 0; tap = dev_get_drvdata(dev); if (!tap) return 0; if (tap->ring.task) rv = sprintf(buf, "%d\n", tap->ring.task->pid); return rv; } static DEVICE_ATTR(task, S_IRUGO, blktap_sysfs_show_task, NULL); static ssize_t blktap_sysfs_show_pool(struct device *dev, struct device_attribute *attr, char *buf) { struct blktap *tap = dev_get_drvdata(dev); return sprintf(buf, "%s", kobject_name(&tap->pool->kobj)); } static ssize_t blktap_sysfs_store_pool(struct device *dev, struct device_attribute *attr, const char *buf, size_t size) { struct blktap *tap = dev_get_drvdata(dev); struct blktap_page_pool *pool, *tmp = tap->pool; if (tap->device.gd) return -EBUSY; pool = blktap_page_pool_get(buf); if (IS_ERR(pool)) return PTR_ERR(pool); tap->pool = pool; kobject_put(&tmp->kobj); return size; } DEVICE_ATTR(pool, S_IRUSR|S_IWUSR, blktap_sysfs_show_pool, blktap_sysfs_store_pool); int blktap_sysfs_create(struct blktap *tap) { struct blktap_ring *ring = &tap->ring; struct device *dev; int err = 0; init_waitqueue_head(&tap->remove_wait); dev = device_create(class, NULL, ring->devno, tap, "blktap%d", tap->minor); if (IS_ERR(dev)) err = PTR_ERR(dev); if (!err) err = device_create_file(dev, &dev_attr_name); if (!err) err = device_create_file(dev, &dev_attr_remove); if (!err) err = device_create_file(dev, &dev_attr_debug); if (!err) err = device_create_file(dev, &dev_attr_task); if (!err) err = device_create_file(dev, &dev_attr_pool); if (!err) ring->dev = dev; else device_unregister(dev); return err; } void blktap_sysfs_destroy(struct blktap *tap) { struct blktap_ring *ring = &tap->ring; struct device *dev; dev = ring->dev; if (!dev) return; dev_set_drvdata(dev, NULL); wake_up(&tap->remove_wait); device_unregister(dev); ring->dev = NULL; } static ssize_t blktap_sysfs_show_verbosity(struct class *class, struct class_attribute *attr, char *buf) { return sprintf(buf, "%d\n", blktap_debug_level); } static ssize_t blktap_sysfs_set_verbosity(struct class *class, struct class_attribute *attr, const char *buf, size_t size) { int level; if (sscanf(buf, "%d", &level) == 1) { blktap_debug_level = level; return size; } return -EINVAL; } static CLASS_ATTR(verbosity, S_IRUGO|S_IWUSR, blktap_sysfs_show_verbosity, blktap_sysfs_set_verbosity); static ssize_t blktap_sysfs_show_devices(struct class *class, struct class_attribute *attr, char *buf) { int i, ret; struct blktap *tap; mutex_lock(&blktap_lock); ret = 0; for (i = 0; i < blktap_max_minor; i++) { tap = blktaps[i]; if (!tap) continue; if (!test_bit(BLKTAP_DEVICE, &tap->dev_inuse)) continue; ret += sprintf(buf + ret, "%d %s\n", tap->minor, tap->name); } mutex_unlock(&blktap_lock); return ret; } static CLASS_ATTR(devices, S_IRUGO, blktap_sysfs_show_devices, NULL); void blktap_sysfs_exit(void) { if (class) class_destroy(class); } int __init blktap_sysfs_init(void) { struct class *cls; int err = 0; cls = class_create(THIS_MODULE, "blktap2"); if (IS_ERR(cls)) err = PTR_ERR(cls); if (!err) err = class_create_file(cls, &class_attr_verbosity); if (!err) err = class_create_file(cls, &class_attr_devices); if (!err) class = cls; else class_destroy(cls); return err; }