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
22 #include <parted/parted.h>
23 #include <parted/debug.h>
34 #include <sys/ioctl.h>
36 #include <sys/types.h>
37 #include <sys/utsname.h> /* for uname() */
38 #include <scsi/scsi.h>
39 #ifdef ENABLE_DEVICE_MAPPER
40 #include <libdevmapper.h>
44 #include "../architecture.h"
48 # define _(String) dgettext (PACKAGE, String)
50 # define _(String) (String)
51 #endif /* ENABLE_NLS */
53 #define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
56 #define __NR__llseek 140
59 #ifndef SCSI_IOCTL_SEND_COMMAND
60 #define SCSI_IOCTL_SEND_COMMAND 1
63 /* from <linux/hdreg.h> */
64 #define HDIO_GETGEO 0x0301 /* get device geometry */
65 #define HDIO_GET_IDENTITY 0x030d /* get IDE identification info */
67 #if defined(O_DIRECT) && !(defined(__s390__) || defined(__s390x__))
68 #define RD_MODE (O_RDONLY | O_DIRECT)
69 #define WR_MODE (O_WRONLY | O_DIRECT)
70 #define RW_MODE (O_RDWR | O_DIRECT)
72 #define RD_MODE (O_RDONLY)
73 #define WR_MODE (O_WRONLY)
74 #define RW_MODE (O_RDWR)
77 #if defined(__s390__) || defined(__s390x__)
78 # include <parted/fdasd.h>
81 #define LINUX_SPECIFIC(dev) ((LinuxSpecific*) (dev)->arch_specific)
83 typedef struct _LinuxSpecific LinuxSpecific;
85 struct _LinuxSpecific {
87 char* dmtype; /**< device map target type */
88 #if defined(__s390__) || defined(__s390x__)
89 unsigned int real_sector_size;
90 /* IBM internal dasd structure (i guess ;), required. */
91 struct fdasd_anchor *anchor;
97 unsigned char sectors;
98 unsigned short cylinders;
102 struct ata7_sectinfo {
109 /* structure returned by HDIO_GET_IDENTITY, as per ANSI ATA2 rev.2f spec */
111 unsigned short config; /* lots of obsolete bit flags */
112 unsigned short cyls; /* "physical" cyls */
113 unsigned short reserved2; /* reserved (word 2) */
114 unsigned short heads; /* "physical" heads */
115 unsigned short track_bytes; /* unformatted bytes per track */
116 unsigned short sector_bytes; /* unformatted bytes per sector */
117 unsigned short sectors; /* "physical" sectors per track */
118 unsigned short vendor0; /* vendor unique */
119 unsigned short vendor1; /* vendor unique */
120 unsigned short vendor2; /* vendor unique */
121 unsigned char serial_no[20]; /* 0 = not_specified */
122 unsigned short buf_type;
123 unsigned short buf_size; /* 512 byte increments;
125 unsigned short ecc_bytes; /* for r/w long cmds;
127 unsigned char fw_rev[8]; /* 0 = not_specified */
128 char model[40]; /* 0 = not_specified */
129 unsigned char max_multsect; /* 0=not_implemented */
130 unsigned char vendor3; /* vendor unique */
131 unsigned short dword_io; /* 0=not_implemented; 1=implemented */
132 unsigned char vendor4; /* vendor unique */
133 unsigned char capability; /* bits 0:DMA 1:LBA 2:IORDYsw
135 unsigned short reserved50; /* reserved (word 50) */
136 unsigned char vendor5; /* vendor unique */
137 unsigned char tPIO; /* 0=slow, 1=medium, 2=fast */
138 unsigned char vendor6; /* vendor unique */
139 unsigned char tDMA; /* 0=slow, 1=medium, 2=fast */
140 unsigned short field_valid; /* bits 0:cur_ok 1:eide_ok */
141 unsigned short cur_cyls; /* logical cylinders */
142 unsigned short cur_heads; /* logical heads */
143 unsigned short cur_sectors; /* logical sectors per track */
144 unsigned short cur_capacity0; /* logical total sectors on drive */
145 unsigned short cur_capacity1; /* (2 words, misaligned int) */
146 unsigned char multsect; /* current multiple sector count */
147 unsigned char multsect_valid; /* when (bit0==1) multsect is ok */
148 unsigned int lba_capacity; /* total number of sectors */
149 unsigned short dma_1word; /* single-word dma info */
150 unsigned short dma_mword; /* multiple-word dma info */
151 unsigned short eide_pio_modes; /* bits 0:mode3 1:mode4 */
152 unsigned short eide_dma_min; /* min mword dma cycle time (ns) */
153 unsigned short eide_dma_time; /* recommended mword dma cycle
155 unsigned short eide_pio; /* min cycle time (ns), no IORDY */
156 unsigned short eide_pio_iordy; /* min cycle time (ns), with IORDY */
157 unsigned short words69_70[2]; /* reserved words 69-70 */
158 /* HDIO_GET_IDENTITY currently returns only words 0 through 70 */
159 unsigned short words71_74[4]; /* reserved words 71-74 */
160 unsigned short queue_depth; /* */
161 unsigned short words76_79[4]; /* reserved words 76-79 */
162 unsigned short major_rev_num; /* */
163 unsigned short minor_rev_num; /* */
164 unsigned short command_set_1; /* bits 0:Smart 1:Security 2:Removable
166 unsigned short command_set_2; /* bits 14:Smart Enabled 13:0 zero */
167 unsigned short cfsse; /* command set-feature supported
169 unsigned short cfs_enable_1; /* command set-feature enabled */
170 unsigned short cfs_enable_2; /* command set-feature enabled */
171 unsigned short csf_default; /* command set-feature default */
172 unsigned short dma_ultra; /* */
173 unsigned short word89; /* reserved (word 89) */
174 unsigned short word90; /* reserved (word 90) */
175 unsigned short CurAPMvalues; /* current APM values */
176 unsigned short word92; /* reserved (word 92) */
177 unsigned short hw_config; /* hardware config */
178 unsigned short words94_105[12];/* reserved words 94-105 */
179 struct ata7_sectinfo ata7_sectinfo; /* ATAPI/ATA7 physical and logical
181 unsigned short words107_116[10];/* reserved words 107-116 */
182 unsigned int logical_sectsize;/* ATAPI/ATA7 logical sector size */
183 unsigned short words119_125[7];/* reserved words 119-125 */
184 unsigned short last_lun; /* reserved (word 126) */
185 unsigned short word127; /* reserved (word 127) */
186 unsigned short dlf; /* device lock function
188 * 8 security level 1:max 0:high
197 unsigned short csfo; /* current set features options
204 unsigned short words130_155[26];/* reserved vendor words 130-155 */
205 unsigned short word156;
206 unsigned short words157_159[3]; /* reserved vendor words 157-159 */
207 unsigned short words160_255[95];/* reserved words 160-255 */
210 /* from <linux/fs.h> */
211 #define BLKRRPART _IO(0x12,95) /* re-read partition table */
212 #define BLKGETSIZE _IO(0x12,96) /* return device size */
213 #define BLKFLSBUF _IO(0x12,97) /* flush buffer cache */
214 #define BLKSSZGET _IO(0x12,104) /* get block device sector size */
215 #define BLKGETLASTSECT _IO(0x12,108) /* get last sector of block device */
216 #define BLKSETLASTSECT _IO(0x12,109) /* set last sector of block device */
218 /* return device size in bytes (u64 *arg) */
219 #define BLKGETSIZE64 _IOR(0x12,114,size_t)
221 struct blkdev_ioctl_param {
223 size_t content_length;
224 char * block_contents;
227 /* from <linux/major.h> */
229 #define IDE1_MAJOR 22
230 #define IDE2_MAJOR 33
231 #define IDE3_MAJOR 34
232 #define IDE4_MAJOR 56
233 #define IDE5_MAJOR 57
234 #define SCSI_CDROM_MAJOR 11
235 #define SCSI_DISK0_MAJOR 8
236 #define SCSI_DISK1_MAJOR 65
237 #define SCSI_DISK2_MAJOR 66
238 #define SCSI_DISK3_MAJOR 67
239 #define SCSI_DISK4_MAJOR 68
240 #define SCSI_DISK5_MAJOR 69
241 #define SCSI_DISK6_MAJOR 70
242 #define SCSI_DISK7_MAJOR 71
243 #define COMPAQ_SMART2_MAJOR 72
244 #define COMPAQ_SMART2_MAJOR1 73
245 #define COMPAQ_SMART2_MAJOR2 74
246 #define COMPAQ_SMART2_MAJOR3 75
247 #define COMPAQ_SMART2_MAJOR4 76
248 #define COMPAQ_SMART2_MAJOR5 77
249 #define COMPAQ_SMART2_MAJOR6 78
250 #define COMPAQ_SMART2_MAJOR7 79
251 #define COMPAQ_SMART_MAJOR 104
252 #define COMPAQ_SMART_MAJOR1 105
253 #define COMPAQ_SMART_MAJOR2 106
254 #define COMPAQ_SMART_MAJOR3 107
255 #define COMPAQ_SMART_MAJOR4 108
256 #define COMPAQ_SMART_MAJOR5 109
257 #define COMPAQ_SMART_MAJOR6 110
258 #define COMPAQ_SMART_MAJOR7 111
259 #define DAC960_MAJOR 48
260 #define ATARAID_MAJOR 114
261 #define I2O_MAJOR1 80
262 #define I2O_MAJOR2 81
263 #define I2O_MAJOR3 82
264 #define I2O_MAJOR4 83
265 #define I2O_MAJOR5 84
266 #define I2O_MAJOR6 85
267 #define I2O_MAJOR7 86
268 #define I2O_MAJOR8 87
270 #define DASD_MAJOR 94
271 #define VIODASD_MAJOR 112
272 #define SX8_MAJOR1 160
273 #define SX8_MAJOR2 161
274 #define XVD_MAJOR 202
275 #define SDMMC_MAJOR 179
277 #define SCSI_BLK_MAJOR(M) ( \
278 (M) == SCSI_DISK0_MAJOR \
279 || (M) == SCSI_CDROM_MAJOR \
280 || ((M) >= SCSI_DISK1_MAJOR && (M) <= SCSI_DISK7_MAJOR))
282 /* Maximum number of partitions supported by linux. */
283 #define MAX_NUM_PARTS 64
285 static char* _device_get_part_path (PedDevice* dev, int num);
286 static int _partition_is_mounted_by_path (const char* path);
289 _is_ide_major (int major)
306 _is_cpqarray_major (int major)
308 return ((COMPAQ_SMART2_MAJOR <= major && major <= COMPAQ_SMART2_MAJOR7)
309 || (COMPAQ_SMART_MAJOR <= major && major <= COMPAQ_SMART_MAJOR7));
313 _is_i2o_major (int major)
315 return (I2O_MAJOR1 <= major && major <= I2O_MAJOR8);
319 _is_sx8_major (int major)
321 return (SX8_MAJOR1 <= major && major <= SX8_MAJOR2);
324 #ifdef ENABLE_DEVICE_MAPPER
326 _dm_maptype (PedDevice *dev)
328 LinuxSpecific* arch_specific = LINUX_SPECIFIC (dev);
331 uint64_t start, length;
332 char *target_type = NULL;
335 const char* dev_dir = getenv ("DM_DEV_DIR");
337 if (dev_dir && *dev_dir && !dm_set_dev_dir(dev_dir))
340 if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
343 if (!dm_task_set_name(dmt, dev->path))
346 dm_task_no_open_count(dmt);
348 if (!dm_task_run(dmt))
351 next = dm_get_next_target(dmt, NULL, &start, &length,
352 &target_type, ¶ms);
354 arch_specific->dmtype = strdup(target_type ? target_type : "NO-TARGET");
355 if (arch_specific->dmtype == NULL)
359 dm_task_destroy(dmt);
364 readFD (int fd, char **buf)
367 size_t size = PROC_DEVICES_BUFSIZ;
370 *buf = malloc (size * sizeof (char));
376 p = &(*buf) [filesize];
377 s = read (fd, p, PROC_DEVICES_BUFSIZ);
378 /* exit if there is an error or EOF is reached */
383 *buf = realloc (*buf, size);
386 if (filesize == 0 && s < 0) {
391 /* there is always some excess memory left unused */
392 *buf = realloc (*buf, filesize+1);
393 (*buf)[filesize] = '\0';
400 _is_dm_major (int major)
409 fd = open ("/proc/devices", O_RDONLY);
413 if (readFD(fd, &buf) < 0) {
419 end = strchr(line, '\n');
428 if (!strncmp(line, "Block devices:", 14))
433 name = strrchr(line, ' ');
434 if (!name || strcmp(name+1, "device-mapper"))
437 maj = strtol(line, &name, 10);
447 end = strchr(line, '\n');
459 char buf [512]; /* readdir(3) claims d_name[256] */
462 mapper_dir = opendir ("/dev/mapper");
466 /* Search the /dev/mapper directory for devices w/ the same major
467 * number that was returned from _probe_lvm_major().
469 while ((dent = readdir (mapper_dir))) {
470 if (strcmp (dent->d_name, ".") == 0 ||
471 strcmp (dent->d_name, "..") == 0)
474 snprintf (buf, sizeof (buf), "/dev/mapper/%s", dent->d_name);
476 if (stat (buf, &st) != 0)
479 if (_is_dm_major(major(st.st_rdev)))
480 _ped_device_probe (buf);
482 closedir (mapper_dir);
489 _device_stat (PedDevice* dev, struct stat * dev_stat)
491 PED_ASSERT (dev != NULL, return 0);
492 PED_ASSERT (!dev->external_mode, return 0);
495 if (!stat (dev->path, dev_stat)) {
498 if (ped_exception_throw (
500 PED_EXCEPTION_RETRY_CANCEL,
501 _("Could not stat device %s - %s."),
504 != PED_EXCEPTION_RETRY)
511 _device_probe_type (PedDevice* dev)
513 struct stat dev_stat;
517 if (!_device_stat (dev, &dev_stat))
520 if (!S_ISBLK(dev_stat.st_mode)) {
521 dev->type = PED_DEVICE_FILE;
525 dev_major = major (dev_stat.st_rdev);
526 dev_minor = minor (dev_stat.st_rdev);
528 if (SCSI_BLK_MAJOR (dev_major) && (dev_minor % 0x10 == 0)) {
529 dev->type = PED_DEVICE_SCSI;
530 } else if (_is_ide_major (dev_major) && (dev_minor % 0x40 == 0)) {
531 dev->type = PED_DEVICE_IDE;
532 } else if (dev_major == DAC960_MAJOR && (dev_minor % 0x8 == 0)) {
533 dev->type = PED_DEVICE_DAC960;
534 } else if (dev_major == ATARAID_MAJOR && (dev_minor % 0x10 == 0)) {
535 dev->type = PED_DEVICE_ATARAID;
536 } else if (dev_major == DASD_MAJOR && (dev_minor % 0x4 == 0)) {
537 dev->type = PED_DEVICE_DASD;
538 } else if (dev_major == VIODASD_MAJOR && (dev_minor % 0x8 == 0)) {
539 dev->type = PED_DEVICE_VIODASD;
540 } else if (_is_sx8_major(dev_major) && (dev_minor % 0x20 == 0)) {
541 dev->type = PED_DEVICE_SX8;
542 } else if (_is_i2o_major (dev_major) && (dev_minor % 0x10 == 0)) {
543 dev->type = PED_DEVICE_I2O;
544 } else if (_is_cpqarray_major (dev_major) && (dev_minor % 0x10 == 0)) {
545 dev->type = PED_DEVICE_CPQARRAY;
546 } else if (dev_major == UBD_MAJOR && (dev_minor % 0x10 == 0)) {
547 dev->type = PED_DEVICE_UBD;
548 #ifdef ENABLE_DEVICE_MAPPER
549 } else if (_is_dm_major(dev_major)) {
550 dev->type = PED_DEVICE_DM;
551 if (_dm_maptype(dev)) {
552 ped_exception_throw (
554 PED_EXCEPTION_CANCEL,
555 _("Unable to determine the dm type of %s."),
559 } else if (dev_major == XVD_MAJOR && (dev_minor % 0x10 == 0)) {
560 dev->type = PED_DEVICE_XVD;
561 } else if (dev_major == SDMMC_MAJOR && (dev_minor % 0x08 == 0)) {
562 dev->type = PED_DEVICE_SDMMC;
564 dev->type = PED_DEVICE_UNKNOWN;
571 _get_linux_version ()
573 static int kver = -1;
585 if (sscanf (uts.release, "%u.%u.%u", &major, &minor, &teeny) != 3)
588 return kver = KERNEL_VERSION (major, minor, teeny);
594 static int have_devfs = -1;
597 if (have_devfs != -1)
600 /* the presence of /dev/.devfsd implies that DevFS is active */
601 if (stat("/dev/.devfsd", &sb) < 0)
602 return have_devfs = 0;
604 return have_devfs = S_ISCHR(sb.st_mode) ? 1 : 0;
608 _device_set_sector_size (PedDevice* dev)
610 LinuxSpecific* arch_specific = LINUX_SPECIFIC (dev);
613 dev->sector_size = PED_SECTOR_SIZE_DEFAULT;
614 dev->phys_sector_size = PED_SECTOR_SIZE_DEFAULT;
616 PED_ASSERT (dev->open_count, return);
618 if (_get_linux_version() < KERNEL_VERSION (2,3,0)) {
619 dev->sector_size = PED_SECTOR_SIZE_DEFAULT;
623 if (ioctl (arch_specific->fd, BLKSSZGET, §or_size)) {
624 ped_exception_throw (
625 PED_EXCEPTION_WARNING,
627 _("Could not determine sector size for %s: %s.\n"
628 "Using the default sector size (%lld)."),
629 dev->path, strerror (errno), PED_SECTOR_SIZE_DEFAULT);
631 dev->sector_size = (long long)sector_size;
634 /* Return PED_SECTOR_SIZE_DEFAULT for DASDs. */
635 if (dev->type == PED_DEVICE_DASD) {
636 dev->sector_size = PED_SECTOR_SIZE_DEFAULT;
639 if (dev->sector_size != PED_SECTOR_SIZE_DEFAULT) {
640 ped_exception_throw (
641 PED_EXCEPTION_WARNING,
643 _("Device %s has a logical sector size of %lld. Not "
644 "all parts of GNU Parted support this at the moment, "
645 "and the working code is HIGHLY EXPERIMENTAL.\n"),
646 dev->path, dev->sector_size);
651 _kernel_has_blkgetsize64(void)
653 int version = _get_linux_version();
655 if (version >= KERNEL_VERSION (2,5,4)) return 1;
656 if (version < KERNEL_VERSION (2,5,0) &&
657 version >= KERNEL_VERSION (2,4,18)) return 1;
661 /* TODO: do a binary search if BLKGETSIZE doesn't work?! */
663 _device_get_length (PedDevice* dev)
666 LinuxSpecific* arch_specific = LINUX_SPECIFIC (dev);
670 PED_ASSERT (dev->open_count > 0, return 0);
671 PED_ASSERT (dev->sector_size % PED_SECTOR_SIZE_DEFAULT == 0, return 0);
673 if (_kernel_has_blkgetsize64()) {
674 if (ioctl(arch_specific->fd, BLKGETSIZE64, &bytes) == 0) {
675 return bytes / dev->sector_size;
679 if (ioctl (arch_specific->fd, BLKGETSIZE, &size)) {
680 ped_exception_throw (
682 PED_EXCEPTION_CANCEL,
683 _("Unable to determine the size of %s (%s)."),
693 _device_probe_geometry (PedDevice* dev)
695 LinuxSpecific* arch_specific = LINUX_SPECIFIC (dev);
696 struct stat dev_stat;
697 struct hd_geometry geometry;
699 if (!_device_stat (dev, &dev_stat))
701 PED_ASSERT (S_ISBLK (dev_stat.st_mode), return 0);
703 _device_set_sector_size (dev);
705 dev->length = _device_get_length (dev);
709 /* The GETGEO ioctl is no longer useful (as of linux 2.6.x). We could
710 * still use it in 2.4.x, but this is contentious. Perhaps we should
712 dev->bios_geom.sectors = 63;
713 dev->bios_geom.heads = 255;
714 dev->bios_geom.cylinders
715 = dev->length / (63 * 255);
717 /* FIXME: what should we put here? (TODO: discuss on linux-kernel) */
718 if (!ioctl (arch_specific->fd, HDIO_GETGEO, &geometry)
719 && geometry.sectors && geometry.heads) {
720 dev->hw_geom.sectors = geometry.sectors;
721 dev->hw_geom.heads = geometry.heads;
722 dev->hw_geom.cylinders
723 = dev->length / (dev->hw_geom.heads
724 * dev->hw_geom.sectors);
726 dev->hw_geom = dev->bios_geom;
733 strip_name(char* str)
738 for (i = 0; str[i] != 0; i++) {
739 if (!isspace (str[i])
740 || (isspace (str[i]) && !isspace (str[i+1]) && str[i+1])) {
750 init_ide (PedDevice* dev)
752 LinuxSpecific* arch_specific = LINUX_SPECIFIC (dev);
753 struct stat dev_stat;
755 struct hd_driveid hdi;
756 PedExceptionOption ex_status;
758 int sector_multiplier = 0;
760 if (!_device_stat (dev, &dev_stat))
763 dev_major = major (dev_stat.st_rdev);
765 if (!ped_device_open (dev))
768 if (ioctl (arch_specific->fd, HDIO_GET_IDENTITY, &hdi)) {
769 ex_status = ped_exception_throw (
770 PED_EXCEPTION_WARNING,
771 PED_EXCEPTION_IGNORE_CANCEL,
772 _("Could not get identity of device %s - %s"),
773 dev->path, strerror (errno));
775 case PED_EXCEPTION_CANCEL:
776 goto error_close_dev;
778 case PED_EXCEPTION_UNHANDLED:
779 ped_exception_catch ();
780 case PED_EXCEPTION_IGNORE:
781 dev->model = strdup(_("Generic IDE"));
784 PED_ASSERT (0, (void) 0);
788 /* hdi.model is not guaranteed to be NULL terminated */
789 memcpy (hdi_buf, hdi.model, 40);
791 dev->model = strip_name (hdi_buf);
793 if (!hdi.ata7_sectinfo.valid1 && hdi.ata7_sectinfo.valid2)
794 sector_multiplier = hdi.ata7_sectinfo.multiplier;
796 sector_multiplier = 1;
798 if (sector_multiplier != 1) {
799 ex_status = ped_exception_throw (
800 PED_EXCEPTION_WARNING,
801 PED_EXCEPTION_IGNORE_CANCEL,
802 _("Device %s has multiple (%d) logical sectors "
803 "per physical sector.\n"
804 "GNU Parted supports this EXPERIMENTALLY for "
805 "some special disk label/file system "
806 "combinations, e.g. GPT and ext2/3.\n"
807 "Please consult the web site for up-to-date "
809 dev->path, sector_multiplier);
812 case PED_EXCEPTION_CANCEL:
813 goto error_close_dev;
815 case PED_EXCEPTION_UNHANDLED:
816 ped_exception_catch ();
817 case PED_EXCEPTION_IGNORE:
820 PED_ASSERT (0, (void) 0);
825 /* XXX sector_size has not been set yet! */
826 /* dev->phys_sector_size = dev->sector_size
827 * sector_multiplier;*/
828 dev->phys_sector_size = PED_SECTOR_SIZE_DEFAULT;
831 if (!_device_probe_geometry (dev))
832 goto error_close_dev;
834 ped_device_close (dev);
838 ped_device_close (dev);
843 /* This function reads the /sys entry named "file" for device "dev". */
845 read_device_sysfs_file (PedDevice *dev, const char *file)
851 snprintf (name_buf, 127, "/sys/block/%s/device/%s",
852 basename (dev->path), file);
854 if ((f = fopen (name_buf, "r")) == NULL)
857 if (fgets (buf, 255, f) == NULL)
861 return strip_name (buf);
864 /* This function sends a query to a SCSI device for vendor and product
865 * information. It uses the deprecated SCSI_IOCTL_SEND_COMMAND to
869 scsi_query_product_info (PedDevice* dev, char **vendor, char **product)
871 /* The following are defined by the SCSI-2 specification. */
872 typedef struct _scsi_inquiry_cmd
875 uint8_t lun; /* bits 5-7 denote the LUN */
878 uint8_t alloc_length;
880 } __attribute__((packed)) scsi_inquiry_cmd_t;
882 typedef struct _scsi_inquiry_data
884 uint8_t peripheral_info;
886 uint8_t version_info;
888 uint8_t additional_length;
892 uint8_t vendor_id[8];
893 uint8_t product_id[16];
894 uint8_t product_revision[4];
895 uint8_t vendor_specific[20];
896 uint8_t _reserved3[40];
897 } __attribute__((packed)) scsi_inquiry_data_t;
906 scsi_inquiry_data_t out;
907 scsi_inquiry_cmd_t in;
911 LinuxSpecific* arch_specific = LINUX_SPECIFIC (dev);
917 memset (&arg, 0x00, sizeof(struct scsi_arg));
919 arg.outlen = sizeof(scsi_inquiry_data_t);
920 arg.data.in.op = INQUIRY;
921 arg.data.in.lun = dev->host << 5;
922 arg.data.in.alloc_length = sizeof(scsi_inquiry_data_t);
923 arg.data.in.page_code = 0;
924 arg.data.in.reserved = 0;
925 arg.data.in.control = 0;
927 if (ioctl (arch_specific->fd, SCSI_IOCTL_SEND_COMMAND, &arg) < 0)
930 memcpy (buf, arg.data.out.vendor_id, 8);
932 *vendor = strip_name (buf);
934 memcpy (buf, arg.data.out.product_id, 16);
936 *product = strip_name (buf);
941 /* This function provides the vendor and product name for a SCSI device.
942 * It supports both the modern /sys interface and direct queries
943 * via the deprecated ioctl, SCSI_IOCTL_SEND_COMMAND.
946 scsi_get_product_info (PedDevice* dev, char **vendor, char **product)
948 *vendor = read_device_sysfs_file (dev, "vendor");
949 *product = read_device_sysfs_file (dev, "model");
950 if (*vendor && *product)
953 return scsi_query_product_info (dev, vendor, product);
957 init_scsi (PedDevice* dev)
962 uint32_t host_unique_id;
965 LinuxSpecific* arch_specific = LINUX_SPECIFIC (dev);
969 if (!ped_device_open (dev))
972 if (ioctl (arch_specific->fd, SCSI_IOCTL_GET_IDLUN, &idlun) < 0) {
975 if (ped_exception_throw (
976 PED_EXCEPTION_ERROR, PED_EXCEPTION_IGNORE_CANCEL,
977 _("Error initialising SCSI device %s - %s"),
978 dev->path, strerror (errno))
979 != PED_EXCEPTION_IGNORE)
980 goto error_close_dev;
981 if (!_device_probe_geometry (dev))
982 goto error_close_dev;
983 ped_device_close (dev);
987 dev->host = idlun.host_unique_id;
988 dev->did = idlun.dev_id;
990 dev->model = (char*) ped_malloc (8 + 16 + 2);
992 goto error_close_dev;
994 if (scsi_get_product_info (dev, &vendor, &product)) {
995 sprintf (dev->model, "%.8s %.16s", vendor, product);
999 strcpy (dev->model, "Generic SCSI");
1002 if (!_device_probe_geometry (dev))
1003 goto error_close_dev;
1005 ped_device_close (dev);
1009 ped_device_close (dev);
1015 init_file (PedDevice* dev)
1017 struct stat dev_stat;
1019 if (!_device_stat (dev, &dev_stat))
1021 if (!ped_device_open (dev))
1024 if (S_ISBLK(dev_stat.st_mode))
1025 dev->length = _device_get_length (dev);
1027 dev->length = dev_stat.st_size / 512;
1028 if (dev->length <= 0) {
1029 ped_exception_throw (
1030 PED_EXCEPTION_ERROR,
1031 PED_EXCEPTION_CANCEL,
1032 _("The device %s has zero length, and can't possibly "
1033 "store a file system or partition table. Perhaps "
1034 "you selected the wrong device?"),
1036 goto error_close_dev;
1039 ped_device_close (dev);
1041 dev->bios_geom.cylinders = dev->length / 4 / 32;
1042 dev->bios_geom.heads = 4;
1043 dev->bios_geom.sectors = 32;
1044 dev->hw_geom = dev->bios_geom;
1045 dev->sector_size = PED_SECTOR_SIZE_DEFAULT;
1046 dev->phys_sector_size = PED_SECTOR_SIZE_DEFAULT;
1047 dev->model = strdup ("");
1052 ped_device_close (dev);
1058 init_dasd (PedDevice* dev, const char* model_name)
1060 struct stat dev_stat;
1061 struct hd_geometry geo;
1064 if (!_device_stat (dev, &dev_stat))
1067 if (!ped_device_open (dev))
1070 LinuxSpecific* arch_specific = LINUX_SPECIFIC (dev);
1072 PED_ASSERT (S_ISBLK (dev_stat.st_mode), return 0);
1074 _device_set_sector_size (dev);
1075 if (!dev->sector_size)
1076 goto error_close_dev;
1078 dev->length = _device_get_length (dev);
1080 goto error_close_dev;
1082 if (!ioctl (arch_specific->fd, HDIO_GETGEO, &geo)) {
1083 dev->hw_geom.sectors = geo.sectors;
1084 dev->hw_geom.heads = geo.heads;
1085 dev->hw_geom.cylinders = dev->length
1086 / (dev->hw_geom.heads * dev->hw_geom.sectors)
1087 / (dev->sector_size / PED_SECTOR_SIZE_DEFAULT);
1088 dev->bios_geom = dev->hw_geom;
1090 dev->bios_geom.sectors = 12;
1091 dev->bios_geom.heads = 15;
1092 dev->bios_geom.cylinders = dev->length
1093 / (dev->hw_geom.heads * dev->hw_geom.sectors)
1094 / (dev->sector_size / PED_SECTOR_SIZE_DEFAULT);
1095 dev->hw_geom = dev->bios_geom;
1098 dev->model = strdup (model_name);
1100 ped_device_close (dev);
1103 ped_exception_throw ( PED_EXCEPTION_ERROR,
1104 PED_EXCEPTION_IGNORE_CANCEL,
1108 ped_device_close (dev);
1114 init_generic (PedDevice* dev, const char* model_name)
1116 struct stat dev_stat;
1117 PedExceptionOption ex_status;
1119 if (!_device_stat (dev, &dev_stat))
1122 if (!ped_device_open (dev))
1125 ped_exception_fetch_all ();
1126 if (_device_probe_geometry (dev)) {
1127 ped_exception_leave_all ();
1129 /* hack to allow use of files, for testing */
1130 ped_exception_catch ();
1131 ped_exception_leave_all ();
1133 ex_status = ped_exception_throw (
1134 PED_EXCEPTION_WARNING,
1135 PED_EXCEPTION_IGNORE_CANCEL,
1136 _("Unable to determine geometry of "
1137 "file/device %s. You should not use Parted "
1138 "unless you REALLY know what you're doing!"),
1140 switch (ex_status) {
1141 case PED_EXCEPTION_CANCEL:
1142 goto error_close_dev;
1144 case PED_EXCEPTION_UNHANDLED:
1145 ped_exception_catch ();
1146 case PED_EXCEPTION_IGNORE:
1149 PED_ASSERT (0, (void) 0);
1153 /* what should we stick in here? */
1154 dev->length = dev_stat.st_size / PED_SECTOR_SIZE_DEFAULT;
1155 dev->bios_geom.cylinders = dev->length / 4 / 32;
1156 dev->bios_geom.heads = 4;
1157 dev->bios_geom.sectors = 32;
1158 dev->sector_size = PED_SECTOR_SIZE_DEFAULT;
1159 dev->phys_sector_size = PED_SECTOR_SIZE_DEFAULT;
1162 dev->model = strdup (model_name);
1164 ped_device_close (dev);
1168 ped_device_close (dev);
1174 sdmmc_get_product_info (PedDevice* dev, char **type, char **name)
1176 *type = read_device_sysfs_file (dev, "type");
1177 *name = read_device_sysfs_file (dev, "name");
1185 init_sdmmc (PedDevice* dev)
1190 if (sdmmc_get_product_info (dev, &type, &name)) {
1191 snprintf (id, sizeof(id) - 1, "%s %s", type, name);
1195 snprintf (id, sizeof(id) - 1, "%s",
1196 _("Generic SD/MMC Storage Card"));
1198 return init_generic(dev, id);
1202 linux_new (const char* path)
1205 LinuxSpecific* arch_specific;
1207 PED_ASSERT (path != NULL, return NULL);
1209 dev = (PedDevice*) ped_malloc (sizeof (PedDevice));
1213 dev->path = strdup (path);
1215 goto error_free_dev;
1218 = (LinuxSpecific*) ped_malloc (sizeof (LinuxSpecific));
1219 if (!dev->arch_specific)
1220 goto error_free_path;
1221 arch_specific = LINUX_SPECIFIC (dev);
1222 arch_specific->dmtype = NULL;
1224 dev->open_count = 0;
1226 dev->external_mode = 0;
1228 dev->boot_dirty = 0;
1230 if (!_device_probe_type (dev))
1231 goto error_free_arch_specific;
1233 switch (dev->type) {
1234 case PED_DEVICE_IDE:
1235 if (!init_ide (dev))
1236 goto error_free_arch_specific;
1239 case PED_DEVICE_SCSI:
1240 if (!init_scsi (dev))
1241 goto error_free_arch_specific;
1244 case PED_DEVICE_DAC960:
1245 if (!init_generic (dev, _("DAC960 RAID controller")))
1246 goto error_free_arch_specific;
1249 case PED_DEVICE_SX8:
1250 if (!init_generic (dev, _("Promise SX8 SATA Device")))
1251 goto error_free_arch_specific;
1254 case PED_DEVICE_DASD:
1255 if (!init_dasd (dev, _("IBM S390 DASD drive")))
1256 goto error_free_arch_specific;
1259 case PED_DEVICE_VIODASD:
1260 if (!init_generic (dev, _("IBM iSeries Virtual DASD")))
1261 goto error_free_arch_specific;
1264 case PED_DEVICE_CPQARRAY:
1265 if (!init_generic (dev, _("Compaq Smart Array")))
1266 goto error_free_arch_specific;
1269 case PED_DEVICE_ATARAID:
1270 if (!init_generic (dev, _("ATARAID Controller")))
1271 goto error_free_arch_specific;
1274 case PED_DEVICE_I2O:
1275 if (!init_generic (dev, _("I2O Controller")))
1276 goto error_free_arch_specific;
1279 case PED_DEVICE_UBD:
1280 if (!init_generic (dev, _("User-Mode Linux UBD")))
1281 goto error_free_arch_specific;
1284 case PED_DEVICE_FILE:
1285 if (!init_file (dev))
1286 goto error_free_arch_specific;
1292 if (arch_specific->dmtype == NULL
1293 || asprintf(&type, _("Linux device-mapper (%s)"),
1294 arch_specific->dmtype) == -1)
1295 goto error_free_arch_specific;
1296 bool ok = init_generic (dev, type);
1299 goto error_free_arch_specific;
1303 case PED_DEVICE_XVD:
1304 if (!init_generic (dev, _("Xen Virtual Block Device")))
1305 goto error_free_arch_specific;
1308 case PED_DEVICE_UNKNOWN:
1309 if (!init_generic (dev, _("Unknown")))
1310 goto error_free_arch_specific;
1313 case PED_DEVICE_SDMMC:
1314 if (!init_sdmmc (dev))
1315 goto error_free_arch_specific;
1319 ped_exception_throw (PED_EXCEPTION_NO_FEATURE,
1320 PED_EXCEPTION_CANCEL,
1321 _("ped_device_new() Unsupported device type"));
1322 goto error_free_arch_specific;
1326 error_free_arch_specific:
1327 free (dev->arch_specific);
1337 linux_destroy (PedDevice* dev)
1339 free (((LinuxSpecific*)dev->arch_specific)->dmtype);
1340 free (dev->arch_specific);
1347 linux_is_busy (PedDevice* dev)
1352 if (_partition_is_mounted_by_path (dev->path))
1355 for (i = 0; i < 32; i++) {
1358 part_name = _device_get_part_path (dev, i);
1361 status = _partition_is_mounted_by_path (part_name);
1371 /* we need to flush the master device, and all the partition devices,
1372 * because there is no coherency between the caches.
1373 * We should only flush unmounted partition devices, because:
1374 * - there is never a need to flush them (we're not doing IO there)
1375 * - flushing a device that is mounted causes unnecessary IO, and can
1376 * even screw journaling & friends up. Even cause oopsen!
1379 _flush_cache (PedDevice* dev)
1381 LinuxSpecific* arch_specific = LINUX_SPECIFIC (dev);
1388 ioctl (arch_specific->fd, BLKFLSBUF);
1390 for (i = 1; i < 16; i++) {
1394 name = _device_get_part_path (dev, i);
1397 if (!_partition_is_mounted_by_path (name)) {
1398 fd = open (name, WR_MODE, 0);
1400 ioctl (fd, BLKFLSBUF);
1409 linux_open (PedDevice* dev)
1411 LinuxSpecific* arch_specific = LINUX_SPECIFIC (dev);
1414 arch_specific->fd = open (dev->path, RW_MODE);
1416 if (arch_specific->fd == -1) {
1417 char* rw_error_msg = strerror (errno);
1419 arch_specific->fd = open (dev->path, RD_MODE);
1421 if (arch_specific->fd == -1) {
1422 if (ped_exception_throw (
1423 PED_EXCEPTION_ERROR,
1424 PED_EXCEPTION_RETRY_CANCEL,
1425 _("Error opening %s: %s"),
1426 dev->path, strerror (errno))
1427 != PED_EXCEPTION_RETRY) {
1433 ped_exception_throw (
1434 PED_EXCEPTION_WARNING,
1436 _("Unable to open %s read-write (%s). %s has "
1437 "been opened read-only."),
1438 dev->path, rw_error_msg, dev->path);
1451 linux_refresh_open (PedDevice* dev)
1457 linux_close (PedDevice* dev)
1459 LinuxSpecific* arch_specific = LINUX_SPECIFIC (dev);
1463 close (arch_specific->fd);
1468 linux_refresh_close (PedDevice* dev)
1475 #if SIZEOF_OFF_T < 8
1477 static _syscall5(int,_llseek,
1479 unsigned long, offset_high,
1480 unsigned long, offset_low,
1482 unsigned int, origin)
1485 llseek (unsigned int fd, loff_t offset, unsigned int whence)
1490 retval = _llseek(fd,
1491 ((unsigned long long)offset) >> 32,
1492 ((unsigned long long)offset) & 0xffffffff,
1495 return (retval==-1 ? (loff_t) retval : result);
1498 #endif /* SIZEOF_OFF_T < 8 */
1501 _device_seek (const PedDevice* dev, PedSector sector)
1503 LinuxSpecific* arch_specific;
1505 PED_ASSERT (dev->sector_size % PED_SECTOR_SIZE_DEFAULT == 0, return 0);
1506 PED_ASSERT (dev != NULL, return 0);
1507 PED_ASSERT (!dev->external_mode, return 0);
1509 arch_specific = LINUX_SPECIFIC (dev);
1511 #if SIZEOF_OFF_T < 8
1512 if (sizeof (off_t) < 8) {
1513 loff_t pos = (loff_t)(sector * dev->sector_size);
1514 return llseek (arch_specific->fd, pos, SEEK_SET) == pos;
1518 off_t pos = sector * dev->sector_size;
1519 return lseek (arch_specific->fd, pos, SEEK_SET) == pos;
1524 _read_lastoddsector (const PedDevice* dev, void* buffer)
1526 LinuxSpecific* arch_specific;
1527 struct blkdev_ioctl_param ioctl_param;
1529 PED_ASSERT(dev != NULL, return 0);
1530 PED_ASSERT(buffer != NULL, return 0);
1532 arch_specific = LINUX_SPECIFIC (dev);
1535 ioctl_param.block = 0; /* read the last sector */
1536 ioctl_param.content_length = dev->sector_size;
1537 ioctl_param.block_contents = buffer;
1539 if (ioctl(arch_specific->fd, BLKGETLASTSECT, &ioctl_param) == -1) {
1540 PedExceptionOption opt;
1541 opt = ped_exception_throw (
1542 PED_EXCEPTION_ERROR,
1543 PED_EXCEPTION_RETRY_IGNORE_CANCEL,
1544 _("%s during read on %s"),
1545 strerror (errno), dev->path);
1547 if (opt == PED_EXCEPTION_CANCEL)
1549 if (opt == PED_EXCEPTION_RETRY)
1557 linux_read (const PedDevice* dev, void* buffer, PedSector start,
1560 LinuxSpecific* arch_specific = LINUX_SPECIFIC (dev);
1561 PedExceptionOption ex_status;
1562 void* diobuf = NULL;
1564 PED_ASSERT (dev != NULL, return 0);
1565 PED_ASSERT (dev->sector_size % PED_SECTOR_SIZE_DEFAULT == 0, return 0);
1567 if (_get_linux_version() < KERNEL_VERSION (2,6,0)) {
1568 /* Kludge. This is necessary to read/write the last
1569 block of an odd-sized disk, until Linux 2.5.x kernel fixes.
1571 if (dev->type != PED_DEVICE_FILE && (dev->length & 1)
1572 && start + count - 1 == dev->length - 1)
1573 return ped_device_read (dev, buffer, start, count - 1)
1574 && _read_lastoddsector (
1575 dev, (char *) buffer + (count-1) * 512);
1578 if (_device_seek (dev, start))
1581 ex_status = ped_exception_throw (
1582 PED_EXCEPTION_ERROR,
1583 PED_EXCEPTION_RETRY_IGNORE_CANCEL,
1584 _("%s during seek for read on %s"),
1585 strerror (errno), dev->path);
1587 switch (ex_status) {
1588 case PED_EXCEPTION_IGNORE:
1591 case PED_EXCEPTION_RETRY:
1594 case PED_EXCEPTION_UNHANDLED:
1595 ped_exception_catch ();
1596 case PED_EXCEPTION_CANCEL:
1599 PED_ASSERT (0, (void) 0);
1604 size_t read_length = count * dev->sector_size;
1605 if (posix_memalign (&diobuf, dev->sector_size, read_length) != 0)
1609 ssize_t status = read (arch_specific->fd, diobuf, read_length);
1611 memcpy(buffer, diobuf, status);
1612 if (status == (ssize_t) read_length)
1615 read_length -= status;
1616 buffer = (char *) buffer + status;
1620 ex_status = ped_exception_throw (
1621 PED_EXCEPTION_ERROR,
1622 PED_EXCEPTION_RETRY_IGNORE_CANCEL,
1623 _("%s during read on %s"),
1627 switch (ex_status) {
1628 case PED_EXCEPTION_IGNORE:
1632 case PED_EXCEPTION_RETRY:
1635 case PED_EXCEPTION_UNHANDLED:
1636 ped_exception_catch ();
1637 case PED_EXCEPTION_CANCEL:
1641 PED_ASSERT (0, (void) 0);
1652 _write_lastoddsector (PedDevice* dev, const void* buffer)
1654 LinuxSpecific* arch_specific;
1655 struct blkdev_ioctl_param ioctl_param;
1657 PED_ASSERT(dev != NULL, return 0);
1658 PED_ASSERT(buffer != NULL, return 0);
1660 arch_specific = LINUX_SPECIFIC (dev);
1663 ioctl_param.block = 0; /* write the last sector */
1664 ioctl_param.content_length = dev->sector_size;
1665 ioctl_param.block_contents = (void*) buffer;
1667 if (ioctl(arch_specific->fd, BLKSETLASTSECT, &ioctl_param) == -1) {
1668 PedExceptionOption opt;
1669 opt = ped_exception_throw (
1670 PED_EXCEPTION_ERROR,
1671 PED_EXCEPTION_RETRY_IGNORE_CANCEL,
1672 _("%s during write on %s"),
1673 strerror (errno), dev->path);
1675 if (opt == PED_EXCEPTION_CANCEL)
1677 if (opt == PED_EXCEPTION_RETRY)
1685 linux_write (PedDevice* dev, const void* buffer, PedSector start,
1688 LinuxSpecific* arch_specific = LINUX_SPECIFIC (dev);
1689 PedExceptionOption ex_status;
1693 PED_ASSERT(dev->sector_size % PED_SECTOR_SIZE_DEFAULT == 0, return 0);
1695 if (dev->read_only) {
1696 if (ped_exception_throw (
1697 PED_EXCEPTION_ERROR,
1698 PED_EXCEPTION_IGNORE_CANCEL,
1699 _("Can't write to %s, because it is opened read-only."),
1701 != PED_EXCEPTION_IGNORE)
1707 if (_get_linux_version() < KERNEL_VERSION (2,6,0)) {
1708 /* Kludge. This is necessary to read/write the last
1709 block of an odd-sized disk, until Linux 2.5.x kernel fixes.
1711 if (dev->type != PED_DEVICE_FILE && (dev->length & 1)
1712 && start + count - 1 == dev->length - 1)
1713 return ped_device_write (dev, buffer, start, count - 1)
1714 && _write_lastoddsector (
1715 dev, (char*) buffer + (count-1) * 512);
1718 if (_device_seek (dev, start))
1721 ex_status = ped_exception_throw (
1722 PED_EXCEPTION_ERROR, PED_EXCEPTION_RETRY_IGNORE_CANCEL,
1723 _("%s during seek for write on %s"),
1724 strerror (errno), dev->path);
1726 switch (ex_status) {
1727 case PED_EXCEPTION_IGNORE:
1730 case PED_EXCEPTION_RETRY:
1733 case PED_EXCEPTION_UNHANDLED:
1734 ped_exception_catch ();
1735 case PED_EXCEPTION_CANCEL:
1738 PED_ASSERT (0, (void) 0);
1744 printf ("ped_device_write (\"%s\", %p, %d, %d)\n",
1745 dev->path, buffer, (int) start, (int) count);
1747 size_t write_length = count * dev->sector_size;
1749 if (posix_memalign(&diobuf, dev->sector_size, write_length) != 0)
1751 memcpy(diobuf, buffer, write_length);
1752 diobuf_start = diobuf;
1754 ssize_t status = write (arch_specific->fd, diobuf, write_length);
1755 if (status == write_length) break;
1757 write_length -= status;
1758 diobuf = (char *) diobuf + status;
1762 ex_status = ped_exception_throw (
1763 PED_EXCEPTION_ERROR,
1764 PED_EXCEPTION_RETRY_IGNORE_CANCEL,
1765 _("%s during write on %s"),
1766 strerror (errno), dev->path);
1768 switch (ex_status) {
1769 case PED_EXCEPTION_IGNORE:
1773 case PED_EXCEPTION_RETRY:
1776 case PED_EXCEPTION_UNHANDLED:
1777 ped_exception_catch ();
1778 case PED_EXCEPTION_CANCEL:
1782 PED_ASSERT (0, (void) 0);
1787 #endif /* !READ_ONLY */
1791 /* returns the number of sectors that are ok.
1794 linux_check (PedDevice* dev, void* buffer, PedSector start, PedSector count)
1796 LinuxSpecific* arch_specific = LINUX_SPECIFIC (dev);
1801 PED_ASSERT(dev != NULL, return 0);
1803 if (!_device_seek (dev, start))
1806 if (posix_memalign(&diobuf, PED_SECTOR_SIZE_DEFAULT,
1807 count * PED_SECTOR_SIZE_DEFAULT) != 0)
1810 for (done = 0; done < count; done += status / dev->sector_size) {
1811 status = read (arch_specific->fd, diobuf,
1812 (size_t) ((count-done) * dev->sector_size));
1814 memcpy(buffer, diobuf, status);
1824 _do_fsync (PedDevice* dev)
1826 LinuxSpecific* arch_specific = LINUX_SPECIFIC (dev);
1828 PedExceptionOption ex_status;
1831 status = fsync (arch_specific->fd);
1832 if (status >= 0) break;
1834 ex_status = ped_exception_throw (
1835 PED_EXCEPTION_ERROR,
1836 PED_EXCEPTION_RETRY_IGNORE_CANCEL,
1837 _("%s during write on %s"),
1838 strerror (errno), dev->path);
1840 switch (ex_status) {
1841 case PED_EXCEPTION_IGNORE:
1844 case PED_EXCEPTION_RETRY:
1847 case PED_EXCEPTION_UNHANDLED:
1848 ped_exception_catch ();
1849 case PED_EXCEPTION_CANCEL:
1852 PED_ASSERT (0, (void) 0);
1860 linux_sync (PedDevice* dev)
1862 PED_ASSERT (dev != NULL, return 0);
1863 PED_ASSERT (!dev->external_mode, return 0);
1867 if (!_do_fsync (dev))
1874 linux_sync_fast (PedDevice* dev)
1876 PED_ASSERT (dev != NULL, return 0);
1877 PED_ASSERT (!dev->external_mode, return 0);
1881 if (!_do_fsync (dev))
1883 /* no cache flush... */
1888 _compare_digit_state (char ch, int need_digit)
1890 return !!isdigit (ch) == need_digit;
1893 /* matches the regexp "[^0-9]+[0-9]+[^0-9]+[0-9]+$".
1894 * Motivation: accept devices looking like /dev/rd/c0d0, but
1895 * not looking like /dev/hda1 and /dev/rd/c0d0p1
1898 _match_rd_device (const char* name)
1903 /* exclude directory names from test */
1904 pos = strrchr(name, '/') ?: name;
1912 for (state = 0; state < 4; state++) {
1913 int want_digits = (state % 2 == 1);
1917 if (!_compare_digit_state (*pos, want_digits))
1920 } while (_compare_digit_state (*pos, want_digits));
1927 _probe_proc_partitions ()
1929 FILE* proc_part_file;
1930 int major, minor, size;
1932 char part_name [256];
1933 char dev_name [256];
1935 proc_part_file = fopen ("/proc/partitions", "r");
1936 if (!proc_part_file)
1939 if (fgets (buf, 256, proc_part_file) == NULL)
1942 if (fgets (buf, 256, proc_part_file) == NULL)
1945 while (fgets (buf, 512, proc_part_file)
1946 && sscanf (buf, "%d %d %d %255s", &major, &minor, &size,
1948 /* Heuristic for telling partitions and devices apart
1949 * Probably needs to be improved
1951 if (!_match_rd_device (part_name)
1952 && isdigit (part_name [strlen (part_name) - 1]))
1955 strcpy (dev_name, "/dev/");
1956 strcat (dev_name, part_name);
1957 _ped_device_probe (dev_name);
1960 fclose (proc_part_file);
1970 _skip_entry (const char *name)
1973 static struct _entry entries[] = {
1974 { ".", sizeof (".") - 1 },
1975 { "..", sizeof ("..") - 1 },
1976 { "dm-", sizeof ("dm-") - 1 },
1977 { "loop", sizeof ("loop") - 1 },
1978 { "ram", sizeof ("ram") - 1 },
1982 for (i = entries; i->name != 0; i++) {
1983 if (strncmp (name, i->name, i->len) == 0)
1994 struct dirent *dirent;
1995 char dev_name [256];
1998 if (!(blockdir = opendir ("/sys/block")))
2000 while ((dirent = readdir (blockdir))) {
2001 if (_skip_entry (dirent->d_name))
2004 if (strlen (dirent->d_name) > sizeof (dev_name) - 6)
2005 continue; /* device name too long! */
2007 strcpy (dev_name, "/dev/");
2008 strcat (dev_name, dirent->d_name);
2009 /* in /sys/block, '/'s are replaced with '!' or '.' */
2010 for (ptr = dev_name; *ptr != '\0'; ptr++) {
2011 if (*ptr == '!' || *ptr == '.')
2014 _ped_device_probe (dev_name);
2017 closedir (blockdir);
2022 _probe_standard_devices ()
2024 _ped_device_probe ("/dev/hda");
2025 _ped_device_probe ("/dev/hdb");
2026 _ped_device_probe ("/dev/hdc");
2027 _ped_device_probe ("/dev/hdd");
2028 _ped_device_probe ("/dev/hde");
2029 _ped_device_probe ("/dev/hdf");
2030 _ped_device_probe ("/dev/hdg");
2031 _ped_device_probe ("/dev/hdh");
2033 _ped_device_probe ("/dev/sda");
2034 _ped_device_probe ("/dev/sdb");
2035 _ped_device_probe ("/dev/sdc");
2036 _ped_device_probe ("/dev/sdd");
2037 _ped_device_probe ("/dev/sde");
2038 _ped_device_probe ("/dev/sdf");
2046 /* we should probe the standard devs too, even with /proc/partitions,
2047 * because /proc/partitions might return devfs stuff, and we might not
2048 * have devfs available
2050 _probe_standard_devices ();
2052 #ifdef ENABLE_DEVICE_MAPPER
2053 /* device-mapper devices aren't listed in /proc/partitions; or, if
2054 * they are, they're listed as dm-X. So, instead of relying on that,
2055 * we do our own checks.
2057 _probe_dm_devices ();
2060 /* /sys/block is more reliable and consistent; fall back to using
2061 * /proc/partitions if the former is unavailable, however.
2063 if (!_probe_sys_block ())
2064 _probe_proc_partitions ();
2068 _device_get_part_path (PedDevice* dev, int num)
2070 int path_len = strlen (dev->path);
2071 int result_len = path_len + 16;
2074 result = (char*) ped_malloc (result_len);
2078 /* Check for devfs-style /disc => /partN transformation
2079 unconditionally; the system might be using udev with devfs rules,
2080 and if not the test is harmless. */
2081 if (!strcmp (dev->path + path_len - 5, "/disc")) {
2082 /* replace /disc with /path%d */
2083 strcpy (result, dev->path);
2084 snprintf (result + path_len - 5, 16, "/part%d", num);
2085 } else if (dev->type == PED_DEVICE_DAC960
2086 || dev->type == PED_DEVICE_CPQARRAY
2087 || dev->type == PED_DEVICE_ATARAID
2088 || dev->type == PED_DEVICE_DM
2089 || isdigit (dev->path[path_len - 1]))
2090 snprintf (result, result_len, "%sp%d", dev->path, num);
2092 snprintf (result, result_len, "%s%d", dev->path, num);
2098 linux_partition_get_path (const PedPartition* part)
2100 return _device_get_part_path (part->disk->dev, part->num);
2104 _partition_get_part_dev (const PedPartition* part)
2106 struct stat dev_stat;
2107 int dev_major, dev_minor;
2109 if (!_device_stat (part->disk->dev, &dev_stat))
2111 dev_major = major (dev_stat.st_rdev);
2112 dev_minor = minor (dev_stat.st_rdev);
2113 return (dev_t)makedev (dev_major, dev_minor + part->num);
2117 _mount_table_search (const char* file_name, dev_t dev)
2119 struct stat part_stat;
2121 char part_name[512];
2125 file = fopen (file_name, "r");
2128 while (fgets (line, 512, file)) {
2129 junk = sscanf (line, "%s", part_name);
2130 if (stat (part_name, &part_stat) == 0) {
2131 if (part_stat.st_rdev == dev) {
2142 _partition_is_mounted_by_dev (dev_t dev)
2144 return _mount_table_search( "/proc/mounts", dev)
2145 || _mount_table_search( "/proc/swaps", dev)
2146 || _mount_table_search( "/etc/mtab", dev);
2150 _partition_is_mounted_by_path (const char *path)
2152 struct stat part_stat;
2153 if (stat (path, &part_stat) != 0)
2155 if (!S_ISBLK(part_stat.st_mode))
2157 return _partition_is_mounted_by_dev (part_stat.st_rdev);
2161 _partition_is_mounted (const PedPartition *part)
2164 if (!ped_partition_is_active (part))
2166 dev = _partition_get_part_dev (part);
2167 return _partition_is_mounted_by_dev (dev);
2171 linux_partition_is_busy (const PedPartition* part)
2175 PED_ASSERT (part != NULL, return 0);
2177 if (_partition_is_mounted (part))
2179 if (part->type == PED_PARTITION_EXTENDED) {
2180 for (walk = part->part_list; walk; walk = walk->next) {
2181 if (linux_partition_is_busy (walk))
2189 _blkpg_part_command (PedDevice* dev, struct blkpg_partition* part, int op)
2191 LinuxSpecific* arch_specific = LINUX_SPECIFIC (dev);
2192 struct blkpg_ioctl_arg ioctl_arg;
2195 ioctl_arg.flags = 0;
2196 ioctl_arg.datalen = sizeof (struct blkpg_partition);
2197 ioctl_arg.data = (void*) part;
2199 return ioctl (arch_specific->fd, BLKPG, &ioctl_arg) == 0;
2203 _blkpg_add_partition (PedDisk* disk, const PedPartition *part)
2205 struct blkpg_partition linux_part;
2206 const char* vol_name;
2209 PED_ASSERT(disk != NULL, return 0);
2210 PED_ASSERT(disk->dev->sector_size % PED_SECTOR_SIZE_DEFAULT == 0,
2213 if (ped_disk_type_check_feature (disk->type,
2214 PED_DISK_TYPE_PARTITION_NAME))
2215 vol_name = ped_partition_get_name (part);
2219 dev_name = _device_get_part_path (disk->dev, part->num);
2223 memset (&linux_part, 0, sizeof (linux_part));
2224 linux_part.start = part->geom.start * disk->dev->sector_size;
2225 /* see fs/partitions/msdos.c:msdos_partition(): "leave room for LILO" */
2226 if (part->type & PED_PARTITION_EXTENDED)
2227 linux_part.length = part->geom.length == 1 ? 512 : 1024;
2229 linux_part.length = part->geom.length * disk->dev->sector_size;
2230 linux_part.pno = part->num;
2231 strncpy (linux_part.devname, dev_name, BLKPG_DEVNAMELTH);
2233 strncpy (linux_part.volname, vol_name, BLKPG_VOLNAMELTH);
2237 if (!_blkpg_part_command (disk->dev, &linux_part,
2238 BLKPG_ADD_PARTITION)) {
2239 return ped_exception_throw (
2240 PED_EXCEPTION_ERROR,
2241 PED_EXCEPTION_IGNORE_CANCEL,
2242 _("Error informing the kernel about modifications to "
2243 "partition %s -- %s. This means Linux won't know "
2244 "about any changes you made to %s until you reboot "
2245 "-- so you shouldn't mount it or use it in any way "
2246 "before rebooting."),
2250 == PED_EXCEPTION_IGNORE;
2257 _blkpg_remove_partition (PedDisk* disk, int n)
2259 struct blkpg_partition linux_part;
2261 memset (&linux_part, 0, sizeof (linux_part));
2263 return _blkpg_part_command (disk->dev, &linux_part,
2264 BLKPG_DEL_PARTITION);
2268 * The number of partitions that a device can have depends on the kernel.
2269 * If we don't find this value in /sys/block/DEV/range, we will use our own
2273 _device_get_partition_range(PedDevice* dev)
2280 r = snprintf(path, sizeof(path), "/sys/block/%s/range",
2281 basename(dev->path));
2282 if(r < 0 || r >= sizeof(path))
2283 return MAX_NUM_PARTS;
2285 fp = fopen(path, "r");
2287 return MAX_NUM_PARTS;
2289 ok = fscanf(fp, "%d", &range) == 1;
2292 /* (range <= 0) is none sense.*/
2293 return ok && range > 0 ? range : MAX_NUM_PARTS;
2297 * Sync the partition table in two step process:
2298 * 1. Remove all of the partitions from the kernel's tables, but do not attempt
2299 * removal of any partition for which the corresponding ioctl call fails.
2300 * 2. Add all the partitions that we hold in disk.
2302 * To achieve this two step process we must calculate the minimum number of
2303 * maximum possible partitions between what linux supports and what the label
2304 * type supports. EX:
2306 * number=MIN(max_parts_supported_in_linux,max_parts_supported_in_msdos_tables)
2309 _disk_sync_part_table (PedDisk* disk)
2311 PED_ASSERT(disk != NULL, return 0);
2312 PED_ASSERT(disk->dev != NULL, return 0);
2315 /* lpn = largest partition number. */
2316 if(ped_disk_get_max_supported_partition_count(disk, &lpn))
2317 lpn = PED_MIN(lpn, _device_get_partition_range(disk->dev));
2319 lpn = _device_get_partition_range(disk->dev);
2321 /* Its not possible to support largest_partnum < 0.
2322 * largest_partnum == 0 would mean does not support partitions.
2327 int *rets = ped_malloc(sizeof(int) * lpn);
2328 int *errnums = ped_malloc(sizeof(int) * lpn);
2332 for (i = 1; i <= lpn; i++) {
2333 rets[i - 1] = _blkpg_remove_partition (disk, i);
2334 errnums[i - 1] = errno;
2337 for (i = 1; i <= lpn; i++) {
2338 const PedPartition *part = ped_disk_get_partition (disk, i);
2340 /* busy... so we won't (can't!) disturb ;) Prolly
2341 * doesn't matter anyway, because users shouldn't be
2342 * changing mounted partitions anyway...
2344 if (!rets[i - 1] && errnums[i - 1] == EBUSY)
2347 /* add the (possibly modified or new) partition */
2348 if (!_blkpg_add_partition (disk, part))
2358 #ifdef ENABLE_DEVICE_MAPPER
2360 _dm_remove_map_name(char *name)
2362 struct dm_task *task = NULL;
2365 task = dm_task_create(DM_DEVICE_REMOVE);
2369 dm_task_set_name (task, name);
2371 rc = dm_task_run(task);
2372 dm_task_update_nodes();
2373 dm_task_destroy(task);
2381 _dm_is_part (struct dm_info *this, char *name)
2383 struct dm_task* task = NULL;
2384 struct dm_info* info = alloca(sizeof *info);
2385 struct dm_deps* deps = NULL;
2389 task = dm_task_create(DM_DEVICE_DEPS);
2393 dm_task_set_name(task, name);
2394 rc = dm_task_run(task);
2401 memset(info, '\0', sizeof *info);
2402 dm_task_get_info(task, info);
2406 deps = dm_task_get_deps(task);
2411 for (i = 0; i < deps->count; i++) {
2412 unsigned int ma = major(deps->device[i]),
2413 mi = minor(deps->device[i]);
2415 if (ma == this->major && mi == this->minor)
2420 dm_task_destroy(task);
2425 _dm_remove_parts (PedDevice* dev)
2427 struct stat dev_stat;
2428 struct dm_task* task = NULL;
2429 struct dm_info* info = alloca(sizeof *info);
2430 struct dm_names* names = NULL;
2431 unsigned int next = 0;
2434 if (!_device_stat (dev, &dev_stat))
2437 task = dm_task_create(DM_DEVICE_LIST);
2441 dm_task_set_major (task, major (dev_stat.st_rdev));
2442 dm_task_set_minor (task, minor (dev_stat.st_rdev));
2444 rc = dm_task_run(task);
2448 memset(info, '\0', sizeof *info);
2449 dm_task_get_info(task, info);
2453 names = dm_task_get_names(task);
2459 names = (void *)names + next;
2461 if (_dm_is_part(info, names->name))
2462 rc += _dm_remove_map_name(names->name);
2467 dm_task_update_nodes();
2468 dm_task_destroy(task);
2475 dm_task_destroy(task);
2476 ped_exception_throw (PED_EXCEPTION_WARNING, PED_EXCEPTION_IGNORE,
2477 _("parted was unable to re-read the partition "
2478 "table on %s (%s). This means Linux won't know "
2479 "anything about the modifications you made. "),
2480 dev->path, strerror (errno));
2485 _dm_add_partition (PedDisk* disk, PedPartition* part)
2487 struct stat dev_stat;
2488 struct dm_task* task = NULL;
2490 char* vol_name = NULL;
2491 char* dev_name = NULL;
2492 char* params = NULL;
2494 dev_name = _device_get_part_path (disk->dev, part->num);
2498 vol_name = strrchr (dev_name, '/');
2499 if (vol_name && *vol_name && *(++vol_name))
2500 vol_name = strdup (vol_name);
2502 vol_name = strdup (dev_name);
2506 if (!_device_stat (disk->dev, &dev_stat))
2509 if (asprintf (¶ms, "%d:%d %lld", major (dev_stat.st_rdev),
2510 minor (dev_stat.st_rdev), part->geom.start) == -1)
2516 task = dm_task_create (DM_DEVICE_CREATE);
2520 dm_task_set_name (task, vol_name);
2521 dm_task_add_target (task, 0, part->geom.length,
2523 rc = dm_task_run(task);
2525 //printf("0 %ld linear %s\n", part->geom.length, params);
2526 dm_task_update_nodes();
2527 dm_task_destroy(task);
2532 _dm_remove_map_name(vol_name);
2535 dm_task_update_nodes();
2537 dm_task_destroy (task);
2544 _dm_reread_part_table (PedDisk* disk)
2546 int largest_partnum = ped_disk_get_last_partition_num (disk);
2547 if (largest_partnum <= 0)
2551 int last = PED_MIN (largest_partnum, 16);
2555 if (!_dm_remove_parts(disk->dev))
2558 for (i = 1; i <= last; i++) {
2561 part = ped_disk_get_partition (disk, i);
2565 if (!_dm_add_partition (disk, part))
2573 _kernel_reread_part_table (PedDevice* dev)
2575 LinuxSpecific* arch_specific = LINUX_SPECIFIC (dev);
2576 int retry_count = 5;
2579 while (ioctl (arch_specific->fd, BLKRRPART)) {
2583 ped_exception_throw (
2584 PED_EXCEPTION_WARNING,
2585 PED_EXCEPTION_IGNORE,
2586 _("The kernel was unable to re-read the partition "
2587 "table on %s (%s). This means Linux won't know "
2588 "anything about the modifications you made "
2589 "until you reboot. You should reboot your computer "
2590 "before doing anything with %s."),
2591 dev->path, strerror (errno), dev->path);
2602 static int have_blkpg = -1;
2605 if (have_blkpg != -1)
2608 kver = _get_linux_version();
2609 return have_blkpg = kver >= KERNEL_VERSION (2,4,0) ? 1 : 0;
2613 linux_disk_commit (PedDisk* disk)
2615 #ifdef ENABLE_DEVICE_MAPPER
2616 if (disk->dev->type == PED_DEVICE_DM)
2617 return _dm_reread_part_table (disk);
2619 if (disk->dev->type != PED_DEVICE_FILE) {
2620 /* The ioctl() command BLKPG_ADD_PARTITION does not notify
2621 * the devfs system; consequently, /proc/partitions will not
2622 * be up to date, and the proper links in /dev are not
2623 * created. Therefore, if using DevFS, we must get the kernel
2624 * to re-read and grok the partition table.
2626 /* Work around kernel dasd problem so we really do BLKRRPART */
2627 if (disk->dev->type != PED_DEVICE_DASD &&
2628 _have_blkpg () && !_have_devfs ()) {
2629 if (_disk_sync_part_table (disk))
2633 return _kernel_reread_part_table (disk->dev);
2639 static PedDeviceArchOps linux_dev_ops = {
2641 destroy: linux_destroy,
2642 is_busy: linux_is_busy,
2644 refresh_open: linux_refresh_open,
2646 refresh_close: linux_refresh_close,
2651 sync_fast: linux_sync_fast,
2652 probe_all: linux_probe_all
2655 PedDiskArchOps linux_disk_ops = {
2656 partition_get_path: linux_partition_get_path,
2657 partition_is_busy: linux_partition_is_busy,
2658 disk_commit: linux_disk_commit
2661 PedArchitecture ped_linux_arch = {
2662 dev_ops: &linux_dev_ops,
2663 disk_ops: &linux_disk_ops