super.c 11 KB

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