Btrfs: don't allow WRITE_SYNC bios to starve out regular writes
[safe/jmp/linux-2.6] / fs / btrfs / volumes.c
1 /*
2  * Copyright (C) 2007 Oracle.  All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public
6  * License v2 as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public
14  * License along with this program; if not, write to the
15  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16  * Boston, MA 021110-1307, USA.
17  */
18 #include <linux/sched.h>
19 #include <linux/bio.h>
20 #include <linux/buffer_head.h>
21 #include <linux/blkdev.h>
22 #include <linux/random.h>
23 #include <linux/iocontext.h>
24 #include <asm/div64.h>
25 #include "compat.h"
26 #include "ctree.h"
27 #include "extent_map.h"
28 #include "disk-io.h"
29 #include "transaction.h"
30 #include "print-tree.h"
31 #include "volumes.h"
32 #include "async-thread.h"
33
34 struct map_lookup {
35         u64 type;
36         int io_align;
37         int io_width;
38         int stripe_len;
39         int sector_size;
40         int num_stripes;
41         int sub_stripes;
42         struct btrfs_bio_stripe stripes[];
43 };
44
45 static int init_first_rw_device(struct btrfs_trans_handle *trans,
46                                 struct btrfs_root *root,
47                                 struct btrfs_device *device);
48 static int btrfs_relocate_sys_chunks(struct btrfs_root *root);
49
50 #define map_lookup_size(n) (sizeof(struct map_lookup) + \
51                             (sizeof(struct btrfs_bio_stripe) * (n)))
52
53 static DEFINE_MUTEX(uuid_mutex);
54 static LIST_HEAD(fs_uuids);
55
56 void btrfs_lock_volumes(void)
57 {
58         mutex_lock(&uuid_mutex);
59 }
60
61 void btrfs_unlock_volumes(void)
62 {
63         mutex_unlock(&uuid_mutex);
64 }
65
66 static void lock_chunks(struct btrfs_root *root)
67 {
68         mutex_lock(&root->fs_info->chunk_mutex);
69 }
70
71 static void unlock_chunks(struct btrfs_root *root)
72 {
73         mutex_unlock(&root->fs_info->chunk_mutex);
74 }
75
76 static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
77 {
78         struct btrfs_device *device;
79         WARN_ON(fs_devices->opened);
80         while (!list_empty(&fs_devices->devices)) {
81                 device = list_entry(fs_devices->devices.next,
82                                     struct btrfs_device, dev_list);
83                 list_del(&device->dev_list);
84                 kfree(device->name);
85                 kfree(device);
86         }
87         kfree(fs_devices);
88 }
89
90 int btrfs_cleanup_fs_uuids(void)
91 {
92         struct btrfs_fs_devices *fs_devices;
93
94         while (!list_empty(&fs_uuids)) {
95                 fs_devices = list_entry(fs_uuids.next,
96                                         struct btrfs_fs_devices, list);
97                 list_del(&fs_devices->list);
98                 free_fs_devices(fs_devices);
99         }
100         return 0;
101 }
102
103 static noinline struct btrfs_device *__find_device(struct list_head *head,
104                                                    u64 devid, u8 *uuid)
105 {
106         struct btrfs_device *dev;
107
108         list_for_each_entry(dev, head, dev_list) {
109                 if (dev->devid == devid &&
110                     (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
111                         return dev;
112                 }
113         }
114         return NULL;
115 }
116
117 static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
118 {
119         struct btrfs_fs_devices *fs_devices;
120
121         list_for_each_entry(fs_devices, &fs_uuids, list) {
122                 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
123                         return fs_devices;
124         }
125         return NULL;
126 }
127
128 static void requeue_list(struct btrfs_pending_bios *pending_bios,
129                         struct bio *head, struct bio *tail)
130 {
131
132         struct bio *old_head;
133
134         old_head = pending_bios->head;
135         pending_bios->head = head;
136         if (pending_bios->tail)
137                 tail->bi_next = old_head;
138         else
139                 pending_bios->tail = tail;
140 }
141
142 /*
143  * we try to collect pending bios for a device so we don't get a large
144  * number of procs sending bios down to the same device.  This greatly
145  * improves the schedulers ability to collect and merge the bios.
146  *
147  * But, it also turns into a long list of bios to process and that is sure
148  * to eventually make the worker thread block.  The solution here is to
149  * make some progress and then put this work struct back at the end of
150  * the list if the block device is congested.  This way, multiple devices
151  * can make progress from a single worker thread.
152  */
153 static noinline int run_scheduled_bios(struct btrfs_device *device)
154 {
155         struct bio *pending;
156         struct backing_dev_info *bdi;
157         struct btrfs_fs_info *fs_info;
158         struct btrfs_pending_bios *pending_bios;
159         struct bio *tail;
160         struct bio *cur;
161         int again = 0;
162         unsigned long num_run;
163         unsigned long num_sync_run;
164         unsigned long limit;
165         unsigned long last_waited = 0;
166         int force_reg = 0;
167
168         bdi = blk_get_backing_dev_info(device->bdev);
169         fs_info = device->dev_root->fs_info;
170         limit = btrfs_async_submit_limit(fs_info);
171         limit = limit * 2 / 3;
172
173         /* we want to make sure that every time we switch from the sync
174          * list to the normal list, we unplug
175          */
176         num_sync_run = 0;
177
178 loop:
179         spin_lock(&device->io_lock);
180
181 loop_lock:
182         num_run = 0;
183
184         /* take all the bios off the list at once and process them
185          * later on (without the lock held).  But, remember the
186          * tail and other pointers so the bios can be properly reinserted
187          * into the list if we hit congestion
188          */
189         if (!force_reg && device->pending_sync_bios.head) {
190                 pending_bios = &device->pending_sync_bios;
191                 force_reg = 1;
192         } else {
193                 pending_bios = &device->pending_bios;
194                 force_reg = 0;
195         }
196
197         pending = pending_bios->head;
198         tail = pending_bios->tail;
199         WARN_ON(pending && !tail);
200
201         /*
202          * if pending was null this time around, no bios need processing
203          * at all and we can stop.  Otherwise it'll loop back up again
204          * and do an additional check so no bios are missed.
205          *
206          * device->running_pending is used to synchronize with the
207          * schedule_bio code.
208          */
209         if (device->pending_sync_bios.head == NULL &&
210             device->pending_bios.head == NULL) {
211                 again = 0;
212                 device->running_pending = 0;
213         } else {
214                 again = 1;
215                 device->running_pending = 1;
216         }
217
218         pending_bios->head = NULL;
219         pending_bios->tail = NULL;
220
221         spin_unlock(&device->io_lock);
222
223         /*
224          * if we're doing the regular priority list, make sure we unplug
225          * for any high prio bios we've sent down
226          */
227         if (pending_bios == &device->pending_bios && num_sync_run > 0) {
228                 num_sync_run = 0;
229                 blk_run_backing_dev(bdi, NULL);
230         }
231
232         while (pending) {
233
234                 rmb();
235                 /* we want to work on both lists, but do more bios on the
236                  * sync list than the regular list
237                  */
238                 if ((num_run > 32 &&
239                     pending_bios != &device->pending_sync_bios &&
240                     device->pending_sync_bios.head) ||
241                    (num_run > 64 && pending_bios == &device->pending_sync_bios &&
242                     device->pending_bios.head)) {
243                         spin_lock(&device->io_lock);
244                         requeue_list(pending_bios, pending, tail);
245                         goto loop_lock;
246                 }
247
248                 cur = pending;
249                 pending = pending->bi_next;
250                 cur->bi_next = NULL;
251                 atomic_dec(&fs_info->nr_async_bios);
252
253                 if (atomic_read(&fs_info->nr_async_bios) < limit &&
254                     waitqueue_active(&fs_info->async_submit_wait))
255                         wake_up(&fs_info->async_submit_wait);
256
257                 BUG_ON(atomic_read(&cur->bi_cnt) == 0);
258                 submit_bio(cur->bi_rw, cur);
259                 num_run++;
260                 if (bio_sync(cur))
261                         num_sync_run++;
262
263                 if (need_resched()) {
264                         if (num_sync_run) {
265                                 blk_run_backing_dev(bdi, NULL);
266                                 num_sync_run = 0;
267                         }
268                         cond_resched();
269                 }
270
271                 /*
272                  * we made progress, there is more work to do and the bdi
273                  * is now congested.  Back off and let other work structs
274                  * run instead
275                  */
276                 if (pending && bdi_write_congested(bdi) && num_run > 16 &&
277                     fs_info->fs_devices->open_devices > 1) {
278                         struct io_context *ioc;
279
280                         ioc = current->io_context;
281
282                         /*
283                          * the main goal here is that we don't want to
284                          * block if we're going to be able to submit
285                          * more requests without blocking.
286                          *
287                          * This code does two great things, it pokes into
288                          * the elevator code from a filesystem _and_
289                          * it makes assumptions about how batching works.
290                          */
291                         if (ioc && ioc->nr_batch_requests > 0 &&
292                             time_before(jiffies, ioc->last_waited + HZ/50UL) &&
293                             (last_waited == 0 ||
294                              ioc->last_waited == last_waited)) {
295                                 /*
296                                  * we want to go through our batch of
297                                  * requests and stop.  So, we copy out
298                                  * the ioc->last_waited time and test
299                                  * against it before looping
300                                  */
301                                 last_waited = ioc->last_waited;
302                                 if (need_resched()) {
303                                         if (num_sync_run) {
304                                                 blk_run_backing_dev(bdi, NULL);
305                                                 num_sync_run = 0;
306                                         }
307                                         cond_resched();
308                                 }
309                                 continue;
310                         }
311                         spin_lock(&device->io_lock);
312                         requeue_list(pending_bios, pending, tail);
313                         device->running_pending = 1;
314
315                         spin_unlock(&device->io_lock);
316                         btrfs_requeue_work(&device->work);
317                         goto done;
318                 }
319         }
320
321         if (num_sync_run) {
322                 num_sync_run = 0;
323                 blk_run_backing_dev(bdi, NULL);
324         }
325
326         cond_resched();
327         if (again)
328                 goto loop;
329
330         spin_lock(&device->io_lock);
331         if (device->pending_bios.head || device->pending_sync_bios.head)
332                 goto loop_lock;
333         spin_unlock(&device->io_lock);
334
335         /*
336          * IO has already been through a long path to get here.  Checksumming,
337          * async helper threads, perhaps compression.  We've done a pretty
338          * good job of collecting a batch of IO and should just unplug
339          * the device right away.
340          *
341          * This will help anyone who is waiting on the IO, they might have
342          * already unplugged, but managed to do so before the bio they
343          * cared about found its way down here.
344          */
345         blk_run_backing_dev(bdi, NULL);
346 done:
347         return 0;
348 }
349
350 static void pending_bios_fn(struct btrfs_work *work)
351 {
352         struct btrfs_device *device;
353
354         device = container_of(work, struct btrfs_device, work);
355         run_scheduled_bios(device);
356 }
357
358 static noinline int device_list_add(const char *path,
359                            struct btrfs_super_block *disk_super,
360                            u64 devid, struct btrfs_fs_devices **fs_devices_ret)
361 {
362         struct btrfs_device *device;
363         struct btrfs_fs_devices *fs_devices;
364         u64 found_transid = btrfs_super_generation(disk_super);
365
366         fs_devices = find_fsid(disk_super->fsid);
367         if (!fs_devices) {
368                 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
369                 if (!fs_devices)
370                         return -ENOMEM;
371                 INIT_LIST_HEAD(&fs_devices->devices);
372                 INIT_LIST_HEAD(&fs_devices->alloc_list);
373                 list_add(&fs_devices->list, &fs_uuids);
374                 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
375                 fs_devices->latest_devid = devid;
376                 fs_devices->latest_trans = found_transid;
377                 device = NULL;
378         } else {
379                 device = __find_device(&fs_devices->devices, devid,
380                                        disk_super->dev_item.uuid);
381         }
382         if (!device) {
383                 if (fs_devices->opened)
384                         return -EBUSY;
385
386                 device = kzalloc(sizeof(*device), GFP_NOFS);
387                 if (!device) {
388                         /* we can safely leave the fs_devices entry around */
389                         return -ENOMEM;
390                 }
391                 device->devid = devid;
392                 device->work.func = pending_bios_fn;
393                 memcpy(device->uuid, disk_super->dev_item.uuid,
394                        BTRFS_UUID_SIZE);
395                 device->barriers = 1;
396                 spin_lock_init(&device->io_lock);
397                 device->name = kstrdup(path, GFP_NOFS);
398                 if (!device->name) {
399                         kfree(device);
400                         return -ENOMEM;
401                 }
402                 INIT_LIST_HEAD(&device->dev_alloc_list);
403                 list_add(&device->dev_list, &fs_devices->devices);
404                 device->fs_devices = fs_devices;
405                 fs_devices->num_devices++;
406         }
407
408         if (found_transid > fs_devices->latest_trans) {
409                 fs_devices->latest_devid = devid;
410                 fs_devices->latest_trans = found_transid;
411         }
412         *fs_devices_ret = fs_devices;
413         return 0;
414 }
415
416 static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
417 {
418         struct btrfs_fs_devices *fs_devices;
419         struct btrfs_device *device;
420         struct btrfs_device *orig_dev;
421
422         fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
423         if (!fs_devices)
424                 return ERR_PTR(-ENOMEM);
425
426         INIT_LIST_HEAD(&fs_devices->devices);
427         INIT_LIST_HEAD(&fs_devices->alloc_list);
428         INIT_LIST_HEAD(&fs_devices->list);
429         fs_devices->latest_devid = orig->latest_devid;
430         fs_devices->latest_trans = orig->latest_trans;
431         memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid));
432
433         list_for_each_entry(orig_dev, &orig->devices, dev_list) {
434                 device = kzalloc(sizeof(*device), GFP_NOFS);
435                 if (!device)
436                         goto error;
437
438                 device->name = kstrdup(orig_dev->name, GFP_NOFS);
439                 if (!device->name)
440                         goto error;
441
442                 device->devid = orig_dev->devid;
443                 device->work.func = pending_bios_fn;
444                 memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid));
445                 device->barriers = 1;
446                 spin_lock_init(&device->io_lock);
447                 INIT_LIST_HEAD(&device->dev_list);
448                 INIT_LIST_HEAD(&device->dev_alloc_list);
449
450                 list_add(&device->dev_list, &fs_devices->devices);
451                 device->fs_devices = fs_devices;
452                 fs_devices->num_devices++;
453         }
454         return fs_devices;
455 error:
456         free_fs_devices(fs_devices);
457         return ERR_PTR(-ENOMEM);
458 }
459
460 int btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
461 {
462         struct btrfs_device *device, *next;
463
464         mutex_lock(&uuid_mutex);
465 again:
466         list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
467                 if (device->in_fs_metadata)
468                         continue;
469
470                 if (device->bdev) {
471                         close_bdev_exclusive(device->bdev, device->mode);
472                         device->bdev = NULL;
473                         fs_devices->open_devices--;
474                 }
475                 if (device->writeable) {
476                         list_del_init(&device->dev_alloc_list);
477                         device->writeable = 0;
478                         fs_devices->rw_devices--;
479                 }
480                 list_del_init(&device->dev_list);
481                 fs_devices->num_devices--;
482                 kfree(device->name);
483                 kfree(device);
484         }
485
486         if (fs_devices->seed) {
487                 fs_devices = fs_devices->seed;
488                 goto again;
489         }
490
491         mutex_unlock(&uuid_mutex);
492         return 0;
493 }
494
495 static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
496 {
497         struct btrfs_device *device;
498
499         if (--fs_devices->opened > 0)
500                 return 0;
501
502         list_for_each_entry(device, &fs_devices->devices, dev_list) {
503                 if (device->bdev) {
504                         close_bdev_exclusive(device->bdev, device->mode);
505                         fs_devices->open_devices--;
506                 }
507                 if (device->writeable) {
508                         list_del_init(&device->dev_alloc_list);
509                         fs_devices->rw_devices--;
510                 }
511
512                 device->bdev = NULL;
513                 device->writeable = 0;
514                 device->in_fs_metadata = 0;
515         }
516         WARN_ON(fs_devices->open_devices);
517         WARN_ON(fs_devices->rw_devices);
518         fs_devices->opened = 0;
519         fs_devices->seeding = 0;
520
521         return 0;
522 }
523
524 int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
525 {
526         struct btrfs_fs_devices *seed_devices = NULL;
527         int ret;
528
529         mutex_lock(&uuid_mutex);
530         ret = __btrfs_close_devices(fs_devices);
531         if (!fs_devices->opened) {
532                 seed_devices = fs_devices->seed;
533                 fs_devices->seed = NULL;
534         }
535         mutex_unlock(&uuid_mutex);
536
537         while (seed_devices) {
538                 fs_devices = seed_devices;
539                 seed_devices = fs_devices->seed;
540                 __btrfs_close_devices(fs_devices);
541                 free_fs_devices(fs_devices);
542         }
543         return ret;
544 }
545
546 static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
547                                 fmode_t flags, void *holder)
548 {
549         struct block_device *bdev;
550         struct list_head *head = &fs_devices->devices;
551         struct btrfs_device *device;
552         struct block_device *latest_bdev = NULL;
553         struct buffer_head *bh;
554         struct btrfs_super_block *disk_super;
555         u64 latest_devid = 0;
556         u64 latest_transid = 0;
557         u64 devid;
558         int seeding = 1;
559         int ret = 0;
560
561         list_for_each_entry(device, head, dev_list) {
562                 if (device->bdev)
563                         continue;
564                 if (!device->name)
565                         continue;
566
567                 bdev = open_bdev_exclusive(device->name, flags, holder);
568                 if (IS_ERR(bdev)) {
569                         printk(KERN_INFO "open %s failed\n", device->name);
570                         goto error;
571                 }
572                 set_blocksize(bdev, 4096);
573
574                 bh = btrfs_read_dev_super(bdev);
575                 if (!bh)
576                         goto error_close;
577
578                 disk_super = (struct btrfs_super_block *)bh->b_data;
579                 devid = le64_to_cpu(disk_super->dev_item.devid);
580                 if (devid != device->devid)
581                         goto error_brelse;
582
583                 if (memcmp(device->uuid, disk_super->dev_item.uuid,
584                            BTRFS_UUID_SIZE))
585                         goto error_brelse;
586
587                 device->generation = btrfs_super_generation(disk_super);
588                 if (!latest_transid || device->generation > latest_transid) {
589                         latest_devid = devid;
590                         latest_transid = device->generation;
591                         latest_bdev = bdev;
592                 }
593
594                 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
595                         device->writeable = 0;
596                 } else {
597                         device->writeable = !bdev_read_only(bdev);
598                         seeding = 0;
599                 }
600
601                 device->bdev = bdev;
602                 device->in_fs_metadata = 0;
603                 device->mode = flags;
604
605                 fs_devices->open_devices++;
606                 if (device->writeable) {
607                         fs_devices->rw_devices++;
608                         list_add(&device->dev_alloc_list,
609                                  &fs_devices->alloc_list);
610                 }
611                 continue;
612
613 error_brelse:
614                 brelse(bh);
615 error_close:
616                 close_bdev_exclusive(bdev, FMODE_READ);
617 error:
618                 continue;
619         }
620         if (fs_devices->open_devices == 0) {
621                 ret = -EIO;
622                 goto out;
623         }
624         fs_devices->seeding = seeding;
625         fs_devices->opened = 1;
626         fs_devices->latest_bdev = latest_bdev;
627         fs_devices->latest_devid = latest_devid;
628         fs_devices->latest_trans = latest_transid;
629         fs_devices->total_rw_bytes = 0;
630 out:
631         return ret;
632 }
633
634 int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
635                        fmode_t flags, void *holder)
636 {
637         int ret;
638
639         mutex_lock(&uuid_mutex);
640         if (fs_devices->opened) {
641                 fs_devices->opened++;
642                 ret = 0;
643         } else {
644                 ret = __btrfs_open_devices(fs_devices, flags, holder);
645         }
646         mutex_unlock(&uuid_mutex);
647         return ret;
648 }
649
650 int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
651                           struct btrfs_fs_devices **fs_devices_ret)
652 {
653         struct btrfs_super_block *disk_super;
654         struct block_device *bdev;
655         struct buffer_head *bh;
656         int ret;
657         u64 devid;
658         u64 transid;
659
660         mutex_lock(&uuid_mutex);
661
662         bdev = open_bdev_exclusive(path, flags, holder);
663
664         if (IS_ERR(bdev)) {
665                 ret = PTR_ERR(bdev);
666                 goto error;
667         }
668
669         ret = set_blocksize(bdev, 4096);
670         if (ret)
671                 goto error_close;
672         bh = btrfs_read_dev_super(bdev);
673         if (!bh) {
674                 ret = -EIO;
675                 goto error_close;
676         }
677         disk_super = (struct btrfs_super_block *)bh->b_data;
678         devid = le64_to_cpu(disk_super->dev_item.devid);
679         transid = btrfs_super_generation(disk_super);
680         if (disk_super->label[0])
681                 printk(KERN_INFO "device label %s ", disk_super->label);
682         else {
683                 /* FIXME, make a readl uuid parser */
684                 printk(KERN_INFO "device fsid %llx-%llx ",
685                        *(unsigned long long *)disk_super->fsid,
686                        *(unsigned long long *)(disk_super->fsid + 8));
687         }
688         printk(KERN_CONT "devid %llu transid %llu %s\n",
689                (unsigned long long)devid, (unsigned long long)transid, path);
690         ret = device_list_add(path, disk_super, devid, fs_devices_ret);
691
692         brelse(bh);
693 error_close:
694         close_bdev_exclusive(bdev, flags);
695 error:
696         mutex_unlock(&uuid_mutex);
697         return ret;
698 }
699
700 /*
701  * this uses a pretty simple search, the expectation is that it is
702  * called very infrequently and that a given device has a small number
703  * of extents
704  */
705 static noinline int find_free_dev_extent(struct btrfs_trans_handle *trans,
706                                          struct btrfs_device *device,
707                                          u64 num_bytes, u64 *start)
708 {
709         struct btrfs_key key;
710         struct btrfs_root *root = device->dev_root;
711         struct btrfs_dev_extent *dev_extent = NULL;
712         struct btrfs_path *path;
713         u64 hole_size = 0;
714         u64 last_byte = 0;
715         u64 search_start = 0;
716         u64 search_end = device->total_bytes;
717         int ret;
718         int slot = 0;
719         int start_found;
720         struct extent_buffer *l;
721
722         path = btrfs_alloc_path();
723         if (!path)
724                 return -ENOMEM;
725         path->reada = 2;
726         start_found = 0;
727
728         /* FIXME use last free of some kind */
729
730         /* we don't want to overwrite the superblock on the drive,
731          * so we make sure to start at an offset of at least 1MB
732          */
733         search_start = max((u64)1024 * 1024, search_start);
734
735         if (root->fs_info->alloc_start + num_bytes <= device->total_bytes)
736                 search_start = max(root->fs_info->alloc_start, search_start);
737
738         key.objectid = device->devid;
739         key.offset = search_start;
740         key.type = BTRFS_DEV_EXTENT_KEY;
741         ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
742         if (ret < 0)
743                 goto error;
744         ret = btrfs_previous_item(root, path, 0, key.type);
745         if (ret < 0)
746                 goto error;
747         l = path->nodes[0];
748         btrfs_item_key_to_cpu(l, &key, path->slots[0]);
749         while (1) {
750                 l = path->nodes[0];
751                 slot = path->slots[0];
752                 if (slot >= btrfs_header_nritems(l)) {
753                         ret = btrfs_next_leaf(root, path);
754                         if (ret == 0)
755                                 continue;
756                         if (ret < 0)
757                                 goto error;
758 no_more_items:
759                         if (!start_found) {
760                                 if (search_start >= search_end) {
761                                         ret = -ENOSPC;
762                                         goto error;
763                                 }
764                                 *start = search_start;
765                                 start_found = 1;
766                                 goto check_pending;
767                         }
768                         *start = last_byte > search_start ?
769                                 last_byte : search_start;
770                         if (search_end <= *start) {
771                                 ret = -ENOSPC;
772                                 goto error;
773                         }
774                         goto check_pending;
775                 }
776                 btrfs_item_key_to_cpu(l, &key, slot);
777
778                 if (key.objectid < device->devid)
779                         goto next;
780
781                 if (key.objectid > device->devid)
782                         goto no_more_items;
783
784                 if (key.offset >= search_start && key.offset > last_byte &&
785                     start_found) {
786                         if (last_byte < search_start)
787                                 last_byte = search_start;
788                         hole_size = key.offset - last_byte;
789                         if (key.offset > last_byte &&
790                             hole_size >= num_bytes) {
791                                 *start = last_byte;
792                                 goto check_pending;
793                         }
794                 }
795                 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
796                         goto next;
797
798                 start_found = 1;
799                 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
800                 last_byte = key.offset + btrfs_dev_extent_length(l, dev_extent);
801 next:
802                 path->slots[0]++;
803                 cond_resched();
804         }
805 check_pending:
806         /* we have to make sure we didn't find an extent that has already
807          * been allocated by the map tree or the original allocation
808          */
809         BUG_ON(*start < search_start);
810
811         if (*start + num_bytes > search_end) {
812                 ret = -ENOSPC;
813                 goto error;
814         }
815         /* check for pending inserts here */
816         ret = 0;
817
818 error:
819         btrfs_free_path(path);
820         return ret;
821 }
822
823 static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
824                           struct btrfs_device *device,
825                           u64 start)
826 {
827         int ret;
828         struct btrfs_path *path;
829         struct btrfs_root *root = device->dev_root;
830         struct btrfs_key key;
831         struct btrfs_key found_key;
832         struct extent_buffer *leaf = NULL;
833         struct btrfs_dev_extent *extent = NULL;
834
835         path = btrfs_alloc_path();
836         if (!path)
837                 return -ENOMEM;
838
839         key.objectid = device->devid;
840         key.offset = start;
841         key.type = BTRFS_DEV_EXTENT_KEY;
842
843         ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
844         if (ret > 0) {
845                 ret = btrfs_previous_item(root, path, key.objectid,
846                                           BTRFS_DEV_EXTENT_KEY);
847                 BUG_ON(ret);
848                 leaf = path->nodes[0];
849                 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
850                 extent = btrfs_item_ptr(leaf, path->slots[0],
851                                         struct btrfs_dev_extent);
852                 BUG_ON(found_key.offset > start || found_key.offset +
853                        btrfs_dev_extent_length(leaf, extent) < start);
854                 ret = 0;
855         } else if (ret == 0) {
856                 leaf = path->nodes[0];
857                 extent = btrfs_item_ptr(leaf, path->slots[0],
858                                         struct btrfs_dev_extent);
859         }
860         BUG_ON(ret);
861
862         if (device->bytes_used > 0)
863                 device->bytes_used -= btrfs_dev_extent_length(leaf, extent);
864         ret = btrfs_del_item(trans, root, path);
865         BUG_ON(ret);
866
867         btrfs_free_path(path);
868         return ret;
869 }
870
871 int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
872                            struct btrfs_device *device,
873                            u64 chunk_tree, u64 chunk_objectid,
874                            u64 chunk_offset, u64 start, u64 num_bytes)
875 {
876         int ret;
877         struct btrfs_path *path;
878         struct btrfs_root *root = device->dev_root;
879         struct btrfs_dev_extent *extent;
880         struct extent_buffer *leaf;
881         struct btrfs_key key;
882
883         WARN_ON(!device->in_fs_metadata);
884         path = btrfs_alloc_path();
885         if (!path)
886                 return -ENOMEM;
887
888         key.objectid = device->devid;
889         key.offset = start;
890         key.type = BTRFS_DEV_EXTENT_KEY;
891         ret = btrfs_insert_empty_item(trans, root, path, &key,
892                                       sizeof(*extent));
893         BUG_ON(ret);
894
895         leaf = path->nodes[0];
896         extent = btrfs_item_ptr(leaf, path->slots[0],
897                                 struct btrfs_dev_extent);
898         btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
899         btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
900         btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
901
902         write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
903                     (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
904                     BTRFS_UUID_SIZE);
905
906         btrfs_set_dev_extent_length(leaf, extent, num_bytes);
907         btrfs_mark_buffer_dirty(leaf);
908         btrfs_free_path(path);
909         return ret;
910 }
911
912 static noinline int find_next_chunk(struct btrfs_root *root,
913                                     u64 objectid, u64 *offset)
914 {
915         struct btrfs_path *path;
916         int ret;
917         struct btrfs_key key;
918         struct btrfs_chunk *chunk;
919         struct btrfs_key found_key;
920
921         path = btrfs_alloc_path();
922         BUG_ON(!path);
923
924         key.objectid = objectid;
925         key.offset = (u64)-1;
926         key.type = BTRFS_CHUNK_ITEM_KEY;
927
928         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
929         if (ret < 0)
930                 goto error;
931
932         BUG_ON(ret == 0);
933
934         ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
935         if (ret) {
936                 *offset = 0;
937         } else {
938                 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
939                                       path->slots[0]);
940                 if (found_key.objectid != objectid)
941                         *offset = 0;
942                 else {
943                         chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
944                                                struct btrfs_chunk);
945                         *offset = found_key.offset +
946                                 btrfs_chunk_length(path->nodes[0], chunk);
947                 }
948         }
949         ret = 0;
950 error:
951         btrfs_free_path(path);
952         return ret;
953 }
954
955 static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
956 {
957         int ret;
958         struct btrfs_key key;
959         struct btrfs_key found_key;
960         struct btrfs_path *path;
961
962         root = root->fs_info->chunk_root;
963
964         path = btrfs_alloc_path();
965         if (!path)
966                 return -ENOMEM;
967
968         key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
969         key.type = BTRFS_DEV_ITEM_KEY;
970         key.offset = (u64)-1;
971
972         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
973         if (ret < 0)
974                 goto error;
975
976         BUG_ON(ret == 0);
977
978         ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
979                                   BTRFS_DEV_ITEM_KEY);
980         if (ret) {
981                 *objectid = 1;
982         } else {
983                 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
984                                       path->slots[0]);
985                 *objectid = found_key.offset + 1;
986         }
987         ret = 0;
988 error:
989         btrfs_free_path(path);
990         return ret;
991 }
992
993 /*
994  * the device information is stored in the chunk root
995  * the btrfs_device struct should be fully filled in
996  */
997 int btrfs_add_device(struct btrfs_trans_handle *trans,
998                      struct btrfs_root *root,
999                      struct btrfs_device *device)
1000 {
1001         int ret;
1002         struct btrfs_path *path;
1003         struct btrfs_dev_item *dev_item;
1004         struct extent_buffer *leaf;
1005         struct btrfs_key key;
1006         unsigned long ptr;
1007
1008         root = root->fs_info->chunk_root;
1009
1010         path = btrfs_alloc_path();
1011         if (!path)
1012                 return -ENOMEM;
1013
1014         key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1015         key.type = BTRFS_DEV_ITEM_KEY;
1016         key.offset = device->devid;
1017
1018         ret = btrfs_insert_empty_item(trans, root, path, &key,
1019                                       sizeof(*dev_item));
1020         if (ret)
1021                 goto out;
1022
1023         leaf = path->nodes[0];
1024         dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1025
1026         btrfs_set_device_id(leaf, dev_item, device->devid);
1027         btrfs_set_device_generation(leaf, dev_item, 0);
1028         btrfs_set_device_type(leaf, dev_item, device->type);
1029         btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1030         btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1031         btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
1032         btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1033         btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1034         btrfs_set_device_group(leaf, dev_item, 0);
1035         btrfs_set_device_seek_speed(leaf, dev_item, 0);
1036         btrfs_set_device_bandwidth(leaf, dev_item, 0);
1037         btrfs_set_device_start_offset(leaf, dev_item, 0);
1038
1039         ptr = (unsigned long)btrfs_device_uuid(dev_item);
1040         write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
1041         ptr = (unsigned long)btrfs_device_fsid(dev_item);
1042         write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
1043         btrfs_mark_buffer_dirty(leaf);
1044
1045         ret = 0;
1046 out:
1047         btrfs_free_path(path);
1048         return ret;
1049 }
1050
1051 static int btrfs_rm_dev_item(struct btrfs_root *root,
1052                              struct btrfs_device *device)
1053 {
1054         int ret;
1055         struct btrfs_path *path;
1056         struct btrfs_key key;
1057         struct btrfs_trans_handle *trans;
1058
1059         root = root->fs_info->chunk_root;
1060
1061         path = btrfs_alloc_path();
1062         if (!path)
1063                 return -ENOMEM;
1064
1065         trans = btrfs_start_transaction(root, 1);
1066         key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1067         key.type = BTRFS_DEV_ITEM_KEY;
1068         key.offset = device->devid;
1069         lock_chunks(root);
1070
1071         ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1072         if (ret < 0)
1073                 goto out;
1074
1075         if (ret > 0) {
1076                 ret = -ENOENT;
1077                 goto out;
1078         }
1079
1080         ret = btrfs_del_item(trans, root, path);
1081         if (ret)
1082                 goto out;
1083 out:
1084         btrfs_free_path(path);
1085         unlock_chunks(root);
1086         btrfs_commit_transaction(trans, root);
1087         return ret;
1088 }
1089
1090 int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1091 {
1092         struct btrfs_device *device;
1093         struct btrfs_device *next_device;
1094         struct block_device *bdev;
1095         struct buffer_head *bh = NULL;
1096         struct btrfs_super_block *disk_super;
1097         u64 all_avail;
1098         u64 devid;
1099         u64 num_devices;
1100         u8 *dev_uuid;
1101         int ret = 0;
1102
1103         mutex_lock(&uuid_mutex);
1104         mutex_lock(&root->fs_info->volume_mutex);
1105
1106         all_avail = root->fs_info->avail_data_alloc_bits |
1107                 root->fs_info->avail_system_alloc_bits |
1108                 root->fs_info->avail_metadata_alloc_bits;
1109
1110         if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
1111             root->fs_info->fs_devices->rw_devices <= 4) {
1112                 printk(KERN_ERR "btrfs: unable to go below four devices "
1113                        "on raid10\n");
1114                 ret = -EINVAL;
1115                 goto out;
1116         }
1117
1118         if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
1119             root->fs_info->fs_devices->rw_devices <= 2) {
1120                 printk(KERN_ERR "btrfs: unable to go below two "
1121                        "devices on raid1\n");
1122                 ret = -EINVAL;
1123                 goto out;
1124         }
1125
1126         if (strcmp(device_path, "missing") == 0) {
1127                 struct list_head *devices;
1128                 struct btrfs_device *tmp;
1129
1130                 device = NULL;
1131                 devices = &root->fs_info->fs_devices->devices;
1132                 list_for_each_entry(tmp, devices, dev_list) {
1133                         if (tmp->in_fs_metadata && !tmp->bdev) {
1134                                 device = tmp;
1135                                 break;
1136                         }
1137                 }
1138                 bdev = NULL;
1139                 bh = NULL;
1140                 disk_super = NULL;
1141                 if (!device) {
1142                         printk(KERN_ERR "btrfs: no missing devices found to "
1143                                "remove\n");
1144                         goto out;
1145                 }
1146         } else {
1147                 bdev = open_bdev_exclusive(device_path, FMODE_READ,
1148                                       root->fs_info->bdev_holder);
1149                 if (IS_ERR(bdev)) {
1150                         ret = PTR_ERR(bdev);
1151                         goto out;
1152                 }
1153
1154                 set_blocksize(bdev, 4096);
1155                 bh = btrfs_read_dev_super(bdev);
1156                 if (!bh) {
1157                         ret = -EIO;
1158                         goto error_close;
1159                 }
1160                 disk_super = (struct btrfs_super_block *)bh->b_data;
1161                 devid = le64_to_cpu(disk_super->dev_item.devid);
1162                 dev_uuid = disk_super->dev_item.uuid;
1163                 device = btrfs_find_device(root, devid, dev_uuid,
1164                                            disk_super->fsid);
1165                 if (!device) {
1166                         ret = -ENOENT;
1167                         goto error_brelse;
1168                 }
1169         }
1170
1171         if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
1172                 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1173                        "device\n");
1174                 ret = -EINVAL;
1175                 goto error_brelse;
1176         }
1177
1178         if (device->writeable) {
1179                 list_del_init(&device->dev_alloc_list);
1180                 root->fs_info->fs_devices->rw_devices--;
1181         }
1182
1183         ret = btrfs_shrink_device(device, 0);
1184         if (ret)
1185                 goto error_brelse;
1186
1187         ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1188         if (ret)
1189                 goto error_brelse;
1190
1191         device->in_fs_metadata = 0;
1192         list_del_init(&device->dev_list);
1193         device->fs_devices->num_devices--;
1194
1195         next_device = list_entry(root->fs_info->fs_devices->devices.next,
1196                                  struct btrfs_device, dev_list);
1197         if (device->bdev == root->fs_info->sb->s_bdev)
1198                 root->fs_info->sb->s_bdev = next_device->bdev;
1199         if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1200                 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1201
1202         if (device->bdev) {
1203                 close_bdev_exclusive(device->bdev, device->mode);
1204                 device->bdev = NULL;
1205                 device->fs_devices->open_devices--;
1206         }
1207
1208         num_devices = btrfs_super_num_devices(&root->fs_info->super_copy) - 1;
1209         btrfs_set_super_num_devices(&root->fs_info->super_copy, num_devices);
1210
1211         if (device->fs_devices->open_devices == 0) {
1212                 struct btrfs_fs_devices *fs_devices;
1213                 fs_devices = root->fs_info->fs_devices;
1214                 while (fs_devices) {
1215                         if (fs_devices->seed == device->fs_devices)
1216                                 break;
1217                         fs_devices = fs_devices->seed;
1218                 }
1219                 fs_devices->seed = device->fs_devices->seed;
1220                 device->fs_devices->seed = NULL;
1221                 __btrfs_close_devices(device->fs_devices);
1222                 free_fs_devices(device->fs_devices);
1223         }
1224
1225         /*
1226          * at this point, the device is zero sized.  We want to
1227          * remove it from the devices list and zero out the old super
1228          */
1229         if (device->writeable) {
1230                 /* make sure this device isn't detected as part of
1231                  * the FS anymore
1232                  */
1233                 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1234                 set_buffer_dirty(bh);
1235                 sync_dirty_buffer(bh);
1236         }
1237
1238         kfree(device->name);
1239         kfree(device);
1240         ret = 0;
1241
1242 error_brelse:
1243         brelse(bh);
1244 error_close:
1245         if (bdev)
1246                 close_bdev_exclusive(bdev, FMODE_READ);
1247 out:
1248         mutex_unlock(&root->fs_info->volume_mutex);
1249         mutex_unlock(&uuid_mutex);
1250         return ret;
1251 }
1252
1253 /*
1254  * does all the dirty work required for changing file system's UUID.
1255  */
1256 static int btrfs_prepare_sprout(struct btrfs_trans_handle *trans,
1257                                 struct btrfs_root *root)
1258 {
1259         struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1260         struct btrfs_fs_devices *old_devices;
1261         struct btrfs_fs_devices *seed_devices;
1262         struct btrfs_super_block *disk_super = &root->fs_info->super_copy;
1263         struct btrfs_device *device;
1264         u64 super_flags;
1265
1266         BUG_ON(!mutex_is_locked(&uuid_mutex));
1267         if (!fs_devices->seeding)
1268                 return -EINVAL;
1269
1270         seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1271         if (!seed_devices)
1272                 return -ENOMEM;
1273
1274         old_devices = clone_fs_devices(fs_devices);
1275         if (IS_ERR(old_devices)) {
1276                 kfree(seed_devices);
1277                 return PTR_ERR(old_devices);
1278         }
1279
1280         list_add(&old_devices->list, &fs_uuids);
1281
1282         memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1283         seed_devices->opened = 1;
1284         INIT_LIST_HEAD(&seed_devices->devices);
1285         INIT_LIST_HEAD(&seed_devices->alloc_list);
1286         list_splice_init(&fs_devices->devices, &seed_devices->devices);
1287         list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1288         list_for_each_entry(device, &seed_devices->devices, dev_list) {
1289                 device->fs_devices = seed_devices;
1290         }
1291
1292         fs_devices->seeding = 0;
1293         fs_devices->num_devices = 0;
1294         fs_devices->open_devices = 0;
1295         fs_devices->seed = seed_devices;
1296
1297         generate_random_uuid(fs_devices->fsid);
1298         memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1299         memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1300         super_flags = btrfs_super_flags(disk_super) &
1301                       ~BTRFS_SUPER_FLAG_SEEDING;
1302         btrfs_set_super_flags(disk_super, super_flags);
1303
1304         return 0;
1305 }
1306
1307 /*
1308  * strore the expected generation for seed devices in device items.
1309  */
1310 static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1311                                struct btrfs_root *root)
1312 {
1313         struct btrfs_path *path;
1314         struct extent_buffer *leaf;
1315         struct btrfs_dev_item *dev_item;
1316         struct btrfs_device *device;
1317         struct btrfs_key key;
1318         u8 fs_uuid[BTRFS_UUID_SIZE];
1319         u8 dev_uuid[BTRFS_UUID_SIZE];
1320         u64 devid;
1321         int ret;
1322
1323         path = btrfs_alloc_path();
1324         if (!path)
1325                 return -ENOMEM;
1326
1327         root = root->fs_info->chunk_root;
1328         key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1329         key.offset = 0;
1330         key.type = BTRFS_DEV_ITEM_KEY;
1331
1332         while (1) {
1333                 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1334                 if (ret < 0)
1335                         goto error;
1336
1337                 leaf = path->nodes[0];
1338 next_slot:
1339                 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1340                         ret = btrfs_next_leaf(root, path);
1341                         if (ret > 0)
1342                                 break;
1343                         if (ret < 0)
1344                                 goto error;
1345                         leaf = path->nodes[0];
1346                         btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1347                         btrfs_release_path(root, path);
1348                         continue;
1349                 }
1350
1351                 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1352                 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1353                     key.type != BTRFS_DEV_ITEM_KEY)
1354                         break;
1355
1356                 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1357                                           struct btrfs_dev_item);
1358                 devid = btrfs_device_id(leaf, dev_item);
1359                 read_extent_buffer(leaf, dev_uuid,
1360                                    (unsigned long)btrfs_device_uuid(dev_item),
1361                                    BTRFS_UUID_SIZE);
1362                 read_extent_buffer(leaf, fs_uuid,
1363                                    (unsigned long)btrfs_device_fsid(dev_item),
1364                                    BTRFS_UUID_SIZE);
1365                 device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
1366                 BUG_ON(!device);
1367
1368                 if (device->fs_devices->seeding) {
1369                         btrfs_set_device_generation(leaf, dev_item,
1370                                                     device->generation);
1371                         btrfs_mark_buffer_dirty(leaf);
1372                 }
1373
1374                 path->slots[0]++;
1375                 goto next_slot;
1376         }
1377         ret = 0;
1378 error:
1379         btrfs_free_path(path);
1380         return ret;
1381 }
1382
1383 int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1384 {
1385         struct btrfs_trans_handle *trans;
1386         struct btrfs_device *device;
1387         struct block_device *bdev;
1388         struct list_head *devices;
1389         struct super_block *sb = root->fs_info->sb;
1390         u64 total_bytes;
1391         int seeding_dev = 0;
1392         int ret = 0;
1393
1394         if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
1395                 return -EINVAL;
1396
1397         bdev = open_bdev_exclusive(device_path, 0, root->fs_info->bdev_holder);
1398         if (!bdev)
1399                 return -EIO;
1400
1401         if (root->fs_info->fs_devices->seeding) {
1402                 seeding_dev = 1;
1403                 down_write(&sb->s_umount);
1404                 mutex_lock(&uuid_mutex);
1405         }
1406
1407         filemap_write_and_wait(bdev->bd_inode->i_mapping);
1408         mutex_lock(&root->fs_info->volume_mutex);
1409
1410         devices = &root->fs_info->fs_devices->devices;
1411         list_for_each_entry(device, devices, dev_list) {
1412                 if (device->bdev == bdev) {
1413                         ret = -EEXIST;
1414                         goto error;
1415                 }
1416         }
1417
1418         device = kzalloc(sizeof(*device), GFP_NOFS);
1419         if (!device) {
1420                 /* we can safely leave the fs_devices entry around */
1421                 ret = -ENOMEM;
1422                 goto error;
1423         }
1424
1425         device->name = kstrdup(device_path, GFP_NOFS);
1426         if (!device->name) {
1427                 kfree(device);
1428                 ret = -ENOMEM;
1429                 goto error;
1430         }
1431
1432         ret = find_next_devid(root, &device->devid);
1433         if (ret) {
1434                 kfree(device);
1435                 goto error;
1436         }
1437
1438         trans = btrfs_start_transaction(root, 1);
1439         lock_chunks(root);
1440
1441         device->barriers = 1;
1442         device->writeable = 1;
1443         device->work.func = pending_bios_fn;
1444         generate_random_uuid(device->uuid);
1445         spin_lock_init(&device->io_lock);
1446         device->generation = trans->transid;
1447         device->io_width = root->sectorsize;
1448         device->io_align = root->sectorsize;
1449         device->sector_size = root->sectorsize;
1450         device->total_bytes = i_size_read(bdev->bd_inode);
1451         device->disk_total_bytes = device->total_bytes;
1452         device->dev_root = root->fs_info->dev_root;
1453         device->bdev = bdev;
1454         device->in_fs_metadata = 1;
1455         device->mode = 0;
1456         set_blocksize(device->bdev, 4096);
1457
1458         if (seeding_dev) {
1459                 sb->s_flags &= ~MS_RDONLY;
1460                 ret = btrfs_prepare_sprout(trans, root);
1461                 BUG_ON(ret);
1462         }
1463
1464         device->fs_devices = root->fs_info->fs_devices;
1465         list_add(&device->dev_list, &root->fs_info->fs_devices->devices);
1466         list_add(&device->dev_alloc_list,
1467                  &root->fs_info->fs_devices->alloc_list);
1468         root->fs_info->fs_devices->num_devices++;
1469         root->fs_info->fs_devices->open_devices++;
1470         root->fs_info->fs_devices->rw_devices++;
1471         root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1472
1473         total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
1474         btrfs_set_super_total_bytes(&root->fs_info->super_copy,
1475                                     total_bytes + device->total_bytes);
1476
1477         total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy);
1478         btrfs_set_super_num_devices(&root->fs_info->super_copy,
1479                                     total_bytes + 1);
1480
1481         if (seeding_dev) {
1482                 ret = init_first_rw_device(trans, root, device);
1483                 BUG_ON(ret);
1484                 ret = btrfs_finish_sprout(trans, root);
1485                 BUG_ON(ret);
1486         } else {
1487                 ret = btrfs_add_device(trans, root, device);
1488         }
1489
1490         /*
1491          * we've got more storage, clear any full flags on the space
1492          * infos
1493          */
1494         btrfs_clear_space_info_full(root->fs_info);
1495
1496         unlock_chunks(root);
1497         btrfs_commit_transaction(trans, root);
1498
1499         if (seeding_dev) {
1500                 mutex_unlock(&uuid_mutex);
1501                 up_write(&sb->s_umount);
1502
1503                 ret = btrfs_relocate_sys_chunks(root);
1504                 BUG_ON(ret);
1505         }
1506 out:
1507         mutex_unlock(&root->fs_info->volume_mutex);
1508         return ret;
1509 error:
1510         close_bdev_exclusive(bdev, 0);
1511         if (seeding_dev) {
1512                 mutex_unlock(&uuid_mutex);
1513                 up_write(&sb->s_umount);
1514         }
1515         goto out;
1516 }
1517
1518 static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
1519                                         struct btrfs_device *device)
1520 {
1521         int ret;
1522         struct btrfs_path *path;
1523         struct btrfs_root *root;
1524         struct btrfs_dev_item *dev_item;
1525         struct extent_buffer *leaf;
1526         struct btrfs_key key;
1527
1528         root = device->dev_root->fs_info->chunk_root;
1529
1530         path = btrfs_alloc_path();
1531         if (!path)
1532                 return -ENOMEM;
1533
1534         key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1535         key.type = BTRFS_DEV_ITEM_KEY;
1536         key.offset = device->devid;
1537
1538         ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1539         if (ret < 0)
1540                 goto out;
1541
1542         if (ret > 0) {
1543                 ret = -ENOENT;
1544                 goto out;
1545         }
1546
1547         leaf = path->nodes[0];
1548         dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1549
1550         btrfs_set_device_id(leaf, dev_item, device->devid);
1551         btrfs_set_device_type(leaf, dev_item, device->type);
1552         btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1553         btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1554         btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
1555         btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
1556         btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
1557         btrfs_mark_buffer_dirty(leaf);
1558
1559 out:
1560         btrfs_free_path(path);
1561         return ret;
1562 }
1563
1564 static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
1565                       struct btrfs_device *device, u64 new_size)
1566 {
1567         struct btrfs_super_block *super_copy =
1568                 &device->dev_root->fs_info->super_copy;
1569         u64 old_total = btrfs_super_total_bytes(super_copy);
1570         u64 diff = new_size - device->total_bytes;
1571
1572         if (!device->writeable)
1573                 return -EACCES;
1574         if (new_size <= device->total_bytes)
1575                 return -EINVAL;
1576
1577         btrfs_set_super_total_bytes(super_copy, old_total + diff);
1578         device->fs_devices->total_rw_bytes += diff;
1579
1580         device->total_bytes = new_size;
1581         btrfs_clear_space_info_full(device->dev_root->fs_info);
1582
1583         return btrfs_update_device(trans, device);
1584 }
1585
1586 int btrfs_grow_device(struct btrfs_trans_handle *trans,
1587                       struct btrfs_device *device, u64 new_size)
1588 {
1589         int ret;
1590         lock_chunks(device->dev_root);
1591         ret = __btrfs_grow_device(trans, device, new_size);
1592         unlock_chunks(device->dev_root);
1593         return ret;
1594 }
1595
1596 static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
1597                             struct btrfs_root *root,
1598                             u64 chunk_tree, u64 chunk_objectid,
1599                             u64 chunk_offset)
1600 {
1601         int ret;
1602         struct btrfs_path *path;
1603         struct btrfs_key key;
1604
1605         root = root->fs_info->chunk_root;
1606         path = btrfs_alloc_path();
1607         if (!path)
1608                 return -ENOMEM;
1609
1610         key.objectid = chunk_objectid;
1611         key.offset = chunk_offset;
1612         key.type = BTRFS_CHUNK_ITEM_KEY;
1613
1614         ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1615         BUG_ON(ret);
1616
1617         ret = btrfs_del_item(trans, root, path);
1618         BUG_ON(ret);
1619
1620         btrfs_free_path(path);
1621         return 0;
1622 }
1623
1624 static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
1625                         chunk_offset)
1626 {
1627         struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1628         struct btrfs_disk_key *disk_key;
1629         struct btrfs_chunk *chunk;
1630         u8 *ptr;
1631         int ret = 0;
1632         u32 num_stripes;
1633         u32 array_size;
1634         u32 len = 0;
1635         u32 cur;
1636         struct btrfs_key key;
1637
1638         array_size = btrfs_super_sys_array_size(super_copy);
1639
1640         ptr = super_copy->sys_chunk_array;
1641         cur = 0;
1642
1643         while (cur < array_size) {
1644                 disk_key = (struct btrfs_disk_key *)ptr;
1645                 btrfs_disk_key_to_cpu(&key, disk_key);
1646
1647                 len = sizeof(*disk_key);
1648
1649                 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
1650                         chunk = (struct btrfs_chunk *)(ptr + len);
1651                         num_stripes = btrfs_stack_chunk_num_stripes(chunk);
1652                         len += btrfs_chunk_item_size(num_stripes);
1653                 } else {
1654                         ret = -EIO;
1655                         break;
1656                 }
1657                 if (key.objectid == chunk_objectid &&
1658                     key.offset == chunk_offset) {
1659                         memmove(ptr, ptr + len, array_size - (cur + len));
1660                         array_size -= len;
1661                         btrfs_set_super_sys_array_size(super_copy, array_size);
1662                 } else {
1663                         ptr += len;
1664                         cur += len;
1665                 }
1666         }
1667         return ret;
1668 }
1669
1670 static int btrfs_relocate_chunk(struct btrfs_root *root,
1671                          u64 chunk_tree, u64 chunk_objectid,
1672                          u64 chunk_offset)
1673 {
1674         struct extent_map_tree *em_tree;
1675         struct btrfs_root *extent_root;
1676         struct btrfs_trans_handle *trans;
1677         struct extent_map *em;
1678         struct map_lookup *map;
1679         int ret;
1680         int i;
1681
1682         root = root->fs_info->chunk_root;
1683         extent_root = root->fs_info->extent_root;
1684         em_tree = &root->fs_info->mapping_tree.map_tree;
1685
1686         /* step one, relocate all the extents inside this chunk */
1687         ret = btrfs_relocate_block_group(extent_root, chunk_offset);
1688         BUG_ON(ret);
1689
1690         trans = btrfs_start_transaction(root, 1);
1691         BUG_ON(!trans);
1692
1693         lock_chunks(root);
1694
1695         /*
1696          * step two, delete the device extents and the
1697          * chunk tree entries
1698          */
1699         spin_lock(&em_tree->lock);
1700         em = lookup_extent_mapping(em_tree, chunk_offset, 1);
1701         spin_unlock(&em_tree->lock);
1702
1703         BUG_ON(em->start > chunk_offset ||
1704                em->start + em->len < chunk_offset);
1705         map = (struct map_lookup *)em->bdev;
1706
1707         for (i = 0; i < map->num_stripes; i++) {
1708                 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
1709                                             map->stripes[i].physical);
1710                 BUG_ON(ret);
1711
1712                 if (map->stripes[i].dev) {
1713                         ret = btrfs_update_device(trans, map->stripes[i].dev);
1714                         BUG_ON(ret);
1715                 }
1716         }
1717         ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
1718                                chunk_offset);
1719
1720         BUG_ON(ret);
1721
1722         if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
1723                 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
1724                 BUG_ON(ret);
1725         }
1726
1727         ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
1728         BUG_ON(ret);
1729
1730         spin_lock(&em_tree->lock);
1731         remove_extent_mapping(em_tree, em);
1732         spin_unlock(&em_tree->lock);
1733
1734         kfree(map);
1735         em->bdev = NULL;
1736
1737         /* once for the tree */
1738         free_extent_map(em);
1739         /* once for us */
1740         free_extent_map(em);
1741
1742         unlock_chunks(root);
1743         btrfs_end_transaction(trans, root);
1744         return 0;
1745 }
1746
1747 static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
1748 {
1749         struct btrfs_root *chunk_root = root->fs_info->chunk_root;
1750         struct btrfs_path *path;
1751         struct extent_buffer *leaf;
1752         struct btrfs_chunk *chunk;
1753         struct btrfs_key key;
1754         struct btrfs_key found_key;
1755         u64 chunk_tree = chunk_root->root_key.objectid;
1756         u64 chunk_type;
1757         int ret;
1758
1759         path = btrfs_alloc_path();
1760         if (!path)
1761                 return -ENOMEM;
1762
1763         key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1764         key.offset = (u64)-1;
1765         key.type = BTRFS_CHUNK_ITEM_KEY;
1766
1767         while (1) {
1768                 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
1769                 if (ret < 0)
1770                         goto error;
1771                 BUG_ON(ret == 0);
1772
1773                 ret = btrfs_previous_item(chunk_root, path, key.objectid,
1774                                           key.type);
1775                 if (ret < 0)
1776                         goto error;
1777                 if (ret > 0)
1778                         break;
1779
1780                 leaf = path->nodes[0];
1781                 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1782
1783                 chunk = btrfs_item_ptr(leaf, path->slots[0],
1784                                        struct btrfs_chunk);
1785                 chunk_type = btrfs_chunk_type(leaf, chunk);
1786                 btrfs_release_path(chunk_root, path);
1787
1788                 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
1789                         ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
1790                                                    found_key.objectid,
1791                                                    found_key.offset);
1792                         BUG_ON(ret);
1793                 }
1794
1795                 if (found_key.offset == 0)
1796                         break;
1797                 key.offset = found_key.offset - 1;
1798         }
1799         ret = 0;
1800 error:
1801         btrfs_free_path(path);
1802         return ret;
1803 }
1804
1805 static u64 div_factor(u64 num, int factor)
1806 {
1807         if (factor == 10)
1808                 return num;
1809         num *= factor;
1810         do_div(num, 10);
1811         return num;
1812 }
1813
1814 int btrfs_balance(struct btrfs_root *dev_root)
1815 {
1816         int ret;
1817         struct list_head *devices = &dev_root->fs_info->fs_devices->devices;
1818         struct btrfs_device *device;
1819         u64 old_size;
1820         u64 size_to_free;
1821         struct btrfs_path *path;
1822         struct btrfs_key key;
1823         struct btrfs_chunk *chunk;
1824         struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root;
1825         struct btrfs_trans_handle *trans;
1826         struct btrfs_key found_key;
1827
1828         if (dev_root->fs_info->sb->s_flags & MS_RDONLY)
1829                 return -EROFS;
1830
1831         mutex_lock(&dev_root->fs_info->volume_mutex);
1832         dev_root = dev_root->fs_info->dev_root;
1833
1834         /* step one make some room on all the devices */
1835         list_for_each_entry(device, devices, dev_list) {
1836                 old_size = device->total_bytes;
1837                 size_to_free = div_factor(old_size, 1);
1838                 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
1839                 if (!device->writeable ||
1840                     device->total_bytes - device->bytes_used > size_to_free)
1841                         continue;
1842
1843                 ret = btrfs_shrink_device(device, old_size - size_to_free);
1844                 BUG_ON(ret);
1845
1846                 trans = btrfs_start_transaction(dev_root, 1);
1847                 BUG_ON(!trans);
1848
1849                 ret = btrfs_grow_device(trans, device, old_size);
1850                 BUG_ON(ret);
1851
1852                 btrfs_end_transaction(trans, dev_root);
1853         }
1854
1855         /* step two, relocate all the chunks */
1856         path = btrfs_alloc_path();
1857         BUG_ON(!path);
1858
1859         key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
1860         key.offset = (u64)-1;
1861         key.type = BTRFS_CHUNK_ITEM_KEY;
1862
1863         while (1) {
1864                 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
1865                 if (ret < 0)
1866                         goto error;
1867
1868                 /*
1869                  * this shouldn't happen, it means the last relocate
1870                  * failed
1871                  */
1872                 if (ret == 0)
1873                         break;
1874
1875                 ret = btrfs_previous_item(chunk_root, path, 0,
1876                                           BTRFS_CHUNK_ITEM_KEY);
1877                 if (ret)
1878                         break;
1879
1880                 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1881                                       path->slots[0]);
1882                 if (found_key.objectid != key.objectid)
1883                         break;
1884
1885                 chunk = btrfs_item_ptr(path->nodes[0],
1886                                        path->slots[0],
1887                                        struct btrfs_chunk);
1888                 key.offset = found_key.offset;
1889                 /* chunk zero is special */
1890                 if (key.offset == 0)
1891                         break;
1892
1893                 btrfs_release_path(chunk_root, path);
1894                 ret = btrfs_relocate_chunk(chunk_root,
1895                                            chunk_root->root_key.objectid,
1896                                            found_key.objectid,
1897                                            found_key.offset);
1898                 BUG_ON(ret);
1899         }
1900         ret = 0;
1901 error:
1902         btrfs_free_path(path);
1903         mutex_unlock(&dev_root->fs_info->volume_mutex);
1904         return ret;
1905 }
1906
1907 /*
1908  * shrinking a device means finding all of the device extents past
1909  * the new size, and then following the back refs to the chunks.
1910  * The chunk relocation code actually frees the device extent
1911  */
1912 int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
1913 {
1914         struct btrfs_trans_handle *trans;
1915         struct btrfs_root *root = device->dev_root;
1916         struct btrfs_dev_extent *dev_extent = NULL;
1917         struct btrfs_path *path;
1918         u64 length;
1919         u64 chunk_tree;
1920         u64 chunk_objectid;
1921         u64 chunk_offset;
1922         int ret;
1923         int slot;
1924         struct extent_buffer *l;
1925         struct btrfs_key key;
1926         struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
1927         u64 old_total = btrfs_super_total_bytes(super_copy);
1928         u64 diff = device->total_bytes - new_size;
1929
1930         if (new_size >= device->total_bytes)
1931                 return -EINVAL;
1932
1933         path = btrfs_alloc_path();
1934         if (!path)
1935                 return -ENOMEM;
1936
1937         trans = btrfs_start_transaction(root, 1);
1938         if (!trans) {
1939                 ret = -ENOMEM;
1940                 goto done;
1941         }
1942
1943         path->reada = 2;
1944
1945         lock_chunks(root);
1946
1947         device->total_bytes = new_size;
1948         if (device->writeable)
1949                 device->fs_devices->total_rw_bytes -= diff;
1950         unlock_chunks(root);
1951         btrfs_end_transaction(trans, root);
1952
1953         key.objectid = device->devid;
1954         key.offset = (u64)-1;
1955         key.type = BTRFS_DEV_EXTENT_KEY;
1956
1957         while (1) {
1958                 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1959                 if (ret < 0)
1960                         goto done;
1961
1962                 ret = btrfs_previous_item(root, path, 0, key.type);
1963                 if (ret < 0)
1964                         goto done;
1965                 if (ret) {
1966                         ret = 0;
1967                         goto done;
1968                 }
1969
1970                 l = path->nodes[0];
1971                 slot = path->slots[0];
1972                 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
1973
1974                 if (key.objectid != device->devid)
1975                         goto done;
1976
1977                 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
1978                 length = btrfs_dev_extent_length(l, dev_extent);
1979
1980                 if (key.offset + length <= new_size)
1981                         break;
1982
1983                 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
1984                 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
1985                 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
1986                 btrfs_release_path(root, path);
1987
1988                 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
1989                                            chunk_offset);
1990                 if (ret)
1991                         goto done;
1992         }
1993
1994         /* Shrinking succeeded, else we would be at "done". */
1995         trans = btrfs_start_transaction(root, 1);
1996         if (!trans) {
1997                 ret = -ENOMEM;
1998                 goto done;
1999         }
2000         lock_chunks(root);
2001
2002         device->disk_total_bytes = new_size;
2003         /* Now btrfs_update_device() will change the on-disk size. */
2004         ret = btrfs_update_device(trans, device);
2005         if (ret) {
2006                 unlock_chunks(root);
2007                 btrfs_end_transaction(trans, root);
2008                 goto done;
2009         }
2010         WARN_ON(diff > old_total);
2011         btrfs_set_super_total_bytes(super_copy, old_total - diff);
2012         unlock_chunks(root);
2013         btrfs_end_transaction(trans, root);
2014 done:
2015         btrfs_free_path(path);
2016         return ret;
2017 }
2018
2019 static int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
2020                            struct btrfs_root *root,
2021                            struct btrfs_key *key,
2022                            struct btrfs_chunk *chunk, int item_size)
2023 {
2024         struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
2025         struct btrfs_disk_key disk_key;
2026         u32 array_size;
2027         u8 *ptr;
2028
2029         array_size = btrfs_super_sys_array_size(super_copy);
2030         if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
2031                 return -EFBIG;
2032
2033         ptr = super_copy->sys_chunk_array + array_size;
2034         btrfs_cpu_key_to_disk(&disk_key, key);
2035         memcpy(ptr, &disk_key, sizeof(disk_key));
2036         ptr += sizeof(disk_key);
2037         memcpy(ptr, chunk, item_size);
2038         item_size += sizeof(disk_key);
2039         btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
2040         return 0;
2041 }
2042
2043 static noinline u64 chunk_bytes_by_type(u64 type, u64 calc_size,
2044                                         int num_stripes, int sub_stripes)
2045 {
2046         if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP))
2047                 return calc_size;
2048         else if (type & BTRFS_BLOCK_GROUP_RAID10)
2049                 return calc_size * (num_stripes / sub_stripes);
2050         else
2051                 return calc_size * num_stripes;
2052 }
2053
2054 static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2055                                struct btrfs_root *extent_root,
2056                                struct map_lookup **map_ret,
2057                                u64 *num_bytes, u64 *stripe_size,
2058                                u64 start, u64 type)
2059 {
2060         struct btrfs_fs_info *info = extent_root->fs_info;
2061         struct btrfs_device *device = NULL;
2062         struct btrfs_fs_devices *fs_devices = info->fs_devices;
2063         struct list_head *cur;
2064         struct map_lookup *map = NULL;
2065         struct extent_map_tree *em_tree;
2066         struct extent_map *em;
2067         struct list_head private_devs;
2068         int min_stripe_size = 1 * 1024 * 1024;
2069         u64 calc_size = 1024 * 1024 * 1024;
2070         u64 max_chunk_size = calc_size;
2071         u64 min_free;
2072         u64 avail;
2073         u64 max_avail = 0;
2074         u64 dev_offset;
2075         int num_stripes = 1;
2076         int min_stripes = 1;
2077         int sub_stripes = 0;
2078         int looped = 0;
2079         int ret;
2080         int index;
2081         int stripe_len = 64 * 1024;
2082
2083         if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
2084             (type & BTRFS_BLOCK_GROUP_DUP)) {
2085                 WARN_ON(1);
2086                 type &= ~BTRFS_BLOCK_GROUP_DUP;
2087         }
2088         if (list_empty(&fs_devices->alloc_list))
2089                 return -ENOSPC;
2090
2091         if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
2092                 num_stripes = fs_devices->rw_devices;
2093                 min_stripes = 2;
2094         }
2095         if (type & (BTRFS_BLOCK_GROUP_DUP)) {
2096                 num_stripes = 2;
2097                 min_stripes = 2;
2098         }
2099         if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
2100                 num_stripes = min_t(u64, 2, fs_devices->rw_devices);
2101                 if (num_stripes < 2)
2102                         return -ENOSPC;
2103                 min_stripes = 2;
2104         }
2105         if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
2106                 num_stripes = fs_devices->rw_devices;
2107                 if (num_stripes < 4)
2108                         return -ENOSPC;
2109                 num_stripes &= ~(u32)1;
2110                 sub_stripes = 2;
2111                 min_stripes = 4;
2112         }
2113
2114         if (type & BTRFS_BLOCK_GROUP_DATA) {
2115                 max_chunk_size = 10 * calc_size;
2116                 min_stripe_size = 64 * 1024 * 1024;
2117         } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
2118                 max_chunk_size = 4 * calc_size;
2119                 min_stripe_size = 32 * 1024 * 1024;
2120         } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
2121                 calc_size = 8 * 1024 * 1024;
2122                 max_chunk_size = calc_size * 2;
2123                 min_stripe_size = 1 * 1024 * 1024;
2124         }
2125
2126         /* we don't want a chunk larger than 10% of writeable space */
2127         max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
2128                              max_chunk_size);
2129
2130 again:
2131         if (!map || map->num_stripes != num_stripes) {
2132                 kfree(map);
2133                 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
2134                 if (!map)
2135                         return -ENOMEM;
2136                 map->num_stripes = num_stripes;
2137         }
2138
2139         if (calc_size * num_stripes > max_chunk_size) {
2140                 calc_size = max_chunk_size;
2141                 do_div(calc_size, num_stripes);
2142                 do_div(calc_size, stripe_len);
2143                 calc_size *= stripe_len;
2144         }
2145         /* we don't want tiny stripes */
2146         calc_size = max_t(u64, min_stripe_size, calc_size);
2147
2148         do_div(calc_size, stripe_len);
2149         calc_size *= stripe_len;
2150
2151         cur = fs_devices->alloc_list.next;
2152         index = 0;
2153
2154         if (type & BTRFS_BLOCK_GROUP_DUP)
2155                 min_free = calc_size * 2;
2156         else
2157                 min_free = calc_size;
2158
2159         /*
2160          * we add 1MB because we never use the first 1MB of the device, unless
2161          * we've looped, then we are likely allocating the maximum amount of
2162          * space left already
2163          */
2164         if (!looped)
2165                 min_free += 1024 * 1024;
2166
2167         INIT_LIST_HEAD(&private_devs);
2168         while (index < num_stripes) {
2169                 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
2170                 BUG_ON(!device->writeable);
2171                 if (device->total_bytes > device->bytes_used)
2172                         avail = device->total_bytes - device->bytes_used;
2173                 else
2174                         avail = 0;
2175                 cur = cur->next;
2176
2177                 if (device->in_fs_metadata && avail >= min_free) {
2178                         ret = find_free_dev_extent(trans, device,
2179                                                    min_free, &dev_offset);
2180                         if (ret == 0) {
2181                                 list_move_tail(&device->dev_alloc_list,
2182                                                &private_devs);
2183                                 map->stripes[index].dev = device;
2184                                 map->stripes[index].physical = dev_offset;
2185                                 index++;
2186                                 if (type & BTRFS_BLOCK_GROUP_DUP) {
2187                                         map->stripes[index].dev = device;
2188                                         map->stripes[index].physical =
2189                                                 dev_offset + calc_size;
2190                                         index++;
2191                                 }
2192                         }
2193                 } else if (device->in_fs_metadata && avail > max_avail)
2194                         max_avail = avail;
2195                 if (cur == &fs_devices->alloc_list)
2196                         break;
2197         }
2198         list_splice(&private_devs, &fs_devices->alloc_list);
2199         if (index < num_stripes) {
2200                 if (index >= min_stripes) {
2201                         num_stripes = index;
2202                         if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
2203                                 num_stripes /= sub_stripes;
2204                                 num_stripes *= sub_stripes;
2205                         }
2206                         looped = 1;
2207                         goto again;
2208                 }
2209                 if (!looped && max_avail > 0) {
2210                         looped = 1;
2211                         calc_size = max_avail;
2212                         goto again;
2213                 }
2214                 kfree(map);
2215                 return -ENOSPC;
2216         }
2217         map->sector_size = extent_root->sectorsize;
2218         map->stripe_len = stripe_len;
2219         map->io_align = stripe_len;
2220         map->io_width = stripe_len;
2221         map->type = type;
2222         map->num_stripes = num_stripes;
2223         map->sub_stripes = sub_stripes;
2224
2225         *map_ret = map;
2226         *stripe_size = calc_size;
2227         *num_bytes = chunk_bytes_by_type(type, calc_size,
2228                                          num_stripes, sub_stripes);
2229
2230         em = alloc_extent_map(GFP_NOFS);
2231         if (!em) {
2232                 kfree(map);
2233                 return -ENOMEM;
2234         }
2235         em->bdev = (struct block_device *)map;
2236         em->start = start;
2237         em->len = *num_bytes;
2238         em->block_start = 0;
2239         em->block_len = em->len;
2240
2241         em_tree = &extent_root->fs_info->mapping_tree.map_tree;
2242         spin_lock(&em_tree->lock);
2243         ret = add_extent_mapping(em_tree, em);
2244         spin_unlock(&em_tree->lock);
2245         BUG_ON(ret);
2246         free_extent_map(em);
2247
2248         ret = btrfs_make_block_group(trans, extent_root, 0, type,
2249                                      BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2250                                      start, *num_bytes);
2251         BUG_ON(ret);
2252
2253         index = 0;
2254         while (index < map->num_stripes) {
2255                 device = map->stripes[index].dev;
2256                 dev_offset = map->stripes[index].physical;
2257
2258                 ret = btrfs_alloc_dev_extent(trans, device,
2259                                 info->chunk_root->root_key.objectid,
2260                                 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2261                                 start, dev_offset, calc_size);
2262                 BUG_ON(ret);
2263                 index++;
2264         }
2265
2266         return 0;
2267 }
2268
2269 static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
2270                                 struct btrfs_root *extent_root,
2271                                 struct map_lookup *map, u64 chunk_offset,
2272                                 u64 chunk_size, u64 stripe_size)
2273 {
2274         u64 dev_offset;
2275         struct btrfs_key key;
2276         struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2277         struct btrfs_device *device;
2278         struct btrfs_chunk *chunk;
2279         struct btrfs_stripe *stripe;
2280         size_t item_size = btrfs_chunk_item_size(map->num_stripes);
2281         int index = 0;
2282         int ret;
2283
2284         chunk = kzalloc(item_size, GFP_NOFS);
2285         if (!chunk)
2286                 return -ENOMEM;
2287
2288         index = 0;
2289         while (index < map->num_stripes) {
2290                 device = map->stripes[index].dev;
2291                 device->bytes_used += stripe_size;
2292                 ret = btrfs_update_device(trans, device);
2293                 BUG_ON(ret);
2294                 index++;
2295         }
2296
2297         index = 0;
2298         stripe = &chunk->stripe;
2299         while (index < map->num_stripes) {
2300                 device = map->stripes[index].dev;
2301                 dev_offset = map->stripes[index].physical;
2302
2303                 btrfs_set_stack_stripe_devid(stripe, device->devid);
2304                 btrfs_set_stack_stripe_offset(stripe, dev_offset);
2305                 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
2306                 stripe++;
2307                 index++;
2308         }
2309
2310         btrfs_set_stack_chunk_length(chunk, chunk_size);
2311         btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
2312         btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
2313         btrfs_set_stack_chunk_type(chunk, map->type);
2314         btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
2315         btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
2316         btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
2317         btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
2318         btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
2319
2320         key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2321         key.type = BTRFS_CHUNK_ITEM_KEY;
2322         key.offset = chunk_offset;
2323
2324         ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
2325         BUG_ON(ret);
2326
2327         if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2328                 ret = btrfs_add_system_chunk(trans, chunk_root, &key, chunk,
2329                                              item_size);
2330                 BUG_ON(ret);
2331         }
2332         kfree(chunk);
2333         return 0;
2334 }
2335
2336 /*
2337  * Chunk allocation falls into two parts. The first part does works
2338  * that make the new allocated chunk useable, but not do any operation
2339  * that modifies the chunk tree. The second part does the works that
2340  * require modifying the chunk tree. This division is important for the
2341  * bootstrap process of adding storage to a seed btrfs.
2342  */
2343 int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
2344                       struct btrfs_root *extent_root, u64 type)
2345 {
2346         u64 chunk_offset;
2347         u64 chunk_size;
2348         u64 stripe_size;
2349         struct map_lookup *map;
2350         struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
2351         int ret;
2352
2353         ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
2354                               &chunk_offset);
2355         if (ret)
2356                 return ret;
2357
2358         ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2359                                   &stripe_size, chunk_offset, type);
2360         if (ret)
2361                 return ret;
2362
2363         ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2364                                    chunk_size, stripe_size);
2365         BUG_ON(ret);
2366         return 0;
2367 }
2368
2369 static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
2370                                          struct btrfs_root *root,
2371                                          struct btrfs_device *device)
2372 {
2373         u64 chunk_offset;
2374         u64 sys_chunk_offset;
2375         u64 chunk_size;
2376         u64 sys_chunk_size;
2377         u64 stripe_size;
2378         u64 sys_stripe_size;
2379         u64 alloc_profile;
2380         struct map_lookup *map;
2381         struct map_lookup *sys_map;
2382         struct btrfs_fs_info *fs_info = root->fs_info;
2383         struct btrfs_root *extent_root = fs_info->extent_root;
2384         int ret;
2385
2386         ret = find_next_chunk(fs_info->chunk_root,
2387                               BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
2388         BUG_ON(ret);
2389
2390         alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
2391                         (fs_info->metadata_alloc_profile &
2392                          fs_info->avail_metadata_alloc_bits);
2393         alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2394
2395         ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
2396                                   &stripe_size, chunk_offset, alloc_profile);
2397         BUG_ON(ret);
2398
2399         sys_chunk_offset = chunk_offset + chunk_size;
2400
2401         alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
2402                         (fs_info->system_alloc_profile &
2403                          fs_info->avail_system_alloc_bits);
2404         alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
2405
2406         ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
2407                                   &sys_chunk_size, &sys_stripe_size,
2408                                   sys_chunk_offset, alloc_profile);
2409         BUG_ON(ret);
2410
2411         ret = btrfs_add_device(trans, fs_info->chunk_root, device);
2412         BUG_ON(ret);
2413
2414         /*
2415          * Modifying chunk tree needs allocating new blocks from both
2416          * system block group and metadata block group. So we only can
2417          * do operations require modifying the chunk tree after both
2418          * block groups were created.
2419          */
2420         ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
2421                                    chunk_size, stripe_size);
2422         BUG_ON(ret);
2423
2424         ret = __finish_chunk_alloc(trans, extent_root, sys_map,
2425                                    sys_chunk_offset, sys_chunk_size,
2426                                    sys_stripe_size);
2427         BUG_ON(ret);
2428         return 0;
2429 }
2430
2431 int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
2432 {
2433         struct extent_map *em;
2434         struct map_lookup *map;
2435         struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
2436         int readonly = 0;
2437         int i;
2438
2439         spin_lock(&map_tree->map_tree.lock);
2440         em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
2441         spin_unlock(&map_tree->map_tree.lock);
2442         if (!em)
2443                 return 1;
2444
2445         map = (struct map_lookup *)em->bdev;
2446         for (i = 0; i < map->num_stripes; i++) {
2447                 if (!map->stripes[i].dev->writeable) {
2448                         readonly = 1;
2449                         break;
2450                 }
2451         }
2452         free_extent_map(em);
2453         return readonly;
2454 }
2455
2456 void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
2457 {
2458         extent_map_tree_init(&tree->map_tree, GFP_NOFS);
2459 }
2460
2461 void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
2462 {
2463         struct extent_map *em;
2464
2465         while (1) {
2466                 spin_lock(&tree->map_tree.lock);
2467                 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
2468                 if (em)
2469                         remove_extent_mapping(&tree->map_tree, em);
2470                 spin_unlock(&tree->map_tree.lock);
2471                 if (!em)
2472                         break;
2473                 kfree(em->bdev);
2474                 /* once for us */
2475                 free_extent_map(em);
2476                 /* once for the tree */
2477                 free_extent_map(em);
2478         }
2479 }
2480
2481 int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
2482 {
2483         struct extent_map *em;
2484         struct map_lookup *map;
2485         struct extent_map_tree *em_tree = &map_tree->map_tree;
2486         int ret;
2487
2488         spin_lock(&em_tree->lock);
2489         em = lookup_extent_mapping(em_tree, logical, len);
2490         spin_unlock(&em_tree->lock);
2491         BUG_ON(!em);
2492
2493         BUG_ON(em->start > logical || em->start + em->len < logical);
2494         map = (struct map_lookup *)em->bdev;
2495         if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
2496                 ret = map->num_stripes;
2497         else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
2498                 ret = map->sub_stripes;
2499         else
2500                 ret = 1;
2501         free_extent_map(em);
2502         return ret;
2503 }
2504
2505 static int find_live_mirror(struct map_lookup *map, int first, int num,
2506                             int optimal)
2507 {
2508         int i;
2509         if (map->stripes[optimal].dev->bdev)
2510                 return optimal;
2511         for (i = first; i < first + num; i++) {
2512                 if (map->stripes[i].dev->bdev)
2513                         return i;
2514         }
2515         /* we couldn't find one that doesn't fail.  Just return something
2516          * and the io error handling code will clean up eventually
2517          */
2518         return optimal;
2519 }
2520
2521 static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2522                              u64 logical, u64 *length,
2523                              struct btrfs_multi_bio **multi_ret,
2524                              int mirror_num, struct page *unplug_page)
2525 {
2526         struct extent_map *em;
2527         struct map_lookup *map;
2528         struct extent_map_tree *em_tree = &map_tree->map_tree;
2529         u64 offset;
2530         u64 stripe_offset;
2531         u64 stripe_nr;
2532         int stripes_allocated = 8;
2533         int stripes_required = 1;
2534         int stripe_index;
2535         int i;
2536         int num_stripes;
2537         int max_errors = 0;
2538         struct btrfs_multi_bio *multi = NULL;
2539
2540         if (multi_ret && !(rw & (1 << BIO_RW)))
2541                 stripes_allocated = 1;
2542 again:
2543         if (multi_ret) {
2544                 multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
2545                                 GFP_NOFS);
2546                 if (!multi)
2547                         return -ENOMEM;
2548
2549                 atomic_set(&multi->error, 0);
2550         }
2551
2552         spin_lock(&em_tree->lock);
2553         em = lookup_extent_mapping(em_tree, logical, *length);
2554         spin_unlock(&em_tree->lock);
2555
2556         if (!em && unplug_page)
2557                 return 0;
2558
2559         if (!em) {
2560                 printk(KERN_CRIT "unable to find logical %llu len %llu\n",
2561                        (unsigned long long)logical,
2562                        (unsigned long long)*length);
2563                 BUG();
2564         }
2565
2566         BUG_ON(em->start > logical || em->start + em->len < logical);
2567         map = (struct map_lookup *)em->bdev;
2568         offset = logical - em->start;
2569
2570         if (mirror_num > map->num_stripes)
2571                 mirror_num = 0;
2572
2573         /* if our multi bio struct is too small, back off and try again */
2574         if (rw & (1 << BIO_RW)) {
2575                 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
2576                                  BTRFS_BLOCK_GROUP_DUP)) {
2577                         stripes_required = map->num_stripes;
2578                         max_errors = 1;
2579                 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2580                         stripes_required = map->sub_stripes;
2581                         max_errors = 1;
2582                 }
2583         }
2584         if (multi_ret && (rw & (1 << BIO_RW)) &&
2585             stripes_allocated < stripes_required) {
2586                 stripes_allocated = map->num_stripes;
2587                 free_extent_map(em);
2588                 kfree(multi);
2589                 goto again;
2590         }
2591         stripe_nr = offset;
2592         /*
2593          * stripe_nr counts the total number of stripes we have to stride
2594          * to get to this block
2595          */
2596         do_div(stripe_nr, map->stripe_len);
2597
2598         stripe_offset = stripe_nr * map->stripe_len;
2599         BUG_ON(offset < stripe_offset);
2600
2601         /* stripe_offset is the offset of this block in its stripe*/
2602         stripe_offset = offset - stripe_offset;
2603
2604         if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
2605                          BTRFS_BLOCK_GROUP_RAID10 |
2606                          BTRFS_BLOCK_GROUP_DUP)) {
2607                 /* we limit the length of each bio to what fits in a stripe */
2608                 *length = min_t(u64, em->len - offset,
2609                               map->stripe_len - stripe_offset);
2610         } else {
2611                 *length = em->len - offset;
2612         }
2613
2614         if (!multi_ret && !unplug_page)
2615                 goto out;
2616
2617         num_stripes = 1;
2618         stripe_index = 0;
2619         if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
2620                 if (unplug_page || (rw & (1 << BIO_RW)))
2621                         num_stripes = map->num_stripes;
2622                 else if (mirror_num)
2623                         stripe_index = mirror_num - 1;
2624                 else {
2625                         stripe_index = find_live_mirror(map, 0,
2626                                             map->num_stripes,
2627                                             current->pid % map->num_stripes);
2628                 }
2629
2630         } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
2631                 if (rw & (1 << BIO_RW))
2632                         num_stripes = map->num_stripes;
2633                 else if (mirror_num)
2634                         stripe_index = mirror_num - 1;
2635
2636         } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2637                 int factor = map->num_stripes / map->sub_stripes;
2638
2639                 stripe_index = do_div(stripe_nr, factor);
2640                 stripe_index *= map->sub_stripes;
2641
2642                 if (unplug_page || (rw & (1 << BIO_RW)))
2643                         num_stripes = map->sub_stripes;
2644                 else if (mirror_num)
2645                         stripe_index += mirror_num - 1;
2646                 else {
2647                         stripe_index = find_live_mirror(map, stripe_index,
2648                                               map->sub_stripes, stripe_index +
2649                                               current->pid % map->sub_stripes);
2650                 }
2651         } else {
2652                 /*
2653                  * after this do_div call, stripe_nr is the number of stripes
2654                  * on this device we have to walk to find the data, and
2655                  * stripe_index is the number of our device in the stripe array
2656                  */
2657                 stripe_index = do_div(stripe_nr, map->num_stripes);
2658         }
2659         BUG_ON(stripe_index >= map->num_stripes);
2660
2661         for (i = 0; i < num_stripes; i++) {
2662                 if (unplug_page) {
2663                         struct btrfs_device *device;
2664                         struct backing_dev_info *bdi;
2665
2666                         device = map->stripes[stripe_index].dev;
2667                         if (device->bdev) {
2668                                 bdi = blk_get_backing_dev_info(device->bdev);
2669                                 if (bdi->unplug_io_fn)
2670                                         bdi->unplug_io_fn(bdi, unplug_page);
2671                         }
2672                 } else {
2673                         multi->stripes[i].physical =
2674                                 map->stripes[stripe_index].physical +
2675                                 stripe_offset + stripe_nr * map->stripe_len;
2676                         multi->stripes[i].dev = map->stripes[stripe_index].dev;
2677                 }
2678                 stripe_index++;
2679         }
2680         if (multi_ret) {
2681                 *multi_ret = multi;
2682                 multi->num_stripes = num_stripes;
2683                 multi->max_errors = max_errors;
2684         }
2685 out:
2686         free_extent_map(em);
2687         return 0;
2688 }
2689
2690 int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
2691                       u64 logical, u64 *length,
2692                       struct btrfs_multi_bio **multi_ret, int mirror_num)
2693 {
2694         return __btrfs_map_block(map_tree, rw, logical, length, multi_ret,
2695                                  mirror_num, NULL);
2696 }
2697
2698 int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
2699                      u64 chunk_start, u64 physical, u64 devid,
2700                      u64 **logical, int *naddrs, int *stripe_len)
2701 {
2702         struct extent_map_tree *em_tree = &map_tree->map_tree;
2703         struct extent_map *em;
2704         struct map_lookup *map;
2705         u64 *buf;
2706         u64 bytenr;
2707         u64 length;
2708         u64 stripe_nr;
2709         int i, j, nr = 0;
2710
2711         spin_lock(&em_tree->lock);
2712         em = lookup_extent_mapping(em_tree, chunk_start, 1);
2713         spin_unlock(&em_tree->lock);
2714
2715         BUG_ON(!em || em->start != chunk_start);
2716         map = (struct map_lookup *)em->bdev;
2717
2718         length = em->len;
2719         if (map->type & BTRFS_BLOCK_GROUP_RAID10)
2720                 do_div(length, map->num_stripes / map->sub_stripes);
2721         else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
2722                 do_div(length, map->num_stripes);
2723
2724         buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
2725         BUG_ON(!buf);
2726
2727         for (i = 0; i < map->num_stripes; i++) {
2728                 if (devid && map->stripes[i].dev->devid != devid)
2729                         continue;
2730                 if (map->stripes[i].physical > physical ||
2731                     map->stripes[i].physical + length <= physical)
2732                         continue;
2733
2734                 stripe_nr = physical - map->stripes[i].physical;
2735                 do_div(stripe_nr, map->stripe_len);
2736
2737                 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
2738                         stripe_nr = stripe_nr * map->num_stripes + i;
2739                         do_div(stripe_nr, map->sub_stripes);
2740                 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
2741                         stripe_nr = stripe_nr * map->num_stripes + i;
2742                 }
2743                 bytenr = chunk_start + stripe_nr * map->stripe_len;
2744                 WARN_ON(nr >= map->num_stripes);
2745                 for (j = 0; j < nr; j++) {
2746                         if (buf[j] == bytenr)
2747                                 break;
2748                 }
2749                 if (j == nr) {
2750                         WARN_ON(nr >= map->num_stripes);
2751                         buf[nr++] = bytenr;
2752                 }
2753         }
2754
2755         for (i = 0; i > nr; i++) {
2756                 struct btrfs_multi_bio *multi;
2757                 struct btrfs_bio_stripe *stripe;
2758                 int ret;
2759
2760                 length = 1;
2761                 ret = btrfs_map_block(map_tree, WRITE, buf[i],
2762                                       &length, &multi, 0);
2763                 BUG_ON(ret);
2764
2765                 stripe = multi->stripes;
2766                 for (j = 0; j < multi->num_stripes; j++) {
2767                         if (stripe->physical >= physical &&
2768                             physical < stripe->physical + length)
2769                                 break;
2770                 }
2771                 BUG_ON(j >= multi->num_stripes);
2772                 kfree(multi);
2773         }
2774
2775         *logical = buf;
2776         *naddrs = nr;
2777         *stripe_len = map->stripe_len;
2778
2779         free_extent_map(em);
2780         return 0;
2781 }
2782
2783 int btrfs_unplug_page(struct btrfs_mapping_tree *map_tree,
2784                       u64 logical, struct page *page)
2785 {
2786         u64 length = PAGE_CACHE_SIZE;
2787         return __btrfs_map_block(map_tree, READ, logical, &length,
2788                                  NULL, 0, page);
2789 }
2790
2791 static void end_bio_multi_stripe(struct bio *bio, int err)
2792 {
2793         struct btrfs_multi_bio *multi = bio->bi_private;
2794         int is_orig_bio = 0;
2795
2796         if (err)
2797                 atomic_inc(&multi->error);
2798
2799         if (bio == multi->orig_bio)
2800                 is_orig_bio = 1;
2801
2802         if (atomic_dec_and_test(&multi->stripes_pending)) {
2803                 if (!is_orig_bio) {
2804                         bio_put(bio);
2805                         bio = multi->orig_bio;
2806                 }
2807                 bio->bi_private = multi->private;
2808                 bio->bi_end_io = multi->end_io;
2809                 /* only send an error to the higher layers if it is
2810                  * beyond the tolerance of the multi-bio
2811                  */
2812                 if (atomic_read(&multi->error) > multi->max_errors) {
2813                         err = -EIO;
2814                 } else if (err) {
2815                         /*
2816                          * this bio is actually up to date, we didn't
2817                          * go over the max number of errors
2818                          */
2819                         set_bit(BIO_UPTODATE, &bio->bi_flags);
2820                         err = 0;
2821                 }
2822                 kfree(multi);
2823
2824                 bio_endio(bio, err);
2825         } else if (!is_orig_bio) {
2826                 bio_put(bio);
2827         }
2828 }
2829
2830 struct async_sched {
2831         struct bio *bio;
2832         int rw;
2833         struct btrfs_fs_info *info;
2834         struct btrfs_work work;
2835 };
2836
2837 /*
2838  * see run_scheduled_bios for a description of why bios are collected for
2839  * async submit.
2840  *
2841  * This will add one bio to the pending list for a device and make sure
2842  * the work struct is scheduled.
2843  */
2844 static noinline int schedule_bio(struct btrfs_root *root,
2845                                  struct btrfs_device *device,
2846                                  int rw, struct bio *bio)
2847 {
2848         int should_queue = 1;
2849         struct btrfs_pending_bios *pending_bios;
2850
2851         /* don't bother with additional async steps for reads, right now */
2852         if (!(rw & (1 << BIO_RW))) {
2853                 bio_get(bio);
2854                 submit_bio(rw, bio);
2855                 bio_put(bio);
2856                 return 0;
2857         }
2858
2859         /*
2860          * nr_async_bios allows us to reliably return congestion to the
2861          * higher layers.  Otherwise, the async bio makes it appear we have
2862          * made progress against dirty pages when we've really just put it
2863          * on a queue for later
2864          */
2865         atomic_inc(&root->fs_info->nr_async_bios);
2866         WARN_ON(bio->bi_next);
2867         bio->bi_next = NULL;
2868         bio->bi_rw |= rw;
2869
2870         spin_lock(&device->io_lock);
2871         if (bio_sync(bio))
2872                 pending_bios = &device->pending_sync_bios;
2873         else
2874                 pending_bios = &device->pending_bios;
2875
2876         if (pending_bios->tail)
2877                 pending_bios->tail->bi_next = bio;
2878
2879         pending_bios->tail = bio;
2880         if (!pending_bios->head)
2881                 pending_bios->head = bio;
2882         if (device->running_pending)
2883                 should_queue = 0;
2884
2885         spin_unlock(&device->io_lock);
2886
2887         if (should_queue)
2888                 btrfs_queue_worker(&root->fs_info->submit_workers,
2889                                    &device->work);
2890         return 0;
2891 }
2892
2893 int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
2894                   int mirror_num, int async_submit)
2895 {
2896         struct btrfs_mapping_tree *map_tree;
2897         struct btrfs_device *dev;
2898         struct bio *first_bio = bio;
2899         u64 logical = (u64)bio->bi_sector << 9;
2900         u64 length = 0;
2901         u64 map_length;
2902         struct btrfs_multi_bio *multi = NULL;
2903         int ret;
2904         int dev_nr = 0;
2905         int total_devs = 1;
2906
2907         length = bio->bi_size;
2908         map_tree = &root->fs_info->mapping_tree;
2909         map_length = length;
2910
2911         ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi,
2912                               mirror_num);
2913         BUG_ON(ret);
2914
2915         total_devs = multi->num_stripes;
2916         if (map_length < length) {
2917                 printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
2918                        "len %llu\n", (unsigned long long)logical,
2919                        (unsigned long long)length,
2920                        (unsigned long long)map_length);
2921                 BUG();
2922         }
2923         multi->end_io = first_bio->bi_end_io;
2924         multi->private = first_bio->bi_private;
2925         multi->orig_bio = first_bio;
2926         atomic_set(&multi->stripes_pending, multi->num_stripes);
2927
2928         while (dev_nr < total_devs) {
2929                 if (total_devs > 1) {
2930                         if (dev_nr < total_devs - 1) {
2931                                 bio = bio_clone(first_bio, GFP_NOFS);
2932                                 BUG_ON(!bio);
2933                         } else {
2934                                 bio = first_bio;
2935                         }
2936                         bio->bi_private = multi;
2937                         bio->bi_end_io = end_bio_multi_stripe;
2938                 }
2939                 bio->bi_sector = multi->stripes[dev_nr].physical >> 9;
2940                 dev = multi->stripes[dev_nr].dev;
2941                 BUG_ON(rw == WRITE && !dev->writeable);
2942                 if (dev && dev->bdev) {
2943                         bio->bi_bdev = dev->bdev;
2944                         if (async_submit)
2945                                 schedule_bio(root, dev, rw, bio);
2946                         else
2947                                 submit_bio(rw, bio);
2948                 } else {
2949                         bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
2950                         bio->bi_sector = logical >> 9;
2951                         bio_endio(bio, -EIO);
2952                 }
2953                 dev_nr++;
2954         }
2955         if (total_devs == 1)
2956                 kfree(multi);
2957         return 0;
2958 }
2959
2960 struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
2961                                        u8 *uuid, u8 *fsid)
2962 {
2963         struct btrfs_device *device;
2964         struct btrfs_fs_devices *cur_devices;
2965
2966         cur_devices = root->fs_info->fs_devices;
2967         while (cur_devices) {
2968                 if (!fsid ||
2969                     !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
2970                         device = __find_device(&cur_devices->devices,
2971                                                devid, uuid);
2972                         if (device)
2973                                 return device;
2974                 }
2975                 cur_devices = cur_devices->seed;
2976         }
2977         return NULL;
2978 }
2979
2980 static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
2981                                             u64 devid, u8 *dev_uuid)
2982 {
2983         struct btrfs_device *device;
2984         struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2985
2986         device = kzalloc(sizeof(*device), GFP_NOFS);
2987         if (!device)
2988                 return NULL;
2989         list_add(&device->dev_list,
2990                  &fs_devices->devices);
2991         device->barriers = 1;
2992         device->dev_root = root->fs_info->dev_root;
2993         device->devid = devid;
2994         device->work.func = pending_bios_fn;
2995         device->fs_devices = fs_devices;
2996         fs_devices->num_devices++;
2997         spin_lock_init(&device->io_lock);
2998         INIT_LIST_HEAD(&device->dev_alloc_list);
2999         memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
3000         return device;
3001 }
3002
3003 static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
3004                           struct extent_buffer *leaf,
3005                           struct btrfs_chunk *chunk)
3006 {
3007         struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
3008         struct map_lookup *map;
3009         struct extent_map *em;
3010         u64 logical;
3011         u64 length;
3012         u64 devid;
3013         u8 uuid[BTRFS_UUID_SIZE];
3014         int num_stripes;
3015         int ret;
3016         int i;
3017
3018         logical = key->offset;
3019         length = btrfs_chunk_length(leaf, chunk);
3020
3021         spin_lock(&map_tree->map_tree.lock);
3022         em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
3023         spin_unlock(&map_tree->map_tree.lock);
3024
3025         /* already mapped? */
3026         if (em && em->start <= logical && em->start + em->len > logical) {
3027                 free_extent_map(em);
3028                 return 0;
3029         } else if (em) {
3030                 free_extent_map(em);
3031         }
3032
3033         em = alloc_extent_map(GFP_NOFS);
3034         if (!em)
3035                 return -ENOMEM;
3036         num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3037         map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
3038         if (!map) {
3039                 free_extent_map(em);
3040                 return -ENOMEM;
3041         }
3042
3043         em->bdev = (struct block_device *)map;
3044         em->start = logical;
3045         em->len = length;
3046         em->block_start = 0;
3047         em->block_len = em->len;
3048
3049         map->num_stripes = num_stripes;
3050         map->io_width = btrfs_chunk_io_width(leaf, chunk);
3051         map->io_align = btrfs_chunk_io_align(leaf, chunk);
3052         map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
3053         map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
3054         map->type = btrfs_chunk_type(leaf, chunk);
3055         map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
3056         for (i = 0; i < num_stripes; i++) {
3057                 map->stripes[i].physical =
3058                         btrfs_stripe_offset_nr(leaf, chunk, i);
3059                 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
3060                 read_extent_buffer(leaf, uuid, (unsigned long)
3061                                    btrfs_stripe_dev_uuid_nr(chunk, i),
3062                                    BTRFS_UUID_SIZE);
3063                 map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
3064                                                         NULL);
3065                 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
3066                         kfree(map);
3067                         free_extent_map(em);
3068                         return -EIO;
3069                 }
3070                 if (!map->stripes[i].dev) {
3071                         map->stripes[i].dev =
3072                                 add_missing_dev(root, devid, uuid);
3073                         if (!map->stripes[i].dev) {
3074                                 kfree(map);
3075                                 free_extent_map(em);
3076                                 return -EIO;
3077                         }
3078                 }
3079                 map->stripes[i].dev->in_fs_metadata = 1;
3080         }
3081
3082         spin_lock(&map_tree->map_tree.lock);
3083         ret = add_extent_mapping(&map_tree->map_tree, em);
3084         spin_unlock(&map_tree->map_tree.lock);
3085         BUG_ON(ret);
3086         free_extent_map(em);
3087
3088         return 0;
3089 }
3090
3091 static int fill_device_from_item(struct extent_buffer *leaf,
3092                                  struct btrfs_dev_item *dev_item,
3093                                  struct btrfs_device *device)
3094 {
3095         unsigned long ptr;
3096
3097         device->devid = btrfs_device_id(leaf, dev_item);
3098         device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
3099         device->total_bytes = device->disk_total_bytes;
3100         device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
3101         device->type = btrfs_device_type(leaf, dev_item);
3102         device->io_align = btrfs_device_io_align(leaf, dev_item);
3103         device->io_width = btrfs_device_io_width(leaf, dev_item);
3104         device->sector_size = btrfs_device_sector_size(leaf, dev_item);
3105
3106         ptr = (unsigned long)btrfs_device_uuid(dev_item);
3107         read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
3108
3109         return 0;
3110 }
3111
3112 static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
3113 {
3114         struct btrfs_fs_devices *fs_devices;
3115         int ret;
3116
3117         mutex_lock(&uuid_mutex);
3118
3119         fs_devices = root->fs_info->fs_devices->seed;
3120         while (fs_devices) {
3121                 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
3122                         ret = 0;
3123                         goto out;
3124                 }
3125                 fs_devices = fs_devices->seed;
3126         }
3127
3128         fs_devices = find_fsid(fsid);
3129         if (!fs_devices) {
3130                 ret = -ENOENT;
3131                 goto out;
3132         }
3133
3134         fs_devices = clone_fs_devices(fs_devices);
3135         if (IS_ERR(fs_devices)) {
3136                 ret = PTR_ERR(fs_devices);
3137                 goto out;
3138         }
3139
3140         ret = __btrfs_open_devices(fs_devices, FMODE_READ,
3141                                    root->fs_info->bdev_holder);
3142         if (ret)
3143                 goto out;
3144
3145         if (!fs_devices->seeding) {
3146                 __btrfs_close_devices(fs_devices);
3147                 free_fs_devices(fs_devices);
3148                 ret = -EINVAL;
3149                 goto out;
3150         }
3151
3152         fs_devices->seed = root->fs_info->fs_devices->seed;
3153         root->fs_info->fs_devices->seed = fs_devices;
3154 out:
3155         mutex_unlock(&uuid_mutex);
3156         return ret;
3157 }
3158
3159 static int read_one_dev(struct btrfs_root *root,
3160                         struct extent_buffer *leaf,
3161                         struct btrfs_dev_item *dev_item)
3162 {
3163         struct btrfs_device *device;
3164         u64 devid;
3165         int ret;
3166         u8 fs_uuid[BTRFS_UUID_SIZE];
3167         u8 dev_uuid[BTRFS_UUID_SIZE];
3168
3169         devid = btrfs_device_id(leaf, dev_item);
3170         read_extent_buffer(leaf, dev_uuid,
3171                            (unsigned long)btrfs_device_uuid(dev_item),
3172                            BTRFS_UUID_SIZE);
3173         read_extent_buffer(leaf, fs_uuid,
3174                            (unsigned long)btrfs_device_fsid(dev_item),
3175                            BTRFS_UUID_SIZE);
3176
3177         if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
3178                 ret = open_seed_devices(root, fs_uuid);
3179                 if (ret && !btrfs_test_opt(root, DEGRADED))
3180                         return ret;
3181         }
3182
3183         device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
3184         if (!device || !device->bdev) {
3185                 if (!btrfs_test_opt(root, DEGRADED))
3186                         return -EIO;
3187
3188                 if (!device) {
3189                         printk(KERN_WARNING "warning devid %llu missing\n",
3190                                (unsigned long long)devid);
3191                         device = add_missing_dev(root, devid, dev_uuid);
3192                         if (!device)
3193                                 return -ENOMEM;
3194                 }
3195         }
3196
3197         if (device->fs_devices != root->fs_info->fs_devices) {
3198                 BUG_ON(device->writeable);
3199                 if (device->generation !=
3200                     btrfs_device_generation(leaf, dev_item))
3201                         return -EINVAL;
3202         }
3203
3204         fill_device_from_item(leaf, dev_item, device);
3205         device->dev_root = root->fs_info->dev_root;
3206         device->in_fs_metadata = 1;
3207         if (device->writeable)
3208                 device->fs_devices->total_rw_bytes += device->total_bytes;
3209         ret = 0;
3210         return ret;
3211 }
3212
3213 int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf)
3214 {
3215         struct btrfs_dev_item *dev_item;
3216
3217         dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
3218                                                      dev_item);
3219         return read_one_dev(root, buf, dev_item);
3220 }
3221
3222 int btrfs_read_sys_array(struct btrfs_root *root)
3223 {
3224         struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
3225         struct extent_buffer *sb;
3226         struct btrfs_disk_key *disk_key;
3227         struct btrfs_chunk *chunk;
3228         u8 *ptr;
3229         unsigned long sb_ptr;
3230         int ret = 0;
3231         u32 num_stripes;
3232         u32 array_size;
3233         u32 len = 0;
3234         u32 cur;
3235         struct btrfs_key key;
3236
3237         sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
3238                                           BTRFS_SUPER_INFO_SIZE);
3239         if (!sb)
3240                 return -ENOMEM;
3241         btrfs_set_buffer_uptodate(sb);
3242         btrfs_set_buffer_lockdep_class(sb, 0);
3243
3244         write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
3245         array_size = btrfs_super_sys_array_size(super_copy);
3246
3247         ptr = super_copy->sys_chunk_array;
3248         sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
3249         cur = 0;
3250
3251         while (cur < array_size) {
3252                 disk_key = (struct btrfs_disk_key *)ptr;
3253                 btrfs_disk_key_to_cpu(&key, disk_key);
3254
3255                 len = sizeof(*disk_key); ptr += len;
3256                 sb_ptr += len;
3257                 cur += len;
3258
3259                 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
3260                         chunk = (struct btrfs_chunk *)sb_ptr;
3261                         ret = read_one_chunk(root, &key, sb, chunk);
3262                         if (ret)
3263                                 break;
3264                         num_stripes = btrfs_chunk_num_stripes(sb, chunk);
3265                         len = btrfs_chunk_item_size(num_stripes);
3266                 } else {
3267                         ret = -EIO;
3268                         break;
3269                 }
3270                 ptr += len;
3271                 sb_ptr += len;
3272                 cur += len;
3273         }
3274         free_extent_buffer(sb);
3275         return ret;
3276 }
3277
3278 int btrfs_read_chunk_tree(struct btrfs_root *root)
3279 {
3280         struct btrfs_path *path;
3281         struct extent_buffer *leaf;
3282         struct btrfs_key key;
3283         struct btrfs_key found_key;
3284         int ret;
3285         int slot;
3286
3287         root = root->fs_info->chunk_root;
3288
3289         path = btrfs_alloc_path();
3290         if (!path)
3291                 return -ENOMEM;
3292
3293         /* first we search for all of the device items, and then we
3294          * read in all of the chunk items.  This way we can create chunk
3295          * mappings that reference all of the devices that are afound
3296          */
3297         key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
3298         key.offset = 0;
3299         key.type = 0;
3300 again:
3301         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3302         while (1) {
3303                 leaf = path->nodes[0];
3304                 slot = path->slots[0];
3305                 if (slot >= btrfs_header_nritems(leaf)) {
3306                         ret = btrfs_next_leaf(root, path);
3307                         if (ret == 0)
3308                                 continue;
3309                         if (ret < 0)
3310                                 goto error;
3311                         break;
3312                 }
3313                 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3314                 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3315                         if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
3316                                 break;
3317                         if (found_key.type == BTRFS_DEV_ITEM_KEY) {
3318                                 struct btrfs_dev_item *dev_item;
3319                                 dev_item = btrfs_item_ptr(leaf, slot,
3320                                                   struct btrfs_dev_item);
3321                                 ret = read_one_dev(root, leaf, dev_item);
3322                                 if (ret)
3323                                         goto error;
3324                         }
3325                 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
3326                         struct btrfs_chunk *chunk;
3327                         chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3328                         ret = read_one_chunk(root, &found_key, leaf, chunk);
3329                         if (ret)
3330                                 goto error;
3331                 }
3332                 path->slots[0]++;
3333         }
3334         if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
3335                 key.objectid = 0;
3336                 btrfs_release_path(root, path);
3337                 goto again;
3338         }
3339         ret = 0;
3340 error:
3341         btrfs_free_path(path);
3342         return ret;
3343 }