static void get_block_device_size(struct device_info *info, int fd) { unsigned long long bytes; if (!blkdev_get_size(fd, &bytes) && bytes != 0) info->size = bytes; }
/* get 512-byte sector count */ int blkdev_get_sectors(int fd, unsigned long long *sectors) { unsigned long long bytes; if (blkdev_get_size(fd, &bytes) == 0) { *sectors = (bytes >> 9); return 0; }
static uint64_t _get_num_sectors(int fd) { unsigned long long bytes=0; if (blkdev_get_size(fd, &bytes) == -1) return 0; return bytes / _get_sector_size(fd); }
/* return size in pages */ static unsigned long long get_size(const struct mkswap_control *ctl) { int fd; unsigned long long size; fd = open(ctl->devname, O_RDONLY); if (fd < 0) err(EXIT_FAILURE, _("cannot open %s"), ctl->devname); if (blkdev_get_size(fd, &size) == 0) size /= ctl->pagesize; close(fd); return size; }
/* return size in pages */ static unsigned long long get_size(const char *file) { int fd; unsigned long long size; fd = open(file, O_RDONLY); if (fd < 0) { perror(file); exit(1); } if (blkdev_get_size(fd, &size) == 0) size /= pagesize; close(fd); return size; }
static void test_super(int *start, size_t * length) { struct stat st; /* find the physical size of the file or block device */ if (stat(filename, &st) < 0) err(FSCK_EX_ERROR, _("stat failed %s"), filename); fd = open(filename, O_RDONLY); if (fd < 0) err(FSCK_EX_ERROR, _("cannot open %s"), filename); if (S_ISBLK(st.st_mode)) { unsigned long long bytes; if (blkdev_get_size(fd, &bytes)) err(FSCK_EX_ERROR, _("ioctl failed: unable to determine device size: %s"), filename); *length = bytes; } else if (S_ISREG(st.st_mode)) *length = st.st_size; else errx(FSCK_EX_ERROR, _("not a block device or file: %s"), filename); if (*length < sizeof(struct cramfs_super)) errx(FSCK_EX_UNCORRECTED, _("file length too short")); /* find superblock */ if (read(fd, &super, sizeof(super)) != sizeof(super)) err(FSCK_EX_ERROR, _("cannot read %s"), filename); if (get_superblock_endianness(super.magic) != -1) *start = 0; else if (*length >= (PAD_SIZE + sizeof(super))) { if (lseek(fd, PAD_SIZE, SEEK_SET) == (off_t) -1) err(FSCK_EX_ERROR, _("seek on %s failed"), filename); if (read(fd, &super, sizeof(super)) != sizeof(super)) err(FSCK_EX_ERROR, _("cannot read %s"), filename); if (get_superblock_endianness(super.magic) != -1) *start = PAD_SIZE; else errx(FSCK_EX_UNCORRECTED, _("superblock magic not found")); } else errx(FSCK_EX_UNCORRECTED, _("superblock magic not found")); if (opt_verbose) printf(_("cramfs endianness is %s\n"), cramfs_is_big_endian ? _("big") : _("little")); super_toggle_endianness(cramfs_is_big_endian, &super); if (super.flags & ~CRAMFS_SUPPORTED_FLAGS) errx(FSCK_EX_ERROR, _("unsupported filesystem features")); /* What are valid superblock sizes? */ if (super.size < sizeof(struct cramfs_super)) errx(FSCK_EX_UNCORRECTED, _("superblock size (%d) too small"), super.size); if (super.flags & CRAMFS_FLAG_FSID_VERSION_2) { if (super.fsid.files == 0) errx(FSCK_EX_UNCORRECTED, _("zero file count")); if (*length < super.size) errx(FSCK_EX_UNCORRECTED, _("file length too short")); else if (*length > super.size) warnx(_("file extends past end of filesystem")); } else warnx(_("old cramfs format")); }
static int swapon_checks(const char *special) { struct stat st; int fd = -1, sig; char *hdr = NULL; unsigned int pagesize; unsigned long long devsize = 0; if (stat(special, &st) < 0) { warn(_("%s: stat failed"), special); goto err; } /* people generally dislike this warning - now it is printed only when `verbose' is set */ if (verbose) { int permMask = (S_ISBLK(st.st_mode) ? 07007 : 07077); if ((st.st_mode & permMask) != 0) warnx(_("%s: insecure permissions %04o, %04o suggested."), special, st.st_mode & 07777, ~permMask & 0666); if (S_ISREG(st.st_mode) && st.st_uid != 0) warnx(_("%s: insecure file owner %d, 0 (root) suggested."), special, st.st_uid); } /* test for holes by LBT */ if (S_ISREG(st.st_mode)) { if (st.st_blocks * 512 < st.st_size) { warnx(_("%s: skipping - it appears to have holes."), special); goto err; } devsize = st.st_size; } fd = open(special, O_RDONLY); if (fd == -1) { warn(_("%s: open failed"), special); goto err; } if (S_ISBLK(st.st_mode) && blkdev_get_size(fd, &devsize)) { warn(_("%s: get size failed"), special); goto err; } hdr = swap_get_header(fd, &sig, &pagesize); if (!hdr) { warn(_("%s: read swap header failed"), special); goto err; } if (sig == SIG_SWAPSPACE && pagesize) { unsigned long long swapsize = swap_get_size(hdr, special, pagesize); int syspg = getpagesize(); if (verbose) warnx(_("%s: pagesize=%d, swapsize=%llu, devsize=%llu"), special, pagesize, swapsize, devsize); if (swapsize > devsize) { if (verbose) warnx(_("%s: last_page 0x%08llx is larger" " than actual size of swapspace"), special, swapsize); } else if (syspg < 0 || (unsigned) syspg != pagesize) { if (fixpgsz) { warnx(_("%s: swap format pagesize does not match."), special); if (swap_reinitialize(special) < 0) goto err; } else warnx(_("%s: swap format pagesize does not match. " "(Use --fixpgsz to reinitialize it.)"), special); } } else if (sig == SIG_SWSUSPEND) { /* We have to reinitialize swap with old (=useless) software suspend * data. The problem is that if we don't do it, then we get data * corruption the next time an attempt at unsuspending is made. */ warnx(_("%s: software suspend data detected. " "Rewriting the swap signature."), special); if (swap_rewrite_signature(special, pagesize) < 0) goto err; } free(hdr); close(fd); return 0; err: if (fd != -1) close(fd); free(hdr); return -1; }