super.c 14 KB

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