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> 10ef510424SDan Williams #include <linux/genhd.h> 11569d0365SDan Williams #include <linux/pfn_t.h> 127b6be844SDan Williams #include <linux/cdev.h> 137b6be844SDan Williams #include <linux/hash.h> 147b6be844SDan Williams #include <linux/slab.h> 157e026c8cSDan Williams #include <linux/uio.h> 166568b08bSDan Williams #include <linux/dax.h> 177b6be844SDan Williams #include <linux/fs.h> 1851cf784cSDan Williams #include "dax-private.h" 197b6be844SDan Williams 201b764601SChristoph Hellwig /** 211b764601SChristoph Hellwig * struct dax_device - anchor object for dax services 221b764601SChristoph Hellwig * @inode: core vfs 231b764601SChristoph Hellwig * @cdev: optional character interface for "device dax" 241b764601SChristoph Hellwig * @host: optional name for lookups where the device path is not available 251b764601SChristoph Hellwig * @private: dax driver private data 261b764601SChristoph Hellwig * @flags: state and boolean properties 271b764601SChristoph Hellwig */ 281b764601SChristoph Hellwig struct dax_device { 291b764601SChristoph Hellwig struct hlist_node list; 301b764601SChristoph Hellwig struct inode inode; 311b764601SChristoph Hellwig struct cdev cdev; 321b764601SChristoph Hellwig const char *host; 331b764601SChristoph Hellwig void *private; 341b764601SChristoph Hellwig unsigned long flags; 351b764601SChristoph Hellwig const struct dax_operations *ops; 361b764601SChristoph Hellwig }; 371b764601SChristoph Hellwig 387b6be844SDan Williams static dev_t dax_devt; 397b6be844SDan Williams DEFINE_STATIC_SRCU(dax_srcu); 407b6be844SDan Williams static struct vfsmount *dax_mnt; 417b6be844SDan Williams static DEFINE_IDA(dax_minor_ida); 427b6be844SDan Williams static struct kmem_cache *dax_cache __read_mostly; 437b6be844SDan Williams static struct super_block *dax_superblock __read_mostly; 447b6be844SDan Williams 4572058005SDan Williams #define DAX_HASH_SIZE (PAGE_SIZE / sizeof(struct hlist_head)) 4672058005SDan Williams static struct hlist_head dax_host_list[DAX_HASH_SIZE]; 4772058005SDan Williams static DEFINE_SPINLOCK(dax_host_lock); 4872058005SDan Williams 497b6be844SDan Williams int dax_read_lock(void) 507b6be844SDan Williams { 517b6be844SDan Williams return srcu_read_lock(&dax_srcu); 527b6be844SDan Williams } 537b6be844SDan Williams EXPORT_SYMBOL_GPL(dax_read_lock); 547b6be844SDan Williams 557b6be844SDan Williams void dax_read_unlock(int id) 567b6be844SDan Williams { 577b6be844SDan Williams srcu_read_unlock(&dax_srcu, id); 587b6be844SDan Williams } 597b6be844SDan Williams EXPORT_SYMBOL_GPL(dax_read_unlock); 607b6be844SDan Williams 611b764601SChristoph Hellwig static int dax_host_hash(const char *host) 621b764601SChristoph Hellwig { 631b764601SChristoph Hellwig return hashlen_hash(hashlen_string("DAX", host)) % DAX_HASH_SIZE; 641b764601SChristoph Hellwig } 651b764601SChristoph Hellwig 66*e765f13eSChristoph Hellwig #ifdef CONFIG_BLOCK 67*e765f13eSChristoph Hellwig #include <linux/blkdev.h> 68*e765f13eSChristoph Hellwig 69*e765f13eSChristoph Hellwig int bdev_dax_pgoff(struct block_device *bdev, sector_t sector, size_t size, 70*e765f13eSChristoph Hellwig pgoff_t *pgoff) 71*e765f13eSChristoph Hellwig { 72*e765f13eSChristoph Hellwig sector_t start_sect = bdev ? get_start_sect(bdev) : 0; 73*e765f13eSChristoph Hellwig phys_addr_t phys_off = (start_sect + sector) * 512; 74*e765f13eSChristoph Hellwig 75*e765f13eSChristoph Hellwig if (pgoff) 76*e765f13eSChristoph Hellwig *pgoff = PHYS_PFN(phys_off); 77*e765f13eSChristoph Hellwig if (phys_off % PAGE_SIZE || size % PAGE_SIZE) 78*e765f13eSChristoph Hellwig return -EINVAL; 79*e765f13eSChristoph Hellwig return 0; 80*e765f13eSChristoph Hellwig } 81*e765f13eSChristoph Hellwig EXPORT_SYMBOL(bdev_dax_pgoff); 82*e765f13eSChristoph Hellwig 83*e765f13eSChristoph Hellwig #if IS_ENABLED(CONFIG_FS_DAX) 841b764601SChristoph Hellwig /** 851b764601SChristoph Hellwig * dax_get_by_host() - temporary lookup mechanism for filesystem-dax 861b764601SChristoph Hellwig * @host: alternate name for the device registered by a dax driver 871b764601SChristoph Hellwig */ 881b764601SChristoph Hellwig static struct dax_device *dax_get_by_host(const char *host) 891b764601SChristoph Hellwig { 901b764601SChristoph Hellwig struct dax_device *dax_dev, *found = NULL; 911b764601SChristoph Hellwig int hash, id; 921b764601SChristoph Hellwig 931b764601SChristoph Hellwig if (!host) 941b764601SChristoph Hellwig return NULL; 951b764601SChristoph Hellwig 961b764601SChristoph Hellwig hash = dax_host_hash(host); 971b764601SChristoph Hellwig 981b764601SChristoph Hellwig id = dax_read_lock(); 991b764601SChristoph Hellwig spin_lock(&dax_host_lock); 1001b764601SChristoph Hellwig hlist_for_each_entry(dax_dev, &dax_host_list[hash], list) { 1011b764601SChristoph Hellwig if (!dax_alive(dax_dev) 1021b764601SChristoph Hellwig || strcmp(host, dax_dev->host) != 0) 1031b764601SChristoph Hellwig continue; 1041b764601SChristoph Hellwig 1051b764601SChristoph Hellwig if (igrab(&dax_dev->inode)) 1061b764601SChristoph Hellwig found = dax_dev; 1071b764601SChristoph Hellwig break; 1081b764601SChristoph Hellwig } 1091b764601SChristoph Hellwig spin_unlock(&dax_host_lock); 1101b764601SChristoph Hellwig dax_read_unlock(id); 1111b764601SChristoph Hellwig 1121b764601SChristoph Hellwig return found; 1131b764601SChristoph Hellwig } 1141b764601SChristoph Hellwig 11578f35473SDan Williams struct dax_device *fs_dax_get_by_bdev(struct block_device *bdev) 11678f35473SDan Williams { 117e556f6baSChristoph Hellwig if (!blk_queue_dax(bdev->bd_disk->queue)) 11878f35473SDan Williams return NULL; 119f01b16a8SVivek Goyal return dax_get_by_host(bdev->bd_disk->disk_name); 12078f35473SDan Williams } 12178f35473SDan Williams EXPORT_SYMBOL_GPL(fs_dax_get_by_bdev); 12278f35473SDan Williams 123cd93a2a4SChristoph Hellwig bool generic_fsdax_supported(struct dax_device *dax_dev, 1247bf7eac8SDan Williams struct block_device *bdev, int blocksize, sector_t start, 1257bf7eac8SDan Williams sector_t sectors) 126ef510424SDan Williams { 127e7638488SDan Williams bool dax_enabled = false; 128ad428cdbSDan Williams pgoff_t pgoff, pgoff_end; 129ad428cdbSDan Williams void *kaddr, *end_kaddr; 130ad428cdbSDan Williams pfn_t pfn, end_pfn; 131ad428cdbSDan Williams sector_t last_page; 132ad428cdbSDan Williams long len, len2; 133ad428cdbSDan Williams int err, id; 134ef510424SDan Williams 135ef510424SDan Williams if (blocksize != PAGE_SIZE) { 13639b6389aSChristoph Hellwig pr_info("%pg: error: unsupported blocksize for dax\n", bdev); 13780660f20SDave Jiang return false; 138ef510424SDan Williams } 139ef510424SDan Williams 140d4c5da50SAdrian Huang if (!dax_dev) { 14139b6389aSChristoph Hellwig pr_debug("%pg: error: dax unsupported by block device\n", bdev); 142d4c5da50SAdrian Huang return false; 143d4c5da50SAdrian Huang } 144d4c5da50SAdrian Huang 1457bf7eac8SDan Williams err = bdev_dax_pgoff(bdev, start, PAGE_SIZE, &pgoff); 146ef510424SDan Williams if (err) { 14739b6389aSChristoph Hellwig pr_info("%pg: error: unaligned partition for dax\n", bdev); 14880660f20SDave Jiang return false; 149ef510424SDan Williams } 150ef510424SDan Williams 1517bf7eac8SDan Williams last_page = PFN_DOWN((start + sectors - 1) * 512) * PAGE_SIZE / 512; 152ad428cdbSDan Williams err = bdev_dax_pgoff(bdev, last_page, PAGE_SIZE, &pgoff_end); 153ad428cdbSDan Williams if (err) { 15439b6389aSChristoph Hellwig pr_info("%pg: error: unaligned partition for dax\n", bdev); 155ad428cdbSDan Williams return false; 156ad428cdbSDan Williams } 157ad428cdbSDan Williams 158ef510424SDan Williams id = dax_read_lock(); 159ad428cdbSDan Williams len = dax_direct_access(dax_dev, pgoff, 1, &kaddr, &pfn); 160ad428cdbSDan Williams len2 = dax_direct_access(dax_dev, pgoff_end, 1, &end_kaddr, &end_pfn); 161ef510424SDan Williams 162ad428cdbSDan Williams if (len < 1 || len2 < 1) { 16339b6389aSChristoph Hellwig pr_info("%pg: error: dax access failed (%ld)\n", 16439b6389aSChristoph Hellwig bdev, len < 1 ? len : len2); 165eedfd73dSIra Weiny dax_read_unlock(id); 16680660f20SDave Jiang return false; 167ef510424SDan Williams } 168ef510424SDan Williams 1693fe0791cSDan Williams if (IS_ENABLED(CONFIG_FS_DAX_LIMITED) && pfn_t_special(pfn)) { 1703fe0791cSDan Williams /* 1713fe0791cSDan Williams * An arch that has enabled the pmem api should also 1723fe0791cSDan Williams * have its drivers support pfn_t_devmap() 1733fe0791cSDan Williams * 1743fe0791cSDan Williams * This is a developer warning and should not trigger in 1753fe0791cSDan Williams * production. dax_flush() will crash since it depends 1763fe0791cSDan Williams * on being able to do (page_address(pfn_to_page())). 1773fe0791cSDan Williams */ 1783fe0791cSDan Williams WARN_ON(IS_ENABLED(CONFIG_ARCH_HAS_PMEM_API)); 179e7638488SDan Williams dax_enabled = true; 180ad428cdbSDan Williams } else if (pfn_t_devmap(pfn) && pfn_t_devmap(end_pfn)) { 181ad428cdbSDan Williams struct dev_pagemap *pgmap, *end_pgmap; 182e7638488SDan Williams 183e7638488SDan Williams pgmap = get_dev_pagemap(pfn_t_to_pfn(pfn), NULL); 184ad428cdbSDan Williams end_pgmap = get_dev_pagemap(pfn_t_to_pfn(end_pfn), NULL); 185ad428cdbSDan Williams if (pgmap && pgmap == end_pgmap && pgmap->type == MEMORY_DEVICE_FS_DAX 186ad428cdbSDan Williams && pfn_t_to_page(pfn)->pgmap == pgmap 187ad428cdbSDan Williams && pfn_t_to_page(end_pfn)->pgmap == pgmap 188ad428cdbSDan Williams && pfn_t_to_pfn(pfn) == PHYS_PFN(__pa(kaddr)) 189ad428cdbSDan Williams && pfn_t_to_pfn(end_pfn) == PHYS_PFN(__pa(end_kaddr))) 190e7638488SDan Williams dax_enabled = true; 191e7638488SDan Williams put_dev_pagemap(pgmap); 192ad428cdbSDan Williams put_dev_pagemap(end_pgmap); 193ad428cdbSDan Williams 194e7638488SDan Williams } 195eedfd73dSIra Weiny dax_read_unlock(id); 196e7638488SDan Williams 197e7638488SDan Williams if (!dax_enabled) { 19839b6389aSChristoph Hellwig pr_info("%pg: error: dax support not enabled\n", bdev); 19980660f20SDave Jiang return false; 200569d0365SDan Williams } 20180660f20SDave Jiang return true; 202ef510424SDan Williams } 203cd93a2a4SChristoph Hellwig EXPORT_SYMBOL_GPL(generic_fsdax_supported); 20460b8340fSChristoph Hellwig 20560b8340fSChristoph Hellwig bool dax_supported(struct dax_device *dax_dev, struct block_device *bdev, 20660b8340fSChristoph Hellwig int blocksize, sector_t start, sector_t len) 20760b8340fSChristoph Hellwig { 20860b8340fSChristoph Hellwig bool ret = false; 20960b8340fSChristoph Hellwig int id; 21060b8340fSChristoph Hellwig 21160b8340fSChristoph Hellwig if (!dax_dev) 21260b8340fSChristoph Hellwig return false; 21360b8340fSChristoph Hellwig 21460b8340fSChristoph Hellwig id = dax_read_lock(); 21560b8340fSChristoph Hellwig if (dax_alive(dax_dev) && dax_dev->ops->dax_supported) 21660b8340fSChristoph Hellwig ret = dax_dev->ops->dax_supported(dax_dev, bdev, blocksize, 21760b8340fSChristoph Hellwig start, len); 21860b8340fSChristoph Hellwig dax_read_unlock(id); 21960b8340fSChristoph Hellwig return ret; 22060b8340fSChristoph Hellwig } 22160b8340fSChristoph Hellwig EXPORT_SYMBOL_GPL(dax_supported); 222cd93a2a4SChristoph Hellwig #endif /* CONFIG_FS_DAX */ 223bdd3c50dSChristoph Hellwig #endif /* CONFIG_BLOCK */ 224ef510424SDan Williams 2259a60c3efSDan Williams enum dax_device_flags { 2269a60c3efSDan Williams /* !alive + rcu grace period == no new operations / mappings */ 2279a60c3efSDan Williams DAXDEV_ALIVE, 2286e0c90d6SDan Williams /* gate whether dax_flush() calls the low level flush routine */ 2296e0c90d6SDan Williams DAXDEV_WRITE_CACHE, 230fefc1d97SPankaj Gupta /* flag to check if device supports synchronous flush */ 231fefc1d97SPankaj Gupta DAXDEV_SYNC, 2329a60c3efSDan Williams }; 2339a60c3efSDan Williams 234b0686260SDan Williams /** 235b0686260SDan Williams * dax_direct_access() - translate a device pgoff to an absolute pfn 236b0686260SDan Williams * @dax_dev: a dax_device instance representing the logical memory range 237b0686260SDan Williams * @pgoff: offset in pages from the start of the device to translate 238b0686260SDan Williams * @nr_pages: number of consecutive pages caller can handle relative to @pfn 239b0686260SDan Williams * @kaddr: output parameter that returns a virtual address mapping of pfn 240b0686260SDan Williams * @pfn: output parameter that returns an absolute pfn translation of @pgoff 241b0686260SDan Williams * 242b0686260SDan Williams * Return: negative errno if an error occurs, otherwise the number of 243b0686260SDan Williams * pages accessible at the device relative @pgoff. 244b0686260SDan Williams */ 245b0686260SDan Williams long dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff, long nr_pages, 246b0686260SDan Williams void **kaddr, pfn_t *pfn) 247b0686260SDan Williams { 248b0686260SDan Williams long avail; 249b0686260SDan Williams 250b0686260SDan Williams if (!dax_dev) 251b0686260SDan Williams return -EOPNOTSUPP; 252b0686260SDan Williams 253b0686260SDan Williams if (!dax_alive(dax_dev)) 254b0686260SDan Williams return -ENXIO; 255b0686260SDan Williams 256b0686260SDan Williams if (nr_pages < 0) 257b05d4c57SIra Weiny return -EINVAL; 258b0686260SDan Williams 259b0686260SDan Williams avail = dax_dev->ops->direct_access(dax_dev, pgoff, nr_pages, 260b0686260SDan Williams kaddr, pfn); 261b0686260SDan Williams if (!avail) 262b0686260SDan Williams return -ERANGE; 263b0686260SDan Williams return min(avail, nr_pages); 264b0686260SDan Williams } 265b0686260SDan Williams EXPORT_SYMBOL_GPL(dax_direct_access); 266b0686260SDan Williams 2677e026c8cSDan Williams size_t dax_copy_from_iter(struct dax_device *dax_dev, pgoff_t pgoff, void *addr, 2687e026c8cSDan Williams size_t bytes, struct iov_iter *i) 2697e026c8cSDan Williams { 2707e026c8cSDan Williams if (!dax_alive(dax_dev)) 2717e026c8cSDan Williams return 0; 2727e026c8cSDan Williams 2737e026c8cSDan Williams return dax_dev->ops->copy_from_iter(dax_dev, pgoff, addr, bytes, i); 2747e026c8cSDan Williams } 2757e026c8cSDan Williams EXPORT_SYMBOL_GPL(dax_copy_from_iter); 2767e026c8cSDan Williams 277b3a9a0c3SDan Williams size_t dax_copy_to_iter(struct dax_device *dax_dev, pgoff_t pgoff, void *addr, 278b3a9a0c3SDan Williams size_t bytes, struct iov_iter *i) 279b3a9a0c3SDan Williams { 280b3a9a0c3SDan Williams if (!dax_alive(dax_dev)) 281b3a9a0c3SDan Williams return 0; 282b3a9a0c3SDan Williams 283b3a9a0c3SDan Williams return dax_dev->ops->copy_to_iter(dax_dev, pgoff, addr, bytes, i); 284b3a9a0c3SDan Williams } 285b3a9a0c3SDan Williams EXPORT_SYMBOL_GPL(dax_copy_to_iter); 286b3a9a0c3SDan Williams 287f605a263SVivek Goyal int dax_zero_page_range(struct dax_device *dax_dev, pgoff_t pgoff, 288f605a263SVivek Goyal size_t nr_pages) 289f605a263SVivek Goyal { 290f605a263SVivek Goyal if (!dax_alive(dax_dev)) 291f605a263SVivek Goyal return -ENXIO; 292f605a263SVivek Goyal /* 293f605a263SVivek Goyal * There are no callers that want to zero more than one page as of now. 294f605a263SVivek Goyal * Once users are there, this check can be removed after the 295f605a263SVivek Goyal * device mapper code has been updated to split ranges across targets. 296f605a263SVivek Goyal */ 297f605a263SVivek Goyal if (nr_pages != 1) 298f605a263SVivek Goyal return -EIO; 299f605a263SVivek Goyal 300f605a263SVivek Goyal return dax_dev->ops->zero_page_range(dax_dev, pgoff, nr_pages); 301f605a263SVivek Goyal } 302f605a263SVivek Goyal EXPORT_SYMBOL_GPL(dax_zero_page_range); 303f605a263SVivek Goyal 304c3ca015fSMikulas Patocka #ifdef CONFIG_ARCH_HAS_PMEM_API 305c3ca015fSMikulas Patocka void arch_wb_cache_pmem(void *addr, size_t size); 306c3ca015fSMikulas Patocka void dax_flush(struct dax_device *dax_dev, void *addr, size_t size) 307abebfbe2SDan Williams { 308808c340bSRoss Zwisler if (unlikely(!dax_write_cache_enabled(dax_dev))) 3096e0c90d6SDan Williams return; 3106e0c90d6SDan Williams 311c3ca015fSMikulas Patocka arch_wb_cache_pmem(addr, size); 312abebfbe2SDan Williams } 313c3ca015fSMikulas Patocka #else 314c3ca015fSMikulas Patocka void dax_flush(struct dax_device *dax_dev, void *addr, size_t size) 315c3ca015fSMikulas Patocka { 316c3ca015fSMikulas Patocka } 317c3ca015fSMikulas Patocka #endif 318abebfbe2SDan Williams EXPORT_SYMBOL_GPL(dax_flush); 319abebfbe2SDan Williams 3206e0c90d6SDan Williams void dax_write_cache(struct dax_device *dax_dev, bool wc) 3216e0c90d6SDan Williams { 3226e0c90d6SDan Williams if (wc) 3236e0c90d6SDan Williams set_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags); 3246e0c90d6SDan Williams else 3256e0c90d6SDan Williams clear_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags); 3266e0c90d6SDan Williams } 3276e0c90d6SDan Williams EXPORT_SYMBOL_GPL(dax_write_cache); 3286e0c90d6SDan Williams 329273752c9SVivek Goyal bool dax_write_cache_enabled(struct dax_device *dax_dev) 330273752c9SVivek Goyal { 331273752c9SVivek Goyal return test_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags); 332273752c9SVivek Goyal } 333273752c9SVivek Goyal EXPORT_SYMBOL_GPL(dax_write_cache_enabled); 334273752c9SVivek Goyal 335fefc1d97SPankaj Gupta bool __dax_synchronous(struct dax_device *dax_dev) 336fefc1d97SPankaj Gupta { 337fefc1d97SPankaj Gupta return test_bit(DAXDEV_SYNC, &dax_dev->flags); 338fefc1d97SPankaj Gupta } 339fefc1d97SPankaj Gupta EXPORT_SYMBOL_GPL(__dax_synchronous); 340fefc1d97SPankaj Gupta 341fefc1d97SPankaj Gupta void __set_dax_synchronous(struct dax_device *dax_dev) 342fefc1d97SPankaj Gupta { 343fefc1d97SPankaj Gupta set_bit(DAXDEV_SYNC, &dax_dev->flags); 344fefc1d97SPankaj Gupta } 345fefc1d97SPankaj Gupta EXPORT_SYMBOL_GPL(__set_dax_synchronous); 346fefc1d97SPankaj Gupta 3477b6be844SDan Williams bool dax_alive(struct dax_device *dax_dev) 3487b6be844SDan Williams { 3497b6be844SDan Williams lockdep_assert_held(&dax_srcu); 3509a60c3efSDan Williams return test_bit(DAXDEV_ALIVE, &dax_dev->flags); 3517b6be844SDan Williams } 3527b6be844SDan Williams EXPORT_SYMBOL_GPL(dax_alive); 3537b6be844SDan Williams 3547b6be844SDan Williams /* 3557b6be844SDan Williams * Note, rcu is not protecting the liveness of dax_dev, rcu is ensuring 3567b6be844SDan Williams * that any fault handlers or operations that might have seen 3577b6be844SDan Williams * dax_alive(), have completed. Any operations that start after 3587b6be844SDan Williams * synchronize_srcu() has run will abort upon seeing !dax_alive(). 3597b6be844SDan Williams */ 3607b6be844SDan Williams void kill_dax(struct dax_device *dax_dev) 3617b6be844SDan Williams { 3627b6be844SDan Williams if (!dax_dev) 3637b6be844SDan Williams return; 3647b6be844SDan Williams 3659a60c3efSDan Williams clear_bit(DAXDEV_ALIVE, &dax_dev->flags); 36672058005SDan Williams 3677b6be844SDan Williams synchronize_srcu(&dax_srcu); 36872058005SDan Williams 36972058005SDan Williams spin_lock(&dax_host_lock); 37072058005SDan Williams hlist_del_init(&dax_dev->list); 37172058005SDan Williams spin_unlock(&dax_host_lock); 3727b6be844SDan Williams } 3737b6be844SDan Williams EXPORT_SYMBOL_GPL(kill_dax); 3747b6be844SDan Williams 3759567da0bSDan Williams void run_dax(struct dax_device *dax_dev) 3769567da0bSDan Williams { 3779567da0bSDan Williams set_bit(DAXDEV_ALIVE, &dax_dev->flags); 3789567da0bSDan Williams } 3799567da0bSDan Williams EXPORT_SYMBOL_GPL(run_dax); 3809567da0bSDan Williams 3817b6be844SDan Williams static struct inode *dax_alloc_inode(struct super_block *sb) 3827b6be844SDan Williams { 3837b6be844SDan Williams struct dax_device *dax_dev; 384b9d39d17SDan Williams struct inode *inode; 3857b6be844SDan Williams 3867b6be844SDan Williams dax_dev = kmem_cache_alloc(dax_cache, GFP_KERNEL); 3879f586fffSMikulas Patocka if (!dax_dev) 3889f586fffSMikulas Patocka return NULL; 3899f586fffSMikulas Patocka 390b9d39d17SDan Williams inode = &dax_dev->inode; 391b9d39d17SDan Williams inode->i_rdev = 0; 392b9d39d17SDan Williams return inode; 3937b6be844SDan Williams } 3947b6be844SDan Williams 3957b6be844SDan Williams static struct dax_device *to_dax_dev(struct inode *inode) 3967b6be844SDan Williams { 3977b6be844SDan Williams return container_of(inode, struct dax_device, inode); 3987b6be844SDan Williams } 3997b6be844SDan Williams 40053e22829SAl Viro static void dax_free_inode(struct inode *inode) 4017b6be844SDan Williams { 4027b6be844SDan Williams struct dax_device *dax_dev = to_dax_dev(inode); 40372058005SDan Williams kfree(dax_dev->host); 40472058005SDan Williams dax_dev->host = NULL; 405b9d39d17SDan Williams if (inode->i_rdev) 4066f24784fSAl Viro ida_simple_remove(&dax_minor_ida, iminor(inode)); 4077b6be844SDan Williams kmem_cache_free(dax_cache, dax_dev); 4087b6be844SDan Williams } 4097b6be844SDan Williams 4107b6be844SDan Williams static void dax_destroy_inode(struct inode *inode) 4117b6be844SDan Williams { 4127b6be844SDan Williams struct dax_device *dax_dev = to_dax_dev(inode); 4139a60c3efSDan Williams WARN_ONCE(test_bit(DAXDEV_ALIVE, &dax_dev->flags), 4147b6be844SDan Williams "kill_dax() must be called before final iput()\n"); 4157b6be844SDan Williams } 4167b6be844SDan Williams 4177b6be844SDan Williams static const struct super_operations dax_sops = { 4187b6be844SDan Williams .statfs = simple_statfs, 4197b6be844SDan Williams .alloc_inode = dax_alloc_inode, 4207b6be844SDan Williams .destroy_inode = dax_destroy_inode, 42153e22829SAl Viro .free_inode = dax_free_inode, 4227b6be844SDan Williams .drop_inode = generic_delete_inode, 4237b6be844SDan Williams }; 4247b6be844SDan Williams 42575d4e06fSDavid Howells static int dax_init_fs_context(struct fs_context *fc) 4267b6be844SDan Williams { 42775d4e06fSDavid Howells struct pseudo_fs_context *ctx = init_pseudo(fc, DAXFS_MAGIC); 42875d4e06fSDavid Howells if (!ctx) 42975d4e06fSDavid Howells return -ENOMEM; 43075d4e06fSDavid Howells ctx->ops = &dax_sops; 43175d4e06fSDavid Howells return 0; 4327b6be844SDan Williams } 4337b6be844SDan Williams 4347b6be844SDan Williams static struct file_system_type dax_fs_type = { 4357b6be844SDan Williams .name = "dax", 43675d4e06fSDavid Howells .init_fs_context = dax_init_fs_context, 4377b6be844SDan Williams .kill_sb = kill_anon_super, 4387b6be844SDan Williams }; 4397b6be844SDan Williams 4407b6be844SDan Williams static int dax_test(struct inode *inode, void *data) 4417b6be844SDan Williams { 4427b6be844SDan Williams dev_t devt = *(dev_t *) data; 4437b6be844SDan Williams 4447b6be844SDan Williams return inode->i_rdev == devt; 4457b6be844SDan Williams } 4467b6be844SDan Williams 4477b6be844SDan Williams static int dax_set(struct inode *inode, void *data) 4487b6be844SDan Williams { 4497b6be844SDan Williams dev_t devt = *(dev_t *) data; 4507b6be844SDan Williams 4517b6be844SDan Williams inode->i_rdev = devt; 4527b6be844SDan Williams return 0; 4537b6be844SDan Williams } 4547b6be844SDan Williams 4557b6be844SDan Williams static struct dax_device *dax_dev_get(dev_t devt) 4567b6be844SDan Williams { 4577b6be844SDan Williams struct dax_device *dax_dev; 4587b6be844SDan Williams struct inode *inode; 4597b6be844SDan Williams 4607b6be844SDan Williams inode = iget5_locked(dax_superblock, hash_32(devt + DAXFS_MAGIC, 31), 4617b6be844SDan Williams dax_test, dax_set, &devt); 4627b6be844SDan Williams 4637b6be844SDan Williams if (!inode) 4647b6be844SDan Williams return NULL; 4657b6be844SDan Williams 4667b6be844SDan Williams dax_dev = to_dax_dev(inode); 4677b6be844SDan Williams if (inode->i_state & I_NEW) { 4689a60c3efSDan Williams set_bit(DAXDEV_ALIVE, &dax_dev->flags); 4697b6be844SDan Williams inode->i_cdev = &dax_dev->cdev; 4707b6be844SDan Williams inode->i_mode = S_IFCHR; 4717b6be844SDan Williams inode->i_flags = S_DAX; 4727b6be844SDan Williams mapping_set_gfp_mask(&inode->i_data, GFP_USER); 4737b6be844SDan Williams unlock_new_inode(inode); 4747b6be844SDan Williams } 4757b6be844SDan Williams 4767b6be844SDan Williams return dax_dev; 4777b6be844SDan Williams } 4787b6be844SDan Williams 47972058005SDan Williams static void dax_add_host(struct dax_device *dax_dev, const char *host) 48072058005SDan Williams { 48172058005SDan Williams int hash; 48272058005SDan Williams 48372058005SDan Williams /* 48472058005SDan Williams * Unconditionally init dax_dev since it's coming from a 48572058005SDan Williams * non-zeroed slab cache 48672058005SDan Williams */ 48772058005SDan Williams INIT_HLIST_NODE(&dax_dev->list); 48872058005SDan Williams dax_dev->host = host; 48972058005SDan Williams if (!host) 49072058005SDan Williams return; 49172058005SDan Williams 49272058005SDan Williams hash = dax_host_hash(host); 49372058005SDan Williams spin_lock(&dax_host_lock); 49472058005SDan Williams hlist_add_head(&dax_dev->list, &dax_host_list[hash]); 49572058005SDan Williams spin_unlock(&dax_host_lock); 49672058005SDan Williams } 49772058005SDan Williams 4986568b08bSDan Williams struct dax_device *alloc_dax(void *private, const char *__host, 499fefc1d97SPankaj Gupta const struct dax_operations *ops, unsigned long flags) 5007b6be844SDan Williams { 5017b6be844SDan Williams struct dax_device *dax_dev; 50272058005SDan Williams const char *host; 5037b6be844SDan Williams dev_t devt; 5047b6be844SDan Williams int minor; 5057b6be844SDan Williams 5064e4ced93SVivek Goyal if (ops && !ops->zero_page_range) { 5074e4ced93SVivek Goyal pr_debug("%s: error: device does not provide dax" 5084e4ced93SVivek Goyal " operation zero_page_range()\n", 5094e4ced93SVivek Goyal __host ? __host : "Unknown"); 5104e4ced93SVivek Goyal return ERR_PTR(-EINVAL); 5114e4ced93SVivek Goyal } 5124e4ced93SVivek Goyal 51372058005SDan Williams host = kstrdup(__host, GFP_KERNEL); 51472058005SDan Williams if (__host && !host) 5154e4ced93SVivek Goyal return ERR_PTR(-ENOMEM); 51672058005SDan Williams 517cf1e2289SDan Williams minor = ida_simple_get(&dax_minor_ida, 0, MINORMASK+1, GFP_KERNEL); 5187b6be844SDan Williams if (minor < 0) 51972058005SDan Williams goto err_minor; 5207b6be844SDan Williams 5217b6be844SDan Williams devt = MKDEV(MAJOR(dax_devt), minor); 5227b6be844SDan Williams dax_dev = dax_dev_get(devt); 5237b6be844SDan Williams if (!dax_dev) 52472058005SDan Williams goto err_dev; 5257b6be844SDan Williams 52672058005SDan Williams dax_add_host(dax_dev, host); 5276568b08bSDan Williams dax_dev->ops = ops; 5287b6be844SDan Williams dax_dev->private = private; 529fefc1d97SPankaj Gupta if (flags & DAXDEV_F_SYNC) 530fefc1d97SPankaj Gupta set_dax_synchronous(dax_dev); 531fefc1d97SPankaj Gupta 5327b6be844SDan Williams return dax_dev; 5337b6be844SDan Williams 53472058005SDan Williams err_dev: 5357b6be844SDan Williams ida_simple_remove(&dax_minor_ida, minor); 53672058005SDan Williams err_minor: 53772058005SDan Williams kfree(host); 5384e4ced93SVivek Goyal return ERR_PTR(-ENOMEM); 5397b6be844SDan Williams } 5407b6be844SDan Williams EXPORT_SYMBOL_GPL(alloc_dax); 5417b6be844SDan Williams 5427b6be844SDan Williams void put_dax(struct dax_device *dax_dev) 5437b6be844SDan Williams { 5447b6be844SDan Williams if (!dax_dev) 5457b6be844SDan Williams return; 5467b6be844SDan Williams iput(&dax_dev->inode); 5477b6be844SDan Williams } 5487b6be844SDan Williams EXPORT_SYMBOL_GPL(put_dax); 5497b6be844SDan Williams 5507b6be844SDan Williams /** 5517b6be844SDan Williams * inode_dax: convert a public inode into its dax_dev 5527b6be844SDan Williams * @inode: An inode with i_cdev pointing to a dax_dev 5537b6be844SDan Williams * 5547b6be844SDan Williams * Note this is not equivalent to to_dax_dev() which is for private 5557b6be844SDan Williams * internal use where we know the inode filesystem type == dax_fs_type. 5567b6be844SDan Williams */ 5577b6be844SDan Williams struct dax_device *inode_dax(struct inode *inode) 5587b6be844SDan Williams { 5597b6be844SDan Williams struct cdev *cdev = inode->i_cdev; 5607b6be844SDan Williams 5617b6be844SDan Williams return container_of(cdev, struct dax_device, cdev); 5627b6be844SDan Williams } 5637b6be844SDan Williams EXPORT_SYMBOL_GPL(inode_dax); 5647b6be844SDan Williams 5657b6be844SDan Williams struct inode *dax_inode(struct dax_device *dax_dev) 5667b6be844SDan Williams { 5677b6be844SDan Williams return &dax_dev->inode; 5687b6be844SDan Williams } 5697b6be844SDan Williams EXPORT_SYMBOL_GPL(dax_inode); 5707b6be844SDan Williams 5717b6be844SDan Williams void *dax_get_private(struct dax_device *dax_dev) 5727b6be844SDan Williams { 5739567da0bSDan Williams if (!test_bit(DAXDEV_ALIVE, &dax_dev->flags)) 5749567da0bSDan Williams return NULL; 5757b6be844SDan Williams return dax_dev->private; 5767b6be844SDan Williams } 5777b6be844SDan Williams EXPORT_SYMBOL_GPL(dax_get_private); 5787b6be844SDan Williams 5797b6be844SDan Williams static void init_once(void *_dax_dev) 5807b6be844SDan Williams { 5817b6be844SDan Williams struct dax_device *dax_dev = _dax_dev; 5827b6be844SDan Williams struct inode *inode = &dax_dev->inode; 5837b6be844SDan Williams 584b9d39d17SDan Williams memset(dax_dev, 0, sizeof(*dax_dev)); 5857b6be844SDan Williams inode_init_once(inode); 5867b6be844SDan Williams } 5877b6be844SDan Williams 5889567da0bSDan Williams static int dax_fs_init(void) 5897b6be844SDan Williams { 5907b6be844SDan Williams int rc; 5917b6be844SDan Williams 5927b6be844SDan Williams dax_cache = kmem_cache_create("dax_cache", sizeof(struct dax_device), 0, 5937b6be844SDan Williams (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT| 5947b6be844SDan Williams SLAB_MEM_SPREAD|SLAB_ACCOUNT), 5957b6be844SDan Williams init_once); 5967b6be844SDan Williams if (!dax_cache) 5977b6be844SDan Williams return -ENOMEM; 5987b6be844SDan Williams 5997b6be844SDan Williams dax_mnt = kern_mount(&dax_fs_type); 6007b6be844SDan Williams if (IS_ERR(dax_mnt)) { 6017b6be844SDan Williams rc = PTR_ERR(dax_mnt); 6027b6be844SDan Williams goto err_mount; 6037b6be844SDan Williams } 6047b6be844SDan Williams dax_superblock = dax_mnt->mnt_sb; 6057b6be844SDan Williams 6067b6be844SDan Williams return 0; 6077b6be844SDan Williams 6087b6be844SDan Williams err_mount: 6097b6be844SDan Williams kmem_cache_destroy(dax_cache); 6107b6be844SDan Williams 6117b6be844SDan Williams return rc; 6127b6be844SDan Williams } 6137b6be844SDan Williams 6149567da0bSDan Williams static void dax_fs_exit(void) 6157b6be844SDan Williams { 6167b6be844SDan Williams kern_unmount(dax_mnt); 6177b6be844SDan Williams kmem_cache_destroy(dax_cache); 6187b6be844SDan Williams } 6197b6be844SDan Williams 6209567da0bSDan Williams static int __init dax_core_init(void) 6217b6be844SDan Williams { 6227b6be844SDan Williams int rc; 6237b6be844SDan Williams 6249567da0bSDan Williams rc = dax_fs_init(); 6257b6be844SDan Williams if (rc) 6267b6be844SDan Williams return rc; 6277b6be844SDan Williams 628cf1e2289SDan Williams rc = alloc_chrdev_region(&dax_devt, 0, MINORMASK+1, "dax"); 6297b6be844SDan Williams if (rc) 6309567da0bSDan Williams goto err_chrdev; 6319567da0bSDan Williams 6329567da0bSDan Williams rc = dax_bus_init(); 6339567da0bSDan Williams if (rc) 6349567da0bSDan Williams goto err_bus; 6359567da0bSDan Williams return 0; 6369567da0bSDan Williams 6379567da0bSDan Williams err_bus: 6389567da0bSDan Williams unregister_chrdev_region(dax_devt, MINORMASK+1); 6399567da0bSDan Williams err_chrdev: 6409567da0bSDan Williams dax_fs_exit(); 6419567da0bSDan Williams return 0; 6427b6be844SDan Williams } 6437b6be844SDan Williams 6449567da0bSDan Williams static void __exit dax_core_exit(void) 6457b6be844SDan Williams { 6461aa57431SWang Hai dax_bus_exit(); 647cf1e2289SDan Williams unregister_chrdev_region(dax_devt, MINORMASK+1); 6487b6be844SDan Williams ida_destroy(&dax_minor_ida); 6499567da0bSDan Williams dax_fs_exit(); 6507b6be844SDan Williams } 6517b6be844SDan Williams 6527b6be844SDan Williams MODULE_AUTHOR("Intel Corporation"); 6537b6be844SDan Williams MODULE_LICENSE("GPL v2"); 6549567da0bSDan Williams subsys_initcall(dax_core_init); 6559567da0bSDan Williams module_exit(dax_core_exit); 656