super.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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. int bdev_dax_pgoff(struct block_device *bdev, sector_t sector, size_t size,
  45. pgoff_t *pgoff)
  46. {
  47. phys_addr_t phys_off = (get_start_sect(bdev) + sector) * 512;
  48. if (pgoff)
  49. *pgoff = PHYS_PFN(phys_off);
  50. if (phys_off % PAGE_SIZE || size % PAGE_SIZE)
  51. return -EINVAL;
  52. return 0;
  53. }
  54. EXPORT_SYMBOL(bdev_dax_pgoff);
  55. /**
  56. * __bdev_dax_supported() - Check if the device supports dax for filesystem
  57. * @sb: The superblock of the device
  58. * @blocksize: The block size of the device
  59. *
  60. * This is a library function for filesystems to check if the block device
  61. * can be mounted with dax option.
  62. *
  63. * Return: negative errno if unsupported, 0 if supported.
  64. */
  65. int __bdev_dax_supported(struct super_block *sb, int blocksize)
  66. {
  67. struct block_device *bdev = sb->s_bdev;
  68. struct dax_device *dax_dev;
  69. pgoff_t pgoff;
  70. int err, id;
  71. void *kaddr;
  72. pfn_t pfn;
  73. long len;
  74. if (blocksize != PAGE_SIZE) {
  75. pr_err("VFS (%s): error: unsupported blocksize for dax\n",
  76. sb->s_id);
  77. return -EINVAL;
  78. }
  79. err = bdev_dax_pgoff(bdev, 0, PAGE_SIZE, &pgoff);
  80. if (err) {
  81. pr_err("VFS (%s): error: unaligned partition for dax\n",
  82. sb->s_id);
  83. return err;
  84. }
  85. dax_dev = dax_get_by_host(bdev->bd_disk->disk_name);
  86. if (!dax_dev) {
  87. pr_err("VFS (%s): error: device does not support dax\n",
  88. sb->s_id);
  89. return -EOPNOTSUPP;
  90. }
  91. id = dax_read_lock();
  92. len = dax_direct_access(dax_dev, pgoff, 1, &kaddr, &pfn);
  93. dax_read_unlock(id);
  94. put_dax(dax_dev);
  95. if (len < 1) {
  96. pr_err("VFS (%s): error: dax access failed (%ld)",
  97. sb->s_id, len);
  98. return len < 0 ? len : -EIO;
  99. }
  100. return 0;
  101. }
  102. EXPORT_SYMBOL_GPL(__bdev_dax_supported);
  103. #endif
  104. /**
  105. * struct dax_device - anchor object for dax services
  106. * @inode: core vfs
  107. * @cdev: optional character interface for "device dax"
  108. * @host: optional name for lookups where the device path is not available
  109. * @private: dax driver private data
  110. * @alive: !alive + rcu grace period == no new operations / mappings
  111. */
  112. struct dax_device {
  113. struct hlist_node list;
  114. struct inode inode;
  115. struct cdev cdev;
  116. const char *host;
  117. void *private;
  118. bool alive;
  119. const struct dax_operations *ops;
  120. };
  121. /**
  122. * dax_direct_access() - translate a device pgoff to an absolute pfn
  123. * @dax_dev: a dax_device instance representing the logical memory range
  124. * @pgoff: offset in pages from the start of the device to translate
  125. * @nr_pages: number of consecutive pages caller can handle relative to @pfn
  126. * @kaddr: output parameter that returns a virtual address mapping of pfn
  127. * @pfn: output parameter that returns an absolute pfn translation of @pgoff
  128. *
  129. * Return: negative errno if an error occurs, otherwise the number of
  130. * pages accessible at the device relative @pgoff.
  131. */
  132. long dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff, long nr_pages,
  133. void **kaddr, pfn_t *pfn)
  134. {
  135. long avail;
  136. /*
  137. * The device driver is allowed to sleep, in order to make the
  138. * memory directly accessible.
  139. */
  140. might_sleep();
  141. if (!dax_dev)
  142. return -EOPNOTSUPP;
  143. if (!dax_alive(dax_dev))
  144. return -ENXIO;
  145. if (nr_pages < 0)
  146. return nr_pages;
  147. avail = dax_dev->ops->direct_access(dax_dev, pgoff, nr_pages,
  148. kaddr, pfn);
  149. if (!avail)
  150. return -ERANGE;
  151. return min(avail, nr_pages);
  152. }
  153. EXPORT_SYMBOL_GPL(dax_direct_access);
  154. size_t dax_copy_from_iter(struct dax_device *dax_dev, pgoff_t pgoff, void *addr,
  155. size_t bytes, struct iov_iter *i)
  156. {
  157. if (!dax_alive(dax_dev))
  158. return 0;
  159. if (!dax_dev->ops->copy_from_iter)
  160. return copy_from_iter(addr, bytes, i);
  161. return dax_dev->ops->copy_from_iter(dax_dev, pgoff, addr, bytes, i);
  162. }
  163. EXPORT_SYMBOL_GPL(dax_copy_from_iter);
  164. void dax_flush(struct dax_device *dax_dev, pgoff_t pgoff, void *addr,
  165. size_t size)
  166. {
  167. if (!dax_alive(dax_dev))
  168. return;
  169. if (dax_dev->ops->flush)
  170. dax_dev->ops->flush(dax_dev, pgoff, addr, size);
  171. }
  172. EXPORT_SYMBOL_GPL(dax_flush);
  173. bool dax_alive(struct dax_device *dax_dev)
  174. {
  175. lockdep_assert_held(&dax_srcu);
  176. return dax_dev->alive;
  177. }
  178. EXPORT_SYMBOL_GPL(dax_alive);
  179. static int dax_host_hash(const char *host)
  180. {
  181. return hashlen_hash(hashlen_string("DAX", host)) % DAX_HASH_SIZE;
  182. }
  183. /*
  184. * Note, rcu is not protecting the liveness of dax_dev, rcu is ensuring
  185. * that any fault handlers or operations that might have seen
  186. * dax_alive(), have completed. Any operations that start after
  187. * synchronize_srcu() has run will abort upon seeing !dax_alive().
  188. */
  189. void kill_dax(struct dax_device *dax_dev)
  190. {
  191. if (!dax_dev)
  192. return;
  193. dax_dev->alive = false;
  194. synchronize_srcu(&dax_srcu);
  195. spin_lock(&dax_host_lock);
  196. hlist_del_init(&dax_dev->list);
  197. spin_unlock(&dax_host_lock);
  198. dax_dev->private = NULL;
  199. }
  200. EXPORT_SYMBOL_GPL(kill_dax);
  201. static struct inode *dax_alloc_inode(struct super_block *sb)
  202. {
  203. struct dax_device *dax_dev;
  204. dax_dev = kmem_cache_alloc(dax_cache, GFP_KERNEL);
  205. return &dax_dev->inode;
  206. }
  207. static struct dax_device *to_dax_dev(struct inode *inode)
  208. {
  209. return container_of(inode, struct dax_device, inode);
  210. }
  211. static void dax_i_callback(struct rcu_head *head)
  212. {
  213. struct inode *inode = container_of(head, struct inode, i_rcu);
  214. struct dax_device *dax_dev = to_dax_dev(inode);
  215. kfree(dax_dev->host);
  216. dax_dev->host = NULL;
  217. ida_simple_remove(&dax_minor_ida, MINOR(inode->i_rdev));
  218. kmem_cache_free(dax_cache, dax_dev);
  219. }
  220. static void dax_destroy_inode(struct inode *inode)
  221. {
  222. struct dax_device *dax_dev = to_dax_dev(inode);
  223. WARN_ONCE(dax_dev->alive,
  224. "kill_dax() must be called before final iput()\n");
  225. call_rcu(&inode->i_rcu, dax_i_callback);
  226. }
  227. static const struct super_operations dax_sops = {
  228. .statfs = simple_statfs,
  229. .alloc_inode = dax_alloc_inode,
  230. .destroy_inode = dax_destroy_inode,
  231. .drop_inode = generic_delete_inode,
  232. };
  233. static struct dentry *dax_mount(struct file_system_type *fs_type,
  234. int flags, const char *dev_name, void *data)
  235. {
  236. return mount_pseudo(fs_type, "dax:", &dax_sops, NULL, DAXFS_MAGIC);
  237. }
  238. static struct file_system_type dax_fs_type = {
  239. .name = "dax",
  240. .mount = dax_mount,
  241. .kill_sb = kill_anon_super,
  242. };
  243. static int dax_test(struct inode *inode, void *data)
  244. {
  245. dev_t devt = *(dev_t *) data;
  246. return inode->i_rdev == devt;
  247. }
  248. static int dax_set(struct inode *inode, void *data)
  249. {
  250. dev_t devt = *(dev_t *) data;
  251. inode->i_rdev = devt;
  252. return 0;
  253. }
  254. static struct dax_device *dax_dev_get(dev_t devt)
  255. {
  256. struct dax_device *dax_dev;
  257. struct inode *inode;
  258. inode = iget5_locked(dax_superblock, hash_32(devt + DAXFS_MAGIC, 31),
  259. dax_test, dax_set, &devt);
  260. if (!inode)
  261. return NULL;
  262. dax_dev = to_dax_dev(inode);
  263. if (inode->i_state & I_NEW) {
  264. dax_dev->alive = true;
  265. inode->i_cdev = &dax_dev->cdev;
  266. inode->i_mode = S_IFCHR;
  267. inode->i_flags = S_DAX;
  268. mapping_set_gfp_mask(&inode->i_data, GFP_USER);
  269. unlock_new_inode(inode);
  270. }
  271. return dax_dev;
  272. }
  273. static void dax_add_host(struct dax_device *dax_dev, const char *host)
  274. {
  275. int hash;
  276. /*
  277. * Unconditionally init dax_dev since it's coming from a
  278. * non-zeroed slab cache
  279. */
  280. INIT_HLIST_NODE(&dax_dev->list);
  281. dax_dev->host = host;
  282. if (!host)
  283. return;
  284. hash = dax_host_hash(host);
  285. spin_lock(&dax_host_lock);
  286. hlist_add_head(&dax_dev->list, &dax_host_list[hash]);
  287. spin_unlock(&dax_host_lock);
  288. }
  289. struct dax_device *alloc_dax(void *private, const char *__host,
  290. const struct dax_operations *ops)
  291. {
  292. struct dax_device *dax_dev;
  293. const char *host;
  294. dev_t devt;
  295. int minor;
  296. host = kstrdup(__host, GFP_KERNEL);
  297. if (__host && !host)
  298. return NULL;
  299. minor = ida_simple_get(&dax_minor_ida, 0, MINORMASK+1, GFP_KERNEL);
  300. if (minor < 0)
  301. goto err_minor;
  302. devt = MKDEV(MAJOR(dax_devt), minor);
  303. dax_dev = dax_dev_get(devt);
  304. if (!dax_dev)
  305. goto err_dev;
  306. dax_add_host(dax_dev, host);
  307. dax_dev->ops = ops;
  308. dax_dev->private = private;
  309. return dax_dev;
  310. err_dev:
  311. ida_simple_remove(&dax_minor_ida, minor);
  312. err_minor:
  313. kfree(host);
  314. return NULL;
  315. }
  316. EXPORT_SYMBOL_GPL(alloc_dax);
  317. void put_dax(struct dax_device *dax_dev)
  318. {
  319. if (!dax_dev)
  320. return;
  321. iput(&dax_dev->inode);
  322. }
  323. EXPORT_SYMBOL_GPL(put_dax);
  324. /**
  325. * dax_get_by_host() - temporary lookup mechanism for filesystem-dax
  326. * @host: alternate name for the device registered by a dax driver
  327. */
  328. struct dax_device *dax_get_by_host(const char *host)
  329. {
  330. struct dax_device *dax_dev, *found = NULL;
  331. int hash, id;
  332. if (!host)
  333. return NULL;
  334. hash = dax_host_hash(host);
  335. id = dax_read_lock();
  336. spin_lock(&dax_host_lock);
  337. hlist_for_each_entry(dax_dev, &dax_host_list[hash], list) {
  338. if (!dax_alive(dax_dev)
  339. || strcmp(host, dax_dev->host) != 0)
  340. continue;
  341. if (igrab(&dax_dev->inode))
  342. found = dax_dev;
  343. break;
  344. }
  345. spin_unlock(&dax_host_lock);
  346. dax_read_unlock(id);
  347. return found;
  348. }
  349. EXPORT_SYMBOL_GPL(dax_get_by_host);
  350. /**
  351. * inode_dax: convert a public inode into its dax_dev
  352. * @inode: An inode with i_cdev pointing to a dax_dev
  353. *
  354. * Note this is not equivalent to to_dax_dev() which is for private
  355. * internal use where we know the inode filesystem type == dax_fs_type.
  356. */
  357. struct dax_device *inode_dax(struct inode *inode)
  358. {
  359. struct cdev *cdev = inode->i_cdev;
  360. return container_of(cdev, struct dax_device, cdev);
  361. }
  362. EXPORT_SYMBOL_GPL(inode_dax);
  363. struct inode *dax_inode(struct dax_device *dax_dev)
  364. {
  365. return &dax_dev->inode;
  366. }
  367. EXPORT_SYMBOL_GPL(dax_inode);
  368. void *dax_get_private(struct dax_device *dax_dev)
  369. {
  370. return dax_dev->private;
  371. }
  372. EXPORT_SYMBOL_GPL(dax_get_private);
  373. static void init_once(void *_dax_dev)
  374. {
  375. struct dax_device *dax_dev = _dax_dev;
  376. struct inode *inode = &dax_dev->inode;
  377. inode_init_once(inode);
  378. }
  379. static int __dax_fs_init(void)
  380. {
  381. int rc;
  382. dax_cache = kmem_cache_create("dax_cache", sizeof(struct dax_device), 0,
  383. (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
  384. SLAB_MEM_SPREAD|SLAB_ACCOUNT),
  385. init_once);
  386. if (!dax_cache)
  387. return -ENOMEM;
  388. rc = register_filesystem(&dax_fs_type);
  389. if (rc)
  390. goto err_register_fs;
  391. dax_mnt = kern_mount(&dax_fs_type);
  392. if (IS_ERR(dax_mnt)) {
  393. rc = PTR_ERR(dax_mnt);
  394. goto err_mount;
  395. }
  396. dax_superblock = dax_mnt->mnt_sb;
  397. return 0;
  398. err_mount:
  399. unregister_filesystem(&dax_fs_type);
  400. err_register_fs:
  401. kmem_cache_destroy(dax_cache);
  402. return rc;
  403. }
  404. static void __dax_fs_exit(void)
  405. {
  406. kern_unmount(dax_mnt);
  407. unregister_filesystem(&dax_fs_type);
  408. kmem_cache_destroy(dax_cache);
  409. }
  410. static int __init dax_fs_init(void)
  411. {
  412. int rc;
  413. rc = __dax_fs_init();
  414. if (rc)
  415. return rc;
  416. rc = alloc_chrdev_region(&dax_devt, 0, MINORMASK+1, "dax");
  417. if (rc)
  418. __dax_fs_exit();
  419. return rc;
  420. }
  421. static void __exit dax_fs_exit(void)
  422. {
  423. unregister_chrdev_region(dax_devt, MINORMASK+1);
  424. ida_destroy(&dax_minor_ida);
  425. __dax_fs_exit();
  426. }
  427. MODULE_AUTHOR("Intel Corporation");
  428. MODULE_LICENSE("GPL v2");
  429. subsys_initcall(dax_fs_init);
  430. module_exit(dax_fs_exit);