Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * dlmfs.c
0004  *
0005  * Code which implements the kernel side of a minimal userspace
0006  * interface to our DLM. This file handles the virtual file system
0007  * used for communication with userspace. Credit should go to ramfs,
0008  * which was a template for the fs side of this module.
0009  *
0010  * Copyright (C) 2003, 2004 Oracle.  All rights reserved.
0011  */
0012 
0013 /* Simple VFS hooks based on: */
0014 /*
0015  * Resizable simple ram filesystem for Linux.
0016  *
0017  * Copyright (C) 2000 Linus Torvalds.
0018  *               2000 Transmeta Corp.
0019  */
0020 
0021 #include <linux/module.h>
0022 #include <linux/fs.h>
0023 #include <linux/pagemap.h>
0024 #include <linux/types.h>
0025 #include <linux/slab.h>
0026 #include <linux/highmem.h>
0027 #include <linux/init.h>
0028 #include <linux/string.h>
0029 #include <linux/backing-dev.h>
0030 #include <linux/poll.h>
0031 
0032 #include <linux/uaccess.h>
0033 
0034 #include "../stackglue.h"
0035 #include "userdlm.h"
0036 
0037 #define MLOG_MASK_PREFIX ML_DLMFS
0038 #include "../cluster/masklog.h"
0039 
0040 
0041 static const struct super_operations dlmfs_ops;
0042 static const struct file_operations dlmfs_file_operations;
0043 static const struct inode_operations dlmfs_dir_inode_operations;
0044 static const struct inode_operations dlmfs_root_inode_operations;
0045 static const struct inode_operations dlmfs_file_inode_operations;
0046 static struct kmem_cache *dlmfs_inode_cache;
0047 
0048 struct workqueue_struct *user_dlm_worker;
0049 
0050 
0051 
0052 /*
0053  * These are the ABI capabilities of dlmfs.
0054  *
0055  * Over time, dlmfs has added some features that were not part of the
0056  * initial ABI.  Unfortunately, some of these features are not detectable
0057  * via standard usage.  For example, Linux's default poll always returns
0058  * EPOLLIN, so there is no way for a caller of poll(2) to know when dlmfs
0059  * added poll support.  Instead, we provide this list of new capabilities.
0060  *
0061  * Capabilities is a read-only attribute.  We do it as a module parameter
0062  * so we can discover it whether dlmfs is built in, loaded, or even not
0063  * loaded.
0064  *
0065  * The ABI features are local to this machine's dlmfs mount.  This is
0066  * distinct from the locking protocol, which is concerned with inter-node
0067  * interaction.
0068  *
0069  * Capabilities:
0070  * - bast   : EPOLLIN against the file descriptor of a held lock
0071  *        signifies a bast fired on the lock.
0072  */
0073 #define DLMFS_CAPABILITIES "bast stackglue"
0074 static int param_set_dlmfs_capabilities(const char *val,
0075                     const struct kernel_param *kp)
0076 {
0077     printk(KERN_ERR "%s: readonly parameter\n", kp->name);
0078     return -EINVAL;
0079 }
0080 static int param_get_dlmfs_capabilities(char *buffer,
0081                     const struct kernel_param *kp)
0082 {
0083     return strlcpy(buffer, DLMFS_CAPABILITIES,
0084                strlen(DLMFS_CAPABILITIES) + 1);
0085 }
0086 module_param_call(capabilities, param_set_dlmfs_capabilities,
0087           param_get_dlmfs_capabilities, NULL, 0444);
0088 MODULE_PARM_DESC(capabilities, DLMFS_CAPABILITIES);
0089 
0090 
0091 /*
0092  * decodes a set of open flags into a valid lock level and a set of flags.
0093  * returns < 0 if we have invalid flags
0094  * flags which mean something to us:
0095  * O_RDONLY -> PRMODE level
0096  * O_WRONLY -> EXMODE level
0097  *
0098  * O_NONBLOCK -> NOQUEUE
0099  */
0100 static int dlmfs_decode_open_flags(int open_flags,
0101                    int *level,
0102                    int *flags)
0103 {
0104     if (open_flags & (O_WRONLY|O_RDWR))
0105         *level = DLM_LOCK_EX;
0106     else
0107         *level = DLM_LOCK_PR;
0108 
0109     *flags = 0;
0110     if (open_flags & O_NONBLOCK)
0111         *flags |= DLM_LKF_NOQUEUE;
0112 
0113     return 0;
0114 }
0115 
0116 static int dlmfs_file_open(struct inode *inode,
0117                struct file *file)
0118 {
0119     int status, level, flags;
0120     struct dlmfs_filp_private *fp = NULL;
0121     struct dlmfs_inode_private *ip;
0122 
0123     if (S_ISDIR(inode->i_mode))
0124         BUG();
0125 
0126     mlog(0, "open called on inode %lu, flags 0x%x\n", inode->i_ino,
0127         file->f_flags);
0128 
0129     status = dlmfs_decode_open_flags(file->f_flags, &level, &flags);
0130     if (status < 0)
0131         goto bail;
0132 
0133     /* We don't want to honor O_APPEND at read/write time as it
0134      * doesn't make sense for LVB writes. */
0135     file->f_flags &= ~O_APPEND;
0136 
0137     fp = kmalloc(sizeof(*fp), GFP_NOFS);
0138     if (!fp) {
0139         status = -ENOMEM;
0140         goto bail;
0141     }
0142     fp->fp_lock_level = level;
0143 
0144     ip = DLMFS_I(inode);
0145 
0146     status = user_dlm_cluster_lock(&ip->ip_lockres, level, flags);
0147     if (status < 0) {
0148         /* this is a strange error to return here but I want
0149          * to be able userspace to be able to distinguish a
0150          * valid lock request from one that simply couldn't be
0151          * granted. */
0152         if (flags & DLM_LKF_NOQUEUE && status == -EAGAIN)
0153             status = -ETXTBSY;
0154         kfree(fp);
0155         goto bail;
0156     }
0157 
0158     file->private_data = fp;
0159 bail:
0160     return status;
0161 }
0162 
0163 static int dlmfs_file_release(struct inode *inode,
0164                   struct file *file)
0165 {
0166     int level;
0167     struct dlmfs_inode_private *ip = DLMFS_I(inode);
0168     struct dlmfs_filp_private *fp = file->private_data;
0169 
0170     if (S_ISDIR(inode->i_mode))
0171         BUG();
0172 
0173     mlog(0, "close called on inode %lu\n", inode->i_ino);
0174 
0175     if (fp) {
0176         level = fp->fp_lock_level;
0177         if (level != DLM_LOCK_IV)
0178             user_dlm_cluster_unlock(&ip->ip_lockres, level);
0179 
0180         kfree(fp);
0181         file->private_data = NULL;
0182     }
0183 
0184     return 0;
0185 }
0186 
0187 /*
0188  * We do ->setattr() just to override size changes.  Our size is the size
0189  * of the LVB and nothing else.
0190  */
0191 static int dlmfs_file_setattr(struct user_namespace *mnt_userns,
0192                   struct dentry *dentry, struct iattr *attr)
0193 {
0194     int error;
0195     struct inode *inode = d_inode(dentry);
0196 
0197     attr->ia_valid &= ~ATTR_SIZE;
0198     error = setattr_prepare(&init_user_ns, dentry, attr);
0199     if (error)
0200         return error;
0201 
0202     setattr_copy(&init_user_ns, inode, attr);
0203     mark_inode_dirty(inode);
0204     return 0;
0205 }
0206 
0207 static __poll_t dlmfs_file_poll(struct file *file, poll_table *wait)
0208 {
0209     __poll_t event = 0;
0210     struct inode *inode = file_inode(file);
0211     struct dlmfs_inode_private *ip = DLMFS_I(inode);
0212 
0213     poll_wait(file, &ip->ip_lockres.l_event, wait);
0214 
0215     spin_lock(&ip->ip_lockres.l_lock);
0216     if (ip->ip_lockres.l_flags & USER_LOCK_BLOCKED)
0217         event = EPOLLIN | EPOLLRDNORM;
0218     spin_unlock(&ip->ip_lockres.l_lock);
0219 
0220     return event;
0221 }
0222 
0223 static ssize_t dlmfs_file_read(struct file *file,
0224                    char __user *buf,
0225                    size_t count,
0226                    loff_t *ppos)
0227 {
0228     char lvb[DLM_LVB_LEN];
0229 
0230     if (!user_dlm_read_lvb(file_inode(file), lvb))
0231         return 0;
0232 
0233     return simple_read_from_buffer(buf, count, ppos, lvb, sizeof(lvb));
0234 }
0235 
0236 static ssize_t dlmfs_file_write(struct file *filp,
0237                 const char __user *buf,
0238                 size_t count,
0239                 loff_t *ppos)
0240 {
0241     char lvb_buf[DLM_LVB_LEN];
0242     int bytes_left;
0243     struct inode *inode = file_inode(filp);
0244 
0245     mlog(0, "inode %lu, count = %zu, *ppos = %llu\n",
0246         inode->i_ino, count, *ppos);
0247 
0248     if (*ppos >= DLM_LVB_LEN)
0249         return -ENOSPC;
0250 
0251     /* don't write past the lvb */
0252     if (count > DLM_LVB_LEN - *ppos)
0253         count = DLM_LVB_LEN - *ppos;
0254 
0255     if (!count)
0256         return 0;
0257 
0258     bytes_left = copy_from_user(lvb_buf, buf, count);
0259     count -= bytes_left;
0260     if (count)
0261         user_dlm_write_lvb(inode, lvb_buf, count);
0262 
0263     *ppos = *ppos + count;
0264     mlog(0, "wrote %zu bytes\n", count);
0265     return count;
0266 }
0267 
0268 static void dlmfs_init_once(void *foo)
0269 {
0270     struct dlmfs_inode_private *ip =
0271         (struct dlmfs_inode_private *) foo;
0272 
0273     ip->ip_conn = NULL;
0274     ip->ip_parent = NULL;
0275 
0276     inode_init_once(&ip->ip_vfs_inode);
0277 }
0278 
0279 static struct inode *dlmfs_alloc_inode(struct super_block *sb)
0280 {
0281     struct dlmfs_inode_private *ip;
0282 
0283     ip = alloc_inode_sb(sb, dlmfs_inode_cache, GFP_NOFS);
0284     if (!ip)
0285         return NULL;
0286 
0287     return &ip->ip_vfs_inode;
0288 }
0289 
0290 static void dlmfs_free_inode(struct inode *inode)
0291 {
0292     kmem_cache_free(dlmfs_inode_cache, DLMFS_I(inode));
0293 }
0294 
0295 static void dlmfs_evict_inode(struct inode *inode)
0296 {
0297     int status;
0298     struct dlmfs_inode_private *ip;
0299     struct user_lock_res *lockres;
0300     int teardown;
0301 
0302     clear_inode(inode);
0303 
0304     mlog(0, "inode %lu\n", inode->i_ino);
0305 
0306     ip = DLMFS_I(inode);
0307     lockres = &ip->ip_lockres;
0308 
0309     if (S_ISREG(inode->i_mode)) {
0310         spin_lock(&lockres->l_lock);
0311         teardown = !!(lockres->l_flags & USER_LOCK_IN_TEARDOWN);
0312         spin_unlock(&lockres->l_lock);
0313         if (!teardown) {
0314             status = user_dlm_destroy_lock(lockres);
0315             if (status < 0)
0316                 mlog_errno(status);
0317         }
0318         iput(ip->ip_parent);
0319         goto clear_fields;
0320     }
0321 
0322     mlog(0, "we're a directory, ip->ip_conn = 0x%p\n", ip->ip_conn);
0323     /* we must be a directory. If required, lets unregister the
0324      * dlm context now. */
0325     if (ip->ip_conn)
0326         user_dlm_unregister(ip->ip_conn);
0327 clear_fields:
0328     ip->ip_parent = NULL;
0329     ip->ip_conn = NULL;
0330 }
0331 
0332 static struct inode *dlmfs_get_root_inode(struct super_block *sb)
0333 {
0334     struct inode *inode = new_inode(sb);
0335     umode_t mode = S_IFDIR | 0755;
0336 
0337     if (inode) {
0338         inode->i_ino = get_next_ino();
0339         inode_init_owner(&init_user_ns, inode, NULL, mode);
0340         inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
0341         inc_nlink(inode);
0342 
0343         inode->i_fop = &simple_dir_operations;
0344         inode->i_op = &dlmfs_root_inode_operations;
0345     }
0346 
0347     return inode;
0348 }
0349 
0350 static struct inode *dlmfs_get_inode(struct inode *parent,
0351                      struct dentry *dentry,
0352                      umode_t mode)
0353 {
0354     struct super_block *sb = parent->i_sb;
0355     struct inode * inode = new_inode(sb);
0356     struct dlmfs_inode_private *ip;
0357 
0358     if (!inode)
0359         return NULL;
0360 
0361     inode->i_ino = get_next_ino();
0362     inode_init_owner(&init_user_ns, inode, parent, mode);
0363     inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
0364 
0365     ip = DLMFS_I(inode);
0366     ip->ip_conn = DLMFS_I(parent)->ip_conn;
0367 
0368     switch (mode & S_IFMT) {
0369     default:
0370         /* for now we don't support anything other than
0371          * directories and regular files. */
0372         BUG();
0373         break;
0374     case S_IFREG:
0375         inode->i_op = &dlmfs_file_inode_operations;
0376         inode->i_fop = &dlmfs_file_operations;
0377 
0378         i_size_write(inode,  DLM_LVB_LEN);
0379 
0380         user_dlm_lock_res_init(&ip->ip_lockres, dentry);
0381 
0382         /* released at clear_inode time, this insures that we
0383          * get to drop the dlm reference on each lock *before*
0384          * we call the unregister code for releasing parent
0385          * directories. */
0386         ip->ip_parent = igrab(parent);
0387         BUG_ON(!ip->ip_parent);
0388         break;
0389     case S_IFDIR:
0390         inode->i_op = &dlmfs_dir_inode_operations;
0391         inode->i_fop = &simple_dir_operations;
0392 
0393         /* directory inodes start off with i_nlink ==
0394          * 2 (for "." entry) */
0395         inc_nlink(inode);
0396         break;
0397     }
0398     return inode;
0399 }
0400 
0401 /*
0402  * File creation. Allocate an inode, and we're done..
0403  */
0404 /* SMP-safe */
0405 static int dlmfs_mkdir(struct user_namespace * mnt_userns,
0406                struct inode * dir,
0407                struct dentry * dentry,
0408                umode_t mode)
0409 {
0410     int status;
0411     struct inode *inode = NULL;
0412     const struct qstr *domain = &dentry->d_name;
0413     struct dlmfs_inode_private *ip;
0414     struct ocfs2_cluster_connection *conn;
0415 
0416     mlog(0, "mkdir %.*s\n", domain->len, domain->name);
0417 
0418     /* verify that we have a proper domain */
0419     if (domain->len >= GROUP_NAME_MAX) {
0420         status = -EINVAL;
0421         mlog(ML_ERROR, "invalid domain name for directory.\n");
0422         goto bail;
0423     }
0424 
0425     inode = dlmfs_get_inode(dir, dentry, mode | S_IFDIR);
0426     if (!inode) {
0427         status = -ENOMEM;
0428         mlog_errno(status);
0429         goto bail;
0430     }
0431 
0432     ip = DLMFS_I(inode);
0433 
0434     conn = user_dlm_register(domain);
0435     if (IS_ERR(conn)) {
0436         status = PTR_ERR(conn);
0437         mlog(ML_ERROR, "Error %d could not register domain \"%.*s\"\n",
0438              status, domain->len, domain->name);
0439         goto bail;
0440     }
0441     ip->ip_conn = conn;
0442 
0443     inc_nlink(dir);
0444     d_instantiate(dentry, inode);
0445     dget(dentry);   /* Extra count - pin the dentry in core */
0446 
0447     status = 0;
0448 bail:
0449     if (status < 0)
0450         iput(inode);
0451     return status;
0452 }
0453 
0454 static int dlmfs_create(struct user_namespace *mnt_userns,
0455             struct inode *dir,
0456             struct dentry *dentry,
0457             umode_t mode,
0458             bool excl)
0459 {
0460     int status = 0;
0461     struct inode *inode;
0462     const struct qstr *name = &dentry->d_name;
0463 
0464     mlog(0, "create %.*s\n", name->len, name->name);
0465 
0466     /* verify name is valid and doesn't contain any dlm reserved
0467      * characters */
0468     if (name->len >= USER_DLM_LOCK_ID_MAX_LEN ||
0469         name->name[0] == '$') {
0470         status = -EINVAL;
0471         mlog(ML_ERROR, "invalid lock name, %.*s\n", name->len,
0472              name->name);
0473         goto bail;
0474     }
0475 
0476     inode = dlmfs_get_inode(dir, dentry, mode | S_IFREG);
0477     if (!inode) {
0478         status = -ENOMEM;
0479         mlog_errno(status);
0480         goto bail;
0481     }
0482 
0483     d_instantiate(dentry, inode);
0484     dget(dentry);   /* Extra count - pin the dentry in core */
0485 bail:
0486     return status;
0487 }
0488 
0489 static int dlmfs_unlink(struct inode *dir,
0490             struct dentry *dentry)
0491 {
0492     int status;
0493     struct inode *inode = d_inode(dentry);
0494 
0495     mlog(0, "unlink inode %lu\n", inode->i_ino);
0496 
0497     /* if there are no current holders, or none that are waiting
0498      * to acquire a lock, this basically destroys our lockres. */
0499     status = user_dlm_destroy_lock(&DLMFS_I(inode)->ip_lockres);
0500     if (status < 0) {
0501         mlog(ML_ERROR, "unlink %pd, error %d from destroy\n",
0502              dentry, status);
0503         goto bail;
0504     }
0505     status = simple_unlink(dir, dentry);
0506 bail:
0507     return status;
0508 }
0509 
0510 static int dlmfs_fill_super(struct super_block * sb,
0511                 void * data,
0512                 int silent)
0513 {
0514     sb->s_maxbytes = MAX_LFS_FILESIZE;
0515     sb->s_blocksize = PAGE_SIZE;
0516     sb->s_blocksize_bits = PAGE_SHIFT;
0517     sb->s_magic = DLMFS_MAGIC;
0518     sb->s_op = &dlmfs_ops;
0519     sb->s_root = d_make_root(dlmfs_get_root_inode(sb));
0520     if (!sb->s_root)
0521         return -ENOMEM;
0522     return 0;
0523 }
0524 
0525 static const struct file_operations dlmfs_file_operations = {
0526     .open       = dlmfs_file_open,
0527     .release    = dlmfs_file_release,
0528     .poll       = dlmfs_file_poll,
0529     .read       = dlmfs_file_read,
0530     .write      = dlmfs_file_write,
0531     .llseek     = default_llseek,
0532 };
0533 
0534 static const struct inode_operations dlmfs_dir_inode_operations = {
0535     .create     = dlmfs_create,
0536     .lookup     = simple_lookup,
0537     .unlink     = dlmfs_unlink,
0538 };
0539 
0540 /* this way we can restrict mkdir to only the toplevel of the fs. */
0541 static const struct inode_operations dlmfs_root_inode_operations = {
0542     .lookup     = simple_lookup,
0543     .mkdir      = dlmfs_mkdir,
0544     .rmdir      = simple_rmdir,
0545 };
0546 
0547 static const struct super_operations dlmfs_ops = {
0548     .statfs     = simple_statfs,
0549     .alloc_inode    = dlmfs_alloc_inode,
0550     .free_inode = dlmfs_free_inode,
0551     .evict_inode    = dlmfs_evict_inode,
0552     .drop_inode = generic_delete_inode,
0553 };
0554 
0555 static const struct inode_operations dlmfs_file_inode_operations = {
0556     .getattr    = simple_getattr,
0557     .setattr    = dlmfs_file_setattr,
0558 };
0559 
0560 static struct dentry *dlmfs_mount(struct file_system_type *fs_type,
0561     int flags, const char *dev_name, void *data)
0562 {
0563     return mount_nodev(fs_type, flags, data, dlmfs_fill_super);
0564 }
0565 
0566 static struct file_system_type dlmfs_fs_type = {
0567     .owner      = THIS_MODULE,
0568     .name       = "ocfs2_dlmfs",
0569     .mount      = dlmfs_mount,
0570     .kill_sb    = kill_litter_super,
0571 };
0572 MODULE_ALIAS_FS("ocfs2_dlmfs");
0573 
0574 static int __init init_dlmfs_fs(void)
0575 {
0576     int status;
0577     int cleanup_inode = 0, cleanup_worker = 0;
0578 
0579     dlmfs_inode_cache = kmem_cache_create("dlmfs_inode_cache",
0580                 sizeof(struct dlmfs_inode_private),
0581                 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
0582                     SLAB_MEM_SPREAD|SLAB_ACCOUNT),
0583                 dlmfs_init_once);
0584     if (!dlmfs_inode_cache) {
0585         status = -ENOMEM;
0586         goto bail;
0587     }
0588     cleanup_inode = 1;
0589 
0590     user_dlm_worker = alloc_workqueue("user_dlm", WQ_MEM_RECLAIM, 0);
0591     if (!user_dlm_worker) {
0592         status = -ENOMEM;
0593         goto bail;
0594     }
0595     cleanup_worker = 1;
0596 
0597     user_dlm_set_locking_protocol();
0598     status = register_filesystem(&dlmfs_fs_type);
0599 bail:
0600     if (status) {
0601         if (cleanup_inode)
0602             kmem_cache_destroy(dlmfs_inode_cache);
0603         if (cleanup_worker)
0604             destroy_workqueue(user_dlm_worker);
0605     } else
0606         printk("OCFS2 User DLM kernel interface loaded\n");
0607     return status;
0608 }
0609 
0610 static void __exit exit_dlmfs_fs(void)
0611 {
0612     unregister_filesystem(&dlmfs_fs_type);
0613 
0614     destroy_workqueue(user_dlm_worker);
0615 
0616     /*
0617      * Make sure all delayed rcu free inodes are flushed before we
0618      * destroy cache.
0619      */
0620     rcu_barrier();
0621     kmem_cache_destroy(dlmfs_inode_cache);
0622 
0623 }
0624 
0625 MODULE_AUTHOR("Oracle");
0626 MODULE_LICENSE("GPL");
0627 MODULE_DESCRIPTION("OCFS2 DLM-Filesystem");
0628 
0629 module_init(init_dlmfs_fs)
0630 module_exit(exit_dlmfs_fs)