blob: e3029389d8097ac818b3d0019afe88c0d9130b63 [file] [log] [blame]
Thomas Gleixner5b497af2019-05-29 07:18:09 -07001// SPDX-License-Identifier: GPL-2.0-only
Dan Williams7b6be842017-04-11 09:49:49 -07002/*
3 * Copyright(c) 2017 Intel Corporation. All rights reserved.
Dan Williams7b6be842017-04-11 09:49:49 -07004 */
5#include <linux/pagemap.h>
6#include <linux/module.h>
7#include <linux/mount.h>
David Howells75d4e062019-03-25 16:38:24 +00008#include <linux/pseudo_fs.h>
Dan Williams7b6be842017-04-11 09:49:49 -07009#include <linux/magic.h>
Dan Williams569d0362017-10-14 11:33:32 -070010#include <linux/pfn_t.h>
Dan Williams7b6be842017-04-11 09:49:49 -070011#include <linux/cdev.h>
Dan Williams7b6be842017-04-11 09:49:49 -070012#include <linux/slab.h>
Dan Williams7e026c82017-05-29 12:57:56 -070013#include <linux/uio.h>
Dan Williams6568b082017-01-24 18:44:18 -080014#include <linux/dax.h>
Dan Williams7b6be842017-04-11 09:49:49 -070015#include <linux/fs.h>
Dan Williams51cf7842017-07-12 17:58:21 -070016#include "dax-private.h"
Dan Williams7b6be842017-04-11 09:49:49 -070017
Christoph Hellwig1b764602021-08-26 15:55:05 +020018/**
19 * struct dax_device - anchor object for dax services
20 * @inode: core vfs
21 * @cdev: optional character interface for "device dax"
Christoph Hellwig1b764602021-08-26 15:55:05 +020022 * @private: dax driver private data
23 * @flags: state and boolean properties
24 */
25struct dax_device {
Christoph Hellwig1b764602021-08-26 15:55:05 +020026 struct inode inode;
27 struct cdev cdev;
Christoph Hellwig1b764602021-08-26 15:55:05 +020028 void *private;
29 unsigned long flags;
30 const struct dax_operations *ops;
31};
32
Dan Williams7b6be842017-04-11 09:49:49 -070033static dev_t dax_devt;
34DEFINE_STATIC_SRCU(dax_srcu);
35static struct vfsmount *dax_mnt;
36static DEFINE_IDA(dax_minor_ida);
37static struct kmem_cache *dax_cache __read_mostly;
38static struct super_block *dax_superblock __read_mostly;
39
40int dax_read_lock(void)
41{
42 return srcu_read_lock(&dax_srcu);
43}
44EXPORT_SYMBOL_GPL(dax_read_lock);
45
46void dax_read_unlock(int id)
47{
48 srcu_read_unlock(&dax_srcu, id);
49}
50EXPORT_SYMBOL_GPL(dax_read_unlock);
51
Christoph Hellwig5d2a2282021-11-29 11:21:36 +010052#if defined(CONFIG_BLOCK) && defined(CONFIG_FS_DAX)
Christoph Hellwige765f132021-09-22 19:34:30 +020053#include <linux/blkdev.h>
54
Christoph Hellwigfb08a192021-11-29 11:21:38 +010055static DEFINE_XARRAY(dax_hosts);
56
57int dax_add_host(struct dax_device *dax_dev, struct gendisk *disk)
58{
59 return xa_insert(&dax_hosts, (unsigned long)disk, dax_dev, GFP_KERNEL);
60}
61EXPORT_SYMBOL_GPL(dax_add_host);
62
63void dax_remove_host(struct gendisk *disk)
64{
65 xa_erase(&dax_hosts, (unsigned long)disk);
66}
67EXPORT_SYMBOL_GPL(dax_remove_host);
68
Christoph Hellwig1b764602021-08-26 15:55:05 +020069/**
Christoph Hellwigfb08a192021-11-29 11:21:38 +010070 * fs_dax_get_by_bdev() - temporary lookup mechanism for filesystem-dax
71 * @bdev: block device to find a dax_device for
Christoph Hellwigcd913c72021-11-29 11:21:59 +010072 * @start_off: returns the byte offset into the dax_device that @bdev starts
Christoph Hellwig1b764602021-08-26 15:55:05 +020073 */
Christoph Hellwigcd913c72021-11-29 11:21:59 +010074struct dax_device *fs_dax_get_by_bdev(struct block_device *bdev, u64 *start_off)
Dan Williams78f35472017-08-30 09:16:38 -070075{
Christoph Hellwigfb08a192021-11-29 11:21:38 +010076 struct dax_device *dax_dev;
Christoph Hellwigcd913c72021-11-29 11:21:59 +010077 u64 part_size;
Christoph Hellwigfb08a192021-11-29 11:21:38 +010078 int id;
79
Christoph Hellwige556f6b2020-06-26 10:01:56 +020080 if (!blk_queue_dax(bdev->bd_disk->queue))
Dan Williams78f35472017-08-30 09:16:38 -070081 return NULL;
Christoph Hellwigfb08a192021-11-29 11:21:38 +010082
Christoph Hellwigcd913c72021-11-29 11:21:59 +010083 *start_off = get_start_sect(bdev) * SECTOR_SIZE;
84 part_size = bdev_nr_sectors(bdev) * SECTOR_SIZE;
85 if (*start_off % PAGE_SIZE || part_size % PAGE_SIZE) {
Christoph Hellwig0c445872021-11-29 11:21:40 +010086 pr_info("%pg: error: unaligned partition for dax\n", bdev);
87 return NULL;
88 }
89
Christoph Hellwigfb08a192021-11-29 11:21:38 +010090 id = dax_read_lock();
91 dax_dev = xa_load(&dax_hosts, (unsigned long)bdev->bd_disk);
92 if (!dax_dev || !dax_alive(dax_dev) || !igrab(&dax_dev->inode))
93 dax_dev = NULL;
94 dax_read_unlock(id);
95
96 return dax_dev;
Dan Williams78f35472017-08-30 09:16:38 -070097}
98EXPORT_SYMBOL_GPL(fs_dax_get_by_bdev);
Christoph Hellwig5d2a2282021-11-29 11:21:36 +010099#endif /* CONFIG_BLOCK && CONFIG_FS_DAX */
Dan Williamsef510422017-05-08 10:55:27 -0700100
Dan Williams9a60c3e2017-06-27 17:59:28 -0700101enum dax_device_flags {
102 /* !alive + rcu grace period == no new operations / mappings */
103 DAXDEV_ALIVE,
Dan Williams6e0c90d2017-06-26 21:28:41 -0700104 /* gate whether dax_flush() calls the low level flush routine */
105 DAXDEV_WRITE_CACHE,
Pankaj Guptafefc1d92019-07-05 19:33:24 +0530106 /* flag to check if device supports synchronous flush */
107 DAXDEV_SYNC,
Christoph Hellwig7ac53602021-12-15 09:45:08 +0100108 /* do not leave the caches dirty after writes */
109 DAXDEV_NOCACHE,
110 /* handle CPU fetch exceptions during reads */
111 DAXDEV_NOMC,
Dan Williams9a60c3e2017-06-27 17:59:28 -0700112};
113
Dan Williamsb0686262017-01-26 20:37:35 -0800114/**
115 * dax_direct_access() - translate a device pgoff to an absolute pfn
116 * @dax_dev: a dax_device instance representing the logical memory range
117 * @pgoff: offset in pages from the start of the device to translate
118 * @nr_pages: number of consecutive pages caller can handle relative to @pfn
119 * @kaddr: output parameter that returns a virtual address mapping of pfn
120 * @pfn: output parameter that returns an absolute pfn translation of @pgoff
121 *
122 * Return: negative errno if an error occurs, otherwise the number of
123 * pages accessible at the device relative @pgoff.
124 */
125long dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff, long nr_pages,
126 void **kaddr, pfn_t *pfn)
127{
128 long avail;
129
Dan Williamsb0686262017-01-26 20:37:35 -0800130 if (!dax_dev)
131 return -EOPNOTSUPP;
132
133 if (!dax_alive(dax_dev))
134 return -ENXIO;
135
136 if (nr_pages < 0)
Ira Weinyb05d4c52021-05-25 10:24:28 -0700137 return -EINVAL;
Dan Williamsb0686262017-01-26 20:37:35 -0800138
139 avail = dax_dev->ops->direct_access(dax_dev, pgoff, nr_pages,
140 kaddr, pfn);
141 if (!avail)
142 return -ERANGE;
143 return min(avail, nr_pages);
144}
145EXPORT_SYMBOL_GPL(dax_direct_access);
146
Dan Williams7e026c82017-05-29 12:57:56 -0700147size_t dax_copy_from_iter(struct dax_device *dax_dev, pgoff_t pgoff, void *addr,
148 size_t bytes, struct iov_iter *i)
149{
150 if (!dax_alive(dax_dev))
151 return 0;
152
Christoph Hellwig7ac53602021-12-15 09:45:08 +0100153 /*
154 * The userspace address for the memory copy has already been validated
155 * via access_ok() in vfs_write, so use the 'no check' version to bypass
156 * the HARDENED_USERCOPY overhead.
157 */
158 if (test_bit(DAXDEV_NOCACHE, &dax_dev->flags))
159 return _copy_from_iter_flushcache(addr, bytes, i);
160 return _copy_from_iter(addr, bytes, i);
Dan Williams7e026c82017-05-29 12:57:56 -0700161}
Dan Williams7e026c82017-05-29 12:57:56 -0700162
Dan Williamsb3a9a0c2018-05-02 06:46:33 -0700163size_t dax_copy_to_iter(struct dax_device *dax_dev, pgoff_t pgoff, void *addr,
164 size_t bytes, struct iov_iter *i)
165{
166 if (!dax_alive(dax_dev))
167 return 0;
168
Christoph Hellwig7ac53602021-12-15 09:45:08 +0100169 /*
170 * The userspace address for the memory copy has already been validated
171 * via access_ok() in vfs_red, so use the 'no check' version to bypass
172 * the HARDENED_USERCOPY overhead.
173 */
174 if (test_bit(DAXDEV_NOMC, &dax_dev->flags))
175 return _copy_mc_to_iter(addr, bytes, i);
176 return _copy_to_iter(addr, bytes, i);
Dan Williamsb3a9a0c2018-05-02 06:46:33 -0700177}
Dan Williamsb3a9a0c2018-05-02 06:46:33 -0700178
Vivek Goyalf605a262020-02-28 11:34:52 -0500179int dax_zero_page_range(struct dax_device *dax_dev, pgoff_t pgoff,
180 size_t nr_pages)
181{
182 if (!dax_alive(dax_dev))
183 return -ENXIO;
Vivek Goyalf605a262020-02-28 11:34:52 -0500184 /*
185 * There are no callers that want to zero more than one page as of now.
186 * Once users are there, this check can be removed after the
187 * device mapper code has been updated to split ranges across targets.
188 */
189 if (nr_pages != 1)
190 return -EIO;
191
192 return dax_dev->ops->zero_page_range(dax_dev, pgoff, nr_pages);
193}
194EXPORT_SYMBOL_GPL(dax_zero_page_range);
195
Mikulas Patockac3ca0152017-08-31 21:47:43 -0400196#ifdef CONFIG_ARCH_HAS_PMEM_API
197void arch_wb_cache_pmem(void *addr, size_t size);
198void dax_flush(struct dax_device *dax_dev, void *addr, size_t size)
Dan Williamsabebfbe22017-05-29 13:02:52 -0700199{
Ross Zwisler808c3402018-06-06 10:45:14 -0600200 if (unlikely(!dax_write_cache_enabled(dax_dev)))
Dan Williams6e0c90d2017-06-26 21:28:41 -0700201 return;
202
Mikulas Patockac3ca0152017-08-31 21:47:43 -0400203 arch_wb_cache_pmem(addr, size);
Dan Williamsabebfbe22017-05-29 13:02:52 -0700204}
Mikulas Patockac3ca0152017-08-31 21:47:43 -0400205#else
206void dax_flush(struct dax_device *dax_dev, void *addr, size_t size)
207{
208}
209#endif
Dan Williamsabebfbe22017-05-29 13:02:52 -0700210EXPORT_SYMBOL_GPL(dax_flush);
211
Dan Williams6e0c90d2017-06-26 21:28:41 -0700212void dax_write_cache(struct dax_device *dax_dev, bool wc)
213{
214 if (wc)
215 set_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags);
216 else
217 clear_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags);
218}
219EXPORT_SYMBOL_GPL(dax_write_cache);
220
Vivek Goyal273752c2017-07-26 09:35:09 -0400221bool dax_write_cache_enabled(struct dax_device *dax_dev)
222{
223 return test_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags);
224}
225EXPORT_SYMBOL_GPL(dax_write_cache_enabled);
226
Christoph Hellwigfd1d00e2021-12-15 09:45:06 +0100227bool dax_synchronous(struct dax_device *dax_dev)
Pankaj Guptafefc1d92019-07-05 19:33:24 +0530228{
229 return test_bit(DAXDEV_SYNC, &dax_dev->flags);
230}
Christoph Hellwigfd1d00e2021-12-15 09:45:06 +0100231EXPORT_SYMBOL_GPL(dax_synchronous);
Pankaj Guptafefc1d92019-07-05 19:33:24 +0530232
Christoph Hellwigfd1d00e2021-12-15 09:45:06 +0100233void set_dax_synchronous(struct dax_device *dax_dev)
Pankaj Guptafefc1d92019-07-05 19:33:24 +0530234{
235 set_bit(DAXDEV_SYNC, &dax_dev->flags);
236}
Christoph Hellwigfd1d00e2021-12-15 09:45:06 +0100237EXPORT_SYMBOL_GPL(set_dax_synchronous);
Pankaj Guptafefc1d92019-07-05 19:33:24 +0530238
Christoph Hellwig7ac53602021-12-15 09:45:08 +0100239void set_dax_nocache(struct dax_device *dax_dev)
240{
241 set_bit(DAXDEV_NOCACHE, &dax_dev->flags);
242}
243EXPORT_SYMBOL_GPL(set_dax_nocache);
244
245void set_dax_nomc(struct dax_device *dax_dev)
246{
247 set_bit(DAXDEV_NOMC, &dax_dev->flags);
248}
249EXPORT_SYMBOL_GPL(set_dax_nomc);
250
Dan Williams7b6be842017-04-11 09:49:49 -0700251bool dax_alive(struct dax_device *dax_dev)
252{
253 lockdep_assert_held(&dax_srcu);
Dan Williams9a60c3e2017-06-27 17:59:28 -0700254 return test_bit(DAXDEV_ALIVE, &dax_dev->flags);
Dan Williams7b6be842017-04-11 09:49:49 -0700255}
256EXPORT_SYMBOL_GPL(dax_alive);
257
258/*
259 * Note, rcu is not protecting the liveness of dax_dev, rcu is ensuring
260 * that any fault handlers or operations that might have seen
261 * dax_alive(), have completed. Any operations that start after
262 * synchronize_srcu() has run will abort upon seeing !dax_alive().
263 */
264void kill_dax(struct dax_device *dax_dev)
265{
266 if (!dax_dev)
267 return;
268
Dan Williams9a60c3e2017-06-27 17:59:28 -0700269 clear_bit(DAXDEV_ALIVE, &dax_dev->flags);
Dan Williams7b6be842017-04-11 09:49:49 -0700270 synchronize_srcu(&dax_srcu);
Dan Williams7b6be842017-04-11 09:49:49 -0700271}
272EXPORT_SYMBOL_GPL(kill_dax);
273
Dan Williams9567da02017-07-12 17:58:21 -0700274void run_dax(struct dax_device *dax_dev)
275{
276 set_bit(DAXDEV_ALIVE, &dax_dev->flags);
277}
278EXPORT_SYMBOL_GPL(run_dax);
279
Dan Williams7b6be842017-04-11 09:49:49 -0700280static struct inode *dax_alloc_inode(struct super_block *sb)
281{
282 struct dax_device *dax_dev;
Dan Williamsb9d39d12017-06-09 08:50:49 -0700283 struct inode *inode;
Dan Williams7b6be842017-04-11 09:49:49 -0700284
285 dax_dev = kmem_cache_alloc(dax_cache, GFP_KERNEL);
Mikulas Patocka9f586ff2017-11-14 09:59:54 -0500286 if (!dax_dev)
287 return NULL;
288
Dan Williamsb9d39d12017-06-09 08:50:49 -0700289 inode = &dax_dev->inode;
290 inode->i_rdev = 0;
291 return inode;
Dan Williams7b6be842017-04-11 09:49:49 -0700292}
293
294static struct dax_device *to_dax_dev(struct inode *inode)
295{
296 return container_of(inode, struct dax_device, inode);
297}
298
Al Viro53e22822019-04-10 14:57:19 -0400299static void dax_free_inode(struct inode *inode)
Dan Williams7b6be842017-04-11 09:49:49 -0700300{
Dan Williams7b6be842017-04-11 09:49:49 -0700301 struct dax_device *dax_dev = to_dax_dev(inode);
Dan Williamsb9d39d12017-06-09 08:50:49 -0700302 if (inode->i_rdev)
Al Viro6f247842021-01-31 19:23:55 -0500303 ida_simple_remove(&dax_minor_ida, iminor(inode));
Dan Williams7b6be842017-04-11 09:49:49 -0700304 kmem_cache_free(dax_cache, dax_dev);
305}
306
307static void dax_destroy_inode(struct inode *inode)
308{
309 struct dax_device *dax_dev = to_dax_dev(inode);
Dan Williams9a60c3e2017-06-27 17:59:28 -0700310 WARN_ONCE(test_bit(DAXDEV_ALIVE, &dax_dev->flags),
Dan Williams7b6be842017-04-11 09:49:49 -0700311 "kill_dax() must be called before final iput()\n");
Dan Williams7b6be842017-04-11 09:49:49 -0700312}
313
314static const struct super_operations dax_sops = {
315 .statfs = simple_statfs,
316 .alloc_inode = dax_alloc_inode,
317 .destroy_inode = dax_destroy_inode,
Al Viro53e22822019-04-10 14:57:19 -0400318 .free_inode = dax_free_inode,
Dan Williams7b6be842017-04-11 09:49:49 -0700319 .drop_inode = generic_delete_inode,
320};
321
David Howells75d4e062019-03-25 16:38:24 +0000322static int dax_init_fs_context(struct fs_context *fc)
Dan Williams7b6be842017-04-11 09:49:49 -0700323{
David Howells75d4e062019-03-25 16:38:24 +0000324 struct pseudo_fs_context *ctx = init_pseudo(fc, DAXFS_MAGIC);
325 if (!ctx)
326 return -ENOMEM;
327 ctx->ops = &dax_sops;
328 return 0;
Dan Williams7b6be842017-04-11 09:49:49 -0700329}
330
331static struct file_system_type dax_fs_type = {
David Howells75d4e062019-03-25 16:38:24 +0000332 .name = "dax",
333 .init_fs_context = dax_init_fs_context,
334 .kill_sb = kill_anon_super,
Dan Williams7b6be842017-04-11 09:49:49 -0700335};
336
337static int dax_test(struct inode *inode, void *data)
338{
339 dev_t devt = *(dev_t *) data;
340
341 return inode->i_rdev == devt;
342}
343
344static int dax_set(struct inode *inode, void *data)
345{
346 dev_t devt = *(dev_t *) data;
347
348 inode->i_rdev = devt;
349 return 0;
350}
351
352static struct dax_device *dax_dev_get(dev_t devt)
353{
354 struct dax_device *dax_dev;
355 struct inode *inode;
356
357 inode = iget5_locked(dax_superblock, hash_32(devt + DAXFS_MAGIC, 31),
358 dax_test, dax_set, &devt);
359
360 if (!inode)
361 return NULL;
362
363 dax_dev = to_dax_dev(inode);
364 if (inode->i_state & I_NEW) {
Dan Williams9a60c3e2017-06-27 17:59:28 -0700365 set_bit(DAXDEV_ALIVE, &dax_dev->flags);
Dan Williams7b6be842017-04-11 09:49:49 -0700366 inode->i_cdev = &dax_dev->cdev;
367 inode->i_mode = S_IFCHR;
368 inode->i_flags = S_DAX;
369 mapping_set_gfp_mask(&inode->i_data, GFP_USER);
370 unlock_new_inode(inode);
371 }
372
373 return dax_dev;
374}
375
Christoph Hellwig30c68282021-12-15 09:45:07 +0100376struct dax_device *alloc_dax(void *private, const struct dax_operations *ops)
Dan Williams7b6be842017-04-11 09:49:49 -0700377{
378 struct dax_device *dax_dev;
379 dev_t devt;
380 int minor;
381
Christoph Hellwigfb08a192021-11-29 11:21:38 +0100382 if (WARN_ON_ONCE(ops && !ops->zero_page_range))
Vivek Goyal4e4ced92020-04-01 12:11:25 -0400383 return ERR_PTR(-EINVAL);
Dan Williams72058002017-04-19 15:14:31 -0700384
Dan Williamscf1e2282017-05-08 12:33:53 -0700385 minor = ida_simple_get(&dax_minor_ida, 0, MINORMASK+1, GFP_KERNEL);
Dan Williams7b6be842017-04-11 09:49:49 -0700386 if (minor < 0)
Christoph Hellwigfb08a192021-11-29 11:21:38 +0100387 return ERR_PTR(-ENOMEM);
Dan Williams7b6be842017-04-11 09:49:49 -0700388
389 devt = MKDEV(MAJOR(dax_devt), minor);
390 dax_dev = dax_dev_get(devt);
391 if (!dax_dev)
Dan Williams72058002017-04-19 15:14:31 -0700392 goto err_dev;
Dan Williams7b6be842017-04-11 09:49:49 -0700393
Dan Williams6568b082017-01-24 18:44:18 -0800394 dax_dev->ops = ops;
Dan Williams7b6be842017-04-11 09:49:49 -0700395 dax_dev->private = private;
396 return dax_dev;
397
Dan Williams72058002017-04-19 15:14:31 -0700398 err_dev:
Dan Williams7b6be842017-04-11 09:49:49 -0700399 ida_simple_remove(&dax_minor_ida, minor);
Vivek Goyal4e4ced92020-04-01 12:11:25 -0400400 return ERR_PTR(-ENOMEM);
Dan Williams7b6be842017-04-11 09:49:49 -0700401}
402EXPORT_SYMBOL_GPL(alloc_dax);
403
404void put_dax(struct dax_device *dax_dev)
405{
406 if (!dax_dev)
407 return;
408 iput(&dax_dev->inode);
409}
410EXPORT_SYMBOL_GPL(put_dax);
411
412/**
Dan Williams7b6be842017-04-11 09:49:49 -0700413 * inode_dax: convert a public inode into its dax_dev
414 * @inode: An inode with i_cdev pointing to a dax_dev
415 *
416 * Note this is not equivalent to to_dax_dev() which is for private
417 * internal use where we know the inode filesystem type == dax_fs_type.
418 */
419struct dax_device *inode_dax(struct inode *inode)
420{
421 struct cdev *cdev = inode->i_cdev;
422
423 return container_of(cdev, struct dax_device, cdev);
424}
425EXPORT_SYMBOL_GPL(inode_dax);
426
427struct inode *dax_inode(struct dax_device *dax_dev)
428{
429 return &dax_dev->inode;
430}
431EXPORT_SYMBOL_GPL(dax_inode);
432
433void *dax_get_private(struct dax_device *dax_dev)
434{
Dan Williams9567da02017-07-12 17:58:21 -0700435 if (!test_bit(DAXDEV_ALIVE, &dax_dev->flags))
436 return NULL;
Dan Williams7b6be842017-04-11 09:49:49 -0700437 return dax_dev->private;
438}
439EXPORT_SYMBOL_GPL(dax_get_private);
440
441static void init_once(void *_dax_dev)
442{
443 struct dax_device *dax_dev = _dax_dev;
444 struct inode *inode = &dax_dev->inode;
445
Dan Williamsb9d39d12017-06-09 08:50:49 -0700446 memset(dax_dev, 0, sizeof(*dax_dev));
Dan Williams7b6be842017-04-11 09:49:49 -0700447 inode_init_once(inode);
448}
449
Dan Williams9567da02017-07-12 17:58:21 -0700450static int dax_fs_init(void)
Dan Williams7b6be842017-04-11 09:49:49 -0700451{
452 int rc;
453
454 dax_cache = kmem_cache_create("dax_cache", sizeof(struct dax_device), 0,
455 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
456 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
457 init_once);
458 if (!dax_cache)
459 return -ENOMEM;
460
Dan Williams7b6be842017-04-11 09:49:49 -0700461 dax_mnt = kern_mount(&dax_fs_type);
462 if (IS_ERR(dax_mnt)) {
463 rc = PTR_ERR(dax_mnt);
464 goto err_mount;
465 }
466 dax_superblock = dax_mnt->mnt_sb;
467
468 return 0;
469
470 err_mount:
Dan Williams7b6be842017-04-11 09:49:49 -0700471 kmem_cache_destroy(dax_cache);
472
473 return rc;
474}
475
Dan Williams9567da02017-07-12 17:58:21 -0700476static void dax_fs_exit(void)
Dan Williams7b6be842017-04-11 09:49:49 -0700477{
478 kern_unmount(dax_mnt);
Dan Williams7b6be842017-04-11 09:49:49 -0700479 kmem_cache_destroy(dax_cache);
480}
481
Dan Williams9567da02017-07-12 17:58:21 -0700482static int __init dax_core_init(void)
Dan Williams7b6be842017-04-11 09:49:49 -0700483{
484 int rc;
485
Dan Williams9567da02017-07-12 17:58:21 -0700486 rc = dax_fs_init();
Dan Williams7b6be842017-04-11 09:49:49 -0700487 if (rc)
488 return rc;
489
Dan Williamscf1e2282017-05-08 12:33:53 -0700490 rc = alloc_chrdev_region(&dax_devt, 0, MINORMASK+1, "dax");
Dan Williams7b6be842017-04-11 09:49:49 -0700491 if (rc)
Dan Williams9567da02017-07-12 17:58:21 -0700492 goto err_chrdev;
493
494 rc = dax_bus_init();
495 if (rc)
496 goto err_bus;
497 return 0;
498
499err_bus:
500 unregister_chrdev_region(dax_devt, MINORMASK+1);
501err_chrdev:
502 dax_fs_exit();
503 return 0;
Dan Williams7b6be842017-04-11 09:49:49 -0700504}
505
Dan Williams9567da02017-07-12 17:58:21 -0700506static void __exit dax_core_exit(void)
Dan Williams7b6be842017-04-11 09:49:49 -0700507{
Wang Hai1aa57432020-12-01 21:59:29 +0800508 dax_bus_exit();
Dan Williamscf1e2282017-05-08 12:33:53 -0700509 unregister_chrdev_region(dax_devt, MINORMASK+1);
Dan Williams7b6be842017-04-11 09:49:49 -0700510 ida_destroy(&dax_minor_ida);
Dan Williams9567da02017-07-12 17:58:21 -0700511 dax_fs_exit();
Dan Williams7b6be842017-04-11 09:49:49 -0700512}
513
514MODULE_AUTHOR("Intel Corporation");
515MODULE_LICENSE("GPL v2");
Dan Williams9567da02017-07-12 17:58:21 -0700516subsys_initcall(dax_core_init);
517module_exit(dax_core_exit);