OSDN Git Service

parted.h: use _GL_ATTRIBUTE_CONST and _GL_ATTRIBUTE_PURE in VC'd files
[android-x86/external-parted.git] / libparted / device.c
1 /*
2     libparted - a library for manipulating disk partitions
3     Copyright (C) 1999 - 2001, 2005, 2007-2010 Free Software Foundation, Inc.
4
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.
9
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.
14
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/>.
17 */
18
19 /** \file device.c */
20
21 /**
22  * \addtogroup PedDevice
23  *
24  * \brief Device access.
25  *
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().
29  *
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().
34  *
35  * @{
36  */
37
38 #include <config.h>
39
40 #include <parted/parted.h>
41 #include <parted/debug.h>
42
43 #include <limits.h>
44 #include <unistd.h>
45 #include <stdlib.h>
46 #include <errno.h>
47
48 #include "architecture.h"
49
50 static PedDevice*       devices; /* legal advice says: initialized to NULL,
51                                     under section 6.7.8 part 10
52                                     of ISO/EIC 9899:1999 */
53
54 static void
55 _device_register (PedDevice* dev)
56 {
57         PedDevice*      walk;
58         for (walk = devices; walk && walk->next; walk = walk->next);
59         if (walk)
60                 walk->next = dev;
61         else
62                 devices = dev;
63         dev->next = NULL;
64 }
65
66 static void
67 _device_unregister (PedDevice* dev)
68 {
69         PedDevice*      walk;
70         PedDevice*      last = NULL;
71
72         for (walk = devices; walk != NULL; last = walk, walk = walk->next) {
73                 if (walk == dev) break;
74         }
75
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
80            ped_device_destroy().
81            ped_device_destroy() will then call us a second time, so if the
82            device is not found in the list do nothing. */
83         if (walk == NULL)
84                 return;
85
86         if (last)
87                 last->next = dev->next;
88         else
89                 devices = dev->next;
90 }
91
92 /**
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.
96  *
97  * \return NULL if dev is the last device.
98  */
99 PedDevice*
100 ped_device_get_next (const PedDevice* dev)
101 {
102         if (dev)
103                 return dev->next;
104         else
105                 return devices;
106 }
107
108 void
109 _ped_device_probe (const char* path)
110 {
111         PedDevice*      dev;
112
113         PED_ASSERT (path != NULL);
114
115         ped_exception_fetch_all ();
116         dev = ped_device_get (path);
117         if (!dev)
118                 ped_exception_catch ();
119         ped_exception_leave_all ();
120 }
121
122 /**
123  * Attempts to detect all devices.
124  */
125 void
126 ped_device_probe_all ()
127 {
128         ped_architecture->dev_ops->probe_all ();
129 }
130
131 /**
132  * Close/free all devices.
133  * Called by ped_done(), so you do not need to worry about it.
134  */
135 void
136 ped_device_free_all ()
137 {
138         while (devices)
139                 ped_device_destroy (devices);
140 }
141
142 /**
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.
147  */
148 PedDevice*
149 ped_device_get (const char* path)
150 {
151         PedDevice*      walk;
152         char*           normal_path = NULL;
153
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);
158         if (!normal_path)
159                 /* Well, maybe it is just that the file does not exist.
160                  * Try it anyway.  */
161                 normal_path = strdup (path);
162         if (!normal_path)
163                 return NULL;
164
165         for (walk = devices; walk != NULL; walk = walk->next) {
166                 if (!strcmp (walk->path, normal_path)) {
167                         free (normal_path);
168                         return walk;
169                 }
170         }
171
172         walk = ped_architecture->dev_ops->_new (normal_path);
173         free (normal_path);
174         if (!walk)
175                 return NULL;
176         _device_register (walk);
177         return walk;
178 }
179
180 /**
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).
184  */
185 void
186 ped_device_destroy (PedDevice* dev)
187 {
188         _device_unregister (dev);
189
190         while (dev->open_count) {
191                 if (!ped_device_close (dev))
192                         break;
193         }
194
195         ped_architecture->dev_ops->destroy (dev);
196 }
197
198 void
199 ped_device_cache_remove(PedDevice *dev)
200 {
201         _device_unregister (dev);
202 }
203
204 int
205 ped_device_is_busy (PedDevice* dev)
206 {
207         return ped_architecture->dev_ops->is_busy (dev);
208 }
209
210 /**
211  * Attempt to open a device to allow use of read, write and sync functions.
212  *
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
215  * caches.
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)
219  *
220  * \return zero on failure
221  */
222 int
223 ped_device_open (PedDevice* dev)
224 {
225         int     status;
226
227         PED_ASSERT (dev != NULL);
228         PED_ASSERT (!dev->external_mode);
229
230         if (dev->open_count)
231                 status = ped_architecture->dev_ops->refresh_open (dev);
232         else
233                 status = ped_architecture->dev_ops->open (dev);
234         if (status)
235                 dev->open_count++;
236         return status;
237 }
238
239 /**
240  * Close dev.
241  * If this is the final close, then resources allocated by
242  * ped_device_open() are freed.
243  *
244  * \return zero on failure
245  */
246 int
247 ped_device_close (PedDevice* dev)
248 {
249         PED_ASSERT (dev != NULL);
250         PED_ASSERT (!dev->external_mode);
251         PED_ASSERT (dev->open_count > 0);
252
253         if (--dev->open_count)
254                 return ped_architecture->dev_ops->refresh_close (dev);
255         else
256                 return ped_architecture->dev_ops->close (dev);
257 }
258
259 /**
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.
271  *
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.)
277  *
278  * In particular, this function keeps track of dev->open_count, so that
279  * reference counting isn't screwed up.
280  *
281  * \return zero on failure.
282  */
283 int
284 ped_device_begin_external_access (PedDevice* dev)
285 {
286         PED_ASSERT (dev != NULL);
287         PED_ASSERT (!dev->external_mode);
288
289         dev->external_mode = 1;
290         if (dev->open_count)
291                 return ped_architecture->dev_ops->close (dev);
292         else
293                 return 1;
294 }
295
296 /**
297  * \brief Complementary function to ped_device_begin_external_access.
298  *
299  * \note does things like tell the kernel to flush the device's cache.
300  *
301  * \return zero on failure.
302  */
303 int
304 ped_device_end_external_access (PedDevice* dev)
305 {
306         PED_ASSERT (dev != NULL);
307         PED_ASSERT (dev->external_mode);
308
309         dev->external_mode = 0;
310         if (dev->open_count)
311                 return ped_architecture->dev_ops->open (dev);
312         else
313                 return 1;
314 }
315
316 /**
317  * \internal Read count sectors from dev into buffer, beginning with sector
318  * start.
319  *
320  * \return zero on failure.
321  */
322 int
323 ped_device_read (const PedDevice* dev, void* buffer, PedSector start,
324                  PedSector count)
325 {
326         PED_ASSERT (dev != NULL);
327         PED_ASSERT (buffer != NULL);
328         PED_ASSERT (!dev->external_mode);
329         PED_ASSERT (dev->open_count > 0);
330
331         return (ped_architecture->dev_ops->read) (dev, buffer, start, count);
332 }
333
334 /**
335  * \internal Write count sectors from buffer to dev, starting at sector
336  * start.
337  *
338  * \return zero on failure.
339  *
340  * \sa PedDevice::sector_size
341  * \sa PedDevice::phys_sector_size
342  */
343 int
344 ped_device_write (PedDevice* dev, const void* buffer, PedSector start,
345                   PedSector count)
346 {
347         PED_ASSERT (dev != NULL);
348         PED_ASSERT (buffer != NULL);
349         PED_ASSERT (!dev->external_mode);
350         PED_ASSERT (dev->open_count > 0);
351
352         return (ped_architecture->dev_ops->write) (dev, buffer, start, count);
353 }
354
355 PedSector
356 ped_device_check (PedDevice* dev, void* buffer, PedSector start,
357                   PedSector count)
358 {
359         PED_ASSERT (dev != NULL);
360         PED_ASSERT (!dev->external_mode);
361         PED_ASSERT (dev->open_count > 0);
362
363         return (ped_architecture->dev_ops->check) (dev, buffer, start, count);
364 }
365
366 /**
367  * \internal Flushes all write-behind caches that might be holding up
368  * writes.
369  * It is slow because it guarantees cache coherency among all relevant caches.
370  *
371  * \return zero on failure
372  */
373 int
374 ped_device_sync (PedDevice* dev)
375 {
376         PED_ASSERT (dev != NULL);
377         PED_ASSERT (!dev->external_mode);
378         PED_ASSERT (dev->open_count > 0);
379
380         return ped_architecture->dev_ops->sync (dev);
381 }
382
383 /**
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.
387  *
388  * \return zero on failure
389  */
390 int
391 ped_device_sync_fast (PedDevice* dev)
392 {
393         PED_ASSERT (dev != NULL);
394         PED_ASSERT (!dev->external_mode);
395         PED_ASSERT (dev->open_count > 0);
396
397         return ped_architecture->dev_ops->sync_fast (dev);
398 }
399
400 /**
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.
404  *
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().
411  *
412  * \return NULL on error, otherwise a pointer to a dynamically allocated
413  *         constraint.
414  */
415 PedConstraint*
416 ped_device_get_constraint (const PedDevice* dev)
417 {
418         PedGeometry *s, *e;
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),
423                                 1, dev->length);
424
425         free (s);
426         free (e);
427         return c;
428 }
429
430 static PedConstraint*
431 _ped_device_get_aligned_constraint(const PedDevice *dev,
432                                    PedAlignment* start_align)
433 {
434         PedAlignment *end_align = NULL;
435         PedGeometry *whole_dev_geom = NULL;
436         PedConstraint *c = NULL;
437
438         if (start_align) {
439                 end_align = ped_alignment_new(start_align->offset - 1,
440                                               start_align->grain_size);
441                 if (!end_align)
442                         goto free_start_align;
443         }
444
445         whole_dev_geom = ped_geometry_new (dev, 0, dev->length);
446
447         if (start_align)
448                 c =  ped_constraint_new (start_align, end_align,
449                                          whole_dev_geom, whole_dev_geom,
450                                          1, dev->length);
451         else
452                 c =  ped_constraint_new (ped_alignment_any, ped_alignment_any,
453                                          whole_dev_geom, whole_dev_geom,
454                                          1, dev->length);
455
456         free (whole_dev_geom);
457         free (end_align);
458 free_start_align:
459         free (start_align);
460         return c;
461 }
462
463 /**
464  * Get a constraint that represents hardware requirements on geometry and
465  * alignment.
466  *
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.
470  *
471  * \return NULL on error, otherwise a pointer to a dynamically allocated
472  *         constraint.
473  */
474 PedConstraint*
475 ped_device_get_minimal_aligned_constraint(const PedDevice *dev)
476 {
477         return _ped_device_get_aligned_constraint(dev,
478                                          ped_device_get_minimum_alignment(dev));
479 }
480
481 /**
482  * Get a constraint that represents hardware requirements on geometry and
483  * alignment.
484  *
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.
488  *
489  * \return NULL on error, otherwise a pointer to a dynamically allocated
490  *         constraint.
491  */
492 PedConstraint*
493 ped_device_get_optimal_aligned_constraint(const PedDevice *dev)
494 {
495         return _ped_device_get_aligned_constraint(dev,
496                                          ped_device_get_optimum_alignment(dev));
497 }
498
499 /**
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.
505  *
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.
509  *
510  * \return the minimum alignment of partition start sectors, or NULL if this
511  *         information is not available.
512  */
513 PedAlignment*
514 ped_device_get_minimum_alignment(const PedDevice *dev)
515 {
516         PedAlignment *align = NULL;
517
518         if (ped_architecture->dev_ops->get_minimum_alignment)
519                 align = ped_architecture->dev_ops->get_minimum_alignment(dev);
520
521         if (align == NULL)
522                 align = ped_alignment_new(0,
523                                 dev->phys_sector_size / dev->sector_size);
524
525         return align;
526 }
527
528 /**
529  * Get an alignment that represents the hardware requirements for optimal
530  * performance.
531  *
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.
535  *
536  * \return the optimal alignment of partition start sectors, or NULL if this
537  *         information is not available.
538  */
539 PedAlignment*
540 ped_device_get_optimum_alignment(const PedDevice *dev)
541 {
542         PedAlignment *align = NULL;
543
544         if (ped_architecture->dev_ops->get_optimum_alignment)
545                 align = ped_architecture->dev_ops->get_optimum_alignment(dev);
546
547         /* If the arch specific code could not give as an alignment
548            return a default value based on the type of device. */
549         if (align == NULL) {
550                 switch (dev->type) {
551                 case PED_DEVICE_DASD:
552                         align = ped_device_get_minimum_alignment(dev);
553                         break;
554                 default:
555                         /* Align to a grain of 1MiB (like vista / win7) */
556                         align = ped_alignment_new(0,
557                                                   (PED_DEFAULT_ALIGNMENT
558                                                    / dev->sector_size));
559                 }
560         }
561
562         return align;
563 }
564
565 /** @} */