]> pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - drivers/block/loop.c
Merge branch 'origin' into devel
[linux-2.6-omap-h63xx.git] / drivers / block / loop.c
index 5c4ee70d5cf319311deab818996928af5f2736f6..2621ed2ce6d299c186391ddfb19635fc3470bf73 100644 (file)
@@ -392,8 +392,7 @@ lo_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
        struct loop_device *lo = p->lo;
        struct page *page = buf->page;
        sector_t IV;
-       size_t size;
-       int ret;
+       int size, ret;
 
        ret = buf->ops->confirm(pipe, buf);
        if (unlikely(ret))
@@ -475,10 +474,35 @@ static int do_bio_filebacked(struct loop_device *lo, struct bio *bio)
        int ret;
 
        pos = ((loff_t) bio->bi_sector << 9) + lo->lo_offset;
-       if (bio_rw(bio) == WRITE)
+
+       if (bio_rw(bio) == WRITE) {
+               int barrier = bio_barrier(bio);
+               struct file *file = lo->lo_backing_file;
+
+               if (barrier) {
+                       if (unlikely(!file->f_op->fsync)) {
+                               ret = -EOPNOTSUPP;
+                               goto out;
+                       }
+
+                       ret = vfs_fsync(file, file->f_path.dentry, 0);
+                       if (unlikely(ret)) {
+                               ret = -EIO;
+                               goto out;
+                       }
+               }
+
                ret = lo_send(lo, bio, pos);
-       else
+
+               if (barrier && !ret) {
+                       ret = vfs_fsync(file, file->f_path.dentry, 0);
+                       if (unlikely(ret))
+                               ret = -EIO;
+               }
+       } else
                ret = lo_receive(lo, bio, lo->lo_blocksize, pos);
+
+out:
        return ret;
 }
 
@@ -623,6 +647,18 @@ static int loop_switch(struct loop_device *lo, struct file *file)
        return 0;
 }
 
+/*
+ * Helper to flush the IOs in loop, but keeping loop thread running
+ */
+static int loop_flush(struct loop_device *lo)
+{
+       /* loop not yet configured, no running thread, nothing to flush */
+       if (!lo->lo_thread)
+               return 0;
+
+       return loop_switch(lo, NULL);
+}
+
 /*
  * Do the actual switch; called from the BIO completion routine
  */
@@ -630,14 +666,20 @@ static void do_loop_switch(struct loop_device *lo, struct switch_request *p)
 {
        struct file *file = p->file;
        struct file *old_file = lo->lo_backing_file;
-       struct address_space *mapping = file->f_mapping;
+       struct address_space *mapping;
 
+       /* if no new file, only flush of queued bios requested */
+       if (!file)
+               goto out;
+
+       mapping = file->f_mapping;
        mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
        lo->lo_backing_file = file;
        lo->lo_blocksize = S_ISBLK(mapping->host->i_mode) ?
                mapping->host->i_bdev->bd_block_size : PAGE_SIZE;
        lo->old_gfp_mask = mapping_gfp_mask(mapping);
        mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
+out:
        complete(&p->wait);
 }
 
@@ -809,6 +851,9 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
        lo->lo_queue->queuedata = lo;
        lo->lo_queue->unplug_fn = loop_unplug;
 
+       if (!(lo_flags & LO_FLAGS_READ_ONLY) && file->f_op->fsync)
+               blk_queue_ordered(lo->lo_queue, QUEUE_ORDERED_DRAIN, NULL);
+
        set_capacity(lo->lo_disk, size);
        bd_set_size(bdev, size << 9);
 
@@ -901,6 +946,7 @@ static int loop_clr_fd(struct loop_device *lo, struct block_device *bdev)
 
        kthread_stop(lo->lo_thread);
 
+       lo->lo_queue->unplug_fn = NULL;
        lo->lo_backing_file = NULL;
 
        loop_release_xfer(lo);
