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 *file, 225 char __user *buf, 226 size_t count, 227 loff_t *ppos) 228 { 229 char lvb[DLM_LVB_LEN]; 230 231 if (!user_dlm_read_lvb(file_inode(file), lvb)) 232 return 0; 233 234 return simple_read_from_buffer(buf, count, ppos, lvb, sizeof(lvb)); 235 } 236 237 static ssize_t dlmfs_file_write(struct file *filp, 238 const char __user *buf, 239 size_t count, 240 loff_t *ppos) 241 { 242 char lvb_buf[DLM_LVB_LEN]; 243 int bytes_left; 244 struct inode *inode = file_inode(filp); 245 246 mlog(0, "inode %lu, count = %zu, *ppos = %llu\n", 247 inode->i_ino, count, *ppos); 248 249 if (*ppos >= DLM_LVB_LEN) 250 return -ENOSPC; 251 252 /* don't write past the lvb */ 253 if (count > DLM_LVB_LEN - *ppos) 254 count = DLM_LVB_LEN - *ppos; 255 256 if (!count) 257 return 0; 258 259 bytes_left = copy_from_user(lvb_buf, buf, count); 260 count -= bytes_left; 261 if (count) 262 user_dlm_write_lvb(inode, lvb_buf, count); 263 264 *ppos = *ppos + count; 265 mlog(0, "wrote %zu bytes\n", count); 266 return count; 267 } 268 269 static void dlmfs_init_once(void *foo) 270 { 271 struct dlmfs_inode_private *ip = 272 (struct dlmfs_inode_private *) foo; 273 274 ip->ip_conn = NULL; 275 ip->ip_parent = NULL; 276 277 inode_init_once(&ip->ip_vfs_inode); 278 } 279 280 static struct inode *dlmfs_alloc_inode(struct super_block *sb) 281 { 282 struct dlmfs_inode_private *ip; 283 284 ip = kmem_cache_alloc(dlmfs_inode_cache, GFP_NOFS); 285 if (!ip) 286 return NULL; 287 288 return &ip->ip_vfs_inode; 289 } 290 291 static void dlmfs_free_inode(struct inode *inode) 292 { 293 kmem_cache_free(dlmfs_inode_cache, DLMFS_I(inode)); 294 } 295 296 static void dlmfs_evict_inode(struct inode *inode) 297 { 298 int status; 299 struct dlmfs_inode_private *ip; 300 301 clear_inode(inode); 302 303 mlog(0, "inode %lu\n", inode->i_ino); 304 305 ip = DLMFS_I(inode); 306 307 if (S_ISREG(inode->i_mode)) { 308 status = user_dlm_destroy_lock(&ip->ip_lockres); 309 if (status < 0) 310 mlog_errno(status); 311 iput(ip->ip_parent); 312 goto clear_fields; 313 } 314 315 mlog(0, "we're a directory, ip->ip_conn = 0x%p\n", ip->ip_conn); 316 /* we must be a directory. If required, lets unregister the 317 * dlm context now. */ 318 if (ip->ip_conn) 319 user_dlm_unregister(ip->ip_conn); 320 clear_fields: 321 ip->ip_parent = NULL; 322 ip->ip_conn = NULL; 323 } 324 325 static struct inode *dlmfs_get_root_inode(struct super_block *sb) 326 { 327 struct inode *inode = new_inode(sb); 328 umode_t mode = S_IFDIR | 0755; 329 330 if (inode) { 331 inode->i_ino = get_next_ino(); 332 inode_init_owner(inode, NULL, mode); 333 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode); 334 inc_nlink(inode); 335 336 inode->i_fop = &simple_dir_operations; 337 inode->i_op = &dlmfs_root_inode_operations; 338 } 339 340 return inode; 341 } 342 343 static struct inode *dlmfs_get_inode(struct inode *parent, 344 struct dentry *dentry, 345 umode_t mode) 346 { 347 struct super_block *sb = parent->i_sb; 348 struct inode * inode = new_inode(sb); 349 struct dlmfs_inode_private *ip; 350 351 if (!inode) 352 return NULL; 353 354 inode->i_ino = get_next_ino(); 355 inode_init_owner(inode, parent, mode); 356 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode); 357 358 ip = DLMFS_I(inode); 359 ip->ip_conn = DLMFS_I(parent)->ip_conn; 360 361 switch (mode & S_IFMT) { 362 default: 363 /* for now we don't support anything other than 364 * directories and regular files. */ 365 BUG(); 366 break; 367 case S_IFREG: 368 inode->i_op = &dlmfs_file_inode_operations; 369 inode->i_fop = &dlmfs_file_operations; 370 371 i_size_write(inode, DLM_LVB_LEN); 372 373 user_dlm_lock_res_init(&ip->ip_lockres, dentry); 374 375 /* released at clear_inode time, this insures that we 376 * get to drop the dlm reference on each lock *before* 377 * we call the unregister code for releasing parent 378 * directories. */ 379 ip->ip_parent = igrab(parent); 380 BUG_ON(!ip->ip_parent); 381 break; 382 case S_IFDIR: 383 inode->i_op = &dlmfs_dir_inode_operations; 384 inode->i_fop = &simple_dir_operations; 385 386 /* directory inodes start off with i_nlink == 387 * 2 (for "." entry) */ 388 inc_nlink(inode); 389 break; 390 } 391 return inode; 392 } 393 394 /* 395 * File creation. Allocate an inode, and we're done.. 396 */ 397 /* SMP-safe */ 398 static int dlmfs_mkdir(struct inode * dir, 399 struct dentry * dentry, 400 umode_t mode) 401 { 402 int status; 403 struct inode *inode = NULL; 404 const struct qstr *domain = &dentry->d_name; 405 struct dlmfs_inode_private *ip; 406 struct ocfs2_cluster_connection *conn; 407 408 mlog(0, "mkdir %.*s\n", domain->len, domain->name); 409 410 /* verify that we have a proper domain */ 411 if (domain->len >= GROUP_NAME_MAX) { 412 status = -EINVAL; 413 mlog(ML_ERROR, "invalid domain name for directory.\n"); 414 goto bail; 415 } 416 417 inode = dlmfs_get_inode(dir, dentry, mode | S_IFDIR); 418 if (!inode) { 419 status = -ENOMEM; 420 mlog_errno(status); 421 goto bail; 422 } 423 424 ip = DLMFS_I(inode); 425 426 conn = user_dlm_register(domain); 427 if (IS_ERR(conn)) { 428 status = PTR_ERR(conn); 429 mlog(ML_ERROR, "Error %d could not register domain \"%.*s\"\n", 430 status, domain->len, domain->name); 431 goto bail; 432 } 433 ip->ip_conn = conn; 434 435 inc_nlink(dir); 436 d_instantiate(dentry, inode); 437 dget(dentry); /* Extra count - pin the dentry in core */ 438 439 status = 0; 440 bail: 441 if (status < 0) 442 iput(inode); 443 return status; 444 } 445 446 static int dlmfs_create(struct inode *dir, 447 struct dentry *dentry, 448 umode_t mode, 449 bool excl) 450 { 451 int status = 0; 452 struct inode *inode; 453 const struct qstr *name = &dentry->d_name; 454 455 mlog(0, "create %.*s\n", name->len, name->name); 456 457 /* verify name is valid and doesn't contain any dlm reserved 458 * characters */ 459 if (name->len >= USER_DLM_LOCK_ID_MAX_LEN || 460 name->name[0] == '$') { 461 status = -EINVAL; 462 mlog(ML_ERROR, "invalid lock name, %.*s\n", name->len, 463 name->name); 464 goto bail; 465 } 466 467 inode = dlmfs_get_inode(dir, dentry, mode | S_IFREG); 468 if (!inode) { 469 status = -ENOMEM; 470 mlog_errno(status); 471 goto bail; 472 } 473 474 d_instantiate(dentry, inode); 475 dget(dentry); /* Extra count - pin the dentry in core */ 476 bail: 477 return status; 478 } 479 480 static int dlmfs_unlink(struct inode *dir, 481 struct dentry *dentry) 482 { 483 int status; 484 struct inode *inode = d_inode(dentry); 485 486 mlog(0, "unlink inode %lu\n", inode->i_ino); 487 488 /* if there are no current holders, or none that are waiting 489 * to acquire a lock, this basically destroys our lockres. */ 490 status = user_dlm_destroy_lock(&DLMFS_I(inode)->ip_lockres); 491 if (status < 0) { 492 mlog(ML_ERROR, "unlink %pd, error %d from destroy\n", 493 dentry, status); 494 goto bail; 495 } 496 status = simple_unlink(dir, dentry); 497 bail: 498 return status; 499 } 500 501 static int dlmfs_fill_super(struct super_block * sb, 502 void * data, 503 int silent) 504 { 505 sb->s_maxbytes = MAX_LFS_FILESIZE; 506 sb->s_blocksize = PAGE_SIZE; 507 sb->s_blocksize_bits = PAGE_SHIFT; 508 sb->s_magic = DLMFS_MAGIC; 509 sb->s_op = &dlmfs_ops; 510 sb->s_root = d_make_root(dlmfs_get_root_inode(sb)); 511 if (!sb->s_root) 512 return -ENOMEM; 513 return 0; 514 } 515 516 static const struct file_operations dlmfs_file_operations = { 517 .open = dlmfs_file_open, 518 .release = dlmfs_file_release, 519 .poll = dlmfs_file_poll, 520 .read = dlmfs_file_read, 521 .write = dlmfs_file_write, 522 .llseek = default_llseek, 523 }; 524 525 static const struct inode_operations dlmfs_dir_inode_operations = { 526 .create = dlmfs_create, 527 .lookup = simple_lookup, 528 .unlink = dlmfs_unlink, 529 }; 530 531 /* this way we can restrict mkdir to only the toplevel of the fs. */ 532 static const struct inode_operations dlmfs_root_inode_operations = { 533 .lookup = simple_lookup, 534 .mkdir = dlmfs_mkdir, 535 .rmdir = simple_rmdir, 536 }; 537 538 static const struct super_operations dlmfs_ops = { 539 .statfs = simple_statfs, 540 .alloc_inode = dlmfs_alloc_inode, 541 .free_inode = dlmfs_free_inode, 542 .evict_inode = dlmfs_evict_inode, 543 .drop_inode = generic_delete_inode, 544 }; 545 546 static const struct inode_operations dlmfs_file_inode_operations = { 547 .getattr = simple_getattr, 548 .setattr = dlmfs_file_setattr, 549 }; 550 551 static struct dentry *dlmfs_mount(struct file_system_type *fs_type, 552 int flags, const char *dev_name, void *data) 553 { 554 return mount_nodev(fs_type, flags, data, dlmfs_fill_super); 555 } 556 557 static struct file_system_type dlmfs_fs_type = { 558 .owner = THIS_MODULE, 559 .name = "ocfs2_dlmfs", 560 .mount = dlmfs_mount, 561 .kill_sb = kill_litter_super, 562 }; 563 MODULE_ALIAS_FS("ocfs2_dlmfs"); 564 565 static int __init init_dlmfs_fs(void) 566 { 567 int status; 568 int cleanup_inode = 0, cleanup_worker = 0; 569 570 dlmfs_inode_cache = kmem_cache_create("dlmfs_inode_cache", 571 sizeof(struct dlmfs_inode_private), 572 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT| 573 SLAB_MEM_SPREAD|SLAB_ACCOUNT), 574 dlmfs_init_once); 575 if (!dlmfs_inode_cache) { 576 status = -ENOMEM; 577 goto bail; 578 } 579 cleanup_inode = 1; 580 581 user_dlm_worker = alloc_workqueue("user_dlm", WQ_MEM_RECLAIM, 0); 582 if (!user_dlm_worker) { 583 status = -ENOMEM; 584 goto bail; 585 } 586 cleanup_worker = 1; 587 588 user_dlm_set_locking_protocol(); 589 status = register_filesystem(&dlmfs_fs_type); 590 bail: 591 if (status) { 592 if (cleanup_inode) 593 kmem_cache_destroy(dlmfs_inode_cache); 594 if (cleanup_worker) 595 destroy_workqueue(user_dlm_worker); 596 } else 597 printk("OCFS2 User DLM kernel interface loaded\n"); 598 return status; 599 } 600 601 static void __exit exit_dlmfs_fs(void) 602 { 603 unregister_filesystem(&dlmfs_fs_type); 604 605 destroy_workqueue(user_dlm_worker); 606 607 /* 608 * Make sure all delayed rcu free inodes are flushed before we 609 * destroy cache. 610 */ 611 rcu_barrier(); 612 kmem_cache_destroy(dlmfs_inode_cache); 613 614 } 615 616 MODULE_AUTHOR("Oracle"); 617 MODULE_LICENSE("GPL"); 618 MODULE_DESCRIPTION("OCFS2 DLM-Filesystem"); 619 620 module_init(init_dlmfs_fs) 621 module_exit(exit_dlmfs_fs) 622