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