xref: /linux/fs/ocfs2/dlmfs/dlmfs.c (revision 2ca23ae59e997f1b6df7662d8ef10d9c471b46d0)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* -*- mode: c; c-basic-offset: 8; -*-
3  * vim: noexpandtab sw=8 ts=8 sts=0:
4  *
5  * dlmfs.c
6  *
7  * Code which implements the kernel side of a minimal userspace
8  * interface to our DLM. This file handles the virtual file system
9  * used for communication with userspace. Credit should go to ramfs,
10  * which was a template for the fs side of this module.
11  *
12  * Copyright (C) 2003, 2004 Oracle.  All rights reserved.
13  */
14 
15 /* Simple VFS hooks based on: */
16 /*
17  * Resizable simple ram filesystem for Linux.
18  *
19  * Copyright (C) 2000 Linus Torvalds.
20  *               2000 Transmeta Corp.
21  */
22 
23 #include <linux/module.h>
24 #include <linux/fs.h>
25 #include <linux/pagemap.h>
26 #include <linux/types.h>
27 #include <linux/slab.h>
28 #include <linux/highmem.h>
29 #include <linux/init.h>
30 #include <linux/string.h>
31 #include <linux/backing-dev.h>
32 #include <linux/poll.h>
33 
34 #include <linux/uaccess.h>
35 
36 #include "../stackglue.h"
37 #include "userdlm.h"
38 
39 #define MLOG_MASK_PREFIX ML_DLMFS
40 #include "../cluster/masklog.h"
41 
42 
43 static const struct super_operations dlmfs_ops;
44 static const struct file_operations dlmfs_file_operations;
45 static const struct inode_operations dlmfs_dir_inode_operations;
46 static const struct inode_operations dlmfs_root_inode_operations;
47 static const struct inode_operations dlmfs_file_inode_operations;
48 static struct kmem_cache *dlmfs_inode_cache;
49 
50 struct workqueue_struct *user_dlm_worker;
51 
52 
53 
54 /*
55  * These are the ABI capabilities of dlmfs.
56  *
57  * Over time, dlmfs has added some features that were not part of the
58  * initial ABI.  Unfortunately, some of these features are not detectable
59  * via standard usage.  For example, Linux's default poll always returns
60  * EPOLLIN, so there is no way for a caller of poll(2) to know when dlmfs
61  * added poll support.  Instead, we provide this list of new capabilities.
62  *
63  * Capabilities is a read-only attribute.  We do it as a module parameter
64  * so we can discover it whether dlmfs is built in, loaded, or even not
65  * loaded.
66  *
67  * The ABI features are local to this machine's dlmfs mount.  This is
68  * distinct from the locking protocol, which is concerned with inter-node
69  * interaction.
70  *
71  * Capabilities:
72  * - bast	: EPOLLIN against the file descriptor of a held lock
73  *		  signifies a bast fired on the lock.
74  */
75 #define DLMFS_CAPABILITIES "bast stackglue"
76 static int param_set_dlmfs_capabilities(const char *val,
77 					const struct kernel_param *kp)
78 {
79 	printk(KERN_ERR "%s: readonly parameter\n", kp->name);
80 	return -EINVAL;
81 }
82 static int param_get_dlmfs_capabilities(char *buffer,
83 					const struct kernel_param *kp)
84 {
85 	return strlcpy(buffer, DLMFS_CAPABILITIES,
86 		       strlen(DLMFS_CAPABILITIES) + 1);
87 }
88 module_param_call(capabilities, param_set_dlmfs_capabilities,
89 		  param_get_dlmfs_capabilities, NULL, 0444);
90 MODULE_PARM_DESC(capabilities, DLMFS_CAPABILITIES);
91 
92 
93 /*
94  * decodes a set of open flags into a valid lock level and a set of flags.
95  * returns < 0 if we have invalid flags
96  * flags which mean something to us:
97  * O_RDONLY -> PRMODE level
98  * O_WRONLY -> EXMODE level
99  *
100  * O_NONBLOCK -> NOQUEUE
101  */
102 static int dlmfs_decode_open_flags(int open_flags,
103 				   int *level,
104 				   int *flags)
105 {
106 	if (open_flags & (O_WRONLY|O_RDWR))
107 		*level = DLM_LOCK_EX;
108 	else
109 		*level = DLM_LOCK_PR;
110 
111 	*flags = 0;
112 	if (open_flags & O_NONBLOCK)
113 		*flags |= DLM_LKF_NOQUEUE;
114 
115 	return 0;
116 }
117 
118 static int dlmfs_file_open(struct inode *inode,
119 			   struct file *file)
120 {
121 	int status, level, flags;
122 	struct dlmfs_filp_private *fp = NULL;
123 	struct dlmfs_inode_private *ip;
124 
125 	if (S_ISDIR(inode->i_mode))
126 		BUG();
127 
128 	mlog(0, "open called on inode %lu, flags 0x%x\n", inode->i_ino,
129 		file->f_flags);
130 
131 	status = dlmfs_decode_open_flags(file->f_flags, &level, &flags);
132 	if (status < 0)
133 		goto bail;
134 
135 	/* We don't want to honor O_APPEND at read/write time as it
136 	 * doesn't make sense for LVB writes. */
137 	file->f_flags &= ~O_APPEND;
138 
139 	fp = kmalloc(sizeof(*fp), GFP_NOFS);
140 	if (!fp) {
141 		status = -ENOMEM;
142 		goto bail;
143 	}
144 	fp->fp_lock_level = level;
145 
146 	ip = DLMFS_I(inode);
147 
148 	status = user_dlm_cluster_lock(&ip->ip_lockres, level, flags);
149 	if (status < 0) {
150 		/* this is a strange error to return here but I want
151 		 * to be able userspace to be able to distinguish a
152 		 * valid lock request from one that simply couldn't be
153 		 * granted. */
154 		if (flags & DLM_LKF_NOQUEUE && status == -EAGAIN)
155 			status = -ETXTBSY;
156 		kfree(fp);
157 		goto bail;
158 	}
159 
160 	file->private_data = fp;
161 bail:
162 	return status;
163 }
164 
165 static int dlmfs_file_release(struct inode *inode,
166 			      struct file *file)
167 {
168 	int level;
169 	struct dlmfs_inode_private *ip = DLMFS_I(inode);
170 	struct dlmfs_filp_private *fp = file->private_data;
171 
172 	if (S_ISDIR(inode->i_mode))
173 		BUG();
174 
175 	mlog(0, "close called on inode %lu\n", inode->i_ino);
176 
177 	if (fp) {
178 		level = fp->fp_lock_level;
179 		if (level != DLM_LOCK_IV)
180 			user_dlm_cluster_unlock(&ip->ip_lockres, level);
181 
182 		kfree(fp);
183 		file->private_data = NULL;
184 	}
185 
186 	return 0;
187 }
188 
189 /*
190  * We do ->setattr() just to override size changes.  Our size is the size
191  * of the LVB and nothing else.
192  */
193 static int dlmfs_file_setattr(struct dentry *dentry, struct iattr *attr)
194 {
195 	int error;
196 	struct inode *inode = d_inode(dentry);
197 
198 	attr->ia_valid &= ~ATTR_SIZE;
199 	error = setattr_prepare(dentry, attr);
200 	if (error)
201 		return error;
202 
203 	setattr_copy(inode, attr);
204 	mark_inode_dirty(inode);
205 	return 0;
206 }
207 
208 static __poll_t dlmfs_file_poll(struct file *file, poll_table *wait)
209 {
210 	__poll_t event = 0;
211 	struct inode *inode = file_inode(file);
212 	struct dlmfs_inode_private *ip = DLMFS_I(inode);
213 
214 	poll_wait(file, &ip->ip_lockres.l_event, wait);
215 
216 	spin_lock(&ip->ip_lockres.l_lock);
217 	if (ip->ip_lockres.l_flags & USER_LOCK_BLOCKED)
218 		event = EPOLLIN | EPOLLRDNORM;
219 	spin_unlock(&ip->ip_lockres.l_lock);
220 
221 	return event;
222 }
223 
224 static ssize_t dlmfs_file_read(struct file *filp,
225 			       char __user *buf,
226 			       size_t count,
227 			       loff_t *ppos)
228 {
229 	int bytes_left;
230 	ssize_t readlen, got;
231 	char *lvb_buf;
232 	struct inode *inode = file_inode(filp);
233 
234 	mlog(0, "inode %lu, count = %zu, *ppos = %llu\n",
235 		inode->i_ino, count, *ppos);
236 
237 	if (*ppos >= i_size_read(inode))
238 		return 0;
239 
240 	if (!count)
241 		return 0;
242 
243 	if (!access_ok(buf, count))
244 		return -EFAULT;
245 
246 	/* don't read past the lvb */
247 	if ((count + *ppos) > i_size_read(inode))
248 		readlen = i_size_read(inode) - *ppos;
249 	else
250 		readlen = count;
251 
252 	lvb_buf = kmalloc(readlen, GFP_NOFS);
253 	if (!lvb_buf)
254 		return -ENOMEM;
255 
256 	got = user_dlm_read_lvb(inode, lvb_buf, readlen);
257 	if (got) {
258 		BUG_ON(got != readlen);
259 		bytes_left = __copy_to_user(buf, lvb_buf, readlen);
260 		readlen -= bytes_left;
261 	} else
262 		readlen = 0;
263 
264 	kfree(lvb_buf);
265 
266 	*ppos = *ppos + readlen;
267 
268 	mlog(0, "read %zd bytes\n", readlen);
269 	return readlen;
270 }
271 
272 static ssize_t dlmfs_file_write(struct file *filp,
273 				const char __user *buf,
274 				size_t count,
275 				loff_t *ppos)
276 {
277 	int bytes_left;
278 	char *lvb_buf;
279 	struct inode *inode = file_inode(filp);
280 
281 	mlog(0, "inode %lu, count = %zu, *ppos = %llu\n",
282 		inode->i_ino, count, *ppos);
283 
284 	if (*ppos >= i_size_read(inode))
285 		return -ENOSPC;
286 
287 	/* don't write past the lvb */
288 	if (count > i_size_read(inode) - *ppos)
289 		count = i_size_read(inode) - *ppos;
290 
291 	if (!count)
292 		return 0;
293 
294 	if (!access_ok(buf, count))
295 		return -EFAULT;
296 
297 	lvb_buf = kmalloc(count, GFP_NOFS);
298 	if (!lvb_buf)
299 		return -ENOMEM;
300 
301 	bytes_left = copy_from_user(lvb_buf, buf, count);
302 	count -= bytes_left;
303 	if (count)
304 		user_dlm_write_lvb(inode, lvb_buf, count);
305 
306 	kfree(lvb_buf);
307 
308 	*ppos = *ppos + count;
309 	mlog(0, "wrote %zu bytes\n", count);
310 	return count;
311 }
312 
313 static void dlmfs_init_once(void *foo)
314 {
315 	struct dlmfs_inode_private *ip =
316 		(struct dlmfs_inode_private *) foo;
317 
318 	ip->ip_conn = NULL;
319 	ip->ip_parent = NULL;
320 
321 	inode_init_once(&ip->ip_vfs_inode);
322 }
323 
324 static struct inode *dlmfs_alloc_inode(struct super_block *sb)
325 {
326 	struct dlmfs_inode_private *ip;
327 
328 	ip = kmem_cache_alloc(dlmfs_inode_cache, GFP_NOFS);
329 	if (!ip)
330 		return NULL;
331 
332 	return &ip->ip_vfs_inode;
333 }
334 
335 static void dlmfs_free_inode(struct inode *inode)
336 {
337 	kmem_cache_free(dlmfs_inode_cache, DLMFS_I(inode));
338 }
339 
340 static void dlmfs_evict_inode(struct inode *inode)
341 {
342 	int status;
343 	struct dlmfs_inode_private *ip;
344 
345 	clear_inode(inode);
346 
347 	mlog(0, "inode %lu\n", inode->i_ino);
348 
349 	ip = DLMFS_I(inode);
350 
351 	if (S_ISREG(inode->i_mode)) {
352 		status = user_dlm_destroy_lock(&ip->ip_lockres);
353 		if (status < 0)
354 			mlog_errno(status);
355 		iput(ip->ip_parent);
356 		goto clear_fields;
357 	}
358 
359 	mlog(0, "we're a directory, ip->ip_conn = 0x%p\n", ip->ip_conn);
360 	/* we must be a directory. If required, lets unregister the
361 	 * dlm context now. */
362 	if (ip->ip_conn)
363 		user_dlm_unregister(ip->ip_conn);
364 clear_fields:
365 	ip->ip_parent = NULL;
366 	ip->ip_conn = NULL;
367 }
368 
369 static struct inode *dlmfs_get_root_inode(struct super_block *sb)
370 {
371 	struct inode *inode = new_inode(sb);
372 	umode_t mode = S_IFDIR | 0755;
373 
374 	if (inode) {
375 		inode->i_ino = get_next_ino();
376 		inode_init_owner(inode, NULL, mode);
377 		inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
378 		inc_nlink(inode);
379 
380 		inode->i_fop = &simple_dir_operations;
381 		inode->i_op = &dlmfs_root_inode_operations;
382 	}
383 
384 	return inode;
385 }
386 
387 static struct inode *dlmfs_get_inode(struct inode *parent,
388 				     struct dentry *dentry,
389 				     umode_t mode)
390 {
391 	struct super_block *sb = parent->i_sb;
392 	struct inode * inode = new_inode(sb);
393 	struct dlmfs_inode_private *ip;
394 
395 	if (!inode)
396 		return NULL;
397 
398 	inode->i_ino = get_next_ino();
399 	inode_init_owner(inode, parent, mode);
400 	inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
401 
402 	ip = DLMFS_I(inode);
403 	ip->ip_conn = DLMFS_I(parent)->ip_conn;
404 
405 	switch (mode & S_IFMT) {
406 	default:
407 		/* for now we don't support anything other than
408 		 * directories and regular files. */
409 		BUG();
410 		break;
411 	case S_IFREG:
412 		inode->i_op = &dlmfs_file_inode_operations;
413 		inode->i_fop = &dlmfs_file_operations;
414 
415 		i_size_write(inode,  DLM_LVB_LEN);
416 
417 		user_dlm_lock_res_init(&ip->ip_lockres, dentry);
418 
419 		/* released at clear_inode time, this insures that we
420 		 * get to drop the dlm reference on each lock *before*
421 		 * we call the unregister code for releasing parent
422 		 * directories. */
423 		ip->ip_parent = igrab(parent);
424 		BUG_ON(!ip->ip_parent);
425 		break;
426 	case S_IFDIR:
427 		inode->i_op = &dlmfs_dir_inode_operations;
428 		inode->i_fop = &simple_dir_operations;
429 
430 		/* directory inodes start off with i_nlink ==
431 		 * 2 (for "." entry) */
432 		inc_nlink(inode);
433 		break;
434 	}
435 	return inode;
436 }
437 
438 /*
439  * File creation. Allocate an inode, and we're done..
440  */
441 /* SMP-safe */
442 static int dlmfs_mkdir(struct inode * dir,
443 		       struct dentry * dentry,
444 		       umode_t mode)
445 {
446 	int status;
447 	struct inode *inode = NULL;
448 	const struct qstr *domain = &dentry->d_name;
449 	struct dlmfs_inode_private *ip;
450 	struct ocfs2_cluster_connection *conn;
451 
452 	mlog(0, "mkdir %.*s\n", domain->len, domain->name);
453 
454 	/* verify that we have a proper domain */
455 	if (domain->len >= GROUP_NAME_MAX) {
456 		status = -EINVAL;
457 		mlog(ML_ERROR, "invalid domain name for directory.\n");
458 		goto bail;
459 	}
460 
461 	inode = dlmfs_get_inode(dir, dentry, mode | S_IFDIR);
462 	if (!inode) {
463 		status = -ENOMEM;
464 		mlog_errno(status);
465 		goto bail;
466 	}
467 
468 	ip = DLMFS_I(inode);
469 
470 	conn = user_dlm_register(domain);
471 	if (IS_ERR(conn)) {
472 		status = PTR_ERR(conn);
473 		mlog(ML_ERROR, "Error %d could not register domain \"%.*s\"\n",
474 		     status, domain->len, domain->name);
475 		goto bail;
476 	}
477 	ip->ip_conn = conn;
478 
479 	inc_nlink(dir);
480 	d_instantiate(dentry, inode);
481 	dget(dentry);	/* Extra count - pin the dentry in core */
482 
483 	status = 0;
484 bail:
485 	if (status < 0)
486 		iput(inode);
487 	return status;
488 }
489 
490 static int dlmfs_create(struct inode *dir,
491 			struct dentry *dentry,
492 			umode_t mode,
493 			bool excl)
494 {
495 	int status = 0;
496 	struct inode *inode;
497 	const struct qstr *name = &dentry->d_name;
498 
499 	mlog(0, "create %.*s\n", name->len, name->name);
500 
501 	/* verify name is valid and doesn't contain any dlm reserved
502 	 * characters */
503 	if (name->len >= USER_DLM_LOCK_ID_MAX_LEN ||
504 	    name->name[0] == '$') {
505 		status = -EINVAL;
506 		mlog(ML_ERROR, "invalid lock name, %.*s\n", name->len,
507 		     name->name);
508 		goto bail;
509 	}
510 
511 	inode = dlmfs_get_inode(dir, dentry, mode | S_IFREG);
512 	if (!inode) {
513 		status = -ENOMEM;
514 		mlog_errno(status);
515 		goto bail;
516 	}
517 
518 	d_instantiate(dentry, inode);
519 	dget(dentry);	/* Extra count - pin the dentry in core */
520 bail:
521 	return status;
522 }
523 
524 static int dlmfs_unlink(struct inode *dir,
525 			struct dentry *dentry)
526 {
527 	int status;
528 	struct inode *inode = d_inode(dentry);
529 
530 	mlog(0, "unlink inode %lu\n", inode->i_ino);
531 
532 	/* if there are no current holders, or none that are waiting
533 	 * to acquire a lock, this basically destroys our lockres. */
534 	status = user_dlm_destroy_lock(&DLMFS_I(inode)->ip_lockres);
535 	if (status < 0) {
536 		mlog(ML_ERROR, "unlink %pd, error %d from destroy\n",
537 		     dentry, status);
538 		goto bail;
539 	}
540 	status = simple_unlink(dir, dentry);
541 bail:
542 	return status;
543 }
544 
545 static int dlmfs_fill_super(struct super_block * sb,
546 			    void * data,
547 			    int silent)
548 {
549 	sb->s_maxbytes = MAX_LFS_FILESIZE;
550 	sb->s_blocksize = PAGE_SIZE;
551 	sb->s_blocksize_bits = PAGE_SHIFT;
552 	sb->s_magic = DLMFS_MAGIC;
553 	sb->s_op = &dlmfs_ops;
554 	sb->s_root = d_make_root(dlmfs_get_root_inode(sb));
555 	if (!sb->s_root)
556 		return -ENOMEM;
557 	return 0;
558 }
559 
560 static const struct file_operations dlmfs_file_operations = {
561 	.open		= dlmfs_file_open,
562 	.release	= dlmfs_file_release,
563 	.poll		= dlmfs_file_poll,
564 	.read		= dlmfs_file_read,
565 	.write		= dlmfs_file_write,
566 	.llseek		= default_llseek,
567 };
568 
569 static const struct inode_operations dlmfs_dir_inode_operations = {
570 	.create		= dlmfs_create,
571 	.lookup		= simple_lookup,
572 	.unlink		= dlmfs_unlink,
573 };
574 
575 /* this way we can restrict mkdir to only the toplevel of the fs. */
576 static const struct inode_operations dlmfs_root_inode_operations = {
577 	.lookup		= simple_lookup,
578 	.mkdir		= dlmfs_mkdir,
579 	.rmdir		= simple_rmdir,
580 };
581 
582 static const struct super_operations dlmfs_ops = {
583 	.statfs		= simple_statfs,
584 	.alloc_inode	= dlmfs_alloc_inode,
585 	.free_inode	= dlmfs_free_inode,
586 	.evict_inode	= dlmfs_evict_inode,
587 	.drop_inode	= generic_delete_inode,
588 };
589 
590 static const struct inode_operations dlmfs_file_inode_operations = {
591 	.getattr	= simple_getattr,
592 	.setattr	= dlmfs_file_setattr,
593 };
594 
595 static struct dentry *dlmfs_mount(struct file_system_type *fs_type,
596 	int flags, const char *dev_name, void *data)
597 {
598 	return mount_nodev(fs_type, flags, data, dlmfs_fill_super);
599 }
600 
601 static struct file_system_type dlmfs_fs_type = {
602 	.owner		= THIS_MODULE,
603 	.name		= "ocfs2_dlmfs",
604 	.mount		= dlmfs_mount,
605 	.kill_sb	= kill_litter_super,
606 };
607 MODULE_ALIAS_FS("ocfs2_dlmfs");
608 
609 static int __init init_dlmfs_fs(void)
610 {
611 	int status;
612 	int cleanup_inode = 0, cleanup_worker = 0;
613 
614 	dlmfs_inode_cache = kmem_cache_create("dlmfs_inode_cache",
615 				sizeof(struct dlmfs_inode_private),
616 				0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
617 					SLAB_MEM_SPREAD|SLAB_ACCOUNT),
618 				dlmfs_init_once);
619 	if (!dlmfs_inode_cache) {
620 		status = -ENOMEM;
621 		goto bail;
622 	}
623 	cleanup_inode = 1;
624 
625 	user_dlm_worker = alloc_workqueue("user_dlm", WQ_MEM_RECLAIM, 0);
626 	if (!user_dlm_worker) {
627 		status = -ENOMEM;
628 		goto bail;
629 	}
630 	cleanup_worker = 1;
631 
632 	user_dlm_set_locking_protocol();
633 	status = register_filesystem(&dlmfs_fs_type);
634 bail:
635 	if (status) {
636 		if (cleanup_inode)
637 			kmem_cache_destroy(dlmfs_inode_cache);
638 		if (cleanup_worker)
639 			destroy_workqueue(user_dlm_worker);
640 	} else
641 		printk("OCFS2 User DLM kernel interface loaded\n");
642 	return status;
643 }
644 
645 static void __exit exit_dlmfs_fs(void)
646 {
647 	unregister_filesystem(&dlmfs_fs_type);
648 
649 	destroy_workqueue(user_dlm_worker);
650 
651 	/*
652 	 * Make sure all delayed rcu free inodes are flushed before we
653 	 * destroy cache.
654 	 */
655 	rcu_barrier();
656 	kmem_cache_destroy(dlmfs_inode_cache);
657 
658 }
659 
660 MODULE_AUTHOR("Oracle");
661 MODULE_LICENSE("GPL");
662 MODULE_DESCRIPTION("OCFS2 DLM-Filesystem");
663 
664 module_init(init_dlmfs_fs)
665 module_exit(exit_dlmfs_fs)
666