super.c 15 KB

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