super.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  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", !!dax_write_cache_enabled(dax_dev));
  164. put_dax(dax_dev);
  165. return rc;
  166. }
  167. static ssize_t write_cache_store(struct device *dev,
  168. struct device_attribute *attr, const char *buf, size_t len)
  169. {
  170. bool write_cache;
  171. int rc = strtobool(buf, &write_cache);
  172. struct dax_device *dax_dev = dax_get_by_host(dev_name(dev));
  173. WARN_ON_ONCE(!dax_dev);
  174. if (!dax_dev)
  175. return -ENXIO;
  176. if (rc)
  177. len = rc;
  178. else
  179. dax_write_cache(dax_dev, write_cache);
  180. put_dax(dax_dev);
  181. return len;
  182. }
  183. static DEVICE_ATTR_RW(write_cache);
  184. static umode_t dax_visible(struct kobject *kobj, struct attribute *a, int n)
  185. {
  186. struct device *dev = container_of(kobj, typeof(*dev), kobj);
  187. struct dax_device *dax_dev = dax_get_by_host(dev_name(dev));
  188. WARN_ON_ONCE(!dax_dev);
  189. if (!dax_dev)
  190. return 0;
  191. #ifndef CONFIG_ARCH_HAS_PMEM_API
  192. if (a == &dev_attr_write_cache.attr)
  193. return 0;
  194. #endif
  195. return a->mode;
  196. }
  197. static struct attribute *dax_attributes[] = {
  198. &dev_attr_write_cache.attr,
  199. NULL,
  200. };
  201. struct attribute_group dax_attribute_group = {
  202. .name = "dax",
  203. .attrs = dax_attributes,
  204. .is_visible = dax_visible,
  205. };
  206. EXPORT_SYMBOL_GPL(dax_attribute_group);
  207. /**
  208. * dax_direct_access() - translate a device pgoff to an absolute pfn
  209. * @dax_dev: a dax_device instance representing the logical memory range
  210. * @pgoff: offset in pages from the start of the device to translate
  211. * @nr_pages: number of consecutive pages caller can handle relative to @pfn
  212. * @kaddr: output parameter that returns a virtual address mapping of pfn
  213. * @pfn: output parameter that returns an absolute pfn translation of @pgoff
  214. *
  215. * Return: negative errno if an error occurs, otherwise the number of
  216. * pages accessible at the device relative @pgoff.
  217. */
  218. long dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff, long nr_pages,
  219. void **kaddr, pfn_t *pfn)
  220. {
  221. long avail;
  222. if (!dax_dev)
  223. return -EOPNOTSUPP;
  224. if (!dax_alive(dax_dev))
  225. return -ENXIO;
  226. if (nr_pages < 0)
  227. return nr_pages;
  228. avail = dax_dev->ops->direct_access(dax_dev, pgoff, nr_pages,
  229. kaddr, pfn);
  230. if (!avail)
  231. return -ERANGE;
  232. return min(avail, nr_pages);
  233. }
  234. EXPORT_SYMBOL_GPL(dax_direct_access);
  235. size_t dax_copy_from_iter(struct dax_device *dax_dev, pgoff_t pgoff, void *addr,
  236. size_t bytes, struct iov_iter *i)
  237. {
  238. if (!dax_alive(dax_dev))
  239. return 0;
  240. return dax_dev->ops->copy_from_iter(dax_dev, pgoff, addr, bytes, i);
  241. }
  242. EXPORT_SYMBOL_GPL(dax_copy_from_iter);
  243. #ifdef CONFIG_ARCH_HAS_PMEM_API
  244. void arch_wb_cache_pmem(void *addr, size_t size);
  245. void dax_flush(struct dax_device *dax_dev, void *addr, size_t size)
  246. {
  247. if (unlikely(!dax_write_cache_enabled(dax_dev)))
  248. return;
  249. arch_wb_cache_pmem(addr, size);
  250. }
  251. #else
  252. void dax_flush(struct dax_device *dax_dev, void *addr, size_t size)
  253. {
  254. }
  255. #endif
  256. EXPORT_SYMBOL_GPL(dax_flush);
  257. void dax_write_cache(struct dax_device *dax_dev, bool wc)
  258. {
  259. if (wc)
  260. set_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags);
  261. else
  262. clear_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags);
  263. }
  264. EXPORT_SYMBOL_GPL(dax_write_cache);
  265. bool dax_write_cache_enabled(struct dax_device *dax_dev)
  266. {
  267. return test_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags);
  268. }
  269. EXPORT_SYMBOL_GPL(dax_write_cache_enabled);
  270. bool dax_alive(struct dax_device *dax_dev)
  271. {
  272. lockdep_assert_held(&dax_srcu);
  273. return test_bit(DAXDEV_ALIVE, &dax_dev->flags);
  274. }
  275. EXPORT_SYMBOL_GPL(dax_alive);
  276. static int dax_host_hash(const char *host)
  277. {
  278. return hashlen_hash(hashlen_string("DAX", host)) % DAX_HASH_SIZE;
  279. }
  280. /*
  281. * Note, rcu is not protecting the liveness of dax_dev, rcu is ensuring
  282. * that any fault handlers or operations that might have seen
  283. * dax_alive(), have completed. Any operations that start after
  284. * synchronize_srcu() has run will abort upon seeing !dax_alive().
  285. */
  286. void kill_dax(struct dax_device *dax_dev)
  287. {
  288. if (!dax_dev)
  289. return;
  290. clear_bit(DAXDEV_ALIVE, &dax_dev->flags);
  291. synchronize_srcu(&dax_srcu);
  292. spin_lock(&dax_host_lock);
  293. hlist_del_init(&dax_dev->list);
  294. spin_unlock(&dax_host_lock);
  295. dax_dev->private = NULL;
  296. }
  297. EXPORT_SYMBOL_GPL(kill_dax);
  298. static struct inode *dax_alloc_inode(struct super_block *sb)
  299. {
  300. struct dax_device *dax_dev;
  301. struct inode *inode;
  302. dax_dev = kmem_cache_alloc(dax_cache, GFP_KERNEL);
  303. if (!dax_dev)
  304. return NULL;
  305. inode = &dax_dev->inode;
  306. inode->i_rdev = 0;
  307. return inode;
  308. }
  309. static struct dax_device *to_dax_dev(struct inode *inode)
  310. {
  311. return container_of(inode, struct dax_device, inode);
  312. }
  313. static void dax_i_callback(struct rcu_head *head)
  314. {
  315. struct inode *inode = container_of(head, struct inode, i_rcu);
  316. struct dax_device *dax_dev = to_dax_dev(inode);
  317. kfree(dax_dev->host);
  318. dax_dev->host = NULL;
  319. if (inode->i_rdev)
  320. ida_simple_remove(&dax_minor_ida, MINOR(inode->i_rdev));
  321. kmem_cache_free(dax_cache, dax_dev);
  322. }
  323. static void dax_destroy_inode(struct inode *inode)
  324. {
  325. struct dax_device *dax_dev = to_dax_dev(inode);
  326. WARN_ONCE(test_bit(DAXDEV_ALIVE, &dax_dev->flags),
  327. "kill_dax() must be called before final iput()\n");
  328. call_rcu(&inode->i_rcu, dax_i_callback);
  329. }
  330. static const struct super_operations dax_sops = {
  331. .statfs = simple_statfs,
  332. .alloc_inode = dax_alloc_inode,
  333. .destroy_inode = dax_destroy_inode,
  334. .drop_inode = generic_delete_inode,
  335. };
  336. static struct dentry *dax_mount(struct file_system_type *fs_type,
  337. int flags, const char *dev_name, void *data)
  338. {
  339. return mount_pseudo(fs_type, "dax:", &dax_sops, NULL, DAXFS_MAGIC);
  340. }
  341. static struct file_system_type dax_fs_type = {
  342. .name = "dax",
  343. .mount = dax_mount,
  344. .kill_sb = kill_anon_super,
  345. };
  346. static int dax_test(struct inode *inode, void *data)
  347. {
  348. dev_t devt = *(dev_t *) data;
  349. return inode->i_rdev == devt;
  350. }
  351. static int dax_set(struct inode *inode, void *data)
  352. {
  353. dev_t devt = *(dev_t *) data;
  354. inode->i_rdev = devt;
  355. return 0;
  356. }
  357. static struct dax_device *dax_dev_get(dev_t devt)
  358. {
  359. struct dax_device *dax_dev;
  360. struct inode *inode;
  361. inode = iget5_locked(dax_superblock, hash_32(devt + DAXFS_MAGIC, 31),
  362. dax_test, dax_set, &devt);
  363. if (!inode)
  364. return NULL;
  365. dax_dev = to_dax_dev(inode);
  366. if (inode->i_state & I_NEW) {
  367. set_bit(DAXDEV_ALIVE, &dax_dev->flags);
  368. inode->i_cdev = &dax_dev->cdev;
  369. inode->i_mode = S_IFCHR;
  370. inode->i_flags = S_DAX;
  371. mapping_set_gfp_mask(&inode->i_data, GFP_USER);
  372. unlock_new_inode(inode);
  373. }
  374. return dax_dev;
  375. }
  376. static void dax_add_host(struct dax_device *dax_dev, const char *host)
  377. {
  378. int hash;
  379. /*
  380. * Unconditionally init dax_dev since it's coming from a
  381. * non-zeroed slab cache
  382. */
  383. INIT_HLIST_NODE(&dax_dev->list);
  384. dax_dev->host = host;
  385. if (!host)
  386. return;
  387. hash = dax_host_hash(host);
  388. spin_lock(&dax_host_lock);
  389. hlist_add_head(&dax_dev->list, &dax_host_list[hash]);
  390. spin_unlock(&dax_host_lock);
  391. }
  392. struct dax_device *alloc_dax(void *private, const char *__host,
  393. const struct dax_operations *ops)
  394. {
  395. struct dax_device *dax_dev;
  396. const char *host;
  397. dev_t devt;
  398. int minor;
  399. host = kstrdup(__host, GFP_KERNEL);
  400. if (__host && !host)
  401. return NULL;
  402. minor = ida_simple_get(&dax_minor_ida, 0, MINORMASK+1, GFP_KERNEL);
  403. if (minor < 0)
  404. goto err_minor;
  405. devt = MKDEV(MAJOR(dax_devt), minor);
  406. dax_dev = dax_dev_get(devt);
  407. if (!dax_dev)
  408. goto err_dev;
  409. dax_add_host(dax_dev, host);
  410. dax_dev->ops = ops;
  411. dax_dev->private = private;
  412. return dax_dev;
  413. err_dev:
  414. ida_simple_remove(&dax_minor_ida, minor);
  415. err_minor:
  416. kfree(host);
  417. return NULL;
  418. }
  419. EXPORT_SYMBOL_GPL(alloc_dax);
  420. void put_dax(struct dax_device *dax_dev)
  421. {
  422. if (!dax_dev)
  423. return;
  424. iput(&dax_dev->inode);
  425. }
  426. EXPORT_SYMBOL_GPL(put_dax);
  427. /**
  428. * dax_get_by_host() - temporary lookup mechanism for filesystem-dax
  429. * @host: alternate name for the device registered by a dax driver
  430. */
  431. struct dax_device *dax_get_by_host(const char *host)
  432. {
  433. struct dax_device *dax_dev, *found = NULL;
  434. int hash, id;
  435. if (!host)
  436. return NULL;
  437. hash = dax_host_hash(host);
  438. id = dax_read_lock();
  439. spin_lock(&dax_host_lock);
  440. hlist_for_each_entry(dax_dev, &dax_host_list[hash], list) {
  441. if (!dax_alive(dax_dev)
  442. || strcmp(host, dax_dev->host) != 0)
  443. continue;
  444. if (igrab(&dax_dev->inode))
  445. found = dax_dev;
  446. break;
  447. }
  448. spin_unlock(&dax_host_lock);
  449. dax_read_unlock(id);
  450. return found;
  451. }
  452. EXPORT_SYMBOL_GPL(dax_get_by_host);
  453. /**
  454. * inode_dax: convert a public inode into its dax_dev
  455. * @inode: An inode with i_cdev pointing to a dax_dev
  456. *
  457. * Note this is not equivalent to to_dax_dev() which is for private
  458. * internal use where we know the inode filesystem type == dax_fs_type.
  459. */
  460. struct dax_device *inode_dax(struct inode *inode)
  461. {
  462. struct cdev *cdev = inode->i_cdev;
  463. return container_of(cdev, struct dax_device, cdev);
  464. }
  465. EXPORT_SYMBOL_GPL(inode_dax);
  466. struct inode *dax_inode(struct dax_device *dax_dev)
  467. {
  468. return &dax_dev->inode;
  469. }
  470. EXPORT_SYMBOL_GPL(dax_inode);
  471. void *dax_get_private(struct dax_device *dax_dev)
  472. {
  473. return dax_dev->private;
  474. }
  475. EXPORT_SYMBOL_GPL(dax_get_private);
  476. static void init_once(void *_dax_dev)
  477. {
  478. struct dax_device *dax_dev = _dax_dev;
  479. struct inode *inode = &dax_dev->inode;
  480. memset(dax_dev, 0, sizeof(*dax_dev));
  481. inode_init_once(inode);
  482. }
  483. static int __dax_fs_init(void)
  484. {
  485. int rc;
  486. dax_cache = kmem_cache_create("dax_cache", sizeof(struct dax_device), 0,
  487. (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
  488. SLAB_MEM_SPREAD|SLAB_ACCOUNT),
  489. init_once);
  490. if (!dax_cache)
  491. return -ENOMEM;
  492. rc = register_filesystem(&dax_fs_type);
  493. if (rc)
  494. goto err_register_fs;
  495. dax_mnt = kern_mount(&dax_fs_type);
  496. if (IS_ERR(dax_mnt)) {
  497. rc = PTR_ERR(dax_mnt);
  498. goto err_mount;
  499. }
  500. dax_superblock = dax_mnt->mnt_sb;
  501. return 0;
  502. err_mount:
  503. unregister_filesystem(&dax_fs_type);
  504. err_register_fs:
  505. kmem_cache_destroy(dax_cache);
  506. return rc;
  507. }
  508. static void __dax_fs_exit(void)
  509. {
  510. kern_unmount(dax_mnt);
  511. unregister_filesystem(&dax_fs_type);
  512. kmem_cache_destroy(dax_cache);
  513. }
  514. static int __init dax_fs_init(void)
  515. {
  516. int rc;
  517. rc = __dax_fs_init();
  518. if (rc)
  519. return rc;
  520. rc = alloc_chrdev_region(&dax_devt, 0, MINORMASK+1, "dax");
  521. if (rc)
  522. __dax_fs_exit();
  523. return rc;
  524. }
  525. static void __exit dax_fs_exit(void)
  526. {
  527. unregister_chrdev_region(dax_devt, MINORMASK+1);
  528. ida_destroy(&dax_minor_ida);
  529. __dax_fs_exit();
  530. }
  531. MODULE_AUTHOR("Intel Corporation");
  532. MODULE_LICENSE("GPL v2");
  533. subsys_initcall(dax_fs_init);
  534. module_exit(dax_fs_exit);