Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright(c) 2017 Intel Corporation. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of version 2 of the GNU General Public License as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, but |
| 9 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * General Public License for more details. |
| 12 | */ |
| 13 | #include <linux/pagemap.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/mount.h> |
| 16 | #include <linux/magic.h> |
Dan Williams | ef51042 | 2017-05-08 10:55:27 -0700 | [diff] [blame] | 17 | #include <linux/genhd.h> |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 18 | #include <linux/cdev.h> |
| 19 | #include <linux/hash.h> |
| 20 | #include <linux/slab.h> |
Dan Williams | 7e026c8 | 2017-05-29 12:57:56 -0700 | [diff] [blame] | 21 | #include <linux/uio.h> |
Dan Williams | 6568b08 | 2017-01-24 18:44:18 -0800 | [diff] [blame] | 22 | #include <linux/dax.h> |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 23 | #include <linux/fs.h> |
| 24 | |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 25 | static dev_t dax_devt; |
| 26 | DEFINE_STATIC_SRCU(dax_srcu); |
| 27 | static struct vfsmount *dax_mnt; |
| 28 | static DEFINE_IDA(dax_minor_ida); |
| 29 | static struct kmem_cache *dax_cache __read_mostly; |
| 30 | static struct super_block *dax_superblock __read_mostly; |
| 31 | |
Dan Williams | 7205800 | 2017-04-19 15:14:31 -0700 | [diff] [blame] | 32 | #define DAX_HASH_SIZE (PAGE_SIZE / sizeof(struct hlist_head)) |
| 33 | static struct hlist_head dax_host_list[DAX_HASH_SIZE]; |
| 34 | static DEFINE_SPINLOCK(dax_host_lock); |
| 35 | |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 36 | int dax_read_lock(void) |
| 37 | { |
| 38 | return srcu_read_lock(&dax_srcu); |
| 39 | } |
| 40 | EXPORT_SYMBOL_GPL(dax_read_lock); |
| 41 | |
| 42 | void dax_read_unlock(int id) |
| 43 | { |
| 44 | srcu_read_unlock(&dax_srcu, id); |
| 45 | } |
| 46 | EXPORT_SYMBOL_GPL(dax_read_unlock); |
| 47 | |
Dan Williams | 9d10908 | 2017-05-13 16:18:21 -0700 | [diff] [blame] | 48 | #ifdef CONFIG_BLOCK |
Dan Williams | 78f3547 | 2017-08-30 09:16:38 -0700 | [diff] [blame^] | 49 | #include <linux/blkdev.h> |
| 50 | |
Dan Williams | ef51042 | 2017-05-08 10:55:27 -0700 | [diff] [blame] | 51 | int bdev_dax_pgoff(struct block_device *bdev, sector_t sector, size_t size, |
| 52 | pgoff_t *pgoff) |
| 53 | { |
| 54 | phys_addr_t phys_off = (get_start_sect(bdev) + sector) * 512; |
| 55 | |
| 56 | if (pgoff) |
| 57 | *pgoff = PHYS_PFN(phys_off); |
| 58 | if (phys_off % PAGE_SIZE || size % PAGE_SIZE) |
| 59 | return -EINVAL; |
| 60 | return 0; |
| 61 | } |
| 62 | EXPORT_SYMBOL(bdev_dax_pgoff); |
| 63 | |
Dan Williams | 78f3547 | 2017-08-30 09:16:38 -0700 | [diff] [blame^] | 64 | struct dax_device *fs_dax_get_by_bdev(struct block_device *bdev) |
| 65 | { |
| 66 | if (!blk_queue_dax(bdev->bd_queue)) |
| 67 | return NULL; |
| 68 | return fs_dax_get_by_host(bdev->bd_disk->disk_name); |
| 69 | } |
| 70 | EXPORT_SYMBOL_GPL(fs_dax_get_by_bdev); |
| 71 | |
Dan Williams | ef51042 | 2017-05-08 10:55:27 -0700 | [diff] [blame] | 72 | /** |
| 73 | * __bdev_dax_supported() - Check if the device supports dax for filesystem |
| 74 | * @sb: The superblock of the device |
| 75 | * @blocksize: The block size of the device |
| 76 | * |
| 77 | * This is a library function for filesystems to check if the block device |
| 78 | * can be mounted with dax option. |
| 79 | * |
| 80 | * Return: negative errno if unsupported, 0 if supported. |
| 81 | */ |
| 82 | int __bdev_dax_supported(struct super_block *sb, int blocksize) |
| 83 | { |
| 84 | struct block_device *bdev = sb->s_bdev; |
| 85 | struct dax_device *dax_dev; |
| 86 | pgoff_t pgoff; |
| 87 | int err, id; |
| 88 | void *kaddr; |
| 89 | pfn_t pfn; |
| 90 | long len; |
| 91 | |
| 92 | if (blocksize != PAGE_SIZE) { |
| 93 | pr_err("VFS (%s): error: unsupported blocksize for dax\n", |
| 94 | sb->s_id); |
| 95 | return -EINVAL; |
| 96 | } |
| 97 | |
| 98 | err = bdev_dax_pgoff(bdev, 0, PAGE_SIZE, &pgoff); |
| 99 | if (err) { |
| 100 | pr_err("VFS (%s): error: unaligned partition for dax\n", |
| 101 | sb->s_id); |
| 102 | return err; |
| 103 | } |
| 104 | |
| 105 | dax_dev = dax_get_by_host(bdev->bd_disk->disk_name); |
| 106 | if (!dax_dev) { |
| 107 | pr_err("VFS (%s): error: device does not support dax\n", |
| 108 | sb->s_id); |
| 109 | return -EOPNOTSUPP; |
| 110 | } |
| 111 | |
| 112 | id = dax_read_lock(); |
| 113 | len = dax_direct_access(dax_dev, pgoff, 1, &kaddr, &pfn); |
| 114 | dax_read_unlock(id); |
| 115 | |
| 116 | put_dax(dax_dev); |
| 117 | |
| 118 | if (len < 1) { |
| 119 | pr_err("VFS (%s): error: dax access failed (%ld)", |
| 120 | sb->s_id, len); |
| 121 | return len < 0 ? len : -EIO; |
| 122 | } |
| 123 | |
| 124 | return 0; |
| 125 | } |
| 126 | EXPORT_SYMBOL_GPL(__bdev_dax_supported); |
Dan Williams | 9d10908 | 2017-05-13 16:18:21 -0700 | [diff] [blame] | 127 | #endif |
Dan Williams | ef51042 | 2017-05-08 10:55:27 -0700 | [diff] [blame] | 128 | |
Dan Williams | 9a60c3e | 2017-06-27 17:59:28 -0700 | [diff] [blame] | 129 | enum dax_device_flags { |
| 130 | /* !alive + rcu grace period == no new operations / mappings */ |
| 131 | DAXDEV_ALIVE, |
Dan Williams | 6e0c90d | 2017-06-26 21:28:41 -0700 | [diff] [blame] | 132 | /* gate whether dax_flush() calls the low level flush routine */ |
| 133 | DAXDEV_WRITE_CACHE, |
Dan Williams | 9a60c3e | 2017-06-27 17:59:28 -0700 | [diff] [blame] | 134 | }; |
| 135 | |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 136 | /** |
| 137 | * struct dax_device - anchor object for dax services |
| 138 | * @inode: core vfs |
| 139 | * @cdev: optional character interface for "device dax" |
Dan Williams | 7205800 | 2017-04-19 15:14:31 -0700 | [diff] [blame] | 140 | * @host: optional name for lookups where the device path is not available |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 141 | * @private: dax driver private data |
Dan Williams | 9a60c3e | 2017-06-27 17:59:28 -0700 | [diff] [blame] | 142 | * @flags: state and boolean properties |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 143 | */ |
| 144 | struct dax_device { |
Dan Williams | 7205800 | 2017-04-19 15:14:31 -0700 | [diff] [blame] | 145 | struct hlist_node list; |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 146 | struct inode inode; |
| 147 | struct cdev cdev; |
Dan Williams | 7205800 | 2017-04-19 15:14:31 -0700 | [diff] [blame] | 148 | const char *host; |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 149 | void *private; |
Dan Williams | 9a60c3e | 2017-06-27 17:59:28 -0700 | [diff] [blame] | 150 | unsigned long flags; |
Dan Williams | 6568b08 | 2017-01-24 18:44:18 -0800 | [diff] [blame] | 151 | const struct dax_operations *ops; |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 152 | }; |
| 153 | |
Dan Williams | 6e0c90d | 2017-06-26 21:28:41 -0700 | [diff] [blame] | 154 | static ssize_t write_cache_show(struct device *dev, |
| 155 | struct device_attribute *attr, char *buf) |
| 156 | { |
| 157 | struct dax_device *dax_dev = dax_get_by_host(dev_name(dev)); |
| 158 | ssize_t rc; |
| 159 | |
| 160 | WARN_ON_ONCE(!dax_dev); |
| 161 | if (!dax_dev) |
| 162 | return -ENXIO; |
| 163 | |
| 164 | rc = sprintf(buf, "%d\n", !!test_bit(DAXDEV_WRITE_CACHE, |
| 165 | &dax_dev->flags)); |
| 166 | put_dax(dax_dev); |
| 167 | return rc; |
| 168 | } |
| 169 | |
| 170 | static ssize_t write_cache_store(struct device *dev, |
| 171 | struct device_attribute *attr, const char *buf, size_t len) |
| 172 | { |
| 173 | bool write_cache; |
| 174 | int rc = strtobool(buf, &write_cache); |
| 175 | struct dax_device *dax_dev = dax_get_by_host(dev_name(dev)); |
| 176 | |
| 177 | WARN_ON_ONCE(!dax_dev); |
| 178 | if (!dax_dev) |
| 179 | return -ENXIO; |
| 180 | |
| 181 | if (rc) |
| 182 | len = rc; |
| 183 | else if (write_cache) |
| 184 | set_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags); |
| 185 | else |
| 186 | clear_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags); |
| 187 | |
| 188 | put_dax(dax_dev); |
| 189 | return len; |
| 190 | } |
| 191 | static DEVICE_ATTR_RW(write_cache); |
| 192 | |
| 193 | static umode_t dax_visible(struct kobject *kobj, struct attribute *a, int n) |
| 194 | { |
| 195 | struct device *dev = container_of(kobj, typeof(*dev), kobj); |
| 196 | struct dax_device *dax_dev = dax_get_by_host(dev_name(dev)); |
| 197 | |
| 198 | WARN_ON_ONCE(!dax_dev); |
| 199 | if (!dax_dev) |
| 200 | return 0; |
| 201 | |
| 202 | if (a == &dev_attr_write_cache.attr && !dax_dev->ops->flush) |
| 203 | return 0; |
| 204 | return a->mode; |
| 205 | } |
| 206 | |
| 207 | static struct attribute *dax_attributes[] = { |
| 208 | &dev_attr_write_cache.attr, |
| 209 | NULL, |
| 210 | }; |
| 211 | |
| 212 | struct attribute_group dax_attribute_group = { |
| 213 | .name = "dax", |
| 214 | .attrs = dax_attributes, |
| 215 | .is_visible = dax_visible, |
| 216 | }; |
| 217 | EXPORT_SYMBOL_GPL(dax_attribute_group); |
| 218 | |
Dan Williams | b068626 | 2017-01-26 20:37:35 -0800 | [diff] [blame] | 219 | /** |
| 220 | * dax_direct_access() - translate a device pgoff to an absolute pfn |
| 221 | * @dax_dev: a dax_device instance representing the logical memory range |
| 222 | * @pgoff: offset in pages from the start of the device to translate |
| 223 | * @nr_pages: number of consecutive pages caller can handle relative to @pfn |
| 224 | * @kaddr: output parameter that returns a virtual address mapping of pfn |
| 225 | * @pfn: output parameter that returns an absolute pfn translation of @pgoff |
| 226 | * |
| 227 | * Return: negative errno if an error occurs, otherwise the number of |
| 228 | * pages accessible at the device relative @pgoff. |
| 229 | */ |
| 230 | long dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff, long nr_pages, |
| 231 | void **kaddr, pfn_t *pfn) |
| 232 | { |
| 233 | long avail; |
| 234 | |
| 235 | /* |
| 236 | * The device driver is allowed to sleep, in order to make the |
| 237 | * memory directly accessible. |
| 238 | */ |
| 239 | might_sleep(); |
| 240 | |
| 241 | if (!dax_dev) |
| 242 | return -EOPNOTSUPP; |
| 243 | |
| 244 | if (!dax_alive(dax_dev)) |
| 245 | return -ENXIO; |
| 246 | |
| 247 | if (nr_pages < 0) |
| 248 | return nr_pages; |
| 249 | |
| 250 | avail = dax_dev->ops->direct_access(dax_dev, pgoff, nr_pages, |
| 251 | kaddr, pfn); |
| 252 | if (!avail) |
| 253 | return -ERANGE; |
| 254 | return min(avail, nr_pages); |
| 255 | } |
| 256 | EXPORT_SYMBOL_GPL(dax_direct_access); |
| 257 | |
Dan Williams | 7e026c8 | 2017-05-29 12:57:56 -0700 | [diff] [blame] | 258 | size_t dax_copy_from_iter(struct dax_device *dax_dev, pgoff_t pgoff, void *addr, |
| 259 | size_t bytes, struct iov_iter *i) |
| 260 | { |
| 261 | if (!dax_alive(dax_dev)) |
| 262 | return 0; |
| 263 | |
Dan Williams | 7e026c8 | 2017-05-29 12:57:56 -0700 | [diff] [blame] | 264 | return dax_dev->ops->copy_from_iter(dax_dev, pgoff, addr, bytes, i); |
| 265 | } |
| 266 | EXPORT_SYMBOL_GPL(dax_copy_from_iter); |
| 267 | |
Dan Williams | abebfbe2 | 2017-05-29 13:02:52 -0700 | [diff] [blame] | 268 | void dax_flush(struct dax_device *dax_dev, pgoff_t pgoff, void *addr, |
| 269 | size_t size) |
| 270 | { |
| 271 | if (!dax_alive(dax_dev)) |
| 272 | return; |
| 273 | |
Dan Williams | 6e0c90d | 2017-06-26 21:28:41 -0700 | [diff] [blame] | 274 | if (!test_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags)) |
| 275 | return; |
| 276 | |
Dan Williams | abebfbe2 | 2017-05-29 13:02:52 -0700 | [diff] [blame] | 277 | if (dax_dev->ops->flush) |
| 278 | dax_dev->ops->flush(dax_dev, pgoff, addr, size); |
| 279 | } |
| 280 | EXPORT_SYMBOL_GPL(dax_flush); |
| 281 | |
Dan Williams | 6e0c90d | 2017-06-26 21:28:41 -0700 | [diff] [blame] | 282 | void dax_write_cache(struct dax_device *dax_dev, bool wc) |
| 283 | { |
| 284 | if (wc) |
| 285 | set_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags); |
| 286 | else |
| 287 | clear_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags); |
| 288 | } |
| 289 | EXPORT_SYMBOL_GPL(dax_write_cache); |
| 290 | |
Vivek Goyal | 273752c | 2017-07-26 09:35:09 -0400 | [diff] [blame] | 291 | bool dax_write_cache_enabled(struct dax_device *dax_dev) |
| 292 | { |
| 293 | return test_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags); |
| 294 | } |
| 295 | EXPORT_SYMBOL_GPL(dax_write_cache_enabled); |
| 296 | |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 297 | bool dax_alive(struct dax_device *dax_dev) |
| 298 | { |
| 299 | lockdep_assert_held(&dax_srcu); |
Dan Williams | 9a60c3e | 2017-06-27 17:59:28 -0700 | [diff] [blame] | 300 | return test_bit(DAXDEV_ALIVE, &dax_dev->flags); |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 301 | } |
| 302 | EXPORT_SYMBOL_GPL(dax_alive); |
| 303 | |
Dan Williams | 7205800 | 2017-04-19 15:14:31 -0700 | [diff] [blame] | 304 | static int dax_host_hash(const char *host) |
| 305 | { |
| 306 | return hashlen_hash(hashlen_string("DAX", host)) % DAX_HASH_SIZE; |
| 307 | } |
| 308 | |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 309 | /* |
| 310 | * Note, rcu is not protecting the liveness of dax_dev, rcu is ensuring |
| 311 | * that any fault handlers or operations that might have seen |
| 312 | * dax_alive(), have completed. Any operations that start after |
| 313 | * synchronize_srcu() has run will abort upon seeing !dax_alive(). |
| 314 | */ |
| 315 | void kill_dax(struct dax_device *dax_dev) |
| 316 | { |
| 317 | if (!dax_dev) |
| 318 | return; |
| 319 | |
Dan Williams | 9a60c3e | 2017-06-27 17:59:28 -0700 | [diff] [blame] | 320 | clear_bit(DAXDEV_ALIVE, &dax_dev->flags); |
Dan Williams | 7205800 | 2017-04-19 15:14:31 -0700 | [diff] [blame] | 321 | |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 322 | synchronize_srcu(&dax_srcu); |
Dan Williams | 7205800 | 2017-04-19 15:14:31 -0700 | [diff] [blame] | 323 | |
| 324 | spin_lock(&dax_host_lock); |
| 325 | hlist_del_init(&dax_dev->list); |
| 326 | spin_unlock(&dax_host_lock); |
| 327 | |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 328 | dax_dev->private = NULL; |
| 329 | } |
| 330 | EXPORT_SYMBOL_GPL(kill_dax); |
| 331 | |
| 332 | static struct inode *dax_alloc_inode(struct super_block *sb) |
| 333 | { |
| 334 | struct dax_device *dax_dev; |
Dan Williams | b9d39d1 | 2017-06-09 08:50:49 -0700 | [diff] [blame] | 335 | struct inode *inode; |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 336 | |
| 337 | dax_dev = kmem_cache_alloc(dax_cache, GFP_KERNEL); |
Dan Williams | b9d39d1 | 2017-06-09 08:50:49 -0700 | [diff] [blame] | 338 | inode = &dax_dev->inode; |
| 339 | inode->i_rdev = 0; |
| 340 | return inode; |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | static struct dax_device *to_dax_dev(struct inode *inode) |
| 344 | { |
| 345 | return container_of(inode, struct dax_device, inode); |
| 346 | } |
| 347 | |
| 348 | static void dax_i_callback(struct rcu_head *head) |
| 349 | { |
| 350 | struct inode *inode = container_of(head, struct inode, i_rcu); |
| 351 | struct dax_device *dax_dev = to_dax_dev(inode); |
| 352 | |
Dan Williams | 7205800 | 2017-04-19 15:14:31 -0700 | [diff] [blame] | 353 | kfree(dax_dev->host); |
| 354 | dax_dev->host = NULL; |
Dan Williams | b9d39d1 | 2017-06-09 08:50:49 -0700 | [diff] [blame] | 355 | if (inode->i_rdev) |
| 356 | ida_simple_remove(&dax_minor_ida, MINOR(inode->i_rdev)); |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 357 | kmem_cache_free(dax_cache, dax_dev); |
| 358 | } |
| 359 | |
| 360 | static void dax_destroy_inode(struct inode *inode) |
| 361 | { |
| 362 | struct dax_device *dax_dev = to_dax_dev(inode); |
| 363 | |
Dan Williams | 9a60c3e | 2017-06-27 17:59:28 -0700 | [diff] [blame] | 364 | WARN_ONCE(test_bit(DAXDEV_ALIVE, &dax_dev->flags), |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 365 | "kill_dax() must be called before final iput()\n"); |
| 366 | call_rcu(&inode->i_rcu, dax_i_callback); |
| 367 | } |
| 368 | |
| 369 | static const struct super_operations dax_sops = { |
| 370 | .statfs = simple_statfs, |
| 371 | .alloc_inode = dax_alloc_inode, |
| 372 | .destroy_inode = dax_destroy_inode, |
| 373 | .drop_inode = generic_delete_inode, |
| 374 | }; |
| 375 | |
| 376 | static struct dentry *dax_mount(struct file_system_type *fs_type, |
| 377 | int flags, const char *dev_name, void *data) |
| 378 | { |
| 379 | return mount_pseudo(fs_type, "dax:", &dax_sops, NULL, DAXFS_MAGIC); |
| 380 | } |
| 381 | |
| 382 | static struct file_system_type dax_fs_type = { |
| 383 | .name = "dax", |
| 384 | .mount = dax_mount, |
| 385 | .kill_sb = kill_anon_super, |
| 386 | }; |
| 387 | |
| 388 | static int dax_test(struct inode *inode, void *data) |
| 389 | { |
| 390 | dev_t devt = *(dev_t *) data; |
| 391 | |
| 392 | return inode->i_rdev == devt; |
| 393 | } |
| 394 | |
| 395 | static int dax_set(struct inode *inode, void *data) |
| 396 | { |
| 397 | dev_t devt = *(dev_t *) data; |
| 398 | |
| 399 | inode->i_rdev = devt; |
| 400 | return 0; |
| 401 | } |
| 402 | |
| 403 | static struct dax_device *dax_dev_get(dev_t devt) |
| 404 | { |
| 405 | struct dax_device *dax_dev; |
| 406 | struct inode *inode; |
| 407 | |
| 408 | inode = iget5_locked(dax_superblock, hash_32(devt + DAXFS_MAGIC, 31), |
| 409 | dax_test, dax_set, &devt); |
| 410 | |
| 411 | if (!inode) |
| 412 | return NULL; |
| 413 | |
| 414 | dax_dev = to_dax_dev(inode); |
| 415 | if (inode->i_state & I_NEW) { |
Dan Williams | 9a60c3e | 2017-06-27 17:59:28 -0700 | [diff] [blame] | 416 | set_bit(DAXDEV_ALIVE, &dax_dev->flags); |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 417 | inode->i_cdev = &dax_dev->cdev; |
| 418 | inode->i_mode = S_IFCHR; |
| 419 | inode->i_flags = S_DAX; |
| 420 | mapping_set_gfp_mask(&inode->i_data, GFP_USER); |
| 421 | unlock_new_inode(inode); |
| 422 | } |
| 423 | |
| 424 | return dax_dev; |
| 425 | } |
| 426 | |
Dan Williams | 7205800 | 2017-04-19 15:14:31 -0700 | [diff] [blame] | 427 | static void dax_add_host(struct dax_device *dax_dev, const char *host) |
| 428 | { |
| 429 | int hash; |
| 430 | |
| 431 | /* |
| 432 | * Unconditionally init dax_dev since it's coming from a |
| 433 | * non-zeroed slab cache |
| 434 | */ |
| 435 | INIT_HLIST_NODE(&dax_dev->list); |
| 436 | dax_dev->host = host; |
| 437 | if (!host) |
| 438 | return; |
| 439 | |
| 440 | hash = dax_host_hash(host); |
| 441 | spin_lock(&dax_host_lock); |
| 442 | hlist_add_head(&dax_dev->list, &dax_host_list[hash]); |
| 443 | spin_unlock(&dax_host_lock); |
| 444 | } |
| 445 | |
Dan Williams | 6568b08 | 2017-01-24 18:44:18 -0800 | [diff] [blame] | 446 | struct dax_device *alloc_dax(void *private, const char *__host, |
| 447 | const struct dax_operations *ops) |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 448 | { |
| 449 | struct dax_device *dax_dev; |
Dan Williams | 7205800 | 2017-04-19 15:14:31 -0700 | [diff] [blame] | 450 | const char *host; |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 451 | dev_t devt; |
| 452 | int minor; |
| 453 | |
Dan Williams | 7205800 | 2017-04-19 15:14:31 -0700 | [diff] [blame] | 454 | host = kstrdup(__host, GFP_KERNEL); |
| 455 | if (__host && !host) |
| 456 | return NULL; |
| 457 | |
Dan Williams | cf1e228 | 2017-05-08 12:33:53 -0700 | [diff] [blame] | 458 | minor = ida_simple_get(&dax_minor_ida, 0, MINORMASK+1, GFP_KERNEL); |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 459 | if (minor < 0) |
Dan Williams | 7205800 | 2017-04-19 15:14:31 -0700 | [diff] [blame] | 460 | goto err_minor; |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 461 | |
| 462 | devt = MKDEV(MAJOR(dax_devt), minor); |
| 463 | dax_dev = dax_dev_get(devt); |
| 464 | if (!dax_dev) |
Dan Williams | 7205800 | 2017-04-19 15:14:31 -0700 | [diff] [blame] | 465 | goto err_dev; |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 466 | |
Dan Williams | 7205800 | 2017-04-19 15:14:31 -0700 | [diff] [blame] | 467 | dax_add_host(dax_dev, host); |
Dan Williams | 6568b08 | 2017-01-24 18:44:18 -0800 | [diff] [blame] | 468 | dax_dev->ops = ops; |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 469 | dax_dev->private = private; |
| 470 | return dax_dev; |
| 471 | |
Dan Williams | 7205800 | 2017-04-19 15:14:31 -0700 | [diff] [blame] | 472 | err_dev: |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 473 | ida_simple_remove(&dax_minor_ida, minor); |
Dan Williams | 7205800 | 2017-04-19 15:14:31 -0700 | [diff] [blame] | 474 | err_minor: |
| 475 | kfree(host); |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 476 | return NULL; |
| 477 | } |
| 478 | EXPORT_SYMBOL_GPL(alloc_dax); |
| 479 | |
| 480 | void put_dax(struct dax_device *dax_dev) |
| 481 | { |
| 482 | if (!dax_dev) |
| 483 | return; |
| 484 | iput(&dax_dev->inode); |
| 485 | } |
| 486 | EXPORT_SYMBOL_GPL(put_dax); |
| 487 | |
| 488 | /** |
Dan Williams | 7205800 | 2017-04-19 15:14:31 -0700 | [diff] [blame] | 489 | * dax_get_by_host() - temporary lookup mechanism for filesystem-dax |
| 490 | * @host: alternate name for the device registered by a dax driver |
| 491 | */ |
| 492 | struct dax_device *dax_get_by_host(const char *host) |
| 493 | { |
| 494 | struct dax_device *dax_dev, *found = NULL; |
| 495 | int hash, id; |
| 496 | |
| 497 | if (!host) |
| 498 | return NULL; |
| 499 | |
| 500 | hash = dax_host_hash(host); |
| 501 | |
| 502 | id = dax_read_lock(); |
| 503 | spin_lock(&dax_host_lock); |
| 504 | hlist_for_each_entry(dax_dev, &dax_host_list[hash], list) { |
| 505 | if (!dax_alive(dax_dev) |
| 506 | || strcmp(host, dax_dev->host) != 0) |
| 507 | continue; |
| 508 | |
| 509 | if (igrab(&dax_dev->inode)) |
| 510 | found = dax_dev; |
| 511 | break; |
| 512 | } |
| 513 | spin_unlock(&dax_host_lock); |
| 514 | dax_read_unlock(id); |
| 515 | |
| 516 | return found; |
| 517 | } |
| 518 | EXPORT_SYMBOL_GPL(dax_get_by_host); |
| 519 | |
| 520 | /** |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 521 | * inode_dax: convert a public inode into its dax_dev |
| 522 | * @inode: An inode with i_cdev pointing to a dax_dev |
| 523 | * |
| 524 | * Note this is not equivalent to to_dax_dev() which is for private |
| 525 | * internal use where we know the inode filesystem type == dax_fs_type. |
| 526 | */ |
| 527 | struct dax_device *inode_dax(struct inode *inode) |
| 528 | { |
| 529 | struct cdev *cdev = inode->i_cdev; |
| 530 | |
| 531 | return container_of(cdev, struct dax_device, cdev); |
| 532 | } |
| 533 | EXPORT_SYMBOL_GPL(inode_dax); |
| 534 | |
| 535 | struct inode *dax_inode(struct dax_device *dax_dev) |
| 536 | { |
| 537 | return &dax_dev->inode; |
| 538 | } |
| 539 | EXPORT_SYMBOL_GPL(dax_inode); |
| 540 | |
| 541 | void *dax_get_private(struct dax_device *dax_dev) |
| 542 | { |
| 543 | return dax_dev->private; |
| 544 | } |
| 545 | EXPORT_SYMBOL_GPL(dax_get_private); |
| 546 | |
| 547 | static void init_once(void *_dax_dev) |
| 548 | { |
| 549 | struct dax_device *dax_dev = _dax_dev; |
| 550 | struct inode *inode = &dax_dev->inode; |
| 551 | |
Dan Williams | b9d39d1 | 2017-06-09 08:50:49 -0700 | [diff] [blame] | 552 | memset(dax_dev, 0, sizeof(*dax_dev)); |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 553 | inode_init_once(inode); |
| 554 | } |
| 555 | |
| 556 | static int __dax_fs_init(void) |
| 557 | { |
| 558 | int rc; |
| 559 | |
| 560 | dax_cache = kmem_cache_create("dax_cache", sizeof(struct dax_device), 0, |
| 561 | (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT| |
| 562 | SLAB_MEM_SPREAD|SLAB_ACCOUNT), |
| 563 | init_once); |
| 564 | if (!dax_cache) |
| 565 | return -ENOMEM; |
| 566 | |
| 567 | rc = register_filesystem(&dax_fs_type); |
| 568 | if (rc) |
| 569 | goto err_register_fs; |
| 570 | |
| 571 | dax_mnt = kern_mount(&dax_fs_type); |
| 572 | if (IS_ERR(dax_mnt)) { |
| 573 | rc = PTR_ERR(dax_mnt); |
| 574 | goto err_mount; |
| 575 | } |
| 576 | dax_superblock = dax_mnt->mnt_sb; |
| 577 | |
| 578 | return 0; |
| 579 | |
| 580 | err_mount: |
| 581 | unregister_filesystem(&dax_fs_type); |
| 582 | err_register_fs: |
| 583 | kmem_cache_destroy(dax_cache); |
| 584 | |
| 585 | return rc; |
| 586 | } |
| 587 | |
| 588 | static void __dax_fs_exit(void) |
| 589 | { |
| 590 | kern_unmount(dax_mnt); |
| 591 | unregister_filesystem(&dax_fs_type); |
| 592 | kmem_cache_destroy(dax_cache); |
| 593 | } |
| 594 | |
| 595 | static int __init dax_fs_init(void) |
| 596 | { |
| 597 | int rc; |
| 598 | |
| 599 | rc = __dax_fs_init(); |
| 600 | if (rc) |
| 601 | return rc; |
| 602 | |
Dan Williams | cf1e228 | 2017-05-08 12:33:53 -0700 | [diff] [blame] | 603 | rc = alloc_chrdev_region(&dax_devt, 0, MINORMASK+1, "dax"); |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 604 | if (rc) |
| 605 | __dax_fs_exit(); |
| 606 | return rc; |
| 607 | } |
| 608 | |
| 609 | static void __exit dax_fs_exit(void) |
| 610 | { |
Dan Williams | cf1e228 | 2017-05-08 12:33:53 -0700 | [diff] [blame] | 611 | unregister_chrdev_region(dax_devt, MINORMASK+1); |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 612 | ida_destroy(&dax_minor_ida); |
| 613 | __dax_fs_exit(); |
| 614 | } |
| 615 | |
| 616 | MODULE_AUTHOR("Intel Corporation"); |
| 617 | MODULE_LICENSE("GPL v2"); |
| 618 | subsys_initcall(dax_fs_init); |
| 619 | module_exit(dax_fs_exit); |