1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * This file is part of UBIFS. 4 * 5 * Copyright (C) 2006-2008 Nokia Corporation. 6 * 7 * Authors: Artem Bityutskiy (Битюцкий Артём) 8 * Adrian Hunter 9 */ 10 11 /* 12 * This file implements UBIFS initialization and VFS superblock operations. Some 13 * initialization stuff which is rather large and complex is placed at 14 * corresponding subsystems, but most of it is here. 15 */ 16 17 #include <linux/init.h> 18 #include <linux/slab.h> 19 #include <linux/module.h> 20 #include <linux/ctype.h> 21 #include <linux/kthread.h> 22 #include <linux/parser.h> 23 #include <linux/seq_file.h> 24 #include <linux/mount.h> 25 #include <linux/math64.h> 26 #include <linux/writeback.h> 27 #include "ubifs.h" 28 29 static int ubifs_default_version_set(const char *val, const struct kernel_param *kp) 30 { 31 int n = 0, ret; 32 33 ret = kstrtoint(val, 10, &n); 34 if (ret != 0 || n < 4 || n > UBIFS_FORMAT_VERSION) 35 return -EINVAL; 36 return param_set_int(val, kp); 37 } 38 39 static const struct kernel_param_ops ubifs_default_version_ops = { 40 .set = ubifs_default_version_set, 41 .get = param_get_int, 42 }; 43 44 int ubifs_default_version = UBIFS_FORMAT_VERSION; 45 module_param_cb(default_version, &ubifs_default_version_ops, &ubifs_default_version, 0600); 46 47 /* 48 * Maximum amount of memory we may 'kmalloc()' without worrying that we are 49 * allocating too much. 50 */ 51 #define UBIFS_KMALLOC_OK (128*1024) 52 53 /* Slab cache for UBIFS inodes */ 54 static struct kmem_cache *ubifs_inode_slab; 55 56 /* UBIFS TNC shrinker description */ 57 static struct shrinker *ubifs_shrinker_info; 58 59 /** 60 * validate_inode - validate inode. 61 * @c: UBIFS file-system description object 62 * @inode: the inode to validate 63 * 64 * This is a helper function for 'ubifs_iget()' which validates various fields 65 * of a newly built inode to make sure they contain sane values and prevent 66 * possible vulnerabilities. Returns zero if the inode is all right and 67 * a non-zero error code if not. 68 */ 69 static int validate_inode(struct ubifs_info *c, const struct inode *inode) 70 { 71 int err; 72 const struct ubifs_inode *ui = ubifs_inode(inode); 73 74 if (inode->i_size > c->max_inode_sz) { 75 ubifs_err(c, "inode is too large (%lld)", 76 (long long)inode->i_size); 77 return 1; 78 } 79 80 if (ui->compr_type >= UBIFS_COMPR_TYPES_CNT) { 81 ubifs_err(c, "unknown compression type %d", ui->compr_type); 82 return 2; 83 } 84 85 if (ui->xattr_names + ui->xattr_cnt > XATTR_LIST_MAX) 86 return 3; 87 88 if (ui->data_len < 0 || ui->data_len > UBIFS_MAX_INO_DATA) 89 return 4; 90 91 if (ui->xattr && !S_ISREG(inode->i_mode)) 92 return 5; 93 94 if (!ubifs_compr_present(c, ui->compr_type)) { 95 ubifs_warn(c, "inode %lu uses '%s' compression, but it was not compiled in", 96 inode->i_ino, ubifs_compr_name(c, ui->compr_type)); 97 } 98 99 err = dbg_check_dir(c, inode); 100 return err; 101 } 102 103 struct inode *ubifs_iget(struct super_block *sb, unsigned long inum) 104 { 105 int err; 106 union ubifs_key key; 107 struct ubifs_ino_node *ino; 108 struct ubifs_info *c = sb->s_fs_info; 109 struct inode *inode; 110 struct ubifs_inode *ui; 111 112 dbg_gen("inode %lu", inum); 113 114 inode = iget_locked(sb, inum); 115 if (!inode) 116 return ERR_PTR(-ENOMEM); 117 if (!(inode->i_state & I_NEW)) 118 return inode; 119 ui = ubifs_inode(inode); 120 121 ino = kmalloc(UBIFS_MAX_INO_NODE_SZ, GFP_NOFS); 122 if (!ino) { 123 err = -ENOMEM; 124 goto out; 125 } 126 127 ino_key_init(c, &key, inode->i_ino); 128 129 err = ubifs_tnc_lookup(c, &key, ino); 130 if (err) 131 goto out_ino; 132 133 inode->i_flags |= S_NOCMTIME; 134 135 if (!IS_ENABLED(CONFIG_UBIFS_ATIME_SUPPORT)) 136 inode->i_flags |= S_NOATIME; 137 138 set_nlink(inode, le32_to_cpu(ino->nlink)); 139 i_uid_write(inode, le32_to_cpu(ino->uid)); 140 i_gid_write(inode, le32_to_cpu(ino->gid)); 141 inode_set_atime(inode, (int64_t)le64_to_cpu(ino->atime_sec), 142 le32_to_cpu(ino->atime_nsec)); 143 inode_set_mtime(inode, (int64_t)le64_to_cpu(ino->mtime_sec), 144 le32_to_cpu(ino->mtime_nsec)); 145 inode_set_ctime(inode, (int64_t)le64_to_cpu(ino->ctime_sec), 146 le32_to_cpu(ino->ctime_nsec)); 147 inode->i_mode = le32_to_cpu(ino->mode); 148 inode->i_size = le64_to_cpu(ino->size); 149 150 ui->data_len = le32_to_cpu(ino->data_len); 151 ui->flags = le32_to_cpu(ino->flags); 152 ui->compr_type = le16_to_cpu(ino->compr_type); 153 ui->creat_sqnum = le64_to_cpu(ino->creat_sqnum); 154 ui->xattr_cnt = le32_to_cpu(ino->xattr_cnt); 155 ui->xattr_size = le32_to_cpu(ino->xattr_size); 156 ui->xattr_names = le32_to_cpu(ino->xattr_names); 157 ui->synced_i_size = ui->ui_size = inode->i_size; 158 159 ui->xattr = (ui->flags & UBIFS_XATTR_FL) ? 1 : 0; 160 161 err = validate_inode(c, inode); 162 if (err) 163 goto out_invalid; 164 165 switch (inode->i_mode & S_IFMT) { 166 case S_IFREG: 167 inode->i_mapping->a_ops = &ubifs_file_address_operations; 168 inode->i_op = &ubifs_file_inode_operations; 169 inode->i_fop = &ubifs_file_operations; 170 if (ui->xattr) { 171 ui->data = kmalloc(ui->data_len + 1, GFP_NOFS); 172 if (!ui->data) { 173 err = -ENOMEM; 174 goto out_ino; 175 } 176 memcpy(ui->data, ino->data, ui->data_len); 177 ((char *)ui->data)[ui->data_len] = '\0'; 178 } else if (ui->data_len != 0) { 179 err = 10; 180 goto out_invalid; 181 } 182 break; 183 case S_IFDIR: 184 inode->i_op = &ubifs_dir_inode_operations; 185 inode->i_fop = &ubifs_dir_operations; 186 if (ui->data_len != 0) { 187 err = 11; 188 goto out_invalid; 189 } 190 break; 191 case S_IFLNK: 192 inode->i_op = &ubifs_symlink_inode_operations; 193 if (ui->data_len <= 0 || ui->data_len > UBIFS_MAX_INO_DATA) { 194 err = 12; 195 goto out_invalid; 196 } 197 ui->data = kmalloc(ui->data_len + 1, GFP_NOFS); 198 if (!ui->data) { 199 err = -ENOMEM; 200 goto out_ino; 201 } 202 memcpy(ui->data, ino->data, ui->data_len); 203 ((char *)ui->data)[ui->data_len] = '\0'; 204 break; 205 case S_IFBLK: 206 case S_IFCHR: 207 { 208 dev_t rdev; 209 union ubifs_dev_desc *dev; 210 211 ui->data = kmalloc(sizeof(union ubifs_dev_desc), GFP_NOFS); 212 if (!ui->data) { 213 err = -ENOMEM; 214 goto out_ino; 215 } 216 217 dev = (union ubifs_dev_desc *)ino->data; 218 if (ui->data_len == sizeof(dev->new)) 219 rdev = new_decode_dev(le32_to_cpu(dev->new)); 220 else if (ui->data_len == sizeof(dev->huge)) 221 rdev = huge_decode_dev(le64_to_cpu(dev->huge)); 222 else { 223 err = 13; 224 goto out_invalid; 225 } 226 memcpy(ui->data, ino->data, ui->data_len); 227 inode->i_op = &ubifs_file_inode_operations; 228 init_special_inode(inode, inode->i_mode, rdev); 229 break; 230 } 231 case S_IFSOCK: 232 case S_IFIFO: 233 inode->i_op = &ubifs_file_inode_operations; 234 init_special_inode(inode, inode->i_mode, 0); 235 if (ui->data_len != 0) { 236 err = 14; 237 goto out_invalid; 238 } 239 break; 240 default: 241 err = 15; 242 goto out_invalid; 243 } 244 245 kfree(ino); 246 ubifs_set_inode_flags(inode); 247 unlock_new_inode(inode); 248 return inode; 249 250 out_invalid: 251 ubifs_err(c, "inode %lu validation failed, error %d", inode->i_ino, err); 252 ubifs_dump_node(c, ino, UBIFS_MAX_INO_NODE_SZ); 253 ubifs_dump_inode(c, inode); 254 err = -EINVAL; 255 out_ino: 256 kfree(ino); 257 out: 258 ubifs_err(c, "failed to read inode %lu, error %d", inode->i_ino, err); 259 iget_failed(inode); 260 return ERR_PTR(err); 261 } 262 263 static struct inode *ubifs_alloc_inode(struct super_block *sb) 264 { 265 struct ubifs_inode *ui; 266 267 ui = alloc_inode_sb(sb, ubifs_inode_slab, GFP_NOFS); 268 if (!ui) 269 return NULL; 270 271 memset((void *)ui + sizeof(struct inode), 0, 272 sizeof(struct ubifs_inode) - sizeof(struct inode)); 273 mutex_init(&ui->ui_mutex); 274 init_rwsem(&ui->xattr_sem); 275 spin_lock_init(&ui->ui_lock); 276 return &ui->vfs_inode; 277 }; 278 279 static void ubifs_free_inode(struct inode *inode) 280 { 281 struct ubifs_inode *ui = ubifs_inode(inode); 282 283 kfree(ui->data); 284 fscrypt_free_inode(inode); 285 286 kmem_cache_free(ubifs_inode_slab, ui); 287 } 288 289 /* 290 * Note, Linux write-back code calls this without 'i_mutex'. 291 */ 292 static int ubifs_write_inode(struct inode *inode, struct writeback_control *wbc) 293 { 294 int err = 0; 295 struct ubifs_info *c = inode->i_sb->s_fs_info; 296 struct ubifs_inode *ui = ubifs_inode(inode); 297 298 ubifs_assert(c, !ui->xattr); 299 if (is_bad_inode(inode)) 300 return 0; 301 302 mutex_lock(&ui->ui_mutex); 303 /* 304 * Due to races between write-back forced by budgeting 305 * (see 'sync_some_inodes()') and background write-back, the inode may 306 * have already been synchronized, do not do this again. This might 307 * also happen if it was synchronized in an VFS operation, e.g. 308 * 'ubifs_link()'. 309 */ 310 if (!ui->dirty) { 311 mutex_unlock(&ui->ui_mutex); 312 return 0; 313 } 314 315 /* 316 * As an optimization, do not write orphan inodes to the media just 317 * because this is not needed. 318 */ 319 dbg_gen("inode %lu, mode %#x, nlink %u", 320 inode->i_ino, (int)inode->i_mode, inode->i_nlink); 321 if (inode->i_nlink) { 322 err = ubifs_jnl_write_inode(c, inode); 323 if (err) 324 ubifs_err(c, "can't write inode %lu, error %d", 325 inode->i_ino, err); 326 else 327 err = dbg_check_inode_size(c, inode, ui->ui_size); 328 } 329 330 ui->dirty = 0; 331 mutex_unlock(&ui->ui_mutex); 332 ubifs_release_dirty_inode_budget(c, ui); 333 return err; 334 } 335 336 static int ubifs_drop_inode(struct inode *inode) 337 { 338 int drop = generic_drop_inode(inode); 339 340 if (!drop) 341 drop = fscrypt_drop_inode(inode); 342 343 return drop; 344 } 345 346 static void ubifs_evict_inode(struct inode *inode) 347 { 348 int err; 349 struct ubifs_info *c = inode->i_sb->s_fs_info; 350 struct ubifs_inode *ui = ubifs_inode(inode); 351 352 if (ui->xattr) 353 /* 354 * Extended attribute inode deletions are fully handled in 355 * 'ubifs_removexattr()'. These inodes are special and have 356 * limited usage, so there is nothing to do here. 357 */ 358 goto out; 359 360 dbg_gen("inode %lu, mode %#x", inode->i_ino, (int)inode->i_mode); 361 ubifs_assert(c, !atomic_read(&inode->i_count)); 362 363 truncate_inode_pages_final(&inode->i_data); 364 365 if (inode->i_nlink) 366 goto done; 367 368 if (is_bad_inode(inode)) 369 goto out; 370 371 ui->ui_size = inode->i_size = 0; 372 err = ubifs_jnl_delete_inode(c, inode); 373 if (err) 374 /* 375 * Worst case we have a lost orphan inode wasting space, so a 376 * simple error message is OK here. 377 */ 378 ubifs_err(c, "can't delete inode %lu, error %d", 379 inode->i_ino, err); 380 381 out: 382 if (ui->dirty) 383 ubifs_release_dirty_inode_budget(c, ui); 384 else { 385 /* We've deleted something - clean the "no space" flags */ 386 c->bi.nospace = c->bi.nospace_rp = 0; 387 smp_wmb(); 388 } 389 done: 390 clear_inode(inode); 391 fscrypt_put_encryption_info(inode); 392 } 393 394 static void ubifs_dirty_inode(struct inode *inode, int flags) 395 { 396 struct ubifs_info *c = inode->i_sb->s_fs_info; 397 struct ubifs_inode *ui = ubifs_inode(inode); 398 399 ubifs_assert(c, mutex_is_locked(&ui->ui_mutex)); 400 if (!ui->dirty) { 401 ui->dirty = 1; 402 dbg_gen("inode %lu", inode->i_ino); 403 } 404 } 405 406 static int ubifs_statfs(struct dentry *dentry, struct kstatfs *buf) 407 { 408 struct ubifs_info *c = dentry->d_sb->s_fs_info; 409 unsigned long long free; 410 __le32 *uuid = (__le32 *)c->uuid; 411 412 free = ubifs_get_free_space(c); 413 dbg_gen("free space %lld bytes (%lld blocks)", 414 free, free >> UBIFS_BLOCK_SHIFT); 415 416 buf->f_type = UBIFS_SUPER_MAGIC; 417 buf->f_bsize = UBIFS_BLOCK_SIZE; 418 buf->f_blocks = c->block_cnt; 419 buf->f_bfree = free >> UBIFS_BLOCK_SHIFT; 420 if (free > c->report_rp_size) 421 buf->f_bavail = (free - c->report_rp_size) >> UBIFS_BLOCK_SHIFT; 422 else 423 buf->f_bavail = 0; 424 buf->f_files = 0; 425 buf->f_ffree = 0; 426 buf->f_namelen = UBIFS_MAX_NLEN; 427 buf->f_fsid.val[0] = le32_to_cpu(uuid[0]) ^ le32_to_cpu(uuid[2]); 428 buf->f_fsid.val[1] = le32_to_cpu(uuid[1]) ^ le32_to_cpu(uuid[3]); 429 ubifs_assert(c, buf->f_bfree <= c->block_cnt); 430 return 0; 431 } 432 433 static int ubifs_show_options(struct seq_file *s, struct dentry *root) 434 { 435 struct ubifs_info *c = root->d_sb->s_fs_info; 436 437 if (c->mount_opts.unmount_mode == 2) 438 seq_puts(s, ",fast_unmount"); 439 else if (c->mount_opts.unmount_mode == 1) 440 seq_puts(s, ",norm_unmount"); 441 442 if (c->mount_opts.bulk_read == 2) 443 seq_puts(s, ",bulk_read"); 444 else if (c->mount_opts.bulk_read == 1) 445 seq_puts(s, ",no_bulk_read"); 446 447 if (c->mount_opts.chk_data_crc == 2) 448 seq_puts(s, ",chk_data_crc"); 449 else if (c->mount_opts.chk_data_crc == 1) 450 seq_puts(s, ",no_chk_data_crc"); 451 452 if (c->mount_opts.override_compr) { 453 seq_printf(s, ",compr=%s", 454 ubifs_compr_name(c, c->mount_opts.compr_type)); 455 } 456 457 seq_printf(s, ",assert=%s", ubifs_assert_action_name(c)); 458 seq_printf(s, ",ubi=%d,vol=%d", c->vi.ubi_num, c->vi.vol_id); 459 460 return 0; 461 } 462 463 static int ubifs_sync_fs(struct super_block *sb, int wait) 464 { 465 int i, err; 466 struct ubifs_info *c = sb->s_fs_info; 467 468 /* 469 * Zero @wait is just an advisory thing to help the file system shove 470 * lots of data into the queues, and there will be the second 471 * '->sync_fs()' call, with non-zero @wait. 472 */ 473 if (!wait) 474 return 0; 475 476 /* 477 * Synchronize write buffers, because 'ubifs_run_commit()' does not 478 * do this if it waits for an already running commit. 479 */ 480 for (i = 0; i < c->jhead_cnt; i++) { 481 err = ubifs_wbuf_sync(&c->jheads[i].wbuf); 482 if (err) 483 return err; 484 } 485 486 /* 487 * Strictly speaking, it is not necessary to commit the journal here, 488 * synchronizing write-buffers would be enough. But committing makes 489 * UBIFS free space predictions much more accurate, so we want to let 490 * the user be able to get more accurate results of 'statfs()' after 491 * they synchronize the file system. 492 */ 493 err = ubifs_run_commit(c); 494 if (err) 495 return err; 496 497 return ubi_sync(c->vi.ubi_num); 498 } 499 500 /** 501 * init_constants_early - initialize UBIFS constants. 502 * @c: UBIFS file-system description object 503 * 504 * This function initialize UBIFS constants which do not need the superblock to 505 * be read. It also checks that the UBI volume satisfies basic UBIFS 506 * requirements. Returns zero in case of success and a negative error code in 507 * case of failure. 508 */ 509 static int init_constants_early(struct ubifs_info *c) 510 { 511 if (c->vi.corrupted) { 512 ubifs_warn(c, "UBI volume is corrupted - read-only mode"); 513 c->ro_media = 1; 514 } 515 516 if (c->di.ro_mode) { 517 ubifs_msg(c, "read-only UBI device"); 518 c->ro_media = 1; 519 } 520 521 if (c->vi.vol_type == UBI_STATIC_VOLUME) { 522 ubifs_msg(c, "static UBI volume - read-only mode"); 523 c->ro_media = 1; 524 } 525 526 c->leb_cnt = c->vi.size; 527 c->leb_size = c->vi.usable_leb_size; 528 c->leb_start = c->di.leb_start; 529 c->half_leb_size = c->leb_size / 2; 530 c->min_io_size = c->di.min_io_size; 531 c->min_io_shift = fls(c->min_io_size) - 1; 532 c->max_write_size = c->di.max_write_size; 533 c->max_write_shift = fls(c->max_write_size) - 1; 534 535 if (c->leb_size < UBIFS_MIN_LEB_SZ) { 536 ubifs_errc(c, "too small LEBs (%d bytes), min. is %d bytes", 537 c->leb_size, UBIFS_MIN_LEB_SZ); 538 return -EINVAL; 539 } 540 541 if (c->leb_cnt < UBIFS_MIN_LEB_CNT) { 542 ubifs_errc(c, "too few LEBs (%d), min. is %d", 543 c->leb_cnt, UBIFS_MIN_LEB_CNT); 544 return -EINVAL; 545 } 546 547 if (!is_power_of_2(c->min_io_size)) { 548 ubifs_errc(c, "bad min. I/O size %d", c->min_io_size); 549 return -EINVAL; 550 } 551 552 /* 553 * Maximum write size has to be greater or equivalent to min. I/O 554 * size, and be multiple of min. I/O size. 555 */ 556 if (c->max_write_size < c->min_io_size || 557 c->max_write_size % c->min_io_size || 558 !is_power_of_2(c->max_write_size)) { 559 ubifs_errc(c, "bad write buffer size %d for %d min. I/O unit", 560 c->max_write_size, c->min_io_size); 561 return -EINVAL; 562 } 563 564 /* 565 * UBIFS aligns all node to 8-byte boundary, so to make function in 566 * io.c simpler, assume minimum I/O unit size to be 8 bytes if it is 567 * less than 8. 568 */ 569 if (c->min_io_size < 8) { 570 c->min_io_size = 8; 571 c->min_io_shift = 3; 572 if (c->max_write_size < c->min_io_size) { 573 c->max_write_size = c->min_io_size; 574 c->max_write_shift = c->min_io_shift; 575 } 576 } 577 578 c->ref_node_alsz = ALIGN(UBIFS_REF_NODE_SZ, c->min_io_size); 579 c->mst_node_alsz = ALIGN(UBIFS_MST_NODE_SZ, c->min_io_size); 580 581 /* 582 * Initialize node length ranges which are mostly needed for node 583 * length validation. 584 */ 585 c->ranges[UBIFS_PAD_NODE].len = UBIFS_PAD_NODE_SZ; 586 c->ranges[UBIFS_SB_NODE].len = UBIFS_SB_NODE_SZ; 587 c->ranges[UBIFS_MST_NODE].len = UBIFS_MST_NODE_SZ; 588 c->ranges[UBIFS_REF_NODE].len = UBIFS_REF_NODE_SZ; 589 c->ranges[UBIFS_TRUN_NODE].len = UBIFS_TRUN_NODE_SZ; 590 c->ranges[UBIFS_CS_NODE].len = UBIFS_CS_NODE_SZ; 591 c->ranges[UBIFS_AUTH_NODE].min_len = UBIFS_AUTH_NODE_SZ; 592 c->ranges[UBIFS_AUTH_NODE].max_len = UBIFS_AUTH_NODE_SZ + 593 UBIFS_MAX_HMAC_LEN; 594 c->ranges[UBIFS_SIG_NODE].min_len = UBIFS_SIG_NODE_SZ; 595 c->ranges[UBIFS_SIG_NODE].max_len = c->leb_size - UBIFS_SB_NODE_SZ; 596 597 c->ranges[UBIFS_INO_NODE].min_len = UBIFS_INO_NODE_SZ; 598 c->ranges[UBIFS_INO_NODE].max_len = UBIFS_MAX_INO_NODE_SZ; 599 c->ranges[UBIFS_ORPH_NODE].min_len = 600 UBIFS_ORPH_NODE_SZ + sizeof(__le64); 601 c->ranges[UBIFS_ORPH_NODE].max_len = c->leb_size; 602 c->ranges[UBIFS_DENT_NODE].min_len = UBIFS_DENT_NODE_SZ; 603 c->ranges[UBIFS_DENT_NODE].max_len = UBIFS_MAX_DENT_NODE_SZ; 604 c->ranges[UBIFS_XENT_NODE].min_len = UBIFS_XENT_NODE_SZ; 605 c->ranges[UBIFS_XENT_NODE].max_len = UBIFS_MAX_XENT_NODE_SZ; 606 c->ranges[UBIFS_DATA_NODE].min_len = UBIFS_DATA_NODE_SZ; 607 c->ranges[UBIFS_DATA_NODE].max_len = UBIFS_MAX_DATA_NODE_SZ; 608 /* 609 * Minimum indexing node size is amended later when superblock is 610 * read and the key length is known. 611 */ 612 c->ranges[UBIFS_IDX_NODE].min_len = UBIFS_IDX_NODE_SZ + UBIFS_BRANCH_SZ; 613 /* 614 * Maximum indexing node size is amended later when superblock is 615 * read and the fanout is known. 616 */ 617 c->ranges[UBIFS_IDX_NODE].max_len = INT_MAX; 618 619 /* 620 * Initialize dead and dark LEB space watermarks. See gc.c for comments 621 * about these values. 622 */ 623 c->dead_wm = ALIGN(MIN_WRITE_SZ, c->min_io_size); 624 c->dark_wm = ALIGN(UBIFS_MAX_NODE_SZ, c->min_io_size); 625 626 /* 627 * Calculate how many bytes would be wasted at the end of LEB if it was 628 * fully filled with data nodes of maximum size. This is used in 629 * calculations when reporting free space. 630 */ 631 c->leb_overhead = c->leb_size % UBIFS_MAX_DATA_NODE_SZ; 632 633 /* Buffer size for bulk-reads */ 634 c->max_bu_buf_len = UBIFS_MAX_BULK_READ * UBIFS_MAX_DATA_NODE_SZ; 635 if (c->max_bu_buf_len > c->leb_size) 636 c->max_bu_buf_len = c->leb_size; 637 638 /* Log is ready, preserve one LEB for commits. */ 639 c->min_log_bytes = c->leb_size; 640 641 return 0; 642 } 643 644 /** 645 * bud_wbuf_callback - bud LEB write-buffer synchronization call-back. 646 * @c: UBIFS file-system description object 647 * @lnum: LEB the write-buffer was synchronized to 648 * @free: how many free bytes left in this LEB 649 * @pad: how many bytes were padded 650 * 651 * This is a callback function which is called by the I/O unit when the 652 * write-buffer is synchronized. We need this to correctly maintain space 653 * accounting in bud logical eraseblocks. This function returns zero in case of 654 * success and a negative error code in case of failure. 655 * 656 * This function actually belongs to the journal, but we keep it here because 657 * we want to keep it static. 658 */ 659 static int bud_wbuf_callback(struct ubifs_info *c, int lnum, int free, int pad) 660 { 661 return ubifs_update_one_lp(c, lnum, free, pad, 0, 0); 662 } 663 664 /* 665 * init_constants_sb - initialize UBIFS constants. 666 * @c: UBIFS file-system description object 667 * 668 * This is a helper function which initializes various UBIFS constants after 669 * the superblock has been read. It also checks various UBIFS parameters and 670 * makes sure they are all right. Returns zero in case of success and a 671 * negative error code in case of failure. 672 */ 673 static int init_constants_sb(struct ubifs_info *c) 674 { 675 int tmp, err; 676 long long tmp64; 677 678 c->main_bytes = (long long)c->main_lebs * c->leb_size; 679 c->max_znode_sz = sizeof(struct ubifs_znode) + 680 c->fanout * sizeof(struct ubifs_zbranch); 681 682 tmp = ubifs_idx_node_sz(c, 1); 683 c->ranges[UBIFS_IDX_NODE].min_len = tmp; 684 c->min_idx_node_sz = ALIGN(tmp, 8); 685 686 tmp = ubifs_idx_node_sz(c, c->fanout); 687 c->ranges[UBIFS_IDX_NODE].max_len = tmp; 688 c->max_idx_node_sz = ALIGN(tmp, 8); 689 690 /* Make sure LEB size is large enough to fit full commit */ 691 tmp = UBIFS_CS_NODE_SZ + UBIFS_REF_NODE_SZ * c->jhead_cnt; 692 tmp = ALIGN(tmp, c->min_io_size); 693 if (tmp > c->leb_size) { 694 ubifs_err(c, "too small LEB size %d, at least %d needed", 695 c->leb_size, tmp); 696 return -EINVAL; 697 } 698 699 /* 700 * Make sure that the log is large enough to fit reference nodes for 701 * all buds plus one reserved LEB. 702 */ 703 tmp64 = c->max_bud_bytes + c->leb_size - 1; 704 c->max_bud_cnt = div_u64(tmp64, c->leb_size); 705 tmp = (c->ref_node_alsz * c->max_bud_cnt + c->leb_size - 1); 706 tmp /= c->leb_size; 707 tmp += 1; 708 if (c->log_lebs < tmp) { 709 ubifs_err(c, "too small log %d LEBs, required min. %d LEBs", 710 c->log_lebs, tmp); 711 return -EINVAL; 712 } 713 714 /* 715 * When budgeting we assume worst-case scenarios when the pages are not 716 * be compressed and direntries are of the maximum size. 717 * 718 * Note, data, which may be stored in inodes is budgeted separately, so 719 * it is not included into 'c->bi.inode_budget'. 720 */ 721 c->bi.page_budget = UBIFS_MAX_DATA_NODE_SZ * UBIFS_BLOCKS_PER_PAGE; 722 c->bi.inode_budget = UBIFS_INO_NODE_SZ; 723 c->bi.dent_budget = UBIFS_MAX_DENT_NODE_SZ; 724 725 /* 726 * When the amount of flash space used by buds becomes 727 * 'c->max_bud_bytes', UBIFS just blocks all writers and starts commit. 728 * The writers are unblocked when the commit is finished. To avoid 729 * writers to be blocked UBIFS initiates background commit in advance, 730 * when number of bud bytes becomes above the limit defined below. 731 */ 732 c->bg_bud_bytes = (c->max_bud_bytes * 13) >> 4; 733 734 /* 735 * Ensure minimum journal size. All the bytes in the journal heads are 736 * considered to be used, when calculating the current journal usage. 737 * Consequently, if the journal is too small, UBIFS will treat it as 738 * always full. 739 */ 740 tmp64 = (long long)(c->jhead_cnt + 1) * c->leb_size + 1; 741 if (c->bg_bud_bytes < tmp64) 742 c->bg_bud_bytes = tmp64; 743 if (c->max_bud_bytes < tmp64 + c->leb_size) 744 c->max_bud_bytes = tmp64 + c->leb_size; 745 746 err = ubifs_calc_lpt_geom(c); 747 if (err) 748 return err; 749 750 /* Initialize effective LEB size used in budgeting calculations */ 751 c->idx_leb_size = c->leb_size - c->max_idx_node_sz; 752 return 0; 753 } 754 755 /* 756 * init_constants_master - initialize UBIFS constants. 757 * @c: UBIFS file-system description object 758 * 759 * This is a helper function which initializes various UBIFS constants after 760 * the master node has been read. It also checks various UBIFS parameters and 761 * makes sure they are all right. 762 */ 763 static void init_constants_master(struct ubifs_info *c) 764 { 765 long long tmp64; 766 767 c->bi.min_idx_lebs = ubifs_calc_min_idx_lebs(c); 768 c->report_rp_size = ubifs_reported_space(c, c->rp_size); 769 770 /* 771 * Calculate total amount of FS blocks. This number is not used 772 * internally because it does not make much sense for UBIFS, but it is 773 * necessary to report something for the 'statfs()' call. 774 * 775 * Subtract the LEB reserved for GC, the LEB which is reserved for 776 * deletions, minimum LEBs for the index, and assume only one journal 777 * head is available. 778 */ 779 tmp64 = c->main_lebs - 1 - 1 - MIN_INDEX_LEBS - c->jhead_cnt + 1; 780 tmp64 *= (long long)c->leb_size - c->leb_overhead; 781 tmp64 = ubifs_reported_space(c, tmp64); 782 c->block_cnt = tmp64 >> UBIFS_BLOCK_SHIFT; 783 } 784 785 /** 786 * take_gc_lnum - reserve GC LEB. 787 * @c: UBIFS file-system description object 788 * 789 * This function ensures that the LEB reserved for garbage collection is marked 790 * as "taken" in lprops. We also have to set free space to LEB size and dirty 791 * space to zero, because lprops may contain out-of-date information if the 792 * file-system was un-mounted before it has been committed. This function 793 * returns zero in case of success and a negative error code in case of 794 * failure. 795 */ 796 static int take_gc_lnum(struct ubifs_info *c) 797 { 798 int err; 799 800 if (c->gc_lnum == -1) { 801 ubifs_err(c, "no LEB for GC"); 802 return -EINVAL; 803 } 804 805 /* And we have to tell lprops that this LEB is taken */ 806 err = ubifs_change_one_lp(c, c->gc_lnum, c->leb_size, 0, 807 LPROPS_TAKEN, 0, 0); 808 return err; 809 } 810 811 /** 812 * alloc_wbufs - allocate write-buffers. 813 * @c: UBIFS file-system description object 814 * 815 * This helper function allocates and initializes UBIFS write-buffers. Returns 816 * zero in case of success and %-ENOMEM in case of failure. 817 */ 818 static int alloc_wbufs(struct ubifs_info *c) 819 { 820 int i, err; 821 822 c->jheads = kcalloc(c->jhead_cnt, sizeof(struct ubifs_jhead), 823 GFP_KERNEL); 824 if (!c->jheads) 825 return -ENOMEM; 826 827 /* Initialize journal heads */ 828 for (i = 0; i < c->jhead_cnt; i++) { 829 INIT_LIST_HEAD(&c->jheads[i].buds_list); 830 err = ubifs_wbuf_init(c, &c->jheads[i].wbuf); 831 if (err) 832 goto out_wbuf; 833 834 c->jheads[i].wbuf.sync_callback = &bud_wbuf_callback; 835 c->jheads[i].wbuf.jhead = i; 836 c->jheads[i].grouped = 1; 837 c->jheads[i].log_hash = ubifs_hash_get_desc(c); 838 if (IS_ERR(c->jheads[i].log_hash)) { 839 err = PTR_ERR(c->jheads[i].log_hash); 840 goto out_log_hash; 841 } 842 } 843 844 /* 845 * Garbage Collector head does not need to be synchronized by timer. 846 * Also GC head nodes are not grouped. 847 */ 848 c->jheads[GCHD].wbuf.no_timer = 1; 849 c->jheads[GCHD].grouped = 0; 850 851 return 0; 852 853 out_log_hash: 854 kfree(c->jheads[i].wbuf.buf); 855 kfree(c->jheads[i].wbuf.inodes); 856 857 out_wbuf: 858 while (i--) { 859 kfree(c->jheads[i].wbuf.buf); 860 kfree(c->jheads[i].wbuf.inodes); 861 kfree(c->jheads[i].log_hash); 862 } 863 kfree(c->jheads); 864 c->jheads = NULL; 865 866 return err; 867 } 868 869 /** 870 * free_wbufs - free write-buffers. 871 * @c: UBIFS file-system description object 872 */ 873 static void free_wbufs(struct ubifs_info *c) 874 { 875 int i; 876 877 if (c->jheads) { 878 for (i = 0; i < c->jhead_cnt; i++) { 879 kfree(c->jheads[i].wbuf.buf); 880 kfree(c->jheads[i].wbuf.inodes); 881 kfree(c->jheads[i].log_hash); 882 } 883 kfree(c->jheads); 884 c->jheads = NULL; 885 } 886 } 887 888 /** 889 * free_orphans - free orphans. 890 * @c: UBIFS file-system description object 891 */ 892 static void free_orphans(struct ubifs_info *c) 893 { 894 struct ubifs_orphan *orph; 895 896 while (c->orph_dnext) { 897 orph = c->orph_dnext; 898 c->orph_dnext = orph->dnext; 899 list_del(&orph->list); 900 kfree(orph); 901 } 902 903 while (!list_empty(&c->orph_list)) { 904 orph = list_entry(c->orph_list.next, struct ubifs_orphan, list); 905 list_del(&orph->list); 906 kfree(orph); 907 ubifs_err(c, "orphan list not empty at unmount"); 908 } 909 910 vfree(c->orph_buf); 911 c->orph_buf = NULL; 912 } 913 914 /** 915 * free_buds - free per-bud objects. 916 * @c: UBIFS file-system description object 917 */ 918 static void free_buds(struct ubifs_info *c) 919 { 920 struct ubifs_bud *bud, *n; 921 922 rbtree_postorder_for_each_entry_safe(bud, n, &c->buds, rb) 923 kfree(bud); 924 } 925 926 /** 927 * check_volume_empty - check if the UBI volume is empty. 928 * @c: UBIFS file-system description object 929 * 930 * This function checks if the UBIFS volume is empty by looking if its LEBs are 931 * mapped or not. The result of checking is stored in the @c->empty variable. 932 * Returns zero in case of success and a negative error code in case of 933 * failure. 934 */ 935 static int check_volume_empty(struct ubifs_info *c) 936 { 937 int lnum, err; 938 939 c->empty = 1; 940 for (lnum = 0; lnum < c->leb_cnt; lnum++) { 941 err = ubifs_is_mapped(c, lnum); 942 if (unlikely(err < 0)) 943 return err; 944 if (err == 1) { 945 c->empty = 0; 946 break; 947 } 948 949 cond_resched(); 950 } 951 952 return 0; 953 } 954 955 /* 956 * UBIFS mount options. 957 * 958 * Opt_fast_unmount: do not run a journal commit before un-mounting 959 * Opt_norm_unmount: run a journal commit before un-mounting 960 * Opt_bulk_read: enable bulk-reads 961 * Opt_no_bulk_read: disable bulk-reads 962 * Opt_chk_data_crc: check CRCs when reading data nodes 963 * Opt_no_chk_data_crc: do not check CRCs when reading data nodes 964 * Opt_override_compr: override default compressor 965 * Opt_assert: set ubifs_assert() action 966 * Opt_auth_key: The key name used for authentication 967 * Opt_auth_hash_name: The hash type used for authentication 968 * Opt_err: just end of array marker 969 */ 970 enum { 971 Opt_fast_unmount, 972 Opt_norm_unmount, 973 Opt_bulk_read, 974 Opt_no_bulk_read, 975 Opt_chk_data_crc, 976 Opt_no_chk_data_crc, 977 Opt_override_compr, 978 Opt_assert, 979 Opt_auth_key, 980 Opt_auth_hash_name, 981 Opt_ignore, 982 Opt_err, 983 }; 984 985 static const match_table_t tokens = { 986 {Opt_fast_unmount, "fast_unmount"}, 987 {Opt_norm_unmount, "norm_unmount"}, 988 {Opt_bulk_read, "bulk_read"}, 989 {Opt_no_bulk_read, "no_bulk_read"}, 990 {Opt_chk_data_crc, "chk_data_crc"}, 991 {Opt_no_chk_data_crc, "no_chk_data_crc"}, 992 {Opt_override_compr, "compr=%s"}, 993 {Opt_auth_key, "auth_key=%s"}, 994 {Opt_auth_hash_name, "auth_hash_name=%s"}, 995 {Opt_ignore, "ubi=%s"}, 996 {Opt_ignore, "vol=%s"}, 997 {Opt_assert, "assert=%s"}, 998 {Opt_err, NULL}, 999 }; 1000 1001 /** 1002 * parse_standard_option - parse a standard mount option. 1003 * @option: the option to parse 1004 * 1005 * Normally, standard mount options like "sync" are passed to file-systems as 1006 * flags. However, when a "rootflags=" kernel boot parameter is used, they may 1007 * be present in the options string. This function tries to deal with this 1008 * situation and parse standard options. Returns 0 if the option was not 1009 * recognized, and the corresponding integer flag if it was. 1010 * 1011 * UBIFS is only interested in the "sync" option, so do not check for anything 1012 * else. 1013 */ 1014 static int parse_standard_option(const char *option) 1015 { 1016 1017 pr_notice("UBIFS: parse %s\n", option); 1018 if (!strcmp(option, "sync")) 1019 return SB_SYNCHRONOUS; 1020 return 0; 1021 } 1022 1023 /** 1024 * ubifs_parse_options - parse mount parameters. 1025 * @c: UBIFS file-system description object 1026 * @options: parameters to parse 1027 * @is_remount: non-zero if this is FS re-mount 1028 * 1029 * This function parses UBIFS mount options and returns zero in case success 1030 * and a negative error code in case of failure. 1031 */ 1032 static int ubifs_parse_options(struct ubifs_info *c, char *options, 1033 int is_remount) 1034 { 1035 char *p; 1036 substring_t args[MAX_OPT_ARGS]; 1037 1038 if (!options) 1039 return 0; 1040 1041 while ((p = strsep(&options, ","))) { 1042 int token; 1043 1044 if (!*p) 1045 continue; 1046 1047 token = match_token(p, tokens, args); 1048 switch (token) { 1049 /* 1050 * %Opt_fast_unmount and %Opt_norm_unmount options are ignored. 1051 * We accept them in order to be backward-compatible. But this 1052 * should be removed at some point. 1053 */ 1054 case Opt_fast_unmount: 1055 c->mount_opts.unmount_mode = 2; 1056 break; 1057 case Opt_norm_unmount: 1058 c->mount_opts.unmount_mode = 1; 1059 break; 1060 case Opt_bulk_read: 1061 c->mount_opts.bulk_read = 2; 1062 c->bulk_read = 1; 1063 break; 1064 case Opt_no_bulk_read: 1065 c->mount_opts.bulk_read = 1; 1066 c->bulk_read = 0; 1067 break; 1068 case Opt_chk_data_crc: 1069 c->mount_opts.chk_data_crc = 2; 1070 c->no_chk_data_crc = 0; 1071 break; 1072 case Opt_no_chk_data_crc: 1073 c->mount_opts.chk_data_crc = 1; 1074 c->no_chk_data_crc = 1; 1075 break; 1076 case Opt_override_compr: 1077 { 1078 char *name = match_strdup(&args[0]); 1079 1080 if (!name) 1081 return -ENOMEM; 1082 if (!strcmp(name, "none")) 1083 c->mount_opts.compr_type = UBIFS_COMPR_NONE; 1084 else if (!strcmp(name, "lzo")) 1085 c->mount_opts.compr_type = UBIFS_COMPR_LZO; 1086 else if (!strcmp(name, "zlib")) 1087 c->mount_opts.compr_type = UBIFS_COMPR_ZLIB; 1088 else if (!strcmp(name, "zstd")) 1089 c->mount_opts.compr_type = UBIFS_COMPR_ZSTD; 1090 else { 1091 ubifs_err(c, "unknown compressor \"%s\"", name); //FIXME: is c ready? 1092 kfree(name); 1093 return -EINVAL; 1094 } 1095 kfree(name); 1096 c->mount_opts.override_compr = 1; 1097 c->default_compr = c->mount_opts.compr_type; 1098 break; 1099 } 1100 case Opt_assert: 1101 { 1102 char *act = match_strdup(&args[0]); 1103 1104 if (!act) 1105 return -ENOMEM; 1106 if (!strcmp(act, "report")) 1107 c->assert_action = ASSACT_REPORT; 1108 else if (!strcmp(act, "read-only")) 1109 c->assert_action = ASSACT_RO; 1110 else if (!strcmp(act, "panic")) 1111 c->assert_action = ASSACT_PANIC; 1112 else { 1113 ubifs_err(c, "unknown assert action \"%s\"", act); 1114 kfree(act); 1115 return -EINVAL; 1116 } 1117 kfree(act); 1118 break; 1119 } 1120 case Opt_auth_key: 1121 if (!is_remount) { 1122 c->auth_key_name = kstrdup(args[0].from, 1123 GFP_KERNEL); 1124 if (!c->auth_key_name) 1125 return -ENOMEM; 1126 } 1127 break; 1128 case Opt_auth_hash_name: 1129 if (!is_remount) { 1130 c->auth_hash_name = kstrdup(args[0].from, 1131 GFP_KERNEL); 1132 if (!c->auth_hash_name) 1133 return -ENOMEM; 1134 } 1135 break; 1136 case Opt_ignore: 1137 break; 1138 default: 1139 { 1140 unsigned long flag; 1141 struct super_block *sb = c->vfs_sb; 1142 1143 flag = parse_standard_option(p); 1144 if (!flag) { 1145 ubifs_err(c, "unrecognized mount option \"%s\" or missing value", 1146 p); 1147 return -EINVAL; 1148 } 1149 sb->s_flags |= flag; 1150 break; 1151 } 1152 } 1153 } 1154 1155 return 0; 1156 } 1157 1158 /* 1159 * ubifs_release_options - release mount parameters which have been dumped. 1160 * @c: UBIFS file-system description object 1161 */ 1162 static void ubifs_release_options(struct ubifs_info *c) 1163 { 1164 kfree(c->auth_key_name); 1165 c->auth_key_name = NULL; 1166 kfree(c->auth_hash_name); 1167 c->auth_hash_name = NULL; 1168 } 1169 1170 /** 1171 * destroy_journal - destroy journal data structures. 1172 * @c: UBIFS file-system description object 1173 * 1174 * This function destroys journal data structures including those that may have 1175 * been created by recovery functions. 1176 */ 1177 static void destroy_journal(struct ubifs_info *c) 1178 { 1179 while (!list_empty(&c->unclean_leb_list)) { 1180 struct ubifs_unclean_leb *ucleb; 1181 1182 ucleb = list_entry(c->unclean_leb_list.next, 1183 struct ubifs_unclean_leb, list); 1184 list_del(&ucleb->list); 1185 kfree(ucleb); 1186 } 1187 while (!list_empty(&c->old_buds)) { 1188 struct ubifs_bud *bud; 1189 1190 bud = list_entry(c->old_buds.next, struct ubifs_bud, list); 1191 list_del(&bud->list); 1192 kfree(bud); 1193 } 1194 ubifs_destroy_idx_gc(c); 1195 ubifs_destroy_size_tree(c); 1196 ubifs_tnc_close(c); 1197 free_buds(c); 1198 } 1199 1200 /** 1201 * bu_init - initialize bulk-read information. 1202 * @c: UBIFS file-system description object 1203 */ 1204 static void bu_init(struct ubifs_info *c) 1205 { 1206 ubifs_assert(c, c->bulk_read == 1); 1207 1208 if (c->bu.buf) 1209 return; /* Already initialized */ 1210 1211 again: 1212 c->bu.buf = kmalloc(c->max_bu_buf_len, GFP_KERNEL | __GFP_NOWARN); 1213 if (!c->bu.buf) { 1214 if (c->max_bu_buf_len > UBIFS_KMALLOC_OK) { 1215 c->max_bu_buf_len = UBIFS_KMALLOC_OK; 1216 goto again; 1217 } 1218 1219 /* Just disable bulk-read */ 1220 ubifs_warn(c, "cannot allocate %d bytes of memory for bulk-read, disabling it", 1221 c->max_bu_buf_len); 1222 c->mount_opts.bulk_read = 1; 1223 c->bulk_read = 0; 1224 return; 1225 } 1226 } 1227 1228 /** 1229 * check_free_space - check if there is enough free space to mount. 1230 * @c: UBIFS file-system description object 1231 * 1232 * This function makes sure UBIFS has enough free space to be mounted in 1233 * read/write mode. UBIFS must always have some free space to allow deletions. 1234 */ 1235 static int check_free_space(struct ubifs_info *c) 1236 { 1237 ubifs_assert(c, c->dark_wm > 0); 1238 if (c->lst.total_free + c->lst.total_dirty < c->dark_wm) { 1239 ubifs_err(c, "insufficient free space to mount in R/W mode"); 1240 ubifs_dump_budg(c, &c->bi); 1241 ubifs_dump_lprops(c); 1242 return -ENOSPC; 1243 } 1244 return 0; 1245 } 1246 1247 /** 1248 * mount_ubifs - mount UBIFS file-system. 1249 * @c: UBIFS file-system description object 1250 * 1251 * This function mounts UBIFS file system. Returns zero in case of success and 1252 * a negative error code in case of failure. 1253 */ 1254 static int mount_ubifs(struct ubifs_info *c) 1255 { 1256 int err; 1257 long long x, y; 1258 size_t sz; 1259 1260 c->ro_mount = !!sb_rdonly(c->vfs_sb); 1261 /* Suppress error messages while probing if SB_SILENT is set */ 1262 c->probing = !!(c->vfs_sb->s_flags & SB_SILENT); 1263 1264 err = init_constants_early(c); 1265 if (err) 1266 return err; 1267 1268 err = ubifs_debugging_init(c); 1269 if (err) 1270 return err; 1271 1272 err = ubifs_sysfs_register(c); 1273 if (err) 1274 goto out_debugging; 1275 1276 err = check_volume_empty(c); 1277 if (err) 1278 goto out_free; 1279 1280 if (c->empty && (c->ro_mount || c->ro_media)) { 1281 /* 1282 * This UBI volume is empty, and read-only, or the file system 1283 * is mounted read-only - we cannot format it. 1284 */ 1285 ubifs_err(c, "can't format empty UBI volume: read-only %s", 1286 c->ro_media ? "UBI volume" : "mount"); 1287 err = -EROFS; 1288 goto out_free; 1289 } 1290 1291 if (c->ro_media && !c->ro_mount) { 1292 ubifs_err(c, "cannot mount read-write - read-only media"); 1293 err = -EROFS; 1294 goto out_free; 1295 } 1296 1297 /* 1298 * The requirement for the buffer is that it should fit indexing B-tree 1299 * height amount of integers. We assume the height if the TNC tree will 1300 * never exceed 64. 1301 */ 1302 err = -ENOMEM; 1303 c->bottom_up_buf = kmalloc_array(BOTTOM_UP_HEIGHT, sizeof(int), 1304 GFP_KERNEL); 1305 if (!c->bottom_up_buf) 1306 goto out_free; 1307 1308 c->sbuf = vmalloc(c->leb_size); 1309 if (!c->sbuf) 1310 goto out_free; 1311 1312 if (!c->ro_mount) { 1313 c->ileb_buf = vmalloc(c->leb_size); 1314 if (!c->ileb_buf) 1315 goto out_free; 1316 } 1317 1318 if (c->bulk_read == 1) 1319 bu_init(c); 1320 1321 if (!c->ro_mount) { 1322 c->write_reserve_buf = kmalloc(COMPRESSED_DATA_NODE_BUF_SZ + \ 1323 UBIFS_CIPHER_BLOCK_SIZE, 1324 GFP_KERNEL); 1325 if (!c->write_reserve_buf) 1326 goto out_free; 1327 } 1328 1329 c->mounting = 1; 1330 1331 if (c->auth_key_name) { 1332 if (IS_ENABLED(CONFIG_UBIFS_FS_AUTHENTICATION)) { 1333 err = ubifs_init_authentication(c); 1334 if (err) 1335 goto out_free; 1336 } else { 1337 ubifs_err(c, "auth_key_name, but UBIFS is built without" 1338 " authentication support"); 1339 err = -EINVAL; 1340 goto out_free; 1341 } 1342 } 1343 1344 err = ubifs_read_superblock(c); 1345 if (err) 1346 goto out_auth; 1347 1348 c->probing = 0; 1349 1350 /* 1351 * Make sure the compressor which is set as default in the superblock 1352 * or overridden by mount options is actually compiled in. 1353 */ 1354 if (!ubifs_compr_present(c, c->default_compr)) { 1355 ubifs_err(c, "'compressor \"%s\" is not compiled in", 1356 ubifs_compr_name(c, c->default_compr)); 1357 err = -ENOTSUPP; 1358 goto out_auth; 1359 } 1360 1361 err = init_constants_sb(c); 1362 if (err) 1363 goto out_auth; 1364 1365 sz = ALIGN(c->max_idx_node_sz, c->min_io_size) * 2; 1366 c->cbuf = kmalloc(sz, GFP_NOFS); 1367 if (!c->cbuf) { 1368 err = -ENOMEM; 1369 goto out_auth; 1370 } 1371 1372 err = alloc_wbufs(c); 1373 if (err) 1374 goto out_cbuf; 1375 1376 sprintf(c->bgt_name, BGT_NAME_PATTERN, c->vi.ubi_num, c->vi.vol_id); 1377 if (!c->ro_mount) { 1378 /* Create background thread */ 1379 c->bgt = kthread_run(ubifs_bg_thread, c, "%s", c->bgt_name); 1380 if (IS_ERR(c->bgt)) { 1381 err = PTR_ERR(c->bgt); 1382 c->bgt = NULL; 1383 ubifs_err(c, "cannot spawn \"%s\", error %d", 1384 c->bgt_name, err); 1385 goto out_wbufs; 1386 } 1387 } 1388 1389 err = ubifs_read_master(c); 1390 if (err) 1391 goto out_master; 1392 1393 init_constants_master(c); 1394 1395 if ((c->mst_node->flags & cpu_to_le32(UBIFS_MST_DIRTY)) != 0) { 1396 ubifs_msg(c, "recovery needed"); 1397 c->need_recovery = 1; 1398 } 1399 1400 if (c->need_recovery && !c->ro_mount) { 1401 err = ubifs_recover_inl_heads(c, c->sbuf); 1402 if (err) 1403 goto out_master; 1404 } 1405 1406 err = ubifs_lpt_init(c, 1, !c->ro_mount); 1407 if (err) 1408 goto out_master; 1409 1410 if (!c->ro_mount && c->space_fixup) { 1411 err = ubifs_fixup_free_space(c); 1412 if (err) 1413 goto out_lpt; 1414 } 1415 1416 if (!c->ro_mount && !c->need_recovery) { 1417 /* 1418 * Set the "dirty" flag so that if we reboot uncleanly we 1419 * will notice this immediately on the next mount. 1420 */ 1421 c->mst_node->flags |= cpu_to_le32(UBIFS_MST_DIRTY); 1422 err = ubifs_write_master(c); 1423 if (err) 1424 goto out_lpt; 1425 } 1426 1427 /* 1428 * Handle offline signed images: Now that the master node is 1429 * written and its validation no longer depends on the hash 1430 * in the superblock, we can update the offline signed 1431 * superblock with a HMAC version, 1432 */ 1433 if (ubifs_authenticated(c) && ubifs_hmac_zero(c, c->sup_node->hmac)) { 1434 err = ubifs_hmac_wkm(c, c->sup_node->hmac_wkm); 1435 if (err) 1436 goto out_lpt; 1437 c->superblock_need_write = 1; 1438 } 1439 1440 if (!c->ro_mount && c->superblock_need_write) { 1441 err = ubifs_write_sb_node(c, c->sup_node); 1442 if (err) 1443 goto out_lpt; 1444 c->superblock_need_write = 0; 1445 } 1446 1447 err = dbg_check_idx_size(c, c->bi.old_idx_sz); 1448 if (err) 1449 goto out_lpt; 1450 1451 err = ubifs_replay_journal(c); 1452 if (err) 1453 goto out_journal; 1454 1455 /* Calculate 'min_idx_lebs' after journal replay */ 1456 c->bi.min_idx_lebs = ubifs_calc_min_idx_lebs(c); 1457 1458 err = ubifs_mount_orphans(c, c->need_recovery, c->ro_mount); 1459 if (err) 1460 goto out_orphans; 1461 1462 if (!c->ro_mount) { 1463 int lnum; 1464 1465 err = check_free_space(c); 1466 if (err) 1467 goto out_orphans; 1468 1469 /* Check for enough log space */ 1470 lnum = c->lhead_lnum + 1; 1471 if (lnum >= UBIFS_LOG_LNUM + c->log_lebs) 1472 lnum = UBIFS_LOG_LNUM; 1473 if (lnum == c->ltail_lnum) { 1474 err = ubifs_consolidate_log(c); 1475 if (err) 1476 goto out_orphans; 1477 } 1478 1479 if (c->need_recovery) { 1480 if (!ubifs_authenticated(c)) { 1481 err = ubifs_recover_size(c, true); 1482 if (err) 1483 goto out_orphans; 1484 } 1485 1486 err = ubifs_rcvry_gc_commit(c); 1487 if (err) 1488 goto out_orphans; 1489 1490 if (ubifs_authenticated(c)) { 1491 err = ubifs_recover_size(c, false); 1492 if (err) 1493 goto out_orphans; 1494 } 1495 } else { 1496 err = take_gc_lnum(c); 1497 if (err) 1498 goto out_orphans; 1499 1500 /* 1501 * GC LEB may contain garbage if there was an unclean 1502 * reboot, and it should be un-mapped. 1503 */ 1504 err = ubifs_leb_unmap(c, c->gc_lnum); 1505 if (err) 1506 goto out_orphans; 1507 } 1508 1509 err = dbg_check_lprops(c); 1510 if (err) 1511 goto out_orphans; 1512 } else if (c->need_recovery) { 1513 err = ubifs_recover_size(c, false); 1514 if (err) 1515 goto out_orphans; 1516 } else { 1517 /* 1518 * Even if we mount read-only, we have to set space in GC LEB 1519 * to proper value because this affects UBIFS free space 1520 * reporting. We do not want to have a situation when 1521 * re-mounting from R/O to R/W changes amount of free space. 1522 */ 1523 err = take_gc_lnum(c); 1524 if (err) 1525 goto out_orphans; 1526 } 1527 1528 spin_lock(&ubifs_infos_lock); 1529 list_add_tail(&c->infos_list, &ubifs_infos); 1530 spin_unlock(&ubifs_infos_lock); 1531 1532 if (c->need_recovery) { 1533 if (c->ro_mount) 1534 ubifs_msg(c, "recovery deferred"); 1535 else { 1536 c->need_recovery = 0; 1537 ubifs_msg(c, "recovery completed"); 1538 /* 1539 * GC LEB has to be empty and taken at this point. But 1540 * the journal head LEBs may also be accounted as 1541 * "empty taken" if they are empty. 1542 */ 1543 ubifs_assert(c, c->lst.taken_empty_lebs > 0); 1544 } 1545 } else 1546 ubifs_assert(c, c->lst.taken_empty_lebs > 0); 1547 1548 err = dbg_check_filesystem(c); 1549 if (err) 1550 goto out_infos; 1551 1552 dbg_debugfs_init_fs(c); 1553 1554 c->mounting = 0; 1555 1556 ubifs_msg(c, "UBIFS: mounted UBI device %d, volume %d, name \"%s\"%s", 1557 c->vi.ubi_num, c->vi.vol_id, c->vi.name, 1558 c->ro_mount ? ", R/O mode" : ""); 1559 x = (long long)c->main_lebs * c->leb_size; 1560 y = (long long)c->log_lebs * c->leb_size + c->max_bud_bytes; 1561 ubifs_msg(c, "LEB size: %d bytes (%d KiB), min./max. I/O unit sizes: %d bytes/%d bytes", 1562 c->leb_size, c->leb_size >> 10, c->min_io_size, 1563 c->max_write_size); 1564 ubifs_msg(c, "FS size: %lld bytes (%lld MiB, %d LEBs), max %d LEBs, journal size %lld bytes (%lld MiB, %d LEBs)", 1565 x, x >> 20, c->main_lebs, c->max_leb_cnt, 1566 y, y >> 20, c->log_lebs + c->max_bud_cnt); 1567 ubifs_msg(c, "reserved for root: %llu bytes (%llu KiB)", 1568 c->report_rp_size, c->report_rp_size >> 10); 1569 ubifs_msg(c, "media format: w%d/r%d (latest is w%d/r%d), UUID %pUB%s", 1570 c->fmt_version, c->ro_compat_version, 1571 UBIFS_FORMAT_VERSION, UBIFS_RO_COMPAT_VERSION, c->uuid, 1572 c->big_lpt ? ", big LPT model" : ", small LPT model"); 1573 1574 dbg_gen("default compressor: %s", ubifs_compr_name(c, c->default_compr)); 1575 dbg_gen("data journal heads: %d", 1576 c->jhead_cnt - NONDATA_JHEADS_CNT); 1577 dbg_gen("log LEBs: %d (%d - %d)", 1578 c->log_lebs, UBIFS_LOG_LNUM, c->log_last); 1579 dbg_gen("LPT area LEBs: %d (%d - %d)", 1580 c->lpt_lebs, c->lpt_first, c->lpt_last); 1581 dbg_gen("orphan area LEBs: %d (%d - %d)", 1582 c->orph_lebs, c->orph_first, c->orph_last); 1583 dbg_gen("main area LEBs: %d (%d - %d)", 1584 c->main_lebs, c->main_first, c->leb_cnt - 1); 1585 dbg_gen("index LEBs: %d", c->lst.idx_lebs); 1586 dbg_gen("total index bytes: %llu (%llu KiB, %llu MiB)", 1587 c->bi.old_idx_sz, c->bi.old_idx_sz >> 10, 1588 c->bi.old_idx_sz >> 20); 1589 dbg_gen("key hash type: %d", c->key_hash_type); 1590 dbg_gen("tree fanout: %d", c->fanout); 1591 dbg_gen("reserved GC LEB: %d", c->gc_lnum); 1592 dbg_gen("max. znode size %d", c->max_znode_sz); 1593 dbg_gen("max. index node size %d", c->max_idx_node_sz); 1594 dbg_gen("node sizes: data %zu, inode %zu, dentry %zu", 1595 UBIFS_DATA_NODE_SZ, UBIFS_INO_NODE_SZ, UBIFS_DENT_NODE_SZ); 1596 dbg_gen("node sizes: trun %zu, sb %zu, master %zu", 1597 UBIFS_TRUN_NODE_SZ, UBIFS_SB_NODE_SZ, UBIFS_MST_NODE_SZ); 1598 dbg_gen("node sizes: ref %zu, cmt. start %zu, orph %zu", 1599 UBIFS_REF_NODE_SZ, UBIFS_CS_NODE_SZ, UBIFS_ORPH_NODE_SZ); 1600 dbg_gen("max. node sizes: data %zu, inode %zu dentry %zu, idx %d", 1601 UBIFS_MAX_DATA_NODE_SZ, UBIFS_MAX_INO_NODE_SZ, 1602 UBIFS_MAX_DENT_NODE_SZ, ubifs_idx_node_sz(c, c->fanout)); 1603 dbg_gen("dead watermark: %d", c->dead_wm); 1604 dbg_gen("dark watermark: %d", c->dark_wm); 1605 dbg_gen("LEB overhead: %d", c->leb_overhead); 1606 x = (long long)c->main_lebs * c->dark_wm; 1607 dbg_gen("max. dark space: %lld (%lld KiB, %lld MiB)", 1608 x, x >> 10, x >> 20); 1609 dbg_gen("maximum bud bytes: %lld (%lld KiB, %lld MiB)", 1610 c->max_bud_bytes, c->max_bud_bytes >> 10, 1611 c->max_bud_bytes >> 20); 1612 dbg_gen("BG commit bud bytes: %lld (%lld KiB, %lld MiB)", 1613 c->bg_bud_bytes, c->bg_bud_bytes >> 10, 1614 c->bg_bud_bytes >> 20); 1615 dbg_gen("current bud bytes %lld (%lld KiB, %lld MiB)", 1616 c->bud_bytes, c->bud_bytes >> 10, c->bud_bytes >> 20); 1617 dbg_gen("max. seq. number: %llu", c->max_sqnum); 1618 dbg_gen("commit number: %llu", c->cmt_no); 1619 dbg_gen("max. xattrs per inode: %d", ubifs_xattr_max_cnt(c)); 1620 dbg_gen("max orphans: %d", c->max_orphans); 1621 1622 return 0; 1623 1624 out_infos: 1625 spin_lock(&ubifs_infos_lock); 1626 list_del(&c->infos_list); 1627 spin_unlock(&ubifs_infos_lock); 1628 out_orphans: 1629 free_orphans(c); 1630 out_journal: 1631 destroy_journal(c); 1632 out_lpt: 1633 ubifs_lpt_free(c, 0); 1634 out_master: 1635 kfree(c->mst_node); 1636 kfree(c->rcvrd_mst_node); 1637 if (c->bgt) 1638 kthread_stop(c->bgt); 1639 out_wbufs: 1640 free_wbufs(c); 1641 out_cbuf: 1642 kfree(c->cbuf); 1643 out_auth: 1644 ubifs_exit_authentication(c); 1645 out_free: 1646 kfree(c->write_reserve_buf); 1647 kfree(c->bu.buf); 1648 vfree(c->ileb_buf); 1649 vfree(c->sbuf); 1650 kfree(c->bottom_up_buf); 1651 kfree(c->sup_node); 1652 ubifs_sysfs_unregister(c); 1653 out_debugging: 1654 ubifs_debugging_exit(c); 1655 return err; 1656 } 1657 1658 /** 1659 * ubifs_umount - un-mount UBIFS file-system. 1660 * @c: UBIFS file-system description object 1661 * 1662 * Note, this function is called to free allocated resourced when un-mounting, 1663 * as well as free resources when an error occurred while we were half way 1664 * through mounting (error path cleanup function). So it has to make sure the 1665 * resource was actually allocated before freeing it. 1666 */ 1667 static void ubifs_umount(struct ubifs_info *c) 1668 { 1669 dbg_gen("un-mounting UBI device %d, volume %d", c->vi.ubi_num, 1670 c->vi.vol_id); 1671 1672 dbg_debugfs_exit_fs(c); 1673 spin_lock(&ubifs_infos_lock); 1674 list_del(&c->infos_list); 1675 spin_unlock(&ubifs_infos_lock); 1676 1677 if (c->bgt) 1678 kthread_stop(c->bgt); 1679 1680 destroy_journal(c); 1681 free_wbufs(c); 1682 free_orphans(c); 1683 ubifs_lpt_free(c, 0); 1684 ubifs_exit_authentication(c); 1685 1686 ubifs_release_options(c); 1687 kfree(c->cbuf); 1688 kfree(c->rcvrd_mst_node); 1689 kfree(c->mst_node); 1690 kfree(c->write_reserve_buf); 1691 kfree(c->bu.buf); 1692 vfree(c->ileb_buf); 1693 vfree(c->sbuf); 1694 kfree(c->bottom_up_buf); 1695 kfree(c->sup_node); 1696 ubifs_debugging_exit(c); 1697 ubifs_sysfs_unregister(c); 1698 } 1699 1700 /** 1701 * ubifs_remount_rw - re-mount in read-write mode. 1702 * @c: UBIFS file-system description object 1703 * 1704 * UBIFS avoids allocating many unnecessary resources when mounted in read-only 1705 * mode. This function allocates the needed resources and re-mounts UBIFS in 1706 * read-write mode. 1707 */ 1708 static int ubifs_remount_rw(struct ubifs_info *c) 1709 { 1710 int err, lnum; 1711 1712 if (c->rw_incompat) { 1713 ubifs_err(c, "the file-system is not R/W-compatible"); 1714 ubifs_msg(c, "on-flash format version is w%d/r%d, but software only supports up to version w%d/r%d", 1715 c->fmt_version, c->ro_compat_version, 1716 UBIFS_FORMAT_VERSION, UBIFS_RO_COMPAT_VERSION); 1717 return -EROFS; 1718 } 1719 1720 mutex_lock(&c->umount_mutex); 1721 dbg_save_space_info(c); 1722 c->remounting_rw = 1; 1723 c->ro_mount = 0; 1724 1725 if (c->space_fixup) { 1726 err = ubifs_fixup_free_space(c); 1727 if (err) 1728 goto out; 1729 } 1730 1731 err = check_free_space(c); 1732 if (err) 1733 goto out; 1734 1735 if (c->need_recovery) { 1736 ubifs_msg(c, "completing deferred recovery"); 1737 err = ubifs_write_rcvrd_mst_node(c); 1738 if (err) 1739 goto out; 1740 if (!ubifs_authenticated(c)) { 1741 err = ubifs_recover_size(c, true); 1742 if (err) 1743 goto out; 1744 } 1745 err = ubifs_clean_lebs(c, c->sbuf); 1746 if (err) 1747 goto out; 1748 err = ubifs_recover_inl_heads(c, c->sbuf); 1749 if (err) 1750 goto out; 1751 } else { 1752 /* A readonly mount is not allowed to have orphans */ 1753 ubifs_assert(c, c->tot_orphans == 0); 1754 err = ubifs_clear_orphans(c); 1755 if (err) 1756 goto out; 1757 } 1758 1759 if (!(c->mst_node->flags & cpu_to_le32(UBIFS_MST_DIRTY))) { 1760 c->mst_node->flags |= cpu_to_le32(UBIFS_MST_DIRTY); 1761 err = ubifs_write_master(c); 1762 if (err) 1763 goto out; 1764 } 1765 1766 if (c->superblock_need_write) { 1767 struct ubifs_sb_node *sup = c->sup_node; 1768 1769 err = ubifs_write_sb_node(c, sup); 1770 if (err) 1771 goto out; 1772 1773 c->superblock_need_write = 0; 1774 } 1775 1776 c->ileb_buf = vmalloc(c->leb_size); 1777 if (!c->ileb_buf) { 1778 err = -ENOMEM; 1779 goto out; 1780 } 1781 1782 c->write_reserve_buf = kmalloc(COMPRESSED_DATA_NODE_BUF_SZ + \ 1783 UBIFS_CIPHER_BLOCK_SIZE, GFP_KERNEL); 1784 if (!c->write_reserve_buf) { 1785 err = -ENOMEM; 1786 goto out; 1787 } 1788 1789 err = ubifs_lpt_init(c, 0, 1); 1790 if (err) 1791 goto out; 1792 1793 /* Create background thread */ 1794 c->bgt = kthread_run(ubifs_bg_thread, c, "%s", c->bgt_name); 1795 if (IS_ERR(c->bgt)) { 1796 err = PTR_ERR(c->bgt); 1797 c->bgt = NULL; 1798 ubifs_err(c, "cannot spawn \"%s\", error %d", 1799 c->bgt_name, err); 1800 goto out; 1801 } 1802 1803 c->orph_buf = vmalloc(c->leb_size); 1804 if (!c->orph_buf) { 1805 err = -ENOMEM; 1806 goto out; 1807 } 1808 1809 /* Check for enough log space */ 1810 lnum = c->lhead_lnum + 1; 1811 if (lnum >= UBIFS_LOG_LNUM + c->log_lebs) 1812 lnum = UBIFS_LOG_LNUM; 1813 if (lnum == c->ltail_lnum) { 1814 err = ubifs_consolidate_log(c); 1815 if (err) 1816 goto out; 1817 } 1818 1819 if (c->need_recovery) { 1820 err = ubifs_rcvry_gc_commit(c); 1821 if (err) 1822 goto out; 1823 1824 if (ubifs_authenticated(c)) { 1825 err = ubifs_recover_size(c, false); 1826 if (err) 1827 goto out; 1828 } 1829 } else { 1830 err = ubifs_leb_unmap(c, c->gc_lnum); 1831 } 1832 if (err) 1833 goto out; 1834 1835 dbg_gen("re-mounted read-write"); 1836 c->remounting_rw = 0; 1837 1838 if (c->need_recovery) { 1839 c->need_recovery = 0; 1840 ubifs_msg(c, "deferred recovery completed"); 1841 } else { 1842 /* 1843 * Do not run the debugging space check if the were doing 1844 * recovery, because when we saved the information we had the 1845 * file-system in a state where the TNC and lprops has been 1846 * modified in memory, but all the I/O operations (including a 1847 * commit) were deferred. So the file-system was in 1848 * "non-committed" state. Now the file-system is in committed 1849 * state, and of course the amount of free space will change 1850 * because, for example, the old index size was imprecise. 1851 */ 1852 err = dbg_check_space_info(c); 1853 } 1854 1855 mutex_unlock(&c->umount_mutex); 1856 return err; 1857 1858 out: 1859 c->ro_mount = 1; 1860 vfree(c->orph_buf); 1861 c->orph_buf = NULL; 1862 if (c->bgt) { 1863 kthread_stop(c->bgt); 1864 c->bgt = NULL; 1865 } 1866 kfree(c->write_reserve_buf); 1867 c->write_reserve_buf = NULL; 1868 vfree(c->ileb_buf); 1869 c->ileb_buf = NULL; 1870 ubifs_lpt_free(c, 1); 1871 c->remounting_rw = 0; 1872 mutex_unlock(&c->umount_mutex); 1873 return err; 1874 } 1875 1876 /** 1877 * ubifs_remount_ro - re-mount in read-only mode. 1878 * @c: UBIFS file-system description object 1879 * 1880 * We assume VFS has stopped writing. Possibly the background thread could be 1881 * running a commit, however kthread_stop will wait in that case. 1882 */ 1883 static void ubifs_remount_ro(struct ubifs_info *c) 1884 { 1885 int i, err; 1886 1887 ubifs_assert(c, !c->need_recovery); 1888 ubifs_assert(c, !c->ro_mount); 1889 1890 mutex_lock(&c->umount_mutex); 1891 if (c->bgt) { 1892 kthread_stop(c->bgt); 1893 c->bgt = NULL; 1894 } 1895 1896 dbg_save_space_info(c); 1897 1898 for (i = 0; i < c->jhead_cnt; i++) { 1899 err = ubifs_wbuf_sync(&c->jheads[i].wbuf); 1900 if (err) 1901 ubifs_ro_mode(c, err); 1902 } 1903 1904 c->mst_node->flags &= ~cpu_to_le32(UBIFS_MST_DIRTY); 1905 c->mst_node->flags |= cpu_to_le32(UBIFS_MST_NO_ORPHS); 1906 c->mst_node->gc_lnum = cpu_to_le32(c->gc_lnum); 1907 err = ubifs_write_master(c); 1908 if (err) 1909 ubifs_ro_mode(c, err); 1910 1911 vfree(c->orph_buf); 1912 c->orph_buf = NULL; 1913 kfree(c->write_reserve_buf); 1914 c->write_reserve_buf = NULL; 1915 vfree(c->ileb_buf); 1916 c->ileb_buf = NULL; 1917 ubifs_lpt_free(c, 1); 1918 c->ro_mount = 1; 1919 err = dbg_check_space_info(c); 1920 if (err) 1921 ubifs_ro_mode(c, err); 1922 mutex_unlock(&c->umount_mutex); 1923 } 1924 1925 static void ubifs_put_super(struct super_block *sb) 1926 { 1927 int i; 1928 struct ubifs_info *c = sb->s_fs_info; 1929 1930 ubifs_msg(c, "un-mount UBI device %d", c->vi.ubi_num); 1931 1932 /* 1933 * The following asserts are only valid if there has not been a failure 1934 * of the media. For example, there will be dirty inodes if we failed 1935 * to write them back because of I/O errors. 1936 */ 1937 if (!c->ro_error) { 1938 ubifs_assert(c, c->bi.idx_growth == 0); 1939 ubifs_assert(c, c->bi.dd_growth == 0); 1940 ubifs_assert(c, c->bi.data_growth == 0); 1941 } 1942 1943 /* 1944 * The 'c->umount_lock' prevents races between UBIFS memory shrinker 1945 * and file system un-mount. Namely, it prevents the shrinker from 1946 * picking this superblock for shrinking - it will be just skipped if 1947 * the mutex is locked. 1948 */ 1949 mutex_lock(&c->umount_mutex); 1950 if (!c->ro_mount) { 1951 /* 1952 * First of all kill the background thread to make sure it does 1953 * not interfere with un-mounting and freeing resources. 1954 */ 1955 if (c->bgt) { 1956 kthread_stop(c->bgt); 1957 c->bgt = NULL; 1958 } 1959 1960 /* 1961 * On fatal errors c->ro_error is set to 1, in which case we do 1962 * not write the master node. 1963 */ 1964 if (!c->ro_error) { 1965 int err; 1966 1967 /* Synchronize write-buffers */ 1968 for (i = 0; i < c->jhead_cnt; i++) { 1969 err = ubifs_wbuf_sync(&c->jheads[i].wbuf); 1970 if (err) 1971 ubifs_ro_mode(c, err); 1972 } 1973 1974 /* 1975 * We are being cleanly unmounted which means the 1976 * orphans were killed - indicate this in the master 1977 * node. Also save the reserved GC LEB number. 1978 */ 1979 c->mst_node->flags &= ~cpu_to_le32(UBIFS_MST_DIRTY); 1980 c->mst_node->flags |= cpu_to_le32(UBIFS_MST_NO_ORPHS); 1981 c->mst_node->gc_lnum = cpu_to_le32(c->gc_lnum); 1982 err = ubifs_write_master(c); 1983 if (err) 1984 /* 1985 * Recovery will attempt to fix the master area 1986 * next mount, so we just print a message and 1987 * continue to unmount normally. 1988 */ 1989 ubifs_err(c, "failed to write master node, error %d", 1990 err); 1991 } else { 1992 for (i = 0; i < c->jhead_cnt; i++) 1993 /* Make sure write-buffer timers are canceled */ 1994 hrtimer_cancel(&c->jheads[i].wbuf.timer); 1995 } 1996 } 1997 1998 ubifs_umount(c); 1999 ubi_close_volume(c->ubi); 2000 mutex_unlock(&c->umount_mutex); 2001 } 2002 2003 static int ubifs_remount_fs(struct super_block *sb, int *flags, char *data) 2004 { 2005 int err; 2006 struct ubifs_info *c = sb->s_fs_info; 2007 2008 sync_filesystem(sb); 2009 dbg_gen("old flags %#lx, new flags %#x", sb->s_flags, *flags); 2010 2011 err = ubifs_parse_options(c, data, 1); 2012 if (err) { 2013 ubifs_err(c, "invalid or unknown remount parameter"); 2014 return err; 2015 } 2016 2017 if (c->ro_mount && !(*flags & SB_RDONLY)) { 2018 if (c->ro_error) { 2019 ubifs_msg(c, "cannot re-mount R/W due to prior errors"); 2020 return -EROFS; 2021 } 2022 if (c->ro_media) { 2023 ubifs_msg(c, "cannot re-mount R/W - UBI volume is R/O"); 2024 return -EROFS; 2025 } 2026 err = ubifs_remount_rw(c); 2027 if (err) 2028 return err; 2029 } else if (!c->ro_mount && (*flags & SB_RDONLY)) { 2030 if (c->ro_error) { 2031 ubifs_msg(c, "cannot re-mount R/O due to prior errors"); 2032 return -EROFS; 2033 } 2034 ubifs_remount_ro(c); 2035 } 2036 2037 if (c->bulk_read == 1) 2038 bu_init(c); 2039 else { 2040 dbg_gen("disable bulk-read"); 2041 mutex_lock(&c->bu_mutex); 2042 kfree(c->bu.buf); 2043 c->bu.buf = NULL; 2044 mutex_unlock(&c->bu_mutex); 2045 } 2046 2047 if (!c->need_recovery) 2048 ubifs_assert(c, c->lst.taken_empty_lebs > 0); 2049 2050 return 0; 2051 } 2052 2053 const struct super_operations ubifs_super_operations = { 2054 .alloc_inode = ubifs_alloc_inode, 2055 .free_inode = ubifs_free_inode, 2056 .put_super = ubifs_put_super, 2057 .write_inode = ubifs_write_inode, 2058 .drop_inode = ubifs_drop_inode, 2059 .evict_inode = ubifs_evict_inode, 2060 .statfs = ubifs_statfs, 2061 .dirty_inode = ubifs_dirty_inode, 2062 .remount_fs = ubifs_remount_fs, 2063 .show_options = ubifs_show_options, 2064 .sync_fs = ubifs_sync_fs, 2065 }; 2066 2067 /** 2068 * open_ubi - parse UBI device name string and open the UBI device. 2069 * @name: UBI volume name 2070 * @mode: UBI volume open mode 2071 * 2072 * The primary method of mounting UBIFS is by specifying the UBI volume 2073 * character device node path. However, UBIFS may also be mounted without any 2074 * character device node using one of the following methods: 2075 * 2076 * o ubiX_Y - mount UBI device number X, volume Y; 2077 * o ubiY - mount UBI device number 0, volume Y; 2078 * o ubiX:NAME - mount UBI device X, volume with name NAME; 2079 * o ubi:NAME - mount UBI device 0, volume with name NAME. 2080 * 2081 * Alternative '!' separator may be used instead of ':' (because some shells 2082 * like busybox may interpret ':' as an NFS host name separator). This function 2083 * returns UBI volume description object in case of success and a negative 2084 * error code in case of failure. 2085 */ 2086 static struct ubi_volume_desc *open_ubi(const char *name, int mode) 2087 { 2088 struct ubi_volume_desc *ubi; 2089 int dev, vol; 2090 char *endptr; 2091 2092 if (!name || !*name) 2093 return ERR_PTR(-EINVAL); 2094 2095 /* First, try to open using the device node path method */ 2096 ubi = ubi_open_volume_path(name, mode); 2097 if (!IS_ERR(ubi)) 2098 return ubi; 2099 2100 /* Try the "nodev" method */ 2101 if (name[0] != 'u' || name[1] != 'b' || name[2] != 'i') 2102 return ERR_PTR(-EINVAL); 2103 2104 /* ubi:NAME method */ 2105 if ((name[3] == ':' || name[3] == '!') && name[4] != '\0') 2106 return ubi_open_volume_nm(0, name + 4, mode); 2107 2108 if (!isdigit(name[3])) 2109 return ERR_PTR(-EINVAL); 2110 2111 dev = simple_strtoul(name + 3, &endptr, 0); 2112 2113 /* ubiY method */ 2114 if (*endptr == '\0') 2115 return ubi_open_volume(0, dev, mode); 2116 2117 /* ubiX_Y method */ 2118 if (*endptr == '_' && isdigit(endptr[1])) { 2119 vol = simple_strtoul(endptr + 1, &endptr, 0); 2120 if (*endptr != '\0') 2121 return ERR_PTR(-EINVAL); 2122 return ubi_open_volume(dev, vol, mode); 2123 } 2124 2125 /* ubiX:NAME method */ 2126 if ((*endptr == ':' || *endptr == '!') && endptr[1] != '\0') 2127 return ubi_open_volume_nm(dev, ++endptr, mode); 2128 2129 return ERR_PTR(-EINVAL); 2130 } 2131 2132 static struct ubifs_info *alloc_ubifs_info(struct ubi_volume_desc *ubi) 2133 { 2134 struct ubifs_info *c; 2135 2136 c = kzalloc(sizeof(struct ubifs_info), GFP_KERNEL); 2137 if (c) { 2138 spin_lock_init(&c->cnt_lock); 2139 spin_lock_init(&c->cs_lock); 2140 spin_lock_init(&c->buds_lock); 2141 spin_lock_init(&c->space_lock); 2142 spin_lock_init(&c->orphan_lock); 2143 init_rwsem(&c->commit_sem); 2144 mutex_init(&c->lp_mutex); 2145 mutex_init(&c->tnc_mutex); 2146 mutex_init(&c->log_mutex); 2147 mutex_init(&c->umount_mutex); 2148 mutex_init(&c->bu_mutex); 2149 mutex_init(&c->write_reserve_mutex); 2150 init_waitqueue_head(&c->cmt_wq); 2151 c->buds = RB_ROOT; 2152 c->old_idx = RB_ROOT; 2153 c->size_tree = RB_ROOT; 2154 c->orph_tree = RB_ROOT; 2155 INIT_LIST_HEAD(&c->infos_list); 2156 INIT_LIST_HEAD(&c->idx_gc); 2157 INIT_LIST_HEAD(&c->replay_list); 2158 INIT_LIST_HEAD(&c->replay_buds); 2159 INIT_LIST_HEAD(&c->uncat_list); 2160 INIT_LIST_HEAD(&c->empty_list); 2161 INIT_LIST_HEAD(&c->freeable_list); 2162 INIT_LIST_HEAD(&c->frdi_idx_list); 2163 INIT_LIST_HEAD(&c->unclean_leb_list); 2164 INIT_LIST_HEAD(&c->old_buds); 2165 INIT_LIST_HEAD(&c->orph_list); 2166 INIT_LIST_HEAD(&c->orph_new); 2167 c->no_chk_data_crc = 1; 2168 c->assert_action = ASSACT_RO; 2169 2170 c->highest_inum = UBIFS_FIRST_INO; 2171 c->lhead_lnum = c->ltail_lnum = UBIFS_LOG_LNUM; 2172 2173 ubi_get_volume_info(ubi, &c->vi); 2174 ubi_get_device_info(c->vi.ubi_num, &c->di); 2175 } 2176 return c; 2177 } 2178 2179 static int ubifs_fill_super(struct super_block *sb, void *data, int silent) 2180 { 2181 struct ubifs_info *c = sb->s_fs_info; 2182 struct inode *root; 2183 int err; 2184 2185 c->vfs_sb = sb; 2186 /* Re-open the UBI device in read-write mode */ 2187 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READWRITE); 2188 if (IS_ERR(c->ubi)) { 2189 err = PTR_ERR(c->ubi); 2190 goto out; 2191 } 2192 2193 err = ubifs_parse_options(c, data, 0); 2194 if (err) 2195 goto out_close; 2196 2197 /* 2198 * UBIFS provides 'backing_dev_info' in order to disable read-ahead. For 2199 * UBIFS, I/O is not deferred, it is done immediately in read_folio, 2200 * which means the user would have to wait not just for their own I/O 2201 * but the read-ahead I/O as well i.e. completely pointless. 2202 * 2203 * Read-ahead will be disabled because @sb->s_bdi->ra_pages is 0. Also 2204 * @sb->s_bdi->capabilities are initialized to 0 so there won't be any 2205 * writeback happening. 2206 */ 2207 err = super_setup_bdi_name(sb, "ubifs_%d_%d", c->vi.ubi_num, 2208 c->vi.vol_id); 2209 if (err) 2210 goto out_close; 2211 sb->s_bdi->ra_pages = 0; 2212 sb->s_bdi->io_pages = 0; 2213 2214 sb->s_fs_info = c; 2215 sb->s_magic = UBIFS_SUPER_MAGIC; 2216 sb->s_blocksize = UBIFS_BLOCK_SIZE; 2217 sb->s_blocksize_bits = UBIFS_BLOCK_SHIFT; 2218 sb->s_maxbytes = c->max_inode_sz = key_max_inode_size(c); 2219 if (c->max_inode_sz > MAX_LFS_FILESIZE) 2220 sb->s_maxbytes = c->max_inode_sz = MAX_LFS_FILESIZE; 2221 sb->s_op = &ubifs_super_operations; 2222 sb->s_xattr = ubifs_xattr_handlers; 2223 fscrypt_set_ops(sb, &ubifs_crypt_operations); 2224 2225 mutex_lock(&c->umount_mutex); 2226 err = mount_ubifs(c); 2227 if (err) { 2228 ubifs_assert(c, err < 0); 2229 goto out_unlock; 2230 } 2231 2232 /* Read the root inode */ 2233 root = ubifs_iget(sb, UBIFS_ROOT_INO); 2234 if (IS_ERR(root)) { 2235 err = PTR_ERR(root); 2236 goto out_umount; 2237 } 2238 2239 sb->s_root = d_make_root(root); 2240 if (!sb->s_root) { 2241 err = -ENOMEM; 2242 goto out_umount; 2243 } 2244 2245 import_uuid(&sb->s_uuid, c->uuid); 2246 2247 mutex_unlock(&c->umount_mutex); 2248 return 0; 2249 2250 out_umount: 2251 ubifs_umount(c); 2252 out_unlock: 2253 mutex_unlock(&c->umount_mutex); 2254 out_close: 2255 ubifs_release_options(c); 2256 ubi_close_volume(c->ubi); 2257 out: 2258 return err; 2259 } 2260 2261 static int sb_test(struct super_block *sb, void *data) 2262 { 2263 struct ubifs_info *c1 = data; 2264 struct ubifs_info *c = sb->s_fs_info; 2265 2266 return c->vi.cdev == c1->vi.cdev; 2267 } 2268 2269 static int sb_set(struct super_block *sb, void *data) 2270 { 2271 sb->s_fs_info = data; 2272 return set_anon_super(sb, NULL); 2273 } 2274 2275 static struct dentry *ubifs_mount(struct file_system_type *fs_type, int flags, 2276 const char *name, void *data) 2277 { 2278 struct ubi_volume_desc *ubi; 2279 struct ubifs_info *c; 2280 struct super_block *sb; 2281 int err; 2282 2283 dbg_gen("name %s, flags %#x", name, flags); 2284 2285 /* 2286 * Get UBI device number and volume ID. Mount it read-only so far 2287 * because this might be a new mount point, and UBI allows only one 2288 * read-write user at a time. 2289 */ 2290 ubi = open_ubi(name, UBI_READONLY); 2291 if (IS_ERR(ubi)) { 2292 if (!(flags & SB_SILENT)) 2293 pr_err("UBIFS error (pid: %d): cannot open \"%s\", error %d", 2294 current->pid, name, (int)PTR_ERR(ubi)); 2295 return ERR_CAST(ubi); 2296 } 2297 2298 c = alloc_ubifs_info(ubi); 2299 if (!c) { 2300 err = -ENOMEM; 2301 goto out_close; 2302 } 2303 2304 dbg_gen("opened ubi%d_%d", c->vi.ubi_num, c->vi.vol_id); 2305 2306 sb = sget(fs_type, sb_test, sb_set, flags, c); 2307 if (IS_ERR(sb)) { 2308 err = PTR_ERR(sb); 2309 kfree(c); 2310 goto out_close; 2311 } 2312 2313 if (sb->s_root) { 2314 struct ubifs_info *c1 = sb->s_fs_info; 2315 kfree(c); 2316 /* A new mount point for already mounted UBIFS */ 2317 dbg_gen("this ubi volume is already mounted"); 2318 if (!!(flags & SB_RDONLY) != c1->ro_mount) { 2319 err = -EBUSY; 2320 goto out_deact; 2321 } 2322 } else { 2323 err = ubifs_fill_super(sb, data, flags & SB_SILENT ? 1 : 0); 2324 if (err) 2325 goto out_deact; 2326 /* We do not support atime */ 2327 sb->s_flags |= SB_ACTIVE; 2328 if (IS_ENABLED(CONFIG_UBIFS_ATIME_SUPPORT)) 2329 ubifs_msg(c, "full atime support is enabled."); 2330 else 2331 sb->s_flags |= SB_NOATIME; 2332 } 2333 2334 /* 'fill_super()' opens ubi again so we must close it here */ 2335 ubi_close_volume(ubi); 2336 2337 return dget(sb->s_root); 2338 2339 out_deact: 2340 deactivate_locked_super(sb); 2341 out_close: 2342 ubi_close_volume(ubi); 2343 return ERR_PTR(err); 2344 } 2345 2346 static void kill_ubifs_super(struct super_block *s) 2347 { 2348 struct ubifs_info *c = s->s_fs_info; 2349 kill_anon_super(s); 2350 kfree(c); 2351 } 2352 2353 static struct file_system_type ubifs_fs_type = { 2354 .name = "ubifs", 2355 .owner = THIS_MODULE, 2356 .mount = ubifs_mount, 2357 .kill_sb = kill_ubifs_super, 2358 }; 2359 MODULE_ALIAS_FS("ubifs"); 2360 2361 /* 2362 * Inode slab cache constructor. 2363 */ 2364 static void inode_slab_ctor(void *obj) 2365 { 2366 struct ubifs_inode *ui = obj; 2367 inode_init_once(&ui->vfs_inode); 2368 } 2369 2370 static int __init ubifs_init(void) 2371 { 2372 int err = -ENOMEM; 2373 2374 BUILD_BUG_ON(sizeof(struct ubifs_ch) != 24); 2375 2376 /* Make sure node sizes are 8-byte aligned */ 2377 BUILD_BUG_ON(UBIFS_CH_SZ & 7); 2378 BUILD_BUG_ON(UBIFS_INO_NODE_SZ & 7); 2379 BUILD_BUG_ON(UBIFS_DENT_NODE_SZ & 7); 2380 BUILD_BUG_ON(UBIFS_XENT_NODE_SZ & 7); 2381 BUILD_BUG_ON(UBIFS_DATA_NODE_SZ & 7); 2382 BUILD_BUG_ON(UBIFS_TRUN_NODE_SZ & 7); 2383 BUILD_BUG_ON(UBIFS_SB_NODE_SZ & 7); 2384 BUILD_BUG_ON(UBIFS_MST_NODE_SZ & 7); 2385 BUILD_BUG_ON(UBIFS_REF_NODE_SZ & 7); 2386 BUILD_BUG_ON(UBIFS_CS_NODE_SZ & 7); 2387 BUILD_BUG_ON(UBIFS_ORPH_NODE_SZ & 7); 2388 2389 BUILD_BUG_ON(UBIFS_MAX_DENT_NODE_SZ & 7); 2390 BUILD_BUG_ON(UBIFS_MAX_XENT_NODE_SZ & 7); 2391 BUILD_BUG_ON(UBIFS_MAX_DATA_NODE_SZ & 7); 2392 BUILD_BUG_ON(UBIFS_MAX_INO_NODE_SZ & 7); 2393 BUILD_BUG_ON(UBIFS_MAX_NODE_SZ & 7); 2394 BUILD_BUG_ON(MIN_WRITE_SZ & 7); 2395 2396 /* Check min. node size */ 2397 BUILD_BUG_ON(UBIFS_INO_NODE_SZ < MIN_WRITE_SZ); 2398 BUILD_BUG_ON(UBIFS_DENT_NODE_SZ < MIN_WRITE_SZ); 2399 BUILD_BUG_ON(UBIFS_XENT_NODE_SZ < MIN_WRITE_SZ); 2400 BUILD_BUG_ON(UBIFS_TRUN_NODE_SZ < MIN_WRITE_SZ); 2401 2402 BUILD_BUG_ON(UBIFS_MAX_DENT_NODE_SZ > UBIFS_MAX_NODE_SZ); 2403 BUILD_BUG_ON(UBIFS_MAX_XENT_NODE_SZ > UBIFS_MAX_NODE_SZ); 2404 BUILD_BUG_ON(UBIFS_MAX_DATA_NODE_SZ > UBIFS_MAX_NODE_SZ); 2405 BUILD_BUG_ON(UBIFS_MAX_INO_NODE_SZ > UBIFS_MAX_NODE_SZ); 2406 2407 /* Defined node sizes */ 2408 BUILD_BUG_ON(UBIFS_SB_NODE_SZ != 4096); 2409 BUILD_BUG_ON(UBIFS_MST_NODE_SZ != 512); 2410 BUILD_BUG_ON(UBIFS_INO_NODE_SZ != 160); 2411 BUILD_BUG_ON(UBIFS_REF_NODE_SZ != 64); 2412 2413 /* 2414 * We use 2 bit wide bit-fields to store compression type, which should 2415 * be amended if more compressors are added. The bit-fields are: 2416 * @compr_type in 'struct ubifs_inode', @default_compr in 2417 * 'struct ubifs_info' and @compr_type in 'struct ubifs_mount_opts'. 2418 */ 2419 BUILD_BUG_ON(UBIFS_COMPR_TYPES_CNT > 4); 2420 2421 /* 2422 * We require that PAGE_SIZE is greater-than-or-equal-to 2423 * UBIFS_BLOCK_SIZE. It is assumed that both are powers of 2. 2424 */ 2425 if (PAGE_SIZE < UBIFS_BLOCK_SIZE) { 2426 pr_err("UBIFS error (pid %d): VFS page cache size is %u bytes, but UBIFS requires at least 4096 bytes", 2427 current->pid, (unsigned int)PAGE_SIZE); 2428 return -EINVAL; 2429 } 2430 2431 ubifs_inode_slab = kmem_cache_create("ubifs_inode_slab", 2432 sizeof(struct ubifs_inode), 0, 2433 SLAB_MEM_SPREAD | SLAB_RECLAIM_ACCOUNT | 2434 SLAB_ACCOUNT, &inode_slab_ctor); 2435 if (!ubifs_inode_slab) 2436 return -ENOMEM; 2437 2438 ubifs_shrinker_info = shrinker_alloc(0, "ubifs-slab"); 2439 if (!ubifs_shrinker_info) 2440 goto out_slab; 2441 2442 ubifs_shrinker_info->count_objects = ubifs_shrink_count; 2443 ubifs_shrinker_info->scan_objects = ubifs_shrink_scan; 2444 2445 shrinker_register(ubifs_shrinker_info); 2446 2447 err = ubifs_compressors_init(); 2448 if (err) 2449 goto out_shrinker; 2450 2451 dbg_debugfs_init(); 2452 2453 err = ubifs_sysfs_init(); 2454 if (err) 2455 goto out_dbg; 2456 2457 err = register_filesystem(&ubifs_fs_type); 2458 if (err) { 2459 pr_err("UBIFS error (pid %d): cannot register file system, error %d", 2460 current->pid, err); 2461 goto out_sysfs; 2462 } 2463 return 0; 2464 2465 out_sysfs: 2466 ubifs_sysfs_exit(); 2467 out_dbg: 2468 dbg_debugfs_exit(); 2469 ubifs_compressors_exit(); 2470 out_shrinker: 2471 shrinker_free(ubifs_shrinker_info); 2472 out_slab: 2473 kmem_cache_destroy(ubifs_inode_slab); 2474 return err; 2475 } 2476 /* late_initcall to let compressors initialize first */ 2477 late_initcall(ubifs_init); 2478 2479 static void __exit ubifs_exit(void) 2480 { 2481 WARN_ON(!list_empty(&ubifs_infos)); 2482 WARN_ON(atomic_long_read(&ubifs_clean_zn_cnt) != 0); 2483 2484 dbg_debugfs_exit(); 2485 ubifs_sysfs_exit(); 2486 ubifs_compressors_exit(); 2487 shrinker_free(ubifs_shrinker_info); 2488 2489 /* 2490 * Make sure all delayed rcu free inodes are flushed before we 2491 * destroy cache. 2492 */ 2493 rcu_barrier(); 2494 kmem_cache_destroy(ubifs_inode_slab); 2495 unregister_filesystem(&ubifs_fs_type); 2496 } 2497 module_exit(ubifs_exit); 2498 2499 MODULE_LICENSE("GPL"); 2500 MODULE_VERSION(__stringify(UBIFS_VERSION)); 2501 MODULE_AUTHOR("Artem Bityutskiy, Adrian Hunter"); 2502 MODULE_DESCRIPTION("UBIFS - UBI File System"); 2503