btrfs-progs: update man page for btrfs filesystem label
[platform/upstream/btrfs-progs.git] / volumes.c
index 01127e7..10fdc48 100644 (file)
--- a/volumes.c
+++ b/volumes.c
@@ -29,6 +29,7 @@
 #include "transaction.h"
 #include "print-tree.h"
 #include "volumes.h"
+#include "math.h"
 
 struct stripe {
        struct btrfs_device *dev;
@@ -52,9 +53,6 @@ static inline int nr_data_stripes(struct map_lookup *map)
 
 #define is_parity_stripe(x) ( ((x) == BTRFS_RAID5_P_STRIPE) || ((x) == BTRFS_RAID6_Q_STRIPE) )
 
-#define map_lookup_size(n) (sizeof(struct map_lookup) + \
-                           (sizeof(struct btrfs_bio_stripe) * (n)))
-
 static LIST_HEAD(fs_uuids);
 
 static struct btrfs_device *__find_device(struct list_head *head, u64 devid,
@@ -116,6 +114,7 @@ static int device_list_add(const char *path,
                        /* we can safely leave the fs_devices entry around */
                        return -ENOMEM;
                }
+               device->fd = -1;
                device->devid = devid;
                memcpy(device->uuid, disk_super->dev_item.uuid,
                       BTRFS_UUID_SIZE);
@@ -161,8 +160,13 @@ int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
 again:
        list_for_each(cur, &fs_devices->devices) {
                device = list_entry(cur, struct btrfs_device, dev_list);
-               close(device->fd);
-               device->fd = -1;
+               if (device->fd != -1) {
+                       fsync(device->fd);
+                       if (posix_fadvise(device->fd, 0, 0, POSIX_FADV_DONTNEED))
+                               fprintf(stderr, "Warning, could not drop caches\n");
+                       close(device->fd);
+                       device->fd = -1;
+               }
                device->writeable = 0;
        }
 
@@ -627,14 +631,6 @@ int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
        return 0;
 }
 
