2 libparted - a library for manipulating disk partitions
3 Copyright (C) 1999 - 2001, 2005, 2007-2010 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 * \addtogroup PedDevice
24 * \brief Device access.
26 * When ped_device_probe_all() is called, libparted attempts to detect all
27 * devices. It constructs a list which can be accessed with
28 * ped_device_get_next().
30 * If you want to use a device that isn't on the list, use
31 * ped_device_get(). Also, there may be OS-specific constructors, for creating
32 * devices from file descriptors, stores, etc. For example,
33 * ped_device_new_from_store().
40 #include <parted/parted.h>
41 #include <parted/debug.h>
48 #include "architecture.h"
50 static PedDevice* devices; /* legal advice says: initialized to NULL,
51 under section 6.7.8 part 10
52 of ISO/EIC 9899:1999 */
55 _device_register (PedDevice* dev)
58 for (walk = devices; walk && walk->next; walk = walk->next);
67 _device_unregister (PedDevice* dev)
70 PedDevice* last = NULL;
72 for (walk = devices; walk != NULL; last = walk, walk = walk->next) {
73 if (walk == dev) break;
76 /* This function may be called twice for the same device if a
77 libparted user explictly removes the device from the cache using
78 ped_device_cache_remove(), we get called and it then becomes the
79 user's responsibility to free the PedDevice by calling
81 ped_device_destroy() will then call us a second time, so if the
82 device is not found in the list do nothing. */
87 last->next = dev->next;
93 * Returns the next device that was detected by ped_device_probe_all(), or
94 * calls to ped_device_get_next().
95 * If dev is NULL, returns the first device.
97 * \return NULL if dev is the last device.
100 ped_device_get_next (const PedDevice* dev)
109 _ped_device_probe (const char* path)
113 PED_ASSERT (path != NULL);
115 ped_exception_fetch_all ();
116 dev = ped_device_get (path);
118 ped_exception_catch ();
119 ped_exception_leave_all ();
123 * Attempts to detect all devices.
126 ped_device_probe_all ()
128 ped_architecture->dev_ops->probe_all ();
132 * Close/free all devices.
133 * Called by ped_done(), so you do not need to worry about it.
136 ped_device_free_all ()
139 ped_device_destroy (devices);
143 * Gets the device "name", where name is usually the block device, e.g.
144 * /dev/sdb. If the device wasn't detected with ped_device_probe_all(),
145 * an attempt will be made to detect it again. If it is found, it will
146 * be added to the list.
149 ped_device_get (const char* path)
152 char* normal_path = NULL;
154 PED_ASSERT (path != NULL);
155 /* Don't canonicalize /dev/mapper paths, see tests/symlink.c */
156 if (strncmp (path, "/dev/mapper/", 12))
157 normal_path = canonicalize_file_name (path);
159 /* Well, maybe it is just that the file does not exist.
161 normal_path = strdup (path);
165 for (walk = devices; walk != NULL; walk = walk->next) {
166 if (!strcmp (walk->path, normal_path)) {
172 walk = ped_architecture->dev_ops->_new (normal_path);
176 _device_register (walk);
181 * Destroys a device and removes it from the device list, and frees
182 * all resources associated with the device (all resources allocated
183 * when the device was created).
186 ped_device_destroy (PedDevice* dev)
188 _device_unregister (dev);
190 while (dev->open_count) {
191 if (!ped_device_close (dev))
195 ped_architecture->dev_ops->destroy (dev);
199 ped_device_cache_remove(PedDevice *dev)
201 _device_unregister (dev);
205 ped_device_is_busy (PedDevice* dev)
207 return ped_architecture->dev_ops->is_busy (dev);
211 * Attempt to open a device to allow use of read, write and sync functions.
213 * The meaning of "open" is architecture-dependent. Apart from requesting
214 * access to the device from the operating system, it does things like flushing
216 * \note May allocate resources. Any resources allocated here will
217 * be freed by a final ped_device_close(). (ped_device_open() may be
218 * called multiple times -- it's a ref-count-like mechanism)
220 * \return zero on failure
223 ped_device_open (PedDevice* dev)
227 PED_ASSERT (dev != NULL);
228 PED_ASSERT (!dev->external_mode);
231 status = ped_architecture->dev_ops->refresh_open (dev);
233 status = ped_architecture->dev_ops->open (dev);
241 * If this is the final close, then resources allocated by
242 * ped_device_open() are freed.
244 * \return zero on failure
247 ped_device_close (PedDevice* dev)
249 PED_ASSERT (dev != NULL);
250 PED_ASSERT (!dev->external_mode);
251 PED_ASSERT (dev->open_count > 0);
253 if (--dev->open_count)
254 return ped_architecture->dev_ops->refresh_close (dev);
256 return ped_architecture->dev_ops->close (dev);
260 * Begins external access mode. External access mode allows you to
261 * safely do IO on the device. If a PedDevice is open, then you should
262 * not do any IO on that device, e.g. by calling an external program
263 * like e2fsck, unless you put it in external access mode. You should
264 * not use any libparted commands that do IO to a device, e.g.
265 * ped_file_system_{open|resize|copy}, ped_disk_{read|write}), while
266 * a device is in external access mode.
267 * Also, you should not ped_device_close() a device, while it is
268 * in external access mode.
269 * Note: ped_device_begin_external_access_mode() does things like
270 * tell the kernel to flush its caches.
272 * Close a device while pretending it is still open.
273 * This is useful for temporarily suspending libparted access to the device
274 * in order for an external program to access it.
275 * (Running external programs while the device is open can cause cache
276 * coherency problems.)
278 * In particular, this function keeps track of dev->open_count, so that
279 * reference counting isn't screwed up.
281 * \return zero on failure.
284 ped_device_begin_external_access (PedDevice* dev)
286 PED_ASSERT (dev != NULL);
287 PED_ASSERT (!dev->external_mode);
289 dev->external_mode = 1;
291 return ped_architecture->dev_ops->close (dev);
297 * \brief Complementary function to ped_device_begin_external_access.
299 * \note does things like tell the kernel to flush the device's cache.
301 * \return zero on failure.
304 ped_device_end_external_access (PedDevice* dev)
306 PED_ASSERT (dev != NULL);
307 PED_ASSERT (dev->external_mode);
309 dev->external_mode = 0;
311 return ped_architecture->dev_ops->open (dev);
317 * \internal Read count sectors from dev into buffer, beginning with sector
320 * \return zero on failure.
323 ped_device_read (const PedDevice* dev, void* buffer, PedSector start,
326 PED_ASSERT (dev != NULL);
327 PED_ASSERT (buffer != NULL);
328 PED_ASSERT (!dev->external_mode);
329 PED_ASSERT (dev->open_count > 0);
331 return (ped_architecture->dev_ops->read) (dev, buffer, start, count);
335 * \internal Write count sectors from buffer to dev, starting at sector
338 * \return zero on failure.
340 * \sa PedDevice::sector_size
341 * \sa PedDevice::phys_sector_size
344 ped_device_write (PedDevice* dev, const void* buffer, PedSector start,
347 PED_ASSERT (dev != NULL);
348 PED_ASSERT (buffer != NULL);
349 PED_ASSERT (!dev->external_mode);
350 PED_ASSERT (dev->open_count > 0);
352 return (ped_architecture->dev_ops->write) (dev, buffer, start, count);
356 ped_device_check (PedDevice* dev, void* buffer, PedSector start,
359 PED_ASSERT (dev != NULL);
360 PED_ASSERT (!dev->external_mode);
361 PED_ASSERT (dev->open_count > 0);
363 return (ped_architecture->dev_ops->check) (dev, buffer, start, count);
367 * \internal Flushes all write-behind caches that might be holding up
369 * It is slow because it guarantees cache coherency among all relevant caches.
371 * \return zero on failure
374 ped_device_sync (PedDevice* dev)
376 PED_ASSERT (dev != NULL);
377 PED_ASSERT (!dev->external_mode);
378 PED_ASSERT (dev->open_count > 0);
380 return ped_architecture->dev_ops->sync (dev);
384 * \internal Flushes all write-behind caches that might be holding writes.
385 * \warning Does NOT ensure cache coherency with other caches.
386 * If you need cache coherency, use ped_device_sync() instead.
388 * \return zero on failure
391 ped_device_sync_fast (PedDevice* dev)
393 PED_ASSERT (dev != NULL);
394 PED_ASSERT (!dev->external_mode);
395 PED_ASSERT (dev->open_count > 0);
397 return ped_architecture->dev_ops->sync_fast (dev);
401 * Get a constraint that represents hardware requirements on geometry.
402 * This function will return a constraint representing the limits imposed
403 * by the size of the disk, it will *not* provide any alignment constraints.
405 * Alignment constraints may be desirable when using media that have a physical
406 * sector size that is a multiple of the logical sector size, as in this case
407 * proper partition alignment can benefit disk performance signigicantly.
408 * When you want a constraint with alignment info, use
409 * ped_device_get_minimal_aligned_constraint() or
410 * ped_device_get_optimal_aligned_constraint().
412 * \return NULL on error, otherwise a pointer to a dynamically allocated
416 ped_device_get_constraint (const PedDevice* dev)
419 PedConstraint* c = ped_constraint_new (
420 ped_alignment_any, ped_alignment_any,
421 s = ped_geometry_new (dev, 0, dev->length),
422 e = ped_geometry_new (dev, 0, dev->length),
430 static PedConstraint*
431 _ped_device_get_aligned_constraint(const PedDevice *dev,
432 PedAlignment* start_align)
434 PedAlignment *end_align = NULL;
435 PedGeometry *whole_dev_geom = NULL;
436 PedConstraint *c = NULL;
439 end_align = ped_alignment_new(start_align->offset - 1,
440 start_align->grain_size);
442 goto free_start_align;
445 whole_dev_geom = ped_geometry_new (dev, 0, dev->length);
448 c = ped_constraint_new (start_align, end_align,
449 whole_dev_geom, whole_dev_geom,
452 c = ped_constraint_new (ped_alignment_any, ped_alignment_any,
453 whole_dev_geom, whole_dev_geom,
456 free (whole_dev_geom);
464 * Get a constraint that represents hardware requirements on geometry and
467 * This function will return a constraint representing the limits imposed
468 * by the size of the disk and the minimal alignment requirements for proper
469 * performance of the disk.
471 * \return NULL on error, otherwise a pointer to a dynamically allocated
475 ped_device_get_minimal_aligned_constraint(const PedDevice *dev)
477 return _ped_device_get_aligned_constraint(dev,
478 ped_device_get_minimum_alignment(dev));
482 * Get a constraint that represents hardware requirements on geometry and
485 * This function will return a constraint representing the limits imposed
486 * by the size of the disk and the alignment requirements for optimal
487 * performance of the disk.
489 * \return NULL on error, otherwise a pointer to a dynamically allocated
493 ped_device_get_optimal_aligned_constraint(const PedDevice *dev)
495 return _ped_device_get_aligned_constraint(dev,
496 ped_device_get_optimum_alignment(dev));
500 * Get an alignment that represents minimum hardware requirements on alignment.
501 * When for example using media that has a physical sector size that is a
502 * multiple of the logical sector size, it is desirable to have disk accesses
503 * (and thus partitions) properly aligned. Having partitions not aligned to
504 * the minimum hardware requirements may lead to a performance penalty.
506 * The returned alignment describes the alignment for the start sector of the
507 * partition, the end sector should be aligned too, to get the end sector
508 * alignment decrease the returned alignment's offset by 1.
510 * \return the minimum alignment of partition start sectors, or NULL if this
511 * information is not available.
514 ped_device_get_minimum_alignment(const PedDevice *dev)
516 PedAlignment *align = NULL;
518 if (ped_architecture->dev_ops->get_minimum_alignment)
519 align = ped_architecture->dev_ops->get_minimum_alignment(dev);
522 align = ped_alignment_new(0,
523 dev->phys_sector_size / dev->sector_size);
529 * Get an alignment that represents the hardware requirements for optimal
532 * The returned alignment describes the alignment for the start sector of the
533 * partition, the end sector should be aligned too, to get the end sector
534 * alignment decrease the returned alignment's offset by 1.
536 * \return the optimal alignment of partition start sectors, or NULL if this
537 * information is not available.
540 ped_device_get_optimum_alignment(const PedDevice *dev)
542 PedAlignment *align = NULL;
544 if (ped_architecture->dev_ops->get_optimum_alignment)
545 align = ped_architecture->dev_ops->get_optimum_alignment(dev);
547 /* If the arch specific code could not give as an alignment
548 return a default value based on the type of device. */
551 case PED_DEVICE_DASD:
552 align = ped_device_get_minimum_alignment(dev);
555 /* Align to a grain of 1MiB (like vista / win7) */
556 align = ped_alignment_new(0,
557 (PED_DEFAULT_ALIGNMENT
558 / dev->sector_size));