super.c 14 KB

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