]> bbs.cooldavid.org Git - net-next-2.6.git/blobdiff - drivers/block/loop.c
splice: divorce the splice structure/function definitions from the pipe header
[net-next-2.6.git] / drivers / block / loop.c
index 18cdd8c7762693d659bcc58c6e42b20e7b0e39dc..08f53df03e8cad09f73bca23c5d8fd477b69e9e1 100644 (file)
@@ -74,6 +74,7 @@
 #include <linux/highmem.h>
 #include <linux/gfp.h>
 #include <linux/kthread.h>
+#include <linux/splice.h>
 
 #include <asm/uaccess.h>
 
@@ -401,50 +402,73 @@ struct lo_read_data {
 };
 
 static int
-lo_read_actor(read_descriptor_t *desc, struct page *page,
-             unsigned long offset, unsigned long size)
+lo_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
+               struct splice_desc *sd)
 {
-       unsigned long count = desc->count;
-       struct lo_read_data *p = desc->arg.data;
+       struct lo_read_data *p = sd->u.data;
        struct loop_device *lo = p->lo;
+       struct page *page = buf->page;
        sector_t IV;
+       size_t size;
+       int ret;
 
-       IV = ((sector_t) page->index << (PAGE_CACHE_SHIFT - 9))+(offset >> 9);
+       ret = buf->ops->pin(pipe, buf);
+       if (unlikely(ret))
+               return ret;
 
-       if (size > count)
-               size = count;
+       IV = ((sector_t) page->index << (PAGE_CACHE_SHIFT - 9)) +
+                                                       (buf->offset >> 9);
+       size = sd->len;
+       if (size > p->bsize)
+               size = p->bsize;
 
-       if (lo_do_transfer(lo, READ, page, offset, p->page, p->offset, size, IV)) {
-               size = 0;
+       if (lo_do_transfer(lo, READ, page, buf->offset, p->page, p->offset, size, IV)) {
                printk(KERN_ERR "loop: transfer error block %ld\n",
                       page->index);
-               desc->error = -EINVAL;
+               size = -EINVAL;
        }
 
        flush_dcache_page(p->page);
 
-       desc->count = count - size;
-       desc->written += size;
-       p->offset += size;
+       if (size > 0)
+               p->offset += size;
+
        return size;
 }
 
+static int
+lo_direct_splice_actor(struct pipe_inode_info *pipe, struct splice_desc *sd)
+{
+       return __splice_from_pipe(pipe, sd, lo_splice_actor);
+}
+
 static int
 do_lo_receive(struct loop_device *lo,
              struct bio_vec *bvec, int bsize, loff_t pos)
 {
        struct lo_read_data cookie;
+       struct splice_desc sd;
        struct file *file;
-       int retval;
+       long retval;
 
        cookie.lo = lo;
        cookie.page = bvec->bv_page;
        cookie.offset = bvec->bv_offset;
        cookie.bsize = bsize;
+
+       sd.len = 0;
+       sd.total_len = bvec->bv_len;
+       sd.flags = 0;
+       sd.pos = pos;
+       sd.u.data = &cookie;
+
        file = lo->lo_backing_file;
-       retval = file->f_op->sendfile(file, &pos, bvec->bv_len,
-                       lo_read_actor, &cookie);
-       return (retval < 0)? retval: 0;
+       retval = splice_direct_to_actor(file, &sd, lo_direct_splice_actor);
+
+       if (retval < 0)
+               return retval;
+
+       return 0;
 }
 
 static int
@@ -679,8 +703,8 @@ static int loop_change_fd(struct loop_device *lo, struct file *lo_file,
        if (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))
                goto out_putf;
 
-       /* new backing store needs to support loop (eg sendfile) */
-       if (!inode->i_fop->sendfile)
+       /* new backing store needs to support loop (eg splice_read) */
+       if (!inode->i_fop->splice_read)
                goto out_putf;
 
        /* size of the new backing store needs to be the same */
@@ -760,7 +784,7 @@ static int loop_set_fd(struct loop_device *lo, struct file *lo_file,
                 * If we can't read - sorry. If we only can't write - well,
                 * it's going to be read-only.
                 */
-               if (!file->f_op->sendfile)
+               if (!file->f_op->splice_read)
                        goto out_putf;
                if (aops->prepare_write && aops->commit_write)
                        lo_flags |= LO_FLAGS_USE_AOPS;
@@ -1317,18 +1341,6 @@ static long lo_compat_ioctl(struct file *file, unsigned int cmd, unsigned long a
 }
 #endif
 
-static struct loop_device *loop_find_dev(int number)
-{
-       struct loop_device *lo;
-
-       list_for_each_entry(lo, &loop_devices, lo_list) {
-               if (lo->lo_number == number)
-                       return lo;
-       }
-       return NULL;
-}
-
-static struct loop_device *loop_init_one(int i);
 static int lo_open(struct inode *inode, struct file *file)
 {
        struct loop_device *lo = inode->i_bdev->bd_disk->private_data;
@@ -1337,11 +1349,6 @@ static int lo_open(struct inode *inode, struct file *file)
        lo->lo_refcnt++;
        mutex_unlock(&lo->lo_ctl_mutex);
 
-       mutex_lock(&loop_devices_mutex);
-       if (!loop_find_dev(lo->lo_number + 1))
-               loop_init_one(lo->lo_number + 1);
-       mutex_unlock(&loop_devices_mutex);
-
        return 0;
 }
 
@@ -1371,7 +1378,7 @@ static struct block_device_operations lo_fops = {
  */
 static int max_loop;
 module_param(max_loop, int, 0);
-MODULE_PARM_DESC(max_loop, "obsolete, loop device is created on-demand");
+MODULE_PARM_DESC(max_loop, "Maximum number of loop devices");
 MODULE_LICENSE("GPL");
 MODULE_ALIAS_BLOCKDEV_MAJOR(LOOP_MAJOR);
 
@@ -1411,7 +1418,7 @@ int loop_unregister_transfer(int number)
 EXPORT_SYMBOL(loop_register_transfer);
 EXPORT_SYMBOL(loop_unregister_transfer);
 
-static struct loop_device *loop_init_one(int i)
+static struct loop_device *loop_alloc(int i)
 {
        struct loop_device *lo;
        struct gendisk *disk;
@@ -1439,8 +1446,6 @@ static struct loop_device *loop_init_one(int i)
        disk->private_data      = lo;
        disk->queue             = lo->lo_queue;
        sprintf(disk->disk_name, "loop%d", i);
-       add_disk(disk);
-       list_add_tail(&lo->lo_list, &loop_devices);
        return lo;
 
 out_free_queue:
@@ -1448,71 +1453,127 @@ out_free_queue:
 out_free_dev:
        kfree(lo);
 out:
-       return ERR_PTR(-ENOMEM);
+       return NULL;
 }
 
-static void loop_del_one(struct loop_device *lo)
+static void loop_free(struct loop_device *lo)
 {
-       del_gendisk(lo->lo_disk);
        blk_cleanup_queue(lo->lo_queue);
        put_disk(lo->lo_disk);
        list_del(&lo->lo_list);
        kfree(lo);
 }
 
+static struct loop_device *loop_init_one(int i)
+{
+       struct loop_device *lo;
+
+       list_for_each_entry(lo, &loop_devices, lo_list) {
+               if (lo->lo_number == i)
+                       return lo;
+       }
+
+       lo = loop_alloc(i);
+       if (lo) {
+               add_disk(lo->lo_disk);
+               list_add_tail(&lo->lo_list, &loop_devices);
+       }
+       return lo;
+}
+
+static void loop_del_one(struct loop_device *lo)
+{
+       del_gendisk(lo->lo_disk);
+       loop_free(lo);
+}
+
 static struct kobject *loop_probe(dev_t dev, int *part, void *data)
 {
-       unsigned int number = dev & MINORMASK;
        struct loop_device *lo;
+       struct kobject *kobj;
 
        mutex_lock(&loop_devices_mutex);
-       lo = loop_find_dev(number);
-       if (lo == NULL)
-               lo = loop_init_one(number);
+       lo = loop_init_one(dev & MINORMASK);
+       kobj = lo ? get_disk(lo->lo_disk) : ERR_PTR(-ENOMEM);
        mutex_unlock(&loop_devices_mutex);
 
        *part = 0;
-       if (IS_ERR(lo))
-               return (void *)lo;
-       else
-               return &lo->lo_disk->kobj;
+       return kobj;
 }
 
 static int __init loop_init(void)
 {
-       struct loop_device *lo;
+       int i, nr;
+       unsigned long range;
+       struct loop_device *lo, *next;
+
+       /*
+        * loop module now has a feature to instantiate underlying device
+        * structure on-demand, provided that there is an access dev node.
+        * However, this will not work well with user space tool that doesn't
+        * know about such "feature".  In order to not break any existing
+        * tool, we do the following:
+        *
+        * (1) if max_loop is specified, create that many upfront, and this
+        *     also becomes a hard limit.
+        * (2) if max_loop is not specified, create 8 loop device on module
+        *     load, user can further extend loop device by create dev node
+        *     themselves and have kernel automatically instantiate actual
+        *     device on-demand.
+        */
+       if (max_loop > 1UL << MINORBITS)
+               return -EINVAL;
+
+       if (max_loop) {
+               nr = max_loop;
+               range = max_loop;
+       } else {
+               nr = 8;
+               range = 1UL << MINORBITS;
+       }
 
        if (register_blkdev(LOOP_MAJOR, "loop"))
                return -EIO;
-       blk_register_region(MKDEV(LOOP_MAJOR, 0), 1UL << MINORBITS,
-                                 THIS_MODULE, loop_probe, NULL, NULL);
 
-       lo = loop_init_one(0);
-       if (IS_ERR(lo))
-               goto out;
+       for (i = 0; i < nr; i++) {
+               lo = loop_alloc(i);
+               if (!lo)
+                       goto Enomem;
+               list_add_tail(&lo->lo_list, &loop_devices);
+       }
 
-       if (max_loop) {
-               printk(KERN_INFO "loop: the max_loop option is obsolete "
-                                "and will be removed in March 2008\n");
+       /* point of no return */
+
+       list_for_each_entry(lo, &loop_devices, lo_list)
+               add_disk(lo->lo_disk);
+
+       blk_register_region(MKDEV(LOOP_MAJOR, 0), range,
+                                 THIS_MODULE, loop_probe, NULL, NULL);
 
-       }
        printk(KERN_INFO "loop: module loaded\n");
        return 0;
 
-out:
+Enomem:
+       printk(KERN_INFO "loop: out of memory\n");
+
+       list_for_each_entry_safe(lo, next, &loop_devices, lo_list)
+               loop_free(lo);
+
        unregister_blkdev(LOOP_MAJOR, "loop");
-       printk(KERN_ERR "loop: ran out of memory\n");
        return -ENOMEM;
 }
 
 static void __exit loop_exit(void)
 {
+       unsigned long range;
        struct loop_device *lo, *next;
 
+       range = max_loop ? max_loop :  1UL << MINORBITS;
+
        list_for_each_entry_safe(lo, next, &loop_devices, lo_list)
                loop_del_one(lo);
 
-       blk_unregister_region(MKDEV(LOOP_MAJOR, 0), 1UL << MINORBITS);
+       blk_unregister_region(MKDEV(LOOP_MAJOR, 0), range);
        if (unregister_blkdev(LOOP_MAJOR, "loop"))
                printk(KERN_WARNING "loop: cannot unregister blkdev\n");
 }