brd: Support for BLKFLSBUF
authorHoegeun Kwon <hoegeun.kwon@samsung.com>
Mon, 7 Nov 2022 04:11:13 +0000 (13:11 +0900)
committerHoegeun Kwon <hoegeun.kwon@samsung.com>
Wed, 23 Nov 2022 02:31:04 +0000 (11:31 +0900)
To support brd flush to free ramdisk brd_mutex is required. also to
free ramdisk memory, BLKFLSBUF support is required. this commit has
been modified to match the interface of the kernel v5.15.

This reverts commit 15f7b41f70ddcca3b555bd0fdc7c8da7466b517e.
This reverts commit ff26956875c2f05e12ecec9938411a2c7dfc767d.

Change-Id: Idde4b352924a0d8bd1c606b6da171f333dfb4f70
Signed-off-by: Hoegeun Kwon <hoegeun.kwon@samsung.com>
drivers/block/brd.c

index f6585ba..fbacf2f 100644 (file)
@@ -373,10 +373,46 @@ static int brd_rw_page(struct block_device *bdev, sector_t sector,
        return err;
 }
 
+static DEFINE_MUTEX(brd_mutex);
+static int brd_ioctl(struct block_device *bdev, fmode_t mode,
+               unsigned int cmd, unsigned long arg)
+{
+       int error;
+       struct brd_device *brd = bdev->bd_disk->private_data;
+
+       if (cmd != BLKFLSBUF)
+               return -ENOTTY;
+
+       /*
+        * ram device BLKFLSBUF has special semantics, we want to actually
+        * release and destroy the ramdisk data.
+        */
+       mutex_lock(&brd_mutex);
+       mutex_lock(&bdev->bd_disk->open_mutex);
+       error = -EBUSY;
+       if (bdev->bd_openers <= 1) {
+               /*
+                * Kill the cache first, so it isn't written back to the
+                * device.
+                *
+                * Another thread might instantiate more buffercache here,
+                * but there is not much we can do to close that race.
+                */
+               kill_bdev(bdev);
+               brd_free_pages(brd);
+               error = 0;
+       }
+       mutex_unlock(&bdev->bd_disk->open_mutex);
+       mutex_unlock(&brd_mutex);
+
+       return error;
+}
+
 static const struct block_device_operations brd_fops = {
        .owner =                THIS_MODULE,
        .submit_bio =           brd_submit_bio,
        .rw_page =              brd_rw_page,
+       .ioctl =                brd_ioctl,
 };
 
 /*