1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright(c) 2017 Intel Corporation. All rights reserved.
4 */
5 #include <linux/pagemap.h>
6 #include <linux/module.h>
7 #include <linux/mount.h>
8 #include <linux/pseudo_fs.h>
9 #include <linux/magic.h>
10 #include <linux/cdev.h>
11 #include <linux/slab.h>
12 #include <linux/uio.h>
13 #include <linux/dax.h>
14 #include <linux/fs.h>
15 #include <linux/cacheinfo.h>
16 #include "dax-private.h"
17 #include "bus.h"
18
19 /**
20 * struct dax_device - anchor object for dax services
21 * @inode: core vfs
22 * @cdev: optional character interface for "device dax"
23 * @private: dax driver private data
24 * @flags: state and boolean properties
25 * @ops: operations for this device
26 * @holder_data: holder of a dax_device: could be filesystem or mapped device
27 * @holder_ops: operations for the inner holder
28 */
29 struct dax_device {
30 struct inode inode;
31 struct cdev cdev;
32 void *private;
33 unsigned long flags;
34 const struct dax_operations *ops;
35 void *holder_data;
36 const struct dax_holder_operations *holder_ops;
37 };
38
39 static dev_t dax_devt;
40 DEFINE_STATIC_SRCU(dax_srcu);
41 static struct vfsmount *dax_mnt;
42 static DEFINE_IDA(dax_minor_ida);
43 static struct kmem_cache *dax_cache __read_mostly;
44 static struct super_block *dax_superblock __read_mostly;
45
dax_read_lock(void)46 int dax_read_lock(void)
47 {
48 return srcu_read_lock(&dax_srcu);
49 }
50 EXPORT_SYMBOL_GPL(dax_read_lock);
51
dax_read_unlock(int id)52 void dax_read_unlock(int id)
53 {
54 srcu_read_unlock(&dax_srcu, id);
55 }
56 EXPORT_SYMBOL_GPL(dax_read_unlock);
57
58 #if defined(CONFIG_BLOCK) && defined(CONFIG_FS_DAX)
59 #include <linux/blkdev.h>
60
61 static DEFINE_XARRAY(dax_hosts);
62
dax_add_host(struct dax_device * dax_dev,struct gendisk * disk)63 int dax_add_host(struct dax_device *dax_dev, struct gendisk *disk)
64 {
65 return xa_insert(&dax_hosts, (unsigned long)disk, dax_dev, GFP_KERNEL);
66 }
67 EXPORT_SYMBOL_GPL(dax_add_host);
68
dax_remove_host(struct gendisk * disk)69 void dax_remove_host(struct gendisk *disk)
70 {
71 xa_erase(&dax_hosts, (unsigned long)disk);
72 }
73 EXPORT_SYMBOL_GPL(dax_remove_host);
74
75 /**
76 * fs_dax_get_by_bdev() - temporary lookup mechanism for filesystem-dax
77 * @bdev: block device to find a dax_device for
78 * @start_off: returns the byte offset into the dax_device that @bdev starts
79 * @holder: filesystem or mapped device inside the dax_device
80 * @ops: operations for the inner holder
81 */
fs_dax_get_by_bdev(struct block_device * bdev,u64 * start_off,void * holder,const struct dax_holder_operations * ops)82 struct dax_device *fs_dax_get_by_bdev(struct block_device *bdev, u64 *start_off,
83 void *holder, const struct dax_holder_operations *ops)
84 {
85 struct dax_device *dax_dev;
86 u64 part_size;
87 int id;
88
89 if (!blk_queue_dax(bdev->bd_disk->queue))
90 return NULL;
91
92 *start_off = get_start_sect(bdev) * SECTOR_SIZE;
93 part_size = bdev_nr_sectors(bdev) * SECTOR_SIZE;
94 if (*start_off % PAGE_SIZE || part_size % PAGE_SIZE) {
95 pr_info("%pg: error: unaligned partition for dax\n", bdev);
96 return NULL;
97 }
98
99 id = dax_read_lock();
100 dax_dev = xa_load(&dax_hosts, (unsigned long)bdev->bd_disk);
101 if (!dax_dev || !dax_alive(dax_dev) || !igrab(&dax_dev->inode))
102 dax_dev = NULL;
103 else if (holder) {
104 if (!cmpxchg(&dax_dev->holder_data, NULL, holder))
105 dax_dev->holder_ops = ops;
106 else
107 dax_dev = NULL;
108 }
109 dax_read_unlock(id);
110
111 return dax_dev;
112 }
113 EXPORT_SYMBOL_GPL(fs_dax_get_by_bdev);
114
115 #endif /* CONFIG_BLOCK && CONFIG_FS_DAX */
116
117 #if IS_ENABLED(CONFIG_FS_DAX)
118
119 /**
120 * fs_put_dax() - release holder ownership of a dax_device
121 * @dax_dev: dax device to release (may be NULL)
122 * @holder: the holder pointer previously passed to fs_dax_get() or
123 * fs_dax_get_by_bdev(); must match exactly, as it is used
124 * in a cmpxchg to atomically release ownership
125 *
126 * Must only be called by the current holder. Clears holder_ops before
127 * holder_data to avoid a race where a concurrent fs_dax_get() could have
128 * its newly installed holder_ops overwritten.
129 */
fs_put_dax(struct dax_device * dax_dev,void * holder)130 void fs_put_dax(struct dax_device *dax_dev, void *holder)
131 {
132 if (dax_dev && holder) {
133 void *prev;
134
135 /*
136 * Clear holder_ops before releasing holder_data. A concurrent
137 * dax_holder_notify_failure() that sees NULL ops returns
138 * -EOPNOTSUPP cleanly. A concurrent fs_dax_get() that acquires
139 * holder_data after the cmpxchg below is guaranteed to observe
140 * holder_ops=NULL first (cmpxchg provides release ordering), so
141 * its subsequent store of new ops will not be overwritten.
142 */
143 WRITE_ONCE(dax_dev->holder_ops, NULL);
144 prev = cmpxchg(&dax_dev->holder_data, holder, NULL);
145
146 /*
147 * prev == holder: normal release.
148 * prev == NULL: already released by kill_dax() when the
149 * device was removed under a live holder;
150 * not a bug.
151 * prev != holder (non-NULL): fs_put_dax() called by something
152 * that is not the current holder; an API
153 * contract violation. A lock would be needed
154 * to guard against this, but we WARN_ON()
155 * instead since violating the contract is
156 * a bug.
157 */
158 WARN_ON(prev && prev != holder);
159 }
160 put_dax(dax_dev);
161 }
162 EXPORT_SYMBOL_GPL(fs_put_dax);
163
164 /**
165 * fs_dax_get() - get ownership of a devdax via holder/holder_ops
166 *
167 * fs-dax file systems call this function to prepare to use a devdax device for
168 * fsdax. This is like fs_dax_get_by_bdev(), but the caller already has struct
169 * dev_dax (and there is no bdev). The holder makes this exclusive.
170 *
171 * @dax_dev: dev to be prepared for fs-dax usage
172 * @holder: filesystem or mapped device inside the dax_device
173 * @hops: operations for the inner holder
174 *
175 * Returns: 0 on success, <0 on failure
176 */
fs_dax_get(struct dax_device * dax_dev,void * holder,const struct dax_holder_operations * hops)177 int fs_dax_get(struct dax_device *dax_dev, void *holder,
178 const struct dax_holder_operations *hops)
179 {
180 struct dev_dax *dev_dax;
181 struct dax_device_driver *dax_drv;
182 int id;
183
184 id = dax_read_lock();
185 if (!dax_dev || !dax_alive(dax_dev) || !igrab(&dax_dev->inode)) {
186 dax_read_unlock(id);
187 return -ENODEV;
188 }
189 dax_read_unlock(id);
190
191 /* Verify the device is bound to fsdev_dax driver */
192 dev_dax = dax_get_private(dax_dev);
193 if (!dev_dax) {
194 iput(&dax_dev->inode);
195 return -ENODEV;
196 }
197
198 device_lock(&dev_dax->dev);
199 if (!dev_dax->dev.driver) {
200 device_unlock(&dev_dax->dev);
201 iput(&dax_dev->inode);
202 return -ENODEV;
203 }
204 dax_drv = to_dax_drv(dev_dax->dev.driver);
205 if (dax_drv->type != DAXDRV_FSDEV_TYPE) {
206 device_unlock(&dev_dax->dev);
207 iput(&dax_dev->inode);
208 return -EOPNOTSUPP;
209 }
210 device_unlock(&dev_dax->dev);
211
212 if (cmpxchg(&dax_dev->holder_data, NULL, holder)) {
213 iput(&dax_dev->inode);
214 return -EBUSY;
215 }
216
217 dax_dev->holder_ops = hops;
218
219 return 0;
220 }
221 EXPORT_SYMBOL_GPL(fs_dax_get);
222 #endif /* CONFIG_FS_DAX */
223
224 enum dax_device_flags {
225 /* !alive + rcu grace period == no new operations / mappings */
226 DAXDEV_ALIVE,
227 /* gate whether dax_flush() calls the low level flush routine */
228 DAXDEV_WRITE_CACHE,
229 /* flag to check if device supports synchronous flush */
230 DAXDEV_SYNC,
231 /* do not leave the caches dirty after writes */
232 DAXDEV_NOCACHE,
233 /* handle CPU fetch exceptions during reads */
234 DAXDEV_NOMC,
235 };
236
237 /**
238 * dax_direct_access() - translate a device pgoff to an absolute pfn
239 * @dax_dev: a dax_device instance representing the logical memory range
240 * @pgoff: offset in pages from the start of the device to translate
241 * @nr_pages: number of consecutive pages caller can handle relative to @pfn
242 * @mode: indicator on normal access or recovery write
243 * @kaddr: output parameter that returns a virtual address mapping of pfn
244 * @pfn: output parameter that returns an absolute pfn translation of @pgoff
245 *
246 * Return: negative errno if an error occurs, otherwise the number of
247 * pages accessible at the device relative @pgoff.
248 */
dax_direct_access(struct dax_device * dax_dev,pgoff_t pgoff,long nr_pages,enum dax_access_mode mode,void ** kaddr,unsigned long * pfn)249 long dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff, long nr_pages,
250 enum dax_access_mode mode, void **kaddr, unsigned long *pfn)
251 {
252 long avail;
253
254 if (!dax_dev)
255 return -EOPNOTSUPP;
256
257 if (!dax_alive(dax_dev))
258 return -ENXIO;
259
260 if (!dax_dev->ops)
261 return -EOPNOTSUPP;
262
263 if (nr_pages < 0)
264 return -EINVAL;
265
266 avail = dax_dev->ops->direct_access(dax_dev, pgoff, nr_pages,
267 mode, kaddr, pfn);
268 if (!avail)
269 return -ERANGE;
270 return min(avail, nr_pages);
271 }
272 EXPORT_SYMBOL_GPL(dax_direct_access);
273
dax_copy_from_iter(struct dax_device * dax_dev,pgoff_t pgoff,void * addr,size_t bytes,struct iov_iter * i)274 size_t dax_copy_from_iter(struct dax_device *dax_dev, pgoff_t pgoff, void *addr,
275 size_t bytes, struct iov_iter *i)
276 {
277 if (!dax_alive(dax_dev))
278 return 0;
279
280 /*
281 * The userspace address for the memory copy has already been validated
282 * via access_ok() in vfs_write, so use the 'no check' version to bypass
283 * the HARDENED_USERCOPY overhead.
284 */
285 if (test_bit(DAXDEV_NOCACHE, &dax_dev->flags))
286 return _copy_from_iter_flushcache(addr, bytes, i);
287 return _copy_from_iter(addr, bytes, i);
288 }
289
dax_copy_to_iter(struct dax_device * dax_dev,pgoff_t pgoff,void * addr,size_t bytes,struct iov_iter * i)290 size_t dax_copy_to_iter(struct dax_device *dax_dev, pgoff_t pgoff, void *addr,
291 size_t bytes, struct iov_iter *i)
292 {
293 if (!dax_alive(dax_dev))
294 return 0;
295
296 /*
297 * The userspace address for the memory copy has already been validated
298 * via access_ok() in vfs_red, so use the 'no check' version to bypass
299 * the HARDENED_USERCOPY overhead.
300 */
301 if (test_bit(DAXDEV_NOMC, &dax_dev->flags))
302 return _copy_mc_to_iter(addr, bytes, i);
303 return _copy_to_iter(addr, bytes, i);
304 }
305
dax_zero_page_range(struct dax_device * dax_dev,pgoff_t pgoff,size_t nr_pages)306 int dax_zero_page_range(struct dax_device *dax_dev, pgoff_t pgoff,
307 size_t nr_pages)
308 {
309 int ret;
310
311 if (!dax_alive(dax_dev))
312 return -ENXIO;
313
314 if (!dax_dev->ops)
315 return -EOPNOTSUPP;
316
317 /*
318 * There are no callers that want to zero more than one page as of now.
319 * Once users are there, this check can be removed after the
320 * device mapper code has been updated to split ranges across targets.
321 */
322 if (nr_pages != 1)
323 return -EIO;
324
325 ret = dax_dev->ops->zero_page_range(dax_dev, pgoff, nr_pages);
326 return dax_mem2blk_err(ret);
327 }
328 EXPORT_SYMBOL_GPL(dax_zero_page_range);
329
dax_recovery_write(struct dax_device * dax_dev,pgoff_t pgoff,void * addr,size_t bytes,struct iov_iter * iter)330 size_t dax_recovery_write(struct dax_device *dax_dev, pgoff_t pgoff,
331 void *addr, size_t bytes, struct iov_iter *iter)
332 {
333 if (!dax_dev->ops || !dax_dev->ops->recovery_write)
334 return 0;
335 return dax_dev->ops->recovery_write(dax_dev, pgoff, addr, bytes, iter);
336 }
337 EXPORT_SYMBOL_GPL(dax_recovery_write);
338
dax_holder_notify_failure(struct dax_device * dax_dev,u64 off,u64 len,int mf_flags)339 int dax_holder_notify_failure(struct dax_device *dax_dev, u64 off,
340 u64 len, int mf_flags)
341 {
342 const struct dax_holder_operations *ops;
343 int rc, id;
344
345 id = dax_read_lock();
346 if (!dax_alive(dax_dev)) {
347 rc = -ENXIO;
348 goto out;
349 }
350
351 /*
352 * Read holder_ops once: a concurrent fs_put_dax() can clear it without
353 * synchronizing against readers. Without the single fetch the compiler
354 * could reload between the NULL check and the call and dereference a
355 * NULL ops.
356 */
357 ops = READ_ONCE(dax_dev->holder_ops);
358 if (!ops) {
359 rc = -EOPNOTSUPP;
360 goto out;
361 }
362
363 rc = ops->notify_failure(dax_dev, off, len, mf_flags);
364 out:
365 dax_read_unlock(id);
366 return rc;
367 }
368 EXPORT_SYMBOL_GPL(dax_holder_notify_failure);
369
370 #ifdef CONFIG_ARCH_HAS_PMEM_API
371 void arch_wb_cache_pmem(void *addr, size_t size);
dax_flush(struct dax_device * dax_dev,void * addr,size_t size)372 void dax_flush(struct dax_device *dax_dev, void *addr, size_t size)
373 {
374 if (unlikely(!dax_write_cache_enabled(dax_dev)))
375 return;
376
377 arch_wb_cache_pmem(addr, size);
378 }
379 #else
dax_flush(struct dax_device * dax_dev,void * addr,size_t size)380 void dax_flush(struct dax_device *dax_dev, void *addr, size_t size)
381 {
382 }
383 #endif
384 EXPORT_SYMBOL_GPL(dax_flush);
385
dax_write_cache(struct dax_device * dax_dev,bool wc)386 void dax_write_cache(struct dax_device *dax_dev, bool wc)
387 {
388 if (wc)
389 set_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags);
390 else
391 clear_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags);
392 }
393 EXPORT_SYMBOL_GPL(dax_write_cache);
394
dax_write_cache_enabled(struct dax_device * dax_dev)395 bool dax_write_cache_enabled(struct dax_device *dax_dev)
396 {
397 return test_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags);
398 }
399 EXPORT_SYMBOL_GPL(dax_write_cache_enabled);
400
dax_synchronous(struct dax_device * dax_dev)401 bool dax_synchronous(struct dax_device *dax_dev)
402 {
403 return test_bit(DAXDEV_SYNC, &dax_dev->flags);
404 }
405 EXPORT_SYMBOL_GPL(dax_synchronous);
406
set_dax_synchronous(struct dax_device * dax_dev)407 void set_dax_synchronous(struct dax_device *dax_dev)
408 {
409 set_bit(DAXDEV_SYNC, &dax_dev->flags);
410 }
411 EXPORT_SYMBOL_GPL(set_dax_synchronous);
412
set_dax_nocache(struct dax_device * dax_dev)413 void set_dax_nocache(struct dax_device *dax_dev)
414 {
415 set_bit(DAXDEV_NOCACHE, &dax_dev->flags);
416 }
417 EXPORT_SYMBOL_GPL(set_dax_nocache);
418
set_dax_nomc(struct dax_device * dax_dev)419 void set_dax_nomc(struct dax_device *dax_dev)
420 {
421 set_bit(DAXDEV_NOMC, &dax_dev->flags);
422 }
423 EXPORT_SYMBOL_GPL(set_dax_nomc);
424
425 /**
426 * dax_set_ops - set the dax_operations for a dax_device
427 * @dax_dev: the dax_device to configure
428 * @ops: the operations to set (may be NULL to clear)
429 *
430 * This allows drivers to set the dax_operations after the dax_device
431 * has been allocated. This is needed when the device is created before
432 * the driver that needs specific ops is bound (e.g., fsdev_dax binding
433 * to a dev_dax created by hmem).
434 *
435 * When setting non-NULL ops, fails if ops are already set (returns -EBUSY).
436 * When clearing ops (NULL), always succeeds.
437 *
438 * Return: 0 on success, -EBUSY if ops already set
439 */
dax_set_ops(struct dax_device * dax_dev,const struct dax_operations * ops)440 int dax_set_ops(struct dax_device *dax_dev, const struct dax_operations *ops)
441 {
442 if (ops) {
443 /* Setting ops: fail if already set */
444 if (cmpxchg(&dax_dev->ops, NULL, ops) != NULL)
445 return -EBUSY;
446 } else {
447 /* Clearing ops: always allowed */
448 dax_dev->ops = NULL;
449 }
450 return 0;
451 }
452 EXPORT_SYMBOL_GPL(dax_set_ops);
453
dax_alive(struct dax_device * dax_dev)454 bool dax_alive(struct dax_device *dax_dev)
455 {
456 lockdep_assert_held(&dax_srcu);
457 return test_bit(DAXDEV_ALIVE, &dax_dev->flags);
458 }
459 EXPORT_SYMBOL_GPL(dax_alive);
460
461 /*
462 * Note, rcu is not protecting the liveness of dax_dev, rcu is ensuring
463 * that any fault handlers or operations that might have seen
464 * dax_alive(), have completed. Any operations that start after
465 * synchronize_srcu() has run will abort upon seeing !dax_alive().
466 *
467 * Note, because alloc_dax() returns an ERR_PTR() on error, callers
468 * typically store its result into a local variable in order to check
469 * the result. Therefore, care must be taken to populate the struct
470 * device dax_dev field make sure the dax_dev is not leaked.
471 */
kill_dax(struct dax_device * dax_dev)472 void kill_dax(struct dax_device *dax_dev)
473 {
474 if (!dax_dev)
475 return;
476
477 if (dax_dev->holder_data != NULL)
478 dax_holder_notify_failure(dax_dev, 0, U64_MAX,
479 MF_MEM_PRE_REMOVE);
480
481 clear_bit(DAXDEV_ALIVE, &dax_dev->flags);
482 synchronize_srcu(&dax_srcu);
483
484 /* clear holder data */
485 dax_dev->holder_ops = NULL;
486 dax_dev->holder_data = NULL;
487 }
488 EXPORT_SYMBOL_GPL(kill_dax);
489
run_dax(struct dax_device * dax_dev)490 void run_dax(struct dax_device *dax_dev)
491 {
492 set_bit(DAXDEV_ALIVE, &dax_dev->flags);
493 }
494 EXPORT_SYMBOL_GPL(run_dax);
495
dax_alloc_inode(struct super_block * sb)496 static struct inode *dax_alloc_inode(struct super_block *sb)
497 {
498 struct dax_device *dax_dev;
499 struct inode *inode;
500
501 dax_dev = alloc_inode_sb(sb, dax_cache, GFP_KERNEL);
502 if (!dax_dev)
503 return NULL;
504
505 inode = &dax_dev->inode;
506 inode->i_rdev = 0;
507 return inode;
508 }
509
to_dax_dev(struct inode * inode)510 static struct dax_device *to_dax_dev(struct inode *inode)
511 {
512 return container_of(inode, struct dax_device, inode);
513 }
514
dax_free_inode(struct inode * inode)515 static void dax_free_inode(struct inode *inode)
516 {
517 struct dax_device *dax_dev = to_dax_dev(inode);
518 if (inode->i_rdev)
519 ida_free(&dax_minor_ida, iminor(inode));
520 kmem_cache_free(dax_cache, dax_dev);
521 }
522
dax_destroy_inode(struct inode * inode)523 static void dax_destroy_inode(struct inode *inode)
524 {
525 struct dax_device *dax_dev = to_dax_dev(inode);
526 WARN_ONCE(test_bit(DAXDEV_ALIVE, &dax_dev->flags),
527 "kill_dax() must be called before final iput()\n");
528 }
529
530 static const struct super_operations dax_sops = {
531 .statfs = simple_statfs,
532 .alloc_inode = dax_alloc_inode,
533 .destroy_inode = dax_destroy_inode,
534 .free_inode = dax_free_inode,
535 .drop_inode = inode_just_drop,
536 };
537
dax_init_fs_context(struct fs_context * fc)538 static int dax_init_fs_context(struct fs_context *fc)
539 {
540 struct pseudo_fs_context *ctx = init_pseudo(fc, DAXFS_MAGIC);
541 if (!ctx)
542 return -ENOMEM;
543 ctx->ops = &dax_sops;
544 return 0;
545 }
546
547 static struct file_system_type dax_fs_type = {
548 .name = "dax",
549 .init_fs_context = dax_init_fs_context,
550 .kill_sb = kill_anon_super,
551 };
552
dax_test(struct inode * inode,void * data)553 static int dax_test(struct inode *inode, void *data)
554 {
555 dev_t devt = *(dev_t *) data;
556
557 return inode->i_rdev == devt;
558 }
559
dax_set(struct inode * inode,void * data)560 static int dax_set(struct inode *inode, void *data)
561 {
562 dev_t devt = *(dev_t *) data;
563
564 inode->i_rdev = devt;
565 return 0;
566 }
567
dax_dev_get(dev_t devt)568 struct dax_device *dax_dev_get(dev_t devt)
569 {
570 struct dax_device *dax_dev;
571 struct inode *inode;
572
573 inode = iget5_locked(dax_superblock, hash_32(devt + DAXFS_MAGIC, 31),
574 dax_test, dax_set, &devt);
575
576 if (!inode)
577 return NULL;
578
579 dax_dev = to_dax_dev(inode);
580 if (inode_state_read_once(inode) & I_NEW) {
581 set_bit(DAXDEV_ALIVE, &dax_dev->flags);
582 inode->i_cdev = &dax_dev->cdev;
583 inode->i_mode = S_IFCHR;
584 inode->i_flags = S_DAX;
585 mapping_set_gfp_mask(&inode->i_data, GFP_USER);
586 unlock_new_inode(inode);
587 }
588
589 return dax_dev;
590 }
591 EXPORT_SYMBOL_GPL(dax_dev_get);
592
alloc_dax(void * private,const struct dax_operations * ops)593 struct dax_device *alloc_dax(void *private, const struct dax_operations *ops)
594 {
595 struct dax_device *dax_dev;
596 dev_t devt;
597 int minor;
598
599 /*
600 * Unavailable on architectures with virtually aliased data caches,
601 * except for device-dax (NULL operations pointer), which does
602 * not use aliased mappings from the kernel.
603 */
604 if (ops && cpu_dcache_is_aliasing())
605 return ERR_PTR(-EOPNOTSUPP);
606
607 if (WARN_ON_ONCE(ops && !ops->zero_page_range))
608 return ERR_PTR(-EINVAL);
609
610 minor = ida_alloc_max(&dax_minor_ida, MINORMASK, GFP_KERNEL);
611 if (minor < 0)
612 return ERR_PTR(-ENOMEM);
613
614 devt = MKDEV(MAJOR(dax_devt), minor);
615 dax_dev = dax_dev_get(devt);
616 if (!dax_dev)
617 goto err_dev;
618
619 dax_dev->ops = ops;
620 dax_dev->private = private;
621 return dax_dev;
622
623 err_dev:
624 ida_free(&dax_minor_ida, minor);
625 return ERR_PTR(-ENOMEM);
626 }
627 EXPORT_SYMBOL_GPL(alloc_dax);
628
put_dax(struct dax_device * dax_dev)629 void put_dax(struct dax_device *dax_dev)
630 {
631 if (!dax_dev)
632 return;
633 iput(&dax_dev->inode);
634 }
635 EXPORT_SYMBOL_GPL(put_dax);
636
637 /**
638 * dax_holder() - obtain the holder of a dax device
639 * @dax_dev: a dax_device instance
640 *
641 * Return: the holder's data which represents the holder if registered,
642 * otherwize NULL.
643 */
dax_holder(struct dax_device * dax_dev)644 void *dax_holder(struct dax_device *dax_dev)
645 {
646 return dax_dev->holder_data;
647 }
648 EXPORT_SYMBOL_GPL(dax_holder);
649
650 /**
651 * inode_dax: convert a public inode into its dax_dev
652 * @inode: An inode with i_cdev pointing to a dax_dev
653 *
654 * Note this is not equivalent to to_dax_dev() which is for private
655 * internal use where we know the inode filesystem type == dax_fs_type.
656 */
inode_dax(struct inode * inode)657 struct dax_device *inode_dax(struct inode *inode)
658 {
659 struct cdev *cdev = inode->i_cdev;
660
661 return container_of(cdev, struct dax_device, cdev);
662 }
663 EXPORT_SYMBOL_GPL(inode_dax);
664
dax_inode(struct dax_device * dax_dev)665 struct inode *dax_inode(struct dax_device *dax_dev)
666 {
667 return &dax_dev->inode;
668 }
669 EXPORT_SYMBOL_GPL(dax_inode);
670
dax_get_private(struct dax_device * dax_dev)671 void *dax_get_private(struct dax_device *dax_dev)
672 {
673 if (!test_bit(DAXDEV_ALIVE, &dax_dev->flags))
674 return NULL;
675 return dax_dev->private;
676 }
677 EXPORT_SYMBOL_GPL(dax_get_private);
678
init_once(void * _dax_dev)679 static void init_once(void *_dax_dev)
680 {
681 struct dax_device *dax_dev = _dax_dev;
682 struct inode *inode = &dax_dev->inode;
683
684 memset(dax_dev, 0, sizeof(*dax_dev));
685 inode_init_once(inode);
686 }
687
dax_fs_init(void)688 static int dax_fs_init(void)
689 {
690 int rc;
691
692 dax_cache = kmem_cache_create("dax_cache", sizeof(struct dax_device), 0,
693 SLAB_HWCACHE_ALIGN | SLAB_RECLAIM_ACCOUNT | SLAB_ACCOUNT,
694 init_once);
695 if (!dax_cache)
696 return -ENOMEM;
697
698 dax_mnt = kern_mount(&dax_fs_type);
699 if (IS_ERR(dax_mnt)) {
700 rc = PTR_ERR(dax_mnt);
701 goto err_mount;
702 }
703 dax_superblock = dax_mnt->mnt_sb;
704
705 return 0;
706
707 err_mount:
708 kmem_cache_destroy(dax_cache);
709
710 return rc;
711 }
712
dax_fs_exit(void)713 static void dax_fs_exit(void)
714 {
715 kern_unmount(dax_mnt);
716 rcu_barrier();
717 kmem_cache_destroy(dax_cache);
718 }
719
dax_core_init(void)720 static int __init dax_core_init(void)
721 {
722 int rc;
723
724 rc = dax_fs_init();
725 if (rc)
726 return rc;
727
728 rc = alloc_chrdev_region(&dax_devt, 0, MINORMASK+1, "dax");
729 if (rc)
730 goto err_chrdev;
731
732 rc = dax_bus_init();
733 if (rc)
734 goto err_bus;
735 return 0;
736
737 err_bus:
738 unregister_chrdev_region(dax_devt, MINORMASK+1);
739 err_chrdev:
740 dax_fs_exit();
741 return 0;
742 }
743
dax_core_exit(void)744 static void __exit dax_core_exit(void)
745 {
746 dax_bus_exit();
747 unregister_chrdev_region(dax_devt, MINORMASK+1);
748 ida_destroy(&dax_minor_ida);
749 dax_fs_exit();
750 }
751
752 MODULE_AUTHOR("Intel Corporation");
753 MODULE_DESCRIPTION("DAX: direct access to differentiated memory");
754 MODULE_LICENSE("GPL v2");
755 subsys_initcall(dax_core_init);
756 module_exit(dax_core_exit);
757