1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Code which implements the kernel side of a minimal userspace
6 * interface to our DLM. This file handles the virtual file system
7 * used for communication with userspace. Credit should go to ramfs,
8 * which was a template for the fs side of this module.
10 * Copyright (C) 2003, 2004 Oracle. All rights reserved.
13 /* Simple VFS hooks based on: */
15 * Resizable simple ram filesystem for Linux.
17 * Copyright (C) 2000 Linus Torvalds.
18 * 2000 Transmeta Corp.
21 #include <linux/module.h>
23 #include <linux/pagemap.h>
24 #include <linux/types.h>
25 #include <linux/slab.h>
26 #include <linux/highmem.h>
27 #include <linux/init.h>
28 #include <linux/string.h>
29 #include <linux/backing-dev.h>
30 #include <linux/poll.h>
32 #include <linux/uaccess.h>
34 #include "../stackglue.h"
37 #define MLOG_MASK_PREFIX ML_DLMFS
38 #include "../cluster/masklog.h"
41 static const struct super_operations dlmfs_ops;
42 static const struct file_operations dlmfs_file_operations;
43 static const struct inode_operations dlmfs_dir_inode_operations;
44 static const struct inode_operations dlmfs_root_inode_operations;
45 static const struct inode_operations dlmfs_file_inode_operations;
46 static struct kmem_cache *dlmfs_inode_cache;
48 struct workqueue_struct *user_dlm_worker;
53 * These are the ABI capabilities of dlmfs.
55 * Over time, dlmfs has added some features that were not part of the
56 * initial ABI. Unfortunately, some of these features are not detectable
57 * via standard usage. For example, Linux's default poll always returns
58 * EPOLLIN, so there is no way for a caller of poll(2) to know when dlmfs
59 * added poll support. Instead, we provide this list of new capabilities.
61 * Capabilities is a read-only attribute. We do it as a module parameter
62 * so we can discover it whether dlmfs is built in, loaded, or even not
65 * The ABI features are local to this machine's dlmfs mount. This is
66 * distinct from the locking protocol, which is concerned with inter-node
70 * - bast : EPOLLIN against the file descriptor of a held lock
71 * signifies a bast fired on the lock.
73 #define DLMFS_CAPABILITIES "bast stackglue"
74 static int param_set_dlmfs_capabilities(const char *val,
75 const struct kernel_param *kp)
77 printk(KERN_ERR "%s: readonly parameter\n", kp->name);
80 static int param_get_dlmfs_capabilities(char *buffer,
81 const struct kernel_param *kp)
83 return sysfs_emit(buffer, DLMFS_CAPABILITIES);
85 module_param_call(capabilities, param_set_dlmfs_capabilities,
86 param_get_dlmfs_capabilities, NULL, 0444);
87 MODULE_PARM_DESC(capabilities, DLMFS_CAPABILITIES);
91 * decodes a set of open flags into a valid lock level and a set of flags.
92 * returns < 0 if we have invalid flags
93 * flags which mean something to us:
94 * O_RDONLY -> PRMODE level
95 * O_WRONLY -> EXMODE level
97 * O_NONBLOCK -> NOQUEUE
99 static int dlmfs_decode_open_flags(int open_flags,
103 if (open_flags & (O_WRONLY|O_RDWR))
104 *level = DLM_LOCK_EX;
106 *level = DLM_LOCK_PR;
109 if (open_flags & O_NONBLOCK)
110 *flags |= DLM_LKF_NOQUEUE;
115 static int dlmfs_file_open(struct inode *inode,
118 int status, level, flags;
119 struct dlmfs_filp_private *fp = NULL;
120 struct dlmfs_inode_private *ip;
122 if (S_ISDIR(inode->i_mode))
125 mlog(0, "open called on inode %lu, flags 0x%x\n", inode->i_ino,
128 status = dlmfs_decode_open_flags(file->f_flags, &level, &flags);
132 /* We don't want to honor O_APPEND at read/write time as it
133 * doesn't make sense for LVB writes. */
134 file->f_flags &= ~O_APPEND;
136 fp = kmalloc(sizeof(*fp), GFP_NOFS);
141 fp->fp_lock_level = level;
145 status = user_dlm_cluster_lock(&ip->ip_lockres, level, flags);
147 /* this is a strange error to return here but I want
148 * to be able userspace to be able to distinguish a
149 * valid lock request from one that simply couldn't be
151 if (flags & DLM_LKF_NOQUEUE && status == -EAGAIN)
157 file->private_data = fp;
162 static int dlmfs_file_release(struct inode *inode,
166 struct dlmfs_inode_private *ip = DLMFS_I(inode);
167 struct dlmfs_filp_private *fp = file->private_data;
169 if (S_ISDIR(inode->i_mode))
172 mlog(0, "close called on inode %lu\n", inode->i_ino);
175 level = fp->fp_lock_level;
176 if (level != DLM_LOCK_IV)
177 user_dlm_cluster_unlock(&ip->ip_lockres, level);
180 file->private_data = NULL;
187 * We do ->setattr() just to override size changes. Our size is the size
188 * of the LVB and nothing else.
190 static int dlmfs_file_setattr(struct mnt_idmap *idmap,
191 struct dentry *dentry, struct iattr *attr)
194 struct inode *inode = d_inode(dentry);
196 attr->ia_valid &= ~ATTR_SIZE;
197 error = setattr_prepare(&nop_mnt_idmap, dentry, attr);
201 setattr_copy(&nop_mnt_idmap, inode, attr);
202 mark_inode_dirty(inode);
206 static __poll_t dlmfs_file_poll(struct file *file, poll_table *wait)
209 struct inode *inode = file_inode(file);
210 struct dlmfs_inode_private *ip = DLMFS_I(inode);
212 poll_wait(file, &ip->ip_lockres.l_event, wait);
214 spin_lock(&ip->ip_lockres.l_lock);
215 if (ip->ip_lockres.l_flags & USER_LOCK_BLOCKED)
216 event = EPOLLIN | EPOLLRDNORM;
217 spin_unlock(&ip->ip_lockres.l_lock);
222 static ssize_t dlmfs_file_read(struct file *file,
227 char lvb[DLM_LVB_LEN];
229 if (!user_dlm_read_lvb(file_inode(file), lvb))
232 return simple_read_from_buffer(buf, count, ppos, lvb, sizeof(lvb));
235 static ssize_t dlmfs_file_write(struct file *filp,
236 const char __user *buf,
240 char lvb_buf[DLM_LVB_LEN];
242 struct inode *inode = file_inode(filp);
244 mlog(0, "inode %lu, count = %zu, *ppos = %llu\n",
245 inode->i_ino, count, *ppos);
247 if (*ppos >= DLM_LVB_LEN)
250 /* don't write past the lvb */
251 if (count > DLM_LVB_LEN - *ppos)
252 count = DLM_LVB_LEN - *ppos;
257 bytes_left = copy_from_user(lvb_buf, buf, count);
260 user_dlm_write_lvb(inode, lvb_buf, count);
262 *ppos = *ppos + count;
263 mlog(0, "wrote %zu bytes\n", count);
267 static void dlmfs_init_once(void *foo)
269 struct dlmfs_inode_private *ip =
270 (struct dlmfs_inode_private *) foo;
273 ip->ip_parent = NULL;
275 inode_init_once(&ip->ip_vfs_inode);
278 static struct inode *dlmfs_alloc_inode(struct super_block *sb)
280 struct dlmfs_inode_private *ip;
282 ip = alloc_inode_sb(sb, dlmfs_inode_cache, GFP_NOFS);
286 return &ip->ip_vfs_inode;
289 static void dlmfs_free_inode(struct inode *inode)
291 kmem_cache_free(dlmfs_inode_cache, DLMFS_I(inode));
294 static void dlmfs_evict_inode(struct inode *inode)
297 struct dlmfs_inode_private *ip;
298 struct user_lock_res *lockres;
303 mlog(0, "inode %lu\n", inode->i_ino);
306 lockres = &ip->ip_lockres;
308 if (S_ISREG(inode->i_mode)) {
309 spin_lock(&lockres->l_lock);
310 teardown = !!(lockres->l_flags & USER_LOCK_IN_TEARDOWN);
311 spin_unlock(&lockres->l_lock);
313 status = user_dlm_destroy_lock(lockres);
321 mlog(0, "we're a directory, ip->ip_conn = 0x%p\n", ip->ip_conn);
322 /* we must be a directory. If required, lets unregister the
323 * dlm context now. */
325 user_dlm_unregister(ip->ip_conn);
327 ip->ip_parent = NULL;
331 static struct inode *dlmfs_get_root_inode(struct super_block *sb)
333 struct inode *inode = new_inode(sb);
334 umode_t mode = S_IFDIR | 0755;
337 inode->i_ino = get_next_ino();
338 inode_init_owner(&nop_mnt_idmap, inode, NULL, mode);
339 simple_inode_init_ts(inode);
342 inode->i_fop = &simple_dir_operations;
343 inode->i_op = &dlmfs_root_inode_operations;
349 static struct inode *dlmfs_get_inode(struct inode *parent,
350 struct dentry *dentry,
353 struct super_block *sb = parent->i_sb;
354 struct inode * inode = new_inode(sb);
355 struct dlmfs_inode_private *ip;
360 inode->i_ino = get_next_ino();
361 inode_init_owner(&nop_mnt_idmap, inode, parent, mode);
362 simple_inode_init_ts(inode);
365 ip->ip_conn = DLMFS_I(parent)->ip_conn;
367 switch (mode & S_IFMT) {
369 /* for now we don't support anything other than
370 * directories and regular files. */
374 inode->i_op = &dlmfs_file_inode_operations;
375 inode->i_fop = &dlmfs_file_operations;
377 i_size_write(inode, DLM_LVB_LEN);
379 user_dlm_lock_res_init(&ip->ip_lockres, dentry);
381 /* released at clear_inode time, this insures that we
382 * get to drop the dlm reference on each lock *before*
383 * we call the unregister code for releasing parent
385 ip->ip_parent = igrab(parent);
386 BUG_ON(!ip->ip_parent);
389 inode->i_op = &dlmfs_dir_inode_operations;
390 inode->i_fop = &simple_dir_operations;
392 /* directory inodes start off with i_nlink ==
393 * 2 (for "." entry) */
401 * File creation. Allocate an inode, and we're done..
404 static int dlmfs_mkdir(struct mnt_idmap * idmap,
406 struct dentry * dentry,
410 struct inode *inode = NULL;
411 const struct qstr *domain = &dentry->d_name;
412 struct dlmfs_inode_private *ip;
413 struct ocfs2_cluster_connection *conn;
415 mlog(0, "mkdir %.*s\n", domain->len, domain->name);
417 /* verify that we have a proper domain */
418 if (domain->len >= GROUP_NAME_MAX) {
420 mlog(ML_ERROR, "invalid domain name for directory.\n");
424 inode = dlmfs_get_inode(dir, dentry, mode | S_IFDIR);
433 conn = user_dlm_register(domain);
435 status = PTR_ERR(conn);
436 mlog(ML_ERROR, "Error %d could not register domain \"%.*s\"\n",
437 status, domain->len, domain->name);
443 d_instantiate(dentry, inode);
444 dget(dentry); /* Extra count - pin the dentry in core */
453 static int dlmfs_create(struct mnt_idmap *idmap,
455 struct dentry *dentry,
461 const struct qstr *name = &dentry->d_name;
463 mlog(0, "create %.*s\n", name->len, name->name);
465 /* verify name is valid and doesn't contain any dlm reserved
467 if (name->len >= USER_DLM_LOCK_ID_MAX_LEN ||
468 name->name[0] == '$') {
470 mlog(ML_ERROR, "invalid lock name, %.*s\n", name->len,
475 inode = dlmfs_get_inode(dir, dentry, mode | S_IFREG);
482 d_instantiate(dentry, inode);
483 dget(dentry); /* Extra count - pin the dentry in core */
488 static int dlmfs_unlink(struct inode *dir,
489 struct dentry *dentry)
492 struct inode *inode = d_inode(dentry);
494 mlog(0, "unlink inode %lu\n", inode->i_ino);
496 /* if there are no current holders, or none that are waiting
497 * to acquire a lock, this basically destroys our lockres. */
498 status = user_dlm_destroy_lock(&DLMFS_I(inode)->ip_lockres);
500 mlog(ML_ERROR, "unlink %pd, error %d from destroy\n",
504 status = simple_unlink(dir, dentry);
509 static int dlmfs_fill_super(struct super_block * sb,
513 sb->s_maxbytes = MAX_LFS_FILESIZE;
514 sb->s_blocksize = PAGE_SIZE;
515 sb->s_blocksize_bits = PAGE_SHIFT;
516 sb->s_magic = DLMFS_MAGIC;
517 sb->s_op = &dlmfs_ops;
518 sb->s_root = d_make_root(dlmfs_get_root_inode(sb));
524 static const struct file_operations dlmfs_file_operations = {
525 .open = dlmfs_file_open,
526 .release = dlmfs_file_release,
527 .poll = dlmfs_file_poll,
528 .read = dlmfs_file_read,
529 .write = dlmfs_file_write,
530 .llseek = default_llseek,
533 static const struct inode_operations dlmfs_dir_inode_operations = {
534 .create = dlmfs_create,
535 .lookup = simple_lookup,
536 .unlink = dlmfs_unlink,
539 /* this way we can restrict mkdir to only the toplevel of the fs. */
540 static const struct inode_operations dlmfs_root_inode_operations = {
541 .lookup = simple_lookup,
542 .mkdir = dlmfs_mkdir,
543 .rmdir = simple_rmdir,
546 static const struct super_operations dlmfs_ops = {
547 .statfs = simple_statfs,
548 .alloc_inode = dlmfs_alloc_inode,
549 .free_inode = dlmfs_free_inode,
550 .evict_inode = dlmfs_evict_inode,
551 .drop_inode = generic_delete_inode,
554 static const struct inode_operations dlmfs_file_inode_operations = {
555 .getattr = simple_getattr,
556 .setattr = dlmfs_file_setattr,
559 static struct dentry *dlmfs_mount(struct file_system_type *fs_type,
560 int flags, const char *dev_name, void *data)
562 return mount_nodev(fs_type, flags, data, dlmfs_fill_super);
565 static struct file_system_type dlmfs_fs_type = {
566 .owner = THIS_MODULE,
567 .name = "ocfs2_dlmfs",
568 .mount = dlmfs_mount,
569 .kill_sb = kill_litter_super,
571 MODULE_ALIAS_FS("ocfs2_dlmfs");
573 static int __init init_dlmfs_fs(void)
576 int cleanup_inode = 0, cleanup_worker = 0;
578 dlmfs_inode_cache = kmem_cache_create("dlmfs_inode_cache",
579 sizeof(struct dlmfs_inode_private),
580 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
581 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
583 if (!dlmfs_inode_cache) {
589 user_dlm_worker = alloc_workqueue("user_dlm", WQ_MEM_RECLAIM, 0);
590 if (!user_dlm_worker) {
596 user_dlm_set_locking_protocol();
597 status = register_filesystem(&dlmfs_fs_type);
601 kmem_cache_destroy(dlmfs_inode_cache);
603 destroy_workqueue(user_dlm_worker);
605 printk("OCFS2 User DLM kernel interface loaded\n");
609 static void __exit exit_dlmfs_fs(void)
611 unregister_filesystem(&dlmfs_fs_type);
613 destroy_workqueue(user_dlm_worker);
616 * Make sure all delayed rcu free inodes are flushed before we
620 kmem_cache_destroy(dlmfs_inode_cache);
624 MODULE_AUTHOR("Oracle");
625 MODULE_LICENSE("GPL");
626 MODULE_DESCRIPTION("OCFS2 DLM-Filesystem");
628 module_init(init_dlmfs_fs)
629 module_exit(exit_dlmfs_fs)