1 /* libparted - a library for manipulating disk partitions
2 Copyright (C) 1999 - 2005, 2007, 2008, 2009 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #define PROC_DEVICES_BUFSIZ 16384
21 #include <arch/linux.h>
23 #include <parted/parted.h>
24 #include <parted/debug.h>
25 #if defined __s390__ || defined __s390x__
26 #include <parted/fdasd.h>
38 #include <sys/ioctl.h>
40 #include <sys/types.h>
41 #include <sys/utsname.h> /* for uname() */
42 #include <scsi/scsi.h>
43 #ifdef ENABLE_DEVICE_MAPPER
44 #include <libdevmapper.h>
48 #include "../architecture.h"
53 # define _(String) dgettext (PACKAGE, String)
55 # define _(String) (String)
56 #endif /* ENABLE_NLS */
58 #define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
61 #define __NR__llseek 140
64 #ifndef SCSI_IOCTL_SEND_COMMAND
65 #define SCSI_IOCTL_SEND_COMMAND 1
68 /* from <linux/hdreg.h> */
69 #define HDIO_GETGEO 0x0301 /* get device geometry */
70 #define HDIO_GET_IDENTITY 0x030d /* get IDE identification info */
72 #define RD_MODE (O_RDONLY)
73 #define WR_MODE (O_WRONLY)
74 #define RW_MODE (O_RDWR)
78 unsigned char sectors;
79 unsigned short cylinders;
83 struct ata7_sectinfo {
90 /* structure returned by HDIO_GET_IDENTITY, as per ANSI ATA2 rev.2f spec */
92 unsigned short config; /* lots of obsolete bit flags */
93 unsigned short cyls; /* "physical" cyls */
94 unsigned short reserved2; /* reserved (word 2) */
95 unsigned short heads; /* "physical" heads */
96 unsigned short track_bytes; /* unformatted bytes per track */
97 unsigned short sector_bytes; /* unformatted bytes per sector */
98 unsigned short sectors; /* "physical" sectors per track */
99 unsigned short vendor0; /* vendor unique */
100 unsigned short vendor1; /* vendor unique */
101 unsigned short vendor2; /* vendor unique */
102 unsigned char serial_no[20]; /* 0 = not_specified */
103 unsigned short buf_type;
104 unsigned short buf_size; /* 512 byte increments;
106 unsigned short ecc_bytes; /* for r/w long cmds;
108 unsigned char fw_rev[8]; /* 0 = not_specified */
109 char model[40]; /* 0 = not_specified */
110 unsigned char max_multsect; /* 0=not_implemented */
111 unsigned char vendor3; /* vendor unique */
112 unsigned short dword_io; /* 0=not_implemented; 1=implemented */
113 unsigned char vendor4; /* vendor unique */
114 unsigned char capability; /* bits 0:DMA 1:LBA 2:IORDYsw
116 unsigned short reserved50; /* reserved (word 50) */
117 unsigned char vendor5; /* vendor unique */
118 unsigned char tPIO; /* 0=slow, 1=medium, 2=fast */
119 unsigned char vendor6; /* vendor unique */
120 unsigned char tDMA; /* 0=slow, 1=medium, 2=fast */
121 unsigned short field_valid; /* bits 0:cur_ok 1:eide_ok */
122 unsigned short cur_cyls; /* logical cylinders */
123 unsigned short cur_heads; /* logical heads */
124 unsigned short cur_sectors; /* logical sectors per track */
125 unsigned short cur_capacity0; /* logical total sectors on drive */
126 unsigned short cur_capacity1; /* (2 words, misaligned int) */
127 unsigned char multsect; /* current multiple sector count */
128 unsigned char multsect_valid; /* when (bit0==1) multsect is ok */
129 unsigned int lba_capacity; /* total number of sectors */
130 unsigned short dma_1word; /* single-word dma info */
131 unsigned short dma_mword; /* multiple-word dma info */
132 unsigned short eide_pio_modes; /* bits 0:mode3 1:mode4 */
133 unsigned short eide_dma_min; /* min mword dma cycle time (ns) */
134 unsigned short eide_dma_time; /* recommended mword dma cycle
136 unsigned short eide_pio; /* min cycle time (ns), no IORDY */
137 unsigned short eide_pio_iordy; /* min cycle time (ns), with IORDY */
138 unsigned short words69_70[2]; /* reserved words 69-70 */
139 /* HDIO_GET_IDENTITY currently returns only words 0 through 70 */
140 unsigned short words71_74[4]; /* reserved words 71-74 */
141 unsigned short queue_depth; /* */
142 unsigned short words76_79[4]; /* reserved words 76-79 */
143 unsigned short major_rev_num; /* */
144 unsigned short minor_rev_num; /* */
145 unsigned short command_set_1; /* bits 0:Smart 1:Security 2:Removable
147 unsigned short command_set_2; /* bits 14:Smart Enabled 13:0 zero */
148 unsigned short cfsse; /* command set-feature supported
150 unsigned short cfs_enable_1; /* command set-feature enabled */
151 unsigned short cfs_enable_2; /* command set-feature enabled */
152 unsigned short csf_default; /* command set-feature default */
153 unsigned short dma_ultra; /* */
154 unsigned short word89; /* reserved (word 89) */
155 unsigned short word90; /* reserved (word 90) */
156 unsigned short CurAPMvalues; /* current APM values */
157 unsigned short word92; /* reserved (word 92) */
158 unsigned short hw_config; /* hardware config */
159 unsigned short words94_105[12];/* reserved words 94-105 */
160 struct ata7_sectinfo ata7_sectinfo; /* ATAPI/ATA7 physical and logical
162 unsigned short words107_116[10];/* reserved words 107-116 */
163 unsigned int logical_sectsize;/* ATAPI/ATA7 logical sector size */
164 unsigned short words119_125[7];/* reserved words 119-125 */
165 unsigned short last_lun; /* reserved (word 126) */
166 unsigned short word127; /* reserved (word 127) */
167 unsigned short dlf; /* device lock function
169 * 8 security level 1:max 0:high
178 unsigned short csfo; /* current set features options
185 unsigned short words130_155[26];/* reserved vendor words 130-155 */
186 unsigned short word156;
187 unsigned short words157_159[3]; /* reserved vendor words 157-159 */
188 unsigned short words160_255[95];/* reserved words 160-255 */
191 /* from <linux/fs.h> */
192 #define BLKRRPART _IO(0x12,95) /* re-read partition table */
193 #define BLKGETSIZE _IO(0x12,96) /* return device size */
194 #define BLKFLSBUF _IO(0x12,97) /* flush buffer cache */
195 #define BLKSSZGET _IO(0x12,104) /* get block device sector size */
196 #define BLKGETLASTSECT _IO(0x12,108) /* get last sector of block device */
197 #define BLKSETLASTSECT _IO(0x12,109) /* set last sector of block device */
199 /* return device size in bytes (u64 *arg) */
200 #define BLKGETSIZE64 _IOR(0x12,114,size_t)
202 struct blkdev_ioctl_param {
204 size_t content_length;
205 char * block_contents;
208 /* from <linux/major.h> */
210 #define IDE1_MAJOR 22
211 #define IDE2_MAJOR 33
212 #define IDE3_MAJOR 34
213 #define IDE4_MAJOR 56
214 #define IDE5_MAJOR 57
215 #define SCSI_CDROM_MAJOR 11
216 #define SCSI_DISK0_MAJOR 8
217 #define SCSI_DISK1_MAJOR 65
218 #define SCSI_DISK2_MAJOR 66
219 #define SCSI_DISK3_MAJOR 67
220 #define SCSI_DISK4_MAJOR 68
221 #define SCSI_DISK5_MAJOR 69
222 #define SCSI_DISK6_MAJOR 70
223 #define SCSI_DISK7_MAJOR 71
224 #define COMPAQ_SMART2_MAJOR 72
225 #define COMPAQ_SMART2_MAJOR1 73
226 #define COMPAQ_SMART2_MAJOR2 74
227 #define COMPAQ_SMART2_MAJOR3 75
228 #define COMPAQ_SMART2_MAJOR4 76
229 #define COMPAQ_SMART2_MAJOR5 77
230 #define COMPAQ_SMART2_MAJOR6 78
231 #define COMPAQ_SMART2_MAJOR7 79
232 #define COMPAQ_SMART_MAJOR 104
233 #define COMPAQ_SMART_MAJOR1 105
234 #define COMPAQ_SMART_MAJOR2 106
235 #define COMPAQ_SMART_MAJOR3 107
236 #define COMPAQ_SMART_MAJOR4 108
237 #define COMPAQ_SMART_MAJOR5 109
238 #define COMPAQ_SMART_MAJOR6 110
239 #define COMPAQ_SMART_MAJOR7 111
240 #define DAC960_MAJOR 48
241 #define ATARAID_MAJOR 114
242 #define I2O_MAJOR1 80
243 #define I2O_MAJOR2 81
244 #define I2O_MAJOR3 82
245 #define I2O_MAJOR4 83
246 #define I2O_MAJOR5 84
247 #define I2O_MAJOR6 85
248 #define I2O_MAJOR7 86
249 #define I2O_MAJOR8 87
251 #define DASD_MAJOR 94
252 #define VIODASD_MAJOR 112
253 #define SX8_MAJOR1 160
254 #define SX8_MAJOR2 161
255 #define XVD_MAJOR 202
256 #define SDMMC_MAJOR 179
258 #define SCSI_BLK_MAJOR(M) ( \
259 (M) == SCSI_DISK0_MAJOR \
260 || (M) == SCSI_CDROM_MAJOR \
261 || ((M) >= SCSI_DISK1_MAJOR && (M) <= SCSI_DISK7_MAJOR))
263 /* Maximum number of partitions supported by linux. */
264 #define MAX_NUM_PARTS 64
266 static char* _device_get_part_path (PedDevice* dev, int num);
267 static int _partition_is_mounted_by_path (const char* path);
270 _read_fd (int fd, char **buf)
273 size_t size = PROC_DEVICES_BUFSIZ;
276 *buf = malloc (size * sizeof (char));
282 p = &(*buf) [filesize];
283 s = read (fd, p, PROC_DEVICES_BUFSIZ);
284 /* exit if there is an error or EOF is reached */
289 char *new_buf = realloc (*buf, size);
290 if (new_buf == NULL) {
291 int saved_errno = errno;
299 if (filesize == 0 && s < 0) {
304 char *new_buf = realloc (*buf, filesize + 1);
305 if (new_buf == NULL) {
306 int saved_errno = errno;
312 (*buf)[filesize] = '\0';
319 _major_type_in_devices (int major, const char* type)
328 fd = open ("/proc/devices", O_RDONLY);
332 if (_read_fd(fd, &buf) < 0) {
338 end = strchr(line, '\n');
347 if (!strncmp(line, "Block devices:", 14))
352 name = strrchr(line, ' ');
353 if (!name || strcmp(name+1, type))
356 maj = strtol(line, &name, 10);
366 end = strchr(line, '\n');
374 _is_ide_major (int major)
391 _is_cpqarray_major (int major)
393 return ((COMPAQ_SMART2_MAJOR <= major && major <= COMPAQ_SMART2_MAJOR7)
394 || (COMPAQ_SMART_MAJOR <= major && major <= COMPAQ_SMART_MAJOR7));
398 _is_i2o_major (int major)
400 return (I2O_MAJOR1 <= major && major <= I2O_MAJOR8);
404 _is_sx8_major (int major)
406 return (SX8_MAJOR1 <= major && major <= SX8_MAJOR2);
410 _is_virtblk_major (int major)
412 return _major_type_in_devices (major, "virtblk");
415 #ifdef ENABLE_DEVICE_MAPPER
417 _is_dm_major (int major)
419 return _major_type_in_devices (major, "device-mapper");
423 _dm_maptype (PedDevice *dev)
425 LinuxSpecific* arch_specific = LINUX_SPECIFIC (dev);
427 uint64_t start, length;
428 char *target_type = NULL;
431 const char* dev_dir = getenv ("DM_DEV_DIR");
433 if (dev_dir && *dev_dir && !dm_set_dev_dir(dev_dir))
436 if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
439 if (!dm_task_set_major_minor(dmt, arch_specific->major,
440 arch_specific->minor, 0))
443 dm_task_no_open_count(dmt);
445 if (!dm_task_run(dmt))
448 dm_get_next_target(dmt, NULL, &start, &length, &target_type, ¶ms);
450 arch_specific->dmtype = strdup(target_type ? target_type : "NO-TARGET");
451 if (arch_specific->dmtype == NULL)
455 dm_task_destroy(dmt);
465 char buf [512]; /* readdir(3) claims d_name[256] */
468 mapper_dir = opendir ("/dev/mapper");
472 /* Search the /dev/mapper directory for devices w/ the same major
473 * number that was returned from _probe_lvm_major().
475 while ((dent = readdir (mapper_dir))) {
476 if (strcmp (dent->d_name, ".") == 0 ||
477 strcmp (dent->d_name, "..") == 0)
480 snprintf (buf, sizeof (buf), "/dev/mapper/%s", dent->d_name);
482 if (stat (buf, &st) != 0)
485 if (_is_dm_major(major(st.st_rdev)))
486 _ped_device_probe (buf);
488 closedir (mapper_dir);
495 _device_stat (PedDevice* dev, struct stat * dev_stat)
497 PED_ASSERT (dev != NULL, return 0);
498 PED_ASSERT (!dev->external_mode, return 0);
501 if (!stat (dev->path, dev_stat)) {
504 if (ped_exception_throw (
506 PED_EXCEPTION_RETRY_CANCEL,
507 _("Could not stat device %s - %s."),
510 != PED_EXCEPTION_RETRY)
517 _device_probe_type (PedDevice* dev)
519 struct stat dev_stat;
522 LinuxSpecific* arch_specific = LINUX_SPECIFIC (dev);
524 if (!_device_stat (dev, &dev_stat))
527 if (!S_ISBLK(dev_stat.st_mode)) {
528 dev->type = PED_DEVICE_FILE;
532 arch_specific->major = dev_major = major (dev_stat.st_rdev);
533 arch_specific->minor = dev_minor = minor (dev_stat.st_rdev);
535 if (SCSI_BLK_MAJOR (dev_major) && (dev_minor % 0x10 == 0)) {
536 dev->type = PED_DEVICE_SCSI;
537 } else if (_is_ide_major (dev_major) && (dev_minor % 0x40 == 0)) {
538 dev->type = PED_DEVICE_IDE;
539 } else if (dev_major == DAC960_MAJOR && (dev_minor % 0x8 == 0)) {
540 dev->type = PED_DEVICE_DAC960;
541 } else if (dev_major == ATARAID_MAJOR && (dev_minor % 0x10 == 0)) {
542 dev->type = PED_DEVICE_ATARAID;
543 } else if (dev_major == DASD_MAJOR && (dev_minor % 0x4 == 0)) {
544 dev->type = PED_DEVICE_DASD;
545 } else if (dev_major == VIODASD_MAJOR && (dev_minor % 0x8 == 0)) {
546 dev->type = PED_DEVICE_VIODASD;
547 } else if (_is_sx8_major(dev_major) && (dev_minor % 0x20 == 0)) {
548 dev->type = PED_DEVICE_SX8;
549 } else if (_is_i2o_major (dev_major) && (dev_minor % 0x10 == 0)) {
550 dev->type = PED_DEVICE_I2O;
551 } else if (_is_cpqarray_major (dev_major) && (dev_minor % 0x10 == 0)) {
552 dev->type = PED_DEVICE_CPQARRAY;
553 } else if (dev_major == UBD_MAJOR && (dev_minor % 0x10 == 0)) {
554 dev->type = PED_DEVICE_UBD;
555 #ifdef ENABLE_DEVICE_MAPPER
556 } else if (_is_dm_major(dev_major)) {
557 dev->type = PED_DEVICE_DM;
558 if (_dm_maptype(dev)) {
559 ped_exception_throw (
561 PED_EXCEPTION_CANCEL,
562 _("Unable to determine the dm type of %s."),
566 } else if (dev_major == XVD_MAJOR && (dev_minor % 0x10 == 0)) {
567 dev->type = PED_DEVICE_XVD;
568 } else if (dev_major == SDMMC_MAJOR && (dev_minor % 0x08 == 0)) {
569 dev->type = PED_DEVICE_SDMMC;
570 } else if (_is_virtblk_major(dev_major)) {
571 dev->type = PED_DEVICE_VIRTBLK;
573 dev->type = PED_DEVICE_UNKNOWN;
580 _get_linux_version ()
582 static int kver = -1;
594 if (sscanf (uts.release, "%u.%u.%u", &major, &minor, &teeny) != 3)
597 return kver = KERNEL_VERSION (major, minor, teeny);
603 static int have_kern26 = -1;
606 if (have_kern26 != -1)
609 kver = _get_linux_version();
610 return have_kern26 = kver >= KERNEL_VERSION (2,6,0) ? 1 : 0;
615 get_blkid_topology (LinuxSpecific *arch_specific)
617 arch_specific->probe = blkid_new_probe ();
618 if (!arch_specific->probe)
621 if (blkid_probe_set_device(arch_specific->probe,
622 arch_specific->fd, 0, 0))
625 arch_specific->topology =
626 blkid_probe_get_topology(arch_specific->probe);
631 _device_set_sector_size (PedDevice* dev)
633 LinuxSpecific* arch_specific = LINUX_SPECIFIC (dev);
636 dev->sector_size = PED_SECTOR_SIZE_DEFAULT;
637 dev->phys_sector_size = PED_SECTOR_SIZE_DEFAULT;
639 PED_ASSERT (dev->open_count, return);
641 if (_get_linux_version() < KERNEL_VERSION (2,3,0)) {
642 dev->sector_size = PED_SECTOR_SIZE_DEFAULT;
646 if (ioctl (arch_specific->fd, BLKSSZGET, §or_size)) {
647 ped_exception_throw (
648 PED_EXCEPTION_WARNING,
650 _("Could not determine sector size for %s: %s.\n"
651 "Using the default sector size (%lld)."),
652 dev->path, strerror (errno), PED_SECTOR_SIZE_DEFAULT);
654 dev->sector_size = (long long)sector_size;
658 get_blkid_topology(arch_specific);
659 if (!arch_specific->topology) {
660 ped_exception_throw (
661 PED_EXCEPTION_WARNING,
663 _("Could not determine minimum io size for %s: %s.\n"
664 "Using the default size (%lld)."),
665 dev->path, strerror (errno), PED_SECTOR_SIZE_DEFAULT);
667 dev->phys_sector_size =
668 blkid_topology_get_minimum_io_size(
669 arch_specific->topology);
673 #if defined __s390__ || defined __s390x__
674 /* Return PED_SECTOR_SIZE_DEFAULT for DASDs. */
675 if (dev->type == PED_DEVICE_DASD
676 || dev->type == PED_DEVICE_FILE) {
677 arch_specific->real_sector_size = dev->sector_size;
678 dev->sector_size = PED_SECTOR_SIZE_DEFAULT;
682 if (dev->sector_size != PED_SECTOR_SIZE_DEFAULT) {
683 ped_exception_throw (
684 PED_EXCEPTION_WARNING,
686 _("Device %s has a logical sector size of %lld. Not "
687 "all parts of GNU Parted support this at the moment, "
688 "and the working code is HIGHLY EXPERIMENTAL.\n"),
689 dev->path, dev->sector_size);
694 _kernel_has_blkgetsize64(void)
696 int version = _get_linux_version();
698 if (version >= KERNEL_VERSION (2,5,4)) return 1;
699 if (version < KERNEL_VERSION (2,5,0) &&
700 version >= KERNEL_VERSION (2,4,18)) return 1;
704 /* TODO: do a binary search if BLKGETSIZE doesn't work?! */
706 _device_get_length (PedDevice* dev)
709 LinuxSpecific* arch_specific = LINUX_SPECIFIC (dev);
713 PED_ASSERT (dev->open_count > 0, return 0);
714 PED_ASSERT (dev->sector_size % PED_SECTOR_SIZE_DEFAULT == 0, return 0);
716 if (_kernel_has_blkgetsize64()) {
717 if (ioctl(arch_specific->fd, BLKGETSIZE64, &bytes) == 0) {
718 return bytes / dev->sector_size;
722 if (ioctl (arch_specific->fd, BLKGETSIZE, &size)) {
723 ped_exception_throw (
725 PED_EXCEPTION_CANCEL,
726 _("Unable to determine the size of %s (%s)."),
736 _device_probe_geometry (PedDevice* dev)
738 LinuxSpecific* arch_specific = LINUX_SPECIFIC (dev);
739 struct stat dev_stat;
740 struct hd_geometry geometry;
742 if (!_device_stat (dev, &dev_stat))
744 PED_ASSERT (S_ISBLK (dev_stat.st_mode), return 0);
746 _device_set_sector_size (dev);
748 dev->length = _device_get_length (dev);
752 /* The GETGEO ioctl is no longer useful (as of linux 2.6.x). We could
753 * still use it in 2.4.x, but this is contentious. Perhaps we should
755 dev->bios_geom.sectors = 63;
756 dev->bios_geom.heads = 255;
757 dev->bios_geom.cylinders
758 = dev->length / (63 * 255);
760 /* FIXME: what should we put here? (TODO: discuss on linux-kernel) */
761 if (!ioctl (arch_specific->fd, HDIO_GETGEO, &geometry)
762 && geometry.sectors && geometry.heads) {
763 dev->hw_geom.sectors = geometry.sectors;
764 dev->hw_geom.heads = geometry.heads;
765 dev->hw_geom.cylinders
766 = dev->length / (dev->hw_geom.heads
767 * dev->hw_geom.sectors);
769 dev->hw_geom = dev->bios_geom;
776 strip_name(char* str)
781 for (i = 0; str[i] != 0; i++) {
782 if (!isspace (str[i])
783 || (isspace (str[i]) && !isspace (str[i+1]) && str[i+1])) {
793 init_ide (PedDevice* dev)
795 LinuxSpecific* arch_specific = LINUX_SPECIFIC (dev);
796 struct stat dev_stat;
797 struct hd_driveid hdi;
798 PedExceptionOption ex_status;
800 int sector_multiplier = 0;
802 if (!_device_stat (dev, &dev_stat))
805 if (!ped_device_open (dev))
808 if (ioctl (arch_specific->fd, HDIO_GET_IDENTITY, &hdi)) {
809 ex_status = ped_exception_throw (
810 PED_EXCEPTION_WARNING,
811 PED_EXCEPTION_IGNORE_CANCEL,
812 _("Could not get identity of device %s - %s"),
813 dev->path, strerror (errno));
815 case PED_EXCEPTION_CANCEL:
816 goto error_close_dev;
818 case PED_EXCEPTION_UNHANDLED:
819 ped_exception_catch ();
820 case PED_EXCEPTION_IGNORE:
821 dev->model = strdup(_("Generic IDE"));
824 PED_ASSERT (0, (void) 0);
828 /* hdi.model is not guaranteed to be NULL terminated */
829 memcpy (hdi_buf, hdi.model, 40);
831 dev->model = strip_name (hdi_buf);
833 if (!hdi.ata7_sectinfo.valid1 && hdi.ata7_sectinfo.valid2)
834 sector_multiplier = hdi.ata7_sectinfo.multiplier;
836 sector_multiplier = 1;
838 if (sector_multiplier != 1) {
839 ex_status = ped_exception_throw (
840 PED_EXCEPTION_WARNING,
841 PED_EXCEPTION_IGNORE_CANCEL,
842 _("Device %s has multiple (%d) logical sectors "
843 "per physical sector.\n"
844 "GNU Parted supports this EXPERIMENTALLY for "
845 "some special disk label/file system "
846 "combinations, e.g. GPT and ext2/3.\n"
847 "Please consult the web site for up-to-date "
849 dev->path, sector_multiplier);
852 case PED_EXCEPTION_CANCEL:
853 goto error_close_dev;
855 case PED_EXCEPTION_UNHANDLED:
856 ped_exception_catch ();
857 case PED_EXCEPTION_IGNORE:
860 PED_ASSERT (0, (void) 0);
865 /* XXX sector_size has not been set yet! */
866 /* dev->phys_sector_size = dev->sector_size
867 * sector_multiplier;*/
868 dev->phys_sector_size = PED_SECTOR_SIZE_DEFAULT;
871 if (!_device_probe_geometry (dev))
872 goto error_close_dev;
874 ped_device_close (dev);
878 ped_device_close (dev);
883 /* This function reads the /sys entry named "file" for device "dev". */
885 read_device_sysfs_file (PedDevice *dev, const char *file)
891 snprintf (name_buf, 127, "/sys/block/%s/device/%s",
892 last_component (dev->path), file);
894 if ((f = fopen (name_buf, "r")) == NULL)
897 if (fgets (buf, 255, f) == NULL)
901 return strip_name (buf);
904 /* This function sends a query to a SCSI device for vendor and product
905 * information. It uses the deprecated SCSI_IOCTL_SEND_COMMAND to
909 scsi_query_product_info (PedDevice* dev, char **vendor, char **product)
911 /* The following are defined by the SCSI-2 specification. */
912 typedef struct _scsi_inquiry_cmd
915 uint8_t lun; /* bits 5-7 denote the LUN */
918 uint8_t alloc_length;
920 } __attribute__((packed)) scsi_inquiry_cmd_t;
922 typedef struct _scsi_inquiry_data
924 uint8_t peripheral_info;
926 uint8_t version_info;
928 uint8_t additional_length;
932 uint8_t vendor_id[8];
933 uint8_t product_id[16];
934 uint8_t product_revision[4];
935 uint8_t vendor_specific[20];
936 uint8_t _reserved3[40];
937 } __attribute__((packed)) scsi_inquiry_data_t;
946 scsi_inquiry_data_t out;
947 scsi_inquiry_cmd_t in;
951 LinuxSpecific* arch_specific = LINUX_SPECIFIC (dev);
957 memset (&arg, 0x00, sizeof(struct scsi_arg));
959 arg.outlen = sizeof(scsi_inquiry_data_t);
960 arg.data.in.op = INQUIRY;
961 arg.data.in.lun = dev->host << 5;
962 arg.data.in.alloc_length = sizeof(scsi_inquiry_data_t);
963 arg.data.in.page_code = 0;
964 arg.data.in.reserved = 0;
965 arg.data.in.control = 0;
967 if (ioctl (arch_specific->fd, SCSI_IOCTL_SEND_COMMAND, &arg) < 0)
970 memcpy (buf, arg.data.out.vendor_id, 8);
972 *vendor = strip_name (buf);
974 memcpy (buf, arg.data.out.product_id, 16);
976 *product = strip_name (buf);
981 /* This function provides the vendor and product name for a SCSI device.
982 * It supports both the modern /sys interface and direct queries
983 * via the deprecated ioctl, SCSI_IOCTL_SEND_COMMAND.
986 scsi_get_product_info (PedDevice* dev, char **vendor, char **product)
988 *vendor = read_device_sysfs_file (dev, "vendor");
989 *product = read_device_sysfs_file (dev, "model");
990 if (*vendor && *product)
993 return scsi_query_product_info (dev, vendor, product);
997 init_scsi (PedDevice* dev)
1002 uint32_t host_unique_id;
1005 LinuxSpecific* arch_specific = LINUX_SPECIFIC (dev);
1009 if (!ped_device_open (dev))
1012 if (ioctl (arch_specific->fd, SCSI_IOCTL_GET_IDLUN, &idlun) < 0) {
1015 if (ped_exception_throw (
1016 PED_EXCEPTION_ERROR, PED_EXCEPTION_IGNORE_CANCEL,
1017 _("Error initialising SCSI device %s - %s"),
1018 dev->path, strerror (errno))
1019 != PED_EXCEPTION_IGNORE)
1020 goto error_close_dev;
1021 if (!_device_probe_geometry (dev))
1022 goto error_close_dev;
1023 ped_device_close (dev);
1027 dev->host = idlun.host_unique_id;
1028 dev->did = idlun.dev_id;
1030 dev->model = (char*) ped_malloc (8 + 16 + 2);
1032 goto error_close_dev;
1034 if (scsi_get_product_info (dev, &vendor, &product)) {
1035 sprintf (dev->model, "%.8s %.16s", vendor, product);
1039 strcpy (dev->model, "Generic SCSI");
1042 if (!_device_probe_geometry (dev))
1043 goto error_close_dev;
1045 ped_device_close (dev);
1049 ped_device_close (dev);
1055 init_file (PedDevice* dev)
1057 struct stat dev_stat;
1059 if (!_device_stat (dev, &dev_stat))
1061 if (!ped_device_open (dev))
1064 dev->sector_size = PED_SECTOR_SIZE_DEFAULT;
1065 char *p = getenv ("PARTED_SECTOR_SIZE");
1068 if (0 < s && s % 512 == 0)
1069 dev->sector_size = s;
1071 dev->phys_sector_size = dev->sector_size;
1073 if (S_ISBLK(dev_stat.st_mode))
1074 dev->length = _device_get_length (dev);
1076 dev->length = dev_stat.st_size / dev->sector_size;
1077 if (dev->length <= 0) {
1078 ped_exception_throw (
1079 PED_EXCEPTION_ERROR,
1080 PED_EXCEPTION_CANCEL,
1081 _("The device %s has zero length, and can't possibly "
1082 "store a file system or partition table. Perhaps "
1083 "you selected the wrong device?"),
1085 goto error_close_dev;
1088 ped_device_close (dev);
1090 dev->bios_geom.cylinders = dev->length / 4 / 32;
1091 dev->bios_geom.heads = 4;
1092 dev->bios_geom.sectors = 32;
1093 dev->hw_geom = dev->bios_geom;
1094 dev->model = strdup ("");
1099 ped_device_close (dev);
1104 #if defined __s390__ || defined __s390x__
1106 init_dasd (PedDevice* dev, const char* model_name)
1108 struct stat dev_stat;
1109 struct hd_geometry geo;
1110 dasd_information_t dasd_info;
1112 if (!_device_stat (dev, &dev_stat))
1115 if (!ped_device_open (dev))
1118 LinuxSpecific* arch_specific = LINUX_SPECIFIC (dev);
1120 PED_ASSERT (S_ISBLK (dev_stat.st_mode), return 0);
1122 _device_set_sector_size (dev);
1123 if (!dev->sector_size)
1124 goto error_close_dev;
1126 dev->length = _device_get_length (dev);
1128 goto error_close_dev;
1130 if (!ioctl (arch_specific->fd, HDIO_GETGEO, &geo)) {
1131 dev->hw_geom.sectors = geo.sectors;
1132 dev->hw_geom.heads = geo.heads;
1133 dev->hw_geom.cylinders = dev->length
1134 / (dev->hw_geom.heads * dev->hw_geom.sectors)
1135 / (dev->sector_size / PED_SECTOR_SIZE_DEFAULT);
1136 dev->bios_geom = dev->hw_geom;
1138 dev->bios_geom.sectors = 12;
1139 dev->bios_geom.heads = 15;
1140 dev->bios_geom.cylinders = dev->length
1141 / (dev->hw_geom.heads * dev->hw_geom.sectors)
1142 / (dev->sector_size / PED_SECTOR_SIZE_DEFAULT);
1143 dev->hw_geom = dev->bios_geom;
1146 if (!ioctl(arch_specific->fd, BIODASDINFO, &dasd_info)) {
1147 arch_specific->devno = dasd_info.devno;
1149 arch_specific->devno = arch_specific->major * 256 +
1150 arch_specific->minor;
1153 dev->model = strdup (model_name);
1155 ped_device_close (dev);
1159 ped_device_close (dev);
1166 init_generic (PedDevice* dev, const char* model_name)
1168 struct stat dev_stat;
1169 PedExceptionOption ex_status;
1171 if (!_device_stat (dev, &dev_stat))
1174 if (!ped_device_open (dev))
1177 ped_exception_fetch_all ();
1178 if (_device_probe_geometry (dev)) {
1179 ped_exception_leave_all ();
1181 /* hack to allow use of files, for testing */
1182 ped_exception_catch ();
1183 ped_exception_leave_all ();
1185 ex_status = ped_exception_throw (
1186 PED_EXCEPTION_WARNING,
1187 PED_EXCEPTION_IGNORE_CANCEL,
1188 _("Unable to determine geometry of "
1189 "file/device %s. You should not use Parted "
1190 "unless you REALLY know what you're doing!"),
1192 switch (ex_status) {
1193 case PED_EXCEPTION_CANCEL:
1194 goto error_close_dev;
1196 case PED_EXCEPTION_UNHANDLED:
1197 ped_exception_catch ();
1198 case PED_EXCEPTION_IGNORE:
1201 PED_ASSERT (0, (void) 0);
1205 /* what should we stick in here? */
1206 dev->length = dev_stat.st_size / PED_SECTOR_SIZE_DEFAULT;
1207 dev->bios_geom.cylinders = dev->length / 4 / 32;
1208 dev->bios_geom.heads = 4;
1209 dev->bios_geom.sectors = 32;
1210 dev->sector_size = PED_SECTOR_SIZE_DEFAULT;
1211 dev->phys_sector_size = PED_SECTOR_SIZE_DEFAULT;
1214 dev->model = strdup (model_name);
1216 ped_device_close (dev);
1220 ped_device_close (dev);
1226 sdmmc_get_product_info (PedDevice* dev, char **type, char **name)
1228 *type = read_device_sysfs_file (dev, "type");
1229 *name = read_device_sysfs_file (dev, "name");
1237 init_sdmmc (PedDevice* dev)
1242 if (sdmmc_get_product_info (dev, &type, &name)) {
1243 snprintf (id, sizeof(id) - 1, "%s %s", type, name);
1247 snprintf (id, sizeof(id) - 1, "%s",
1248 _("Generic SD/MMC Storage Card"));
1250 return init_generic(dev, id);
1254 linux_new (const char* path)
1257 LinuxSpecific* arch_specific;
1259 PED_ASSERT (path != NULL, return NULL);
1261 dev = (PedDevice*) ped_malloc (sizeof (PedDevice));
1265 dev->path = strdup (path);
1267 goto error_free_dev;
1270 = (LinuxSpecific*) ped_malloc (sizeof (LinuxSpecific));
1271 if (!dev->arch_specific)
1272 goto error_free_path;
1273 arch_specific = LINUX_SPECIFIC (dev);
1274 arch_specific->dmtype = NULL;
1276 arch_specific->probe = NULL;
1277 arch_specific->topology = NULL;
1280 dev->open_count = 0;
1282 dev->external_mode = 0;
1284 dev->boot_dirty = 0;
1286 if (!_device_probe_type (dev))
1287 goto error_free_arch_specific;
1289 switch (dev->type) {
1290 case PED_DEVICE_IDE:
1291 if (!init_ide (dev))
1292 goto error_free_arch_specific;
1295 case PED_DEVICE_SCSI:
1296 if (!init_scsi (dev))
1297 goto error_free_arch_specific;
1300 case PED_DEVICE_DAC960:
1301 if (!init_generic (dev, _("DAC960 RAID controller")))
1302 goto error_free_arch_specific;
1305 case PED_DEVICE_SX8:
1306 if (!init_generic (dev, _("Promise SX8 SATA Device")))
1307 goto error_free_arch_specific;
1310 #if defined __s390__ || defined __s390x__
1311 case PED_DEVICE_DASD:
1312 if (!init_dasd (dev, _("IBM S390 DASD drive")))
1313 goto error_free_arch_specific;
1317 case PED_DEVICE_VIODASD:
1318 if (!init_generic (dev, _("IBM iSeries Virtual DASD")))
1319 goto error_free_arch_specific;
1322 case PED_DEVICE_CPQARRAY:
1323 if (!init_generic (dev, _("Compaq Smart Array")))
1324 goto error_free_arch_specific;
1327 case PED_DEVICE_ATARAID:
1328 if (!init_generic (dev, _("ATARAID Controller")))
1329 goto error_free_arch_specific;
1332 case PED_DEVICE_I2O:
1333 if (!init_generic (dev, _("I2O Controller")))
1334 goto error_free_arch_specific;
1337 case PED_DEVICE_UBD:
1338 if (!init_generic (dev, _("User-Mode Linux UBD")))
1339 goto error_free_arch_specific;
1342 case PED_DEVICE_FILE:
1343 if (!init_file (dev))
1344 goto error_free_arch_specific;
1350 if (arch_specific->dmtype == NULL
1351 || asprintf(&type, _("Linux device-mapper (%s)"),
1352 arch_specific->dmtype) == -1)
1353 goto error_free_arch_specific;
1354 bool ok = init_generic (dev, type);
1357 goto error_free_arch_specific;
1361 case PED_DEVICE_XVD:
1362 if (!init_generic (dev, _("Xen Virtual Block Device")))
1363 goto error_free_arch_specific;
1366 case PED_DEVICE_UNKNOWN:
1367 if (!init_generic (dev, _("Unknown")))
1368 goto error_free_arch_specific;
1371 case PED_DEVICE_SDMMC:
1372 if (!init_sdmmc (dev))
1373 goto error_free_arch_specific;
1375 case PED_DEVICE_VIRTBLK:
1376 if (!init_generic(dev, _("Virtio Block Device")))
1377 goto error_free_arch_specific;
1381 ped_exception_throw (PED_EXCEPTION_NO_FEATURE,
1382 PED_EXCEPTION_CANCEL,
1383 _("ped_device_new() Unsupported device type"));
1384 goto error_free_arch_specific;
1388 error_free_arch_specific:
1389 free (dev->arch_specific);
1399 linux_destroy (PedDevice* dev)
1401 LinuxSpecific *arch_specific = LINUX_SPECIFIC(dev);
1402 void *p = arch_specific->dmtype;
1405 if (arch_specific->probe)
1406 blkid_free_probe(arch_specific->probe);
1409 free (dev->arch_specific);
1416 linux_is_busy (PedDevice* dev)
1421 if (_partition_is_mounted_by_path (dev->path))
1424 for (i = 0; i < 32; i++) {
1427 part_name = _device_get_part_path (dev, i);
1430 status = _partition_is_mounted_by_path (part_name);
1440 /* we need to flush the master device, and with kernel < 2.6 all the partition
1441 * devices, because there is no coherency between the caches with old kernels.
1442 * We should only flush unmounted partition devices, because:
1443 * - there is never a need to flush them (we're not doing IO there)
1444 * - flushing a device that is mounted causes unnecessary IO, and can
1445 * even screw journaling & friends up. Even cause oopsen!
1448 _flush_cache (PedDevice* dev)
1450 LinuxSpecific* arch_specific = LINUX_SPECIFIC (dev);
1457 ioctl (arch_specific->fd, BLKFLSBUF);
1459 /* With linux-2.6.0 and newer, we're done. */
1463 for (i = 1; i < 16; i++) {
1467 name = _device_get_part_path (dev, i);
1470 if (!_partition_is_mounted_by_path (name)) {
1471 fd = open (name, WR_MODE, 0);
1473 ioctl (fd, BLKFLSBUF);
1475 if (fsync (fd) < 0 || close (fd) < 0)
1476 if (ped_exception_throw (
1477 PED_EXCEPTION_WARNING,
1478 PED_EXCEPTION_RETRY +
1479 PED_EXCEPTION_IGNORE,
1480 _("Error fsyncing/closing %s: %s"),
1481 name, strerror (errno))
1482 == PED_EXCEPTION_RETRY)
1491 linux_open (PedDevice* dev)
1493 LinuxSpecific* arch_specific = LINUX_SPECIFIC (dev);
1496 arch_specific->fd = open (dev->path, RW_MODE);
1498 if (arch_specific->fd == -1) {
1499 char* rw_error_msg = strerror (errno);
1501 arch_specific->fd = open (dev->path, RD_MODE);
1503 if (arch_specific->fd == -1) {
1504 if (ped_exception_throw (
1505 PED_EXCEPTION_ERROR,
1506 PED_EXCEPTION_RETRY_CANCEL,
1507 _("Error opening %s: %s"),
1508 dev->path, strerror (errno))
1509 != PED_EXCEPTION_RETRY) {
1515 ped_exception_throw (
1516 PED_EXCEPTION_WARNING,
1518 _("Unable to open %s read-write (%s). %s has "
1519 "been opened read-only."),
1520 dev->path, rw_error_msg, dev->path);
1527 /* With kernels < 2.6 flush cache for cache coherence issues */
1528 if (!_have_kern26())
1535 linux_refresh_open (PedDevice* dev)
1541 linux_close (PedDevice* dev)
1543 LinuxSpecific* arch_specific = LINUX_SPECIFIC (dev);
1548 if (fsync (arch_specific->fd) < 0 || close (arch_specific->fd) < 0)
1549 if (ped_exception_throw (
1550 PED_EXCEPTION_WARNING,
1551 PED_EXCEPTION_RETRY + PED_EXCEPTION_IGNORE,
1552 _("Error fsyncing/closing %s: %s"),
1553 dev->path, strerror (errno))
1554 == PED_EXCEPTION_RETRY)
1560 linux_refresh_close (PedDevice* dev)
1567 #if SIZEOF_OFF_T < 8
1569 static _syscall5(int,_llseek,
1571 unsigned long, offset_high,
1572 unsigned long, offset_low,
1574 unsigned int, origin)
1577 llseek (unsigned int fd, loff_t offset, unsigned int whence)
1582 retval = _llseek(fd,
1583 ((unsigned long long)offset) >> 32,
1584 ((unsigned long long)offset) & 0xffffffff,
1587 return (retval==-1 ? (loff_t) retval : result);
1590 #endif /* SIZEOF_OFF_T < 8 */
1593 _device_seek (const PedDevice* dev, PedSector sector)
1595 LinuxSpecific* arch_specific;
1597 PED_ASSERT (dev->sector_size % PED_SECTOR_SIZE_DEFAULT == 0, return 0);
1598 PED_ASSERT (dev != NULL, return 0);
1599 PED_ASSERT (!dev->external_mode, return 0);
1601 arch_specific = LINUX_SPECIFIC (dev);
1603 #if SIZEOF_OFF_T < 8
1604 if (sizeof (off_t) < 8) {
1605 loff_t pos = (loff_t)(sector * dev->sector_size);
1606 return llseek (arch_specific->fd, pos, SEEK_SET) == pos;
1610 off_t pos = sector * dev->sector_size;
1611 return lseek (arch_specific->fd, pos, SEEK_SET) == pos;
1616 _read_lastoddsector (const PedDevice* dev, void* buffer)
1618 LinuxSpecific* arch_specific;
1619 struct blkdev_ioctl_param ioctl_param;
1621 PED_ASSERT(dev != NULL, return 0);
1622 PED_ASSERT(buffer != NULL, return 0);
1624 arch_specific = LINUX_SPECIFIC (dev);
1627 ioctl_param.block = 0; /* read the last sector */
1628 ioctl_param.content_length = dev->sector_size;
1629 ioctl_param.block_contents = buffer;
1631 if (ioctl(arch_specific->fd, BLKGETLASTSECT, &ioctl_param) == -1) {
1632 PedExceptionOption opt;
1633 opt = ped_exception_throw (
1634 PED_EXCEPTION_ERROR,
1635 PED_EXCEPTION_RETRY_IGNORE_CANCEL,
1636 _("%s during read on %s"),
1637 strerror (errno), dev->path);
1639 if (opt == PED_EXCEPTION_CANCEL)
1641 if (opt == PED_EXCEPTION_RETRY)
1649 linux_read (const PedDevice* dev, void* buffer, PedSector start,
1652 LinuxSpecific* arch_specific = LINUX_SPECIFIC (dev);
1653 PedExceptionOption ex_status;
1654 void* diobuf = NULL;
1656 PED_ASSERT (dev != NULL, return 0);
1657 PED_ASSERT (dev->sector_size % PED_SECTOR_SIZE_DEFAULT == 0, return 0);
1659 if (_get_linux_version() < KERNEL_VERSION (2,6,0)) {
1660 /* Kludge. This is necessary to read/write the last
1661 block of an odd-sized disk, until Linux 2.5.x kernel fixes.
1663 if (dev->type != PED_DEVICE_FILE && (dev->length & 1)
1664 && start + count - 1 == dev->length - 1)
1665 return ped_device_read (dev, buffer, start, count - 1)
1666 && _read_lastoddsector (
1667 dev, (char *) buffer + (count-1) * 512);
1670 if (_device_seek (dev, start))
1673 ex_status = ped_exception_throw (
1674 PED_EXCEPTION_ERROR,
1675 PED_EXCEPTION_RETRY_IGNORE_CANCEL,
1676 _("%s during seek for read on %s"),
1677 strerror (errno), dev->path);
1679 switch (ex_status) {
1680 case PED_EXCEPTION_IGNORE:
1683 case PED_EXCEPTION_RETRY:
1686 case PED_EXCEPTION_UNHANDLED:
1687 ped_exception_catch ();
1688 case PED_EXCEPTION_CANCEL:
1691 PED_ASSERT (0, (void) 0);
1696 size_t read_length = count * dev->sector_size;
1697 if (posix_memalign (&diobuf, dev->sector_size, read_length) != 0)
1701 ssize_t status = read (arch_specific->fd, diobuf, read_length);
1703 memcpy(buffer, diobuf, status);
1704 if (status == (ssize_t) read_length)
1707 read_length -= status;
1708 buffer = (char *) buffer + status;
1712 ex_status = ped_exception_throw (
1713 PED_EXCEPTION_ERROR,
1714 PED_EXCEPTION_RETRY_IGNORE_CANCEL,
1716 ? _("end of file while reading %s")
1717 : _("%s during read on %s")),
1721 switch (ex_status) {
1722 case PED_EXCEPTION_IGNORE:
1726 case PED_EXCEPTION_RETRY:
1729 case PED_EXCEPTION_UNHANDLED:
1730 ped_exception_catch ();
1731 case PED_EXCEPTION_CANCEL:
1735 PED_ASSERT (0, (void) 0);
1746 _write_lastoddsector (PedDevice* dev, const void* buffer)
1748 LinuxSpecific* arch_specific;
1749 struct blkdev_ioctl_param ioctl_param;
1751 PED_ASSERT(dev != NULL, return 0);
1752 PED_ASSERT(buffer != NULL, return 0);
1754 arch_specific = LINUX_SPECIFIC (dev);
1757 ioctl_param.block = 0; /* write the last sector */
1758 ioctl_param.content_length = dev->sector_size;
1759 ioctl_param.block_contents = (void*) buffer;
1761 if (ioctl(arch_specific->fd, BLKSETLASTSECT, &ioctl_param) == -1) {
1762 PedExceptionOption opt;
1763 opt = ped_exception_throw (
1764 PED_EXCEPTION_ERROR,
1765 PED_EXCEPTION_RETRY_IGNORE_CANCEL,
1766 _("%s during write on %s"),
1767 strerror (errno), dev->path);
1769 if (opt == PED_EXCEPTION_CANCEL)
1771 if (opt == PED_EXCEPTION_RETRY)
1779 linux_write (PedDevice* dev, const void* buffer, PedSector start,
1782 LinuxSpecific* arch_specific = LINUX_SPECIFIC (dev);
1783 PedExceptionOption ex_status;
1787 PED_ASSERT(dev->sector_size % PED_SECTOR_SIZE_DEFAULT == 0, return 0);
1789 if (dev->read_only) {
1790 if (ped_exception_throw (
1791 PED_EXCEPTION_ERROR,
1792 PED_EXCEPTION_IGNORE_CANCEL,
1793 _("Can't write to %s, because it is opened read-only."),
1795 != PED_EXCEPTION_IGNORE)
1801 if (_get_linux_version() < KERNEL_VERSION (2,6,0)) {
1802 /* Kludge. This is necessary to read/write the last
1803 block of an odd-sized disk, until Linux 2.5.x kernel fixes.
1805 if (dev->type != PED_DEVICE_FILE && (dev->length & 1)
1806 && start + count - 1 == dev->length - 1)
1807 return ped_device_write (dev, buffer, start, count - 1)
1808 && _write_lastoddsector (
1809 dev, ((char*) buffer
1810 + (count-1) * dev->sector_size));
1813 if (_device_seek (dev, start))
1816 ex_status = ped_exception_throw (
1817 PED_EXCEPTION_ERROR, PED_EXCEPTION_RETRY_IGNORE_CANCEL,
1818 _("%s during seek for write on %s"),
1819 strerror (errno), dev->path);
1821 switch (ex_status) {
1822 case PED_EXCEPTION_IGNORE:
1825 case PED_EXCEPTION_RETRY:
1828 case PED_EXCEPTION_UNHANDLED:
1829 ped_exception_catch ();
1830 case PED_EXCEPTION_CANCEL:
1833 PED_ASSERT (0, (void) 0);
1839 printf ("ped_device_write (\"%s\", %p, %d, %d)\n",
1840 dev->path, buffer, (int) start, (int) count);
1842 size_t write_length = count * dev->sector_size;
1844 if (posix_memalign(&diobuf, dev->sector_size, write_length) != 0)
1846 memcpy(diobuf, buffer, write_length);
1847 diobuf_start = diobuf;
1849 ssize_t status = write (arch_specific->fd, diobuf, write_length);
1850 if (status == write_length) break;
1852 write_length -= status;
1853 diobuf = (char *) diobuf + status;
1857 ex_status = ped_exception_throw (
1858 PED_EXCEPTION_ERROR,
1859 PED_EXCEPTION_RETRY_IGNORE_CANCEL,
1860 _("%s during write on %s"),
1861 strerror (errno), dev->path);
1863 switch (ex_status) {
1864 case PED_EXCEPTION_IGNORE:
1868 case PED_EXCEPTION_RETRY:
1871 case PED_EXCEPTION_UNHANDLED:
1872 ped_exception_catch ();
1873 case PED_EXCEPTION_CANCEL:
1877 PED_ASSERT (0, (void) 0);
1882 #endif /* !READ_ONLY */
1886 /* returns the number of sectors that are ok.
1889 linux_check (PedDevice* dev, void* buffer, PedSector start, PedSector count)
1891 LinuxSpecific* arch_specific = LINUX_SPECIFIC (dev);
1896 PED_ASSERT(dev != NULL, return 0);
1898 if (!_device_seek (dev, start))
1901 if (posix_memalign(&diobuf, PED_SECTOR_SIZE_DEFAULT,
1902 count * PED_SECTOR_SIZE_DEFAULT) != 0)
1905 for (done = 0; done < count; done += status / dev->sector_size) {
1906 status = read (arch_specific->fd, diobuf,
1907 (size_t) ((count-done) * dev->sector_size));
1909 memcpy(buffer, diobuf, status);
1919 _do_fsync (PedDevice* dev)
1921 LinuxSpecific* arch_specific = LINUX_SPECIFIC (dev);
1923 PedExceptionOption ex_status;
1926 status = fsync (arch_specific->fd);
1927 if (status >= 0) break;
1929 ex_status = ped_exception_throw (
1930 PED_EXCEPTION_ERROR,
1931 PED_EXCEPTION_RETRY_IGNORE_CANCEL,
1932 _("%s during write on %s"),
1933 strerror (errno), dev->path);
1935 switch (ex_status) {
1936 case PED_EXCEPTION_IGNORE:
1939 case PED_EXCEPTION_RETRY:
1942 case PED_EXCEPTION_UNHANDLED:
1943 ped_exception_catch ();
1944 case PED_EXCEPTION_CANCEL:
1947 PED_ASSERT (0, (void) 0);
1955 linux_sync (PedDevice* dev)
1957 PED_ASSERT (dev != NULL, return 0);
1958 PED_ASSERT (!dev->external_mode, return 0);
1962 if (!_do_fsync (dev))
1969 linux_sync_fast (PedDevice* dev)
1971 PED_ASSERT (dev != NULL, return 0);
1972 PED_ASSERT (!dev->external_mode, return 0);
1976 if (!_do_fsync (dev))
1978 /* no cache flush... */
1983 _compare_digit_state (char ch, int need_digit)
1985 return !!isdigit (ch) == need_digit;
1988 /* matches the regexp "[^0-9]+[0-9]+[^0-9]+[0-9]+$".
1989 * Motivation: accept devices looking like /dev/rd/c0d0, but
1990 * not looking like /dev/hda1 and /dev/rd/c0d0p1
1993 _match_rd_device (const char* name)
1998 /* exclude directory names from test */
1999 pos = strrchr(name, '/') ?: name;
2007 for (state = 0; state < 4; state++) {
2008 int want_digits = (state % 2 == 1);
2012 if (!_compare_digit_state (*pos, want_digits))
2015 } while (_compare_digit_state (*pos, want_digits));
2022 _probe_proc_partitions ()
2024 FILE* proc_part_file;
2025 int major, minor, size;
2027 char part_name [256];
2028 char dev_name [256];
2030 proc_part_file = fopen ("/proc/partitions", "r");
2031 if (!proc_part_file)
2034 if (fgets (buf, 256, proc_part_file) == NULL)
2037 if (fgets (buf, 256, proc_part_file) == NULL)
2040 while (fgets (buf, 512, proc_part_file)
2041 && sscanf (buf, "%d %d %d %255s", &major, &minor, &size,
2043 /* Heuristic for telling partitions and devices apart
2044 * Probably needs to be improved
2046 if (!_match_rd_device (part_name)
2047 && isdigit (part_name [strlen (part_name) - 1]))
2050 strcpy (dev_name, "/dev/");
2051 strcat (dev_name, part_name);
2052 _ped_device_probe (dev_name);
2055 fclose (proc_part_file);
2065 _skip_entry (const char *name)
2068 static struct _entry entries[] = {
2069 { ".", sizeof (".") - 1 },
2070 { "..", sizeof ("..") - 1 },
2071 { "dm-", sizeof ("dm-") - 1 },
2072 { "loop", sizeof ("loop") - 1 },
2073 { "ram", sizeof ("ram") - 1 },
2077 for (i = entries; i->name != 0; i++) {
2078 if (strncmp (name, i->name, i->len) == 0)
2089 struct dirent *dirent;
2090 char dev_name [256];
2093 if (!(blockdir = opendir ("/sys/block")))
2095 while ((dirent = readdir (blockdir))) {
2096 if (_skip_entry (dirent->d_name))
2099 if (strlen (dirent->d_name) > sizeof (dev_name) - 6)
2100 continue; /* device name too long! */
2102 strcpy (dev_name, "/dev/");
2103 strcat (dev_name, dirent->d_name);
2104 /* in /sys/block, '/'s are replaced with '!' or '.' */
2105 for (ptr = dev_name; *ptr != '\0'; ptr++) {
2106 if (*ptr == '!' || *ptr == '.')
2109 _ped_device_probe (dev_name);
2112 closedir (blockdir);
2117 _probe_standard_devices ()
2119 _ped_device_probe ("/dev/hda");
2120 _ped_device_probe ("/dev/hdb");
2121 _ped_device_probe ("/dev/hdc");
2122 _ped_device_probe ("/dev/hdd");
2123 _ped_device_probe ("/dev/hde");
2124 _ped_device_probe ("/dev/hdf");
2125 _ped_device_probe ("/dev/hdg");
2126 _ped_device_probe ("/dev/hdh");
2128 _ped_device_probe ("/dev/sda");
2129 _ped_device_probe ("/dev/sdb");
2130 _ped_device_probe ("/dev/sdc");
2131 _ped_device_probe ("/dev/sdd");
2132 _ped_device_probe ("/dev/sde");
2133 _ped_device_probe ("/dev/sdf");
2141 /* we should probe the standard devs too, even with /proc/partitions,
2142 * because /proc/partitions might return devfs stuff, and we might not
2143 * have devfs available
2145 _probe_standard_devices ();
2147 #ifdef ENABLE_DEVICE_MAPPER
2148 /* device-mapper devices aren't listed in /proc/partitions; or, if
2149 * they are, they're listed as dm-X. So, instead of relying on that,
2150 * we do our own checks.
2152 _probe_dm_devices ();
2155 /* /sys/block is more reliable and consistent; fall back to using
2156 * /proc/partitions if the former is unavailable, however.
2158 if (!_probe_sys_block ())
2159 _probe_proc_partitions ();
2163 _device_get_part_path (PedDevice* dev, int num)
2165 int path_len = strlen (dev->path);
2166 int result_len = path_len + 16;
2169 result = (char*) ped_malloc (result_len);
2173 /* Check for devfs-style /disc => /partN transformation
2174 unconditionally; the system might be using udev with devfs rules,
2175 and if not the test is harmless. */
2176 if (!strcmp (dev->path + path_len - 5, "/disc")) {
2177 /* replace /disc with /path%d */
2178 strcpy (result, dev->path);
2179 snprintf (result + path_len - 5, 16, "/part%d", num);
2180 } else if (dev->type == PED_DEVICE_DAC960
2181 || dev->type == PED_DEVICE_CPQARRAY
2182 || dev->type == PED_DEVICE_ATARAID
2183 || dev->type == PED_DEVICE_DM
2184 || isdigit (dev->path[path_len - 1]))
2185 snprintf (result, result_len, "%sp%d", dev->path, num);
2187 snprintf (result, result_len, "%s%d", dev->path, num);
2193 linux_partition_get_path (const PedPartition* part)
2195 return _device_get_part_path (part->disk->dev, part->num);
2199 _partition_get_part_dev (const PedPartition* part)
2201 struct stat dev_stat;
2202 int dev_major, dev_minor;
2204 if (stat (part->disk->dev->path, &dev_stat))
2206 dev_major = major (dev_stat.st_rdev);
2207 dev_minor = minor (dev_stat.st_rdev);
2208 return (dev_t)makedev (dev_major, dev_minor + part->num);
2212 _mount_table_search (const char* file_name, dev_t dev)
2214 struct stat part_stat;
2216 char part_name[512];
2219 file = fopen (file_name, "r");
2222 while (fgets (line, 512, file)) {
2223 if (sscanf (line, "%s", part_name) == 1
2224 && stat (part_name, &part_stat) == 0) {
2225 if (part_stat.st_rdev == dev) {
2236 _partition_is_mounted_by_dev (dev_t dev)
2238 return _mount_table_search( "/proc/mounts", dev)
2239 || _mount_table_search( "/proc/swaps", dev)
2240 || _mount_table_search( "/etc/mtab", dev);
2244 _partition_is_mounted_by_path (const char *path)
2246 struct stat part_stat;
2247 if (stat (path, &part_stat) != 0)
2249 if (!S_ISBLK(part_stat.st_mode))
2251 return _partition_is_mounted_by_dev (part_stat.st_rdev);
2255 _partition_is_mounted (const PedPartition *part)
2258 if (!ped_partition_is_active (part))
2260 dev = _partition_get_part_dev (part);
2263 return _partition_is_mounted_by_dev (dev);
2267 _has_partitions (const PedDisk* disk)
2269 PED_ASSERT(disk != NULL, return 0);
2271 /* Some devices can't be partitioned. */
2272 if (!strcmp (disk->type->name, "loop"))
2279 linux_partition_is_busy (const PedPartition* part)
2283 PED_ASSERT (part != NULL, return 0);
2285 if (_partition_is_mounted (part))
2287 if (part->type == PED_PARTITION_EXTENDED) {
2288 for (walk = part->part_list; walk; walk = walk->next) {
2289 if (linux_partition_is_busy (walk))
2296 #ifdef ENABLE_DEVICE_MAPPER
2298 _dm_remove_map_name(char *name)
2300 struct dm_task *task = NULL;
2303 task = dm_task_create(DM_DEVICE_REMOVE);
2307 dm_task_set_name (task, name);
2309 rc = dm_task_run(task);
2310 dm_task_update_nodes();
2311 dm_task_destroy(task);
2319 _dm_is_part (struct dm_info *this, char *name)
2321 struct dm_task* task = NULL;
2322 struct dm_info* info = alloca(sizeof *info);
2323 struct dm_deps* deps = NULL;
2327 task = dm_task_create(DM_DEVICE_DEPS);
2331 dm_task_set_name(task, name);
2332 rc = dm_task_run(task);
2339 memset(info, '\0', sizeof *info);
2340 dm_task_get_info(task, info);
2344 deps = dm_task_get_deps(task);
2349 for (i = 0; i < deps->count; i++) {
2350 unsigned int ma = major(deps->device[i]),
2351 mi = minor(deps->device[i]);
2353 if (ma == this->major && mi == this->minor)
2358 dm_task_destroy(task);
2363 _dm_remove_parts (PedDevice* dev)
2365 struct dm_task* task = NULL;
2366 struct dm_info* info = alloca(sizeof *info);
2367 struct dm_names* names = NULL;
2368 unsigned int next = 0;
2370 LinuxSpecific* arch_specific = LINUX_SPECIFIC (dev);
2372 task = dm_task_create(DM_DEVICE_LIST);
2376 if (!dm_task_set_major_minor (task, arch_specific->major,
2377 arch_specific->minor, 0))
2380 rc = dm_task_run(task);
2384 memset(info, '\0', sizeof *info);
2385 dm_task_get_info(task, info);
2389 names = dm_task_get_names(task);
2395 names = (void *) ((char *) names + next);
2397 if (_dm_is_part(info, names->name))
2398 rc += _dm_remove_map_name(names->name);
2403 dm_task_update_nodes();
2404 dm_task_destroy(task);
2411 dm_task_destroy(task);
2412 ped_exception_throw (PED_EXCEPTION_WARNING, PED_EXCEPTION_IGNORE,
2413 _("parted was unable to re-read the partition "
2414 "table on %s (%s). This means Linux won't know "
2415 "anything about the modifications you made. "),
2416 dev->path, strerror (errno));
2421 _dm_add_partition (PedDisk* disk, PedPartition* part)
2423 struct dm_task* task = NULL;
2425 char* vol_name = NULL;
2426 const char* dev_name = NULL;
2427 char* params = NULL;
2428 LinuxSpecific* arch_specific = LINUX_SPECIFIC (disk->dev);
2430 if (!_has_partitions(disk))
2433 /* Get map name from devicemapper */
2434 task = dm_task_create (DM_DEVICE_INFO);
2438 if (!dm_task_set_major_minor (task, arch_specific->major,
2439 arch_specific->minor, 0))
2442 rc = dm_task_run(task);
2446 dev_name = dm_task_get_name (task);
2447 dm_task_destroy (task);
2450 if (asprintf (&vol_name, "%sp%d", dev_name, part->num) == -1)
2453 if (asprintf (¶ms, "%d:%d %lld", arch_specific->major,
2454 arch_specific->minor, part->geom.start) == -1)
2457 task = dm_task_create (DM_DEVICE_CREATE);
2461 dm_task_set_name (task, vol_name);
2462 dm_task_add_target (task, 0, part->geom.length,
2464 rc = dm_task_run (task);
2466 //printf("0 %ld linear %s\n", part->geom.length, params);
2467 dm_task_update_nodes();
2468 dm_task_destroy(task);
2473 _dm_remove_map_name(vol_name);
2476 dm_task_update_nodes();
2478 dm_task_destroy (task);
2485 _dm_reread_part_table (PedDisk* disk)
2487 int largest_partnum = ped_disk_get_last_partition_num (disk);
2488 if (largest_partnum <= 0)
2492 int last = PED_MIN (largest_partnum, 16);
2496 if (!_dm_remove_parts(disk->dev))
2499 for (i = 1; i <= last; i++) {
2502 part = ped_disk_get_partition (disk, i);
2506 if (!_dm_add_partition (disk, part))
2514 _kernel_reread_part_table (PedDevice* dev)
2516 LinuxSpecific* arch_specific = LINUX_SPECIFIC (dev);
2517 int retry_count = 5;
2520 while (ioctl (arch_specific->fd, BLKRRPART)) {
2524 ped_exception_throw (
2525 PED_EXCEPTION_WARNING,
2526 PED_EXCEPTION_IGNORE,
2527 _("WARNING: the kernel failed to re-read the partition "
2528 "table on %s (%s). As a result, it may not "
2529 "reflect all of your changes until after reboot."),
2530 dev->path, strerror (errno));
2539 linux_disk_commit (PedDisk* disk)
2541 if (!_has_partitions (disk))
2544 #ifdef ENABLE_DEVICE_MAPPER
2545 if (disk->dev->type == PED_DEVICE_DM)
2546 return _dm_reread_part_table (disk);
2548 if (disk->dev->type != PED_DEVICE_FILE) {
2549 return _kernel_reread_part_table (disk->dev);
2557 linux_get_minimum_alignment(const PedDevice *dev)
2559 blkid_topology tp = LINUX_SPECIFIC(dev)->topology;
2561 if (!tp || blkid_topology_get_minimum_io_size(tp) == 0)
2562 return NULL; /* ped_alignment_none */
2564 return ped_alignment_new(
2565 blkid_topology_get_alignment_offset(tp) / dev->sector_size,
2566 blkid_topology_get_minimum_io_size(tp) / dev->sector_size);
2570 linux_get_optimum_alignment(const PedDevice *dev)
2572 blkid_topology tp = LINUX_SPECIFIC(dev)->topology;
2574 if (!tp || blkid_topology_get_optimal_io_size(tp) == 0)
2575 return NULL; /* ped_alignment_none */
2577 return ped_alignment_new(
2578 blkid_topology_get_alignment_offset(tp) / dev->sector_size,
2579 blkid_topology_get_optimal_io_size(tp) / dev->sector_size);
2583 static PedDeviceArchOps linux_dev_ops = {
2585 destroy: linux_destroy,
2586 is_busy: linux_is_busy,
2588 refresh_open: linux_refresh_open,
2590 refresh_close: linux_refresh_close,
2595 sync_fast: linux_sync_fast,
2596 probe_all: linux_probe_all,
2598 get_minimum_alignment: linux_get_minimum_alignment,
2599 get_optimum_alignment: linux_get_optimum_alignment,
2603 PedDiskArchOps linux_disk_ops = {
2604 partition_get_path: linux_partition_get_path,
2605 partition_is_busy: linux_partition_is_busy,
2606 disk_commit: linux_disk_commit
2609 PedArchitecture ped_linux_arch = {
2610 dev_ops: &linux_dev_ops,
2611 disk_ops: &linux_disk_ops