Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright(c) 2017 Intel Corporation. All rights reserved.
0004  */
0005 #include <linux/pagemap.h>
0006 #include <linux/module.h>
0007 #include <linux/mount.h>
0008 #include <linux/pseudo_fs.h>
0009 #include <linux/magic.h>
0010 #include <linux/pfn_t.h>
0011 #include <linux/cdev.h>
0012 #include <linux/slab.h>
0013 #include <linux/uio.h>
0014 #include <linux/dax.h>
0015 #include <linux/fs.h>
0016 #include "dax-private.h"
0017 
0018 /**
0019  * struct dax_device - anchor object for dax services
0020  * @inode: core vfs
0021  * @cdev: optional character interface for "device dax"
0022  * @private: dax driver private data
0023  * @flags: state and boolean properties
0024  * @ops: operations for this device
0025  * @holder_data: holder of a dax_device: could be filesystem or mapped device
0026  * @holder_ops: operations for the inner holder
0027  */
0028 struct dax_device {
0029     struct inode inode;
0030     struct cdev cdev;
0031     void *private;
0032     unsigned long flags;
0033     const struct dax_operations *ops;
0034     void *holder_data;
0035     const struct dax_holder_operations *holder_ops;
0036 };
0037 
0038 static dev_t dax_devt;
0039 DEFINE_STATIC_SRCU(dax_srcu);
0040 static struct vfsmount *dax_mnt;
0041 static DEFINE_IDA(dax_minor_ida);
0042 static struct kmem_cache *dax_cache __read_mostly;
0043 static struct super_block *dax_superblock __read_mostly;
0044 
0045 int dax_read_lock(void)
0046 {
0047     return srcu_read_lock(&dax_srcu);
0048 }
0049 EXPORT_SYMBOL_GPL(dax_read_lock);
0050 
0051 void dax_read_unlock(int id)
0052 {
0053     srcu_read_unlock(&dax_srcu, id);
0054 }
0055 EXPORT_SYMBOL_GPL(dax_read_unlock);
0056 
0057 #if defined(CONFIG_BLOCK) && defined(CONFIG_FS_DAX)
0058 #include <linux/blkdev.h>
0059 
0060 static DEFINE_XARRAY(dax_hosts);
0061 
0062 int dax_add_host(struct dax_device *dax_dev, struct gendisk *disk)
0063 {
0064     return xa_insert(&dax_hosts, (unsigned long)disk, dax_dev, GFP_KERNEL);
0065 }
0066 EXPORT_SYMBOL_GPL(dax_add_host);
0067 
0068 void dax_remove_host(struct gendisk *disk)
0069 {
0070     xa_erase(&dax_hosts, (unsigned long)disk);
0071 }
0072 EXPORT_SYMBOL_GPL(dax_remove_host);
0073 
0074 /**
0075  * fs_dax_get_by_bdev() - temporary lookup mechanism for filesystem-dax
0076  * @bdev: block device to find a dax_device for
0077  * @start_off: returns the byte offset into the dax_device that @bdev starts
0078  * @holder: filesystem or mapped device inside the dax_device
0079  * @ops: operations for the inner holder
0080  */
0081 struct dax_device *fs_dax_get_by_bdev(struct block_device *bdev, u64 *start_off,
0082         void *holder, const struct dax_holder_operations *ops)
0083 {
0084     struct dax_device *dax_dev;
0085     u64 part_size;
0086     int id;
0087 
0088     if (!blk_queue_dax(bdev->bd_disk->queue))
0089         return NULL;
0090 
0091     *start_off = get_start_sect(bdev) * SECTOR_SIZE;
0092     part_size = bdev_nr_sectors(bdev) * SECTOR_SIZE;
0093     if (*start_off % PAGE_SIZE || part_size % PAGE_SIZE) {
0094         pr_info("%pg: error: unaligned partition for dax\n", bdev);
0095         return NULL;
0096     }
0097 
0098     id = dax_read_lock();
0099     dax_dev = xa_load(&dax_hosts, (unsigned long)bdev->bd_disk);
0100     if (!dax_dev || !dax_alive(dax_dev) || !igrab(&dax_dev->inode))
0101         dax_dev = NULL;
0102     else if (holder) {
0103         if (!cmpxchg(&dax_dev->holder_data, NULL, holder))
0104             dax_dev->holder_ops = ops;
0105         else
0106             dax_dev = NULL;
0107     }
0108     dax_read_unlock(id);
0109 
0110     return dax_dev;
0111 }
0112 EXPORT_SYMBOL_GPL(fs_dax_get_by_bdev);
0113 
0114 void fs_put_dax(struct dax_device *dax_dev, void *holder)
0115 {
0116     if (dax_dev && holder &&
0117         cmpxchg(&dax_dev->holder_data, holder, NULL) == holder)
0118         dax_dev->holder_ops = NULL;
0119     put_dax(dax_dev);
0120 }
0121 EXPORT_SYMBOL_GPL(fs_put_dax);
0122 #endif /* CONFIG_BLOCK && CONFIG_FS_DAX */
0123 
0124 enum dax_device_flags {
0125     /* !alive + rcu grace period == no new operations / mappings */
0126     DAXDEV_ALIVE,
0127     /* gate whether dax_flush() calls the low level flush routine */
0128     DAXDEV_WRITE_CACHE,
0129     /* flag to check if device supports synchronous flush */
0130     DAXDEV_SYNC,
0131     /* do not leave the caches dirty after writes */
0132     DAXDEV_NOCACHE,
0133     /* handle CPU fetch exceptions during reads */
0134     DAXDEV_NOMC,
0135 };
0136 
0137 /**
0138  * dax_direct_access() - translate a device pgoff to an absolute pfn
0139  * @dax_dev: a dax_device instance representing the logical memory range
0140  * @pgoff: offset in pages from the start of the device to translate
0141  * @nr_pages: number of consecutive pages caller can handle relative to @pfn
0142  * @mode: indicator on normal access or recovery write
0143  * @kaddr: output parameter that returns a virtual address mapping of pfn
0144  * @pfn: output parameter that returns an absolute pfn translation of @pgoff
0145  *
0146  * Return: negative errno if an error occurs, otherwise the number of
0147  * pages accessible at the device relative @pgoff.
0148  */
0149 long dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff, long nr_pages,
0150         enum dax_access_mode mode, void **kaddr, pfn_t *pfn)
0151 {
0152     long avail;
0153 
0154     if (!dax_dev)
0155         return -EOPNOTSUPP;
0156 
0157     if (!dax_alive(dax_dev))
0158         return -ENXIO;
0159 
0160     if (nr_pages < 0)
0161         return -EINVAL;
0162 
0163     avail = dax_dev->ops->direct_access(dax_dev, pgoff, nr_pages,
0164             mode, kaddr, pfn);
0165     if (!avail)
0166         return -ERANGE;
0167     return min(avail, nr_pages);
0168 }
0169 EXPORT_SYMBOL_GPL(dax_direct_access);
0170 
0171 size_t dax_copy_from_iter(struct dax_device *dax_dev, pgoff_t pgoff, void *addr,
0172         size_t bytes, struct iov_iter *i)
0173 {
0174     if (!dax_alive(dax_dev))
0175         return 0;
0176 
0177     /*
0178      * The userspace address for the memory copy has already been validated
0179      * via access_ok() in vfs_write, so use the 'no check' version to bypass
0180      * the HARDENED_USERCOPY overhead.
0181      */
0182     if (test_bit(DAXDEV_NOCACHE, &dax_dev->flags))
0183         return _copy_from_iter_flushcache(addr, bytes, i);
0184     return _copy_from_iter(addr, bytes, i);
0185 }
0186 
0187 size_t dax_copy_to_iter(struct dax_device *dax_dev, pgoff_t pgoff, void *addr,
0188         size_t bytes, struct iov_iter *i)
0189 {
0190     if (!dax_alive(dax_dev))
0191         return 0;
0192 
0193     /*
0194      * The userspace address for the memory copy has already been validated
0195      * via access_ok() in vfs_red, so use the 'no check' version to bypass
0196      * the HARDENED_USERCOPY overhead.
0197      */
0198     if (test_bit(DAXDEV_NOMC, &dax_dev->flags))
0199         return _copy_mc_to_iter(addr, bytes, i);
0200     return _copy_to_iter(addr, bytes, i);
0201 }
0202 
0203 int dax_zero_page_range(struct dax_device *dax_dev, pgoff_t pgoff,
0204             size_t nr_pages)
0205 {
0206     if (!dax_alive(dax_dev))
0207         return -ENXIO;
0208     /*
0209      * There are no callers that want to zero more than one page as of now.
0210      * Once users are there, this check can be removed after the
0211      * device mapper code has been updated to split ranges across targets.
0212      */
0213     if (nr_pages != 1)
0214         return -EIO;
0215 
0216     return dax_dev->ops->zero_page_range(dax_dev, pgoff, nr_pages);
0217 }
0218 EXPORT_SYMBOL_GPL(dax_zero_page_range);
0219 
0220 size_t dax_recovery_write(struct dax_device *dax_dev, pgoff_t pgoff,
0221         void *addr, size_t bytes, struct iov_iter *iter)
0222 {
0223     if (!dax_dev->ops->recovery_write)
0224         return 0;
0225     return dax_dev->ops->recovery_write(dax_dev, pgoff, addr, bytes, iter);
0226 }
0227 EXPORT_SYMBOL_GPL(dax_recovery_write);
0228 
0229 int dax_holder_notify_failure(struct dax_device *dax_dev, u64 off,
0230                   u64 len, int mf_flags)
0231 {
0232     int rc, id;
0233 
0234     id = dax_read_lock();
0235     if (!dax_alive(dax_dev)) {
0236         rc = -ENXIO;
0237         goto out;
0238     }
0239 
0240     if (!dax_dev->holder_ops) {
0241         rc = -EOPNOTSUPP;
0242         goto out;
0243     }
0244 
0245     rc = dax_dev->holder_ops->notify_failure(dax_dev, off, len, mf_flags);
0246 out:
0247     dax_read_unlock(id);
0248     return rc;
0249 }
0250 EXPORT_SYMBOL_GPL(dax_holder_notify_failure);
0251 
0252 #ifdef CONFIG_ARCH_HAS_PMEM_API
0253 void arch_wb_cache_pmem(void *addr, size_t size);
0254 void dax_flush(struct dax_device *dax_dev, void *addr, size_t size)
0255 {
0256     if (unlikely(!dax_write_cache_enabled(dax_dev)))
0257         return;
0258 
0259     arch_wb_cache_pmem(addr, size);
0260 }
0261 #else
0262 void dax_flush(struct dax_device *dax_dev, void *addr, size_t size)
0263 {
0264 }
0265 #endif
0266 EXPORT_SYMBOL_GPL(dax_flush);
0267 
0268 void dax_write_cache(struct dax_device *dax_dev, bool wc)
0269 {
0270     if (wc)
0271         set_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags);
0272     else
0273         clear_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags);
0274 }
0275 EXPORT_SYMBOL_GPL(dax_write_cache);
0276 
0277 bool dax_write_cache_enabled(struct dax_device *dax_dev)
0278 {
0279     return test_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags);
0280 }
0281 EXPORT_SYMBOL_GPL(dax_write_cache_enabled);
0282 
0283 bool dax_synchronous(struct dax_device *dax_dev)
0284 {
0285     return test_bit(DAXDEV_SYNC, &dax_dev->flags);
0286 }
0287 EXPORT_SYMBOL_GPL(dax_synchronous);
0288 
0289 void set_dax_synchronous(struct dax_device *dax_dev)
0290 {
0291     set_bit(DAXDEV_SYNC, &dax_dev->flags);
0292 }
0293 EXPORT_SYMBOL_GPL(set_dax_synchronous);
0294 
0295 void set_dax_nocache(struct dax_device *dax_dev)
0296 {
0297     set_bit(DAXDEV_NOCACHE, &dax_dev->flags);
0298 }
0299 EXPORT_SYMBOL_GPL(set_dax_nocache);
0300 
0301 void set_dax_nomc(struct dax_device *dax_dev)
0302 {
0303     set_bit(DAXDEV_NOMC, &dax_dev->flags);
0304 }
0305 EXPORT_SYMBOL_GPL(set_dax_nomc);
0306 
0307 bool dax_alive(struct dax_device *dax_dev)
0308 {
0309     lockdep_assert_held(&dax_srcu);
0310     return test_bit(DAXDEV_ALIVE, &dax_dev->flags);
0311 }
0312 EXPORT_SYMBOL_GPL(dax_alive);
0313 
0314 /*
0315  * Note, rcu is not protecting the liveness of dax_dev, rcu is ensuring
0316  * that any fault handlers or operations that might have seen
0317  * dax_alive(), have completed.  Any operations that start after
0318  * synchronize_srcu() has run will abort upon seeing !dax_alive().
0319  */
0320 void kill_dax(struct dax_device *dax_dev)
0321 {
0322     if (!dax_dev)
0323         return;
0324 
0325     if (dax_dev->holder_data != NULL)
0326         dax_holder_notify_failure(dax_dev, 0, U64_MAX, 0);
0327 
0328     clear_bit(DAXDEV_ALIVE, &dax_dev->flags);
0329     synchronize_srcu(&dax_srcu);
0330 
0331     /* clear holder data */
0332     dax_dev->holder_ops = NULL;
0333     dax_dev->holder_data = NULL;
0334 }
0335 EXPORT_SYMBOL_GPL(kill_dax);
0336 
0337 void run_dax(struct dax_device *dax_dev)
0338 {
0339     set_bit(DAXDEV_ALIVE, &dax_dev->flags);
0340 }
0341 EXPORT_SYMBOL_GPL(run_dax);
0342 
0343 static struct inode *dax_alloc_inode(struct super_block *sb)
0344 {
0345     struct dax_device *dax_dev;
0346     struct inode *inode;
0347 
0348     dax_dev = alloc_inode_sb(sb, dax_cache, GFP_KERNEL);
0349     if (!dax_dev)
0350         return NULL;
0351 
0352     inode = &dax_dev->inode;
0353     inode->i_rdev = 0;
0354     return inode;
0355 }
0356 
0357 static struct dax_device *to_dax_dev(struct inode *inode)
0358 {
0359     return container_of(inode, struct dax_device, inode);
0360 }
0361 
0362 static void dax_free_inode(struct inode *inode)
0363 {
0364     struct dax_device *dax_dev = to_dax_dev(inode);
0365     if (inode->i_rdev)
0366         ida_simple_remove(&dax_minor_ida, iminor(inode));
0367     kmem_cache_free(dax_cache, dax_dev);
0368 }
0369 
0370 static void dax_destroy_inode(struct inode *inode)
0371 {
0372     struct dax_device *dax_dev = to_dax_dev(inode);
0373     WARN_ONCE(test_bit(DAXDEV_ALIVE, &dax_dev->flags),
0374             "kill_dax() must be called before final iput()\n");
0375 }
0376 
0377 static const struct super_operations dax_sops = {
0378     .statfs = simple_statfs,
0379     .alloc_inode = dax_alloc_inode,
0380     .destroy_inode = dax_destroy_inode,
0381     .free_inode = dax_free_inode,
0382     .drop_inode = generic_delete_inode,
0383 };
0384 
0385 static int dax_init_fs_context(struct fs_context *fc)
0386 {
0387     struct pseudo_fs_context *ctx = init_pseudo(fc, DAXFS_MAGIC);
0388     if (!ctx)
0389         return -ENOMEM;
0390     ctx->ops = &dax_sops;
0391     return 0;
0392 }
0393 
0394 static struct file_system_type dax_fs_type = {
0395     .name       = "dax",
0396     .init_fs_context = dax_init_fs_context,
0397     .kill_sb    = kill_anon_super,
0398 };
0399 
0400 static int dax_test(struct inode *inode, void *data)
0401 {
0402     dev_t devt = *(dev_t *) data;
0403 
0404     return inode->i_rdev == devt;
0405 }
0406 
0407 static int dax_set(struct inode *inode, void *data)
0408 {
0409     dev_t devt = *(dev_t *) data;
0410 
0411     inode->i_rdev = devt;
0412     return 0;
0413 }
0414 
0415 static struct dax_device *dax_dev_get(dev_t devt)
0416 {
0417     struct dax_device *dax_dev;
0418     struct inode *inode;
0419 
0420     inode = iget5_locked(dax_superblock, hash_32(devt + DAXFS_MAGIC, 31),
0421             dax_test, dax_set, &devt);
0422 
0423     if (!inode)
0424         return NULL;
0425 
0426     dax_dev = to_dax_dev(inode);
0427     if (inode->i_state & I_NEW) {
0428         set_bit(DAXDEV_ALIVE, &dax_dev->flags);
0429         inode->i_cdev = &dax_dev->cdev;
0430         inode->i_mode = S_IFCHR;
0431         inode->i_flags = S_DAX;
0432         mapping_set_gfp_mask(&inode->i_data, GFP_USER);
0433         unlock_new_inode(inode);
0434     }
0435 
0436     return dax_dev;
0437 }
0438 
0439 struct dax_device *alloc_dax(void *private, const struct dax_operations *ops)
0440 {
0441     struct dax_device *dax_dev;
0442     dev_t devt;
0443     int minor;
0444 
0445     if (WARN_ON_ONCE(ops && !ops->zero_page_range))
0446         return ERR_PTR(-EINVAL);
0447 
0448     minor = ida_simple_get(&dax_minor_ida, 0, MINORMASK+1, GFP_KERNEL);
0449     if (minor < 0)
0450         return ERR_PTR(-ENOMEM);
0451 
0452     devt = MKDEV(MAJOR(dax_devt), minor);
0453     dax_dev = dax_dev_get(devt);
0454     if (!dax_dev)
0455         goto err_dev;
0456 
0457     dax_dev->ops = ops;
0458     dax_dev->private = private;
0459     return dax_dev;
0460 
0461  err_dev:
0462     ida_simple_remove(&dax_minor_ida, minor);
0463     return ERR_PTR(-ENOMEM);
0464 }
0465 EXPORT_SYMBOL_GPL(alloc_dax);
0466 
0467 void put_dax(struct dax_device *dax_dev)
0468 {
0469     if (!dax_dev)
0470         return;
0471     iput(&dax_dev->inode);
0472 }
0473 EXPORT_SYMBOL_GPL(put_dax);
0474 
0475 /**
0476  * dax_holder() - obtain the holder of a dax device
0477  * @dax_dev: a dax_device instance
0478 
0479  * Return: the holder's data which represents the holder if registered,
0480  * otherwize NULL.
0481  */
0482 void *dax_holder(struct dax_device *dax_dev)
0483 {
0484     return dax_dev->holder_data;
0485 }
0486 EXPORT_SYMBOL_GPL(dax_holder);
0487 
0488 /**
0489  * inode_dax: convert a public inode into its dax_dev
0490  * @inode: An inode with i_cdev pointing to a dax_dev
0491  *
0492  * Note this is not equivalent to to_dax_dev() which is for private
0493  * internal use where we know the inode filesystem type == dax_fs_type.
0494  */
0495 struct dax_device *inode_dax(struct inode *inode)
0496 {
0497     struct cdev *cdev = inode->i_cdev;
0498 
0499     return container_of(cdev, struct dax_device, cdev);
0500 }
0501 EXPORT_SYMBOL_GPL(inode_dax);
0502 
0503 struct inode *dax_inode(struct dax_device *dax_dev)
0504 {
0505     return &dax_dev->inode;
0506 }
0507 EXPORT_SYMBOL_GPL(dax_inode);
0508 
0509 void *dax_get_private(struct dax_device *dax_dev)
0510 {
0511     if (!test_bit(DAXDEV_ALIVE, &dax_dev->flags))
0512         return NULL;
0513     return dax_dev->private;
0514 }
0515 EXPORT_SYMBOL_GPL(dax_get_private);
0516 
0517 static void init_once(void *_dax_dev)
0518 {
0519     struct dax_device *dax_dev = _dax_dev;
0520     struct inode *inode = &dax_dev->inode;
0521 
0522     memset(dax_dev, 0, sizeof(*dax_dev));
0523     inode_init_once(inode);
0524 }
0525 
0526 static int dax_fs_init(void)
0527 {
0528     int rc;
0529 
0530     dax_cache = kmem_cache_create("dax_cache", sizeof(struct dax_device), 0,
0531             (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
0532              SLAB_MEM_SPREAD|SLAB_ACCOUNT),
0533             init_once);
0534     if (!dax_cache)
0535         return -ENOMEM;
0536 
0537     dax_mnt = kern_mount(&dax_fs_type);
0538     if (IS_ERR(dax_mnt)) {
0539         rc = PTR_ERR(dax_mnt);
0540         goto err_mount;
0541     }
0542     dax_superblock = dax_mnt->mnt_sb;
0543 
0544     return 0;
0545 
0546  err_mount:
0547     kmem_cache_destroy(dax_cache);
0548 
0549     return rc;
0550 }
0551 
0552 static void dax_fs_exit(void)
0553 {
0554     kern_unmount(dax_mnt);
0555     rcu_barrier();
0556     kmem_cache_destroy(dax_cache);
0557 }
0558 
0559 static int __init dax_core_init(void)
0560 {
0561     int rc;
0562 
0563     rc = dax_fs_init();
0564     if (rc)
0565         return rc;
0566 
0567     rc = alloc_chrdev_region(&dax_devt, 0, MINORMASK+1, "dax");
0568     if (rc)
0569         goto err_chrdev;
0570 
0571     rc = dax_bus_init();
0572     if (rc)
0573         goto err_bus;
0574     return 0;
0575 
0576 err_bus:
0577     unregister_chrdev_region(dax_devt, MINORMASK+1);
0578 err_chrdev:
0579     dax_fs_exit();
0580     return 0;
0581 }
0582 
0583 static void __exit dax_core_exit(void)
0584 {
0585     dax_bus_exit();
0586     unregister_chrdev_region(dax_devt, MINORMASK+1);
0587     ida_destroy(&dax_minor_ida);
0588     dax_fs_exit();
0589 }
0590 
0591 MODULE_AUTHOR("Intel Corporation");
0592 MODULE_LICENSE("GPL v2");
0593 subsys_initcall(dax_core_init);
0594 module_exit(dax_core_exit);