256e57eec8ecb342c80f5e0299c25dba18a75985
[sfrench/cifs-2.6.git] / fs / btrfs / volumes.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2007 Oracle.  All rights reserved.
4  */
5
6 #include <linux/sched.h>
7 #include <linux/bio.h>
8 #include <linux/slab.h>
9 #include <linux/buffer_head.h>
10 #include <linux/blkdev.h>
11 #include <linux/iocontext.h>
12 #include <linux/capability.h>
13 #include <linux/ratelimit.h>
14 #include <linux/kthread.h>
15 #include <linux/raid/pq.h>
16 #include <linux/semaphore.h>
17 #include <linux/uuid.h>
18 #include <linux/list_sort.h>
19 #include <asm/div64.h>
20 #include "ctree.h"
21 #include "extent_map.h"
22 #include "disk-io.h"
23 #include "transaction.h"
24 #include "print-tree.h"
25 #include "volumes.h"
26 #include "raid56.h"
27 #include "async-thread.h"
28 #include "check-integrity.h"
29 #include "rcu-string.h"
30 #include "math.h"
31 #include "dev-replace.h"
32 #include "sysfs.h"
33
34 const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
35         [BTRFS_RAID_RAID10] = {
36                 .sub_stripes    = 2,
37                 .dev_stripes    = 1,
38                 .devs_max       = 0,    /* 0 == as many as possible */
39                 .devs_min       = 4,
40                 .tolerated_failures = 1,
41                 .devs_increment = 2,
42                 .ncopies        = 2,
43                 .raid_name      = "raid10",
44                 .bg_flag        = BTRFS_BLOCK_GROUP_RAID10,
45                 .mindev_error   = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET,
46         },
47         [BTRFS_RAID_RAID1] = {
48                 .sub_stripes    = 1,
49                 .dev_stripes    = 1,
50                 .devs_max       = 2,
51                 .devs_min       = 2,
52                 .tolerated_failures = 1,
53                 .devs_increment = 2,
54                 .ncopies        = 2,
55                 .raid_name      = "raid1",
56                 .bg_flag        = BTRFS_BLOCK_GROUP_RAID1,
57                 .mindev_error   = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET,
58         },
59         [BTRFS_RAID_DUP] = {
60                 .sub_stripes    = 1,
61                 .dev_stripes    = 2,
62                 .devs_max       = 1,
63                 .devs_min       = 1,
64                 .tolerated_failures = 0,
65                 .devs_increment = 1,
66                 .ncopies        = 2,
67                 .raid_name      = "dup",
68                 .bg_flag        = BTRFS_BLOCK_GROUP_DUP,
69                 .mindev_error   = 0,
70         },
71         [BTRFS_RAID_RAID0] = {
72                 .sub_stripes    = 1,
73                 .dev_stripes    = 1,
74                 .devs_max       = 0,
75                 .devs_min       = 2,
76                 .tolerated_failures = 0,
77                 .devs_increment = 1,
78                 .ncopies        = 1,
79                 .raid_name      = "raid0",
80                 .bg_flag        = BTRFS_BLOCK_GROUP_RAID0,
81                 .mindev_error   = 0,
82         },
83         [BTRFS_RAID_SINGLE] = {
84                 .sub_stripes    = 1,
85                 .dev_stripes    = 1,
86                 .devs_max       = 1,
87                 .devs_min       = 1,
88                 .tolerated_failures = 0,
89                 .devs_increment = 1,
90                 .ncopies        = 1,
91                 .raid_name      = "single",
92                 .bg_flag        = 0,
93                 .mindev_error   = 0,
94         },
95         [BTRFS_RAID_RAID5] = {
96                 .sub_stripes    = 1,
97                 .dev_stripes    = 1,
98                 .devs_max       = 0,
99                 .devs_min       = 2,
100                 .tolerated_failures = 1,
101                 .devs_increment = 1,
102                 .ncopies        = 2,
103                 .raid_name      = "raid5",
104                 .bg_flag        = BTRFS_BLOCK_GROUP_RAID5,
105                 .mindev_error   = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET,
106         },
107         [BTRFS_RAID_RAID6] = {
108                 .sub_stripes    = 1,
109                 .dev_stripes    = 1,
110                 .devs_max       = 0,
111                 .devs_min       = 3,
112                 .tolerated_failures = 2,
113                 .devs_increment = 1,
114                 .ncopies        = 3,
115                 .raid_name      = "raid6",
116                 .bg_flag        = BTRFS_BLOCK_GROUP_RAID6,
117                 .mindev_error   = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET,
118         },
119 };
120
121 const char *get_raid_name(enum btrfs_raid_types type)
122 {
123         if (type >= BTRFS_NR_RAID_TYPES)
124                 return NULL;
125
126         return btrfs_raid_array[type].raid_name;
127 }
128
129 static int init_first_rw_device(struct btrfs_trans_handle *trans,
130                                 struct btrfs_fs_info *fs_info);
131 static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info);
132 static void __btrfs_reset_dev_stats(struct btrfs_device *dev);
133 static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev);
134 static void btrfs_dev_stat_print_on_load(struct btrfs_device *device);
135 static int __btrfs_map_block(struct btrfs_fs_info *fs_info,
136                              enum btrfs_map_op op,
137                              u64 logical, u64 *length,
138                              struct btrfs_bio **bbio_ret,
139                              int mirror_num, int need_raid_map);
140
141 /*
142  * Device locking
143  * ==============
144  *
145  * There are several mutexes that protect manipulation of devices and low-level
146  * structures like chunks but not block groups, extents or files
147  *
148  * uuid_mutex (global lock)
149  * ------------------------
150  * protects the fs_uuids list that tracks all per-fs fs_devices, resulting from
151  * the SCAN_DEV ioctl registration or from mount either implicitly (the first
152  * device) or requested by the device= mount option
153  *
154  * the mutex can be very coarse and can cover long-running operations
155  *
156  * protects: updates to fs_devices counters like missing devices, rw devices,
157  * seeding, structure cloning, openning/closing devices at mount/umount time
158  *
159  * global::fs_devs - add, remove, updates to the global list
160  *
161  * does not protect: manipulation of the fs_devices::devices list!
162  *
163  * btrfs_device::name - renames (write side), read is RCU
164  *
165  * fs_devices::device_list_mutex (per-fs, with RCU)
166  * ------------------------------------------------
167  * protects updates to fs_devices::devices, ie. adding and deleting
168  *
169  * simple list traversal with read-only actions can be done with RCU protection
170  *
171  * may be used to exclude some operations from running concurrently without any
172  * modifications to the list (see write_all_supers)
173  *
174  * balance_mutex
175  * -------------
176  * protects balance structures (status, state) and context accessed from
177  * several places (internally, ioctl)
178  *
179  * chunk_mutex
180  * -----------
181  * protects chunks, adding or removing during allocation, trim or when a new
182  * device is added/removed
183  *
184  * cleaner_mutex
185  * -------------
186  * a big lock that is held by the cleaner thread and prevents running subvolume
187  * cleaning together with relocation or delayed iputs
188  *
189  *
190  * Lock nesting
191  * ============
192  *
193  * uuid_mutex
194  *   volume_mutex
195  *     device_list_mutex
196  *       chunk_mutex
197  *     balance_mutex
198  *
199  *
200  * Exclusive operations, BTRFS_FS_EXCL_OP
201  * ======================================
202  *
203  * Maintains the exclusivity of the following operations that apply to the
204  * whole filesystem and cannot run in parallel.
205  *
206  * - Balance (*)
207  * - Device add
208  * - Device remove
209  * - Device replace (*)
210  * - Resize
211  *
212  * The device operations (as above) can be in one of the following states:
213  *
214  * - Running state
215  * - Paused state
216  * - Completed state
217  *
218  * Only device operations marked with (*) can go into the Paused state for the
219  * following reasons:
220  *
221  * - ioctl (only Balance can be Paused through ioctl)
222  * - filesystem remounted as read-only
223  * - filesystem unmounted and mounted as read-only
224  * - system power-cycle and filesystem mounted as read-only
225  * - filesystem or device errors leading to forced read-only
226  *
227  * BTRFS_FS_EXCL_OP flag is set and cleared using atomic operations.
228  * During the course of Paused state, the BTRFS_FS_EXCL_OP remains set.
229  * A device operation in Paused or Running state can be canceled or resumed
230  * either by ioctl (Balance only) or when remounted as read-write.
231  * BTRFS_FS_EXCL_OP flag is cleared when the device operation is canceled or
232  * completed.
233  */
234
235 DEFINE_MUTEX(uuid_mutex);
236 static LIST_HEAD(fs_uuids);
237 struct list_head *btrfs_get_fs_uuids(void)
238 {
239         return &fs_uuids;
240 }
241
242 /*
243  * alloc_fs_devices - allocate struct btrfs_fs_devices
244  * @fsid:       if not NULL, copy the uuid to fs_devices::fsid
245  *
246  * Return a pointer to a new struct btrfs_fs_devices on success, or ERR_PTR().
247  * The returned struct is not linked onto any lists and can be destroyed with
248  * kfree() right away.
249  */
250 static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid)
251 {
252         struct btrfs_fs_devices *fs_devs;
253
254         fs_devs = kzalloc(sizeof(*fs_devs), GFP_KERNEL);
255         if (!fs_devs)
256                 return ERR_PTR(-ENOMEM);
257
258         mutex_init(&fs_devs->device_list_mutex);
259
260         INIT_LIST_HEAD(&fs_devs->devices);
261         INIT_LIST_HEAD(&fs_devs->resized_devices);
262         INIT_LIST_HEAD(&fs_devs->alloc_list);
263         INIT_LIST_HEAD(&fs_devs->fs_list);
264         if (fsid)
265                 memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE);
266
267         return fs_devs;
268 }
269
270 void btrfs_free_device(struct btrfs_device *device)
271 {
272         rcu_string_free(device->name);
273         bio_put(device->flush_bio);
274         kfree(device);
275 }
276
277 static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
278 {
279         struct btrfs_device *device;
280         WARN_ON(fs_devices->opened);
281         while (!list_empty(&fs_devices->devices)) {
282                 device = list_entry(fs_devices->devices.next,
283                                     struct btrfs_device, dev_list);
284                 list_del(&device->dev_list);
285                 btrfs_free_device(device);
286         }
287         kfree(fs_devices);
288 }
289
290 static void btrfs_kobject_uevent(struct block_device *bdev,
291                                  enum kobject_action action)
292 {
293         int ret;
294
295         ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
296         if (ret)
297                 pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
298                         action,
299                         kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
300                         &disk_to_dev(bdev->bd_disk)->kobj);
301 }
302
303 void __exit btrfs_cleanup_fs_uuids(void)
304 {
305         struct btrfs_fs_devices *fs_devices;
306
307         while (!list_empty(&fs_uuids)) {
308                 fs_devices = list_entry(fs_uuids.next,
309                                         struct btrfs_fs_devices, fs_list);
310                 list_del(&fs_devices->fs_list);
311                 free_fs_devices(fs_devices);
312         }
313 }
314
315 /*
316  * Returns a pointer to a new btrfs_device on success; ERR_PTR() on error.
317  * Returned struct is not linked onto any lists and must be destroyed using
318  * btrfs_free_device.
319  */
320 static struct btrfs_device *__alloc_device(void)
321 {
322         struct btrfs_device *dev;
323
324         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
325         if (!dev)
326                 return ERR_PTR(-ENOMEM);
327
328         /*
329          * Preallocate a bio that's always going to be used for flushing device
330          * barriers and matches the device lifespan
331          */
332         dev->flush_bio = bio_alloc_bioset(GFP_KERNEL, 0, NULL);
333         if (!dev->flush_bio) {
334                 kfree(dev);
335                 return ERR_PTR(-ENOMEM);
336         }
337
338         INIT_LIST_HEAD(&dev->dev_list);
339         INIT_LIST_HEAD(&dev->dev_alloc_list);
340         INIT_LIST_HEAD(&dev->resized_list);
341
342         spin_lock_init(&dev->io_lock);
343
344         atomic_set(&dev->reada_in_flight, 0);
345         atomic_set(&dev->dev_stats_ccnt, 0);
346         btrfs_device_data_ordered_init(dev);
347         INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
348         INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
349
350         return dev;
351 }
352
353 /*
354  * Find a device specified by @devid or @uuid in the list of @fs_devices, or
355  * return NULL.
356  *
357  * If devid and uuid are both specified, the match must be exact, otherwise
358  * only devid is used.
359  */
360 static struct btrfs_device *find_device(struct btrfs_fs_devices *fs_devices,
361                 u64 devid, const u8 *uuid)
362 {
363         struct btrfs_device *dev;
364
365         list_for_each_entry(dev, &fs_devices->devices, dev_list) {
366                 if (dev->devid == devid &&
367                     (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
368                         return dev;
369                 }
370         }
371         return NULL;
372 }
373
374 static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
375 {
376         struct btrfs_fs_devices *fs_devices;
377
378         list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
379                 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
380                         return fs_devices;
381         }
382         return NULL;
383 }
384
385 static int
386 btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder,
387                       int flush, struct block_device **bdev,
388                       struct buffer_head **bh)
389 {
390         int ret;
391
392         *bdev = blkdev_get_by_path(device_path, flags, holder);
393
394         if (IS_ERR(*bdev)) {
395                 ret = PTR_ERR(*bdev);
396                 goto error;
397         }
398
399         if (flush)
400                 filemap_write_and_wait((*bdev)->bd_inode->i_mapping);
401         ret = set_blocksize(*bdev, BTRFS_BDEV_BLOCKSIZE);
402         if (ret) {
403                 blkdev_put(*bdev, flags);
404                 goto error;
405         }
406         invalidate_bdev(*bdev);
407         *bh = btrfs_read_dev_super(*bdev);
408         if (IS_ERR(*bh)) {
409                 ret = PTR_ERR(*bh);
410                 blkdev_put(*bdev, flags);
411                 goto error;
412         }
413
414         return 0;
415
416 error:
417         *bdev = NULL;
418         *bh = NULL;
419         return ret;
420 }
421
422 static void requeue_list(struct btrfs_pending_bios *pending_bios,
423                         struct bio *head, struct bio *tail)
424 {
425
426         struct bio *old_head;
427
428         old_head = pending_bios->head;
429         pending_bios->head = head;
430         if (pending_bios->tail)
431                 tail->bi_next = old_head;
432         else
433                 pending_bios->tail = tail;
434 }
435
436 /*
437  * we try to collect pending bios for a device so we don't get a large
438  * number of procs sending bios down to the same device.  This greatly
439  * improves the schedulers ability to collect and merge the bios.
440  *
441  * But, it also turns into a long list of bios to process and that is sure
442  * to eventually make the worker thread block.  The solution here is to
443  * make some progress and then put this work struct back at the end of
444  * the list if the block device is congested.  This way, multiple devices
445  * can make progress from a single worker thread.
446  */
447 static noinline void run_scheduled_bios(struct btrfs_device *device)
448 {
449         struct btrfs_fs_info *fs_info = device->fs_info;
450         struct bio *pending;
451         struct backing_dev_info *bdi;
452         struct btrfs_pending_bios *pending_bios;
453         struct bio *tail;
454         struct bio *cur;
455         int again = 0;
456         unsigned long num_run;
457         unsigned long batch_run = 0;
458         unsigned long last_waited = 0;
459         int force_reg = 0;
460         int sync_pending = 0;
461         struct blk_plug plug;
462
463         /*
464          * this function runs all the bios we've collected for
465          * a particular device.  We don't want to wander off to
466          * another device without first sending all of these down.
467          * So, setup a plug here and finish it off before we return
468          */
469         blk_start_plug(&plug);
470
471         bdi = device->bdev->bd_bdi;
472
473 loop:
474         spin_lock(&device->io_lock);
475
476 loop_lock:
477         num_run = 0;
478
479         /* take all the bios off the list at once and process them
480          * later on (without the lock held).  But, remember the
481          * tail and other pointers so the bios can be properly reinserted
482          * into the list if we hit congestion
483          */
484         if (!force_reg && device->pending_sync_bios.head) {
485                 pending_bios = &device->pending_sync_bios;
486                 force_reg = 1;
487         } else {
488                 pending_bios = &device->pending_bios;
489                 force_reg = 0;
490         }
491
492         pending = pending_bios->head;
493         tail = pending_bios->tail;
494         WARN_ON(pending && !tail);
495
496         /*
497          * if pending was null this time around, no bios need processing
498          * at all and we can stop.  Otherwise it'll loop back up again
499          * and do an additional check so no bios are missed.
500          *
501          * device->running_pending is used to synchronize with the
502          * schedule_bio code.
503          */
504         if (device->pending_sync_bios.head == NULL &&
505             device->pending_bios.head == NULL) {
506                 again = 0;
507                 device->running_pending = 0;
508         } else {
509                 again = 1;
510                 device->running_pending = 1;
511         }
512
513         pending_bios->head = NULL;
514         pending_bios->tail = NULL;
515
516         spin_unlock(&device->io_lock);
517
518         while (pending) {
519
520                 rmb();
521                 /* we want to work on both lists, but do more bios on the
522                  * sync list than the regular list
523                  */
524                 if ((num_run > 32 &&
525                     pending_bios != &device->pending_sync_bios &&
526                     device->pending_sync_bios.head) ||
527                    (num_run > 64 && pending_bios == &device->pending_sync_bios &&
528                     device->pending_bios.head)) {
529                         spin_lock(&device->io_lock);
530                         requeue_list(pending_bios, pending, tail);
531                         goto loop_lock;
532                 }
533
534                 cur = pending;
535                 pending = pending->bi_next;
536                 cur->bi_next = NULL;
537
538                 BUG_ON(atomic_read(&cur->__bi_cnt) == 0);
539
540                 /*
541                  * if we're doing the sync list, record that our
542                  * plug has some sync requests on it
543                  *
544                  * If we're doing the regular list and there are
545                  * sync requests sitting around, unplug before
546                  * we add more
547                  */
548                 if (pending_bios == &device->pending_sync_bios) {
549                         sync_pending = 1;
550                 } else if (sync_pending) {
551                         blk_finish_plug(&plug);
552                         blk_start_plug(&plug);
553                         sync_pending = 0;
554                 }
555
556                 btrfsic_submit_bio(cur);
557                 num_run++;
558                 batch_run++;
559
560                 cond_resched();
561
562                 /*
563                  * we made progress, there is more work to do and the bdi
564                  * is now congested.  Back off and let other work structs
565                  * run instead
566                  */
567                 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
568                     fs_info->fs_devices->open_devices > 1) {
569                         struct io_context *ioc;
570
571                         ioc = current->io_context;
572
573                         /*
574                          * the main goal here is that we don't want to
575                          * block if we're going to be able to submit
576                          * more requests without blocking.
577                          *
578                          * This code does two great things, it pokes into
579                          * the elevator code from a filesystem _and_
580                          * it makes assumptions about how batching works.
581                          */
582                         if (ioc && ioc->nr_batch_requests > 0 &&
583                             time_before(jiffies, ioc->last_waited + HZ/50UL) &&
584                             (last_waited == 0 ||
585                              ioc->last_waited == last_waited)) {
586                                 /*
587                                  * we want to go through our batch of
588                                  * requests and stop.  So, we copy out
589                                  * the ioc->last_waited time and test
590                                  * against it before looping
591                                  */
592                                 last_waited = ioc->last_waited;
593                                 cond_resched();
594                                 continue;
595                         }
596                         spin_lock(&device->io_lock);
597                         requeue_list(pending_bios, pending, tail);
598                         device->running_pending = 1;
599
600                         spin_unlock(&device->io_lock);
601                         btrfs_queue_work(fs_info->submit_workers,
602                                          &device->work);
603                         goto done;
604                 }
605         }
606
607         cond_resched();
608         if (again)
609                 goto loop;
610
611         spin_lock(&device->io_lock);
612         if (device->pending_bios.head || device->pending_sync_bios.head)
613                 goto loop_lock;
614         spin_unlock(&device->io_lock);
615
616 done:
617         blk_finish_plug(&plug);
618 }
619
620 static void pending_bios_fn(struct btrfs_work *work)
621 {
622         struct btrfs_device *device;
623
624         device = container_of(work, struct btrfs_device, work);
625         run_scheduled_bios(device);
626 }
627
628 /*
629  *  Search and remove all stale (devices which are not mounted) devices.
630  *  When both inputs are NULL, it will search and release all stale devices.
631  *  path:       Optional. When provided will it release all unmounted devices
632  *              matching this path only.
633  *  skip_dev:   Optional. Will skip this device when searching for the stale
634  *              devices.
635  */
636 static void btrfs_free_stale_devices(const char *path,
637                                      struct btrfs_device *skip_dev)
638 {
639         struct btrfs_fs_devices *fs_devs, *tmp_fs_devs;
640         struct btrfs_device *dev, *tmp_dev;
641
642         list_for_each_entry_safe(fs_devs, tmp_fs_devs, &fs_uuids, fs_list) {
643
644                 if (fs_devs->opened)
645                         continue;
646
647                 list_for_each_entry_safe(dev, tmp_dev,
648                                          &fs_devs->devices, dev_list) {
649                         int not_found = 0;
650
651                         if (skip_dev && skip_dev == dev)
652                                 continue;
653                         if (path && !dev->name)
654                                 continue;
655
656                         rcu_read_lock();
657                         if (path)
658                                 not_found = strcmp(rcu_str_deref(dev->name),
659                                                    path);
660                         rcu_read_unlock();
661                         if (not_found)
662                                 continue;
663
664                         /* delete the stale device */
665                         if (fs_devs->num_devices == 1) {
666                                 btrfs_sysfs_remove_fsid(fs_devs);
667                                 list_del(&fs_devs->fs_list);
668                                 free_fs_devices(fs_devs);
669                                 break;
670                         } else {
671                                 fs_devs->num_devices--;
672                                 list_del(&dev->dev_list);
673                                 btrfs_free_device(dev);
674                         }
675                 }
676         }
677 }
678
679 static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices,
680                         struct btrfs_device *device, fmode_t flags,
681                         void *holder)
682 {
683         struct request_queue *q;
684         struct block_device *bdev;
685         struct buffer_head *bh;
686         struct btrfs_super_block *disk_super;
687         u64 devid;
688         int ret;
689
690         if (device->bdev)
691                 return -EINVAL;
692         if (!device->name)
693                 return -EINVAL;
694
695         ret = btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
696                                     &bdev, &bh);
697         if (ret)
698                 return ret;
699
700         disk_super = (struct btrfs_super_block *)bh->b_data;
701         devid = btrfs_stack_device_id(&disk_super->dev_item);
702         if (devid != device->devid)
703                 goto error_brelse;
704
705         if (memcmp(device->uuid, disk_super->dev_item.uuid, BTRFS_UUID_SIZE))
706                 goto error_brelse;
707
708         device->generation = btrfs_super_generation(disk_super);
709
710         if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
711                 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
712                 fs_devices->seeding = 1;
713         } else {
714                 if (bdev_read_only(bdev))
715                         clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
716                 else
717                         set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
718         }
719
720         q = bdev_get_queue(bdev);
721         if (!blk_queue_nonrot(q))
722                 fs_devices->rotating = 1;
723
724         device->bdev = bdev;
725         clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
726         device->mode = flags;
727
728         fs_devices->open_devices++;
729         if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
730             device->devid != BTRFS_DEV_REPLACE_DEVID) {
731                 fs_devices->rw_devices++;
732                 list_add_tail(&device->dev_alloc_list, &fs_devices->alloc_list);
733         }
734         brelse(bh);
735
736         return 0;
737
738 error_brelse:
739         brelse(bh);
740         blkdev_put(bdev, flags);
741
742         return -EINVAL;
743 }
744
745 /*
746  * Add new device to list of registered devices
747  *
748  * Returns:
749  * device pointer which was just added or updated when successful
750  * error pointer when failed
751  */
752 static noinline struct btrfs_device *device_list_add(const char *path,
753                            struct btrfs_super_block *disk_super)
754 {
755         struct btrfs_device *device;
756         struct btrfs_fs_devices *fs_devices;
757         struct rcu_string *name;
758         u64 found_transid = btrfs_super_generation(disk_super);
759         u64 devid = btrfs_stack_device_id(&disk_super->dev_item);
760
761         fs_devices = find_fsid(disk_super->fsid);
762         if (!fs_devices) {
763                 fs_devices = alloc_fs_devices(disk_super->fsid);
764                 if (IS_ERR(fs_devices))
765                         return ERR_CAST(fs_devices);
766
767                 list_add(&fs_devices->fs_list, &fs_uuids);
768
769                 device = NULL;
770         } else {
771                 device = find_device(fs_devices, devid,
772                                 disk_super->dev_item.uuid);
773         }
774
775         if (!device) {
776                 if (fs_devices->opened)
777                         return ERR_PTR(-EBUSY);
778
779                 device = btrfs_alloc_device(NULL, &devid,
780                                             disk_super->dev_item.uuid);
781                 if (IS_ERR(device)) {
782                         /* we can safely leave the fs_devices entry around */
783                         return device;
784                 }
785
786                 name = rcu_string_strdup(path, GFP_NOFS);
787                 if (!name) {
788                         btrfs_free_device(device);
789                         return ERR_PTR(-ENOMEM);
790                 }
791                 rcu_assign_pointer(device->name, name);
792
793                 mutex_lock(&fs_devices->device_list_mutex);
794                 list_add_rcu(&device->dev_list, &fs_devices->devices);
795                 fs_devices->num_devices++;
796                 mutex_unlock(&fs_devices->device_list_mutex);
797
798                 device->fs_devices = fs_devices;
799                 btrfs_free_stale_devices(path, device);
800
801                 if (disk_super->label[0])
802                         pr_info("BTRFS: device label %s devid %llu transid %llu %s\n",
803                                 disk_super->label, devid, found_transid, path);
804                 else
805                         pr_info("BTRFS: device fsid %pU devid %llu transid %llu %s\n",
806                                 disk_super->fsid, devid, found_transid, path);
807
808         } else if (!device->name || strcmp(device->name->str, path)) {
809                 /*
810                  * When FS is already mounted.
811                  * 1. If you are here and if the device->name is NULL that
812                  *    means this device was missing at time of FS mount.
813                  * 2. If you are here and if the device->name is different
814                  *    from 'path' that means either
815                  *      a. The same device disappeared and reappeared with
816                  *         different name. or
817                  *      b. The missing-disk-which-was-replaced, has
818                  *         reappeared now.
819                  *
820                  * We must allow 1 and 2a above. But 2b would be a spurious
821                  * and unintentional.
822                  *
823                  * Further in case of 1 and 2a above, the disk at 'path'
824                  * would have missed some transaction when it was away and
825                  * in case of 2a the stale bdev has to be updated as well.
826                  * 2b must not be allowed at all time.
827                  */
828
829                 /*
830                  * For now, we do allow update to btrfs_fs_device through the
831                  * btrfs dev scan cli after FS has been mounted.  We're still
832                  * tracking a problem where systems fail mount by subvolume id
833                  * when we reject replacement on a mounted FS.
834                  */
835                 if (!fs_devices->opened && found_transid < device->generation) {
836                         /*
837                          * That is if the FS is _not_ mounted and if you
838                          * are here, that means there is more than one
839                          * disk with same uuid and devid.We keep the one
840                          * with larger generation number or the last-in if
841                          * generation are equal.
842                          */
843                         return ERR_PTR(-EEXIST);
844                 }
845
846                 name = rcu_string_strdup(path, GFP_NOFS);
847                 if (!name)
848                         return ERR_PTR(-ENOMEM);
849                 rcu_string_free(device->name);
850                 rcu_assign_pointer(device->name, name);
851                 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) {
852                         fs_devices->missing_devices--;
853                         clear_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
854                 }
855         }
856
857         /*
858          * Unmount does not free the btrfs_device struct but would zero
859          * generation along with most of the other members. So just update
860          * it back. We need it to pick the disk with largest generation
861          * (as above).
862          */
863         if (!fs_devices->opened)
864                 device->generation = found_transid;
865
866         fs_devices->total_devices = btrfs_super_num_devices(disk_super);
867
868         return device;
869 }
870
871 static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
872 {
873         struct btrfs_fs_devices *fs_devices;
874         struct btrfs_device *device;
875         struct btrfs_device *orig_dev;
876
877         fs_devices = alloc_fs_devices(orig->fsid);
878         if (IS_ERR(fs_devices))
879                 return fs_devices;
880
881         mutex_lock(&orig->device_list_mutex);
882         fs_devices->total_devices = orig->total_devices;
883
884         /* We have held the volume lock, it is safe to get the devices. */
885         list_for_each_entry(orig_dev, &orig->devices, dev_list) {
886                 struct rcu_string *name;
887
888                 device = btrfs_alloc_device(NULL, &orig_dev->devid,
889                                             orig_dev->uuid);
890                 if (IS_ERR(device))
891                         goto error;
892
893                 /*
894                  * This is ok to do without rcu read locked because we hold the
895                  * uuid mutex so nothing we touch in here is going to disappear.
896                  */
897                 if (orig_dev->name) {
898                         name = rcu_string_strdup(orig_dev->name->str,
899                                         GFP_KERNEL);
900                         if (!name) {
901                                 btrfs_free_device(device);
902                                 goto error;
903                         }
904                         rcu_assign_pointer(device->name, name);
905                 }
906
907                 list_add(&device->dev_list, &fs_devices->devices);
908                 device->fs_devices = fs_devices;
909                 fs_devices->num_devices++;
910         }
911         mutex_unlock(&orig->device_list_mutex);
912         return fs_devices;
913 error:
914         mutex_unlock(&orig->device_list_mutex);
915         free_fs_devices(fs_devices);
916         return ERR_PTR(-ENOMEM);
917 }
918
919 /*
920  * After we have read the system tree and know devids belonging to
921  * this filesystem, remove the device which does not belong there.
922  */
923 void btrfs_free_extra_devids(struct btrfs_fs_devices *fs_devices, int step)
924 {
925         struct btrfs_device *device, *next;
926         struct btrfs_device *latest_dev = NULL;
927
928         mutex_lock(&uuid_mutex);
929 again:
930         /* This is the initialized path, it is safe to release the devices. */
931         list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
932                 if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
933                                                         &device->dev_state)) {
934                         if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
935                              &device->dev_state) &&
936                              (!latest_dev ||
937                               device->generation > latest_dev->generation)) {
938                                 latest_dev = device;
939                         }
940                         continue;
941                 }
942
943                 if (device->devid == BTRFS_DEV_REPLACE_DEVID) {
944                         /*
945                          * In the first step, keep the device which has
946                          * the correct fsid and the devid that is used
947                          * for the dev_replace procedure.
948                          * In the second step, the dev_replace state is
949                          * read from the device tree and it is known
950                          * whether the procedure is really active or
951                          * not, which means whether this device is
952                          * used or whether it should be removed.
953                          */
954                         if (step == 0 || test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
955                                                   &device->dev_state)) {
956                                 continue;
957                         }
958                 }
959                 if (device->bdev) {
960                         blkdev_put(device->bdev, device->mode);
961                         device->bdev = NULL;
962                         fs_devices->open_devices--;
963                 }
964                 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
965                         list_del_init(&device->dev_alloc_list);
966                         clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
967                         if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
968                                       &device->dev_state))
969                                 fs_devices->rw_devices--;
970                 }
971                 list_del_init(&device->dev_list);
972                 fs_devices->num_devices--;
973                 btrfs_free_device(device);
974         }
975
976         if (fs_devices->seed) {
977                 fs_devices = fs_devices->seed;
978                 goto again;
979         }
980
981         fs_devices->latest_bdev = latest_dev->bdev;
982
983         mutex_unlock(&uuid_mutex);
984 }
985
986 static void free_device_rcu(struct rcu_head *head)
987 {
988         struct btrfs_device *device;
989
990         device = container_of(head, struct btrfs_device, rcu);
991         btrfs_free_device(device);
992 }
993
994 static void btrfs_close_bdev(struct btrfs_device *device)
995 {
996         if (!device->bdev)
997                 return;
998
999         if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
1000                 sync_blockdev(device->bdev);
1001                 invalidate_bdev(device->bdev);
1002         }
1003
1004         blkdev_put(device->bdev, device->mode);
1005 }
1006
1007 static void btrfs_prepare_close_one_device(struct btrfs_device *device)
1008 {
1009         struct btrfs_fs_devices *fs_devices = device->fs_devices;
1010         struct btrfs_device *new_device;
1011         struct rcu_string *name;
1012
1013         if (device->bdev)
1014                 fs_devices->open_devices--;
1015
1016         if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
1017             device->devid != BTRFS_DEV_REPLACE_DEVID) {
1018                 list_del_init(&device->dev_alloc_list);
1019                 fs_devices->rw_devices--;
1020         }
1021
1022         if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
1023                 fs_devices->missing_devices--;
1024
1025         new_device = btrfs_alloc_device(NULL, &device->devid,
1026                                         device->uuid);
1027         BUG_ON(IS_ERR(new_device)); /* -ENOMEM */
1028
1029         /* Safe because we are under uuid_mutex */
1030         if (device->name) {
1031                 name = rcu_string_strdup(device->name->str, GFP_NOFS);
1032                 BUG_ON(!name); /* -ENOMEM */
1033                 rcu_assign_pointer(new_device->name, name);
1034         }
1035
1036         list_replace_rcu(&device->dev_list, &new_device->dev_list);
1037         new_device->fs_devices = device->fs_devices;
1038 }
1039
1040 static int close_fs_devices(struct btrfs_fs_devices *fs_devices)
1041 {
1042         struct btrfs_device *device, *tmp;
1043         struct list_head pending_put;
1044
1045         INIT_LIST_HEAD(&pending_put);
1046
1047         if (--fs_devices->opened > 0)
1048                 return 0;
1049
1050         mutex_lock(&fs_devices->device_list_mutex);
1051         list_for_each_entry_safe(device, tmp, &fs_devices->devices, dev_list) {
1052                 btrfs_prepare_close_one_device(device);
1053                 list_add(&device->dev_list, &pending_put);
1054         }
1055         mutex_unlock(&fs_devices->device_list_mutex);
1056
1057         /*
1058          * btrfs_show_devname() is using the device_list_mutex,
1059          * sometimes call to blkdev_put() leads vfs calling
1060          * into this func. So do put outside of device_list_mutex,
1061          * as of now.
1062          */
1063         while (!list_empty(&pending_put)) {
1064                 device = list_first_entry(&pending_put,
1065                                 struct btrfs_device, dev_list);
1066                 list_del(&device->dev_list);
1067                 btrfs_close_bdev(device);
1068                 call_rcu(&device->rcu, free_device_rcu);
1069         }
1070
1071         WARN_ON(fs_devices->open_devices);
1072         WARN_ON(fs_devices->rw_devices);
1073         fs_devices->opened = 0;
1074         fs_devices->seeding = 0;
1075
1076         return 0;
1077 }
1078
1079 int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
1080 {
1081         struct btrfs_fs_devices *seed_devices = NULL;
1082         int ret;
1083
1084         mutex_lock(&uuid_mutex);
1085         ret = close_fs_devices(fs_devices);
1086         if (!fs_devices->opened) {
1087                 seed_devices = fs_devices->seed;
1088                 fs_devices->seed = NULL;
1089         }
1090         mutex_unlock(&uuid_mutex);
1091
1092         while (seed_devices) {
1093                 fs_devices = seed_devices;
1094                 seed_devices = fs_devices->seed;
1095                 close_fs_devices(fs_devices);
1096                 free_fs_devices(fs_devices);
1097         }
1098         return ret;
1099 }
1100
1101 static int open_fs_devices(struct btrfs_fs_devices *fs_devices,
1102                                 fmode_t flags, void *holder)
1103 {
1104         struct btrfs_device *device;
1105         struct btrfs_device *latest_dev = NULL;
1106         int ret = 0;
1107
1108         flags |= FMODE_EXCL;
1109
1110         list_for_each_entry(device, &fs_devices->devices, dev_list) {
1111                 /* Just open everything we can; ignore failures here */
1112                 if (btrfs_open_one_device(fs_devices, device, flags, holder))
1113                         continue;
1114
1115                 if (!latest_dev ||
1116                     device->generation > latest_dev->generation)
1117                         latest_dev = device;
1118         }
1119         if (fs_devices->open_devices == 0) {
1120                 ret = -EINVAL;
1121                 goto out;
1122         }
1123         fs_devices->opened = 1;
1124         fs_devices->latest_bdev = latest_dev->bdev;
1125         fs_devices->total_rw_bytes = 0;
1126 out:
1127         return ret;
1128 }
1129
1130 static int devid_cmp(void *priv, struct list_head *a, struct list_head *b)
1131 {
1132         struct btrfs_device *dev1, *dev2;
1133
1134         dev1 = list_entry(a, struct btrfs_device, dev_list);
1135         dev2 = list_entry(b, struct btrfs_device, dev_list);
1136
1137         if (dev1->devid < dev2->devid)
1138                 return -1;
1139         else if (dev1->devid > dev2->devid)
1140                 return 1;
1141         return 0;
1142 }
1143
1144 int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
1145                        fmode_t flags, void *holder)
1146 {
1147         int ret;
1148
1149         mutex_lock(&uuid_mutex);
1150         mutex_lock(&fs_devices->device_list_mutex);
1151         if (fs_devices->opened) {
1152                 fs_devices->opened++;
1153                 ret = 0;
1154         } else {
1155                 list_sort(NULL, &fs_devices->devices, devid_cmp);
1156                 ret = open_fs_devices(fs_devices, flags, holder);
1157         }
1158         mutex_unlock(&fs_devices->device_list_mutex);
1159         mutex_unlock(&uuid_mutex);
1160
1161         return ret;
1162 }
1163
1164 static void btrfs_release_disk_super(struct page *page)
1165 {
1166         kunmap(page);
1167         put_page(page);
1168 }
1169
1170 static int btrfs_read_disk_super(struct block_device *bdev, u64 bytenr,
1171                                  struct page **page,
1172                                  struct btrfs_super_block **disk_super)
1173 {
1174         void *p;
1175         pgoff_t index;
1176
1177         /* make sure our super fits in the device */
1178         if (bytenr + PAGE_SIZE >= i_size_read(bdev->bd_inode))
1179                 return 1;
1180
1181         /* make sure our super fits in the page */
1182         if (sizeof(**disk_super) > PAGE_SIZE)
1183                 return 1;
1184
1185         /* make sure our super doesn't straddle pages on disk */
1186         index = bytenr >> PAGE_SHIFT;
1187         if ((bytenr + sizeof(**disk_super) - 1) >> PAGE_SHIFT != index)
1188                 return 1;
1189
1190         /* pull in the page with our super */
1191         *page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
1192                                    index, GFP_KERNEL);
1193
1194         if (IS_ERR_OR_NULL(*page))
1195                 return 1;
1196
1197         p = kmap(*page);
1198
1199         /* align our pointer to the offset of the super block */
1200         *disk_super = p + (bytenr & ~PAGE_MASK);
1201
1202         if (btrfs_super_bytenr(*disk_super) != bytenr ||
1203             btrfs_super_magic(*disk_super) != BTRFS_MAGIC) {
1204                 btrfs_release_disk_super(*page);
1205                 return 1;
1206         }
1207
1208         if ((*disk_super)->label[0] &&
1209                 (*disk_super)->label[BTRFS_LABEL_SIZE - 1])
1210                 (*disk_super)->label[BTRFS_LABEL_SIZE - 1] = '\0';
1211
1212         return 0;
1213 }
1214
1215 /*
1216  * Look for a btrfs signature on a device. This may be called out of the mount path
1217  * and we are not allowed to call set_blocksize during the scan. The superblock
1218  * is read via pagecache
1219  */
1220 int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
1221                           struct btrfs_fs_devices **fs_devices_ret)
1222 {
1223         struct btrfs_super_block *disk_super;
1224         struct btrfs_device *device;
1225         struct block_device *bdev;
1226         struct page *page;
1227         int ret = 0;
1228         u64 bytenr;
1229
1230         /*
1231          * we would like to check all the supers, but that would make
1232          * a btrfs mount succeed after a mkfs from a different FS.
1233          * So, we need to add a special mount option to scan for
1234          * later supers, using BTRFS_SUPER_MIRROR_MAX instead
1235          */
1236         bytenr = btrfs_sb_offset(0);
1237         flags |= FMODE_EXCL;
1238
1239         bdev = blkdev_get_by_path(path, flags, holder);
1240         if (IS_ERR(bdev))
1241                 return PTR_ERR(bdev);
1242
1243         if (btrfs_read_disk_super(bdev, bytenr, &page, &disk_super)) {
1244                 ret = -EINVAL;
1245                 goto error_bdev_put;
1246         }
1247
1248         mutex_lock(&uuid_mutex);
1249         device = device_list_add(path, disk_super);
1250         if (IS_ERR(device))
1251                 ret = PTR_ERR(device);
1252         else
1253                 *fs_devices_ret = device->fs_devices;
1254         mutex_unlock(&uuid_mutex);
1255
1256         btrfs_release_disk_super(page);
1257
1258 error_bdev_put:
1259         blkdev_put(bdev, flags);
1260
1261         return ret;
1262 }
1263
1264 /* helper to account the used device space in the range */
1265 int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
1266                                    u64 end, u64 *length)
1267 {
1268         struct btrfs_key key;
1269         struct btrfs_root *root = device->fs_info->dev_root;
1270         struct btrfs_dev_extent *dev_extent;
1271         struct btrfs_path *path;
1272         u64 extent_end;
1273         int ret;
1274         int slot;
1275         struct extent_buffer *l;
1276
1277         *length = 0;
1278
1279         if (start >= device->total_bytes ||
1280                 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
1281                 return 0;
1282
1283         path = btrfs_alloc_path();
1284         if (!path)
1285                 return -ENOMEM;
1286         path->reada = READA_FORWARD;
1287
1288         key.objectid = device->devid;
1289         key.offset = start;
1290         key.type = BTRFS_DEV_EXTENT_KEY;
1291
1292         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1293         if (ret < 0)
1294                 goto out;
1295         if (ret > 0) {
1296                 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1297                 if (ret < 0)
1298                         goto out;
1299         }
1300
1301         while (1) {
1302                 l = path->nodes[0];
1303                 slot = path->slots[0];
1304                 if (slot >= btrfs_header_nritems(l)) {
1305                         ret = btrfs_next_leaf(root, path);
1306                         if (ret == 0)
1307                                 continue;
1308                         if (ret < 0)
1309                                 goto out;
1310
1311                         break;
1312                 }
1313                 btrfs_item_key_to_cpu(l, &key, slot);
1314
1315                 if (key.objectid < device->devid)
1316                         goto next;
1317
1318                 if (key.objectid > device->devid)
1319                         break;
1320
1321                 if (key.type != BTRFS_DEV_EXTENT_KEY)
1322                         goto next;
1323
1324                 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
1325                 extent_end = key.offset + btrfs_dev_extent_length(l,
1326                                                                   dev_extent);
1327                 if (key.offset <= start && extent_end > end) {
1328                         *length = end - start + 1;
1329                         break;
1330                 } else if (key.offset <= start && extent_end > start)
1331                         *length += extent_end - start;
1332                 else if (key.offset > start && extent_end <= end)
1333                         *length += extent_end - key.offset;
1334                 else if (key.offset > start && key.offset <= end) {
1335                         *length += end - key.offset + 1;
1336                         break;
1337                 } else if (key.offset > end)
1338                         break;
1339
1340 next:
1341                 path->slots[0]++;
1342         }
1343         ret = 0;
1344 out:
1345         btrfs_free_path(path);
1346         return ret;
1347 }
1348
1349 static int contains_pending_extent(struct btrfs_transaction *transaction,
1350                                    struct btrfs_device *device,
1351                                    u64 *start, u64 len)
1352 {
1353         struct btrfs_fs_info *fs_info = device->fs_info;
1354         struct extent_map *em;
1355         struct list_head *search_list = &fs_info->pinned_chunks;
1356         int ret = 0;
1357         u64 physical_start = *start;
1358
1359         if (transaction)
1360                 search_list = &transaction->pending_chunks;
1361 again:
1362         list_for_each_entry(em, search_list, list) {
1363                 struct map_lookup *map;
1364                 int i;
1365
1366                 map = em->map_lookup;
1367                 for (i = 0; i < map->num_stripes; i++) {
1368                         u64 end;
1369
1370                         if (map->stripes[i].dev != device)
1371                                 continue;
1372                         if (map->stripes[i].physical >= physical_start + len ||
1373                             map->stripes[i].physical + em->orig_block_len <=
1374                             physical_start)
1375                                 continue;
1376                         /*
1377                          * Make sure that while processing the pinned list we do
1378                          * not override our *start with a lower value, because
1379                          * we can have pinned chunks that fall within this
1380                          * device hole and that have lower physical addresses
1381                          * than the pending chunks we processed before. If we
1382                          * do not take this special care we can end up getting
1383                          * 2 pending chunks that start at the same physical
1384                          * device offsets because the end offset of a pinned
1385                          * chunk can be equal to the start offset of some
1386                          * pending chunk.
1387                          */
1388                         end = map->stripes[i].physical + em->orig_block_len;
1389                         if (end > *start) {
1390                                 *start = end;
1391                                 ret = 1;
1392                         }
1393                 }
1394         }
1395         if (search_list != &fs_info->pinned_chunks) {
1396                 search_list = &fs_info->pinned_chunks;
1397                 goto again;
1398         }
1399
1400         return ret;
1401 }
1402
1403
1404 /*
1405  * find_free_dev_extent_start - find free space in the specified device
1406  * @device:       the device which we search the free space in
1407  * @num_bytes:    the size of the free space that we need
1408  * @search_start: the position from which to begin the search
1409  * @start:        store the start of the free space.
1410  * @len:          the size of the free space. that we find, or the size
1411  *                of the max free space if we don't find suitable free space
1412  *
1413  * this uses a pretty simple search, the expectation is that it is
1414  * called very infrequently and that a given device has a small number
1415  * of extents
1416  *
1417  * @start is used to store the start of the free space if we find. But if we
1418  * don't find suitable free space, it will be used to store the start position
1419  * of the max free space.
1420  *
1421  * @len is used to store the size of the free space that we find.
1422  * But if we don't find suitable free space, it is used to store the size of
1423  * the max free space.
1424  */
1425 int find_free_dev_extent_start(struct btrfs_transaction *transaction,
1426                                struct btrfs_device *device, u64 num_bytes,
1427                                u64 search_start, u64 *start, u64 *len)
1428 {
1429         struct btrfs_fs_info *fs_info = device->fs_info;
1430         struct btrfs_root *root = fs_info->dev_root;
1431         struct btrfs_key key;
1432         struct btrfs_dev_extent *dev_extent;
1433         struct btrfs_path *path;
1434         u64 hole_size;
1435         u64 max_hole_start;
1436         u64 max_hole_size;
1437         u64 extent_end;
1438         u64 search_end = device->total_bytes;
1439         int ret;
1440         int slot;
1441         struct extent_buffer *l;
1442
1443         /*
1444          * We don't want to overwrite the superblock on the drive nor any area
1445          * used by the boot loader (grub for example), so we make sure to start
1446          * at an offset of at least 1MB.
1447          */
1448         search_start = max_t(u64, search_start, SZ_1M);
1449
1450         path = btrfs_alloc_path();
1451         if (!path)
1452                 return -ENOMEM;
1453
1454         max_hole_start = search_start;
1455         max_hole_size = 0;
1456
1457 again:
1458         if (search_start >= search_end ||
1459                 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
1460                 ret = -ENOSPC;
1461                 goto out;
1462         }
1463
1464         path->reada = READA_FORWARD;
1465         path->search_commit_root = 1;
1466         path->skip_locking = 1;
1467
1468         key.objectid = device->devid;
1469         key.offset = search_start;
1470         key.type = BTRFS_DEV_EXTENT_KEY;
1471
1472         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1473         if (ret < 0)
1474                 goto out;
1475         if (ret > 0) {
1476                 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1477                 if (ret < 0)
1478                         goto out;
1479         }
1480
1481         while (1) {
1482                 l = path->nodes[0];
1483                 slot = path->slots[0];
1484                 if (slot >= btrfs_header_nritems(l)) {
1485                         ret = btrfs_next_leaf(root, path);
1486                         if (ret == 0)
1487                                 continue;
1488                         if (ret < 0)
1489                                 goto out;
1490
1491                         break;
1492                 }
1493                 btrfs_item_key_to_cpu(l, &key, slot);
1494
1495                 if (key.objectid < device->devid)
1496                         goto next;
1497
1498                 if (key.objectid > device->devid)
1499                         break;
1500
1501                 if (key.type != BTRFS_DEV_EXTENT_KEY)
1502                         goto next;
1503
1504                 if (key.offset > search_start) {
1505                         hole_size = key.offset - search_start;
1506
1507                         /*
1508                          * Have to check before we set max_hole_start, otherwise
1509                          * we could end up sending back this offset anyway.
1510                          */
1511                         if (contains_pending_extent(transaction, device,
1512                                                     &search_start,
1513                                                     hole_size)) {
1514                                 if (key.offset >= search_start) {
1515                                         hole_size = key.offset - search_start;
1516                                 } else {
1517                                         WARN_ON_ONCE(1);
1518                                         hole_size = 0;
1519                                 }
1520                         }
1521
1522                         if (hole_size > max_hole_size) {
1523                                 max_hole_start = search_start;
1524                                 max_hole_size = hole_size;
1525                         }
1526
1527                         /*
1528                          * If this free space is greater than which we need,
1529                          * it must be the max free space that we have found
1530                          * until now, so max_hole_start must point to the start
1531                          * of this free space and the length of this free space
1532                          * is stored in max_hole_size. Thus, we return
1533                          * max_hole_start and max_hole_size and go back to the
1534                          * caller.
1535                          */
1536                         if (hole_size >= num_bytes) {
1537                                 ret = 0;
1538                                 goto out;
1539                         }
1540                 }
1541
1542                 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
1543                 extent_end = key.offset + btrfs_dev_extent_length(l,
1544                                                                   dev_extent);
1545                 if (extent_end > search_start)
1546                         search_start = extent_end;
1547 next:
1548                 path->slots[0]++;
1549                 cond_resched();
1550         }
1551
1552         /*
1553          * At this point, search_start should be the end of
1554          * allocated dev extents, and when shrinking the device,
1555          * search_end may be smaller than search_start.
1556          */
1557         if (search_end > search_start) {
1558                 hole_size = search_end - search_start;
1559
1560                 if (contains_pending_extent(transaction, device, &search_start,
1561                                             hole_size)) {
1562                         btrfs_release_path(path);
1563                         goto again;
1564                 }
1565
1566                 if (hole_size > max_hole_size) {
1567                         max_hole_start = search_start;
1568                         max_hole_size = hole_size;
1569                 }
1570         }
1571
1572         /* See above. */
1573         if (max_hole_size < num_bytes)
1574                 ret = -ENOSPC;
1575         else
1576                 ret = 0;
1577
1578 out:
1579         btrfs_free_path(path);
1580         *start = max_hole_start;
1581         if (len)
1582                 *len = max_hole_size;
1583         return ret;
1584 }
1585
1586 int find_free_dev_extent(struct btrfs_trans_handle *trans,
1587                          struct btrfs_device *device, u64 num_bytes,
1588                          u64 *start, u64 *len)
1589 {
1590         /* FIXME use last free of some kind */
1591         return find_free_dev_extent_start(trans->transaction, device,
1592                                           num_bytes, 0, start, len);
1593 }
1594
1595 static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
1596                           struct btrfs_device *device,
1597                           u64 start, u64 *dev_extent_len)
1598 {
1599         struct btrfs_fs_info *fs_info = device->fs_info;
1600         struct btrfs_root *root = fs_info->dev_root;
1601         int ret;
1602         struct btrfs_path *path;
1603         struct btrfs_key key;
1604         struct btrfs_key found_key;
1605         struct extent_buffer *leaf = NULL;
1606         struct btrfs_dev_extent *extent = NULL;
1607
1608         path = btrfs_alloc_path();
1609         if (!path)
1610                 return -ENOMEM;
1611
1612         key.objectid = device->devid;
1613         key.offset = start;
1614         key.type = BTRFS_DEV_EXTENT_KEY;
1615 again:
1616         ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1617         if (ret > 0) {
1618                 ret = btrfs_previous_item(root, path, key.objectid,
1619                                           BTRFS_DEV_EXTENT_KEY);
1620                 if (ret)
1621                         goto out;
1622                 leaf = path->nodes[0];
1623                 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1624                 extent = btrfs_item_ptr(leaf, path->slots[0],
1625                                         struct btrfs_dev_extent);
1626                 BUG_ON(found_key.offset > start || found_key.offset +
1627                        btrfs_dev_extent_length(leaf, extent) < start);
1628                 key = found_key;
1629                 btrfs_release_path(path);
1630                 goto again;
1631         } else if (ret == 0) {
1632                 leaf = path->nodes[0];
1633                 extent = btrfs_item_ptr(leaf, path->slots[0],
1634                                         struct btrfs_dev_extent);
1635         } else {
1636                 btrfs_handle_fs_error(fs_info, ret, "Slot search failed");
1637                 goto out;
1638         }
1639
1640         *dev_extent_len = btrfs_dev_extent_length(leaf, extent);
1641
1642         ret = btrfs_del_item(trans, root, path);
1643         if (ret) {
1644                 btrfs_handle_fs_error(fs_info, ret,
1645                                       "Failed to remove dev extent item");
1646         } else {
1647                 set_bit(BTRFS_TRANS_HAVE_FREE_BGS, &trans->transaction->flags);
1648         }
1649 out:
1650         btrfs_free_path(path);
1651         return ret;
1652 }
1653
1654 static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1655                                   struct btrfs_device *device,
1656                                   u64 chunk_offset, u64 start, u64 num_bytes)
1657 {
1658         int ret;
1659         struct btrfs_path *path;
1660         struct btrfs_fs_info *fs_info = device->fs_info;
1661         struct btrfs_root *root = fs_info->dev_root;
1662         struct btrfs_dev_extent *extent;
1663         struct extent_buffer *leaf;
1664         struct btrfs_key key;
1665
1666         WARN_ON(!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state));
1667         WARN_ON(test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state));
1668         path = btrfs_alloc_path();
1669         if (!path)
1670                 return -ENOMEM;
1671
1672         key.objectid = device->devid;
1673         key.offset = start;
1674         key.type = BTRFS_DEV_EXTENT_KEY;
1675         ret = btrfs_insert_empty_item(trans, root, path, &key,
1676                                       sizeof(*extent));
1677         if (ret)
1678                 goto out;
1679
1680         leaf = path->nodes[0];
1681         extent = btrfs_item_ptr(leaf, path->slots[0],
1682                                 struct btrfs_dev_extent);
1683         btrfs_set_dev_extent_chunk_tree(leaf, extent,
1684                                         BTRFS_CHUNK_TREE_OBJECTID);
1685         btrfs_set_dev_extent_chunk_objectid(leaf, extent,
1686                                             BTRFS_FIRST_CHUNK_TREE_OBJECTID);
1687         btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1688
1689         btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1690         btrfs_mark_buffer_dirty(leaf);
1691 out:
1692         btrfs_free_path(path);
1693         return ret;
1694 }
1695
1696 static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
1697 {
1698         struct extent_map_tree *em_tree;
1699         struct extent_map *em;
1700         struct rb_node *n;
1701         u64 ret = 0;
1702
1703         em_tree = &fs_info->mapping_tree.map_tree;
1704         read_lock(&em_tree->lock);
1705         n = rb_last(&em_tree->map);
1706         if (n) {
1707                 em = rb_entry(n, struct extent_map, rb_node);
1708                 ret = em->start + em->len;
1709         }
1710         read_unlock(&em_tree->lock);
1711
1712         return ret;
1713 }
1714
1715 static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
1716                                     u64 *devid_ret)
1717 {
1718         int ret;
1719         struct btrfs_key key;
1720         struct btrfs_key found_key;
1721         struct btrfs_path *path;
1722
1723         path = btrfs_alloc_path();
1724         if (!path)
1725                 return -ENOMEM;
1726
1727         key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1728         key.type = BTRFS_DEV_ITEM_KEY;
1729         key.offset = (u64)-1;
1730
1731         ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
1732         if (ret < 0)
1733                 goto error;
1734
1735         BUG_ON(ret == 0); /* Corruption */
1736
1737         ret = btrfs_previous_item(fs_info->chunk_root, path,
1738                                   BTRFS_DEV_ITEMS_OBJECTID,
1739                                   BTRFS_DEV_ITEM_KEY);
1740         if (ret) {
1741                 *devid_ret = 1;
1742         } else {
1743                 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1744                                       path->slots[0]);
1745                 *devid_ret = found_key.offset + 1;
1746         }
1747         ret = 0;
1748 error:
1749         btrfs_free_path(path);
1750         return ret;
1751 }
1752
1753 /*
1754  * the device information is stored in the chunk root
1755  * the btrfs_device struct should be fully filled in
1756  */
1757 static int btrfs_add_dev_item(struct btrfs_trans_handle *trans,
1758                             struct btrfs_fs_info *fs_info,
1759                             struct btrfs_device *device)
1760 {
1761         struct btrfs_root *root = fs_info->chunk_root;
1762         int ret;
1763         struct btrfs_path *path;
1764         struct btrfs_dev_item *dev_item;
1765         struct extent_buffer *leaf;
1766         struct btrfs_key key;
1767         unsigned long ptr;
1768
1769         path = btrfs_alloc_path();
1770         if (!path)
1771                 return -ENOMEM;
1772
1773         key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1774         key.type = BTRFS_DEV_ITEM_KEY;
1775         key.offset = device->devid;
1776
1777         ret = btrfs_insert_empty_item(trans, root, path, &key,
1778                                       sizeof(*dev_item));
1779         if (ret)
1780                 goto out;
1781
1782         leaf = path->nodes[0];
1783         dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1784
1785         btrfs_set_device_id(leaf, dev_item, device->devid);
1786         btrfs_set_device_generation(leaf, dev_item, 0);
1787         btrfs_set_device_type(leaf, dev_item, device->type);
1788         btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1789         btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1790         btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
1791         btrfs_set_device_total_bytes(leaf, dev_item,
1792                                      btrfs_device_get_disk_total_bytes(device));
1793         btrfs_set_device_bytes_used(leaf, dev_item,
1794                                     btrfs_device_get_bytes_used(device));
1795         btrfs_set_device_group(leaf, dev_item, 0);
1796         btrfs_set_device_seek_speed(leaf, dev_item, 0);
1797         btrfs_set_device_bandwidth(leaf, dev_item, 0);
1798         btrfs_set_device_start_offset(leaf, dev_item, 0);
1799
1800         ptr = btrfs_device_uuid(dev_item);
1801         write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
1802         ptr = btrfs_device_fsid(dev_item);
1803         write_extent_buffer(leaf, fs_info->fsid, ptr, BTRFS_FSID_SIZE);
1804         btrfs_mark_buffer_dirty(leaf);
1805
1806         ret = 0;
1807 out:
1808         btrfs_free_path(path);
1809         return ret;
1810 }
1811
1812 /*
1813  * Function to update ctime/mtime for a given device path.
1814  * Mainly used for ctime/mtime based probe like libblkid.
1815  */
1816 static void update_dev_time(const char *path_name)
1817 {
1818         struct file *filp;
1819
1820         filp = filp_open(path_name, O_RDWR, 0);
1821         if (IS_ERR(filp))
1822                 return;
1823         file_update_time(filp);
1824         filp_close(filp, NULL);
1825 }
1826
1827 static int btrfs_rm_dev_item(struct btrfs_fs_info *fs_info,
1828                              struct btrfs_device *device)
1829 {
1830         struct btrfs_root *root = fs_info->chunk_root;
1831         int ret;
1832         struct btrfs_path *path;
1833         struct btrfs_key key;
1834         struct btrfs_trans_handle *trans;
1835
1836         path = btrfs_alloc_path();
1837         if (!path)
1838                 return -ENOMEM;
1839
1840         trans = btrfs_start_transaction(root, 0);
1841         if (IS_ERR(trans)) {
1842                 btrfs_free_path(path);
1843                 return PTR_ERR(trans);
1844         }
1845         key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1846         key.type = BTRFS_DEV_ITEM_KEY;
1847         key.offset = device->devid;
1848
1849         ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1850         if (ret) {
1851                 if (ret > 0)
1852                         ret = -ENOENT;
1853                 btrfs_abort_transaction(trans, ret);
1854                 btrfs_end_transaction(trans);
1855                 goto out;
1856         }
1857
1858         ret = btrfs_del_item(trans, root, path);
1859         if (ret) {
1860                 btrfs_abort_transaction(trans, ret);
1861                 btrfs_end_transaction(trans);
1862         }
1863
1864 out:
1865         btrfs_free_path(path);
1866         if (!ret)
1867                 ret = btrfs_commit_transaction(trans);
1868         return ret;
1869 }
1870
1871 /*
1872  * Verify that @num_devices satisfies the RAID profile constraints in the whole
1873  * filesystem. It's up to the caller to adjust that number regarding eg. device
1874  * replace.
1875  */
1876 static int btrfs_check_raid_min_devices(struct btrfs_fs_info *fs_info,
1877                 u64 num_devices)
1878 {
1879         u64 all_avail;
1880         unsigned seq;
1881         int i;
1882
1883         do {
1884                 seq = read_seqbegin(&fs_info->profiles_lock);
1885
1886                 all_avail = fs_info->avail_data_alloc_bits |
1887                             fs_info->avail_system_alloc_bits |
1888                             fs_info->avail_metadata_alloc_bits;
1889         } while (read_seqretry(&fs_info->profiles_lock, seq));
1890
1891         for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
1892                 if (!(all_avail & btrfs_raid_array[i].bg_flag))
1893                         continue;
1894
1895                 if (num_devices < btrfs_raid_array[i].devs_min) {
1896                         int ret = btrfs_raid_array[i].mindev_error;
1897
1898                         if (ret)
1899                                 return ret;
1900                 }
1901         }
1902
1903         return 0;
1904 }
1905
1906 static struct btrfs_device * btrfs_find_next_active_device(
1907                 struct btrfs_fs_devices *fs_devs, struct btrfs_device *device)
1908 {
1909         struct btrfs_device *next_device;
1910
1911         list_for_each_entry(next_device, &fs_devs->devices, dev_list) {
1912                 if (next_device != device &&
1913                     !test_bit(BTRFS_DEV_STATE_MISSING, &next_device->dev_state)
1914                     && next_device->bdev)
1915                         return next_device;
1916         }
1917
1918         return NULL;
1919 }
1920
1921 /*
1922  * Helper function to check if the given device is part of s_bdev / latest_bdev
1923  * and replace it with the provided or the next active device, in the context
1924  * where this function called, there should be always be another device (or
1925  * this_dev) which is active.
1926  */
1927 void btrfs_assign_next_active_device(struct btrfs_fs_info *fs_info,
1928                 struct btrfs_device *device, struct btrfs_device *this_dev)
1929 {
1930         struct btrfs_device *next_device;
1931
1932         if (this_dev)
1933                 next_device = this_dev;
1934         else
1935                 next_device = btrfs_find_next_active_device(fs_info->fs_devices,
1936                                                                 device);
1937         ASSERT(next_device);
1938
1939         if (fs_info->sb->s_bdev &&
1940                         (fs_info->sb->s_bdev == device->bdev))
1941                 fs_info->sb->s_bdev = next_device->bdev;
1942
1943         if (fs_info->fs_devices->latest_bdev == device->bdev)
1944                 fs_info->fs_devices->latest_bdev = next_device->bdev;
1945 }
1946
1947 int btrfs_rm_device(struct btrfs_fs_info *fs_info, const char *device_path,
1948                 u64 devid)
1949 {
1950         struct btrfs_device *device;
1951         struct btrfs_fs_devices *cur_devices;
1952         struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
1953         u64 num_devices;
1954         int ret = 0;
1955
1956         mutex_lock(&uuid_mutex);
1957
1958         num_devices = fs_devices->num_devices;
1959         btrfs_dev_replace_read_lock(&fs_info->dev_replace);
1960         if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
1961                 WARN_ON(num_devices < 1);
1962                 num_devices--;
1963         }
1964         btrfs_dev_replace_read_unlock(&fs_info->dev_replace);
1965
1966         ret = btrfs_check_raid_min_devices(fs_info, num_devices - 1);
1967         if (ret)
1968                 goto out;
1969
1970         ret = btrfs_find_device_by_devspec(fs_info, devid, device_path,
1971                                            &device);
1972         if (ret)
1973                 goto out;
1974
1975         if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
1976                 ret = BTRFS_ERROR_DEV_TGT_REPLACE;
1977                 goto out;
1978         }
1979
1980         if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
1981             fs_info->fs_devices->rw_devices == 1) {
1982                 ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
1983                 goto out;
1984         }
1985
1986         if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
1987                 mutex_lock(&fs_info->chunk_mutex);
1988                 list_del_init(&device->dev_alloc_list);
1989                 device->fs_devices->rw_devices--;
1990                 mutex_unlock(&fs_info->chunk_mutex);
1991         }
1992
1993         mutex_unlock(&uuid_mutex);
1994         ret = btrfs_shrink_device(device, 0);
1995         mutex_lock(&uuid_mutex);
1996         if (ret)
1997                 goto error_undo;
1998
1999         /*
2000          * TODO: the superblock still includes this device in its num_devices
2001          * counter although write_all_supers() is not locked out. This
2002          * could give a filesystem state which requires a degraded mount.
2003          */
2004         ret = btrfs_rm_dev_item(fs_info, device);
2005         if (ret)
2006                 goto error_undo;
2007
2008         clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
2009         btrfs_scrub_cancel_dev(fs_info, device);
2010
2011         /*
2012          * the device list mutex makes sure that we don't change
2013          * the device list while someone else is writing out all
2014          * the device supers. Whoever is writing all supers, should
2015          * lock the device list mutex before getting the number of
2016          * devices in the super block (super_copy). Conversely,
2017          * whoever updates the number of devices in the super block
2018          * (super_copy) should hold the device list mutex.
2019          */
2020
2021         /*
2022          * In normal cases the cur_devices == fs_devices. But in case
2023          * of deleting a seed device, the cur_devices should point to
2024          * its own fs_devices listed under the fs_devices->seed.
2025          */
2026         cur_devices = device->fs_devices;
2027         mutex_lock(&fs_devices->device_list_mutex);
2028         list_del_rcu(&device->dev_list);
2029
2030         cur_devices->num_devices--;
2031         cur_devices->total_devices--;
2032
2033         if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
2034                 cur_devices->missing_devices--;
2035
2036         btrfs_assign_next_active_device(fs_info, device, NULL);
2037
2038         if (device->bdev) {
2039                 cur_devices->open_devices--;
2040                 /* remove sysfs entry */
2041                 btrfs_sysfs_rm_device_link(fs_devices, device);
2042         }
2043
2044         num_devices = btrfs_super_num_devices(fs_info->super_copy) - 1;
2045         btrfs_set_super_num_devices(fs_info->super_copy, num_devices);
2046         mutex_unlock(&fs_devices->device_list_mutex);
2047
2048         /*
2049          * at this point, the device is zero sized and detached from
2050          * the devices list.  All that's left is to zero out the old
2051          * supers and free the device.
2052          */
2053         if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
2054                 btrfs_scratch_superblocks(device->bdev, device->name->str);
2055
2056         btrfs_close_bdev(device);
2057         call_rcu(&device->rcu, free_device_rcu);
2058
2059         if (cur_devices->open_devices == 0) {
2060                 while (fs_devices) {
2061                         if (fs_devices->seed == cur_devices) {
2062                                 fs_devices->seed = cur_devices->seed;
2063                                 break;
2064                         }
2065                         fs_devices = fs_devices->seed;
2066                 }
2067                 cur_devices->seed = NULL;
2068                 close_fs_devices(cur_devices);
2069                 free_fs_devices(cur_devices);
2070         }
2071
2072 out:
2073         mutex_unlock(&uuid_mutex);
2074         return ret;
2075
2076 error_undo:
2077         if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
2078                 mutex_lock(&fs_info->chunk_mutex);
2079                 list_add(&device->dev_alloc_list,
2080                          &fs_devices->alloc_list);
2081                 device->fs_devices->rw_devices++;
2082                 mutex_unlock(&fs_info->chunk_mutex);
2083         }
2084         goto out;
2085 }
2086
2087 void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_fs_info *fs_info,
2088                                         struct btrfs_device *srcdev)
2089 {
2090         struct btrfs_fs_devices *fs_devices;
2091
2092         lockdep_assert_held(&fs_info->fs_devices->device_list_mutex);
2093
2094         /*
2095          * in case of fs with no seed, srcdev->fs_devices will point
2096          * to fs_devices of fs_info. However when the dev being replaced is
2097          * a seed dev it will point to the seed's local fs_devices. In short
2098          * srcdev will have its correct fs_devices in both the cases.
2099          */
2100         fs_devices = srcdev->fs_devices;
2101
2102         list_del_rcu(&srcdev->dev_list);
2103         list_del(&srcdev->dev_alloc_list);
2104         fs_devices->num_devices--;
2105         if (test_bit(BTRFS_DEV_STATE_MISSING, &srcdev->dev_state))
2106                 fs_devices->missing_devices--;
2107
2108         if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state))
2109                 fs_devices->rw_devices--;
2110
2111         if (srcdev->bdev)
2112                 fs_devices->open_devices--;
2113 }
2114
2115 void btrfs_rm_dev_replace_free_srcdev(struct btrfs_fs_info *fs_info,
2116                                       struct btrfs_device *srcdev)
2117 {
2118         struct btrfs_fs_devices *fs_devices = srcdev->fs_devices;
2119
2120         if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state)) {
2121                 /* zero out the old super if it is writable */
2122                 btrfs_scratch_superblocks(srcdev->bdev, srcdev->name->str);
2123         }
2124
2125         btrfs_close_bdev(srcdev);
2126         call_rcu(&srcdev->rcu, free_device_rcu);
2127
2128         /* if this is no devs we rather delete the fs_devices */
2129         if (!fs_devices->num_devices) {
2130                 struct btrfs_fs_devices *tmp_fs_devices;
2131
2132                 /*
2133                  * On a mounted FS, num_devices can't be zero unless it's a
2134                  * seed. In case of a seed device being replaced, the replace
2135                  * target added to the sprout FS, so there will be no more
2136                  * device left under the seed FS.
2137                  */
2138                 ASSERT(fs_devices->seeding);
2139
2140                 tmp_fs_devices = fs_info->fs_devices;
2141                 while (tmp_fs_devices) {
2142                         if (tmp_fs_devices->seed == fs_devices) {
2143                                 tmp_fs_devices->seed = fs_devices->seed;
2144                                 break;
2145                         }
2146                         tmp_fs_devices = tmp_fs_devices->seed;
2147                 }
2148                 fs_devices->seed = NULL;
2149                 close_fs_devices(fs_devices);
2150                 free_fs_devices(fs_devices);
2151         }
2152 }
2153
2154 void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
2155                                       struct btrfs_device *tgtdev)
2156 {
2157         struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
2158
2159         WARN_ON(!tgtdev);
2160         mutex_lock(&fs_devices->device_list_mutex);
2161
2162         btrfs_sysfs_rm_device_link(fs_devices, tgtdev);
2163
2164         if (tgtdev->bdev)
2165                 fs_devices->open_devices--;
2166
2167         fs_devices->num_devices--;
2168
2169         btrfs_assign_next_active_device(fs_info, tgtdev, NULL);
2170
2171         list_del_rcu(&tgtdev->dev_list);
2172
2173         mutex_unlock(&fs_devices->device_list_mutex);
2174
2175         /*
2176          * The update_dev_time() with in btrfs_scratch_superblocks()
2177          * may lead to a call to btrfs_show_devname() which will try
2178          * to hold device_list_mutex. And here this device
2179          * is already out of device list, so we don't have to hold
2180          * the device_list_mutex lock.
2181          */
2182         btrfs_scratch_superblocks(tgtdev->bdev, tgtdev->name->str);
2183
2184         btrfs_close_bdev(tgtdev);
2185         call_rcu(&tgtdev->rcu, free_device_rcu);
2186 }
2187
2188 static int btrfs_find_device_by_path(struct btrfs_fs_info *fs_info,
2189                                      const char *device_path,
2190                                      struct btrfs_device **device)
2191 {
2192         int ret = 0;
2193         struct btrfs_super_block *disk_super;
2194         u64 devid;
2195         u8 *dev_uuid;
2196         struct block_device *bdev;
2197         struct buffer_head *bh;
2198
2199         *device = NULL;
2200         ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
2201                                     fs_info->bdev_holder, 0, &bdev, &bh);
2202         if (ret)
2203                 return ret;
2204         disk_super = (struct btrfs_super_block *)bh->b_data;
2205         devid = btrfs_stack_device_id(&disk_super->dev_item);
2206         dev_uuid = disk_super->dev_item.uuid;
2207         *device = btrfs_find_device(fs_info, devid, dev_uuid, disk_super->fsid);
2208         brelse(bh);
2209         if (!*device)
2210                 ret = -ENOENT;
2211         blkdev_put(bdev, FMODE_READ);
2212         return ret;
2213 }
2214
2215 int btrfs_find_device_missing_or_by_path(struct btrfs_fs_info *fs_info,
2216                                          const char *device_path,
2217                                          struct btrfs_device **device)
2218 {
2219         *device = NULL;
2220         if (strcmp(device_path, "missing") == 0) {
2221                 struct list_head *devices;
2222                 struct btrfs_device *tmp;
2223
2224                 devices = &fs_info->fs_devices->devices;
2225                 list_for_each_entry(tmp, devices, dev_list) {
2226                         if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
2227                                         &tmp->dev_state) && !tmp->bdev) {
2228                                 *device = tmp;
2229                                 break;
2230                         }
2231                 }
2232
2233                 if (!*device)
2234                         return BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
2235
2236                 return 0;
2237         } else {
2238                 return btrfs_find_device_by_path(fs_info, device_path, device);
2239         }
2240 }
2241
2242 /*
2243  * Lookup a device given by device id, or the path if the id is 0.
2244  */
2245 int btrfs_find_device_by_devspec(struct btrfs_fs_info *fs_info, u64 devid,
2246                                  const char *devpath,
2247                                  struct btrfs_device **device)
2248 {
2249         int ret;
2250
2251         if (devid) {
2252                 ret = 0;
2253                 *device = btrfs_find_device(fs_info, devid, NULL, NULL);
2254                 if (!*device)
2255                         ret = -ENOENT;
2256         } else {
2257                 if (!devpath || !devpath[0])
2258                         return -EINVAL;
2259
2260                 ret = btrfs_find_device_missing_or_by_path(fs_info, devpath,
2261                                                            device);
2262         }
2263         return ret;
2264 }
2265
2266 /*
2267  * does all the dirty work required for changing file system's UUID.
2268  */
2269 static int btrfs_prepare_sprout(struct btrfs_fs_info *fs_info)
2270 {
2271         struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
2272         struct btrfs_fs_devices *old_devices;
2273         struct btrfs_fs_devices *seed_devices;
2274         struct btrfs_super_block *disk_super = fs_info->super_copy;
2275         struct btrfs_device *device;
2276         u64 super_flags;
2277
2278         lockdep_assert_held(&uuid_mutex);
2279         if (!fs_devices->seeding)
2280                 return -EINVAL;
2281
2282         seed_devices = alloc_fs_devices(NULL);
2283         if (IS_ERR(seed_devices))
2284                 return PTR_ERR(seed_devices);
2285
2286         old_devices = clone_fs_devices(fs_devices);
2287         if (IS_ERR(old_devices)) {
2288                 kfree(seed_devices);
2289                 return PTR_ERR(old_devices);
2290         }
2291
2292         list_add(&old_devices->fs_list, &fs_uuids);
2293
2294         memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
2295         seed_devices->opened = 1;
2296         INIT_LIST_HEAD(&seed_devices->devices);
2297         INIT_LIST_HEAD(&seed_devices->alloc_list);
2298         mutex_init(&seed_devices->device_list_mutex);
2299
2300         mutex_lock(&fs_info->fs_devices->device_list_mutex);
2301         list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
2302                               synchronize_rcu);
2303         list_for_each_entry(device, &seed_devices->devices, dev_list)
2304                 device->fs_devices = seed_devices;
2305
2306         mutex_lock(&fs_info->chunk_mutex);
2307         list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
2308         mutex_unlock(&fs_info->chunk_mutex);
2309
2310         fs_devices->seeding = 0;
2311         fs_devices->num_devices = 0;
2312         fs_devices->open_devices = 0;
2313         fs_devices->missing_devices = 0;
2314         fs_devices->rotating = 0;
2315         fs_devices->seed = seed_devices;
2316
2317         generate_random_uuid(fs_devices->fsid);
2318         memcpy(fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
2319         memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
2320         mutex_unlock(&fs_info->fs_devices->device_list_mutex);
2321
2322         super_flags = btrfs_super_flags(disk_super) &
2323                       ~BTRFS_SUPER_FLAG_SEEDING;
2324         btrfs_set_super_flags(disk_super, super_flags);
2325
2326         return 0;
2327 }
2328
2329 /*
2330  * Store the expected generation for seed devices in device items.
2331  */
2332 static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
2333                                struct btrfs_fs_info *fs_info)
2334 {
2335         struct btrfs_root *root = fs_info->chunk_root;
2336         struct btrfs_path *path;
2337         struct extent_buffer *leaf;
2338         struct btrfs_dev_item *dev_item;
2339         struct btrfs_device *device;
2340         struct btrfs_key key;
2341         u8 fs_uuid[BTRFS_FSID_SIZE];
2342         u8 dev_uuid[BTRFS_UUID_SIZE];
2343         u64 devid;
2344         int ret;
2345
2346         path = btrfs_alloc_path();
2347         if (!path)
2348                 return -ENOMEM;
2349
2350         key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2351         key.offset = 0;
2352         key.type = BTRFS_DEV_ITEM_KEY;
2353
2354         while (1) {
2355                 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2356                 if (ret < 0)
2357                         goto error;
2358
2359                 leaf = path->nodes[0];
2360 next_slot:
2361                 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
2362                         ret = btrfs_next_leaf(root, path);
2363                         if (ret > 0)
2364                                 break;
2365                         if (ret < 0)
2366                                 goto error;
2367                         leaf = path->nodes[0];
2368                         btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2369                         btrfs_release_path(path);
2370                         continue;
2371                 }
2372
2373                 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2374                 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
2375                     key.type != BTRFS_DEV_ITEM_KEY)
2376                         break;
2377
2378                 dev_item = btrfs_item_ptr(leaf, path->slots[0],
2379                                           struct btrfs_dev_item);
2380                 devid = btrfs_device_id(leaf, dev_item);
2381                 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
2382                                    BTRFS_UUID_SIZE);
2383                 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
2384                                    BTRFS_FSID_SIZE);
2385                 device = btrfs_find_device(fs_info, devid, dev_uuid, fs_uuid);
2386                 BUG_ON(!device); /* Logic error */
2387
2388                 if (device->fs_devices->seeding) {
2389                         btrfs_set_device_generation(leaf, dev_item,
2390                                                     device->generation);
2391                         btrfs_mark_buffer_dirty(leaf);
2392                 }
2393
2394                 path->slots[0]++;
2395                 goto next_slot;
2396         }
2397         ret = 0;
2398 error:
2399         btrfs_free_path(path);
2400         return ret;
2401 }
2402
2403 int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path)
2404 {
2405         struct btrfs_root *root = fs_info->dev_root;
2406         struct request_queue *q;
2407         struct btrfs_trans_handle *trans;
2408         struct btrfs_device *device;
2409         struct block_device *bdev;
2410         struct list_head *devices;
2411         struct super_block *sb = fs_info->sb;
2412         struct rcu_string *name;
2413         u64 tmp;
2414         int seeding_dev = 0;
2415         int ret = 0;
2416         bool unlocked = false;
2417
2418         if (sb_rdonly(sb) && !fs_info->fs_devices->seeding)
2419                 return -EROFS;
2420
2421         bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
2422                                   fs_info->bdev_holder);
2423         if (IS_ERR(bdev))
2424                 return PTR_ERR(bdev);
2425
2426         if (fs_info->fs_devices->seeding) {
2427                 seeding_dev = 1;
2428                 down_write(&sb->s_umount);
2429                 mutex_lock(&uuid_mutex);
2430         }
2431
2432         filemap_write_and_wait(bdev->bd_inode->i_mapping);
2433
2434         devices = &fs_info->fs_devices->devices;
2435
2436         mutex_lock(&fs_info->fs_devices->device_list_mutex);
2437         list_for_each_entry(device, devices, dev_list) {
2438                 if (device->bdev == bdev) {
2439                         ret = -EEXIST;
2440                         mutex_unlock(
2441                                 &fs_info->fs_devices->device_list_mutex);
2442                         goto error;
2443                 }
2444         }
2445         mutex_unlock(&fs_info->fs_devices->device_list_mutex);
2446
2447         device = btrfs_alloc_device(fs_info, NULL, NULL);
2448         if (IS_ERR(device)) {
2449                 /* we can safely leave the fs_devices entry around */
2450                 ret = PTR_ERR(device);
2451                 goto error;
2452         }
2453
2454         name = rcu_string_strdup(device_path, GFP_KERNEL);
2455         if (!name) {
2456                 ret = -ENOMEM;
2457                 goto error_free_device;
2458         }
2459         rcu_assign_pointer(device->name, name);
2460
2461         trans = btrfs_start_transaction(root, 0);
2462         if (IS_ERR(trans)) {
2463                 ret = PTR_ERR(trans);
2464                 goto error_free_device;
2465         }
2466
2467         q = bdev_get_queue(bdev);
2468         set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
2469         device->generation = trans->transid;
2470         device->io_width = fs_info->sectorsize;
2471         device->io_align = fs_info->sectorsize;
2472         device->sector_size = fs_info->sectorsize;
2473         device->total_bytes = round_down(i_size_read(bdev->bd_inode),
2474                                          fs_info->sectorsize);
2475         device->disk_total_bytes = device->total_bytes;
2476         device->commit_total_bytes = device->total_bytes;
2477         device->fs_info = fs_info;
2478         device->bdev = bdev;
2479         set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
2480         clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
2481         device->mode = FMODE_EXCL;
2482         device->dev_stats_valid = 1;
2483         set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE);
2484
2485         if (seeding_dev) {
2486                 sb->s_flags &= ~SB_RDONLY;
2487                 ret = btrfs_prepare_sprout(fs_info);
2488                 if (ret) {
2489                         btrfs_abort_transaction(trans, ret);
2490                         goto error_trans;
2491                 }
2492         }
2493
2494         device->fs_devices = fs_info->fs_devices;
2495
2496         mutex_lock(&fs_info->fs_devices->device_list_mutex);
2497         mutex_lock(&fs_info->chunk_mutex);
2498         list_add_rcu(&device->dev_list, &fs_info->fs_devices->devices);
2499         list_add(&device->dev_alloc_list,
2500                  &fs_info->fs_devices->alloc_list);
2501         fs_info->fs_devices->num_devices++;
2502         fs_info->fs_devices->open_devices++;
2503         fs_info->fs_devices->rw_devices++;
2504         fs_info->fs_devices->total_devices++;
2505         fs_info->fs_devices->total_rw_bytes += device->total_bytes;
2506
2507         atomic64_add(device->total_bytes, &fs_info->free_chunk_space);
2508
2509         if (!blk_queue_nonrot(q))
2510                 fs_info->fs_devices->rotating = 1;
2511
2512         tmp = btrfs_super_total_bytes(fs_info->super_copy);
2513         btrfs_set_super_total_bytes(fs_info->super_copy,
2514                 round_down(tmp + device->total_bytes, fs_info->sectorsize));
2515
2516         tmp = btrfs_super_num_devices(fs_info->super_copy);
2517         btrfs_set_super_num_devices(fs_info->super_copy, tmp + 1);
2518
2519         /* add sysfs device entry */
2520         btrfs_sysfs_add_device_link(fs_info->fs_devices, device);
2521
2522         /*
2523          * we've got more storage, clear any full flags on the space
2524          * infos
2525          */
2526         btrfs_clear_space_info_full(fs_info);
2527
2528         mutex_unlock(&fs_info->chunk_mutex);
2529         mutex_unlock(&fs_info->fs_devices->device_list_mutex);
2530
2531         if (seeding_dev) {
2532                 mutex_lock(&fs_info->chunk_mutex);
2533                 ret = init_first_rw_device(trans, fs_info);
2534                 mutex_unlock(&fs_info->chunk_mutex);
2535                 if (ret) {
2536                         btrfs_abort_transaction(trans, ret);
2537                         goto error_sysfs;
2538                 }
2539         }
2540
2541         ret = btrfs_add_dev_item(trans, fs_info, device);
2542         if (ret) {
2543                 btrfs_abort_transaction(trans, ret);
2544                 goto error_sysfs;
2545         }
2546
2547         if (seeding_dev) {
2548                 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
2549
2550                 ret = btrfs_finish_sprout(trans, fs_info);
2551                 if (ret) {
2552                         btrfs_abort_transaction(trans, ret);
2553                         goto error_sysfs;
2554                 }
2555
2556                 /* Sprouting would change fsid of the mounted root,
2557                  * so rename the fsid on the sysfs
2558                  */
2559                 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU",
2560                                                 fs_info->fsid);
2561                 if (kobject_rename(&fs_info->fs_devices->fsid_kobj, fsid_buf))
2562                         btrfs_warn(fs_info,
2563                                    "sysfs: failed to create fsid for sprout");
2564         }
2565
2566         ret = btrfs_commit_transaction(trans);
2567
2568         if (seeding_dev) {
2569                 mutex_unlock(&uuid_mutex);
2570                 up_write(&sb->s_umount);
2571                 unlocked = true;
2572
2573                 if (ret) /* transaction commit */
2574                         return ret;
2575
2576                 ret = btrfs_relocate_sys_chunks(fs_info);
2577                 if (ret < 0)
2578                         btrfs_handle_fs_error(fs_info, ret,
2579                                     "Failed to relocate sys chunks after device initialization. This can be fixed using the \"btrfs balance\" command.");
2580                 trans = btrfs_attach_transaction(root);
2581                 if (IS_ERR(trans)) {
2582                         if (PTR_ERR(trans) == -ENOENT)
2583                                 return 0;
2584                         ret = PTR_ERR(trans);
2585                         trans = NULL;
2586                         goto error_sysfs;
2587                 }
2588                 ret = btrfs_commit_transaction(trans);
2589         }
2590
2591         /* Update ctime/mtime for libblkid */
2592         update_dev_time(device_path);
2593         return ret;
2594
2595 error_sysfs:
2596         btrfs_sysfs_rm_device_link(fs_info->fs_devices, device);
2597 error_trans:
2598         if (seeding_dev)
2599                 sb->s_flags |= SB_RDONLY;
2600         if (trans)
2601                 btrfs_end_transaction(trans);
2602 error_free_device:
2603         btrfs_free_device(device);
2604 error:
2605         blkdev_put(bdev, FMODE_EXCL);
2606         if (seeding_dev && !unlocked) {
2607                 mutex_unlock(&uuid_mutex);
2608                 up_write(&sb->s_umount);
2609         }
2610         return ret;
2611 }
2612
2613 static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2614                                         struct btrfs_device *device)
2615 {
2616         int ret;
2617         struct btrfs_path *path;
2618         struct btrfs_root *root = device->fs_info->chunk_root;
2619         struct btrfs_dev_item *dev_item;
2620         struct extent_buffer *leaf;
2621         struct btrfs_key key;
2622
2623         path = btrfs_alloc_path();
2624         if (!path)
2625                 return -ENOMEM;
2626
2627         key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2628         key.type = BTRFS_DEV_ITEM_KEY;
2629         key.offset = device->devid;
2630
2631         ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2632         if (ret < 0)
2633                 goto out;
2634
2635         if (ret > 0) {
2636                 ret = -ENOENT;
2637                 goto out;
2638         }
2639
2640         leaf = path->nodes[0];
2641         dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2642
2643         btrfs_set_device_id(leaf, dev_item, device->devid);
2644         btrfs_set_device_type(leaf, dev_item, device->type);
2645         btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2646         btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2647         btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
2648         btrfs_set_device_total_bytes(leaf, dev_item,
2649                                      btrfs_device_get_disk_total_bytes(device));
2650         btrfs_set_device_bytes_used(leaf, dev_item,
2651                                     btrfs_device_get_bytes_used(device));
2652         btrfs_mark_buffer_dirty(leaf);
2653
2654 out:
2655         btrfs_free_path(path);
2656         return ret;
2657 }
2658
2659 int btrfs_grow_device(struct btrfs_trans_handle *trans,
2660                       struct btrfs_device *device, u64 new_size)
2661 {
2662         struct btrfs_fs_info *fs_info = device->fs_info;
2663         struct btrfs_super_block *super_copy = fs_info->super_copy;
2664         struct btrfs_fs_devices *fs_devices;
2665         u64 old_total;
2666         u64 diff;
2667
2668         if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
2669                 return -EACCES;
2670
2671         new_size = round_down(new_size, fs_info->sectorsize);
2672
2673         mutex_lock(&fs_info->chunk_mutex);
2674         old_total = btrfs_super_total_bytes(super_copy);
2675         diff = round_down(new_size - device->total_bytes, fs_info->sectorsize);
2676
2677         if (new_size <= device->total_bytes ||
2678             test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
2679                 mutex_unlock(&fs_info->chunk_mutex);
2680                 return -EINVAL;
2681         }
2682
2683         fs_devices = fs_info->fs_devices;
2684
2685         btrfs_set_super_total_bytes(super_copy,
2686                         round_down(old_total + diff, fs_info->sectorsize));
2687         device->fs_devices->total_rw_bytes += diff;
2688
2689         btrfs_device_set_total_bytes(device, new_size);
2690         btrfs_device_set_disk_total_bytes(device, new_size);
2691         btrfs_clear_space_info_full(device->fs_info);
2692         if (list_empty(&device->resized_list))
2693                 list_add_tail(&device->resized_list,
2694                               &fs_devices->resized_devices);
2695         mutex_unlock(&fs_info->chunk_mutex);
2696
2697         return btrfs_update_device(trans, device);
2698 }
2699
2700 static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
2701                             struct btrfs_fs_info *fs_info, u64 chunk_offset)
2702 {
2703         struct btrfs_root *root = fs_info->chunk_root;
2704         int ret;
2705         struct btrfs_path *path;
2706         struct btrfs_key key;
2707
2708         path = btrfs_alloc_path();
2709         if (!path)
2710                 return -ENOMEM;
2711
2712         key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2713         key.offset = chunk_offset;
2714         key.type = BTRFS_CHUNK_ITEM_KEY;
2715
2716         ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2717         if (ret < 0)
2718                 goto out;
2719         else if (ret > 0) { /* Logic error or corruption */
2720                 btrfs_handle_fs_error(fs_info, -ENOENT,
2721                                       "Failed lookup while freeing chunk.");
2722                 ret = -ENOENT;
2723                 goto out;
2724         }
2725
2726         ret = btrfs_del_item(trans, root, path);
2727         if (ret < 0)
2728                 btrfs_handle_fs_error(fs_info, ret,
2729                                       "Failed to delete chunk item.");
2730 out:
2731         btrfs_free_path(path);
2732         return ret;
2733 }
2734
2735 static int btrfs_del_sys_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
2736 {
2737         struct btrfs_super_block *super_copy = fs_info->super_copy;
2738         struct btrfs_disk_key *disk_key;
2739         struct btrfs_chunk *chunk;
2740         u8 *ptr;
2741         int ret = 0;
2742         u32 num_stripes;
2743         u32 array_size;
2744         u32 len = 0;
2745         u32 cur;
2746         struct btrfs_key key;
2747
2748         mutex_lock(&fs_info->chunk_mutex);
2749         array_size = btrfs_super_sys_array_size(super_copy);
2750
2751         ptr = super_copy->sys_chunk_array;
2752         cur = 0;
2753
2754         while (cur < array_size) {
2755                 disk_key = (struct btrfs_disk_key *)ptr;
2756                 btrfs_disk_key_to_cpu(&key, disk_key);
2757
2758                 len = sizeof(*disk_key);
2759
2760                 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2761                         chunk = (struct btrfs_chunk *)(ptr + len);
2762                         num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2763                         len += btrfs_chunk_item_size(num_stripes);
2764                 } else {
2765                         ret = -EIO;
2766                         break;
2767                 }
2768                 if (key.objectid == BTRFS_FIRST_CHUNK_TREE_OBJECTID &&
2769                     key.offset == chunk_offset) {
2770                         memmove(ptr, ptr + len, array_size - (cur + len));
2771                         array_size -= len;
2772                         btrfs_set_super_sys_array_size(super_copy, array_size);
2773                 } else {
2774                         ptr += len;
2775                         cur += len;
2776                 }
2777         }
2778         mutex_unlock(&fs_info->chunk_mutex);
2779         return ret;
2780 }
2781
2782 static struct extent_map *get_chunk_map(struct btrfs_fs_info *fs_info,
2783                                         u64 logical, u64 length)
2784 {
2785         struct extent_map_tree *em_tree;
2786         struct extent_map *em;
2787
2788         em_tree = &fs_info->mapping_tree.map_tree;
2789         read_lock(&em_tree->lock);
2790         em = lookup_extent_mapping(em_tree, logical, length);
2791         read_unlock(&em_tree->lock);
2792
2793         if (!em) {
2794                 btrfs_crit(fs_info, "unable to find logical %llu length %llu",
2795                            logical, length);
2796                 return ERR_PTR(-EINVAL);
2797         }
2798
2799         if (em->start > logical || em->start + em->len < logical) {
2800                 btrfs_crit(fs_info,
2801                            "found a bad mapping, wanted %llu-%llu, found %llu-%llu",
2802                            logical, length, em->start, em->start + em->len);
2803                 free_extent_map(em);
2804                 return ERR_PTR(-EINVAL);
2805         }
2806
2807         /* callers are responsible for dropping em's ref. */
2808         return em;
2809 }
2810
2811 int btrfs_remove_chunk(struct btrfs_trans_handle *trans,
2812                        struct btrfs_fs_info *fs_info, u64 chunk_offset)
2813 {
2814         struct extent_map *em;
2815         struct map_lookup *map;
2816         u64 dev_extent_len = 0;
2817         int i, ret = 0;
2818         struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
2819
2820         em = get_chunk_map(fs_info, chunk_offset, 1);
2821         if (IS_ERR(em)) {
2822                 /*
2823                  * This is a logic error, but we don't want to just rely on the
2824                  * user having built with ASSERT enabled, so if ASSERT doesn't
2825                  * do anything we still error out.
2826                  */
2827                 ASSERT(0);
2828                 return PTR_ERR(em);
2829         }
2830         map = em->map_lookup;
2831         mutex_lock(&fs_info->chunk_mutex);
2832         check_system_chunk(trans, map->type);
2833         mutex_unlock(&fs_info->chunk_mutex);
2834
2835         /*
2836          * Take the device list mutex to prevent races with the final phase of
2837          * a device replace operation that replaces the device object associated
2838          * with map stripes (dev-replace.c:btrfs_dev_replace_finishing()).
2839          */
2840         mutex_lock(&fs_devices->device_list_mutex);
2841         for (i = 0; i < map->num_stripes; i++) {
2842                 struct btrfs_device *device = map->stripes[i].dev;
2843                 ret = btrfs_free_dev_extent(trans, device,
2844                                             map->stripes[i].physical,
2845                                             &dev_extent_len);
2846                 if (ret) {
2847                         mutex_unlock(&fs_devices->device_list_mutex);
2848                         btrfs_abort_transaction(trans, ret);
2849                         goto out;
2850                 }
2851
2852                 if (device->bytes_used > 0) {
2853                         mutex_lock(&fs_info->chunk_mutex);
2854                         btrfs_device_set_bytes_used(device,
2855                                         device->bytes_used - dev_extent_len);
2856                         atomic64_add(dev_extent_len, &fs_info->free_chunk_space);
2857                         btrfs_clear_space_info_full(fs_info);
2858                         mutex_unlock(&fs_info->chunk_mutex);
2859                 }
2860
2861                 if (map->stripes[i].dev) {
2862                         ret = btrfs_update_device(trans, map->stripes[i].dev);
2863                         if (ret) {
2864                                 mutex_unlock(&fs_devices->device_list_mutex);
2865                                 btrfs_abort_transaction(trans, ret);
2866                                 goto out;
2867                         }
2868                 }
2869         }
2870         mutex_unlock(&fs_devices->device_list_mutex);
2871
2872         ret = btrfs_free_chunk(trans, fs_info, chunk_offset);
2873         if (ret) {
2874                 btrfs_abort_transaction(trans, ret);
2875                 goto out;
2876         }
2877
2878         trace_btrfs_chunk_free(fs_info, map, chunk_offset, em->len);
2879
2880         if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2881                 ret = btrfs_del_sys_chunk(fs_info, chunk_offset);
2882                 if (ret) {
2883                         btrfs_abort_transaction(trans, ret);
2884                         goto out;
2885                 }
2886         }
2887
2888         ret = btrfs_remove_block_group(trans, chunk_offset, em);
2889         if (ret) {
2890                 btrfs_abort_transaction(trans, ret);
2891                 goto out;
2892         }
2893
2894 out:
2895         /* once for us */
2896         free_extent_map(em);
2897         return ret;
2898 }
2899
2900 static int btrfs_relocate_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
2901 {
2902         struct btrfs_root *root = fs_info->chunk_root;
2903         struct btrfs_trans_handle *trans;
2904         int ret;
2905
2906         /*
2907          * Prevent races with automatic removal of unused block groups.
2908          * After we relocate and before we remove the chunk with offset
2909          * chunk_offset, automatic removal of the block group can kick in,
2910          * resulting in a failure when calling btrfs_remove_chunk() below.
2911          *
2912          * Make sure to acquire this mutex before doing a tree search (dev
2913          * or chunk trees) to find chunks. Otherwise the cleaner kthread might
2914          * call btrfs_remove_chunk() (through btrfs_delete_unused_bgs()) after
2915          * we release the path used to search the chunk/dev tree and before
2916          * the current task acquires this mutex and calls us.
2917          */
2918         lockdep_assert_held(&fs_info->delete_unused_bgs_mutex);
2919
2920         ret = btrfs_can_relocate(fs_info, chunk_offset);
2921         if (ret)
2922                 return -ENOSPC;
2923
2924         /* step one, relocate all the extents inside this chunk */
2925         btrfs_scrub_pause(fs_info);
2926         ret = btrfs_relocate_block_group(fs_info, chunk_offset);
2927         btrfs_scrub_continue(fs_info);
2928         if (ret)
2929                 return ret;
2930
2931         /*
2932          * We add the kobjects here (and after forcing data chunk creation)
2933          * since relocation is the only place we'll create chunks of a new
2934          * type at runtime.  The only place where we'll remove the last
2935          * chunk of a type is the call immediately below this one.  Even
2936          * so, we're protected against races with the cleaner thread since
2937          * we're covered by the delete_unused_bgs_mutex.
2938          */
2939         btrfs_add_raid_kobjects(fs_info);
2940
2941         trans = btrfs_start_trans_remove_block_group(root->fs_info,
2942                                                      chunk_offset);
2943         if (IS_ERR(trans)) {
2944                 ret = PTR_ERR(trans);
2945                 btrfs_handle_fs_error(root->fs_info, ret, NULL);
2946                 return ret;
2947         }
2948
2949         /*
2950          * step two, delete the device extents and the
2951          * chunk tree entries
2952          */
2953         ret = btrfs_remove_chunk(trans, fs_info, chunk_offset);
2954         btrfs_end_transaction(trans);
2955         return ret;
2956 }
2957
2958 static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info)
2959 {
2960         struct btrfs_root *chunk_root = fs_info->chunk_root;
2961         struct btrfs_path *path;
2962         struct extent_buffer *leaf;
2963         struct btrfs_chunk *chunk;
2964         struct btrfs_key key;
2965         struct btrfs_key found_key;
2966         u64 chunk_type;
2967         bool retried = false;
2968         int failed = 0;
2969         int ret;
2970
2971         path = btrfs_alloc_path();
2972         if (!path)
2973                 return -ENOMEM;
2974
2975 again:
2976         key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2977         key.offset = (u64)-1;
2978         key.type = BTRFS_CHUNK_ITEM_KEY;
2979
2980         while (1) {
2981                 mutex_lock(&fs_info->delete_unused_bgs_mutex);
2982                 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2983                 if (ret < 0) {
2984                         mutex_unlock(&fs_info->delete_unused_bgs_mutex);
2985                         goto error;
2986                 }
2987                 BUG_ON(ret == 0); /* Corruption */
2988
2989                 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2990                                           key.type);
2991                 if (ret)
2992                         mutex_unlock(&fs_info->delete_unused_bgs_mutex);
2993                 if (ret < 0)
2994                         goto error;
2995                 if (ret > 0)
2996                         break;
2997
2998                 leaf = path->nodes[0];
2999                 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3000
3001                 chunk = btrfs_item_ptr(leaf, path->slots[0],
3002                                        struct btrfs_chunk);
3003                 chunk_type = btrfs_chunk_type(leaf, chunk);
3004                 btrfs_release_path(path);
3005
3006                 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
3007                         ret = btrfs_relocate_chunk(fs_info, found_key.offset);
3008                         if (ret == -ENOSPC)
3009                                 failed++;
3010                         else
3011                                 BUG_ON(ret);
3012                 }
3013                 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3014
3015                 if (found_key.offset == 0)
3016                         break;
3017                 key.offset = found_key.offset - 1;
3018         }
3019         ret = 0;
3020         if (failed && !retried) {
3021                 failed = 0;
3022                 retried = true;
3023                 goto again;
3024         } else if (WARN_ON(failed && retried)) {
3025                 ret = -ENOSPC;
3026         }
3027 error:
3028         btrfs_free_path(path);
3029         return ret;
3030 }
3031
3032 /*
3033  * return 1 : allocate a data chunk successfully,
3034  * return <0: errors during allocating a data chunk,
3035  * return 0 : no need to allocate a data chunk.
3036  */
3037 static int btrfs_may_alloc_data_chunk(struct btrfs_fs_info *fs_info,
3038                                       u64 chunk_offset)
3039 {
3040         struct btrfs_block_group_cache *cache;
3041         u64 bytes_used;
3042         u64 chunk_type;
3043
3044         cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3045         ASSERT(cache);
3046         chunk_type = cache->flags;
3047         btrfs_put_block_group(cache);
3048
3049         if (chunk_type & BTRFS_BLOCK_GROUP_DATA) {
3050                 spin_lock(&fs_info->data_sinfo->lock);
3051                 bytes_used = fs_info->data_sinfo->bytes_used;
3052                 spin_unlock(&fs_info->data_sinfo->lock);
3053
3054                 if (!bytes_used) {
3055                         struct btrfs_trans_handle *trans;
3056                         int ret;
3057
3058                         trans = btrfs_join_transaction(fs_info->tree_root);
3059                         if (IS_ERR(trans))
3060                                 return PTR_ERR(trans);
3061
3062                         ret = btrfs_force_chunk_alloc(trans,
3063                                                       BTRFS_BLOCK_GROUP_DATA);
3064                         btrfs_end_transaction(trans);
3065                         if (ret < 0)
3066                                 return ret;
3067
3068                         btrfs_add_raid_kobjects(fs_info);
3069
3070                         return 1;
3071                 }
3072         }
3073         return 0;
3074 }
3075
3076 static int insert_balance_item(struct btrfs_fs_info *fs_info,
3077                                struct btrfs_balance_control *bctl)
3078 {
3079         struct btrfs_root *root = fs_info->tree_root;
3080         struct btrfs_trans_handle *trans;
3081         struct btrfs_balance_item *item;
3082         struct btrfs_disk_balance_args disk_bargs;
3083         struct btrfs_path *path;
3084         struct extent_buffer *leaf;
3085         struct btrfs_key key;
3086         int ret, err;
3087
3088         path = btrfs_alloc_path();
3089         if (!path)
3090                 return -ENOMEM;
3091
3092         trans = btrfs_start_transaction(root, 0);
3093         if (IS_ERR(trans)) {
3094                 btrfs_free_path(path);
3095                 return PTR_ERR(trans);
3096         }
3097
3098         key.objectid = BTRFS_BALANCE_OBJECTID;
3099         key.type = BTRFS_TEMPORARY_ITEM_KEY;
3100         key.offset = 0;
3101
3102         ret = btrfs_insert_empty_item(trans, root, path, &key,
3103                                       sizeof(*item));
3104         if (ret)
3105                 goto out;
3106
3107         leaf = path->nodes[0];
3108         item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3109
3110         memzero_extent_buffer(leaf, (unsigned long)item, sizeof(*item));
3111
3112         btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
3113         btrfs_set_balance_data(leaf, item, &disk_bargs);
3114         btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
3115         btrfs_set_balance_meta(leaf, item, &disk_bargs);
3116         btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
3117         btrfs_set_balance_sys(leaf, item, &disk_bargs);
3118
3119         btrfs_set_balance_flags(leaf, item, bctl->flags);
3120
3121         btrfs_mark_buffer_dirty(leaf);
3122 out:
3123         btrfs_free_path(path);
3124         err = btrfs_commit_transaction(trans);
3125         if (err && !ret)
3126                 ret = err;
3127         return ret;
3128 }
3129
3130 static int del_balance_item(struct btrfs_fs_info *fs_info)
3131 {
3132         struct btrfs_root *root = fs_info->tree_root;
3133         struct btrfs_trans_handle *trans;
3134         struct btrfs_path *path;
3135         struct btrfs_key key;
3136         int ret, err;
3137
3138         path = btrfs_alloc_path();
3139         if (!path)
3140                 return -ENOMEM;
3141
3142         trans = btrfs_start_transaction(root, 0);
3143         if (IS_ERR(trans)) {
3144                 btrfs_free_path(path);
3145                 return PTR_ERR(trans);
3146         }
3147
3148         key.objectid = BTRFS_BALANCE_OBJECTID;
3149         key.type = BTRFS_TEMPORARY_ITEM_KEY;
3150         key.offset = 0;
3151
3152         ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3153         if (ret < 0)
3154                 goto out;
3155         if (ret > 0) {
3156                 ret = -ENOENT;
3157                 goto out;
3158         }
3159
3160         ret = btrfs_del_item(trans, root, path);
3161 out:
3162         btrfs_free_path(path);
3163         err = btrfs_commit_transaction(trans);
3164         if (err && !ret)
3165                 ret = err;
3166         return ret;
3167 }
3168
3169 /*
3170  * This is a heuristic used to reduce the number of chunks balanced on
3171  * resume after balance was interrupted.
3172  */
3173 static void update_balance_args(struct btrfs_balance_control *bctl)
3174 {
3175         /*
3176          * Turn on soft mode for chunk types that were being converted.
3177          */
3178         if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
3179                 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
3180         if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
3181                 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
3182         if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
3183                 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
3184
3185         /*
3186          * Turn on usage filter if is not already used.  The idea is
3187          * that chunks that we have already balanced should be
3188          * reasonably full.  Don't do it for chunks that are being
3189          * converted - that will keep us from relocating unconverted
3190          * (albeit full) chunks.
3191          */
3192         if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
3193             !(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
3194             !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3195                 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
3196                 bctl->data.usage = 90;
3197         }
3198         if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
3199             !(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
3200             !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3201                 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
3202                 bctl->sys.usage = 90;
3203         }
3204         if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
3205             !(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
3206             !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3207                 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
3208                 bctl->meta.usage = 90;
3209         }
3210 }
3211
3212 /*
3213  * Clear the balance status in fs_info and delete the balance item from disk.
3214  */
3215 static void reset_balance_state(struct btrfs_fs_info *fs_info)
3216 {
3217         struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3218         int ret;
3219
3220         BUG_ON(!fs_info->balance_ctl);
3221
3222         spin_lock(&fs_info->balance_lock);
3223         fs_info->balance_ctl = NULL;
3224         spin_unlock(&fs_info->balance_lock);
3225
3226         kfree(bctl);
3227         ret = del_balance_item(fs_info);
3228         if (ret)
3229                 btrfs_handle_fs_error(fs_info, ret, NULL);
3230 }
3231
3232 /*
3233  * Balance filters.  Return 1 if chunk should be filtered out
3234  * (should not be balanced).
3235  */
3236 static int chunk_profiles_filter(u64 chunk_type,
3237                                  struct btrfs_balance_args *bargs)
3238 {
3239         chunk_type = chunk_to_extended(chunk_type) &
3240                                 BTRFS_EXTENDED_PROFILE_MASK;
3241
3242         if (bargs->profiles & chunk_type)
3243                 return 0;
3244
3245         return 1;
3246 }
3247
3248 static int chunk_usage_range_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
3249                               struct btrfs_balance_args *bargs)
3250 {
3251         struct btrfs_block_group_cache *cache;
3252         u64 chunk_used;
3253         u64 user_thresh_min;
3254         u64 user_thresh_max;
3255         int ret = 1;
3256
3257         cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3258         chunk_used = btrfs_block_group_used(&cache->item);
3259
3260         if (bargs->usage_min == 0)
3261                 user_thresh_min = 0;
3262         else
3263                 user_thresh_min = div_factor_fine(cache->key.offset,
3264                                         bargs->usage_min);
3265
3266         if (bargs->usage_max == 0)
3267                 user_thresh_max = 1;
3268         else if (bargs->usage_max > 100)
3269                 user_thresh_max = cache->key.offset;
3270         else
3271                 user_thresh_max = div_factor_fine(cache->key.offset,
3272                                         bargs->usage_max);
3273
3274         if (user_thresh_min <= chunk_used && chunk_used < user_thresh_max)
3275                 ret = 0;
3276
3277         btrfs_put_block_group(cache);
3278         return ret;
3279 }
3280
3281 static int chunk_usage_filter(struct btrfs_fs_info *fs_info,
3282                 u64 chunk_offset, struct btrfs_balance_args *bargs)
3283 {
3284         struct btrfs_block_group_cache *cache;
3285         u64 chunk_used, user_thresh;
3286         int ret = 1;
3287
3288         cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3289         chunk_used = btrfs_block_group_used(&cache->item);
3290
3291         if (bargs->usage_min == 0)
3292                 user_thresh = 1;
3293         else if (bargs->usage > 100)
3294                 user_thresh = cache->key.offset;
3295         else
3296                 user_thresh = div_factor_fine(cache->key.offset,
3297                                               bargs->usage);
3298
3299         if (chunk_used < user_thresh)
3300                 ret = 0;
3301
3302         btrfs_put_block_group(cache);
3303         return ret;
3304 }
3305
3306 static int chunk_devid_filter(struct extent_buffer *leaf,
3307                               struct btrfs_chunk *chunk,
3308                               struct btrfs_balance_args *bargs)
3309 {
3310         struct btrfs_stripe *stripe;
3311         int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3312         int i;
3313
3314         for (i = 0; i < num_stripes; i++) {
3315                 stripe = btrfs_stripe_nr(chunk, i);
3316                 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
3317                         return 0;
3318         }
3319
3320         return 1;
3321 }
3322
3323 /* [pstart, pend) */
3324 static int chunk_drange_filter(struct extent_buffer *leaf,
3325                                struct btrfs_chunk *chunk,
3326                                struct btrfs_balance_args *bargs)
3327 {
3328         struct btrfs_stripe *stripe;
3329         int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3330         u64 stripe_offset;
3331         u64 stripe_length;
3332         int factor;
3333         int i;
3334
3335         if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
3336                 return 0;
3337
3338         if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
3339              BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) {
3340                 factor = num_stripes / 2;
3341         } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) {
3342                 factor = num_stripes - 1;
3343         } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) {
3344                 factor = num_stripes - 2;
3345         } else {
3346                 factor = num_stripes;
3347         }
3348
3349         for (i = 0; i < num_stripes; i++) {
3350                 stripe = btrfs_stripe_nr(chunk, i);
3351                 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
3352                         continue;
3353
3354                 stripe_offset = btrfs_stripe_offset(leaf, stripe);
3355                 stripe_length = btrfs_chunk_length(leaf, chunk);
3356                 stripe_length = div_u64(stripe_length, factor);
3357
3358                 if (stripe_offset < bargs->pend &&
3359                     stripe_offset + stripe_length > bargs->pstart)
3360                         return 0;
3361         }
3362
3363         return 1;
3364 }
3365
3366 /* [vstart, vend) */
3367 static int chunk_vrange_filter(struct extent_buffer *leaf,
3368                                struct btrfs_chunk *chunk,
3369                                u64 chunk_offset,
3370                                struct btrfs_balance_args *bargs)
3371 {
3372         if (chunk_offset < bargs->vend &&
3373             chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
3374                 /* at least part of the chunk is inside this vrange */
3375                 return 0;
3376
3377         return 1;
3378 }
3379
3380 static int chunk_stripes_range_filter(struct extent_buffer *leaf,
3381                                struct btrfs_chunk *chunk,
3382                                struct btrfs_balance_args *bargs)
3383 {
3384         int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3385
3386         if (bargs->stripes_min <= num_stripes
3387                         && num_stripes <= bargs->stripes_max)
3388                 return 0;
3389
3390         return 1;
3391 }
3392
3393 static int chunk_soft_convert_filter(u64 chunk_type,
3394                                      struct btrfs_balance_args *bargs)
3395 {
3396         if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
3397                 return 0;
3398
3399         chunk_type = chunk_to_extended(chunk_type) &
3400                                 BTRFS_EXTENDED_PROFILE_MASK;
3401
3402         if (bargs->target == chunk_type)
3403                 return 1;
3404
3405         return 0;
3406 }
3407
3408 static int should_balance_chunk(struct btrfs_fs_info *fs_info,
3409                                 struct extent_buffer *leaf,
3410                                 struct btrfs_chunk *chunk, u64 chunk_offset)
3411 {
3412         struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3413         struct btrfs_balance_args *bargs = NULL;
3414         u64 chunk_type = btrfs_chunk_type(leaf, chunk);
3415
3416         /* type filter */
3417         if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
3418               (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
3419                 return 0;
3420         }
3421
3422         if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3423                 bargs = &bctl->data;
3424         else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3425                 bargs = &bctl->sys;
3426         else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3427                 bargs = &bctl->meta;
3428
3429         /* profiles filter */
3430         if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
3431             chunk_profiles_filter(chunk_type, bargs)) {
3432                 return 0;
3433         }
3434
3435         /* usage filter */
3436         if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
3437             chunk_usage_filter(fs_info, chunk_offset, bargs)) {
3438                 return 0;
3439         } else if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
3440             chunk_usage_range_filter(fs_info, chunk_offset, bargs)) {
3441                 return 0;
3442         }
3443
3444         /* devid filter */
3445         if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
3446             chunk_devid_filter(leaf, chunk, bargs)) {
3447                 return 0;
3448         }
3449
3450         /* drange filter, makes sense only with devid filter */
3451         if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
3452             chunk_drange_filter(leaf, chunk, bargs)) {
3453                 return 0;
3454         }
3455
3456         /* vrange filter */
3457         if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
3458             chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
3459                 return 0;
3460         }
3461
3462         /* stripes filter */
3463         if ((bargs->flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE) &&
3464             chunk_stripes_range_filter(leaf, chunk, bargs)) {
3465                 return 0;
3466         }
3467
3468         /* soft profile changing mode */
3469         if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
3470             chunk_soft_convert_filter(chunk_type, bargs)) {
3471                 return 0;
3472         }
3473
3474         /*
3475          * limited by count, must be the last filter
3476          */
3477         if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) {
3478                 if (bargs->limit == 0)
3479                         return 0;
3480                 else
3481                         bargs->limit--;
3482         } else if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)) {
3483                 /*
3484                  * Same logic as the 'limit' filter; the minimum cannot be
3485                  * determined here because we do not have the global information
3486                  * about the count of all chunks that satisfy the filters.
3487                  */
3488                 if (bargs->limit_max == 0)
3489                         return 0;
3490                 else
3491                         bargs->limit_max--;
3492         }
3493
3494         return 1;
3495 }
3496
3497 static int __btrfs_balance(struct btrfs_fs_info *fs_info)
3498 {
3499         struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3500         struct btrfs_root *chunk_root = fs_info->chunk_root;
3501         struct btrfs_root *dev_root = fs_info->dev_root;
3502         struct list_head *devices;
3503         struct btrfs_device *device;
3504         u64 old_size;
3505         u64 size_to_free;
3506         u64 chunk_type;
3507         struct btrfs_chunk *chunk;
3508         struct btrfs_path *path = NULL;
3509         struct btrfs_key key;
3510         struct btrfs_key found_key;
3511         struct btrfs_trans_handle *trans;
3512         struct extent_buffer *leaf;
3513         int slot;
3514         int ret;
3515         int enospc_errors = 0;
3516         bool counting = true;
3517         /* The single value limit and min/max limits use the same bytes in the */
3518         u64 limit_data = bctl->data.limit;
3519         u64 limit_meta = bctl->meta.limit;
3520         u64 limit_sys = bctl->sys.limit;
3521         u32 count_data = 0;
3522         u32 count_meta = 0;
3523         u32 count_sys = 0;
3524         int chunk_reserved = 0;
3525
3526         /* step one make some room on all the devices */
3527         devices = &fs_info->fs_devices->devices;
3528         list_for_each_entry(device, devices, dev_list) {
3529                 old_size = btrfs_device_get_total_bytes(device);
3530                 size_to_free = div_factor(old_size, 1);
3531                 size_to_free = min_t(u64, size_to_free, SZ_1M);
3532                 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) ||
3533                     btrfs_device_get_total_bytes(device) -
3534                     btrfs_device_get_bytes_used(device) > size_to_free ||
3535                     test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
3536                         continue;
3537
3538                 ret = btrfs_shrink_device(device, old_size - size_to_free);
3539                 if (ret == -ENOSPC)
3540                         break;
3541                 if (ret) {
3542                         /* btrfs_shrink_device never returns ret > 0 */
3543                         WARN_ON(ret > 0);
3544                         goto error;
3545                 }
3546
3547                 trans = btrfs_start_transaction(dev_root, 0);
3548                 if (IS_ERR(trans)) {
3549                         ret = PTR_ERR(trans);
3550                         btrfs_info_in_rcu(fs_info,
3551                  "resize: unable to start transaction after shrinking device %s (error %d), old size %llu, new size %llu",
3552                                           rcu_str_deref(device->name), ret,
3553                                           old_size, old_size - size_to_free);
3554                         goto error;
3555                 }
3556
3557                 ret = btrfs_grow_device(trans, device, old_size);
3558                 if (ret) {
3559                         btrfs_end_transaction(trans);
3560                         /* btrfs_grow_device never returns ret > 0 */
3561                         WARN_ON(ret > 0);
3562                         btrfs_info_in_rcu(fs_info,
3563                  "resize: unable to grow device after shrinking device %s (error %d), old size %llu, new size %llu",
3564                                           rcu_str_deref(device->name), ret,
3565                                           old_size, old_size - size_to_free);
3566                         goto error;
3567                 }
3568
3569                 btrfs_end_transaction(trans);
3570         }
3571
3572         /* step two, relocate all the chunks */
3573         path = btrfs_alloc_path();
3574         if (!path) {
3575                 ret = -ENOMEM;
3576                 goto error;
3577         }
3578
3579         /* zero out stat counters */
3580         spin_lock(&fs_info->balance_lock);
3581         memset(&bctl->stat, 0, sizeof(bctl->stat));
3582         spin_unlock(&fs_info->balance_lock);
3583 again:
3584         if (!counting) {
3585                 /*
3586                  * The single value limit and min/max limits use the same bytes
3587                  * in the
3588                  */
3589                 bctl->data.limit = limit_data;
3590                 bctl->meta.limit = limit_meta;
3591                 bctl->sys.limit = limit_sys;
3592         }
3593         key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3594         key.offset = (u64)-1;
3595         key.type = BTRFS_CHUNK_ITEM_KEY;
3596
3597         while (1) {
3598                 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
3599                     atomic_read(&fs_info->balance_cancel_req)) {
3600                         ret = -ECANCELED;
3601                         goto error;
3602                 }
3603
3604                 mutex_lock(&fs_info->delete_unused_bgs_mutex);
3605                 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
3606                 if (ret < 0) {
3607                         mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3608                         goto error;
3609                 }
3610
3611                 /*
3612                  * this shouldn't happen, it means the last relocate
3613                  * failed
3614                  */
3615                 if (ret == 0)
3616                         BUG(); /* FIXME break ? */
3617
3618                 ret = btrfs_previous_item(chunk_root, path, 0,
3619                                           BTRFS_CHUNK_ITEM_KEY);
3620                 if (ret) {
3621                         mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3622                         ret = 0;
3623                         break;
3624                 }
3625
3626                 leaf = path->nodes[0];
3627                 slot = path->slots[0];
3628                 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3629
3630                 if (found_key.objectid != key.objectid) {
3631                         mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3632                         break;
3633                 }
3634
3635                 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3636                 chunk_type = btrfs_chunk_type(leaf, chunk);
3637
3638                 if (!counting) {
3639                         spin_lock(&fs_info->balance_lock);
3640                         bctl->stat.considered++;
3641                         spin_unlock(&fs_info->balance_lock);
3642                 }
3643
3644                 ret = should_balance_chunk(fs_info, leaf, chunk,
3645                                            found_key.offset);
3646
3647                 btrfs_release_path(path);
3648                 if (!ret) {
3649                         mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3650                         goto loop;
3651                 }
3652
3653                 if (counting) {
3654                         mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3655                         spin_lock(&fs_info->balance_lock);
3656                         bctl->stat.expected++;
3657                         spin_unlock(&fs_info->balance_lock);
3658
3659                         if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3660                                 count_data++;
3661                         else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3662                                 count_sys++;
3663                         else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3664                                 count_meta++;
3665
3666                         goto loop;
3667                 }
3668
3669                 /*
3670                  * Apply limit_min filter, no need to check if the LIMITS
3671                  * filter is used, limit_min is 0 by default
3672                  */
3673                 if (((chunk_type & BTRFS_BLOCK_GROUP_DATA) &&
3674                                         count_data < bctl->data.limit_min)
3675                                 || ((chunk_type & BTRFS_BLOCK_GROUP_METADATA) &&
3676                                         count_meta < bctl->meta.limit_min)
3677                                 || ((chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) &&
3678                                         count_sys < bctl->sys.limit_min)) {
3679                         mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3680                         goto loop;
3681                 }
3682
3683                 if (!chunk_reserved) {
3684                         /*
3685                          * We may be relocating the only data chunk we have,
3686                          * which could potentially end up with losing data's
3687                          * raid profile, so lets allocate an empty one in
3688                          * advance.
3689                          */
3690                         ret = btrfs_may_alloc_data_chunk(fs_info,
3691                                                          found_key.offset);
3692                         if (ret < 0) {
3693                                 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3694                                 goto error;
3695                         } else if (ret == 1) {
3696                                 chunk_reserved = 1;
3697                         }
3698                 }
3699
3700                 ret = btrfs_relocate_chunk(fs_info, found_key.offset);
3701                 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3702                 if (ret && ret != -ENOSPC)
3703                         goto error;
3704                 if (ret == -ENOSPC) {
3705                         enospc_errors++;
3706                 } else {
3707                         spin_lock(&fs_info->balance_lock);
3708                         bctl->stat.completed++;
3709                         spin_unlock(&fs_info->balance_lock);
3710                 }
3711 loop:
3712                 if (found_key.offset == 0)
3713                         break;
3714                 key.offset = found_key.offset - 1;
3715         }
3716
3717         if (counting) {
3718                 btrfs_release_path(path);
3719                 counting = false;
3720                 goto again;
3721         }
3722 error:
3723         btrfs_free_path(path);
3724         if (enospc_errors) {
3725                 btrfs_info(fs_info, "%d enospc errors during balance",
3726                            enospc_errors);
3727                 if (!ret)
3728                         ret = -ENOSPC;
3729         }
3730
3731         return ret;
3732 }
3733
3734 /**
3735  * alloc_profile_is_valid - see if a given profile is valid and reduced
3736  * @flags: profile to validate
3737  * @extended: if true @flags is treated as an extended profile
3738  */
3739 static int alloc_prof