Thomas Gleixner | 5b497af | 2019-05-29 07:18:09 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright(c) 2017 Intel Corporation. All rights reserved. |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 4 | */ |
| 5 | #include <linux/pagemap.h> |
| 6 | #include <linux/module.h> |
| 7 | #include <linux/mount.h> |
David Howells | 75d4e06 | 2019-03-25 16:38:24 +0000 | [diff] [blame] | 8 | #include <linux/pseudo_fs.h> |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 9 | #include <linux/magic.h> |
Dan Williams | 569d036 | 2017-10-14 11:33:32 -0700 | [diff] [blame] | 10 | #include <linux/pfn_t.h> |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 11 | #include <linux/cdev.h> |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 12 | #include <linux/slab.h> |
Dan Williams | 7e026c8 | 2017-05-29 12:57:56 -0700 | [diff] [blame] | 13 | #include <linux/uio.h> |
Dan Williams | 6568b08 | 2017-01-24 18:44:18 -0800 | [diff] [blame] | 14 | #include <linux/dax.h> |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 15 | #include <linux/fs.h> |
Dan Williams | 51cf784 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 16 | #include "dax-private.h" |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 17 | |
Christoph Hellwig | 1b76460 | 2021-08-26 15:55:05 +0200 | [diff] [blame] | 18 | /** |
| 19 | * struct dax_device - anchor object for dax services |
| 20 | * @inode: core vfs |
| 21 | * @cdev: optional character interface for "device dax" |
Christoph Hellwig | 1b76460 | 2021-08-26 15:55:05 +0200 | [diff] [blame] | 22 | * @private: dax driver private data |
| 23 | * @flags: state and boolean properties |
| 24 | */ |
| 25 | struct dax_device { |
Christoph Hellwig | 1b76460 | 2021-08-26 15:55:05 +0200 | [diff] [blame] | 26 | struct inode inode; |
| 27 | struct cdev cdev; |
Christoph Hellwig | 1b76460 | 2021-08-26 15:55:05 +0200 | [diff] [blame] | 28 | void *private; |
| 29 | unsigned long flags; |
| 30 | const struct dax_operations *ops; |
| 31 | }; |
| 32 | |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 33 | static dev_t dax_devt; |
| 34 | DEFINE_STATIC_SRCU(dax_srcu); |
| 35 | static struct vfsmount *dax_mnt; |
| 36 | static DEFINE_IDA(dax_minor_ida); |
| 37 | static struct kmem_cache *dax_cache __read_mostly; |
| 38 | static struct super_block *dax_superblock __read_mostly; |
| 39 | |
| 40 | int dax_read_lock(void) |
| 41 | { |
| 42 | return srcu_read_lock(&dax_srcu); |
| 43 | } |
| 44 | EXPORT_SYMBOL_GPL(dax_read_lock); |
| 45 | |
| 46 | void dax_read_unlock(int id) |
| 47 | { |
| 48 | srcu_read_unlock(&dax_srcu, id); |
| 49 | } |
| 50 | EXPORT_SYMBOL_GPL(dax_read_unlock); |
| 51 | |
Christoph Hellwig | 5d2a228 | 2021-11-29 11:21:36 +0100 | [diff] [blame] | 52 | #if defined(CONFIG_BLOCK) && defined(CONFIG_FS_DAX) |
Christoph Hellwig | e765f13 | 2021-09-22 19:34:30 +0200 | [diff] [blame] | 53 | #include <linux/blkdev.h> |
| 54 | |
Christoph Hellwig | fb08a19 | 2021-11-29 11:21:38 +0100 | [diff] [blame] | 55 | static DEFINE_XARRAY(dax_hosts); |
| 56 | |
| 57 | int dax_add_host(struct dax_device *dax_dev, struct gendisk *disk) |
| 58 | { |
| 59 | return xa_insert(&dax_hosts, (unsigned long)disk, dax_dev, GFP_KERNEL); |
| 60 | } |
| 61 | EXPORT_SYMBOL_GPL(dax_add_host); |
| 62 | |
| 63 | void dax_remove_host(struct gendisk *disk) |
| 64 | { |
| 65 | xa_erase(&dax_hosts, (unsigned long)disk); |
| 66 | } |
| 67 | EXPORT_SYMBOL_GPL(dax_remove_host); |
| 68 | |
Christoph Hellwig | 1b76460 | 2021-08-26 15:55:05 +0200 | [diff] [blame] | 69 | /** |
Christoph Hellwig | fb08a19 | 2021-11-29 11:21:38 +0100 | [diff] [blame] | 70 | * fs_dax_get_by_bdev() - temporary lookup mechanism for filesystem-dax |
| 71 | * @bdev: block device to find a dax_device for |
Christoph Hellwig | cd913c7 | 2021-11-29 11:21:59 +0100 | [diff] [blame] | 72 | * @start_off: returns the byte offset into the dax_device that @bdev starts |
Christoph Hellwig | 1b76460 | 2021-08-26 15:55:05 +0200 | [diff] [blame] | 73 | */ |
Christoph Hellwig | cd913c7 | 2021-11-29 11:21:59 +0100 | [diff] [blame] | 74 | struct dax_device *fs_dax_get_by_bdev(struct block_device *bdev, u64 *start_off) |
Dan Williams | 78f3547 | 2017-08-30 09:16:38 -0700 | [diff] [blame] | 75 | { |
Christoph Hellwig | fb08a19 | 2021-11-29 11:21:38 +0100 | [diff] [blame] | 76 | struct dax_device *dax_dev; |
Christoph Hellwig | cd913c7 | 2021-11-29 11:21:59 +0100 | [diff] [blame] | 77 | u64 part_size; |
Christoph Hellwig | fb08a19 | 2021-11-29 11:21:38 +0100 | [diff] [blame] | 78 | int id; |
| 79 | |
Christoph Hellwig | e556f6b | 2020-06-26 10:01:56 +0200 | [diff] [blame] | 80 | if (!blk_queue_dax(bdev->bd_disk->queue)) |
Dan Williams | 78f3547 | 2017-08-30 09:16:38 -0700 | [diff] [blame] | 81 | return NULL; |
Christoph Hellwig | fb08a19 | 2021-11-29 11:21:38 +0100 | [diff] [blame] | 82 | |
Christoph Hellwig | cd913c7 | 2021-11-29 11:21:59 +0100 | [diff] [blame] | 83 | *start_off = get_start_sect(bdev) * SECTOR_SIZE; |
| 84 | part_size = bdev_nr_sectors(bdev) * SECTOR_SIZE; |
| 85 | if (*start_off % PAGE_SIZE || part_size % PAGE_SIZE) { |
Christoph Hellwig | 0c44587 | 2021-11-29 11:21:40 +0100 | [diff] [blame] | 86 | pr_info("%pg: error: unaligned partition for dax\n", bdev); |
| 87 | return NULL; |
| 88 | } |
| 89 | |
Christoph Hellwig | fb08a19 | 2021-11-29 11:21:38 +0100 | [diff] [blame] | 90 | id = dax_read_lock(); |
| 91 | dax_dev = xa_load(&dax_hosts, (unsigned long)bdev->bd_disk); |
| 92 | if (!dax_dev || !dax_alive(dax_dev) || !igrab(&dax_dev->inode)) |
| 93 | dax_dev = NULL; |
| 94 | dax_read_unlock(id); |
| 95 | |
| 96 | return dax_dev; |
Dan Williams | 78f3547 | 2017-08-30 09:16:38 -0700 | [diff] [blame] | 97 | } |
| 98 | EXPORT_SYMBOL_GPL(fs_dax_get_by_bdev); |
Christoph Hellwig | 5d2a228 | 2021-11-29 11:21:36 +0100 | [diff] [blame] | 99 | #endif /* CONFIG_BLOCK && CONFIG_FS_DAX */ |
Dan Williams | ef51042 | 2017-05-08 10:55:27 -0700 | [diff] [blame] | 100 | |
Dan Williams | 9a60c3e | 2017-06-27 17:59:28 -0700 | [diff] [blame] | 101 | enum dax_device_flags { |
| 102 | /* !alive + rcu grace period == no new operations / mappings */ |
| 103 | DAXDEV_ALIVE, |
Dan Williams | 6e0c90d | 2017-06-26 21:28:41 -0700 | [diff] [blame] | 104 | /* gate whether dax_flush() calls the low level flush routine */ |
| 105 | DAXDEV_WRITE_CACHE, |
Pankaj Gupta | fefc1d9 | 2019-07-05 19:33:24 +0530 | [diff] [blame] | 106 | /* flag to check if device supports synchronous flush */ |
| 107 | DAXDEV_SYNC, |
Christoph Hellwig | 7ac5360 | 2021-12-15 09:45:08 +0100 | [diff] [blame] | 108 | /* do not leave the caches dirty after writes */ |
| 109 | DAXDEV_NOCACHE, |
| 110 | /* handle CPU fetch exceptions during reads */ |
| 111 | DAXDEV_NOMC, |
Dan Williams | 9a60c3e | 2017-06-27 17:59:28 -0700 | [diff] [blame] | 112 | }; |
| 113 | |
Dan Williams | b068626 | 2017-01-26 20:37:35 -0800 | [diff] [blame] | 114 | /** |
| 115 | * dax_direct_access() - translate a device pgoff to an absolute pfn |
| 116 | * @dax_dev: a dax_device instance representing the logical memory range |
| 117 | * @pgoff: offset in pages from the start of the device to translate |
| 118 | * @nr_pages: number of consecutive pages caller can handle relative to @pfn |
| 119 | * @kaddr: output parameter that returns a virtual address mapping of pfn |
| 120 | * @pfn: output parameter that returns an absolute pfn translation of @pgoff |
| 121 | * |
| 122 | * Return: negative errno if an error occurs, otherwise the number of |
| 123 | * pages accessible at the device relative @pgoff. |
| 124 | */ |
| 125 | long dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff, long nr_pages, |
| 126 | void **kaddr, pfn_t *pfn) |
| 127 | { |
| 128 | long avail; |
| 129 | |
Dan Williams | b068626 | 2017-01-26 20:37:35 -0800 | [diff] [blame] | 130 | if (!dax_dev) |
| 131 | return -EOPNOTSUPP; |
| 132 | |
| 133 | if (!dax_alive(dax_dev)) |
| 134 | return -ENXIO; |
| 135 | |
| 136 | if (nr_pages < 0) |
Ira Weiny | b05d4c5 | 2021-05-25 10:24:28 -0700 | [diff] [blame] | 137 | return -EINVAL; |
Dan Williams | b068626 | 2017-01-26 20:37:35 -0800 | [diff] [blame] | 138 | |
| 139 | avail = dax_dev->ops->direct_access(dax_dev, pgoff, nr_pages, |
| 140 | kaddr, pfn); |
| 141 | if (!avail) |
| 142 | return -ERANGE; |
| 143 | return min(avail, nr_pages); |
| 144 | } |
| 145 | EXPORT_SYMBOL_GPL(dax_direct_access); |
| 146 | |
Dan Williams | 7e026c8 | 2017-05-29 12:57:56 -0700 | [diff] [blame] | 147 | size_t dax_copy_from_iter(struct dax_device *dax_dev, pgoff_t pgoff, void *addr, |
| 148 | size_t bytes, struct iov_iter *i) |
| 149 | { |
| 150 | if (!dax_alive(dax_dev)) |
| 151 | return 0; |
| 152 | |
Christoph Hellwig | 7ac5360 | 2021-12-15 09:45:08 +0100 | [diff] [blame] | 153 | /* |
| 154 | * The userspace address for the memory copy has already been validated |
| 155 | * via access_ok() in vfs_write, so use the 'no check' version to bypass |
| 156 | * the HARDENED_USERCOPY overhead. |
| 157 | */ |
| 158 | if (test_bit(DAXDEV_NOCACHE, &dax_dev->flags)) |
| 159 | return _copy_from_iter_flushcache(addr, bytes, i); |
| 160 | return _copy_from_iter(addr, bytes, i); |
Dan Williams | 7e026c8 | 2017-05-29 12:57:56 -0700 | [diff] [blame] | 161 | } |
Dan Williams | 7e026c8 | 2017-05-29 12:57:56 -0700 | [diff] [blame] | 162 | |
Dan Williams | b3a9a0c | 2018-05-02 06:46:33 -0700 | [diff] [blame] | 163 | size_t dax_copy_to_iter(struct dax_device *dax_dev, pgoff_t pgoff, void *addr, |
| 164 | size_t bytes, struct iov_iter *i) |
| 165 | { |
| 166 | if (!dax_alive(dax_dev)) |
| 167 | return 0; |
| 168 | |
Christoph Hellwig | 7ac5360 | 2021-12-15 09:45:08 +0100 | [diff] [blame] | 169 | /* |
| 170 | * The userspace address for the memory copy has already been validated |
| 171 | * via access_ok() in vfs_red, so use the 'no check' version to bypass |
| 172 | * the HARDENED_USERCOPY overhead. |
| 173 | */ |
| 174 | if (test_bit(DAXDEV_NOMC, &dax_dev->flags)) |
| 175 | return _copy_mc_to_iter(addr, bytes, i); |
| 176 | return _copy_to_iter(addr, bytes, i); |
Dan Williams | b3a9a0c | 2018-05-02 06:46:33 -0700 | [diff] [blame] | 177 | } |
Dan Williams | b3a9a0c | 2018-05-02 06:46:33 -0700 | [diff] [blame] | 178 | |
Vivek Goyal | f605a26 | 2020-02-28 11:34:52 -0500 | [diff] [blame] | 179 | int dax_zero_page_range(struct dax_device *dax_dev, pgoff_t pgoff, |
| 180 | size_t nr_pages) |
| 181 | { |
| 182 | if (!dax_alive(dax_dev)) |
| 183 | return -ENXIO; |
Vivek Goyal | f605a26 | 2020-02-28 11:34:52 -0500 | [diff] [blame] | 184 | /* |
| 185 | * There are no callers that want to zero more than one page as of now. |
| 186 | * Once users are there, this check can be removed after the |
| 187 | * device mapper code has been updated to split ranges across targets. |
| 188 | */ |
| 189 | if (nr_pages != 1) |
| 190 | return -EIO; |
| 191 | |
| 192 | return dax_dev->ops->zero_page_range(dax_dev, pgoff, nr_pages); |
| 193 | } |
| 194 | EXPORT_SYMBOL_GPL(dax_zero_page_range); |
| 195 | |
Mikulas Patocka | c3ca015 | 2017-08-31 21:47:43 -0400 | [diff] [blame] | 196 | #ifdef CONFIG_ARCH_HAS_PMEM_API |
| 197 | void arch_wb_cache_pmem(void *addr, size_t size); |
| 198 | void dax_flush(struct dax_device *dax_dev, void *addr, size_t size) |
Dan Williams | abebfbe2 | 2017-05-29 13:02:52 -0700 | [diff] [blame] | 199 | { |
Ross Zwisler | 808c340 | 2018-06-06 10:45:14 -0600 | [diff] [blame] | 200 | if (unlikely(!dax_write_cache_enabled(dax_dev))) |
Dan Williams | 6e0c90d | 2017-06-26 21:28:41 -0700 | [diff] [blame] | 201 | return; |
| 202 | |
Mikulas Patocka | c3ca015 | 2017-08-31 21:47:43 -0400 | [diff] [blame] | 203 | arch_wb_cache_pmem(addr, size); |
Dan Williams | abebfbe2 | 2017-05-29 13:02:52 -0700 | [diff] [blame] | 204 | } |
Mikulas Patocka | c3ca015 | 2017-08-31 21:47:43 -0400 | [diff] [blame] | 205 | #else |
| 206 | void dax_flush(struct dax_device *dax_dev, void *addr, size_t size) |
| 207 | { |
| 208 | } |
| 209 | #endif |
Dan Williams | abebfbe2 | 2017-05-29 13:02:52 -0700 | [diff] [blame] | 210 | EXPORT_SYMBOL_GPL(dax_flush); |
| 211 | |
Dan Williams | 6e0c90d | 2017-06-26 21:28:41 -0700 | [diff] [blame] | 212 | void dax_write_cache(struct dax_device *dax_dev, bool wc) |
| 213 | { |
| 214 | if (wc) |
| 215 | set_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags); |
| 216 | else |
| 217 | clear_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags); |
| 218 | } |
| 219 | EXPORT_SYMBOL_GPL(dax_write_cache); |
| 220 | |
Vivek Goyal | 273752c | 2017-07-26 09:35:09 -0400 | [diff] [blame] | 221 | bool dax_write_cache_enabled(struct dax_device *dax_dev) |
| 222 | { |
| 223 | return test_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags); |
| 224 | } |
| 225 | EXPORT_SYMBOL_GPL(dax_write_cache_enabled); |
| 226 | |
Christoph Hellwig | fd1d00e | 2021-12-15 09:45:06 +0100 | [diff] [blame] | 227 | bool dax_synchronous(struct dax_device *dax_dev) |
Pankaj Gupta | fefc1d9 | 2019-07-05 19:33:24 +0530 | [diff] [blame] | 228 | { |
| 229 | return test_bit(DAXDEV_SYNC, &dax_dev->flags); |
| 230 | } |
Christoph Hellwig | fd1d00e | 2021-12-15 09:45:06 +0100 | [diff] [blame] | 231 | EXPORT_SYMBOL_GPL(dax_synchronous); |
Pankaj Gupta | fefc1d9 | 2019-07-05 19:33:24 +0530 | [diff] [blame] | 232 | |
Christoph Hellwig | fd1d00e | 2021-12-15 09:45:06 +0100 | [diff] [blame] | 233 | void set_dax_synchronous(struct dax_device *dax_dev) |
Pankaj Gupta | fefc1d9 | 2019-07-05 19:33:24 +0530 | [diff] [blame] | 234 | { |
| 235 | set_bit(DAXDEV_SYNC, &dax_dev->flags); |
| 236 | } |
Christoph Hellwig | fd1d00e | 2021-12-15 09:45:06 +0100 | [diff] [blame] | 237 | EXPORT_SYMBOL_GPL(set_dax_synchronous); |
Pankaj Gupta | fefc1d9 | 2019-07-05 19:33:24 +0530 | [diff] [blame] | 238 | |
Christoph Hellwig | 7ac5360 | 2021-12-15 09:45:08 +0100 | [diff] [blame] | 239 | void set_dax_nocache(struct dax_device *dax_dev) |
| 240 | { |
| 241 | set_bit(DAXDEV_NOCACHE, &dax_dev->flags); |
| 242 | } |
| 243 | EXPORT_SYMBOL_GPL(set_dax_nocache); |
| 244 | |
| 245 | void set_dax_nomc(struct dax_device *dax_dev) |
| 246 | { |
| 247 | set_bit(DAXDEV_NOMC, &dax_dev->flags); |
| 248 | } |
| 249 | EXPORT_SYMBOL_GPL(set_dax_nomc); |
| 250 | |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 251 | bool dax_alive(struct dax_device *dax_dev) |
| 252 | { |
| 253 | lockdep_assert_held(&dax_srcu); |
Dan Williams | 9a60c3e | 2017-06-27 17:59:28 -0700 | [diff] [blame] | 254 | return test_bit(DAXDEV_ALIVE, &dax_dev->flags); |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 255 | } |
| 256 | EXPORT_SYMBOL_GPL(dax_alive); |
| 257 | |
| 258 | /* |
| 259 | * Note, rcu is not protecting the liveness of dax_dev, rcu is ensuring |
| 260 | * that any fault handlers or operations that might have seen |
| 261 | * dax_alive(), have completed. Any operations that start after |
| 262 | * synchronize_srcu() has run will abort upon seeing !dax_alive(). |
| 263 | */ |
| 264 | void kill_dax(struct dax_device *dax_dev) |
| 265 | { |
| 266 | if (!dax_dev) |
| 267 | return; |
| 268 | |
Dan Williams | 9a60c3e | 2017-06-27 17:59:28 -0700 | [diff] [blame] | 269 | clear_bit(DAXDEV_ALIVE, &dax_dev->flags); |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 270 | synchronize_srcu(&dax_srcu); |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 271 | } |
| 272 | EXPORT_SYMBOL_GPL(kill_dax); |
| 273 | |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 274 | void run_dax(struct dax_device *dax_dev) |
| 275 | { |
| 276 | set_bit(DAXDEV_ALIVE, &dax_dev->flags); |
| 277 | } |
| 278 | EXPORT_SYMBOL_GPL(run_dax); |
| 279 | |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 280 | static struct inode *dax_alloc_inode(struct super_block *sb) |
| 281 | { |
| 282 | struct dax_device *dax_dev; |
Dan Williams | b9d39d1 | 2017-06-09 08:50:49 -0700 | [diff] [blame] | 283 | struct inode *inode; |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 284 | |
| 285 | dax_dev = kmem_cache_alloc(dax_cache, GFP_KERNEL); |
Mikulas Patocka | 9f586ff | 2017-11-14 09:59:54 -0500 | [diff] [blame] | 286 | if (!dax_dev) |
| 287 | return NULL; |
| 288 | |
Dan Williams | b9d39d1 | 2017-06-09 08:50:49 -0700 | [diff] [blame] | 289 | inode = &dax_dev->inode; |
| 290 | inode->i_rdev = 0; |
| 291 | return inode; |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | static struct dax_device *to_dax_dev(struct inode *inode) |
| 295 | { |
| 296 | return container_of(inode, struct dax_device, inode); |
| 297 | } |
| 298 | |
Al Viro | 53e2282 | 2019-04-10 14:57:19 -0400 | [diff] [blame] | 299 | static void dax_free_inode(struct inode *inode) |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 300 | { |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 301 | struct dax_device *dax_dev = to_dax_dev(inode); |
Dan Williams | b9d39d1 | 2017-06-09 08:50:49 -0700 | [diff] [blame] | 302 | if (inode->i_rdev) |
Al Viro | 6f24784 | 2021-01-31 19:23:55 -0500 | [diff] [blame] | 303 | ida_simple_remove(&dax_minor_ida, iminor(inode)); |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 304 | kmem_cache_free(dax_cache, dax_dev); |
| 305 | } |
| 306 | |
| 307 | static void dax_destroy_inode(struct inode *inode) |
| 308 | { |
| 309 | struct dax_device *dax_dev = to_dax_dev(inode); |
Dan Williams | 9a60c3e | 2017-06-27 17:59:28 -0700 | [diff] [blame] | 310 | WARN_ONCE(test_bit(DAXDEV_ALIVE, &dax_dev->flags), |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 311 | "kill_dax() must be called before final iput()\n"); |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | static const struct super_operations dax_sops = { |
| 315 | .statfs = simple_statfs, |
| 316 | .alloc_inode = dax_alloc_inode, |
| 317 | .destroy_inode = dax_destroy_inode, |
Al Viro | 53e2282 | 2019-04-10 14:57:19 -0400 | [diff] [blame] | 318 | .free_inode = dax_free_inode, |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 319 | .drop_inode = generic_delete_inode, |
| 320 | }; |
| 321 | |
David Howells | 75d4e06 | 2019-03-25 16:38:24 +0000 | [diff] [blame] | 322 | static int dax_init_fs_context(struct fs_context *fc) |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 323 | { |
David Howells | 75d4e06 | 2019-03-25 16:38:24 +0000 | [diff] [blame] | 324 | struct pseudo_fs_context *ctx = init_pseudo(fc, DAXFS_MAGIC); |
| 325 | if (!ctx) |
| 326 | return -ENOMEM; |
| 327 | ctx->ops = &dax_sops; |
| 328 | return 0; |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | static struct file_system_type dax_fs_type = { |
David Howells | 75d4e06 | 2019-03-25 16:38:24 +0000 | [diff] [blame] | 332 | .name = "dax", |
| 333 | .init_fs_context = dax_init_fs_context, |
| 334 | .kill_sb = kill_anon_super, |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 335 | }; |
| 336 | |
| 337 | static int dax_test(struct inode *inode, void *data) |
| 338 | { |
| 339 | dev_t devt = *(dev_t *) data; |
| 340 | |
| 341 | return inode->i_rdev == devt; |
| 342 | } |
| 343 | |
| 344 | static int dax_set(struct inode *inode, void *data) |
| 345 | { |
| 346 | dev_t devt = *(dev_t *) data; |
| 347 | |
| 348 | inode->i_rdev = devt; |
| 349 | return 0; |
| 350 | } |
| 351 | |
| 352 | static struct dax_device *dax_dev_get(dev_t devt) |
| 353 | { |
| 354 | struct dax_device *dax_dev; |
| 355 | struct inode *inode; |
| 356 | |
| 357 | inode = iget5_locked(dax_superblock, hash_32(devt + DAXFS_MAGIC, 31), |
| 358 | dax_test, dax_set, &devt); |
| 359 | |
| 360 | if (!inode) |
| 361 | return NULL; |
| 362 | |
| 363 | dax_dev = to_dax_dev(inode); |
| 364 | if (inode->i_state & I_NEW) { |
Dan Williams | 9a60c3e | 2017-06-27 17:59:28 -0700 | [diff] [blame] | 365 | set_bit(DAXDEV_ALIVE, &dax_dev->flags); |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 366 | inode->i_cdev = &dax_dev->cdev; |
| 367 | inode->i_mode = S_IFCHR; |
| 368 | inode->i_flags = S_DAX; |
| 369 | mapping_set_gfp_mask(&inode->i_data, GFP_USER); |
| 370 | unlock_new_inode(inode); |
| 371 | } |
| 372 | |
| 373 | return dax_dev; |
| 374 | } |
| 375 | |
Christoph Hellwig | 30c6828 | 2021-12-15 09:45:07 +0100 | [diff] [blame] | 376 | struct dax_device *alloc_dax(void *private, const struct dax_operations *ops) |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 377 | { |
| 378 | struct dax_device *dax_dev; |
| 379 | dev_t devt; |
| 380 | int minor; |
| 381 | |
Christoph Hellwig | fb08a19 | 2021-11-29 11:21:38 +0100 | [diff] [blame] | 382 | if (WARN_ON_ONCE(ops && !ops->zero_page_range)) |
Vivek Goyal | 4e4ced9 | 2020-04-01 12:11:25 -0400 | [diff] [blame] | 383 | return ERR_PTR(-EINVAL); |
Dan Williams | 7205800 | 2017-04-19 15:14:31 -0700 | [diff] [blame] | 384 | |
Dan Williams | cf1e228 | 2017-05-08 12:33:53 -0700 | [diff] [blame] | 385 | minor = ida_simple_get(&dax_minor_ida, 0, MINORMASK+1, GFP_KERNEL); |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 386 | if (minor < 0) |
Christoph Hellwig | fb08a19 | 2021-11-29 11:21:38 +0100 | [diff] [blame] | 387 | return ERR_PTR(-ENOMEM); |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 388 | |
| 389 | devt = MKDEV(MAJOR(dax_devt), minor); |
| 390 | dax_dev = dax_dev_get(devt); |
| 391 | if (!dax_dev) |
Dan Williams | 7205800 | 2017-04-19 15:14:31 -0700 | [diff] [blame] | 392 | goto err_dev; |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 393 | |
Dan Williams | 6568b08 | 2017-01-24 18:44:18 -0800 | [diff] [blame] | 394 | dax_dev->ops = ops; |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 395 | dax_dev->private = private; |
| 396 | return dax_dev; |
| 397 | |
Dan Williams | 7205800 | 2017-04-19 15:14:31 -0700 | [diff] [blame] | 398 | err_dev: |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 399 | ida_simple_remove(&dax_minor_ida, minor); |
Vivek Goyal | 4e4ced9 | 2020-04-01 12:11:25 -0400 | [diff] [blame] | 400 | return ERR_PTR(-ENOMEM); |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 401 | } |
| 402 | EXPORT_SYMBOL_GPL(alloc_dax); |
| 403 | |
| 404 | void put_dax(struct dax_device *dax_dev) |
| 405 | { |
| 406 | if (!dax_dev) |
| 407 | return; |
| 408 | iput(&dax_dev->inode); |
| 409 | } |
| 410 | EXPORT_SYMBOL_GPL(put_dax); |
| 411 | |
| 412 | /** |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 413 | * inode_dax: convert a public inode into its dax_dev |
| 414 | * @inode: An inode with i_cdev pointing to a dax_dev |
| 415 | * |
| 416 | * Note this is not equivalent to to_dax_dev() which is for private |
| 417 | * internal use where we know the inode filesystem type == dax_fs_type. |
| 418 | */ |
| 419 | struct dax_device *inode_dax(struct inode *inode) |
| 420 | { |
| 421 | struct cdev *cdev = inode->i_cdev; |
| 422 | |
| 423 | return container_of(cdev, struct dax_device, cdev); |
| 424 | } |
| 425 | EXPORT_SYMBOL_GPL(inode_dax); |
| 426 | |
| 427 | struct inode *dax_inode(struct dax_device *dax_dev) |
| 428 | { |
| 429 | return &dax_dev->inode; |
| 430 | } |
| 431 | EXPORT_SYMBOL_GPL(dax_inode); |
| 432 | |
| 433 | void *dax_get_private(struct dax_device *dax_dev) |
| 434 | { |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 435 | if (!test_bit(DAXDEV_ALIVE, &dax_dev->flags)) |
| 436 | return NULL; |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 437 | return dax_dev->private; |
| 438 | } |
| 439 | EXPORT_SYMBOL_GPL(dax_get_private); |
| 440 | |
| 441 | static void init_once(void *_dax_dev) |
| 442 | { |
| 443 | struct dax_device *dax_dev = _dax_dev; |
| 444 | struct inode *inode = &dax_dev->inode; |
| 445 | |
Dan Williams | b9d39d1 | 2017-06-09 08:50:49 -0700 | [diff] [blame] | 446 | memset(dax_dev, 0, sizeof(*dax_dev)); |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 447 | inode_init_once(inode); |
| 448 | } |
| 449 | |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 450 | static int dax_fs_init(void) |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 451 | { |
| 452 | int rc; |
| 453 | |
| 454 | dax_cache = kmem_cache_create("dax_cache", sizeof(struct dax_device), 0, |
| 455 | (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT| |
| 456 | SLAB_MEM_SPREAD|SLAB_ACCOUNT), |
| 457 | init_once); |
| 458 | if (!dax_cache) |
| 459 | return -ENOMEM; |
| 460 | |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 461 | dax_mnt = kern_mount(&dax_fs_type); |
| 462 | if (IS_ERR(dax_mnt)) { |
| 463 | rc = PTR_ERR(dax_mnt); |
| 464 | goto err_mount; |
| 465 | } |
| 466 | dax_superblock = dax_mnt->mnt_sb; |
| 467 | |
| 468 | return 0; |
| 469 | |
| 470 | err_mount: |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 471 | kmem_cache_destroy(dax_cache); |
| 472 | |
| 473 | return rc; |
| 474 | } |
| 475 | |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 476 | static void dax_fs_exit(void) |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 477 | { |
| 478 | kern_unmount(dax_mnt); |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 479 | kmem_cache_destroy(dax_cache); |
| 480 | } |
| 481 | |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 482 | static int __init dax_core_init(void) |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 483 | { |
| 484 | int rc; |
| 485 | |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 486 | rc = dax_fs_init(); |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 487 | if (rc) |
| 488 | return rc; |
| 489 | |
Dan Williams | cf1e228 | 2017-05-08 12:33:53 -0700 | [diff] [blame] | 490 | rc = alloc_chrdev_region(&dax_devt, 0, MINORMASK+1, "dax"); |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 491 | if (rc) |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 492 | goto err_chrdev; |
| 493 | |
| 494 | rc = dax_bus_init(); |
| 495 | if (rc) |
| 496 | goto err_bus; |
| 497 | return 0; |
| 498 | |
| 499 | err_bus: |
| 500 | unregister_chrdev_region(dax_devt, MINORMASK+1); |
| 501 | err_chrdev: |
| 502 | dax_fs_exit(); |
| 503 | return 0; |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 504 | } |
| 505 | |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 506 | static void __exit dax_core_exit(void) |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 507 | { |
Wang Hai | 1aa5743 | 2020-12-01 21:59:29 +0800 | [diff] [blame] | 508 | dax_bus_exit(); |
Dan Williams | cf1e228 | 2017-05-08 12:33:53 -0700 | [diff] [blame] | 509 | unregister_chrdev_region(dax_devt, MINORMASK+1); |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 510 | ida_destroy(&dax_minor_ida); |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 511 | dax_fs_exit(); |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | MODULE_AUTHOR("Intel Corporation"); |
| 515 | MODULE_LICENSE("GPL v2"); |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 516 | subsys_initcall(dax_core_init); |
| 517 | module_exit(dax_core_exit); |