Commit | Line | Data |
---|---|---|
7b6be844 DW |
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> | |
ef510424 | 17 | #include <linux/genhd.h> |
569d0365 | 18 | #include <linux/pfn_t.h> |
7b6be844 DW |
19 | #include <linux/cdev.h> |
20 | #include <linux/hash.h> | |
21 | #include <linux/slab.h> | |
7e026c8c | 22 | #include <linux/uio.h> |
6568b08b | 23 | #include <linux/dax.h> |
7b6be844 DW |
24 | #include <linux/fs.h> |
25 | ||
7b6be844 DW |
26 | static dev_t dax_devt; |
27 | DEFINE_STATIC_SRCU(dax_srcu); | |
28 | static struct vfsmount *dax_mnt; | |
29 | static DEFINE_IDA(dax_minor_ida); | |
30 | static struct kmem_cache *dax_cache __read_mostly; | |
31 | static struct super_block *dax_superblock __read_mostly; | |
32 | ||
72058005 DW |
33 | #define DAX_HASH_SIZE (PAGE_SIZE / sizeof(struct hlist_head)) |
34 | static struct hlist_head dax_host_list[DAX_HASH_SIZE]; | |
35 | static DEFINE_SPINLOCK(dax_host_lock); | |
36 | ||
7b6be844 DW |
37 | int dax_read_lock(void) |
38 | { | |
39 | return srcu_read_lock(&dax_srcu); | |
40 | } | |
41 | EXPORT_SYMBOL_GPL(dax_read_lock); | |
42 | ||
43 | void dax_read_unlock(int id) | |
44 | { | |
45 | srcu_read_unlock(&dax_srcu, id); | |
46 | } | |
47 | EXPORT_SYMBOL_GPL(dax_read_unlock); | |
48 | ||
9d109081 | 49 | #ifdef CONFIG_BLOCK |
78f35473 DW |
50 | #include <linux/blkdev.h> |
51 | ||
ef510424 DW |
52 | int bdev_dax_pgoff(struct block_device *bdev, sector_t sector, size_t size, |
53 | pgoff_t *pgoff) | |
54 | { | |
55 | phys_addr_t phys_off = (get_start_sect(bdev) + sector) * 512; | |
56 | ||
57 | if (pgoff) | |
58 | *pgoff = PHYS_PFN(phys_off); | |
59 | if (phys_off % PAGE_SIZE || size % PAGE_SIZE) | |
60 | return -EINVAL; | |
61 | return 0; | |
62 | } | |
63 | EXPORT_SYMBOL(bdev_dax_pgoff); | |
64 | ||
26f2f4de | 65 | #if IS_ENABLED(CONFIG_FS_DAX) |
78f35473 DW |
66 | struct dax_device *fs_dax_get_by_bdev(struct block_device *bdev) |
67 | { | |
68 | if (!blk_queue_dax(bdev->bd_queue)) | |
69 | return NULL; | |
70 | return fs_dax_get_by_host(bdev->bd_disk->disk_name); | |
71 | } | |
72 | EXPORT_SYMBOL_GPL(fs_dax_get_by_bdev); | |
26f2f4de | 73 | #endif |
78f35473 | 74 | |
ef510424 DW |
75 | /** |
76 | * __bdev_dax_supported() - Check if the device supports dax for filesystem | |
77 | * @sb: The superblock of the device | |
78 | * @blocksize: The block size of the device | |
79 | * | |
80 | * This is a library function for filesystems to check if the block device | |
81 | * can be mounted with dax option. | |
82 | * | |
83 | * Return: negative errno if unsupported, 0 if supported. | |
84 | */ | |
85 | int __bdev_dax_supported(struct super_block *sb, int blocksize) | |
86 | { | |
87 | struct block_device *bdev = sb->s_bdev; | |
88 | struct dax_device *dax_dev; | |
e7638488 | 89 | bool dax_enabled = false; |
ef510424 DW |
90 | pgoff_t pgoff; |
91 | int err, id; | |
92 | void *kaddr; | |
93 | pfn_t pfn; | |
94 | long len; | |
95 | ||
96 | if (blocksize != PAGE_SIZE) { | |
66a86cc1 | 97 | pr_debug("VFS (%s): error: unsupported blocksize for dax\n", |
ef510424 DW |
98 | sb->s_id); |
99 | return -EINVAL; | |
100 | } | |
101 | ||
102 | err = bdev_dax_pgoff(bdev, 0, PAGE_SIZE, &pgoff); | |
103 | if (err) { | |
66a86cc1 | 104 | pr_debug("VFS (%s): error: unaligned partition for dax\n", |
ef510424 DW |
105 | sb->s_id); |
106 | return err; | |
107 | } | |
108 | ||
109 | dax_dev = dax_get_by_host(bdev->bd_disk->disk_name); | |
110 | if (!dax_dev) { | |
66a86cc1 | 111 | pr_debug("VFS (%s): error: device does not support dax\n", |
ef510424 DW |
112 | sb->s_id); |
113 | return -EOPNOTSUPP; | |
114 | } | |
115 | ||
116 | id = dax_read_lock(); | |
117 | len = dax_direct_access(dax_dev, pgoff, 1, &kaddr, &pfn); | |
118 | dax_read_unlock(id); | |
119 | ||
120 | put_dax(dax_dev); | |
121 | ||
122 | if (len < 1) { | |
66a86cc1 | 123 | pr_debug("VFS (%s): error: dax access failed (%ld)\n", |
ef510424 DW |
124 | sb->s_id, len); |
125 | return len < 0 ? len : -EIO; | |
126 | } | |
127 | ||
3fe0791c DW |
128 | if (IS_ENABLED(CONFIG_FS_DAX_LIMITED) && pfn_t_special(pfn)) { |
129 | /* | |
130 | * An arch that has enabled the pmem api should also | |
131 | * have its drivers support pfn_t_devmap() | |
132 | * | |
133 | * This is a developer warning and should not trigger in | |
134 | * production. dax_flush() will crash since it depends | |
135 | * on being able to do (page_address(pfn_to_page())). | |
136 | */ | |
137 | WARN_ON(IS_ENABLED(CONFIG_ARCH_HAS_PMEM_API)); | |
e7638488 | 138 | dax_enabled = true; |
3fe0791c | 139 | } else if (pfn_t_devmap(pfn)) { |
e7638488 DW |
140 | struct dev_pagemap *pgmap; |
141 | ||
142 | pgmap = get_dev_pagemap(pfn_t_to_pfn(pfn), NULL); | |
143 | if (pgmap && pgmap->type == MEMORY_DEVICE_FS_DAX) | |
144 | dax_enabled = true; | |
145 | put_dev_pagemap(pgmap); | |
146 | } | |
147 | ||
148 | if (!dax_enabled) { | |
569d0365 DW |
149 | pr_debug("VFS (%s): error: dax support not enabled\n", |
150 | sb->s_id); | |
151 | return -EOPNOTSUPP; | |
152 | } | |
ef510424 DW |
153 | return 0; |
154 | } | |
155 | EXPORT_SYMBOL_GPL(__bdev_dax_supported); | |
9d109081 | 156 | #endif |
ef510424 | 157 | |
9a60c3ef DW |
158 | enum dax_device_flags { |
159 | /* !alive + rcu grace period == no new operations / mappings */ | |
160 | DAXDEV_ALIVE, | |
6e0c90d6 DW |
161 | /* gate whether dax_flush() calls the low level flush routine */ |
162 | DAXDEV_WRITE_CACHE, | |
9a60c3ef DW |
163 | }; |
164 | ||
7b6be844 DW |
165 | /** |
166 | * struct dax_device - anchor object for dax services | |
167 | * @inode: core vfs | |
168 | * @cdev: optional character interface for "device dax" | |
72058005 | 169 | * @host: optional name for lookups where the device path is not available |
7b6be844 | 170 | * @private: dax driver private data |
9a60c3ef | 171 | * @flags: state and boolean properties |
7b6be844 DW |
172 | */ |
173 | struct dax_device { | |
72058005 | 174 | struct hlist_node list; |
7b6be844 DW |
175 | struct inode inode; |
176 | struct cdev cdev; | |
72058005 | 177 | const char *host; |
7b6be844 | 178 | void *private; |
9a60c3ef | 179 | unsigned long flags; |
6568b08b | 180 | const struct dax_operations *ops; |
7b6be844 DW |
181 | }; |
182 | ||
6e0c90d6 DW |
183 | static ssize_t write_cache_show(struct device *dev, |
184 | struct device_attribute *attr, char *buf) | |
185 | { | |
186 | struct dax_device *dax_dev = dax_get_by_host(dev_name(dev)); | |
187 | ssize_t rc; | |
188 | ||
189 | WARN_ON_ONCE(!dax_dev); | |
190 | if (!dax_dev) | |
191 | return -ENXIO; | |
192 | ||
193 | rc = sprintf(buf, "%d\n", !!test_bit(DAXDEV_WRITE_CACHE, | |
194 | &dax_dev->flags)); | |
195 | put_dax(dax_dev); | |
196 | return rc; | |
197 | } | |
198 | ||
199 | static ssize_t write_cache_store(struct device *dev, | |
200 | struct device_attribute *attr, const char *buf, size_t len) | |
201 | { | |
202 | bool write_cache; | |
203 | int rc = strtobool(buf, &write_cache); | |
204 | struct dax_device *dax_dev = dax_get_by_host(dev_name(dev)); | |
205 | ||
206 | WARN_ON_ONCE(!dax_dev); | |
207 | if (!dax_dev) | |
208 | return -ENXIO; | |
209 | ||
210 | if (rc) | |
211 | len = rc; | |
212 | else if (write_cache) | |
213 | set_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags); | |
214 | else | |
215 | clear_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags); | |
216 | ||
217 | put_dax(dax_dev); | |
218 | return len; | |
219 | } | |
220 | static DEVICE_ATTR_RW(write_cache); | |
221 | ||
222 | static umode_t dax_visible(struct kobject *kobj, struct attribute *a, int n) | |
223 | { | |
224 | struct device *dev = container_of(kobj, typeof(*dev), kobj); | |
225 | struct dax_device *dax_dev = dax_get_by_host(dev_name(dev)); | |
226 | ||
227 | WARN_ON_ONCE(!dax_dev); | |
228 | if (!dax_dev) | |
229 | return 0; | |
230 | ||
c3ca015f MP |
231 | #ifndef CONFIG_ARCH_HAS_PMEM_API |
232 | if (a == &dev_attr_write_cache.attr) | |
6e0c90d6 | 233 | return 0; |
c3ca015f | 234 | #endif |
6e0c90d6 DW |
235 | return a->mode; |
236 | } | |
237 | ||
238 | static struct attribute *dax_attributes[] = { | |
239 | &dev_attr_write_cache.attr, | |
240 | NULL, | |
241 | }; | |
242 | ||
243 | struct attribute_group dax_attribute_group = { | |
244 | .name = "dax", | |
245 | .attrs = dax_attributes, | |
246 | .is_visible = dax_visible, | |
247 | }; | |
248 | EXPORT_SYMBOL_GPL(dax_attribute_group); | |
249 | ||
b0686260 DW |
250 | /** |
251 | * dax_direct_access() - translate a device pgoff to an absolute pfn | |
252 | * @dax_dev: a dax_device instance representing the logical memory range | |
253 | * @pgoff: offset in pages from the start of the device to translate | |
254 | * @nr_pages: number of consecutive pages caller can handle relative to @pfn | |
255 | * @kaddr: output parameter that returns a virtual address mapping of pfn | |
256 | * @pfn: output parameter that returns an absolute pfn translation of @pgoff | |
257 | * | |
258 | * Return: negative errno if an error occurs, otherwise the number of | |
259 | * pages accessible at the device relative @pgoff. | |
260 | */ | |
261 | long dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff, long nr_pages, | |
262 | void **kaddr, pfn_t *pfn) | |
263 | { | |
264 | long avail; | |
265 | ||
b0686260 DW |
266 | if (!dax_dev) |
267 | return -EOPNOTSUPP; | |
268 | ||
269 | if (!dax_alive(dax_dev)) | |
270 | return -ENXIO; | |
271 | ||
272 | if (nr_pages < 0) | |
273 | return nr_pages; | |
274 | ||
275 | avail = dax_dev->ops->direct_access(dax_dev, pgoff, nr_pages, | |
276 | kaddr, pfn); | |
277 | if (!avail) | |
278 | return -ERANGE; | |
279 | return min(avail, nr_pages); | |
280 | } | |
281 | EXPORT_SYMBOL_GPL(dax_direct_access); | |
282 | ||
7e026c8c DW |
283 | size_t dax_copy_from_iter(struct dax_device *dax_dev, pgoff_t pgoff, void *addr, |
284 | size_t bytes, struct iov_iter *i) | |
285 | { | |
286 | if (!dax_alive(dax_dev)) | |
287 | return 0; | |
288 | ||
7e026c8c DW |
289 | return dax_dev->ops->copy_from_iter(dax_dev, pgoff, addr, bytes, i); |
290 | } | |
291 | EXPORT_SYMBOL_GPL(dax_copy_from_iter); | |
292 | ||
c3ca015f MP |
293 | #ifdef CONFIG_ARCH_HAS_PMEM_API |
294 | void arch_wb_cache_pmem(void *addr, size_t size); | |
295 | void dax_flush(struct dax_device *dax_dev, void *addr, size_t size) | |
abebfbe2 | 296 | { |
c3ca015f | 297 | if (unlikely(!test_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags))) |
6e0c90d6 DW |
298 | return; |
299 | ||
c3ca015f | 300 | arch_wb_cache_pmem(addr, size); |
abebfbe2 | 301 | } |
c3ca015f MP |
302 | #else |
303 | void dax_flush(struct dax_device *dax_dev, void *addr, size_t size) | |
304 | { | |
305 | } | |
306 | #endif | |
abebfbe2 DW |
307 | EXPORT_SYMBOL_GPL(dax_flush); |
308 | ||
6e0c90d6 DW |
309 | void dax_write_cache(struct dax_device *dax_dev, bool wc) |
310 | { | |
311 | if (wc) | |
312 | set_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags); | |
313 | else | |
314 | clear_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags); | |
315 | } | |
316 | EXPORT_SYMBOL_GPL(dax_write_cache); | |
317 | ||
273752c9 VG |
318 | bool dax_write_cache_enabled(struct dax_device *dax_dev) |
319 | { | |
320 | return test_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags); | |
321 | } | |
322 | EXPORT_SYMBOL_GPL(dax_write_cache_enabled); | |
323 | ||
7b6be844 DW |
324 | bool dax_alive(struct dax_device *dax_dev) |
325 | { | |
326 | lockdep_assert_held(&dax_srcu); | |
9a60c3ef | 327 | return test_bit(DAXDEV_ALIVE, &dax_dev->flags); |
7b6be844 DW |
328 | } |
329 | EXPORT_SYMBOL_GPL(dax_alive); | |
330 | ||
72058005 DW |
331 | static int dax_host_hash(const char *host) |
332 | { | |
333 | return hashlen_hash(hashlen_string("DAX", host)) % DAX_HASH_SIZE; | |
334 | } | |
335 | ||
7b6be844 DW |
336 | /* |
337 | * Note, rcu is not protecting the liveness of dax_dev, rcu is ensuring | |
338 | * that any fault handlers or operations that might have seen | |
339 | * dax_alive(), have completed. Any operations that start after | |
340 | * synchronize_srcu() has run will abort upon seeing !dax_alive(). | |
341 | */ | |
342 | void kill_dax(struct dax_device *dax_dev) | |
343 | { | |
344 | if (!dax_dev) | |
345 | return; | |
346 | ||
9a60c3ef | 347 | clear_bit(DAXDEV_ALIVE, &dax_dev->flags); |
72058005 | 348 | |
7b6be844 | 349 | synchronize_srcu(&dax_srcu); |
72058005 DW |
350 | |
351 | spin_lock(&dax_host_lock); | |
352 | hlist_del_init(&dax_dev->list); | |
353 | spin_unlock(&dax_host_lock); | |
354 | ||
7b6be844 DW |
355 | dax_dev->private = NULL; |
356 | } | |
357 | EXPORT_SYMBOL_GPL(kill_dax); | |
358 | ||
359 | static struct inode *dax_alloc_inode(struct super_block *sb) | |
360 | { | |
361 | struct dax_device *dax_dev; | |
b9d39d17 | 362 | struct inode *inode; |
7b6be844 DW |
363 | |
364 | dax_dev = kmem_cache_alloc(dax_cache, GFP_KERNEL); | |
9f586fff MP |
365 | if (!dax_dev) |
366 | return NULL; | |
367 | ||
b9d39d17 DW |
368 | inode = &dax_dev->inode; |
369 | inode->i_rdev = 0; | |
370 | return inode; | |
7b6be844 DW |
371 | } |
372 | ||
373 | static struct dax_device *to_dax_dev(struct inode *inode) | |
374 | { | |
375 | return container_of(inode, struct dax_device, inode); | |
376 | } | |
377 | ||
378 | static void dax_i_callback(struct rcu_head *head) | |
379 | { | |
380 | struct inode *inode = container_of(head, struct inode, i_rcu); | |
381 | struct dax_device *dax_dev = to_dax_dev(inode); | |
382 | ||
72058005 DW |
383 | kfree(dax_dev->host); |
384 | dax_dev->host = NULL; | |
b9d39d17 DW |
385 | if (inode->i_rdev) |
386 | ida_simple_remove(&dax_minor_ida, MINOR(inode->i_rdev)); | |
7b6be844 DW |
387 | kmem_cache_free(dax_cache, dax_dev); |
388 | } | |
389 | ||
390 | static void dax_destroy_inode(struct inode *inode) | |
391 | { | |
392 | struct dax_device *dax_dev = to_dax_dev(inode); | |
393 | ||
9a60c3ef | 394 | WARN_ONCE(test_bit(DAXDEV_ALIVE, &dax_dev->flags), |
7b6be844 DW |
395 | "kill_dax() must be called before final iput()\n"); |
396 | call_rcu(&inode->i_rcu, dax_i_callback); | |
397 | } | |
398 | ||
399 | static const struct super_operations dax_sops = { | |
400 | .statfs = simple_statfs, | |
401 | .alloc_inode = dax_alloc_inode, | |
402 | .destroy_inode = dax_destroy_inode, | |
403 | .drop_inode = generic_delete_inode, | |
404 | }; | |
405 | ||
406 | static struct dentry *dax_mount(struct file_system_type *fs_type, | |
407 | int flags, const char *dev_name, void *data) | |
408 | { | |
409 | return mount_pseudo(fs_type, "dax:", &dax_sops, NULL, DAXFS_MAGIC); | |
410 | } | |
411 | ||
412 | static struct file_system_type dax_fs_type = { | |
413 | .name = "dax", | |
414 | .mount = dax_mount, | |
415 | .kill_sb = kill_anon_super, | |
416 | }; | |
417 | ||
418 | static int dax_test(struct inode *inode, void *data) | |
419 | { | |
420 | dev_t devt = *(dev_t *) data; | |
421 | ||
422 | return inode->i_rdev == devt; | |
423 | } | |
424 | ||
425 | static int dax_set(struct inode *inode, void *data) | |
426 | { | |
427 | dev_t devt = *(dev_t *) data; | |
428 | ||
429 | inode->i_rdev = devt; | |
430 | return 0; | |
431 | } | |
432 | ||
433 | static struct dax_device *dax_dev_get(dev_t devt) | |
434 | { | |
435 | struct dax_device *dax_dev; | |
436 | struct inode *inode; | |
437 | ||
438 | inode = iget5_locked(dax_superblock, hash_32(devt + DAXFS_MAGIC, 31), | |
439 | dax_test, dax_set, &devt); | |
440 | ||
441 | if (!inode) | |
442 | return NULL; | |
443 | ||
444 | dax_dev = to_dax_dev(inode); | |
445 | if (inode->i_state & I_NEW) { | |
9a60c3ef | 446 | set_bit(DAXDEV_ALIVE, &dax_dev->flags); |
7b6be844 DW |
447 | inode->i_cdev = &dax_dev->cdev; |
448 | inode->i_mode = S_IFCHR; | |
449 | inode->i_flags = S_DAX; | |
450 | mapping_set_gfp_mask(&inode->i_data, GFP_USER); | |
451 | unlock_new_inode(inode); | |
452 | } | |
453 | ||
454 | return dax_dev; | |
455 | } | |
456 | ||
72058005 DW |
457 | static void dax_add_host(struct dax_device *dax_dev, const char *host) |
458 | { | |
459 | int hash; | |
460 | ||
461 | /* | |
462 | * Unconditionally init dax_dev since it's coming from a | |
463 | * non-zeroed slab cache | |
464 | */ | |
465 | INIT_HLIST_NODE(&dax_dev->list); | |
466 | dax_dev->host = host; | |
467 | if (!host) | |
468 | return; | |
469 | ||
470 | hash = dax_host_hash(host); | |
471 | spin_lock(&dax_host_lock); | |
472 | hlist_add_head(&dax_dev->list, &dax_host_list[hash]); | |
473 | spin_unlock(&dax_host_lock); | |
474 | } | |
475 | ||
6568b08b DW |
476 | struct dax_device *alloc_dax(void *private, const char *__host, |
477 | const struct dax_operations *ops) | |
7b6be844 DW |
478 | { |
479 | struct dax_device *dax_dev; | |
72058005 | 480 | const char *host; |
7b6be844 DW |
481 | dev_t devt; |
482 | int minor; | |
483 | ||
72058005 DW |
484 | host = kstrdup(__host, GFP_KERNEL); |
485 | if (__host && !host) | |
486 | return NULL; | |
487 | ||
cf1e2289 | 488 | minor = ida_simple_get(&dax_minor_ida, 0, MINORMASK+1, GFP_KERNEL); |
7b6be844 | 489 | if (minor < 0) |
72058005 | 490 | goto err_minor; |
7b6be844 DW |
491 | |
492 | devt = MKDEV(MAJOR(dax_devt), minor); | |
493 | dax_dev = dax_dev_get(devt); | |
494 | if (!dax_dev) | |
72058005 | 495 | goto err_dev; |
7b6be844 | 496 | |
72058005 | 497 | dax_add_host(dax_dev, host); |
6568b08b | 498 | dax_dev->ops = ops; |
7b6be844 DW |
499 | dax_dev->private = private; |
500 | return dax_dev; | |
501 | ||
72058005 | 502 | err_dev: |
7b6be844 | 503 | ida_simple_remove(&dax_minor_ida, minor); |
72058005 DW |
504 | err_minor: |
505 | kfree(host); | |
7b6be844 DW |
506 | return NULL; |
507 | } | |
508 | EXPORT_SYMBOL_GPL(alloc_dax); | |
509 | ||
510 | void put_dax(struct dax_device *dax_dev) | |
511 | { | |
512 | if (!dax_dev) | |
513 | return; | |
514 | iput(&dax_dev->inode); | |
515 | } | |
516 | EXPORT_SYMBOL_GPL(put_dax); | |
517 | ||
72058005 DW |
518 | /** |
519 | * dax_get_by_host() - temporary lookup mechanism for filesystem-dax | |
520 | * @host: alternate name for the device registered by a dax driver | |
521 | */ | |
522 | struct dax_device *dax_get_by_host(const char *host) | |
523 | { | |
524 | struct dax_device *dax_dev, *found = NULL; | |
525 | int hash, id; | |
526 | ||
527 | if (!host) | |
528 | return NULL; | |
529 | ||
530 | hash = dax_host_hash(host); | |
531 | ||
532 | id = dax_read_lock(); | |
533 | spin_lock(&dax_host_lock); | |
534 | hlist_for_each_entry(dax_dev, &dax_host_list[hash], list) { | |
535 | if (!dax_alive(dax_dev) | |
536 | || strcmp(host, dax_dev->host) != 0) | |
537 | continue; | |
538 | ||
539 | if (igrab(&dax_dev->inode)) | |
540 | found = dax_dev; | |
541 | break; | |
542 | } | |
543 | spin_unlock(&dax_host_lock); | |
544 | dax_read_unlock(id); | |
545 | ||
546 | return found; | |
547 | } | |
548 | EXPORT_SYMBOL_GPL(dax_get_by_host); | |
549 | ||
7b6be844 DW |
550 | /** |
551 | * inode_dax: convert a public inode into its dax_dev | |
552 | * @inode: An inode with i_cdev pointing to a dax_dev | |
553 | * | |
554 | * Note this is not equivalent to to_dax_dev() which is for private | |
555 | * internal use where we know the inode filesystem type == dax_fs_type. | |
556 | */ | |
557 | struct dax_device *inode_dax(struct inode *inode) | |
558 | { | |
559 | struct cdev *cdev = inode->i_cdev; | |
560 | ||
561 | return container_of(cdev, struct dax_device, cdev); | |
562 | } | |
563 | EXPORT_SYMBOL_GPL(inode_dax); | |
564 | ||
565 | struct inode *dax_inode(struct dax_device *dax_dev) | |
566 | { | |
567 | return &dax_dev->inode; | |
568 | } | |
569 | EXPORT_SYMBOL_GPL(dax_inode); | |
570 | ||
571 | void *dax_get_private(struct dax_device *dax_dev) | |
572 | { | |
573 | return dax_dev->private; | |
574 | } | |
575 | EXPORT_SYMBOL_GPL(dax_get_private); | |
576 | ||
577 | static void init_once(void *_dax_dev) | |
578 | { | |
579 | struct dax_device *dax_dev = _dax_dev; | |
580 | struct inode *inode = &dax_dev->inode; | |
581 | ||
b9d39d17 | 582 | memset(dax_dev, 0, sizeof(*dax_dev)); |
7b6be844 DW |
583 | inode_init_once(inode); |
584 | } | |
585 | ||
586 | static int __dax_fs_init(void) | |
587 | { | |
588 | int rc; | |
589 | ||
590 | dax_cache = kmem_cache_create("dax_cache", sizeof(struct dax_device), 0, | |
591 | (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT| | |
592 | SLAB_MEM_SPREAD|SLAB_ACCOUNT), | |
593 | init_once); | |
594 | if (!dax_cache) | |
595 | return -ENOMEM; | |
596 | ||
597 | rc = register_filesystem(&dax_fs_type); | |
598 | if (rc) | |
599 | goto err_register_fs; | |
600 | ||
601 | dax_mnt = kern_mount(&dax_fs_type); | |
602 | if (IS_ERR(dax_mnt)) { | |
603 | rc = PTR_ERR(dax_mnt); | |
604 | goto err_mount; | |
605 | } | |
606 | dax_superblock = dax_mnt->mnt_sb; | |
607 | ||
608 | return 0; | |
609 | ||
610 | err_mount: | |
611 | unregister_filesystem(&dax_fs_type); | |
612 | err_register_fs: | |
613 | kmem_cache_destroy(dax_cache); | |
614 | ||
615 | return rc; | |
616 | } | |
617 | ||
618 | static void __dax_fs_exit(void) | |
619 | { | |
620 | kern_unmount(dax_mnt); | |
621 | unregister_filesystem(&dax_fs_type); | |
622 | kmem_cache_destroy(dax_cache); | |
623 | } | |
624 | ||
625 | static int __init dax_fs_init(void) | |
626 | { | |
627 | int rc; | |
628 | ||
629 | rc = __dax_fs_init(); | |
630 | if (rc) | |
631 | return rc; | |
632 | ||
cf1e2289 | 633 | rc = alloc_chrdev_region(&dax_devt, 0, MINORMASK+1, "dax"); |
7b6be844 DW |
634 | if (rc) |
635 | __dax_fs_exit(); | |
636 | return rc; | |
637 | } | |
638 | ||
639 | static void __exit dax_fs_exit(void) | |
640 | { | |
cf1e2289 | 641 | unregister_chrdev_region(dax_devt, MINORMASK+1); |
7b6be844 DW |
642 | ida_destroy(&dax_minor_ida); |
643 | __dax_fs_exit(); | |
644 | } | |
645 | ||
646 | MODULE_AUTHOR("Intel Corporation"); | |
647 | MODULE_LICENSE("GPL v2"); | |
648 | subsys_initcall(dax_fs_init); | |
649 | module_exit(dax_fs_exit); |