super.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  1. /*
  2. * Copyright(c) 2017 Intel Corporation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of version 2 of the GNU General Public License as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. */
  13. #include <linux/pagemap.h>
  14. #include <linux/module.h>
  15. #include <linux/mount.h>
  16. #include <linux/magic.h>
  17. #include <linux/genhd.h>
  18. #include <linux/pfn_t.h>
  19. #include <linux/cdev.h>
  20. #include <linux/hash.h>
  21. #include <linux/slab.h>
  22. #include <linux/uio.h>
  23. #include <linux/dax.h>
  24. #include <linux/fs.h>
  25. static dev_t dax_devt;
  26. DEFINE_STATIC_SRCU(dax_srcu);
  27. static struct vfsmount *dax_mnt;
  28. static DEFINE_IDA(dax_minor_ida);
  29. static struct kmem_cache *dax_cache __read_mostly;
  30. static struct super_block *dax_superblock __read_mostly;
  31. #define DAX_HASH_SIZE (PAGE_SIZE / sizeof(struct hlist_head))
  32. static struct hlist_head dax_host_list[DAX_HASH_SIZE];
  33. static DEFINE_SPINLOCK(dax_host_lock);
  34. int dax_read_lock(void)
  35. {
  36. return srcu_read_lock(&dax_srcu);
  37. }
  38. EXPORT_SYMBOL_GPL(dax_read_lock);
  39. void dax_read_unlock(int id)
  40. {
  41. srcu_read_unlock(&dax_srcu, id);
  42. }
  43. EXPORT_SYMBOL_GPL(dax_read_unlock);
  44. #ifdef CONFIG_BLOCK
  45. #include <linux/blkdev.h>
  46. int bdev_dax_pgoff(struct block_device *bdev, sector_t sector, size_t size,
  47. pgoff_t *pgoff)
  48. {
  49. phys_addr_t phys_off = (get_start_sect(bdev) + sector) * 512;
  50. if (pgoff)
  51. *pgoff = PHYS_PFN(phys_off);
  52. if (phys_off % PAGE_SIZE || size % PAGE_SIZE)
  53. return -EINVAL;
  54. return 0;
  55. }
  56. EXPORT_SYMBOL(bdev_dax_pgoff);
  57. #if IS_ENABLED(CONFIG_FS_DAX)
  58. struct dax_device *fs_dax_get_by_bdev(struct block_device *bdev)
  59. {
  60. if (!blk_queue_dax(bdev->bd_queue))
  61. return NULL;
  62. return fs_dax_get_by_host(bdev->bd_disk->disk_name);
  63. }
  64. EXPORT_SYMBOL_GPL(fs_dax_get_by_bdev);
  65. #endif
  66. /**
  67. * __bdev_dax_supported() - Check if the device supports dax for filesystem
  68. * @sb: The superblock of the device
  69. * @blocksize: The block size of the device
  70. *
  71. * This is a library function for filesystems to check if the block device
  72. * can be mounted with dax option.
  73. *
  74. * Return: negative errno if unsupported, 0 if supported.
  75. */
  76. int __bdev_dax_supported(struct super_block *sb, int blocksize)
  77. {
  78. struct block_device *bdev = sb->s_bdev;
  79. struct dax_device *dax_dev;
  80. pgoff_t pgoff;
  81. int err, id;
  82. void *kaddr;
  83. pfn_t pfn;
  84. long len;
  85. if (blocksize != PAGE_SIZE) {
  86. pr_debug("VFS (%s): error: unsupported blocksize for dax\n",
  87. sb->s_id);
  88. return -EINVAL;
  89. }
  90. err = bdev_dax_pgoff(bdev, 0, PAGE_SIZE, &pgoff);
  91. if (err) {
  92. pr_debug("VFS (%s): error: unaligned partition for dax\n",
  93. sb->s_id);
  94. return err;
  95. }
  96. dax_dev = dax_get_by_host(bdev->bd_disk->disk_name);
  97. if (!dax_dev) {
  98. pr_debug("VFS (%s): error: device does not support dax\n",
  99. sb->s_id);
  100. return -EOPNOTSUPP;
  101. }
  102. id = dax_read_lock();
  103. len = dax_direct_access(dax_dev, pgoff, 1, &kaddr, &pfn);
  104. dax_read_unlock(id);
  105. put_dax(dax_dev);
  106. if (len < 1) {
  107. pr_debug("VFS (%s): error: dax access failed (%ld)\n",
  108. sb->s_id, len);
  109. return len < 0 ? len : -EIO;
  110. }
  111. if (IS_ENABLED(CONFIG_FS_DAX_LIMITED) && pfn_t_special(pfn)) {
  112. /*
  113. * An arch that has enabled the pmem api should also
  114. * have its drivers support pfn_t_devmap()
  115. *
  116. * This is a developer warning and should not trigger in
  117. * production. dax_flush() will crash since it depends
  118. * on being able to do (page_address(pfn_to_page())).
  119. */
  120. WARN_ON(IS_ENABLED(CONFIG_ARCH_HAS_PMEM_API));
  121. } else if (pfn_t_devmap(pfn)) {
  122. /* pass */;
  123. } else {
  124. pr_debug("VFS (%s): error: dax support not enabled\n",
  125. sb->s_id);
  126. return -EOPNOTSUPP;
  127. }
  128. return 0;
  129. }
  130. EXPORT_SYMBOL_GPL(__bdev_dax_supported);
  131. #endif
  132. enum dax_device_flags {
  133. /* !alive + rcu grace period == no new operations / mappings */
  134. DAXDEV_ALIVE,
  135. /* gate whether dax_flush() calls the low level flush routine */
  136. DAXDEV_WRITE_CACHE,
  137. };
  138. /**
  139. * struct dax_device - anchor object for dax services
  140. * @inode: core vfs
  141. * @cdev: optional character interface for "device dax"
  142. * @host: optional name for lookups where the device path is not available
  143. * @private: dax driver private data
  144. * @flags: state and boolean properties
  145. */
  146. struct dax_device {
  147. struct hlist_node list;
  148. struct inode inode;
  149. struct cdev cdev;
  150. const char *host;
  151. void *private;
  152. unsigned long flags;
  153. const struct dax_operations *ops;
  154. };
  155. static ssize_t write_cache_show(struct device *dev,
  156. struct device_attribute *attr, char *buf)
  157. {
  158. struct dax_device *dax_dev = dax_get_by_host(dev_name(dev));
  159. ssize_t rc;
  160. WARN_ON_ONCE(!dax_dev);
  161. if (!dax_dev)
  162. return -ENXIO;
  163. rc = sprintf(buf, "%d\n", !!test_bit(DAXDEV_WRITE_CACHE,
  164. &dax_dev->flags));
  165. put_dax(dax_dev);
  166. return rc;
  167. }
  168. static ssize_t write_cache_store(struct device *dev,
  169. struct device_attribute *attr, const char *buf, size_t len)
  170. {
  171. bool write_cache;
  172. int rc = strtobool(buf, &write_cache);
  173. struct dax_device *dax_dev = dax_get_by_host(dev_name(dev));
  174. WARN_ON_ONCE(!dax_dev);
  175. if (!dax_dev)
  176. return -ENXIO;
  177. if (rc)
  178. len = rc;
  179. else if (write_cache)
  180. set_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags);
  181. else
  182. clear_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags);
  183. put_dax(dax_dev);
  184. return len;
  185. }
  186. static DEVICE_ATTR_RW(write_cache);
  187. static umode_t dax_visible(struct kobject *kobj, struct attribute *a, int n)
  188. {
  189. struct device *dev = container_of(kobj, typeof(*dev), kobj);
  190. struct dax_device *dax_dev = dax_get_by_host(dev_name(dev));
  191. WARN_ON_ONCE(!dax_dev);
  192. if (!dax_dev)
  193. return 0;
  194. #ifndef CONFIG_ARCH_HAS_PMEM_API
  195. if (a == &dev_attr_write_cache.attr)
  196. return 0;
  197. #endif
  198. return a->mode;
  199. }
  200. static struct attribute *dax_attributes[] = {
  201. &dev_attr_write_cache.attr,
  202. NULL,
  203. };
  204. struct attribute_group dax_attribute_group = {
  205. .name = "dax",
  206. .attrs = dax_attributes,
  207. .is_visible = dax_visible,
  208. };
  209. EXPORT_SYMBOL_GPL(dax_attribute_group);
  210. /**
  211. * dax_direct_access() - translate a device pgoff to an absolute pfn
  212. * @dax_dev: a dax_device instance representing the logical memory range
  213. * @pgoff: offset in pages from the start of the device to translate
  214. * @nr_pages: number of consecutive pages caller can handle relative to @pfn
  215. * @kaddr: output parameter that returns a virtual address mapping of pfn
  216. * @pfn: output parameter that returns an absolute pfn translation of @pgoff
  217. *
  218. * Return: negative errno if an error occurs, otherwise the number of
  219. * pages accessible at the device relative @pgoff.
  220. */
  221. long dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff, long nr_pages,
  222. void **kaddr, pfn_t *pfn)
  223. {
  224. long avail;
  225. if (!dax_dev)
  226. return -EOPNOTSUPP;
  227. if (!dax_alive(dax_dev))
  228. return -ENXIO;
  229. if (nr_pages < 0)
  230. return nr_pages;
  231. avail = dax_dev->ops->direct_access(dax_dev, pgoff, nr_pages,
  232. kaddr, pfn);
  233. if (!avail)
  234. return -ERANGE;
  235. return min(avail, nr_pages);
  236. }
  237. EXPORT_SYMBOL_GPL(dax_direct_access);
  238. size_t dax_copy_from_iter(struct dax_device *dax_dev, pgoff_t pgoff, void *addr,
  239. size_t bytes, struct iov_iter *i)
  240. {
  241. if (!dax_alive(dax_dev))
  242. return 0;
  243. return dax_dev->ops->copy_from_iter(dax_dev, pgoff, addr, bytes, i);
  244. }
  245. EXPORT_SYMBOL_GPL(dax_copy_from_iter);
  246. #ifdef CONFIG_ARCH_HAS_PMEM_API
  247. void arch_wb_cache_pmem(void *addr, size_t size);
  248. void dax_flush(struct dax_device *dax_dev, void *addr, size_t size)
  249. {
  250. if (unlikely(!test_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags)))
  251. return;
  252. arch_wb_cache_pmem(addr, size);
  253. }
  254. #else
  255. void dax_flush(struct dax_device *dax_dev, void *addr, size_t size)
  256. {
  257. }
  258. #endif
  259. EXPORT_SYMBOL_GPL(dax_flush);
  260. void dax_write_cache(struct dax_device *dax_dev, bool wc)
  261. {
  262. if (wc)
  263. set_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags);
  264. else
  265. clear_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags);
  266. }
  267. EXPORT_SYMBOL_GPL(dax_write_cache);
  268. bool dax_write_cache_enabled(struct dax_device *dax_dev)
  269. {
  270. return test_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags);
  271. }
  272. EXPORT_SYMBOL_GPL(dax_write_cache_enabled);
  273. bool dax_alive(struct dax_device *dax_dev)
  274. {
  275. lockdep_assert_held(&dax_srcu);
  276. return test_bit(DAXDEV_ALIVE, &dax_dev->flags);
  277. }
  278. EXPORT_SYMBOL_GPL(dax_alive);
  279. static int dax_host_hash(const char *host)
  280. {
  281. return hashlen_hash(hashlen_string("DAX", host)) % DAX_HASH_SIZE;
  282. }
  283. /*
  284. * Note, rcu is not protecting the liveness of dax_dev, rcu is ensuring
  285. * that any fault handlers or operations that might have seen
  286. * dax_alive(), have completed. Any operations that start after
  287. * synchronize_srcu() has run will abort upon seeing !dax_alive().
  288. */
  289. void kill_dax(struct dax_device *dax_dev)
  290. {
  291. if (!dax_dev)
  292. return;
  293. clear_bit(DAXDEV_ALIVE, &dax_dev->flags);
  294. synchronize_srcu(&dax_srcu);
  295. spin_lock(&dax_host_lock);
  296. hlist_del_init(&dax_dev->list);
  297. spin_unlock(&dax_host_lock);
  298. dax_dev->private = NULL;
  299. }
  300. EXPORT_SYMBOL_GPL(kill_dax);
  301. static struct inode *dax_alloc_inode(struct super_block *sb)
  302. {
  303. struct dax_device *dax_dev;
  304. struct inode *inode;
  305. dax_dev = kmem_cache_alloc(dax_cache, GFP_KERNEL);
  306. if (!dax_dev)
  307. return NULL;
  308. inode = &dax_dev->inode;
  309. inode->i_rdev = 0;
  310. return inode;
  311. }
  312. static struct dax_device *to_dax_dev(struct inode *inode)
  313. {
  314. return container_of(inode, struct dax_device, inode);
  315. }
  316. static void dax_i_callback(struct rcu_head *head)
  317. {
  318. struct inode *inode = container_of(head, struct inode, i_rcu);
  319. struct dax_device *dax_dev = to_dax_dev(inode);
  320. kfree(dax_dev->host);
  321. dax_dev->host = NULL;
  322. if (inode->i_rdev)
  323. ida_simple_remove(&dax_minor_ida, MINOR(inode->i_rdev));
  324. kmem_cache_free(dax_cache, dax_dev);
  325. }
  326. static void dax_destroy_inode(struct inode *inode)
  327. {
  328. struct dax_device *dax_dev = to_dax_dev(inode);
  329. WARN_ONCE(test_bit(DAXDEV_ALIVE, &dax_dev->flags),
  330. "kill_dax() must be called before final iput()\n");
  331. call_rcu(&inode->i_rcu, dax_i_callback);
  332. }
  333. static const struct super_operations dax_sops = {
  334. .statfs = simple_statfs,
  335. .alloc_inode = dax_alloc_inode,
  336. .destroy_inode = dax_destroy_inode,
  337. .drop_inode = generic_delete_inode,
  338. };
  339. static struct dentry *dax_mount(struct file_system_type *fs_type,
  340. int flags, const char *dev_name, void *data)
  341. {
  342. return mount_pseudo(fs_type, "dax:", &dax_sops, NULL, DAXFS_MAGIC);
  343. }
  344. static struct file_system_type dax_fs_type = {
  345. .name = "dax",
  346. .mount = dax_mount,
  347. .kill_sb = kill_anon_super,
  348. };
  349. static int dax_test(struct inode *inode, void *data)
  350. {
  351. dev_t devt = *(dev_t *) data;
  352. return inode->i_rdev == devt;
  353. }
  354. static int dax_set(struct inode *inode, void *data)
  355. {
  356. dev_t devt = *(dev_t *) data;
  357. inode->i_rdev = devt;
  358. return 0;
  359. }
  360. static struct dax_device *dax_dev_get(dev_t devt)
  361. {
  362. struct dax_device *dax_dev;
  363. struct inode *inode;
  364. inode = iget5_locked(dax_superblock, hash_32(devt + DAXFS_MAGIC, 31),
  365. dax_test, dax_set, &devt);
  366. if (!inode)
  367. return NULL;
  368. dax_dev = to_dax_dev(inode);
  369. if (inode->i_state & I_NEW) {
  370. set_bit(DAXDEV_ALIVE, &dax_dev->flags);
  371. inode->i_cdev = &dax_dev->cdev;
  372. inode->i_mode = S_IFCHR;
  373. inode->i_flags = S_DAX;
  374. mapping_set_gfp_mask(&inode->i_data, GFP_USER);
  375. unlock_new_inode(inode);
  376. }
  377. return dax_dev;
  378. }
  379. static void dax_add_host(struct dax_device *dax_dev, const char *host)
  380. {
  381. int hash;
  382. /*
  383. * Unconditionally init dax_dev since it's coming from a
  384. * non-zeroed slab cache
  385. */
  386. INIT_HLIST_NODE(&dax_dev->list);
  387. dax_dev->host = host;
  388. if (!host)
  389. return;
  390. hash = dax_host_hash(host);
  391. spin_lock(&dax_host_lock);
  392. hlist_add_head(&dax_dev->list, &dax_host_list[hash]);
  393. spin_unlock(&dax_host_lock);
  394. }
  395. struct dax_device *alloc_dax(void *private, const char *__host,
  396. const struct dax_operations *ops)
  397. {
  398. struct dax_device *dax_dev;
  399. const char *host;
  400. dev_t devt;
  401. int minor;
  402. host = kstrdup(__host, GFP_KERNEL);
  403. if (__host && !host)
  404. return NULL;
  405. minor = ida_simple_get(&dax_minor_ida, 0, MINORMASK+1, GFP_KERNEL);
  406. if (minor < 0)
  407. goto err_minor;
  408. devt = MKDEV(MAJOR(dax_devt), minor);
  409. dax_dev = dax_dev_get(devt);
  410. if (!dax_dev)
  411. goto err_dev;
  412. dax_add_host(dax_dev, host);
  413. dax_dev->ops = ops;
  414. dax_dev->private = private;
  415. return dax_dev;
  416. err_dev:
  417. ida_simple_remove(&dax_minor_ida, minor);
  418. err_minor:
  419. kfree(host);
  420. return NULL;
  421. }
  422. EXPORT_SYMBOL_GPL(alloc_dax);
  423. void put_dax(struct dax_device *dax_dev)
  424. {
  425. if (!dax_dev)
  426. return;
  427. iput(&dax_dev->inode);
  428. }
  429. EXPORT_SYMBOL_GPL(put_dax);
  430. /**
  431. * dax_get_by_host() - temporary lookup mechanism for filesystem-dax
  432. * @host: alternate name for the device registered by a dax driver
  433. */
  434. struct dax_device *dax_get_by_host(const char *host)
  435. {
  436. struct dax_device *dax_dev, *found = NULL;
  437. int hash, id;
  438. if (!host)
  439. return NULL;
  440. hash = dax_host_hash(host);
  441. id = dax_read_lock();
  442. spin_lock(&dax_host_lock);
  443. hlist_for_each_entry(dax_dev, &dax_host_list[hash], list) {
  444. if (!dax_alive(dax_dev)
  445. || strcmp(host, dax_dev->host) != 0)
  446. continue;
  447. if (igrab(&dax_dev->inode))
  448. found = dax_dev;
  449. break;
  450. }
  451. spin_unlock(&dax_host_lock);
  452. dax_read_unlock(id);
  453. return found;
  454. }
  455. EXPORT_SYMBOL_GPL(dax_get_by_host);
  456. /**
  457. * inode_dax: convert a public inode into its dax_dev
  458. * @inode: An inode with i_cdev pointing to a dax_dev
  459. *
  460. * Note this is not equivalent to to_dax_dev() which is for private
  461. * internal use where we know the inode filesystem type == dax_fs_type.
  462. */
  463. struct dax_device *inode_dax(struct inode *inode)
  464. {
  465. struct cdev *cdev = inode->i_cdev;
  466. return container_of(cdev, struct dax_device, cdev);
  467. }
  468. EXPORT_SYMBOL_GPL(inode_dax);
  469. struct inode *dax_inode(struct dax_device *dax_dev)
  470. {
  471. return &dax_dev->inode;
  472. }
  473. EXPORT_SYMBOL_GPL(dax_inode);
  474. void *dax_get_private(struct dax_device *dax_dev)
  475. {
  476. return dax_dev->private;
  477. }
  478. EXPORT_SYMBOL_GPL(dax_get_private);
  479. static void init_once(void *_dax_dev)
  480. {
  481. struct dax_device *dax_dev = _dax_dev;
  482. struct inode *inode = &dax_dev->inode;
  483. memset(dax_dev, 0, sizeof(*dax_dev));
  484. inode_init_once(inode);
  485. }
  486. static int __dax_fs_init(void)
  487. {
  488. int rc;
  489. dax_cache = kmem_cache_create("dax_cache", sizeof(struct dax_device), 0,
  490. (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
  491. SLAB_MEM_SPREAD|SLAB_ACCOUNT),
  492. init_once);
  493. if (!dax_cache)
  494. return -ENOMEM;
  495. rc = register_filesystem(&dax_fs_type);
  496. if (rc)
  497. goto err_register_fs;
  498. dax_mnt = kern_mount(&dax_fs_type);
  499. if (IS_ERR(dax_mnt)) {
  500. rc = PTR_ERR(dax_mnt);
  501. goto err_mount;
  502. }
  503. dax_superblock = dax_mnt->mnt_sb;
  504. return 0;
  505. err_mount:
  506. unregister_filesystem(&dax_fs_type);
  507. err_register_fs:
  508. kmem_cache_destroy(dax_cache);
  509. return rc;
  510. }
  511. static void __dax_fs_exit(void)
  512. {
  513. kern_unmount(dax_mnt);
  514. unregister_filesystem(&dax_fs_type);
  515. kmem_cache_destroy(dax_cache);
  516. }
  517. static int __init dax_fs_init(void)
  518. {
  519. int rc;
  520. rc = __dax_fs_init();
  521. if (rc)
  522. return rc;
  523. rc = alloc_chrdev_region(&dax_devt, 0, MINORMASK+1, "dax");
  524. if (rc)
  525. __dax_fs_exit();
  526. return rc;
  527. }
  528. static void __exit dax_fs_exit(void)
  529. {
  530. unregister_chrdev_region(dax_devt, MINORMASK+1);
  531. ida_destroy(&dax_minor_ida);
  532. __dax_fs_exit();
  533. }
  534. MODULE_AUTHOR("Intel Corporation");
  535. MODULE_LICENSE("GPL v2");
  536. subsys_initcall(dax_fs_init);
  537. module_exit(dax_fs_exit);