@@ -923,11 +969,18 @@ static int loop_clr_fd(struct loop_device *lo, struct block_device *bdev)
                bd_set_size(bdev, 0);
        mapping_set_gfp_mask(filp->f_mapping, gfp);
        lo->lo_state = Lo_unbound;
-       fput(filp);
        /* This is safe: open() is still holding a reference. */
        module_put(THIS_MODULE);
        if (max_part > 0)
                ioctl_by_bdev(bdev, BLKRRPART, 0);
+       mutex_unlock(&lo->lo_ctl_mutex);
+       /*
+        * Need not hold lo_ctl_mutex to fput backing file.
+        * Calling fput holding lo_ctl_mutex triggers a circular
+        * lock dependency possibility warning as fput can take
+        * bd_mutex which is usually taken before lo_ctl_mutex.
+        */
+       fput(filp);
        return 0;
 }
 
@@ -936,8 +989,10 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info)
 {
        int err;
        struct loop_func_table *xfer;
+       uid_t uid = current_uid();
 
-       if (lo->lo_encrypt_key_size && lo->lo_key_owner != current->uid &&
+       if (lo->lo_encrypt_key_size &&
+           lo->lo_key_owner != uid &&
            !capable(CAP_SYS_ADMIN))
                return -EPERM;
        if (lo->lo_state != Lo_bound)
@@ -992,7 +1047,7 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info)
        if (info->lo_encrypt_key_size) {
                memcpy(lo->lo_encrypt_key, info->lo_encrypt_key,
                       info->lo_encrypt_key_size);
-               lo->lo_key_owner = current->uid;
+               lo->lo_key_owner = uid;
        }       
 
        return 0;
@@ -1143,7 +1198,7 @@ static int lo_ioctl(struct block_device *bdev, fmode_t mode,
        struct loop_device *lo = bdev->bd_disk->private_data;
        int err;
 
-       mutex_lock(&lo->lo_ctl_mutex);
+       mutex_lock_nested(&lo->lo_ctl_mutex, 1);
        switch (cmd) {
        case LOOP_SET_FD:
                err = loop_set_fd(lo, mode, bdev, arg);
@@ -1152,7 +1207,10 @@ static int lo_ioctl(struct block_device *bdev, fmode_t mode,
                err = loop_change_fd(lo, bdev, arg);
                break;
        case LOOP_CLR_FD:
+               /* loop_clr_fd would have unlocked lo_ctl_mutex on success */
                err = loop_clr_fd(lo, bdev);
+               if (!err)
+                       goto out_unlocked;
                break;
        case LOOP_SET_STATUS:
                err = loop_set_status_old(lo, (struct loop_info __user *) arg);
@@ -1170,6 +1228,8 @@ static int lo_ioctl(struct block_device *bdev, fmode_t mode,
                err = lo->ioctl ? lo->ioctl(lo, cmd, arg) : -EINVAL;
        }
        mutex_unlock(&lo->lo_ctl_mutex);
+
+out_unlocked:
        return err;
 }
 
@@ -1343,11 +1403,25 @@ static int lo_release(struct gendisk *disk, fmode_t mode)
        struct loop_device *lo = disk->private_data;
 
        mutex_lock(&lo->lo_ctl_mutex);
-       --lo->lo_refcnt;
 
-       if ((lo->lo_flags & LO_FLAGS_AUTOCLEAR) && !lo->lo_refcnt)
+       if (--lo->lo_refcnt)
+               goto out;
+
+       if (lo->lo_flags & LO_FLAGS_AUTOCLEAR) {
+               /*
+                * In autoclear mode, stop the loop thread
+                * and remove configuration after last close.
+                */
                loop_clr_fd(lo, NULL);
+       } else {
+               /*
+                * Otherwise keep thread (if running) and config,
+                * but flush possible ongoing bios in thread.
+                */
+               loop_flush(lo);
+       }
 
+out:
        mutex_unlock(&lo->lo_ctl_mutex);
 
        return 0;