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