-static u64 div_factor(u64 num, int factor)
-{
-       if (factor == 10)
-               return num;
-       num *= factor;
-       return num / 10;
-}
-
 static u64 chunk_bytes_by_type(u64 type, u64 calc_size, int num_stripes,
                               int sub_stripes)
 {
@@ -663,12 +659,12 @@ int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
 {
        u64 dev_offset;
        struct btrfs_fs_info *info = extent_root->fs_info;
-       struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
+       struct btrfs_root *chunk_root = info->chunk_root;
        struct btrfs_stripe *stripes;
        struct btrfs_device *device = NULL;
        struct btrfs_chunk *chunk;
        struct list_head private_devs;
-       struct list_head *dev_list = &extent_root->fs_info->fs_devices->devices;
+       struct list_head *dev_list = &info->fs_devices->devices;
        struct list_head *cur;
        struct map_lookup *map;
        int min_stripe_size = 1 * 1024 * 1024;
@@ -821,7 +817,7 @@ again:
        if (!chunk)
                return -ENOMEM;
 
-       map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
+       map = kmalloc(btrfs_map_lookup_size(num_stripes), GFP_NOFS);
        if (!map) {
                kfree(chunk);
                return -ENOMEM;
@@ -888,9 +884,7 @@ again:
        map->ce.start = key.offset;
        map->ce.size = *num_bytes;
 
-       ret = insert_existing_cache_extent(
-                          &extent_root->fs_info->mapping_tree.cache_tree,
-                          &map->ce);
+       ret = insert_cache_extent(&info->mapping_tree.cache_tree, &map->ce);
        BUG_ON(ret);
 
        if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
@@ -909,11 +903,11 @@ int btrfs_alloc_data_chunk(struct btrfs_trans_handle *trans,
 {
        u64 dev_offset;
        struct btrfs_fs_info *info = extent_root->fs_info;
-       struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
+       struct btrfs_root *chunk_root = info->chunk_root;
        struct btrfs_stripe *stripes;
        struct btrfs_device *device = NULL;
        struct btrfs_chunk *chunk;
-       struct list_head *dev_list = &extent_root->fs_info->fs_devices->devices;
+       struct list_head *dev_list = &info->fs_devices->devices;
        struct list_head *cur;
        struct map_lookup *map;
        u64 calc_size = 8 * 1024 * 1024;
@@ -935,7 +929,7 @@ int btrfs_alloc_data_chunk(struct btrfs_trans_handle *trans,
        if (!chunk)
                return -ENOMEM;
 
-       map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
+       map = kmalloc(btrfs_map_lookup_size(num_stripes), GFP_NOFS);
        if (!map) {
                kfree(chunk);
                return -ENOMEM;
@@ -996,9 +990,7 @@ int btrfs_alloc_data_chunk(struct btrfs_trans_handle *trans,
        map->ce.start = key.offset;
        map->ce.size = num_bytes;
 
-       ret = insert_existing_cache_extent(
-                          &extent_root->fs_info->mapping_tree.cache_tree,
-                          &map->ce);
+       ret = insert_cache_extent(&info->mapping_tree.cache_tree, &map->ce);
        BUG_ON(ret);
 
        kfree(chunk);
@@ -1016,7 +1008,7 @@ int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
        struct map_lookup *map;
        int ret;
 
-       ce = find_first_cache_extent(&map_tree->cache_tree, logical);
+       ce = search_cache_extent(&map_tree->cache_tree, logical);
        BUG_ON(!ce);
        BUG_ON(ce->start > logical || ce->start + ce->size < logical);
        map = container_of(ce, struct map_lookup, ce);
@@ -1040,7 +1032,7 @@ int btrfs_next_metadata(struct btrfs_mapping_tree *map_tree, u64 *logical,
        struct cache_extent *ce;
        struct map_lookup *map;
 
-       ce = find_first_cache_extent(&map_tree->cache_tree, *logical);
+       ce = search_cache_extent(&map_tree->cache_tree, *logical);
 
        while (ce) {
                ce = next_cache_extent(ce);
@@ -1071,7 +1063,7 @@ int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
        u64 rmap_len;
        int i, j, nr = 0;
 
-       ce = find_first_cache_extent(&map_tree->cache_tree, chunk_start);
+       ce = search_cache_extent(&map_tree->cache_tree, chunk_start);
        BUG_ON(!ce);
        map = container_of(ce, struct map_lookup, ce);
 
@@ -1183,7 +1175,7 @@ int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
                stripes_allocated = 1;
        }
 again:
-       ce = find_first_cache_extent(&map_tree->cache_tree, logical);
+       ce = search_cache_extent(&map_tree->cache_tree, logical);
        if (!ce) {
                if (multi)
                        kfree(multi);
@@ -1391,16 +1383,15 @@ struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
        return NULL;
 }
 
-struct btrfs_device *btrfs_find_device_by_devid(struct btrfs_root *root,
-                                               u64 devid, int instance)
+struct btrfs_device *
+btrfs_find_device_by_devid(struct btrfs_fs_devices *fs_devices,
+                          u64 devid, int instance)
 {
-       struct list_head *head = &root->fs_info->fs_devices->devices;
+       struct list_head *head = &fs_devices->devices;
        struct btrfs_device *dev;
-       struct list_head *cur;
        int num_found = 0;
 
-       list_for_each(cur, head) {
-               dev = list_entry(cur, struct btrfs_device, dev_list);
+       list_for_each_entry(dev, head, dev_list) {
                if (dev->devid == devid && num_found++ == instance)
                        return dev;
        }
@@ -1422,7 +1413,7 @@ int btrfs_bootstrap_super_map(struct btrfs_mapping_tree *map_tree,
        list_for_each(cur, &fs_devices->devices) {
                num_stripes++;
        }
-       map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
+       map = kmalloc(btrfs_map_lookup_size(num_stripes), GFP_NOFS);
        if (!map)
                return -ENOMEM;
 
@@ -1445,15 +1436,16 @@ int btrfs_bootstrap_super_map(struct btrfs_mapping_tree *map_tree,
                map->stripes[i].dev = device;
                i++;
        }
-       ret = insert_existing_cache_extent(&map_tree->cache_tree, &map->ce);
+       ret = insert_cache_extent(&map_tree->cache_tree, &map->ce);
        if (ret == -EEXIST) {
                struct cache_extent *old;
                struct map_lookup *old_map;
-               old = find_cache_extent(&map_tree->cache_tree, logical, length);
+               old = lookup_cache_extent(&map_tree->cache_tree,
+                                         logical, length);
                old_map = container_of(old, struct map_lookup, ce);
                remove_cache_extent(&map_tree->cache_tree, old);
                kfree(old_map);
-               ret = insert_existing_cache_extent(&map_tree->cache_tree,
+               ret = insert_cache_extent(&map_tree->cache_tree,
                                                   &map->ce);
        }
        BUG_ON(ret);
@@ -1468,7 +1460,7 @@ int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
        int readonly = 0;
        int i;
 
-       ce = find_first_cache_extent(&map_tree->cache_tree, chunk_offset);
+       ce = search_cache_extent(&map_tree->cache_tree, chunk_offset);
        BUG_ON(!ce);
 
        map = container_of(ce, struct map_lookup, ce);
@@ -1510,7 +1502,7 @@ static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
        logical = key->offset;
        length = btrfs_chunk_length(leaf, chunk);
 
-       ce = find_first_cache_extent(&map_tree->cache_tree, logical);
+       ce = search_cache_extent(&map_tree->cache_tree, logical);
 
        /* already mapped? */
        if (ce && ce->start <= logical && ce->start + ce->size > logical) {
@@ -1518,7 +1510,7 @@ static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
        }
 
        num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
-       map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
+       map = kmalloc(btrfs_map_lookup_size(num_stripes), GFP_NOFS);
        if (!map)
                return -ENOMEM;
 
@@ -1548,7 +1540,7 @@ static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
                }
 
        }
-       ret = insert_existing_cache_extent(&map_tree->cache_tree, &map->ce);
+       ret = insert_cache_extent(&map_tree->cache_tree, &map->ce);
        BUG_ON(ret);
 
        return 0;
@@ -1632,10 +1624,10 @@ static int read_one_dev(struct btrfs_root *root,
        if (!device) {
                printk("warning devid %llu not found already\n",
                        (unsigned long long)devid);
-               device = kmalloc(sizeof(*device), GFP_NOFS);
+               device = kzalloc(sizeof(*device), GFP_NOFS);
                if (!device)
                        return -ENOMEM;
-               device->total_ios = 0;
+               device->fd = -1;
                list_add(&device->dev_list,
                         &root->fs_info->fs_devices->devices);
        }