super.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright(c) 2017 Intel Corporation. All rights reserved.
  4. */
  5. #include <linux/pagemap.h>
  6. #include <linux/module.h>
  7. #include <linux/mount.h>
  8. #include <linux/pseudo_fs.h>
  9. #include <linux/magic.h>
  10. #include <linux/pfn_t.h>
  11. #include <linux/cdev.h>
  12. #include <linux/slab.h>
  13. #include <linux/uio.h>
  14. #include <linux/dax.h>
  15. #include <linux/fs.h>
  16. #include "dax-private.h"
  17. /**
  18. * struct dax_device - anchor object for dax services
  19. * @inode: core vfs
  20. * @cdev: optional character interface for "device dax"
  21. * @private: dax driver private data
  22. * @flags: state and boolean properties
  23. * @ops: operations for this device
  24. * @holder_data: holder of a dax_device: could be filesystem or mapped device
  25. * @holder_ops: operations for the inner holder
  26. */
  27. struct dax_device {
  28. struct inode inode;
  29. struct cdev cdev;
  30. void *private;
  31. unsigned long flags;
  32. const struct dax_operations *ops;
  33. void *holder_data;
  34. const struct dax_holder_operations *holder_ops;
  35. };
  36. static dev_t dax_devt;
  37. DEFINE_STATIC_SRCU(dax_srcu);
  38. static struct vfsmount *dax_mnt;
  39. static DEFINE_IDA(dax_minor_ida);
  40. static struct kmem_cache *dax_cache __read_mostly;
  41. static struct super_block *dax_superblock __read_mostly;
  42. int dax_read_lock(void)
  43. {
  44. return srcu_read_lock(&dax_srcu);
  45. }
  46. EXPORT_SYMBOL_GPL(dax_read_lock);
  47. void dax_read_unlock(int id)
  48. {
  49. srcu_read_unlock(&dax_srcu, id);
  50. }
  51. EXPORT_SYMBOL_GPL(dax_read_unlock);
  52. #if defined(CONFIG_BLOCK) && defined(CONFIG_FS_DAX)
  53. #include <linux/blkdev.h>
  54. static DEFINE_XARRAY(dax_hosts);
  55. int dax_add_host(struct dax_device *dax_dev, struct gendisk *disk)
  56. {
  57. return xa_insert(&dax_hosts, (unsigned long)disk, dax_dev, GFP_KERNEL);
  58. }
  59. EXPORT_SYMBOL_GPL(dax_add_host);
  60. void dax_remove_host(struct gendisk *disk)
  61. {
  62. xa_erase(&dax_hosts, (unsigned long)disk);
  63. }
  64. EXPORT_SYMBOL_GPL(dax_remove_host);
  65. /**
  66. * fs_dax_get_by_bdev() - temporary lookup mechanism for filesystem-dax
  67. * @bdev: block device to find a dax_device for
  68. * @start_off: returns the byte offset into the dax_device that @bdev starts
  69. * @holder: filesystem or mapped device inside the dax_device
  70. * @ops: operations for the inner holder
  71. */
  72. struct dax_device *fs_dax_get_by_bdev(struct block_device *bdev, u64 *start_off,
  73. void *holder, const struct dax_holder_operations *ops)
  74. {
  75. struct dax_device *dax_dev;
  76. u64 part_size;
  77. int id;
  78. if (!blk_queue_dax(bdev->bd_disk->queue))
  79. return NULL;
  80. *start_off = get_start_sect(bdev) * SECTOR_SIZE;
  81. part_size = bdev_nr_sectors(bdev) * SECTOR_SIZE;
  82. if (*start_off % PAGE_SIZE || part_size % PAGE_SIZE) {
  83. pr_info("%pg: error: unaligned partition for dax\n", bdev);
  84. return NULL;
  85. }
  86. id = dax_read_lock();
  87. dax_dev = xa_load(&dax_hosts, (unsigned long)bdev->bd_disk);
  88. if (!dax_dev || !dax_alive(dax_dev) || !igrab(&dax_dev->inode))
  89. dax_dev = NULL;
  90. else if (holder) {
  91. if (!cmpxchg(&dax_dev->holder_data, NULL, holder))
  92. dax_dev->holder_ops = ops;
  93. else
  94. dax_dev = NULL;
  95. }
  96. dax_read_unlock(id);
  97. return dax_dev;
  98. }
  99. EXPORT_SYMBOL_GPL(fs_dax_get_by_bdev);
  100. void fs_put_dax(struct dax_device *dax_dev, void *holder)
  101. {
  102. if (dax_dev && holder &&
  103. cmpxchg(&dax_dev->holder_data, holder, NULL) == holder)
  104. dax_dev->holder_ops = NULL;
  105. put_dax(dax_dev);
  106. }
  107. EXPORT_SYMBOL_GPL(fs_put_dax);
  108. #endif /* CONFIG_BLOCK && CONFIG_FS_DAX */
  109. enum dax_device_flags {
  110. /* !alive + rcu grace period == no new operations / mappings */
  111. DAXDEV_ALIVE,
  112. /* gate whether dax_flush() calls the low level flush routine */
  113. DAXDEV_WRITE_CACHE,
  114. /* flag to check if device supports synchronous flush */
  115. DAXDEV_SYNC,
  116. /* do not leave the caches dirty after writes */
  117. DAXDEV_NOCACHE,
  118. /* handle CPU fetch exceptions during reads */
  119. DAXDEV_NOMC,
  120. };
  121. /**
  122. * dax_direct_access() - translate a device pgoff to an absolute pfn
  123. * @dax_dev: a dax_device instance representing the logical memory range
  124. * @pgoff: offset in pages from the start of the device to translate
  125. * @nr_pages: number of consecutive pages caller can handle relative to @pfn
  126. * @mode: indicator on normal access or recovery write
  127. * @kaddr: output parameter that returns a virtual address mapping of pfn
  128. * @pfn: output parameter that returns an absolute pfn translation of @pgoff
  129. *
  130. * Return: negative errno if an error occurs, otherwise the number of
  131. * pages accessible at the device relative @pgoff.
  132. */
  133. long dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff, long nr_pages,
  134. enum dax_access_mode mode, void **kaddr, pfn_t *pfn)
  135. {
  136. long avail;
  137. if (!dax_dev)
  138. return -EOPNOTSUPP;
  139. if (!dax_alive(dax_dev))
  140. return -ENXIO;
  141. if (nr_pages < 0)
  142. return -EINVAL;
  143. avail = dax_dev->ops->direct_access(dax_dev, pgoff, nr_pages,
  144. mode, kaddr, pfn);
  145. if (!avail)
  146. return -ERANGE;
  147. return min(avail, nr_pages);
  148. }
  149. EXPORT_SYMBOL_GPL(dax_direct_access);
  150. size_t dax_copy_from_iter(struct dax_device *dax_dev, pgoff_t pgoff, void *addr,
  151. size_t bytes, struct iov_iter *i)
  152. {
  153. if (!dax_alive(dax_dev))
  154. return 0;
  155. /*
  156. * The userspace address for the memory copy has already been validated
  157. * via access_ok() in vfs_write, so use the 'no check' version to bypass
  158. * the HARDENED_USERCOPY overhead.
  159. */
  160. if (test_bit(DAXDEV_NOCACHE, &dax_dev->flags))
  161. return _copy_from_iter_flushcache(addr, bytes, i);
  162. return _copy_from_iter(addr, bytes, i);
  163. }
  164. size_t dax_copy_to_iter(struct dax_device *dax_dev, pgoff_t pgoff, void *addr,
  165. size_t bytes, struct iov_iter *i)
  166. {
  167. if (!dax_alive(dax_dev))
  168. return 0;
  169. /*
  170. * The userspace address for the memory copy has already been validated
  171. * via access_ok() in vfs_red, so use the 'no check' version to bypass
  172. * the HARDENED_USERCOPY overhead.
  173. */
  174. if (test_bit(DAXDEV_NOMC, &dax_dev->flags))
  175. return _copy_mc_to_iter(addr, bytes, i);
  176. return _copy_to_iter(addr, bytes, i);
  177. }
  178. int dax_zero_page_range(struct dax_device *dax_dev, pgoff_t pgoff,
  179. size_t nr_pages)
  180. {
  181. if (!dax_alive(dax_dev))
  182. return -ENXIO;
  183. /*
  184. * There are no callers that want to zero more than one page as of now.
  185. * Once users are there, this check can be removed after the
  186. * device mapper code has been updated to split ranges across targets.
  187. */
  188. if (nr_pages != 1)
  189. return -EIO;
  190. return dax_dev->ops->zero_page_range(dax_dev, pgoff, nr_pages);
  191. }
  192. EXPORT_SYMBOL_GPL(dax_zero_page_range);
  193. size_t dax_recovery_write(struct dax_device *dax_dev, pgoff_t pgoff,
  194. void *addr, size_t bytes, struct iov_iter *iter)
  195. {
  196. if (!dax_dev->ops->recovery_write)
  197. return 0;
  198. return dax_dev->ops->recovery_write(dax_dev, pgoff, addr, bytes, iter);
  199. }
  200. EXPORT_SYMBOL_GPL(dax_recovery_write);
  201. int dax_holder_notify_failure(struct dax_device *dax_dev, u64 off,
  202. u64 len, int mf_flags)
  203. {
  204. int rc, id;
  205. id = dax_read_lock();
  206. if (!dax_alive(dax_dev)) {
  207. rc = -ENXIO;
  208. goto out;
  209. }
  210. if (!dax_dev->holder_ops) {
  211. rc = -EOPNOTSUPP;
  212. goto out;
  213. }
  214. rc = dax_dev->holder_ops->notify_failure(dax_dev, off, len, mf_flags);
  215. out:
  216. dax_read_unlock(id);
  217. return rc;
  218. }
  219. EXPORT_SYMBOL_GPL(dax_holder_notify_failure);
  220. #ifdef CONFIG_ARCH_HAS_PMEM_API
  221. void arch_wb_cache_pmem(void *addr, size_t size);
  222. void dax_flush(struct dax_device *dax_dev, void *addr, size_t size)
  223. {
  224. if (unlikely(!dax_write_cache_enabled(dax_dev)))
  225. return;
  226. arch_wb_cache_pmem(addr, size);
  227. }
  228. #else
  229. void dax_flush(struct dax_device *dax_dev, void *addr, size_t size)
  230. {
  231. }
  232. #endif
  233. EXPORT_SYMBOL_GPL(dax_flush);
  234. void dax_write_cache(struct dax_device *dax_dev, bool wc)
  235. {
  236. if (wc)
  237. set_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags);
  238. else
  239. clear_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags);
  240. }
  241. EXPORT_SYMBOL_GPL(dax_write_cache);
  242. bool dax_write_cache_enabled(struct dax_device *dax_dev)
  243. {
  244. return test_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags);
  245. }
  246. EXPORT_SYMBOL_GPL(dax_write_cache_enabled);
  247. bool dax_synchronous(struct dax_device *dax_dev)
  248. {
  249. return test_bit(DAXDEV_SYNC, &dax_dev->flags);
  250. }
  251. EXPORT_SYMBOL_GPL(dax_synchronous);
  252. void set_dax_synchronous(struct dax_device *dax_dev)
  253. {
  254. set_bit(DAXDEV_SYNC, &dax_dev->flags);
  255. }
  256. EXPORT_SYMBOL_GPL(set_dax_synchronous);
  257. void set_dax_nocache(struct dax_device *dax_dev)
  258. {
  259. set_bit(DAXDEV_NOCACHE, &dax_dev->flags);
  260. }
  261. EXPORT_SYMBOL_GPL(set_dax_nocache);
  262. void set_dax_nomc(struct dax_device *dax_dev)
  263. {
  264. set_bit(DAXDEV_NOMC, &dax_dev->flags);
  265. }
  266. EXPORT_SYMBOL_GPL(set_dax_nomc);
  267. bool dax_alive(struct dax_device *dax_dev)
  268. {
  269. lockdep_assert_held(&dax_srcu);
  270. return test_bit(DAXDEV_ALIVE, &dax_dev->flags);
  271. }
  272. EXPORT_SYMBOL_GPL(dax_alive);
  273. /*
  274. * Note, rcu is not protecting the liveness of dax_dev, rcu is ensuring
  275. * that any fault handlers or operations that might have seen
  276. * dax_alive(), have completed. Any operations that start after
  277. * synchronize_srcu() has run will abort upon seeing !dax_alive().
  278. */
  279. void kill_dax(struct dax_device *dax_dev)
  280. {
  281. if (!dax_dev)
  282. return;
  283. if (dax_dev->holder_data != NULL)
  284. dax_holder_notify_failure(dax_dev, 0, U64_MAX, 0);
  285. clear_bit(DAXDEV_ALIVE, &dax_dev->flags);
  286. synchronize_srcu(&dax_srcu);
  287. /* clear holder data */
  288. dax_dev->holder_ops = NULL;
  289. dax_dev->holder_data = NULL;
  290. }
  291. EXPORT_SYMBOL_GPL(kill_dax);
  292. void run_dax(struct dax_device *dax_dev)
  293. {
  294. set_bit(DAXDEV_ALIVE, &dax_dev->flags);
  295. }
  296. EXPORT_SYMBOL_GPL(run_dax);
  297. static struct inode *dax_alloc_inode(struct super_block *sb)
  298. {
  299. struct dax_device *dax_dev;
  300. struct inode *inode;
  301. dax_dev = alloc_inode_sb(sb, dax_cache, GFP_KERNEL);
  302. if (!dax_dev)
  303. return NULL;
  304. inode = &dax_dev->inode;
  305. inode->i_rdev = 0;
  306. return inode;
  307. }
  308. static struct dax_device *to_dax_dev(struct inode *inode)
  309. {
  310. return container_of(inode, struct dax_device, inode);
  311. }
  312. static void dax_free_inode(struct inode *inode)
  313. {
  314. struct dax_device *dax_dev = to_dax_dev(inode);
  315. if (inode->i_rdev)
  316. ida_free(&dax_minor_ida, iminor(inode));
  317. kmem_cache_free(dax_cache, dax_dev);
  318. }
  319. static void dax_destroy_inode(struct inode *inode)
  320. {
  321. struct dax_device *dax_dev = to_dax_dev(inode);
  322. WARN_ONCE(test_bit(DAXDEV_ALIVE, &dax_dev->flags),
  323. "kill_dax() must be called before final iput()\n");
  324. }
  325. static const struct super_operations dax_sops = {
  326. .statfs = simple_statfs,
  327. .alloc_inode = dax_alloc_inode,
  328. .destroy_inode = dax_destroy_inode,
  329. .free_inode = dax_free_inode,
  330. .drop_inode = generic_delete_inode,
  331. };
  332. static int dax_init_fs_context(struct fs_context *fc)
  333. {
  334. struct pseudo_fs_context *ctx = init_pseudo(fc, DAXFS_MAGIC);
  335. if (!ctx)
  336. return -ENOMEM;
  337. ctx->ops = &dax_sops;
  338. return 0;
  339. }
  340. static struct file_system_type dax_fs_type = {
  341. .name = "dax",
  342. .init_fs_context = dax_init_fs_context,
  343. .kill_sb = kill_anon_super,
  344. };
  345. static int dax_test(struct inode *inode, void *data)
  346. {
  347. dev_t devt = *(dev_t *) data;
  348. return inode->i_rdev == devt;
  349. }
  350. static int dax_set(struct inode *inode, void *data)
  351. {
  352. dev_t devt = *(dev_t *) data;
  353. inode->i_rdev = devt;
  354. return 0;
  355. }
  356. static struct dax_device *dax_dev_get(dev_t devt)
  357. {
  358. struct dax_device *dax_dev;
  359. struct inode *inode;
  360. inode = iget5_locked(dax_superblock, hash_32(devt + DAXFS_MAGIC, 31),
  361. dax_test, dax_set, &devt);
  362. if (!inode)
  363. return NULL;
  364. dax_dev = to_dax_dev(inode);
  365. if (inode->i_state & I_NEW) {
  366. set_bit(DAXDEV_ALIVE, &dax_dev->flags);
  367. inode->i_cdev = &dax_dev->cdev;
  368. inode->i_mode = S_IFCHR;
  369. inode->i_flags = S_DAX;
  370. mapping_set_gfp_mask(&inode->i_data, GFP_USER);
  371. unlock_new_inode(inode);
  372. }
  373. return dax_dev;
  374. }
  375. struct dax_device *alloc_dax(void *private, const struct dax_operations *ops)
  376. {
  377. struct dax_device *dax_dev;
  378. dev_t devt;
  379. int minor;
  380. if (WARN_ON_ONCE(ops && !ops->zero_page_range))
  381. return ERR_PTR(-EINVAL);
  382. minor = ida_alloc_max(&dax_minor_ida, MINORMASK, GFP_KERNEL);
  383. if (minor < 0)
  384. return ERR_PTR(-ENOMEM);
  385. devt = MKDEV(MAJOR(dax_devt), minor);
  386. dax_dev = dax_dev_get(devt);
  387. if (!dax_dev)
  388. goto err_dev;
  389. dax_dev->ops = ops;
  390. dax_dev->private = private;
  391. return dax_dev;
  392. err_dev:
  393. ida_free(&dax_minor_ida, minor);
  394. return ERR_PTR(-ENOMEM);
  395. }
  396. EXPORT_SYMBOL_GPL(alloc_dax);
  397. void put_dax(struct dax_device *dax_dev)
  398. {
  399. if (!dax_dev)
  400. return;
  401. iput(&dax_dev->inode);
  402. }
  403. EXPORT_SYMBOL_GPL(put_dax);
  404. /**
  405. * dax_holder() - obtain the holder of a dax device
  406. * @dax_dev: a dax_device instance
  407. * Return: the holder's data which represents the holder if registered,
  408. * otherwize NULL.
  409. */
  410. void *dax_holder(struct dax_device *dax_dev)
  411. {
  412. return dax_dev->holder_data;
  413. }
  414. EXPORT_SYMBOL_GPL(dax_holder);
  415. /**
  416. * inode_dax: convert a public inode into its dax_dev
  417. * @inode: An inode with i_cdev pointing to a dax_dev
  418. *
  419. * Note this is not equivalent to to_dax_dev() which is for private
  420. * internal use where we know the inode filesystem type == dax_fs_type.
  421. */
  422. struct dax_device *inode_dax(struct inode *inode)
  423. {
  424. struct cdev *cdev = inode->i_cdev;
  425. return container_of(cdev, struct dax_device, cdev);
  426. }
  427. EXPORT_SYMBOL_GPL(inode_dax);
  428. struct inode *dax_inode(struct dax_device *dax_dev)
  429. {
  430. return &dax_dev->inode;
  431. }
  432. EXPORT_SYMBOL_GPL(dax_inode);
  433. void *dax_get_private(struct dax_device *dax_dev)
  434. {
  435. if (!test_bit(DAXDEV_ALIVE, &dax_dev->flags))
  436. return NULL;
  437. return dax_dev->private;
  438. }
  439. EXPORT_SYMBOL_GPL(dax_get_private);
  440. static void init_once(void *_dax_dev)
  441. {
  442. struct dax_device *dax_dev = _dax_dev;
  443. struct inode *inode = &dax_dev->inode;
  444. memset(dax_dev, 0, sizeof(*dax_dev));
  445. inode_init_once(inode);
  446. }
  447. static int dax_fs_init(void)
  448. {
  449. int rc;
  450. dax_cache = kmem_cache_create("dax_cache", sizeof(struct dax_device), 0,
  451. (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
  452. SLAB_MEM_SPREAD|SLAB_ACCOUNT),
  453. init_once);
  454. if (!dax_cache)
  455. return -ENOMEM;
  456. dax_mnt = kern_mount(&dax_fs_type);
  457. if (IS_ERR(dax_mnt)) {
  458. rc = PTR_ERR(dax_mnt);
  459. goto err_mount;
  460. }
  461. dax_superblock = dax_mnt->mnt_sb;
  462. return 0;
  463. err_mount:
  464. kmem_cache_destroy(dax_cache);
  465. return rc;
  466. }
  467. static void dax_fs_exit(void)
  468. {
  469. kern_unmount(dax_mnt);
  470. rcu_barrier();
  471. kmem_cache_destroy(dax_cache);
  472. }
  473. static int __init dax_core_init(void)
  474. {
  475. int rc;
  476. rc = dax_fs_init();
  477. if (rc)
  478. return rc;
  479. rc = alloc_chrdev_region(&dax_devt, 0, MINORMASK+1, "dax");
  480. if (rc)
  481. goto err_chrdev;
  482. rc = dax_bus_init();
  483. if (rc)
  484. goto err_bus;
  485. return 0;
  486. err_bus:
  487. unregister_chrdev_region(dax_devt, MINORMASK+1);
  488. err_chrdev:
  489. dax_fs_exit();
  490. return 0;
  491. }
  492. static void __exit dax_core_exit(void)
  493. {
  494. dax_bus_exit();
  495. unregister_chrdev_region(dax_devt, MINORMASK+1);
  496. ida_destroy(&dax_minor_ida);
  497. dax_fs_exit();
  498. }
  499. MODULE_AUTHOR("Intel Corporation");
  500. MODULE_LICENSE("GPL v2");
  501. subsys_initcall(dax_core_init);
  502. module_exit(dax_core_exit);