super.c 14 KB

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