Linux 5.8-rc4
[linux/fpc-iii.git] / fs / ocfs2 / dlmfs / dlmfs.c
blobea868c6f980053daf90e90c3acefba4f5e18e993
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:
5 * dlmfs.c
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.
12 * Copyright (C) 2003, 2004 Oracle. All rights reserved.
15 /* Simple VFS hooks based on: */
17 * Resizable simple ram filesystem for Linux.
19 * Copyright (C) 2000 Linus Torvalds.
20 * 2000 Transmeta Corp.
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>
34 #include <linux/uaccess.h>
36 #include "../stackglue.h"
37 #include "userdlm.h"
39 #define MLOG_MASK_PREFIX ML_DLMFS
40 #include "../cluster/masklog.h"
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;
50 struct workqueue_struct *user_dlm_worker;
55 * These are the ABI capabilities of dlmfs.
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.
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.
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.
71 * Capabilities:
72 * - bast : EPOLLIN against the file descriptor of a held lock
73 * signifies a bast fired on the lock.
75 #define DLMFS_CAPABILITIES "bast stackglue"
76 static int param_set_dlmfs_capabilities(const char *val,
77 const struct kernel_param *kp)
79 printk(KERN_ERR "%s: readonly parameter\n", kp->name);
80 return -EINVAL;
82 static int param_get_dlmfs_capabilities(char *buffer,
83 const struct kernel_param *kp)
85 return strlcpy(buffer, DLMFS_CAPABILITIES,
86 strlen(DLMFS_CAPABILITIES) + 1);
88 module_param_call(capabilities, param_set_dlmfs_capabilities,
89 param_get_dlmfs_capabilities, NULL, 0444);
90 MODULE_PARM_DESC(capabilities, DLMFS_CAPABILITIES);
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
100 * O_NONBLOCK -> NOQUEUE
102 static int dlmfs_decode_open_flags(int open_flags,
103 int *level,
104 int *flags)
106 if (open_flags & (O_WRONLY|O_RDWR))
107 *level = DLM_LOCK_EX;
108 else
109 *level = DLM_LOCK_PR;
111 *flags = 0;
112 if (open_flags & O_NONBLOCK)
113 *flags |= DLM_LKF_NOQUEUE;
115 return 0;
118 static int dlmfs_file_open(struct inode *inode,
119 struct file *file)
121 int status, level, flags;
122 struct dlmfs_filp_private *fp = NULL;
123 struct dlmfs_inode_private *ip;
125 if (S_ISDIR(inode->i_mode))
126 BUG();
128 mlog(0, "open called on inode %lu, flags 0x%x\n", inode->i_ino,
129 file->f_flags);
131 status = dlmfs_decode_open_flags(file->f_flags, &level, &flags);
132 if (status < 0)
133 goto bail;
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;
139 fp = kmalloc(sizeof(*fp), GFP_NOFS);
140 if (!fp) {
141 status = -ENOMEM;
142 goto bail;
144 fp->fp_lock_level = level;
146 ip = DLMFS_I(inode);
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;
160 file->private_data = fp;
161 bail:
162 return status;
165 static int dlmfs_file_release(struct inode *inode,
166 struct file *file)
168 int level;
169 struct dlmfs_inode_private *ip = DLMFS_I(inode);
170 struct dlmfs_filp_private *fp = file->private_data;
172 if (S_ISDIR(inode->i_mode))
173 BUG();
175 mlog(0, "close called on inode %lu\n", inode->i_ino);
177 if (fp) {
178 level = fp->fp_lock_level;
179 if (level != DLM_LOCK_IV)
180 user_dlm_cluster_unlock(&ip->ip_lockres, level);
182 kfree(fp);
183 file->private_data = NULL;
186 return 0;
190 * We do ->setattr() just to override size changes. Our size is the size
191 * of the LVB and nothing else.
193 static int dlmfs_file_setattr(struct dentry *dentry, struct iattr *attr)
195 int error;
196 struct inode *inode = d_inode(dentry);
198 attr->ia_valid &= ~ATTR_SIZE;
199 error = setattr_prepare(dentry, attr);
200 if (error)
201 return error;
203 setattr_copy(inode, attr);
204 mark_inode_dirty(inode);
205 return 0;
208 static __poll_t dlmfs_file_poll(struct file *file, poll_table *wait)
210 __poll_t event = 0;
211 struct inode *inode = file_inode(file);
212 struct dlmfs_inode_private *ip = DLMFS_I(inode);
214 poll_wait(file, &ip->ip_lockres.l_event, wait);
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);
221 return event;
224 static ssize_t dlmfs_file_read(struct file *filp,
225 char __user *buf,
226 size_t count,
227 loff_t *ppos)
229 int bytes_left;
230 ssize_t got;
231 char *lvb_buf;
232 struct inode *inode = file_inode(filp);
234 mlog(0, "inode %lu, count = %zu, *ppos = %llu\n",
235 inode->i_ino, count, *ppos);
237 if (*ppos >= i_size_read(inode))
238 return 0;
240 /* don't read past the lvb */
241 if (count > i_size_read(inode) - *ppos)
242 count = i_size_read(inode) - *ppos;
244 if (!count)
245 return 0;
247 lvb_buf = kmalloc(count, GFP_NOFS);
248 if (!lvb_buf)
249 return -ENOMEM;
251 got = user_dlm_read_lvb(inode, lvb_buf, count);
252 if (got) {
253 BUG_ON(got != count);
254 bytes_left = copy_to_user(buf, lvb_buf, count);
255 count -= bytes_left;
256 } else
257 count = 0;
259 kfree(lvb_buf);
261 *ppos = *ppos + count;
263 mlog(0, "read %zu bytes\n", count);
264 return count;
267 static ssize_t dlmfs_file_write(struct file *filp,
268 const char __user *buf,
269 size_t count,
270 loff_t *ppos)
272 int bytes_left;
273 char *lvb_buf;
274 struct inode *inode = file_inode(filp);
276 mlog(0, "inode %lu, count = %zu, *ppos = %llu\n",
277 inode->i_ino, count, *ppos);
279 if (*ppos >= i_size_read(inode))
280 return -ENOSPC;
282 /* don't write past the lvb */
283 if (count > i_size_read(inode) - *ppos)
284 count = i_size_read(inode) - *ppos;
286 if (!count)
287 return 0;
289 lvb_buf = kmalloc(count, GFP_NOFS);
290 if (!lvb_buf)
291 return -ENOMEM;
293 bytes_left = copy_from_user(lvb_buf, buf, count);
294 count -= bytes_left;
295 if (count)
296 user_dlm_write_lvb(inode, lvb_buf, count);
298 kfree(lvb_buf);
300 *ppos = *ppos + count;
301 mlog(0, "wrote %zu bytes\n", count);
302 return count;
305 static void dlmfs_init_once(void *foo)
307 struct dlmfs_inode_private *ip =
308 (struct dlmfs_inode_private *) foo;
310 ip->ip_conn = NULL;
311 ip->ip_parent = NULL;
313 inode_init_once(&ip->ip_vfs_inode);
316 static struct inode *dlmfs_alloc_inode(struct super_block *sb)
318 struct dlmfs_inode_private *ip;
320 ip = kmem_cache_alloc(dlmfs_inode_cache, GFP_NOFS);
321 if (!ip)
322 return NULL;
324 return &ip->ip_vfs_inode;
327 static void dlmfs_free_inode(struct inode *inode)
329 kmem_cache_free(dlmfs_inode_cache, DLMFS_I(inode));
332 static void dlmfs_evict_inode(struct inode *inode)
334 int status;
335 struct dlmfs_inode_private *ip;
337 clear_inode(inode);
339 mlog(0, "inode %lu\n", inode->i_ino);
341 ip = DLMFS_I(inode);
343 if (S_ISREG(inode->i_mode)) {
344 status = user_dlm_destroy_lock(&ip->ip_lockres);
345 if (status < 0)
346 mlog_errno(status);
347 iput(ip->ip_parent);
348 goto clear_fields;
351 mlog(0, "we're a directory, ip->ip_conn = 0x%p\n", ip->ip_conn);
352 /* we must be a directory. If required, lets unregister the
353 * dlm context now. */
354 if (ip->ip_conn)
355 user_dlm_unregister(ip->ip_conn);
356 clear_fields:
357 ip->ip_parent = NULL;
358 ip->ip_conn = NULL;
361 static struct inode *dlmfs_get_root_inode(struct super_block *sb)
363 struct inode *inode = new_inode(sb);
364 umode_t mode = S_IFDIR | 0755;
366 if (inode) {
367 inode->i_ino = get_next_ino();
368 inode_init_owner(inode, NULL, mode);
369 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
370 inc_nlink(inode);
372 inode->i_fop = &simple_dir_operations;
373 inode->i_op = &dlmfs_root_inode_operations;
376 return inode;
379 static struct inode *dlmfs_get_inode(struct inode *parent,
380 struct dentry *dentry,
381 umode_t mode)
383 struct super_block *sb = parent->i_sb;
384 struct inode * inode = new_inode(sb);
385 struct dlmfs_inode_private *ip;
387 if (!inode)
388 return NULL;
390 inode->i_ino = get_next_ino();
391 inode_init_owner(inode, parent, mode);
392 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
394 ip = DLMFS_I(inode);
395 ip->ip_conn = DLMFS_I(parent)->ip_conn;
397 switch (mode & S_IFMT) {
398 default:
399 /* for now we don't support anything other than
400 * directories and regular files. */
401 BUG();
402 break;
403 case S_IFREG:
404 inode->i_op = &dlmfs_file_inode_operations;
405 inode->i_fop = &dlmfs_file_operations;
407 i_size_write(inode, DLM_LVB_LEN);
409 user_dlm_lock_res_init(&ip->ip_lockres, dentry);
411 /* released at clear_inode time, this insures that we
412 * get to drop the dlm reference on each lock *before*
413 * we call the unregister code for releasing parent
414 * directories. */
415 ip->ip_parent = igrab(parent);
416 BUG_ON(!ip->ip_parent);
417 break;
418 case S_IFDIR:
419 inode->i_op = &dlmfs_dir_inode_operations;
420 inode->i_fop = &simple_dir_operations;
422 /* directory inodes start off with i_nlink ==
423 * 2 (for "." entry) */
424 inc_nlink(inode);
425 break;
427 return inode;
431 * File creation. Allocate an inode, and we're done..
433 /* SMP-safe */
434 static int dlmfs_mkdir(struct inode * dir,
435 struct dentry * dentry,
436 umode_t mode)
438 int status;
439 struct inode *inode = NULL;
440 const struct qstr *domain = &dentry->d_name;
441 struct dlmfs_inode_private *ip;
442 struct ocfs2_cluster_connection *conn;
444 mlog(0, "mkdir %.*s\n", domain->len, domain->name);
446 /* verify that we have a proper domain */
447 if (domain->len >= GROUP_NAME_MAX) {
448 status = -EINVAL;
449 mlog(ML_ERROR, "invalid domain name for directory.\n");
450 goto bail;
453 inode = dlmfs_get_inode(dir, dentry, mode | S_IFDIR);
454 if (!inode) {
455 status = -ENOMEM;
456 mlog_errno(status);
457 goto bail;
460 ip = DLMFS_I(inode);
462 conn = user_dlm_register(domain);
463 if (IS_ERR(conn)) {
464 status = PTR_ERR(conn);
465 mlog(ML_ERROR, "Error %d could not register domain \"%.*s\"\n",
466 status, domain->len, domain->name);
467 goto bail;
469 ip->ip_conn = conn;
471 inc_nlink(dir);
472 d_instantiate(dentry, inode);
473 dget(dentry); /* Extra count - pin the dentry in core */
475 status = 0;
476 bail:
477 if (status < 0)
478 iput(inode);
479 return status;
482 static int dlmfs_create(struct inode *dir,
483 struct dentry *dentry,
484 umode_t mode,
485 bool excl)
487 int status = 0;
488 struct inode *inode;
489 const struct qstr *name = &dentry->d_name;
491 mlog(0, "create %.*s\n", name->len, name->name);
493 /* verify name is valid and doesn't contain any dlm reserved
494 * characters */
495 if (name->len >= USER_DLM_LOCK_ID_MAX_LEN ||
496 name->name[0] == '$') {
497 status = -EINVAL;
498 mlog(ML_ERROR, "invalid lock name, %.*s\n", name->len,
499 name->name);
500 goto bail;
503 inode = dlmfs_get_inode(dir, dentry, mode | S_IFREG);
504 if (!inode) {
505 status = -ENOMEM;
506 mlog_errno(status);
507 goto bail;
510 d_instantiate(dentry, inode);
511 dget(dentry); /* Extra count - pin the dentry in core */
512 bail:
513 return status;
516 static int dlmfs_unlink(struct inode *dir,
517 struct dentry *dentry)
519 int status;
520 struct inode *inode = d_inode(dentry);
522 mlog(0, "unlink inode %lu\n", inode->i_ino);
524 /* if there are no current holders, or none that are waiting
525 * to acquire a lock, this basically destroys our lockres. */
526 status = user_dlm_destroy_lock(&DLMFS_I(inode)->ip_lockres);
527 if (status < 0) {
528 mlog(ML_ERROR, "unlink %pd, error %d from destroy\n",
529 dentry, status);
530 goto bail;
532 status = simple_unlink(dir, dentry);
533 bail:
534 return status;
537 static int dlmfs_fill_super(struct super_block * sb,
538 void * data,
539 int silent)
541 sb->s_maxbytes = MAX_LFS_FILESIZE;
542 sb->s_blocksize = PAGE_SIZE;
543 sb->s_blocksize_bits = PAGE_SHIFT;
544 sb->s_magic = DLMFS_MAGIC;
545 sb->s_op = &dlmfs_ops;
546 sb->s_root = d_make_root(dlmfs_get_root_inode(sb));
547 if (!sb->s_root)
548 return -ENOMEM;
549 return 0;
552 static const struct file_operations dlmfs_file_operations = {
553 .open = dlmfs_file_open,
554 .release = dlmfs_file_release,
555 .poll = dlmfs_file_poll,
556 .read = dlmfs_file_read,
557 .write = dlmfs_file_write,
558 .llseek = default_llseek,
561 static const struct inode_operations dlmfs_dir_inode_operations = {
562 .create = dlmfs_create,
563 .lookup = simple_lookup,
564 .unlink = dlmfs_unlink,
567 /* this way we can restrict mkdir to only the toplevel of the fs. */
568 static const struct inode_operations dlmfs_root_inode_operations = {
569 .lookup = simple_lookup,
570 .mkdir = dlmfs_mkdir,
571 .rmdir = simple_rmdir,
574 static const struct super_operations dlmfs_ops = {
575 .statfs = simple_statfs,
576 .alloc_inode = dlmfs_alloc_inode,
577 .free_inode = dlmfs_free_inode,
578 .evict_inode = dlmfs_evict_inode,
579 .drop_inode = generic_delete_inode,
582 static const struct inode_operations dlmfs_file_inode_operations = {
583 .getattr = simple_getattr,
584 .setattr = dlmfs_file_setattr,
587 static struct dentry *dlmfs_mount(struct file_system_type *fs_type,
588 int flags, const char *dev_name, void *data)
590 return mount_nodev(fs_type, flags, data, dlmfs_fill_super);
593 static struct file_system_type dlmfs_fs_type = {
594 .owner = THIS_MODULE,
595 .name = "ocfs2_dlmfs",
596 .mount = dlmfs_mount,
597 .kill_sb = kill_litter_super,
599 MODULE_ALIAS_FS("ocfs2_dlmfs");
601 static int __init init_dlmfs_fs(void)
603 int status;
604 int cleanup_inode = 0, cleanup_worker = 0;
606 dlmfs_inode_cache = kmem_cache_create("dlmfs_inode_cache",
607 sizeof(struct dlmfs_inode_private),
608 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
609 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
610 dlmfs_init_once);
611 if (!dlmfs_inode_cache) {
612 status = -ENOMEM;
613 goto bail;
615 cleanup_inode = 1;
617 user_dlm_worker = alloc_workqueue("user_dlm", WQ_MEM_RECLAIM, 0);
618 if (!user_dlm_worker) {
619 status = -ENOMEM;
620 goto bail;
622 cleanup_worker = 1;
624 user_dlm_set_locking_protocol();
625 status = register_filesystem(&dlmfs_fs_type);
626 bail:
627 if (status) {
628 if (cleanup_inode)
629 kmem_cache_destroy(dlmfs_inode_cache);
630 if (cleanup_worker)
631 destroy_workqueue(user_dlm_worker);
632 } else
633 printk("OCFS2 User DLM kernel interface loaded\n");
634 return status;
637 static void __exit exit_dlmfs_fs(void)
639 unregister_filesystem(&dlmfs_fs_type);
641 destroy_workqueue(user_dlm_worker);
644 * Make sure all delayed rcu free inodes are flushed before we
645 * destroy cache.
647 rcu_barrier();
648 kmem_cache_destroy(dlmfs_inode_cache);
652 MODULE_AUTHOR("Oracle");
653 MODULE_LICENSE("GPL");
654 MODULE_DESCRIPTION("OCFS2 DLM-Filesystem");
656 module_init(init_dlmfs_fs)
657 module_exit(exit_dlmfs_fs)