15b497af4SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 27b6be844SDan Williams /* 37b6be844SDan Williams * Copyright(c) 2017 Intel Corporation. All rights reserved. 47b6be844SDan Williams */ 57b6be844SDan Williams #include <linux/pagemap.h> 67b6be844SDan Williams #include <linux/module.h> 77b6be844SDan Williams #include <linux/mount.h> 875d4e06fSDavid Howells #include <linux/pseudo_fs.h> 97b6be844SDan Williams #include <linux/magic.h> 10569d0365SDan Williams #include <linux/pfn_t.h> 117b6be844SDan Williams #include <linux/cdev.h> 127b6be844SDan Williams #include <linux/slab.h> 137e026c8cSDan Williams #include <linux/uio.h> 146568b08bSDan Williams #include <linux/dax.h> 157b6be844SDan Williams #include <linux/fs.h> 1651cf784cSDan Williams #include "dax-private.h" 177b6be844SDan Williams 181b764601SChristoph Hellwig /** 191b764601SChristoph Hellwig * struct dax_device - anchor object for dax services 201b764601SChristoph Hellwig * @inode: core vfs 211b764601SChristoph Hellwig * @cdev: optional character interface for "device dax" 221b764601SChristoph Hellwig * @private: dax driver private data 231b764601SChristoph Hellwig * @flags: state and boolean properties 241b764601SChristoph Hellwig */ 251b764601SChristoph Hellwig struct dax_device { 261b764601SChristoph Hellwig struct inode inode; 271b764601SChristoph Hellwig struct cdev cdev; 281b764601SChristoph Hellwig void *private; 291b764601SChristoph Hellwig unsigned long flags; 301b764601SChristoph Hellwig const struct dax_operations *ops; 311b764601SChristoph Hellwig }; 321b764601SChristoph Hellwig 337b6be844SDan Williams static dev_t dax_devt; 347b6be844SDan Williams DEFINE_STATIC_SRCU(dax_srcu); 357b6be844SDan Williams static struct vfsmount *dax_mnt; 367b6be844SDan Williams static DEFINE_IDA(dax_minor_ida); 377b6be844SDan Williams static struct kmem_cache *dax_cache __read_mostly; 387b6be844SDan Williams static struct super_block *dax_superblock __read_mostly; 397b6be844SDan Williams 407b6be844SDan Williams int dax_read_lock(void) 417b6be844SDan Williams { 427b6be844SDan Williams return srcu_read_lock(&dax_srcu); 437b6be844SDan Williams } 447b6be844SDan Williams EXPORT_SYMBOL_GPL(dax_read_lock); 457b6be844SDan Williams 467b6be844SDan Williams void dax_read_unlock(int id) 477b6be844SDan Williams { 487b6be844SDan Williams srcu_read_unlock(&dax_srcu, id); 497b6be844SDan Williams } 507b6be844SDan Williams EXPORT_SYMBOL_GPL(dax_read_unlock); 517b6be844SDan Williams 525d2a228bSChristoph Hellwig #if defined(CONFIG_BLOCK) && defined(CONFIG_FS_DAX) 53e765f13eSChristoph Hellwig #include <linux/blkdev.h> 54e765f13eSChristoph Hellwig 55fb08a190SChristoph Hellwig static DEFINE_XARRAY(dax_hosts); 56fb08a190SChristoph Hellwig 57fb08a190SChristoph Hellwig int dax_add_host(struct dax_device *dax_dev, struct gendisk *disk) 58fb08a190SChristoph Hellwig { 59fb08a190SChristoph Hellwig return xa_insert(&dax_hosts, (unsigned long)disk, dax_dev, GFP_KERNEL); 60fb08a190SChristoph Hellwig } 61fb08a190SChristoph Hellwig EXPORT_SYMBOL_GPL(dax_add_host); 62fb08a190SChristoph Hellwig 63fb08a190SChristoph Hellwig void dax_remove_host(struct gendisk *disk) 64fb08a190SChristoph Hellwig { 65fb08a190SChristoph Hellwig xa_erase(&dax_hosts, (unsigned long)disk); 66fb08a190SChristoph Hellwig } 67fb08a190SChristoph Hellwig EXPORT_SYMBOL_GPL(dax_remove_host); 68fb08a190SChristoph Hellwig 691b764601SChristoph Hellwig /** 70fb08a190SChristoph Hellwig * fs_dax_get_by_bdev() - temporary lookup mechanism for filesystem-dax 71fb08a190SChristoph Hellwig * @bdev: block device to find a dax_device for 72cd913c76SChristoph Hellwig * @start_off: returns the byte offset into the dax_device that @bdev starts 731b764601SChristoph Hellwig */ 74cd913c76SChristoph Hellwig struct dax_device *fs_dax_get_by_bdev(struct block_device *bdev, u64 *start_off) 7578f35473SDan Williams { 76fb08a190SChristoph Hellwig struct dax_device *dax_dev; 77cd913c76SChristoph Hellwig u64 part_size; 78fb08a190SChristoph Hellwig int id; 79fb08a190SChristoph Hellwig 80e556f6baSChristoph Hellwig if (!blk_queue_dax(bdev->bd_disk->queue)) 8178f35473SDan Williams return NULL; 82fb08a190SChristoph Hellwig 83cd913c76SChristoph Hellwig *start_off = get_start_sect(bdev) * SECTOR_SIZE; 84cd913c76SChristoph Hellwig part_size = bdev_nr_sectors(bdev) * SECTOR_SIZE; 85cd913c76SChristoph Hellwig if (*start_off % PAGE_SIZE || part_size % PAGE_SIZE) { 860c445871SChristoph Hellwig pr_info("%pg: error: unaligned partition for dax\n", bdev); 870c445871SChristoph Hellwig return NULL; 880c445871SChristoph Hellwig } 890c445871SChristoph Hellwig 90fb08a190SChristoph Hellwig id = dax_read_lock(); 91fb08a190SChristoph Hellwig dax_dev = xa_load(&dax_hosts, (unsigned long)bdev->bd_disk); 92fb08a190SChristoph Hellwig if (!dax_dev || !dax_alive(dax_dev) || !igrab(&dax_dev->inode)) 93fb08a190SChristoph Hellwig dax_dev = NULL; 94fb08a190SChristoph Hellwig dax_read_unlock(id); 95fb08a190SChristoph Hellwig 96fb08a190SChristoph Hellwig return dax_dev; 9778f35473SDan Williams } 9878f35473SDan Williams EXPORT_SYMBOL_GPL(fs_dax_get_by_bdev); 995d2a228bSChristoph Hellwig #endif /* CONFIG_BLOCK && CONFIG_FS_DAX */ 100ef510424SDan Williams 1019a60c3efSDan Williams enum dax_device_flags { 1029a60c3efSDan Williams /* !alive + rcu grace period == no new operations / mappings */ 1039a60c3efSDan Williams DAXDEV_ALIVE, 1046e0c90d6SDan Williams /* gate whether dax_flush() calls the low level flush routine */ 1056e0c90d6SDan Williams DAXDEV_WRITE_CACHE, 106fefc1d97SPankaj Gupta /* flag to check if device supports synchronous flush */ 107fefc1d97SPankaj Gupta DAXDEV_SYNC, 1087ac5360cSChristoph Hellwig /* do not leave the caches dirty after writes */ 1097ac5360cSChristoph Hellwig DAXDEV_NOCACHE, 1107ac5360cSChristoph Hellwig /* handle CPU fetch exceptions during reads */ 1117ac5360cSChristoph Hellwig DAXDEV_NOMC, 1129a60c3efSDan Williams }; 1139a60c3efSDan Williams 114b0686260SDan Williams /** 115b0686260SDan Williams * dax_direct_access() - translate a device pgoff to an absolute pfn 116b0686260SDan Williams * @dax_dev: a dax_device instance representing the logical memory range 117b0686260SDan Williams * @pgoff: offset in pages from the start of the device to translate 118b0686260SDan Williams * @nr_pages: number of consecutive pages caller can handle relative to @pfn 119b0686260SDan Williams * @kaddr: output parameter that returns a virtual address mapping of pfn 120b0686260SDan Williams * @pfn: output parameter that returns an absolute pfn translation of @pgoff 121b0686260SDan Williams * 122b0686260SDan Williams * Return: negative errno if an error occurs, otherwise the number of 123b0686260SDan Williams * pages accessible at the device relative @pgoff. 124b0686260SDan Williams */ 125b0686260SDan Williams long dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff, long nr_pages, 126b0686260SDan Williams void **kaddr, pfn_t *pfn) 127b0686260SDan Williams { 128b0686260SDan Williams long avail; 129b0686260SDan Williams 130b0686260SDan Williams if (!dax_dev) 131b0686260SDan Williams return -EOPNOTSUPP; 132b0686260SDan Williams 133b0686260SDan Williams if (!dax_alive(dax_dev)) 134b0686260SDan Williams return -ENXIO; 135b0686260SDan Williams 136b0686260SDan Williams if (nr_pages < 0) 137b05d4c57SIra Weiny return -EINVAL; 138b0686260SDan Williams 139b0686260SDan Williams avail = dax_dev->ops->direct_access(dax_dev, pgoff, nr_pages, 140b0686260SDan Williams kaddr, pfn); 141b0686260SDan Williams if (!avail) 142b0686260SDan Williams return -ERANGE; 143b0686260SDan Williams return min(avail, nr_pages); 144b0686260SDan Williams } 145b0686260SDan Williams EXPORT_SYMBOL_GPL(dax_direct_access); 146b0686260SDan Williams 1477e026c8cSDan Williams size_t dax_copy_from_iter(struct dax_device *dax_dev, pgoff_t pgoff, void *addr, 1487e026c8cSDan Williams size_t bytes, struct iov_iter *i) 1497e026c8cSDan Williams { 1507e026c8cSDan Williams if (!dax_alive(dax_dev)) 1517e026c8cSDan Williams return 0; 1527e026c8cSDan Williams 1537ac5360cSChristoph Hellwig /* 1547ac5360cSChristoph Hellwig * The userspace address for the memory copy has already been validated 1557ac5360cSChristoph Hellwig * via access_ok() in vfs_write, so use the 'no check' version to bypass 1567ac5360cSChristoph Hellwig * the HARDENED_USERCOPY overhead. 1577ac5360cSChristoph Hellwig */ 1587ac5360cSChristoph Hellwig if (test_bit(DAXDEV_NOCACHE, &dax_dev->flags)) 1597ac5360cSChristoph Hellwig return _copy_from_iter_flushcache(addr, bytes, i); 1607ac5360cSChristoph Hellwig return _copy_from_iter(addr, bytes, i); 1617e026c8cSDan Williams } 1627e026c8cSDan Williams 163b3a9a0c3SDan Williams size_t dax_copy_to_iter(struct dax_device *dax_dev, pgoff_t pgoff, void *addr, 164b3a9a0c3SDan Williams size_t bytes, struct iov_iter *i) 165b3a9a0c3SDan Williams { 166b3a9a0c3SDan Williams if (!dax_alive(dax_dev)) 167b3a9a0c3SDan Williams return 0; 168b3a9a0c3SDan Williams 1697ac5360cSChristoph Hellwig /* 1707ac5360cSChristoph Hellwig * The userspace address for the memory copy has already been validated 1717ac5360cSChristoph Hellwig * via access_ok() in vfs_red, so use the 'no check' version to bypass 1727ac5360cSChristoph Hellwig * the HARDENED_USERCOPY overhead. 1737ac5360cSChristoph Hellwig */ 1747ac5360cSChristoph Hellwig if (test_bit(DAXDEV_NOMC, &dax_dev->flags)) 1757ac5360cSChristoph Hellwig return _copy_mc_to_iter(addr, bytes, i); 1767ac5360cSChristoph Hellwig return _copy_to_iter(addr, bytes, i); 177b3a9a0c3SDan Williams } 178b3a9a0c3SDan Williams 179f605a263SVivek Goyal int dax_zero_page_range(struct dax_device *dax_dev, pgoff_t pgoff, 180f605a263SVivek Goyal size_t nr_pages) 181f605a263SVivek Goyal { 182f605a263SVivek Goyal if (!dax_alive(dax_dev)) 183f605a263SVivek Goyal return -ENXIO; 184f605a263SVivek Goyal /* 185f605a263SVivek Goyal * There are no callers that want to zero more than one page as of now. 186f605a263SVivek Goyal * Once users are there, this check can be removed after the 187f605a263SVivek Goyal * device mapper code has been updated to split ranges across targets. 188f605a263SVivek Goyal */ 189f605a263SVivek Goyal if (nr_pages != 1) 190f605a263SVivek Goyal return -EIO; 191f605a263SVivek Goyal 192f605a263SVivek Goyal return dax_dev->ops->zero_page_range(dax_dev, pgoff, nr_pages); 193f605a263SVivek Goyal } 194f605a263SVivek Goyal EXPORT_SYMBOL_GPL(dax_zero_page_range); 195f605a263SVivek Goyal 196c3ca015fSMikulas Patocka #ifdef CONFIG_ARCH_HAS_PMEM_API 197c3ca015fSMikulas Patocka void arch_wb_cache_pmem(void *addr, size_t size); 198c3ca015fSMikulas Patocka void dax_flush(struct dax_device *dax_dev, void *addr, size_t size) 199abebfbe2SDan Williams { 200808c340bSRoss Zwisler if (unlikely(!dax_write_cache_enabled(dax_dev))) 2016e0c90d6SDan Williams return; 2026e0c90d6SDan Williams 203c3ca015fSMikulas Patocka arch_wb_cache_pmem(addr, size); 204abebfbe2SDan Williams } 205c3ca015fSMikulas Patocka #else 206c3ca015fSMikulas Patocka void dax_flush(struct dax_device *dax_dev, void *addr, size_t size) 207c3ca015fSMikulas Patocka { 208c3ca015fSMikulas Patocka } 209c3ca015fSMikulas Patocka #endif 210abebfbe2SDan Williams EXPORT_SYMBOL_GPL(dax_flush); 211abebfbe2SDan Williams 2126e0c90d6SDan Williams void dax_write_cache(struct dax_device *dax_dev, bool wc) 2136e0c90d6SDan Williams { 2146e0c90d6SDan Williams if (wc) 2156e0c90d6SDan Williams set_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags); 2166e0c90d6SDan Williams else 2176e0c90d6SDan Williams clear_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags); 2186e0c90d6SDan Williams } 2196e0c90d6SDan Williams EXPORT_SYMBOL_GPL(dax_write_cache); 2206e0c90d6SDan Williams 221273752c9SVivek Goyal bool dax_write_cache_enabled(struct dax_device *dax_dev) 222273752c9SVivek Goyal { 223273752c9SVivek Goyal return test_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags); 224273752c9SVivek Goyal } 225273752c9SVivek Goyal EXPORT_SYMBOL_GPL(dax_write_cache_enabled); 226273752c9SVivek Goyal 227fd1d00ecSChristoph Hellwig bool dax_synchronous(struct dax_device *dax_dev) 228fefc1d97SPankaj Gupta { 229fefc1d97SPankaj Gupta return test_bit(DAXDEV_SYNC, &dax_dev->flags); 230fefc1d97SPankaj Gupta } 231fd1d00ecSChristoph Hellwig EXPORT_SYMBOL_GPL(dax_synchronous); 232fefc1d97SPankaj Gupta 233fd1d00ecSChristoph Hellwig void set_dax_synchronous(struct dax_device *dax_dev) 234fefc1d97SPankaj Gupta { 235fefc1d97SPankaj Gupta set_bit(DAXDEV_SYNC, &dax_dev->flags); 236fefc1d97SPankaj Gupta } 237fd1d00ecSChristoph Hellwig EXPORT_SYMBOL_GPL(set_dax_synchronous); 238fefc1d97SPankaj Gupta 2397ac5360cSChristoph Hellwig void set_dax_nocache(struct dax_device *dax_dev) 2407ac5360cSChristoph Hellwig { 2417ac5360cSChristoph Hellwig set_bit(DAXDEV_NOCACHE, &dax_dev->flags); 2427ac5360cSChristoph Hellwig } 2437ac5360cSChristoph Hellwig EXPORT_SYMBOL_GPL(set_dax_nocache); 2447ac5360cSChristoph Hellwig 2457ac5360cSChristoph Hellwig void set_dax_nomc(struct dax_device *dax_dev) 2467ac5360cSChristoph Hellwig { 2477ac5360cSChristoph Hellwig set_bit(DAXDEV_NOMC, &dax_dev->flags); 2487ac5360cSChristoph Hellwig } 2497ac5360cSChristoph Hellwig EXPORT_SYMBOL_GPL(set_dax_nomc); 2507ac5360cSChristoph Hellwig 2517b6be844SDan Williams bool dax_alive(struct dax_device *dax_dev) 2527b6be844SDan Williams { 2537b6be844SDan Williams lockdep_assert_held(&dax_srcu); 2549a60c3efSDan Williams return test_bit(DAXDEV_ALIVE, &dax_dev->flags); 2557b6be844SDan Williams } 2567b6be844SDan Williams EXPORT_SYMBOL_GPL(dax_alive); 2577b6be844SDan Williams 2587b6be844SDan Williams /* 2597b6be844SDan Williams * Note, rcu is not protecting the liveness of dax_dev, rcu is ensuring 2607b6be844SDan Williams * that any fault handlers or operations that might have seen 2617b6be844SDan Williams * dax_alive(), have completed. Any operations that start after 2627b6be844SDan Williams * synchronize_srcu() has run will abort upon seeing !dax_alive(). 2637b6be844SDan Williams */ 2647b6be844SDan Williams void kill_dax(struct dax_device *dax_dev) 2657b6be844SDan Williams { 2667b6be844SDan Williams if (!dax_dev) 2677b6be844SDan Williams return; 2687b6be844SDan Williams 2699a60c3efSDan Williams clear_bit(DAXDEV_ALIVE, &dax_dev->flags); 2707b6be844SDan Williams synchronize_srcu(&dax_srcu); 2717b6be844SDan Williams } 2727b6be844SDan Williams EXPORT_SYMBOL_GPL(kill_dax); 2737b6be844SDan Williams 2749567da0bSDan Williams void run_dax(struct dax_device *dax_dev) 2759567da0bSDan Williams { 2769567da0bSDan Williams set_bit(DAXDEV_ALIVE, &dax_dev->flags); 2779567da0bSDan Williams } 2789567da0bSDan Williams EXPORT_SYMBOL_GPL(run_dax); 2799567da0bSDan Williams 2807b6be844SDan Williams static struct inode *dax_alloc_inode(struct super_block *sb) 2817b6be844SDan Williams { 2827b6be844SDan Williams struct dax_device *dax_dev; 283b9d39d17SDan Williams struct inode *inode; 2847b6be844SDan Williams 285*fd60b288SMuchun Song dax_dev = alloc_inode_sb(sb, dax_cache, GFP_KERNEL); 2869f586fffSMikulas Patocka if (!dax_dev) 2879f586fffSMikulas Patocka return NULL; 2889f586fffSMikulas Patocka 289b9d39d17SDan Williams inode = &dax_dev->inode; 290b9d39d17SDan Williams inode->i_rdev = 0; 291b9d39d17SDan Williams return inode; 2927b6be844SDan Williams } 2937b6be844SDan Williams 2947b6be844SDan Williams static struct dax_device *to_dax_dev(struct inode *inode) 2957b6be844SDan Williams { 2967b6be844SDan Williams return container_of(inode, struct dax_device, inode); 2977b6be844SDan Williams } 2987b6be844SDan Williams 29953e22829SAl Viro static void dax_free_inode(struct inode *inode) 3007b6be844SDan Williams { 3017b6be844SDan Williams struct dax_device *dax_dev = to_dax_dev(inode); 302b9d39d17SDan Williams if (inode->i_rdev) 3036f24784fSAl Viro ida_simple_remove(&dax_minor_ida, iminor(inode)); 3047b6be844SDan Williams kmem_cache_free(dax_cache, dax_dev); 3057b6be844SDan Williams } 3067b6be844SDan Williams 3077b6be844SDan Williams static void dax_destroy_inode(struct inode *inode) 3087b6be844SDan Williams { 3097b6be844SDan Williams struct dax_device *dax_dev = to_dax_dev(inode); 3109a60c3efSDan Williams WARN_ONCE(test_bit(DAXDEV_ALIVE, &dax_dev->flags), 3117b6be844SDan Williams "kill_dax() must be called before final iput()\n"); 3127b6be844SDan Williams } 3137b6be844SDan Williams 3147b6be844SDan Williams static const struct super_operations dax_sops = { 3157b6be844SDan Williams .statfs = simple_statfs, 3167b6be844SDan Williams .alloc_inode = dax_alloc_inode, 3177b6be844SDan Williams .destroy_inode = dax_destroy_inode, 31853e22829SAl Viro .free_inode = dax_free_inode, 3197b6be844SDan Williams .drop_inode = generic_delete_inode, 3207b6be844SDan Williams }; 3217b6be844SDan Williams 32275d4e06fSDavid Howells static int dax_init_fs_context(struct fs_context *fc) 3237b6be844SDan Williams { 32475d4e06fSDavid Howells struct pseudo_fs_context *ctx = init_pseudo(fc, DAXFS_MAGIC); 32575d4e06fSDavid Howells if (!ctx) 32675d4e06fSDavid Howells return -ENOMEM; 32775d4e06fSDavid Howells ctx->ops = &dax_sops; 32875d4e06fSDavid Howells return 0; 3297b6be844SDan Williams } 3307b6be844SDan Williams 3317b6be844SDan Williams static struct file_system_type dax_fs_type = { 3327b6be844SDan Williams .name = "dax", 33375d4e06fSDavid Howells .init_fs_context = dax_init_fs_context, 3347b6be844SDan Williams .kill_sb = kill_anon_super, 3357b6be844SDan Williams }; 3367b6be844SDan Williams 3377b6be844SDan Williams static int dax_test(struct inode *inode, void *data) 3387b6be844SDan Williams { 3397b6be844SDan Williams dev_t devt = *(dev_t *) data; 3407b6be844SDan Williams 3417b6be844SDan Williams return inode->i_rdev == devt; 3427b6be844SDan Williams } 3437b6be844SDan Williams 3447b6be844SDan Williams static int dax_set(struct inode *inode, void *data) 3457b6be844SDan Williams { 3467b6be844SDan Williams dev_t devt = *(dev_t *) data; 3477b6be844SDan Williams 3487b6be844SDan Williams inode->i_rdev = devt; 3497b6be844SDan Williams return 0; 3507b6be844SDan Williams } 3517b6be844SDan Williams 3527b6be844SDan Williams static struct dax_device *dax_dev_get(dev_t devt) 3537b6be844SDan Williams { 3547b6be844SDan Williams struct dax_device *dax_dev; 3557b6be844SDan Williams struct inode *inode; 3567b6be844SDan Williams 3577b6be844SDan Williams inode = iget5_locked(dax_superblock, hash_32(devt + DAXFS_MAGIC, 31), 3587b6be844SDan Williams dax_test, dax_set, &devt); 3597b6be844SDan Williams 3607b6be844SDan Williams if (!inode) 3617b6be844SDan Williams return NULL; 3627b6be844SDan Williams 3637b6be844SDan Williams dax_dev = to_dax_dev(inode); 3647b6be844SDan Williams if (inode->i_state & I_NEW) { 3659a60c3efSDan Williams set_bit(DAXDEV_ALIVE, &dax_dev->flags); 3667b6be844SDan Williams inode->i_cdev = &dax_dev->cdev; 3677b6be844SDan Williams inode->i_mode = S_IFCHR; 3687b6be844SDan Williams inode->i_flags = S_DAX; 3697b6be844SDan Williams mapping_set_gfp_mask(&inode->i_data, GFP_USER); 3707b6be844SDan Williams unlock_new_inode(inode); 3717b6be844SDan Williams } 3727b6be844SDan Williams 3737b6be844SDan Williams return dax_dev; 3747b6be844SDan Williams } 3757b6be844SDan Williams 37630c6828aSChristoph Hellwig struct dax_device *alloc_dax(void *private, const struct dax_operations *ops) 3777b6be844SDan Williams { 3787b6be844SDan Williams struct dax_device *dax_dev; 3797b6be844SDan Williams dev_t devt; 3807b6be844SDan Williams int minor; 3817b6be844SDan Williams 382fb08a190SChristoph Hellwig if (WARN_ON_ONCE(ops && !ops->zero_page_range)) 3834e4ced93SVivek Goyal return ERR_PTR(-EINVAL); 38472058005SDan Williams 385cf1e2289SDan Williams minor = ida_simple_get(&dax_minor_ida, 0, MINORMASK+1, GFP_KERNEL); 3867b6be844SDan Williams if (minor < 0) 387fb08a190SChristoph Hellwig return ERR_PTR(-ENOMEM); 3887b6be844SDan Williams 3897b6be844SDan Williams devt = MKDEV(MAJOR(dax_devt), minor); 3907b6be844SDan Williams dax_dev = dax_dev_get(devt); 3917b6be844SDan Williams if (!dax_dev) 39272058005SDan Williams goto err_dev; 3937b6be844SDan Williams 3946568b08bSDan Williams dax_dev->ops = ops; 3957b6be844SDan Williams dax_dev->private = private; 3967b6be844SDan Williams return dax_dev; 3977b6be844SDan Williams 39872058005SDan Williams err_dev: 3997b6be844SDan Williams ida_simple_remove(&dax_minor_ida, minor); 4004e4ced93SVivek Goyal return ERR_PTR(-ENOMEM); 4017b6be844SDan Williams } 4027b6be844SDan Williams EXPORT_SYMBOL_GPL(alloc_dax); 4037b6be844SDan Williams 4047b6be844SDan Williams void put_dax(struct dax_device *dax_dev) 4057b6be844SDan Williams { 4067b6be844SDan Williams if (!dax_dev) 4077b6be844SDan Williams return; 4087b6be844SDan Williams iput(&dax_dev->inode); 4097b6be844SDan Williams } 4107b6be844SDan Williams EXPORT_SYMBOL_GPL(put_dax); 4117b6be844SDan Williams 4127b6be844SDan Williams /** 4137b6be844SDan Williams * inode_dax: convert a public inode into its dax_dev 4147b6be844SDan Williams * @inode: An inode with i_cdev pointing to a dax_dev 4157b6be844SDan Williams * 4167b6be844SDan Williams * Note this is not equivalent to to_dax_dev() which is for private 4177b6be844SDan Williams * internal use where we know the inode filesystem type == dax_fs_type. 4187b6be844SDan Williams */ 4197b6be844SDan Williams struct dax_device *inode_dax(struct inode *inode) 4207b6be844SDan Williams { 4217b6be844SDan Williams struct cdev *cdev = inode->i_cdev; 4227b6be844SDan Williams 4237b6be844SDan Williams return container_of(cdev, struct dax_device, cdev); 4247b6be844SDan Williams } 4257b6be844SDan Williams EXPORT_SYMBOL_GPL(inode_dax); 4267b6be844SDan Williams 4277b6be844SDan Williams struct inode *dax_inode(struct dax_device *dax_dev) 4287b6be844SDan Williams { 4297b6be844SDan Williams return &dax_dev->inode; 4307b6be844SDan Williams } 4317b6be844SDan Williams EXPORT_SYMBOL_GPL(dax_inode); 4327b6be844SDan Williams 4337b6be844SDan Williams void *dax_get_private(struct dax_device *dax_dev) 4347b6be844SDan Williams { 4359567da0bSDan Williams if (!test_bit(DAXDEV_ALIVE, &dax_dev->flags)) 4369567da0bSDan Williams return NULL; 4377b6be844SDan Williams return dax_dev->private; 4387b6be844SDan Williams } 4397b6be844SDan Williams EXPORT_SYMBOL_GPL(dax_get_private); 4407b6be844SDan Williams 4417b6be844SDan Williams static void init_once(void *_dax_dev) 4427b6be844SDan Williams { 4437b6be844SDan Williams struct dax_device *dax_dev = _dax_dev; 4447b6be844SDan Williams struct inode *inode = &dax_dev->inode; 4457b6be844SDan Williams 446b9d39d17SDan Williams memset(dax_dev, 0, sizeof(*dax_dev)); 4477b6be844SDan Williams inode_init_once(inode); 4487b6be844SDan Williams } 4497b6be844SDan Williams 4509567da0bSDan Williams static int dax_fs_init(void) 4517b6be844SDan Williams { 4527b6be844SDan Williams int rc; 4537b6be844SDan Williams 4547b6be844SDan Williams dax_cache = kmem_cache_create("dax_cache", sizeof(struct dax_device), 0, 4557b6be844SDan Williams (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT| 4567b6be844SDan Williams SLAB_MEM_SPREAD|SLAB_ACCOUNT), 4577b6be844SDan Williams init_once); 4587b6be844SDan Williams if (!dax_cache) 4597b6be844SDan Williams return -ENOMEM; 4607b6be844SDan Williams 4617b6be844SDan Williams dax_mnt = kern_mount(&dax_fs_type); 4627b6be844SDan Williams if (IS_ERR(dax_mnt)) { 4637b6be844SDan Williams rc = PTR_ERR(dax_mnt); 4647b6be844SDan Williams goto err_mount; 4657b6be844SDan Williams } 4667b6be844SDan Williams dax_superblock = dax_mnt->mnt_sb; 4677b6be844SDan Williams 4687b6be844SDan Williams return 0; 4697b6be844SDan Williams 4707b6be844SDan Williams err_mount: 4717b6be844SDan Williams kmem_cache_destroy(dax_cache); 4727b6be844SDan Williams 4737b6be844SDan Williams return rc; 4747b6be844SDan Williams } 4757b6be844SDan Williams 4769567da0bSDan Williams static void dax_fs_exit(void) 4777b6be844SDan Williams { 4787b6be844SDan Williams kern_unmount(dax_mnt); 4797b6be844SDan Williams kmem_cache_destroy(dax_cache); 4807b6be844SDan Williams } 4817b6be844SDan Williams 4829567da0bSDan Williams static int __init dax_core_init(void) 4837b6be844SDan Williams { 4847b6be844SDan Williams int rc; 4857b6be844SDan Williams 4869567da0bSDan Williams rc = dax_fs_init(); 4877b6be844SDan Williams if (rc) 4887b6be844SDan Williams return rc; 4897b6be844SDan Williams 490cf1e2289SDan Williams rc = alloc_chrdev_region(&dax_devt, 0, MINORMASK+1, "dax"); 4917b6be844SDan Williams if (rc) 4929567da0bSDan Williams goto err_chrdev; 4939567da0bSDan Williams 4949567da0bSDan Williams rc = dax_bus_init(); 4959567da0bSDan Williams if (rc) 4969567da0bSDan Williams goto err_bus; 4979567da0bSDan Williams return 0; 4989567da0bSDan Williams 4999567da0bSDan Williams err_bus: 5009567da0bSDan Williams unregister_chrdev_region(dax_devt, MINORMASK+1); 5019567da0bSDan Williams err_chrdev: 5029567da0bSDan Williams dax_fs_exit(); 5039567da0bSDan Williams return 0; 5047b6be844SDan Williams } 5057b6be844SDan Williams 5069567da0bSDan Williams static void __exit dax_core_exit(void) 5077b6be844SDan Williams { 5081aa57431SWang Hai dax_bus_exit(); 509cf1e2289SDan Williams unregister_chrdev_region(dax_devt, MINORMASK+1); 5107b6be844SDan Williams ida_destroy(&dax_minor_ida); 5119567da0bSDan Williams dax_fs_exit(); 5127b6be844SDan Williams } 5137b6be844SDan Williams 5147b6be844SDan Williams MODULE_AUTHOR("Intel Corporation"); 5157b6be844SDan Williams MODULE_LICENSE("GPL v2"); 5169567da0bSDan Williams subsys_initcall(dax_core_init); 5179567da0bSDan Williams module_exit(dax_core_exit); 518