]> git.karo-electronics.de Git - karo-tx-linux.git/blob - block/partition-generic.c
block: partition: introduce hd_free_part()
[karo-tx-linux.git] / block / partition-generic.c
1 /*
2  *  Code extracted from drivers/block/genhd.c
3  *  Copyright (C) 1991-1998  Linus Torvalds
4  *  Re-organised Feb 1998 Russell King
5  *
6  *  We now have independent partition support from the
7  *  block drivers, which allows all the partition code to
8  *  be grouped in one location, and it to be mostly self
9  *  contained.
10  */
11
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/fs.h>
15 #include <linux/slab.h>
16 #include <linux/kmod.h>
17 #include <linux/ctype.h>
18 #include <linux/genhd.h>
19 #include <linux/blktrace_api.h>
20
21 #include "partitions/check.h"
22
23 #ifdef CONFIG_BLK_DEV_MD
24 extern void md_autodetect_dev(dev_t dev);
25 #endif
26  
27 /*
28  * disk_name() is used by partition check code and the genhd driver.
29  * It formats the devicename of the indicated disk into
30  * the supplied buffer (of size at least 32), and returns
31  * a pointer to that same buffer (for convenience).
32  */
33
34 char *disk_name(struct gendisk *hd, int partno, char *buf)
35 {
36         if (!partno)
37                 snprintf(buf, BDEVNAME_SIZE, "%s", hd->disk_name);
38         else if (isdigit(hd->disk_name[strlen(hd->disk_name)-1]))
39                 snprintf(buf, BDEVNAME_SIZE, "%sp%d", hd->disk_name, partno);
40         else
41                 snprintf(buf, BDEVNAME_SIZE, "%s%d", hd->disk_name, partno);
42
43         return buf;
44 }
45
46 const char *bdevname(struct block_device *bdev, char *buf)
47 {
48         return disk_name(bdev->bd_disk, bdev->bd_part->partno, buf);
49 }
50
51 EXPORT_SYMBOL(bdevname);
52
53 /*
54  * There's very little reason to use this, you should really
55  * have a struct block_device just about everywhere and use
56  * bdevname() instead.
57  */
58 const char *__bdevname(dev_t dev, char *buffer)
59 {
60         scnprintf(buffer, BDEVNAME_SIZE, "unknown-block(%u,%u)",
61                                 MAJOR(dev), MINOR(dev));
62         return buffer;
63 }
64
65 EXPORT_SYMBOL(__bdevname);
66
67 static ssize_t part_partition_show(struct device *dev,
68                                    struct device_attribute *attr, char *buf)
69 {
70         struct hd_struct *p = dev_to_part(dev);
71
72         return sprintf(buf, "%d\n", p->partno);
73 }
74
75 static ssize_t part_start_show(struct device *dev,
76                                struct device_attribute *attr, char *buf)
77 {
78         struct hd_struct *p = dev_to_part(dev);
79
80         return sprintf(buf, "%llu\n",(unsigned long long)p->start_sect);
81 }
82
83 ssize_t part_size_show(struct device *dev,
84                        struct device_attribute *attr, char *buf)
85 {
86         struct hd_struct *p = dev_to_part(dev);
87         return sprintf(buf, "%llu\n",(unsigned long long)part_nr_sects_read(p));
88 }
89
90 static ssize_t part_ro_show(struct device *dev,
91                             struct device_attribute *attr, char *buf)
92 {
93         struct hd_struct *p = dev_to_part(dev);
94         return sprintf(buf, "%d\n", p->policy ? 1 : 0);
95 }
96
97 static ssize_t part_alignment_offset_show(struct device *dev,
98                                           struct device_attribute *attr, char *buf)
99 {
100         struct hd_struct *p = dev_to_part(dev);
101         return sprintf(buf, "%llu\n", (unsigned long long)p->alignment_offset);
102 }
103
104 static ssize_t part_discard_alignment_show(struct device *dev,
105                                            struct device_attribute *attr, char *buf)
106 {
107         struct hd_struct *p = dev_to_part(dev);
108         return sprintf(buf, "%u\n", p->discard_alignment);
109 }
110
111 ssize_t part_stat_show(struct device *dev,
112                        struct device_attribute *attr, char *buf)
113 {
114         struct hd_struct *p = dev_to_part(dev);
115         int cpu;
116
117         cpu = part_stat_lock();
118         part_round_stats(cpu, p);
119         part_stat_unlock();
120         return sprintf(buf,
121                 "%8lu %8lu %8llu %8u "
122                 "%8lu %8lu %8llu %8u "
123                 "%8u %8u %8u"
124                 "\n",
125                 part_stat_read(p, ios[READ]),
126                 part_stat_read(p, merges[READ]),
127                 (unsigned long long)part_stat_read(p, sectors[READ]),
128                 jiffies_to_msecs(part_stat_read(p, ticks[READ])),
129                 part_stat_read(p, ios[WRITE]),
130                 part_stat_read(p, merges[WRITE]),
131                 (unsigned long long)part_stat_read(p, sectors[WRITE]),
132                 jiffies_to_msecs(part_stat_read(p, ticks[WRITE])),
133                 part_in_flight(p),
134                 jiffies_to_msecs(part_stat_read(p, io_ticks)),
135                 jiffies_to_msecs(part_stat_read(p, time_in_queue)));
136 }
137
138 ssize_t part_inflight_show(struct device *dev,
139                         struct device_attribute *attr, char *buf)
140 {
141         struct hd_struct *p = dev_to_part(dev);
142
143         return sprintf(buf, "%8u %8u\n", atomic_read(&p->in_flight[0]),
144                 atomic_read(&p->in_flight[1]));
145 }
146
147 #ifdef CONFIG_FAIL_MAKE_REQUEST
148 ssize_t part_fail_show(struct device *dev,
149                        struct device_attribute *attr, char *buf)
150 {
151         struct hd_struct *p = dev_to_part(dev);
152
153         return sprintf(buf, "%d\n", p->make_it_fail);
154 }
155
156 ssize_t part_fail_store(struct device *dev,
157                         struct device_attribute *attr,
158                         const char *buf, size_t count)
159 {
160         struct hd_struct *p = dev_to_part(dev);
161         int i;
162
163         if (count > 0 && sscanf(buf, "%d", &i) > 0)
164                 p->make_it_fail = (i == 0) ? 0 : 1;
165
166         return count;
167 }
168 #endif
169
170 static DEVICE_ATTR(partition, S_IRUGO, part_partition_show, NULL);
171 static DEVICE_ATTR(start, S_IRUGO, part_start_show, NULL);
172 static DEVICE_ATTR(size, S_IRUGO, part_size_show, NULL);
173 static DEVICE_ATTR(ro, S_IRUGO, part_ro_show, NULL);
174 static DEVICE_ATTR(alignment_offset, S_IRUGO, part_alignment_offset_show, NULL);
175 static DEVICE_ATTR(discard_alignment, S_IRUGO, part_discard_alignment_show,
176                    NULL);
177 static DEVICE_ATTR(stat, S_IRUGO, part_stat_show, NULL);
178 static DEVICE_ATTR(inflight, S_IRUGO, part_inflight_show, NULL);
179 #ifdef CONFIG_FAIL_MAKE_REQUEST
180 static struct device_attribute dev_attr_fail =
181         __ATTR(make-it-fail, S_IRUGO|S_IWUSR, part_fail_show, part_fail_store);
182 #endif
183
184 static struct attribute *part_attrs[] = {
185         &dev_attr_partition.attr,
186         &dev_attr_start.attr,
187         &dev_attr_size.attr,
188         &dev_attr_ro.attr,
189         &dev_attr_alignment_offset.attr,
190         &dev_attr_discard_alignment.attr,
191         &dev_attr_stat.attr,
192         &dev_attr_inflight.attr,
193 #ifdef CONFIG_FAIL_MAKE_REQUEST
194         &dev_attr_fail.attr,
195 #endif
196         NULL
197 };
198
199 static struct attribute_group part_attr_group = {
200         .attrs = part_attrs,
201 };
202
203 static const struct attribute_group *part_attr_groups[] = {
204         &part_attr_group,
205 #ifdef CONFIG_BLK_DEV_IO_TRACE
206         &blk_trace_attr_group,
207 #endif
208         NULL
209 };
210
211 static void part_release(struct device *dev)
212 {
213         struct hd_struct *p = dev_to_part(dev);
214         blk_free_devt(dev->devt);
215         hd_free_part(p);
216         kfree(p);
217 }
218
219 struct device_type part_type = {
220         .name           = "partition",
221         .groups         = part_attr_groups,
222         .release        = part_release,
223 };
224
225 static void delete_partition_rcu_cb(struct rcu_head *head)
226 {
227         struct hd_struct *part = container_of(head, struct hd_struct, rcu_head);
228
229         part->start_sect = 0;
230         part->nr_sects = 0;
231         part_stat_set_all(part, 0);
232         put_device(part_to_dev(part));
233 }
234
235 void __delete_partition(struct hd_struct *part)
236 {
237         call_rcu(&part->rcu_head, delete_partition_rcu_cb);
238 }
239
240 void delete_partition(struct gendisk *disk, int partno)
241 {
242         struct disk_part_tbl *ptbl = disk->part_tbl;
243         struct hd_struct *part;
244
245         if (partno >= ptbl->len)
246                 return;
247
248         part = ptbl->part[partno];
249         if (!part)
250                 return;
251
252         rcu_assign_pointer(ptbl->part[partno], NULL);
253         rcu_assign_pointer(ptbl->last_lookup, NULL);
254         kobject_put(part->holder_dir);
255         device_del(part_to_dev(part));
256
257         hd_struct_put(part);
258 }
259
260 static ssize_t whole_disk_show(struct device *dev,
261                                struct device_attribute *attr, char *buf)
262 {
263         return 0;
264 }
265 static DEVICE_ATTR(whole_disk, S_IRUSR | S_IRGRP | S_IROTH,
266                    whole_disk_show, NULL);
267
268 struct hd_struct *add_partition(struct gendisk *disk, int partno,
269                                 sector_t start, sector_t len, int flags,
270                                 struct partition_meta_info *info)
271 {
272         struct hd_struct *p;
273         dev_t devt = MKDEV(0, 0);
274         struct device *ddev = disk_to_dev(disk);
275         struct device *pdev;
276         struct disk_part_tbl *ptbl;
277         const char *dname;
278         int err;
279
280         err = disk_expand_part_tbl(disk, partno);
281         if (err)
282                 return ERR_PTR(err);
283         ptbl = disk->part_tbl;
284
285         if (ptbl->part[partno])
286                 return ERR_PTR(-EBUSY);
287
288         p = kzalloc(sizeof(*p), GFP_KERNEL);
289         if (!p)
290                 return ERR_PTR(-EBUSY);
291
292         if (!init_part_stats(p)) {
293                 err = -ENOMEM;
294                 goto out_free;
295         }
296
297         seqcount_init(&p->nr_sects_seq);
298         pdev = part_to_dev(p);
299
300         p->start_sect = start;
301         p->alignment_offset =
302                 queue_limit_alignment_offset(&disk->queue->limits, start);
303         p->discard_alignment =
304                 queue_limit_discard_alignment(&disk->queue->limits, start);
305         p->nr_sects = len;
306         p->partno = partno;
307         p->policy = get_disk_ro(disk);
308
309         if (info) {
310                 struct partition_meta_info *pinfo = alloc_part_info(disk);
311                 if (!pinfo)
312                         goto out_free_stats;
313                 memcpy(pinfo, info, sizeof(*info));
314                 p->info = pinfo;
315         }
316
317         dname = dev_name(ddev);
318         if (isdigit(dname[strlen(dname) - 1]))
319                 dev_set_name(pdev, "%sp%d", dname, partno);
320         else
321                 dev_set_name(pdev, "%s%d", dname, partno);
322
323         device_initialize(pdev);
324         pdev->class = &block_class;
325         pdev->type = &part_type;
326         pdev->parent = ddev;
327
328         err = blk_alloc_devt(p, &devt);
329         if (err)
330                 goto out_free_info;
331         pdev->devt = devt;
332
333         /* delay uevent until 'holders' subdir is created */
334         dev_set_uevent_suppress(pdev, 1);
335         err = device_add(pdev);
336         if (err)
337                 goto out_put;
338
339         err = -ENOMEM;
340         p->holder_dir = kobject_create_and_add("holders", &pdev->kobj);
341         if (!p->holder_dir)
342                 goto out_del;
343
344         dev_set_uevent_suppress(pdev, 0);
345         if (flags & ADDPART_FLAG_WHOLEDISK) {
346                 err = device_create_file(pdev, &dev_attr_whole_disk);
347                 if (err)
348                         goto out_del;
349         }
350
351         /* everything is up and running, commence */
352         rcu_assign_pointer(ptbl->part[partno], p);
353
354         /* suppress uevent if the disk suppresses it */
355         if (!dev_get_uevent_suppress(ddev))
356                 kobject_uevent(&pdev->kobj, KOBJ_ADD);
357
358         hd_ref_init(p);
359         return p;
360
361 out_free_info:
362         free_part_info(p);
363 out_free_stats:
364         free_part_stats(p);
365 out_free:
366         kfree(p);
367         return ERR_PTR(err);
368 out_del:
369         kobject_put(p->holder_dir);
370         device_del(pdev);
371 out_put:
372         put_device(pdev);
373         blk_free_devt(devt);
374         return ERR_PTR(err);
375 }
376
377 static bool disk_unlock_native_capacity(struct gendisk *disk)
378 {
379         const struct block_device_operations *bdops = disk->fops;
380
381         if (bdops->unlock_native_capacity &&
382             !(disk->flags & GENHD_FL_NATIVE_CAPACITY)) {
383                 printk(KERN_CONT "enabling native capacity\n");
384                 bdops->unlock_native_capacity(disk);
385                 disk->flags |= GENHD_FL_NATIVE_CAPACITY;
386                 return true;
387         } else {
388                 printk(KERN_CONT "truncated\n");
389                 return false;
390         }
391 }
392
393 static int drop_partitions(struct gendisk *disk, struct block_device *bdev)
394 {
395         struct disk_part_iter piter;
396         struct hd_struct *part;
397         int res;
398
399         if (bdev->bd_part_count)
400                 return -EBUSY;
401         res = invalidate_partition(disk, 0);
402         if (res)
403                 return res;
404
405         disk_part_iter_init(&piter, disk, DISK_PITER_INCL_EMPTY);
406         while ((part = disk_part_iter_next(&piter)))
407                 delete_partition(disk, part->partno);
408         disk_part_iter_exit(&piter);
409
410         return 0;
411 }
412
413 int rescan_partitions(struct gendisk *disk, struct block_device *bdev)
414 {
415         struct parsed_partitions *state = NULL;
416         struct hd_struct *part;
417         int p, highest, res;
418 rescan:
419         if (state && !IS_ERR(state)) {
420                 free_partitions(state);
421                 state = NULL;
422         }
423
424         res = drop_partitions(disk, bdev);
425         if (res)
426                 return res;
427
428         if (disk->fops->revalidate_disk)
429                 disk->fops->revalidate_disk(disk);
430         check_disk_size_change(disk, bdev);
431         bdev->bd_invalidated = 0;
432         if (!get_capacity(disk) || !(state = check_partition(disk, bdev)))
433                 return 0;
434         if (IS_ERR(state)) {
435                 /*
436                  * I/O error reading the partition table.  If any
437                  * partition code tried to read beyond EOD, retry
438                  * after unlocking native capacity.
439                  */
440                 if (PTR_ERR(state) == -ENOSPC) {
441                         printk(KERN_WARNING "%s: partition table beyond EOD, ",
442                                disk->disk_name);
443                         if (disk_unlock_native_capacity(disk))
444                                 goto rescan;
445                 }
446                 return -EIO;
447         }
448         /*
449          * If any partition code tried to read beyond EOD, try
450          * unlocking native capacity even if partition table is
451          * successfully read as we could be missing some partitions.
452          */
453         if (state->access_beyond_eod) {
454                 printk(KERN_WARNING
455                        "%s: partition table partially beyond EOD, ",
456                        disk->disk_name);
457                 if (disk_unlock_native_capacity(disk))
458                         goto rescan;
459         }
460
461         /* tell userspace that the media / partition table may have changed */
462         kobject_uevent(&disk_to_dev(disk)->kobj, KOBJ_CHANGE);
463
464         /* Detect the highest partition number and preallocate
465          * disk->part_tbl.  This is an optimization and not strictly
466          * necessary.
467          */
468         for (p = 1, highest = 0; p < state->limit; p++)
469                 if (state->parts[p].size)
470                         highest = p;
471
472         disk_expand_part_tbl(disk, highest);
473
474         /* add partitions */
475         for (p = 1; p < state->limit; p++) {
476                 sector_t size, from;
477                 struct partition_meta_info *info = NULL;
478
479                 size = state->parts[p].size;
480                 if (!size)
481                         continue;
482
483                 from = state->parts[p].from;
484                 if (from >= get_capacity(disk)) {
485                         printk(KERN_WARNING
486                                "%s: p%d start %llu is beyond EOD, ",
487                                disk->disk_name, p, (unsigned long long) from);
488                         if (disk_unlock_native_capacity(disk))
489                                 goto rescan;
490                         continue;
491                 }
492
493                 if (from + size > get_capacity(disk)) {
494                         printk(KERN_WARNING
495                                "%s: p%d size %llu extends beyond EOD, ",
496                                disk->disk_name, p, (unsigned long long) size);
497
498                         if (disk_unlock_native_capacity(disk)) {
499                                 /* free state and restart */
500                                 goto rescan;
501                         } else {
502                                 /*
503                                  * we can not ignore partitions of broken tables
504                                  * created by for example camera firmware, but
505                                  * we limit them to the end of the disk to avoid
506                                  * creating invalid block devices
507                                  */
508                                 size = get_capacity(disk) - from;
509                         }
510                 }
511
512                 if (state->parts[p].has_info)
513                         info = &state->parts[p].info;
514                 part = add_partition(disk, p, from, size,
515                                      state->parts[p].flags,
516                                      &state->parts[p].info);
517                 if (IS_ERR(part)) {
518                         printk(KERN_ERR " %s: p%d could not be added: %ld\n",
519                                disk->disk_name, p, -PTR_ERR(part));
520                         continue;
521                 }
522 #ifdef CONFIG_BLK_DEV_MD
523                 if (state->parts[p].flags & ADDPART_FLAG_RAID)
524                         md_autodetect_dev(part_to_dev(part)->devt);
525 #endif
526         }
527         free_partitions(state);
528         return 0;
529 }
530
531 int invalidate_partitions(struct gendisk *disk, struct block_device *bdev)
532 {
533         int res;
534
535         if (!bdev->bd_invalidated)
536                 return 0;
537
538         res = drop_partitions(disk, bdev);
539         if (res)
540                 return res;
541
542         set_capacity(disk, 0);
543         check_disk_size_change(disk, bdev);
544         bdev->bd_invalidated = 0;
545         /* tell userspace that the media / partition table may have changed */
546         kobject_uevent(&disk_to_dev(disk)->kobj, KOBJ_CHANGE);
547
548         return 0;
549 }
550
551 unsigned char *read_dev_sector(struct block_device *bdev, sector_t n, Sector *p)
552 {
553         struct address_space *mapping = bdev->bd_inode->i_mapping;
554         struct page *page;
555
556         page = read_mapping_page(mapping, (pgoff_t)(n >> (PAGE_CACHE_SHIFT-9)),
557                                  NULL);
558         if (!IS_ERR(page)) {
559                 if (PageError(page))
560                         goto fail;
561                 p->v = page;
562                 return (unsigned char *)page_address(page) +  ((n & ((1 << (PAGE_CACHE_SHIFT - 9)) - 1)) << 9);
563 fail:
564                 page_cache_release(page);
565         }
566         p->v = NULL;
567         return NULL;
568 }
569
570 EXPORT_SYMBOL(read_dev_sector);