1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * (C) 2001 Clemson University and The University of Chicago 4 * 5 * See COPYING in top-level directory. 6 */ 7 8 #include "protocol.h" 9 #include "orangefs-kernel.h" 10 #include "orangefs-bufmap.h" 11 12 #include <linux/hashtable.h> 13 #include <linux/seq_file.h> 14 15 /* a cache for orangefs-inode objects (i.e. orangefs inode private data) */ 16 static struct kmem_cache *orangefs_inode_cache; 17 18 /* list for storing orangefs specific superblocks in use */ 19 LIST_HEAD(orangefs_superblocks); 20 21 DEFINE_SPINLOCK(orangefs_superblocks_lock); 22 23 enum { 24 Opt_acl, 25 Opt_intr, 26 Opt_local_lock, 27 }; 28 29 const struct fs_parameter_spec orangefs_fs_param_spec[] = { 30 fsparam_flag ("acl", Opt_acl), 31 fsparam_flag ("intr", Opt_intr), 32 fsparam_flag ("local_lock", Opt_local_lock), 33 {} 34 }; 35 36 uint64_t orangefs_features; 37 38 static int orangefs_show_options(struct seq_file *m, struct dentry *root) 39 { 40 struct orangefs_sb_info_s *orangefs_sb = ORANGEFS_SB(root->d_sb); 41 42 if (root->d_sb->s_flags & SB_POSIXACL) 43 seq_puts(m, ",acl"); 44 if (orangefs_sb->flags & ORANGEFS_OPT_INTR) 45 seq_puts(m, ",intr"); 46 if (orangefs_sb->flags & ORANGEFS_OPT_LOCAL_LOCK) 47 seq_puts(m, ",local_lock"); 48 return 0; 49 } 50 51 static int orangefs_parse_param(struct fs_context *fc, 52 struct fs_parameter *param) 53 { 54 struct orangefs_sb_info_s *orangefs_sb = fc->s_fs_info; 55 struct fs_parse_result result; 56 int opt; 57 58 opt = fs_parse(fc, orangefs_fs_param_spec, param, &result); 59 if (opt < 0) 60 return opt; 61 62 switch (opt) { 63 case Opt_acl: 64 fc->sb_flags |= SB_POSIXACL; 65 break; 66 case Opt_intr: 67 orangefs_sb->flags |= ORANGEFS_OPT_INTR; 68 break; 69 case Opt_local_lock: 70 orangefs_sb->flags |= ORANGEFS_OPT_LOCAL_LOCK; 71 break; 72 } 73 74 return 0; 75 } 76 77 static void orangefs_inode_cache_ctor(void *req) 78 { 79 struct orangefs_inode_s *orangefs_inode = req; 80 81 inode_init_once(&orangefs_inode->vfs_inode); 82 init_rwsem(&orangefs_inode->xattr_sem); 83 } 84 85 static struct inode *orangefs_alloc_inode(struct super_block *sb) 86 { 87 struct orangefs_inode_s *orangefs_inode; 88 89 orangefs_inode = alloc_inode_sb(sb, orangefs_inode_cache, GFP_KERNEL); 90 if (!orangefs_inode) 91 return NULL; 92 93 /* 94 * We want to clear everything except for rw_semaphore and the 95 * vfs_inode. 96 */ 97 memset(&orangefs_inode->refn.khandle, 0, 16); 98 orangefs_inode->refn.fs_id = ORANGEFS_FS_ID_NULL; 99 orangefs_inode->last_failed_block_index_read = 0; 100 memset(orangefs_inode->link_target, 0, sizeof(orangefs_inode->link_target)); 101 102 gossip_debug(GOSSIP_SUPER_DEBUG, 103 "orangefs_alloc_inode: allocated %p\n", 104 &orangefs_inode->vfs_inode); 105 return &orangefs_inode->vfs_inode; 106 } 107 108 static void orangefs_free_inode(struct inode *inode) 109 { 110 struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode); 111 struct orangefs_cached_xattr *cx; 112 struct hlist_node *tmp; 113 int i; 114 115 hash_for_each_safe(orangefs_inode->xattr_cache, i, tmp, cx, node) { 116 hlist_del(&cx->node); 117 kfree(cx); 118 } 119 120 kmem_cache_free(orangefs_inode_cache, orangefs_inode); 121 } 122 123 static void orangefs_destroy_inode(struct inode *inode) 124 { 125 struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode); 126 127 gossip_debug(GOSSIP_SUPER_DEBUG, 128 "%s: deallocated %p destroying inode %pU\n", 129 __func__, orangefs_inode, get_khandle_from_ino(inode)); 130 } 131 132 static int orangefs_write_inode(struct inode *inode, 133 struct writeback_control *wbc) 134 { 135 gossip_debug(GOSSIP_SUPER_DEBUG, "orangefs_write_inode\n"); 136 return orangefs_inode_setattr(inode); 137 } 138 139 /* 140 * NOTE: information filled in here is typically reflected in the 141 * output of the system command 'df' 142 */ 143 static int orangefs_statfs(struct dentry *dentry, struct kstatfs *buf) 144 { 145 int ret = -ENOMEM; 146 struct orangefs_kernel_op_s *new_op = NULL; 147 int flags = 0; 148 struct super_block *sb = NULL; 149 150 sb = dentry->d_sb; 151 152 gossip_debug(GOSSIP_SUPER_DEBUG, 153 "%s: called on sb %p (fs_id is %d)\n", 154 __func__, 155 sb, 156 (int)(ORANGEFS_SB(sb)->fs_id)); 157 158 new_op = op_alloc(ORANGEFS_VFS_OP_STATFS); 159 if (!new_op) 160 return ret; 161 new_op->upcall.req.statfs.fs_id = ORANGEFS_SB(sb)->fs_id; 162 163 if (ORANGEFS_SB(sb)->flags & ORANGEFS_OPT_INTR) 164 flags = ORANGEFS_OP_INTERRUPTIBLE; 165 166 ret = service_operation(new_op, "orangefs_statfs", flags); 167 168 if (new_op->downcall.status < 0) 169 goto out_op_release; 170 171 gossip_debug(GOSSIP_SUPER_DEBUG, 172 "%s: got %ld blocks available | " 173 "%ld blocks total | %ld block size | " 174 "%ld files total | %ld files avail\n", 175 __func__, 176 (long)new_op->downcall.resp.statfs.blocks_avail, 177 (long)new_op->downcall.resp.statfs.blocks_total, 178 (long)new_op->downcall.resp.statfs.block_size, 179 (long)new_op->downcall.resp.statfs.files_total, 180 (long)new_op->downcall.resp.statfs.files_avail); 181 182 buf->f_type = sb->s_magic; 183 buf->f_fsid.val[0] = ORANGEFS_SB(sb)->fs_id; 184 buf->f_fsid.val[1] = ORANGEFS_SB(sb)->id; 185 buf->f_bsize = new_op->downcall.resp.statfs.block_size; 186 buf->f_namelen = ORANGEFS_NAME_MAX; 187 188 buf->f_blocks = (sector_t) new_op->downcall.resp.statfs.blocks_total; 189 buf->f_bfree = (sector_t) new_op->downcall.resp.statfs.blocks_avail; 190 buf->f_bavail = (sector_t) new_op->downcall.resp.statfs.blocks_avail; 191 buf->f_files = (sector_t) new_op->downcall.resp.statfs.files_total; 192 buf->f_ffree = (sector_t) new_op->downcall.resp.statfs.files_avail; 193 buf->f_frsize = 0; 194 195 out_op_release: 196 op_release(new_op); 197 gossip_debug(GOSSIP_SUPER_DEBUG, "%s: returning %d\n", __func__, ret); 198 return ret; 199 } 200 201 /* 202 * Remount as initiated by VFS layer. We just need to reparse the mount 203 * options, no need to signal pvfs2-client-core about it. 204 */ 205 static int orangefs_reconfigure(struct fs_context *fc) 206 { 207 struct super_block *sb = fc->root->d_sb; 208 struct orangefs_sb_info_s *orangefs_sb = ORANGEFS_SB(sb); 209 struct orangefs_sb_info_s *revised = fc->s_fs_info; 210 unsigned int flags; 211 212 flags = orangefs_sb->flags; 213 flags &= ~(ORANGEFS_OPT_INTR | ORANGEFS_OPT_LOCAL_LOCK); 214 flags |= revised->flags; 215 WRITE_ONCE(orangefs_sb->flags, flags); 216 217 gossip_debug(GOSSIP_SUPER_DEBUG, "orangefs_reconfigure: called\n"); 218 return 0; 219 } 220 221 /* 222 * Remount as initiated by pvfs2-client-core on restart. This is used to 223 * repopulate mount information left from previous pvfs2-client-core. 224 * 225 * the idea here is that given a valid superblock, we're 226 * re-initializing the user space client with the initial mount 227 * information specified when the super block was first initialized. 228 * this is very different than the first initialization/creation of a 229 * superblock. we use the special service_priority_operation to make 230 * sure that the mount gets ahead of any other pending operation that 231 * is waiting for servicing. this means that the pvfs2-client won't 232 * fail to start several times for all other pending operations before 233 * the client regains all of the mount information from us. 234 * NOTE: this function assumes that the request_mutex is already acquired! 235 */ 236 int orangefs_remount(struct orangefs_sb_info_s *orangefs_sb) 237 { 238 struct orangefs_kernel_op_s *new_op; 239 int ret = -EINVAL; 240 241 gossip_debug(GOSSIP_SUPER_DEBUG, "orangefs_remount: called\n"); 242 243 new_op = op_alloc(ORANGEFS_VFS_OP_FS_MOUNT); 244 if (!new_op) 245 return -ENOMEM; 246 strscpy(new_op->upcall.req.fs_mount.orangefs_config_server, 247 orangefs_sb->devname); 248 249 gossip_debug(GOSSIP_SUPER_DEBUG, 250 "Attempting ORANGEFS Remount via host %s\n", 251 new_op->upcall.req.fs_mount.orangefs_config_server); 252 253 /* 254 * we assume that the calling function has already acquired the 255 * request_mutex to prevent other operations from bypassing 256 * this one 257 */ 258 ret = service_operation(new_op, "orangefs_remount", 259 ORANGEFS_OP_PRIORITY | ORANGEFS_OP_NO_MUTEX); 260 gossip_debug(GOSSIP_SUPER_DEBUG, 261 "orangefs_remount: mount got return value of %d\n", 262 ret); 263 if (ret == 0) { 264 /* 265 * store the id assigned to this sb -- it's just a 266 * short-lived mapping that the system interface uses 267 * to map this superblock to a particular mount entry 268 */ 269 orangefs_sb->id = new_op->downcall.resp.fs_mount.id; 270 orangefs_sb->mount_pending = 0; 271 } 272 273 op_release(new_op); 274 275 if (orangefs_userspace_version >= 20906) { 276 new_op = op_alloc(ORANGEFS_VFS_OP_FEATURES); 277 if (!new_op) 278 return -ENOMEM; 279 new_op->upcall.req.features.features = 0; 280 ret = service_operation(new_op, "orangefs_features", 281 ORANGEFS_OP_PRIORITY | ORANGEFS_OP_NO_MUTEX); 282 if (!ret) 283 orangefs_features = 284 new_op->downcall.resp.features.features; 285 else 286 orangefs_features = 0; 287 op_release(new_op); 288 } else { 289 orangefs_features = 0; 290 } 291 292 return ret; 293 } 294 295 int fsid_key_table_initialize(void) 296 { 297 return 0; 298 } 299 300 void fsid_key_table_finalize(void) 301 { 302 } 303 304 static const struct super_operations orangefs_s_ops = { 305 .alloc_inode = orangefs_alloc_inode, 306 .free_inode = orangefs_free_inode, 307 .destroy_inode = orangefs_destroy_inode, 308 .write_inode = orangefs_write_inode, 309 .drop_inode = generic_delete_inode, 310 .statfs = orangefs_statfs, 311 .show_options = orangefs_show_options, 312 }; 313 314 static struct dentry *orangefs_fh_to_dentry(struct super_block *sb, 315 struct fid *fid, 316 int fh_len, 317 int fh_type) 318 { 319 struct orangefs_object_kref refn; 320 321 if (fh_len < 5 || fh_type > 2) 322 return NULL; 323 324 ORANGEFS_khandle_from(&(refn.khandle), fid->raw, 16); 325 refn.fs_id = (u32) fid->raw[4]; 326 gossip_debug(GOSSIP_SUPER_DEBUG, 327 "fh_to_dentry: handle %pU, fs_id %d\n", 328 &refn.khandle, 329 refn.fs_id); 330 331 return d_obtain_alias(orangefs_iget(sb, &refn)); 332 } 333 334 static int orangefs_encode_fh(struct inode *inode, 335 __u32 *fh, 336 int *max_len, 337 struct inode *parent) 338 { 339 int len = parent ? 10 : 5; 340 int type = 1; 341 struct orangefs_object_kref refn; 342 343 if (*max_len < len) { 344 gossip_err("fh buffer is too small for encoding\n"); 345 *max_len = len; 346 type = 255; 347 goto out; 348 } 349 350 refn = ORANGEFS_I(inode)->refn; 351 ORANGEFS_khandle_to(&refn.khandle, fh, 16); 352 fh[4] = refn.fs_id; 353 354 gossip_debug(GOSSIP_SUPER_DEBUG, 355 "Encoding fh: handle %pU, fsid %u\n", 356 &refn.khandle, 357 refn.fs_id); 358 359 360 if (parent) { 361 refn = ORANGEFS_I(parent)->refn; 362 ORANGEFS_khandle_to(&refn.khandle, (char *) fh + 20, 16); 363 fh[9] = refn.fs_id; 364 365 type = 2; 366 gossip_debug(GOSSIP_SUPER_DEBUG, 367 "Encoding parent: handle %pU, fsid %u\n", 368 &refn.khandle, 369 refn.fs_id); 370 } 371 *max_len = len; 372 373 out: 374 return type; 375 } 376 377 static const struct export_operations orangefs_export_ops = { 378 .encode_fh = orangefs_encode_fh, 379 .fh_to_dentry = orangefs_fh_to_dentry, 380 }; 381 382 static int orangefs_unmount(int id, __s32 fs_id, const char *devname) 383 { 384 struct orangefs_kernel_op_s *op; 385 int r; 386 op = op_alloc(ORANGEFS_VFS_OP_FS_UMOUNT); 387 if (!op) 388 return -ENOMEM; 389 op->upcall.req.fs_umount.id = id; 390 op->upcall.req.fs_umount.fs_id = fs_id; 391 strscpy(op->upcall.req.fs_umount.orangefs_config_server, devname); 392 r = service_operation(op, "orangefs_fs_umount", 0); 393 /* Not much to do about an error here. */ 394 if (r) 395 gossip_err("orangefs_unmount: service_operation %d\n", r); 396 op_release(op); 397 return r; 398 } 399 400 static int orangefs_fill_sb(struct super_block *sb, 401 struct fs_context *fc, 402 struct orangefs_fs_mount_response *fs_mount) 403 { 404 int ret; 405 struct inode *root; 406 struct dentry *root_dentry; 407 struct orangefs_object_kref root_object; 408 409 ORANGEFS_SB(sb)->sb = sb; 410 411 ORANGEFS_SB(sb)->root_khandle = fs_mount->root_khandle; 412 ORANGEFS_SB(sb)->fs_id = fs_mount->fs_id; 413 ORANGEFS_SB(sb)->id = fs_mount->id; 414 415 /* Hang the xattr handlers off the superblock */ 416 sb->s_xattr = orangefs_xattr_handlers; 417 sb->s_magic = ORANGEFS_SUPER_MAGIC; 418 sb->s_op = &orangefs_s_ops; 419 sb->s_d_op = &orangefs_dentry_operations; 420 421 sb->s_blocksize = PAGE_SIZE; 422 sb->s_blocksize_bits = PAGE_SHIFT; 423 sb->s_maxbytes = MAX_LFS_FILESIZE; 424 425 ret = super_setup_bdi(sb); 426 if (ret) 427 return ret; 428 429 root_object.khandle = ORANGEFS_SB(sb)->root_khandle; 430 root_object.fs_id = ORANGEFS_SB(sb)->fs_id; 431 gossip_debug(GOSSIP_SUPER_DEBUG, 432 "get inode %pU, fsid %d\n", 433 &root_object.khandle, 434 root_object.fs_id); 435 436 root = orangefs_iget(sb, &root_object); 437 if (IS_ERR(root)) 438 return PTR_ERR(root); 439 440 gossip_debug(GOSSIP_SUPER_DEBUG, 441 "Allocated root inode [%p] with mode %x\n", 442 root, 443 root->i_mode); 444 445 /* allocates and places root dentry in dcache */ 446 root_dentry = d_make_root(root); 447 if (!root_dentry) 448 return -ENOMEM; 449 450 sb->s_export_op = &orangefs_export_ops; 451 sb->s_root = root_dentry; 452 return 0; 453 } 454 455 static int orangefs_get_tree(struct fs_context *fc) 456 { 457 int ret; 458 struct super_block *sb = ERR_PTR(-EINVAL); 459 struct orangefs_kernel_op_s *new_op; 460 461 if (!fc->source) 462 return invalf(fc, "Device name not specified.\n"); 463 464 gossip_debug(GOSSIP_SUPER_DEBUG, 465 "orangefs_mount: called with devname %s\n", 466 fc->source); 467 468 new_op = op_alloc(ORANGEFS_VFS_OP_FS_MOUNT); 469 if (!new_op) 470 return -ENOMEM; 471 472 strscpy(new_op->upcall.req.fs_mount.orangefs_config_server, fc->source); 473 474 gossip_debug(GOSSIP_SUPER_DEBUG, 475 "Attempting ORANGEFS Mount via host %s\n", 476 new_op->upcall.req.fs_mount.orangefs_config_server); 477 478 ret = service_operation(new_op, "orangefs_mount", 0); 479 gossip_debug(GOSSIP_SUPER_DEBUG, 480 "orangefs_mount: mount got return value of %d\n", ret); 481 if (ret) 482 goto free_op; 483 484 if (new_op->downcall.resp.fs_mount.fs_id == ORANGEFS_FS_ID_NULL) { 485 gossip_err("ERROR: Retrieved null fs_id\n"); 486 ret = -EINVAL; 487 goto free_op; 488 } 489 490 sb = sget_fc(fc, NULL, set_anon_super_fc); 491 492 if (IS_ERR(sb)) { 493 ret = PTR_ERR(sb); 494 orangefs_unmount(new_op->downcall.resp.fs_mount.id, 495 new_op->downcall.resp.fs_mount.fs_id, 496 fc->source); 497 goto free_op; 498 } 499 500 /* init our private orangefs sb info */ 501 ret = orangefs_fill_sb(sb, fc, &new_op->downcall.resp.fs_mount); 502 503 if (ret) 504 goto free_sb_and_op; 505 506 /* 507 * on successful mount, store the devname and data 508 * used 509 */ 510 strscpy(ORANGEFS_SB(sb)->devname, fc->source); 511 512 /* mount_pending must be cleared */ 513 ORANGEFS_SB(sb)->mount_pending = 0; 514 515 /* 516 * finally, add this sb to our list of known orangefs 517 * sb's 518 */ 519 gossip_debug(GOSSIP_SUPER_DEBUG, 520 "Adding SB %p to orangefs superblocks\n", 521 ORANGEFS_SB(sb)); 522 spin_lock(&orangefs_superblocks_lock); 523 list_add_tail(&ORANGEFS_SB(sb)->list, &orangefs_superblocks); 524 spin_unlock(&orangefs_superblocks_lock); 525 op_release(new_op); 526 527 /* Must be removed from the list now. */ 528 ORANGEFS_SB(sb)->no_list = 0; 529 530 if (orangefs_userspace_version >= 20906) { 531 new_op = op_alloc(ORANGEFS_VFS_OP_FEATURES); 532 if (!new_op) 533 return -ENOMEM; 534 new_op->upcall.req.features.features = 0; 535 ret = service_operation(new_op, "orangefs_features", 0); 536 orangefs_features = new_op->downcall.resp.features.features; 537 op_release(new_op); 538 } else { 539 orangefs_features = 0; 540 } 541 542 fc->root = dget(sb->s_root); 543 return 0; 544 545 free_sb_and_op: 546 /* Will call orangefs_kill_sb with sb not in list. */ 547 ORANGEFS_SB(sb)->no_list = 1; 548 /* ORANGEFS_VFS_OP_FS_UMOUNT is done by orangefs_kill_sb. */ 549 deactivate_locked_super(sb); 550 free_op: 551 gossip_err("orangefs_mount: mount request failed with %d\n", ret); 552 if (ret == -EINVAL) { 553 gossip_err("Ensure that all orangefs-servers have the same FS configuration files\n"); 554 gossip_err("Look at pvfs2-client-core log file (typically /tmp/pvfs2-client.log) for more details\n"); 555 } 556 557 op_release(new_op); 558 559 return ret; 560 } 561 562 static void orangefs_free_fc(struct fs_context *fc) 563 { 564 kfree(fc->s_fs_info); 565 } 566 567 static const struct fs_context_operations orangefs_context_ops = { 568 .free = orangefs_free_fc, 569 .parse_param = orangefs_parse_param, 570 .get_tree = orangefs_get_tree, 571 .reconfigure = orangefs_reconfigure, 572 }; 573 574 /* 575 * Set up the filesystem mount context. 576 */ 577 int orangefs_init_fs_context(struct fs_context *fc) 578 { 579 struct orangefs_sb_info_s *osi; 580 581 osi = kzalloc(sizeof(struct orangefs_sb_info_s), GFP_KERNEL); 582 if (!osi) 583 return -ENOMEM; 584 585 /* 586 * Force any potential flags that might be set from the mount 587 * to zero, ie, initialize to unset. 588 */ 589 fc->sb_flags_mask &= ~SB_POSIXACL; 590 osi->flags &= ~ORANGEFS_OPT_INTR; 591 osi->flags &= ~ORANGEFS_OPT_LOCAL_LOCK; 592 593 fc->s_fs_info = osi; 594 fc->ops = &orangefs_context_ops; 595 return 0; 596 } 597 598 void orangefs_kill_sb(struct super_block *sb) 599 { 600 int r; 601 gossip_debug(GOSSIP_SUPER_DEBUG, "orangefs_kill_sb: called\n"); 602 603 /* provided sb cleanup */ 604 kill_anon_super(sb); 605 606 if (!ORANGEFS_SB(sb)) { 607 mutex_lock(&orangefs_request_mutex); 608 mutex_unlock(&orangefs_request_mutex); 609 return; 610 } 611 /* 612 * issue the unmount to userspace to tell it to remove the 613 * dynamic mount info it has for this superblock 614 */ 615 r = orangefs_unmount(ORANGEFS_SB(sb)->id, ORANGEFS_SB(sb)->fs_id, 616 ORANGEFS_SB(sb)->devname); 617 if (!r) 618 ORANGEFS_SB(sb)->mount_pending = 1; 619 620 if (!ORANGEFS_SB(sb)->no_list) { 621 /* remove the sb from our list of orangefs specific sb's */ 622 spin_lock(&orangefs_superblocks_lock); 623 /* not list_del_init */ 624 __list_del_entry(&ORANGEFS_SB(sb)->list); 625 ORANGEFS_SB(sb)->list.prev = NULL; 626 spin_unlock(&orangefs_superblocks_lock); 627 } 628 629 /* 630 * make sure that ORANGEFS_DEV_REMOUNT_ALL loop that might've seen us 631 * gets completed before we free the dang thing. 632 */ 633 mutex_lock(&orangefs_request_mutex); 634 mutex_unlock(&orangefs_request_mutex); 635 636 /* free the orangefs superblock private data */ 637 kfree(ORANGEFS_SB(sb)); 638 } 639 640 int orangefs_inode_cache_initialize(void) 641 { 642 orangefs_inode_cache = kmem_cache_create_usercopy( 643 "orangefs_inode_cache", 644 sizeof(struct orangefs_inode_s), 645 0, 646 0, 647 offsetof(struct orangefs_inode_s, 648 link_target), 649 sizeof_field(struct orangefs_inode_s, 650 link_target), 651 orangefs_inode_cache_ctor); 652 653 if (!orangefs_inode_cache) { 654 gossip_err("Cannot create orangefs_inode_cache\n"); 655 return -ENOMEM; 656 } 657 return 0; 658 } 659 660 int orangefs_inode_cache_finalize(void) 661 { 662 kmem_cache_destroy(orangefs_inode_cache); 663 return 0; 664 } 665