Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

dax: define a unified inode/address_space for device-dax mappings

In support of enabling resize / truncate of device-dax instances, define
a pseudo-fs to provide a unified inode/address space for vm operations.

Cc: Al Viro <viro@zeniv.linux.org.uk>
Signed-off-by: Dan Williams <dan.j.williams@intel.com>

+150 -6
+148 -6
drivers/dax/dax.c
··· 13 13 #include <linux/pagemap.h> 14 14 #include <linux/module.h> 15 15 #include <linux/device.h> 16 + #include <linux/mount.h> 16 17 #include <linux/pfn_t.h> 18 + #include <linux/hash.h> 17 19 #include <linux/cdev.h> 18 20 #include <linux/slab.h> 19 21 #include <linux/dax.h> ··· 28 26 static DEFINE_IDA(dax_minor_ida); 29 27 static int nr_dax = CONFIG_NR_DEV_DAX; 30 28 module_param(nr_dax, int, S_IRUGO); 29 + static struct vfsmount *dax_mnt; 30 + static struct kmem_cache *dax_cache __read_mostly; 31 + static struct super_block *dax_superblock __read_mostly; 31 32 MODULE_PARM_DESC(nr_dax, "max number of device-dax instances"); 32 33 33 34 /** ··· 66 61 */ 67 62 struct dax_dev { 68 63 struct dax_region *region; 64 + struct inode *inode; 69 65 struct device dev; 70 66 struct cdev cdev; 71 67 bool alive; ··· 74 68 int num_resources; 75 69 struct resource res[0]; 76 70 }; 71 + 72 + static struct inode *dax_alloc_inode(struct super_block *sb) 73 + { 74 + return kmem_cache_alloc(dax_cache, GFP_KERNEL); 75 + } 76 + 77 + static void dax_i_callback(struct rcu_head *head) 78 + { 79 + struct inode *inode = container_of(head, struct inode, i_rcu); 80 + 81 + kmem_cache_free(dax_cache, inode); 82 + } 83 + 84 + static void dax_destroy_inode(struct inode *inode) 85 + { 86 + call_rcu(&inode->i_rcu, dax_i_callback); 87 + } 88 + 89 + static const struct super_operations dax_sops = { 90 + .statfs = simple_statfs, 91 + .alloc_inode = dax_alloc_inode, 92 + .destroy_inode = dax_destroy_inode, 93 + .drop_inode = generic_delete_inode, 94 + }; 95 + 96 + static struct dentry *dax_mount(struct file_system_type *fs_type, 97 + int flags, const char *dev_name, void *data) 98 + { 99 + return mount_pseudo(fs_type, "dax:", &dax_sops, NULL, DAXFS_MAGIC); 100 + } 101 + 102 + static struct file_system_type dax_type = { 103 + .name = "dax", 104 + .mount = dax_mount, 105 + .kill_sb = kill_anon_super, 106 + }; 107 + 108 + static int dax_test(struct inode *inode, void *data) 109 + { 110 + return inode->i_cdev == data; 111 + } 112 + 113 + static int dax_set(struct inode *inode, void *data) 114 + { 115 + inode->i_cdev = data; 116 + return 0; 117 + } 118 + 119 + static struct inode *dax_inode_get(struct cdev *cdev, dev_t devt) 120 + { 121 + struct inode *inode; 122 + 123 + inode = iget5_locked(dax_superblock, hash_32(devt + DAXFS_MAGIC, 31), 124 + dax_test, dax_set, cdev); 125 + 126 + if (!inode) 127 + return NULL; 128 + 129 + if (inode->i_state & I_NEW) { 130 + inode->i_mode = S_IFCHR; 131 + inode->i_flags = S_DAX; 132 + inode->i_rdev = devt; 133 + mapping_set_gfp_mask(&inode->i_data, GFP_USER); 134 + unlock_new_inode(inode); 135 + } 136 + return inode; 137 + } 138 + 139 + static void init_once(void *inode) 140 + { 141 + inode_init_once(inode); 142 + } 143 + 144 + static int dax_inode_init(void) 145 + { 146 + int rc; 147 + 148 + dax_cache = kmem_cache_create("dax_cache", sizeof(struct inode), 0, 149 + (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT| 150 + SLAB_MEM_SPREAD|SLAB_ACCOUNT), 151 + init_once); 152 + if (!dax_cache) 153 + return -ENOMEM; 154 + 155 + rc = register_filesystem(&dax_type); 156 + if (rc) 157 + goto err_register_fs; 158 + 159 + dax_mnt = kern_mount(&dax_type); 160 + if (IS_ERR(dax_mnt)) { 161 + rc = PTR_ERR(dax_mnt); 162 + goto err_mount; 163 + } 164 + dax_superblock = dax_mnt->mnt_sb; 165 + 166 + return 0; 167 + 168 + err_mount: 169 + unregister_filesystem(&dax_type); 170 + err_register_fs: 171 + kmem_cache_destroy(dax_cache); 172 + 173 + return rc; 174 + } 175 + 176 + static void dax_inode_exit(void) 177 + { 178 + kern_unmount(dax_mnt); 179 + unregister_filesystem(&dax_type); 180 + kmem_cache_destroy(dax_cache); 181 + } 77 182 78 183 static void dax_region_free(struct kref *kref) 79 184 { ··· 496 379 497 380 dax_dev = container_of(inode->i_cdev, struct dax_dev, cdev); 498 381 dev_dbg(&dax_dev->dev, "%s\n", __func__); 382 + inode->i_mapping = dax_dev->inode->i_mapping; 383 + inode->i_mapping->host = dax_dev->inode; 384 + filp->f_mapping = inode->i_mapping; 499 385 filp->private_data = dax_dev; 500 386 inode->i_flags = S_DAX; 501 387 ··· 530 410 ida_simple_remove(&dax_region->ida, dax_dev->id); 531 411 ida_simple_remove(&dax_minor_ida, MINOR(dev->devt)); 532 412 dax_region_put(dax_region); 413 + iput(dax_dev->inode); 533 414 kfree(dax_dev); 534 415 } 535 416 ··· 580 459 goto err_minor; 581 460 } 582 461 462 + dax_dev->inode = dax_inode_get(&dax_dev->cdev, dev_t); 463 + if (!dax_dev->inode) { 464 + rc = -ENOMEM; 465 + goto err_inode; 466 + } 467 + 583 468 /* device_initialize() so cdev can reference kobj parent */ 584 469 dev_t = MKDEV(MAJOR(dax_devt), minor); 585 470 dev = &dax_dev->dev; ··· 621 494 return devm_add_action_or_reset(dax_region->dev, unregister_dax_dev, dev); 622 495 623 496 err_cdev: 497 + iput(dax_dev->inode); 498 + err_inode: 624 499 ida_simple_remove(&dax_minor_ida, minor); 625 500 err_minor: 626 501 ida_simple_remove(&dax_region->ida, dax_dev->id); ··· 637 508 { 638 509 int rc; 639 510 640 - nr_dax = max(nr_dax, 256); 641 - rc = alloc_chrdev_region(&dax_devt, 0, nr_dax, "dax"); 511 + rc = dax_inode_init(); 642 512 if (rc) 643 513 return rc; 644 514 645 - dax_class = class_create(THIS_MODULE, "dax"); 646 - if (IS_ERR(dax_class)) 647 - unregister_chrdev_region(dax_devt, nr_dax); 515 + nr_dax = max(nr_dax, 256); 516 + rc = alloc_chrdev_region(&dax_devt, 0, nr_dax, "dax"); 517 + if (rc) 518 + goto err_chrdev; 648 519 649 - return PTR_ERR_OR_ZERO(dax_class); 520 + dax_class = class_create(THIS_MODULE, "dax"); 521 + if (IS_ERR(dax_class)) { 522 + rc = PTR_ERR(dax_class); 523 + goto err_class; 524 + } 525 + 526 + return 0; 527 + 528 + err_class: 529 + unregister_chrdev_region(dax_devt, nr_dax); 530 + err_chrdev: 531 + dax_inode_exit(); 532 + return rc; 650 533 } 651 534 652 535 static void __exit dax_exit(void) ··· 666 525 class_destroy(dax_class); 667 526 unregister_chrdev_region(dax_devt, nr_dax); 668 527 ida_destroy(&dax_minor_ida); 528 + dax_inode_exit(); 669 529 } 670 530 671 531 MODULE_AUTHOR("Intel Corporation");
+1
fs/char_dev.c
··· 406 406 spin_lock(&cdev_lock); 407 407 list_del_init(&inode->i_devices); 408 408 inode->i_cdev = NULL; 409 + inode->i_mapping = &inode->i_data; 409 410 spin_unlock(&cdev_lock); 410 411 } 411 412
+1
include/uapi/linux/magic.h
··· 65 65 #define V9FS_MAGIC 0x01021997 66 66 67 67 #define BDEVFS_MAGIC 0x62646576 68 + #define DAXFS_MAGIC 0x64646178 68 69 #define BINFMTFS_MAGIC 0x42494e4d 69 70 #define DEVPTS_SUPER_MAGIC 0x1cd1 70 71 #define FUTEXFS_SUPER_MAGIC 0xBAD1DEA