📄 mkfs.c
字号:
aggr_block_size = strtol(optarg, NULL, 0); break;#endif case 'c': /* check for bad blocks */ verify_blocks = true; break; case 'j': /* external journal device */ if (j_selected) { mkfs_usage(); return EINVAL; } else { strcpy(logdev, optarg); j_selected = true; } break; case 'J': /* external journal device */ if (j_selected) { mkfs_usage(); return EINVAL; } else { parse_journal_opts(optarg, &ext_journal_opt, logdev); j_selected = true; } break; case 'L': /* volume label */ strncpy(volume_label, optarg, 16); break; case 'O': /* case insensitive for OS/2 compatibility */ type_jfs |= JFS_OS2; type_jfs &= ~(JFS_LINUX | JFS_DIR_INDEX); break; case 'f': case 'q': /* quiet, don't ask questions */ no_questions_asked = true; break; case 's': /* log size */ logsize = strtol(optarg, NULL, 0); if (!logsize) { printf("\nLog size not specified for -s.\n"); mkfs_usage(); return EINVAL; } logsize_in_bytes = ((int64_t) logsize) * MEGABYTE; break; case 'V': /* print version and exit */ exit(0); break; default: mkfs_usage(); return EINVAL; } } if (optind != argc - 1) { printf ("\nError: Device not specified or command format error\n"); mkfs_usage(); return EINVAL; } device_name = argv[optind]; /* * If we are only creating an external journal, set the * log device name for later formatting of the log device */ if (ext_journal_opt == create_journal_only) { strcpy(logdev, argv[optind]); } /* Check for block device */ { struct stat stat_data; int rc; rc = stat(device_name, &stat_data); if (rc != 0) { message_user(MSG_OSO_CANT_FIND_DRIVE, NULL, 0, OSO_MSG); return EINVAL; } /* Do we have a block special device or regular file? */ if (!S_ISBLK(stat_data.st_mode) && !S_ISREG(stat_data.st_mode)) { msg_parms[0] = device_name; message_user(MSG_OSO_NOT_VALID_BLOCK_DEVICE, msg_parms, 1, OSO_MSG); return EINVAL; } } /* Is the device mounted? We will NOT format a mounted device! */ rc = Is_Device_Mounted(device_name); if (rc) { message_user(rc, NULL, 0, JFS_MSG); return ERROR_INVALID_ACCESS; } /* * Open the device and lock it from all others * Get the physical block size of the device. */ dev_handle = open(device_name, O_RDWR | O_EXCL, 0); if (dev_handle < 0) { printf("\nError: Cannot open device %s.\n", device_name); return (ERROR_FILE_NOT_FOUND); } if (Bad_Device(dev_handle)) { message_user(MSG_OSO_DISK_LOCKED, NULL, 0, OSO_MSG); return (ERROR_FILE_NOT_FOUND); } /* * Check if aggr_block_size was specified. If not, we make it 4K. */ if (aggr_block_size == 0) { aggr_block_size = PSIZE; /* 4096 */ } else if (aggr_block_size < phys_block_size) { /* * Make sure the aggr_block_size is not smaller than the * logical volume block size. */ sprintf(msgstr, "%d", aggr_block_size); msg_parms[0] = msgstr; msg_parms[1] = "BS"; message_user(MSG_OSO_VALUE_NOT_ALLOWED, msg_parms, 2, OSO_MSG); rc = EINVAL; close(dev_handle); return (rc); } else { /* * Validate user specified aggregate block size */ if (!inrange(aggr_block_size, MINBLOCKSIZE, MAXBLOCKSIZE)) { sprintf(msgstr, "%d", aggr_block_size); msg_parms[0] = msgstr; msg_parms[1] = "BS"; message_user(MSG_OSO_VALUE_NOT_ALLOWED, msg_parms, 2, OSO_MSG); rc = EINVAL; close(dev_handle); return (rc); } } /* * get size of the logical volume */ rc = ujfs_get_dev_size(dev_handle, &bytes_on_device); if (rc != 0) { DBG_ERROR(("ujfs_get_dev_size: FAILED rc = %lld\n", rc)) message_user(MSG_OSO_FORMAT_FAILED, NULL, 0, OSO_MSG); close(dev_handle); return (rc); } number_of_bytes = bytes_on_device; DBG_TRACE(("ujfs_get_dev_size: size = %lld\n", number_of_bytes)) /* * Make sure we have at least MINJFS for our file system * Notes: The operating system takes some of the bytes from the * partition to use for its own information. The end user is not * aware of this space, so we want to compare a posted minimum * size to the actual size of the partition, not just the space * available for our use. */ if (number_of_bytes < MINJFS) { if (number_of_bytes == 0) { /* Not readable at all */ message_user(MSG_JFS_BAD_PART, msg_parms, 0, JFS_MSG); } else { /* Really too small */ strcpy(msgstr, MINJFSTEXT); msg_parms[0] = msgstr; message_user(MSG_JFS_PART_SMALL, msg_parms, 1, JFS_MSG); } rc = EINVAL; close(dev_handle); return (rc); } /* * Size of filesystem in terms of block size */ number_of_blocks = number_of_bytes / aggr_block_size; DBG_TRACE(("number of blocks = %lld\n", number_of_blocks)) /* now ask the user if he really wants to destoy his data */ if (no_questions_asked == false) { if (ext_journal_opt != create_journal_only) { msg_parms[0] = device_name; message_user(MSG_OSO_DESTROY_DATA, msg_parms, 1, OSO_MSG); } if (logdev[0] && (ext_journal_opt != use_existing_journal)) { msg_parms[0] = logdev; message_user(MSG_OSO_DESTROY_DATA, msg_parms, 1, OSO_MSG); } do { printf("\nContinue? (Y/N) "); c = getchar(); if (c == 'n' || c == 'N') { close(dev_handle); return 0; } else if (c == EOF) abort(); } while (c != 'y' && c != 'Y'); } /* * Create journal log for aggregate to use * * For the prototype we will only create a journal log if one was * specified on the command line. Eventually we will always need one * and we will need a default method of finding and creating the log. */ if (logdev[0] != '\0') { int fd; /* A log device was specified on the command line. Call * jfs_logform() to initialize the log */ fd = open(logdev, LogOpenMode, 0); if (fd < 0) { message_user(MSG_OSO_FORMAT_FAILED, NULL, 0, OSO_MSG); close(dev_handle); return -1; } /* * If the user specified logging to an existing external * journal device, find it and validate its superblock. */ if (ext_journal_opt == use_existing_journal) { struct logsuper logsup; rc = ujfs_get_logsuper(fd, &logsup); if (!rc) { /* see if superblock is JFS log superblock */ rc = ujfs_validate_logsuper(&logsup); if (!rc) { uuid_copy(log_uuid, logsup.uuid); } else { rc = -1; printf ("\n%s does not contain a valid log superblock.\n" "You may only attach a previously formatted \n" "JFS external journal to a JFS file system.\n\n", logdev); } } else { printf ("\nError: Couldn't read log superblock from %s\n", logdev); } } else { /* create new external journal device */ uuid_clear(log_uuid); /* * If a volume label has been specified and we're only * creating an external journal, apply the label to the * external journal. */ if ((ext_journal_opt == create_journal_only) && volume_label) { label_ptr = volume_label; } rc = jfs_logform(fd, aggr_block_size, log2shift(aggr_block_size), type_jfs | type_commit, 0, 0, log_uuid, label_ptr); } close(fd); if (rc != 0) { message_user(MSG_OSO_FORMAT_FAILED, NULL, 0, OSO_MSG); DBG_ERROR(("Internal error: Format failed rc=%x\n", rc)) close(dev_handle); return (rc); } else if (ext_journal_opt == create_journal_only) { printf ("\nJFS external journal created SUCCESSFULLY on %s.\n", logdev); close(dev_handle); return (rc); } } else { type_jfs |= JFS_INLINELOG; if (logsize == 0) { /* If no size specified, let's default to .4 of * aggregate size; Which for simplicity is * 4/1024 == 2**2/2**10 == 1/2**8 == >> 8 * * Round logsize up to a megabyte boundary */ /* BYTES / 256 */ logsize_in_bytes = number_of_bytes >> 8; /* round up to meg */ logsize_in_bytes = (logsize_in_bytes + MEGABYTE - 1) & ~(MEGABYTE - 1); if (logsize_in_bytes > MEGABYTE32) { logsize_in_bytes = MEGABYTE32; } } /* Convert logsize into aggregate blocks */ logsize = logsize_in_bytes / aggr_block_size; if (logsize >= (number_of_blocks / MAX_LOG_PERCENTAGE)) { message_user(MSG_JFS_LOG_LARGE, NULL, 0, JFS_MSG); rc = ENOSPC; message_user(MSG_OSO_FORMAT_FAILED, NULL, 0, OSO_MSG); DBG_ERROR(("Internal error: Format failed rc=%x\n", rc)) close(dev_handle); return (rc); } logloc = number_of_blocks - logsize; number_of_blocks -= logsize; l2absize = log2shift(aggr_block_size); rc = jfs_logform(dev_handle, aggr_block_size, l2absize, type_jfs | type_commit, logloc, logsize, NULL, NULL); if (rc != 0) { message_user(MSG_OSO_FORMAT_FAILED, NULL, 0, OSO_MSG); DBG_ERROR(("Internal error: Format failed rc=%x\n", rc)) close(dev_handle); return (rc); } } /* * Create aggregate, which will also create a fileset as necessary */ rc = create_aggregate(dev_handle, volume_label, number_of_blocks, aggr_block_size, phys_block_size, type_jfs | type_commit, logdev, logloc, logsize, verify_blocks, log_uuid); close(dev_handle); /* Format Complete message */ if (rc == 0) { message_user(MSG_OSO_FORMAT_COMPLETE, NULL, 0, OSO_MSG); sprintf(msgstr, "%lld", (long long) (number_of_bytes / 1024)); msg_parms[0] = msgstr; /* total disk space msg */ message_user(MSG_OSO_DISK_SPACE2, msg_parms, 1, OSO_MSG); } if (rc) { message_user(MSG_OSO_FORMAT_FAILED, NULL, 0, OSO_MSG); DBG_ERROR(("Internal error: Format failed rc=%x\n", rc)) } return rc;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -